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