GG
TextControl.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /* GG is a GUI for SDL and OpenGL.
3  Copyright (C) 2003-2008 T. Zachary Laine
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public License
7  as published by the Free Software Foundation; either version 2.1
8  of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  02111-1307 USA
19 
20  If you do not wish to comply with the terms of the LGPL please
21  contact the author as other terms are available for a fee.
22 
23  Zach Laine
24  whatwasthataddress@gmail.com */
25 
29 #ifndef _GG_TextControl_h_
30 #define _GG_TextControl_h_
31 
32 #include <GG/ClrConstants.h>
33 #include <GG/Control.h>
34 #include <GG/Font.h>
35 
36 #include <boost/lexical_cast.hpp>
37 
38 
39 namespace GG {
40 
65 class GG_API TextControl : public Control
66 {
67 public:
68  using Wnd::SetMinSize;
69 
71 
72  TextControl(X x, Y y, X w, Y h, const std::string& str, const boost::shared_ptr<Font>& font,
73  Clr color = CLR_BLACK, Flags<TextFormat> format = FORMAT_NONE,
74  Flags<WndFlag> flags = Flags<WndFlag>());
75 
80  TextControl(X x, Y y, const std::string& str, const boost::shared_ptr<Font>& font,
81  Clr color = CLR_BLACK, Flags<TextFormat> format = FORMAT_NONE,
82  Flags<WndFlag> flags = Flags<WndFlag>());
84 
86  virtual Pt MinUsableSize() const;
87 
89  const std::string& Text() const;
90 
93  Flags<TextFormat> GetTextFormat() const;
94 
97  Clr TextColor() const;
98 
101  bool ClipText() const;
102 
110  bool SetMinSize() const;
111 
125  template <class T> void operator>>(T& t) const;
126 
139  template <class T> T GetValue() const;
140 
143  operator const std::string&() const;
144 
145  bool Empty() const;
146  CPSize Length() const;
147 
150  Pt TextUpperLeft() const;
151 
154  Pt TextLowerRight() const;
156 
158  virtual void Render();
159 
164  virtual void SetText(const std::string& str);
165 
166  virtual void SizeMove(const Pt& ul, const Pt& lr);
167 
169  void SetTextFormat(Flags<TextFormat> format);
170 
172  void SetTextColor(Clr color);
173 
176  virtual void SetColor(Clr c);
177 
179  void ClipText(bool b);
180 
183  void SetMinSize(bool b);
184 
192  template <class T>
193  void operator<<(T t);
194 
195  void operator+=(const std::string& s);
196  void operator+=(char c);
197  void Clear();
198 
202  void Insert(CPSize pos, char c);
203 
205  void Insert(CPSize pos, const std::string& s);
206 
209  void Erase(CPSize pos, CPSize num = CP1);
210 
214  void Insert(std::size_t line, CPSize pos, char c);
215 
217  void Insert(std::size_t line, CPSize pos, const std::string& s);
218 
221  void Erase(std::size_t line, CPSize pos, CPSize num = CP1);
223 
224 protected:
226  TextControl();
227 
228 
230 
231  const std::vector<Font::LineData>& GetLineData() const;
232 
234  const boost::shared_ptr<Font>& GetFont() const;
235 
238  bool FitToText() const;
240 
241 private:
242  void ValidateFormat();
243  void AdjustMinimumSize();
244  void RecomputeTextBounds();
245 
246  std::string m_text;
247  Flags<TextFormat> m_format;
248  Clr m_text_color;
249  bool m_clip_text;
250  bool m_set_min_size;
251  std::vector<boost::shared_ptr<Font::TextElement> >
252  m_text_elements;
253  std::vector<Font::LineData> m_line_data;
254  CPSize m_code_points;
255  boost::shared_ptr<Font> m_font;
256  bool m_fit_to_text;
257  Pt m_text_ul;
258  Pt m_text_lr;
259 };
260 
261 } // namespace GG
262 
263 // template implementations
264 template <class T>
266 {
267  try {
268  t = boost::lexical_cast<T>(m_text);
269  } catch (boost::bad_lexical_cast) {
270  t = T();
271  }
272 }
273 
274 template <class T>
276 { return boost::lexical_cast<T, std::string>(m_text); }
277 
278 template <class T>
280 { SetText(boost::lexical_cast<std::string>(t)); }
281 
282 #endif