00001 /* 00002 libwt - Vassilis Virvilis Toolkit - a widget library 00003 Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm> 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 00007 License as published by the Free Software Foundation; either 00008 version 2.1 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 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #ifndef WT_GRIDLAYOUT_H 00022 #define WT_GRIDLAYOUT_H 00023 00024 #include <queue> 00025 00026 #include <wt/matrix.h> 00027 #include <wt/layout.h> 00028 00029 namespace Wt { 00030 00031 class MatrixLayoutItemValue { 00032 public: 00033 typedef enum { 00034 Original, 00035 Reference 00036 } Type; 00037 00038 MatrixLayoutItemValue(int type = Original, 00039 int row_span = 1, int col_span = 1) : 00040 type_(type), row_span_(row_span), col_span_(col_span) {} 00041 00042 bool isReference() const { 00043 return (type_ == Reference); 00044 } 00045 00046 int type() const { 00047 return type_; 00048 } 00049 00050 int rowSpan() const { 00051 return row_span_; 00052 } 00053 00054 int columnSpan() const { 00055 return col_span_; 00056 } 00057 00058 bool isMultiCell() const { 00059 return col_span_ > 1 || row_span_ > 1; 00060 } 00061 00062 private: 00063 int type_; 00064 int row_span_; 00065 int col_span_; 00066 }; 00067 00068 typedef LayoutItemCompound<MatrixLayoutItemValue> MatrixLayoutItemCompound; 00069 typedef Matrix<MatrixLayoutItemCompound> MatrixLayoutItemMatrix; 00070 typedef LayoutStdContainer<MatrixLayoutItemMatrix> GridLayoutBase; 00071 00072 class GridLayoutIterator : public LayoutStdContainerIterator<MatrixLayoutItemMatrix> { 00073 public: 00074 GridLayoutIterator(MatrixLayoutItemMatrix& matrix) : 00075 LayoutStdContainerIterator<MatrixLayoutItemMatrix>(matrix) {} 00076 private: 00077 }; 00078 00079 class GridLayout : public GridLayoutBase { 00080 friend class Grid; 00081 // equivalent inside class shortcut 00082 typedef MatrixLayoutItemMatrix::Edge Edge; 00083 00084 public: 00085 GridLayout(Widget *parent, int margin = 0, int spacing = -1, 00086 const std::string &name = "GridLayout::anon"); 00087 00088 GridLayout(Layout * parent_layout, int margin = 0, 00089 int spacing = -1, const std::string &name = "GridLayout::anon"); 00090 00091 GridLayout(int spacing = -1, 00092 const std::string &name = "GridLayout::anon"); 00093 00094 void insert(const LayoutItem *li, int row, int column); 00095 void insert(const LayoutItem *li, int row_from, 00096 int row_to, int column_from, int column_to); 00097 virtual void addItem(LayoutItem *li); 00098 00099 virtual void setGeometry(const Rect& r); 00100 00101 virtual void setRowStretch(int row, int stretch); 00102 virtual void setColStretch(int col, int stretch); 00103 00104 int rowStretch (int row) const { 00105 return row_stretch[row]; 00106 } 00107 int colStretch (int col) const { 00108 return column_stretch[col]; 00109 } 00110 00111 protected: 00112 virtual void validate(); 00113 00114 int secondaryEdges() const { 00115 return isWidthPrimaryLength() ? numCols() : numRows(); 00116 } 00117 00118 std::vector<int>& secondaryStretch() { 00119 return isWidthPrimaryLength() ? column_stretch : row_stretch; 00120 } 00121 00122 std::vector<int>& secondaryEffectiveStretch() { 00123 return isWidthPrimaryLength() ? 00124 column_stretch_effective : row_stretch_effective; 00125 } 00126 00127 std::vector<int>& secondaryExtraSpace() { 00128 return isWidthPrimaryLength() ? column_extra_space : row_extra_space; 00129 } 00130 00131 std::vector<int>& primaryCellItemLength() { 00132 return isWidthPrimaryLength() ? column_width : row_height; 00133 } 00134 00135 std::vector<int>& primaryCellLength() { 00136 return isWidthPrimaryLength() ? 00137 container_column_width : container_row_height; 00138 } 00139 00140 bool mayGrowPrimally(int edge_index) const { 00141 return isWidthPrimaryLength() ? 00142 may_grow_horizontally[edge_index] : 00143 may_grow_vertically[edge_index]; 00144 } 00145 00146 bool expandingPrimally(int edge_index) const { 00147 return isWidthPrimaryLength() ? 00148 expanding_horizontally[edge_index] : 00149 expanding_vertically[edge_index]; 00150 } 00151 00152 std::vector<int>& primaryMinLength() { 00153 return isWidthPrimaryLength() ? max_min_width : max_min_height; 00154 } 00155 00156 std::vector<int>& primaryHintLength() { 00157 return isWidthPrimaryLength() ? max_hint_width : max_hint_height; 00158 } 00159 00160 int preferredLength(int edge_index, bool expand) { 00161 return (expand) ? primaryHintLength()[edge_index] : 00162 primaryMinLength()[edge_index]; 00163 } 00164 00165 class CellSpan { 00166 public: 00167 CellSpan(const LayoutItem *li = 0, 00168 int row = -1, int column = -1, 00169 int row_span = 1, int column_span = 1) : 00170 li_(li), 00171 row_(row), 00172 column_(column), 00173 row_span_(row_span), 00174 column_span_(column_span) {} 00175 00176 void invalidate() { 00177 li_ = 0; 00178 row_ = -1; 00179 column_ = -1; 00180 } 00181 00182 bool isValid() const { 00183 return row_ >= 0 && column_ >= 0; 00184 } 00185 00186 void setLayoutItem(const LayoutItem *li) { 00187 li_ = li; 00188 } 00189 00190 const LayoutItem *layoutItem() const { 00191 return li_; 00192 } 00193 00194 void setSpan(int row, int column, 00195 int row_span = 1, int column_span = 1) { 00196 row_ = row; 00197 column_ = column; 00198 row_span_ = row_span; 00199 column_span_ = column_span; 00200 } 00201 00202 void setFromTo(int row_from, int row_to, 00203 int column_from, int column_to) { 00204 row_ = row_from; 00205 column_ = column_from; 00206 row_span_ = row_to - row_from + 1; 00207 column_span_ = column_to - column_from + 1; 00208 } 00209 00210 int rowSpan() const { 00211 return row_span_; 00212 } 00213 00214 int columnSpan() const { 00215 return column_span_; 00216 } 00217 00218 int rowFrom() const { 00219 return row_; 00220 } 00221 00222 int columnFrom() const { 00223 return column_; 00224 } 00225 00226 int rowTo() const { 00227 return row_ + row_span_ - 1; 00228 } 00229 00230 int columnTo() const { 00231 return column_ + column_span_ - 1; 00232 } 00233 00234 private: 00235 const LayoutItem *li_; 00236 int row_; 00237 int column_; 00238 int row_span_; 00239 int column_span_; 00240 }; 00241 00242 std::queue<CellSpan> cellSpanQueue; 00243 00244 private: 00245 void init(); 00246 void onGridSizeChange(int rows_old, int cols_old, int rows, int cols); 00247 void calcEdge(const int available_space, bool width_as_primary); 00248 00249 std::vector<int> max_min_width; 00250 std::vector<int> max_min_height; 00251 std::vector<int> max_hint_width; 00252 std::vector<int> max_hint_height; 00253 std::vector<bool> row_empty; 00254 std::vector<bool> col_empty; 00255 00256 std::vector<bool> may_grow_horizontally; 00257 std::vector<bool> may_grow_vertically; 00258 std::vector<bool> expanding_horizontally; 00259 std::vector<bool> expanding_vertically; 00260 00261 std::vector<int> row_stretch; 00262 std::vector<int> column_stretch; 00263 std::vector<int> row_stretch_effective; 00264 std::vector<int> column_stretch_effective; 00265 std::vector<int> row_extra_space; 00266 std::vector<int> column_extra_space; 00267 std::vector<int> row_height; 00268 std::vector<int> column_width; 00269 std::vector<int> container_row_height; 00270 std::vector<int> container_column_width; 00271 }; 00272 00273 } // namespace 00274 00275 #endif // WT_GRIDLAYOUT_H
This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.