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