]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/dialogs/dialogs.cpp
no changes, testing cvs logging
[wxWidgets.git] / samples / dialogs / dialogs.cpp
... / ...
CommitLineData
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/progdlg.h"
35
36// New wxGenericDirCtrl
37#include "wx/dirctrl.h"
38
39#define wxTEST_GENERIC_DIALOGS_IN_MSW 0
40
41#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
42#include <wx/generic/colrdlgg.h>
43#include <wx/generic/fontdlgg.h>
44#endif
45
46#include "dialogs.h"
47
48IMPLEMENT_APP(MyApp)
49
50BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
51 EVT_PAINT(MyCanvas::OnPaint)
52END_EVENT_TABLE()
53
54BEGIN_EVENT_TABLE(MyFrame, wxFrame)
55 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
56 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
57 EVT_MENU(DIALOGS_LOG_DIALOG, MyFrame::LogDialog)
58 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
59 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
60 EVT_MENU(DIALOGS_PASSWORD_ENTRY, MyFrame::PasswordEntry)
61 EVT_MENU(DIALOGS_NUM_ENTRY, MyFrame::NumericEntry)
62 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
63 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
64 EVT_MENU(DIALOGS_FILES_OPEN, MyFrame::FilesOpen)
65 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
66 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
67 EVT_MENU(DIALOGS_GENERIC_DIR_CHOOSE, MyFrame::GenericDirChoose)
68 EVT_MENU(DIALOGS_MODAL, MyFrame::ModalDlg)
69 EVT_MENU(DIALOGS_MODELESS, MyFrame::ModelessDlg)
70 EVT_MENU(DIALOGS_TIP, MyFrame::ShowTip)
71#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
72 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
73 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
74#endif
75#if wxUSE_PROGRESSDLG
76 EVT_MENU(DIALOGS_PROGRESS, MyFrame::ShowProgress)
77#endif
78 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
79
80 EVT_BUTTON(DIALOGS_MODELESS_BTN, MyFrame::OnButton)
81END_EVENT_TABLE()
82
83BEGIN_EVENT_TABLE(MyModalDialog, wxDialog)
84 EVT_BUTTON(-1, MyModalDialog::OnButton)
85END_EVENT_TABLE()
86
87BEGIN_EVENT_TABLE(MyModelessDialog, wxDialog)
88 EVT_CLOSE(MyModelessDialog::OnClose)
89END_EVENT_TABLE()
90
91MyCanvas *myCanvas = (MyCanvas *) NULL;
92
93// `Main program' equivalent, creating windows and returning main app frame
94bool MyApp::OnInit()
95{
96#if defined(__WXGTK__) && defined(wxUSE_UNICODE)
97 wxConvCurrent = &wxConvLibc;
98#endif
99
100 m_canvasTextColour = wxColour("BLACK");
101 m_canvasFont = *wxNORMAL_FONT;
102
103 // Create the main frame window
104 MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows dialogs example", wxPoint(20, 20), wxSize(400, 300));
105
106 // Make a menubar
107 wxMenu *file_menu = new wxMenu;
108
109 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
110
111#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
112 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
113#endif
114
115 file_menu->AppendSeparator();
116 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
117
118#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
119 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
120
121#endif
122 file_menu->AppendSeparator();
123 file_menu->Append(DIALOGS_LOG_DIALOG, "&Log dialog\tCtrl-L");
124 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box\tCtrl-M");
125 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry\tCtrl-E");
126 file_menu->Append(DIALOGS_PASSWORD_ENTRY, "&Password entry\tCtrl-P");
127 file_menu->Append(DIALOGS_NUM_ENTRY, "&Numeric entry\tCtrl-N");
128 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice\tCtrl-C");
129 file_menu->AppendSeparator();
130 file_menu->Append(DIALOGS_TIP, "&Tip of the day\tCtrl-T");
131 file_menu->AppendSeparator();
132 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file\tCtrl-O");
133 file_menu->Append(DIALOGS_FILES_OPEN, "Open &files\tCtrl-Q");
134 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file\tCtrl-S");
135 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory\tCtrl-D");
136 file_menu->Append(DIALOGS_GENERIC_DIR_CHOOSE, "&Choose a directory (generic implementation)");
137#if wxUSE_PROGRESSDLG
138 file_menu->Append(DIALOGS_PROGRESS, "Pro&gress dialog\tCtrl-G");
139#endif // wxUSE_PROGRESSDLG
140 file_menu->AppendSeparator();
141 file_menu->Append(DIALOGS_MODAL, "Mo&dal dialog\tCtrl-F");
142 file_menu->Append(DIALOGS_MODELESS, "Modeless &dialog\tCtrl-Z", "", TRUE);
143 file_menu->AppendSeparator();
144 file_menu->Append(wxID_EXIT, "E&xit\tAlt-X");
145 wxMenuBar *menu_bar = new wxMenuBar;
146 menu_bar->Append(file_menu, "&File");
147 frame->SetMenuBar(menu_bar);
148
149 myCanvas = new MyCanvas(frame);
150 myCanvas->SetBackgroundColour(*wxWHITE);
151
152 frame->Centre(wxBOTH);
153
154 // Show the frame
155 frame->Show(TRUE);
156
157 SetTopWindow(frame);
158
159 return TRUE;
160}
161
162// My frame constructor
163MyFrame::MyFrame(wxWindow *parent,
164 const wxString& title,
165 const wxPoint& pos,
166 const wxSize& size)
167 : wxFrame(parent, -1, title, pos, size)
168{
169 m_dialog = (MyModelessDialog *)NULL;
170}
171
172void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
173{
174 wxColourData data;
175 data.SetChooseFull(TRUE);
176 for (int i = 0; i < 16; i++)
177 {
178 wxColour colour(i*16, i*16, i*16);
179 data.SetCustomColour(i, colour);
180 }
181
182 wxColourDialog *dialog = new wxColourDialog(this, &data);
183 dialog->SetTitle("Choose the background colour");
184 if (dialog->ShowModal() == wxID_OK)
185 {
186 wxColourData retData = dialog->GetColourData();
187 wxColour col = retData.GetColour();
188 myCanvas->SetBackgroundColour(col);
189 myCanvas->Clear();
190 myCanvas->Refresh();
191 }
192 dialog->Destroy();
193}
194
195void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
196{
197 wxFontData data;
198 data.SetInitialFont(wxGetApp().m_canvasFont);
199 data.SetColour(wxGetApp().m_canvasTextColour);
200
201 wxFontDialog *dialog = new wxFontDialog(this, &data);
202 if (dialog->ShowModal() == wxID_OK)
203 {
204 wxFontData retData = dialog->GetFontData();
205 wxGetApp().m_canvasFont = retData.GetChosenFont();
206 wxGetApp().m_canvasTextColour = retData.GetColour();
207 myCanvas->Refresh();
208 }
209 dialog->Destroy();
210}
211
212#if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
213void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
214{
215 wxColourData data;
216 data.SetChooseFull(TRUE);
217 for (int i = 0; i < 16; i++)
218 {
219 wxColour colour(i*16, i*16, i*16);
220 data.SetCustomColour(i, colour);
221 }
222
223 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
224 if (dialog->ShowModal() == wxID_OK)
225 {
226 wxColourData retData = dialog->GetColourData();
227 wxColour col = retData.GetColour();
228// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
229 myCanvas->SetBackgroundColour(col);
230 myCanvas->Clear();
231 myCanvas->Refresh();
232 }
233 dialog->Destroy();
234}
235
236void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
237{
238 wxFontData data;
239 data.SetInitialFont(wxGetApp().m_canvasFont);
240 data.SetColour(wxGetApp().m_canvasTextColour);
241
242 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
243 if (dialog->ShowModal() == wxID_OK)
244 {
245 wxFontData retData = dialog->GetFontData();
246 wxGetApp().m_canvasFont = retData.GetChosenFont();
247 wxGetApp().m_canvasTextColour = retData.GetColour();
248 myCanvas->Refresh();
249 }
250 dialog->Destroy();
251}
252#endif // wxTEST_GENERIC_DIALOGS_IN_MSW
253
254void MyFrame::LogDialog(wxCommandEvent& event)
255{
256 // calling wxYield() (as ~wxBusyCursor does) shouldn't result in messages
257 // being flushed -- test it
258 {
259 wxBusyCursor bc;
260 wxLogMessage("This is some message - everything is ok so far.");
261 wxLogMessage("Another message...\n... this one is on multiple lines");
262 wxLogWarning("And then something went wrong!");
263 }
264
265 wxLogError("Intermediary error handler decided to abort.");
266 wxLogError("The top level caller detected an unrecoverable error.");
267
268 wxLog::FlushActive();
269
270 wxLogMessage("And this is the same dialog but with only one message.");
271}
272
273void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
274{
275 wxMessageDialog dialog( NULL, "This is a message box\nA long, long string to test out the message box properly",
276 "Message box text", wxYES_NO|wxCANCEL|wxICON_INFORMATION);
277
278 dialog.ShowModal();
279}
280
281void MyFrame::NumericEntry(wxCommandEvent& WXUNUSED(event) )
282{
283 long res = wxGetNumberFromUser( "This is some text, actually a lot of text.\n"
284 "Even two rows of text.",
285 "Enter a number:", "Numeric input test",
286 50, 0, 100, this );
287
288 wxString msg;
289 int icon;
290 if ( res == -1 )
291 {
292 msg = "Invalid number entered or dialog cancelled.";
293 icon = wxICON_HAND;
294 }
295 else
296 {
297 msg.Printf(_T("You've entered %lu"), res );
298 icon = wxICON_INFORMATION;
299 }
300
301 wxMessageBox(msg, "Numeric test result", wxOK | icon, this);
302}
303
304void MyFrame::PasswordEntry(wxCommandEvent& WXUNUSED(event))
305{
306 wxString pwd = wxGetPasswordFromUser("Enter password:",
307 "Password entry dialog",
308 "",
309 this);
310 if ( !!pwd )
311 {
312 wxMessageBox(wxString::Format("Your password is '%s'", pwd.c_str()),
313 "Got password", wxOK | wxICON_INFORMATION, this);
314 }
315}
316
317void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event))
318{
319 wxTextEntryDialog dialog(this,
320 "This is a small sample\n"
321 "A long, long string to test out the text entrybox",
322 "Please enter a string",
323 "Default value",
324 wxOK | wxCANCEL);
325
326 if (dialog.ShowModal() == wxID_OK)
327 {
328 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
329 dialog2.ShowModal();
330 }
331}
332
333void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
334{
335 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
336 int n = 5;
337
338 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
339 "Please select a value", n, (const wxString *)choices);
340
341 dialog.SetSelection(2);
342
343 if (dialog.ShowModal() == wxID_OK)
344 {
345 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
346 dialog2.ShowModal();
347 }
348}
349
350void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
351{
352 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
353
354 if (dialog.ShowModal() == wxID_OK)
355 {
356 wxString info;
357 info.Printf(_T("Full file name: %s\n")
358 _T("Path: %s\n")
359 _T("Name: %s"),
360 dialog.GetPath().c_str(),
361 dialog.GetDirectory().c_str(),
362 dialog.GetFilename().c_str());
363 wxMessageDialog dialog2(this, info, "Selected file");
364 dialog2.ShowModal();
365 }
366}
367
368void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
369{
370 wxFileDialog dialog(this, "Testing open multiple file dialog",
371 "", "", wxFileSelectorDefaultWildcardStr,
372 wxMULTIPLE);
373
374 if (dialog.ShowModal() == wxID_OK)
375 {
376 wxArrayString paths, filenames;
377
378 dialog.GetPaths(paths);
379 dialog.GetFilenames(filenames);
380
381 wxString msg, s;
382 size_t count = paths.GetCount();
383 for ( size_t n = 0; n < count; n++ )
384 {
385 s.Printf(_T("File %d: %s (%s)\n"),
386 n, paths[n].c_str(), filenames[n].c_str());
387
388 msg += s;
389 }
390
391 wxMessageDialog dialog2(this, msg, "Selected files");
392 dialog2.ShowModal();
393 }
394}
395
396void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
397{
398 wxFileDialog dialog(this, "Testing save file dialog", "", "myletter.txt",
399 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
400 wxSAVE|wxOVERWRITE_PROMPT);
401
402 if (dialog.ShowModal() == wxID_OK)
403 {
404 wxChar buf[400];
405 wxSprintf(buf, _T("%s, filter %d"), (const wxChar*)dialog.GetPath(), dialog.GetFilterIndex());
406 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
407 dialog2.ShowModal();
408 }
409}
410
411void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
412{
413 // pass some initial dir to wxDirDialog
414 wxString dirHome;
415 wxGetHomeDir(&dirHome);
416
417 wxDirDialog dialog(this, "Testing directory picker", dirHome);
418
419 if (dialog.ShowModal() == wxID_OK)
420 {
421 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
422 dialog2.ShowModal();
423 }
424}
425
426void MyFrame::GenericDirChoose(wxCommandEvent& WXUNUSED(event) )
427{
428 // pass some initial dir to wxDirDialog
429 wxString dirHome;
430 wxGetHomeDir(&dirHome);
431
432 wxGenericDirDialog dialog(this, "Testing generic directory picker", dirHome);
433
434 if (dialog.ShowModal() == wxID_OK)
435 {
436 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
437 dialog2.ShowModal();
438 }
439}
440
441void MyFrame::ModalDlg(wxCommandEvent& WXUNUSED(event))
442{
443 MyModalDialog dlg(this);
444 dlg.ShowModal();
445}
446
447void MyFrame::ModelessDlg(wxCommandEvent& event)
448{
449 bool show = GetMenuBar()->IsChecked(event.GetInt());
450
451 if ( show )
452 {
453 if ( !m_dialog )
454 {
455 m_dialog = new MyModelessDialog(this);
456 }
457
458 m_dialog->Show(TRUE);
459 }
460 else // hide
461 {
462 m_dialog->Hide();
463 }
464}
465
466void MyFrame::OnButton(wxCommandEvent& WXUNUSED(event))
467{
468 wxMessageBox("Button pressed in modeless dialog", "Info",
469 wxOK | wxICON_INFORMATION, this);
470}
471
472void MyFrame::ShowTip(wxCommandEvent& event)
473{
474#if wxUSE_STARTUP_TIPS
475 static size_t s_index = (size_t)-1;
476
477 if ( s_index == (size_t)-1 )
478 {
479 srand(time(NULL));
480
481 // this is completely bogus, we don't know how many lines are there
482 // in the file, but who cares, it's a demo only...
483 s_index = rand() % 5;
484 }
485
486 wxTipProvider *tipProvider = wxCreateFileTipProvider("tips.txt", s_index);
487
488 bool showAtStartup = wxShowTip(this, tipProvider);
489
490 if ( showAtStartup )
491 {
492 wxMessageBox("Will show tips on startup", "Tips dialog",
493 wxOK | wxICON_INFORMATION, this);
494 }
495
496 s_index = tipProvider->GetCurrentTip();
497 delete tipProvider;
498#endif
499}
500
501void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
502{
503 Close(TRUE);
504}
505
506#if wxUSE_PROGRESSDLG
507
508void MyFrame::ShowProgress( wxCommandEvent& WXUNUSED(event) )
509{
510 static const int max = 10;
511
512 wxProgressDialog dialog("Progress dialog example",
513 "An informative message",
514 max, // range
515 this, // parent
516 wxPD_CAN_ABORT |
517 wxPD_APP_MODAL |
518 wxPD_ELAPSED_TIME |
519 wxPD_ESTIMATED_TIME |
520 wxPD_REMAINING_TIME);
521
522 bool cont = TRUE;
523 for ( int i = 0; i <= max && cont; i++ )
524 {
525 wxSleep(1);
526 if ( i == max )
527 {
528 cont = dialog.Update(i, "That's all, folks!");
529 }
530 else if ( i == max / 2 )
531 {
532 cont = dialog.Update(i, "Only a half left (very long message)!");
533 }
534 else
535 {
536 cont = dialog.Update(i);
537 }
538 }
539
540 if ( !cont )
541 {
542 wxLogStatus("Progress dialog aborted!");
543 }
544 else
545 {
546 wxLogStatus("Countdown from %d finished", max);
547 }
548}
549
550#endif // wxUSE_PROGRESSDLG
551
552// ----------------------------------------------------------------------------
553// MyCanvas
554// ----------------------------------------------------------------------------
555
556void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
557{
558 wxPaintDC dc(this);
559 dc.SetFont(wxGetApp().m_canvasFont);
560 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
561 dc.SetBackgroundMode(wxTRANSPARENT);
562 dc.DrawText("wxWindows common dialogs test application", 10, 10);
563}
564
565// ----------------------------------------------------------------------------
566// MyModelessDialog
567// ----------------------------------------------------------------------------
568
569MyModelessDialog::MyModelessDialog(wxWindow *parent)
570 : wxDialog(parent, -1, wxString("Modeless dialog"))
571{
572 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
573
574 wxButton *btn = new wxButton(this, DIALOGS_MODELESS_BTN, "Press me");
575 wxCheckBox *check = new wxCheckBox(this, -1, "Should be disabled");
576 check->Disable();
577
578 sizerTop->Add(btn, 1, wxEXPAND | wxALL, 5);
579 sizerTop->Add(check, 1, wxEXPAND | wxALL, 5);
580
581 SetAutoLayout(TRUE);
582 SetSizer(sizerTop);
583
584 sizerTop->SetSizeHints(this);
585 sizerTop->Fit(this);
586}
587
588void MyModelessDialog::OnClose(wxCloseEvent& event)
589{
590 if ( event.CanVeto() )
591 {
592 wxMessageBox("Use the menu item to close this dialog",
593 "Modeless dialog",
594 wxOK | wxICON_INFORMATION, this);
595
596 event.Veto();
597 }
598}
599
600// ----------------------------------------------------------------------------
601// MyModalDialog
602// ----------------------------------------------------------------------------
603
604MyModalDialog::MyModalDialog(wxWindow *parent)
605 : wxDialog(parent, -1, wxString("Modal dialog"))
606{
607 wxBoxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
608
609 m_btnFocused = new wxButton(this, -1, "Default button");
610 m_btnDelete = new wxButton(this, -1, "&Delete button");
611 sizerTop->Add(m_btnFocused, 0, wxALIGN_CENTER | wxALL, 5);
612 sizerTop->Add(m_btnDelete, 0, wxALIGN_CENTER | wxALL, 5);
613
614 SetAutoLayout(TRUE);
615 SetSizer(sizerTop);
616
617 sizerTop->SetSizeHints(this);
618 sizerTop->Fit(this);
619
620 m_btnFocused->SetFocus();
621 m_btnFocused->SetDefault();
622}
623
624void MyModalDialog::OnButton(wxCommandEvent& event)
625{
626 if ( event.GetEventObject() == m_btnDelete )
627 {
628 delete m_btnFocused;
629 m_btnFocused = NULL;
630
631 m_btnDelete->Disable();
632 }
633 else if ( event.GetEventObject() == m_btnFocused )
634 {
635 wxGetTextFromUser("Dummy prompt", "Modal dialog called from dialog",
636 "", this);
637 }
638 else
639 {
640 event.Skip();
641 }
642}