1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Common dialogs demo
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
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"
34 #include "wx/progdlg.h"
36 // New wxGenericDirCtrl
37 #include "wx/dirctrl.h"
39 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
41 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
42 #include <wx/generic/colrdlgg.h>
43 #include <wx/generic/fontdlgg.h>
50 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
51 EVT_PAINT(MyCanvas::OnPaint
)
54 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
55 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
56 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
57 EVT_MENU(DIALOGS_LOG_DIALOG
, MyFrame::LogDialog
)
58 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
59 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
60 EVT_MENU(DIALOGS_PASSWORD_ENTRY
, MyFrame::PasswordEntry
)
61 EVT_MENU(DIALOGS_NUM_ENTRY
, MyFrame::NumericEntry
)
62 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
63 EVT_MENU(DIALOGS_MULTI_CHOICE
, MyFrame::MultiChoice
)
64 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
65 EVT_MENU(DIALOGS_FILE_OPEN2
, MyFrame::FileOpen2
)
66 EVT_MENU(DIALOGS_FILES_OPEN
, MyFrame::FilesOpen
)
67 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
68 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
69 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE
, MyFrame::GenericDirChoose
)
70 EVT_MENU(DIALOGS_MODAL
, MyFrame::ModalDlg
)
71 EVT_MENU(DIALOGS_MODELESS
, MyFrame::ModelessDlg
)
72 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
73 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
74 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
75 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
78 EVT_MENU(DIALOGS_PROGRESS
, MyFrame::ShowProgress
)
80 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
82 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
85 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
86 EVT_BUTTON(-1, MyModalDialog::OnButton
)
89 BEGIN_EVENT_TABLE(MyModelessDialog
, wxDialog
)
90 EVT_CLOSE(MyModelessDialog::OnClose
)
93 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
95 // `Main program' equivalent, creating windows and returning main app frame
98 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
99 wxConvCurrent
= &wxConvLibc
;
102 m_canvasTextColour
= wxColour("BLACK");
103 m_canvasFont
= *wxNORMAL_FONT
;
105 // Create the main frame window
106 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
109 wxMenu
*file_menu
= new wxMenu
;
111 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
113 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
114 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
117 file_menu
->AppendSeparator();
118 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
120 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
121 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
124 file_menu
->AppendSeparator();
125 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
126 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
127 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
128 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
129 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
130 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
131 file_menu
->Append(DIALOGS_MULTI_CHOICE
, "M&ultiple choice\tCtrl-U");
132 file_menu
->AppendSeparator();
133 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
134 file_menu
->AppendSeparator();
135 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
136 file_menu
->Append(DIALOGS_FILE_OPEN2
, "&Second open file\tCtrl-2");
137 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
138 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
139 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
140 file_menu
->Append(DIALOGS_GENERIC_DIR_CHOOSE
, "&Choose a directory (generic implementation)");
141 #if wxUSE_PROGRESSDLG
142 file_menu
->Append(DIALOGS_PROGRESS
, "Pro&gress dialog\tCtrl-G");
143 #endif // wxUSE_PROGRESSDLG
144 file_menu
->AppendSeparator();
145 file_menu
->Append(DIALOGS_MODAL
, "Mo&dal dialog\tCtrl-F");
146 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
147 file_menu
->AppendSeparator();
148 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
149 wxMenuBar
*menu_bar
= new wxMenuBar
;
150 menu_bar
->Append(file_menu
, "&File");
151 frame
->SetMenuBar(menu_bar
);
153 myCanvas
= new MyCanvas(frame
);
154 myCanvas
->SetBackgroundColour(*wxWHITE
);
156 frame
->Centre(wxBOTH
);
166 // My frame constructor
167 MyFrame::MyFrame(wxWindow
*parent
,
168 const wxString
& title
,
171 : wxFrame(parent
, -1, title
, pos
, size
)
173 m_dialog
= (MyModelessDialog
*)NULL
;
176 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
179 data
.SetChooseFull(TRUE
);
180 for (int i
= 0; i
< 16; i
++)
182 wxColour
colour(i
*16, i
*16, i
*16);
183 data
.SetCustomColour(i
, colour
);
186 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
187 dialog
->SetTitle("Choose the background colour");
188 if (dialog
->ShowModal() == wxID_OK
)
190 wxColourData retData
= dialog
->GetColourData();
191 wxColour col
= retData
.GetColour();
192 myCanvas
->SetBackgroundColour(col
);
199 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
202 data
.SetInitialFont(wxGetApp().m_canvasFont
);
203 data
.SetColour(wxGetApp().m_canvasTextColour
);
205 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
206 if (dialog
->ShowModal() == wxID_OK
)
208 wxFontData retData
= dialog
->GetFontData();
209 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
210 wxGetApp().m_canvasTextColour
= retData
.GetColour();
216 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
217 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
220 data
.SetChooseFull(TRUE
);
221 for (int i
= 0; i
< 16; i
++)
223 wxColour
colour(i
*16, i
*16, i
*16);
224 data
.SetCustomColour(i
, colour
);
227 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
228 if (dialog
->ShowModal() == wxID_OK
)
230 wxColourData retData
= dialog
->GetColourData();
231 wxColour col
= retData
.GetColour();
232 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
233 myCanvas
->SetBackgroundColour(col
);
240 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
243 data
.SetInitialFont(wxGetApp().m_canvasFont
);
244 data
.SetColour(wxGetApp().m_canvasTextColour
);
246 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
247 if (dialog
->ShowModal() == wxID_OK
)
249 wxFontData retData
= dialog
->GetFontData();
250 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
251 wxGetApp().m_canvasTextColour
= retData
.GetColour();
256 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
258 void MyFrame::LogDialog(wxCommandEvent
& event
)
260 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
261 // being flushed -- test it
264 wxLogMessage("This is some message - everything is ok so far.");
265 wxLogMessage("Another message...\n... this one is on multiple lines");
266 wxLogWarning("And then something went wrong!");
269 wxLogError("Intermediary error handler decided to abort.");
270 wxLogError("The top level caller detected an unrecoverable error.");
272 wxLog::FlushActive();
274 wxLogMessage("And this is the same dialog but with only one message.");
277 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
279 wxMessageDialog
dialog( NULL
, "This is a message box\nA long, long string to test out the message box properly",
280 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
285 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
287 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
288 "Even two rows of text.",
289 "Enter a number:", "Numeric input test",
296 msg
= "Invalid number entered or dialog cancelled.";
301 msg
.Printf(_T("You've entered %lu"), res
);
302 icon
= wxICON_INFORMATION
;
305 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
308 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
310 wxString pwd
= wxGetPasswordFromUser("Enter password:",
311 "Password entry dialog",
316 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
317 "Got password", wxOK
| wxICON_INFORMATION
, this);
321 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
323 wxTextEntryDialog
dialog(this,
324 "This is a small sample\n"
325 "A long, long string to test out the text entrybox",
326 "Please enter a string",
330 if (dialog
.ShowModal() == wxID_OK
)
332 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
337 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
339 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
342 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
343 "Please select a value", n
, (const wxString
*)choices
);
345 dialog
.SetSelection(2);
347 if (dialog
.ShowModal() == wxID_OK
)
349 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
354 void MyFrame::MultiChoice(wxCommandEvent
& WXUNUSED(event
) )
356 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
359 wxArrayInt selections
;
360 size_t count
= wxGetMultipleChoices(selections
,
361 "This is a small sample\n"
362 "A multi-choice convenience dialog",
363 "Please select a value",
364 n
, (const wxString
*)choices
,
368 wxLogMessage("You selected %u items:", count
);
369 for ( size_t n
= 0; n
< count
; n
++ )
371 wxLogMessage("\t%u: %u (%s)", n
, selections
[n
],
372 choices
[selections
[n
]].c_str());
375 //else: cancelled or nothing selected
378 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
380 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
382 if (dialog
.ShowModal() == wxID_OK
)
385 info
.Printf(_T("Full file name: %s\n")
388 dialog
.GetPath().c_str(),
389 dialog
.GetDirectory().c_str(),
390 dialog
.GetFilename().c_str());
391 wxMessageDialog
dialog2(this, info
, "Selected file");
396 // this shows how to take advantage of specifying a default extension in the
397 // call to wxFileSelector: it is remembered after each new call and the next
398 // one will use it by default
399 void MyFrame::FileOpen2(wxCommandEvent
& WXUNUSED(event
) )
401 static wxString s_extDef
;
402 wxString path
= wxFileSelector(
403 _T("Select the file to load"),
406 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
414 // it is just a sample, would use wxSplitPath in real program
415 s_extDef
= path
.AfterLast(_T('.'));
417 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
418 (const wxChar
*) path
, (const wxChar
*) s_extDef
);
421 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
423 wxFileDialog
dialog(this, "Testing open multiple file dialog",
424 "", "", wxFileSelectorDefaultWildcardStr
,
427 if (dialog
.ShowModal() == wxID_OK
)
429 wxArrayString paths
, filenames
;
431 dialog
.GetPaths(paths
);
432 dialog
.GetFilenames(filenames
);
435 size_t count
= paths
.GetCount();
436 for ( size_t n
= 0; n
< count
; n
++ )
438 s
.Printf(_T("File %d: %s (%s)\n"),
439 n
, paths
[n
].c_str(), filenames
[n
].c_str());
444 wxMessageDialog
dialog2(this, msg
, "Selected files");
449 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
451 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
452 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
453 wxSAVE
|wxOVERWRITE_PROMPT
);
455 if (dialog
.ShowModal() == wxID_OK
)
458 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
459 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
464 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
466 // pass some initial dir to wxDirDialog
468 wxGetHomeDir(&dirHome
);
470 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
472 if (dialog
.ShowModal() == wxID_OK
)
474 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
479 void MyFrame::GenericDirChoose(wxCommandEvent
& WXUNUSED(event
) )
481 // pass some initial dir to wxDirDialog
483 wxGetHomeDir(&dirHome
);
485 wxGenericDirDialog
dialog(this, "Testing generic directory picker", dirHome
);
487 if (dialog
.ShowModal() == wxID_OK
)
489 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
494 void MyFrame::ModalDlg(wxCommandEvent
& WXUNUSED(event
))
496 MyModalDialog
dlg(this);
500 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
502 bool show
= GetMenuBar()->IsChecked(event
.GetId());
508 m_dialog
= new MyModelessDialog(this);
511 m_dialog
->Show(TRUE
);
519 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
521 wxMessageBox("Button pressed in modeless dialog", "Info",
522 wxOK
| wxICON_INFORMATION
, this);
525 void MyFrame::ShowTip(wxCommandEvent
& event
)
527 #if wxUSE_STARTUP_TIPS
528 static size_t s_index
= (size_t)-1;
530 if ( s_index
== (size_t)-1 )
534 // this is completely bogus, we don't know how many lines are there
535 // in the file, but who cares, it's a demo only...
536 s_index
= rand() % 5;
539 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
541 bool showAtStartup
= wxShowTip(this, tipProvider
);
545 wxMessageBox("Will show tips on startup", "Tips dialog",
546 wxOK
| wxICON_INFORMATION
, this);
549 s_index
= tipProvider
->GetCurrentTip();
554 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
559 #if wxUSE_PROGRESSDLG
561 void MyFrame::ShowProgress( wxCommandEvent
& WXUNUSED(event
) )
563 static const int max
= 10;
565 wxProgressDialog
dialog("Progress dialog example",
566 "An informative message",
572 wxPD_ESTIMATED_TIME
|
573 wxPD_REMAINING_TIME
);
576 for ( int i
= 0; i
<= max
&& cont
; i
++ )
581 cont
= dialog
.Update(i
, "That's all, folks!");
583 else if ( i
== max
/ 2 )
585 cont
= dialog
.Update(i
, "Only a half left (very long message)!");
589 cont
= dialog
.Update(i
);
595 wxLogStatus("Progress dialog aborted!");
599 wxLogStatus("Countdown from %d finished", max
);
603 #endif // wxUSE_PROGRESSDLG
605 // ----------------------------------------------------------------------------
607 // ----------------------------------------------------------------------------
609 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
612 dc
.SetFont(wxGetApp().m_canvasFont
);
613 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
614 dc
.SetBackgroundMode(wxTRANSPARENT
);
615 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
618 // ----------------------------------------------------------------------------
620 // ----------------------------------------------------------------------------
622 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
623 : wxDialog(parent
, -1, wxString("Modeless dialog"))
625 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
627 wxButton
*btn
= new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");
628 wxCheckBox
*check
= new wxCheckBox(this, -1, "Should be disabled");
631 sizerTop
->Add(btn
, 1, wxEXPAND
| wxALL
, 5);
632 sizerTop
->Add(check
, 1, wxEXPAND
| wxALL
, 5);
637 sizerTop
->SetSizeHints(this);
641 void MyModelessDialog::OnClose(wxCloseEvent
& event
)
643 if ( event
.CanVeto() )
645 wxMessageBox("Use the menu item to close this dialog",
647 wxOK
| wxICON_INFORMATION
, this);
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
657 MyModalDialog::MyModalDialog(wxWindow
*parent
)
658 : wxDialog(parent
, -1, wxString("Modal dialog"))
660 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
662 m_btnFocused
= new wxButton(this, -1, "Default button");
663 m_btnDelete
= new wxButton(this, -1, "&Delete button");
664 sizerTop
->Add(m_btnFocused
, 0, wxALIGN_CENTER
| wxALL
, 5);
665 sizerTop
->Add(m_btnDelete
, 0, wxALIGN_CENTER
| wxALL
, 5);
670 sizerTop
->SetSizeHints(this);
673 m_btnFocused
->SetFocus();
674 m_btnFocused
->SetDefault();
677 void MyModalDialog::OnButton(wxCommandEvent
& event
)
679 if ( event
.GetEventObject() == m_btnDelete
)
684 m_btnDelete
->Disable();
686 else if ( event
.GetEventObject() == m_btnFocused
)
688 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",