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