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