GG

GUI.h

Go to the documentation of this file.
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_GUI_h_
00030 #define _GG_GUI_h_
00031 
00032 #include <GG/Font.h>
00033 #include <GG/WndEvent.h>
00034 
00035 
00036 namespace boost { namespace archive {
00037     class xml_oarchive;
00038     class xml_iarchive;
00039 } }
00040 
00041 namespace GG {
00042 
00043 class Cursor;
00044 class Wnd;
00045 class EventPumpBase;
00046 class ModalEventPump;
00047 class PluginInterface;
00048 class StyleFactory;
00049 class Texture;
00050 class Timer;
00051 struct GUIImpl;
00052 
00116 class GG_API GUI
00117 {
00118 private:
00119     struct OrCombiner 
00120     {
00121         typedef bool result_type; 
00122         template<class InIt> bool operator()(InIt first, InIt last) const;
00123     };
00124 
00125 public: 
00127 
00130     typedef boost::signal<bool (), OrCombiner> AcceleratorSignalType;
00132 
00135     enum EventType {
00136         IDLE,        
00137         KEYPRESS,    
00138         KEYRELEASE,  
00139         LPRESS,      
00140         MPRESS,      
00141         RPRESS,      
00142         LRELEASE,    
00143         MRELEASE,    
00144         RRELEASE,    
00145         MOUSEMOVE,   
00146         MOUSEWHEEL   
00147     };
00148 
00151     typedef std::set<std::pair<Key, Flags<ModKey> > >::iterator accel_iterator;
00152 
00155     typedef std::set<std::pair<Key, Flags<ModKey> > >::const_iterator const_accel_iterator;
00156 
00158     typedef void (*SaveWndFn)(const Wnd* wnd, const std::string& name, boost::archive::xml_oarchive& ar);
00159 
00161     typedef void (*LoadWndFn)(Wnd*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00162  
00164     virtual ~GUI(); 
00165 
00166  
00168     const std::string&
00169                    AppName() const;                    
00170     Wnd*           FocusWnd() const;                   
00171     Wnd*           GetWindowUnder(const Pt& pt) const; 
00172     unsigned int   DeltaT() const;                     
00173     virtual unsigned int
00174                    Ticks() const = 0;                  
00175     bool           RenderingDragDropWnds() const;      
00176     bool           FPSEnabled() const;                 
00177     double         FPS() const;                        
00178     std::string    FPSString() const;                  
00179     double         MaxFPS() const;                     
00180     virtual X      AppWidth() const = 0;               
00181     virtual Y      AppHeight() const = 0;              
00182     unsigned int   ButtonDownRepeatDelay() const;      
00183     unsigned int   ButtonDownRepeatInterval() const;   
00184     unsigned int   DoubleClickInterval() const;        
00185     unsigned int   MinDragTime() const;                
00186     unsigned int   MinDragDistance() const;            
00187     bool           DragDropWnd(const Wnd* wnd) const;  
00188     bool           AcceptedDragDropWnd(const Wnd* wnd) const; 
00189     bool           MouseButtonDown(unsigned int bn) const; 
00190     Pt             MousePosition() const;              
00191     Pt             MouseMovement() const;              
00192     Flags<ModKey>  ModKeys() const;                    
00193 
00199     virtual std::set<std::pair<CPSize, CPSize> > FindWords(const std::string& str) const;
00200 
00201     const boost::shared_ptr<StyleFactory>& GetStyleFactory() const; 
00202 
00203     bool                                   RenderCursor() const; 
00204     const boost::shared_ptr<Cursor>&       GetCursor() const; 
00205 
00207     const_accel_iterator accel_begin() const;
00208 
00210     const_accel_iterator accel_end() const;
00211 
00213     AcceleratorSignalType& AcceleratorSignal(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE) const;
00214 
00218     void SaveWndAsPNG(const Wnd* wnd, const std::string& filename) const;
00220  
00222     void           operator()();                 
00223     virtual void   Exit(int code) = 0;           
00224 
00228     virtual void   HandleSystemEvents() = 0;
00229     void           HandleGGEvent(EventType event, Key key, boost::uint32_t key_code_point, Flags<ModKey> mod_keys, const Pt& pos, const Pt& rel); 
00230 
00231     void           SetFocusWnd(Wnd* wnd);        
00232     virtual void   Wait(unsigned int ms);        
00233     void           Register(Wnd* wnd);           
00234     void           RegisterModal(Wnd* wnd);      
00235     void           Remove(Wnd* wnd);             
00236     void           WndDying(Wnd* wnd);           
00237     void           MoveUp(Wnd* wnd);             
00238     void           MoveDown(Wnd* wnd);           
00239 
00240     virtual boost::shared_ptr<ModalEventPump>
00241                    CreateModalEventPump(bool& done); 
00242 
00249     void           RegisterDragDropWnd(Wnd* wnd, const Pt& offset, Wnd* originating_wnd);
00250     void           CancelDragDrop();             
00251 
00252     void           RegisterTimer(Timer& timer);  
00253     void           RemoveTimer(Timer& timer);    
00254 
00255     virtual void   Enter2DMode() = 0;            
00256     virtual void   Exit2DMode() = 0;             
00257     void           EnableFPS(bool b = true);     
00258     void           SetMaxFPS(double max);        
00259     void           EnableMouseButtonDownRepeat(unsigned int delay, unsigned int interval); 
00260     void           SetDoubleClickInterval(unsigned int interval); 
00261     void           SetMinDragTime(unsigned int time);     
00262     void           SetMinDragDistance(unsigned int distance); 
00263 
00265     accel_iterator accel_begin();
00266 
00268     accel_iterator accel_end();
00269 
00272     void           SetAccelerator(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE);
00273 
00276     void           RemoveAccelerator(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE);
00277 
00279     void           RemoveAccelerator(accel_iterator it);
00280 
00283     boost::shared_ptr<Font>    GetFont(const std::string& font_filename, unsigned int pts);
00284 
00287     boost::shared_ptr<Font>    GetFont(const std::string& font_filename, unsigned int pts,
00288                                        const std::vector<unsigned char>& file_contents);
00289 
00292     template <class CharSetIter>
00293     boost::shared_ptr<Font>    GetFont(const std::string& font_filename, unsigned int pts,
00294                                        CharSetIter first, CharSetIter last);
00295 
00299     template <class CharSetIter>
00300     boost::shared_ptr<Font>    GetFont(const std::string& font_filename, unsigned int pts,
00301                                        const std::vector<unsigned char>& file_contents,
00302                                        CharSetIter first, CharSetIter last);
00303 
00305     boost::shared_ptr<Font>    GetFont(const boost::shared_ptr<Font>& font, unsigned int pts);
00306 
00309     void                       FreeFont(const std::string& font_filename, unsigned int pts);
00310 
00314     boost::shared_ptr<Texture> StoreTexture(Texture* texture, const std::string& texture_name);
00315 
00316     boost::shared_ptr<Texture> StoreTexture(const boost::shared_ptr<Texture> &texture, const std::string& texture_name); 
00317     boost::shared_ptr<Texture> GetTexture(const std::string& name, bool mipmap = false); 
00318     void                       FreeTexture(const std::string& name); 
00319 
00320     void SetStyleFactory(const boost::shared_ptr<StyleFactory>& factory); 
00321 
00322     void RenderCursor(bool render); 
00323     void SetCursor(const boost::shared_ptr<Cursor>& cursor); 
00324 
00329     void SaveWnd(const Wnd* wnd, const std::string& name, boost::archive::xml_oarchive& ar);
00330 
00335     void LoadWnd(Wnd*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00336 
00340     template <class T>
00341     void LoadWnd(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00342 
00343     void SetSaveWndFunction(SaveWndFn fn); 
00344     void SetLoadWndFunction(LoadWndFn fn); 
00345 
00348     void SetSaveLoadFunctions(const PluginInterface& interface);
00350 
00351     static GUI*  GetGUI();                
00352     static void  RenderWindow(Wnd* wnd);  
00353  
00355 
00356     GG_ABSTRACT_EXCEPTION(Exception);
00357 
00360     GG_CONCRETE_EXCEPTION(BadFunctionPointer, GG::GUI, Exception);
00362 
00363 protected: 
00365     GUI(const std::string& app_name); 
00366 
00367  
00369     void           ProcessBrowseInfo();    
00370     virtual void   RenderBegin() = 0;      
00371     virtual void   Render();               
00372     virtual void   RenderEnd() = 0;        
00373 
00374     // EventPumpBase interface
00375     void SetFPS(double FPS);               
00376     void SetDeltaT(unsigned int delta_t);  
00377 
00378 
00379     virtual void   Run() = 0;              
00380 
00381 private:
00382     bool           ProcessBrowseInfoImpl(Wnd* wnd);
00383     Wnd*           ModalWindow() const;    // returns the current modal window, if any
00384 
00385     // Returns the window under \a pt, sending Mouse{Enter|Leave} or
00386     // DragDrop{Enter|Leave} as appropriate
00387     Wnd*           CheckedGetWindowUnder(const Pt& pt, Flags<ModKey> mod_keys);
00388 
00389     static GUI*                       s_gui;
00390     static boost::shared_ptr<GUIImpl> s_impl;
00391 
00392     friend class EventPumpBase; 
00393     friend struct GUIImpl;
00394 };
00395 
00396 
00398 GG_API bool MatchesOrContains(const Wnd* lwnd, const Wnd* rwnd);
00399 
00400 
00401 // template implementations
00402 template<class InIt> 
00403 bool GUI::OrCombiner::operator()(InIt first, InIt last) const
00404 {
00405     bool retval = false;
00406     while (first != last)
00407         retval |= static_cast<bool>(*first++);
00408     return retval;
00409 }
00410 
00411 template <class CharSetIter>
00412 boost::shared_ptr<Font> GUI::GetFont(const std::string& font_filename, unsigned int pts,
00413                                      CharSetIter first, CharSetIter last)
00414 { return GetFontManager().GetFont(font_filename, pts, first, last); }
00415 
00416 template <class CharSetIter>
00417 boost::shared_ptr<Font> GUI::GetFont(const std::string& font_filename, unsigned int pts,
00418                                      const std::vector<unsigned char>& file_contents,
00419                                      CharSetIter first, CharSetIter last)
00420 { return GetFontManager().GetFont(font_filename, pts, file_contents, first, last); }
00421 
00422 template <class T>
00423 void GUI::LoadWnd(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar)
00424 {
00425     Wnd* wnd_as_base = wnd;
00426     LoadWnd(wnd_as_base, name, ar);
00427     wnd = dynamic_cast<T*>(wnd_as_base);
00428     assert(wnd || !wnd_as_base);
00429 }
00430 
00431 } // namespace GG
00432 
00433 #endif // _GG_GUI_h_