]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
draw the selected items in another colour when the control doesn't have focus, a...
[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
VZ
93 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
94
95 EVT_BUTTON(DIALOGS_MODELESS_BTN, MyFrame::OnButton)
96END_EVENT_TABLE()
abceee76 97
f6bcfd97
BP
98BEGIN_EVENT_TABLE(MyModalDialog, wxDialog)
99 EVT_BUTTON(-1, MyModalDialog::OnButton)
100END_EVENT_TABLE()
101
abceee76
VZ
102BEGIN_EVENT_TABLE(MyModelessDialog, wxDialog)
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;
191}
457814b5 192
d355d3fe 193void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5 194{
f6bcfd97
BP
195 wxColourData data;
196 data.SetChooseFull(TRUE);
197 for (int i = 0; i < 16; i++)
198 {
457814b5
JS
199 wxColour colour(i*16, i*16, i*16);
200 data.SetCustomColour(i, colour);
f6bcfd97 201 }
d93c719a 202
f6bcfd97
BP
203 wxColourDialog *dialog = new wxColourDialog(this, &data);
204 dialog->SetTitle("Choose the background colour");
205 if (dialog->ShowModal() == wxID_OK)
206 {
457814b5
JS
207 wxColourData retData = dialog->GetColourData();
208 wxColour col = retData.GetColour();
457814b5
JS
209 myCanvas->SetBackgroundColour(col);
210 myCanvas->Clear();
211 myCanvas->Refresh();
f6bcfd97
BP
212 }
213 dialog->Destroy();
457814b5
JS
214}
215
d355d3fe 216void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
217{
218 wxFontData data;
219 data.SetInitialFont(wxGetApp().m_canvasFont);
220 data.SetColour(wxGetApp().m_canvasTextColour);
d93c719a 221
457814b5
JS
222 wxFontDialog *dialog = new wxFontDialog(this, &data);
223 if (dialog->ShowModal() == wxID_OK)
224 {
225 wxFontData retData = dialog->GetFontData();
226 wxGetApp().m_canvasFont = retData.GetChosenFont();
227 wxGetApp().m_canvasTextColour = retData.GetColour();
228 myCanvas->Refresh();
229 }
5919d76b 230 dialog->Destroy();
457814b5
JS
231}
232
dfad0599 233#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
d355d3fe 234void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
235{
236 wxColourData data;
237 data.SetChooseFull(TRUE);
238 for (int i = 0; i < 16; i++)
239 {
240 wxColour colour(i*16, i*16, i*16);
241 data.SetCustomColour(i, colour);
242 }
d93c719a 243
457814b5
JS
244 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
245 if (dialog->ShowModal() == wxID_OK)
246 {
247 wxColourData retData = dialog->GetColourData();
248 wxColour col = retData.GetColour();
249// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
250 myCanvas->SetBackgroundColour(col);
251 myCanvas->Clear();
252 myCanvas->Refresh();
253 }
5919d76b 254 dialog->Destroy();
457814b5
JS
255}
256
d355d3fe 257void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
258{
259 wxFontData data;
260 data.SetInitialFont(wxGetApp().m_canvasFont);
261 data.SetColour(wxGetApp().m_canvasTextColour);
262
263 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
264 if (dialog->ShowModal() == wxID_OK)
265 {
266 wxFontData retData = dialog->GetFontData();
267 wxGetApp().m_canvasFont = retData.GetChosenFont();
268 wxGetApp().m_canvasTextColour = retData.GetColour();
269 myCanvas->Refresh();
270 }
5919d76b 271 dialog->Destroy();
457814b5 272}
d93c719a
VZ
273#endif // wxTEST_GENERIC_DIALOGS_IN_MSW
274
275void MyFrame::LogDialog(wxCommandEvent& event)
276{
2c8e4738
VZ
277 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
278 // being flushed -- test it
279 {
280 wxBusyCursor bc;
281 wxLogMessage("This is some message - everything is ok so far.");
282 wxLogMessage("Another message...\n... this one is on multiple lines");
283 wxLogWarning("And then something went wrong!");
9fe52545
VZ
284
285 // and if ~wxBusyCursor doesn't do it, then call it manually
286 wxYield();
2c8e4738
VZ
287 }
288
d93c719a 289 wxLogError("Intermediary error handler decided to abort.");
5888ef1e
VZ
290 wxLogError("The top level caller detected an unrecoverable error.");
291
7923c649
VZ
292 wxLog::FlushActive();
293
c86423f9 294 wxLogMessage("And this is the same dialog but with only one message.");
d93c719a 295}
457814b5 296
d355d3fe 297void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5 298{
f6bcfd97 299 wxMessageDialog dialog( NULL, "This is a message box\nA long, long string to test out the message box properly",
c50f1fb9 300 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
457814b5
JS
301
302 dialog.ShowModal();
303}
304
c49245f8
VZ
305void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
306{
e65cc56a
RR
307 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
308 "Even two rows of text.",
d93c719a 309 "Enter a number:", "Numeric input test",
e65cc56a 310 50, 0, 100, this );
c49245f8
VZ
311
312 wxString msg;
313 int icon;
314 if ( res == -1 )
315 {
316 msg = "Invalid number entered or dialog cancelled.";
317 icon = wxICON_HAND;
318 }
319 else
d93c719a
VZ
320 {
321 msg.Printf(_T("You've entered %lu"), res );
c49245f8
VZ
322 icon = wxICON_INFORMATION;
323 }
324
325 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
326}
327
a294c6d5 328void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
457814b5 329{
a294c6d5 330 wxString pwd = wxGetPasswordFromUser("Enter password:",
3ca6a5f0 331 "Password entry dialog",
a294c6d5
VZ
332 "",
333 this);
334 if ( !!pwd )
335 {
336 wxMessageBox(wxString::Format("Your password is '%s'", pwd.c_str()),
337 "Got password", wxOK | wxICON_INFORMATION, this);
338 }
339}
340
341void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
342{
343 wxTextEntryDialog dialog(this,
344 "This is a small sample\n"
345 "A long, long string to test out the text entrybox",
346 "Please enter a string",
347 "Default value",
348 wxOK | wxCANCEL);
457814b5
JS
349
350 if (dialog.ShowModal() == wxID_OK)
351 {
c50f1fb9
VZ
352 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
353 dialog2.ShowModal();
457814b5
JS
354 }
355}
356
d355d3fe 357void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 358{
ef77f91e
JS
359 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
360 int n = 5;
457814b5 361
ef77f91e
JS
362 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
363 "Please select a value", n, (const wxString *)choices);
457814b5 364
ef77f91e
JS
365 dialog.SetSelection(2);
366
367 if (dialog.ShowModal() == wxID_OK)
368 {
369 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
370 dialog2.ShowModal();
371 }
457814b5
JS
372}
373
d6c9c1b7
VZ
374void MyFrame::MultiChoice(wxCommandEvent& WXUNUSED(event) )
375{
376 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
377 int n = 5;
378
379 wxArrayInt selections;
380 size_t count = wxGetMultipleChoices(selections,
381 "This is a small sample\n"
382 "A multi-choice convenience dialog",
383 "Please select a value",
384 n, (const wxString *)choices,
385 this);
386 if ( count )
387 {
3d49ce44
VZ
388 wxString msg;
389 msg.Printf("You selected %u items:\n", count);
d6c9c1b7
VZ
390 for ( size_t n = 0; n < count; n++ )
391 {
3d49ce44
VZ
392 msg += wxString::Format("\t%u: %u (%s)\n", n, selections[n],
393 choices[selections[n]].c_str());
d6c9c1b7 394 }
3d49ce44 395 wxLogMessage(msg);
d6c9c1b7
VZ
396 }
397 //else: cancelled or nothing selected
398}
399
d355d3fe 400void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5 401{
159b66c0
RR
402// wxFAIL_MSG( "Test assert" );
403
c50f1fb9 404 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
457814b5 405
c50f1fb9
VZ
406 if (dialog.ShowModal() == wxID_OK)
407 {
5919d76b 408 wxString info;
13393ab6
RR
409 info.Printf(_T("Full file name: %s\n")
410 _T("Path: %s\n")
411 _T("Name: %s"),
5919d76b
VZ
412 dialog.GetPath().c_str(),
413 dialog.GetDirectory().c_str(),
414 dialog.GetFilename().c_str());
c50f1fb9
VZ
415 wxMessageDialog dialog2(this, info, "Selected file");
416 dialog2.ShowModal();
417 }
457814b5
JS
418}
419
35b45b33
VZ
420// this shows how to take advantage of specifying a default extension in the
421// call to wxFileSelector: it is remembered after each new call and the next
422// one will use it by default
423void MyFrame::FileOpen2(wxCommandEvent& WXUNUSED(event) )
424{
159b66c0
RR
425 wxOnAssert( "Test assert.txt", 20, "Test" );
426 return;
427
35b45b33
VZ
428 static wxString s_extDef;
429 wxString path = wxFileSelector(
430 _T("Select the file to load"),
431 _T(""), _T(""),
432 s_extDef,
433 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
434 0,
435 this
436 );
437
438 if ( !path )
439 return;
440
441 // it is just a sample, would use wxSplitPath in real program
442 s_extDef = path.AfterLast(_T('.'));
443
444 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
37d403aa 445 (const wxChar*) path, (const wxChar*) s_extDef);
35b45b33
VZ
446}
447
c61f4f6d
VZ
448void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
449{
450 wxFileDialog dialog(this, "Testing open multiple file dialog",
470caaf9
VZ
451 "", "", wxFileSelectorDefaultWildcardStr,
452 wxMULTIPLE);
c61f4f6d
VZ
453
454 if (dialog.ShowModal() == wxID_OK)
455 {
456 wxArrayString paths, filenames;
457
458 dialog.GetPaths(paths);
459 dialog.GetFilenames(filenames);
460
461 wxString msg, s;
462 size_t count = paths.GetCount();
463 for ( size_t n = 0; n < count; n++ )
464 {
465 s.Printf(_T("File %d: %s (%s)\n"),
466 n, paths[n].c_str(), filenames[n].c_str());
467
468 msg += s;
469 }
470
471 wxMessageDialog dialog2(this, msg, "Selected files");
472 dialog2.ShowModal();
473 }
474}
475
d355d3fe 476void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5 477{
ed58dbea 478 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
c50f1fb9
VZ
479 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
480 wxSAVE|wxOVERWRITE_PROMPT);
481
482 if (dialog.ShowModal() == wxID_OK)
483 {
484 wxChar buf[400];
485 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
486 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
487 dialog2.ShowModal();
488 }
457814b5
JS
489}
490
d355d3fe 491void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 492{
3f2711d5
VZ
493 // pass some initial dir to wxDirDialog
494 wxString dirHome;
495 wxGetHomeDir(&dirHome);
496
497 wxDirDialog dialog(this, "Testing directory picker", dirHome);
c50f1fb9
VZ
498
499 if (dialog.ShowModal() == wxID_OK)
500 {
501 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
502 dialog2.ShowModal();
503 }
504}
505
51a58d8b
JS
506void MyFrame::GenericDirChoose(wxCommandEvent& WXUNUSED(event) )
507{
4ad3c82f 508#if !defined(__WXMSW__) || defined(wxUSE_DIRDLGG) && wxUSE_DIRDLGG
51a58d8b
JS
509 // pass some initial dir to wxDirDialog
510 wxString dirHome;
511 wxGetHomeDir(&dirHome);
512
513 wxGenericDirDialog dialog(this, "Testing generic directory picker", dirHome);
514
515 if (dialog.ShowModal() == wxID_OK)
516 {
517 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
518 dialog2.ShowModal();
519 }
4ad3c82f
VZ
520#else
521 wxLogError("Sorry, generic dir dialog not available:\n"
522 "set wxUSE_DIRDLGG to 1 and recompile");
523#endif
51a58d8b
JS
524}
525
f6bcfd97
BP
526void MyFrame::ModalDlg(wxCommandEvent& WXUNUSED(event))
527{
528 MyModalDialog dlg(this);
529 dlg.ShowModal();
530}
531
4c45f240
VZ
532void MyFrame::ModelessDlg(wxCommandEvent& event)
533{
2f82899b 534 bool show = GetMenuBar()->IsChecked(event.GetId());
4c45f240
VZ
535
536 if ( show )
537 {
538 if ( !m_dialog )
539 {
540 m_dialog = new MyModelessDialog(this);
541 }
542
543 m_dialog->Show(TRUE);
544 }
545 else // hide
546 {
547 m_dialog->Hide();
548 }
549}
550
551void MyFrame::OnButton(wxCommandEvent& WXUNUSED(event))
552{
553 wxMessageBox("Button pressed in modeless dialog", "Info",
554 wxOK | wxICON_INFORMATION, this);
555}
556
c50f1fb9
VZ
557void MyFrame::ShowTip(wxCommandEvent& event)
558{
78bd7ed3 559#if wxUSE_STARTUP_TIPS
c50f1fb9
VZ
560 static size_t s_index = (size_t)-1;
561
562 if ( s_index == (size_t)-1 )
563 {
564 srand(time(NULL));
565
566 // this is completely bogus, we don't know how many lines are there
567 // in the file, but who cares, it's a demo only...
568 s_index = rand() % 5;
569 }
570
571 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
572
573 bool showAtStartup = wxShowTip(this, tipProvider);
574
575 if ( showAtStartup )
576 {
577 wxMessageBox("Will show tips on startup", "Tips dialog",
578 wxOK | wxICON_INFORMATION, this);
579 }
457814b5 580
c50f1fb9
VZ
581 s_index = tipProvider->GetCurrentTip();
582 delete tipProvider;
78bd7ed3 583#endif
457814b5
JS
584}
585
d355d3fe 586void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5 587{
c50f1fb9 588 Close(TRUE);
457814b5
JS
589}
590
abceee76
VZ
591#if wxUSE_PROGRESSDLG
592
593void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
594{
595 static const int max = 10;
596
597 wxProgressDialog dialog("Progress dialog example",
598 "An informative message",
599 max, // range
600 this, // parent
601 wxPD_CAN_ABORT |
602 wxPD_APP_MODAL |
603 wxPD_ELAPSED_TIME |
604 wxPD_ESTIMATED_TIME |
605 wxPD_REMAINING_TIME);
606
607 bool cont = TRUE;
608 for ( int i = 0; i <= max && cont; i++ )
609 {
610 wxSleep(1);
611 if ( i == max )
612 {
613 cont = dialog.Update(i, "That's all, folks!");
614 }
615 else if ( i == max / 2 )
616 {
617 cont = dialog.Update(i, "Only a half left (very long message)!");
618 }
619 else
620 {
621 cont = dialog.Update(i);
622 }
623 }
624
625 if ( !cont )
626 {
627 wxLogStatus("Progress dialog aborted!");
628 }
629 else
630 {
631 wxLogStatus("Countdown from %d finished", max);
632 }
633}
634
635#endif // wxUSE_PROGRESSDLG
636
761989ff
VZ
637#if wxUSE_FINDREPLDLG
638
639void MyFrame::ShowReplaceDialog( wxCommandEvent& WXUNUSED(event) )
640{
641 wxFindReplaceDialog *dialog = new wxFindReplaceDialog
642 (
643 this,
644 &m_findData,
645 "Find and replace dialog",
646 wxFR_REPLACEDIALOG
647 );
648 dialog->Show();
649}
650
651void MyFrame::ShowFindDialog( wxCommandEvent& WXUNUSED(event) )
652{
653 wxFindReplaceDialog *dialog = new wxFindReplaceDialog
654 (
655 this,
656 &m_findData,
657 "Find dialog",
658 // just for testing
659 wxFR_NOWHOLEWORD
660 );
661 dialog->Show();
662}
663
664static wxString DecodeFindDialogEventFlags(int flags)
665{
666 wxString str;
667 str << (flags & wxFR_DOWN ? "down" : "up") << ", "
668 << (flags & wxFR_WHOLEWORD ? "whole words only, " : "")
669 << (flags & wxFR_MATCHCASE ? "" : "not ")
670 << "case sensitive";
671
672 return str;
673}
674
675void MyFrame::OnFindDialog(wxFindDialogEvent& event)
676{
677 wxEventType type = event.GetEventType();
678
679 if ( type == wxEVT_COMMAND_FIND || type == wxEVT_COMMAND_FIND_NEXT )
680 {
681 wxLogMessage("Find %s'%s' (flags: %s)",
682 type == wxEVT_COMMAND_FIND_NEXT ? "next " : "",
683 event.GetFindString().c_str(),
684 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
685 }
686 else if ( type == wxEVT_COMMAND_FIND_REPLACE ||
687 type == wxEVT_COMMAND_FIND_REPLACE_ALL )
688 {
689 wxLogMessage("Replace %s'%s' with '%s' (flags: %s)",
690 type == wxEVT_COMMAND_FIND_REPLACE_ALL ? "all " : "",
691 event.GetFindString().c_str(),
692 event.GetReplaceString().c_str(),
693 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
694 }
695 else if ( type == wxEVT_COMMAND_FIND_CLOSE )
696 {
697 wxLogMessage("Find dialog is being closed.");
698
699 event.GetDialog()->Destroy();
700 }
701 else
702 {
703 wxLogError("Unknown find dialog event!");
704 }
705}
706
707#endif // wxUSE_FINDREPLDLG
708
abceee76
VZ
709// ----------------------------------------------------------------------------
710// MyCanvas
711// ----------------------------------------------------------------------------
712
d355d3fe 713void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5 714{
c50f1fb9 715 wxPaintDC dc(this);
457814b5
JS
716 dc.SetFont(wxGetApp().m_canvasFont);
717 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
718 dc.SetBackgroundMode(wxTRANSPARENT);
719 dc.DrawText("wxWindows common dialogs test application", 10, 10);
720}
721
4c45f240
VZ
722// ----------------------------------------------------------------------------
723// MyModelessDialog
724// ----------------------------------------------------------------------------
457814b5 725
4c45f240
VZ
726MyModelessDialog::MyModelessDialog(wxWindow *parent)
727 : wxDialog(parent, -1, wxString("Modeless dialog"))
728{
cbc66a27
VZ
729 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
730
731 wxButton *btn = new wxButton(this, DIALOGS_MODELESS_BTN, "Press me");
732 wxCheckBox *check = new wxCheckBox(this, -1, "Should be disabled");
733 check->Disable();
734
735 sizerTop->Add(btn, 1, wxEXPAND | wxALL, 5);
736 sizerTop->Add(check, 1, wxEXPAND | wxALL, 5);
737
738 SetAutoLayout(TRUE);
739 SetSizer(sizerTop);
740
741 sizerTop->SetSizeHints(this);
742 sizerTop->Fit(this);
4c45f240 743}
abceee76
VZ
744
745void MyModelessDialog::OnClose(wxCloseEvent& event)
746{
747 if ( event.CanVeto() )
748 {
749 wxMessageBox("Use the menu item to close this dialog",
750 "Modeless dialog",
751 wxOK | wxICON_INFORMATION, this);
752
753 event.Veto();
754 }
755}
756
f6bcfd97
BP
757// ----------------------------------------------------------------------------
758// MyModalDialog
759// ----------------------------------------------------------------------------
760
761MyModalDialog::MyModalDialog(wxWindow *parent)
762 : wxDialog(parent, -1, wxString("Modal dialog"))
763{
764 wxBoxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
765
766 m_btnFocused = new wxButton(this, -1, "Default button");
767 m_btnDelete = new wxButton(this, -1, "&Delete button");
768 sizerTop->Add(m_btnFocused, 0, wxALIGN_CENTER | wxALL, 5);
769 sizerTop->Add(m_btnDelete, 0, wxALIGN_CENTER | wxALL, 5);
770
771 SetAutoLayout(TRUE);
772 SetSizer(sizerTop);
773
774 sizerTop->SetSizeHints(this);
775 sizerTop->Fit(this);
776
777 m_btnFocused->SetFocus();
778 m_btnFocused->SetDefault();
779}
780
781void MyModalDialog::OnButton(wxCommandEvent& event)
782{
783 if ( event.GetEventObject() == m_btnDelete )
784 {
785 delete m_btnFocused;
786 m_btnFocused = NULL;
787
788 m_btnDelete->Disable();
789 }
790 else if ( event.GetEventObject() == m_btnFocused )
791 {
792 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",
793 "", this);
794 }
795 else
796 {
797 event.Skip();
798 }
799}