GG

Wnd.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_Wnd_h_
00030 #define _GG_Wnd_h_
00031 
00032 #include <GG/Base.h>
00033 #include <GG/Exception.h>
00034 #include <GG/Flags.h>
00035 
00036 #include <list>
00037 #include <set>
00038 
00039 
00040 namespace GG {
00041 
00042 class BrowseInfoWnd;
00043 class Layout;
00044 class StyleFactory;
00045 class Timer;
00046 class WndEditor;
00047 class WndEvent;
00048 
00049 
00051 GG_FLAG_TYPE(WndFlag);
00052 
00055 extern GG_API const WndFlag INTERACTIVE;
00056 
00059 extern GG_API const WndFlag REPEAT_BUTTON_DOWN;
00060 
00062 extern GG_API const WndFlag DRAGABLE;
00063 
00065 extern GG_API const WndFlag RESIZABLE;
00066 
00070 extern GG_API const WndFlag ONTOP;
00071 
00076 extern GG_API const WndFlag MODAL;
00077 
00078 
00227 class GG_API Wnd : public boost::signals::trackable
00228 {
00229 public:
00235     struct GG_API BrowseInfoMode
00236     {
00239         unsigned int time;
00240 
00243         boost::shared_ptr<BrowseInfoWnd> wnd;
00244 
00246         std::string                      text;
00247 
00248         template <class Archive>
00249         void serialize(Archive& ar, const unsigned int version);
00250     };
00251 
00253     typedef std::map<const Wnd*, bool>::iterator DropsAcceptableIter;
00254 
00256     enum ChildClippingMode {
00258         DontClip,
00259 
00262         ClipToClient,
00263 
00266         ClipToWindow,
00267 
00274         ClipToClientAndWindowSeparately
00275     };
00276  
00278     virtual ~Wnd(); 
00279 
00280  
00282 
00285     bool Interactive() const;
00286 
00289     bool RepeatButtonDown() const;
00290 
00292     bool Dragable() const;
00293 
00295     bool Resizable() const;
00296 
00298     bool OnTop() const;
00299 
00301     bool Modal() const;
00302 
00304     ChildClippingMode GetChildClippingMode() const;
00305 
00308     bool NonClientChild() const;
00309 
00311     bool Visible() const;
00312 
00315      const std::string& Name() const;
00316 
00320     const std::string& DragDropDataType() const;
00321 
00325     virtual void DropsAcceptable(DropsAcceptableIter first,
00326                                  DropsAcceptableIter last,
00327                                  const Pt& pt) const;
00328 
00331     Pt UpperLeft() const;
00332 
00336     Pt LowerRight() const;
00337 
00340     Pt RelativeUpperLeft() const;
00341 
00345     Pt RelativeLowerRight() const;
00346 
00347     X Width() const;  
00348     Y Height() const; 
00349 
00352     int ZOrder() const;
00353 
00355     Pt Size() const;
00356 
00357     Pt MinSize() const; 
00358     Pt MaxSize() const; 
00359 
00366     virtual Pt MinUsableSize() const;
00367 
00372     virtual Pt ClientUpperLeft() const;
00373 
00378     virtual Pt ClientLowerRight() const;
00379 
00381     Pt ClientSize() const;
00382 
00383     X ClientWidth() const;  
00384     Y ClientHeight() const; 
00385 
00387     Pt ScreenToWindow(const Pt& pt) const;
00388 
00390     Pt ScreenToClient(const Pt& pt) const;
00391 
00394     virtual bool InWindow(const Pt& pt) const;
00395 
00398     virtual bool InClient(const Pt& pt) const;
00399 
00402     const std::list<Wnd*>& Children() const;
00403 
00405     Wnd* Parent() const;
00406 
00408     Wnd* RootParent() const;
00409 
00411     Layout* GetLayout() const;
00412 
00414     Layout* ContainingLayout() const;
00415 
00423     const std::vector<BrowseInfoMode>&  BrowseModes() const;
00424 
00428     const std::string& BrowseInfoText(std::size_t mode) const;
00429 
00432     const boost::shared_ptr<StyleFactory>& GetStyleFactory() const;
00433 
00435     virtual WndRegion WindowRegion(const Pt& pt) const;
00437  
00439 
00442     void SetDragDropDataType(const std::string& data_type);
00443 
00449     virtual void StartingChildDragDrop(const Wnd* wnd, const Pt& offset);
00450 
00455     virtual void AcceptDrops(const std::vector<Wnd*>& wnds, const Pt& pt);
00456 
00465     virtual void CancellingChildDragDrop(const std::vector<const Wnd*>& wnds);
00466 
00475     virtual void ChildrenDraggedAway(const std::vector<Wnd*>& wnds,
00476                                      const Wnd* destination);
00477 
00480     void SetName(const std::string& name);
00481 
00484     void Hide(bool children = true);
00485 
00488     void Show(bool children = true);
00489 
00493     virtual void ModalInit();
00494 
00496     void SetChildClippingMode(ChildClippingMode mode);
00497 
00500     void NonClientChild(bool b);
00501 
00502     void MoveTo(const Pt& pt);     
00503     void OffsetMove(const Pt& pt); 
00504 
00507     virtual void SizeMove(const Pt& ul, const Pt& lr);
00508 
00510     void Resize(const Pt& sz);
00511 
00513     void SetMinSize(const Pt& sz);
00514 
00516     void SetMaxSize(const Pt& sz);
00517 
00520     void AttachChild(Wnd* wnd);
00521 
00524     void MoveChildUp(Wnd* wnd);
00525 
00528     void MoveChildDown(Wnd* wnd);
00529 
00531     void DetachChild(Wnd* wnd);
00532 
00534     void DetachChildren();
00535 
00538     void DeleteChild(Wnd* wnd);
00539 
00541     void DeleteChildren();
00542 
00544     void InstallEventFilter(Wnd* wnd);
00545 
00547     void RemoveEventFilter(Wnd* wnd);
00548 
00552     void HorizontalLayout();
00553 
00557     void VerticalLayout();
00558 
00562     void GridLayout();
00563 
00566     void SetLayout(Layout* layout);
00567 
00572     void RemoveLayout();
00573 
00577     Layout* DetachLayout();
00578 
00582     void SetLayoutBorderMargin(unsigned int margin);
00583 
00586     void SetLayoutCellMargin(unsigned int margin);
00587 
00588 
00595     virtual void Render();
00596 
00600     virtual bool Run();
00601 
00603     void EndRun();
00604 
00610     void SetBrowseModeTime(unsigned int time, std::size_t mode = 0);
00611 
00615     void SetBrowseInfoWnd(const boost::shared_ptr<BrowseInfoWnd>& wnd, std::size_t mode = 0);
00616 
00621     void ClearBrowseInfoWnd(std::size_t mode = 0);
00622 
00627     void SetBrowseText(const std::string& text, std::size_t mode = 0);
00628 
00636     void SetBrowseModes(const std::vector<BrowseInfoMode>& modes);
00637 
00639     void SetStyleFactory(const boost::shared_ptr<StyleFactory>& factory);
00640 
00643     virtual void DefineAttributes(WndEditor* editor);
00645 
00646 
00649     static unsigned int DefaultBrowseTime();
00650 
00653     static void SetDefaultBrowseTime(unsigned int time);
00654 
00658     static const boost::shared_ptr<BrowseInfoWnd>& DefaultBrowseInfoWnd();
00659 
00662     static void SetDefaultBrowseInfoWnd(const boost::shared_ptr<BrowseInfoWnd>& browse_info_wnd);
00663  
00665 
00666     GG_ABSTRACT_EXCEPTION(Exception);
00667 
00671     GG_CONCRETE_EXCEPTION(BadLayout, GG::Wnd, Exception);
00673 
00674 protected:
00678     enum DragDropRenderingState {
00680         NOT_DRAGGED,
00681 
00684         IN_PLACE_COPY,
00685 
00688         DRAGGED_OVER_UNACCEPTING_DROP_TARGET,
00689 
00692         DRAGGED_OVER_ACCEPTING_DROP_TARGET
00693     };
00694  
00696     Wnd(); 
00697 
00700     Wnd(X x, Y y, X w, Y h, Flags<WndFlag> flags = INTERACTIVE | DRAGABLE);
00702  
00704 
00707     DragDropRenderingState GetDragDropRenderingState() const;
00709  
00711 
00716     virtual void LButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
00717 
00724     virtual void LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
00725 
00730     virtual void LButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
00731 
00736     virtual void LClick(const Pt& pt, Flags<ModKey> mod_keys);
00737 
00745     virtual void LDoubleClick(const Pt& pt, Flags<ModKey> mod_keys);
00746 
00748     virtual void MButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
00749 
00752     virtual void MDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
00753 
00756     virtual void MButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
00757 
00760     virtual void MClick(const Pt& pt, Flags<ModKey> mod_keys);
00761 
00764     virtual void MDoubleClick(const Pt& pt, Flags<ModKey> mod_keys);
00765 
00767     virtual void RButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
00768 
00771     virtual void RDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
00772 
00775     virtual void RButtonUp(const Pt& pt, Flags<ModKey> mod_keys);
00776 
00779     virtual void RClick(const Pt& pt, Flags<ModKey> mod_keys);
00780 
00783     virtual void RDoubleClick(const Pt& pt, Flags<ModKey> mod_keys);
00784 
00786     virtual void MouseEnter(const Pt& pt, Flags<ModKey> mod_keys);
00787 
00792     virtual void MouseHere(const Pt& pt, Flags<ModKey> mod_keys);
00793 
00795     virtual void MouseLeave();
00796 
00799     virtual void MouseWheel(const Pt& pt, int move, Flags<ModKey> mod_keys);
00800 
00804     virtual void DragDropEnter(const Pt& pt, const std::map<Wnd*, Pt>& drag_drop_wnds,
00805                                Flags<ModKey> mod_keys);
00806 
00813     virtual void DragDropHere(const Pt& pt, const std::map<Wnd*, Pt>& drag_drop_wnds,
00814                               Flags<ModKey> mod_keys);
00815 
00818     virtual void DragDropLeave();
00819 
00828     virtual void KeyPress(Key key, boost::uint32_t key_code_point, Flags<ModKey> mod_keys);
00829 
00835     virtual void KeyRelease(Key key, boost::uint32_t key_code_point, Flags<ModKey> mod_keys);
00836 
00838     virtual void GainingFocus();
00839 
00841     virtual void LosingFocus();
00842 
00844     virtual void TimerFiring(unsigned int ticks, Timer* timer);
00845 
00849     virtual bool EventFilter(Wnd* w, const WndEvent& event);
00850 
00853     void HandleEvent(const WndEvent& event);
00854 
00858     void ForwardEventToParent();
00859 
00861     void BeginClipping();
00862 
00864     void EndClipping();
00865 
00869     void BeginNonclientClipping();
00870 
00874     void EndNonclientClipping();
00876 
00878     bool m_done;
00879 
00880 private:
00881     void ValidateFlags();              
00882     virtual void BeginClippingImpl(ChildClippingMode mode);
00883     virtual void EndClippingImpl(ChildClippingMode mode);
00884     virtual void BeginNonclientClippingImpl();
00885     virtual void EndNonclientClippingImpl();
00886 
00887 
00888     Wnd*              m_parent;        
00889     std::string       m_name;          
00890     std::list<Wnd*>   m_children;      
00891     int               m_zorder;        
00892     bool              m_visible;
00893     std::string       m_drag_drop_data_type; 
00894     ChildClippingMode m_child_clipping_mode;
00895     bool              m_non_client_child;
00896     Pt                m_upperleft;     
00897     Pt                m_lowerright;    
00898     Pt                m_min_size;      
00899     Pt                m_max_size;      
00900 
00903     std::vector<Wnd*> m_filters;
00904 
00905     std::set<Wnd*>    m_filtering;         
00906     Layout*           m_layout;            
00907     Layout*           m_containing_layout; 
00908     std::vector<BrowseInfoMode>
00909                       m_browse_modes;      
00910 
00911     boost::shared_ptr<StyleFactory>
00912                       m_style_factory;     
00913 
00916     Flags<WndFlag> m_flags;
00917 
00920     static unsigned int s_default_browse_time;
00921 
00924     static boost::shared_ptr<BrowseInfoWnd> s_default_browse_info_wnd;
00925 
00926     friend class GUI;   
00927     friend class GUIImpl;
00928     friend class Timer; 
00929     friend class ZList; 
00930 
00931     friend class boost::serialization::access;
00932     template <class Archive>
00933     void serialize(Archive& ar, const unsigned int version);
00934 };
00935 
00936 } // namespace GG
00937 
00938 // template implementations
00939 template <class Archive>
00940 void GG::Wnd::BrowseInfoMode::serialize(Archive& ar, const unsigned int version)
00941 {
00942     ar  & BOOST_SERIALIZATION_NVP(time)
00943         & BOOST_SERIALIZATION_NVP(wnd)
00944         & BOOST_SERIALIZATION_NVP(text);
00945 }
00946 
00947 template <class Archive>
00948 void GG::Wnd::serialize(Archive& ar, const unsigned int version)
00949 {
00950     ar  & BOOST_SERIALIZATION_NVP(m_done)
00951         & BOOST_SERIALIZATION_NVP(m_parent)
00952         & BOOST_SERIALIZATION_NVP(m_name)
00953         & BOOST_SERIALIZATION_NVP(m_children)
00954         & BOOST_SERIALIZATION_NVP(m_zorder)
00955         & BOOST_SERIALIZATION_NVP(m_visible)
00956         & BOOST_SERIALIZATION_NVP(m_drag_drop_data_type)
00957         & BOOST_SERIALIZATION_NVP(m_child_clipping_mode)
00958         & BOOST_SERIALIZATION_NVP(m_non_client_child)
00959         & BOOST_SERIALIZATION_NVP(m_upperleft)
00960         & BOOST_SERIALIZATION_NVP(m_lowerright)
00961         & BOOST_SERIALIZATION_NVP(m_min_size)
00962         & BOOST_SERIALIZATION_NVP(m_max_size)
00963         & BOOST_SERIALIZATION_NVP(m_filters)
00964         & BOOST_SERIALIZATION_NVP(m_filtering)
00965         & BOOST_SERIALIZATION_NVP(m_layout)
00966         & BOOST_SERIALIZATION_NVP(m_containing_layout)
00967         & BOOST_SERIALIZATION_NVP(m_browse_modes)
00968         & BOOST_SERIALIZATION_NVP(m_style_factory)
00969         & BOOST_SERIALIZATION_NVP(m_flags);
00970 
00971     if (Archive::is_loading::value)
00972         ValidateFlags();
00973 }
00974 
00975 #endif // _GG_Wnd_h_