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