]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
Added an #ifdef
[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");
81 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice");
329e86bf 82 file_menu->Append(DIALOGS_EXT_DIALOG, "&Extended dialog");
457814b5 83 file_menu->AppendSeparator();
c50f1fb9
VZ
84 file_menu->Append(DIALOGS_TIP, "&Tip of the day");
85 file_menu->AppendSeparator();
457814b5
JS
86 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file");
87 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file");
88 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory");
89 file_menu->AppendSeparator();
90 file_menu->Append(wxID_EXIT, "E&xit");
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
329e86bf
RR
113void MyFrame::ExtDialog(wxCommandEvent& WXUNUSED(event) )
114{
115 // The standard flags causes this dialog to display a
116 // wxStaticLine under wxMotif and wxGTK, but none under
117 // other platforms. Also, it will not be resizable
118 // anywhere.
119
8dbf4589
RR
120 wxExtDialog dialog( this, -1, "Test 1 for wxExtDialog", wxOK|wxFORWARD|wxBACKWARD );
121 dialog.AddButton( new wxButton( &dialog, -1, "Custom") );
329e86bf
RR
122
123 dialog.SetClientWindow( new wxTextCtrl( &dialog, -1, "Test", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ) );
124
125 // query minimal recommended size from the buttons
126 dialog.SetSize( dialog.GetButtonAreaSize().x, 170 );
127
128 dialog.Centre( wxBOTH );
129 dialog.ShowModal();
130
131 // This dialog uses the standard dialog styles but is also
132 // resizable on all platforms and shows a wxStaticLine on
133 // all platforms.
134
135 wxExtDialog dialog2( this, -1, "Test 2 for wxExtDialog",
8dbf4589
RR
136 wxOK|wxFORWARD|wxBACKWARD|wxCANCEL,
137 wxDefaultPosition, wxSize(400,170),
329e86bf
RR
138 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxED_BUTTONS_RIGHT | wxED_STATIC_LINE | wxED_CLIENT_MARGIN );
139
140 dialog2.SetClientWindow( new wxTextCtrl( &dialog2, -1, "Test", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ) );
141
142 // query minimal recommended size from the buttons
143 wxSize min_size( dialog2.GetButtonAreaSize() );
144 dialog2.SetSizeHints( min_size.x + 200, min_size.y );
145
146 dialog2.Centre( wxBOTH );
147 dialog2.ShowModal();
148}
149
d355d3fe 150void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
151{
152 wxColourData data;
153 data.SetChooseFull(TRUE);
154 for (int i = 0; i < 16; i++)
155 {
156 wxColour colour(i*16, i*16, i*16);
157 data.SetCustomColour(i, colour);
158 }
159
160 wxColourDialog *dialog = new wxColourDialog(this, &data);
161 if (dialog->ShowModal() == wxID_OK)
162 {
163 wxColourData retData = dialog->GetColourData();
164 wxColour col = retData.GetColour();
165// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
166 myCanvas->SetBackgroundColour(col);
167 myCanvas->Clear();
168 myCanvas->Refresh();
169 }
5919d76b 170 dialog->Destroy();
457814b5
JS
171}
172
d355d3fe 173void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
174{
175 wxFontData data;
176 data.SetInitialFont(wxGetApp().m_canvasFont);
177 data.SetColour(wxGetApp().m_canvasTextColour);
178
179 wxFontDialog *dialog = new wxFontDialog(this, &data);
180 if (dialog->ShowModal() == wxID_OK)
181 {
182 wxFontData retData = dialog->GetFontData();
183 wxGetApp().m_canvasFont = retData.GetChosenFont();
184 wxGetApp().m_canvasTextColour = retData.GetColour();
185 myCanvas->Refresh();
186 }
5919d76b 187 dialog->Destroy();
457814b5
JS
188}
189
dfad0599 190#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
d355d3fe 191void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
192{
193 wxColourData data;
194 data.SetChooseFull(TRUE);
195 for (int i = 0; i < 16; i++)
196 {
197 wxColour colour(i*16, i*16, i*16);
198 data.SetCustomColour(i, colour);
199 }
200
201 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
202 if (dialog->ShowModal() == wxID_OK)
203 {
204 wxColourData retData = dialog->GetColourData();
205 wxColour col = retData.GetColour();
206// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
207 myCanvas->SetBackgroundColour(col);
208 myCanvas->Clear();
209 myCanvas->Refresh();
210 }
5919d76b 211 dialog->Destroy();
457814b5
JS
212}
213
d355d3fe 214void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
215{
216 wxFontData data;
217 data.SetInitialFont(wxGetApp().m_canvasFont);
218 data.SetColour(wxGetApp().m_canvasTextColour);
219
220 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
221 if (dialog->ShowModal() == wxID_OK)
222 {
223 wxFontData retData = dialog->GetFontData();
224 wxGetApp().m_canvasFont = retData.GetChosenFont();
225 wxGetApp().m_canvasTextColour = retData.GetColour();
226 myCanvas->Refresh();
227 }
5919d76b 228 dialog->Destroy();
457814b5
JS
229}
230#endif
231
d355d3fe 232void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5 233{
8e360a08 234 wxMessageDialog dialog(NULL, "This is a message box\nA long, long string to test out the message box properly",
c50f1fb9 235 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
457814b5
JS
236
237 dialog.ShowModal();
238}
239
d355d3fe 240void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
241{
242 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
c50f1fb9 243 "Please enter a string", "Default value", wxOK|wxCANCEL);
457814b5
JS
244
245 if (dialog.ShowModal() == wxID_OK)
246 {
c50f1fb9
VZ
247 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
248 dialog2.ShowModal();
457814b5
JS
249 }
250}
251
d355d3fe 252void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 253{
ef77f91e
JS
254 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
255 int n = 5;
457814b5 256
ef77f91e
JS
257 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
258 "Please select a value", n, (const wxString *)choices);
457814b5 259
ef77f91e
JS
260 dialog.SetSelection(2);
261
262 if (dialog.ShowModal() == wxID_OK)
263 {
264 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
265 dialog2.ShowModal();
266 }
457814b5
JS
267}
268
d355d3fe 269void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5 270{
c50f1fb9 271 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
457814b5 272
c50f1fb9
VZ
273 if (dialog.ShowModal() == wxID_OK)
274 {
5919d76b 275 wxString info;
13393ab6
RR
276 info.Printf(_T("Full file name: %s\n")
277 _T("Path: %s\n")
278 _T("Name: %s"),
5919d76b
VZ
279 dialog.GetPath().c_str(),
280 dialog.GetDirectory().c_str(),
281 dialog.GetFilename().c_str());
c50f1fb9
VZ
282 wxMessageDialog dialog2(this, info, "Selected file");
283 dialog2.ShowModal();
284 }
457814b5
JS
285}
286
d355d3fe 287void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5 288{
c50f1fb9
VZ
289 wxFileDialog dialog(this, "Testing save file dialog", "", "",
290 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
291 wxSAVE|wxOVERWRITE_PROMPT);
292
293 if (dialog.ShowModal() == wxID_OK)
294 {
295 wxChar buf[400];
296 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
297 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
298 dialog2.ShowModal();
299 }
457814b5
JS
300}
301
d355d3fe 302void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 303{
c50f1fb9
VZ
304 wxDirDialog dialog(this, "Testing directory picker", "");
305
306 if (dialog.ShowModal() == wxID_OK)
307 {
308 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
309 dialog2.ShowModal();
310 }
311}
312
313void MyFrame::ShowTip(wxCommandEvent& event)
314{
78bd7ed3 315#if wxUSE_STARTUP_TIPS
c50f1fb9
VZ
316 static size_t s_index = (size_t)-1;
317
318 if ( s_index == (size_t)-1 )
319 {
320 srand(time(NULL));
321
322 // this is completely bogus, we don't know how many lines are there
323 // in the file, but who cares, it's a demo only...
324 s_index = rand() % 5;
325 }
326
327 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
328
329 bool showAtStartup = wxShowTip(this, tipProvider);
330
331 if ( showAtStartup )
332 {
333 wxMessageBox("Will show tips on startup", "Tips dialog",
334 wxOK | wxICON_INFORMATION, this);
335 }
457814b5 336
c50f1fb9
VZ
337 s_index = tipProvider->GetCurrentTip();
338 delete tipProvider;
78bd7ed3 339#endif
457814b5
JS
340}
341
d355d3fe 342void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5 343{
c50f1fb9 344 Close(TRUE);
457814b5
JS
345}
346
d355d3fe 347void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5 348{
c50f1fb9 349 wxPaintDC dc(this);
457814b5
JS
350 dc.SetFont(wxGetApp().m_canvasFont);
351 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
352 dc.SetBackgroundMode(wxTRANSPARENT);
353 dc.DrawText("wxWindows common dialogs test application", 10, 10);
354}
355
356BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
c50f1fb9 357 EVT_PAINT(MyCanvas::OnPaint)
457814b5
JS
358END_EVENT_TABLE()
359
360BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c50f1fb9
VZ
361 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
362 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
363 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
364 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
365 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
366 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
367 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
368 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
369 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
329e86bf 370 EVT_MENU(DIALOGS_EXT_DIALOG, MyFrame::ExtDialog)
dfad0599 371#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
c50f1fb9
VZ
372 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
373 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
457814b5 374#endif
c50f1fb9 375 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
457814b5
JS
376END_EVENT_TABLE()
377