*** empty log message ***
[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{
e65cc56a
RR
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 );
c49245f8
VZ
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 {
dae3fbd8 257 msg.Printf(_T("You've entered %lu"), res);
c49245f8
VZ
258 icon = wxICON_INFORMATION;
259 }
260
261 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
262}
263
d355d3fe 264void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
265{
266 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
c50f1fb9 267 "Please enter a string", "Default value", wxOK|wxCANCEL);
457814b5
JS
268
269 if (dialog.ShowModal() == wxID_OK)
270 {
c50f1fb9
VZ
271 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
272 dialog2.ShowModal();
457814b5
JS
273 }
274}
275
d355d3fe 276void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 277{
ef77f91e
JS
278 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
279 int n = 5;
457814b5 280
ef77f91e
JS
281 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
282 "Please select a value", n, (const wxString *)choices);
457814b5 283
ef77f91e
JS
284 dialog.SetSelection(2);
285
286 if (dialog.ShowModal() == wxID_OK)
287 {
288 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
289 dialog2.ShowModal();
290 }
457814b5
JS
291}
292
d355d3fe 293void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5 294{
c50f1fb9 295 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
457814b5 296
c50f1fb9
VZ
297 if (dialog.ShowModal() == wxID_OK)
298 {
5919d76b 299 wxString info;
13393ab6
RR
300 info.Printf(_T("Full file name: %s\n")
301 _T("Path: %s\n")
302 _T("Name: %s"),
5919d76b
VZ
303 dialog.GetPath().c_str(),
304 dialog.GetDirectory().c_str(),
305 dialog.GetFilename().c_str());
c50f1fb9
VZ
306 wxMessageDialog dialog2(this, info, "Selected file");
307 dialog2.ShowModal();
308 }
457814b5
JS
309}
310
d355d3fe 311void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5 312{
c50f1fb9
VZ
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 }
457814b5
JS
324}
325
d355d3fe 326void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 327{
c50f1fb9
VZ
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
337void MyFrame::ShowTip(wxCommandEvent& event)
338{
78bd7ed3 339#if wxUSE_STARTUP_TIPS
c50f1fb9
VZ
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 }
457814b5 360
c50f1fb9
VZ
361 s_index = tipProvider->GetCurrentTip();
362 delete tipProvider;
78bd7ed3 363#endif
457814b5
JS
364}
365
d355d3fe 366void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5 367{
c50f1fb9 368 Close(TRUE);
457814b5
JS
369}
370
d355d3fe 371void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5 372{
c50f1fb9 373 wxPaintDC dc(this);
457814b5
JS
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
380BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
c50f1fb9 381 EVT_PAINT(MyCanvas::OnPaint)
457814b5
JS
382END_EVENT_TABLE()
383
384BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c50f1fb9
VZ
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)
c49245f8 389 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
c50f1fb9
VZ
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)
329e86bf 395 EVT_MENU(DIALOGS_EXT_DIALOG, MyFrame::ExtDialog)
dfad0599 396#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
c50f1fb9
VZ
397 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
398 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
457814b5 399#endif
c50f1fb9 400 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
457814b5
JS
401END_EVENT_TABLE()
402