GG
|
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<boost::shared_ptr<Font::TextElement> > 00261 m_text_elements; 00262 std::vector<Font::LineData> m_line_data; 00263 CPSize m_code_points; 00264 boost::shared_ptr<Font> m_font; 00265 bool m_fit_to_text; 00266 Pt m_text_ul; 00267 Pt m_text_lr; 00268 00269 friend class boost::serialization::access; 00270 template <class Archive> 00271 void serialize(Archive& ar, const unsigned int version); 00272 00273 bool m_dirty_load; 00274 }; 00275 00276 } // namespace GG 00277 00278 // template implementations 00279 template <class T> 00280 void GG::TextControl::operator>>(T& t) const 00281 { 00282 try { 00283 t = boost::lexical_cast<T>(m_text); 00284 } catch (boost::bad_lexical_cast) { 00285 t = T(); 00286 } 00287 } 00288 00289 template <class T> 00290 T GG::TextControl::GetValue() const 00291 { return boost::lexical_cast<T, std::string>(m_text); } 00292 00293 template <class T> 00294 void GG::TextControl::operator<<(T t) 00295 { SetText(boost::lexical_cast<std::string>(t)); } 00296 00297 template <class Archive> 00298 void GG::TextControl::serialize(Archive& ar, const unsigned int version) 00299 { 00300 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control) 00301 & BOOST_SERIALIZATION_NVP(m_text) 00302 & BOOST_SERIALIZATION_NVP(m_format) 00303 & BOOST_SERIALIZATION_NVP(m_text_color) 00304 & BOOST_SERIALIZATION_NVP(m_clip_text) 00305 & BOOST_SERIALIZATION_NVP(m_set_min_size) 00306 & BOOST_SERIALIZATION_NVP(m_line_data) 00307 & BOOST_SERIALIZATION_NVP(m_code_points) 00308 & BOOST_SERIALIZATION_NVP(m_font) 00309 & BOOST_SERIALIZATION_NVP(m_fit_to_text) 00310 & BOOST_SERIALIZATION_NVP(m_text_ul) 00311 & BOOST_SERIALIZATION_NVP(m_text_lr); 00312 00313 if (Archive::is_loading::value) { 00314 ValidateFormat(); 00315 m_dirty_load = true; 00316 } 00317 } 00318 00319 #endif // _GG_TextControl_h_