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 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
36 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
37 #include <wx/generic/colrdlgg.h>
38 #include <wx/generic/fontdlgg.h>
45 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
47 // `Main program' equivalent, creating windows and returning main app frame
48 bool MyApp::OnInit(void)
50 m_canvasTextColour
= wxColour("BLACK");
51 m_canvasFont
= *wxNORMAL_FONT
;
53 // Create the main frame window
54 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
57 wxMenu
*file_menu
= new wxMenu
;
59 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
61 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
62 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
65 file_menu
->AppendSeparator();
66 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
68 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
69 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
72 file_menu
->AppendSeparator();
73 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box");
74 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry");
75 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice");
76 file_menu
->AppendSeparator();
77 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file");
78 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file");
79 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory");
80 file_menu
->AppendSeparator();
81 file_menu
->Append(wxID_EXIT
, "E&xit");
82 wxMenuBar
*menu_bar
= new wxMenuBar
;
83 menu_bar
->Append(file_menu
, "&File");
84 frame
->SetMenuBar(menu_bar
);
86 myCanvas
= new MyCanvas(frame
);
87 myCanvas
->SetBackgroundColour(*wxWHITE
);
89 frame
->Centre(wxBOTH
);
99 // My frame constructor
100 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
101 wxFrame(parent
, -1, title
, pos
, size
)
104 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
107 data
.SetChooseFull(TRUE
);
108 for (int i
= 0; i
< 16; i
++)
110 wxColour
colour(i
*16, i
*16, i
*16);
111 data
.SetCustomColour(i
, colour
);
114 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
115 if (dialog
->ShowModal() == wxID_OK
)
117 wxColourData retData
= dialog
->GetColourData();
118 wxColour col
= retData
.GetColour();
119 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
120 myCanvas
->SetBackgroundColour(col
);
127 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
130 data
.SetInitialFont(wxGetApp().m_canvasFont
);
131 data
.SetColour(wxGetApp().m_canvasTextColour
);
133 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
134 if (dialog
->ShowModal() == wxID_OK
)
136 wxFontData retData
= dialog
->GetFontData();
137 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
138 wxGetApp().m_canvasTextColour
= retData
.GetColour();
144 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
145 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
148 data
.SetChooseFull(TRUE
);
149 for (int i
= 0; i
< 16; i
++)
151 wxColour
colour(i
*16, i
*16, i
*16);
152 data
.SetCustomColour(i
, colour
);
155 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
156 if (dialog
->ShowModal() == wxID_OK
)
158 wxColourData retData
= dialog
->GetColourData();
159 wxColour col
= retData
.GetColour();
160 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
161 myCanvas
->SetBackgroundColour(col
);
168 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
171 data
.SetInitialFont(wxGetApp().m_canvasFont
);
172 data
.SetColour(wxGetApp().m_canvasTextColour
);
174 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
175 if (dialog
->ShowModal() == wxID_OK
)
177 wxFontData retData
= dialog
->GetFontData();
178 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
179 wxGetApp().m_canvasTextColour
= retData
.GetColour();
186 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
188 wxMessageDialog
dialog(this, "This is a message box\nA long, long string to test out the message box properly",
189 "Message box text", wxYES_NO
|wxCANCEL
);
194 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
) )
196 wxTextEntryDialog
dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
197 "Please enter a string", "Default value", wxOK
|wxCANCEL
);
199 if (dialog
.ShowModal() == wxID_OK
)
201 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
206 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
208 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
211 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
212 "Please select a value", n
, (const wxString
*)choices
);
214 dialog
.SetSelection(2);
216 if (dialog
.ShowModal() == wxID_OK
)
218 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
223 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(event
) )
265 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(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(__WXMSW__) && wxTEST_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
)