]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/button.cpp
Bring osx class naming into line with the other ports.
[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 #if wxUSE_COMMANDLINKBUTTON
132 *m_chkCommandLink,
133 #endif // wxUSE_COMMANDLINKBUTTON
134 #if wxUSE_MARKUP
135 *m_chkUseMarkup,
136 #endif // wxUSE_MARKUP
137 *m_chkDefault;
138
139 // more checkboxes for wxBitmapButton only
140 wxCheckBox *m_chkUsePressed,
141 *m_chkUseFocused,
142 *m_chkUseCurrent,
143 *m_chkUseDisabled;
144
145 // and an image position choice used if m_chkTextAndBitmap is on
146 wxRadioBox *m_radioImagePos;
147
148 wxRadioBox *m_radioHAlign,
149 *m_radioVAlign;
150
151 // the button itself and the sizer it is in
152 wxButton *m_button;
153
154 #if wxUSE_COMMANDLINKBUTTON
155 // same as m_button or NULL if not showing a command link button currently
156 wxCommandLinkButton *m_cmdLnkButton;
157 #endif // wxUSE_COMMANDLINKBUTTON
158
159 wxSizer *m_sizerButton;
160
161 // the text entries for command parameters
162 wxTextCtrl *m_textLabel;
163
164 #if wxUSE_COMMANDLINKBUTTON
165 wxTextCtrl *m_textNote;
166
167 // used to hide or show button for changing note
168 wxSizer *m_sizerNote;
169 #endif // wxUSE_COMMANDLINKBUTTON
170
171 private:
172 DECLARE_EVENT_TABLE()
173 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
174 };
175
176 // ----------------------------------------------------------------------------
177 // event tables
178 // ----------------------------------------------------------------------------
179
180 BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
181 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
182
183 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
184 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
185 EVT_BUTTON(ButtonPage_ChangeNote, ButtonWidgetsPage::OnButtonChangeNote)
186
187 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
188 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
189 END_EVENT_TABLE()
190
191 // ============================================================================
192 // implementation
193 // ============================================================================
194
195 #if defined(__WXUNIVERSAL__)
196 #define FAMILY_CTRLS UNIVERSAL_CTRLS
197 #else
198 #define FAMILY_CTRLS NATIVE_CTRLS
199 #endif
200
201 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, wxT("Button"), FAMILY_CTRLS );
202
203 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
204 wxImageList *imaglist)
205 : WidgetsPage(book, imaglist, button_xpm)
206 {
207 // init everything
208 m_chkBitmapOnly =
209 m_chkTextAndBitmap =
210 m_chkFit =
211 m_chkAuthNeeded =
212 #if wxUSE_COMMANDLINKBUTTON
213 m_chkCommandLink =
214 #endif // wxUSE_COMMANDLINKBUTTON
215 #if wxUSE_MARKUP
216 m_chkUseMarkup =
217 #endif // wxUSE_MARKUP
218 m_chkDefault =
219 m_chkUsePressed =
220 m_chkUseFocused =
221 m_chkUseCurrent =
222 m_chkUseDisabled = (wxCheckBox *)NULL;
223
224 m_radioImagePos =
225 m_radioHAlign =
226 m_radioVAlign = (wxRadioBox *)NULL;
227
228 m_textLabel = (wxTextCtrl *)NULL;
229
230 m_button = (wxButton *)NULL;
231 m_sizerButton = (wxSizer *)NULL;
232 }
233
234 void ButtonWidgetsPage::CreateContent()
235 {
236 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
237
238 // left pane
239 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
240
241 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
242
243 m_chkBitmapOnly = CreateCheckBoxAndAddToSizer(sizerLeft, "&Bitmap only");
244 m_chkTextAndBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, "Text &and bitmap");
245 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Fit exactly"));
246 m_chkAuthNeeded = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Require a&uth"));
247 #if wxUSE_COMMANDLINKBUTTON
248 m_chkCommandLink = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Use command &link button"));
249 #endif
250 #if wxUSE_MARKUP
251 m_chkUseMarkup = CreateCheckBoxAndAddToSizer(sizerLeft, "Interpret &markup");
252 #endif // wxUSE_MARKUP
253 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Default"));
254
255 sizerLeft->AddSpacer(5);
256
257 wxSizer *sizerUseLabels =
258 new wxStaticBoxSizer(wxVERTICAL, this,
259 "&Use the following bitmaps in addition to the normal one?");
260 m_chkUsePressed = CreateCheckBoxAndAddToSizer(sizerUseLabels,
261 "&Pressed (small help icon)");
262 m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels,
263 "&Focused (small error icon)");
264 m_chkUseCurrent = CreateCheckBoxAndAddToSizer(sizerUseLabels,
265 "&Current (small warning icon)");
266 m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels,
267 "&Disabled (broken image icon)");
268 sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border());
269
270 sizerLeft->AddSpacer(10);
271
272 static const wxString dirs[] =
273 {
274 "left", "right", "top", "bottom",
275 };
276 m_radioImagePos = new wxRadioBox(this, wxID_ANY, "Image &position",
277 wxDefaultPosition, wxDefaultSize,
278 WXSIZEOF(dirs), dirs);
279 sizerLeft->Add(m_radioImagePos, 0, wxGROW | wxALL, 5);
280 sizerLeft->AddSpacer(15);
281
282 // should be in sync with enums Button[HV]Align!
283 static const wxString halign[] =
284 {
285 wxT("left"),
286 wxT("centre"),
287 wxT("right"),
288 };
289
290 static const wxString valign[] =
291 {
292 wxT("top"),
293 wxT("centre"),
294 wxT("bottom"),
295 };
296
297 m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"),
298 wxDefaultPosition, wxDefaultSize,
299 WXSIZEOF(halign), halign);
300 m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"),
301 wxDefaultPosition, wxDefaultSize,
302 WXSIZEOF(valign), valign);
303
304 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
305 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
306
307 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
308
309 wxButton *btn = new wxButton(this, ButtonPage_Reset, wxT("&Reset"));
310 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
311
312 // middle pane
313 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Operations"));
314 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
315
316 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
317 wxT("Change label"),
318 wxID_ANY,
319 &m_textLabel);
320 m_textLabel->SetValue(wxT("&Press me!"));
321 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
322
323 #if wxUSE_COMMANDLINKBUTTON
324 m_sizerNote = CreateSizerWithTextAndButton(ButtonPage_ChangeNote,
325 wxT("Change note"),
326 wxID_ANY,
327 &m_textNote);
328 m_textNote->SetValue(wxT("Writes down button clicks in the log."));
329
330 sizerMiddle->Add(m_sizerNote, 0, wxALL | wxGROW, 5);
331 #endif
332
333 // right pane
334 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
335 m_sizerButton->SetMinSize(150, 0);
336
337 // the 3 panes panes compose the window
338 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
339 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
340 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
341
342 // do create the main control
343 Reset();
344 CreateButton();
345
346 SetSizer(sizerTop);
347 }
348
349 // ----------------------------------------------------------------------------
350 // operations
351 // ----------------------------------------------------------------------------
352
353 void ButtonWidgetsPage::Reset()
354 {
355 m_chkBitmapOnly->SetValue(false);
356 m_chkFit->SetValue(true);
357 m_chkAuthNeeded->SetValue(false);
358 m_chkTextAndBitmap->SetValue(false);
359 m_chkDefault->SetValue(false);
360 #if wxUSE_COMMANDLINKBUTTON
361 m_chkCommandLink->SetValue(false);
362 #endif
363 #if wxUSE_MARKUP
364 m_chkUseMarkup->SetValue(false);
365 #endif // wxUSE_MARKUP
366
367 m_chkUsePressed->SetValue(true);
368 m_chkUseFocused->SetValue(true);
369 m_chkUseCurrent->SetValue(true);
370 m_chkUseDisabled->SetValue(true);
371
372 m_radioImagePos->SetSelection(ButtonImagePos_Left);
373 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
374 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
375 }
376
377 void ButtonWidgetsPage::CreateButton()
378 {
379 wxString label;
380 if ( m_button )
381 {
382 #if wxUSE_COMMANDLINKBUTTON
383 if ( m_cmdLnkButton )
384 label = m_cmdLnkButton->GetMainLabel();
385 else
386 #endif
387 label = m_button->GetLabel();
388
389 size_t count = m_sizerButton->GetChildren().GetCount();
390 for ( size_t n = 0; n < count; n++ )
391 {
392 m_sizerButton->Remove( 0 );
393 }
394
395 delete m_button;
396 }
397
398 if ( label.empty() )
399 {
400 // creating for the first time or recreating a button after bitmap
401 // button
402 label = m_textLabel->GetValue();
403 }
404
405 int flags = ms_defaultFlags;
406 switch ( m_radioHAlign->GetSelection() )
407 {
408 case ButtonHAlign_Left:
409 flags |= wxBU_LEFT;
410 break;
411
412 default:
413 wxFAIL_MSG(wxT("unexpected radiobox selection"));
414 // fall through
415
416 case ButtonHAlign_Centre:
417 break;
418
419 case ButtonHAlign_Right:
420 flags |= wxBU_RIGHT;
421 break;
422 }
423
424 switch ( m_radioVAlign->GetSelection() )
425 {
426 case ButtonVAlign_Top:
427 flags |= wxBU_TOP;
428 break;
429
430 default:
431 wxFAIL_MSG(wxT("unexpected radiobox selection"));
432 // fall through
433
434 case ButtonVAlign_Centre:
435 // centre vertical alignment is the default (no style)
436 break;
437
438 case ButtonVAlign_Bottom:
439 flags |= wxBU_BOTTOM;
440 break;
441 }
442
443 #if wxUSE_COMMANDLINKBUTTON
444 m_sizerNote->Show(m_chkCommandLink->GetValue());
445 #endif
446
447 bool showsBitmap = false;
448 if ( m_chkBitmapOnly->GetValue() )
449 {
450 showsBitmap = true;
451
452 wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
453 CreateBitmap(wxT("normal")));
454 if ( m_chkUsePressed->GetValue() )
455 bbtn->SetBitmapPressed(CreateBitmap(wxT("pushed")));
456 if ( m_chkUseFocused->GetValue() )
457 bbtn->SetBitmapFocus(CreateBitmap(wxT("focused")));
458 if ( m_chkUseCurrent->GetValue() )
459 bbtn->SetBitmapCurrent(CreateBitmap(wxT("hover")));
460 if ( m_chkUseDisabled->GetValue() )
461 bbtn->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
462 m_button = bbtn;
463 #if wxUSE_COMMANDLINKBUTTON
464 m_cmdLnkButton = NULL;
465 #endif
466 }
467 else // normal button
468 {
469 #if wxUSE_COMMANDLINKBUTTON
470 m_cmdLnkButton = NULL;
471
472 if ( m_chkCommandLink->GetValue() )
473 {
474 m_cmdLnkButton = new wxCommandLinkButton(this, ButtonPage_Button,
475 label,
476 m_textNote->GetValue(),
477 wxDefaultPosition,
478 wxDefaultSize,
479 flags);
480 m_button = m_cmdLnkButton;
481 }
482 else
483 #endif // wxUSE_COMMANDLINKBUTTON
484 {
485 m_button = new wxButton(this, ButtonPage_Button, label,
486 wxDefaultPosition, wxDefaultSize,
487 flags);
488 }
489 }
490
491 if ( !showsBitmap && m_chkTextAndBitmap->GetValue() )
492 {
493 showsBitmap = true;
494
495 static const wxDirection positions[] =
496 {
497 wxLEFT, wxRIGHT, wxTOP, wxBOTTOM
498 };
499
500 m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_BUTTON),
501 positions[m_radioImagePos->GetSelection()]);
502
503 if ( m_chkUsePressed->GetValue() )
504 m_button->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP, wxART_BUTTON));
505 if ( m_chkUseFocused->GetValue() )
506 m_button->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR, wxART_BUTTON));
507 if ( m_chkUseCurrent->GetValue() )
508 m_button->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING, wxART_BUTTON));
509 if ( m_chkUseDisabled->GetValue() )
510 m_button->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE, wxART_BUTTON));
511 }
512
513 m_chkUsePressed->Enable(showsBitmap);
514 m_chkUseFocused->Enable(showsBitmap);
515 m_chkUseCurrent->Enable(showsBitmap);
516 m_chkUseDisabled->Enable(showsBitmap);
517
518 if ( m_chkAuthNeeded->GetValue() )
519 m_button->SetAuthNeeded();
520
521 if ( m_chkDefault->GetValue() )
522 m_button->SetDefault();
523
524 AddButtonToSizer();
525
526 m_sizerButton->Layout();
527 }
528
529 void ButtonWidgetsPage::AddButtonToSizer()
530 {
531 if ( m_chkFit->GetValue() )
532 {
533 m_sizerButton->AddStretchSpacer(1);
534 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
535 m_sizerButton->AddStretchSpacer(1);
536 }
537 else // take up the entire space
538 {
539 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
540 }
541 }
542
543 // ----------------------------------------------------------------------------
544 // event handlers
545 // ----------------------------------------------------------------------------
546
547 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
548 {
549 Reset();
550
551 CreateButton();
552 }
553
554 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
555 {
556 CreateButton();
557 Layout(); // make sure the text field for changing note displays correctly.
558 }
559
560 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
561 {
562 const wxString labelText = m_textLabel->GetValue();
563
564 #if wxUSE_COMMANDLINKBUTTON
565 if ( m_cmdLnkButton )
566 m_cmdLnkButton->SetMainLabel(labelText);
567 else
568 #endif // wxUSE_COMMANDLINKBUTTON
569 {
570 #if wxUSE_MARKUP
571 if ( m_chkUseMarkup->GetValue() )
572 m_button->SetLabelMarkup(labelText);
573 else
574 #endif // wxUSE_MARKUP
575 m_button->SetLabel(labelText);
576 }
577
578 m_sizerButton->Layout();
579 }
580
581 void ButtonWidgetsPage::OnButtonChangeNote(wxCommandEvent& WXUNUSED(event))
582 {
583 #if wxUSE_COMMANDLINKBUTTON
584 m_cmdLnkButton->SetNote(m_textNote->GetValue());
585
586 m_sizerButton->Layout();
587 #endif // wxUSE_COMMANDLINKBUTTON
588 }
589
590 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
591 {
592 wxLogMessage(wxT("Test button clicked."));
593 }
594
595 // ----------------------------------------------------------------------------
596 // bitmap button stuff
597 // ----------------------------------------------------------------------------
598
599 wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
600 {
601 wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
602 wxMemoryDC dc;
603 dc.SelectObject(bmp);
604 dc.SetBackground(wxBrush(*wxCYAN));
605 dc.Clear();
606 dc.SetTextForeground(*wxBLACK);
607 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + wxT("\n")
608 wxT("(") + label + wxT(" state)"),
609 wxArtProvider::GetBitmap(wxART_INFORMATION),
610 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
611 wxALIGN_CENTRE);
612
613 return bmp;
614 }
615