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