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