]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
Added the forgotten file txtstrm.tex
[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("", "Enter a number:", "Numeric input test",
244 50, 0, 100, this);
245
246 wxString msg;
247 int icon;
248 if ( res == -1 )
249 {
250 msg = "Invalid number entered or dialog cancelled.";
251 icon = wxICON_HAND;
252 }
253 else
254 {
255 msg.Printf(_T("You've entered %lu"), res);
256 icon = wxICON_INFORMATION;
257 }
258
259 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
260 }
261
262 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
263 {
264 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
265 "Please enter a string", "Default value", wxOK|wxCANCEL);
266
267 if (dialog.ShowModal() == wxID_OK)
268 {
269 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
270 dialog2.ShowModal();
271 }
272 }
273
274 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
275 {
276 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
277 int n = 5;
278
279 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
280 "Please select a value", n, (const wxString *)choices);
281
282 dialog.SetSelection(2);
283
284 if (dialog.ShowModal() == wxID_OK)
285 {
286 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
287 dialog2.ShowModal();
288 }
289 }
290
291 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
292 {
293 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
294
295 if (dialog.ShowModal() == wxID_OK)
296 {
297 wxString info;
298 info.Printf(_T("Full file name: %s\n")
299 _T("Path: %s\n")
300 _T("Name: %s"),
301 dialog.GetPath().c_str(),
302 dialog.GetDirectory().c_str(),
303 dialog.GetFilename().c_str());
304 wxMessageDialog dialog2(this, info, "Selected file");
305 dialog2.ShowModal();
306 }
307 }
308
309 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
310 {
311 wxFileDialog dialog(this, "Testing save file dialog", "", "",
312 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
313 wxSAVE|wxOVERWRITE_PROMPT);
314
315 if (dialog.ShowModal() == wxID_OK)
316 {
317 wxChar buf[400];
318 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
319 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
320 dialog2.ShowModal();
321 }
322 }
323
324 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
325 {
326 wxDirDialog dialog(this, "Testing directory picker", "");
327
328 if (dialog.ShowModal() == wxID_OK)
329 {
330 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
331 dialog2.ShowModal();
332 }
333 }
334
335 void MyFrame::ShowTip(wxCommandEvent& event)
336 {
337 #if wxUSE_STARTUP_TIPS
338 static size_t s_index = (size_t)-1;
339
340 if ( s_index == (size_t)-1 )
341 {
342 srand(time(NULL));
343
344 // this is completely bogus, we don't know how many lines are there
345 // in the file, but who cares, it's a demo only...
346 s_index = rand() % 5;
347 }
348
349 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
350
351 bool showAtStartup = wxShowTip(this, tipProvider);
352
353 if ( showAtStartup )
354 {
355 wxMessageBox("Will show tips on startup", "Tips dialog",
356 wxOK | wxICON_INFORMATION, this);
357 }
358
359 s_index = tipProvider->GetCurrentTip();
360 delete tipProvider;
361 #endif
362 }
363
364 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
365 {
366 Close(TRUE);
367 }
368
369 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
370 {
371 wxPaintDC dc(this);
372 dc.SetFont(wxGetApp().m_canvasFont);
373 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
374 dc.SetBackgroundMode(wxTRANSPARENT);
375 dc.DrawText("wxWindows common dialogs test application", 10, 10);
376 }
377
378 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
379 EVT_PAINT(MyCanvas::OnPaint)
380 END_EVENT_TABLE()
381
382 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
383 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
384 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
385 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
386 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
387 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
388 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
389 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
390 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
391 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
392 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
393 EVT_MENU(DIALOGS_EXT_DIALOG, MyFrame::ExtDialog)
394 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
395 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
396 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
397 #endif
398 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
399 END_EVENT_TABLE()
400