]> git.saurik.com Git - wxWidgets.git/blob - samples/xrc/myframe.cpp
regenerated
[wxWidgets.git] / samples / xrc / myframe.cpp
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
10 //-----------------------------------------------------------------------------
11 // Standard wxWidgets headers
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
22 // need because it includes almost all "standard" wxWidgets headers)
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/sysopt.h"
28
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
70 // The event tables connect the wxWidgets events with the functions (event
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.
73 // The reason why the menuitems and tools are given the same name in the
74 // XRC file, is that both a tool (a toolbar item) and a menuitem are designed
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
77 // when starting out with wxWidgets.
78 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
79 EVT_MENU(XRCID("unload_resource_menuitem"), MyFrame::OnUnloadResourceMenuCommand)
80 EVT_MENU(XRCID("reload_resource_menuitem"), MyFrame::OnReloadResourceMenuCommand)
81 EVT_MENU(wxID_EXIT, MyFrame::OnExitToolOrMenuCommand)
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)
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)
90 EVT_MENU(wxID_ABOUT, MyFrame::OnAboutToolOrMenuCommand)
91 END_EVENT_TABLE()
92
93 //-----------------------------------------------------------------------------
94 // Public methods
95 //-----------------------------------------------------------------------------
96
97 // Constructor
98 MyFrame::MyFrame(wxWindow* parent)
99 {
100 // Load up this frame from XRC. [Note, instead of making a class's
101 // constructor take a wxWindow* parent with a default value of NULL,
102 // we could have just had designed MyFrame class with an empty
103 // constructor and then written here:
104 // wxXmlResource::Get()->LoadFrame(this, (wxWindow* )NULL, "main_frame");
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
107 // for other frames that aren't the top level window.]
108 wxXmlResource::Get()->LoadFrame(this, parent, wxT("main_frame"));
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.
114 SetMenuBar(wxXmlResource::Get()->LoadMenuBar(wxT("main_menu")));
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.
119 wxSystemOptions::SetOption ( wxT("msw.remap"), 0 );
120 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar")));
121
122 #if wxUSE_STATUSBAR
123 // Give the frame an optional statusbar. The '1' just means one field.
124 // A gripsizer will automatically get put on into the corner, if that
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 );
129 #endif // wxUSE_STATUSBAR
130 }
131
132 //-----------------------------------------------------------------------------
133 // Private methods
134 //-----------------------------------------------------------------------------
135
136 void MyFrame::OnUnloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
137 {
138 if ( wxXmlResource::Get()->Unload(wxT("rc/basicdlg.xrc")) )
139 {
140 wxLogMessage(wxT("Basic dialog resource has now been unloaded, you ")
141 wxT("won't be able to use it before loading it again"));
142 }
143 else
144 {
145 wxLogWarning(wxT("Failed to unload basic dialog resource"));
146 }
147 }
148
149 void MyFrame::OnReloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
150 {
151 if ( wxXmlResource::Get()->Load(wxT("rc/basicdlg.xrc")) )
152 {
153 wxLogStatus(wxT("Basic dialog resource has been loaded."));
154 }
155 else
156 {
157 wxLogError(wxT("Failed to load basic dialog resource"));
158 }
159 }
160
161 void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
162 {
163 // true is to force the frame to close.
164 Close(true);
165 }
166
167
168 void MyFrame::OnNonDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
169 {
170 wxDialog dlg;
171 // "non_derived_dialog" is the name of the wxDialog XRC node that should
172 // be loaded.
173 if ( wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("non_derived_dialog")) )
174 dlg.ShowModal();
175 }
176
177
178 void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
179 {
180 // Make an instance of our derived dialog, passing it "this" window
181 // (the main frame) as the parent of the dialog. This allows the dialog
182 // to be destructed automatically when the parent is destroyed.
183 PreferencesDialog preferencesDialog(this);
184 // Show the instance of the dialog, modally.
185 preferencesDialog.ShowModal();
186 }
187
188 void MyFrame::OnAnimationCtrlPlay(wxCommandEvent& event)
189 {
190 #if wxUSE_ANIMATIONCTRL
191 // get the pointers we need
192 wxButton *btn = wxDynamicCast(event.GetEventObject(), wxButton);
193 if (!btn || !btn->GetParent()) return;
194
195 wxWindow *win = btn->GetParent();
196 wxAnimationCtrl *ctrl = XRCCTRL(*win, "controls_animation_ctrl", wxAnimationCtrl);
197 if (ctrl->IsPlaying())
198 {
199 ctrl->Stop();
200 btn->SetLabel(wxT("Play"));
201 }
202 else
203 {
204 if (ctrl->Play())
205 btn->SetLabel(wxT("Stop"));
206 else
207 wxLogError(wxT("Cannot play the animation..."));
208 }
209 #endif
210 }
211
212 void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
213 {
214 wxDialog dlg;
215 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog"));
216
217 #if wxUSE_LISTCTRL
218 // The resource file specifies the columns of the control as they are
219 // typically static while the items themselves are added from here as
220 // usually they are not static (but if they are, they can be defined in the
221 // resources too, see the two other list controls definitions in
222 // controls.xrc)
223
224 // Insert some items into the listctrl: notice that we can access it using
225 // XRCCTRL
226 wxListCtrl * const list = XRCCTRL(dlg, "controls_listctrl", wxListCtrl);
227
228 list->InsertItem(0, "Athos"); list->SetItem(0, 1, "90");
229 list->InsertItem(1, "Porthos"); list->SetItem(1, 1, "120");
230 list->InsertItem(2, "Aramis"); list->SetItem(2, 1, "80");
231 #endif // wxUSE_LISTCTRL
232
233 #if wxUSE_TREECTRL
234 // There is no data in the tree ctrl. These lines will add some.
235 // (1) Instead of having to write out
236 // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction()
237 // each time (which is also OK), this example code will shown how
238 // to make a pointer to the XRC control, so we can use
239 // treectrl->SomeFunction() as a short cut. This is useful if you
240 // will be referring to this control often in the code.
241 wxTreeCtrl* treectrl = XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl);
242 // (2) Add a root node
243 treectrl->AddRoot(_("Godfather"));
244 // (3)Append some items to the root node.
245 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 1"));
246 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 2"));
247 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil accountant"));
248 #endif
249
250 #if wxUSE_ANIMATIONCTRL
251 // dynamically connect our event handler for the "clicked" event of the "play" button
252 // in the animation ctrl page of our dialog
253 dlg.Connect(XRCID("controls_animation_button_play"), wxEVT_COMMAND_BUTTON_CLICKED,
254 wxCommandEventHandler(MyFrame::OnAnimationCtrlPlay));
255 #endif
256
257 // All done. Show the dialog.
258 dlg.ShowModal();
259 }
260
261
262 void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
263 {
264 wxDialog dlg;
265 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("uncentered_dialog"));
266 dlg.ShowModal();
267 }
268
269
270 void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
271 {
272 wxDialog dlg;
273 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("custom_class_dialog"));
274
275 // Make an instance of our new custom class.
276 MyResizableListCtrl* a_myResizableListCtrl = new MyResizableListCtrl(&dlg,
277 wxID_ANY,
278 wxDefaultPosition,
279 wxDefaultSize,
280 wxLC_REPORT,
281 wxDefaultValidator);
282
283 // "custom_control_placeholder" is the name of the "unknown" tag in the
284 // custctrl.xrc XRC file.
285 wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"),
286 a_myResizableListCtrl);
287 dlg.ShowModal();
288 }
289
290
291 void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
292 {
293 wxDialog dlg;
294 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("platform_property_dialog"));
295 dlg.ShowModal();
296 }
297
298
299 void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
300 {
301 wxDialog dlg;
302 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("art_provider_dialog"));
303 dlg.ShowModal();
304 }
305
306
307 void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
308 {
309 wxDialog dlg;
310 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variable_expansion_dialog"));
311 dlg.ShowModal();
312 }
313
314
315 void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
316 {
317 wxString msg;
318 msg.Printf( wxT("This is the about dialog of XML resources demo.\n")
319 wxT("Welcome to %s"), wxVERSION_STRING);
320
321 wxMessageBox(msg, _("About XML resources demo"), wxOK | wxICON_INFORMATION, this);
322 }