collpane.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: collpane.cpp
  3. // Purpose: wxCollapsiblePane sample
  4. // Author: Francesco Montorsi
  5. // Modified by:
  6. // Created: 14/10/06
  7. // Copyright: (c) Francesco Montorsi
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #ifndef WX_PRECOMP
  22. #include "wx/log.h"
  23. #include "wx/app.h"
  24. #include "wx/frame.h"
  25. #include "wx/scrolwin.h"
  26. #include "wx/menu.h"
  27. #include "wx/textdlg.h" // for wxGetTextFromUser
  28. #endif
  29. #include "wx/collpane.h"
  30. #include "wx/sizer.h"
  31. #include "wx/stattext.h"
  32. #include "wx/clrpicker.h"
  33. #include "wx/filepicker.h"
  34. #include "wx/fontpicker.h"
  35. #include "wx/aboutdlg.h"
  36. #ifndef wxHAS_IMAGES_IN_RESOURCES
  37. #include "../sample.xpm"
  38. #endif
  39. // ----------------------------------------------------------------------------
  40. // constants
  41. // ----------------------------------------------------------------------------
  42. // ID for the menu commands
  43. enum
  44. {
  45. PANE_COLLAPSE = 100,
  46. PANE_EXPAND,
  47. PANE_SETLABEL,
  48. PANE_SHOWDLG,
  49. PANE_ABOUT = wxID_ABOUT,
  50. PANE_QUIT = wxID_EXIT,
  51. PANE_BUTTON,
  52. PANE_TEXTCTRL
  53. };
  54. // ----------------------------------------------------------------------------
  55. // our classes
  56. // ----------------------------------------------------------------------------
  57. class MyApp: public wxApp
  58. {
  59. public:
  60. MyApp() { }
  61. virtual bool OnInit();
  62. wxDECLARE_NO_COPY_CLASS(MyApp);
  63. };
  64. class MyFrame: public wxFrame
  65. {
  66. public:
  67. MyFrame();
  68. virtual ~MyFrame();
  69. // Menu commands
  70. void OnCollapse(wxCommandEvent& event);
  71. void OnExpand(wxCommandEvent& event);
  72. void OnSetLabel(wxCommandEvent& event);
  73. void OnShowDialog(wxCommandEvent& event);
  74. void Quit(wxCommandEvent& event);
  75. void OnAbout(wxCommandEvent& event);
  76. // UI update handlers
  77. void OnCollapseUpdateUI(wxUpdateUIEvent& event);
  78. void OnExpandUpdateUI(wxUpdateUIEvent& event);
  79. private:
  80. wxCollapsiblePane *m_collPane;
  81. wxBoxSizer *m_paneSizer;
  82. wxDECLARE_EVENT_TABLE();
  83. wxDECLARE_NO_COPY_CLASS(MyFrame);
  84. };
  85. class MyDialog : public wxDialog
  86. {
  87. public:
  88. MyDialog(wxFrame *parent);
  89. void OnToggleStatus(wxCommandEvent& WXUNUSED(ev));
  90. void OnAlignButton(wxCommandEvent& WXUNUSED(ev));
  91. void OnPaneChanged(wxCollapsiblePaneEvent& event);
  92. private:
  93. wxCollapsiblePane *m_collPane;
  94. wxGridSizer *m_paneSizer;
  95. wxDECLARE_EVENT_TABLE();
  96. wxDECLARE_NO_COPY_CLASS(MyDialog);
  97. };
  98. // ============================================================================
  99. // implementation
  100. // ============================================================================
  101. // ----------------------------------------------------------------------------
  102. // MyApp
  103. // ----------------------------------------------------------------------------
  104. IMPLEMENT_APP(MyApp)
  105. bool MyApp::OnInit()
  106. {
  107. if ( !wxApp::OnInit() )
  108. return false;
  109. // create and show the main frame
  110. MyFrame* frame = new MyFrame;
  111. frame->Show(true);
  112. return true;
  113. }
  114. // ----------------------------------------------------------------------------
  115. // MyFrame
  116. // ----------------------------------------------------------------------------
  117. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  118. EVT_MENU(PANE_COLLAPSE, MyFrame::OnCollapse)
  119. EVT_MENU(PANE_EXPAND, MyFrame::OnExpand)
  120. EVT_MENU(PANE_SETLABEL, MyFrame::OnSetLabel)
  121. EVT_MENU(PANE_SHOWDLG, MyFrame::OnShowDialog)
  122. EVT_MENU(PANE_ABOUT, MyFrame::OnAbout)
  123. EVT_MENU(PANE_QUIT, MyFrame::Quit)
  124. EVT_UPDATE_UI(PANE_COLLAPSE, MyFrame::OnCollapseUpdateUI)
  125. EVT_UPDATE_UI(PANE_EXPAND, MyFrame::OnExpandUpdateUI)
  126. wxEND_EVENT_TABLE()
  127. // My frame constructor
  128. MyFrame::MyFrame()
  129. : wxFrame(NULL, wxID_ANY, wxT("wxCollapsiblePane sample"),
  130. wxDefaultPosition, wxSize(420, 300),
  131. wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
  132. {
  133. SetIcon(wxICON(sample));
  134. #if wxUSE_STATUSBAR
  135. CreateStatusBar(2);
  136. #endif // wxUSE_STATUSBAR
  137. // Make a menubar
  138. wxMenu *paneMenu = new wxMenu;
  139. paneMenu->Append(PANE_COLLAPSE, wxT("Collapse\tCtrl-C"));
  140. paneMenu->Append(PANE_EXPAND, wxT("Expand\tCtrl-E"));
  141. paneMenu->AppendSeparator();
  142. paneMenu->Append(PANE_SETLABEL, wxT("Set label...\tCtrl-L"));
  143. paneMenu->AppendSeparator();
  144. paneMenu->Append(PANE_SHOWDLG, wxT("Show dialog...\tCtrl-S"));
  145. paneMenu->AppendSeparator();
  146. paneMenu->Append(PANE_QUIT);
  147. wxMenu *helpMenu = new wxMenu;
  148. helpMenu->Append(PANE_ABOUT);
  149. wxMenuBar *menuBar = new wxMenuBar;
  150. menuBar->Append(paneMenu, wxT("&Pane"));
  151. menuBar->Append(helpMenu, wxT("&Help"));
  152. SetMenuBar(menuBar);
  153. m_collPane = new wxCollapsiblePane(this, -1, wxT("test!"));
  154. wxWindow *win = m_collPane->GetPane();
  155. m_paneSizer = new wxBoxSizer( wxHORIZONTAL );
  156. wxBoxSizer* paneSubSizer = new wxBoxSizer( wxVERTICAL );
  157. m_paneSizer->AddSpacer( 20 );
  158. m_paneSizer->Add( paneSubSizer, 1 );
  159. paneSubSizer->Add( new wxStaticText(win, -1, wxT("Static text") ), 0, wxALIGN_LEFT | wxALL, 3 );
  160. paneSubSizer->Add( new wxStaticText(win, -1, wxT("Yet another one!") ), 0, wxALIGN_LEFT | wxALL, 3 );
  161. paneSubSizer->Add( new wxTextCtrl(win, PANE_TEXTCTRL, wxT("Text control"), wxDefaultPosition, wxSize(80,-1) ), 0, wxALIGN_LEFT | wxALL, 3 );
  162. paneSubSizer->Add( new wxButton(win, PANE_BUTTON, wxT("Press to align right") ), 0, wxALIGN_LEFT | wxALL, 3 );
  163. win->SetSizer( m_paneSizer );
  164. }
  165. MyFrame::~MyFrame()
  166. {
  167. }
  168. // menu command handlers
  169. void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
  170. {
  171. Close(true);
  172. }
  173. void MyFrame::OnCollapse(wxCommandEvent& WXUNUSED(event) )
  174. {
  175. m_collPane->Collapse();
  176. }
  177. void MyFrame::OnExpand(wxCommandEvent& WXUNUSED(event) )
  178. {
  179. m_collPane->Expand();
  180. }
  181. void MyFrame::OnSetLabel(wxCommandEvent& WXUNUSED(event) )
  182. {
  183. wxString text = wxGetTextFromUser
  184. (
  185. wxT("Enter new label"),
  186. wxGetTextFromUserPromptStr,
  187. m_collPane->GetLabel()
  188. );
  189. m_collPane->SetLabel(text);
  190. }
  191. void MyFrame::OnShowDialog(wxCommandEvent& WXUNUSED(event) )
  192. {
  193. MyDialog dlg(this);
  194. dlg.ShowModal();
  195. }
  196. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
  197. {
  198. wxAboutDialogInfo info;
  199. info.SetName(_("wxCollapsiblePane sample"));
  200. info.SetDescription(_("This sample program demonstrates usage of wxCollapsiblePane"));
  201. info.SetCopyright(wxT("(C) 2006 Francesco Montorsi <frm@users.sourceforge.net>"));
  202. wxAboutBox(info);
  203. }
  204. void MyFrame::OnCollapseUpdateUI(wxUpdateUIEvent& event)
  205. {
  206. event.Enable(!m_collPane->IsCollapsed());
  207. }
  208. void MyFrame::OnExpandUpdateUI(wxUpdateUIEvent& event)
  209. {
  210. event.Enable(m_collPane->IsCollapsed());
  211. }
  212. // ----------------------------------------------------------------------------
  213. // MyDialog
  214. // ----------------------------------------------------------------------------
  215. enum
  216. {
  217. PANEDLG_TOGGLESTATUS_BTN = wxID_HIGHEST
  218. };
  219. wxBEGIN_EVENT_TABLE(MyDialog, wxDialog)
  220. EVT_BUTTON(PANEDLG_TOGGLESTATUS_BTN, MyDialog::OnToggleStatus)
  221. EVT_COLLAPSIBLEPANE_CHANGED(wxID_ANY, MyDialog::OnPaneChanged)
  222. EVT_BUTTON(PANE_BUTTON, MyDialog::OnAlignButton)
  223. wxEND_EVENT_TABLE()
  224. MyDialog::MyDialog(wxFrame *parent)
  225. : wxDialog(parent, wxID_ANY, wxT("Test dialog"),
  226. wxDefaultPosition, wxDefaultSize,
  227. wxRESIZE_BORDER|wxDEFAULT_DIALOG_STYLE )
  228. {
  229. wxSizer *sz = new wxBoxSizer(wxVERTICAL);
  230. sz->Add(new wxStaticText(this, -1,
  231. wxT("This dialog allows you to test the wxCollapsiblePane control")),
  232. 0, wxALL, 5);
  233. sz->Add(new wxButton(this, PANEDLG_TOGGLESTATUS_BTN, wxT("Change status")),
  234. 1, wxGROW|wxALL, 5);
  235. m_collPane = new wxCollapsiblePane(this, -1, wxT("Click here for a surprise"));
  236. sz->Add(m_collPane, 0, wxGROW|wxALL, 5);
  237. sz->Add(new wxTextCtrl(this, -1, wxT("just a test")), 0, wxGROW|wxALL, 5);
  238. sz->AddSpacer(10);
  239. sz->Add(new wxButton(this, wxID_OK), 0, wxALIGN_RIGHT|wxALL, 5);
  240. // now add test controls in the collapsible pane
  241. wxWindow *win = m_collPane->GetPane();
  242. m_paneSizer = new wxGridSizer(4, 1, 5, 5);
  243. m_paneSizer->Add( new wxStaticText(win, -1, wxT("Static text") ), 0, wxALIGN_LEFT );
  244. m_paneSizer->Add( new wxStaticText(win, -1, wxT("Yet another one!") ), 0, wxALIGN_LEFT );
  245. m_paneSizer->Add( new wxTextCtrl(win, PANE_TEXTCTRL, wxT("Text control"), wxDefaultPosition, wxSize(80,-1) ), 0, wxALIGN_LEFT );
  246. m_paneSizer->Add( new wxButton(win, PANE_BUTTON, wxT("Press to align right") ), 0, wxALIGN_LEFT );
  247. win->SetSizer( m_paneSizer );
  248. win->SetSizer( m_paneSizer );
  249. m_paneSizer->SetSizeHints(win);
  250. SetSizer(sz);
  251. sz->SetSizeHints(this);
  252. }
  253. void MyDialog::OnToggleStatus(wxCommandEvent& WXUNUSED(ev))
  254. {
  255. m_collPane->Collapse(!m_collPane->IsCollapsed());
  256. }
  257. void MyDialog::OnAlignButton(wxCommandEvent& WXUNUSED(ev))
  258. {
  259. wxSizerItem *item = m_paneSizer->GetItem( FindWindow(PANE_TEXTCTRL), true );
  260. item->SetFlag( wxALIGN_RIGHT );
  261. Layout();
  262. }
  263. void MyDialog::OnPaneChanged(wxCollapsiblePaneEvent& event)
  264. {
  265. wxLogMessage(wxT("The pane has just been %s by the user"),
  266. event.GetCollapsed() ? wxT("collapsed") : wxT("expanded"));
  267. }