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