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_MODELESS
, MyFrame::ModelessDlg
)
65 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
66 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
67 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
68 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
71 EVT_MENU(DIALOGS_PROGRESS
, MyFrame::ShowProgress
)
73 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
75 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
78 BEGIN_EVENT_TABLE(MyModelessDialog
, wxDialog
)
79 EVT_CLOSE(MyModelessDialog::OnClose
)
82 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
84 // `Main program' equivalent, creating windows and returning main app frame
87 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
88 wxConvCurrent
= &wxConvLibc
;
91 m_canvasTextColour
= wxColour("BLACK");
92 m_canvasFont
= *wxNORMAL_FONT
;
94 // Create the main frame window
95 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
98 wxMenu
*file_menu
= new wxMenu
;
100 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
102 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
103 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
106 file_menu
->AppendSeparator();
107 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
109 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
110 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
113 file_menu
->AppendSeparator();
114 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
115 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
116 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
117 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
118 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
119 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
120 file_menu
->AppendSeparator();
121 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
122 file_menu
->AppendSeparator();
123 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
124 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
125 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
126 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
127 file_menu
->AppendSeparator();
128 #if wxUSE_PROGRESSDLG
129 file_menu
->Append(DIALOGS_PROGRESS
, "Pro&gress dialog\tCtrl-G");
130 #endif // wxUSE_PROGRESSDLG
131 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
132 file_menu
->AppendSeparator();
133 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
134 wxMenuBar
*menu_bar
= new wxMenuBar
;
135 menu_bar
->Append(file_menu
, "&File");
136 frame
->SetMenuBar(menu_bar
);
138 myCanvas
= new MyCanvas(frame
);
139 myCanvas
->SetBackgroundColour(*wxWHITE
);
141 frame
->Centre(wxBOTH
);
151 // My frame constructor
152 MyFrame::MyFrame(wxWindow
*parent
,
153 const wxString
& title
,
156 : wxFrame(parent
, -1, title
, pos
, size
)
158 m_dialog
= (MyModelessDialog
*)NULL
;
161 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
164 data
.SetChooseFull(TRUE
);
165 for (int i
= 0; i
< 16; i
++)
167 wxColour
colour(i
*16, i
*16, i
*16);
168 data
.SetCustomColour(i
, colour
);
171 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
172 if (dialog
->ShowModal() == wxID_OK
)
174 wxColourData retData
= dialog
->GetColourData();
175 wxColour col
= retData
.GetColour();
176 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
177 myCanvas
->SetBackgroundColour(col
);
184 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
187 data
.SetInitialFont(wxGetApp().m_canvasFont
);
188 data
.SetColour(wxGetApp().m_canvasTextColour
);
190 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
191 if (dialog
->ShowModal() == wxID_OK
)
193 wxFontData retData
= dialog
->GetFontData();
194 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
195 wxGetApp().m_canvasTextColour
= retData
.GetColour();
201 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
202 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
205 data
.SetChooseFull(TRUE
);
206 for (int i
= 0; i
< 16; i
++)
208 wxColour
colour(i
*16, i
*16, i
*16);
209 data
.SetCustomColour(i
, colour
);
212 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
213 if (dialog
->ShowModal() == wxID_OK
)
215 wxColourData retData
= dialog
->GetColourData();
216 wxColour col
= retData
.GetColour();
217 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
218 myCanvas
->SetBackgroundColour(col
);
225 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
228 data
.SetInitialFont(wxGetApp().m_canvasFont
);
229 data
.SetColour(wxGetApp().m_canvasTextColour
);
231 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
232 if (dialog
->ShowModal() == wxID_OK
)
234 wxFontData retData
= dialog
->GetFontData();
235 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
236 wxGetApp().m_canvasTextColour
= retData
.GetColour();
241 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
243 void MyFrame::LogDialog(wxCommandEvent
& event
)
245 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
246 // being flushed -- test it
249 wxLogMessage("This is some message - everything is ok so far.");
250 wxLogMessage("Another message...\n... this one is on multiple lines");
251 wxLogWarning("And then something went wrong!");
254 wxLogError("Intermediary error handler decided to abort.");
255 wxLogError("The top level caller detected an unrecoverable error.");
257 wxLog::FlushActive();
259 wxLogMessage("And this is the same dialog but with only one message.");
262 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
264 wxMessageDialog
dialog( this, "This is a message box\nA long, long string to test out the message box properly",
265 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
270 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
272 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
273 "Even two rows of text.",
274 "Enter a number:", "Numeric input test",
281 msg
= "Invalid number entered or dialog cancelled.";
286 msg
.Printf(_T("You've entered %lu"), res
);
287 icon
= wxICON_INFORMATION
;
290 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
293 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
295 wxString pwd
= wxGetPasswordFromUser("Enter password:",
296 "Passowrd entry dialog",
301 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
302 "Got password", wxOK
| wxICON_INFORMATION
, this);
306 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
308 wxTextEntryDialog
dialog(this,
309 "This is a small sample\n"
310 "A long, long string to test out the text entrybox",
311 "Please enter a string",
315 if (dialog
.ShowModal() == wxID_OK
)
317 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
322 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
324 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
327 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
328 "Please select a value", n
, (const wxString
*)choices
);
330 dialog
.SetSelection(2);
332 if (dialog
.ShowModal() == wxID_OK
)
334 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
339 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
341 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
343 if (dialog
.ShowModal() == wxID_OK
)
346 info
.Printf(_T("Full file name: %s\n")
349 dialog
.GetPath().c_str(),
350 dialog
.GetDirectory().c_str(),
351 dialog
.GetFilename().c_str());
352 wxMessageDialog
dialog2(this, info
, "Selected file");
357 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
359 wxFileDialog
dialog(this, "Testing open multiple file dialog",
360 "", "", wxFileSelectorDefaultWildcardStr
,
363 if (dialog
.ShowModal() == wxID_OK
)
365 wxArrayString paths
, filenames
;
367 dialog
.GetPaths(paths
);
368 dialog
.GetFilenames(filenames
);
371 size_t count
= paths
.GetCount();
372 for ( size_t n
= 0; n
< count
; n
++ )
374 s
.Printf(_T("File %d: %s (%s)\n"),
375 n
, paths
[n
].c_str(), filenames
[n
].c_str());
380 wxMessageDialog
dialog2(this, msg
, "Selected files");
385 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
387 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
388 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
389 wxSAVE
|wxOVERWRITE_PROMPT
);
391 if (dialog
.ShowModal() == wxID_OK
)
394 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
395 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
400 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
402 // pass some initial dir to wxDirDialog
404 wxGetHomeDir(&dirHome
);
406 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
408 if (dialog
.ShowModal() == wxID_OK
)
410 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
415 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
417 bool show
= GetMenuBar()->IsChecked(event
.GetInt());
423 m_dialog
= new MyModelessDialog(this);
426 m_dialog
->Show(TRUE
);
434 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
436 wxMessageBox("Button pressed in modeless dialog", "Info",
437 wxOK
| wxICON_INFORMATION
, this);
440 void MyFrame::ShowTip(wxCommandEvent
& event
)
442 #if wxUSE_STARTUP_TIPS
443 static size_t s_index
= (size_t)-1;
445 if ( s_index
== (size_t)-1 )
449 // this is completely bogus, we don't know how many lines are there
450 // in the file, but who cares, it's a demo only...
451 s_index
= rand() % 5;
454 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
456 bool showAtStartup
= wxShowTip(this, tipProvider
);
460 wxMessageBox("Will show tips on startup", "Tips dialog",
461 wxOK
| wxICON_INFORMATION
, this);
464 s_index
= tipProvider
->GetCurrentTip();
469 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
474 #if wxUSE_PROGRESSDLG
476 void MyFrame::ShowProgress( wxCommandEvent
& WXUNUSED(event
) )
478 static const int max
= 10;
480 wxProgressDialog
dialog("Progress dialog example",
481 "An informative message",
487 wxPD_ESTIMATED_TIME
|
488 wxPD_REMAINING_TIME
);
491 for ( int i
= 0; i
<= max
&& cont
; i
++ )
496 cont
= dialog
.Update(i
, "That's all, folks!");
498 else if ( i
== max
/ 2 )
500 cont
= dialog
.Update(i
, "Only a half left (very long message)!");
504 cont
= dialog
.Update(i
);
510 wxLogStatus("Progress dialog aborted!");
514 wxLogStatus("Countdown from %d finished", max
);
518 #endif // wxUSE_PROGRESSDLG
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
524 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
527 dc
.SetFont(wxGetApp().m_canvasFont
);
528 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
529 dc
.SetBackgroundMode(wxTRANSPARENT
);
530 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
538 : wxDialog(parent
, -1, wxString("Modeless dialog"))
540 (void)new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");
545 void MyModelessDialog::OnClose(wxCloseEvent
& event
)
547 if ( event
.CanVeto() )
549 wxMessageBox("Use the menu item to close this dialog",
551 wxOK
| wxICON_INFORMATION
, this);