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 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
51 wxConvCurrent
= &wxConvLocal
;
54 m_canvasTextColour
= wxColour("BLACK");
55 m_canvasFont
= *wxNORMAL_FONT
;
57 // Create the main frame window
58 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
61 wxMenu
*file_menu
= new wxMenu
;
63 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
65 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
66 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
69 file_menu
->AppendSeparator();
70 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
72 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
73 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
76 file_menu
->AppendSeparator();
77 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box");
78 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry");
79 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice");
80 file_menu
->AppendSeparator();
81 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file");
82 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file");
83 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory");
84 file_menu
->AppendSeparator();
85 file_menu
->Append(wxID_EXIT
, "E&xit");
86 wxMenuBar
*menu_bar
= new wxMenuBar
;
87 menu_bar
->Append(file_menu
, "&File");
88 frame
->SetMenuBar(menu_bar
);
90 myCanvas
= new MyCanvas(frame
);
91 myCanvas
->SetBackgroundColour(*wxWHITE
);
93 frame
->Centre(wxBOTH
);
103 // My frame constructor
104 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
105 wxFrame(parent
, -1, title
, pos
, size
)
108 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
111 data
.SetChooseFull(TRUE
);
112 for (int i
= 0; i
< 16; i
++)
114 wxColour
colour(i
*16, i
*16, i
*16);
115 data
.SetCustomColour(i
, colour
);
118 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
119 if (dialog
->ShowModal() == wxID_OK
)
121 wxColourData retData
= dialog
->GetColourData();
122 wxColour col
= retData
.GetColour();
123 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
124 myCanvas
->SetBackgroundColour(col
);
131 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
134 data
.SetInitialFont(wxGetApp().m_canvasFont
);
135 data
.SetColour(wxGetApp().m_canvasTextColour
);
137 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
138 if (dialog
->ShowModal() == wxID_OK
)
140 wxFontData retData
= dialog
->GetFontData();
141 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
142 wxGetApp().m_canvasTextColour
= retData
.GetColour();
148 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
149 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
152 data
.SetChooseFull(TRUE
);
153 for (int i
= 0; i
< 16; i
++)
155 wxColour
colour(i
*16, i
*16, i
*16);
156 data
.SetCustomColour(i
, colour
);
159 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
160 if (dialog
->ShowModal() == wxID_OK
)
162 wxColourData retData
= dialog
->GetColourData();
163 wxColour col
= retData
.GetColour();
164 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
165 myCanvas
->SetBackgroundColour(col
);
172 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
175 data
.SetInitialFont(wxGetApp().m_canvasFont
);
176 data
.SetColour(wxGetApp().m_canvasTextColour
);
178 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
179 if (dialog
->ShowModal() == wxID_OK
)
181 wxFontData retData
= dialog
->GetFontData();
182 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
183 wxGetApp().m_canvasTextColour
= retData
.GetColour();
190 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
193 wxMessageDialog
dialog(NULL
, "This is a message box\nA long, long string to test out the message box properly",
194 "Message box text", wxYES_NO
|wxCANCEL
);
198 ::wxMessageBox("MsgBox with a really long long string",
199 "this is the text", wxYES_NO
|wxICON_EXCLAMATION
);
202 ::wxMessageBox("Short",
203 "This is the text", wxYES_NO
|wxICON_EXCLAMATION
);
206 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
) )
208 wxTextEntryDialog
dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
209 "Please enter a string", "Default value", wxOK
|wxCANCEL
);
211 if (dialog
.ShowModal() == wxID_OK
)
213 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
218 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
220 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
223 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
224 "Please select a value", n
, (const wxString
*)choices
);
226 dialog
.SetSelection(2);
228 if (dialog
.ShowModal() == wxID_OK
)
230 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
235 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
237 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
239 if (dialog
.ShowModal() == wxID_OK
)
242 info
.Printf(_T("Full file name: %s\n")
245 dialog
.GetPath().c_str(),
246 dialog
.GetDirectory().c_str(),
247 dialog
.GetFilename().c_str());
248 wxMessageDialog
dialog2(this, info
, "Selected file");
253 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
255 wxFileDialog
dialog(this, "Testing save file dialog", "", "",
256 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
257 wxSAVE
|wxOVERWRITE_PROMPT
);
259 if (dialog
.ShowModal() == wxID_OK
)
262 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
263 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
268 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
270 wxDirDialog
dialog(this, "Testing directory picker", "");
272 if (dialog
.ShowModal() == wxID_OK
)
274 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
279 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
284 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
287 dc
.SetFont(wxGetApp().m_canvasFont
);
288 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
289 dc
.SetBackgroundMode(wxTRANSPARENT
);
290 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
293 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
294 EVT_PAINT(MyCanvas::OnPaint
)
297 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
298 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
299 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
300 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
301 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
302 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
303 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
304 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
305 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
306 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
307 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
308 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
310 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)