]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
5f40eb07f98642206ace378f0b7222bd7ba99138
[wxWidgets.git] / samples / dialogs / dialogs.cpp
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
9 // Licence: wxWindows license
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
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
35 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
36
37 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
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
46 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
47 EVT_PAINT(MyCanvas::OnPaint)
48 END_EVENT_TABLE()
49
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)
68 #endif
69 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
70
71 EVT_BUTTON(DIALOGS_MODELESS_BTN, MyFrame::OnButton)
72 END_EVENT_TABLE()
73 MyCanvas *myCanvas = (MyCanvas *) NULL;
74
75 // `Main program' equivalent, creating windows and returning main app frame
76 bool MyApp::OnInit()
77 {
78 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
79 wxConvCurrent = &wxConvLibc;
80 #endif
81
82 m_canvasTextColour = wxColour("BLACK");
83 m_canvasFont = *wxNORMAL_FONT;
84
85 // Create the main frame window
86 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
87
88 // Make a menubar
89 wxMenu *file_menu = new wxMenu;
90
91 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
92
93 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
94 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
95 #endif
96
97 file_menu->AppendSeparator();
98 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
99
100 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
101 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
102
103 #endif
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);
125
126 myCanvas = new MyCanvas(frame);
127 myCanvas->SetBackgroundColour(*wxWHITE);
128
129 frame->Centre(wxBOTH);
130
131 // Show the frame
132 frame->Show(TRUE);
133
134 SetTopWindow(frame);
135
136 return TRUE;
137 }
138
139 // My frame constructor
140 MyFrame::MyFrame(wxWindow *parent,
141 const wxString& title,
142 const wxPoint& pos,
143 const wxSize& size)
144 : wxFrame(parent, -1, title, pos, size)
145 {
146 m_dialog = (MyModelessDialog *)NULL;
147 }
148
149 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
150 {
151 wxColourData data;
152 data.SetChooseFull(TRUE);
153 for (int i = 0; i < 16; i++)
154 {
155 wxColour colour(i*16, i*16, i*16);
156 data.SetCustomColour(i, colour);
157 }
158
159 wxColourDialog *dialog = new wxColourDialog(this, &data);
160 if (dialog->ShowModal() == wxID_OK)
161 {
162 wxColourData retData = dialog->GetColourData();
163 wxColour col = retData.GetColour();
164 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
165 myCanvas->SetBackgroundColour(col);
166 myCanvas->Clear();
167 myCanvas->Refresh();
168 }
169 dialog->Destroy();
170 }
171
172 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
173 {
174 wxFontData data;
175 data.SetInitialFont(wxGetApp().m_canvasFont);
176 data.SetColour(wxGetApp().m_canvasTextColour);
177
178 wxFontDialog *dialog = new wxFontDialog(this, &data);
179 if (dialog->ShowModal() == wxID_OK)
180 {
181 wxFontData retData = dialog->GetFontData();
182 wxGetApp().m_canvasFont = retData.GetChosenFont();
183 wxGetApp().m_canvasTextColour = retData.GetColour();
184 myCanvas->Refresh();
185 }
186 dialog->Destroy();
187 }
188
189 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
190 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
191 {
192 wxColourData data;
193 data.SetChooseFull(TRUE);
194 for (int i = 0; i < 16; i++)
195 {
196 wxColour colour(i*16, i*16, i*16);
197 data.SetCustomColour(i, colour);
198 }
199
200 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
201 if (dialog->ShowModal() == wxID_OK)
202 {
203 wxColourData retData = dialog->GetColourData();
204 wxColour col = retData.GetColour();
205 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
206 myCanvas->SetBackgroundColour(col);
207 myCanvas->Clear();
208 myCanvas->Refresh();
209 }
210 dialog->Destroy();
211 }
212
213 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
214 {
215 wxFontData data;
216 data.SetInitialFont(wxGetApp().m_canvasFont);
217 data.SetColour(wxGetApp().m_canvasTextColour);
218
219 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
220 if (dialog->ShowModal() == wxID_OK)
221 {
222 wxFontData retData = dialog->GetFontData();
223 wxGetApp().m_canvasFont = retData.GetChosenFont();
224 wxGetApp().m_canvasTextColour = retData.GetColour();
225 myCanvas->Refresh();
226 }
227 dialog->Destroy();
228 }
229 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
230
231 void MyFrame::LogDialog(wxCommandEvent& event)
232 {
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 // if we have this wxYield() here, everything breaks under GTK
237 wxYield();
238 wxLogError("Intermediary error handler decided to abort.");
239 wxLogError("The top level caller detected an unrecoverable error.");
240
241 wxLog::FlushActive();
242
243 wxLogMessage("And this is the same dialog but with only one message.");
244 }
245
246 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
247 {
248 wxMessageDialog dialog( this, "This is a message box\nA long, long string to test out the message box properly",
249 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
250
251 dialog.ShowModal();
252 }
253
254 void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
255 {
256 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
257 "Even two rows of text.",
258 "Enter a number:", "Numeric input test",
259 50, 0, 100, this );
260
261 wxString msg;
262 int icon;
263 if ( res == -1 )
264 {
265 msg = "Invalid number entered or dialog cancelled.";
266 icon = wxICON_HAND;
267 }
268 else
269 {
270 msg.Printf(_T("You've entered %lu"), res );
271 icon = wxICON_INFORMATION;
272 }
273
274 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
275 }
276
277 void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
278 {
279 wxString pwd = wxGetPasswordFromUser("Enter password:",
280 "Passowrd entry dialog",
281 "",
282 this);
283 if ( !!pwd )
284 {
285 wxMessageBox(wxString::Format("Your password is '%s'", pwd.c_str()),
286 "Got password", wxOK | wxICON_INFORMATION, this);
287 }
288 }
289
290 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
291 {
292 wxTextEntryDialog dialog(this,
293 "This is a small sample\n"
294 "A long, long string to test out the text entrybox",
295 "Please enter a string",
296 "Default value",
297 wxOK | wxCANCEL);
298
299 if (dialog.ShowModal() == wxID_OK)
300 {
301 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
302 dialog2.ShowModal();
303 }
304 }
305
306 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
307 {
308 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
309 int n = 5;
310
311 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
312 "Please select a value", n, (const wxString *)choices);
313
314 dialog.SetSelection(2);
315
316 if (dialog.ShowModal() == wxID_OK)
317 {
318 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
319 dialog2.ShowModal();
320 }
321 }
322
323 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
324 {
325 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
326
327 if (dialog.ShowModal() == wxID_OK)
328 {
329 wxString info;
330 info.Printf(_T("Full file name: %s\n")
331 _T("Path: %s\n")
332 _T("Name: %s"),
333 dialog.GetPath().c_str(),
334 dialog.GetDirectory().c_str(),
335 dialog.GetFilename().c_str());
336 wxMessageDialog dialog2(this, info, "Selected file");
337 dialog2.ShowModal();
338 }
339 }
340
341 void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
342 {
343 wxFileDialog dialog(this, "Testing open multiple file dialog",
344 "", "", wxFileSelectorDefaultWildcardStr,
345 wxMULTIPLE);
346
347 if (dialog.ShowModal() == wxID_OK)
348 {
349 wxArrayString paths, filenames;
350
351 dialog.GetPaths(paths);
352 dialog.GetFilenames(filenames);
353
354 wxString msg, s;
355 size_t count = paths.GetCount();
356 for ( size_t n = 0; n < count; n++ )
357 {
358 s.Printf(_T("File %d: %s (%s)\n"),
359 n, paths[n].c_str(), filenames[n].c_str());
360
361 msg += s;
362 }
363
364 wxMessageDialog dialog2(this, msg, "Selected files");
365 dialog2.ShowModal();
366 }
367 }
368
369 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
370 {
371 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
372 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
373 wxSAVE|wxOVERWRITE_PROMPT);
374
375 if (dialog.ShowModal() == wxID_OK)
376 {
377 wxChar buf[400];
378 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
379 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
380 dialog2.ShowModal();
381 }
382 }
383
384 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
385 {
386 // pass some initial dir to wxDirDialog
387 wxString dirHome;
388 wxGetHomeDir(&dirHome);
389
390 wxDirDialog dialog(this, "Testing directory picker", dirHome);
391
392 if (dialog.ShowModal() == wxID_OK)
393 {
394 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
395 dialog2.ShowModal();
396 }
397 }
398
399 void MyFrame::ModelessDlg(wxCommandEvent& event)
400 {
401 bool show = GetMenuBar()->IsChecked(event.GetInt());
402
403 if ( show )
404 {
405 if ( !m_dialog )
406 {
407 m_dialog = new MyModelessDialog(this);
408 }
409
410 m_dialog->Show(TRUE);
411 }
412 else // hide
413 {
414 m_dialog->Hide();
415 }
416 }
417
418 void MyFrame::OnButton(wxCommandEvent& WXUNUSED(event))
419 {
420 wxMessageBox("Button pressed in modeless dialog", "Info",
421 wxOK | wxICON_INFORMATION, this);
422 }
423
424 void MyFrame::ShowTip(wxCommandEvent& event)
425 {
426 #if wxUSE_STARTUP_TIPS
427 static size_t s_index = (size_t)-1;
428
429 if ( s_index == (size_t)-1 )
430 {
431 srand(time(NULL));
432
433 // this is completely bogus, we don't know how many lines are there
434 // in the file, but who cares, it's a demo only...
435 s_index = rand() % 5;
436 }
437
438 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
439
440 bool showAtStartup = wxShowTip(this, tipProvider);
441
442 if ( showAtStartup )
443 {
444 wxMessageBox("Will show tips on startup", "Tips dialog",
445 wxOK | wxICON_INFORMATION, this);
446 }
447
448 s_index = tipProvider->GetCurrentTip();
449 delete tipProvider;
450 #endif
451 }
452
453 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
454 {
455 Close(TRUE);
456 }
457
458 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
459 {
460 wxPaintDC dc(this);
461 dc.SetFont(wxGetApp().m_canvasFont);
462 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
463 dc.SetBackgroundMode(wxTRANSPARENT);
464 dc.DrawText("wxWindows common dialogs test application", 10, 10);
465 }
466
467 // ----------------------------------------------------------------------------
468 // MyModelessDialog
469 // ----------------------------------------------------------------------------
470
471 MyModelessDialog::MyModelessDialog(wxWindow *parent)
472 : wxDialog(parent, -1, wxString("Modeless dialog"))
473 {
474 (void)new wxButton(this, DIALOGS_MODELESS_BTN, "Press me");
475 Fit();
476 Centre();
477 }