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