]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
compiles under win64
[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{
f0f43012
JS
521 wxString wildcards =
522#ifdef __WXMOTIF__
523 _T("C++ files (*.cpp)|*.cpp");
524#else
525 _T("All files (*.*)|*.*|C++ files (*.h;*.cpp)|*.h;*.cpp");
526#endif
78a189c9 527 wxFileDialog dialog(this, _T("Testing open multiple file dialog"),
f0f43012 528 _T(""), _T(""), wildcards,
470caaf9 529 wxMULTIPLE);
c61f4f6d
VZ
530
531 if (dialog.ShowModal() == wxID_OK)
532 {
533 wxArrayString paths, filenames;
534
535 dialog.GetPaths(paths);
536 dialog.GetFilenames(filenames);
537
538 wxString msg, s;
539 size_t count = paths.GetCount();
540 for ( size_t n = 0; n < count; n++ )
541 {
542 s.Printf(_T("File %d: %s (%s)\n"),
fba1b53b 543 (int)n, paths[n].c_str(), filenames[n].c_str());
c61f4f6d
VZ
544
545 msg += s;
546 }
f0f43012
JS
547 s.Printf(_T("Filter index: %d"), dialog.GetFilterIndex());
548 msg += s;
c61f4f6d 549
78a189c9 550 wxMessageDialog dialog2(this, msg, _T("Selected files"));
c61f4f6d
VZ
551 dialog2.ShowModal();
552 }
553}
554
d355d3fe 555void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5 556{
2f1cd905
VZ
557 wxFileDialog dialog(this,
558 _T("Testing save file dialog"),
559 _T(""),
2b5f62a0 560 _T("myletter.doc"),
2f1cd905
VZ
561 _T("Text files (*.txt)|*.txt|Document files (*.doc)|*.doc"),
562 wxSAVE|wxOVERWRITE_PROMPT);
c50f1fb9 563
2b5f62a0
VZ
564 dialog.SetFilterIndex(1);
565
c50f1fb9
VZ
566 if (dialog.ShowModal() == wxID_OK)
567 {
2b5f62a0
VZ
568 wxLogMessage(_T("%s, filter %d"),
569 dialog.GetPath().c_str(), dialog.GetFilterIndex());
c50f1fb9 570 }
457814b5
JS
571}
572
f09c8393 573void MyFrame::DoDirChoose(int style)
457814b5 574{
3f2711d5
VZ
575 // pass some initial dir to wxDirDialog
576 wxString dirHome;
577 wxGetHomeDir(&dirHome);
578
f09c8393 579 wxDirDialog dialog(this, _T("Testing directory picker"), dirHome, style);
c50f1fb9
VZ
580
581 if (dialog.ShowModal() == wxID_OK)
582 {
2b5f62a0 583 wxLogMessage(_T("Selected path: %s"), dialog.GetPath().c_str());
c50f1fb9
VZ
584 }
585}
586
f09c8393
VZ
587void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
588{
589 DoDirChoose(wxDD_DEFAULT_STYLE & ~wxDD_NEW_DIR_BUTTON);
590}
591
592void MyFrame::DirChooseNew(wxCommandEvent& WXUNUSED(event) )
593{
8768548b 594 DoDirChoose(wxDD_DEFAULT_STYLE | wxDD_NEW_DIR_BUTTON);
f09c8393
VZ
595}
596
af2b012d
VZ
597#if defined(__WXMSW__) || defined(__WXMAC__)
598
51a58d8b
JS
599void MyFrame::GenericDirChoose(wxCommandEvent& WXUNUSED(event) )
600{
9f8b329b 601#if !(defined(__WXMSW__) || defined(__WXMAC__)) || wxUSE_DIRDLGG
51a58d8b
JS
602 // pass some initial dir to wxDirDialog
603 wxString dirHome;
604 wxGetHomeDir(&dirHome);
605
78a189c9 606 wxGenericDirDialog dialog(this, _T("Testing generic directory picker"), dirHome);
51a58d8b
JS
607
608 if (dialog.ShowModal() == wxID_OK)
609 {
78a189c9 610 wxMessageDialog dialog2(this, dialog.GetPath(), _T("Selected path"));
51a58d8b
JS
611 dialog2.ShowModal();
612 }
4ad3c82f 613#else
4693b20c
MB
614 wxLogError(wxT("Sorry, generic dir dialog not available:\n")
615 wxT("set wxUSE_DIRDLGG to 1 and recompile"));
4ad3c82f 616#endif
51a58d8b
JS
617}
618
af2b012d
VZ
619#endif // wxMSW || wxMAC
620
f6bcfd97
BP
621void MyFrame::ModalDlg(wxCommandEvent& WXUNUSED(event))
622{
623 MyModalDialog dlg(this);
624 dlg.ShowModal();
625}
626
4c45f240
VZ
627void MyFrame::ModelessDlg(wxCommandEvent& event)
628{
2f82899b 629 bool show = GetMenuBar()->IsChecked(event.GetId());
4c45f240
VZ
630
631 if ( show )
632 {
633 if ( !m_dialog )
634 {
635 m_dialog = new MyModelessDialog(this);
636 }
637
638 m_dialog->Show(TRUE);
639 }
640 else // hide
641 {
642 m_dialog->Hide();
643 }
644}
645
87728739 646void MyFrame::ShowTip(wxCommandEvent& WXUNUSED(event))
c50f1fb9 647{
78bd7ed3 648#if wxUSE_STARTUP_TIPS
c50f1fb9
VZ
649 static size_t s_index = (size_t)-1;
650
651 if ( s_index == (size_t)-1 )
652 {
653 srand(time(NULL));
654
655 // this is completely bogus, we don't know how many lines are there
656 // in the file, but who cares, it's a demo only...
657 s_index = rand() % 5;
658 }
659
2f1cd905 660 wxTipProvider *tipProvider = wxCreateFileTipProvider(_T("tips.txt"), s_index);
c50f1fb9
VZ
661
662 bool showAtStartup = wxShowTip(this, tipProvider);
663
664 if ( showAtStartup )
665 {
2f1cd905 666 wxMessageBox(_T("Will show tips on startup"), _T("Tips dialog"),
c50f1fb9
VZ
667 wxOK | wxICON_INFORMATION, this);
668 }
457814b5 669
c50f1fb9
VZ
670 s_index = tipProvider->GetCurrentTip();
671 delete tipProvider;
78bd7ed3 672#endif
457814b5
JS
673}
674
d355d3fe 675void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5 676{
c50f1fb9 677 Close(TRUE);
457814b5
JS
678}
679
abceee76
VZ
680#if wxUSE_PROGRESSDLG
681
682void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
683{
684 static const int max = 10;
685
2f1cd905 686 wxProgressDialog dialog(_T("Progress dialog example"),
78a189c9 687 _T("An informative message"),
abceee76
VZ
688 max, // range
689 this, // parent
690 wxPD_CAN_ABORT |
691 wxPD_APP_MODAL |
df26c4c6 692 // wxPD_AUTO_HIDE | -- try this as well
abceee76
VZ
693 wxPD_ELAPSED_TIME |
694 wxPD_ESTIMATED_TIME |
695 wxPD_REMAINING_TIME);
696
697 bool cont = TRUE;
d8ddee9c 698 for ( int i = 0; i <= max; i++ )
abceee76
VZ
699 {
700 wxSleep(1);
701 if ( i == max )
702 {
78a189c9 703 cont = dialog.Update(i, _T("That's all, folks!"));
abceee76
VZ
704 }
705 else if ( i == max / 2 )
706 {
78a189c9 707 cont = dialog.Update(i, _T("Only a half left (very long message)!"));
abceee76
VZ
708 }
709 else
710 {
711 cont = dialog.Update(i);
712 }
d8ddee9c
VZ
713
714 if ( !cont )
715 {
78a189c9
VZ
716 if ( wxMessageBox(_T("Do you really want to cancel?"),
717 _T("Progress dialog question"), // caption
718 wxYES_NO | wxICON_QUESTION) == wxYES )
d8ddee9c
VZ
719 break;
720
721 dialog.Resume();
722 }
abceee76
VZ
723 }
724
725 if ( !cont )
726 {
4693b20c 727 wxLogStatus(wxT("Progress dialog aborted!"));
abceee76
VZ
728 }
729 else
730 {
4693b20c 731 wxLogStatus(wxT("Countdown from %d finished"), max);
abceee76
VZ
732 }
733}
734
735#endif // wxUSE_PROGRESSDLG
736
a62b0bcc
VZ
737#if wxUSE_BUSYINFO
738
739void MyFrame::ShowBusyInfo(wxCommandEvent& WXUNUSED(event))
740{
21977bac
VZ
741 wxWindowDisabler disableAll;
742
9f84eccd 743 wxBusyInfo info(_T("Working, please wait..."), this);
a62b0bcc 744
5b636c67 745 for ( int i = 0; i < 18; i++ )
a62b0bcc 746 {
5b636c67 747 //wxUsleep(100);
a62b0bcc
VZ
748 wxTheApp->Yield();
749 }
5b636c67
VZ
750
751 wxSleep(2);
752 //wxWakeUpIdle();
a62b0bcc
VZ
753}
754
755#endif // wxUSE_BUSYINFO
756
761989ff
VZ
757#if wxUSE_FINDREPLDLG
758
759void MyFrame::ShowReplaceDialog( wxCommandEvent& WXUNUSED(event) )
760{
14fca738
VZ
761 if ( m_dlgReplace )
762 {
763 delete m_dlgReplace;
764 m_dlgReplace = NULL;
765 }
766 else
767 {
768 m_dlgReplace = new wxFindReplaceDialog
769 (
770 this,
771 &m_findData,
78a189c9 772 _T("Find and replace dialog"),
14fca738
VZ
773 wxFR_REPLACEDIALOG
774 );
775
776 m_dlgReplace->Show(TRUE);
777 }
761989ff
VZ
778}
779
780void MyFrame::ShowFindDialog( wxCommandEvent& WXUNUSED(event) )
781{
14fca738
VZ
782 if ( m_dlgFind )
783 {
784 delete m_dlgFind;
785 m_dlgFind = NULL;
786 }
787 else
788 {
789 m_dlgFind = new wxFindReplaceDialog
790 (
791 this,
792 &m_findData,
78a189c9 793 _T("Find dialog"),
14fca738
VZ
794 // just for testing
795 wxFR_NOWHOLEWORD
796 );
797
798 m_dlgFind->Show(TRUE);
799 }
761989ff
VZ
800}
801
802static wxString DecodeFindDialogEventFlags(int flags)
803{
804 wxString str;
78a189c9 805 str << (flags & wxFR_DOWN ? _T("down") : _T("up")) << _T(", ")
2f1cd905
VZ
806 << (flags & wxFR_WHOLEWORD ? _T("whole words only, ") : _T(""))
807 << (flags & wxFR_MATCHCASE ? _T("") : _T("not "))
78a189c9 808 << _T("case sensitive");
761989ff
VZ
809
810 return str;
811}
812
813void MyFrame::OnFindDialog(wxFindDialogEvent& event)
814{
815 wxEventType type = event.GetEventType();
816
817 if ( type == wxEVT_COMMAND_FIND || type == wxEVT_COMMAND_FIND_NEXT )
818 {
4693b20c 819 wxLogMessage(wxT("Find %s'%s' (flags: %s)"),
eba33006 820 type == wxEVT_COMMAND_FIND_NEXT ? wxT("next ") : wxT(""),
761989ff
VZ
821 event.GetFindString().c_str(),
822 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
823 }
824 else if ( type == wxEVT_COMMAND_FIND_REPLACE ||
825 type == wxEVT_COMMAND_FIND_REPLACE_ALL )
826 {
4693b20c 827 wxLogMessage(wxT("Replace %s'%s' with '%s' (flags: %s)"),
eba33006 828 type == wxEVT_COMMAND_FIND_REPLACE_ALL ? _T("all ") : wxT(""),
761989ff
VZ
829 event.GetFindString().c_str(),
830 event.GetReplaceString().c_str(),
831 DecodeFindDialogEventFlags(event.GetFlags()).c_str());
832 }
833 else if ( type == wxEVT_COMMAND_FIND_CLOSE )
834 {
14fca738
VZ
835 wxFindReplaceDialog *dlg = event.GetDialog();
836
df26c4c6 837 int idMenu;
14fca738
VZ
838 const wxChar *txt;
839 if ( dlg == m_dlgFind )
840 {
841 txt = _T("Find");
df26c4c6 842 idMenu = DIALOGS_FIND;
14fca738
VZ
843 m_dlgFind = NULL;
844 }
845 else if ( dlg == m_dlgReplace )
846 {
847 txt = _T("Replace");
df26c4c6 848 idMenu = DIALOGS_REPLACE;
14fca738
VZ
849 m_dlgReplace = NULL;
850 }
851 else
852 {
853 txt = _T("Unknown");
df26c4c6 854 idMenu = -1;
14fca738 855
2f1cd905 856 wxFAIL_MSG( _T("unexpected event") );
14fca738
VZ
857 }
858
df26c4c6
VZ
859 wxLogMessage(wxT("%s dialog is being closed."), txt);
860
861 if ( idMenu != -1 )
862 {
863 GetMenuBar()->Check(idMenu, FALSE);
864 }
761989ff 865
14fca738 866 dlg->Destroy();
761989ff
VZ
867 }
868 else
869 {
4693b20c 870 wxLogError(wxT("Unknown find dialog event!"));
761989ff
VZ
871 }
872}
873
874#endif // wxUSE_FINDREPLDLG
875
abceee76
VZ
876// ----------------------------------------------------------------------------
877// MyCanvas
878// ----------------------------------------------------------------------------
879
d355d3fe 880void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5 881{
c50f1fb9 882 wxPaintDC dc(this);
457814b5
JS
883 dc.SetFont(wxGetApp().m_canvasFont);
884 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
885 dc.SetBackgroundMode(wxTRANSPARENT);
9f84eccd 886 dc.DrawText(_T("wxWindows common dialogs test application"), 10, 10);
457814b5
JS
887}
888
4c45f240
VZ
889// ----------------------------------------------------------------------------
890// MyModelessDialog
891// ----------------------------------------------------------------------------
457814b5 892
4c45f240 893MyModelessDialog::MyModelessDialog(wxWindow *parent)
9f84eccd 894 : wxDialog(parent, -1, wxString(_T("Modeless dialog")))
4c45f240 895{
cbc66a27
VZ
896 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
897
78a189c9
VZ
898 wxButton *btn = new wxButton(this, DIALOGS_MODELESS_BTN, _T("Press me"));
899 wxCheckBox *check = new wxCheckBox(this, -1, _T("Should be disabled"));
cbc66a27
VZ
900 check->Disable();
901
902 sizerTop->Add(btn, 1, wxEXPAND | wxALL, 5);
903 sizerTop->Add(check, 1, wxEXPAND | wxALL, 5);
904
905 SetAutoLayout(TRUE);
906 SetSizer(sizerTop);
907
908 sizerTop->SetSizeHints(this);
909 sizerTop->Fit(this);
4c45f240 910}
abceee76 911
5d987909
VZ
912void MyModelessDialog::OnButton(wxCommandEvent& WXUNUSED(event))
913{
2f1cd905 914 wxMessageBox(_T("Button pressed in modeless dialog"), _T("Info"),
5d987909
VZ
915 wxOK | wxICON_INFORMATION, this);
916}
917
abceee76
VZ
918void MyModelessDialog::OnClose(wxCloseEvent& event)
919{
920 if ( event.CanVeto() )
921 {
2f1cd905 922 wxMessageBox(_T("Use the menu item to close this dialog"),
78a189c9 923 _T("Modeless dialog"),
abceee76
VZ
924 wxOK | wxICON_INFORMATION, this);
925
926 event.Veto();
927 }
928}
929
f6bcfd97
BP
930// ----------------------------------------------------------------------------
931// MyModalDialog
932// ----------------------------------------------------------------------------
933
934MyModalDialog::MyModalDialog(wxWindow *parent)
f3dbeba4 935 : wxDialog(parent, -1, wxString(_T("Modal dialog")))
f6bcfd97
BP
936{
937 wxBoxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
938
5315ebfa
VZ
939 m_btnModal = new wxButton(this, -1, _T("&Modal dialog..."));
940 m_btnModeless = new wxButton(this, -1, _T("Mode&less dialog"));
78a189c9 941 m_btnDelete = new wxButton(this, -1, _T("&Delete button"));
5315ebfa 942
78a189c9 943 wxButton *btnOk = new wxButton(this, wxID_CANCEL, _T("&Close"));
5315ebfa
VZ
944 sizerTop->Add(m_btnModal, 0, wxALIGN_CENTER | wxALL, 5);
945 sizerTop->Add(m_btnModeless, 0, wxALIGN_CENTER | wxALL, 5);
f6bcfd97 946 sizerTop->Add(m_btnDelete, 0, wxALIGN_CENTER | wxALL, 5);
9b70bb91 947 sizerTop->Add(btnOk, 0, wxALIGN_CENTER | wxALL, 5);
f6bcfd97
BP
948
949 SetAutoLayout(TRUE);
950 SetSizer(sizerTop);
951
952 sizerTop->SetSizeHints(this);
953 sizerTop->Fit(this);
954
5315ebfa
VZ
955 m_btnModal->SetFocus();
956 m_btnModal->SetDefault();
f6bcfd97
BP
957}
958
959void MyModalDialog::OnButton(wxCommandEvent& event)
960{
961 if ( event.GetEventObject() == m_btnDelete )
962 {
5315ebfa
VZ
963 delete m_btnModal;
964 m_btnModal = NULL;
f6bcfd97
BP
965
966 m_btnDelete->Disable();
967 }
5315ebfa 968 else if ( event.GetEventObject() == m_btnModal )
f6bcfd97 969 {
78a189c9
VZ
970 wxGetTextFromUser(_T("Dummy prompt"),
971 _T("Modal dialog called from dialog"),
972 _T(""), this);
f6bcfd97 973 }
5315ebfa
VZ
974 else if ( event.GetEventObject() == m_btnModeless )
975 {
976 (new MyModelessDialog(this))->Show();
977 }
f6bcfd97
BP
978 else
979 {
980 event.Skip();
981 }
982}