replaced menu calls Append([...], true) with the less cryptic AppendCheckItem
[wxWidgets.git] / samples / dialogs / dialogs.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialogs.cpp
3 // Purpose: Common dialogs demo
4 // Author: Julian Smart
5 // Modified by: ABX (2004) - adjustementd for conditional building
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(__APPLE__)
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/image.h"
29
30 #if wxUSE_COLOURDLG
31 #include "wx/colordlg.h"
32 #endif // wxUSE_COLOURDLG
33
34 #if wxUSE_CHOICEDLG
35 #include "wx/choicdlg.h"
36 #endif // wxUSE_CHOICEDLG
37
38 #if wxUSE_STARTUP_TIPS
39 #include "wx/tipdlg.h"
40 #endif // wxUSE_STARTUP_TIPS
41
42 #if wxUSE_PROGRESSDLG
43 #include "wx/progdlg.h"
44 #endif // wxUSE_PROGRESSDLG
45
46 #if wxUSE_BUSYINFO
47 #include "wx/busyinfo.h"
48 #endif // wxUSE_BUSYINFO
49
50 #if wxUSE_NUMBERDLG
51 #include "wx/numdlg.h"
52 #endif // wxUSE_NUMBERDLG
53
54 #if wxUSE_FILEDLG
55 #include "wx/filedlg.h"
56 #endif // wxUSE_FILEDLG
57
58 #if wxUSE_DIRDLG
59 #include "wx/dirdlg.h"
60 #endif // wxUSE_DIRDLG
61
62 #if wxUSE_FONTDLG
63 #include "wx/fontdlg.h"
64 #endif // wxUSE_FONTDLG
65
66 #if wxUSE_FINDREPLDLG
67 #include "wx/fdrepdlg.h"
68 #endif // wxUSE_FINDREPLDLG
69
70 #include "dialogs.h"
71
72 #if USE_COLOURDLG_GENERIC
73 #include "wx/generic/colrdlgg.h"
74 #endif // USE_COLOURDLG_GENERIC
75
76 #if USE_FONTDLG_GENERIC
77 #include "wx/generic/fontdlgg.h"
78 #endif // USE_FONTDLG_GENERIC
79
80 #if USE_DIRDLG_GENERIC
81 #include "wx/generic/dirdlgg.h"
82 #endif // USE_DIRDLG_GENERIC
83
84 IMPLEMENT_APP(MyApp)
85
86 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
87 EVT_PAINT(MyCanvas::OnPaint)
88 END_EVENT_TABLE()
89
90
91
92 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
93 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
94
95 #if wxUSE_COLOURDLG
96 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
97 #endif // wxUSE_COLOURDLG
98
99 #if wxUSE_FONTDLG
100 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
101 #endif // wxUSE_FONTDLG
102
103 #if wxUSE_LOG_DIALOG
104 EVT_MENU(DIALOGS_LOG_DIALOG, MyFrame::LogDialog)
105 #endif // wxUSE_LOG_DIALOG
106
107 #if wxUSE_TEXTDLG
108 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
109 EVT_MENU(DIALOGS_PASSWORD_ENTRY, MyFrame::PasswordEntry)
110 #endif // wxUSE_TEXTDLG
111
112 #if wxUSE_NUMBERDLG
113 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
114 #endif // wxUSE_NUMBERDLG
115
116 #if wxUSE_CHOICEDLG
117 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
118 EVT_MENU(DIALOGS_MULTI_CHOICE, MyFrame::MultiChoice)
119 #endif // wxUSE_CHOICEDLG
120
121 #if wxUSE_FILEDLG
122 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
123 EVT_MENU(DIALOGS_FILE_OPEN2, MyFrame::FileOpen2)
124 EVT_MENU(DIALOGS_FILES_OPEN, MyFrame::FilesOpen)
125 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
126 #endif // wxUSE_FILEDLG
127
128 #if wxUSE_DIRDLG
129 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
130 EVT_MENU(DIALOGS_DIRNEW_CHOOSE, MyFrame::DirChooseNew)
131 #endif // wxUSE_DIRDLG
132
133 #if USE_MODAL_PRESENTATION
134 EVT_MENU(DIALOGS_MODAL, MyFrame::ModalDlg)
135 EVT_MENU(DIALOGS_MODELESS, MyFrame::ModelessDlg)
136 #endif // USE_MODAL
137
138 #if wxUSE_STARTUP_TIPS
139 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
140 #endif // wxUSE_STARTUP_TIPS
141
142 #if USE_FONTDLG_GENERIC
143 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
144 #endif // USE_FONTDLG_GENERIC
145
146 #if USE_DIRDLG_GENERIC
147 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE, MyFrame::GenericDirChoose)
148 #endif // wxMSW || wxMAC
149
150 #if USE_COLOURDLG_GENERIC
151 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
152 #endif // USE_COLOURDLG_GENERIC
153
154 #if wxUSE_PROGRESSDLG
155 EVT_MENU(DIALOGS_PROGRESS, MyFrame::ShowProgress)
156 #endif // wxUSE_PROGRESSDLG
157
158 #if wxUSE_BUSYINFO
159 EVT_MENU(DIALOGS_BUSYINFO, MyFrame::ShowBusyInfo)
160 #endif // wxUSE_BUSYINFO
161
162 #if wxUSE_FINDREPLDLG
163 EVT_MENU(DIALOGS_FIND, MyFrame::ShowFindDialog)
164 EVT_MENU(DIALOGS_REPLACE, MyFrame::ShowReplaceDialog)
165
166 EVT_FIND(wxID_ANY, MyFrame::OnFindDialog)
167 EVT_FIND_NEXT(wxID_ANY, MyFrame::OnFindDialog)
168 EVT_FIND_REPLACE(wxID_ANY, MyFrame::OnFindDialog)
169 EVT_FIND_REPLACE_ALL(wxID_ANY, MyFrame::OnFindDialog)
170 EVT_FIND_CLOSE(wxID_ANY, MyFrame::OnFindDialog)
171 #endif // wxUSE_FINDREPLDLG
172
173 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
174 END_EVENT_TABLE()
175
176 #if USE_MODAL_PRESENTATION
177
178 BEGIN_EVENT_TABLE(MyModalDialog, wxDialog)
179 EVT_BUTTON(wxID_ANY, MyModalDialog::OnButton)
180 END_EVENT_TABLE()
181
182 BEGIN_EVENT_TABLE(MyModelessDialog, wxDialog)
183 EVT_BUTTON(DIALOGS_MODELESS_BTN, MyModelessDialog::OnButton)
184 EVT_CLOSE(MyModelessDialog::OnClose)
185 END_EVENT_TABLE()
186
187 #endif // USE_MODAL_PRESENTATION
188
189 MyCanvas *myCanvas = (MyCanvas *) NULL;
190
191 // `Main program' equivalent, creating windows and returning main app frame
192 bool MyApp::OnInit()
193 {
194 #if wxUSE_IMAGE
195 wxInitAllImageHandlers();
196 #endif
197
198 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
199 wxConvCurrent = &wxConvLibc;
200 #endif
201
202 m_canvasTextColour = wxColour(_T("BLACK"));
203 m_canvasFont = *wxNORMAL_FONT;
204
205 // Create the main frame window
206 MyFrame *frame = new MyFrame((wxFrame *) NULL, _T("wxWidgets dialogs example"));
207
208 // Make a menubar
209 wxMenu *file_menu = new wxMenu;
210
211 file_menu->Append(DIALOGS_MESSAGE_BOX, _T("&Message box\tCtrl-M"));
212
213
214 #if wxUSE_COLOURDLG || wxUSE_FONTDLG || wxUSE_CHOICEDLG
215
216 wxMenu *choices_menu = new wxMenu;
217
218 #if wxUSE_COLOURDLG
219 choices_menu->Append(DIALOGS_CHOOSE_COLOUR, _T("&Choose colour"));
220 #endif // wxUSE_COLOURDLG
221
222 #if USE_COLOURDLG_GENERIC
223 choices_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, _T("&Choose colour (generic)"));
224 #endif // USE_COLOURDLG_GENERIC
225
226 #if wxUSE_FONTDLG
227 choices_menu->Append(DIALOGS_CHOOSE_FONT, _T("Choose &font"));
228 #endif // wxUSE_FONTDLG
229
230 #if USE_FONTDLG_GENERIC
231 choices_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, _T("Choose &font (generic)"));
232 #endif // USE_FONTDLG_GENERIC
233
234 #if wxUSE_CHOICEDLG
235 choices_menu->Append(DIALOGS_SINGLE_CHOICE, _T("&Single choice\tCtrl-C"));
236 choices_menu->Append(DIALOGS_MULTI_CHOICE, _T("M&ultiple choice\tCtrl-U"));
237 #endif // wxUSE_CHOICEDLG
238
239 file_menu->Append(wxID_ANY,_T("Choices and selectors"),choices_menu);
240
241 #endif // wxUSE_COLOURDLG || wxUSE_FONTDLG || wxUSE_CHOICEDLG
242
243
244 #if wxUSE_TEXTDLG || wxUSE_NUMBERDLG
245
246 wxMenu *entry_menu = new wxMenu;
247
248 #if wxUSE_TEXTDLG
249 entry_menu->Append(DIALOGS_TEXT_ENTRY, _T("Text &entry\tCtrl-E"));
250 entry_menu->Append(DIALOGS_PASSWORD_ENTRY, _T("&Password entry\tCtrl-P"));
251 #endif // wxUSE_TEXTDLG
252
253 #if wxUSE_NUMBERDLG
254 entry_menu->Append(DIALOGS_NUM_ENTRY, _T("&Numeric entry\tCtrl-N"));
255 #endif // wxUSE_NUMBERDLG
256
257 file_menu->Append(wxID_ANY,_T("Entry dialogs"),entry_menu);
258
259 #endif
260
261
262 #if wxUSE_FILEDLG
263
264 wxMenu *filedlg_menu = new wxMenu;
265 filedlg_menu->Append(DIALOGS_FILE_OPEN, _T("&Open file\tCtrl-O"));
266 filedlg_menu->Append(DIALOGS_FILE_OPEN2, _T("&Second open file\tCtrl-2"));
267 filedlg_menu->Append(DIALOGS_FILES_OPEN, _T("Open &files\tCtrl-Q"));
268 filedlg_menu->Append(DIALOGS_FILE_SAVE, _T("Sa&ve file\tCtrl-S"));
269 file_menu->Append(wxID_ANY,_T("File operations"),filedlg_menu);
270
271 #endif // wxUSE_FILEDLG
272
273 #if wxUSE_DIRDLG
274 wxMenu *dir_menu = new wxMenu;
275
276 dir_menu->Append(DIALOGS_DIR_CHOOSE, _T("&Choose a directory\tCtrl-D"));
277
278 #if USE_DIRDLG_GENERIC
279 dir_menu->Append(DIALOGS_GENERIC_DIR_CHOOSE, _T("&Choose a directory (generic)"));
280 #endif // USE_DIRDLG_GENERIC
281
282 dir_menu->Append(DIALOGS_DIRNEW_CHOOSE, _T("Choose a directory (with \"Ne&w\" button)\tShift-Ctrl-D"));
283 file_menu->Append(wxID_ANY,_T("Directory operations"),dir_menu);
284
285 #endif // wxUSE_DIRDLG
286
287
288 #if wxUSE_STARTUP_TIPS || wxUSE_PROGRESSDLG || wxUSE_BUSYINFO || wxUSE_LOG_DIALOG
289
290 wxMenu *info_menu = new wxMenu;
291
292 #if wxUSE_STARTUP_TIPS
293 info_menu->Append(DIALOGS_TIP, _T("&Tip of the day\tCtrl-T"));
294 #endif // wxUSE_STARTUP_TIPS
295
296 #if wxUSE_PROGRESSDLG
297 info_menu->Append(DIALOGS_PROGRESS, _T("Pro&gress dialog\tCtrl-G"));
298 #endif // wxUSE_PROGRESSDLG
299
300 #if wxUSE_BUSYINFO
301 info_menu->Append(DIALOGS_BUSYINFO, _T("&Busy info dialog\tCtrl-B"));
302 #endif // wxUSE_BUSYINFO
303
304 #if wxUSE_LOG_DIALOG
305 info_menu->Append(DIALOGS_LOG_DIALOG, _T("&Log dialog\tCtrl-L"));
306 #endif // wxUSE_LOG_DIALOG
307
308 file_menu->Append(wxID_ANY,_T("Informative dialogs"),info_menu);
309
310 #endif // wxUSE_STARTUP_TIPS || wxUSE_PROGRESSDLG || wxUSE_BUSYINFO || wxUSE_LOG_DIALOG
311
312
313 #if wxUSE_FINDREPLDLG
314 wxMenu *find_menu = new wxMenu;
315 find_menu->AppendCheckItem(DIALOGS_FIND, _T("&Find dialog\tCtrl-F"));
316 find_menu->AppendCheckItem(DIALOGS_REPLACE, _T("Find and &replace dialog\tShift-Ctrl-F"));
317 file_menu->Append(wxID_ANY,_T("Searching"),find_menu);
318 #endif // wxUSE_FINDREPLDLG
319
320 #if USE_MODAL_PRESENTATION
321 wxMenu *modal_menu = new wxMenu;
322 modal_menu->Append(DIALOGS_MODAL, _T("Mo&dal dialog\tCtrl-W"));
323 modal_menu->AppendCheckItem(DIALOGS_MODELESS, _T("Modeless &dialog\tCtrl-Z"));
324 file_menu->Append(wxID_ANY,_T("Modal/Modeless"),modal_menu);
325 #endif // USE_MODAL_PRESENTATION
326
327 file_menu->AppendSeparator();
328 file_menu->Append(wxID_EXIT, _T("E&xit\tAlt-X"));
329
330 wxMenuBar *menu_bar = new wxMenuBar;
331 menu_bar->Append(file_menu, _T("&File"));
332 frame->SetMenuBar(menu_bar);
333
334 myCanvas = new MyCanvas(frame);
335 myCanvas->SetBackgroundColour(*wxWHITE);
336
337 frame->Centre(wxBOTH);
338
339 // Show the frame
340 frame->Show(true);
341
342 SetTopWindow(frame);
343
344 return true;
345 }
346
347 // My frame constructor
348 MyFrame::MyFrame(wxWindow *parent,
349 const wxString& title)
350 : wxFrame(parent, wxID_ANY, title)
351 {
352 #if USE_MODAL_PRESENTATION
353 m_dialog = (MyModelessDialog *)NULL;
354 #endif // USE_MODAL_PRESENTATION
355
356 #if wxUSE_FINDREPLDLG
357 m_dlgFind =
358 m_dlgReplace = NULL;
359 #endif
360
361 #if wxUSE_COLOURDLG
362 m_clrData.SetChooseFull(true);
363 for (int i = 0; i < 16; i++)
364 {
365 m_clrData.SetCustomColour(i, wxColour(i*16, i*16, i*16));
366 }
367 #endif // wxUSE_COLOURDLG
368
369 CreateStatusBar();
370 }
371
372 #if wxUSE_COLOURDLG
373 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
374 {
375 m_clrData.SetColour(myCanvas->GetBackgroundColour());
376
377 wxColourDialog dialog(this, &m_clrData);
378 dialog.SetTitle(_T("Choose the background colour"));
379 if (dialog.ShowModal() == wxID_OK)
380 {
381 m_clrData = dialog.GetColourData();
382 myCanvas->SetBackgroundColour(m_clrData.GetColour());
383 myCanvas->ClearBackground();
384 myCanvas->Refresh();
385 }
386 }
387 #endif // wxUSE_COLOURDLG
388
389 #if wxUSE_FONTDLG
390 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
391 {
392 wxFontData data;
393 data.SetInitialFont(wxGetApp().m_canvasFont);
394 data.SetColour(wxGetApp().m_canvasTextColour);
395
396 // you might also do this:
397 //
398 // wxFontDialog dialog;
399 // if ( !dialog.Create(this, data) { ... error ... }
400 //
401 wxFontDialog dialog(this, data);
402
403 if (dialog.ShowModal() == wxID_OK)
404 {
405 wxFontData retData = dialog.GetFontData();
406 wxGetApp().m_canvasFont = retData.GetChosenFont();
407 wxGetApp().m_canvasTextColour = retData.GetColour();
408 myCanvas->Refresh();
409 }
410 //else: cancelled by the user, don't change the font
411 }
412 #endif // wxUSE_FONTDLG
413
414 #if USE_COLOURDLG_GENERIC
415 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
416 {
417 wxColourData data;
418 data.SetChooseFull(true);
419 for (int i = 0; i < 16; i++)
420 {
421 wxColour colour(i*16, i*16, i*16);
422 data.SetCustomColour(i, colour);
423 }
424
425 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
426 if (dialog->ShowModal() == wxID_OK)
427 {
428 wxColourData retData = dialog->GetColourData();
429 wxColour col = retData.GetColour();
430 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
431 myCanvas->SetBackgroundColour(col);
432 myCanvas->ClearBackground();
433 myCanvas->Refresh();
434 }
435 dialog->Destroy();
436 }
437 #endif // USE_COLOURDLG_GENERIC
438
439 #if USE_FONTDLG_GENERIC
440 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
441 {
442 wxFontData data;
443 data.SetInitialFont(wxGetApp().m_canvasFont);
444 data.SetColour(wxGetApp().m_canvasTextColour);
445
446 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
447 if (dialog->ShowModal() == wxID_OK)
448 {
449 wxFontData retData = dialog->GetFontData();
450 wxGetApp().m_canvasFont = retData.GetChosenFont();
451 wxGetApp().m_canvasTextColour = retData.GetColour();
452 myCanvas->Refresh();
453 }
454 dialog->Destroy();
455 }
456 #endif // USE_FONTDLG_GENERIC
457
458 void MyFrame::LogDialog(wxCommandEvent& WXUNUSED(event))
459 {
460 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
461 // being flushed -- test it
462 {
463 wxBusyCursor bc;
464 wxLogMessage(wxT("This is some message - everything is ok so far."));
465 wxLogMessage(wxT("Another message...\n... this one is on multiple lines"));
466 wxLogWarning(wxT("And then something went wrong!"));
467
468 // and if ~wxBusyCursor doesn't do it, then call it manually
469 wxYield();
470 }
471
472 wxLogError(wxT("Intermediary error handler decided to abort."));
473 wxLogError(wxT("The top level caller detected an unrecoverable error."));
474
475 wxLog::FlushActive();
476
477 wxLogMessage(wxT("And this is the same dialog but with only one message."));
478 }
479
480 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
481 {
482 wxMessageDialog dialog( NULL, _T("This is a message box\nA long, long string to test out the message box properly"),
483 _T("Message box text"), wxNO_DEFAULT|wxYES_NO|wxCANCEL|wxICON_INFORMATION);
484
485 switch ( dialog.ShowModal() )
486 {
487 case wxID_YES:
488 wxLogStatus(wxT("You pressed \"Yes\""));
489 break;
490
491 case wxID_NO:
492 wxLogStatus(wxT("You pressed \"No\""));
493 break;
494
495 case wxID_CANCEL:
496 wxLogStatus(wxT("You pressed \"Cancel\""));
497 break;
498
499 default:
500 wxLogError(wxT("Unexpected wxMessageDialog return code!"));
501 }
502 }
503
504 #if wxUSE_NUMBERDLG
505 void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
506 {
507 long res = wxGetNumberFromUser( _T("This is some text, actually a lot of text.\n")
508 _T("Even two rows of text."),
509 _T("Enter a number:"), _T("Numeric input test"),
510 50, 0, 100, this );
511
512 wxString msg;
513 int icon;
514 if ( res == -1 )
515 {
516 msg = _T("Invalid number entered or dialog cancelled.");
517 icon = wxICON_HAND;
518 }
519 else
520 {
521 msg.Printf(_T("You've entered %lu"), res );
522 icon = wxICON_INFORMATION;
523 }
524
525 wxMessageBox(msg, _T("Numeric test result"), wxOK | icon, this);
526 }
527 #endif // wxUSE_NUMBERDLG
528
529 #if wxUSE_TEXTDLG
530 void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
531 {
532 wxString pwd = wxGetPasswordFromUser(_T("Enter password:"),
533 _T("Password entry dialog"),
534 wxEmptyString,
535 this);
536 if ( !!pwd )
537 {
538 wxMessageBox(wxString::Format(wxT("Your password is '%s'"), pwd.c_str()),
539 _T("Got password"), wxOK | wxICON_INFORMATION, this);
540 }
541 }
542
543 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
544 {
545 wxTextEntryDialog dialog(this,
546 _T("This is a small sample\n")
547 _T("A long, long string to test out the text entrybox"),
548 _T("Please enter a string"),
549 _T("Default value"),
550 wxOK | wxCANCEL);
551
552 if (dialog.ShowModal() == wxID_OK)
553 {
554 wxMessageDialog dialog2(this, dialog.GetValue(), _T("Got string"));
555 dialog2.ShowModal();
556 }
557 }
558 #endif // wxUSE_TEXTDLG
559
560 #if wxUSE_CHOICEDLG
561 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
562 {
563 const wxString choices[] = { _T("One"), _T("Two"), _T("Three"), _T("Four"), _T("Five") } ;
564
565 wxSingleChoiceDialog dialog(this,
566 _T("This is a small sample\n")
567 _T("A single-choice convenience dialog"),
568 _T("Please select a value"),
569 WXSIZEOF(choices), choices);
570
571 dialog.SetSelection(2);
572
573 if (dialog.ShowModal() == wxID_OK)
574 {
575 wxMessageDialog dialog2(this, dialog.GetStringSelection(), _T("Got string"));
576 dialog2.ShowModal();
577 }
578 }
579
580 void MyFrame::MultiChoice(wxCommandEvent& WXUNUSED(event) )
581 {
582 const wxString choices[] =
583 {
584 _T("One"), _T("Two"), _T("Three"), _T("Four"), _T("Five"),
585 _T("Six"), _T("Seven"), _T("Eight"), _T("Nine"), _T("Ten"),
586 _T("Eleven"), _T("Twelve"), _T("Seventeen"),
587 };
588
589 wxArrayInt selections;
590 size_t count = wxGetMultipleChoices(selections,
591 _T("This is a small sample\n")
592 _T("A multi-choice convenience dialog"),
593 _T("Please select a value"),
594 WXSIZEOF(choices), choices,
595 this);
596 if ( count )
597 {
598 wxString msg;
599 msg.Printf(wxT("You selected %u items:\n"), (unsigned)count);
600 for ( size_t n = 0; n < count; n++ )
601 {
602 msg += wxString::Format(wxT("\t%u: %u (%s)\n"),
603 (unsigned)n, (unsigned)selections[n],
604 choices[selections[n]].c_str());
605 }
606 wxLogMessage(msg);
607 }
608 //else: cancelled or nothing selected
609 }
610 #endif // wxUSE_CHOICEDLG
611
612 #if wxUSE_FILEDLG
613 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
614 {
615 wxFileDialog dialog
616 (
617 this,
618 _T("Testing open file dialog"),
619 wxEmptyString,
620 wxEmptyString,
621 #ifdef __WXMOTIF__
622 _T("C++ files (*.cpp)|*.cpp")
623 #else
624 _T("C++ files (*.h;*.cpp)|*.h;*.cpp")
625 #endif
626 );
627
628 dialog.SetDirectory(wxGetHomeDir());
629
630 if (dialog.ShowModal() == wxID_OK)
631 {
632 wxString info;
633 info.Printf(_T("Full file name: %s\n")
634 _T("Path: %s\n")
635 _T("Name: %s"),
636 dialog.GetPath().c_str(),
637 dialog.GetDirectory().c_str(),
638 dialog.GetFilename().c_str());
639 wxMessageDialog dialog2(this, info, _T("Selected file"));
640 dialog2.ShowModal();
641 }
642 }
643
644 // this shows how to take advantage of specifying a default extension in the
645 // call to wxFileSelector: it is remembered after each new call and the next
646 // one will use it by default
647 void MyFrame::FileOpen2(wxCommandEvent& WXUNUSED(event) )
648 {
649 static wxString s_extDef;
650 wxString path = wxFileSelector(
651 _T("Select the file to load"),
652 wxEmptyString, wxEmptyString,
653 s_extDef,
654 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
655 wxCHANGE_DIR,
656 this
657 );
658
659 if ( !path )
660 return;
661
662 // it is just a sample, would use wxSplitPath in real program
663 s_extDef = path.AfterLast(_T('.'));
664
665 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
666 (const wxChar*) path, (const wxChar*) s_extDef);
667 }
668
669 void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
670 {
671 wxString wildcards =
672 #ifdef __WXMOTIF__
673 _T("C++ files (*.cpp)|*.cpp");
674 #else
675 _T("All files (*.*)|*.*|C++ files (*.h;*.cpp)|*.h;*.cpp");
676 #endif
677 wxFileDialog dialog(this, _T("Testing open multiple file dialog"),
678 wxEmptyString, wxEmptyString, wildcards,
679 wxMULTIPLE);
680
681 if (dialog.ShowModal() == wxID_OK)
682 {
683 wxArrayString paths, filenames;
684
685 dialog.GetPaths(paths);
686 dialog.GetFilenames(filenames);
687
688 wxString msg, s;
689 size_t count = paths.GetCount();
690 for ( size_t n = 0; n < count; n++ )
691 {
692 s.Printf(_T("File %d: %s (%s)\n"),
693 (int)n, paths[n].c_str(), filenames[n].c_str());
694
695 msg += s;
696 }
697 s.Printf(_T("Filter index: %d"), dialog.GetFilterIndex());
698 msg += s;
699
700 wxMessageDialog dialog2(this, msg, _T("Selected files"));
701 dialog2.ShowModal();
702 }
703 }
704
705 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
706 {
707 wxFileDialog dialog(this,
708 _T("Testing save file dialog"),
709 wxEmptyString,
710 _T("myletter.doc"),
711 _T("Text files (*.txt)|*.txt|Document files (*.doc)|*.doc"),
712 wxSAVE|wxOVERWRITE_PROMPT);
713
714 dialog.SetFilterIndex(1);
715
716 if (dialog.ShowModal() == wxID_OK)
717 {
718 wxLogMessage(_T("%s, filter %d"),
719 dialog.GetPath().c_str(), dialog.GetFilterIndex());
720 }
721 }
722 #endif // wxUSE_FILEDLG
723
724 #if wxUSE_DIRDLG
725 void MyFrame::DoDirChoose(int style)
726 {
727 // pass some initial dir to wxDirDialog
728 wxString dirHome;
729 wxGetHomeDir(&dirHome);
730
731 wxDirDialog dialog(this, _T("Testing directory picker"), dirHome, style);
732
733 if (dialog.ShowModal() == wxID_OK)
734 {
735 wxLogMessage(_T("Selected path: %s"), dialog.GetPath().c_str());
736 }
737 }
738
739 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
740 {
741 DoDirChoose(wxDD_DEFAULT_STYLE & ~wxDD_NEW_DIR_BUTTON);
742 }
743
744 void MyFrame::DirChooseNew(wxCommandEvent& WXUNUSED(event) )
745 {
746 DoDirChoose(wxDD_DEFAULT_STYLE | wxDD_NEW_DIR_BUTTON);
747 }
748 #endif // wxUSE_DIRDLG
749
750 #if USE_DIRDLG_GENERIC
751 void MyFrame::GenericDirChoose(wxCommandEvent& WXUNUSED(event) )
752 {
753 // pass some initial dir to wxDirDialog
754 wxString dirHome;
755 wxGetHomeDir(&dirHome);
756
757 wxGenericDirDialog dialog(this, _T("Testing generic directory picker"), dirHome);
758
759 if (dialog.ShowModal() == wxID_OK)
760 {
761 wxMessageDialog dialog2(this, dialog.GetPath(), _T("Selected path"));
762 dialog2.ShowModal();
763 }
764 }
765 #endif // USE_DIRDLG_GENERIC
766
767 #if USE_MODAL_PRESENTATION
768 void MyFrame::ModalDlg(wxCommandEvent& WXUNUSED(event))
769 {
770 MyModalDialog dlg(this);
771 dlg.ShowModal();
772 }
773
774 void MyFrame::ModelessDlg(wxCommandEvent& event)
775 {
776 bool show = GetMenuBar()->IsChecked(event.GetId());
777
778 if ( show )
779 {
780 if ( !m_dialog )
781 {
782 m_dialog = new MyModelessDialog(this);
783 }
784
785 m_dialog->Show(true);
786 }
787 else // hide
788 {
789 m_dialog->Hide();
790 }
791 }
792 #endif // USE_MODAL_PRESENTATION
793
794 #if wxUSE_STARTUP_TIPS
795 void MyFrame::ShowTip(wxCommandEvent& WXUNUSED(event))
796 {
797 static size_t s_index = (size_t)-1;
798
799 if ( s_index == (size_t)-1 )
800 {
801 srand(time(NULL));
802
803 // this is completely bogus, we don't know how many lines are there
804 // in the file, but who cares, it's a demo only...
805 s_index = rand() % 5;
806 }
807
808 wxTipProvider *tipProvider = wxCreateFileTipProvider(_T("tips.txt"), s_index);
809
810 bool showAtStartup = wxShowTip(this, tipProvider);
811
812 if ( showAtStartup )
813 {
814 wxMessageBox(_T("Will show tips on startup"), _T("Tips dialog"),
815 wxOK | wxICON_INFORMATION, this);
816 }
817
818 s_index = tipProvider->GetCurrentTip();
819 delete tipProvider;
820 }
821 #endif // wxUSE_STARTUP_TIPS
822
823 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
824 {
825 Close(true);
826 }
827
828 #if wxUSE_PROGRESSDLG
829
830 void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
831 {
832 static const int max = 10;
833
834 wxProgressDialog dialog(_T("Progress dialog example"),
835 _T("An informative message"),
836 max, // range
837 this, // parent
838 wxPD_CAN_ABORT |
839 wxPD_APP_MODAL |
840 // wxPD_AUTO_HIDE | -- try this as well
841 wxPD_ELAPSED_TIME |
842 wxPD_ESTIMATED_TIME |
843 wxPD_REMAINING_TIME);
844
845 bool cont = true;
846 for ( int i = 0; i <= max; i++ )
847 {
848 wxSleep(1);
849 if ( i == max )
850 {
851 cont = dialog.Update(i, _T("That's all, folks!"));
852 }
853 else if ( i == max / 2 )
854 {
855 cont = dialog.Update(i, _T("Only a half left (very long message)!"));
856 }
857 else
858 {
859 cont = dialog.Update(i);
860 }
861
862 if ( !cont )
863 {
864 if ( wxMessageBox(_T("Do you really want to cancel?"),
865 _T("Progress dialog question"), // caption
866 wxYES_NO | wxICON_QUESTION) == wxYES )
867 break;
868
869 dialog.Resume();
870 }
871 }
872
873 if ( !cont )
874 {
875 wxLogStatus(wxT("Progress dialog aborted!"));
876 }
877 else
878 {
879 wxLogStatus(wxT("Countdown from %d finished"), max);
880 }
881 }
882
883 #endif // wxUSE_PROGRESSDLG
884
885 #if wxUSE_BUSYINFO
886
887 void MyFrame::ShowBusyInfo(wxCommandEvent& WXUNUSED(event))
888 {
889 wxWindowDisabler disableAll;
890
891 wxBusyInfo info(_T("Working, please wait..."), this);
892
893 for ( int i = 0; i < 18; i++ )
894 {
895 //wxUsleep(100);
896 wxTheApp->Yield();
897 }
898
899 wxSleep(2);
900 //wxWakeUpIdle();
901 }
902
903 #endif // wxUSE_BUSYINFO
904
905 #if wxUSE_FINDREPLDLG
906
907 void MyFrame::ShowReplaceDialog( wxCommandEvent& WXUNUSED(event) )
908 {
909 if ( m_dlgReplace )
910 {
911 delete m_dlgReplace;
912 m_dlgReplace = NULL;
913 }
914 else
915 {
916 m_dlgReplace = new wxFindReplaceDialog
917 (
918 this,
919 &m_findData,
920 _T("Find and replace dialog"),
921 wxFR_REPLACEDIALOG
922 );
923
924 m_dlgReplace->Show(true);
925 }
926 }
927
928 void MyFrame::ShowFindDialog( wxCommandEvent& WXUNUSED(event) )
929 {
930 if ( m_dlgFind )
931 {
932 delete m_dlgFind;
933 m_dlgFind = NULL;
934 }
935 else
936 {
937 m_dlgFind = new wxFindReplaceDialog
938 (
939 this,
940 &m_findData,
941 _T("Find dialog"),
942 // just for testing
943 wxFR_NOWHOLEWORD
944 );
945
946 m_dlgFind->Show(true);
947 }
948 }
949
950 static wxString DecodeFindDialogEventFlags(int flags)
951 {
952 wxString str;
953 str << (flags & wxFR_DOWN ? _T("down") : _T("up")) << _T(", ")
954 << (flags & wxFR_WHOLEWORD ? _T("whole words only, ") : _T(""))
955 << (flags & wxFR_MATCHCASE ? _T("") : _T("not "))
956 << _T("case sensitive");
957
958 return str;
959 }
960
961 void MyFrame::OnFindDialog(wxFindDialogEvent& event)
962 {
963 wxEventType type = event.GetEventType();
964
965 if ( type == wxEVT_COMMAND_FIND || type == wxEVT_COMMAND_FIND_NEXT )
966 {
967 wxLogMessage(wxT("Find %s'%s' (flags: %s)"),
968 type == wxEVT_COMMAND_FIND_NEXT ? wxT("next ") : wxT(""),
969 event.GetFindString().c_str(),
970 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
971 }
972 else if ( type == wxEVT_COMMAND_FIND_REPLACE ||
973 type == wxEVT_COMMAND_FIND_REPLACE_ALL )
974 {
975 wxLogMessage(wxT("Replace %s'%s' with '%s' (flags: %s)"),
976 type == wxEVT_COMMAND_FIND_REPLACE_ALL ? _T("all ") : wxT(""),
977 event.GetFindString().c_str(),
978 event.GetReplaceString().c_str(),
979 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
980 }
981 else if ( type == wxEVT_COMMAND_FIND_CLOSE )
982 {
983 wxFindReplaceDialog *dlg = event.GetDialog();
984
985 int idMenu;
986 const wxChar *txt;
987 if ( dlg == m_dlgFind )
988 {
989 txt = _T("Find");
990 idMenu = DIALOGS_FIND;
991 m_dlgFind = NULL;
992 }
993 else if ( dlg == m_dlgReplace )
994 {
995 txt = _T("Replace");
996 idMenu = DIALOGS_REPLACE;
997 m_dlgReplace = NULL;
998 }
999 else
1000 {
1001 txt = _T("Unknown");
1002 idMenu = wxID_ANY;
1003
1004 wxFAIL_MSG( _T("unexpected event") );
1005 }
1006
1007 wxLogMessage(wxT("%s dialog is being closed."), txt);
1008
1009 if ( idMenu != wxID_ANY )
1010 {
1011 GetMenuBar()->Check(idMenu, false);
1012 }
1013
1014 dlg->Destroy();
1015 }
1016 else
1017 {
1018 wxLogError(wxT("Unknown find dialog event!"));
1019 }
1020 }
1021
1022 #endif // wxUSE_FINDREPLDLG
1023
1024 // ----------------------------------------------------------------------------
1025 // MyCanvas
1026 // ----------------------------------------------------------------------------
1027
1028 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
1029 {
1030 wxPaintDC dc(this);
1031 dc.SetFont(wxGetApp().m_canvasFont);
1032 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
1033 dc.SetBackgroundMode(wxTRANSPARENT);
1034 dc.DrawText(_T("wxWidgets common dialogs test application"), 10, 10);
1035 }
1036
1037 // ----------------------------------------------------------------------------
1038 // MyModelessDialog
1039 // ----------------------------------------------------------------------------
1040
1041 MyModelessDialog::MyModelessDialog(wxWindow *parent)
1042 : wxDialog(parent, wxID_ANY, wxString(_T("Modeless dialog")))
1043 {
1044 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
1045
1046 wxButton *btn = new wxButton(this, DIALOGS_MODELESS_BTN, _T("Press me"));
1047 wxCheckBox *check = new wxCheckBox(this, wxID_ANY, _T("Should be disabled"));
1048 check->Disable();
1049
1050 sizerTop->Add(btn, 1, wxEXPAND | wxALL, 5);
1051 sizerTop->Add(check, 1, wxEXPAND | wxALL, 5);
1052
1053 SetSizer(sizerTop);
1054
1055 sizerTop->SetSizeHints(this);
1056 sizerTop->Fit(this);
1057 }
1058
1059 void MyModelessDialog::OnButton(wxCommandEvent& WXUNUSED(event))
1060 {
1061 wxMessageBox(_T("Button pressed in modeless dialog"), _T("Info"),
1062 wxOK | wxICON_INFORMATION, this);
1063 }
1064
1065 void MyModelessDialog::OnClose(wxCloseEvent& event)
1066 {
1067 if ( event.CanVeto() )
1068 {
1069 wxMessageBox(_T("Use the menu item to close this dialog"),
1070 _T("Modeless dialog"),
1071 wxOK | wxICON_INFORMATION, this);
1072
1073 event.Veto();
1074 }
1075 }
1076
1077 // ----------------------------------------------------------------------------
1078 // MyModalDialog
1079 // ----------------------------------------------------------------------------
1080
1081 MyModalDialog::MyModalDialog(wxWindow *parent)
1082 : wxDialog(parent, wxID_ANY, wxString(_T("Modal dialog")))
1083 {
1084 wxBoxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
1085
1086 m_btnModal = new wxButton(this, wxID_ANY, _T("&Modal dialog..."));
1087 m_btnModeless = new wxButton(this, wxID_ANY, _T("Mode&less dialog"));
1088 m_btnDelete = new wxButton(this, wxID_ANY, _T("&Delete button"));
1089
1090 wxButton *btnOk = new wxButton(this, wxID_CANCEL, _T("&Close"));
1091 sizerTop->Add(m_btnModal, 0, wxALIGN_CENTER | wxALL, 5);
1092 sizerTop->Add(m_btnModeless, 0, wxALIGN_CENTER | wxALL, 5);
1093 sizerTop->Add(m_btnDelete, 0, wxALIGN_CENTER | wxALL, 5);
1094 sizerTop->Add(btnOk, 0, wxALIGN_CENTER | wxALL, 5);
1095
1096 SetSizer(sizerTop);
1097
1098 sizerTop->SetSizeHints(this);
1099 sizerTop->Fit(this);
1100
1101 m_btnModal->SetFocus();
1102 m_btnModal->SetDefault();
1103 }
1104
1105 void MyModalDialog::OnButton(wxCommandEvent& event)
1106 {
1107 if ( event.GetEventObject() == m_btnDelete )
1108 {
1109 delete m_btnModal;
1110 m_btnModal = NULL;
1111
1112 m_btnDelete->Disable();
1113 }
1114 else if ( event.GetEventObject() == m_btnModal )
1115 {
1116 #if wxUSE_TEXTDLG
1117 wxGetTextFromUser(_T("Dummy prompt"),
1118 _T("Modal dialog called from dialog"),
1119 wxEmptyString, this);
1120 #else
1121 wxMessageBox(_T("Modal dialog called from dialog"));
1122 #endif // wxUSE_TEXTDLG
1123 }
1124 else if ( event.GetEventObject() == m_btnModeless )
1125 {
1126 (new MyModelessDialog(this))->Show();
1127 }
1128 else
1129 {
1130 event.Skip();
1131 }
1132 }