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