grid.hpp

Go to the documentation of this file.
00001 /* $Id: grid.hpp 26684 2008-05-18 07:46:57Z mordante $ */
00002 /*
00003    copyright (C) 2008 by mark de wever <koraq@xs4all.nl>
00004    part of the battle for wesnoth project http://www.wesnoth.org/
00005 
00006    this program is free software; you can redistribute it and/or modify
00007    it under the terms of the gnu general public license version 2
00008    or at your option any later version.
00009    this program is distributed in the hope that it will be useful,
00010    but without any warranty.
00011 
00012    see the copying file for more details.
00013 */
00014 
00015 #ifndef __GUI_WIDGETS_GRID_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_GRID_HPP_INCLUDED__
00017 
00018 #include "gui/widgets/widget.hpp"
00019 #include "gui/widgets/control.hpp"
00020 
00021 #include <cassert>
00022 
00023 namespace gui2 {
00024 
00025 //! Base container class which needs to size children
00026 class tgrid : public virtual twidget
00027 {
00028 
00029 public:
00030     // ***** ***** FLAGS ***** *****
00031     static const unsigned VERTICAL_GROW_SEND_TO_CLIENT   = 1 << 0;
00032 
00033     static const unsigned VERTICAL_ALIGN_TOP             = 3 << 1;   
00034     static const unsigned VERTICAL_ALIGN_CENTER          = 2 << 1;   
00035     static const unsigned VERTICAL_ALIGN_BOTTOM          = 1 << 1;   
00036 
00037     static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT = 1 << 3;
00038 
00039     static const unsigned HORIZONTAL_ALIGN_LEFT          = 3 << 4;   
00040     static const unsigned HORIZONTAL_ALIGN_CENTER        = 2 << 4;   
00041     static const unsigned HORIZONTAL_ALIGN_RIGHT         = 1 << 4;   
00042 
00043     static const unsigned BORDER_TOP                     = 1 << 6;
00044     static const unsigned BORDER_BOTTOM                  = 1 << 7;
00045     static const unsigned BORDER_LEFT                    = 1 << 8;
00046     static const unsigned BORDER_RIGHT                   = 1 << 9;
00047 
00048     
00049     tgrid(const unsigned rows = 0, const unsigned cols = 0); 
00050 
00051     virtual ~tgrid();
00052 
00053     void add_child(twidget* widget, const unsigned row, 
00054         const unsigned col, const unsigned flags, const unsigned border_size);
00055     
00056     void set_rows(const unsigned rows);
00057     unsigned int get_rows() const { return rows_; }
00058 
00059     /**
00060      * Addes a row to end of the grid.
00061      *
00062      * @param count               Number of rows to add, should be > 0.
00063      *
00064      * @returns                   The row number of the first row added.
00065      */
00066     unsigned add_row(const unsigned count = 1);
00067 
00068     void set_cols(const unsigned cols);
00069     unsigned int get_cols() const { return cols_; }
00070 
00071     void set_rows_cols(const unsigned rows, const unsigned cols);
00072 
00073     void set_row_grow_factor(const unsigned row, const unsigned factor)
00074     { 
00075         assert(row < row_grow_factor_.size()); 
00076         row_grow_factor_[row] = factor; 
00077         set_dirty(); 
00078     } 
00079 
00080     void set_col_grow_factor(const unsigned col, const unsigned factor)
00081     { 
00082         assert(col< col_grow_factor_.size());
00083         col_grow_factor_[col] = factor; 
00084         set_dirty(); 
00085     }
00086 
00087     void remove_child(const unsigned row, const unsigned col);
00088     void remove_child(const std::string& id, const bool find_all = false);
00089 
00090     /**
00091      * Activates all children.
00092      *
00093      * If a child inherits from tcontrol or is a tgrid it will call
00094      * set_active() for the child otherwise it ignores the widget.
00095      *
00096      * @param active              Parameter for set_active.
00097      */
00098     void set_active(const bool active);
00099 
00100     /** Inherited from twidget. */
00101     bool has_vertical_scrollbar() const;
00102 
00103     //! Inherited from twidget.
00104     tpoint get_minimum_size() const;
00105     tpoint get_best_size() const;
00106     tpoint get_maximum_size() const;
00107 
00108     //! Inherited from twidget.
00109     void set_size(const SDL_Rect& rect);
00110 
00111     /** Inherited from twidget. */
00112     twidget* find_widget(const tpoint& coordinate, const bool must_be_active);
00113 
00114     /** Inherited from twidget. */
00115     const twidget* find_widget(const tpoint& coordinate, 
00116             const bool must_be_active) const;
00117 
00118     /** Inherited from twidget.*/
00119     twidget* find_widget(const std::string& id, const bool must_be_active);
00120 
00121     /** Inherited from twidget.*/
00122     const twidget* find_widget(const std::string& id, 
00123             const bool must_be_active) const;
00124 
00125     /** Inherited from twidget.*/
00126     bool has_widget(const twidget* widget) const;
00127 
00128     //! Inherited from twidget.
00129     void draw(surface& surface);
00130 
00131     /** Returns the widget in the selected cell. */
00132     const twidget* widget(const unsigned row, const unsigned col) const
00133         { return child(row, col).widget(); }
00134 
00135     /** Returns the widget in the selected cell. */
00136     twidget* widget(const unsigned row, const unsigned col) 
00137         { return child(row, col).widget(); }
00138 
00139 private:
00140     class tchild 
00141     {
00142     public:
00143         tchild() : 
00144             id_(),
00145             flags_(0),
00146             border_size_(0),
00147             widget_(0),
00148             best_size_(0, 0),
00149             minimum_size_(0, 0),
00150             maximum_size_(0, 0),
00151             clip_()
00152 
00153             // Fixme make a class wo we can store some properties in the cache 
00154             // regarding size etc.
00155             {}
00156     
00157         const std::string& id() const { return id_; }
00158         void set_id(const std::string& id) { id_ = id; }
00159 
00160         unsigned get_flags() const { return flags_; }
00161         void set_flags(const unsigned flags) { flags_ = flags; set_dirty(); }
00162 
00163         unsigned get_border_size() const { return border_size_; }
00164         void set_border_size(const unsigned border_size) 
00165             {  border_size_ = border_size; set_dirty(); }
00166 
00167         const twidget* widget() const { return widget_; }
00168         twidget* widget() { return widget_; }
00169 
00170         void set_widget(twidget* widget) { widget_ = widget; set_dirty(); }
00171 
00172         //! Returns the best size for the cell.
00173         tpoint get_best_size() const;
00174 
00175         //! Returns the minimum size for the cell.
00176         tpoint get_minimum_size() const;
00177 
00178         //! Returns the maximum size for the cell.
00179         tpoint get_maximum_size() const;
00180 
00181         void set_size(tpoint orig, tpoint size);
00182 
00183     private:
00184         //! The id of the widget if it has a widget.
00185         std::string id_;
00186 
00187         //! The flags for the border and cell setup.
00188         unsigned flags_;
00189 
00190         //! The size of the border, the actual configuration of the border
00191         //! is determined by the flags.
00192         unsigned border_size_;
00193 
00194         //! Pointer to the widget. FIXME who owns the widget....
00195         twidget* widget_;
00196 
00197         //! The best size for this cell, determined by the best size
00198         //! of the widget and the border_size_ and flags_.
00199         mutable tpoint best_size_;
00200 
00201         //! The minimum size for this cell, like best_size_.
00202         mutable tpoint minimum_size_;
00203 
00204         //! The maximum size for this cell, like best_size_.
00205         mutable tpoint maximum_size_;
00206 
00207         //! Returns the space needed for the border.
00208         tpoint border_space() const;
00209 
00210         //! The clipping area for the widget. This is also the size of 
00211         //! the container.
00212         SDL_Rect clip_;
00213 
00214         //! Sets the calculations to be dirty, this means all caches
00215         //! are simply cleared.
00216         void set_dirty()  // FIXME rename to clear cache??
00217         { 
00218             best_size_ = tpoint(0, 0); 
00219             minimum_size_ = tpoint(0, 0); 
00220             maximum_size_ = tpoint(0, 0);
00221         }
00222 
00223     }; // class tchild
00224 
00225 public:
00226     class iterator 
00227     {
00228 
00229     public:
00230 
00231         iterator(std::vector<tchild>::iterator itor) :
00232             itor_(itor) 
00233             {}
00234 
00235         iterator operator++() { return iterator(++itor_); }
00236         iterator operator--() { return iterator(--itor_); }
00237         twidget* operator->() { return itor_->widget(); }
00238         twidget* operator*() { return itor_->widget(); }
00239 
00240         bool operator!=(const iterator& i) const
00241             { return i.itor_ != this->itor_; }
00242 
00243     private:
00244         std::vector<tchild>::iterator itor_;
00245 
00246     };
00247 
00248     iterator begin() { return iterator(children_.begin()); }
00249     iterator end() { return iterator(children_.end()); }
00250 
00251 private:
00252     //! The number of rows / columns.
00253     unsigned rows_;
00254     unsigned cols_;
00255 
00256     //! The optimal row heights / col widths.
00257     mutable std::vector<unsigned> best_row_height_;
00258     mutable std::vector<unsigned> best_col_width_;
00259 
00260     //! The minimal row heights / col widths.
00261     mutable std::vector<unsigned> minimum_row_height_;
00262     mutable std::vector<unsigned> minimum_col_width_;
00263 
00264     //! The row heights / col widths currently used.
00265     std::vector<unsigned> row_height_;
00266     std::vector<unsigned> col_width_;
00267 
00268     //! The resize factors for rows / cols.
00269     std::vector<unsigned> row_grow_factor_;
00270     std::vector<unsigned> col_grow_factor_;
00271 
00272     //! Contains all cells.
00273     std::vector<tchild> children_;
00274     const tchild& child(const unsigned row, const unsigned col) const
00275         { return children_[rows_ * col + row]; }
00276     tchild& child(const unsigned row, const unsigned col)
00277         { clear_cache(); return children_[rows_ * col + row]; }
00278 
00279     void clear_cache();
00280 
00281     void layout(const tpoint& origin);
00282 
00283     //! Helper function to get the best or minimum size.
00284     tpoint get_size(const std::string& id, std::vector<unsigned>& width, 
00285         std::vector<unsigned>& height, tpoint (tchild::*size_proc)() const) const;
00286 };
00287 
00288 } // namespace gui2
00289 
00290 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs