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