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>
47 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
48 EVT_PAINT(MyCanvas::OnPaint
)
51 BEGIN_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_MODAL
, MyFrame::ModalDlg
)
65 EVT_MENU(DIALOGS_MODELESS
, MyFrame::ModelessDlg
)
66 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
67 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
68 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
69 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
72 EVT_MENU(DIALOGS_PROGRESS
, MyFrame::ShowProgress
)
74 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
76 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
79 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
80 EVT_BUTTON(-1, MyModalDialog::OnButton
)
83 BEGIN_EVENT_TABLE(MyModelessDialog
, wxDialog
)
84 EVT_CLOSE(MyModelessDialog::OnClose
)
87 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
89 // `Main program' equivalent, creating windows and returning main app frame
92 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
93 wxConvCurrent
= &wxConvLibc
;
96 m_canvasTextColour
= wxColour("BLACK");
97 m_canvasFont
= *wxNORMAL_FONT
;
99 // Create the main frame window
100 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
103 wxMenu
*file_menu
= new wxMenu
;
105 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
107 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
108 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
111 file_menu
->AppendSeparator();
112 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
114 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
115 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
118 file_menu
->AppendSeparator();
119 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
120 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
121 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
122 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
123 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
124 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
125 file_menu
->AppendSeparator();
126 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
127 file_menu
->AppendSeparator();
128 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
129 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
130 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
131 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
132 #if wxUSE_PROGRESSDLG
133 file_menu
->Append(DIALOGS_PROGRESS
, "Pro&gress dialog\tCtrl-G");
134 #endif // wxUSE_PROGRESSDLG
135 file_menu
->AppendSeparator();
136 file_menu
->Append(DIALOGS_MODAL
, "Mo&dal dialog\tCtrl-F");
137 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
138 file_menu
->AppendSeparator();
139 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
140 wxMenuBar
*menu_bar
= new wxMenuBar
;
141 menu_bar
->Append(file_menu
, "&File");
142 frame
->SetMenuBar(menu_bar
);
144 myCanvas
= new MyCanvas(frame
);
145 myCanvas
->SetBackgroundColour(*wxWHITE
);
147 frame
->Centre(wxBOTH
);
157 // My frame constructor
158 MyFrame::MyFrame(wxWindow
*parent
,
159 const wxString
& title
,
162 : wxFrame(parent
, -1, title
, pos
, size
)
164 m_dialog
= (MyModelessDialog
*)NULL
;
167 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
170 data
.SetChooseFull(TRUE
);
171 for (int i
= 0; i
< 16; i
++)
173 wxColour
colour(i
*16, i
*16, i
*16);
174 data
.SetCustomColour(i
, colour
);
177 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
178 dialog
->SetTitle("Choose the background colour");
179 if (dialog
->ShowModal() == wxID_OK
)
181 wxColourData retData
= dialog
->GetColourData();
182 wxColour col
= retData
.GetColour();
183 myCanvas
->SetBackgroundColour(col
);
190 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
193 data
.SetInitialFont(wxGetApp().m_canvasFont
);
194 data
.SetColour(wxGetApp().m_canvasTextColour
);
196 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
197 if (dialog
->ShowModal() == wxID_OK
)
199 wxFontData retData
= dialog
->GetFontData();
200 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
201 wxGetApp().m_canvasTextColour
= retData
.GetColour();
207 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
208 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
211 data
.SetChooseFull(TRUE
);
212 for (int i
= 0; i
< 16; i
++)
214 wxColour
colour(i
*16, i
*16, i
*16);
215 data
.SetCustomColour(i
, colour
);
218 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
219 if (dialog
->ShowModal() == wxID_OK
)
221 wxColourData retData
= dialog
->GetColourData();
222 wxColour col
= retData
.GetColour();
223 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
224 myCanvas
->SetBackgroundColour(col
);
231 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
234 data
.SetInitialFont(wxGetApp().m_canvasFont
);
235 data
.SetColour(wxGetApp().m_canvasTextColour
);
237 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
238 if (dialog
->ShowModal() == wxID_OK
)
240 wxFontData retData
= dialog
->GetFontData();
241 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
242 wxGetApp().m_canvasTextColour
= retData
.GetColour();
247 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
249 void MyFrame::LogDialog(wxCommandEvent
& event
)
251 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
252 // being flushed -- test it
255 wxLogMessage("This is some message - everything is ok so far.");
256 wxLogMessage("Another message...\n... this one is on multiple lines");
257 wxLogWarning("And then something went wrong!");
260 wxLogError("Intermediary error handler decided to abort.");
261 wxLogError("The top level caller detected an unrecoverable error.");
263 wxLog::FlushActive();
265 wxLogMessage("And this is the same dialog but with only one message.");
268 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
270 wxMessageDialog
dialog( NULL
, "This is a message box\nA long, long string to test out the message box properly",
271 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
276 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
278 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
279 "Even two rows of text.",
280 "Enter a number:", "Numeric input test",
287 msg
= "Invalid number entered or dialog cancelled.";
292 msg
.Printf(_T("You've entered %lu"), res
);
293 icon
= wxICON_INFORMATION
;
296 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
299 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
301 wxString pwd
= wxGetPasswordFromUser("Enter password:",
302 "Passowrd entry dialog",
307 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
308 "Got password", wxOK
| wxICON_INFORMATION
, this);
312 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
314 wxTextEntryDialog
dialog(this,
315 "This is a small sample\n"
316 "A long, long string to test out the text entrybox",
317 "Please enter a string",
321 if (dialog
.ShowModal() == wxID_OK
)
323 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
328 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
330 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
333 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
334 "Please select a value", n
, (const wxString
*)choices
);
336 dialog
.SetSelection(2);
338 if (dialog
.ShowModal() == wxID_OK
)
340 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
345 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
347 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
349 if (dialog
.ShowModal() == wxID_OK
)
352 info
.Printf(_T("Full file name: %s\n")
355 dialog
.GetPath().c_str(),
356 dialog
.GetDirectory().c_str(),
357 dialog
.GetFilename().c_str());
358 wxMessageDialog
dialog2(this, info
, "Selected file");
363 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
365 wxFileDialog
dialog(this, "Testing open multiple file dialog",
366 "", "", wxFileSelectorDefaultWildcardStr
,
369 if (dialog
.ShowModal() == wxID_OK
)
371 wxArrayString paths
, filenames
;
373 dialog
.GetPaths(paths
);
374 dialog
.GetFilenames(filenames
);
377 size_t count
= paths
.GetCount();
378 for ( size_t n
= 0; n
< count
; n
++ )
380 s
.Printf(_T("File %d: %s (%s)\n"),
381 n
, paths
[n
].c_str(), filenames
[n
].c_str());
386 wxMessageDialog
dialog2(this, msg
, "Selected files");
391 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
393 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
394 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
395 wxSAVE
|wxOVERWRITE_PROMPT
);
397 if (dialog
.ShowModal() == wxID_OK
)
400 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
401 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
406 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
408 // pass some initial dir to wxDirDialog
410 wxGetHomeDir(&dirHome
);
412 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
414 if (dialog
.ShowModal() == wxID_OK
)
416 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
421 void MyFrame::ModalDlg(wxCommandEvent
& WXUNUSED(event
))
423 MyModalDialog
dlg(this);
427 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
429 bool show
= GetMenuBar()->IsChecked(event
.GetInt());
435 m_dialog
= new MyModelessDialog(this);
438 m_dialog
->Show(TRUE
);
446 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
448 wxMessageBox("Button pressed in modeless dialog", "Info",
449 wxOK
| wxICON_INFORMATION
, this);
452 void MyFrame::ShowTip(wxCommandEvent
& event
)
454 #if wxUSE_STARTUP_TIPS
455 static size_t s_index
= (size_t)-1;
457 if ( s_index
== (size_t)-1 )
461 // this is completely bogus, we don't know how many lines are there
462 // in the file, but who cares, it's a demo only...
463 s_index
= rand() % 5;
466 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
468 bool showAtStartup
= wxShowTip(this, tipProvider
);
472 wxMessageBox("Will show tips on startup", "Tips dialog",
473 wxOK
| wxICON_INFORMATION
, this);
476 s_index
= tipProvider
->GetCurrentTip();
481 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
486 #if wxUSE_PROGRESSDLG
488 void MyFrame::ShowProgress( wxCommandEvent
& WXUNUSED(event
) )
490 static const int max
= 10;
492 wxProgressDialog
dialog("Progress dialog example",
493 "An informative message",
499 wxPD_ESTIMATED_TIME
|
500 wxPD_REMAINING_TIME
);
503 for ( int i
= 0; i
<= max
&& cont
; i
++ )
508 cont
= dialog
.Update(i
, "That's all, folks!");
510 else if ( i
== max
/ 2 )
512 cont
= dialog
.Update(i
, "Only a half left (very long message)!");
516 cont
= dialog
.Update(i
);
522 wxLogStatus("Progress dialog aborted!");
526 wxLogStatus("Countdown from %d finished", max
);
530 #endif // wxUSE_PROGRESSDLG
532 // ----------------------------------------------------------------------------
534 // ----------------------------------------------------------------------------
536 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
539 dc
.SetFont(wxGetApp().m_canvasFont
);
540 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
541 dc
.SetBackgroundMode(wxTRANSPARENT
);
542 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
545 // ----------------------------------------------------------------------------
547 // ----------------------------------------------------------------------------
549 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
550 : wxDialog(parent
, -1, wxString("Modeless dialog"))
552 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
554 wxButton
*btn
= new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");
555 wxCheckBox
*check
= new wxCheckBox(this, -1, "Should be disabled");
558 sizerTop
->Add(btn
, 1, wxEXPAND
| wxALL
, 5);
559 sizerTop
->Add(check
, 1, wxEXPAND
| wxALL
, 5);
564 sizerTop
->SetSizeHints(this);
568 void MyModelessDialog::OnClose(wxCloseEvent
& event
)
570 if ( event
.CanVeto() )
572 wxMessageBox("Use the menu item to close this dialog",
574 wxOK
| wxICON_INFORMATION
, this);
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 MyModalDialog::MyModalDialog(wxWindow
*parent
)
585 : wxDialog(parent
, -1, wxString("Modal dialog"))
587 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
589 m_btnFocused
= new wxButton(this, -1, "Default button");
590 m_btnDelete
= new wxButton(this, -1, "&Delete button");
591 sizerTop
->Add(m_btnFocused
, 0, wxALIGN_CENTER
| wxALL
, 5);
592 sizerTop
->Add(m_btnDelete
, 0, wxALIGN_CENTER
| wxALL
, 5);
597 sizerTop
->SetSizeHints(this);
600 m_btnFocused
->SetFocus();
601 m_btnFocused
->SetDefault();
604 void MyModalDialog::OnButton(wxCommandEvent
& event
)
606 if ( event
.GetEventObject() == m_btnDelete
)
611 m_btnDelete
->Disable();
613 else if ( event
.GetEventObject() == m_btnFocused
)
615 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",