00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00026 class tgrid : public virtual twidget
00027 {
00028
00029 public:
00030
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
00061
00062
00063
00064
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
00092
00093
00094
00095
00096
00097
00098 void set_active(const bool active);
00099
00100
00101 bool has_vertical_scrollbar() const;
00102
00103
00104 tpoint get_minimum_size() const;
00105 tpoint get_best_size() const;
00106 tpoint get_maximum_size() const;
00107
00108
00109 void set_size(const SDL_Rect& rect);
00110
00111
00112 twidget* find_widget(const tpoint& coordinate, const bool must_be_active);
00113
00114
00115 const twidget* find_widget(const tpoint& coordinate,
00116 const bool must_be_active) const;
00117
00118
00119 twidget* find_widget(const std::string& id, const bool must_be_active);
00120
00121
00122 const twidget* find_widget(const std::string& id,
00123 const bool must_be_active) const;
00124
00125
00126 bool has_widget(const twidget* widget) const;
00127
00128
00129 void draw(surface& surface);
00130
00131
00132 const twidget* widget(const unsigned row, const unsigned col) const
00133 { return child(row, col).widget(); }
00134
00135
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
00154
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
00173 tpoint get_best_size() const;
00174
00175
00176 tpoint get_minimum_size() const;
00177
00178
00179 tpoint get_maximum_size() const;
00180
00181 void set_size(tpoint orig, tpoint size);
00182
00183 private:
00184
00185 std::string id_;
00186
00187
00188 unsigned flags_;
00189
00190
00191
00192 unsigned border_size_;
00193
00194
00195 twidget* widget_;
00196
00197
00198
00199 mutable tpoint best_size_;
00200
00201
00202 mutable tpoint minimum_size_;
00203
00204
00205 mutable tpoint maximum_size_;
00206
00207
00208 tpoint border_space() const;
00209
00210
00211
00212 SDL_Rect clip_;
00213
00214
00215
00216 void set_dirty()
00217 {
00218 best_size_ = tpoint(0, 0);
00219 minimum_size_ = tpoint(0, 0);
00220 maximum_size_ = tpoint(0, 0);
00221 }
00222
00223 };
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
00253 unsigned rows_;
00254 unsigned cols_;
00255
00256
00257 mutable std::vector<unsigned> best_row_height_;
00258 mutable std::vector<unsigned> best_col_width_;
00259
00260
00261 mutable std::vector<unsigned> minimum_row_height_;
00262 mutable std::vector<unsigned> minimum_col_width_;
00263
00264
00265 std::vector<unsigned> row_height_;
00266 std::vector<unsigned> col_width_;
00267
00268
00269 std::vector<unsigned> row_grow_factor_;
00270 std::vector<unsigned> col_grow_factor_;
00271
00272
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
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 }
00289
00290 #endif