]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
b5bd0ccfc88031ead7f702f88f728aa9d02a152d
[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 #include "wx/extdlg.h"
35
36 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
37
38 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
39 #include <wx/generic/colrdlgg.h>
40 #include <wx/generic/fontdlgg.h>
41 #endif
42
43 #include "dialogs.h"
44
45 IMPLEMENT_APP(MyApp)
46
47 MyCanvas *myCanvas = (MyCanvas *) NULL;
48
49 // `Main program' equivalent, creating windows and returning main app frame
50 bool MyApp::OnInit(void)
51 {
52 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
53 wxConvCurrent = &wxConvLibc;
54 #endif
55
56 m_canvasTextColour = wxColour("BLACK");
57 m_canvasFont = *wxNORMAL_FONT;
58
59 // Create the main frame window
60 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
61
62 // Make a menubar
63 wxMenu *file_menu = new wxMenu;
64
65 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
66
67 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
68 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
69 #endif
70
71 file_menu->AppendSeparator();
72 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
73
74 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
75 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
76
77 #endif
78 file_menu->AppendSeparator();
79 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box");
80 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry");
81 file_menu->Append(DIALOGS_NUM_ENTRY, "&Numeric entry\tCtrl-N");
82 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice");
83 file_menu->Append(DIALOGS_EXT_DIALOG, "&Extended dialog");
84 file_menu->AppendSeparator();
85 file_menu->Append(DIALOGS_TIP, "&Tip of the day");
86 file_menu->AppendSeparator();
87 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file");
88 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file");
89 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory");
90 file_menu->AppendSeparator();
91 file_menu->Append(wxID_EXIT, "E&xit");
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::ExtDialog(wxCommandEvent& WXUNUSED(event) )
115 {
116 // The standard flags causes this dialog to display a
117 // wxStaticLine under wxMotif and wxGTK, but none under
118 // other platforms. Also, it will not be resizable
119 // anywhere.
120
121 wxExtDialog dialog( this, -1, "Test 1 for wxExtDialog", wxOK|wxFORWARD|wxBACKWARD );
122 dialog.AddButton( new wxButton( &dialog, -1, "Custom") );
123
124 dialog.SetClientWindow( new wxTextCtrl( &dialog, -1, "Test", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ) );
125
126 // query minimal recommended size from the buttons
127 dialog.SetSize( dialog.GetButtonAreaSize().x, 170 );
128
129 dialog.Centre( wxBOTH );
130 dialog.ShowModal();
131
132 // This dialog uses the standard dialog styles but is also
133 // resizable on all platforms and shows a wxStaticLine on
134 // all platforms.
135
136 wxExtDialog dialog2( this, -1, "Test 2 for wxExtDialog",
137 wxOK|wxFORWARD|wxBACKWARD|wxCANCEL,
138 wxDefaultPosition, wxSize(400,170),
139 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxED_BUTTONS_RIGHT | wxED_STATIC_LINE | wxED_CLIENT_MARGIN );
140
141 dialog2.SetClientWindow( new wxTextCtrl( &dialog2, -1, "Test", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ) );
142
143 // query minimal recommended size from the buttons
144 wxSize min_size( dialog2.GetButtonAreaSize() );
145 dialog2.SetSizeHints( min_size.x + 200, min_size.y );
146
147 dialog2.Centre( wxBOTH );
148 dialog2.ShowModal();
149 }
150
151 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
152 {
153 wxColourData data;
154 data.SetChooseFull(TRUE);
155 for (int i = 0; i < 16; i++)
156 {
157 wxColour colour(i*16, i*16, i*16);
158 data.SetCustomColour(i, colour);
159 }
160
161 wxColourDialog *dialog = new wxColourDialog(this, &data);
162 if (dialog->ShowModal() == wxID_OK)
163 {
164 wxColourData retData = dialog->GetColourData();
165 wxColour col = retData.GetColour();
166 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
167 myCanvas->SetBackgroundColour(col);
168 myCanvas->Clear();
169 myCanvas->Refresh();
170 }
171 dialog->Destroy();
172 }
173
174 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
175 {
176 wxFontData data;
177 data.SetInitialFont(wxGetApp().m_canvasFont);
178 data.SetColour(wxGetApp().m_canvasTextColour);
179
180 wxFontDialog *dialog = new wxFontDialog(this, &data);
181 if (dialog->ShowModal() == wxID_OK)
182 {
183 wxFontData retData = dialog->GetFontData();
184 wxGetApp().m_canvasFont = retData.GetChosenFont();
185 wxGetApp().m_canvasTextColour = retData.GetColour();
186 myCanvas->Refresh();
187 }
188 dialog->Destroy();
189 }
190
191 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
192 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
193 {
194 wxColourData data;
195 data.SetChooseFull(TRUE);
196 for (int i = 0; i < 16; i++)
197 {
198 wxColour colour(i*16, i*16, i*16);
199 data.SetCustomColour(i, colour);
200 }
201
202 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
203 if (dialog->ShowModal() == wxID_OK)
204 {
205 wxColourData retData = dialog->GetColourData();
206 wxColour col = retData.GetColour();
207 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
208 myCanvas->SetBackgroundColour(col);
209 myCanvas->Clear();
210 myCanvas->Refresh();
211 }
212 dialog->Destroy();
213 }
214
215 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
216 {
217 wxFontData data;
218 data.SetInitialFont(wxGetApp().m_canvasFont);
219 data.SetColour(wxGetApp().m_canvasTextColour);
220
221 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
222 if (dialog->ShowModal() == wxID_OK)
223 {
224 wxFontData retData = dialog->GetFontData();
225 wxGetApp().m_canvasFont = retData.GetChosenFont();
226 wxGetApp().m_canvasTextColour = retData.GetColour();
227 myCanvas->Refresh();
228 }
229 dialog->Destroy();
230 }
231 #endif
232
233 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
234 {
235 wxMessageDialog dialog(NULL, "This is a message box\nA long, long string to test out the message box properly",
236 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
237
238 dialog.ShowModal();
239 }
240
241 void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
242 {
243 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
244 "Even two rows of text.",
245 "Enter a number:", "Numeric input test",
246 50, 0, 100, this );
247
248 wxString msg;
249 int icon;
250 if ( res == -1 )
251 {
252 msg = "Invalid number entered or dialog cancelled.";
253 icon = wxICON_HAND;
254 }
255 else
256 {
257 msg.Printf(_T("You've entered %lu"), res);
258 icon = wxICON_INFORMATION;
259 }
260
261 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
262 }
263
264 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
265 {
266 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
267 "Please enter a string", "Default value", wxOK|wxCANCEL);
268
269 if (dialog.ShowModal() == wxID_OK)
270 {
271 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
272 dialog2.ShowModal();
273 }
274 }
275
276 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
277 {
278 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
279 int n = 5;
280
281 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
282 "Please select a value", n, (const wxString *)choices);
283
284 dialog.SetSelection(2);
285
286 if (dialog.ShowModal() == wxID_OK)
287 {
288 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
289 dialog2.ShowModal();
290 }
291 }
292
293 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
294 {
295 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
296
297 if (dialog.ShowModal() == wxID_OK)
298 {
299 wxString info;
300 info.Printf(_T("Full file name: %s\n")
301 _T("Path: %s\n")
302 _T("Name: %s"),
303 dialog.GetPath().c_str(),
304 dialog.GetDirectory().c_str(),
305 dialog.GetFilename().c_str());
306 wxMessageDialog dialog2(this, info, "Selected file");
307 dialog2.ShowModal();
308 }
309 }
310
311 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
312 {
313 wxFileDialog dialog(this, "Testing save file dialog", "", "",
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 wxDirDialog dialog(this, "Testing directory picker", "");
329
330 if (dialog.ShowModal() == wxID_OK)
331 {
332 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
333 dialog2.ShowModal();
334 }
335 }
336
337 void MyFrame::ShowTip(wxCommandEvent& event)
338 {
339 #if wxUSE_STARTUP_TIPS
340 static size_t s_index = (size_t)-1;
341
342 if ( s_index == (size_t)-1 )
343 {
344 srand(time(NULL));
345
346 // this is completely bogus, we don't know how many lines are there
347 // in the file, but who cares, it's a demo only...
348 s_index = rand() % 5;
349 }
350
351 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
352
353 bool showAtStartup = wxShowTip(this, tipProvider);
354
355 if ( showAtStartup )
356 {
357 wxMessageBox("Will show tips on startup", "Tips dialog",
358 wxOK | wxICON_INFORMATION, this);
359 }
360
361 s_index = tipProvider->GetCurrentTip();
362 delete tipProvider;
363 #endif
364 }
365
366 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
367 {
368 Close(TRUE);
369 }
370
371 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
372 {
373 wxPaintDC dc(this);
374 dc.SetFont(wxGetApp().m_canvasFont);
375 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
376 dc.SetBackgroundMode(wxTRANSPARENT);
377 dc.DrawText("wxWindows common dialogs test application", 10, 10);
378 }
379
380 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
381 EVT_PAINT(MyCanvas::OnPaint)
382 END_EVENT_TABLE()
383
384 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
385 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
386 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
387 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
388 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
389 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
390 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
391 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
392 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
393 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
394 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
395 EVT_MENU(DIALOGS_EXT_DIALOG, MyFrame::ExtDialog)
396 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
397 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
398 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
399 #endif
400 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
401 END_EVENT_TABLE()
402