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(__WINDOWS__) || 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
= *wxNORMAL_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(__WINDOWS__) || 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(__WINDOWS__) || 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
& 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
& 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(__WINDOWS__) || USE_GENERIC_DIALOGS_IN_MSW
147 void MyFrame::ChooseColourGeneric(wxCommandEvent
& 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
& 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
& 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
& 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
& 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 if (dialog
.ShowModal() == wxID_OK
)
218 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
223 void MyFrame::FileOpen(wxCommandEvent
& event
)
225 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
227 if (dialog
.ShowModal() == wxID_OK
)
229 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
234 void MyFrame::FileSave(wxCommandEvent
& event
)
236 wxFileDialog
dialog(this, "Testing save file dialog", "", "",
237 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
238 wxSAVE
|wxOVERWRITE_PROMPT
);
240 if (dialog
.ShowModal() == wxID_OK
)
243 sprintf(buf
, "%s, filter %d", (const char *)dialog
.GetPath(), dialog
.GetFilterIndex());
244 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
249 void MyFrame::DirChoose(wxCommandEvent
& event
)
251 wxDirDialog
dialog(this, "Testing directory picker", "");
253 if (dialog
.ShowModal() == wxID_OK
)
255 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
260 void MyFrame::OnExit(wxCommandEvent
& event
)
265 void MyCanvas::OnPaint(wxPaintEvent
& event
)
268 dc
.SetFont(wxGetApp().m_canvasFont
);
269 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
270 dc
.SetBackgroundMode(wxTRANSPARENT
);
271 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
274 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
275 EVT_PAINT(MyCanvas::OnPaint
)
278 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
279 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
280 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
281 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
282 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
283 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
284 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
285 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
286 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
287 #if !defined(__WINDOWS__) || USE_GENERIC_DIALOGS_IN_MSW
288 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
289 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
291 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)