TextControl.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003 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@hotmail.com */
00025 
00029 #ifndef _GG_TextControl_h_
00030 #define _GG_TextControl_h_
00031 
00032 #include <GG/Control.h>
00033 #include <GG/Font.h>
00034 
00035 #include <boost/lexical_cast.hpp>
00036 
00037 
00038 namespace GG {
00039 
00054 class GG_API TextControl : public Control
00055 {
00056 public:
00057     using Wnd::SetMinSize;
00058  
00060     TextControl(int x, int y, int w, int h, const std::string& str, const boost::shared_ptr<Font>& font, Clr color = CLR_BLACK, Flags<TextFormat> format = FORMAT_NONE, Flags<WndFlag> flags = Flags<WndFlag>()); 
00061 
00065     TextControl(int x, int y, const std::string& str, const boost::shared_ptr<Font>& font, Clr color = CLR_BLACK, Flags<TextFormat> format = FORMAT_NONE, Flags<WndFlag> flags = Flags<WndFlag>());
00067  
00069     virtual Pt        MinUsableSize() const;
00070 
00072     Flags<TextFormat> GetTextFormat() const;
00073 
00075     Clr               TextColor() const;
00076 
00078     bool              ClipText() const;
00079 
00084     bool              SetMinSize() const;
00085 
00094     template <class T> void operator>>(T& t) const;
00095 
00104     template <class T> T GetValue() const;
00105 
00106     operator const std::string&() const; 
00107 
00108     bool  Empty() const;   
00109     int   Length() const;  
00110 
00113     Pt    TextUpperLeft() const;
00114 
00117     Pt    TextLowerRight() const;
00119  
00121     virtual void Render();
00122 
00126     virtual void   SetText(const std::string& str);
00127     virtual void   SetText(const char* str);
00128     virtual void   SizeMove(const Pt& ul, const Pt& lr);
00129     void           SetTextFormat(Flags<TextFormat> format); 
00130     void           SetTextColor(Clr color);      
00131     virtual void   SetColor(Clr c);              
00132     void           ClipText(bool b);             
00133     void           SetMinSize(bool b);           
00134 
00139     template <class T> void operator<<(T t);
00140 
00141     void  operator+=(const std::string& str); 
00142     void  operator+=(const char* str);        
00143     void  operator+=(char ch);                
00144     void  Clear();                            
00145     void  Insert(int pos, char ch);           
00146     void  Erase(int pos, int num = 1);        
00147 
00148     virtual void DefineAttributes(WndEditor* editor);
00150 
00151 protected: 
00153     TextControl(); 
00154 
00155  
00157     const std::vector<Font::LineData>&  GetLineData() const;
00158     const boost::shared_ptr<Font>&      GetFont() const;
00159     bool                                FitToText() const;
00160     bool                                DirtyLoad() const;
00162 
00163 private:
00164     void ValidateFormat();      
00165     void AdjustMinimumSize();
00166     void RecomputeTextBounds(); 
00167 
00168     Flags<TextFormat>           m_format;      
00169     Clr                         m_text_color;  
00170     bool                        m_clip_text;
00171     bool                        m_set_min_size;
00172     std::vector<Font::LineData> m_line_data;
00173     boost::shared_ptr<Font>     m_font;
00174     bool                        m_fit_to_text; 
00175     Pt                          m_text_ul;     
00176     Pt                          m_text_lr;     
00177 
00178     friend class boost::serialization::access;
00179     template <class Archive>
00180     void serialize(Archive& ar, const unsigned int version);
00181     // true iff the object has just been loaded from a serialized form, meaning changes may have been made that leave
00182     // the control in an inconsistent state; this must be remedied the next time Render() is called
00183     bool m_dirty_load;
00184 };
00185 
00186 } // namespace GG
00187 
00188 // template implementations
00189 template <class T>
00190 void GG::TextControl::operator>>(T& t) const
00191 {
00192     try {
00193         t = boost::lexical_cast<T>(Control::m_text);
00194     } catch (boost::bad_lexical_cast) {
00195         t = T();
00196     }
00197 }
00198 
00199 template <class T>
00200 T GG::TextControl::GetValue() const
00201 {
00202     return boost::lexical_cast<T, std::string>(Control::m_text);
00203 }
00204 
00205 template <class T>
00206 void GG::TextControl::operator<<(T t)
00207 {
00208     SetText(boost::lexical_cast<std::string>(t));
00209 }
00210 
00211 template <class Archive>
00212 void GG::TextControl::serialize(Archive& ar, const unsigned int version)
00213 {
00214     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00215         & BOOST_SERIALIZATION_NVP(m_format)
00216         & BOOST_SERIALIZATION_NVP(m_text_color)
00217         & BOOST_SERIALIZATION_NVP(m_clip_text)
00218         & BOOST_SERIALIZATION_NVP(m_set_min_size)
00219         & BOOST_SERIALIZATION_NVP(m_line_data)
00220         & BOOST_SERIALIZATION_NVP(m_font)
00221         & BOOST_SERIALIZATION_NVP(m_fit_to_text)
00222         & BOOST_SERIALIZATION_NVP(m_text_ul)
00223         & BOOST_SERIALIZATION_NVP(m_text_lr);
00224 
00225     if (Archive::is_loading::value) {
00226         ValidateFormat();
00227         m_dirty_load = true;
00228     }
00229 }
00230 
00231 #endif // _GG_TextControl_h_

Generated on Wed Mar 26 14:35:42 2008 for GG by  doxygen 1.5.2