Filter index wasn't being set for multiple file dialog
[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(__WXMAC__)) || 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& WXUNUSED(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 wxString wildcards =
522 #ifdef __WXMOTIF__
523 _T("C++ files (*.cpp)|*.cpp");
524 #else
525 _T("All files (*.*)|*.*|C++ files (*.h;*.cpp)|*.h;*.cpp");
526 #endif
527 wxFileDialog dialog(this, _T("Testing open multiple file dialog"),
528 _T(""), _T(""), wildcards,
529 wxMULTIPLE);
530
531 if (dialog.ShowModal() == wxID_OK)
532 {
533 wxArrayString paths, filenames;
534
535 dialog.GetPaths(paths);
536 dialog.GetFilenames(filenames);
537
538 wxString msg, s;
539 size_t count = paths.GetCount();
540 for ( size_t n = 0; n < count; n++ )
541 {
542 s.Printf(_T("File %d: %s (%s)\n"),
543 (int)n, paths[n].c_str(), filenames[n].c_str());
544
545 msg += s;
546 }
547 s.Printf(_T("Filter index: %d"), dialog.GetFilterIndex());
548 msg += s;
549
550 wxMessageDialog dialog2(this, msg, _T("Selected files"));
551 dialog2.ShowModal();
552 }
553 }
554
555 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
556 {
557 wxFileDialog dialog(this,
558 _T("Testing save file dialog"),
559 _T(""),
560 _T("myletter.doc"),
561 _T("Text files (*.txt)|*.txt|Document files (*.doc)|*.doc"),
562 wxSAVE|wxOVERWRITE_PROMPT);
563
564 dialog.SetFilterIndex(1);
565
566 if (dialog.ShowModal() == wxID_OK)
567 {
568 wxLogMessage(_T("%s, filter %d"),
569 dialog.GetPath().c_str(), dialog.GetFilterIndex());
570 }
571 }
572
573 void MyFrame::DoDirChoose(int style)
574 {
575 // pass some initial dir to wxDirDialog
576 wxString dirHome;
577 wxGetHomeDir(&dirHome);
578
579 wxDirDialog dialog(this, _T("Testing directory picker"), dirHome, style);
580
581 if (dialog.ShowModal() == wxID_OK)
582 {
583 wxLogMessage(_T("Selected path: %s"), dialog.GetPath().c_str());
584 }
585 }
586
587 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
588 {
589 DoDirChoose(wxDD_DEFAULT_STYLE & ~wxDD_NEW_DIR_BUTTON);
590 }
591
592 void MyFrame::DirChooseNew(wxCommandEvent& WXUNUSED(event) )
593 {
594 DoDirChoose(wxDD_DEFAULT_STYLE | wxDD_NEW_DIR_BUTTON);
595 }
596
597 #if defined(__WXMSW__) || defined(__WXMAC__)
598
599 void MyFrame::GenericDirChoose(wxCommandEvent& WXUNUSED(event) )
600 {
601 #if !(defined(__WXMSW__) || defined(__WXMAC__)) || wxUSE_DIRDLGG
602 // pass some initial dir to wxDirDialog
603 wxString dirHome;
604 wxGetHomeDir(&dirHome);
605
606 wxGenericDirDialog dialog(this, _T("Testing generic directory picker"), dirHome);
607
608 if (dialog.ShowModal() == wxID_OK)
609 {
610 wxMessageDialog dialog2(this, dialog.GetPath(), _T("Selected path"));
611 dialog2.ShowModal();
612 }
613 #else
614 wxLogError(wxT("Sorry, generic dir dialog not available:\n")
615 wxT("set wxUSE_DIRDLGG to 1 and recompile"));
616 #endif
617 }
618
619 #endif // wxMSW || wxMAC
620
621 void MyFrame::ModalDlg(wxCommandEvent& WXUNUSED(event))
622 {
623 MyModalDialog dlg(this);
624 dlg.ShowModal();
625 }
626
627 void MyFrame::ModelessDlg(wxCommandEvent& event)
628 {
629 bool show = GetMenuBar()->IsChecked(event.GetId());
630
631 if ( show )
632 {
633 if ( !m_dialog )
634 {
635 m_dialog = new MyModelessDialog(this);
636 }
637
638 m_dialog->Show(TRUE);
639 }
640 else // hide
641 {
642 m_dialog->Hide();
643 }
644 }
645
646 void MyFrame::ShowTip(wxCommandEvent& WXUNUSED(event))
647 {
648 #if wxUSE_STARTUP_TIPS
649 static size_t s_index = (size_t)-1;
650
651 if ( s_index == (size_t)-1 )
652 {
653 srand(time(NULL));
654
655 // this is completely bogus, we don't know how many lines are there
656 // in the file, but who cares, it's a demo only...
657 s_index = rand() % 5;
658 }
659
660 wxTipProvider *tipProvider = wxCreateFileTipProvider(_T("tips.txt"), s_index);
661
662 bool showAtStartup = wxShowTip(this, tipProvider);
663
664 if ( showAtStartup )
665 {
666 wxMessageBox(_T("Will show tips on startup"), _T("Tips dialog"),
667 wxOK | wxICON_INFORMATION, this);
668 }
669
670 s_index = tipProvider->GetCurrentTip();
671 delete tipProvider;
672 #endif
673 }
674
675 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
676 {
677 Close(TRUE);
678 }
679
680 #if wxUSE_PROGRESSDLG
681
682 void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
683 {
684 static const int max = 10;
685
686 wxProgressDialog dialog(_T("Progress dialog example"),
687 _T("An informative message"),
688 max, // range
689 this, // parent
690 wxPD_CAN_ABORT |
691 wxPD_APP_MODAL |
692 // wxPD_AUTO_HIDE | -- try this as well
693 wxPD_ELAPSED_TIME |
694 wxPD_ESTIMATED_TIME |
695 wxPD_REMAINING_TIME);
696
697 bool cont = TRUE;
698 for ( int i = 0; i <= max; i++ )
699 {
700 wxSleep(1);
701 if ( i == max )
702 {
703 cont = dialog.Update(i, _T("That's all, folks!"));
704 }
705 else if ( i == max / 2 )
706 {
707 cont = dialog.Update(i, _T("Only a half left (very long message)!"));
708 }
709 else
710 {
711 cont = dialog.Update(i);
712 }
713
714 if ( !cont )
715 {
716 if ( wxMessageBox(_T("Do you really want to cancel?"),
717 _T("Progress dialog question"), // caption
718 wxYES_NO | wxICON_QUESTION) == wxYES )
719 break;
720
721 dialog.Resume();
722 }
723 }
724
725 if ( !cont )
726 {
727 wxLogStatus(wxT("Progress dialog aborted!"));
728 }
729 else
730 {
731 wxLogStatus(wxT("Countdown from %d finished"), max);
732 }
733 }
734
735 #endif // wxUSE_PROGRESSDLG
736
737 #if wxUSE_BUSYINFO
738
739 void MyFrame::ShowBusyInfo(wxCommandEvent& WXUNUSED(event))
740 {
741 wxWindowDisabler disableAll;
742
743 wxBusyInfo info(_T("Working, please wait..."), this);
744
745 for ( int i = 0; i < 18; i++ )
746 {
747 //wxUsleep(100);
748 wxTheApp->Yield();
749 }
750
751 wxSleep(2);
752 //wxWakeUpIdle();
753 }
754
755 #endif // wxUSE_BUSYINFO
756
757 #if wxUSE_FINDREPLDLG
758
759 void MyFrame::ShowReplaceDialog( wxCommandEvent& WXUNUSED(event) )
760 {
761 if ( m_dlgReplace )
762 {
763 delete m_dlgReplace;
764 m_dlgReplace = NULL;
765 }
766 else
767 {
768 m_dlgReplace = new wxFindReplaceDialog
769 (
770 this,
771 &m_findData,
772 _T("Find and replace dialog"),
773 wxFR_REPLACEDIALOG
774 );
775
776 m_dlgReplace->Show(TRUE);
777 }
778 }
779
780 void MyFrame::ShowFindDialog( wxCommandEvent& WXUNUSED(event) )
781 {
782 if ( m_dlgFind )
783 {
784 delete m_dlgFind;
785 m_dlgFind = NULL;
786 }
787 else
788 {
789 m_dlgFind = new wxFindReplaceDialog
790 (
791 this,
792 &m_findData,
793 _T("Find dialog"),
794 // just for testing
795 wxFR_NOWHOLEWORD
796 );
797
798 m_dlgFind->Show(TRUE);
799 }
800 }
801
802 static wxString DecodeFindDialogEventFlags(int flags)
803 {
804 wxString str;
805 str << (flags & wxFR_DOWN ? _T("down") : _T("up")) << _T(", ")
806 << (flags & wxFR_WHOLEWORD ? _T("whole words only, ") : _T(""))
807 << (flags & wxFR_MATCHCASE ? _T("") : _T("not "))
808 << _T("case sensitive");
809
810 return str;
811 }
812
813 void MyFrame::OnFindDialog(wxFindDialogEvent& event)
814 {
815 wxEventType type = event.GetEventType();
816
817 if ( type == wxEVT_COMMAND_FIND || type == wxEVT_COMMAND_FIND_NEXT )
818 {
819 wxLogMessage(wxT("Find %s'%s' (flags: %s)"),
820 type == wxEVT_COMMAND_FIND_NEXT ? wxT("next ") : wxT(""),
821 event.GetFindString().c_str(),
822 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
823 }
824 else if ( type == wxEVT_COMMAND_FIND_REPLACE ||
825 type == wxEVT_COMMAND_FIND_REPLACE_ALL )
826 {
827 wxLogMessage(wxT("Replace %s'%s' with '%s' (flags: %s)"),
828 type == wxEVT_COMMAND_FIND_REPLACE_ALL ? _T("all ") : wxT(""),
829 event.GetFindString().c_str(),
830 event.GetReplaceString().c_str(),
831 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
832 }
833 else if ( type == wxEVT_COMMAND_FIND_CLOSE )
834 {
835 wxFindReplaceDialog *dlg = event.GetDialog();
836
837 int idMenu;
838 const wxChar *txt;
839 if ( dlg == m_dlgFind )
840 {
841 txt = _T("Find");
842 idMenu = DIALOGS_FIND;
843 m_dlgFind = NULL;
844 }
845 else if ( dlg == m_dlgReplace )
846 {
847 txt = _T("Replace");
848 idMenu = DIALOGS_REPLACE;
849 m_dlgReplace = NULL;
850 }
851 else
852 {
853 txt = _T("Unknown");
854 idMenu = -1;
855
856 wxFAIL_MSG( _T("unexpected event") );
857 }
858
859 wxLogMessage(wxT("%s dialog is being closed."), txt);
860
861 if ( idMenu != -1 )
862 {
863 GetMenuBar()->Check(idMenu, FALSE);
864 }
865
866 dlg->Destroy();
867 }
868 else
869 {
870 wxLogError(wxT("Unknown find dialog event!"));
871 }
872 }
873
874 #endif // wxUSE_FINDREPLDLG
875
876 // ----------------------------------------------------------------------------
877 // MyCanvas
878 // ----------------------------------------------------------------------------
879
880 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
881 {
882 wxPaintDC dc(this);
883 dc.SetFont(wxGetApp().m_canvasFont);
884 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
885 dc.SetBackgroundMode(wxTRANSPARENT);
886 dc.DrawText(_T("wxWindows common dialogs test application"), 10, 10);
887 }
888
889 // ----------------------------------------------------------------------------
890 // MyModelessDialog
891 // ----------------------------------------------------------------------------
892
893 MyModelessDialog::MyModelessDialog(wxWindow *parent)
894 : wxDialog(parent, -1, wxString(_T("Modeless dialog")))
895 {
896 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
897
898 wxButton *btn = new wxButton(this, DIALOGS_MODELESS_BTN, _T("Press me"));
899 wxCheckBox *check = new wxCheckBox(this, -1, _T("Should be disabled"));
900 check->Disable();
901
902 sizerTop->Add(btn, 1, wxEXPAND | wxALL, 5);
903 sizerTop->Add(check, 1, wxEXPAND | wxALL, 5);
904
905 SetAutoLayout(TRUE);
906 SetSizer(sizerTop);
907
908 sizerTop->SetSizeHints(this);
909 sizerTop->Fit(this);
910 }
911
912 void MyModelessDialog::OnButton(wxCommandEvent& WXUNUSED(event))
913 {
914 wxMessageBox(_T("Button pressed in modeless dialog"), _T("Info"),
915 wxOK | wxICON_INFORMATION, this);
916 }
917
918 void MyModelessDialog::OnClose(wxCloseEvent& event)
919 {
920 if ( event.CanVeto() )
921 {
922 wxMessageBox(_T("Use the menu item to close this dialog"),
923 _T("Modeless dialog"),
924 wxOK | wxICON_INFORMATION, this);
925
926 event.Veto();
927 }
928 }
929
930 // ----------------------------------------------------------------------------
931 // MyModalDialog
932 // ----------------------------------------------------------------------------
933
934 MyModalDialog::MyModalDialog(wxWindow *parent)
935 : wxDialog(parent, -1, wxString(_T("Modal dialog")))
936 {
937 wxBoxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
938
939 m_btnModal = new wxButton(this, -1, _T("&Modal dialog..."));
940 m_btnModeless = new wxButton(this, -1, _T("Mode&less dialog"));
941 m_btnDelete = new wxButton(this, -1, _T("&Delete button"));
942
943 wxButton *btnOk = new wxButton(this, wxID_CANCEL, _T("&Close"));
944 sizerTop->Add(m_btnModal, 0, wxALIGN_CENTER | wxALL, 5);
945 sizerTop->Add(m_btnModeless, 0, wxALIGN_CENTER | wxALL, 5);
946 sizerTop->Add(m_btnDelete, 0, wxALIGN_CENTER | wxALL, 5);
947 sizerTop->Add(btnOk, 0, wxALIGN_CENTER | wxALL, 5);
948
949 SetAutoLayout(TRUE);
950 SetSizer(sizerTop);
951
952 sizerTop->SetSizeHints(this);
953 sizerTop->Fit(this);
954
955 m_btnModal->SetFocus();
956 m_btnModal->SetDefault();
957 }
958
959 void MyModalDialog::OnButton(wxCommandEvent& event)
960 {
961 if ( event.GetEventObject() == m_btnDelete )
962 {
963 delete m_btnModal;
964 m_btnModal = NULL;
965
966 m_btnDelete->Disable();
967 }
968 else if ( event.GetEventObject() == m_btnModal )
969 {
970 wxGetTextFromUser(_T("Dummy prompt"),
971 _T("Modal dialog called from dialog"),
972 _T(""), this);
973 }
974 else if ( event.GetEventObject() == m_btnModeless )
975 {
976 (new MyModelessDialog(this))->Show();
977 }
978 else
979 {
980 event.Skip();
981 }
982 }