1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Common dialogs demo
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
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"
36 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
38 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
39 #include <wx/generic/colrdlgg.h>
40 #include <wx/generic/fontdlgg.h>
47 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
49 // `Main program' equivalent, creating windows and returning main app frame
50 bool MyApp::OnInit(void)
52 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
53 wxConvCurrent
= &wxConvLibc
;
56 m_canvasTextColour
= wxColour("BLACK");
57 m_canvasFont
= *wxNORMAL_FONT
;
59 // Create the main frame window
60 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
63 wxMenu
*file_menu
= new wxMenu
;
65 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
67 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
68 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
71 file_menu
->AppendSeparator();
72 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
74 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
75 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
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_NUM_ENTRY
, "&Numeric entry\tCtrl-N");
82 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice");
83 file_menu
->Append(DIALOGS_EXT_DIALOG
, "&Extended dialog");
84 file_menu
->AppendSeparator();
85 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day");
86 file_menu
->AppendSeparator();
87 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file");
88 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file");
89 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory");
90 file_menu
->AppendSeparator();
91 file_menu
->Append(wxID_EXIT
, "E&xit");
92 wxMenuBar
*menu_bar
= new wxMenuBar
;
93 menu_bar
->Append(file_menu
, "&File");
94 frame
->SetMenuBar(menu_bar
);
96 myCanvas
= new MyCanvas(frame
);
97 myCanvas
->SetBackgroundColour(*wxWHITE
);
99 frame
->Centre(wxBOTH
);
109 // My frame constructor
110 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
111 wxFrame(parent
, -1, title
, pos
, size
)
114 void MyFrame::ExtDialog(wxCommandEvent
& WXUNUSED(event
) )
116 // The standard flags causes this dialog to display a
117 // wxStaticLine under wxMotif and wxGTK, but none under
118 // other platforms. Also, it will not be resizable
121 wxExtDialog
dialog( this, -1, "Test 1 for wxExtDialog", wxOK
|wxFORWARD
|wxBACKWARD
);
122 dialog
.AddButton( new wxButton( &dialog
, -1, "Custom") );
124 dialog
.SetClientWindow( new wxTextCtrl( &dialog
, -1, "Test", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
) );
126 // query minimal recommended size from the buttons
127 dialog
.SetSize( dialog
.GetButtonAreaSize().x
, 170 );
129 dialog
.Centre( wxBOTH
);
132 // This dialog uses the standard dialog styles but is also
133 // resizable on all platforms and shows a wxStaticLine on
136 wxExtDialog
dialog2( this, -1, "Test 2 for wxExtDialog",
137 wxOK
|wxFORWARD
|wxBACKWARD
|wxCANCEL
,
138 wxDefaultPosition
, wxSize(400,170),
139 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
| wxED_BUTTONS_RIGHT
| wxED_STATIC_LINE
| wxED_CLIENT_MARGIN
);
141 dialog2
.SetClientWindow( new wxTextCtrl( &dialog2
, -1, "Test", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
) );
143 // query minimal recommended size from the buttons
144 wxSize
min_size( dialog2
.GetButtonAreaSize() );
145 dialog2
.SetSizeHints( min_size
.x
+ 200, min_size
.y
);
147 dialog2
.Centre( wxBOTH
);
151 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
154 data
.SetChooseFull(TRUE
);
155 for (int i
= 0; i
< 16; i
++)
157 wxColour
colour(i
*16, i
*16, i
*16);
158 data
.SetCustomColour(i
, colour
);
161 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
162 if (dialog
->ShowModal() == wxID_OK
)
164 wxColourData retData
= dialog
->GetColourData();
165 wxColour col
= retData
.GetColour();
166 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
167 myCanvas
->SetBackgroundColour(col
);
174 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
177 data
.SetInitialFont(wxGetApp().m_canvasFont
);
178 data
.SetColour(wxGetApp().m_canvasTextColour
);
180 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
181 if (dialog
->ShowModal() == wxID_OK
)
183 wxFontData retData
= dialog
->GetFontData();
184 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
185 wxGetApp().m_canvasTextColour
= retData
.GetColour();
191 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
192 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
195 data
.SetChooseFull(TRUE
);
196 for (int i
= 0; i
< 16; i
++)
198 wxColour
colour(i
*16, i
*16, i
*16);
199 data
.SetCustomColour(i
, colour
);
202 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
203 if (dialog
->ShowModal() == wxID_OK
)
205 wxColourData retData
= dialog
->GetColourData();
206 wxColour col
= retData
.GetColour();
207 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
208 myCanvas
->SetBackgroundColour(col
);
215 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
218 data
.SetInitialFont(wxGetApp().m_canvasFont
);
219 data
.SetColour(wxGetApp().m_canvasTextColour
);
221 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
222 if (dialog
->ShowModal() == wxID_OK
)
224 wxFontData retData
= dialog
->GetFontData();
225 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
226 wxGetApp().m_canvasTextColour
= retData
.GetColour();
233 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
235 wxMessageDialog
dialog(NULL
, "This is a message box\nA long, long string to test out the message box properly",
236 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
241 void MyFrame::NumericEntry(wxCommandEvent
& WXUNUSED(event
) )
243 long res
= wxGetNumberFromUser("", "Enter a number:", "Numeric input test",
250 msg
= "Invalid number entered or dialog cancelled.";
255 msg
.Printf(_T("You've entered %lu"), res
);
256 icon
= wxICON_INFORMATION
;
259 wxMessageBox(msg
, "Numeric test result", wxOK
| icon
, this);
262 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
) )
264 wxTextEntryDialog
dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
265 "Please enter a string", "Default value", wxOK
|wxCANCEL
);
267 if (dialog
.ShowModal() == wxID_OK
)
269 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
274 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
276 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
279 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
280 "Please select a value", n
, (const wxString
*)choices
);
282 dialog
.SetSelection(2);
284 if (dialog
.ShowModal() == wxID_OK
)
286 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
291 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
293 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
295 if (dialog
.ShowModal() == wxID_OK
)
298 info
.Printf(_T("Full file name: %s\n")
301 dialog
.GetPath().c_str(),
302 dialog
.GetDirectory().c_str(),
303 dialog
.GetFilename().c_str());
304 wxMessageDialog
dialog2(this, info
, "Selected file");
309 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
311 wxFileDialog
dialog(this, "Testing save file dialog", "", "",
312 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
313 wxSAVE
|wxOVERWRITE_PROMPT
);
315 if (dialog
.ShowModal() == wxID_OK
)
318 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
319 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
324 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
326 wxDirDialog
dialog(this, "Testing directory picker", "");
328 if (dialog
.ShowModal() == wxID_OK
)
330 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
335 void MyFrame::ShowTip(wxCommandEvent
& event
)
337 #if wxUSE_STARTUP_TIPS
338 static size_t s_index
= (size_t)-1;
340 if ( s_index
== (size_t)-1 )
344 // this is completely bogus, we don't know how many lines are there
345 // in the file, but who cares, it's a demo only...
346 s_index
= rand() % 5;
349 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
351 bool showAtStartup
= wxShowTip(this, tipProvider
);
355 wxMessageBox("Will show tips on startup", "Tips dialog",
356 wxOK
| wxICON_INFORMATION
, this);
359 s_index
= tipProvider
->GetCurrentTip();
364 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
369 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
372 dc
.SetFont(wxGetApp().m_canvasFont
);
373 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
374 dc
.SetBackgroundMode(wxTRANSPARENT
);
375 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
378 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
379 EVT_PAINT(MyCanvas::OnPaint
)
382 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
383 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
384 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
385 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
386 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
387 EVT_MENU(DIALOGS_NUM_ENTRY
, MyFrame::NumericEntry
)
388 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
389 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
390 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
391 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
392 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
393 EVT_MENU(DIALOGS_EXT_DIALOG
, MyFrame::ExtDialog
)
394 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
395 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
396 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
398 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)