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
= (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((wxFrame
*) NULL
, (char *) "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");
82 // don't add a menu entry for something that isn't in wxGTK. It only
84 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory");
86 file_menu
->AppendSeparator();
87 file_menu
->Append(wxID_EXIT
, "E&xit");
88 wxMenuBar
*menu_bar
= new wxMenuBar
;
89 menu_bar
->Append(file_menu
, "&File");
90 frame
->SetMenuBar(menu_bar
);
92 myCanvas
= new MyCanvas(frame
);
93 myCanvas
->SetBackgroundColour(*wxWHITE
);
95 frame
->Centre(wxBOTH
);
105 // My frame constructor
106 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
107 wxFrame(parent
, -1, title
, pos
, size
)
110 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
113 data
.SetChooseFull(TRUE
);
114 for (int i
= 0; i
< 16; i
++)
116 wxColour
colour(i
*16, i
*16, i
*16);
117 data
.SetCustomColour(i
, colour
);
120 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
121 if (dialog
->ShowModal() == wxID_OK
)
123 wxColourData retData
= dialog
->GetColourData();
124 wxColour col
= retData
.GetColour();
125 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
126 myCanvas
->SetBackgroundColour(col
);
133 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
136 data
.SetInitialFont(wxGetApp().m_canvasFont
);
137 data
.SetColour(wxGetApp().m_canvasTextColour
);
139 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
140 if (dialog
->ShowModal() == wxID_OK
)
142 wxFontData retData
= dialog
->GetFontData();
143 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
144 wxGetApp().m_canvasTextColour
= retData
.GetColour();
150 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
151 void MyFrame::ChooseColourGeneric(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 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(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::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
177 data
.SetInitialFont(wxGetApp().m_canvasFont
);
178 data
.SetColour(wxGetApp().m_canvasTextColour
);
180 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(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();
192 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
194 wxMessageDialog
dialog(this, "This is a message box\nA long, long string to test out the message box properly",
195 "Message box text", wxYES_NO
|wxCANCEL
);
200 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
) )
202 wxTextEntryDialog
dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
203 "Please enter a string", "Default value", wxOK
|wxCANCEL
);
205 if (dialog
.ShowModal() == wxID_OK
)
207 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
212 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
214 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
217 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
218 "Please select a value", n
, (const wxString
*)choices
);
220 dialog
.SetSelection(2);
222 if (dialog
.ShowModal() == wxID_OK
)
224 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
229 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
231 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
233 if (dialog
.ShowModal() == wxID_OK
)
235 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
240 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
242 wxFileDialog
dialog(this, "Testing save file dialog", "", "",
243 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
244 wxSAVE
|wxOVERWRITE_PROMPT
);
246 if (dialog
.ShowModal() == wxID_OK
)
249 sprintf(buf
, "%s, filter %d", (const char *)dialog
.GetPath(), dialog
.GetFilterIndex());
250 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
255 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
258 wxDirDialog
dialog(this, "Testing directory picker", "");
260 if (dialog
.ShowModal() == wxID_OK
)
262 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
268 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
273 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
276 dc
.SetFont(wxGetApp().m_canvasFont
);
277 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
278 dc
.SetBackgroundMode(wxTRANSPARENT
);
279 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
282 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
283 EVT_PAINT(MyCanvas::OnPaint
)
286 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
287 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
288 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
289 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
290 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
291 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
292 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
293 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
294 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
295 #if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
296 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
297 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
299 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)