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 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
48 // `Main program' equivalent, creating windows and returning main app frame
49 bool MyApp::OnInit(void)
51 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
52 wxConvCurrent
= &wxConvLibc
;
55 m_canvasTextColour
= wxColour("BLACK");
56 m_canvasFont
= *wxNORMAL_FONT
;
58 // Create the main frame window
59 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
62 wxMenu
*file_menu
= new wxMenu
;
64 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
66 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
67 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
70 file_menu
->AppendSeparator();
71 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
73 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
74 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
77 file_menu
->AppendSeparator();
78 file_menu
->Append(DIALOGS_LOG_DIALOG
, "&Log dialog\tCtrl-L");
79 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box\tCtrl-M");
80 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
81 file_menu
->Append(DIALOGS_PASSWORD_ENTRY
, "&Password entry\tCtrl-P");
82 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
83 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-S");
84 file_menu
->AppendSeparator();
85 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
86 file_menu
->AppendSeparator();
87 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
88 file_menu
->Append(DIALOGS_FILES_OPEN
, "Open &files\tCtrl-Q");
89 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file");
90 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
91 file_menu
->AppendSeparator();
92 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
93 wxMenuBar
*menu_bar
= new wxMenuBar
;
94 menu_bar
->Append(file_menu
, "&File");
95 frame
->SetMenuBar(menu_bar
);
97 myCanvas
= new MyCanvas(frame
);
98 myCanvas
->SetBackgroundColour(*wxWHITE
);
100 frame
->Centre(wxBOTH
);
110 // My frame constructor
111 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
112 wxFrame(parent
, -1, title
, pos
, size
)
115 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
118 data
.SetChooseFull(TRUE
);
119 for (int i
= 0; i
< 16; i
++)
121 wxColour
colour(i
*16, i
*16, i
*16);
122 data
.SetCustomColour(i
, colour
);
125 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
126 if (dialog
->ShowModal() == wxID_OK
)
128 wxColourData retData
= dialog
->GetColourData();
129 wxColour col
= retData
.GetColour();
130 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
131 myCanvas
->SetBackgroundColour(col
);
138 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
141 data
.SetInitialFont(wxGetApp().m_canvasFont
);
142 data
.SetColour(wxGetApp().m_canvasTextColour
);
144 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
145 if (dialog
->ShowModal() == wxID_OK
)
147 wxFontData retData
= dialog
->GetFontData();
148 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
149 wxGetApp().m_canvasTextColour
= retData
.GetColour();
155 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
156 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
159 data
.SetChooseFull(TRUE
);
160 for (int i
= 0; i
< 16; i
++)
162 wxColour
colour(i
*16, i
*16, i
*16);
163 data
.SetCustomColour(i
, colour
);
166 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
167 if (dialog
->ShowModal() == wxID_OK
)
169 wxColourData retData
= dialog
->GetColourData();
170 wxColour col
= retData
.GetColour();
171 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
172 myCanvas
->SetBackgroundColour(col
);
179 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
182 data
.SetInitialFont(wxGetApp().m_canvasFont
);
183 data
.SetColour(wxGetApp().m_canvasTextColour
);
185 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
186 if (dialog
->ShowModal() == wxID_OK
)
188 wxFontData retData
= dialog
->GetFontData();
189 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
190 wxGetApp().m_canvasTextColour
= retData
.GetColour();
195 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
197 void MyFrame::LogDialog(wxCommandEvent
& event
)
199 wxLogMessage("This is some message - everything is ok so far.");
200 wxLogMessage("Another message...");
201 wxLogWarning("And then something went wrong!");
202 wxLogError("Intermediary error handler decided to abort.");
203 wxLogError("The top level caller detected an error.");
205 wxLog::FlushActive();
207 wxLogMessage("And this is the same dialog but with onle one message");
210 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
212 wxMessageDialog
dialog( this, "This is a message box\nA long, long string to test out the message box properly",
213 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
218 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
220 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
221 "Even two rows of text.",
222 "Enter a number:", "Numeric input test",
229 msg
= "Invalid number entered or dialog cancelled.";
234 msg
.Printf(_T("You've entered %lu"), res
);
235 icon
= wxICON_INFORMATION
;
238 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
241 void MyFrame::PasswordEntry(wxCommandEvent
& WXUNUSED(event
))
243 wxString pwd
= wxGetPasswordFromUser("Enter password:",
244 "Passowrd entry dialog",
249 wxMessageBox(wxString::Format("Your password is '%s'", pwd
.c_str()),
250 "Got password", wxOK
| wxICON_INFORMATION
, this);
254 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
))
256 wxTextEntryDialog
dialog(this,
257 "This is a small sample\n"
258 "A long, long string to test out the text entrybox",
259 "Please enter a string",
263 if (dialog
.ShowModal() == wxID_OK
)
265 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
270 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
272 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
275 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
276 "Please select a value", n
, (const wxString
*)choices
);
278 dialog
.SetSelection(2);
280 if (dialog
.ShowModal() == wxID_OK
)
282 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
287 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
289 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
291 if (dialog
.ShowModal() == wxID_OK
)
294 info
.Printf(_T("Full file name: %s\n")
297 dialog
.GetPath().c_str(),
298 dialog
.GetDirectory().c_str(),
299 dialog
.GetFilename().c_str());
300 wxMessageDialog
dialog2(this, info
, "Selected file");
305 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
307 wxFileDialog
dialog(this, "Testing open multiple file dialog",
308 "", "", wxFileSelectorDefaultWildcardStr
,
311 if (dialog
.ShowModal() == wxID_OK
)
313 wxArrayString paths
, filenames
;
315 dialog
.GetPaths(paths
);
316 dialog
.GetFilenames(filenames
);
319 size_t count
= paths
.GetCount();
320 for ( size_t n
= 0; n
< count
; n
++ )
322 s
.Printf(_T("File %d: %s (%s)\n"),
323 n
, paths
[n
].c_str(), filenames
[n
].c_str());
328 wxMessageDialog
dialog2(this, msg
, "Selected files");
333 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
335 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
336 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
337 wxSAVE
|wxOVERWRITE_PROMPT
);
339 if (dialog
.ShowModal() == wxID_OK
)
342 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
343 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
348 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
350 // pass some initial dir to wxDirDialog
352 wxGetHomeDir(&dirHome
);
354 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
356 if (dialog
.ShowModal() == wxID_OK
)
358 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
363 void MyFrame::ShowTip(wxCommandEvent
& event
)
365 #if wxUSE_STARTUP_TIPS
366 static size_t s_index
= (size_t)-1;
368 if ( s_index
== (size_t)-1 )
372 // this is completely bogus, we don't know how many lines are there
373 // in the file, but who cares, it's a demo only...
374 s_index
= rand() % 5;
377 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
379 bool showAtStartup
= wxShowTip(this, tipProvider
);
383 wxMessageBox("Will show tips on startup", "Tips dialog",
384 wxOK
| wxICON_INFORMATION
, this);
387 s_index
= tipProvider
->GetCurrentTip();
392 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
397 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
400 dc
.SetFont(wxGetApp().m_canvasFont
);
401 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
402 dc
.SetBackgroundMode(wxTRANSPARENT
);
403 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
406 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
407 EVT_PAINT(MyCanvas::OnPaint
)
410 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
411 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
412 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
413 EVT_MENU(DIALOGS_LOG_DIALOG
, MyFrame::LogDialog
)
414 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
415 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
416 EVT_MENU(DIALOGS_PASSWORD_ENTRY
, MyFrame::PasswordEntry
)
417 EVT_MENU(DIALOGS_NUM_ENTRY
, MyFrame::NumericEntry
)
418 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
419 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
420 EVT_MENU(DIALOGS_FILES_OPEN
, MyFrame::FilesOpen
)
421 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
422 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
423 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
424 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
425 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
426 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
428 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)