]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
0d922d4aa26782a9625c82c8c0dc27c56b4f62ef
[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 MyCanvas *myCanvas = (MyCanvas *) NULL;
47
48 // `Main program' equivalent, creating windows and returning main app frame
49 bool MyApp::OnInit(void)
50 {
51 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
52 wxConvCurrent = &wxConvLibc;
53 #endif
54
55 m_canvasTextColour = wxColour("BLACK");
56 m_canvasFont = *wxNORMAL_FONT;
57
58 // Create the main frame window
59 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
60
61 // Make a menubar
62 wxMenu *file_menu = new wxMenu;
63
64 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
65
66 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
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
73 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
74 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
75
76 #endif
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);
96
97 myCanvas = new MyCanvas(frame);
98 myCanvas->SetBackgroundColour(*wxWHITE);
99
100 frame->Centre(wxBOTH);
101
102 // Show the frame
103 frame->Show(TRUE);
104
105 SetTopWindow(frame);
106
107 return TRUE;
108 }
109
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)
113 {}
114
115 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
116 {
117 wxColourData data;
118 data.SetChooseFull(TRUE);
119 for (int i = 0; i < 16; i++)
120 {
121 wxColour colour(i*16, i*16, i*16);
122 data.SetCustomColour(i, colour);
123 }
124
125 wxColourDialog *dialog = new wxColourDialog(this, &data);
126 if (dialog->ShowModal() == wxID_OK)
127 {
128 wxColourData retData = dialog->GetColourData();
129 wxColour col = retData.GetColour();
130 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
131 myCanvas->SetBackgroundColour(col);
132 myCanvas->Clear();
133 myCanvas->Refresh();
134 }
135 dialog->Destroy();
136 }
137
138 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
139 {
140 wxFontData data;
141 data.SetInitialFont(wxGetApp().m_canvasFont);
142 data.SetColour(wxGetApp().m_canvasTextColour);
143
144 wxFontDialog *dialog = new wxFontDialog(this, &data);
145 if (dialog->ShowModal() == wxID_OK)
146 {
147 wxFontData retData = dialog->GetFontData();
148 wxGetApp().m_canvasFont = retData.GetChosenFont();
149 wxGetApp().m_canvasTextColour = retData.GetColour();
150 myCanvas->Refresh();
151 }
152 dialog->Destroy();
153 }
154
155 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
156 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
157 {
158 wxColourData data;
159 data.SetChooseFull(TRUE);
160 for (int i = 0; i < 16; i++)
161 {
162 wxColour colour(i*16, i*16, i*16);
163 data.SetCustomColour(i, colour);
164 }
165
166 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
167 if (dialog->ShowModal() == wxID_OK)
168 {
169 wxColourData retData = dialog->GetColourData();
170 wxColour col = retData.GetColour();
171 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
172 myCanvas->SetBackgroundColour(col);
173 myCanvas->Clear();
174 myCanvas->Refresh();
175 }
176 dialog->Destroy();
177 }
178
179 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
180 {
181 wxFontData data;
182 data.SetInitialFont(wxGetApp().m_canvasFont);
183 data.SetColour(wxGetApp().m_canvasTextColour);
184
185 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
186 if (dialog->ShowModal() == wxID_OK)
187 {
188 wxFontData retData = dialog->GetFontData();
189 wxGetApp().m_canvasFont = retData.GetChosenFont();
190 wxGetApp().m_canvasTextColour = retData.GetColour();
191 myCanvas->Refresh();
192 }
193 dialog->Destroy();
194 }
195 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
196
197 void MyFrame::LogDialog(wxCommandEvent& event)
198 {
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.");
204
205 wxLog::FlushActive();
206
207 wxLogMessage("And this is the same dialog but with onle one message");
208 }
209
210 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
211 {
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);
214
215 dialog.ShowModal();
216 }
217
218 void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
219 {
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",
223 50, 0, 100, this );
224
225 wxString msg;
226 int icon;
227 if ( res == -1 )
228 {
229 msg = "Invalid number entered or dialog cancelled.";
230 icon = wxICON_HAND;
231 }
232 else
233 {
234 msg.Printf(_T("You've entered %lu"), res );
235 icon = wxICON_INFORMATION;
236 }
237
238 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
239 }
240
241 void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
242 {
243 wxString pwd = wxGetPasswordFromUser("Enter password:",
244 "Passowrd entry dialog",
245 "",
246 this);
247 if ( !!pwd )
248 {
249 wxMessageBox(wxString::Format("Your password is '%s'", pwd.c_str()),
250 "Got password", wxOK | wxICON_INFORMATION, this);
251 }
252 }
253
254 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
255 {
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",
260 "Default value",
261 wxOK | wxCANCEL);
262
263 if (dialog.ShowModal() == wxID_OK)
264 {
265 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
266 dialog2.ShowModal();
267 }
268 }
269
270 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
271 {
272 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
273 int n = 5;
274
275 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
276 "Please select a value", n, (const wxString *)choices);
277
278 dialog.SetSelection(2);
279
280 if (dialog.ShowModal() == wxID_OK)
281 {
282 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
283 dialog2.ShowModal();
284 }
285 }
286
287 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
288 {
289 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
290
291 if (dialog.ShowModal() == wxID_OK)
292 {
293 wxString info;
294 info.Printf(_T("Full file name: %s\n")
295 _T("Path: %s\n")
296 _T("Name: %s"),
297 dialog.GetPath().c_str(),
298 dialog.GetDirectory().c_str(),
299 dialog.GetFilename().c_str());
300 wxMessageDialog dialog2(this, info, "Selected file");
301 dialog2.ShowModal();
302 }
303 }
304
305 void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
306 {
307 wxFileDialog dialog(this, "Testing open multiple file dialog",
308 "", "", wxFileSelectorDefaultWildcardStr,
309 wxMULTIPLE);
310
311 if (dialog.ShowModal() == wxID_OK)
312 {
313 wxArrayString paths, filenames;
314
315 dialog.GetPaths(paths);
316 dialog.GetFilenames(filenames);
317
318 wxString msg, s;
319 size_t count = paths.GetCount();
320 for ( size_t n = 0; n < count; n++ )
321 {
322 s.Printf(_T("File %d: %s (%s)\n"),
323 n, paths[n].c_str(), filenames[n].c_str());
324
325 msg += s;
326 }
327
328 wxMessageDialog dialog2(this, msg, "Selected files");
329 dialog2.ShowModal();
330 }
331 }
332
333 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
334 {
335 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
336 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
337 wxSAVE|wxOVERWRITE_PROMPT);
338
339 if (dialog.ShowModal() == wxID_OK)
340 {
341 wxChar buf[400];
342 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
343 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
344 dialog2.ShowModal();
345 }
346 }
347
348 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
349 {
350 // pass some initial dir to wxDirDialog
351 wxString dirHome;
352 wxGetHomeDir(&dirHome);
353
354 wxDirDialog dialog(this, "Testing directory picker", dirHome);
355
356 if (dialog.ShowModal() == wxID_OK)
357 {
358 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
359 dialog2.ShowModal();
360 }
361 }
362
363 void MyFrame::ShowTip(wxCommandEvent& event)
364 {
365 #if wxUSE_STARTUP_TIPS
366 static size_t s_index = (size_t)-1;
367
368 if ( s_index == (size_t)-1 )
369 {
370 srand(time(NULL));
371
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;
375 }
376
377 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
378
379 bool showAtStartup = wxShowTip(this, tipProvider);
380
381 if ( showAtStartup )
382 {
383 wxMessageBox("Will show tips on startup", "Tips dialog",
384 wxOK | wxICON_INFORMATION, this);
385 }
386
387 s_index = tipProvider->GetCurrentTip();
388 delete tipProvider;
389 #endif
390 }
391
392 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
393 {
394 Close(TRUE);
395 }
396
397 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
398 {
399 wxPaintDC dc(this);
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);
404 }
405
406 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
407 EVT_PAINT(MyCanvas::OnPaint)
408 END_EVENT_TABLE()
409
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)
427 #endif
428 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
429 END_EVENT_TABLE()
430