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