]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/button.cpp
Remember last values used in exec sample "Kill" menu item dialogs.
[wxWidgets.git] / samples / widgets / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: button.cpp
4 // Purpose: Part of the widgets sample showing wxButton
5 // Author: Vadim Zeitlin
6 // Created: 10.04.01
7 // Id: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
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 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
31
32 #include "wx/bmpbuttn.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
38 #endif
39
40 #include "wx/artprov.h"
41 #include "wx/sizer.h"
42 #include "wx/dcmemory.h"
43 #include "wx/commandlinkbutton.h"
44
45 #include "widgets.h"
46
47 #include "icons/button.xpm"
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 // control ids
54 enum
55 {
56 ButtonPage_Reset = wxID_HIGHEST,
57 ButtonPage_ChangeLabel,
58 ButtonPage_ChangeNote,
59 ButtonPage_Button
60 };
61
62 // radio boxes
63 enum
64 {
65 ButtonImagePos_Left,
66 ButtonImagePos_Right,
67 ButtonImagePos_Top,
68 ButtonImagePos_Bottom
69 };
70
71 enum
72 {
73 ButtonHAlign_Left,
74 ButtonHAlign_Centre,
75 ButtonHAlign_Right
76 };
77
78 enum
79 {
80 ButtonVAlign_Top,
81 ButtonVAlign_Centre,
82 ButtonVAlign_Bottom
83 };
84
85 // ----------------------------------------------------------------------------
86 // ButtonWidgetsPage
87 // ----------------------------------------------------------------------------
88
89 class ButtonWidgetsPage : public WidgetsPage
90 {
91 public:
92 ButtonWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
93 virtual ~ButtonWidgetsPage(){};
94
95 virtual wxControl *GetWidget() const { return m_button; }
96 virtual void RecreateWidget() { CreateButton(); }
97
98 // lazy creation of the content
99 virtual void CreateContent();
100
101 protected:
102 // event handlers
103 void OnCheckOrRadioBox(wxCommandEvent& event);
104
105 void OnButton(wxCommandEvent& event);
106 void OnButtonReset(wxCommandEvent& event);
107 void OnButtonChangeLabel(wxCommandEvent& event);
108 void OnButtonChangeNote(wxCommandEvent& event);
109
110 // reset the wxButton parameters
111 void Reset();
112
113 // (re)create the wxButton
114 void CreateButton();
115
116 // add m_button to m_sizerButton using current value of m_chkFit
117 void AddButtonToSizer();
118
119 // helper function: create a bitmap for wxBitmapButton
120 wxBitmap CreateBitmap(const wxString& label);
121
122
123 // the controls
124 // ------------
125
126 // the check/radio boxes for styles
127 wxCheckBox *m_chkBitmapOnly,
128 *m_chkTextAndBitmap,
129 *m_chkFit,
130 *m_chkAuthNeeded,
131 *m_chkCommandLink,
132 *m_chkDefault;
133
134 // more checkboxes for wxBitmapButton only
135 wxCheckBox *m_chkUsePressed,
136 *m_chkUseFocused,
137 *m_chkUseCurrent,
138 *m_chkUseDisabled;
139
140 // and an image position choice used if m_chkTextAndBitmap is on
141 wxRadioBox *m_radioImagePos;
142
143 wxRadioBox *m_radioHAlign,
144 *m_radioVAlign;
145
146 // the button itself and the sizer it is in
147 wxButton *m_button;
148
149 #if wxUSE_COMMANDLINKBUTTON
150 // same as m_button or NULL if not showing a command link button currently
151 wxCommandLinkButton *m_cmdLnkButton;
152 #endif // wxUSE_COMMANDLINKBUTTON
153
154 wxSizer *m_sizerButton;
155
156 // the text entries for command parameters
157 wxTextCtrl *m_textLabel;
158
159 #if wxUSE_COMMANDLINKBUTTON
160 wxTextCtrl *m_textNote;
161
162 // used to hide or show button for changing note
163 wxSizer *m_sizerNote;
164 #endif // wxUSE_COMMANDLINKBUTTON
165
166 private:
167 DECLARE_EVENT_TABLE()
168 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
169 };
170
171 // ----------------------------------------------------------------------------
172 // event tables
173 // ----------------------------------------------------------------------------
174
175 BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
176 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
177
178 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
179 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
180 EVT_BUTTON(ButtonPage_ChangeNote, ButtonWidgetsPage::OnButtonChangeNote)
181
182 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
183 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
184 END_EVENT_TABLE()
185
186 // ============================================================================
187 // implementation
188 // ============================================================================
189
190 #if defined(__WXUNIVERSAL__)
191 #define FAMILY_CTRLS UNIVERSAL_CTRLS
192 #else
193 #define FAMILY_CTRLS NATIVE_CTRLS
194 #endif
195
196 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, wxT("Button"), FAMILY_CTRLS );
197
198 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
199 wxImageList *imaglist)
200 : WidgetsPage(book, imaglist, button_xpm)
201 {
202 // init everything
203 m_chkBitmapOnly =
204 m_chkTextAndBitmap =
205 m_chkFit =
206 m_chkAuthNeeded =
207 m_chkCommandLink =
208 m_chkDefault =
209 m_chkUsePressed =
210 m_chkUseFocused =
211 m_chkUseCurrent =
212 m_chkUseDisabled = (wxCheckBox *)NULL;
213
214 m_radioImagePos =
215 m_radioHAlign =
216 m_radioVAlign = (wxRadioBox *)NULL;
217
218 m_textLabel = (wxTextCtrl *)NULL;
219
220 m_button = (wxButton *)NULL;
221 m_sizerButton = (wxSizer *)NULL;
222 }
223
224 void ButtonWidgetsPage::CreateContent()
225 {
226 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
227
228 // left pane
229 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
230
231 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
232
233 m_chkBitmapOnly = CreateCheckBoxAndAddToSizer(sizerLeft, "&Bitmap only");
234 m_chkTextAndBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, "Text &and bitmap");
235 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Fit exactly"));
236 m_chkAuthNeeded = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Require a&uth"));
237 #if wxUSE_COMMANDLINKBUTTON
238 m_chkCommandLink = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Use command &link button"));
239 #endif
240 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Default"));
241
242 sizerLeft->AddSpacer(5);
243
244 wxSizer *sizerUseLabels =
245 new wxStaticBoxSizer(wxVERTICAL, this,
246 "&Use the following bitmaps in addition to the normal one?");
247 m_chkUsePressed = CreateCheckBoxAndAddToSizer(sizerUseLabels,
248 "&Pressed (small help icon)");
249 m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels,
250 "&Focused (small error icon)");
251 m_chkUseCurrent = CreateCheckBoxAndAddToSizer(sizerUseLabels,
252 "&Current (small warning icon)");
253 m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels,
254 "&Disabled (broken image icon)");
255 sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border());
256
257 sizerLeft->AddSpacer(10);
258
259 static const wxString dirs[] =
260 {
261 "left", "right", "top", "bottom",
262 };
263 m_radioImagePos = new wxRadioBox(this, wxID_ANY, "Image &position",
264 wxDefaultPosition, wxDefaultSize,
265 WXSIZEOF(dirs), dirs);
266 sizerLeft->Add(m_radioImagePos, 0, wxGROW | wxALL, 5);
267 sizerLeft->AddSpacer(15);
268
269 // should be in sync with enums Button[HV]Align!
270 static const wxString halign[] =
271 {
272 wxT("left"),
273 wxT("centre"),
274 wxT("right"),
275 };
276
277 static const wxString valign[] =
278 {
279 wxT("top"),
280 wxT("centre"),
281 wxT("bottom"),
282 };
283
284 m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"),
285 wxDefaultPosition, wxDefaultSize,
286 WXSIZEOF(halign), halign);
287 m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"),
288 wxDefaultPosition, wxDefaultSize,
289 WXSIZEOF(valign), valign);
290
291 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
292 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
293
294 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
295
296 wxButton *btn = new wxButton(this, ButtonPage_Reset, wxT("&Reset"));
297 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
298
299 // middle pane
300 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Operations"));
301 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
302
303 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
304 wxT("Change label"),
305 wxID_ANY,
306 &m_textLabel);
307 m_textLabel->SetValue(wxT("&Press me!"));
308 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
309
310 #if wxUSE_COMMANDLINKBUTTON
311 m_sizerNote = CreateSizerWithTextAndButton(ButtonPage_ChangeNote,
312 wxT("Change note"),
313 wxID_ANY,
314 &m_textNote);
315 m_textNote->SetValue(wxT("Writes down button clicks in the log."));
316
317 sizerMiddle->Add(m_sizerNote, 0, wxALL | wxGROW, 5);
318 #endif
319
320 // right pane
321 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
322 m_sizerButton->SetMinSize(150, 0);
323
324 // the 3 panes panes compose the window
325 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
326 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
327 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
328
329 // do create the main control
330 Reset();
331 CreateButton();
332
333 SetSizer(sizerTop);
334 }
335
336 // ----------------------------------------------------------------------------
337 // operations
338 // ----------------------------------------------------------------------------
339
340 void ButtonWidgetsPage::Reset()
341 {
342 m_chkBitmapOnly->SetValue(false);
343 m_chkFit->SetValue(true);
344 m_chkAuthNeeded->SetValue(false);
345 m_chkTextAndBitmap->SetValue(false);
346 m_chkDefault->SetValue(false);
347 #if wxUSE_COMMANDLINKBUTTON
348 m_chkCommandLink->SetValue(false);
349 #endif
350
351 m_chkUsePressed->SetValue(true);
352 m_chkUseFocused->SetValue(true);
353 m_chkUseCurrent->SetValue(true);
354 m_chkUseDisabled->SetValue(true);
355
356 m_radioImagePos->SetSelection(ButtonImagePos_Left);
357 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
358 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
359 }
360
361 void ButtonWidgetsPage::CreateButton()
362 {
363 wxString label;
364 if ( m_button )
365 {
366 #if wxUSE_COMMANDLINKBUTTON
367 if ( m_cmdLnkButton )
368 label = m_cmdLnkButton->GetMainLabel();
369 else
370 #endif
371 label = m_button->GetLabel();
372
373 size_t count = m_sizerButton->GetChildren().GetCount();
374 for ( size_t n = 0; n < count; n++ )
375 {
376 m_sizerButton->Remove( 0 );
377 }
378
379 delete m_button;
380 }
381
382 if ( label.empty() )
383 {
384 // creating for the first time or recreating a button after bitmap
385 // button
386 label = m_textLabel->GetValue();
387 }
388
389 int flags = ms_defaultFlags;
390 switch ( m_radioHAlign->GetSelection() )
391 {
392 case ButtonHAlign_Left:
393 flags |= wxBU_LEFT;
394 break;
395
396 default:
397 wxFAIL_MSG(wxT("unexpected radiobox selection"));
398 // fall through
399
400 case ButtonHAlign_Centre:
401 break;
402
403 case ButtonHAlign_Right:
404 flags |= wxBU_RIGHT;
405 break;
406 }
407
408 switch ( m_radioVAlign->GetSelection() )
409 {
410 case ButtonVAlign_Top:
411 flags |= wxBU_TOP;
412 break;
413
414 default:
415 wxFAIL_MSG(wxT("unexpected radiobox selection"));
416 // fall through
417
418 case ButtonVAlign_Centre:
419 // centre vertical alignment is the default (no style)
420 break;
421
422 case ButtonVAlign_Bottom:
423 flags |= wxBU_BOTTOM;
424 break;
425 }
426
427 #if wxUSE_COMMANDLINKBUTTON
428 m_sizerNote->Show(m_chkCommandLink->GetValue());
429 #endif
430
431 bool showsBitmap = false;
432 if ( m_chkBitmapOnly->GetValue() )
433 {
434 showsBitmap = true;
435
436 wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
437 CreateBitmap(wxT("normal")));
438 if ( m_chkUsePressed->GetValue() )
439 bbtn->SetBitmapPressed(CreateBitmap(wxT("pushed")));
440 if ( m_chkUseFocused->GetValue() )
441 bbtn->SetBitmapFocus(CreateBitmap(wxT("focused")));
442 if ( m_chkUseCurrent->GetValue() )
443 bbtn->SetBitmapCurrent(CreateBitmap(wxT("hover")));
444 if ( m_chkUseDisabled->GetValue() )
445 bbtn->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
446 m_button = bbtn;
447 #if wxUSE_COMMANDLINKBUTTON
448 m_cmdLnkButton = NULL;
449 #endif
450 }
451 else // normal button
452 {
453 #if wxUSE_COMMANDLINKBUTTON
454 m_cmdLnkButton = NULL;
455
456 if ( m_chkCommandLink->GetValue() )
457 {
458 m_cmdLnkButton = new wxCommandLinkButton(this, ButtonPage_Button,
459 label,
460 m_textNote->GetValue(),
461 wxDefaultPosition,
462 wxDefaultSize,
463 flags);
464 m_button = m_cmdLnkButton;
465 }
466 else
467 #endif // wxUSE_COMMANDLINKBUTTON
468 {
469 m_button = new wxButton(this, ButtonPage_Button, label,
470 wxDefaultPosition, wxDefaultSize,
471 flags);
472 }
473 }
474
475 if ( !showsBitmap && m_chkTextAndBitmap->GetValue() )
476 {
477 showsBitmap = true;
478
479 static const wxDirection positions[] =
480 {
481 wxLEFT, wxRIGHT, wxTOP, wxBOTTOM
482 };
483
484 m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION),
485 positions[m_radioImagePos->GetSelection()]);
486
487 if ( m_chkUsePressed->GetValue() )
488 m_button->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP));
489 if ( m_chkUseFocused->GetValue() )
490 m_button->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR));
491 if ( m_chkUseCurrent->GetValue() )
492 m_button->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING));
493 if ( m_chkUseDisabled->GetValue() )
494 m_button->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE));
495 }
496
497 m_chkUsePressed->Enable(showsBitmap);
498 m_chkUseFocused->Enable(showsBitmap);
499 m_chkUseCurrent->Enable(showsBitmap);
500 m_chkUseDisabled->Enable(showsBitmap);
501
502 if ( m_chkAuthNeeded->GetValue() )
503 m_button->SetAuthNeeded();
504
505 if ( m_chkDefault->GetValue() )
506 m_button->SetDefault();
507
508 AddButtonToSizer();
509
510 m_sizerButton->Layout();
511 }
512
513 void ButtonWidgetsPage::AddButtonToSizer()
514 {
515 if ( m_chkFit->GetValue() )
516 {
517 m_sizerButton->AddStretchSpacer(1);
518 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
519 m_sizerButton->AddStretchSpacer(1);
520 }
521 else // take up the entire space
522 {
523 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
524 }
525 }
526
527 // ----------------------------------------------------------------------------
528 // event handlers
529 // ----------------------------------------------------------------------------
530
531 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
532 {
533 Reset();
534
535 CreateButton();
536 }
537
538 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
539 {
540 CreateButton();
541 Layout(); // make sure the text field for changing note displays correctly.
542 }
543
544 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
545 {
546 #if wxUSE_COMMANDLINKBUTTON
547 if ( m_cmdLnkButton )
548 m_cmdLnkButton->SetMainLabel(m_textLabel->GetValue());
549 else
550 #endif // wxUSE_COMMANDLINKBUTTON
551 m_button->SetLabel(m_textLabel->GetValue());
552
553 m_sizerButton->Layout();
554 }
555
556 void ButtonWidgetsPage::OnButtonChangeNote(wxCommandEvent& WXUNUSED(event))
557 {
558 #if wxUSE_COMMANDLINKBUTTON
559 m_cmdLnkButton->SetNote(m_textNote->GetValue());
560
561 m_sizerButton->Layout();
562 #endif // wxUSE_COMMANDLINKBUTTON
563 }
564
565 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
566 {
567 wxLogMessage(wxT("Test button clicked."));
568 }
569
570 // ----------------------------------------------------------------------------
571 // bitmap button stuff
572 // ----------------------------------------------------------------------------
573
574 wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
575 {
576 wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
577 wxMemoryDC dc;
578 dc.SelectObject(bmp);
579 dc.SetBackground(wxBrush(*wxCYAN));
580 dc.Clear();
581 dc.SetTextForeground(*wxBLACK);
582 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + wxT("\n")
583 wxT("(") + label + wxT(" state)"),
584 wxArtProvider::GetBitmap(wxART_INFORMATION),
585 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
586 wxALIGN_CENTRE);
587
588 return bmp;
589 }
590