TextControl.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003-2008 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@gmail.com */
00025 
00029 #ifndef _GG_TextControl_h_
00030 #define _GG_TextControl_h_
00031 
00032 #include <GG/ClrConstants.h>
00033 #include <GG/Control.h>
00034 #include <GG/Font.h>
00035 
00036 #include <boost/lexical_cast.hpp>
00037 
00038 
00039 namespace GG {
00040 
00065 class GG_API TextControl : public Control
00066 {
00067 public:
00068     using Wnd::SetMinSize;
00069  
00071 
00072     TextControl(X x, Y y, X w, Y h, const std::string& str, const boost::shared_ptr<Font>& font,
00073                 Clr color = CLR_BLACK, Flags<TextFormat> format = FORMAT_NONE,
00074                 Flags<WndFlag> flags = Flags<WndFlag>());
00075 
00080     TextControl(X x, Y y, const std::string& str, const boost::shared_ptr<Font>& font,
00081                 Clr color = CLR_BLACK, Flags<TextFormat> format = FORMAT_NONE,
00082                 Flags<WndFlag> flags = Flags<WndFlag>());
00084  
00086     virtual Pt        MinUsableSize() const;
00087 
00089     const std::string& Text() const;
00090 
00093     Flags<TextFormat> GetTextFormat() const;
00094 
00097     Clr               TextColor() const;
00098 
00101     bool              ClipText() const;
00102 
00110     bool              SetMinSize() const;
00111 
00125     template <class T> void operator>>(T& t) const;
00126 
00139     template <class T> T GetValue() const;
00140 
00143     operator const std::string&() const;
00144 
00145     bool   Empty() const;   
00146     CPSize Length() const;  
00147 
00150     Pt TextUpperLeft() const;
00151 
00154     Pt TextLowerRight() const;
00156  
00158     virtual void Render();
00159 
00164     virtual void SetText(const std::string& str);
00165 
00166     virtual void SizeMove(const Pt& ul, const Pt& lr);
00167 
00169     void         SetTextFormat(Flags<TextFormat> format);
00170 
00172     void         SetTextColor(Clr color);
00173 
00176     virtual void SetColor(Clr c);
00177 
00179     void         ClipText(bool b);
00180 
00183     void         SetMinSize(bool b);
00184 
00192     template <class T>
00193     void operator<<(T t);
00194 
00195     void  operator+=(const std::string& s); 
00196     void  operator+=(char c);               
00197     void  Clear();                          
00198 
00202     void  Insert(CPSize pos, char c);
00203 
00205     void  Insert(CPSize pos, const std::string& s);
00206     
00209     void  Erase(CPSize pos, CPSize num = CP1);
00210 
00214     void  Insert(std::size_t line, CPSize pos, char c);
00215 
00217     void  Insert(std::size_t line, CPSize pos, const std::string& s);
00218 
00221     void  Erase(std::size_t line, CPSize pos, CPSize num = CP1);
00222 
00223     virtual void DefineAttributes(WndEditor* editor);
00225 
00226 protected: 
00228     TextControl(); 
00229 
00230  
00232 
00233     const std::vector<Font::LineData>& GetLineData() const;
00234 
00236     const boost::shared_ptr<Font>& GetFont() const;
00237 
00240     bool FitToText() const;
00241 
00247     bool DirtyLoad() const;
00249 
00250 private:
00251     void ValidateFormat();      
00252     void AdjustMinimumSize();
00253     void RecomputeTextBounds(); 
00254 
00255     std::string                 m_text;
00256     Flags<TextFormat>           m_format;      
00257     Clr                         m_text_color;  
00258     bool                        m_clip_text;
00259     bool                        m_set_min_size;
00260     std::vector<Font::LineData> m_line_data;
00261     CPSize                      m_code_points;
00262     boost::shared_ptr<Font>     m_font;
00263     bool                        m_fit_to_text; 
00264     Pt                          m_text_ul;     
00265     Pt                          m_text_lr;     
00266 
00267     // variables for caching the results of MinUsableSize()
00268     mutable Pt                  m_min_usable_size;
00269     mutable X                   m_previous_client_width;
00270     mutable Flags<TextFormat>   m_previous_format;
00271 
00272     friend class boost::serialization::access;
00273     template <class Archive>
00274     void serialize(Archive& ar, const unsigned int version);
00275 
00276     bool m_dirty_load;
00277 };
00278 
00279 } // namespace GG
00280 
00281 // template implementations
00282 template <class T>
00283 void GG::TextControl::operator>>(T& t) const
00284 {
00285     try {
00286         t = boost::lexical_cast<T>(m_text);
00287     } catch (boost::bad_lexical_cast) {
00288         t = T();
00289     }
00290 }
00291 
00292 template <class T>
00293 T GG::TextControl::GetValue() const
00294 { return boost::lexical_cast<T, std::string>(m_text); }
00295 
00296 template <class T>
00297 void GG::TextControl::operator<<(T t)
00298 { SetText(boost::lexical_cast<std::string>(t)); }
00299 
00300 template <class Archive>
00301 void GG::TextControl::serialize(Archive& ar, const unsigned int version)
00302 {
00303     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00304         & BOOST_SERIALIZATION_NVP(m_text)
00305         & BOOST_SERIALIZATION_NVP(m_format)
00306         & BOOST_SERIALIZATION_NVP(m_text_color)
00307         & BOOST_SERIALIZATION_NVP(m_clip_text)
00308         & BOOST_SERIALIZATION_NVP(m_set_min_size)
00309         & BOOST_SERIALIZATION_NVP(m_line_data)
00310         & BOOST_SERIALIZATION_NVP(m_code_points)
00311         & BOOST_SERIALIZATION_NVP(m_font)
00312         & BOOST_SERIALIZATION_NVP(m_fit_to_text)
00313         & BOOST_SERIALIZATION_NVP(m_text_ul)
00314         & BOOST_SERIALIZATION_NVP(m_text_lr);
00315 
00316     if (Archive::is_loading::value) {
00317         ValidateFormat();
00318         m_dirty_load = true;
00319     }
00320 }
00321 
00322 #endif // _GG_TextControl_h_

Generated on Sat Mar 26 07:08:37 2011 for GG by  doxygen 1.5.9