sizer.h 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/sizer.h
  3. // Purpose: provide wxSizer class for layout
  4. // Author: Robert Roebling and Robin Dunn
  5. // Modified by: Ron Lee, Vadim Zeitlin (wxSizerFlags)
  6. // Created:
  7. // Copyright: (c) Robin Dunn, Robert Roebling
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef __WXSIZER_H__
  11. #define __WXSIZER_H__
  12. #include "wx/defs.h"
  13. #include "wx/window.h"
  14. //---------------------------------------------------------------------------
  15. // classes
  16. //---------------------------------------------------------------------------
  17. class WXDLLIMPEXP_FWD_CORE wxButton;
  18. class WXDLLIMPEXP_FWD_CORE wxBoxSizer;
  19. class WXDLLIMPEXP_FWD_CORE wxSizerItem;
  20. class WXDLLIMPEXP_FWD_CORE wxSizer;
  21. #ifndef wxUSE_BORDER_BY_DEFAULT
  22. #ifdef __SMARTPHONE__
  23. // no borders by default on limited size screen
  24. #define wxUSE_BORDER_BY_DEFAULT 0
  25. #else
  26. #define wxUSE_BORDER_BY_DEFAULT 1
  27. #endif
  28. #endif
  29. // ----------------------------------------------------------------------------
  30. // wxSizerFlags: flags used for an item in the sizer
  31. // ----------------------------------------------------------------------------
  32. class WXDLLIMPEXP_CORE wxSizerFlags
  33. {
  34. public:
  35. // construct the flags object initialized with the given proportion (0 by
  36. // default)
  37. wxSizerFlags(int proportion = 0) : m_proportion(proportion)
  38. {
  39. m_flags = 0;
  40. m_borderInPixels = 0;
  41. }
  42. // setters for all sizer flags, they all return the object itself so that
  43. // calls to them can be chained
  44. wxSizerFlags& Proportion(int proportion)
  45. {
  46. m_proportion = proportion;
  47. return *this;
  48. }
  49. wxSizerFlags& Expand()
  50. {
  51. m_flags |= wxEXPAND;
  52. return *this;
  53. }
  54. // notice that Align() replaces the current alignment flags, use specific
  55. // methods below such as Top(), Left() &c if you want to set just the
  56. // vertical or horizontal alignment
  57. wxSizerFlags& Align(int alignment) // combination of wxAlignment values
  58. {
  59. m_flags &= ~wxALIGN_MASK;
  60. m_flags |= alignment;
  61. return *this;
  62. }
  63. // some shortcuts for Align()
  64. wxSizerFlags& Centre() { return Align(wxALIGN_CENTRE); }
  65. wxSizerFlags& Center() { return Centre(); }
  66. wxSizerFlags& Top()
  67. {
  68. m_flags &= ~(wxALIGN_BOTTOM | wxALIGN_CENTRE_VERTICAL);
  69. return *this;
  70. }
  71. wxSizerFlags& Left()
  72. {
  73. m_flags &= ~(wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL);
  74. return *this;
  75. }
  76. wxSizerFlags& Right()
  77. {
  78. m_flags = (m_flags & ~wxALIGN_CENTRE_HORIZONTAL) | wxALIGN_RIGHT;
  79. return *this;
  80. }
  81. wxSizerFlags& Bottom()
  82. {
  83. m_flags = (m_flags & ~wxALIGN_CENTRE_VERTICAL) | wxALIGN_BOTTOM;
  84. return *this;
  85. }
  86. // default border size used by Border() below
  87. static int GetDefaultBorder()
  88. {
  89. #if wxUSE_BORDER_BY_DEFAULT
  90. #ifdef __WXGTK20__
  91. // GNOME HIG says to use 6px as the base unit:
  92. // http://library.gnome.org/devel/hig-book/stable/design-window.html.en
  93. return 6;
  94. #else
  95. // FIXME: default border size shouldn't be hardcoded and at the very
  96. // least they should depend on the current font size
  97. return 5;
  98. #endif
  99. #else
  100. return 0;
  101. #endif
  102. }
  103. wxSizerFlags& Border(int direction, int borderInPixels)
  104. {
  105. wxCHECK_MSG( !(direction & ~wxALL), *this,
  106. wxS("direction must be a combination of wxDirection ")
  107. wxS("enum values.") );
  108. m_flags &= ~wxALL;
  109. m_flags |= direction;
  110. m_borderInPixels = borderInPixels;
  111. return *this;
  112. }
  113. wxSizerFlags& Border(int direction = wxALL)
  114. {
  115. #if wxUSE_BORDER_BY_DEFAULT
  116. return Border(direction, GetDefaultBorder());
  117. #else
  118. // no borders by default on limited size screen
  119. wxUnusedVar(direction);
  120. return *this;
  121. #endif
  122. }
  123. wxSizerFlags& DoubleBorder(int direction = wxALL)
  124. {
  125. #if wxUSE_BORDER_BY_DEFAULT
  126. return Border(direction, 2*GetDefaultBorder());
  127. #else
  128. wxUnusedVar(direction);
  129. return *this;
  130. #endif
  131. }
  132. wxSizerFlags& TripleBorder(int direction = wxALL)
  133. {
  134. #if wxUSE_BORDER_BY_DEFAULT
  135. return Border(direction, 3*GetDefaultBorder());
  136. #else
  137. wxUnusedVar(direction);
  138. return *this;
  139. #endif
  140. }
  141. wxSizerFlags& HorzBorder()
  142. {
  143. #if wxUSE_BORDER_BY_DEFAULT
  144. return Border(wxLEFT | wxRIGHT, GetDefaultBorder());
  145. #else
  146. return *this;
  147. #endif
  148. }
  149. wxSizerFlags& DoubleHorzBorder()
  150. {
  151. #if wxUSE_BORDER_BY_DEFAULT
  152. return Border(wxLEFT | wxRIGHT, 2*GetDefaultBorder());
  153. #else
  154. return *this;
  155. #endif
  156. }
  157. // setters for the others flags
  158. wxSizerFlags& Shaped()
  159. {
  160. m_flags |= wxSHAPED;
  161. return *this;
  162. }
  163. wxSizerFlags& FixedMinSize()
  164. {
  165. m_flags |= wxFIXED_MINSIZE;
  166. return *this;
  167. }
  168. // makes the item ignore window's visibility status
  169. wxSizerFlags& ReserveSpaceEvenIfHidden()
  170. {
  171. m_flags |= wxRESERVE_SPACE_EVEN_IF_HIDDEN;
  172. return *this;
  173. }
  174. // accessors for wxSizer only
  175. int GetProportion() const { return m_proportion; }
  176. int GetFlags() const { return m_flags; }
  177. int GetBorderInPixels() const { return m_borderInPixels; }
  178. private:
  179. int m_proportion;
  180. int m_flags;
  181. int m_borderInPixels;
  182. };
  183. // ----------------------------------------------------------------------------
  184. // wxSizerSpacer: used by wxSizerItem to represent a spacer
  185. // ----------------------------------------------------------------------------
  186. class WXDLLIMPEXP_CORE wxSizerSpacer
  187. {
  188. public:
  189. wxSizerSpacer(const wxSize& size) : m_size(size), m_isShown(true) { }
  190. void SetSize(const wxSize& size) { m_size = size; }
  191. const wxSize& GetSize() const { return m_size; }
  192. void Show(bool show) { m_isShown = show; }
  193. bool IsShown() const { return m_isShown; }
  194. private:
  195. // the size, in pixel
  196. wxSize m_size;
  197. // is the spacer currently shown?
  198. bool m_isShown;
  199. };
  200. // ----------------------------------------------------------------------------
  201. // wxSizerItem
  202. // ----------------------------------------------------------------------------
  203. class WXDLLIMPEXP_CORE wxSizerItem : public wxObject
  204. {
  205. public:
  206. // window
  207. wxSizerItem( wxWindow *window,
  208. int proportion=0,
  209. int flag=0,
  210. int border=0,
  211. wxObject* userData=NULL );
  212. // window with flags
  213. wxSizerItem(wxWindow *window, const wxSizerFlags& flags)
  214. {
  215. Init(flags);
  216. DoSetWindow(window);
  217. }
  218. // subsizer
  219. wxSizerItem( wxSizer *sizer,
  220. int proportion=0,
  221. int flag=0,
  222. int border=0,
  223. wxObject* userData=NULL );
  224. // sizer with flags
  225. wxSizerItem(wxSizer *sizer, const wxSizerFlags& flags)
  226. {
  227. Init(flags);
  228. DoSetSizer(sizer);
  229. }
  230. // spacer
  231. wxSizerItem( int width,
  232. int height,
  233. int proportion=0,
  234. int flag=0,
  235. int border=0,
  236. wxObject* userData=NULL);
  237. // spacer with flags
  238. wxSizerItem(int width, int height, const wxSizerFlags& flags)
  239. {
  240. Init(flags);
  241. DoSetSpacer(wxSize(width, height));
  242. }
  243. wxSizerItem();
  244. virtual ~wxSizerItem();
  245. virtual void DeleteWindows();
  246. // Enable deleting the SizerItem without destroying the contained sizer.
  247. void DetachSizer() { m_sizer = NULL; }
  248. virtual wxSize GetSize() const;
  249. virtual wxSize CalcMin();
  250. virtual void SetDimension( const wxPoint& pos, const wxSize& size );
  251. wxSize GetMinSize() const
  252. { return m_minSize; }
  253. wxSize GetMinSizeWithBorder() const;
  254. wxSize GetMaxSize() const
  255. { return IsWindow() ? m_window->GetMaxSize() : wxDefaultSize; }
  256. wxSize GetMaxSizeWithBorder() const;
  257. void SetMinSize(const wxSize& size)
  258. {
  259. if ( IsWindow() )
  260. m_window->SetMinSize(size);
  261. m_minSize = size;
  262. }
  263. void SetMinSize( int x, int y )
  264. { SetMinSize(wxSize(x, y)); }
  265. void SetInitSize( int x, int y )
  266. { SetMinSize(wxSize(x, y)); }
  267. // if either of dimensions is zero, ratio is assumed to be 1
  268. // to avoid "divide by zero" errors
  269. void SetRatio(int width, int height)
  270. { m_ratio = (width && height) ? ((float) width / (float) height) : 1; }
  271. void SetRatio(const wxSize& size)
  272. { SetRatio(size.x, size.y); }
  273. void SetRatio(float ratio)
  274. { m_ratio = ratio; }
  275. float GetRatio() const
  276. { return m_ratio; }
  277. virtual wxRect GetRect() { return m_rect; }
  278. // set a sizer item id (different from a window id, all sizer items,
  279. // including spacers, can have an associated id)
  280. void SetId(int id) { m_id = id; }
  281. int GetId() const { return m_id; }
  282. bool IsWindow() const { return m_kind == Item_Window; }
  283. bool IsSizer() const { return m_kind == Item_Sizer; }
  284. bool IsSpacer() const { return m_kind == Item_Spacer; }
  285. #if WXWIN_COMPATIBILITY_2_6
  286. // Deprecated in 2.6, use {G,S}etProportion instead.
  287. wxDEPRECATED( void SetOption( int option ) );
  288. wxDEPRECATED( int GetOption() const );
  289. #endif // WXWIN_COMPATIBILITY_2_6
  290. void SetProportion( int proportion )
  291. { m_proportion = proportion; }
  292. int GetProportion() const
  293. { return m_proportion; }
  294. void SetFlag( int flag )
  295. { m_flag = flag; }
  296. int GetFlag() const
  297. { return m_flag; }
  298. void SetBorder( int border )
  299. { m_border = border; }
  300. int GetBorder() const
  301. { return m_border; }
  302. wxWindow *GetWindow() const
  303. { return m_kind == Item_Window ? m_window : NULL; }
  304. wxSizer *GetSizer() const
  305. { return m_kind == Item_Sizer ? m_sizer : NULL; }
  306. wxSize GetSpacer() const;
  307. // This function behaves obviously for the windows and spacers but for the
  308. // sizers it returns true if any sizer element is shown and only returns
  309. // false if all of them are hidden. Also, it always returns true if
  310. // wxRESERVE_SPACE_EVEN_IF_HIDDEN flag was used.
  311. bool IsShown() const;
  312. void Show(bool show);
  313. void SetUserData(wxObject* userData)
  314. { delete m_userData; m_userData = userData; }
  315. wxObject* GetUserData() const
  316. { return m_userData; }
  317. wxPoint GetPosition() const
  318. { return m_pos; }
  319. // Called once the first component of an item has been decided. This is
  320. // used in algorithms that depend on knowing the size in one direction
  321. // before the min size in the other direction can be known.
  322. // Returns true if it made use of the information (and min size was changed).
  323. bool InformFirstDirection( int direction, int size, int availableOtherDir=-1 );
  324. // these functions delete the current contents of the item if it's a sizer
  325. // or a spacer but not if it is a window
  326. void AssignWindow(wxWindow *window)
  327. {
  328. Free();
  329. DoSetWindow(window);
  330. }
  331. void AssignSizer(wxSizer *sizer)
  332. {
  333. Free();
  334. DoSetSizer(sizer);
  335. }
  336. void AssignSpacer(const wxSize& size)
  337. {
  338. Free();
  339. DoSetSpacer(size);
  340. }
  341. void AssignSpacer(int w, int h) { AssignSpacer(wxSize(w, h)); }
  342. #if WXWIN_COMPATIBILITY_2_8
  343. // these functions do not free the old sizer/spacer and so can easily
  344. // provoke the memory leaks and so shouldn't be used, use Assign() instead
  345. wxDEPRECATED( void SetWindow(wxWindow *window) );
  346. wxDEPRECATED( void SetSizer(wxSizer *sizer) );
  347. wxDEPRECATED( void SetSpacer(const wxSize& size) );
  348. wxDEPRECATED( void SetSpacer(int width, int height) );
  349. #endif // WXWIN_COMPATIBILITY_2_8
  350. protected:
  351. // common part of several ctors
  352. void Init() { m_userData = NULL; m_kind = Item_None; }
  353. // common part of ctors taking wxSizerFlags
  354. void Init(const wxSizerFlags& flags);
  355. // free current contents
  356. void Free();
  357. // common parts of Set/AssignXXX()
  358. void DoSetWindow(wxWindow *window);
  359. void DoSetSizer(wxSizer *sizer);
  360. void DoSetSpacer(const wxSize& size);
  361. // Add the border specified for this item to the given size
  362. // if it's != wxDefaultSize, just return wxDefaultSize otherwise.
  363. wxSize AddBorderToSize(const wxSize& size) const;
  364. // discriminated union: depending on m_kind one of the fields is valid
  365. enum
  366. {
  367. Item_None,
  368. Item_Window,
  369. Item_Sizer,
  370. Item_Spacer,
  371. Item_Max
  372. } m_kind;
  373. union
  374. {
  375. wxWindow *m_window;
  376. wxSizer *m_sizer;
  377. wxSizerSpacer *m_spacer;
  378. };
  379. wxPoint m_pos;
  380. wxSize m_minSize;
  381. int m_proportion;
  382. int m_border;
  383. int m_flag;
  384. int m_id;
  385. // on screen rectangle of this item (not including borders)
  386. wxRect m_rect;
  387. // Aspect ratio can always be calculated from m_size,
  388. // but this would cause precision loss when the window
  389. // is shrunk. It is safer to preserve the initial value.
  390. float m_ratio;
  391. wxObject *m_userData;
  392. private:
  393. DECLARE_CLASS(wxSizerItem)
  394. wxDECLARE_NO_COPY_CLASS(wxSizerItem);
  395. };
  396. WX_DECLARE_EXPORTED_LIST( wxSizerItem, wxSizerItemList );
  397. //---------------------------------------------------------------------------
  398. // wxSizer
  399. //---------------------------------------------------------------------------
  400. class WXDLLIMPEXP_CORE wxSizer: public wxObject, public wxClientDataContainer
  401. {
  402. public:
  403. wxSizer() { m_containingWindow = NULL; }
  404. virtual ~wxSizer();
  405. // methods for adding elements to the sizer: there are Add/Insert/Prepend
  406. // overloads for each of window/sizer/spacer/wxSizerItem
  407. wxSizerItem* Add(wxWindow *window,
  408. int proportion = 0,
  409. int flag = 0,
  410. int border = 0,
  411. wxObject* userData = NULL);
  412. wxSizerItem* Add(wxSizer *sizer,
  413. int proportion = 0,
  414. int flag = 0,
  415. int border = 0,
  416. wxObject* userData = NULL);
  417. wxSizerItem* Add(int width,
  418. int height,
  419. int proportion = 0,
  420. int flag = 0,
  421. int border = 0,
  422. wxObject* userData = NULL);
  423. wxSizerItem* Add( wxWindow *window, const wxSizerFlags& flags);
  424. wxSizerItem* Add( wxSizer *sizer, const wxSizerFlags& flags);
  425. wxSizerItem* Add( int width, int height, const wxSizerFlags& flags);
  426. wxSizerItem* Add( wxSizerItem *item);
  427. virtual wxSizerItem *AddSpacer(int size);
  428. wxSizerItem* AddStretchSpacer(int prop = 1);
  429. wxSizerItem* Insert(size_t index,
  430. wxWindow *window,
  431. int proportion = 0,
  432. int flag = 0,
  433. int border = 0,
  434. wxObject* userData = NULL);
  435. wxSizerItem* Insert(size_t index,
  436. wxSizer *sizer,
  437. int proportion = 0,
  438. int flag = 0,
  439. int border = 0,
  440. wxObject* userData = NULL);
  441. wxSizerItem* Insert(size_t index,
  442. int width,
  443. int height,
  444. int proportion = 0,
  445. int flag = 0,
  446. int border = 0,
  447. wxObject* userData = NULL);
  448. wxSizerItem* Insert(size_t index,
  449. wxWindow *window,
  450. const wxSizerFlags& flags);
  451. wxSizerItem* Insert(size_t index,
  452. wxSizer *sizer,
  453. const wxSizerFlags& flags);
  454. wxSizerItem* Insert(size_t index,
  455. int width,
  456. int height,
  457. const wxSizerFlags& flags);
  458. // NB: do _not_ override this function in the derived classes, this one is
  459. // virtual for compatibility reasons only to allow old code overriding
  460. // it to continue to work, override DoInsert() instead in the new code
  461. virtual wxSizerItem* Insert(size_t index, wxSizerItem *item);
  462. wxSizerItem* InsertSpacer(size_t index, int size);
  463. wxSizerItem* InsertStretchSpacer(size_t index, int prop = 1);
  464. wxSizerItem* Prepend(wxWindow *window,
  465. int proportion = 0,
  466. int flag = 0,
  467. int border = 0,
  468. wxObject* userData = NULL);
  469. wxSizerItem* Prepend(wxSizer *sizer,
  470. int proportion = 0,
  471. int flag = 0,
  472. int border = 0,
  473. wxObject* userData = NULL);
  474. wxSizerItem* Prepend(int width,
  475. int height,
  476. int proportion = 0,
  477. int flag = 0,
  478. int border = 0,
  479. wxObject* userData = NULL);
  480. wxSizerItem* Prepend(wxWindow *window, const wxSizerFlags& flags);
  481. wxSizerItem* Prepend(wxSizer *sizer, const wxSizerFlags& flags);
  482. wxSizerItem* Prepend(int width, int height, const wxSizerFlags& flags);
  483. wxSizerItem* Prepend(wxSizerItem *item);
  484. wxSizerItem* PrependSpacer(int size);
  485. wxSizerItem* PrependStretchSpacer(int prop = 1);
  486. // set (or possibly unset if window is NULL) or get the window this sizer
  487. // is used in
  488. void SetContainingWindow(wxWindow *window);
  489. wxWindow *GetContainingWindow() const { return m_containingWindow; }
  490. #if WXWIN_COMPATIBILITY_2_6
  491. // Deprecated in 2.6 since historically it does not delete the window,
  492. // use Detach instead.
  493. wxDEPRECATED( virtual bool Remove( wxWindow *window ) );
  494. #endif // WXWIN_COMPATIBILITY_2_6
  495. virtual bool Remove( wxSizer *sizer );
  496. virtual bool Remove( int index );
  497. virtual bool Detach( wxWindow *window );
  498. virtual bool Detach( wxSizer *sizer );
  499. virtual bool Detach( int index );
  500. virtual bool Replace( wxWindow *oldwin, wxWindow *newwin, bool recursive = false );
  501. virtual bool Replace( wxSizer *oldsz, wxSizer *newsz, bool recursive = false );
  502. virtual bool Replace( size_t index, wxSizerItem *newitem );
  503. virtual void Clear( bool delete_windows = false );
  504. virtual void DeleteWindows();
  505. // Inform sizer about the first direction that has been decided (by parent item)
  506. // Returns true if it made use of the information (and recalculated min size)
  507. virtual bool InformFirstDirection( int WXUNUSED(direction), int WXUNUSED(size), int WXUNUSED(availableOtherDir) )
  508. { return false; }
  509. void SetMinSize( int width, int height )
  510. { DoSetMinSize( width, height ); }
  511. void SetMinSize( const wxSize& size )
  512. { DoSetMinSize( size.x, size.y ); }
  513. // Searches recursively
  514. bool SetItemMinSize( wxWindow *window, int width, int height )
  515. { return DoSetItemMinSize( window, width, height ); }
  516. bool SetItemMinSize( wxWindow *window, const wxSize& size )
  517. { return DoSetItemMinSize( window, size.x, size.y ); }
  518. // Searches recursively
  519. bool SetItemMinSize( wxSizer *sizer, int width, int height )
  520. { return DoSetItemMinSize( sizer, width, height ); }
  521. bool SetItemMinSize( wxSizer *sizer, const wxSize& size )
  522. { return DoSetItemMinSize( sizer, size.x, size.y ); }
  523. bool SetItemMinSize( size_t index, int width, int height )
  524. { return DoSetItemMinSize( index, width, height ); }
  525. bool SetItemMinSize( size_t index, const wxSize& size )
  526. { return DoSetItemMinSize( index, size.x, size.y ); }
  527. wxSize GetSize() const
  528. { return m_size; }
  529. wxPoint GetPosition() const
  530. { return m_position; }
  531. // Calculate the minimal size or return m_minSize if bigger.
  532. wxSize GetMinSize();
  533. // These virtual functions are used by the layout algorithm: first
  534. // CalcMin() is called to calculate the minimal size of the sizer and
  535. // prepare for laying it out and then RecalcSizes() is called to really
  536. // update all the sizer items
  537. virtual wxSize CalcMin() = 0;
  538. virtual void RecalcSizes() = 0;
  539. virtual void Layout();
  540. wxSize ComputeFittingClientSize(wxWindow *window);
  541. wxSize ComputeFittingWindowSize(wxWindow *window);
  542. wxSize Fit( wxWindow *window );
  543. void FitInside( wxWindow *window );
  544. void SetSizeHints( wxWindow *window );
  545. #if WXWIN_COMPATIBILITY_2_8
  546. // This only calls FitInside() since 2.9
  547. wxDEPRECATED( void SetVirtualSizeHints( wxWindow *window ) );
  548. #endif
  549. wxSizerItemList& GetChildren()
  550. { return m_children; }
  551. const wxSizerItemList& GetChildren() const
  552. { return m_children; }
  553. void SetDimension(const wxPoint& pos, const wxSize& size)
  554. {
  555. m_position = pos;
  556. m_size = size;
  557. Layout();
  558. // This call is required for wxWrapSizer to be able to calculate its
  559. // minimal size correctly.
  560. InformFirstDirection(wxHORIZONTAL, size.x, size.y);
  561. }
  562. void SetDimension(int x, int y, int width, int height)
  563. { SetDimension(wxPoint(x, y), wxSize(width, height)); }
  564. size_t GetItemCount() const { return m_children.GetCount(); }
  565. bool IsEmpty() const { return m_children.IsEmpty(); }
  566. wxSizerItem* GetItem( wxWindow *window, bool recursive = false );
  567. wxSizerItem* GetItem( wxSizer *sizer, bool recursive = false );
  568. wxSizerItem* GetItem( size_t index );
  569. wxSizerItem* GetItemById( int id, bool recursive = false );
  570. // Manage whether individual scene items are considered
  571. // in the layout calculations or not.
  572. bool Show( wxWindow *window, bool show = true, bool recursive = false );
  573. bool Show( wxSizer *sizer, bool show = true, bool recursive = false );
  574. bool Show( size_t index, bool show = true );
  575. bool Hide( wxSizer *sizer, bool recursive = false )
  576. { return Show( sizer, false, recursive ); }
  577. bool Hide( wxWindow *window, bool recursive = false )
  578. { return Show( window, false, recursive ); }
  579. bool Hide( size_t index )
  580. { return Show( index, false ); }
  581. bool IsShown( wxWindow *window ) const;
  582. bool IsShown( wxSizer *sizer ) const;
  583. bool IsShown( size_t index ) const;
  584. // Recursively call wxWindow::Show () on all sizer items.
  585. virtual void ShowItems (bool show);
  586. void Show(bool show) { ShowItems(show); }
  587. // This is the ShowItems() counterpart and returns true if any of the sizer
  588. // items are shown.
  589. virtual bool AreAnyItemsShown() const;
  590. protected:
  591. wxSize m_size;
  592. wxSize m_minSize;
  593. wxPoint m_position;
  594. wxSizerItemList m_children;
  595. // the window this sizer is used in, can be NULL
  596. wxWindow *m_containingWindow;
  597. wxSize GetMaxClientSize( wxWindow *window ) const;
  598. wxSize GetMinClientSize( wxWindow *window );
  599. wxSize VirtualFitSize( wxWindow *window );
  600. virtual void DoSetMinSize( int width, int height );
  601. virtual bool DoSetItemMinSize( wxWindow *window, int width, int height );
  602. virtual bool DoSetItemMinSize( wxSizer *sizer, int width, int height );
  603. virtual bool DoSetItemMinSize( size_t index, int width, int height );
  604. // insert a new item into m_children at given index and return the item
  605. // itself
  606. virtual wxSizerItem* DoInsert(size_t index, wxSizerItem *item);
  607. private:
  608. DECLARE_CLASS(wxSizer)
  609. };
  610. //---------------------------------------------------------------------------
  611. // wxGridSizer
  612. //---------------------------------------------------------------------------
  613. class WXDLLIMPEXP_CORE wxGridSizer: public wxSizer
  614. {
  615. public:
  616. // ctors specifying the number of columns only: number of rows will be
  617. // deduced automatically depending on the number of sizer elements
  618. wxGridSizer( int cols, int vgap, int hgap );
  619. wxGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
  620. // ctors specifying the number of rows and columns
  621. wxGridSizer( int rows, int cols, int vgap, int hgap );
  622. wxGridSizer( int rows, int cols, const wxSize& gap );
  623. virtual void RecalcSizes();
  624. virtual wxSize CalcMin();
  625. void SetCols( int cols )
  626. {
  627. wxASSERT_MSG( cols >= 0, "Number of columns must be non-negative");
  628. m_cols = cols;
  629. }
  630. void SetRows( int rows )
  631. {
  632. wxASSERT_MSG( rows >= 0, "Number of rows must be non-negative");
  633. m_rows = rows;
  634. }
  635. void SetVGap( int gap ) { m_vgap = gap; }
  636. void SetHGap( int gap ) { m_hgap = gap; }
  637. int GetCols() const { return m_cols; }
  638. int GetRows() const { return m_rows; }
  639. int GetVGap() const { return m_vgap; }
  640. int GetHGap() const { return m_hgap; }
  641. int GetEffectiveColsCount() const { return m_cols ? m_cols : CalcCols(); }
  642. int GetEffectiveRowsCount() const { return m_rows ? m_rows : CalcRows(); }
  643. // return the number of total items and the number of columns and rows
  644. // (for internal use only)
  645. int CalcRowsCols(int& rows, int& cols) const;
  646. protected:
  647. // the number of rows/columns in the sizer, if 0 then it is determined
  648. // dynamically depending on the total number of items
  649. int m_rows;
  650. int m_cols;
  651. // gaps between rows and columns
  652. int m_vgap;
  653. int m_hgap;
  654. virtual wxSizerItem *DoInsert(size_t index, wxSizerItem *item);
  655. void SetItemBounds( wxSizerItem *item, int x, int y, int w, int h );
  656. // returns the number of columns/rows needed for the current total number
  657. // of children (and the fixed number of rows/columns)
  658. int CalcCols() const
  659. {
  660. wxCHECK_MSG
  661. (
  662. m_rows, 0,
  663. "Can't calculate number of cols if number of rows is not specified"
  664. );
  665. return int(m_children.GetCount() + m_rows - 1) / m_rows;
  666. }
  667. int CalcRows() const
  668. {
  669. wxCHECK_MSG
  670. (
  671. m_cols, 0,
  672. "Can't calculate number of cols if number of rows is not specified"
  673. );
  674. return int(m_children.GetCount() + m_cols - 1) / m_cols;
  675. }
  676. private:
  677. DECLARE_CLASS(wxGridSizer)
  678. };
  679. //---------------------------------------------------------------------------
  680. // wxFlexGridSizer
  681. //---------------------------------------------------------------------------
  682. // values which define the behaviour for resizing wxFlexGridSizer cells in the
  683. // "non-flexible" direction
  684. enum wxFlexSizerGrowMode
  685. {
  686. // don't resize the cells in non-flexible direction at all
  687. wxFLEX_GROWMODE_NONE,
  688. // uniformly resize only the specified ones (default)
  689. wxFLEX_GROWMODE_SPECIFIED,
  690. // uniformly resize all cells
  691. wxFLEX_GROWMODE_ALL
  692. };
  693. class WXDLLIMPEXP_CORE wxFlexGridSizer: public wxGridSizer
  694. {
  695. public:
  696. // ctors specifying the number of columns only: number of rows will be
  697. // deduced automatically depending on the number of sizer elements
  698. wxFlexGridSizer( int cols, int vgap, int hgap );
  699. wxFlexGridSizer( int cols, const wxSize& gap = wxSize(0, 0) );
  700. // ctors specifying the number of rows and columns
  701. wxFlexGridSizer( int rows, int cols, int vgap, int hgap );
  702. wxFlexGridSizer( int rows, int cols, const wxSize& gap );
  703. // dtor
  704. virtual ~wxFlexGridSizer();
  705. // set the rows/columns which will grow (the others will remain of the
  706. // constant initial size)
  707. void AddGrowableRow( size_t idx, int proportion = 0 );
  708. void RemoveGrowableRow( size_t idx );
  709. void AddGrowableCol( size_t idx, int proportion = 0 );
  710. void RemoveGrowableCol( size_t idx );
  711. bool IsRowGrowable( size_t idx );
  712. bool IsColGrowable( size_t idx );
  713. // the sizer cells may grow in both directions, not grow at all or only
  714. // grow in one direction but not the other
  715. // the direction may be wxVERTICAL, wxHORIZONTAL or wxBOTH (default)
  716. void SetFlexibleDirection(int direction) { m_flexDirection = direction; }
  717. int GetFlexibleDirection() const { return m_flexDirection; }
  718. // note that the grow mode only applies to the direction which is not
  719. // flexible
  720. void SetNonFlexibleGrowMode(wxFlexSizerGrowMode mode) { m_growMode = mode; }
  721. wxFlexSizerGrowMode GetNonFlexibleGrowMode() const { return m_growMode; }
  722. // Read-only access to the row heights and col widths arrays
  723. const wxArrayInt& GetRowHeights() const { return m_rowHeights; }
  724. const wxArrayInt& GetColWidths() const { return m_colWidths; }
  725. // implementation
  726. virtual void RecalcSizes();
  727. virtual wxSize CalcMin();
  728. protected:
  729. void AdjustForFlexDirection();
  730. void AdjustForGrowables(const wxSize& sz);
  731. void FindWidthsAndHeights(int nrows, int ncols);
  732. // the heights/widths of all rows/columns
  733. wxArrayInt m_rowHeights,
  734. m_colWidths;
  735. // indices of the growable columns and rows
  736. wxArrayInt m_growableRows,
  737. m_growableCols;
  738. // proportion values of the corresponding growable rows and columns
  739. wxArrayInt m_growableRowsProportions,
  740. m_growableColsProportions;
  741. // parameters describing whether the growable cells should be resized in
  742. // both directions or only one
  743. int m_flexDirection;
  744. wxFlexSizerGrowMode m_growMode;
  745. // saves CalcMin result to optimize RecalcSizes
  746. wxSize m_calculatedMinSize;
  747. private:
  748. DECLARE_CLASS(wxFlexGridSizer)
  749. wxDECLARE_NO_COPY_CLASS(wxFlexGridSizer);
  750. };
  751. //---------------------------------------------------------------------------
  752. // wxBoxSizer
  753. //---------------------------------------------------------------------------
  754. class WXDLLIMPEXP_CORE wxBoxSizer: public wxSizer
  755. {
  756. public:
  757. wxBoxSizer(int orient)
  758. {
  759. m_orient = orient;
  760. m_totalProportion = 0;
  761. wxASSERT_MSG( m_orient == wxHORIZONTAL || m_orient == wxVERTICAL,
  762. wxT("invalid value for wxBoxSizer orientation") );
  763. }
  764. virtual wxSizerItem *AddSpacer(int size);
  765. int GetOrientation() const { return m_orient; }
  766. bool IsVertical() const { return m_orient == wxVERTICAL; }
  767. void SetOrientation(int orient) { m_orient = orient; }
  768. // implementation of our resizing logic
  769. virtual wxSize CalcMin();
  770. virtual void RecalcSizes();
  771. protected:
  772. // helpers for our code: this returns the component of the given wxSize in
  773. // the direction of the sizer and in the other direction, respectively
  774. int GetSizeInMajorDir(const wxSize& sz) const
  775. {
  776. return m_orient == wxHORIZONTAL ? sz.x : sz.y;
  777. }
  778. int& SizeInMajorDir(wxSize& sz)
  779. {
  780. return m_orient == wxHORIZONTAL ? sz.x : sz.y;
  781. }
  782. int& PosInMajorDir(wxPoint& pt)
  783. {
  784. return m_orient == wxHORIZONTAL ? pt.x : pt.y;
  785. }
  786. int GetSizeInMinorDir(const wxSize& sz) const
  787. {
  788. return m_orient == wxHORIZONTAL ? sz.y : sz.x;
  789. }
  790. int& SizeInMinorDir(wxSize& sz)
  791. {
  792. return m_orient == wxHORIZONTAL ? sz.y : sz.x;
  793. }
  794. int& PosInMinorDir(wxPoint& pt)
  795. {
  796. return m_orient == wxHORIZONTAL ? pt.y : pt.x;
  797. }
  798. // another helper: creates wxSize from major and minor components
  799. wxSize SizeFromMajorMinor(int major, int minor) const
  800. {
  801. if ( m_orient == wxHORIZONTAL )
  802. {
  803. return wxSize(major, minor);
  804. }
  805. else // wxVERTICAL
  806. {
  807. return wxSize(minor, major);
  808. }
  809. }
  810. // either wxHORIZONTAL or wxVERTICAL
  811. int m_orient;
  812. // the sum of proportion of all of our elements
  813. int m_totalProportion;
  814. // the minimal size needed for this sizer as calculated by the last call to
  815. // our CalcMin()
  816. wxSize m_minSize;
  817. private:
  818. DECLARE_CLASS(wxBoxSizer)
  819. };
  820. //---------------------------------------------------------------------------
  821. // wxStaticBoxSizer
  822. //---------------------------------------------------------------------------
  823. #if wxUSE_STATBOX
  824. class WXDLLIMPEXP_FWD_CORE wxStaticBox;
  825. class WXDLLIMPEXP_CORE wxStaticBoxSizer: public wxBoxSizer
  826. {
  827. public:
  828. wxStaticBoxSizer(wxStaticBox *box, int orient);
  829. wxStaticBoxSizer(int orient, wxWindow *win, const wxString& label = wxEmptyString);
  830. virtual ~wxStaticBoxSizer();
  831. void RecalcSizes();
  832. wxSize CalcMin();
  833. wxStaticBox *GetStaticBox() const
  834. { return m_staticBox; }
  835. // override to hide/show the static box as well
  836. virtual void ShowItems (bool show);
  837. virtual bool AreAnyItemsShown() const;
  838. virtual bool Detach( wxWindow *window );
  839. virtual bool Detach( wxSizer *sizer ) { return wxBoxSizer::Detach(sizer); }
  840. virtual bool Detach( int index ) { return wxBoxSizer::Detach(index); }
  841. protected:
  842. wxStaticBox *m_staticBox;
  843. private:
  844. DECLARE_CLASS(wxStaticBoxSizer)
  845. wxDECLARE_NO_COPY_CLASS(wxStaticBoxSizer);
  846. };
  847. #endif // wxUSE_STATBOX
  848. //---------------------------------------------------------------------------
  849. // wxStdDialogButtonSizer
  850. //---------------------------------------------------------------------------
  851. #if wxUSE_BUTTON
  852. class WXDLLIMPEXP_CORE wxStdDialogButtonSizer: public wxBoxSizer
  853. {
  854. public:
  855. // Constructor just creates a new wxBoxSizer, not much else.
  856. // Box sizer orientation is automatically determined here:
  857. // vertical for PDAs, horizontal for everything else?
  858. wxStdDialogButtonSizer();
  859. // Checks button ID against system IDs and sets one of the pointers below
  860. // to this button. Does not do any sizer-related things here.
  861. void AddButton(wxButton *button);
  862. // Use these if no standard ID can/should be used
  863. void SetAffirmativeButton( wxButton *button );
  864. void SetNegativeButton( wxButton *button );
  865. void SetCancelButton( wxButton *button );
  866. // All platform-specific code here, checks which buttons exist and add
  867. // them to the sizer accordingly.
  868. // Note - one potential hack on Mac we could use here,
  869. // if m_buttonAffirmative is wxID_SAVE then ensure wxID_SAVE
  870. // is set to _("Save") and m_buttonNegative is set to _("Don't Save")
  871. // I wouldn't add any other hacks like that into here,
  872. // but this one I can see being useful.
  873. void Realize();
  874. wxButton *GetAffirmativeButton() const { return m_buttonAffirmative; }
  875. wxButton *GetApplyButton() const { return m_buttonApply; }
  876. wxButton *GetNegativeButton() const { return m_buttonNegative; }
  877. wxButton *GetCancelButton() const { return m_buttonCancel; }
  878. wxButton *GetHelpButton() const { return m_buttonHelp; }
  879. protected:
  880. wxButton *m_buttonAffirmative; // wxID_OK, wxID_YES, wxID_SAVE go here
  881. wxButton *m_buttonApply; // wxID_APPLY
  882. wxButton *m_buttonNegative; // wxID_NO
  883. wxButton *m_buttonCancel; // wxID_CANCEL, wxID_CLOSE
  884. wxButton *m_buttonHelp; // wxID_HELP, wxID_CONTEXT_HELP
  885. private:
  886. DECLARE_CLASS(wxStdDialogButtonSizer)
  887. wxDECLARE_NO_COPY_CLASS(wxStdDialogButtonSizer);
  888. };
  889. #endif // wxUSE_BUTTON
  890. // ----------------------------------------------------------------------------
  891. // inline functions implementation
  892. // ----------------------------------------------------------------------------
  893. #if WXWIN_COMPATIBILITY_2_8
  894. inline void wxSizerItem::SetWindow(wxWindow *window)
  895. {
  896. DoSetWindow(window);
  897. }
  898. inline void wxSizerItem::SetSizer(wxSizer *sizer)
  899. {
  900. DoSetSizer(sizer);
  901. }
  902. inline void wxSizerItem::SetSpacer(const wxSize& size)
  903. {
  904. DoSetSpacer(size);
  905. }
  906. inline void wxSizerItem::SetSpacer(int width, int height)
  907. {
  908. DoSetSpacer(wxSize(width, height));
  909. }
  910. #endif // WXWIN_COMPATIBILITY_2_8
  911. inline wxSizerItem*
  912. wxSizer::Insert(size_t index, wxSizerItem *item)
  913. {
  914. return DoInsert(index, item);
  915. }
  916. inline wxSizerItem*
  917. wxSizer::Add( wxSizerItem *item )
  918. {
  919. return Insert( m_children.GetCount(), item );
  920. }
  921. inline wxSizerItem*
  922. wxSizer::Add( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
  923. {
  924. return Add( new wxSizerItem( window, proportion, flag, border, userData ) );
  925. }
  926. inline wxSizerItem*
  927. wxSizer::Add( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
  928. {
  929. return Add( new wxSizerItem( sizer, proportion, flag, border, userData ) );
  930. }
  931. inline wxSizerItem*
  932. wxSizer::Add( int width, int height, int proportion, int flag, int border, wxObject* userData )
  933. {
  934. return Add( new wxSizerItem( width, height, proportion, flag, border, userData ) );
  935. }
  936. inline wxSizerItem*
  937. wxSizer::Add( wxWindow *window, const wxSizerFlags& flags )
  938. {
  939. return Add( new wxSizerItem(window, flags) );
  940. }
  941. inline wxSizerItem*
  942. wxSizer::Add( wxSizer *sizer, const wxSizerFlags& flags )
  943. {
  944. return Add( new wxSizerItem(sizer, flags) );
  945. }
  946. inline wxSizerItem*
  947. wxSizer::Add( int width, int height, const wxSizerFlags& flags )
  948. {
  949. return Add( new wxSizerItem(width, height, flags) );
  950. }
  951. inline wxSizerItem*
  952. wxSizer::AddSpacer(int size)
  953. {
  954. return Add(size, size);
  955. }
  956. inline wxSizerItem*
  957. wxSizer::AddStretchSpacer(int prop)
  958. {
  959. return Add(0, 0, prop);
  960. }
  961. inline wxSizerItem*
  962. wxSizer::Prepend( wxSizerItem *item )
  963. {
  964. return Insert( 0, item );
  965. }
  966. inline wxSizerItem*
  967. wxSizer::Prepend( wxWindow *window, int proportion, int flag, int border, wxObject* userData )
  968. {
  969. return Prepend( new wxSizerItem( window, proportion, flag, border, userData ) );
  970. }
  971. inline wxSizerItem*
  972. wxSizer::Prepend( wxSizer *sizer, int proportion, int flag, int border, wxObject* userData )
  973. {
  974. return Prepend( new wxSizerItem( sizer, proportion, flag, border, userData ) );
  975. }
  976. inline wxSizerItem*
  977. wxSizer::Prepend( int width, int height, int proportion, int flag, int border, wxObject* userData )
  978. {
  979. return Prepend( new wxSizerItem( width, height, proportion, flag, border, userData ) );
  980. }
  981. inline wxSizerItem*
  982. wxSizer::PrependSpacer(int size)
  983. {
  984. return Prepend(size, size);
  985. }
  986. inline wxSizerItem*
  987. wxSizer::PrependStretchSpacer(int prop)
  988. {
  989. return Prepend(0, 0, prop);
  990. }
  991. inline wxSizerItem*
  992. wxSizer::Prepend( wxWindow *window, const wxSizerFlags& flags )
  993. {
  994. return Prepend( new wxSizerItem(window, flags) );
  995. }
  996. inline wxSizerItem*
  997. wxSizer::Prepend( wxSizer *sizer, const wxSizerFlags& flags )
  998. {
  999. return Prepend( new wxSizerItem(sizer, flags) );
  1000. }
  1001. inline wxSizerItem*
  1002. wxSizer::Prepend( int width, int height, const wxSizerFlags& flags )
  1003. {
  1004. return Prepend( new wxSizerItem(width, height, flags) );
  1005. }
  1006. inline wxSizerItem*
  1007. wxSizer::Insert( size_t index,
  1008. wxWindow *window,
  1009. int proportion,
  1010. int flag,
  1011. int border,
  1012. wxObject* userData )
  1013. {
  1014. return Insert( index, new wxSizerItem( window, proportion, flag, border, userData ) );
  1015. }
  1016. inline wxSizerItem*
  1017. wxSizer::Insert( size_t index,
  1018. wxSizer *sizer,
  1019. int proportion,
  1020. int flag,
  1021. int border,
  1022. wxObject* userData )
  1023. {
  1024. return Insert( index, new wxSizerItem( sizer, proportion, flag, border, userData ) );
  1025. }
  1026. inline wxSizerItem*
  1027. wxSizer::Insert( size_t index,
  1028. int width,
  1029. int height,
  1030. int proportion,
  1031. int flag,
  1032. int border,
  1033. wxObject* userData )
  1034. {
  1035. return Insert( index, new wxSizerItem( width, height, proportion, flag, border, userData ) );
  1036. }
  1037. inline wxSizerItem*
  1038. wxSizer::Insert( size_t index, wxWindow *window, const wxSizerFlags& flags )
  1039. {
  1040. return Insert( index, new wxSizerItem(window, flags) );
  1041. }
  1042. inline wxSizerItem*
  1043. wxSizer::Insert( size_t index, wxSizer *sizer, const wxSizerFlags& flags )
  1044. {
  1045. return Insert( index, new wxSizerItem(sizer, flags) );
  1046. }
  1047. inline wxSizerItem*
  1048. wxSizer::Insert( size_t index, int width, int height, const wxSizerFlags& flags )
  1049. {
  1050. return Insert( index, new wxSizerItem(width, height, flags) );
  1051. }
  1052. inline wxSizerItem*
  1053. wxSizer::InsertSpacer(size_t index, int size)
  1054. {
  1055. return Insert(index, size, size);
  1056. }
  1057. inline wxSizerItem*
  1058. wxSizer::InsertStretchSpacer(size_t index, int prop)
  1059. {
  1060. return Insert(index, 0, 0, prop);
  1061. }
  1062. #endif // __WXSIZER_H__