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