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