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