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