]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dialogs.cpp | |
3 | // Purpose: Common dialogs demo | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c50f1fb9 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
329e86bf RR |
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" | |
457814b5 | 34 | |
dfad0599 JS |
35 | #define wxTEST_GENERIC_DIALOGS_IN_MSW 0 |
36 | ||
37 | #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW | |
457814b5 JS |
38 | #include <wx/generic/colrdlgg.h> |
39 | #include <wx/generic/fontdlgg.h> | |
40 | #endif | |
41 | ||
42 | #include "dialogs.h" | |
43 | ||
44 | IMPLEMENT_APP(MyApp) | |
45 | ||
c67daf87 | 46 | MyCanvas *myCanvas = (MyCanvas *) NULL; |
457814b5 | 47 | |
457814b5 JS |
48 | // `Main program' equivalent, creating windows and returning main app frame |
49 | bool MyApp::OnInit(void) | |
50 | { | |
e5ea3f7a | 51 | #if defined(__WXGTK__) && defined(wxUSE_UNICODE) |
dcf924a3 | 52 | wxConvCurrent = &wxConvLibc; |
e5ea3f7a RR |
53 | #endif |
54 | ||
457814b5 | 55 | m_canvasTextColour = wxColour("BLACK"); |
66bd6b93 | 56 | m_canvasFont = *wxNORMAL_FONT; |
457814b5 JS |
57 | |
58 | // Create the main frame window | |
c67daf87 | 59 | MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300)); |
457814b5 JS |
60 | |
61 | // Make a menubar | |
62 | wxMenu *file_menu = new wxMenu; | |
63 | ||
64 | file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour"); | |
65 | ||
dfad0599 | 66 | #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW |
457814b5 JS |
67 | file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)"); |
68 | #endif | |
69 | ||
70 | file_menu->AppendSeparator(); | |
71 | file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font"); | |
72 | ||
dfad0599 | 73 | #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW |
457814b5 JS |
74 | file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)"); |
75 | ||
76 | #endif | |
77 | file_menu->AppendSeparator(); | |
78 | file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box"); | |
79 | file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry"); | |
c49245f8 | 80 | file_menu->Append(DIALOGS_NUM_ENTRY, "&Numeric entry\tCtrl-N"); |
457814b5 JS |
81 | file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice"); |
82 | file_menu->AppendSeparator(); | |
c50f1fb9 VZ |
83 | file_menu->Append(DIALOGS_TIP, "&Tip of the day"); |
84 | file_menu->AppendSeparator(); | |
457814b5 JS |
85 | file_menu->Append(DIALOGS_FILE_OPEN, "&Open file"); |
86 | file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file"); | |
87 | file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory"); | |
88 | file_menu->AppendSeparator(); | |
89 | file_menu->Append(wxID_EXIT, "E&xit"); | |
90 | wxMenuBar *menu_bar = new wxMenuBar; | |
91 | menu_bar->Append(file_menu, "&File"); | |
92 | frame->SetMenuBar(menu_bar); | |
93 | ||
94 | myCanvas = new MyCanvas(frame); | |
95 | myCanvas->SetBackgroundColour(*wxWHITE); | |
96 | ||
97 | frame->Centre(wxBOTH); | |
98 | ||
99 | // Show the frame | |
100 | frame->Show(TRUE); | |
101 | ||
102 | SetTopWindow(frame); | |
103 | ||
104 | return TRUE; | |
105 | } | |
106 | ||
107 | // My frame constructor | |
108 | MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size): | |
109 | wxFrame(parent, -1, title, pos, size) | |
110 | {} | |
111 | ||
d355d3fe | 112 | void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) ) |
457814b5 JS |
113 | { |
114 | wxColourData data; | |
115 | data.SetChooseFull(TRUE); | |
116 | for (int i = 0; i < 16; i++) | |
117 | { | |
118 | wxColour colour(i*16, i*16, i*16); | |
119 | data.SetCustomColour(i, colour); | |
120 | } | |
121 | ||
122 | wxColourDialog *dialog = new wxColourDialog(this, &data); | |
123 | if (dialog->ShowModal() == wxID_OK) | |
124 | { | |
125 | wxColourData retData = dialog->GetColourData(); | |
126 | wxColour col = retData.GetColour(); | |
127 | // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID); | |
128 | myCanvas->SetBackgroundColour(col); | |
129 | myCanvas->Clear(); | |
130 | myCanvas->Refresh(); | |
131 | } | |
5919d76b | 132 | dialog->Destroy(); |
457814b5 JS |
133 | } |
134 | ||
d355d3fe | 135 | void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) ) |
457814b5 JS |
136 | { |
137 | wxFontData data; | |
138 | data.SetInitialFont(wxGetApp().m_canvasFont); | |
139 | data.SetColour(wxGetApp().m_canvasTextColour); | |
140 | ||
141 | wxFontDialog *dialog = new wxFontDialog(this, &data); | |
142 | if (dialog->ShowModal() == wxID_OK) | |
143 | { | |
144 | wxFontData retData = dialog->GetFontData(); | |
145 | wxGetApp().m_canvasFont = retData.GetChosenFont(); | |
146 | wxGetApp().m_canvasTextColour = retData.GetColour(); | |
147 | myCanvas->Refresh(); | |
148 | } | |
5919d76b | 149 | dialog->Destroy(); |
457814b5 JS |
150 | } |
151 | ||
dfad0599 | 152 | #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW |
d355d3fe | 153 | void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event)) |
457814b5 JS |
154 | { |
155 | wxColourData data; | |
156 | data.SetChooseFull(TRUE); | |
157 | for (int i = 0; i < 16; i++) | |
158 | { | |
159 | wxColour colour(i*16, i*16, i*16); | |
160 | data.SetCustomColour(i, colour); | |
161 | } | |
162 | ||
163 | wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data); | |
164 | if (dialog->ShowModal() == wxID_OK) | |
165 | { | |
166 | wxColourData retData = dialog->GetColourData(); | |
167 | wxColour col = retData.GetColour(); | |
168 | // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID); | |
169 | myCanvas->SetBackgroundColour(col); | |
170 | myCanvas->Clear(); | |
171 | myCanvas->Refresh(); | |
172 | } | |
5919d76b | 173 | dialog->Destroy(); |
457814b5 JS |
174 | } |
175 | ||
d355d3fe | 176 | void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) ) |
457814b5 JS |
177 | { |
178 | wxFontData data; | |
179 | data.SetInitialFont(wxGetApp().m_canvasFont); | |
180 | data.SetColour(wxGetApp().m_canvasTextColour); | |
181 | ||
182 | wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data); | |
183 | if (dialog->ShowModal() == wxID_OK) | |
184 | { | |
185 | wxFontData retData = dialog->GetFontData(); | |
186 | wxGetApp().m_canvasFont = retData.GetChosenFont(); | |
187 | wxGetApp().m_canvasTextColour = retData.GetColour(); | |
188 | myCanvas->Refresh(); | |
189 | } | |
5919d76b | 190 | dialog->Destroy(); |
457814b5 JS |
191 | } |
192 | #endif | |
193 | ||
d355d3fe | 194 | void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) ) |
457814b5 | 195 | { |
8e360a08 | 196 | wxMessageDialog dialog(NULL, "This is a message box\nA long, long string to test out the message box properly", |
c50f1fb9 | 197 | "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION); |
457814b5 JS |
198 | |
199 | dialog.ShowModal(); | |
200 | } | |
201 | ||
c49245f8 VZ |
202 | void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) ) |
203 | { | |
e65cc56a RR |
204 | long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n" |
205 | "Even two rows of text.", | |
206 | "Enter a number:", "Numeric input test", | |
207 | 50, 0, 100, this ); | |
c49245f8 VZ |
208 | |
209 | wxString msg; | |
210 | int icon; | |
211 | if ( res == -1 ) | |
212 | { | |
213 | msg = "Invalid number entered or dialog cancelled."; | |
214 | icon = wxICON_HAND; | |
215 | } | |
216 | else | |
fb39c7ec RR |
217 | { |
218 | msg.Printf(_T("You've entered %lu"), res ); | |
c49245f8 VZ |
219 | icon = wxICON_INFORMATION; |
220 | } | |
221 | ||
222 | wxMessageBox(msg, "Numeric test result", wxOK | icon, this); | |
223 | } | |
224 | ||
d355d3fe | 225 | void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) ) |
457814b5 JS |
226 | { |
227 | wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox", | |
c50f1fb9 | 228 | "Please enter a string", "Default value", wxOK|wxCANCEL); |
457814b5 JS |
229 | |
230 | if (dialog.ShowModal() == wxID_OK) | |
231 | { | |
c50f1fb9 VZ |
232 | wxMessageDialog dialog2(this, dialog.GetValue(), "Got string"); |
233 | dialog2.ShowModal(); | |
457814b5 JS |
234 | } |
235 | } | |
236 | ||
d355d3fe | 237 | void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) ) |
457814b5 | 238 | { |
ef77f91e JS |
239 | const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ; |
240 | int n = 5; | |
457814b5 | 241 | |
ef77f91e JS |
242 | wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog", |
243 | "Please select a value", n, (const wxString *)choices); | |
457814b5 | 244 | |
ef77f91e JS |
245 | dialog.SetSelection(2); |
246 | ||
247 | if (dialog.ShowModal() == wxID_OK) | |
248 | { | |
249 | wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string"); | |
250 | dialog2.ShowModal(); | |
251 | } | |
457814b5 JS |
252 | } |
253 | ||
d355d3fe | 254 | void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) ) |
457814b5 | 255 | { |
c50f1fb9 | 256 | wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0); |
457814b5 | 257 | |
c50f1fb9 VZ |
258 | if (dialog.ShowModal() == wxID_OK) |
259 | { | |
5919d76b | 260 | wxString info; |
13393ab6 RR |
261 | info.Printf(_T("Full file name: %s\n") |
262 | _T("Path: %s\n") | |
263 | _T("Name: %s"), | |
5919d76b VZ |
264 | dialog.GetPath().c_str(), |
265 | dialog.GetDirectory().c_str(), | |
266 | dialog.GetFilename().c_str()); | |
c50f1fb9 VZ |
267 | wxMessageDialog dialog2(this, info, "Selected file"); |
268 | dialog2.ShowModal(); | |
269 | } | |
457814b5 JS |
270 | } |
271 | ||
d355d3fe | 272 | void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) ) |
457814b5 | 273 | { |
ed58dbea | 274 | wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt", |
c50f1fb9 VZ |
275 | "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc", |
276 | wxSAVE|wxOVERWRITE_PROMPT); | |
277 | ||
278 | if (dialog.ShowModal() == wxID_OK) | |
279 | { | |
280 | wxChar buf[400]; | |
281 | wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex()); | |
282 | wxMessageDialog dialog2(this, wxString(buf), "Selected path"); | |
283 | dialog2.ShowModal(); | |
284 | } | |
457814b5 JS |
285 | } |
286 | ||
d355d3fe | 287 | void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) ) |
457814b5 | 288 | { |
c50f1fb9 VZ |
289 | wxDirDialog dialog(this, "Testing directory picker", ""); |
290 | ||
291 | if (dialog.ShowModal() == wxID_OK) | |
292 | { | |
293 | wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path"); | |
294 | dialog2.ShowModal(); | |
295 | } | |
296 | } | |
297 | ||
298 | void MyFrame::ShowTip(wxCommandEvent& event) | |
299 | { | |
78bd7ed3 | 300 | #if wxUSE_STARTUP_TIPS |
c50f1fb9 VZ |
301 | static size_t s_index = (size_t)-1; |
302 | ||
303 | if ( s_index == (size_t)-1 ) | |
304 | { | |
305 | srand(time(NULL)); | |
306 | ||
307 | // this is completely bogus, we don't know how many lines are there | |
308 | // in the file, but who cares, it's a demo only... | |
309 | s_index = rand() % 5; | |
310 | } | |
311 | ||
312 | wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index); | |
313 | ||
314 | bool showAtStartup = wxShowTip(this, tipProvider); | |
315 | ||
316 | if ( showAtStartup ) | |
317 | { | |
318 | wxMessageBox("Will show tips on startup", "Tips dialog", | |
319 | wxOK | wxICON_INFORMATION, this); | |
320 | } | |
457814b5 | 321 | |
c50f1fb9 VZ |
322 | s_index = tipProvider->GetCurrentTip(); |
323 | delete tipProvider; | |
78bd7ed3 | 324 | #endif |
457814b5 JS |
325 | } |
326 | ||
d355d3fe | 327 | void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) ) |
457814b5 | 328 | { |
c50f1fb9 | 329 | Close(TRUE); |
457814b5 JS |
330 | } |
331 | ||
d355d3fe | 332 | void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) ) |
457814b5 | 333 | { |
c50f1fb9 | 334 | wxPaintDC dc(this); |
457814b5 JS |
335 | dc.SetFont(wxGetApp().m_canvasFont); |
336 | dc.SetTextForeground(wxGetApp().m_canvasTextColour); | |
337 | dc.SetBackgroundMode(wxTRANSPARENT); | |
338 | dc.DrawText("wxWindows common dialogs test application", 10, 10); | |
339 | } | |
340 | ||
341 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
c50f1fb9 | 342 | EVT_PAINT(MyCanvas::OnPaint) |
457814b5 JS |
343 | END_EVENT_TABLE() |
344 | ||
345 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
c50f1fb9 VZ |
346 | EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour) |
347 | EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont) | |
348 | EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox) | |
349 | EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry) | |
c49245f8 | 350 | EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry) |
c50f1fb9 VZ |
351 | EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice) |
352 | EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen) | |
353 | EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave) | |
354 | EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose) | |
355 | EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip) | |
dfad0599 | 356 | #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW |
c50f1fb9 VZ |
357 | EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric) |
358 | EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric) | |
457814b5 | 359 | #endif |
c50f1fb9 | 360 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) |
457814b5 JS |
361 | END_EVENT_TABLE() |
362 |