]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
don't crash when invalid colour is set as fg/bg colour
[wxWidgets.git] / samples / dialogs / dialogs.cpp
CommitLineData
457814b5
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialogs.cpp
3// Purpose: Common dialogs demo
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
c50f1fb9 9// Licence: wxWindows license
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#pragma interface
15#endif
16
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include "wx/wx.h"
26#endif
27
329e86bf
RR
28#include "wx/colordlg.h"
29#include "wx/filedlg.h"
30#include "wx/dirdlg.h"
31#include "wx/fontdlg.h"
32#include "wx/choicdlg.h"
33#include "wx/tipdlg.h"
abceee76 34#include "wx/progdlg.h"
457814b5 35
dfad0599
JS
36#define wxTEST_GENERIC_DIALOGS_IN_MSW 0
37
38#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
39#include <wx/generic/colrdlgg.h>
40#include <wx/generic/fontdlgg.h>
41#endif
42
4ad3c82f
VZ
43#if !defined(__WXMSW__) || defined(wxUSE_DIRDLGG) && wxUSE_DIRDLGG
44// New wxGenericDirCtrl
45#include "wx/dirctrl.h"
46#endif
47
457814b5
JS
48#include "dialogs.h"
49
50IMPLEMENT_APP(MyApp)
51
4c45f240
VZ
52BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
53 EVT_PAINT(MyCanvas::OnPaint)
54END_EVENT_TABLE()
55
56BEGIN_EVENT_TABLE(MyFrame, wxFrame)
57 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
58 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
59 EVT_MENU(DIALOGS_LOG_DIALOG, MyFrame::LogDialog)
60 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
61 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
62 EVT_MENU(DIALOGS_PASSWORD_ENTRY, MyFrame::PasswordEntry)
63 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
64 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
d6c9c1b7 65 EVT_MENU(DIALOGS_MULTI_CHOICE, MyFrame::MultiChoice)
4c45f240 66 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
35b45b33 67 EVT_MENU(DIALOGS_FILE_OPEN2, MyFrame::FileOpen2)
4c45f240
VZ
68 EVT_MENU(DIALOGS_FILES_OPEN, MyFrame::FilesOpen)
69 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
70 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
51a58d8b 71 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE, MyFrame::GenericDirChoose)
f6bcfd97 72 EVT_MENU(DIALOGS_MODAL, MyFrame::ModalDlg)
4c45f240
VZ
73 EVT_MENU(DIALOGS_MODELESS, MyFrame::ModelessDlg)
74 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
75#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
76 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
77 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
abceee76
VZ
78#endif
79#if wxUSE_PROGRESSDLG
80 EVT_MENU(DIALOGS_PROGRESS, MyFrame::ShowProgress)
4c45f240
VZ
81#endif
82 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
83
84 EVT_BUTTON(DIALOGS_MODELESS_BTN, MyFrame::OnButton)
85END_EVENT_TABLE()
abceee76 86
f6bcfd97
BP
87BEGIN_EVENT_TABLE(MyModalDialog, wxDialog)
88 EVT_BUTTON(-1, MyModalDialog::OnButton)
89END_EVENT_TABLE()
90
abceee76
VZ
91BEGIN_EVENT_TABLE(MyModelessDialog, wxDialog)
92 EVT_CLOSE(MyModelessDialog::OnClose)
93END_EVENT_TABLE()
94
c67daf87 95MyCanvas *myCanvas = (MyCanvas *) NULL;
457814b5 96
457814b5 97// `Main program' equivalent, creating windows and returning main app frame
4c45f240 98bool MyApp::OnInit()
457814b5 99{
e5ea3f7a 100#if defined(__WXGTK__) && defined(wxUSE_UNICODE)
dcf924a3 101 wxConvCurrent = &wxConvLibc;
e5ea3f7a
RR
102#endif
103
457814b5 104 m_canvasTextColour = wxColour("BLACK");
66bd6b93 105 m_canvasFont = *wxNORMAL_FONT;
457814b5
JS
106
107 // Create the main frame window
8487f887 108 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
457814b5
JS
109
110 // Make a menubar
111 wxMenu *file_menu = new wxMenu;
112
113 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
114
dfad0599 115#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
116 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
117#endif
118
119 file_menu->AppendSeparator();
120 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
121
dfad0599 122#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
123 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
124
125#endif
126 file_menu->AppendSeparator();
d93c719a 127 file_menu->Append(DIALOGS_LOG_DIALOG, "&Log dialog\tCtrl-L");
c61f4f6d
VZ
128 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box\tCtrl-M");
129 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry\tCtrl-E");
a294c6d5 130 file_menu->Append(DIALOGS_PASSWORD_ENTRY, "&Password entry\tCtrl-P");
c49245f8 131 file_menu->Append(DIALOGS_NUM_ENTRY, "&Numeric entry\tCtrl-N");
4c45f240 132 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice\tCtrl-C");
d6c9c1b7 133 file_menu->Append(DIALOGS_MULTI_CHOICE, "M&ultiple choice\tCtrl-U");
457814b5 134 file_menu->AppendSeparator();
c61f4f6d 135 file_menu->Append(DIALOGS_TIP, "&Tip of the day\tCtrl-T");
c50f1fb9 136 file_menu->AppendSeparator();
c61f4f6d 137 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file\tCtrl-O");
35b45b33 138 file_menu->Append(DIALOGS_FILE_OPEN2, "&Second open file\tCtrl-2");
a294c6d5 139 file_menu->Append(DIALOGS_FILES_OPEN, "Open &files\tCtrl-Q");
4c45f240 140 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file\tCtrl-S");
c61f4f6d 141 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory\tCtrl-D");
51a58d8b 142 file_menu->Append(DIALOGS_GENERIC_DIR_CHOOSE, "&Choose a directory (generic implementation)");
abceee76
VZ
143#if wxUSE_PROGRESSDLG
144 file_menu->Append(DIALOGS_PROGRESS, "Pro&gress dialog\tCtrl-G");
145#endif // wxUSE_PROGRESSDLG
f6bcfd97
BP
146 file_menu->AppendSeparator();
147 file_menu->Append(DIALOGS_MODAL, "Mo&dal dialog\tCtrl-F");
4c45f240
VZ
148 file_menu->Append(DIALOGS_MODELESS, "Modeless &dialog\tCtrl-Z", "", TRUE);
149 file_menu->AppendSeparator();
c61f4f6d 150 file_menu->Append(wxID_EXIT, "E&xit\tAlt-X");
457814b5
JS
151 wxMenuBar *menu_bar = new wxMenuBar;
152 menu_bar->Append(file_menu, "&File");
153 frame->SetMenuBar(menu_bar);
154
155 myCanvas = new MyCanvas(frame);
156 myCanvas->SetBackgroundColour(*wxWHITE);
157
158 frame->Centre(wxBOTH);
d93c719a 159
457814b5
JS
160 // Show the frame
161 frame->Show(TRUE);
162
163 SetTopWindow(frame);
164
165 return TRUE;
166}
167
168// My frame constructor
4c45f240
VZ
169MyFrame::MyFrame(wxWindow *parent,
170 const wxString& title,
171 const wxPoint& pos,
172 const wxSize& size)
173 : wxFrame(parent, -1, title, pos, size)
174{
175 m_dialog = (MyModelessDialog *)NULL;
176}
457814b5 177
d355d3fe 178void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5 179{
f6bcfd97
BP
180 wxColourData data;
181 data.SetChooseFull(TRUE);
182 for (int i = 0; i < 16; i++)
183 {
457814b5
JS
184 wxColour colour(i*16, i*16, i*16);
185 data.SetCustomColour(i, colour);
f6bcfd97 186 }
d93c719a 187
f6bcfd97
BP
188 wxColourDialog *dialog = new wxColourDialog(this, &data);
189 dialog->SetTitle("Choose the background colour");
190 if (dialog->ShowModal() == wxID_OK)
191 {
457814b5
JS
192 wxColourData retData = dialog->GetColourData();
193 wxColour col = retData.GetColour();
457814b5
JS
194 myCanvas->SetBackgroundColour(col);
195 myCanvas->Clear();
196 myCanvas->Refresh();
f6bcfd97
BP
197 }
198 dialog->Destroy();
457814b5
JS
199}
200
d355d3fe 201void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
202{
203 wxFontData data;
204 data.SetInitialFont(wxGetApp().m_canvasFont);
205 data.SetColour(wxGetApp().m_canvasTextColour);
d93c719a 206
457814b5
JS
207 wxFontDialog *dialog = new wxFontDialog(this, &data);
208 if (dialog->ShowModal() == wxID_OK)
209 {
210 wxFontData retData = dialog->GetFontData();
211 wxGetApp().m_canvasFont = retData.GetChosenFont();
212 wxGetApp().m_canvasTextColour = retData.GetColour();
213 myCanvas->Refresh();
214 }
5919d76b 215 dialog->Destroy();
457814b5
JS
216}
217
dfad0599 218#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
d355d3fe 219void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
220{
221 wxColourData data;
222 data.SetChooseFull(TRUE);
223 for (int i = 0; i < 16; i++)
224 {
225 wxColour colour(i*16, i*16, i*16);
226 data.SetCustomColour(i, colour);
227 }
d93c719a 228
457814b5
JS
229 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
230 if (dialog->ShowModal() == wxID_OK)
231 {
232 wxColourData retData = dialog->GetColourData();
233 wxColour col = retData.GetColour();
234// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
235 myCanvas->SetBackgroundColour(col);
236 myCanvas->Clear();
237 myCanvas->Refresh();
238 }
5919d76b 239 dialog->Destroy();
457814b5
JS
240}
241
d355d3fe 242void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
243{
244 wxFontData data;
245 data.SetInitialFont(wxGetApp().m_canvasFont);
246 data.SetColour(wxGetApp().m_canvasTextColour);
247
248 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
249 if (dialog->ShowModal() == wxID_OK)
250 {
251 wxFontData retData = dialog->GetFontData();
252 wxGetApp().m_canvasFont = retData.GetChosenFont();
253 wxGetApp().m_canvasTextColour = retData.GetColour();
254 myCanvas->Refresh();
255 }
5919d76b 256 dialog->Destroy();
457814b5 257}
d93c719a
VZ
258#endif // wxTEST_GENERIC_DIALOGS_IN_MSW
259
260void MyFrame::LogDialog(wxCommandEvent& event)
261{
2c8e4738
VZ
262 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
263 // being flushed -- test it
264 {
265 wxBusyCursor bc;
266 wxLogMessage("This is some message - everything is ok so far.");
267 wxLogMessage("Another message...\n... this one is on multiple lines");
268 wxLogWarning("And then something went wrong!");
9fe52545
VZ
269
270 // and if ~wxBusyCursor doesn't do it, then call it manually
271 wxYield();
2c8e4738
VZ
272 }
273
d93c719a 274 wxLogError("Intermediary error handler decided to abort.");
5888ef1e
VZ
275 wxLogError("The top level caller detected an unrecoverable error.");
276
7923c649
VZ
277 wxLog::FlushActive();
278
c86423f9 279 wxLogMessage("And this is the same dialog but with only one message.");
d93c719a 280}
457814b5 281
d355d3fe 282void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5 283{
f6bcfd97 284 wxMessageDialog dialog( NULL, "This is a message box\nA long, long string to test out the message box properly",
c50f1fb9 285 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
457814b5
JS
286
287 dialog.ShowModal();
288}
289
c49245f8
VZ
290void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
291{
e65cc56a
RR
292 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
293 "Even two rows of text.",
d93c719a 294 "Enter a number:", "Numeric input test",
e65cc56a 295 50, 0, 100, this );
c49245f8
VZ
296
297 wxString msg;
298 int icon;
299 if ( res == -1 )
300 {
301 msg = "Invalid number entered or dialog cancelled.";
302 icon = wxICON_HAND;
303 }
304 else
d93c719a
VZ
305 {
306 msg.Printf(_T("You've entered %lu"), res );
c49245f8
VZ
307 icon = wxICON_INFORMATION;
308 }
309
310 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
311}
312
a294c6d5 313void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
457814b5 314{
a294c6d5 315 wxString pwd = wxGetPasswordFromUser("Enter password:",
3ca6a5f0 316 "Password entry dialog",
a294c6d5
VZ
317 "",
318 this);
319 if ( !!pwd )
320 {
321 wxMessageBox(wxString::Format("Your password is '%s'", pwd.c_str()),
322 "Got password", wxOK | wxICON_INFORMATION, this);
323 }
324}
325
326void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
327{
328 wxTextEntryDialog dialog(this,
329 "This is a small sample\n"
330 "A long, long string to test out the text entrybox",
331 "Please enter a string",
332 "Default value",
333 wxOK | wxCANCEL);
457814b5
JS
334
335 if (dialog.ShowModal() == wxID_OK)
336 {
c50f1fb9
VZ
337 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
338 dialog2.ShowModal();
457814b5
JS
339 }
340}
341
d355d3fe 342void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 343{
ef77f91e
JS
344 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
345 int n = 5;
457814b5 346
ef77f91e
JS
347 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
348 "Please select a value", n, (const wxString *)choices);
457814b5 349
ef77f91e
JS
350 dialog.SetSelection(2);
351
352 if (dialog.ShowModal() == wxID_OK)
353 {
354 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
355 dialog2.ShowModal();
356 }
457814b5
JS
357}
358
d6c9c1b7
VZ
359void MyFrame::MultiChoice(wxCommandEvent& WXUNUSED(event) )
360{
361 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
362 int n = 5;
363
364 wxArrayInt selections;
365 size_t count = wxGetMultipleChoices(selections,
366 "This is a small sample\n"
367 "A multi-choice convenience dialog",
368 "Please select a value",
369 n, (const wxString *)choices,
370 this);
371 if ( count )
372 {
3d49ce44
VZ
373 wxString msg;
374 msg.Printf("You selected %u items:\n", count);
d6c9c1b7
VZ
375 for ( size_t n = 0; n < count; n++ )
376 {
3d49ce44
VZ
377 msg += wxString::Format("\t%u: %u (%s)\n", n, selections[n],
378 choices[selections[n]].c_str());
d6c9c1b7 379 }
3d49ce44 380 wxLogMessage(msg);
d6c9c1b7
VZ
381 }
382 //else: cancelled or nothing selected
383}
384
d355d3fe 385void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5 386{
c50f1fb9 387 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
457814b5 388
c50f1fb9
VZ
389 if (dialog.ShowModal() == wxID_OK)
390 {
5919d76b 391 wxString info;
13393ab6
RR
392 info.Printf(_T("Full file name: %s\n")
393 _T("Path: %s\n")
394 _T("Name: %s"),
5919d76b
VZ
395 dialog.GetPath().c_str(),
396 dialog.GetDirectory().c_str(),
397 dialog.GetFilename().c_str());
c50f1fb9
VZ
398 wxMessageDialog dialog2(this, info, "Selected file");
399 dialog2.ShowModal();
400 }
457814b5
JS
401}
402
35b45b33
VZ
403// this shows how to take advantage of specifying a default extension in the
404// call to wxFileSelector: it is remembered after each new call and the next
405// one will use it by default
406void MyFrame::FileOpen2(wxCommandEvent& WXUNUSED(event) )
407{
408 static wxString s_extDef;
409 wxString path = wxFileSelector(
410 _T("Select the file to load"),
411 _T(""), _T(""),
412 s_extDef,
413 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
414 0,
415 this
416 );
417
418 if ( !path )
419 return;
420
421 // it is just a sample, would use wxSplitPath in real program
422 s_extDef = path.AfterLast(_T('.'));
423
424 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
37d403aa 425 (const wxChar*) path, (const wxChar*) s_extDef);
35b45b33
VZ
426}
427
c61f4f6d
VZ
428void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
429{
430 wxFileDialog dialog(this, "Testing open multiple file dialog",
470caaf9
VZ
431 "", "", wxFileSelectorDefaultWildcardStr,
432 wxMULTIPLE);
c61f4f6d
VZ
433
434 if (dialog.ShowModal() == wxID_OK)
435 {
436 wxArrayString paths, filenames;
437
438 dialog.GetPaths(paths);
439 dialog.GetFilenames(filenames);
440
441 wxString msg, s;
442 size_t count = paths.GetCount();
443 for ( size_t n = 0; n < count; n++ )
444 {
445 s.Printf(_T("File %d: %s (%s)\n"),
446 n, paths[n].c_str(), filenames[n].c_str());
447
448 msg += s;
449 }
450
451 wxMessageDialog dialog2(this, msg, "Selected files");
452 dialog2.ShowModal();
453 }
454}
455
d355d3fe 456void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5 457{
ed58dbea 458 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
c50f1fb9
VZ
459 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
460 wxSAVE|wxOVERWRITE_PROMPT);
461
462 if (dialog.ShowModal() == wxID_OK)
463 {
464 wxChar buf[400];
465 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
466 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
467 dialog2.ShowModal();
468 }
457814b5
JS
469}
470
d355d3fe 471void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 472{
3f2711d5
VZ
473 // pass some initial dir to wxDirDialog
474 wxString dirHome;
475 wxGetHomeDir(&dirHome);
476
477 wxDirDialog dialog(this, "Testing directory picker", dirHome);
c50f1fb9
VZ
478
479 if (dialog.ShowModal() == wxID_OK)
480 {
481 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
482 dialog2.ShowModal();
483 }
484}
485
51a58d8b
JS
486void MyFrame::GenericDirChoose(wxCommandEvent& WXUNUSED(event) )
487{
4ad3c82f 488#if !defined(__WXMSW__) || defined(wxUSE_DIRDLGG) && wxUSE_DIRDLGG
51a58d8b
JS
489 // pass some initial dir to wxDirDialog
490 wxString dirHome;
491 wxGetHomeDir(&dirHome);
492
493 wxGenericDirDialog dialog(this, "Testing generic directory picker", dirHome);
494
495 if (dialog.ShowModal() == wxID_OK)
496 {
497 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
498 dialog2.ShowModal();
499 }
4ad3c82f
VZ
500#else
501 wxLogError("Sorry, generic dir dialog not available:\n"
502 "set wxUSE_DIRDLGG to 1 and recompile");
503#endif
51a58d8b
JS
504}
505
f6bcfd97
BP
506void MyFrame::ModalDlg(wxCommandEvent& WXUNUSED(event))
507{
508 MyModalDialog dlg(this);
509 dlg.ShowModal();
510}
511
4c45f240
VZ
512void MyFrame::ModelessDlg(wxCommandEvent& event)
513{
2f82899b 514 bool show = GetMenuBar()->IsChecked(event.GetId());
4c45f240
VZ
515
516 if ( show )
517 {
518 if ( !m_dialog )
519 {
520 m_dialog = new MyModelessDialog(this);
521 }
522
523 m_dialog->Show(TRUE);
524 }
525 else // hide
526 {
527 m_dialog->Hide();
528 }
529}
530
531void MyFrame::OnButton(wxCommandEvent& WXUNUSED(event))
532{
533 wxMessageBox("Button pressed in modeless dialog", "Info",
534 wxOK | wxICON_INFORMATION, this);
535}
536
c50f1fb9
VZ
537void MyFrame::ShowTip(wxCommandEvent& event)
538{
78bd7ed3 539#if wxUSE_STARTUP_TIPS
c50f1fb9
VZ
540 static size_t s_index = (size_t)-1;
541
542 if ( s_index == (size_t)-1 )
543 {
544 srand(time(NULL));
545
546 // this is completely bogus, we don't know how many lines are there
547 // in the file, but who cares, it's a demo only...
548 s_index = rand() % 5;
549 }
550
551 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
552
553 bool showAtStartup = wxShowTip(this, tipProvider);
554
555 if ( showAtStartup )
556 {
557 wxMessageBox("Will show tips on startup", "Tips dialog",
558 wxOK | wxICON_INFORMATION, this);
559 }
457814b5 560
c50f1fb9
VZ
561 s_index = tipProvider->GetCurrentTip();
562 delete tipProvider;
78bd7ed3 563#endif
457814b5
JS
564}
565
d355d3fe 566void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5 567{
c50f1fb9 568 Close(TRUE);
457814b5
JS
569}
570
abceee76
VZ
571#if wxUSE_PROGRESSDLG
572
573void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
574{
575 static const int max = 10;
576
577 wxProgressDialog dialog("Progress dialog example",
578 "An informative message",
579 max, // range
580 this, // parent
581 wxPD_CAN_ABORT |
582 wxPD_APP_MODAL |
583 wxPD_ELAPSED_TIME |
584 wxPD_ESTIMATED_TIME |
585 wxPD_REMAINING_TIME);
586
587 bool cont = TRUE;
588 for ( int i = 0; i <= max && cont; i++ )
589 {
590 wxSleep(1);
591 if ( i == max )
592 {
593 cont = dialog.Update(i, "That's all, folks!");
594 }
595 else if ( i == max / 2 )
596 {
597 cont = dialog.Update(i, "Only a half left (very long message)!");
598 }
599 else
600 {
601 cont = dialog.Update(i);
602 }
603 }
604
605 if ( !cont )
606 {
607 wxLogStatus("Progress dialog aborted!");
608 }
609 else
610 {
611 wxLogStatus("Countdown from %d finished", max);
612 }
613}
614
615#endif // wxUSE_PROGRESSDLG
616
617// ----------------------------------------------------------------------------
618// MyCanvas
619// ----------------------------------------------------------------------------
620
d355d3fe 621void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5 622{
c50f1fb9 623 wxPaintDC dc(this);
457814b5
JS
624 dc.SetFont(wxGetApp().m_canvasFont);
625 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
626 dc.SetBackgroundMode(wxTRANSPARENT);
627 dc.DrawText("wxWindows common dialogs test application", 10, 10);
628}
629
4c45f240
VZ
630// ----------------------------------------------------------------------------
631// MyModelessDialog
632// ----------------------------------------------------------------------------
457814b5 633
4c45f240
VZ
634MyModelessDialog::MyModelessDialog(wxWindow *parent)
635 : wxDialog(parent, -1, wxString("Modeless dialog"))
636{
cbc66a27
VZ
637 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
638
639 wxButton *btn = new wxButton(this, DIALOGS_MODELESS_BTN, "Press me");
640 wxCheckBox *check = new wxCheckBox(this, -1, "Should be disabled");
641 check->Disable();
642
643 sizerTop->Add(btn, 1, wxEXPAND | wxALL, 5);
644 sizerTop->Add(check, 1, wxEXPAND | wxALL, 5);
645
646 SetAutoLayout(TRUE);
647 SetSizer(sizerTop);
648
649 sizerTop->SetSizeHints(this);
650 sizerTop->Fit(this);
4c45f240 651}
abceee76
VZ
652
653void MyModelessDialog::OnClose(wxCloseEvent& event)
654{
655 if ( event.CanVeto() )
656 {
657 wxMessageBox("Use the menu item to close this dialog",
658 "Modeless dialog",
659 wxOK | wxICON_INFORMATION, this);
660
661 event.Veto();
662 }
663}
664
f6bcfd97
BP
665// ----------------------------------------------------------------------------
666// MyModalDialog
667// ----------------------------------------------------------------------------
668
669MyModalDialog::MyModalDialog(wxWindow *parent)
670 : wxDialog(parent, -1, wxString("Modal dialog"))
671{
672 wxBoxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
673
674 m_btnFocused = new wxButton(this, -1, "Default button");
675 m_btnDelete = new wxButton(this, -1, "&Delete button");
676 sizerTop->Add(m_btnFocused, 0, wxALIGN_CENTER | wxALL, 5);
677 sizerTop->Add(m_btnDelete, 0, wxALIGN_CENTER | wxALL, 5);
678
679 SetAutoLayout(TRUE);
680 SetSizer(sizerTop);
681
682 sizerTop->SetSizeHints(this);
683 sizerTop->Fit(this);
684
685 m_btnFocused->SetFocus();
686 m_btnFocused->SetDefault();
687}
688
689void MyModalDialog::OnButton(wxCommandEvent& event)
690{
691 if ( event.GetEventObject() == m_btnDelete )
692 {
693 delete m_btnFocused;
694 m_btnFocused = NULL;
695
696 m_btnDelete->Disable();
697 }
698 else if ( event.GetEventObject() == m_btnFocused )
699 {
700 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",
701 "", this);
702 }
703 else
704 {
705 event.Skip();
706 }
707}