gbsizer.h 12 KB

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