]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gbsizer.h
Allow to customize wxGrid column auto-sizing.
[wxWidgets.git] / include / wx / gbsizer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gbsizer.h
3 // Purpose: wxGridBagSizer: A sizer that can lay out items in a grid,
4 // with items at specified cells, and with the option of row
5 // and/or column spanning
6 //
7 // Author: Robin Dunn
8 // Created: 03-Nov-2003
9 // RCS-ID: $Id$
10 // Copyright: (c) Robin Dunn
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #ifndef __WXGBSIZER_H__
15 #define __WXGBSIZER_H__
16
17 #include "wx/sizer.h"
18
19
20 //---------------------------------------------------------------------------
21 // Classes to represent a position in the grid and a size of an item in the
22 // grid, IOW, the number of rows and columns it occupies. I chose to use these
23 // instead of wxPoint and wxSize because they are (x,y) and usually pixel
24 // oriented while grids and tables are usually thought of as (row,col) so some
25 // confusion would definitely result in using wxPoint...
26 //
27 // NOTE: This should probably be refactored to a common RowCol data type which
28 // is used for this and also for wxGridCellCoords.
29 //---------------------------------------------------------------------------
30
31 class WXDLLIMPEXP_CORE wxGBPosition
32 {
33 public:
34 wxGBPosition() : m_row(0), m_col(0) {}
35 wxGBPosition(int row, int col) : m_row(row), m_col(col) {}
36
37 // default copy ctor and assignment operator are okay.
38
39 int GetRow() const { return m_row; }
40 int GetCol() const { return m_col; }
41 void SetRow(int row) { m_row = row; }
42 void SetCol(int col) { m_col = col; }
43
44 bool operator==(const wxGBPosition& p) const { return m_row == p.m_row && m_col == p.m_col; }
45 bool operator!=(const wxGBPosition& p) const { return !(*this == p); }
46
47 private:
48 int m_row;
49 int m_col;
50 };
51
52
53 class WXDLLIMPEXP_CORE wxGBSpan
54 {
55 public:
56 wxGBSpan() { Init(); }
57 wxGBSpan(int rowspan, int colspan)
58 {
59 // Initialize the members to valid values as not doing it may result in
60 // infinite loop in wxGBSizer code if the user passed 0 for any of
61 // them, see #12934.
62 Init();
63
64 SetRowspan(rowspan);
65 SetColspan(colspan);
66 }
67
68 // default copy ctor and assignment operator are okay.
69
70 // Factor constructor creating an invalid wxGBSpan: this is mostly supposed
71 // to be used as return value for functions returning wxGBSpan in case of
72 // errors.
73 static wxGBSpan Invalid()
74 {
75 return wxGBSpan(NULL);
76 }
77
78 int GetRowspan() const { return m_rowspan; }
79 int GetColspan() const { return m_colspan; }
80 void SetRowspan(int rowspan)
81 {
82 wxCHECK_RET( rowspan > 0, "Row span should be strictly positive" );
83
84 m_rowspan = rowspan;
85 }
86
87 void SetColspan(int colspan)
88 {
89 wxCHECK_RET( colspan > 0, "Column span should be strictly positive" );
90
91 m_colspan = colspan;
92 }
93
94 bool operator==(const wxGBSpan& o) const { return m_rowspan == o.m_rowspan && m_colspan == o.m_colspan; }
95 bool operator!=(const wxGBSpan& o) const { return !(*this == o); }
96
97 private:
98 // This private ctor is used by Invalid() only.
99 wxGBSpan(struct InvalidCtorTag*)
100 {
101 m_rowspan =
102 m_colspan = -1;
103 }
104
105 void Init()
106 {
107 m_rowspan =
108 m_colspan = 1;
109 }
110
111 int m_rowspan;
112 int m_colspan;
113 };
114
115
116 extern WXDLLIMPEXP_DATA_CORE(const wxGBSpan) wxDefaultSpan;
117
118
119 //---------------------------------------------------------------------------
120 // wxGBSizerItem
121 //---------------------------------------------------------------------------
122
123 class WXDLLIMPEXP_FWD_CORE wxGridBagSizer;
124
125
126 class WXDLLIMPEXP_CORE wxGBSizerItem : public wxSizerItem
127 {
128 public:
129 // spacer
130 wxGBSizerItem( int width,
131 int height,
132 const wxGBPosition& pos,
133 const wxGBSpan& span=wxDefaultSpan,
134 int flag=0,
135 int border=0,
136 wxObject* userData=NULL);
137
138 // window
139 wxGBSizerItem( wxWindow *window,
140 const wxGBPosition& pos,
141 const wxGBSpan& span=wxDefaultSpan,
142 int flag=0,
143 int border=0,
144 wxObject* userData=NULL );
145
146 // subsizer
147 wxGBSizerItem( wxSizer *sizer,
148 const wxGBPosition& pos,
149 const wxGBSpan& span=wxDefaultSpan,
150 int flag=0,
151 int border=0,
152 wxObject* userData=NULL );
153
154 // default ctor
155 wxGBSizerItem();
156
157
158 // Get the grid position of the item
159 wxGBPosition GetPos() const { return m_pos; }
160 void GetPos(int& row, int& col) const;
161
162 // Get the row and column spanning of the item
163 wxGBSpan GetSpan() const { return m_span; }
164 void GetSpan(int& rowspan, int& colspan) const;
165
166 // If the item is already a member of a sizer then first ensure that there
167 // is no other item that would intersect with this one at the new
168 // position, then set the new position. Returns true if the change is
169 // successful and after the next Layout the item will be moved.
170 bool SetPos( const wxGBPosition& pos );
171
172 // If the item is already a member of a sizer then first ensure that there
173 // is no other item that would intersect with this one with its new
174 // spanning size, then set the new spanning. Returns true if the change
175 // is successful and after the next Layout the item will be resized.
176 bool SetSpan( const wxGBSpan& span );
177
178 // Returns true if this item and the other item intersect
179 bool Intersects(const wxGBSizerItem& other);
180
181 // Returns true if the given pos/span would intersect with this item.
182 bool Intersects(const wxGBPosition& pos, const wxGBSpan& span);
183
184 // Get the row and column of the endpoint of this item
185 void GetEndPos(int& row, int& col);
186
187
188 wxGridBagSizer* GetGBSizer() const { return m_gbsizer; }
189 void SetGBSizer(wxGridBagSizer* sizer) { m_gbsizer = sizer; }
190
191
192 protected:
193 wxGBPosition m_pos;
194 wxGBSpan m_span;
195 wxGridBagSizer* m_gbsizer; // so SetPos/SetSpan can check for intersects
196
197
198 private:
199 DECLARE_DYNAMIC_CLASS(wxGBSizerItem)
200 wxDECLARE_NO_COPY_CLASS(wxGBSizerItem);
201 };
202
203
204 //---------------------------------------------------------------------------
205 // wxGridBagSizer
206 //---------------------------------------------------------------------------
207
208
209 class WXDLLIMPEXP_CORE wxGridBagSizer : public wxFlexGridSizer
210 {
211 public:
212 wxGridBagSizer(int vgap = 0, int hgap = 0 );
213
214 // The Add methods return true if the item was successfully placed at the
215 // given position, false if something was already there.
216 wxSizerItem* Add( wxWindow *window,
217 const wxGBPosition& pos,
218 const wxGBSpan& span = wxDefaultSpan,
219 int flag = 0,
220 int border = 0,
221 wxObject* userData = NULL );
222 wxSizerItem* Add( wxSizer *sizer,
223 const wxGBPosition& pos,
224 const wxGBSpan& span = wxDefaultSpan,
225 int flag = 0,
226 int border = 0,
227 wxObject* userData = NULL );
228 wxSizerItem* Add( int width,
229 int height,
230 const wxGBPosition& pos,
231 const wxGBSpan& span = wxDefaultSpan,
232 int flag = 0,
233 int border = 0,
234 wxObject* userData = NULL );
235 wxSizerItem* Add( wxGBSizerItem *item );
236
237
238 // Get/Set the size used for cells in the grid with no item.
239 wxSize GetEmptyCellSize() const { return m_emptyCellSize; }
240 void SetEmptyCellSize(const wxSize& sz) { m_emptyCellSize = sz; }
241
242 // Get the size of the specified cell, including hgap and vgap. Only
243 // valid after a Layout.
244 wxSize GetCellSize(int row, int col) const;
245
246 // Get the grid position of the specified item (non-recursive)
247 wxGBPosition GetItemPosition(wxWindow *window);
248 wxGBPosition GetItemPosition(wxSizer *sizer);
249 wxGBPosition GetItemPosition(size_t index);
250
251 // Set the grid position of the specified item. Returns true on success.
252 // If the move is not allowed (because an item is already there) then
253 // false is returned. (non-recursive)
254 bool SetItemPosition(wxWindow *window, const wxGBPosition& pos);
255 bool SetItemPosition(wxSizer *sizer, const wxGBPosition& pos);
256 bool SetItemPosition(size_t index, const wxGBPosition& pos);
257
258 // Get the row/col spanning of the specified item (non-recursive)
259 wxGBSpan GetItemSpan(wxWindow *window);
260 wxGBSpan GetItemSpan(wxSizer *sizer);
261 wxGBSpan GetItemSpan(size_t index);
262
263 // Set the row/col spanning of the specified item. Returns true on
264 // success. If the move is not allowed (because an item is already there)
265 // then false is returned. (non-recursive)
266 bool SetItemSpan(wxWindow *window, const wxGBSpan& span);
267 bool SetItemSpan(wxSizer *sizer, const wxGBSpan& span);
268 bool SetItemSpan(size_t index, const wxGBSpan& span);
269
270
271 // Find the sizer item for the given window or subsizer, returns NULL if
272 // not found. (non-recursive)
273 wxGBSizerItem* FindItem(wxWindow* window);
274 wxGBSizerItem* FindItem(wxSizer* sizer);
275
276
277 // Return the sizer item for the given grid cell, or NULL if there is no
278 // item at that position. (non-recursive)
279 wxGBSizerItem* FindItemAtPosition(const wxGBPosition& pos);
280
281
282 // Return the sizer item located at the point given in pt, or NULL if
283 // there is no item at that point. The (x,y) coordinates in pt correspond
284 // to the client coordinates of the window using the sizer for
285 // layout. (non-recursive)
286 wxGBSizerItem* FindItemAtPoint(const wxPoint& pt);
287
288
289 // Return the sizer item that has a matching user data (it only compares
290 // pointer values) or NULL if not found. (non-recursive)
291 wxGBSizerItem* FindItemWithData(const wxObject* userData);
292
293
294 // These are what make the sizer do size calculations and layout
295 virtual void RecalcSizes();
296 virtual wxSize CalcMin();
297
298
299 // Look at all items and see if any intersect (or would overlap) the given
300 // item. Returns true if so, false if there would be no overlap. If an
301 // excludeItem is given then it will not be checked for intersection, for
302 // example it may be the item we are checking the position of.
303 bool CheckForIntersection(wxGBSizerItem* item, wxGBSizerItem* excludeItem = NULL);
304 bool CheckForIntersection(const wxGBPosition& pos, const wxGBSpan& span, wxGBSizerItem* excludeItem = NULL);
305
306
307 // The Add base class virtuals should not be used with this class, but
308 // we'll try to make them automatically select a location for the item
309 // anyway.
310 virtual wxSizerItem* Add( wxWindow *window, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
311 virtual wxSizerItem* Add( wxSizer *sizer, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
312 virtual wxSizerItem* Add( int width, int height, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
313
314 // The Insert and Prepend base class virtuals that are not appropriate for
315 // this class and should not be used. Their implementation in this class
316 // simply fails.
317 virtual wxSizerItem* Add( wxSizerItem *item );
318 virtual wxSizerItem* Insert( size_t index, wxWindow *window, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
319 virtual wxSizerItem* Insert( size_t index, wxSizer *sizer, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
320 virtual wxSizerItem* Insert( size_t index, int width, int height, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
321 virtual wxSizerItem* Insert( size_t index, wxSizerItem *item );
322 virtual wxSizerItem* Prepend( wxWindow *window, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
323 virtual wxSizerItem* Prepend( wxSizer *sizer, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
324 virtual wxSizerItem* Prepend( int width, int height, int proportion = 0, int flag = 0, int border = 0, wxObject* userData = NULL );
325 virtual wxSizerItem* Prepend( wxSizerItem *item );
326
327
328 protected:
329 wxGBPosition FindEmptyCell();
330 void AdjustForOverflow();
331
332 wxSize m_emptyCellSize;
333
334
335 private:
336
337 DECLARE_CLASS(wxGridBagSizer)
338 wxDECLARE_NO_COPY_CLASS(wxGridBagSizer);
339 };
340
341 //---------------------------------------------------------------------------
342 #endif