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