added wxBusyInfo test
[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");
169 file_menu->Append(DIALOGS_REPLACE, "Find and &replace dialog\tShift-Ctrl-F");
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 CreateStatusBar();
203 }
204
205 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
206 {
207 wxColourData data;
208 data.SetChooseFull(TRUE);
209 for (int i = 0; i < 16; i++)
210 {
211 wxColour colour(i*16, i*16, i*16);
212 data.SetCustomColour(i, colour);
213 }
214
215 wxColourDialog *dialog = new wxColourDialog(this, &data);
216 dialog->SetTitle("Choose the background colour");
217 if (dialog->ShowModal() == wxID_OK)
218 {
219 wxColourData retData = dialog->GetColourData();
220 wxColour col = retData.GetColour();
221 myCanvas->SetBackgroundColour(col);
222 myCanvas->Clear();
223 myCanvas->Refresh();
224 }
225 dialog->Destroy();
226 }
227
228 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
229 {
230 wxFontData data;
231 data.SetInitialFont(wxGetApp().m_canvasFont);
232 data.SetColour(wxGetApp().m_canvasTextColour);
233
234 wxFontDialog *dialog = new wxFontDialog(this, &data);
235 if (dialog->ShowModal() == wxID_OK)
236 {
237 wxFontData retData = dialog->GetFontData();
238 wxGetApp().m_canvasFont = retData.GetChosenFont();
239 wxGetApp().m_canvasTextColour = retData.GetColour();
240 myCanvas->Refresh();
241 }
242 dialog->Destroy();
243 }
244
245 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
246 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
247 {
248 wxColourData data;
249 data.SetChooseFull(TRUE);
250 for (int i = 0; i < 16; i++)
251 {
252 wxColour colour(i*16, i*16, i*16);
253 data.SetCustomColour(i, colour);
254 }
255
256 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
257 if (dialog->ShowModal() == wxID_OK)
258 {
259 wxColourData retData = dialog->GetColourData();
260 wxColour col = retData.GetColour();
261 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
262 myCanvas->SetBackgroundColour(col);
263 myCanvas->Clear();
264 myCanvas->Refresh();
265 }
266 dialog->Destroy();
267 }
268
269 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
270 {
271 wxFontData data;
272 data.SetInitialFont(wxGetApp().m_canvasFont);
273 data.SetColour(wxGetApp().m_canvasTextColour);
274
275 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
276 if (dialog->ShowModal() == wxID_OK)
277 {
278 wxFontData retData = dialog->GetFontData();
279 wxGetApp().m_canvasFont = retData.GetChosenFont();
280 wxGetApp().m_canvasTextColour = retData.GetColour();
281 myCanvas->Refresh();
282 }
283 dialog->Destroy();
284 }
285 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
286
287 void MyFrame::LogDialog(wxCommandEvent& event)
288 {
289 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
290 // being flushed -- test it
291 {
292 wxBusyCursor bc;
293 wxLogMessage(wxT("This is some message - everything is ok so far."));
294 wxLogMessage(wxT("Another message...\n... this one is on multiple lines"));
295 wxLogWarning(wxT("And then something went wrong!"));
296
297 // and if ~wxBusyCursor doesn't do it, then call it manually
298 wxYield();
299 }
300
301 wxLogError(wxT("Intermediary error handler decided to abort."));
302 wxLogError(wxT("The top level caller detected an unrecoverable error."));
303
304 wxLog::FlushActive();
305
306 wxLogMessage(wxT("And this is the same dialog but with only one message."));
307 }
308
309 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
310 {
311 wxMessageDialog dialog( NULL, "This is a message box\nA long, long string to test out the message box properly",
312 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
313
314 dialog.ShowModal();
315 }
316
317 void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
318 {
319 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
320 "Even two rows of text.",
321 "Enter a number:", "Numeric input test",
322 50, 0, 100, this );
323
324 wxString msg;
325 int icon;
326 if ( res == -1 )
327 {
328 msg = "Invalid number entered or dialog cancelled.";
329 icon = wxICON_HAND;
330 }
331 else
332 {
333 msg.Printf(_T("You've entered %lu"), res );
334 icon = wxICON_INFORMATION;
335 }
336
337 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
338 }
339
340 void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
341 {
342 wxString pwd = wxGetPasswordFromUser("Enter password:",
343 "Password entry dialog",
344 "",
345 this);
346 if ( !!pwd )
347 {
348 wxMessageBox(wxString::Format(wxT("Your password is '%s'"), pwd.c_str()),
349 "Got password", wxOK | wxICON_INFORMATION, this);
350 }
351 }
352
353 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
354 {
355 wxTextEntryDialog dialog(this,
356 "This is a small sample\n"
357 "A long, long string to test out the text entrybox",
358 "Please enter a string",
359 "Default value",
360 wxOK | wxCANCEL);
361
362 if (dialog.ShowModal() == wxID_OK)
363 {
364 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
365 dialog2.ShowModal();
366 }
367 }
368
369 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
370 {
371 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
372 int n = 5;
373
374 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
375 "Please select a value", n, (const wxString *)choices);
376
377 dialog.SetSelection(2);
378
379 if (dialog.ShowModal() == wxID_OK)
380 {
381 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
382 dialog2.ShowModal();
383 }
384 }
385
386 void MyFrame::MultiChoice(wxCommandEvent& WXUNUSED(event) )
387 {
388 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
389 int n = 5;
390
391 wxArrayInt selections;
392 size_t count = wxGetMultipleChoices(selections,
393 "This is a small sample\n"
394 "A multi-choice convenience dialog",
395 "Please select a value",
396 n, (const wxString *)choices,
397 this);
398 if ( count )
399 {
400 wxString msg;
401 msg.Printf(wxT("You selected %u items:\n"), count);
402 for ( size_t n = 0; n < count; n++ )
403 {
404 msg += wxString::Format(wxT("\t%u: %u (%s)\n"), n, selections[n],
405 choices[selections[n]].c_str());
406 }
407 wxLogMessage(msg);
408 }
409 //else: cancelled or nothing selected
410 }
411
412 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
413 {
414 wxFileDialog dialog(this, "Testing open file dialog",
415 "", "",
416 "C++ files (*.h;*.cpp)|*.h;*.cpp");
417
418 if (dialog.ShowModal() == wxID_OK)
419 {
420 wxString info;
421 info.Printf(_T("Full file name: %s\n")
422 _T("Path: %s\n")
423 _T("Name: %s"),
424 dialog.GetPath().c_str(),
425 dialog.GetDirectory().c_str(),
426 dialog.GetFilename().c_str());
427 wxMessageDialog dialog2(this, info, "Selected file");
428 dialog2.ShowModal();
429 }
430 }
431
432 // this shows how to take advantage of specifying a default extension in the
433 // call to wxFileSelector: it is remembered after each new call and the next
434 // one will use it by default
435 void MyFrame::FileOpen2(wxCommandEvent& WXUNUSED(event) )
436 {
437 static wxString s_extDef;
438 wxString path = wxFileSelector(
439 _T("Select the file to load"),
440 _T(""), _T(""),
441 s_extDef,
442 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
443 0,
444 this
445 );
446
447 if ( !path )
448 return;
449
450 // it is just a sample, would use wxSplitPath in real program
451 s_extDef = path.AfterLast(_T('.'));
452
453 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
454 (const wxChar*) path, (const wxChar*) s_extDef);
455 }
456
457 void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
458 {
459 wxFileDialog dialog(this, "Testing open multiple file dialog",
460 "", "", wxFileSelectorDefaultWildcardStr,
461 wxMULTIPLE);
462
463 if (dialog.ShowModal() == wxID_OK)
464 {
465 wxArrayString paths, filenames;
466
467 dialog.GetPaths(paths);
468 dialog.GetFilenames(filenames);
469
470 wxString msg, s;
471 size_t count = paths.GetCount();
472 for ( size_t n = 0; n < count; n++ )
473 {
474 s.Printf(_T("File %d: %s (%s)\n"),
475 n, paths[n].c_str(), filenames[n].c_str());
476
477 msg += s;
478 }
479
480 wxMessageDialog dialog2(this, msg, "Selected files");
481 dialog2.ShowModal();
482 }
483 }
484
485 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
486 {
487 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
488 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
489 wxSAVE|wxOVERWRITE_PROMPT);
490
491 if (dialog.ShowModal() == wxID_OK)
492 {
493 wxChar buf[400];
494 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
495 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
496 dialog2.ShowModal();
497 }
498 }
499
500 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
501 {
502 // pass some initial dir to wxDirDialog
503 wxString dirHome;
504 wxGetHomeDir(&dirHome);
505
506 wxDirDialog dialog(this, "Testing directory picker", dirHome);
507
508 if (dialog.ShowModal() == wxID_OK)
509 {
510 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
511 dialog2.ShowModal();
512 }
513 }
514
515 void MyFrame::GenericDirChoose(wxCommandEvent& WXUNUSED(event) )
516 {
517 #if !defined(__WXMSW__) || defined(wxUSE_DIRDLGG) && wxUSE_DIRDLGG
518 // pass some initial dir to wxDirDialog
519 wxString dirHome;
520 wxGetHomeDir(&dirHome);
521
522 wxGenericDirDialog dialog(this, "Testing generic directory picker", dirHome);
523
524 if (dialog.ShowModal() == wxID_OK)
525 {
526 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
527 dialog2.ShowModal();
528 }
529 #else
530 wxLogError(wxT("Sorry, generic dir dialog not available:\n")
531 wxT("set wxUSE_DIRDLGG to 1 and recompile"));
532 #endif
533 }
534
535 void MyFrame::ModalDlg(wxCommandEvent& WXUNUSED(event))
536 {
537 MyModalDialog dlg(this);
538 dlg.ShowModal();
539 }
540
541 void MyFrame::ModelessDlg(wxCommandEvent& event)
542 {
543 bool show = GetMenuBar()->IsChecked(event.GetId());
544
545 if ( show )
546 {
547 if ( !m_dialog )
548 {
549 m_dialog = new MyModelessDialog(this);
550 }
551
552 m_dialog->Show(TRUE);
553 }
554 else // hide
555 {
556 m_dialog->Hide();
557 }
558 }
559
560 void MyFrame::ShowTip(wxCommandEvent& event)
561 {
562 #if wxUSE_STARTUP_TIPS
563 static size_t s_index = (size_t)-1;
564
565 if ( s_index == (size_t)-1 )
566 {
567 srand(time(NULL));
568
569 // this is completely bogus, we don't know how many lines are there
570 // in the file, but who cares, it's a demo only...
571 s_index = rand() % 5;
572 }
573
574 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
575
576 bool showAtStartup = wxShowTip(this, tipProvider);
577
578 if ( showAtStartup )
579 {
580 wxMessageBox("Will show tips on startup", "Tips dialog",
581 wxOK | wxICON_INFORMATION, this);
582 }
583
584 s_index = tipProvider->GetCurrentTip();
585 delete tipProvider;
586 #endif
587 }
588
589 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
590 {
591 Close(TRUE);
592 }
593
594 #if wxUSE_PROGRESSDLG
595
596 void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
597 {
598 static const int max = 10;
599
600 wxProgressDialog dialog("Progress dialog example",
601 "An informative message",
602 max, // range
603 this, // parent
604 wxPD_CAN_ABORT |
605 wxPD_APP_MODAL |
606 wxPD_ELAPSED_TIME |
607 wxPD_ESTIMATED_TIME |
608 wxPD_REMAINING_TIME);
609
610 bool cont = TRUE;
611 for ( int i = 0; i <= max && cont; i++ )
612 {
613 wxSleep(1);
614 if ( i == max )
615 {
616 cont = dialog.Update(i, "That's all, folks!");
617 }
618 else if ( i == max / 2 )
619 {
620 cont = dialog.Update(i, "Only a half left (very long message)!");
621 }
622 else
623 {
624 cont = dialog.Update(i);
625 }
626 }
627
628 if ( !cont )
629 {
630 wxLogStatus(wxT("Progress dialog aborted!"));
631 }
632 else
633 {
634 wxLogStatus(wxT("Countdown from %d finished"), max);
635 }
636 }
637
638 #endif // wxUSE_PROGRESSDLG
639
640 #if wxUSE_BUSYINFO
641
642 void MyFrame::ShowBusyInfo(wxCommandEvent& WXUNUSED(event))
643 {
644 wxBusyInfo info("Sleep^H^H^H^H^HWorkiing, please wait...\n... a bit more");
645
646 for ( int i = 0; i < 30; i++ )
647 {
648 wxUsleep(100);
649 wxTheApp->Yield();
650 }
651 }
652
653 #endif // wxUSE_BUSYINFO
654
655 #if wxUSE_FINDREPLDLG
656
657 void MyFrame::ShowReplaceDialog( wxCommandEvent& WXUNUSED(event) )
658 {
659 wxFindReplaceDialog *dialog = new wxFindReplaceDialog
660 (
661 this,
662 &m_findData,
663 "Find and replace dialog",
664 wxFR_REPLACEDIALOG
665 );
666 dialog->Show(TRUE);
667 }
668
669 void MyFrame::ShowFindDialog( wxCommandEvent& WXUNUSED(event) )
670 {
671 wxFindReplaceDialog *dialog = new wxFindReplaceDialog
672 (
673 this,
674 &m_findData,
675 "Find dialog",
676 // just for testing
677 wxFR_NOWHOLEWORD
678 );
679 dialog->Show(TRUE);
680 }
681
682 static wxString DecodeFindDialogEventFlags(int flags)
683 {
684 wxString str;
685 str << (flags & wxFR_DOWN ? "down" : "up") << ", "
686 << (flags & wxFR_WHOLEWORD ? "whole words only, " : "")
687 << (flags & wxFR_MATCHCASE ? "" : "not ")
688 << "case sensitive";
689
690 return str;
691 }
692
693 void MyFrame::OnFindDialog(wxFindDialogEvent& event)
694 {
695 wxEventType type = event.GetEventType();
696
697 if ( type == wxEVT_COMMAND_FIND || type == wxEVT_COMMAND_FIND_NEXT )
698 {
699 wxLogMessage(wxT("Find %s'%s' (flags: %s)"),
700 type == wxEVT_COMMAND_FIND_NEXT ? "next " : "",
701 event.GetFindString().c_str(),
702 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
703 }
704 else if ( type == wxEVT_COMMAND_FIND_REPLACE ||
705 type == wxEVT_COMMAND_FIND_REPLACE_ALL )
706 {
707 wxLogMessage(wxT("Replace %s'%s' with '%s' (flags: %s)"),
708 type == wxEVT_COMMAND_FIND_REPLACE_ALL ? "all " : "",
709 event.GetFindString().c_str(),
710 event.GetReplaceString().c_str(),
711 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
712 }
713 else if ( type == wxEVT_COMMAND_FIND_CLOSE )
714 {
715 wxLogMessage(wxT("Find dialog is being closed."));
716
717 event.GetDialog()->Destroy();
718 }
719 else
720 {
721 wxLogError(wxT("Unknown find dialog event!"));
722 }
723 }
724
725 #endif // wxUSE_FINDREPLDLG
726
727 // ----------------------------------------------------------------------------
728 // MyCanvas
729 // ----------------------------------------------------------------------------
730
731 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
732 {
733 wxPaintDC dc(this);
734 dc.SetFont(wxGetApp().m_canvasFont);
735 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
736 dc.SetBackgroundMode(wxTRANSPARENT);
737 dc.DrawText("wxWindows common dialogs test application", 10, 10);
738 }
739
740 // ----------------------------------------------------------------------------
741 // MyModelessDialog
742 // ----------------------------------------------------------------------------
743
744 MyModelessDialog::MyModelessDialog(wxWindow *parent)
745 : wxDialog(parent, -1, wxString("Modeless dialog"))
746 {
747 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
748
749 wxButton *btn = new wxButton(this, DIALOGS_MODELESS_BTN, "Press me");
750 wxCheckBox *check = new wxCheckBox(this, -1, "Should be disabled");
751 check->Disable();
752
753 sizerTop->Add(btn, 1, wxEXPAND | wxALL, 5);
754 sizerTop->Add(check, 1, wxEXPAND | wxALL, 5);
755
756 SetAutoLayout(TRUE);
757 SetSizer(sizerTop);
758
759 sizerTop->SetSizeHints(this);
760 sizerTop->Fit(this);
761 }
762
763 void MyModelessDialog::OnButton(wxCommandEvent& WXUNUSED(event))
764 {
765 wxMessageBox("Button pressed in modeless dialog", "Info",
766 wxOK | wxICON_INFORMATION, this);
767 }
768
769 void MyModelessDialog::OnClose(wxCloseEvent& event)
770 {
771 if ( event.CanVeto() )
772 {
773 wxMessageBox("Use the menu item to close this dialog",
774 "Modeless dialog",
775 wxOK | wxICON_INFORMATION, this);
776
777 event.Veto();
778 }
779 }
780
781 // ----------------------------------------------------------------------------
782 // MyModalDialog
783 // ----------------------------------------------------------------------------
784
785 MyModalDialog::MyModalDialog(wxWindow *parent)
786 : wxDialog(parent, -1, wxString("Modal dialog"))
787 {
788 wxBoxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
789
790 m_btnFocused = new wxButton(this, -1, "Default button");
791 m_btnDelete = new wxButton(this, -1, "&Delete button");
792 sizerTop->Add(m_btnFocused, 0, wxALIGN_CENTER | wxALL, 5);
793 sizerTop->Add(m_btnDelete, 0, wxALIGN_CENTER | wxALL, 5);
794
795 SetAutoLayout(TRUE);
796 SetSizer(sizerTop);
797
798 sizerTop->SetSizeHints(this);
799 sizerTop->Fit(this);
800
801 m_btnFocused->SetFocus();
802 m_btnFocused->SetDefault();
803 }
804
805 void MyModalDialog::OnButton(wxCommandEvent& event)
806 {
807 if ( event.GetEventObject() == m_btnDelete )
808 {
809 delete m_btnFocused;
810 m_btnFocused = NULL;
811
812 m_btnDelete->Disable();
813 }
814 else if ( event.GetEventObject() == m_btnFocused )
815 {
816 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",
817 "", this);
818 }
819 else
820 {
821 event.Skip();
822 }
823 }