layout.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: layout.cpp
  3. // Purpose: Layout sample
  4. // Author: Julian Smart
  5. // Modified by: Robin Dunn, Vadim Zeitlin
  6. // Created: 04/01/98
  7. // Copyright: (c) 1998 Julian Smart
  8. // 2005 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. // ----------------------------------------------------------------------------
  12. // headers
  13. // ----------------------------------------------------------------------------
  14. // For compilers that support precompilation, includes "wx/wx.h".
  15. #include "wx/wxprec.h"
  16. #ifdef __BORLANDC__
  17. #pragma hdrstop
  18. #endif
  19. #ifndef WX_PRECOMP
  20. #include "wx/wx.h"
  21. #endif
  22. #include "wx/sizer.h"
  23. #include "wx/gbsizer.h"
  24. #include "wx/statline.h"
  25. #include "wx/notebook.h"
  26. #include "wx/spinctrl.h"
  27. #include "wx/wrapsizer.h"
  28. #include "wx/generic/stattextg.h"
  29. #include "layout.h"
  30. #ifndef wxHAS_IMAGES_IN_RESOURCES
  31. #include "../sample.xpm"
  32. #endif
  33. // ----------------------------------------------------------------------------
  34. // MyApp
  35. // ----------------------------------------------------------------------------
  36. IMPLEMENT_APP(MyApp)
  37. bool MyApp::OnInit()
  38. {
  39. if ( !wxApp::OnInit() )
  40. return false;
  41. // Create the main frame window
  42. MyFrame *frame = new MyFrame;
  43. frame->Show(true);
  44. return true;
  45. }
  46. // ----------------------------------------------------------------------------
  47. // MyFrame
  48. // ----------------------------------------------------------------------------
  49. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  50. EVT_MENU(LAYOUT_ABOUT, MyFrame::OnAbout)
  51. EVT_MENU(LAYOUT_QUIT, MyFrame::OnQuit)
  52. EVT_MENU(LAYOUT_TEST_PROPORTIONS, MyFrame::TestProportions)
  53. EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestFlexSizers)
  54. EVT_MENU(LAYOUT_TEST_NB_SIZER, MyFrame::TestNotebookSizers)
  55. EVT_MENU(LAYOUT_TEST_GB_SIZER, MyFrame::TestGridBagSizer)
  56. EVT_MENU(LAYOUT_TEST_SET_MINIMAL, MyFrame::TestSetMinimal)
  57. EVT_MENU(LAYOUT_TEST_NESTED, MyFrame::TestNested)
  58. EVT_MENU(LAYOUT_TEST_WRAP, MyFrame::TestWrap)
  59. wxEND_EVENT_TABLE()
  60. // Define my frame constructor
  61. MyFrame::MyFrame()
  62. : wxFrame(NULL, wxID_ANY, wxT("wxWidgets Layout Demo"))
  63. {
  64. SetIcon(wxICON(sample));
  65. // Make a menubar
  66. wxMenu *file_menu = new wxMenu;
  67. file_menu->Append(LAYOUT_TEST_PROPORTIONS, wxT("&Proportions demo...\tF1"));
  68. file_menu->Append(LAYOUT_TEST_SIZER, wxT("Test wx&FlexSizer...\tF2"));
  69. file_menu->Append(LAYOUT_TEST_NB_SIZER, wxT("Test &notebook sizers...\tF3"));
  70. file_menu->Append(LAYOUT_TEST_GB_SIZER, wxT("Test &gridbag sizer...\tF4"));
  71. file_menu->Append(LAYOUT_TEST_SET_MINIMAL, wxT("Test Set&ItemMinSize...\tF5"));
  72. file_menu->Append(LAYOUT_TEST_NESTED, wxT("Test nested sizer in a wxPanel...\tF6"));
  73. file_menu->Append(LAYOUT_TEST_WRAP, wxT("Test wrap sizers...\tF7"));
  74. file_menu->AppendSeparator();
  75. file_menu->Append(LAYOUT_QUIT, wxT("E&xit"), wxT("Quit program"));
  76. wxMenu *help_menu = new wxMenu;
  77. help_menu->Append(LAYOUT_ABOUT, wxT("&About"), wxT("About layout demo..."));
  78. wxMenuBar *menu_bar = new wxMenuBar;
  79. menu_bar->Append(file_menu, wxT("&File"));
  80. menu_bar->Append(help_menu, wxT("&Help"));
  81. // Associate the menu bar with the frame
  82. SetMenuBar(menu_bar);
  83. #if wxUSE_STATUSBAR
  84. CreateStatusBar(2);
  85. SetStatusText(wxT("wxWidgets layout demo"));
  86. #endif // wxUSE_STATUSBAR
  87. wxPanel* p = new wxPanel(this, wxID_ANY);
  88. // we want to get a dialog that is stretchable because it
  89. // has a text ctrl in the middle. at the bottom, we have
  90. // two buttons which.
  91. wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
  92. // 1) top: create wxStaticText with minimum size equal to its default size
  93. topsizer->Add(
  94. new wxStaticText( p, wxID_ANY, wxT("An explanation (wxALIGN_RIGHT).") ),
  95. wxSizerFlags().Align(wxALIGN_RIGHT).Border(wxALL & ~wxBOTTOM, 5));
  96. topsizer->Add(
  97. new wxStaticText( p, wxID_ANY, wxT("An explanation (wxALIGN_LEFT).") ),
  98. wxSizerFlags().Align(wxALIGN_LEFT).Border(wxALL & ~wxBOTTOM, 5));
  99. topsizer->Add(
  100. new wxStaticText( p, wxID_ANY, wxT("An explanation (wxALIGN_CENTRE_HORIZONTAL).") ),
  101. wxSizerFlags().Align(wxALIGN_CENTRE_HORIZONTAL).Border(wxALL & ~wxBOTTOM, 5));
  102. // 2) top: create wxTextCtrl with minimum size (100x60)
  103. topsizer->Add(
  104. new wxTextCtrl( p, wxID_ANY, wxT("My text (wxEXPAND)."), wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
  105. wxSizerFlags(1).Expand().Border(wxALL, 5));
  106. // 2.5) Gratuitous test of wxStaticBoxSizers
  107. wxBoxSizer *statsizer = new wxStaticBoxSizer(
  108. new wxStaticBox(p, wxID_ANY, wxT("A wxStaticBoxSizer")), wxVERTICAL );
  109. statsizer->Add(
  110. new wxStaticText(p, wxID_ANY, wxT("And some TEXT inside it")),
  111. wxSizerFlags().Border(wxALL, 30));
  112. topsizer->Add(
  113. statsizer,
  114. wxSizerFlags(1).Expand().Border(wxALL, 10));
  115. // 2.7) And a test of wxGridSizer
  116. wxGridSizer *gridsizer = new wxGridSizer(2, 5, 5);
  117. gridsizer->Add(new wxStaticText(p, wxID_ANY, wxT("Label")),
  118. wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
  119. gridsizer->Add(new wxTextCtrl(p, wxID_ANY, wxT("Grid sizer demo")),
  120. wxSizerFlags(1).Align(wxGROW | wxALIGN_CENTER_VERTICAL));
  121. gridsizer->Add(new wxStaticText(p, wxID_ANY, wxT("Another label")),
  122. wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
  123. gridsizer->Add(new wxTextCtrl(p, wxID_ANY, wxT("More text")),
  124. wxSizerFlags(1).Align(wxGROW | wxALIGN_CENTER_VERTICAL));
  125. gridsizer->Add(new wxStaticText(p, wxID_ANY, wxT("Final label")),
  126. wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
  127. gridsizer->Add(new wxTextCtrl(p, wxID_ANY, wxT("And yet more text")),
  128. wxSizerFlags().Align(wxGROW | wxALIGN_CENTER_VERTICAL));
  129. topsizer->Add(
  130. gridsizer,
  131. wxSizerFlags().Proportion(1).Expand().Border(wxALL, 10));
  132. #if wxUSE_STATLINE
  133. // 3) middle: create wxStaticLine with minimum size (3x3)
  134. topsizer->Add(
  135. new wxStaticLine( p, wxID_ANY, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
  136. wxSizerFlags().Expand());
  137. #endif // wxUSE_STATLINE
  138. // 4) bottom: create two centred wxButtons
  139. wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
  140. button_box->Add(
  141. new wxButton( p, wxID_ANY, wxT("Two buttons in a box") ),
  142. wxSizerFlags().Border(wxALL, 7));
  143. button_box->Add(
  144. new wxButton( p, wxID_ANY, wxT("(wxCENTER)") ),
  145. wxSizerFlags().Border(wxALL, 7));
  146. topsizer->Add(button_box, wxSizerFlags().Center());
  147. p->SetSizer( topsizer );
  148. // don't allow frame to get smaller than what the sizers tell it and also set
  149. // the initial size as calculated by the sizers
  150. topsizer->SetSizeHints( this );
  151. }
  152. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  153. {
  154. Close(true);
  155. }
  156. void MyFrame::TestProportions(wxCommandEvent& WXUNUSED(event))
  157. {
  158. (new MyProportionsFrame(this))->Show();
  159. }
  160. void MyFrame::TestFlexSizers(wxCommandEvent& WXUNUSED(event) )
  161. {
  162. (new MyFlexSizerFrame(this))->Show();
  163. }
  164. void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
  165. {
  166. MySizerDialog dialog( this, wxT("Notebook Sizer Test Dialog") );
  167. dialog.ShowModal();
  168. }
  169. void MyFrame::TestSetMinimal(wxCommandEvent& WXUNUSED(event) )
  170. {
  171. (new MySimpleSizerFrame(this))->Show();
  172. }
  173. void MyFrame::TestNested(wxCommandEvent& WXUNUSED(event) )
  174. {
  175. (new MyNestedSizerFrame(this))->Show();
  176. }
  177. void MyFrame::TestWrap(wxCommandEvent& WXUNUSED(event) )
  178. {
  179. (new MyWrapSizerFrame(this))->Show();
  180. }
  181. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
  182. {
  183. (void)wxMessageBox(wxT("wxWidgets GUI library layout demo\n"),
  184. wxT("About Layout Demo"), wxOK|wxICON_INFORMATION);
  185. }
  186. void MyFrame::TestGridBagSizer(wxCommandEvent& WXUNUSED(event) )
  187. {
  188. (new MyGridBagSizerFrame(this))->Show();
  189. }
  190. // ----------------------------------------------------------------------------
  191. // MyProportionsFrame
  192. // ----------------------------------------------------------------------------
  193. MyProportionsFrame::MyProportionsFrame(wxFrame *parent)
  194. : wxFrame(parent, wxID_ANY, wxT("Box Sizer Proportions Demo"))
  195. {
  196. size_t n;
  197. // create the controls
  198. wxPanel *panel = new wxPanel(this, wxID_ANY);
  199. for ( n = 0; n < WXSIZEOF(m_spins); n++ )
  200. {
  201. m_spins[n] = new wxSpinCtrl(panel);
  202. m_spins[n]->SetValue(n);
  203. }
  204. // lay them out
  205. m_sizer = new wxStaticBoxSizer(wxHORIZONTAL, panel,
  206. wxT("Try changing elements proportions and resizing the window"));
  207. for ( n = 0; n < WXSIZEOF(m_spins); n++ )
  208. m_sizer->Add(m_spins[n], wxSizerFlags().Border());
  209. // put everything together
  210. panel->SetSizer(m_sizer);
  211. wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
  212. sizerTop->Add(panel, wxSizerFlags(1).Expand().Border());
  213. UpdateProportions();
  214. SetSizerAndFit(sizerTop);
  215. // and connect the events
  216. Connect(wxEVT_TEXT,
  217. wxCommandEventHandler(MyProportionsFrame::OnProportionUpdated));
  218. Connect(wxEVT_SPINCTRL,
  219. wxSpinEventHandler(MyProportionsFrame::OnProportionChanged));
  220. }
  221. void MyProportionsFrame::UpdateProportions()
  222. {
  223. for ( size_t n = 0; n < WXSIZEOF(m_spins); n++ )
  224. {
  225. m_sizer->GetItem(n)->SetProportion(m_spins[n]->GetValue());
  226. }
  227. m_sizer->Layout();
  228. }
  229. void MyProportionsFrame::OnProportionUpdated(wxCommandEvent& WXUNUSED(event))
  230. {
  231. UpdateProportions();
  232. }
  233. void MyProportionsFrame::OnProportionChanged(wxSpinEvent& WXUNUSED(event))
  234. {
  235. UpdateProportions();
  236. }
  237. // ----------------------------------------------------------------------------
  238. // MyFlexSizerFrame
  239. // ----------------------------------------------------------------------------
  240. void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer *sizer, wxWindow* parent)
  241. {
  242. for ( int i = 0; i < 3; i++ )
  243. {
  244. for ( int j = 0; j < 3; j++ )
  245. {
  246. wxWindow * const cell = new wxGenericStaticText
  247. (
  248. parent,
  249. wxID_ANY,
  250. wxString::Format("(%d, %d)",
  251. i + 1, j + 1)
  252. );
  253. if ( (i + j) % 2 )
  254. cell->SetBackgroundColour( *wxLIGHT_GREY );
  255. sizer->Add(cell, 0, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 3);
  256. }
  257. }
  258. }
  259. MyFlexSizerFrame::MyFlexSizerFrame(wxFrame* parent)
  260. : wxFrame(parent, wxID_ANY, "Flex Sizer Test Frame")
  261. {
  262. wxFlexGridSizer *sizerFlex;
  263. wxPanel* p = new wxPanel(this, wxID_ANY);
  264. // consttuct the first column
  265. wxSizer *sizerCol1 = new wxBoxSizer(wxVERTICAL);
  266. sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("Ungrowable:")), 0, wxCENTER | wxTOP, 20);
  267. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  268. InitFlexSizer(sizerFlex, p);
  269. sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  270. sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("Growable middle column:")), 0, wxCENTER | wxTOP, 20);
  271. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  272. InitFlexSizer(sizerFlex, p);
  273. sizerFlex->AddGrowableCol(1);
  274. sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  275. sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("Growable middle row:")), 0, wxCENTER | wxTOP, 20);
  276. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  277. InitFlexSizer(sizerFlex, p);
  278. sizerFlex->AddGrowableRow(1);
  279. sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  280. sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("All growable columns:")), 0, wxCENTER | wxTOP, 20);
  281. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  282. InitFlexSizer(sizerFlex, p);
  283. sizerFlex->AddGrowableCol(0, 1);
  284. sizerFlex->AddGrowableCol(1, 2);
  285. sizerFlex->AddGrowableCol(2, 3);
  286. sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  287. // the second one
  288. wxSizer *sizerCol2 = new wxBoxSizer(wxVERTICAL);
  289. sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Growable middle row and column:")), 0, wxCENTER | wxTOP, 20);
  290. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  291. InitFlexSizer(sizerFlex, p);
  292. sizerFlex->AddGrowableCol(1);
  293. sizerFlex->AddGrowableRow(1);
  294. sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  295. sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Same with horz flex direction")), 0, wxCENTER | wxTOP, 20);
  296. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  297. InitFlexSizer(sizerFlex, p);
  298. sizerFlex->AddGrowableCol(1);
  299. sizerFlex->AddGrowableRow(1);
  300. sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
  301. sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  302. sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Same with grow mode == \"none\"")), 0, wxCENTER | wxTOP, 20);
  303. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  304. InitFlexSizer(sizerFlex, p);
  305. sizerFlex->AddGrowableCol(1);
  306. sizerFlex->AddGrowableRow(1);
  307. sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
  308. sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_NONE);
  309. sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  310. sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Same with grow mode == \"all\"")), 0, wxCENTER | wxTOP, 20);
  311. sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
  312. InitFlexSizer(sizerFlex, p);
  313. sizerFlex->AddGrowableCol(1);
  314. sizerFlex->AddGrowableRow(1);
  315. sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
  316. sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_ALL);
  317. sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
  318. // add both columns to grid sizer
  319. wxGridSizer *sizerTop = new wxGridSizer(2, 0, 20);
  320. sizerTop->Add(sizerCol1, 1, wxEXPAND);
  321. sizerTop->Add(sizerCol2, 1, wxEXPAND);
  322. p->SetSizer(sizerTop);
  323. sizerTop->SetSizeHints(this);
  324. }
  325. // ----------------------------------------------------------------------------
  326. // MySizerDialog
  327. // ----------------------------------------------------------------------------
  328. MySizerDialog::MySizerDialog(wxWindow *parent, const wxString &title)
  329. : wxDialog(parent, wxID_ANY, wxString(title))
  330. {
  331. // Begin with first hierarchy: a notebook at the top and
  332. // and OK button at the bottom.
  333. wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
  334. wxNotebook *notebook = new wxNotebook( this, wxID_ANY );
  335. topsizer->Add( notebook, 1, wxGROW );
  336. wxButton *button = new wxButton( this, wxID_OK, wxT("OK") );
  337. topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 );
  338. // First page: one big text ctrl
  339. wxTextCtrl *multi = new wxTextCtrl( notebook, wxID_ANY, wxT("TextCtrl."), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
  340. notebook->AddPage( multi, wxT("Page One") );
  341. // Second page: a text ctrl and a button
  342. wxPanel *panel = new wxPanel( notebook, wxID_ANY );
  343. notebook->AddPage( panel, wxT("Page Two") );
  344. wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
  345. wxTextCtrl *text = new wxTextCtrl( panel, wxID_ANY, wxT("TextLine 1."), wxDefaultPosition, wxSize(250,wxDefaultCoord) );
  346. panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
  347. text = new wxTextCtrl( panel, wxID_ANY, wxT("TextLine 2."), wxDefaultPosition, wxSize(250,wxDefaultCoord) );
  348. panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
  349. wxButton *button2 = new wxButton( panel, wxID_ANY, wxT("Hallo") );
  350. panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 );
  351. panel->SetSizer( panelsizer );
  352. // Tell dialog to use sizer
  353. SetSizerAndFit( topsizer );
  354. }
  355. // ----------------------------------------------------------------------------
  356. // MyGridBagSizerFrame
  357. // ----------------------------------------------------------------------------
  358. // some simple macros to help make the sample code below more clear
  359. #define TEXTCTRL(text) new wxTextCtrl(p, wxID_ANY, wxT(text))
  360. #define MLTEXTCTRL(text) new wxTextCtrl(p, wxID_ANY, wxT(text), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE)
  361. #define POS(r, c) wxGBPosition(r,c)
  362. #define SPAN(r, c) wxGBSpan(r,c)
  363. const wxChar gbsDescription[] =wxT("\
  364. The wxGridBagSizer is similar to the wxFlexGridSizer except the items are explicitly positioned\n\
  365. in a virtual cell of the layout grid, and column or row spanning is allowed. For example, this\n\
  366. static text is positioned at (0,0) and it spans 7 columns.");
  367. // Some IDs
  368. enum {
  369. GBS_HIDE_BTN = 1212,
  370. GBS_SHOW_BTN,
  371. GBS_MOVE_BTN1,
  372. GBS_MOVE_BTN2,
  373. GBS_MAX
  374. };
  375. wxBEGIN_EVENT_TABLE(MyGridBagSizerFrame, wxFrame)
  376. EVT_BUTTON( GBS_HIDE_BTN, MyGridBagSizerFrame::OnHideBtn)
  377. EVT_BUTTON( GBS_SHOW_BTN, MyGridBagSizerFrame::OnShowBtn)
  378. EVT_BUTTON( GBS_MOVE_BTN1, MyGridBagSizerFrame::OnMoveBtn)
  379. EVT_BUTTON( GBS_MOVE_BTN2, MyGridBagSizerFrame::OnMoveBtn)
  380. wxEND_EVENT_TABLE()
  381. MyGridBagSizerFrame::MyGridBagSizerFrame(wxFrame* parent)
  382. : wxFrame(parent, wxID_ANY, "wxGridBagSizer Test Frame")
  383. {
  384. wxPanel* p = new wxPanel(this, wxID_ANY);
  385. m_panel = p;
  386. m_gbs = new wxGridBagSizer();
  387. m_gbs->Add( new wxStaticText(p, wxID_ANY, gbsDescription),
  388. POS(0,0), SPAN(1, 7),
  389. wxALIGN_CENTER | wxALL, 5);
  390. m_gbs->Add( TEXTCTRL("pos(1,0)"), POS(1,0) );
  391. m_gbs->Add( TEXTCTRL("pos(1,1)"), POS(1,1) );
  392. m_gbs->Add( TEXTCTRL("pos(2,0)"), POS(2,0) );
  393. m_gbs->Add( TEXTCTRL("pos(2,1)"), POS(2,1) );
  394. m_gbs->Add( MLTEXTCTRL("pos(3,2), span(1,2)\nthis row and col are growable"),
  395. POS(3,2), SPAN(1,2), wxEXPAND );
  396. m_gbs->Add( MLTEXTCTRL("pos(4,3)\nspan(3,1)"),
  397. POS(4,3), SPAN(3,1), wxEXPAND );
  398. m_gbs->Add( TEXTCTRL("pos(5,4)"), POS(5,4), wxDefaultSpan, wxEXPAND );
  399. m_gbs->Add( TEXTCTRL("pos(6,5)"), POS(6,5), wxDefaultSpan, wxEXPAND );
  400. m_gbs->Add( TEXTCTRL("pos(7,6)"), POS(7,6) );
  401. //m_gbs->Add( TEXTCTRL("bad position"), POS(4,3) ); // Test for assert
  402. //m_gbs->Add( TEXTCTRL("bad position"), POS(5,3) ); // Test for assert
  403. m_moveBtn1 = new wxButton(p, GBS_MOVE_BTN1, wxT("Move this to (3,6)"));
  404. m_moveBtn2 = new wxButton(p, GBS_MOVE_BTN2, wxT("Move this to (3,6)"));
  405. m_gbs->Add( m_moveBtn1, POS(10,2) );
  406. m_gbs->Add( m_moveBtn2, POS(10,3) );
  407. m_hideBtn = new wxButton(p, GBS_HIDE_BTN, wxT("Hide this item -->"));
  408. m_gbs->Add(m_hideBtn, POS(12, 3));
  409. m_hideTxt = new wxTextCtrl(p, wxID_ANY, wxT("pos(12,4), size(150, wxDefaultCoord)"),
  410. wxDefaultPosition, wxSize(150,wxDefaultCoord));
  411. m_gbs->Add( m_hideTxt, POS(12,4) );
  412. m_showBtn = new wxButton(p, GBS_SHOW_BTN, wxT("<-- Show it again"));
  413. m_gbs->Add(m_showBtn, POS(12, 5));
  414. m_showBtn->Disable();
  415. m_gbs->Add(10,10, POS(14,0));
  416. m_gbs->AddGrowableRow(3);
  417. m_gbs->AddGrowableCol(2);
  418. p->SetSizerAndFit(m_gbs);
  419. SetClientSize(p->GetSize());
  420. }
  421. void MyGridBagSizerFrame::OnHideBtn(wxCommandEvent&)
  422. {
  423. m_gbs->Hide(m_hideTxt);
  424. m_hideBtn->Disable();
  425. m_showBtn->Enable();
  426. m_gbs->Layout();
  427. }
  428. void MyGridBagSizerFrame::OnShowBtn(wxCommandEvent&)
  429. {
  430. m_gbs->Show(m_hideTxt);
  431. m_hideBtn->Enable();
  432. m_showBtn->Disable();
  433. m_gbs->Layout();
  434. }
  435. void MyGridBagSizerFrame::OnMoveBtn(wxCommandEvent& event)
  436. {
  437. wxButton* btn = (wxButton*)event.GetEventObject();
  438. wxGBPosition curPos = m_gbs->GetItemPosition(btn);
  439. // if it's already at the "other" spot then move it back
  440. if (curPos == wxGBPosition(3,6))
  441. {
  442. m_gbs->SetItemPosition(btn, m_lastPos);
  443. btn->SetLabel(wxT("Move this to (3,6)"));
  444. }
  445. else
  446. {
  447. if ( m_gbs->CheckForIntersection(wxGBPosition(3,6), wxGBSpan(1,1)) )
  448. wxMessageBox(
  449. wxT("wxGridBagSizer will not allow items to be in the same cell as\n\
  450. another item, so this operation will fail. You will also get an assert\n\
  451. when compiled in debug mode."), wxT("Warning"), wxOK | wxICON_INFORMATION);
  452. if ( m_gbs->SetItemPosition(btn, wxGBPosition(3,6)) )
  453. {
  454. m_lastPos = curPos;
  455. btn->SetLabel(wxT("Move it back"));
  456. }
  457. }
  458. m_gbs->Layout();
  459. }
  460. // ----------------------------------------------------------------------------
  461. // MySimpleSizerFrame
  462. // ----------------------------------------------------------------------------
  463. // Some IDs
  464. enum {
  465. ID_SET_SMALL = 1300,
  466. ID_SET_BIG
  467. };
  468. wxBEGIN_EVENT_TABLE(MySimpleSizerFrame, wxFrame)
  469. EVT_MENU( ID_SET_SMALL, MySimpleSizerFrame::OnSetSmallSize)
  470. EVT_MENU( ID_SET_BIG, MySimpleSizerFrame::OnSetBigSize)
  471. wxEND_EVENT_TABLE()
  472. MySimpleSizerFrame::MySimpleSizerFrame(wxFrame* parent)
  473. : wxFrame(parent, wxID_ANY, "Simple Sizer Test Frame")
  474. {
  475. wxMenu *menu = new wxMenu;
  476. menu->Append(ID_SET_SMALL, wxT("Make text control small\tF4"));
  477. menu->Append(ID_SET_BIG, wxT("Make text control big\tF5"));
  478. wxMenuBar *menu_bar = new wxMenuBar;
  479. menu_bar->Append(menu, wxT("&File"));
  480. SetMenuBar( menu_bar );
  481. wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
  482. m_target = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
  483. main_sizer->Add( m_target, 1, wxALL, 5 );
  484. main_sizer->Add( new wxStaticText( this, wxID_ANY, wxT("Set alternating sizes using F4 and F5") ), 0, wxALL, 5 );
  485. SetSizer( main_sizer);
  486. Layout();
  487. GetSizer()->Fit( this );
  488. }
  489. void MySimpleSizerFrame::OnSetSmallSize( wxCommandEvent& WXUNUSED(event))
  490. {
  491. GetSizer()->SetItemMinSize( m_target, 40, -1 );
  492. Layout();
  493. GetSizer()->Fit( this );
  494. }
  495. void MySimpleSizerFrame::OnSetBigSize( wxCommandEvent& WXUNUSED(event))
  496. {
  497. GetSizer()->SetItemMinSize( m_target, 140, -1 );
  498. Layout();
  499. GetSizer()->Fit( this );
  500. }
  501. // ----------------------------------------------------------------------------
  502. // MyNestedSizerFrame
  503. // ----------------------------------------------------------------------------
  504. MyNestedSizerFrame::MyNestedSizerFrame(wxFrame* parent)
  505. : wxFrame(parent, wxID_ANY, "Nested Sizer Test Frame")
  506. {
  507. wxMenu *menu = new wxMenu;
  508. menu->Append(wxID_ABOUT, wxT("Do nothing"));
  509. wxMenuBar *menu_bar = new wxMenuBar;
  510. menu_bar->Append(menu, wxT("&File"));
  511. SetMenuBar( menu_bar );
  512. wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
  513. main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
  514. main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
  515. main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
  516. main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
  517. wxPanel *panel = new wxPanel( this, -1, wxDefaultPosition, wxDefaultSize,
  518. wxTAB_TRAVERSAL | wxSUNKEN_BORDER );
  519. main_sizer->Add( panel, 0, wxALIGN_CENTER );
  520. wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
  521. panel->SetSizer( panel_sizer );
  522. panel_sizer->Add( new wxStaticText( panel, -1, wxT("Hello inside") ) );
  523. panel_sizer->Add( new wxStaticText( panel, -1, wxT("Hello inside") ) );
  524. panel_sizer->Add( new wxStaticText( panel, -1, wxT("Hello inside") ) );
  525. main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
  526. m_target = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
  527. main_sizer->Add( m_target, 1, wxALL|wxGROW, 5 );
  528. SetSizerAndFit( main_sizer);
  529. }
  530. // ----------------------------------------------------------------------------
  531. // MyWrapSizerFrame
  532. // ----------------------------------------------------------------------------
  533. wxBEGIN_EVENT_TABLE(MyWrapSizerFrame, wxFrame)
  534. EVT_MENU(wxID_ADD, MyWrapSizerFrame::OnAddCheckbox)
  535. EVT_MENU(wxID_REMOVE, MyWrapSizerFrame::OnRemoveCheckbox)
  536. wxEND_EVENT_TABLE()
  537. MyWrapSizerFrame::MyWrapSizerFrame(wxFrame* parent)
  538. : wxFrame(parent, wxID_ANY, "Wrap Sizer Test Frame",
  539. wxDefaultPosition, wxSize(200,-1))
  540. {
  541. wxMenu *menu = new wxMenu;
  542. menu->Append(wxID_ADD, "&Add a checkbox\tCtrl-+");
  543. menu->Append(wxID_REMOVE, "&Remove a checkbox\tCtrl--");
  544. wxMenuBar *menu_bar = new wxMenuBar;
  545. menu_bar->Append(menu, "&Wrap sizer");
  546. SetMenuBar( menu_bar );
  547. wxBoxSizer *root = new wxBoxSizer( wxVERTICAL );
  548. wxStaticBoxSizer *topSizer = new wxStaticBoxSizer( wxVERTICAL, this, "Wrapping check-boxes" );
  549. m_checkboxParent = topSizer->GetStaticBox();
  550. m_wrapSizer = new wxWrapSizer(wxHORIZONTAL);
  551. // A number of checkboxes inside a wrap sizer
  552. for( int i = 0; i < 6; i++ )
  553. DoAddCheckbox();
  554. topSizer->Add( m_wrapSizer, wxSizerFlags(1).Expand());
  555. root->Add( topSizer, wxSizerFlags().Expand().Border());
  556. // A shaped item inside a box sizer
  557. wxSizer *bottomSizer = new wxStaticBoxSizer( wxVERTICAL, this, "With wxSHAPED item" );
  558. wxSizer *horzBoxSizer = new wxBoxSizer(wxHORIZONTAL);
  559. bottomSizer->Add( horzBoxSizer, 100, wxEXPAND );
  560. horzBoxSizer->Add( new wxListBox(this,wxID_ANY,wxPoint(0,0),wxSize(70,70)), 0, wxEXPAND|wxSHAPED );
  561. horzBoxSizer->Add( 10,10 );
  562. horzBoxSizer->Add( new wxCheckBox(this,wxID_ANY,"A much longer option..."), 100, 0, 5 );
  563. root->Add( bottomSizer, 1, wxEXPAND | wxALL, 5 );
  564. // Set sizer for window
  565. SetSizerAndFit( root );
  566. }
  567. void MyWrapSizerFrame::DoAddCheckbox()
  568. {
  569. m_wrapSizer->Add(new wxCheckBox
  570. (
  571. m_checkboxParent,
  572. wxID_ANY,
  573. wxString::Format
  574. (
  575. "Option %d",
  576. (int)m_wrapSizer->GetItemCount() + 1
  577. )
  578. ),
  579. wxSizerFlags().Centre().Border());
  580. }
  581. void MyWrapSizerFrame::OnAddCheckbox(wxCommandEvent& WXUNUSED(event))
  582. {
  583. DoAddCheckbox();
  584. Layout();
  585. }
  586. void MyWrapSizerFrame::OnRemoveCheckbox(wxCommandEvent& WXUNUSED(event))
  587. {
  588. if ( m_wrapSizer->IsEmpty() )
  589. {
  590. wxLogStatus(this, "No more checkboxes to remove.");
  591. return;
  592. }
  593. delete m_wrapSizer->GetItem(m_wrapSizer->GetItemCount() - 1)->GetWindow();
  594. Layout();
  595. }