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_FILES_OPEN
, MyFrame::FilesOpen
)
65 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
66 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
67 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE
, MyFrame::GenericDirChoose
)
68 EVT_MENU(DIALOGS_MODAL
, MyFrame::ModalDlg
)
69 EVT_MENU(DIALOGS_MODELESS
, MyFrame::ModelessDlg
)
70 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
71 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
72 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
73 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
76 EVT_MENU(DIALOGS_PROGRESS
, MyFrame::ShowProgress
)
78 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
80 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
83 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
84 EVT_BUTTON(-1, MyModalDialog::OnButton
)
87 BEGIN_EVENT_TABLE(MyModelessDialog
, wxDialog
)
88 EVT_CLOSE(MyModelessDialog::OnClose
)
91 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
93 // `Main program' equivalent, creating windows and returning main app frame
96 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
97 wxConvCurrent
= &wxConvLibc
;
100 m_canvasTextColour
= wxColour("BLACK");
101 m_canvasFont
= *wxNORMAL_FONT
;
103 // Create the main frame window
104 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
107 wxMenu
*file_menu
= new wxMenu
;
109 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
111 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
112 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
115 file_menu
->AppendSeparator();
116 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
118 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
119 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
122 file_menu
->AppendSeparator();
123 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
124 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
125 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
126 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
127 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
128 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
129 file_menu
->AppendSeparator();
130 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
131 file_menu
->AppendSeparator();
132 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
133 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
134 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
135 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
136 file_menu
->Append(DIALOGS_GENERIC_DIR_CHOOSE
, "&Choose a directory (generic implementation)");
137 #if wxUSE_PROGRESSDLG
138 file_menu
->Append(DIALOGS_PROGRESS
, "Pro&gress dialog\tCtrl-G");
139 #endif // wxUSE_PROGRESSDLG
140 file_menu
->AppendSeparator();
141 file_menu
->Append(DIALOGS_MODAL
, "Mo&dal dialog\tCtrl-F");
142 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
143 file_menu
->AppendSeparator();
144 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
145 wxMenuBar
*menu_bar
= new wxMenuBar
;
146 menu_bar
->Append(file_menu
, "&File");
147 frame
->SetMenuBar(menu_bar
);
149 myCanvas
= new MyCanvas(frame
);
150 myCanvas
->SetBackgroundColour(*wxWHITE
);
152 frame
->Centre(wxBOTH
);
162 // My frame constructor
163 MyFrame::MyFrame(wxWindow
*parent
,
164 const wxString
& title
,
167 : wxFrame(parent
, -1, title
, pos
, size
)
169 m_dialog
= (MyModelessDialog
*)NULL
;
172 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
175 data
.SetChooseFull(TRUE
);
176 for (int i
= 0; i
< 16; i
++)
178 wxColour
colour(i
*16, i
*16, i
*16);
179 data
.SetCustomColour(i
, colour
);
182 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
183 dialog
->SetTitle("Choose the background colour");
184 if (dialog
->ShowModal() == wxID_OK
)
186 wxColourData retData
= dialog
->GetColourData();
187 wxColour col
= retData
.GetColour();
188 myCanvas
->SetBackgroundColour(col
);
195 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
198 data
.SetInitialFont(wxGetApp().m_canvasFont
);
199 data
.SetColour(wxGetApp().m_canvasTextColour
);
201 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
202 if (dialog
->ShowModal() == wxID_OK
)
204 wxFontData retData
= dialog
->GetFontData();
205 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
206 wxGetApp().m_canvasTextColour
= retData
.GetColour();
212 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
213 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
216 data
.SetChooseFull(TRUE
);
217 for (int i
= 0; i
< 16; i
++)
219 wxColour
colour(i
*16, i
*16, i
*16);
220 data
.SetCustomColour(i
, colour
);
223 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
224 if (dialog
->ShowModal() == wxID_OK
)
226 wxColourData retData
= dialog
->GetColourData();
227 wxColour col
= retData
.GetColour();
228 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
229 myCanvas
->SetBackgroundColour(col
);
236 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
239 data
.SetInitialFont(wxGetApp().m_canvasFont
);
240 data
.SetColour(wxGetApp().m_canvasTextColour
);
242 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
243 if (dialog
->ShowModal() == wxID_OK
)
245 wxFontData retData
= dialog
->GetFontData();
246 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
247 wxGetApp().m_canvasTextColour
= retData
.GetColour();
252 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
254 void MyFrame::LogDialog(wxCommandEvent
& event
)
256 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
257 // being flushed -- test it
260 wxLogMessage("This is some message - everything is ok so far.");
261 wxLogMessage("Another message...\n... this one is on multiple lines");
262 wxLogWarning("And then something went wrong!");
265 wxLogError("Intermediary error handler decided to abort.");
266 wxLogError("The top level caller detected an unrecoverable error.");
268 wxLog::FlushActive();
270 wxLogMessage("And this is the same dialog but with only one message.");
273 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
275 wxMessageDialog
dialog( NULL
, "This is a message box\nA long, long string to test out the message box properly",
276 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
281 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
283 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
284 "Even two rows of text.",
285 "Enter a number:", "Numeric input test",
292 msg
= "Invalid number entered or dialog cancelled.";
297 msg
.Printf(_T("You've entered %lu"), res
);
298 icon
= wxICON_INFORMATION
;
301 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
304 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
306 wxString pwd
= wxGetPasswordFromUser("Enter password:",
307 "Password entry dialog",
312 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
313 "Got password", wxOK
| wxICON_INFORMATION
, this);
317 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
319 wxTextEntryDialog
dialog(this,
320 "This is a small sample\n"
321 "A long, long string to test out the text entrybox",
322 "Please enter a string",
326 if (dialog
.ShowModal() == wxID_OK
)
328 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
333 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
335 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
338 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
339 "Please select a value", n
, (const wxString
*)choices
);
341 dialog
.SetSelection(2);
343 if (dialog
.ShowModal() == wxID_OK
)
345 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
350 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
352 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
354 if (dialog
.ShowModal() == wxID_OK
)
357 info
.Printf(_T("Full file name: %s\n")
360 dialog
.GetPath().c_str(),
361 dialog
.GetDirectory().c_str(),
362 dialog
.GetFilename().c_str());
363 wxMessageDialog
dialog2(this, info
, "Selected file");
368 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
370 wxFileDialog
dialog(this, "Testing open multiple file dialog",
371 "", "", wxFileSelectorDefaultWildcardStr
,
374 if (dialog
.ShowModal() == wxID_OK
)
376 wxArrayString paths
, filenames
;
378 dialog
.GetPaths(paths
);
379 dialog
.GetFilenames(filenames
);
382 size_t count
= paths
.GetCount();
383 for ( size_t n
= 0; n
< count
; n
++ )
385 s
.Printf(_T("File %d: %s (%s)\n"),
386 n
, paths
[n
].c_str(), filenames
[n
].c_str());
391 wxMessageDialog
dialog2(this, msg
, "Selected files");
396 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
398 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
399 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
400 wxSAVE
|wxOVERWRITE_PROMPT
);
402 if (dialog
.ShowModal() == wxID_OK
)
405 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
406 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
411 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
413 // pass some initial dir to wxDirDialog
415 wxGetHomeDir(&dirHome
);
417 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
419 if (dialog
.ShowModal() == wxID_OK
)
421 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
426 void MyFrame::GenericDirChoose(wxCommandEvent
& WXUNUSED(event
) )
428 // pass some initial dir to wxDirDialog
430 wxGetHomeDir(&dirHome
);
432 wxGenericDirDialog
dialog(this, "Testing generic directory picker", dirHome
);
434 if (dialog
.ShowModal() == wxID_OK
)
436 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
441 void MyFrame::ModalDlg(wxCommandEvent
& WXUNUSED(event
))
443 MyModalDialog
dlg(this);
447 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
449 bool show
= GetMenuBar()->IsChecked(event
.GetInt());
455 m_dialog
= new MyModelessDialog(this);
458 m_dialog
->Show(TRUE
);
466 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
468 wxMessageBox("Button pressed in modeless dialog", "Info",
469 wxOK
| wxICON_INFORMATION
, this);
472 void MyFrame::ShowTip(wxCommandEvent
& event
)
474 #if wxUSE_STARTUP_TIPS
475 static size_t s_index
= (size_t)-1;
477 if ( s_index
== (size_t)-1 )
481 // this is completely bogus, we don't know how many lines are there
482 // in the file, but who cares, it's a demo only...
483 s_index
= rand() % 5;
486 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
488 bool showAtStartup
= wxShowTip(this, tipProvider
);
492 wxMessageBox("Will show tips on startup", "Tips dialog",
493 wxOK
| wxICON_INFORMATION
, this);
496 s_index
= tipProvider
->GetCurrentTip();
501 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
506 #if wxUSE_PROGRESSDLG
508 void MyFrame::ShowProgress( wxCommandEvent
& WXUNUSED(event
) )
510 static const int max
= 10;
512 wxProgressDialog
dialog("Progress dialog example",
513 "An informative message",
519 wxPD_ESTIMATED_TIME
|
520 wxPD_REMAINING_TIME
);
523 for ( int i
= 0; i
<= max
&& cont
; i
++ )
528 cont
= dialog
.Update(i
, "That's all, folks!");
530 else if ( i
== max
/ 2 )
532 cont
= dialog
.Update(i
, "Only a half left (very long message)!");
536 cont
= dialog
.Update(i
);
542 wxLogStatus("Progress dialog aborted!");
546 wxLogStatus("Countdown from %d finished", max
);
550 #endif // wxUSE_PROGRESSDLG
552 // ----------------------------------------------------------------------------
554 // ----------------------------------------------------------------------------
556 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
559 dc
.SetFont(wxGetApp().m_canvasFont
);
560 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
561 dc
.SetBackgroundMode(wxTRANSPARENT
);
562 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
570 : wxDialog(parent
, -1, wxString("Modeless dialog"))
572 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
574 wxButton
*btn
= new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");
575 wxCheckBox
*check
= new wxCheckBox(this, -1, "Should be disabled");
578 sizerTop
->Add(btn
, 1, wxEXPAND
| wxALL
, 5);
579 sizerTop
->Add(check
, 1, wxEXPAND
| wxALL
, 5);
584 sizerTop
->SetSizeHints(this);
588 void MyModelessDialog::OnClose(wxCloseEvent
& event
)
590 if ( event
.CanVeto() )
592 wxMessageBox("Use the menu item to close this dialog",
594 wxOK
| wxICON_INFORMATION
, this);
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 MyModalDialog::MyModalDialog(wxWindow
*parent
)
605 : wxDialog(parent
, -1, wxString("Modal dialog"))
607 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
609 m_btnFocused
= new wxButton(this, -1, "Default button");
610 m_btnDelete
= new wxButton(this, -1, "&Delete button");
611 sizerTop
->Add(m_btnFocused
, 0, wxALIGN_CENTER
| wxALL
, 5);
612 sizerTop
->Add(m_btnDelete
, 0, wxALIGN_CENTER
| wxALL
, 5);
617 sizerTop
->SetSizeHints(this);
620 m_btnFocused
->SetFocus();
621 m_btnFocused
->SetDefault();
624 void MyModalDialog::OnButton(wxCommandEvent
& event
)
626 if ( event
.GetEventObject() == m_btnDelete
)
631 m_btnDelete
->Disable();
633 else if ( event
.GetEventObject() == m_btnFocused
)
635 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",