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