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