GG
GUI.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_GUI_h_
30 #define _GG_GUI_h_
31 
32 #include <GG/Font.h>
33 #include <GG/WndEvent.h>
34 
35 
36 namespace boost { namespace archive {
37  class xml_oarchive;
38  class xml_iarchive;
39 } }
40 
41 namespace GG {
42 
43 class Cursor;
44 class Wnd;
45 class EventPumpBase;
46 class ModalEventPump;
47 class PluginInterface;
48 class StyleFactory;
49 class Texture;
50 class Timer;
51 struct GUIImpl;
52 
116 class GG_API GUI
117 {
118 private:
119  struct OrCombiner
120  {
121  typedef bool result_type;
122  template<class InIt> bool operator()(InIt first, InIt last) const;
123  };
124 
125 public:
127 
130  typedef boost::signal<bool (), OrCombiner> AcceleratorSignalType;
132 
135  enum EventType {
146  MOUSEWHEEL
147  };
148 
151  typedef std::set<std::pair<Key, Flags<ModKey> > >::iterator accel_iterator;
152 
155  typedef std::set<std::pair<Key, Flags<ModKey> > >::const_iterator const_accel_iterator;
156 
158  virtual ~GUI();
159 
160 
162  const std::string&
163  AppName() const;
164  Wnd* FocusWnd() const;
165  Wnd* GetWindowUnder(const Pt& pt) const;
166  unsigned int DeltaT() const;
167  virtual unsigned int
168  Ticks() const = 0;
169  bool RenderingDragDropWnds() const;
170  bool FPSEnabled() const;
171  double FPS() const;
172  std::string FPSString() const;
173  double MaxFPS() const;
174  virtual X AppWidth() const = 0;
175  virtual Y AppHeight() const = 0;
176  unsigned int ButtonDownRepeatDelay() const;
177  unsigned int ButtonDownRepeatInterval() const;
178  unsigned int DoubleClickInterval() const;
179  unsigned int MinDragTime() const;
180  unsigned int MinDragDistance() const;
181  bool DragDropWnd(const Wnd* wnd) const;
182  bool AcceptedDragDropWnd(const Wnd* wnd) const;
183  bool MouseButtonDown(unsigned int bn) const;
184  Pt MousePosition() const;
185  Pt MouseMovement() const;
186  Flags<ModKey> ModKeys() const;
187 
193  virtual std::set<std::pair<CPSize, CPSize> > FindWords(const std::string& str) const;
194 
195  const boost::shared_ptr<StyleFactory>& GetStyleFactory() const;
196 
197  bool RenderCursor() const;
198  const boost::shared_ptr<Cursor>& GetCursor() const;
199 
201  const_accel_iterator accel_begin() const;
202 
204  const_accel_iterator accel_end() const;
205 
207  AcceleratorSignalType& AcceleratorSignal(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE) const;
208 
212  void SaveWndAsPNG(const Wnd* wnd, const std::string& filename) const;
214 
216  void operator()();
217  virtual void Exit(int code) = 0;
218 
222  virtual void HandleSystemEvents() = 0;
223  void HandleGGEvent(EventType event, Key key, boost::uint32_t key_code_point, Flags<ModKey> mod_keys, const Pt& pos, const Pt& rel);
224 
225  void SetFocusWnd(Wnd* wnd);
226  virtual void Wait(unsigned int ms);
227  void Register(Wnd* wnd);
228  void RegisterModal(Wnd* wnd);
229  void Remove(Wnd* wnd);
230  void WndDying(Wnd* wnd);
231  void MoveUp(Wnd* wnd);
232  void MoveDown(Wnd* wnd);
233 
234  virtual boost::shared_ptr<ModalEventPump>
235  CreateModalEventPump(bool& done);
236 
243  void RegisterDragDropWnd(Wnd* wnd, const Pt& offset, Wnd* originating_wnd);
244  void CancelDragDrop();
245 
246  void RegisterTimer(Timer& timer);
247  void RemoveTimer(Timer& timer);
248 
249  virtual void Enter2DMode() = 0;
250  virtual void Exit2DMode() = 0;
251  void EnableFPS(bool b = true);
252  void SetMaxFPS(double max);
253  void EnableMouseButtonDownRepeat(unsigned int delay, unsigned int interval);
254  void SetDoubleClickInterval(unsigned int interval);
255  void SetMinDragTime(unsigned int time);
256  void SetMinDragDistance(unsigned int distance);
257 
259  accel_iterator accel_begin();
260 
262  accel_iterator accel_end();
263 
266  void SetAccelerator(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE);
267 
270  void RemoveAccelerator(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE);
271 
273  void RemoveAccelerator(accel_iterator it);
274 
277  boost::shared_ptr<Font> GetFont(const std::string& font_filename, unsigned int pts);
278 
281  boost::shared_ptr<Font> GetFont(const std::string& font_filename, unsigned int pts,
282  const std::vector<unsigned char>& file_contents);
283 
286  template <class CharSetIter>
287  boost::shared_ptr<Font> GetFont(const std::string& font_filename, unsigned int pts,
288  CharSetIter first, CharSetIter last);
289 
293  template <class CharSetIter>
294  boost::shared_ptr<Font> GetFont(const std::string& font_filename, unsigned int pts,
295  const std::vector<unsigned char>& file_contents,
296  CharSetIter first, CharSetIter last);
297 
299  boost::shared_ptr<Font> GetFont(const boost::shared_ptr<Font>& font, unsigned int pts);
300 
303  void FreeFont(const std::string& font_filename, unsigned int pts);
304 
308  boost::shared_ptr<Texture> StoreTexture(Texture* texture, const std::string& texture_name);
309 
310  boost::shared_ptr<Texture> StoreTexture(const boost::shared_ptr<Texture> &texture, const std::string& texture_name);
311  boost::shared_ptr<Texture> GetTexture(const std::string& name, bool mipmap = false);
312  void FreeTexture(const std::string& name);
313 
314  void SetStyleFactory(const boost::shared_ptr<StyleFactory>& factory);
315 
316  void RenderCursor(bool render);
317  void SetCursor(const boost::shared_ptr<Cursor>& cursor);
318 
319 
320  static GUI* GetGUI();
321  static void RenderWindow(Wnd* wnd);
322 
324 
325  GG_ABSTRACT_EXCEPTION(Exception);
326 
329  GG_CONCRETE_EXCEPTION(BadFunctionPointer, GG::GUI, Exception);
331 
332 protected:
334  GUI(const std::string& app_name);
335 
336 
338  void ProcessBrowseInfo();
339  virtual void RenderBegin() = 0;
340  virtual void Render();
341  virtual void RenderEnd() = 0;
342 
343  // EventPumpBase interface
344  void SetFPS(double FPS);
345  void SetDeltaT(unsigned int delta_t);
346 
347 
348  virtual void Run() = 0;
349 
350 private:
351  bool ProcessBrowseInfoImpl(Wnd* wnd);
352  Wnd* ModalWindow() const; // returns the current modal window, if any
353 
354  // Returns the window under \a pt, sending Mouse{Enter|Leave} or
355  // DragDrop{Enter|Leave} as appropriate
356  Wnd* CheckedGetWindowUnder(const Pt& pt, Flags<ModKey> mod_keys);
357 
358  static GUI* s_gui;
359  static boost::shared_ptr<GUIImpl> s_impl;
360 
361  friend class EventPumpBase;
362  friend struct GUIImpl;
363 };
364 
365 
367 GG_API bool MatchesOrContains(const Wnd* lwnd, const Wnd* rwnd);
368 
369 
370 // template implementations
371 template<class InIt>
372 bool GUI::OrCombiner::operator()(InIt first, InIt last) const
373 {
374  bool retval = false;
375  while (first != last)
376  retval |= static_cast<bool>(*first++);
377  return retval;
378 }
379 
380 template <class CharSetIter>
381 boost::shared_ptr<Font> GUI::GetFont(const std::string& font_filename, unsigned int pts,
382  CharSetIter first, CharSetIter last)
383 { return GetFontManager().GetFont(font_filename, pts, first, last); }
384 
385 template <class CharSetIter>
386 boost::shared_ptr<Font> GUI::GetFont(const std::string& font_filename, unsigned int pts,
387  const std::vector<unsigned char>& file_contents,
388  CharSetIter first, CharSetIter last)
389 { return GetFontManager().GetFont(font_filename, pts, file_contents, first, last); }
390 
391 } // namespace GG
392 
393 #endif