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