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