Layout.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_Layout_h_
00030 #define _GG_Layout_h_
00031 
00032 #include <GG/AlignmentFlags.h>
00033 #include <GG/Wnd.h>
00034 
00035 #include <boost/serialization/version.hpp>
00036 
00037 
00038 namespace GG {
00039 
00040 struct SetMarginAction;
00041 
00105 class GG_API Layout : public Wnd
00106 {
00107 public: 
00109 
00110     Layout(X x, Y y, X w, Y h, std::size_t rows, std::size_t columns, unsigned int border_margin = 0, unsigned int cell_margin = INVALID_CELL_MARGIN);
00112  
00114     virtual Pt MinUsableSize() const;
00115 
00116     std::size_t      Rows() const;                             
00117     std::size_t      Columns() const;                          
00118     Flags<Alignment> ChildAlignment(Wnd* wnd) const;           
00119     unsigned int     BorderMargin() const;                     
00120     unsigned int     CellMargin() const;                       
00121     double           RowStretch(std::size_t row) const;            
00122     double           ColumnStretch(std::size_t column) const;      
00123     Y                MinimumRowHeight(std::size_t row) const;      
00124     X                MinimumColumnWidth(std::size_t column) const; 
00125     std::vector<std::vector<const Wnd*> >
00126                      Cells() const;                            
00127     std::vector<std::vector<Rect> >
00128                      CellRects() const;                        
00129     std::vector<std::vector<Rect> >
00130                      RelativeCellRects() const;                
00131 
00134     bool   RenderOutline() const;
00135 
00139     Clr    OutlineColor() const;
00141     
00143     virtual void StartingChildDragDrop(const Wnd* wnd, const Pt& offset);
00144     virtual void CancellingChildDragDrop(const std::vector<const Wnd*>& wnds);
00145     virtual void ChildrenDraggedAway(const std::vector<Wnd*>& wnds, const Wnd* destination);
00146     virtual void SizeMove(const Pt& ul, const Pt& lr);
00147     virtual void Render();
00148 
00152     void Add(Wnd* wnd, std::size_t row, std::size_t column, Flags<Alignment> alignment = ALIGN_NONE);
00153 
00161     void Add(Wnd* wnd, std::size_t row, std::size_t column, std::size_t num_rows, std::size_t num_columns, Flags<Alignment> alignment = ALIGN_NONE);
00162 
00166     void Remove(Wnd* wnd);
00167 
00171     void DetachAndResetChildren();
00172 
00176     void ResizeLayout(std::size_t rows, std::size_t columns);
00177 
00180     void SetChildAlignment(Wnd* wnd, Flags<Alignment> alignment);
00181 
00184     void SetBorderMargin(unsigned int margin);
00185 
00188     void SetCellMargin(unsigned int margin);
00189 
00194     void SetRowStretch(std::size_t row, double stretch);
00195 
00200     void SetColumnStretch(std::size_t column, double stretch);
00201 
00204     void SetMinimumRowHeight(std::size_t row, Y height);
00205 
00208     void SetMinimumColumnWidth(std::size_t column, X width);
00209 
00212     void RenderOutline(bool render_outline);
00213 
00217     void SetOutlineColor(Clr color);
00218 
00219     virtual void DefineAttributes(WndEditor* editor);
00221  
00223 
00224     GG_ABSTRACT_EXCEPTION(Exception);
00225 
00227     GG_CONCRETE_EXCEPTION(InvalidMargin, GG::Layout, Exception);
00228 
00230     GG_CONCRETE_EXCEPTION(NoSuchChild, GG::Layout, Exception);
00231 
00234     GG_CONCRETE_EXCEPTION(FailedCalculationCheck, GG::Layout, Exception);
00235 
00238     GG_CONCRETE_EXCEPTION(AttemptedOverwrite, GG::Layout, Exception);
00240 
00241     static const unsigned int INVALID_CELL_MARGIN;
00242 
00243 protected: 
00245     Layout(); 
00246 
00247  
00249     virtual void MouseWheel(const Pt& pt, int move, Flags<ModKey> mod_keys);
00250     virtual void KeyPress(Key key, boost::uint32_t key_code_point, Flags<ModKey> mod_keys);
00251     virtual void KeyRelease(Key key, boost::uint32_t key_code_point, Flags<ModKey> mod_keys);
00253 
00254 private:
00255     struct GG_API RowColParams
00256     {
00257         RowColParams();
00258 
00259         double stretch;
00260         unsigned int    min;
00261         unsigned int    effective_min;   
00262         int             current_origin;  
00263         unsigned int    current_width;   
00264 
00265     private:
00266         friend class boost::serialization::access;
00267         template <class Archive>
00268         void serialize(Archive& ar, const unsigned int version);
00269     };
00270 
00271     struct GG_API WndPosition
00272     {
00273         WndPosition();
00274         WndPosition(std::size_t first_row_, std::size_t first_column_,
00275                     std::size_t last_row_, std::size_t last_column_,
00276                     Flags<Alignment> alignment_, const Pt& original_ul_, const Pt& original_size_);
00277 
00278         std::size_t      first_row;
00279         std::size_t      first_column;
00280         std::size_t      last_row;
00281         std::size_t      last_column;
00282         Flags<Alignment> alignment;
00283         Pt               original_ul;
00284         Pt               original_size;
00285 
00286     private:
00287         friend class boost::serialization::access;
00288         template <class Archive>
00289         void serialize(Archive& ar, const unsigned int version);
00290     };
00291 
00292     double TotalStretch(const std::vector<RowColParams>& params_vec) const;
00293     X      TotalMinWidth() const;
00294     Y      TotalMinHeight() const;
00295     void   ValidateAlignment(Flags<Alignment>& alignment);
00296     void   RedoLayout();
00297     void   ChildSizeOrMinSizeOrMaxSizeChanged();
00298 
00299     std::vector<std::vector<Wnd*> > m_cells;
00300     unsigned int                    m_border_margin;
00301     unsigned int                    m_cell_margin;
00302     std::vector<RowColParams>       m_row_params;
00303     std::vector<RowColParams>       m_column_params;
00304     std::map<Wnd*, WndPosition>     m_wnd_positions;
00305     Pt                              m_min_usable_size;
00306     bool                            m_ignore_child_resize;
00307     bool                            m_ignore_parent_resize;
00308     bool                            m_render_outline;
00309     Clr                             m_outline_color;
00310 
00311     friend class Wnd;
00312     friend struct SetMarginAction;
00313 
00314     friend class boost::serialization::access;
00315     template <class Archive>
00316     void serialize(Archive& ar, const unsigned int version);
00317 };
00318 
00319 } // namespace GG
00320 
00321 
00322 // template implementations
00323 template <class Archive>
00324 void GG::Layout::RowColParams::serialize(Archive& ar, const unsigned int version)
00325 {
00326     ar  & BOOST_SERIALIZATION_NVP(stretch)
00327         & BOOST_SERIALIZATION_NVP(min)
00328         & BOOST_SERIALIZATION_NVP(effective_min)
00329         & BOOST_SERIALIZATION_NVP(current_origin)
00330         & BOOST_SERIALIZATION_NVP(current_width);
00331 }
00332 
00333 template <class Archive>
00334 void GG::Layout::WndPosition::serialize(Archive& ar, const unsigned int version)
00335 {
00336     ar  & BOOST_SERIALIZATION_NVP(first_row)
00337         & BOOST_SERIALIZATION_NVP(first_column)
00338         & BOOST_SERIALIZATION_NVP(last_row)
00339         & BOOST_SERIALIZATION_NVP(last_column)
00340         & BOOST_SERIALIZATION_NVP(alignment)
00341         & BOOST_SERIALIZATION_NVP(original_ul)
00342         & BOOST_SERIALIZATION_NVP(original_size);
00343 }
00344 
00345 template <class Archive>
00346 void GG::Layout::serialize(Archive& ar, const unsigned int version)
00347 {
00348     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Wnd)
00349         & BOOST_SERIALIZATION_NVP(m_cells)
00350         & BOOST_SERIALIZATION_NVP(m_border_margin)
00351         & BOOST_SERIALIZATION_NVP(m_cell_margin)
00352         & BOOST_SERIALIZATION_NVP(m_row_params)
00353         & BOOST_SERIALIZATION_NVP(m_column_params)
00354         & BOOST_SERIALIZATION_NVP(m_wnd_positions)
00355         & BOOST_SERIALIZATION_NVP(m_ignore_child_resize)
00356         & BOOST_SERIALIZATION_NVP(m_render_outline)
00357         & BOOST_SERIALIZATION_NVP(m_outline_color)
00358         & BOOST_SERIALIZATION_NVP(m_min_usable_size);
00359 }
00360 
00361 #endif // _GG_Layout_h_

Generated on Sat Mar 26 07:08:37 2011 for GG by  doxygen 1.5.9