]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
Added bitmaps and icons to samples
[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 = 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 = *wxSWISS_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(__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 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& WXUNUSED(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& WXUNUSED(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(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
147 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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& WXUNUSED(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 dialog.SetSelection(2);
217
218 if (dialog.ShowModal() == wxID_OK)
219 {
220 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
221 dialog2.ShowModal();
222 }
223 }
224
225 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
226 {
227 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
228
229 if (dialog.ShowModal() == wxID_OK)
230 {
231 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
232 dialog2.ShowModal();
233 }
234 }
235
236 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
237 {
238 wxFileDialog dialog(this, "Testing save file dialog", "", "",
239 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
240 wxSAVE|wxOVERWRITE_PROMPT);
241
242 if (dialog.ShowModal() == wxID_OK)
243 {
244 char buf[400];
245 sprintf(buf, "%s, filter %d", (const char *)dialog.GetPath(), dialog.GetFilterIndex());
246 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
247 dialog2.ShowModal();
248 }
249 }
250
251 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
252 {
253 #ifndef __WXGTK__
254 wxDirDialog dialog(this, "Testing directory picker", "");
255
256 if (dialog.ShowModal() == wxID_OK)
257 {
258 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
259 dialog2.ShowModal();
260 }
261 #endif
262 }
263
264 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
265 {
266 Close(TRUE);
267 }
268
269 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
270 {
271 wxPaintDC dc(this);
272 dc.SetFont(wxGetApp().m_canvasFont);
273 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
274 dc.SetBackgroundMode(wxTRANSPARENT);
275 dc.DrawText("wxWindows common dialogs test application", 10, 10);
276 }
277
278 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
279 EVT_PAINT(MyCanvas::OnPaint)
280 END_EVENT_TABLE()
281
282 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
283 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
284 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
285 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
286 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
287 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
288 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
289 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
290 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
291 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
292 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
293 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
294 #endif
295 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
296 END_EVENT_TABLE()
297