]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
no 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
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
JS
46
47// A macro needed for some compilers (AIX) that need 'main' to be defined
48// in the application itself.
49IMPLEMENT_WXWIN_MAIN
50
51// `Main program' equivalent, creating windows and returning main app frame
52bool MyApp::OnInit(void)
53{
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");
c67daf87
UR
83#ifndef __WXGTK__
84 // don't add a menu entry for something that isn't in wxGTK. It only
85 // confuses the user
457814b5 86 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory");
c67daf87 87#endif
457814b5
JS
88 file_menu->AppendSeparator();
89 file_menu->Append(wxID_EXIT, "E&xit");
90 wxMenuBar *menu_bar = new wxMenuBar;
91 menu_bar->Append(file_menu, "&File");
92 frame->SetMenuBar(menu_bar);
93
94 myCanvas = new MyCanvas(frame);
95 myCanvas->SetBackgroundColour(*wxWHITE);
96
97 frame->Centre(wxBOTH);
98
99 // Show the frame
100 frame->Show(TRUE);
101
102 SetTopWindow(frame);
103
104 return TRUE;
105}
106
107// My frame constructor
108MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
109 wxFrame(parent, -1, title, pos, size)
110{}
111
d355d3fe 112void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
113{
114 wxColourData data;
115 data.SetChooseFull(TRUE);
116 for (int i = 0; i < 16; i++)
117 {
118 wxColour colour(i*16, i*16, i*16);
119 data.SetCustomColour(i, colour);
120 }
121
122 wxColourDialog *dialog = new wxColourDialog(this, &data);
123 if (dialog->ShowModal() == wxID_OK)
124 {
125 wxColourData retData = dialog->GetColourData();
126 wxColour col = retData.GetColour();
127// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
128 myCanvas->SetBackgroundColour(col);
129 myCanvas->Clear();
130 myCanvas->Refresh();
131 }
132 dialog->Close();
133}
134
d355d3fe 135void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
136{
137 wxFontData data;
138 data.SetInitialFont(wxGetApp().m_canvasFont);
139 data.SetColour(wxGetApp().m_canvasTextColour);
140
141 wxFontDialog *dialog = new wxFontDialog(this, &data);
142 if (dialog->ShowModal() == wxID_OK)
143 {
144 wxFontData retData = dialog->GetFontData();
145 wxGetApp().m_canvasFont = retData.GetChosenFont();
146 wxGetApp().m_canvasTextColour = retData.GetColour();
147 myCanvas->Refresh();
148 }
149 dialog->Close();
150}
151
dfad0599 152#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
d355d3fe 153void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
154{
155 wxColourData data;
156 data.SetChooseFull(TRUE);
157 for (int i = 0; i < 16; i++)
158 {
159 wxColour colour(i*16, i*16, i*16);
160 data.SetCustomColour(i, colour);
161 }
162
163 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
164 if (dialog->ShowModal() == wxID_OK)
165 {
166 wxColourData retData = dialog->GetColourData();
167 wxColour col = retData.GetColour();
168// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
169 myCanvas->SetBackgroundColour(col);
170 myCanvas->Clear();
171 myCanvas->Refresh();
172 }
173 dialog->Close();
174}
175
d355d3fe 176void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
177{
178 wxFontData data;
179 data.SetInitialFont(wxGetApp().m_canvasFont);
180 data.SetColour(wxGetApp().m_canvasTextColour);
181
182 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
183 if (dialog->ShowModal() == wxID_OK)
184 {
185 wxFontData retData = dialog->GetFontData();
186 wxGetApp().m_canvasFont = retData.GetChosenFont();
187 wxGetApp().m_canvasTextColour = retData.GetColour();
188 myCanvas->Refresh();
189 }
190 dialog->Close();
191}
192#endif
193
d355d3fe 194void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
195{
196 wxMessageDialog dialog(this, "This is a message box\nA long, long string to test out the message box properly",
197 "Message box text", wxYES_NO|wxCANCEL);
198
199 dialog.ShowModal();
200}
201
d355d3fe 202void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
203{
204 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
205 "Please enter a string", "Default value", wxOK|wxCANCEL);
206
207 if (dialog.ShowModal() == wxID_OK)
208 {
209 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
210 dialog2.ShowModal();
211 }
212}
213
d355d3fe 214void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 215{
ef77f91e
JS
216 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
217 int n = 5;
457814b5 218
ef77f91e
JS
219 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
220 "Please select a value", n, (const wxString *)choices);
457814b5 221
ef77f91e
JS
222 dialog.SetSelection(2);
223
224 if (dialog.ShowModal() == wxID_OK)
225 {
226 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
227 dialog2.ShowModal();
228 }
457814b5
JS
229}
230
d355d3fe 231void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
232{
233 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
234
235 if (dialog.ShowModal() == wxID_OK)
236 {
237 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
238 dialog2.ShowModal();
239 }
240}
241
d355d3fe 242void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
243{
244 wxFileDialog dialog(this, "Testing save file dialog", "", "",
245 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
246 wxSAVE|wxOVERWRITE_PROMPT);
247
248 if (dialog.ShowModal() == wxID_OK)
249 {
250 char buf[400];
251 sprintf(buf, "%s, filter %d", (const char *)dialog.GetPath(), dialog.GetFilterIndex());
252 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
253 dialog2.ShowModal();
254 }
255}
256
d355d3fe 257void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 258{
d355d3fe 259#ifndef __WXGTK__
457814b5
JS
260 wxDirDialog dialog(this, "Testing directory picker", "");
261
262 if (dialog.ShowModal() == wxID_OK)
263 {
264 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
265 dialog2.ShowModal();
266 }
d355d3fe 267#endif
457814b5
JS
268}
269
d355d3fe 270void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
271{
272 Close(TRUE);
273}
274
d355d3fe 275void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5
JS
276{
277 wxPaintDC dc(this);
278 dc.SetFont(wxGetApp().m_canvasFont);
279 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
280 dc.SetBackgroundMode(wxTRANSPARENT);
281 dc.DrawText("wxWindows common dialogs test application", 10, 10);
282}
283
284BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
285 EVT_PAINT(MyCanvas::OnPaint)
286END_EVENT_TABLE()
287
288BEGIN_EVENT_TABLE(MyFrame, wxFrame)
289 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
290 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
291 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
292 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
293 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
294 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
295 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
296 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
dfad0599 297#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
298 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
299 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
300#endif
301 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
302END_EVENT_TABLE()
303