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