]> git.saurik.com Git - wxWidgets.git/blame - samples/xrc/myframe.cpp
correct typo in my previous commit
[wxWidgets.git] / samples / xrc / myframe.cpp
CommitLineData
af1337b0
JS
1//-----------------------------------------------------------------------------
2// Name: myframe.cpp
3// Purpose: XML resources sample: A derived frame, called MyFrame
4// Author: Robert O'Connor (rob@medicalmnemonics.com), Vaclav Slavik
5// RCS-ID: $Id$
6// Copyright: (c) Robert O'Connor and Vaclav Slavik
7// Licence: wxWindows licence
8//-----------------------------------------------------------------------------
9
af1337b0 10//-----------------------------------------------------------------------------
be5a51fb 11// Standard wxWidgets headers
af1337b0
JS
12//-----------------------------------------------------------------------------
13
14// For compilers that support precompilation, includes "wx/wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
21// For all others, include the necessary headers (this file is usually all you
be5a51fb 22// need because it includes almost all "standard" wxWidgets headers)
af1337b0
JS
23#ifndef WX_PRECOMP
24 #include "wx/wx.h"
25#endif
26
188cb2af
VZ
27#include "wx/sysopt.h"
28
af1337b0
JS
29//-----------------------------------------------------------------------------
30// Header of this .cpp file
31//-----------------------------------------------------------------------------
32
33#include "myframe.h"
34
35//-----------------------------------------------------------------------------
36// Remaining headers: Needed wx headers, then wx/contrib headers, then application headers
37//-----------------------------------------------------------------------------
38
39// Since setting an icon
40#include "wx/image.h"
41
42//-----------------------------------------------------------------------------
43
44#include "wx/xrc/xmlres.h" // XRC XML resouces
45
46//-----------------------------------------------------------------------------
47
48// Our derived dialog for the derived dialog example.
49#include "derivdlg.h"
50// Our custom class, for the custom class example.
51#include "custclas.h"
52// For functions to manipulate our wxTreeCtrl and wxListCtrl
53#include "wx/treectrl.h"
54#include "wx/listctrl.h"
55
56//-----------------------------------------------------------------------------
57// Regular resources (the non-XRC kind).
58//-----------------------------------------------------------------------------
59
60// The application icon
61// All non-MSW platforms use an xpm. MSW uses an .ico file
62#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
63 #include "rc/appicon.xpm"
64#endif
65
66//-----------------------------------------------------------------------------
67// Event table: connect the events to the handler functions to process them
68//-----------------------------------------------------------------------------
69
be5a51fb 70// The event tables connect the wxWidgets events with the functions (event
af1337b0
JS
71// handlers) which process them. It can be also done at run-time, but for the
72// simple menu events like this the static method is much simpler.
f80ea77b 73// The reason why the menuitems and tools are given the same name in the
af1337b0 74// XRC file, is that both a tool (a toolbar item) and a menuitem are designed
f80ea77b
WS
75// to fire the same kind of event (an EVT_MENU) and thus I give them the same
76// ID name to help new users emphasize this point which is often overlooked
be5a51fb 77// when starting out with wxWidgets.
af1337b0 78BEGIN_EVENT_TABLE(MyFrame, wxFrame)
60982dea
VZ
79 EVT_MENU(XRCID("unload_resource_menuitem"), MyFrame::OnUnloadResourceMenuCommand)
80 EVT_MENU(XRCID("reload_resource_menuitem"), MyFrame::OnReloadResourceMenuCommand)
80a06ad2 81 EVT_MENU(wxID_EXIT, MyFrame::OnExitToolOrMenuCommand)
f80ea77b
WS
82 EVT_MENU(XRCID("non_derived_dialog_tool_or_menuitem"), MyFrame::OnNonDerivedDialogToolOrMenuCommand)
83 EVT_MENU(XRCID("derived_tool_or_menuitem"), MyFrame::OnDerivedDialogToolOrMenuCommand)
84 EVT_MENU(XRCID("controls_tool_or_menuitem"), MyFrame::OnControlsToolOrMenuCommand)
85 EVT_MENU(XRCID("uncentered_tool_or_menuitem"), MyFrame::OnUncenteredToolOrMenuCommand)
86 EVT_MENU(XRCID("custom_class_tool_or_menuitem"), MyFrame::OnCustomClassToolOrMenuCommand)
af1337b0
JS
87 EVT_MENU(XRCID("platform_property_tool_or_menuitem"), MyFrame::OnPlatformPropertyToolOrMenuCommand)
88 EVT_MENU(XRCID("art_provider_tool_or_menuitem"), MyFrame::OnArtProviderToolOrMenuCommand)
89 EVT_MENU(XRCID("variable_expansion_tool_or_menuitem"), MyFrame::OnVariableExpansionToolOrMenuCommand)
80a06ad2 90 EVT_MENU(wxID_ABOUT, MyFrame::OnAboutToolOrMenuCommand)
af1337b0
JS
91END_EVENT_TABLE()
92
93//-----------------------------------------------------------------------------
94// Public methods
95//-----------------------------------------------------------------------------
96
97// Constructor
98MyFrame::MyFrame(wxWindow* parent)
99{
100 // Load up this frame from XRC. [Note, instead of making a class's
f80ea77b
WS
101 // constructor take a wxWindow* parent with a default value of NULL,
102 // we could have just had designed MyFrame class with an empty
af1337b0
JS
103 // constructor and then written here:
104 // wxXmlResource::Get()->LoadFrame(this, (wxWindow* )NULL, "main_frame");
f80ea77b
WS
105 // since this frame will always be the top window, and thus parentless.
106 // However, the current approach has source code that can be recycled
af1337b0 107 // for other frames that aren't the top level window.]
2b5f62a0 108 wxXmlResource::Get()->LoadFrame(this, parent, wxT("main_frame"));
af1337b0
JS
109
110 // Set the icon for the frame.
111 SetIcon(wxICON(appicon));
112
113 // Load the menubar from XRC and set this frame's menubar to it.
2b5f62a0 114 SetMenuBar(wxXmlResource::Get()->LoadMenuBar(wxT("main_menu")));
af1337b0
JS
115 // Load the toolbar from XRC and set this frame's toolbar to it.
116 // NOTE: For toolbars you currently should do it exactly like this.
117 // With toolbars, you currently can't create one, and set it later. It
118 // needs to be all in one step.
188cb2af 119 wxSystemOptions::SetOption ( wxT("msw.remap"), 0 );
2b5f62a0 120 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar")));
f80ea77b 121
d96cdd4a 122#if wxUSE_STATUSBAR
43e8916f 123 // Give the frame an optional statusbar. The '1' just means one field.
f80ea77b 124 // A gripsizer will automatically get put on into the corner, if that
af1337b0
JS
125 // is the normal OS behaviour for frames on that platform. Helptext
126 // for menu items and toolbar tools will automatically get displayed
127 // here.
128 CreateStatusBar( 1 );
d96cdd4a 129#endif // wxUSE_STATUSBAR
af1337b0
JS
130}
131
132//-----------------------------------------------------------------------------
133// Private methods
134//-----------------------------------------------------------------------------
135
60982dea
VZ
136void MyFrame::OnUnloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
137{
138 if ( wxXmlResource::Get()->Unload(wxT("rc/basicdlg.xrc")) )
139 wxLogMessage(_T("Basic dialog resource has now been unloaded, you ")
140 _T("won't be able to use it before loading it again"));
141 else
142 wxLogWarning(_T("Failed to unload basic dialog resource"));
143}
144
145void MyFrame::OnReloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
146{
147 if ( wxXmlResource::Get()->Load(wxT("rc/basicdlg.xrc")) )
148 wxLogStatus(_T("Basic dialog resource has been loaded."));
149 else
150 wxLogError(_T("Failed to load basic dialog resource"));
151}
152
af1337b0
JS
153void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
154{
f80ea77b
WS
155 // true is to force the frame to close.
156 Close(true);
af1337b0
JS
157}
158
159
160void MyFrame::OnNonDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
161{
162 wxDialog dlg;
163 // "non_derived_dialog" is the name of the wxDialog XRC node that should
164 // be loaded.
60982dea
VZ
165 if ( wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("non_derived_dialog")) )
166 dlg.ShowModal();
af1337b0
JS
167}
168
169
f80ea77b 170void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
af1337b0
JS
171{
172 // Make an instance of our derived dialog, passing it "this" window
f80ea77b 173 // (the main frame) as the parent of the dialog. This allows the dialog
af1337b0
JS
174 // to be destructed automatically when the parent is destroyed.
175 PreferencesDialog preferencesDialog(this);
176 // Show the instance of the dialog, modally.
177 preferencesDialog.ShowModal();
178}
179
d956f3e7
VZ
180void MyFrame::OnAnimationCtrlPlay(wxCommandEvent& event)
181{
182#if wxUSE_ANIMATIONCTRL
183 // get the pointers we need
184 wxButton *btn = wxDynamicCast(event.GetEventObject(), wxButton);
185 if (!btn || !btn->GetParent()) return;
186
187 wxWindow *win = btn->GetParent();
188 wxAnimationCtrl *ctrl = XRCCTRL(*win, "controls_animation_ctrl", wxAnimationCtrl);
189 if (ctrl->IsPlaying())
190 {
191 ctrl->Stop();
192 btn->SetLabel(wxT("Play"));
193 }
194 else
195 {
863d562d
VZ
196 if (ctrl->Play())
197 btn->SetLabel(wxT("Stop"));
198 else
199 wxLogError(wxT("Cannot play the animation..."));
d956f3e7
VZ
200 }
201#endif
202}
af1337b0 203
f80ea77b 204void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
af1337b0
JS
205{
206 wxDialog dlg;
2b5f62a0 207 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog"));
af1337b0
JS
208
209#if wxUSE_LISTCTRL
210 // There is no data in the listctrl. This will add some columns
43e8916f 211 // and some data. You don't need to use any pointers
af1337b0
JS
212 // at all to manipulate the controls, just simply use the XRCCTL(...) macros.
213 // "controls_treectrl" is the name of this control in the XRC.
214 // (1) Insert a column, with the column header of "Name"
215 // (The '_' function around "Name" marks this string as one to translate).
f80ea77b 216 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertColumn( 0,
af1337b0
JS
217 _("Name"),
218 wxLIST_FORMAT_LEFT,
219 ( 200 )
220 );
221 // (2) Insert some items into the listctrl
222 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(0,wxT("Todd Hope"));
223 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(1,wxT("Kim Wynd"));
224 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(2,wxT("Leon Li"));
225#endif
226
f80ea77b 227#if wxUSE_TREECTRL
af1337b0 228 // There is no data in the tree ctrl. These lines will add some.
f80ea77b
WS
229 // (1) Instead of having to write out
230 // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction()
af1337b0 231 // each time (which is also OK), this example code will shown how
f80ea77b 232 // to make a pointer to the XRC control, so we can use
af1337b0
JS
233 // treectrl->SomeFunction() as a short cut. This is useful if you
234 // will be referring to this control often in the code.
235 wxTreeCtrl* treectrl = XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl);
236 // (2) Add a root node
237 treectrl->AddRoot(_("Godfather"));
238 // (3)Append some items to the root node.
239 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 1"));
240 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 2"));
f80ea77b 241 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil accountant"));
af1337b0 242#endif
f80ea77b 243
d956f3e7
VZ
244#if wxUSE_ANIMATIONCTRL
245 // dynamically connect our event handler for the "clicked" event of the "play" button
246 // in the animation ctrl page of our dialog
247 dlg.Connect(XRCID("controls_animation_button_play"), wxEVT_COMMAND_BUTTON_CLICKED,
248 wxCommandEventHandler(MyFrame::OnAnimationCtrlPlay));
249#endif
250
af1337b0
JS
251 // All done. Show the dialog.
252 dlg.ShowModal();
253}
254
255
f80ea77b 256void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
af1337b0
JS
257{
258 wxDialog dlg;
2b5f62a0 259 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("uncentered_dialog"));
af1337b0
JS
260 dlg.ShowModal();
261}
262
263
264void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
265{
266 wxDialog dlg;
2b5f62a0 267 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("custom_class_dialog"));
f80ea77b
WS
268
269 // Make an instance of our new custom class.
270 MyResizableListCtrl* a_myResizableListCtrl = new MyResizableListCtrl(&dlg,
271 wxID_ANY,
af1337b0
JS
272 wxDefaultPosition,
273 wxDefaultSize,
274 wxLC_REPORT,
275 wxDefaultValidator);
f80ea77b
WS
276
277 // "custom_control_placeholder" is the name of the "unknown" tag in the
af1337b0 278 // custctrl.xrc XRC file.
2b5f62a0 279 wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"),
af1337b0
JS
280 a_myResizableListCtrl);
281 dlg.ShowModal();
282}
283
284
285void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
286{
287 wxDialog dlg;
2b5f62a0 288 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("platform_property_dialog"));
af1337b0
JS
289 dlg.ShowModal();
290}
291
292
293void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
294{
295 wxDialog dlg;
2b5f62a0 296 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("art_provider_dialog"));
af1337b0
JS
297 dlg.ShowModal();
298}
299
300
301void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
302{
303 wxDialog dlg;
2b5f62a0 304 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variable_expansion_dialog"));
af1337b0
JS
305 dlg.ShowModal();
306}
307
308
309void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
310{
311 wxString msg;
312 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
313 _T("Welcome to %s"), wxVERSION_STRING);
314
2b5f62a0 315 wxMessageBox(msg, _("About XML resources demo"), wxOK | wxICON_INFORMATION, this);
af1337b0 316}