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_MULTI_CHOICE
, MyFrame::MultiChoice
)
64 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
65 EVT_MENU(DIALOGS_FILE_OPEN2
, MyFrame::FileOpen2
)
66 EVT_MENU(DIALOGS_FILES_OPEN
, MyFrame::FilesOpen
)
67 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
68 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
69 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE
, MyFrame::GenericDirChoose
)
70 EVT_MENU(DIALOGS_MODAL
, MyFrame::ModalDlg
)
71 EVT_MENU(DIALOGS_MODELESS
, MyFrame::ModelessDlg
)
72 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
73 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
74 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
75 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
78 EVT_MENU(DIALOGS_PROGRESS
, MyFrame::ShowProgress
)
80 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
82 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
85 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
86 EVT_BUTTON(-1, MyModalDialog::OnButton
)
89 BEGIN_EVENT_TABLE(MyModelessDialog
, wxDialog
)
90 EVT_CLOSE(MyModelessDialog::OnClose
)
93 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
95 // `Main program' equivalent, creating windows and returning main app frame
98 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
99 wxConvCurrent
= &wxConvLibc
;
102 m_canvasTextColour
= wxColour("BLACK");
103 m_canvasFont
= *wxNORMAL_FONT
;
105 // Create the main frame window
106 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
109 wxMenu
*file_menu
= new wxMenu
;
111 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
113 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
114 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
117 file_menu
->AppendSeparator();
118 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
120 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
121 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
124 file_menu
->AppendSeparator();
125 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
126 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
127 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
128 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
129 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
130 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
131 file_menu
->Append(DIALOGS_MULTI_CHOICE
, "M&ultiple choice\tCtrl-U");
132 file_menu
->AppendSeparator();
133 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
134 file_menu
->AppendSeparator();
135 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
136 file_menu
->Append(DIALOGS_FILE_OPEN2
, "&Second open file\tCtrl-2");
137 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
138 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
139 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
140 file_menu
->Append(DIALOGS_GENERIC_DIR_CHOOSE
, "&Choose a directory (generic implementation)");
141 #if wxUSE_PROGRESSDLG
142 file_menu
->Append(DIALOGS_PROGRESS
, "Pro&gress dialog\tCtrl-G");
143 #endif // wxUSE_PROGRESSDLG
144 file_menu
->AppendSeparator();
145 file_menu
->Append(DIALOGS_MODAL
, "Mo&dal dialog\tCtrl-F");
146 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
147 file_menu
->AppendSeparator();
148 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
149 wxMenuBar
*menu_bar
= new wxMenuBar
;
150 menu_bar
->Append(file_menu
, "&File");
151 frame
->SetMenuBar(menu_bar
);
153 myCanvas
= new MyCanvas(frame
);
154 myCanvas
->SetBackgroundColour(*wxWHITE
);
156 frame
->Centre(wxBOTH
);
166 // My frame constructor
167 MyFrame::MyFrame(wxWindow
*parent
,
168 const wxString
& title
,
171 : wxFrame(parent
, -1, title
, pos
, size
)
173 m_dialog
= (MyModelessDialog
*)NULL
;
176 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
179 data
.SetChooseFull(TRUE
);
180 for (int i
= 0; i
< 16; i
++)
182 wxColour
colour(i
*16, i
*16, i
*16);
183 data
.SetCustomColour(i
, colour
);
186 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
187 dialog
->SetTitle("Choose the background colour");
188 if (dialog
->ShowModal() == wxID_OK
)
190 wxColourData retData
= dialog
->GetColourData();
191 wxColour col
= retData
.GetColour();
192 myCanvas
->SetBackgroundColour(col
);
199 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
202 data
.SetInitialFont(wxGetApp().m_canvasFont
);
203 data
.SetColour(wxGetApp().m_canvasTextColour
);
205 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
206 if (dialog
->ShowModal() == wxID_OK
)
208 wxFontData retData
= dialog
->GetFontData();
209 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
210 wxGetApp().m_canvasTextColour
= retData
.GetColour();
216 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
217 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
220 data
.SetChooseFull(TRUE
);
221 for (int i
= 0; i
< 16; i
++)
223 wxColour
colour(i
*16, i
*16, i
*16);
224 data
.SetCustomColour(i
, colour
);
227 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
228 if (dialog
->ShowModal() == wxID_OK
)
230 wxColourData retData
= dialog
->GetColourData();
231 wxColour col
= retData
.GetColour();
232 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
233 myCanvas
->SetBackgroundColour(col
);
240 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
243 data
.SetInitialFont(wxGetApp().m_canvasFont
);
244 data
.SetColour(wxGetApp().m_canvasTextColour
);
246 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
247 if (dialog
->ShowModal() == wxID_OK
)
249 wxFontData retData
= dialog
->GetFontData();
250 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
251 wxGetApp().m_canvasTextColour
= retData
.GetColour();
256 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
258 void MyFrame::LogDialog(wxCommandEvent
& event
)
260 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
261 // being flushed -- test it
264 wxLogMessage("This is some message - everything is ok so far.");
265 wxLogMessage("Another message...\n... this one is on multiple lines");
266 wxLogWarning("And then something went wrong!");
269 wxLogError("Intermediary error handler decided to abort.");
270 wxLogError("The top level caller detected an unrecoverable error.");
272 wxLog::FlushActive();
274 wxLogMessage("And this is the same dialog but with only one message.");
277 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
279 wxMessageDialog
dialog( NULL
, "This is a message box\nA long, long string to test out the message box properly",
280 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
285 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
287 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
288 "Even two rows of text.",
289 "Enter a number:", "Numeric input test",
296 msg
= "Invalid number entered or dialog cancelled.";
301 msg
.Printf(_T("You've entered %lu"), res
);
302 icon
= wxICON_INFORMATION
;
305 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
308 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
310 wxString pwd
= wxGetPasswordFromUser("Enter password:",
311 "Password entry dialog",
316 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
317 "Got password", wxOK
| wxICON_INFORMATION
, this);
321 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
323 wxTextEntryDialog
dialog(this,
324 "This is a small sample\n"
325 "A long, long string to test out the text entrybox",
326 "Please enter a string",
330 if (dialog
.ShowModal() == wxID_OK
)
332 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
337 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
339 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
342 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
343 "Please select a value", n
, (const wxString
*)choices
);
345 dialog
.SetSelection(2);
347 if (dialog
.ShowModal() == wxID_OK
)
349 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
354 void MyFrame::MultiChoice(wxCommandEvent
& WXUNUSED(event
) )
356 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
359 wxArrayInt selections
;
360 size_t count
= wxGetMultipleChoices(selections
,
361 "This is a small sample\n"
362 "A multi-choice convenience dialog",
363 "Please select a value",
364 n
, (const wxString
*)choices
,
369 msg
.Printf("You selected %u items:\n", count
);
370 for ( size_t n
= 0; n
< count
; n
++ )
372 msg
+= wxString::Format("\t%u: %u (%s)\n", n
, selections
[n
],
373 choices
[selections
[n
]].c_str());
377 //else: cancelled or nothing selected
380 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
382 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
384 if (dialog
.ShowModal() == wxID_OK
)
387 info
.Printf(_T("Full file name: %s\n")
390 dialog
.GetPath().c_str(),
391 dialog
.GetDirectory().c_str(),
392 dialog
.GetFilename().c_str());
393 wxMessageDialog
dialog2(this, info
, "Selected file");
398 // this shows how to take advantage of specifying a default extension in the
399 // call to wxFileSelector: it is remembered after each new call and the next
400 // one will use it by default
401 void MyFrame::FileOpen2(wxCommandEvent
& WXUNUSED(event
) )
403 static wxString s_extDef
;
404 wxString path
= wxFileSelector(
405 _T("Select the file to load"),
408 _T("Waveform (*.wav)|*.wav|Plain text (*.txt)|*.txt|All files (*.*)|*.*"),
416 // it is just a sample, would use wxSplitPath in real program
417 s_extDef
= path
.AfterLast(_T('.'));
419 wxLogMessage(_T("You selected the file '%s', remembered extension '%s'"),
420 (const wxChar
*) path
, (const wxChar
*) s_extDef
);
423 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
425 wxFileDialog
dialog(this, "Testing open multiple file dialog",
426 "", "", wxFileSelectorDefaultWildcardStr
,
429 if (dialog
.ShowModal() == wxID_OK
)
431 wxArrayString paths
, filenames
;
433 dialog
.GetPaths(paths
);
434 dialog
.GetFilenames(filenames
);
437 size_t count
= paths
.GetCount();
438 for ( size_t n
= 0; n
< count
; n
++ )
440 s
.Printf(_T("File %d: %s (%s)\n"),
441 n
, paths
[n
].c_str(), filenames
[n
].c_str());
446 wxMessageDialog
dialog2(this, msg
, "Selected files");
451 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
453 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
454 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
455 wxSAVE
|wxOVERWRITE_PROMPT
);
457 if (dialog
.ShowModal() == wxID_OK
)
460 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
461 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
466 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
468 // pass some initial dir to wxDirDialog
470 wxGetHomeDir(&dirHome
);
472 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
474 if (dialog
.ShowModal() == wxID_OK
)
476 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
481 void MyFrame::GenericDirChoose(wxCommandEvent
& WXUNUSED(event
) )
483 // pass some initial dir to wxDirDialog
485 wxGetHomeDir(&dirHome
);
487 wxGenericDirDialog
dialog(this, "Testing generic directory picker", dirHome
);
489 if (dialog
.ShowModal() == wxID_OK
)
491 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
496 void MyFrame::ModalDlg(wxCommandEvent
& WXUNUSED(event
))
498 MyModalDialog
dlg(this);
502 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
504 bool show
= GetMenuBar()->IsChecked(event
.GetId());
510 m_dialog
= new MyModelessDialog(this);
513 m_dialog
->Show(TRUE
);
521 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
523 wxMessageBox("Button pressed in modeless dialog", "Info",
524 wxOK
| wxICON_INFORMATION
, this);
527 void MyFrame::ShowTip(wxCommandEvent
& event
)
529 #if wxUSE_STARTUP_TIPS
530 static size_t s_index
= (size_t)-1;
532 if ( s_index
== (size_t)-1 )
536 // this is completely bogus, we don't know how many lines are there
537 // in the file, but who cares, it's a demo only...
538 s_index
= rand() % 5;
541 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
543 bool showAtStartup
= wxShowTip(this, tipProvider
);
547 wxMessageBox("Will show tips on startup", "Tips dialog",
548 wxOK
| wxICON_INFORMATION
, this);
551 s_index
= tipProvider
->GetCurrentTip();
556 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
561 #if wxUSE_PROGRESSDLG
563 void MyFrame::ShowProgress( wxCommandEvent
& WXUNUSED(event
) )
565 static const int max
= 10;
567 wxProgressDialog
dialog("Progress dialog example",
568 "An informative message",
574 wxPD_ESTIMATED_TIME
|
575 wxPD_REMAINING_TIME
);
578 for ( int i
= 0; i
<= max
&& cont
; i
++ )
583 cont
= dialog
.Update(i
, "That's all, folks!");
585 else if ( i
== max
/ 2 )
587 cont
= dialog
.Update(i
, "Only a half left (very long message)!");
591 cont
= dialog
.Update(i
);
597 wxLogStatus("Progress dialog aborted!");
601 wxLogStatus("Countdown from %d finished", max
);
605 #endif // wxUSE_PROGRESSDLG
607 // ----------------------------------------------------------------------------
609 // ----------------------------------------------------------------------------
611 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
614 dc
.SetFont(wxGetApp().m_canvasFont
);
615 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
616 dc
.SetBackgroundMode(wxTRANSPARENT
);
617 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
620 // ----------------------------------------------------------------------------
622 // ----------------------------------------------------------------------------
624 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
625 : wxDialog(parent
, -1, wxString("Modeless dialog"))
627 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
629 wxButton
*btn
= new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");
630 wxCheckBox
*check
= new wxCheckBox(this, -1, "Should be disabled");
633 sizerTop
->Add(btn
, 1, wxEXPAND
| wxALL
, 5);
634 sizerTop
->Add(check
, 1, wxEXPAND
| wxALL
, 5);
639 sizerTop
->SetSizeHints(this);
643 void MyModelessDialog::OnClose(wxCloseEvent
& event
)
645 if ( event
.CanVeto() )
647 wxMessageBox("Use the menu item to close this dialog",
649 wxOK
| wxICON_INFORMATION
, this);
655 // ----------------------------------------------------------------------------
657 // ----------------------------------------------------------------------------
659 MyModalDialog::MyModalDialog(wxWindow
*parent
)
660 : wxDialog(parent
, -1, wxString("Modal dialog"))
662 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
664 m_btnFocused
= new wxButton(this, -1, "Default button");
665 m_btnDelete
= new wxButton(this, -1, "&Delete button");
666 sizerTop
->Add(m_btnFocused
, 0, wxALIGN_CENTER
| wxALL
, 5);
667 sizerTop
->Add(m_btnDelete
, 0, wxALIGN_CENTER
| wxALL
, 5);
672 sizerTop
->SetSizeHints(this);
675 m_btnFocused
->SetFocus();
676 m_btnFocused
->SetDefault();
679 void MyModalDialog::OnButton(wxCommandEvent
& event
)
681 if ( event
.GetEventObject() == m_btnDelete
)
686 m_btnDelete
->Disable();
688 else if ( event
.GetEventObject() == m_btnFocused
)
690 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",