]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
Shift edit control (since we shifted cells in last version!)
[wxWidgets.git] / samples / dialogs / dialogs.cpp
CommitLineData
457814b5
JS
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
c50f1fb9 9// Licence: wxWindows license
457814b5
JS
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
329e86bf
RR
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"
457814b5 34
dfad0599
JS
35#define wxTEST_GENERIC_DIALOGS_IN_MSW 0
36
37#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
38#include <wx/generic/colrdlgg.h>
39#include <wx/generic/fontdlgg.h>
40#endif
41
42#include "dialogs.h"
43
44IMPLEMENT_APP(MyApp)
45
4c45f240
VZ
46BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
47 EVT_PAINT(MyCanvas::OnPaint)
48END_EVENT_TABLE()
49
50BEGIN_EVENT_TABLE(MyFrame, wxFrame)
51 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
52 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
53 EVT_MENU(DIALOGS_LOG_DIALOG, MyFrame::LogDialog)
54 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
55 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
56 EVT_MENU(DIALOGS_PASSWORD_ENTRY, MyFrame::PasswordEntry)
57 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
58 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
59 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
60 EVT_MENU(DIALOGS_FILES_OPEN, MyFrame::FilesOpen)
61 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
62 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
63 EVT_MENU(DIALOGS_MODELESS, MyFrame::ModelessDlg)
64 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
65#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
66 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
67 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
68#endif
69 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
70
71 EVT_BUTTON(DIALOGS_MODELESS_BTN, MyFrame::OnButton)
72END_EVENT_TABLE()
c67daf87 73MyCanvas *myCanvas = (MyCanvas *) NULL;
457814b5 74
457814b5 75// `Main program' equivalent, creating windows and returning main app frame
4c45f240 76bool MyApp::OnInit()
457814b5 77{
e5ea3f7a 78#if defined(__WXGTK__) && defined(wxUSE_UNICODE)
dcf924a3 79 wxConvCurrent = &wxConvLibc;
e5ea3f7a
RR
80#endif
81
457814b5 82 m_canvasTextColour = wxColour("BLACK");
66bd6b93 83 m_canvasFont = *wxNORMAL_FONT;
457814b5
JS
84
85 // Create the main frame window
8487f887 86 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
457814b5
JS
87
88 // Make a menubar
89 wxMenu *file_menu = new wxMenu;
90
91 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
92
dfad0599 93#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
94 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
95#endif
96
97 file_menu->AppendSeparator();
98 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
99
dfad0599 100#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
457814b5
JS
101 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
102
103#endif
104 file_menu->AppendSeparator();
d93c719a 105 file_menu->Append(DIALOGS_LOG_DIALOG, "&Log dialog\tCtrl-L");
c61f4f6d
VZ
106 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box\tCtrl-M");
107 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry\tCtrl-E");
a294c6d5 108 file_menu->Append(DIALOGS_PASSWORD_ENTRY, "&Password entry\tCtrl-P");
c49245f8 109 file_menu->Append(DIALOGS_NUM_ENTRY, "&Numeric entry\tCtrl-N");
4c45f240 110 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice\tCtrl-C");
457814b5 111 file_menu->AppendSeparator();
c61f4f6d 112 file_menu->Append(DIALOGS_TIP, "&Tip of the day\tCtrl-T");
c50f1fb9 113 file_menu->AppendSeparator();
c61f4f6d 114 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file\tCtrl-O");
a294c6d5 115 file_menu->Append(DIALOGS_FILES_OPEN, "Open &files\tCtrl-Q");
4c45f240 116 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file\tCtrl-S");
c61f4f6d 117 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory\tCtrl-D");
457814b5 118 file_menu->AppendSeparator();
4c45f240
VZ
119 file_menu->Append(DIALOGS_MODELESS, "Modeless &dialog\tCtrl-Z", "", TRUE);
120 file_menu->AppendSeparator();
c61f4f6d 121 file_menu->Append(wxID_EXIT, "E&xit\tAlt-X");
457814b5
JS
122 wxMenuBar *menu_bar = new wxMenuBar;
123 menu_bar->Append(file_menu, "&File");
124 frame->SetMenuBar(menu_bar);
125
126 myCanvas = new MyCanvas(frame);
127 myCanvas->SetBackgroundColour(*wxWHITE);
128
129 frame->Centre(wxBOTH);
d93c719a 130
457814b5
JS
131 // Show the frame
132 frame->Show(TRUE);
133
134 SetTopWindow(frame);
135
136 return TRUE;
137}
138
139// My frame constructor
4c45f240
VZ
140MyFrame::MyFrame(wxWindow *parent,
141 const wxString& title,
142 const wxPoint& pos,
143 const wxSize& size)
144 : wxFrame(parent, -1, title, pos, size)
145{
146 m_dialog = (MyModelessDialog *)NULL;
147}
457814b5 148
d355d3fe 149void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
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 }
d93c719a 158
457814b5
JS
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 }
5919d76b 169 dialog->Destroy();
457814b5
JS
170}
171
d355d3fe 172void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
173{
174 wxFontData data;
175 data.SetInitialFont(wxGetApp().m_canvasFont);
176 data.SetColour(wxGetApp().m_canvasTextColour);
d93c719a 177
457814b5
JS
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 }
5919d76b 186 dialog->Destroy();
457814b5
JS
187}
188
dfad0599 189#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
d355d3fe 190void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
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 }
d93c719a 199
457814b5
JS
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 }
5919d76b 210 dialog->Destroy();
457814b5
JS
211}
212
d355d3fe 213void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
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 }
5919d76b 227 dialog->Destroy();
457814b5 228}
d93c719a
VZ
229#endif // wxTEST_GENERIC_DIALOGS_IN_MSW
230
231void MyFrame::LogDialog(wxCommandEvent& event)
232{
233 wxLogMessage("This is some message - everything is ok so far.");
234 wxLogMessage("Another message...");
235 wxLogWarning("And then something went wrong!");
236 wxLogError("Intermediary error handler decided to abort.");
237 wxLogError("The top level caller detected an error.");
7923c649
VZ
238
239 wxLog::FlushActive();
240
c86423f9 241 wxLogMessage("And this is the same dialog but with only one message.");
d93c719a 242}
457814b5 243
d355d3fe 244void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5 245{
76380910 246 wxMessageDialog dialog( this, "This is a message box\nA long, long string to test out the message box properly",
c50f1fb9 247 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
457814b5
JS
248
249 dialog.ShowModal();
250}
251
c49245f8
VZ
252void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
253{
e65cc56a
RR
254 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
255 "Even two rows of text.",
d93c719a 256 "Enter a number:", "Numeric input test",
e65cc56a 257 50, 0, 100, this );
c49245f8
VZ
258
259 wxString msg;
260 int icon;
261 if ( res == -1 )
262 {
263 msg = "Invalid number entered or dialog cancelled.";
264 icon = wxICON_HAND;
265 }
266 else
d93c719a
VZ
267 {
268 msg.Printf(_T("You've entered %lu"), res );
c49245f8
VZ
269 icon = wxICON_INFORMATION;
270 }
271
272 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
273}
274
a294c6d5 275void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
457814b5 276{
a294c6d5
VZ
277 wxString pwd = wxGetPasswordFromUser("Enter password:",
278 "Passowrd entry dialog",
279 "",
280 this);
281 if ( !!pwd )
282 {
283 wxMessageBox(wxString::Format("Your password is '%s'", pwd.c_str()),
284 "Got password", wxOK | wxICON_INFORMATION, this);
285 }
286}
287
288void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
289{
290 wxTextEntryDialog dialog(this,
291 "This is a small sample\n"
292 "A long, long string to test out the text entrybox",
293 "Please enter a string",
294 "Default value",
295 wxOK | wxCANCEL);
457814b5
JS
296
297 if (dialog.ShowModal() == wxID_OK)
298 {
c50f1fb9
VZ
299 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
300 dialog2.ShowModal();
457814b5
JS
301 }
302}
303
d355d3fe 304void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 305{
ef77f91e
JS
306 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
307 int n = 5;
457814b5 308
ef77f91e
JS
309 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
310 "Please select a value", n, (const wxString *)choices);
457814b5 311
ef77f91e
JS
312 dialog.SetSelection(2);
313
314 if (dialog.ShowModal() == wxID_OK)
315 {
316 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
317 dialog2.ShowModal();
318 }
457814b5
JS
319}
320
d355d3fe 321void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5 322{
c50f1fb9 323 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
457814b5 324
c50f1fb9
VZ
325 if (dialog.ShowModal() == wxID_OK)
326 {
5919d76b 327 wxString info;
13393ab6
RR
328 info.Printf(_T("Full file name: %s\n")
329 _T("Path: %s\n")
330 _T("Name: %s"),
5919d76b
VZ
331 dialog.GetPath().c_str(),
332 dialog.GetDirectory().c_str(),
333 dialog.GetFilename().c_str());
c50f1fb9
VZ
334 wxMessageDialog dialog2(this, info, "Selected file");
335 dialog2.ShowModal();
336 }
457814b5
JS
337}
338
c61f4f6d
VZ
339void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
340{
341 wxFileDialog dialog(this, "Testing open multiple file dialog",
470caaf9
VZ
342 "", "", wxFileSelectorDefaultWildcardStr,
343 wxMULTIPLE);
c61f4f6d
VZ
344
345 if (dialog.ShowModal() == wxID_OK)
346 {
347 wxArrayString paths, filenames;
348
349 dialog.GetPaths(paths);
350 dialog.GetFilenames(filenames);
351
352 wxString msg, s;
353 size_t count = paths.GetCount();
354 for ( size_t n = 0; n < count; n++ )
355 {
356 s.Printf(_T("File %d: %s (%s)\n"),
357 n, paths[n].c_str(), filenames[n].c_str());
358
359 msg += s;
360 }
361
362 wxMessageDialog dialog2(this, msg, "Selected files");
363 dialog2.ShowModal();
364 }
365}
366
d355d3fe 367void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5 368{
ed58dbea 369 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
c50f1fb9
VZ
370 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
371 wxSAVE|wxOVERWRITE_PROMPT);
372
373 if (dialog.ShowModal() == wxID_OK)
374 {
375 wxChar buf[400];
376 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
377 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
378 dialog2.ShowModal();
379 }
457814b5
JS
380}
381
d355d3fe 382void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 383{
3f2711d5
VZ
384 // pass some initial dir to wxDirDialog
385 wxString dirHome;
386 wxGetHomeDir(&dirHome);
387
388 wxDirDialog dialog(this, "Testing directory picker", dirHome);
c50f1fb9
VZ
389
390 if (dialog.ShowModal() == wxID_OK)
391 {
392 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
393 dialog2.ShowModal();
394 }
395}
396
4c45f240
VZ
397void MyFrame::ModelessDlg(wxCommandEvent& event)
398{
399 bool show = GetMenuBar()->IsChecked(event.GetInt());
400
401 if ( show )
402 {
403 if ( !m_dialog )
404 {
405 m_dialog = new MyModelessDialog(this);
406 }
407
408 m_dialog->Show(TRUE);
409 }
410 else // hide
411 {
412 m_dialog->Hide();
413 }
414}
415
416void MyFrame::OnButton(wxCommandEvent& WXUNUSED(event))
417{
418 wxMessageBox("Button pressed in modeless dialog", "Info",
419 wxOK | wxICON_INFORMATION, this);
420}
421
c50f1fb9
VZ
422void MyFrame::ShowTip(wxCommandEvent& event)
423{
78bd7ed3 424#if wxUSE_STARTUP_TIPS
c50f1fb9
VZ
425 static size_t s_index = (size_t)-1;
426
427 if ( s_index == (size_t)-1 )
428 {
429 srand(time(NULL));
430
431 // this is completely bogus, we don't know how many lines are there
432 // in the file, but who cares, it's a demo only...
433 s_index = rand() % 5;
434 }
435
436 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
437
438 bool showAtStartup = wxShowTip(this, tipProvider);
439
440 if ( showAtStartup )
441 {
442 wxMessageBox("Will show tips on startup", "Tips dialog",
443 wxOK | wxICON_INFORMATION, this);
444 }
457814b5 445
c50f1fb9
VZ
446 s_index = tipProvider->GetCurrentTip();
447 delete tipProvider;
78bd7ed3 448#endif
457814b5
JS
449}
450
d355d3fe 451void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5 452{
c50f1fb9 453 Close(TRUE);
457814b5
JS
454}
455
d355d3fe 456void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5 457{
c50f1fb9 458 wxPaintDC dc(this);
457814b5
JS
459 dc.SetFont(wxGetApp().m_canvasFont);
460 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
461 dc.SetBackgroundMode(wxTRANSPARENT);
462 dc.DrawText("wxWindows common dialogs test application", 10, 10);
463}
464
4c45f240
VZ
465// ----------------------------------------------------------------------------
466// MyModelessDialog
467// ----------------------------------------------------------------------------
457814b5 468
4c45f240
VZ
469MyModelessDialog::MyModelessDialog(wxWindow *parent)
470 : wxDialog(parent, -1, wxString("Modeless dialog"))
471{
472 (void)new wxButton(this, DIALOGS_MODELESS_BTN, "Press me");
473 Fit();
474 Centre();
475}