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>
33 #include <wx/tipdlg.h>
35 #define wxTEST_GENERIC_DIALOGS_IN_MSW 0
37 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
38 #include <wx/generic/colrdlgg.h>
39 #include <wx/generic/fontdlgg.h>
46 MyCanvas
*myCanvas
= (MyCanvas
*) NULL
;
48 // `Main program' equivalent, creating windows and returning main app frame
49 bool MyApp::OnInit(void)
51 #if defined(__WXGTK__) && defined(wxUSE_UNICODE)
52 wxConvCurrent
= &wxConvLibc
;
55 m_canvasTextColour
= wxColour("BLACK");
56 m_canvasFont
= *wxNORMAL_FONT
;
58 // Create the main frame window
59 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
62 wxMenu
*file_menu
= new wxMenu
;
64 file_menu
->Append(DIALOGS_CHOOSE_COLOUR
, "&Choose colour");
66 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
67 file_menu
->Append(DIALOGS_CHOOSE_COLOUR_GENERIC
, "Choose colour (&generic)");
70 file_menu
->AppendSeparator();
71 file_menu
->Append(DIALOGS_CHOOSE_FONT
, "Choose &font");
73 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
74 file_menu
->Append(DIALOGS_CHOOSE_FONT_GENERIC
, "Choose f&ont (generic)");
77 file_menu
->AppendSeparator();
78 file_menu
->Append(DIALOGS_MESSAGE_BOX
, "&Message box");
79 file_menu
->Append(DIALOGS_TEXT_ENTRY
, "Text &entry");
80 file_menu
->Append(DIALOGS_SINGLE_CHOICE
, "&Single choice");
81 file_menu
->AppendSeparator();
82 file_menu
->Append(DIALOGS_TIP
, "&Tip of the day");
83 file_menu
->AppendSeparator();
84 file_menu
->Append(DIALOGS_FILE_OPEN
, "&Open file");
85 file_menu
->Append(DIALOGS_FILE_SAVE
, "Sa&ve file");
86 file_menu
->Append(DIALOGS_DIR_CHOOSE
, "&Choose a directory");
87 file_menu
->AppendSeparator();
88 file_menu
->Append(wxID_EXIT
, "E&xit");
89 wxMenuBar
*menu_bar
= new wxMenuBar
;
90 menu_bar
->Append(file_menu
, "&File");
91 frame
->SetMenuBar(menu_bar
);
93 myCanvas
= new MyCanvas(frame
);
94 myCanvas
->SetBackgroundColour(*wxWHITE
);
96 frame
->Centre(wxBOTH
);
106 // My frame constructor
107 MyFrame::MyFrame(wxWindow
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
108 wxFrame(parent
, -1, title
, pos
, size
)
111 void MyFrame::ChooseColour(wxCommandEvent
& WXUNUSED(event
) )
114 data
.SetChooseFull(TRUE
);
115 for (int i
= 0; i
< 16; i
++)
117 wxColour
colour(i
*16, i
*16, i
*16);
118 data
.SetCustomColour(i
, colour
);
121 wxColourDialog
*dialog
= new wxColourDialog(this, &data
);
122 if (dialog
->ShowModal() == wxID_OK
)
124 wxColourData retData
= dialog
->GetColourData();
125 wxColour col
= retData
.GetColour();
126 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
127 myCanvas
->SetBackgroundColour(col
);
134 void MyFrame::ChooseFont(wxCommandEvent
& WXUNUSED(event
) )
137 data
.SetInitialFont(wxGetApp().m_canvasFont
);
138 data
.SetColour(wxGetApp().m_canvasTextColour
);
140 wxFontDialog
*dialog
= new wxFontDialog(this, &data
);
141 if (dialog
->ShowModal() == wxID_OK
)
143 wxFontData retData
= dialog
->GetFontData();
144 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
145 wxGetApp().m_canvasTextColour
= retData
.GetColour();
151 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
152 void MyFrame::ChooseColourGeneric(wxCommandEvent
& WXUNUSED(event
))
155 data
.SetChooseFull(TRUE
);
156 for (int i
= 0; i
< 16; i
++)
158 wxColour
colour(i
*16, i
*16, i
*16);
159 data
.SetCustomColour(i
, colour
);
162 wxGenericColourDialog
*dialog
= new wxGenericColourDialog(this, &data
);
163 if (dialog
->ShowModal() == wxID_OK
)
165 wxColourData retData
= dialog
->GetColourData();
166 wxColour col
= retData
.GetColour();
167 // wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
168 myCanvas
->SetBackgroundColour(col
);
175 void MyFrame::ChooseFontGeneric(wxCommandEvent
& WXUNUSED(event
) )
178 data
.SetInitialFont(wxGetApp().m_canvasFont
);
179 data
.SetColour(wxGetApp().m_canvasTextColour
);
181 wxGenericFontDialog
*dialog
= new wxGenericFontDialog(this, &data
);
182 if (dialog
->ShowModal() == wxID_OK
)
184 wxFontData retData
= dialog
->GetFontData();
185 wxGetApp().m_canvasFont
= retData
.GetChosenFont();
186 wxGetApp().m_canvasTextColour
= retData
.GetColour();
193 void MyFrame::MessageBox(wxCommandEvent
& WXUNUSED(event
) )
195 wxMessageDialog
dialog(NULL
, "This is a message box\nA long, long string to test out the message box properly",
196 "Message box text", wxYES_NO
|wxCANCEL
|wxICON_INFORMATION
);
201 void MyFrame::TextEntry(wxCommandEvent
& WXUNUSED(event
) )
203 wxTextEntryDialog
dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
204 "Please enter a string", "Default value", wxOK
|wxCANCEL
);
206 if (dialog
.ShowModal() == wxID_OK
)
208 wxMessageDialog
dialog2(this, dialog
.GetValue(), "Got string");
213 void MyFrame::SingleChoice(wxCommandEvent
& WXUNUSED(event
) )
215 const wxString choices
[] = { "One", "Two", "Three", "Four", "Five" } ;
218 wxSingleChoiceDialog
dialog(this, "This is a small sample\nA single-choice convenience dialog",
219 "Please select a value", n
, (const wxString
*)choices
);
221 dialog
.SetSelection(2);
223 if (dialog
.ShowModal() == wxID_OK
)
225 wxMessageDialog
dialog2(this, dialog
.GetStringSelection(), "Got string");
230 void MyFrame::FileOpen(wxCommandEvent
& WXUNUSED(event
) )
232 wxFileDialog
dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
234 if (dialog
.ShowModal() == wxID_OK
)
237 info
.Printf(_T("Full file name: %s\n")
240 dialog
.GetPath().c_str(),
241 dialog
.GetDirectory().c_str(),
242 dialog
.GetFilename().c_str());
243 wxMessageDialog
dialog2(this, info
, "Selected file");
248 void MyFrame::FileSave(wxCommandEvent
& WXUNUSED(event
) )
250 wxFileDialog
dialog(this, "Testing save file dialog", "", "",
251 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
252 wxSAVE
|wxOVERWRITE_PROMPT
);
254 if (dialog
.ShowModal() == wxID_OK
)
257 wxSprintf(buf
, _T("%s, filter %d"), (const wxChar
*)dialog
.GetPath(), dialog
.GetFilterIndex());
258 wxMessageDialog
dialog2(this, wxString(buf
), "Selected path");
263 void MyFrame::DirChoose(wxCommandEvent
& WXUNUSED(event
) )
265 wxDirDialog
dialog(this, "Testing directory picker", "");
267 if (dialog
.ShowModal() == wxID_OK
)
269 wxMessageDialog
dialog2(this, dialog
.GetPath(), "Selected path");
274 void MyFrame::ShowTip(wxCommandEvent
& event
)
276 static size_t s_index
= (size_t)-1;
278 if ( s_index
== (size_t)-1 )
282 // this is completely bogus, we don't know how many lines are there
283 // in the file, but who cares, it's a demo only...
284 s_index
= rand() % 5;
287 wxTipProvider
*tipProvider
= wxCreateFileTipProvider("tips.txt", s_index
);
289 bool showAtStartup
= wxShowTip(this, tipProvider
);
293 wxMessageBox("Will show tips on startup", "Tips dialog",
294 wxOK
| wxICON_INFORMATION
, this);
297 s_index
= tipProvider
->GetCurrentTip();
301 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
) )
306 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
309 dc
.SetFont(wxGetApp().m_canvasFont
);
310 dc
.SetTextForeground(wxGetApp().m_canvasTextColour
);
311 dc
.SetBackgroundMode(wxTRANSPARENT
);
312 dc
.DrawText("wxWindows common dialogs test application", 10, 10);
315 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
316 EVT_PAINT(MyCanvas::OnPaint
)
319 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
320 EVT_MENU(DIALOGS_CHOOSE_COLOUR
, MyFrame::ChooseColour
)
321 EVT_MENU(DIALOGS_CHOOSE_FONT
, MyFrame::ChooseFont
)
322 EVT_MENU(DIALOGS_MESSAGE_BOX
, MyFrame::MessageBox
)
323 EVT_MENU(DIALOGS_TEXT_ENTRY
, MyFrame::TextEntry
)
324 EVT_MENU(DIALOGS_SINGLE_CHOICE
, MyFrame::SingleChoice
)
325 EVT_MENU(DIALOGS_FILE_OPEN
, MyFrame::FileOpen
)
326 EVT_MENU(DIALOGS_FILE_SAVE
, MyFrame::FileSave
)
327 EVT_MENU(DIALOGS_DIR_CHOOSE
, MyFrame::DirChoose
)
328 EVT_MENU(DIALOGS_TIP
, MyFrame::ShowTip
)
329 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
330 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC
, MyFrame::ChooseColourGeneric
)
331 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC
, MyFrame::ChooseFontGeneric
)
333 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)