1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Common dialogs demo
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
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"
36 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
38 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
39 #include <wx/generic/colrdlgg.h>
40 #include <wx/generic/fontdlgg.h>
43 #if !defined(__WXMSW__) || defined(wxUSE_DIRDLGG) && wxUSE_DIRDLGG
44 // New wxGenericDirCtrl
45 #include "wx/dirctrl.h"
52 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
53 EVT_PAINT(MyCanvas::OnPaint
)
56 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
57 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
58 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
59 EVT_MENU(DIALOGS_LOG_DIALOG
, MyFrame::LogDialog
)
60 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
61 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
62 EVT_MENU(DIALOGS_PASSWORD_ENTRY
, MyFrame::PasswordEntry
)
63 EVT_MENU(DIALOGS_NUM_ENTRY
, MyFrame::NumericEntry
)
64 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
65 EVT_MENU(DIALOGS_MULTI_CHOICE
, MyFrame::MultiChoice
)
66 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
67 EVT_MENU(DIALOGS_FILE_OPEN2
, MyFrame::FileOpen2
)
68 EVT_MENU(DIALOGS_FILES_OPEN
, MyFrame::FilesOpen
)
69 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
70 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
71 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE
, MyFrame::GenericDirChoose
)
72 EVT_MENU(DIALOGS_MODAL
, MyFrame::ModalDlg
)
73 EVT_MENU(DIALOGS_MODELESS
, MyFrame::ModelessDlg
)
74 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
75 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
76 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
77 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
80 EVT_MENU(DIALOGS_PROGRESS
, MyFrame::ShowProgress
)
82 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
84 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
87 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
88 EVT_BUTTON(-1, MyModalDialog::OnButton
)
91 BEGIN_EVENT_TABLE(MyModelessDialog
, wxDialog
)
92 EVT_CLOSE(MyModelessDialog::OnClose
)
95 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
97 // `Main program' equivalent, creating windows and returning main app frame
100 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
101 wxConvCurrent
= &wxConvLibc
;
104 m_canvasTextColour
= wxColour("BLACK");
105 m_canvasFont
= *wxNORMAL_FONT
;
107 // Create the main frame window
108 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
111 wxMenu
*file_menu
= new wxMenu
;
113 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
115 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
116 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
119 file_menu
->AppendSeparator();
120 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
122 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
123 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
126 file_menu
->AppendSeparator();
127 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
128 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
129 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
130 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
131 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
132 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
133 file_menu
->Append(DIALOGS_MULTI_CHOICE
, "M&ultiple choice\tCtrl-U");
134 file_menu
->AppendSeparator();
135 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
136 file_menu
->AppendSeparator();
137 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
138 file_menu
->Append(DIALOGS_FILE_OPEN2
, "&Second open file\tCtrl-2");
139 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
140 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
141 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
142 file_menu
->Append(DIALOGS_GENERIC_DIR_CHOOSE
, "&Choose a directory (generic implementation)");
143 #if wxUSE_PROGRESSDLG
144 file_menu
->Append(DIALOGS_PROGRESS
, "Pro&gress dialog\tCtrl-G");
145 #endif // wxUSE_PROGRESSDLG
146 file_menu
->AppendSeparator();
147 file_menu
->Append(DIALOGS_MODAL
, "Mo&dal dialog\tCtrl-F");
148 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
149 file_menu
->AppendSeparator();
150 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
151 wxMenuBar
*menu_bar
= new wxMenuBar
;
152 menu_bar
->Append(file_menu
, "&File");
153 frame
->SetMenuBar(menu_bar
);
155 myCanvas
= new MyCanvas(frame
);
156 myCanvas
->SetBackgroundColour(*wxWHITE
);
158 frame
->Centre(wxBOTH
);
168 // My frame constructor
169 MyFrame::MyFrame(wxWindow
*parent
,
170 const wxString
& title
,
173 : wxFrame(parent
, -1, title
, pos
, size
)
175 m_dialog
= (MyModelessDialog
*)NULL
;
178 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
181 data
.SetChooseFull(TRUE
);
182 for (int i
= 0; i
< 16; i
++)
184 wxColour
colour(i
*16, i
*16, i
*16);
185 data
.SetCustomColour(i
, colour
);
188 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
189 dialog
->SetTitle("Choose the background colour");
190 if (dialog
->ShowModal() == wxID_OK
)
192 wxColourData retData
= dialog
->GetColourData();
193 wxColour col
= retData
.GetColour();
194 myCanvas
->SetBackgroundColour(col
);
201 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
204 data
.SetInitialFont(wxGetApp().m_canvasFont
);
205 data
.SetColour(wxGetApp().m_canvasTextColour
);
207 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
208 if (dialog
->ShowModal() == wxID_OK
)
210 wxFontData retData
= dialog
->GetFontData();
211 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
212 wxGetApp().m_canvasTextColour
= retData
.GetColour();
218 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
219 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
222 data
.SetChooseFull(TRUE
);
223 for (int i
= 0; i
< 16; i
++)
225 wxColour
colour(i
*16, i
*16, i
*16);
226 data
.SetCustomColour(i
, colour
);
229 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
230 if (dialog
->ShowModal() == wxID_OK
)
232 wxColourData retData
= dialog
->GetColourData();
233 wxColour col
= retData
.GetColour();
234 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
235 myCanvas
->SetBackgroundColour(col
);
242 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
245 data
.SetInitialFont(wxGetApp().m_canvasFont
);
246 data
.SetColour(wxGetApp().m_canvasTextColour
);
248 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
249 if (dialog
->ShowModal() == wxID_OK
)
251 wxFontData retData
= dialog
->GetFontData();
252 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
253 wxGetApp().m_canvasTextColour
= retData
.GetColour();
258 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
260 void MyFrame::LogDialog(wxCommandEvent
& event
)
262 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
263 // being flushed -- test it
266 wxLogMessage("This is some message - everything is ok so far.");
267 wxLogMessage("Another message...\n... this one is on multiple lines");
268 wxLogWarning("And then something went wrong!");
270 // and if ~wxBusyCursor doesn't do it, then call it manually
274 wxLogError("Intermediary error handler decided to abort.");
275 wxLogError("The top level caller detected an unrecoverable error.");
277 wxLog::FlushActive();
279 wxLogMessage("And this is the same dialog but with only one message.");
282 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
284 wxMessageDialog
dialog( NULL
, "This is a message box\nA long, long string to test out the message box properly",
285 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
290 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
292 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
293 "Even two rows of text.",
294 "Enter a number:", "Numeric input test",
301 msg
= "Invalid number entered or dialog cancelled.";
306 msg
.Printf(_T("You've entered %lu"), res
);
307 icon
= wxICON_INFORMATION
;
310 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
313 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
315 wxString pwd
= wxGetPasswordFromUser("Enter password:",
316 "Password entry dialog",
321 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
322 "Got password", wxOK
| wxICON_INFORMATION
, this);
326 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
328 wxTextEntryDialog
dialog(this,
329 "This is a small sample\n"
330 "A long, long string to test out the text entrybox",
331 "Please enter a string",
335 if (dialog
.ShowModal() == wxID_OK
)
337 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
342 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
344 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
347 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
348 "Please select a value", n
, (const wxString
*)choices
);
350 dialog
.SetSelection(2);
352 if (dialog
.ShowModal() == wxID_OK
)
354 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
359 void MyFrame::MultiChoice(wxCommandEvent
& WXUNUSED(event
) )
361 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
364 wxArrayInt selections
;
365 size_t count
= wxGetMultipleChoices(selections
,
366 "This is a small sample\n"
367 "A multi-choice convenience dialog",
368 "Please select a value",
369 n
, (const wxString
*)choices
,
374 msg
.Printf("You selected %u items:\n", count
);
375 for ( size_t n
= 0; n
< count
; n
++ )
377 msg
+= wxString::Format("\t%u: %u (%s)\n", n
, selections
[n
],
378 choices
[selections
[n
]].c_str());
382 //else: cancelled or nothing selected
385 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
387 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
389 if (dialog
.ShowModal() == wxID_OK
)
392 info
.Printf(_T("Full file name: %s\n")
395 dialog
.GetPath().c_str(),
396 dialog
.GetDirectory().c_str(),
397 dialog
.GetFilename().c_str());
398 wxMessageDialog
dialog2(this, info
, "Selected file");
403 // this shows how to take advantage of specifying a default extension in the
404 // call to wxFileSelector: it is remembered after each new call and the next
405 // one will use it by default
406 void MyFrame::FileOpen2(wxCommandEvent
& WXUNUSED(event
) )
408 static wxString s_extDef
;
409 wxString path
= wxFileSelector(
410 _T("Select the file to load"),
413 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
421 // it is just a sample, would use wxSplitPath in real program
422 s_extDef
= path
.AfterLast(_T('.'));
424 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
425 (const wxChar
*) path
, (const wxChar
*) s_extDef
);
428 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
430 wxFileDialog
dialog(this, "Testing open multiple file dialog",
431 "", "", wxFileSelectorDefaultWildcardStr
,
434 if (dialog
.ShowModal() == wxID_OK
)
436 wxArrayString paths
, filenames
;
438 dialog
.GetPaths(paths
);
439 dialog
.GetFilenames(filenames
);
442 size_t count
= paths
.GetCount();
443 for ( size_t n
= 0; n
< count
; n
++ )
445 s
.Printf(_T("File %d: %s (%s)\n"),
446 n
, paths
[n
].c_str(), filenames
[n
].c_str());
451 wxMessageDialog
dialog2(this, msg
, "Selected files");
456 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
458 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
459 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
460 wxSAVE
|wxOVERWRITE_PROMPT
);
462 if (dialog
.ShowModal() == wxID_OK
)
465 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
466 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
471 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
473 // pass some initial dir to wxDirDialog
475 wxGetHomeDir(&dirHome
);
477 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
479 if (dialog
.ShowModal() == wxID_OK
)
481 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
486 void MyFrame::GenericDirChoose(wxCommandEvent
& WXUNUSED(event
) )
488 #if !defined(__WXMSW__) || defined(wxUSE_DIRDLGG) && wxUSE_DIRDLGG
489 // pass some initial dir to wxDirDialog
491 wxGetHomeDir(&dirHome
);
493 wxGenericDirDialog
dialog(this, "Testing generic directory picker", dirHome
);
495 if (dialog
.ShowModal() == wxID_OK
)
497 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
501 wxLogError("Sorry, generic dir dialog not available:\n"
502 "set wxUSE_DIRDLGG to 1 and recompile");
506 void MyFrame::ModalDlg(wxCommandEvent
& WXUNUSED(event
))
508 MyModalDialog
dlg(this);
512 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
514 bool show
= GetMenuBar()->IsChecked(event
.GetId());
520 m_dialog
= new MyModelessDialog(this);
523 m_dialog
->Show(TRUE
);
531 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
533 wxMessageBox("Button pressed in modeless dialog", "Info",
534 wxOK
| wxICON_INFORMATION
, this);
537 void MyFrame::ShowTip(wxCommandEvent
& event
)
539 #if wxUSE_STARTUP_TIPS
540 static size_t s_index
= (size_t)-1;
542 if ( s_index
== (size_t)-1 )
546 // this is completely bogus, we don't know how many lines are there
547 // in the file, but who cares, it's a demo only...
548 s_index
= rand() % 5;
551 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
553 bool showAtStartup
= wxShowTip(this, tipProvider
);
557 wxMessageBox("Will show tips on startup", "Tips dialog",
558 wxOK
| wxICON_INFORMATION
, this);
561 s_index
= tipProvider
->GetCurrentTip();
566 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
571 #if wxUSE_PROGRESSDLG
573 void MyFrame::ShowProgress( wxCommandEvent
& WXUNUSED(event
) )
575 static const int max
= 10;
577 wxProgressDialog
dialog("Progress dialog example",
578 "An informative message",
584 wxPD_ESTIMATED_TIME
|
585 wxPD_REMAINING_TIME
);
588 for ( int i
= 0; i
<= max
&& cont
; i
++ )
593 cont
= dialog
.Update(i
, "That's all, folks!");
595 else if ( i
== max
/ 2 )
597 cont
= dialog
.Update(i
, "Only a half left (very long message)!");
601 cont
= dialog
.Update(i
);
607 wxLogStatus("Progress dialog aborted!");
611 wxLogStatus("Countdown from %d finished", max
);
615 #endif // wxUSE_PROGRESSDLG
617 // ----------------------------------------------------------------------------
619 // ----------------------------------------------------------------------------
621 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
624 dc
.SetFont(wxGetApp().m_canvasFont
);
625 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
626 dc
.SetBackgroundMode(wxTRANSPARENT
);
627 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
635 : wxDialog(parent
, -1, wxString("Modeless dialog"))
637 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
639 wxButton
*btn
= new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");
640 wxCheckBox
*check
= new wxCheckBox(this, -1, "Should be disabled");
643 sizerTop
->Add(btn
, 1, wxEXPAND
| wxALL
, 5);
644 sizerTop
->Add(check
, 1, wxEXPAND
| wxALL
, 5);
649 sizerTop
->SetSizeHints(this);
653 void MyModelessDialog::OnClose(wxCloseEvent
& event
)
655 if ( event
.CanVeto() )
657 wxMessageBox("Use the menu item to close this dialog",
659 wxOK
| wxICON_INFORMATION
, this);
665 // ----------------------------------------------------------------------------
667 // ----------------------------------------------------------------------------
669 MyModalDialog::MyModalDialog(wxWindow
*parent
)
670 : wxDialog(parent
, -1, wxString("Modal dialog"))
672 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
674 m_btnFocused
= new wxButton(this, -1, "Default button");
675 m_btnDelete
= new wxButton(this, -1, "&Delete button");
676 sizerTop
->Add(m_btnFocused
, 0, wxALIGN_CENTER
| wxALL
, 5);
677 sizerTop
->Add(m_btnDelete
, 0, wxALIGN_CENTER
| wxALL
, 5);
682 sizerTop
->SetSizeHints(this);
685 m_btnFocused
->SetFocus();
686 m_btnFocused
->SetDefault();
689 void MyModalDialog::OnButton(wxCommandEvent
& event
)
691 if ( event
.GetEventObject() == m_btnDelete
)
696 m_btnDelete
->Disable();
698 else if ( event
.GetEventObject() == m_btnFocused
)
700 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",