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