00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00182
00183 bool m_dirty_load;
00184 };
00185
00186 }
00187
00188
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_