]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
1. corrected problem with label updating in wxProgressDialog
[wxWidgets.git] / samples / dialogs / dialogs.cpp
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
36 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
37
38 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
39 #include <wx/generic/colrdlgg.h>
40 #include <wx/generic/fontdlgg.h>
41 #endif
42
43 #include "dialogs.h"
44
45 IMPLEMENT_APP(MyApp)
46
47 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
48 EVT_PAINT(MyCanvas::OnPaint)
49 END_EVENT_TABLE()
50
51 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
52 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
53 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
54 EVT_MENU(DIALOGS_LOG_DIALOG, MyFrame::LogDialog)
55 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
56 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
57 EVT_MENU(DIALOGS_PASSWORD_ENTRY, MyFrame::PasswordEntry)
58 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
59 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
60 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
61 EVT_MENU(DIALOGS_FILES_OPEN, MyFrame::FilesOpen)
62 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
63 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
64 EVT_MENU(DIALOGS_MODELESS, MyFrame::ModelessDlg)
65 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
66 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
67 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
68 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
69 #endif
70 #if wxUSE_PROGRESSDLG
71 EVT_MENU(DIALOGS_PROGRESS, MyFrame::ShowProgress)
72 #endif
73 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
74
75 EVT_BUTTON(DIALOGS_MODELESS_BTN, MyFrame::OnButton)
76 END_EVENT_TABLE()
77
78 BEGIN_EVENT_TABLE(MyModelessDialog, wxDialog)
79 EVT_CLOSE(MyModelessDialog::OnClose)
80 END_EVENT_TABLE()
81
82 MyCanvas *myCanvas = (MyCanvas *) NULL;
83
84 // `Main program' equivalent, creating windows and returning main app frame
85 bool MyApp::OnInit()
86 {
87 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
88 wxConvCurrent = &wxConvLibc;
89 #endif
90
91 m_canvasTextColour = wxColour("BLACK");
92 m_canvasFont = *wxNORMAL_FONT;
93
94 // Create the main frame window
95 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
96
97 // Make a menubar
98 wxMenu *file_menu = new wxMenu;
99
100 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
101
102 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
103 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
104 #endif
105
106 file_menu->AppendSeparator();
107 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
108
109 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
110 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
111
112 #endif
113 file_menu->AppendSeparator();
114 file_menu->Append(DIALOGS_LOG_DIALOG, "&Log dialog\tCtrl-L");
115 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box\tCtrl-M");
116 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry\tCtrl-E");
117 file_menu->Append(DIALOGS_PASSWORD_ENTRY, "&Password entry\tCtrl-P");
118 file_menu->Append(DIALOGS_NUM_ENTRY, "&Numeric entry\tCtrl-N");
119 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice\tCtrl-C");
120 file_menu->AppendSeparator();
121 file_menu->Append(DIALOGS_TIP, "&Tip of the day\tCtrl-T");
122 file_menu->AppendSeparator();
123 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file\tCtrl-O");
124 file_menu->Append(DIALOGS_FILES_OPEN, "Open &files\tCtrl-Q");
125 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file\tCtrl-S");
126 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory\tCtrl-D");
127 file_menu->AppendSeparator();
128 #if wxUSE_PROGRESSDLG
129 file_menu->Append(DIALOGS_PROGRESS, "Pro&gress dialog\tCtrl-G");
130 #endif // wxUSE_PROGRESSDLG
131 file_menu->Append(DIALOGS_MODELESS, "Modeless &dialog\tCtrl-Z", "", TRUE);
132 file_menu->AppendSeparator();
133 file_menu->Append(wxID_EXIT, "E&xit\tAlt-X");
134 wxMenuBar *menu_bar = new wxMenuBar;
135 menu_bar->Append(file_menu, "&File");
136 frame->SetMenuBar(menu_bar);
137
138 myCanvas = new MyCanvas(frame);
139 myCanvas->SetBackgroundColour(*wxWHITE);
140
141 frame->Centre(wxBOTH);
142
143 // Show the frame
144 frame->Show(TRUE);
145
146 SetTopWindow(frame);
147
148 return TRUE;
149 }
150
151 // My frame constructor
152 MyFrame::MyFrame(wxWindow *parent,
153 const wxString& title,
154 const wxPoint& pos,
155 const wxSize& size)
156 : wxFrame(parent, -1, title, pos, size)
157 {
158 m_dialog = (MyModelessDialog *)NULL;
159 }
160
161 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
162 {
163 wxColourData data;
164 data.SetChooseFull(TRUE);
165 for (int i = 0; i < 16; i++)
166 {
167 wxColour colour(i*16, i*16, i*16);
168 data.SetCustomColour(i, colour);
169 }
170
171 wxColourDialog *dialog = new wxColourDialog(this, &data);
172 if (dialog->ShowModal() == wxID_OK)
173 {
174 wxColourData retData = dialog->GetColourData();
175 wxColour col = retData.GetColour();
176 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
177 myCanvas->SetBackgroundColour(col);
178 myCanvas->Clear();
179 myCanvas->Refresh();
180 }
181 dialog->Destroy();
182 }
183
184 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
185 {
186 wxFontData data;
187 data.SetInitialFont(wxGetApp().m_canvasFont);
188 data.SetColour(wxGetApp().m_canvasTextColour);
189
190 wxFontDialog *dialog = new wxFontDialog(this, &data);
191 if (dialog->ShowModal() == wxID_OK)
192 {
193 wxFontData retData = dialog->GetFontData();
194 wxGetApp().m_canvasFont = retData.GetChosenFont();
195 wxGetApp().m_canvasTextColour = retData.GetColour();
196 myCanvas->Refresh();
197 }
198 dialog->Destroy();
199 }
200
201 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
202 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
203 {
204 wxColourData data;
205 data.SetChooseFull(TRUE);
206 for (int i = 0; i < 16; i++)
207 {
208 wxColour colour(i*16, i*16, i*16);
209 data.SetCustomColour(i, colour);
210 }
211
212 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
213 if (dialog->ShowModal() == wxID_OK)
214 {
215 wxColourData retData = dialog->GetColourData();
216 wxColour col = retData.GetColour();
217 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
218 myCanvas->SetBackgroundColour(col);
219 myCanvas->Clear();
220 myCanvas->Refresh();
221 }
222 dialog->Destroy();
223 }
224
225 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
226 {
227 wxFontData data;
228 data.SetInitialFont(wxGetApp().m_canvasFont);
229 data.SetColour(wxGetApp().m_canvasTextColour);
230
231 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
232 if (dialog->ShowModal() == wxID_OK)
233 {
234 wxFontData retData = dialog->GetFontData();
235 wxGetApp().m_canvasFont = retData.GetChosenFont();
236 wxGetApp().m_canvasTextColour = retData.GetColour();
237 myCanvas->Refresh();
238 }
239 dialog->Destroy();
240 }
241 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
242
243 void MyFrame::LogDialog(wxCommandEvent& event)
244 {
245 wxLogMessage("This is some message - everything is ok so far.");
246 wxLogMessage("Another message...\n... this one is on multiple lines");
247 wxLogWarning("And then something went wrong!");
248 wxLogError("Intermediary error handler decided to abort.");
249 wxLogError("The top level caller detected an unrecoverable error.");
250
251 wxLog::FlushActive();
252
253 wxLogMessage("And this is the same dialog but with only one message.");
254 }
255
256 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
257 {
258 wxMessageDialog dialog( this, "This is a message box\nA long, long string to test out the message box properly",
259 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
260
261 dialog.ShowModal();
262 }
263
264 void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
265 {
266 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
267 "Even two rows of text.",
268 "Enter a number:", "Numeric input test",
269 50, 0, 100, this );
270
271 wxString msg;
272 int icon;
273 if ( res == -1 )
274 {
275 msg = "Invalid number entered or dialog cancelled.";
276 icon = wxICON_HAND;
277 }
278 else
279 {
280 msg.Printf(_T("You've entered %lu"), res );
281 icon = wxICON_INFORMATION;
282 }
283
284 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
285 }
286
287 void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
288 {
289 wxString pwd = wxGetPasswordFromUser("Enter password:",
290 "Passowrd entry dialog",
291 "",
292 this);
293 if ( !!pwd )
294 {
295 wxMessageBox(wxString::Format("Your password is '%s'", pwd.c_str()),
296 "Got password", wxOK | wxICON_INFORMATION, this);
297 }
298 }
299
300 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
301 {
302 wxTextEntryDialog dialog(this,
303 "This is a small sample\n"
304 "A long, long string to test out the text entrybox",
305 "Please enter a string",
306 "Default value",
307 wxOK | wxCANCEL);
308
309 if (dialog.ShowModal() == wxID_OK)
310 {
311 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
312 dialog2.ShowModal();
313 }
314 }
315
316 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
317 {
318 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
319 int n = 5;
320
321 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
322 "Please select a value", n, (const wxString *)choices);
323
324 dialog.SetSelection(2);
325
326 if (dialog.ShowModal() == wxID_OK)
327 {
328 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
329 dialog2.ShowModal();
330 }
331 }
332
333 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
334 {
335 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
336
337 if (dialog.ShowModal() == wxID_OK)
338 {
339 wxString info;
340 info.Printf(_T("Full file name: %s\n")
341 _T("Path: %s\n")
342 _T("Name: %s"),
343 dialog.GetPath().c_str(),
344 dialog.GetDirectory().c_str(),
345 dialog.GetFilename().c_str());
346 wxMessageDialog dialog2(this, info, "Selected file");
347 dialog2.ShowModal();
348 }
349 }
350
351 void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
352 {
353 wxFileDialog dialog(this, "Testing open multiple file dialog",
354 "", "", wxFileSelectorDefaultWildcardStr,
355 wxMULTIPLE);
356
357 if (dialog.ShowModal() == wxID_OK)
358 {
359 wxArrayString paths, filenames;
360
361 dialog.GetPaths(paths);
362 dialog.GetFilenames(filenames);
363
364 wxString msg, s;
365 size_t count = paths.GetCount();
366 for ( size_t n = 0; n < count; n++ )
367 {
368 s.Printf(_T("File %d: %s (%s)\n"),
369 n, paths[n].c_str(), filenames[n].c_str());
370
371 msg += s;
372 }
373
374 wxMessageDialog dialog2(this, msg, "Selected files");
375 dialog2.ShowModal();
376 }
377 }
378
379 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
380 {
381 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
382 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
383 wxSAVE|wxOVERWRITE_PROMPT);
384
385 if (dialog.ShowModal() == wxID_OK)
386 {
387 wxChar buf[400];
388 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
389 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
390 dialog2.ShowModal();
391 }
392 }
393
394 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
395 {
396 // pass some initial dir to wxDirDialog
397 wxString dirHome;
398 wxGetHomeDir(&dirHome);
399
400 wxDirDialog dialog(this, "Testing directory picker", dirHome);
401
402 if (dialog.ShowModal() == wxID_OK)
403 {
404 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
405 dialog2.ShowModal();
406 }
407 }
408
409 void MyFrame::ModelessDlg(wxCommandEvent& event)
410 {
411 bool show = GetMenuBar()->IsChecked(event.GetInt());
412
413 if ( show )
414 {
415 if ( !m_dialog )
416 {
417 m_dialog = new MyModelessDialog(this);
418 }
419
420 m_dialog->Show(TRUE);
421 }
422 else // hide
423 {
424 m_dialog->Hide();
425 }
426 }
427
428 void MyFrame::OnButton(wxCommandEvent& WXUNUSED(event))
429 {
430 wxMessageBox("Button pressed in modeless dialog", "Info",
431 wxOK | wxICON_INFORMATION, this);
432 }
433
434 void MyFrame::ShowTip(wxCommandEvent& event)
435 {
436 #if wxUSE_STARTUP_TIPS
437 static size_t s_index = (size_t)-1;
438
439 if ( s_index == (size_t)-1 )
440 {
441 srand(time(NULL));
442
443 // this is completely bogus, we don't know how many lines are there
444 // in the file, but who cares, it's a demo only...
445 s_index = rand() % 5;
446 }
447
448 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
449
450 bool showAtStartup = wxShowTip(this, tipProvider);
451
452 if ( showAtStartup )
453 {
454 wxMessageBox("Will show tips on startup", "Tips dialog",
455 wxOK | wxICON_INFORMATION, this);
456 }
457
458 s_index = tipProvider->GetCurrentTip();
459 delete tipProvider;
460 #endif
461 }
462
463 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
464 {
465 Close(TRUE);
466 }
467
468 #if wxUSE_PROGRESSDLG
469
470 void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
471 {
472 static const int max = 10;
473
474 wxProgressDialog dialog("Progress dialog example",
475 "An informative message",
476 max, // range
477 this, // parent
478 wxPD_CAN_ABORT |
479 wxPD_APP_MODAL |
480 wxPD_ELAPSED_TIME |
481 wxPD_ESTIMATED_TIME |
482 wxPD_REMAINING_TIME);
483
484 bool cont = TRUE;
485 for ( int i = 0; i <= max && cont; i++ )
486 {
487 wxSleep(1);
488 if ( i == max )
489 {
490 cont = dialog.Update(i, "That's all, folks!");
491 }
492 else if ( i == max / 2 )
493 {
494 cont = dialog.Update(i, "Only a half left (very long message)!");
495 }
496 else
497 {
498 cont = dialog.Update(i);
499 }
500 }
501
502 if ( !cont )
503 {
504 wxLogStatus("Progress dialog aborted!");
505 }
506 else
507 {
508 wxLogStatus("Countdown from %d finished", max);
509 }
510 }
511
512 #endif // wxUSE_PROGRESSDLG
513
514 // ----------------------------------------------------------------------------
515 // MyCanvas
516 // ----------------------------------------------------------------------------
517
518 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
519 {
520 wxPaintDC dc(this);
521 dc.SetFont(wxGetApp().m_canvasFont);
522 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
523 dc.SetBackgroundMode(wxTRANSPARENT);
524 dc.DrawText("wxWindows common dialogs test application", 10, 10);
525 }
526
527 // ----------------------------------------------------------------------------
528 // MyModelessDialog
529 // ----------------------------------------------------------------------------
530
531 MyModelessDialog::MyModelessDialog(wxWindow *parent)
532 : wxDialog(parent, -1, wxString("Modeless dialog"))
533 {
534 (void)new wxButton(this, DIALOGS_MODELESS_BTN, "Press me");
535 Fit();
536 Centre();
537 }
538
539 void MyModelessDialog::OnClose(wxCloseEvent& event)
540 {
541 if ( event.CanVeto() )
542 {
543 wxMessageBox("Use the menu item to close this dialog",
544 "Modeless dialog",
545 wxOK | wxICON_INFORMATION, this);
546
547 event.Veto();
548 }
549 }
550