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_MESSAGE_BOX
, "&Message box\tCtrl-M");
79 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry\tCtrl-E");
80 file_menu
->Append(DIALOGS_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
81 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice\tCtrl-S");
82 file_menu
->AppendSeparator();
83 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day\tCtrl-T");
84 file_menu
->AppendSeparator();
85 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file\tCtrl-O");
86 file_menu
->Append(DIALOGS_FILES_OPEN
, "&Open files\tCtrl-Q");
87 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file");
88 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory\tCtrl-D");
89 file_menu
->AppendSeparator();
90 file_menu
->Append(wxID_EXIT
, "E&xit\tAlt-X");
91 wxMenuBar
*menu_bar
= new wxMenuBar
;
92 menu_bar
->Append(file_menu
, "&File");
93 frame
->SetMenuBar(menu_bar
);
95 myCanvas
= new MyCanvas(frame
);
96 myCanvas
->SetBackgroundColour(*wxWHITE
);
98 frame
->Centre(wxBOTH
);
108 // My frame constructor
109 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
110 wxFrame(parent
, -1, title
, pos
, size
)
113 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
116 data
.SetChooseFull(TRUE
);
117 for (int i
= 0; i
< 16; i
++)
119 wxColour
colour(i
*16, i
*16, i
*16);
120 data
.SetCustomColour(i
, colour
);
123 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
124 if (dialog
->ShowModal() == wxID_OK
)
126 wxColourData retData
= dialog
->GetColourData();
127 wxColour col
= retData
.GetColour();
128 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
129 myCanvas
->SetBackgroundColour(col
);
136 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
139 data
.SetInitialFont(wxGetApp().m_canvasFont
);
140 data
.SetColour(wxGetApp().m_canvasTextColour
);
142 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
143 if (dialog
->ShowModal() == wxID_OK
)
145 wxFontData retData
= dialog
->GetFontData();
146 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
147 wxGetApp().m_canvasTextColour
= retData
.GetColour();
153 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
154 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
157 data
.SetChooseFull(TRUE
);
158 for (int i
= 0; i
< 16; i
++)
160 wxColour
colour(i
*16, i
*16, i
*16);
161 data
.SetCustomColour(i
, colour
);
164 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
165 if (dialog
->ShowModal() == wxID_OK
)
167 wxColourData retData
= dialog
->GetColourData();
168 wxColour col
= retData
.GetColour();
169 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
170 myCanvas
->SetBackgroundColour(col
);
177 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
180 data
.SetInitialFont(wxGetApp().m_canvasFont
);
181 data
.SetColour(wxGetApp().m_canvasTextColour
);
183 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
184 if (dialog
->ShowModal() == wxID_OK
)
186 wxFontData retData
= dialog
->GetFontData();
187 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
188 wxGetApp().m_canvasTextColour
= retData
.GetColour();
195 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
197 wxMessageDialog
dialog( this, "This is a message box\nA long, long string to test out the message box properly",
198 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
203 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
205 long res
= wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
206 "Even two rows of text.",
207 "Enter a number:", "Numeric input test",
214 msg
= "Invalid number entered or dialog cancelled.";
219 msg
.Printf(_T("You've entered %lu"), res
);
220 icon
= wxICON_INFORMATION
;
223 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
226 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
) )
228 wxTextEntryDialog
dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
229 "Please enter a string", "Default value", wxOK
|wxCANCEL
);
231 if (dialog
.ShowModal() == wxID_OK
)
233 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
238 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
240 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
243 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
244 "Please select a value", n
, (const wxString
*)choices
);
246 dialog
.SetSelection(2);
248 if (dialog
.ShowModal() == wxID_OK
)
250 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
255 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
257 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
259 if (dialog
.ShowModal() == wxID_OK
)
262 info
.Printf(_T("Full file name: %s\n")
265 dialog
.GetPath().c_str(),
266 dialog
.GetDirectory().c_str(),
267 dialog
.GetFilename().c_str());
268 wxMessageDialog
dialog2(this, info
, "Selected file");
273 void MyFrame::FilesOpen(wxCommandEvent
& WXUNUSED(event
) )
275 wxFileDialog
dialog(this, "Testing open multiple file dialog",
276 "", "", "*.*", wxMULTIPLE
);
278 if (dialog
.ShowModal() == wxID_OK
)
280 wxArrayString paths
, filenames
;
282 dialog
.GetPaths(paths
);
283 dialog
.GetFilenames(filenames
);
286 size_t count
= paths
.GetCount();
287 for ( size_t n
= 0; n
< count
; n
++ )
289 s
.Printf(_T("File %d: %s (%s)\n"),
290 n
, paths
[n
].c_str(), filenames
[n
].c_str());
295 wxMessageDialog
dialog2(this, msg
, "Selected files");
300 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
302 wxFileDialog
dialog(this, "Testing save file dialog", "", "myletter.txt",
303 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
304 wxSAVE
|wxOVERWRITE_PROMPT
);
306 if (dialog
.ShowModal() == wxID_OK
)
309 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
310 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
315 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
317 // pass some initial dir to wxDirDialog
319 wxGetHomeDir(&dirHome
);
321 wxDirDialog
dialog(this, "Testing directory picker", dirHome
);
323 if (dialog
.ShowModal() == wxID_OK
)
325 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
330 void MyFrame::ShowTip(wxCommandEvent
& event
)
332 #if wxUSE_STARTUP_TIPS
333 static size_t s_index
= (size_t)-1;
335 if ( s_index
== (size_t)-1 )
339 // this is completely bogus, we don't know how many lines are there
340 // in the file, but who cares, it's a demo only...
341 s_index
= rand() % 5;
344 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
346 bool showAtStartup
= wxShowTip(this, tipProvider
);
350 wxMessageBox("Will show tips on startup", "Tips dialog",
351 wxOK
| wxICON_INFORMATION
, this);
354 s_index
= tipProvider
->GetCurrentTip();
359 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
364 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
367 dc
.SetFont(wxGetApp().m_canvasFont
);
368 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
369 dc
.SetBackgroundMode(wxTRANSPARENT
);
370 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
373 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
374 EVT_PAINT(MyCanvas::OnPaint
)
377 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
378 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
379 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
380 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
381 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
382 EVT_MENU(DIALOGS_NUM_ENTRY
, MyFrame::NumericEntry
)
383 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
384 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
385 EVT_MENU(DIALOGS_FILES_OPEN
, MyFrame::FilesOpen
)
386 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
387 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
388 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
389 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
390 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
391 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
393 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)