]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
moved bookmarks to Contents panel
[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_NUM_ENTRY, "&Numeric entry\tCtrl-N");
82 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice\tCtrl-S");
83 file_menu->AppendSeparator();
84 file_menu->Append(DIALOGS_TIP, "&Tip of the day\tCtrl-T");
85 file_menu->AppendSeparator();
86 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file\tCtrl-O");
87 file_menu->Append(DIALOGS_FILES_OPEN, "&Open files\tCtrl-Q");
88 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file");
89 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory\tCtrl-D");
90 file_menu->AppendSeparator();
91 file_menu->Append(wxID_EXIT, "E&xit\tAlt-X");
92 wxMenuBar *menu_bar = new wxMenuBar;
93 menu_bar->Append(file_menu, "&File");
94 frame->SetMenuBar(menu_bar);
95
96 myCanvas = new MyCanvas(frame);
97 myCanvas->SetBackgroundColour(*wxWHITE);
98
99 frame->Centre(wxBOTH);
100
101 // Show the frame
102 frame->Show(TRUE);
103
104 SetTopWindow(frame);
105
106 return TRUE;
107 }
108
109 // My frame constructor
110 MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
111 wxFrame(parent, -1, title, pos, size)
112 {}
113
114 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
115 {
116 wxColourData data;
117 data.SetChooseFull(TRUE);
118 for (int i = 0; i < 16; i++)
119 {
120 wxColour colour(i*16, i*16, i*16);
121 data.SetCustomColour(i, colour);
122 }
123
124 wxColourDialog *dialog = new wxColourDialog(this, &data);
125 if (dialog->ShowModal() == wxID_OK)
126 {
127 wxColourData retData = dialog->GetColourData();
128 wxColour col = retData.GetColour();
129 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
130 myCanvas->SetBackgroundColour(col);
131 myCanvas->Clear();
132 myCanvas->Refresh();
133 }
134 dialog->Destroy();
135 }
136
137 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
138 {
139 wxFontData data;
140 data.SetInitialFont(wxGetApp().m_canvasFont);
141 data.SetColour(wxGetApp().m_canvasTextColour);
142
143 wxFontDialog *dialog = new wxFontDialog(this, &data);
144 if (dialog->ShowModal() == wxID_OK)
145 {
146 wxFontData retData = dialog->GetFontData();
147 wxGetApp().m_canvasFont = retData.GetChosenFont();
148 wxGetApp().m_canvasTextColour = retData.GetColour();
149 myCanvas->Refresh();
150 }
151 dialog->Destroy();
152 }
153
154 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
155 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
156 {
157 wxColourData data;
158 data.SetChooseFull(TRUE);
159 for (int i = 0; i < 16; i++)
160 {
161 wxColour colour(i*16, i*16, i*16);
162 data.SetCustomColour(i, colour);
163 }
164
165 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
166 if (dialog->ShowModal() == wxID_OK)
167 {
168 wxColourData retData = dialog->GetColourData();
169 wxColour col = retData.GetColour();
170 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
171 myCanvas->SetBackgroundColour(col);
172 myCanvas->Clear();
173 myCanvas->Refresh();
174 }
175 dialog->Destroy();
176 }
177
178 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
179 {
180 wxFontData data;
181 data.SetInitialFont(wxGetApp().m_canvasFont);
182 data.SetColour(wxGetApp().m_canvasTextColour);
183
184 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
185 if (dialog->ShowModal() == wxID_OK)
186 {
187 wxFontData retData = dialog->GetFontData();
188 wxGetApp().m_canvasFont = retData.GetChosenFont();
189 wxGetApp().m_canvasTextColour = retData.GetColour();
190 myCanvas->Refresh();
191 }
192 dialog->Destroy();
193 }
194 #endif // wxTEST_GENERIC_DIALOGS_IN_MSW
195
196 void MyFrame::LogDialog(wxCommandEvent& event)
197 {
198 wxLogMessage("This is some message - everything is ok so far.");
199 wxLogMessage("Another message...");
200 wxLogWarning("And then something went wrong!");
201 wxLogError("Intermediary error handler decided to abort.");
202 wxLogError("The top level caller detected an error.");
203 }
204
205 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
206 {
207 wxMessageDialog dialog( this, "This is a message box\nA long, long string to test out the message box properly",
208 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
209
210 dialog.ShowModal();
211 }
212
213 void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
214 {
215 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
216 "Even two rows of text.",
217 "Enter a number:", "Numeric input test",
218 50, 0, 100, this );
219
220 wxString msg;
221 int icon;
222 if ( res == -1 )
223 {
224 msg = "Invalid number entered or dialog cancelled.";
225 icon = wxICON_HAND;
226 }
227 else
228 {
229 msg.Printf(_T("You've entered %lu"), res );
230 icon = wxICON_INFORMATION;
231 }
232
233 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
234 }
235
236 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
237 {
238 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
239 "Please enter a string", "Default value", wxOK|wxCANCEL);
240
241 if (dialog.ShowModal() == wxID_OK)
242 {
243 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
244 dialog2.ShowModal();
245 }
246 }
247
248 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
249 {
250 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
251 int n = 5;
252
253 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
254 "Please select a value", n, (const wxString *)choices);
255
256 dialog.SetSelection(2);
257
258 if (dialog.ShowModal() == wxID_OK)
259 {
260 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
261 dialog2.ShowModal();
262 }
263 }
264
265 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
266 {
267 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
268
269 if (dialog.ShowModal() == wxID_OK)
270 {
271 wxString info;
272 info.Printf(_T("Full file name: %s\n")
273 _T("Path: %s\n")
274 _T("Name: %s"),
275 dialog.GetPath().c_str(),
276 dialog.GetDirectory().c_str(),
277 dialog.GetFilename().c_str());
278 wxMessageDialog dialog2(this, info, "Selected file");
279 dialog2.ShowModal();
280 }
281 }
282
283 void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
284 {
285 wxFileDialog dialog(this, "Testing open multiple file dialog",
286 "", "", wxFileSelectorDefaultWildcardStr,
287 wxMULTIPLE);
288
289 if (dialog.ShowModal() == wxID_OK)
290 {
291 wxArrayString paths, filenames;
292
293 dialog.GetPaths(paths);
294 dialog.GetFilenames(filenames);
295
296 wxString msg, s;
297 size_t count = paths.GetCount();
298 for ( size_t n = 0; n < count; n++ )
299 {
300 s.Printf(_T("File %d: %s (%s)\n"),
301 n, paths[n].c_str(), filenames[n].c_str());
302
303 msg += s;
304 }
305
306 wxMessageDialog dialog2(this, msg, "Selected files");
307 dialog2.ShowModal();
308 }
309 }
310
311 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
312 {
313 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
314 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
315 wxSAVE|wxOVERWRITE_PROMPT);
316
317 if (dialog.ShowModal() == wxID_OK)
318 {
319 wxChar buf[400];
320 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
321 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
322 dialog2.ShowModal();
323 }
324 }
325
326 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
327 {
328 // pass some initial dir to wxDirDialog
329 wxString dirHome;
330 wxGetHomeDir(&dirHome);
331
332 wxDirDialog dialog(this, "Testing directory picker", dirHome);
333
334 if (dialog.ShowModal() == wxID_OK)
335 {
336 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
337 dialog2.ShowModal();
338 }
339 }
340
341 void MyFrame::ShowTip(wxCommandEvent& event)
342 {
343 #if wxUSE_STARTUP_TIPS
344 static size_t s_index = (size_t)-1;
345
346 if ( s_index == (size_t)-1 )
347 {
348 srand(time(NULL));
349
350 // this is completely bogus, we don't know how many lines are there
351 // in the file, but who cares, it's a demo only...
352 s_index = rand() % 5;
353 }
354
355 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
356
357 bool showAtStartup = wxShowTip(this, tipProvider);
358
359 if ( showAtStartup )
360 {
361 wxMessageBox("Will show tips on startup", "Tips dialog",
362 wxOK | wxICON_INFORMATION, this);
363 }
364
365 s_index = tipProvider->GetCurrentTip();
366 delete tipProvider;
367 #endif
368 }
369
370 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
371 {
372 Close(TRUE);
373 }
374
375 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
376 {
377 wxPaintDC dc(this);
378 dc.SetFont(wxGetApp().m_canvasFont);
379 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
380 dc.SetBackgroundMode(wxTRANSPARENT);
381 dc.DrawText("wxWindows common dialogs test application", 10, 10);
382 }
383
384 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
385 EVT_PAINT(MyCanvas::OnPaint)
386 END_EVENT_TABLE()
387
388 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
389 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
390 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
391 EVT_MENU(DIALOGS_LOG_DIALOG, MyFrame::LogDialog)
392 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
393 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
394 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
395 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
396 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
397 EVT_MENU(DIALOGS_FILES_OPEN, MyFrame::FilesOpen)
398 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
399 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
400 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
401 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
402 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
403 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
404 #endif
405 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
406 END_EVENT_TABLE()
407