]> git.saurik.com Git - wxWidgets.git/blob - samples/dialogs/dialogs.cpp
Added wxExtDialog and sample.
[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 #include "wx/tipdlg.h"
34 #include "wx/extdlg.h"
35
36 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
37
38 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
39 #include <wx/generic/colrdlgg.h>
40 #include <wx/generic/fontdlgg.h>
41 #endif
42
43 #include "dialogs.h"
44
45 IMPLEMENT_APP(MyApp)
46
47 MyCanvas *myCanvas = (MyCanvas *) NULL;
48
49 // `Main program' equivalent, creating windows and returning main app frame
50 bool MyApp::OnInit(void)
51 {
52 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
53 wxConvCurrent = &wxConvLibc;
54 #endif
55
56 m_canvasTextColour = wxColour("BLACK");
57 m_canvasFont = *wxNORMAL_FONT;
58
59 // Create the main frame window
60 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
61
62 // Make a menubar
63 wxMenu *file_menu = new wxMenu;
64
65 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
66
67 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
68 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
69 #endif
70
71 file_menu->AppendSeparator();
72 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
73
74 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
75 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
76
77 #endif
78 file_menu->AppendSeparator();
79 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box");
80 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry");
81 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice");
82 file_menu->Append(DIALOGS_EXT_DIALOG, "&Extended dialog");
83 file_menu->AppendSeparator();
84 file_menu->Append(DIALOGS_TIP, "&Tip of the day");
85 file_menu->AppendSeparator();
86 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file");
87 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file");
88 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory");
89 file_menu->AppendSeparator();
90 file_menu->Append(wxID_EXIT, "E&xit");
91 wxMenuBar *menu_bar = new wxMenuBar;
92 menu_bar->Append(file_menu, "&File");
93 frame->SetMenuBar(menu_bar);
94
95 myCanvas = new MyCanvas(frame);
96 myCanvas->SetBackgroundColour(*wxWHITE);
97
98 frame->Centre(wxBOTH);
99
100 // Show the frame
101 frame->Show(TRUE);
102
103 SetTopWindow(frame);
104
105 return TRUE;
106 }
107
108 // My frame constructor
109 MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
110 wxFrame(parent, -1, title, pos, size)
111 {}
112
113 void MyFrame::ExtDialog(wxCommandEvent& WXUNUSED(event) )
114 {
115 // The standard flags causes this dialog to display a
116 // wxStaticLine under wxMotif and wxGTK, but none under
117 // other platforms. Also, it will not be resizable
118 // anywhere.
119
120 wxExtDialog dialog( this, -1, "Test 1 for wxExtDialog",
121 wxOK|wxFORWARD|wxBACKWARD );
122
123 dialog.SetClientWindow( new wxTextCtrl( &dialog, -1, "Test", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ) );
124
125 // query minimal recommended size from the buttons
126 dialog.SetSize( dialog.GetButtonAreaSize().x, 170 );
127
128 dialog.Centre( wxBOTH );
129 dialog.ShowModal();
130
131 // This dialog uses the standard dialog styles but is also
132 // resizable on all platforms and shows a wxStaticLine on
133 // all platforms.
134
135 wxExtDialog dialog2( this, -1, "Test 2 for wxExtDialog",
136 wxOK|wxFORWARD|wxBACKWARD|wxCANCEL, wxDefaultPosition, wxSize(400,170),
137 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxED_BUTTONS_RIGHT | wxED_STATIC_LINE | wxED_CLIENT_MARGIN );
138
139 dialog2.SetClientWindow( new wxTextCtrl( &dialog2, -1, "Test", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ) );
140
141 // query minimal recommended size from the buttons
142 wxSize min_size( dialog2.GetButtonAreaSize() );
143 dialog2.SetSizeHints( min_size.x + 200, min_size.y );
144
145 dialog2.Centre( wxBOTH );
146 dialog2.ShowModal();
147 }
148
149 void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
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 wxColourDialog *dialog = new wxColourDialog(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 }
169 dialog->Destroy();
170 }
171
172 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
173 {
174 wxFontData data;
175 data.SetInitialFont(wxGetApp().m_canvasFont);
176 data.SetColour(wxGetApp().m_canvasTextColour);
177
178 wxFontDialog *dialog = new wxFontDialog(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 }
186 dialog->Destroy();
187 }
188
189 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
190 void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
191 {
192 wxColourData data;
193 data.SetChooseFull(TRUE);
194 for (int i = 0; i < 16; i++)
195 {
196 wxColour colour(i*16, i*16, i*16);
197 data.SetCustomColour(i, colour);
198 }
199
200 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
201 if (dialog->ShowModal() == wxID_OK)
202 {
203 wxColourData retData = dialog->GetColourData();
204 wxColour col = retData.GetColour();
205 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
206 myCanvas->SetBackgroundColour(col);
207 myCanvas->Clear();
208 myCanvas->Refresh();
209 }
210 dialog->Destroy();
211 }
212
213 void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
214 {
215 wxFontData data;
216 data.SetInitialFont(wxGetApp().m_canvasFont);
217 data.SetColour(wxGetApp().m_canvasTextColour);
218
219 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
220 if (dialog->ShowModal() == wxID_OK)
221 {
222 wxFontData retData = dialog->GetFontData();
223 wxGetApp().m_canvasFont = retData.GetChosenFont();
224 wxGetApp().m_canvasTextColour = retData.GetColour();
225 myCanvas->Refresh();
226 }
227 dialog->Destroy();
228 }
229 #endif
230
231 void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
232 {
233 wxMessageDialog dialog(NULL, "This is a message box\nA long, long string to test out the message box properly",
234 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
235
236 dialog.ShowModal();
237 }
238
239 void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
240 {
241 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
242 "Please enter a string", "Default value", wxOK|wxCANCEL);
243
244 if (dialog.ShowModal() == wxID_OK)
245 {
246 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
247 dialog2.ShowModal();
248 }
249 }
250
251 void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
252 {
253 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
254 int n = 5;
255
256 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
257 "Please select a value", n, (const wxString *)choices);
258
259 dialog.SetSelection(2);
260
261 if (dialog.ShowModal() == wxID_OK)
262 {
263 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
264 dialog2.ShowModal();
265 }
266 }
267
268 void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
269 {
270 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
271
272 if (dialog.ShowModal() == wxID_OK)
273 {
274 wxString info;
275 info.Printf(_T("Full file name: %s\n")
276 _T("Path: %s\n")
277 _T("Name: %s"),
278 dialog.GetPath().c_str(),
279 dialog.GetDirectory().c_str(),
280 dialog.GetFilename().c_str());
281 wxMessageDialog dialog2(this, info, "Selected file");
282 dialog2.ShowModal();
283 }
284 }
285
286 void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
287 {
288 wxFileDialog dialog(this, "Testing save file dialog", "", "",
289 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
290 wxSAVE|wxOVERWRITE_PROMPT);
291
292 if (dialog.ShowModal() == wxID_OK)
293 {
294 wxChar buf[400];
295 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
296 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
297 dialog2.ShowModal();
298 }
299 }
300
301 void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
302 {
303 wxDirDialog dialog(this, "Testing directory picker", "");
304
305 if (dialog.ShowModal() == wxID_OK)
306 {
307 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
308 dialog2.ShowModal();
309 }
310 }
311
312 void MyFrame::ShowTip(wxCommandEvent& event)
313 {
314 static size_t s_index = (size_t)-1;
315
316 if ( s_index == (size_t)-1 )
317 {
318 srand(time(NULL));
319
320 // this is completely bogus, we don't know how many lines are there
321 // in the file, but who cares, it's a demo only...
322 s_index = rand() % 5;
323 }
324
325 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
326
327 bool showAtStartup = wxShowTip(this, tipProvider);
328
329 if ( showAtStartup )
330 {
331 wxMessageBox("Will show tips on startup", "Tips dialog",
332 wxOK | wxICON_INFORMATION, this);
333 }
334
335 s_index = tipProvider->GetCurrentTip();
336 delete tipProvider;
337 }
338
339 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
340 {
341 Close(TRUE);
342 }
343
344 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
345 {
346 wxPaintDC dc(this);
347 dc.SetFont(wxGetApp().m_canvasFont);
348 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
349 dc.SetBackgroundMode(wxTRANSPARENT);
350 dc.DrawText("wxWindows common dialogs test application", 10, 10);
351 }
352
353 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
354 EVT_PAINT(MyCanvas::OnPaint)
355 END_EVENT_TABLE()
356
357 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
358 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
359 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
360 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
361 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
362 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
363 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
364 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
365 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
366 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
367 EVT_MENU(DIALOGS_EXT_DIALOG, MyFrame::ExtDialog)
368 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
369 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
370 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
371 #endif
372 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
373 END_EVENT_TABLE()
374