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