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 // New wxGenericDirCtrl
37 #include "wx/dirctrl.h"
39 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
41 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
42 #include <wx/generic/colrdlgg.h>
43 #include <wx/generic/fontdlgg.h>
50 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
51 EVT_PAINT(MyCanvas::OnPaint
)
54 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
55 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
56 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
57 EVT_MENU(DIALOGS_LOG_DIALOG
, MyFrame::LogDialog
)
58 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
59 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
60 EVT_MENU(DIALOGS_PASSWORD_ENTRY
, MyFrame::PasswordEntry
)
61 EVT_MENU(DIALOGS_NUM_ENTRY
, MyFrame::NumericEntry
)
62 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
63 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
64 EVT_MENU(DIALOGS_FILE_OPEN2
, MyFrame::FileOpen2
)
65 EVT_MENU(DIALOGS_FILES_OPEN
, MyFrame::FilesOpen
)
66 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
67 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
68 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE
, MyFrame::GenericDirChoose
)
69 EVT_MENU(DIALOGS_MODAL
, MyFrame::ModalDlg
)
70 EVT_MENU(DIALOGS_MODELESS
, MyFrame::ModelessDlg
)
71 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
72 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
73 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
74 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
77 EVT_MENU(DIALOGS_PROGRESS
, MyFrame::ShowProgress
)
79 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
81 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
84 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
85 EVT_BUTTON(-1, MyModalDialog::OnButton
)
88 BEGIN_EVENT_TABLE(MyModelessDialog
, wxDialog
)
89 EVT_CLOSE(MyModelessDialog::OnClose
)
92 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
94 // `Main program' equivalent, creating windows and returning main app frame
97 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
98 wxConvCurrent
= &wxConvLibc
;
101 m_canvasTextColour
= wxColour("BLACK");
102 m_canvasFont
= *wxNORMAL_FONT
;
104 // Create the main frame window
105 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
108 wxMenu
*file_menu
= new wxMenu
;
110 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
112 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
113 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
116 file_menu
->AppendSeparator();
117 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
119 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
120 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
123 file_menu
->AppendSeparator();
124 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
125 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
126 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
127 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
128 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
129 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
130 file_menu
->AppendSeparator();
131 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
132 file_menu
->AppendSeparator();
133 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
134 file_menu
->Append(DIALOGS_FILE_OPEN2
, "&Second open file\tCtrl-2");
135 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
136 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
137 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
138 file_menu
->Append(DIALOGS_GENERIC_DIR_CHOOSE
, "&Choose a directory (generic implementation)");
139 #if wxUSE_PROGRESSDLG
140 file_menu
->Append(DIALOGS_PROGRESS
, "Pro&gress dialog\tCtrl-G");
141 #endif // wxUSE_PROGRESSDLG
142 file_menu
->AppendSeparator();
143 file_menu
->Append(DIALOGS_MODAL
, "Mo&dal dialog\tCtrl-F");
144 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
145 file_menu
->AppendSeparator();
146 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
147 wxMenuBar
*menu_bar
= new wxMenuBar
;
148 menu_bar
->Append(file_menu
, "&File");
149 frame
->SetMenuBar(menu_bar
);
151 myCanvas
= new MyCanvas(frame
);
152 myCanvas
->SetBackgroundColour(*wxWHITE
);
154 frame
->Centre(wxBOTH
);
164 // My frame constructor
165 MyFrame::MyFrame(wxWindow
*parent
,
166 const wxString
& title
,
169 : wxFrame(parent
, -1, title
, pos
, size
)
171 m_dialog
= (MyModelessDialog
*)NULL
;
174 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
177 data
.SetChooseFull(TRUE
);
178 for (int i
= 0; i
< 16; i
++)
180 wxColour
colour(i
*16, i
*16, i
*16);
181 data
.SetCustomColour(i
, colour
);
184 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
185 dialog
->SetTitle("Choose the background colour");
186 if (dialog
->ShowModal() == wxID_OK
)
188 wxColourData retData
= dialog
->GetColourData();
189 wxColour col
= retData
.GetColour();
190 myCanvas
->SetBackgroundColour(col
);
197 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
200 data
.SetInitialFont(wxGetApp().m_canvasFont
);
201 data
.SetColour(wxGetApp().m_canvasTextColour
);
203 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
204 if (dialog
->ShowModal() == wxID_OK
)
206 wxFontData retData
= dialog
->GetFontData();
207 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
208 wxGetApp().m_canvasTextColour
= retData
.GetColour();
214 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
215 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
218 data
.SetChooseFull(TRUE
);
219 for (int i
= 0; i
< 16; i
++)
221 wxColour
colour(i
*16, i
*16, i
*16);
222 data
.SetCustomColour(i
, colour
);
225 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
226 if (dialog
->ShowModal() == wxID_OK
)
228 wxColourData retData
= dialog
->GetColourData();
229 wxColour col
= retData
.GetColour();
230 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
231 myCanvas
->SetBackgroundColour(col
);
238 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
241 data
.SetInitialFont(wxGetApp().m_canvasFont
);
242 data
.SetColour(wxGetApp().m_canvasTextColour
);
244 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
245 if (dialog
->ShowModal() == wxID_OK
)
247 wxFontData retData
= dialog
->GetFontData();
248 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
249 wxGetApp().m_canvasTextColour
= retData
.GetColour();
254 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
256 void MyFrame::LogDialog(wxCommandEvent
& event
)
258 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
259 // being flushed -- test it
262 wxLogMessage("This is some message - everything is ok so far.");
263 wxLogMessage("Another message...\n... this one is on multiple lines");
264 wxLogWarning("And then something went wrong!");
267 wxLogError("Intermediary error handler decided to abort.");
268 wxLogError("The top level caller detected an unrecoverable error.");
270 wxLog::FlushActive();
272 wxLogMessage("And this is the same dialog but with only one message.");
275 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
277 wxMessageDialog
dialog( NULL
, "This is a message box\nA long, long string to test out the message box properly",
278 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
283 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
285 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
286 "Even two rows of text.",
287 "Enter a number:", "Numeric input test",
294 msg
= "Invalid number entered or dialog cancelled.";
299 msg
.Printf(_T("You've entered %lu"), res
);
300 icon
= wxICON_INFORMATION
;
303 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
306 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
308 wxString pwd
= wxGetPasswordFromUser("Enter password:",
309 "Password entry dialog",
314 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
315 "Got password", wxOK
| wxICON_INFORMATION
, this);
319 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
321 wxTextEntryDialog
dialog(this,
322 "This is a small sample\n"
323 "A long, long string to test out the text entrybox",
324 "Please enter a string",
328 if (dialog
.ShowModal() == wxID_OK
)
330 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
335 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
337 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
340 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
341 "Please select a value", n
, (const wxString
*)choices
);
343 dialog
.SetSelection(2);
345 if (dialog
.ShowModal() == wxID_OK
)
347 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
352 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
354 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
356 if (dialog
.ShowModal() == wxID_OK
)
359 info
.Printf(_T("Full file name: %s\n")
362 dialog
.GetPath().c_str(),
363 dialog
.GetDirectory().c_str(),
364 dialog
.GetFilename().c_str());
365 wxMessageDialog
dialog2(this, info
, "Selected file");
370 // this shows how to take advantage of specifying a default extension in the
371 // call to wxFileSelector: it is remembered after each new call and the next
372 // one will use it by default
373 void MyFrame::FileOpen2(wxCommandEvent
& WXUNUSED(event
) )
375 static wxString s_extDef
;
376 wxString path
= wxFileSelector(
377 _T("Select the file to load"),
380 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
388 // it is just a sample, would use wxSplitPath in real program
389 s_extDef
= path
.AfterLast(_T('.'));
391 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
392 (const wxChar
*) path
, (const wxChar
*) s_extDef
);
395 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
397 wxFileDialog
dialog(this, "Testing open multiple file dialog",
398 "", "", wxFileSelectorDefaultWildcardStr
,
401 if (dialog
.ShowModal() == wxID_OK
)
403 wxArrayString paths
, filenames
;
405 dialog
.GetPaths(paths
);
406 dialog
.GetFilenames(filenames
);
409 size_t count
= paths
.GetCount();
410 for ( size_t n
= 0; n
< count
; n
++ )
412 s
.Printf(_T("File %d: %s (%s)\n"),
413 n
, paths
[n
].c_str(), filenames
[n
].c_str());
418 wxMessageDialog
dialog2(this, msg
, "Selected files");
423 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
425 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
426 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
427 wxSAVE
|wxOVERWRITE_PROMPT
);
429 if (dialog
.ShowModal() == wxID_OK
)
432 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
433 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
438 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
440 // pass some initial dir to wxDirDialog
442 wxGetHomeDir(&dirHome
);
444 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
446 if (dialog
.ShowModal() == wxID_OK
)
448 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
453 void MyFrame::GenericDirChoose(wxCommandEvent
& WXUNUSED(event
) )
455 // pass some initial dir to wxDirDialog
457 wxGetHomeDir(&dirHome
);
459 wxGenericDirDialog
dialog(this, "Testing generic directory picker", dirHome
);
461 if (dialog
.ShowModal() == wxID_OK
)
463 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
468 void MyFrame::ModalDlg(wxCommandEvent
& WXUNUSED(event
))
470 MyModalDialog
dlg(this);
474 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
476 bool show
= GetMenuBar()->IsChecked(event
.GetId());
482 m_dialog
= new MyModelessDialog(this);
485 m_dialog
->Show(TRUE
);
493 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
495 wxMessageBox("Button pressed in modeless dialog", "Info",
496 wxOK
| wxICON_INFORMATION
, this);
499 void MyFrame::ShowTip(wxCommandEvent
& event
)
501 #if wxUSE_STARTUP_TIPS
502 static size_t s_index
= (size_t)-1;
504 if ( s_index
== (size_t)-1 )
508 // this is completely bogus, we don't know how many lines are there
509 // in the file, but who cares, it's a demo only...
510 s_index
= rand() % 5;
513 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
515 bool showAtStartup
= wxShowTip(this, tipProvider
);
519 wxMessageBox("Will show tips on startup", "Tips dialog",
520 wxOK
| wxICON_INFORMATION
, this);
523 s_index
= tipProvider
->GetCurrentTip();
528 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
533 #if wxUSE_PROGRESSDLG
535 void MyFrame::ShowProgress( wxCommandEvent
& WXUNUSED(event
) )
537 static const int max
= 10;
539 wxProgressDialog
dialog("Progress dialog example",
540 "An informative message",
546 wxPD_ESTIMATED_TIME
|
547 wxPD_REMAINING_TIME
);
550 for ( int i
= 0; i
<= max
&& cont
; i
++ )
555 cont
= dialog
.Update(i
, "That's all, folks!");
557 else if ( i
== max
/ 2 )
559 cont
= dialog
.Update(i
, "Only a half left (very long message)!");
563 cont
= dialog
.Update(i
);
569 wxLogStatus("Progress dialog aborted!");
573 wxLogStatus("Countdown from %d finished", max
);
577 #endif // wxUSE_PROGRESSDLG
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
583 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
586 dc
.SetFont(wxGetApp().m_canvasFont
);
587 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
588 dc
.SetBackgroundMode(wxTRANSPARENT
);
589 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
592 // ----------------------------------------------------------------------------
594 // ----------------------------------------------------------------------------
596 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
597 : wxDialog(parent
, -1, wxString("Modeless dialog"))
599 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
601 wxButton
*btn
= new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");
602 wxCheckBox
*check
= new wxCheckBox(this, -1, "Should be disabled");
605 sizerTop
->Add(btn
, 1, wxEXPAND
| wxALL
, 5);
606 sizerTop
->Add(check
, 1, wxEXPAND
| wxALL
, 5);
611 sizerTop
->SetSizeHints(this);
615 void MyModelessDialog::OnClose(wxCloseEvent
& event
)
617 if ( event
.CanVeto() )
619 wxMessageBox("Use the menu item to close this dialog",
621 wxOK
| wxICON_INFORMATION
, this);
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
631 MyModalDialog::MyModalDialog(wxWindow
*parent
)
632 : wxDialog(parent
, -1, wxString("Modal dialog"))
634 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
636 m_btnFocused
= new wxButton(this, -1, "Default button");
637 m_btnDelete
= new wxButton(this, -1, "&Delete button");
638 sizerTop
->Add(m_btnFocused
, 0, wxALIGN_CENTER
| wxALL
, 5);
639 sizerTop
->Add(m_btnDelete
, 0, wxALIGN_CENTER
| wxALL
, 5);
644 sizerTop
->SetSizeHints(this);
647 m_btnFocused
->SetFocus();
648 m_btnFocused
->SetDefault();
651 void MyModalDialog::OnButton(wxCommandEvent
& event
)
653 if ( event
.GetEventObject() == m_btnDelete
)
658 m_btnDelete
->Disable();
660 else if ( event
.GetEventObject() == m_btnFocused
)
662 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",