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