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_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 
00101 class GG_API GUI
00102 {
00103 private:
00104     struct OrCombiner 
00105     {
00106         typedef bool result_type; 
00107         template<class InIt> bool operator()(InIt first, InIt last) const;
00108     };
00109 
00110 public: 
00112 
00114     typedef boost::signal<bool (), OrCombiner> AcceleratorSignalType;
00116  
00118     typedef AcceleratorSignalType::slot_type   AcceleratorSlotType; 
00119 
00120 
00122     enum EventType {
00123         IDLE,        
00124         KEYPRESS,    
00125         KEYRELEASE,  
00126         LPRESS,      
00127         MPRESS,      
00128         RPRESS,      
00129         LRELEASE,    
00130         MRELEASE,    
00131         RRELEASE,    
00132         MOUSEMOVE,   
00133         MOUSEWHEEL   
00134     };
00135 
00136     typedef std::set<std::pair<Key, Flags<ModKey> > >::const_iterator const_accel_iterator; 
00137 
00139     typedef void (*SaveWndFn)(const Wnd* wnd, const std::string& name, boost::archive::xml_oarchive& ar);
00140 
00142     typedef void (*LoadWndFn)(Wnd*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00143  
00145     virtual ~GUI(); 
00146 
00147  
00149     const std::string&
00150                    AppName() const;                    
00151     Wnd*           FocusWnd() const;                   
00152     Wnd*           GetWindowUnder(const Pt& pt) const; 
00153     int            DeltaT() const;                     
00154     virtual int    Ticks() const = 0;                  
00155     bool           RenderingDragDropWnds() const;      
00156     bool           FPSEnabled() const;                 
00157     double         FPS() const;                        
00158     std::string    FPSString() const;                  
00159     double         MaxFPS() const;                     
00160     virtual int    AppWidth() const = 0;               
00161     virtual int    AppHeight() const = 0;              
00162     int            ButtonDownRepeatDelay() const;      
00163     int            ButtonDownRepeatInterval() const;   
00164     int            DoubleClickInterval() const;        
00165     int            MinDragTime() const;                
00166     int            MinDragDistance() const;            
00167     bool           DragDropWnd(const Wnd* wnd) const;  
00168     bool           MouseButtonDown(int bn) const;      
00169     Pt             MousePosition() const;              
00170     Pt             MouseMovement() const;              
00171     Flags<ModKey>  ModKeys() const;                    
00172 
00176     virtual std::set<std::pair<int, int> > FindWords(const std::string& str) const;
00177 
00178     const boost::shared_ptr<StyleFactory>& GetStyleFactory() const; 
00179 
00180     bool                                   RenderCursor() const; 
00181     const boost::shared_ptr<Cursor>&       GetCursor() const; 
00182 
00183     const_accel_iterator accel_begin() const;    
00184     const_accel_iterator accel_end() const;      
00185 
00187     AcceleratorSignalType& AcceleratorSignal(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE) const;
00189  
00191     void           operator()();                 
00192     virtual void   Exit(int code) = 0;           
00193 
00196     virtual void   HandleSystemEvents() = 0;
00197     void           HandleGGEvent(EventType event, Key key, Flags<ModKey> mod_keys, const Pt& pos, const Pt& rel); 
00198 
00199     void           SetFocusWnd(Wnd* wnd);        
00200     virtual void   Wait(int ms);                 
00201     void           Register(Wnd* wnd);           
00202     void           RegisterModal(Wnd* wnd);      
00203     void           Remove(Wnd* wnd);             
00204     void           WndDying(Wnd* wnd);           
00205     void           MoveUp(Wnd* wnd);             
00206     void           MoveDown(Wnd* wnd);           
00207 
00208     virtual boost::shared_ptr<ModalEventPump>
00209                    CreateModalEventPump(bool& done); 
00210 
00215     void           RegisterDragDropWnd(Wnd* wnd, const Pt& offset, Wnd* originating_wnd);
00216     void           CancelDragDrop();             
00217 
00218     void           RegisterTimer(Timer& timer);  
00219     void           RemoveTimer(Timer& timer);    
00220 
00221     virtual void   Enter2DMode() = 0;            
00222     virtual void   Exit2DMode() = 0;             
00223     void           EnableFPS(bool b = true);     
00224     void           SetMaxFPS(double max);        
00225     void           EnableMouseButtonDownRepeat(int delay, int interval); 
00226     void           SetDoubleClickInterval(int interval); 
00227     void           SetMinDragTime(int time);     
00228     void           SetMinDragDistance(int distance); 
00229 
00231     void           SetAccelerator(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE);
00232 
00234     void           RemoveAccelerator(Key key, Flags<ModKey> mod_keys = MOD_KEY_NONE);
00235 
00236     boost::shared_ptr<Font>    GetFont(const std::string& font_filename, int pts, unsigned int range = Font::ALL_CHARS); 
00237     void                       FreeFont(const std::string& font_filename, int pts); 
00238 
00240     boost::shared_ptr<Texture> StoreTexture(Texture* texture, const std::string& texture_name);
00241 
00242     boost::shared_ptr<Texture> StoreTexture(const boost::shared_ptr<Texture> &texture, const std::string& texture_name); 
00243     boost::shared_ptr<Texture> GetTexture(const std::string& name, bool mipmap = false); 
00244     void                       FreeTexture(const std::string& name); 
00245 
00246     void SetStyleFactory(const boost::shared_ptr<StyleFactory>& factory); 
00247 
00248     void RenderCursor(bool render); 
00249     void SetCursor(const boost::shared_ptr<Cursor>& cursor); 
00250 
00254     void SaveWnd(const Wnd* wnd, const std::string& name, boost::archive::xml_oarchive& ar);
00255 
00259     void LoadWnd(Wnd*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00260 
00263     template <class T>
00264     void LoadWnd(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00265 
00266     void SetSaveWndFunction(SaveWndFn fn); 
00267     void SetLoadWndFunction(LoadWndFn fn); 
00268 
00270     void SetSaveLoadFunctions(const PluginInterface& interface);
00272 
00273     static GUI*  GetGUI();                
00274     static void  RenderWindow(Wnd* wnd);  
00275  
00277 
00278     GG_ABSTRACT_EXCEPTION(Exception);
00279 
00281     GG_CONCRETE_EXCEPTION(BadFunctionPointer, GG::GUI, Exception);
00283 
00284 protected: 
00286     GUI(const std::string& app_name); 
00287 
00288  
00290     void           ProcessBrowseInfo();    
00291     virtual void   RenderBegin() = 0;      
00292     virtual void   Render();               
00293     virtual void   RenderEnd() = 0;        
00294 
00295     
00296     void SetFPS(double FPS);               
00297     void SetDeltaT(int delta_t);           
00298 
00299 
00300     virtual void   Run() = 0;              
00301 
00302 private:
00303     bool           ProcessBrowseInfoImpl(Wnd* wnd);
00304     Wnd*           ModalWindow() const;    
00305 
00306     
00307     Wnd*           CheckedGetWindowUnder(const Pt& pt, Flags<ModKey> mod_keys);
00308 
00309     static GUI*                       s_gui;
00310     static boost::shared_ptr<GUIImpl> s_impl;
00311 
00312     friend class EventPumpBase; 
00313     friend struct GUIImpl;
00314 };
00315 
00316 
00318 GG_API bool MatchesOrContains(const Wnd* lwnd, const Wnd* rwnd);
00319 
00320 
00321 
00322 template<class InIt> 
00323 bool GUI::OrCombiner::operator()(InIt first, InIt last) const
00324 {
00325     bool retval = false;
00326     while (first != last)
00327         retval |= static_cast<bool>(*first++);
00328     return retval;
00329 }
00330 
00331 template <class T>
00332 void GUI::LoadWnd(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar)
00333 {
00334     Wnd* wnd_as_base = wnd;
00335     LoadWnd(wnd_as_base, name, ar);
00336     wnd = dynamic_cast<T*>(wnd_as_base);
00337     assert(wnd || !wnd_as_base);
00338 }
00339 
00340 } 
00341 
00342 #endif // _GG_GUI_h_