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