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