]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
Added the forgotten file txtstrm.tex
[wxWidgets.git] / samples / dialogs / dialogs.cpp
CommitLineData
457814b5
JS
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
c50f1fb9 9// Licence: wxWindows license
457814b5
JS
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
329e86bf
RR
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"
457814b5 35
dfad0599
JS
36#define wxTEST_GENERIC_DIALOGS_IN_MSW 0
37
38#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
39#include <wx/generic/colrdlgg.h>
40#include <wx/generic/fontdlgg.h>
41#endif
42
43#include "dialogs.h"
44
45IMPLEMENT_APP(MyApp)
46
c67daf87 47MyCanvas *myCanvas = (MyCanvas *) NULL;
457814b5 48
457814b5
JS
49// `Main program' equivalent, creating windows and returning main app frame
50bool MyApp::OnInit(void)
51{
e5ea3f7a 52#if defined(__WXGTK__) && defined(wxUSE_UNICODE)
dcf924a3 53 wxConvCurrent = &wxConvLibc;
e5ea3f7a
RR
54#endif
55
457814b5 56 m_canvasTextColour = wxColour("BLACK");
66bd6b93 57 m_canvasFont = *wxNORMAL_FONT;
457814b5
JS
58
59 // Create the main frame window
c67daf87 60 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
457814b5
JS
61
62 // Make a menubar
63 wxMenu *file_menu = new wxMenu;
64
65 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
66
dfad0599 67#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
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
dfad0599 74#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
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");
c49245f8 81 file_menu->Append(DIALOGS_NUM_ENTRY, "&Numeric entry\tCtrl-N");
457814b5 82 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice");
329e86bf 83 file_menu->Append(DIALOGS_EXT_DIALOG, "&Extended dialog");
457814b5 84 file_menu->AppendSeparator();
c50f1fb9
VZ
85 file_menu->Append(DIALOGS_TIP, "&Tip of the day");
86 file_menu->AppendSeparator();
457814b5
JS
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
110MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
111 wxFrame(parent, -1, title, pos, size)
112{}
113
329e86bf
RR
114void 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
8dbf4589
RR
121 wxExtDialog dialog( this, -1, "Test 1 for wxExtDialog", wxOK|wxFORWARD|wxBACKWARD );
122 dialog.AddButton( new wxButton( &dialog, -1, "Custom") );
329e86bf
RR
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",
8dbf4589
RR
137 wxOK|wxFORWARD|wxBACKWARD|wxCANCEL,
138 wxDefaultPosition, wxSize(400,170),
329e86bf
RR
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
d355d3fe 151void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
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 }
5919d76b 171 dialog->Destroy();
457814b5
JS
172}
173
d355d3fe 174void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
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 }
5919d76b 188 dialog->Destroy();
457814b5
JS
189}
190
dfad0599 191#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
d355d3fe 192void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
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 }
5919d76b 212 dialog->Destroy();
457814b5
JS
213}
214
d355d3fe 215void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
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 }
5919d76b 229 dialog->Destroy();
457814b5
JS
230}
231#endif
232
d355d3fe 233void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5 234{
8e360a08 235 wxMessageDialog dialog(NULL, "This is a message box\nA long, long string to test out the message box properly",
c50f1fb9 236 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
457814b5
JS
237
238 dialog.ShowModal();
239}
240
c49245f8
VZ
241void 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 {
dae3fbd8 255 msg.Printf(_T("You've entered %lu"), res);
c49245f8
VZ
256 icon = wxICON_INFORMATION;
257 }
258
259 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
260}
261
d355d3fe 262void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
263{
264 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
c50f1fb9 265 "Please enter a string", "Default value", wxOK|wxCANCEL);
457814b5
JS
266
267 if (dialog.ShowModal() == wxID_OK)
268 {
c50f1fb9
VZ
269 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
270 dialog2.ShowModal();
457814b5
JS
271 }
272}
273
d355d3fe 274void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 275{
ef77f91e
JS
276 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
277 int n = 5;
457814b5 278
ef77f91e
JS
279 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
280 "Please select a value", n, (const wxString *)choices);
457814b5 281
ef77f91e
JS
282 dialog.SetSelection(2);
283
284 if (dialog.ShowModal() == wxID_OK)
285 {
286 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
287 dialog2.ShowModal();
288 }
457814b5
JS
289}
290
d355d3fe 291void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5 292{
c50f1fb9 293 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
457814b5 294
c50f1fb9
VZ
295 if (dialog.ShowModal() == wxID_OK)
296 {
5919d76b 297 wxString info;
13393ab6
RR
298 info.Printf(_T("Full file name: %s\n")
299 _T("Path: %s\n")
300 _T("Name: %s"),
5919d76b
VZ
301 dialog.GetPath().c_str(),
302 dialog.GetDirectory().c_str(),
303 dialog.GetFilename().c_str());
c50f1fb9
VZ
304 wxMessageDialog dialog2(this, info, "Selected file");
305 dialog2.ShowModal();
306 }
457814b5
JS
307}
308
d355d3fe 309void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5 310{
c50f1fb9
VZ
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 }
457814b5
JS
322}
323
d355d3fe 324void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 325{
c50f1fb9
VZ
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
335void MyFrame::ShowTip(wxCommandEvent& event)
336{
78bd7ed3 337#if wxUSE_STARTUP_TIPS
c50f1fb9
VZ
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 }
457814b5 358
c50f1fb9
VZ
359 s_index = tipProvider->GetCurrentTip();
360 delete tipProvider;
78bd7ed3 361#endif
457814b5
JS
362}
363
d355d3fe 364void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5 365{
c50f1fb9 366 Close(TRUE);
457814b5
JS
367}
368
d355d3fe 369void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5 370{
c50f1fb9 371 wxPaintDC dc(this);
457814b5
JS
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
378BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
c50f1fb9 379 EVT_PAINT(MyCanvas::OnPaint)
457814b5
JS
380END_EVENT_TABLE()
381
382BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c50f1fb9
VZ
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)
c49245f8 387 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
c50f1fb9
VZ
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)
329e86bf 393 EVT_MENU(DIALOGS_EXT_DIALOG, MyFrame::ExtDialog)
dfad0599 394#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
c50f1fb9
VZ
395 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
396 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
457814b5 397#endif
c50f1fb9 398 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
457814b5
JS
399END_EVENT_TABLE()
400