]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
Heavier checks for wxUSE_WCHAR_T
[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
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
dfad0599
JS
34#define wxTEST_GENERIC_DIALOGS_IN_MSW 0
35
36#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
37#include <wx/generic/colrdlgg.h>
38#include <wx/generic/fontdlgg.h>
39#endif
40
41#include "dialogs.h"
42
43IMPLEMENT_APP(MyApp)
44
c67daf87 45MyCanvas *myCanvas = (MyCanvas *) NULL;
457814b5 46
457814b5
JS
47// `Main program' equivalent, creating windows and returning main app frame
48bool MyApp::OnInit(void)
49{
50 m_canvasTextColour = wxColour("BLACK");
66bd6b93 51 m_canvasFont = *wxNORMAL_FONT;
457814b5
JS
52
53 // Create the main frame window
c67daf87 54 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
457814b5
JS
55
56 // Make a menubar
57 wxMenu *file_menu = new wxMenu;
58
59 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
60
dfad0599 61#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
62 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
63#endif
64
65 file_menu->AppendSeparator();
66 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
67
dfad0599 68#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
69 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
70
71#endif
72 file_menu->AppendSeparator();
73 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box");
74 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry");
75 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice");
76 file_menu->AppendSeparator();
77 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file");
78 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file");
79 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory");
80 file_menu->AppendSeparator();
81 file_menu->Append(wxID_EXIT, "E&xit");
82 wxMenuBar *menu_bar = new wxMenuBar;
83 menu_bar->Append(file_menu, "&File");
84 frame->SetMenuBar(menu_bar);
85
86 myCanvas = new MyCanvas(frame);
87 myCanvas->SetBackgroundColour(*wxWHITE);
88
89 frame->Centre(wxBOTH);
90
91 // Show the frame
92 frame->Show(TRUE);
93
94 SetTopWindow(frame);
95
96 return TRUE;
97}
98
99// My frame constructor
100MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
101 wxFrame(parent, -1, title, pos, size)
102{}
103
d355d3fe 104void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
105{
106 wxColourData data;
107 data.SetChooseFull(TRUE);
108 for (int i = 0; i < 16; i++)
109 {
110 wxColour colour(i*16, i*16, i*16);
111 data.SetCustomColour(i, colour);
112 }
113
114 wxColourDialog *dialog = new wxColourDialog(this, &data);
115 if (dialog->ShowModal() == wxID_OK)
116 {
117 wxColourData retData = dialog->GetColourData();
118 wxColour col = retData.GetColour();
119// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
120 myCanvas->SetBackgroundColour(col);
121 myCanvas->Clear();
122 myCanvas->Refresh();
123 }
5919d76b 124 dialog->Destroy();
457814b5
JS
125}
126
d355d3fe 127void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
128{
129 wxFontData data;
130 data.SetInitialFont(wxGetApp().m_canvasFont);
131 data.SetColour(wxGetApp().m_canvasTextColour);
132
133 wxFontDialog *dialog = new wxFontDialog(this, &data);
134 if (dialog->ShowModal() == wxID_OK)
135 {
136 wxFontData retData = dialog->GetFontData();
137 wxGetApp().m_canvasFont = retData.GetChosenFont();
138 wxGetApp().m_canvasTextColour = retData.GetColour();
139 myCanvas->Refresh();
140 }
5919d76b 141 dialog->Destroy();
457814b5
JS
142}
143
dfad0599 144#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
d355d3fe 145void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
146{
147 wxColourData data;
148 data.SetChooseFull(TRUE);
149 for (int i = 0; i < 16; i++)
150 {
151 wxColour colour(i*16, i*16, i*16);
152 data.SetCustomColour(i, colour);
153 }
154
155 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
156 if (dialog->ShowModal() == wxID_OK)
157 {
158 wxColourData retData = dialog->GetColourData();
159 wxColour col = retData.GetColour();
160// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
161 myCanvas->SetBackgroundColour(col);
162 myCanvas->Clear();
163 myCanvas->Refresh();
164 }
5919d76b 165 dialog->Destroy();
457814b5
JS
166}
167
d355d3fe 168void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
169{
170 wxFontData data;
171 data.SetInitialFont(wxGetApp().m_canvasFont);
172 data.SetColour(wxGetApp().m_canvasTextColour);
173
174 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
175 if (dialog->ShowModal() == wxID_OK)
176 {
177 wxFontData retData = dialog->GetFontData();
178 wxGetApp().m_canvasFont = retData.GetChosenFont();
179 wxGetApp().m_canvasTextColour = retData.GetColour();
180 myCanvas->Refresh();
181 }
5919d76b 182 dialog->Destroy();
457814b5
JS
183}
184#endif
185
d355d3fe 186void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5 187{
8e360a08
KB
188#if 0
189 wxMessageDialog dialog(NULL, "This is a message box\nA long, long string to test out the message box properly",
457814b5
JS
190 "Message box text", wxYES_NO|wxCANCEL);
191
192 dialog.ShowModal();
8e360a08
KB
193#endif
194 ::wxMessageBox("MsgBox with a really long long string",
195 "this is the text", wxYES_NO|wxICON_EXCLAMATION);
457814b5
JS
196}
197
d355d3fe 198void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
199{
200 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
201 "Please enter a string", "Default value", wxOK|wxCANCEL);
202
203 if (dialog.ShowModal() == wxID_OK)
204 {
205 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
206 dialog2.ShowModal();
207 }
208}
209
d355d3fe 210void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 211{
ef77f91e
JS
212 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
213 int n = 5;
457814b5 214
ef77f91e
JS
215 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
216 "Please select a value", n, (const wxString *)choices);
457814b5 217
ef77f91e
JS
218 dialog.SetSelection(2);
219
220 if (dialog.ShowModal() == wxID_OK)
221 {
222 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
223 dialog2.ShowModal();
224 }
457814b5
JS
225}
226
d355d3fe 227void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
228{
229 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
230
231 if (dialog.ShowModal() == wxID_OK)
232 {
5919d76b 233 wxString info;
13393ab6
RR
234 info.Printf(_T("Full file name: %s\n")
235 _T("Path: %s\n")
236 _T("Name: %s"),
5919d76b
VZ
237 dialog.GetPath().c_str(),
238 dialog.GetDirectory().c_str(),
239 dialog.GetFilename().c_str());
240 wxMessageDialog dialog2(this, info, "Selected file");
457814b5
JS
241 dialog2.ShowModal();
242 }
243}
244
d355d3fe 245void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
246{
247 wxFileDialog dialog(this, "Testing save file dialog", "", "",
248 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
249 wxSAVE|wxOVERWRITE_PROMPT);
250
251 if (dialog.ShowModal() == wxID_OK)
252 {
13393ab6
RR
253 wxChar buf[400];
254 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
457814b5
JS
255 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
256 dialog2.ShowModal();
257 }
258}
259
d355d3fe 260void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
261{
262 wxDirDialog dialog(this, "Testing directory picker", "");
263
264 if (dialog.ShowModal() == wxID_OK)
265 {
266 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
267 dialog2.ShowModal();
268 }
269}
270
d355d3fe 271void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
272{
273 Close(TRUE);
274}
275
d355d3fe 276void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5
JS
277{
278 wxPaintDC dc(this);
279 dc.SetFont(wxGetApp().m_canvasFont);
280 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
281 dc.SetBackgroundMode(wxTRANSPARENT);
282 dc.DrawText("wxWindows common dialogs test application", 10, 10);
283}
284
285BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
286 EVT_PAINT(MyCanvas::OnPaint)
287END_EVENT_TABLE()
288
289BEGIN_EVENT_TABLE(MyFrame, wxFrame)
290 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
291 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
292 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
293 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
294 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
295 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
296 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
297 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
dfad0599 298#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
299 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
300 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
301#endif
302 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
303END_EVENT_TABLE()
304