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