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>
34 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
35 #include <wx/generic/colrdlgg.h>
36 #include <wx/generic/fontdlgg.h>
43 MyCanvas
*myCanvas
= NULL
;
45 // A macro needed for some compilers (AIX) that need 'main' to be defined
46 // in the application itself.
49 // `Main program' equivalent, creating windows and returning main app frame
50 bool MyApp::OnInit(void)
52 m_canvasTextColour
= wxColour("BLACK");
53 m_canvasFont
= *wxSWISS_FONT
;
55 // Create the main frame window
56 MyFrame
*frame
= new MyFrame(NULL
, "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
59 wxMenu
*file_menu
= new wxMenu
;
61 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
63 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
64 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
67 file_menu
->AppendSeparator();
68 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
70 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
71 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
74 file_menu
->AppendSeparator();
75 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box");
76 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry");
77 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice");
78 file_menu
->AppendSeparator();
79 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file");
80 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file");
81 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory");
82 file_menu
->AppendSeparator();
83 file_menu
->Append(wxID_EXIT
, "E&xit");
84 wxMenuBar
*menu_bar
= new wxMenuBar
;
85 menu_bar
->Append(file_menu
, "&File");
86 frame
->SetMenuBar(menu_bar
);
88 myCanvas
= new MyCanvas(frame
);
89 myCanvas
->SetBackgroundColour(*wxWHITE
);
91 frame
->Centre(wxBOTH
);
101 // My frame constructor
102 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
103 wxFrame(parent
, -1, title
, pos
, size
)
106 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
109 data
.SetChooseFull(TRUE
);
110 for (int i
= 0; i
< 16; i
++)
112 wxColour
colour(i
*16, i
*16, i
*16);
113 data
.SetCustomColour(i
, colour
);
116 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
117 if (dialog
->ShowModal() == wxID_OK
)
119 wxColourData retData
= dialog
->GetColourData();
120 wxColour col
= retData
.GetColour();
121 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
122 myCanvas
->SetBackgroundColour(col
);
129 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
132 data
.SetInitialFont(wxGetApp().m_canvasFont
);
133 data
.SetColour(wxGetApp().m_canvasTextColour
);
135 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
136 if (dialog
->ShowModal() == wxID_OK
)
138 wxFontData retData
= dialog
->GetFontData();
139 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
140 wxGetApp().m_canvasTextColour
= retData
.GetColour();
146 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
147 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
150 data
.SetChooseFull(TRUE
);
151 for (int i
= 0; i
< 16; i
++)
153 wxColour
colour(i
*16, i
*16, i
*16);
154 data
.SetCustomColour(i
, colour
);
157 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
158 if (dialog
->ShowModal() == wxID_OK
)
160 wxColourData retData
= dialog
->GetColourData();
161 wxColour col
= retData
.GetColour();
162 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
163 myCanvas
->SetBackgroundColour(col
);
170 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
173 data
.SetInitialFont(wxGetApp().m_canvasFont
);
174 data
.SetColour(wxGetApp().m_canvasTextColour
);
176 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
177 if (dialog
->ShowModal() == wxID_OK
)
179 wxFontData retData
= dialog
->GetFontData();
180 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
181 wxGetApp().m_canvasTextColour
= retData
.GetColour();
188 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
190 wxMessageDialog
dialog(this, "This is a message box\nA long, long string to test out the message box properly",
191 "Message box text", wxYES_NO
|wxCANCEL
);
196 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
) )
198 wxTextEntryDialog
dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
199 "Please enter a string", "Default value", wxOK
|wxCANCEL
);
201 if (dialog
.ShowModal() == wxID_OK
)
203 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
208 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
210 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
213 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
214 "Please select a value", n
, (const wxString
*)choices
);
216 dialog
.SetSelection(2);
218 if (dialog
.ShowModal() == wxID_OK
)
220 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
225 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
227 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
229 if (dialog
.ShowModal() == wxID_OK
)
231 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
236 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
238 wxFileDialog
dialog(this, "Testing save file dialog", "", "",
239 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
240 wxSAVE
|wxOVERWRITE_PROMPT
);
242 if (dialog
.ShowModal() == wxID_OK
)
245 sprintf(buf
, "%s, filter %d", (const char *)dialog
.GetPath(), dialog
.GetFilterIndex());
246 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
251 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
254 wxDirDialog
dialog(this, "Testing directory picker", "");
256 if (dialog
.ShowModal() == wxID_OK
)
258 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
264 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
269 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
272 dc
.SetFont(wxGetApp().m_canvasFont
);
273 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
274 dc
.SetBackgroundMode(wxTRANSPARENT
);
275 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
278 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
279 EVT_PAINT(MyCanvas::OnPaint
)
282 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
283 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
284 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
285 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
286 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
287 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
288 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
289 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
290 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
291 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
292 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
293 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
295 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)