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