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