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"
35 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
37 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
38 #include <wx/generic/colrdlgg.h>
39 #include <wx/generic/fontdlgg.h>
46 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
47 EVT_PAINT(MyCanvas::OnPaint
)
50 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
51 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
52 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
53 EVT_MENU(DIALOGS_LOG_DIALOG
, MyFrame::LogDialog
)
54 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
55 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
56 EVT_MENU(DIALOGS_PASSWORD_ENTRY
, MyFrame::PasswordEntry
)
57 EVT_MENU(DIALOGS_NUM_ENTRY
, MyFrame::NumericEntry
)
58 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
59 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
60 EVT_MENU(DIALOGS_FILES_OPEN
, MyFrame::FilesOpen
)
61 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
62 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
63 EVT_MENU(DIALOGS_MODELESS
, MyFrame::ModelessDlg
)
64 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
65 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
66 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
67 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
69 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
71 EVT_BUTTON(DIALOGS_MODELESS_BTN
, MyFrame::OnButton
)
73 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
75 // `Main program' equivalent, creating windows and returning main app frame
78 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
79 wxConvCurrent
= &wxConvLibc
;
82 m_canvasTextColour
= wxColour("BLACK");
83 m_canvasFont
= *wxNORMAL_FONT
;
85 // Create the main frame window
86 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
89 wxMenu
*file_menu
= new wxMenu
;
91 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
93 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
94 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
97 file_menu
->AppendSeparator();
98 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
100 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
101 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
104 file_menu
->AppendSeparator();
105 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
106 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
107 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
108 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
109 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
110 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-C");
111 file_menu
->AppendSeparator();
112 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
113 file_menu
->AppendSeparator();
114 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
115 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
116 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file\tCtrl-S");
117 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
118 file_menu
->AppendSeparator();
119 file_menu
->Append(DIALOGS_MODELESS
, "Modeless &dialog\tCtrl-Z", "", TRUE
);
120 file_menu
->AppendSeparator();
121 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
122 wxMenuBar
*menu_bar
= new wxMenuBar
;
123 menu_bar
->Append(file_menu
, "&File");
124 frame
->SetMenuBar(menu_bar
);
126 myCanvas
= new MyCanvas(frame
);
127 myCanvas
->SetBackgroundColour(*wxWHITE
);
129 frame
->Centre(wxBOTH
);
139 // My frame constructor
140 MyFrame::MyFrame(wxWindow
*parent
,
141 const wxString
& title
,
144 : wxFrame(parent
, -1, title
, pos
, size
)
146 m_dialog
= (MyModelessDialog
*)NULL
;
149 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
152 data
.SetChooseFull(TRUE
);
153 for (int i
= 0; i
< 16; i
++)
155 wxColour
colour(i
*16, i
*16, i
*16);
156 data
.SetCustomColour(i
, colour
);
159 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
160 if (dialog
->ShowModal() == wxID_OK
)
162 wxColourData retData
= dialog
->GetColourData();
163 wxColour col
= retData
.GetColour();
164 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
165 myCanvas
->SetBackgroundColour(col
);
172 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
175 data
.SetInitialFont(wxGetApp().m_canvasFont
);
176 data
.SetColour(wxGetApp().m_canvasTextColour
);
178 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
179 if (dialog
->ShowModal() == wxID_OK
)
181 wxFontData retData
= dialog
->GetFontData();
182 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
183 wxGetApp().m_canvasTextColour
= retData
.GetColour();
189 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
190 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
193 data
.SetChooseFull(TRUE
);
194 for (int i
= 0; i
< 16; i
++)
196 wxColour
colour(i
*16, i
*16, i
*16);
197 data
.SetCustomColour(i
, colour
);
200 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
201 if (dialog
->ShowModal() == wxID_OK
)
203 wxColourData retData
= dialog
->GetColourData();
204 wxColour col
= retData
.GetColour();
205 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
206 myCanvas
->SetBackgroundColour(col
);
213 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
216 data
.SetInitialFont(wxGetApp().m_canvasFont
);
217 data
.SetColour(wxGetApp().m_canvasTextColour
);
219 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
220 if (dialog
->ShowModal() == wxID_OK
)
222 wxFontData retData
= dialog
->GetFontData();
223 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
224 wxGetApp().m_canvasTextColour
= retData
.GetColour();
229 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
231 void MyFrame::LogDialog(wxCommandEvent
& event
)
233 wxLogMessage("This is some message - everything is ok so far.");
234 wxLogMessage("Another message...\n... this one is on multiple lines");
235 wxLogWarning("And then something went wrong!");
236 wxLogError("Intermediary error handler decided to abort.");
237 wxLogError("The top level caller detected an unrecoverable error.");
239 wxLog::FlushActive();
241 wxLogMessage("And this is the same dialog but with only one message.");
244 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
246 wxMessageDialog
dialog( this, "This is a message box\nA long, long string to test out the message box properly",
247 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
252 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
254 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
255 "Even two rows of text.",
256 "Enter a number:", "Numeric input test",
263 msg
= "Invalid number entered or dialog cancelled.";
268 msg
.Printf(_T("You've entered %lu"), res
);
269 icon
= wxICON_INFORMATION
;
272 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
275 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
277 wxString pwd
= wxGetPasswordFromUser("Enter password:",
278 "Passowrd entry dialog",
283 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
284 "Got password", wxOK
| wxICON_INFORMATION
, this);
288 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
290 wxTextEntryDialog
dialog(this,
291 "This is a small sample\n"
292 "A long, long string to test out the text entrybox",
293 "Please enter a string",
297 if (dialog
.ShowModal() == wxID_OK
)
299 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
304 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
306 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
309 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
310 "Please select a value", n
, (const wxString
*)choices
);
312 dialog
.SetSelection(2);
314 if (dialog
.ShowModal() == wxID_OK
)
316 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
321 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
323 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
325 if (dialog
.ShowModal() == wxID_OK
)
328 info
.Printf(_T("Full file name: %s\n")
331 dialog
.GetPath().c_str(),
332 dialog
.GetDirectory().c_str(),
333 dialog
.GetFilename().c_str());
334 wxMessageDialog
dialog2(this, info
, "Selected file");
339 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
341 wxFileDialog
dialog(this, "Testing open multiple file dialog",
342 "", "", wxFileSelectorDefaultWildcardStr
,
345 if (dialog
.ShowModal() == wxID_OK
)
347 wxArrayString paths
, filenames
;
349 dialog
.GetPaths(paths
);
350 dialog
.GetFilenames(filenames
);
353 size_t count
= paths
.GetCount();
354 for ( size_t n
= 0; n
< count
; n
++ )
356 s
.Printf(_T("File %d: %s (%s)\n"),
357 n
, paths
[n
].c_str(), filenames
[n
].c_str());
362 wxMessageDialog
dialog2(this, msg
, "Selected files");
367 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
369 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
370 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
371 wxSAVE
|wxOVERWRITE_PROMPT
);
373 if (dialog
.ShowModal() == wxID_OK
)
376 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
377 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
382 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
384 // pass some initial dir to wxDirDialog
386 wxGetHomeDir(&dirHome
);
388 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
390 if (dialog
.ShowModal() == wxID_OK
)
392 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
397 void MyFrame::ModelessDlg(wxCommandEvent
& event
)
399 bool show
= GetMenuBar()->IsChecked(event
.GetInt());
405 m_dialog
= new MyModelessDialog(this);
408 m_dialog
->Show(TRUE
);
416 void MyFrame::OnButton(wxCommandEvent
& WXUNUSED(event
))
418 wxMessageBox("Button pressed in modeless dialog", "Info",
419 wxOK
| wxICON_INFORMATION
, this);
422 void MyFrame::ShowTip(wxCommandEvent
& event
)
424 #if wxUSE_STARTUP_TIPS
425 static size_t s_index
= (size_t)-1;
427 if ( s_index
== (size_t)-1 )
431 // this is completely bogus, we don't know how many lines are there
432 // in the file, but who cares, it's a demo only...
433 s_index
= rand() % 5;
436 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
438 bool showAtStartup
= wxShowTip(this, tipProvider
);
442 wxMessageBox("Will show tips on startup", "Tips dialog",
443 wxOK
| wxICON_INFORMATION
, this);
446 s_index
= tipProvider
->GetCurrentTip();
451 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
456 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
459 dc
.SetFont(wxGetApp().m_canvasFont
);
460 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
461 dc
.SetBackgroundMode(wxTRANSPARENT
);
462 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
465 // ----------------------------------------------------------------------------
467 // ----------------------------------------------------------------------------
469 MyModelessDialog::MyModelessDialog(wxWindow
*parent
)
470 : wxDialog(parent
, -1, wxString("Modeless dialog"))
472 (void)new wxButton(this, DIALOGS_MODELESS_BTN
, "Press me");