]> git.saurik.com Git - wxWidgets.git/blob - samples/xrc/myframe.cpp
Make it possible to use svn-find-native-eols script without svn checkout.
[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 // And our objref dialog, for the object reference and ID range example.
53 #include "objrefdlg.h"
54 // For functions to manipulate the corresponding controls.
55 #include "wx/animate.h"
56 #include "wx/treectrl.h"
57 #include "wx/listctrl.h"
58
59 //-----------------------------------------------------------------------------
60 // Regular resources (the non-XRC kind).
61 //-----------------------------------------------------------------------------
62
63 // the application icon (under Windows and OS/2 it is in resources and even
64 // though we could still include the XPM here it would be unused)
65 #ifndef wxHAS_IMAGES_IN_RESOURCES
66 #include "../sample.xpm"
67 #endif
68
69 //-----------------------------------------------------------------------------
70 // Event table: connect the events to the handler functions to process them
71 //-----------------------------------------------------------------------------
72
73 // The event tables connect the wxWidgets events with the functions (event
74 // handlers) which process them. It can be also done at run-time, but for the
75 // simple menu events like this the static method is much simpler.
76 // The reason why the menuitems and tools are given the same name in the
77 // XRC file, is that both a tool (a toolbar item) and a menuitem are designed
78 // to fire the same kind of event (an EVT_MENU) and thus I give them the same
79 // ID name to help new users emphasize this point which is often overlooked
80 // when starting out with wxWidgets.
81 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
82 EVT_MENU(XRCID("unload_resource_menuitem"), MyFrame::OnUnloadResourceMenuCommand)
83 EVT_MENU(XRCID("reload_resource_menuitem"), MyFrame::OnReloadResourceMenuCommand)
84 EVT_MENU(wxID_EXIT, MyFrame::OnExitToolOrMenuCommand)
85 EVT_MENU(XRCID("non_derived_dialog_tool_or_menuitem"), MyFrame::OnNonDerivedDialogToolOrMenuCommand)
86 EVT_MENU(XRCID("derived_tool_or_menuitem"), MyFrame::OnDerivedDialogToolOrMenuCommand)
87 EVT_MENU(XRCID("controls_tool_or_menuitem"), MyFrame::OnControlsToolOrMenuCommand)
88 EVT_MENU(XRCID("uncentered_tool_or_menuitem"), MyFrame::OnUncenteredToolOrMenuCommand)
89 EVT_MENU(XRCID("obj_ref_tool_or_menuitem"), MyFrame::OnObjRefToolOrMenuCommand)
90 EVT_MENU(XRCID("custom_class_tool_or_menuitem"), MyFrame::OnCustomClassToolOrMenuCommand)
91 EVT_MENU(XRCID("platform_property_tool_or_menuitem"), MyFrame::OnPlatformPropertyToolOrMenuCommand)
92 EVT_MENU(XRCID("art_provider_tool_or_menuitem"), MyFrame::OnArtProviderToolOrMenuCommand)
93 EVT_MENU(XRCID("variable_expansion_tool_or_menuitem"), MyFrame::OnVariableExpansionToolOrMenuCommand)
94 EVT_MENU(XRCID("recursive_load"), MyFrame::OnRecursiveLoad)
95 EVT_MENU(wxID_ABOUT, MyFrame::OnAboutToolOrMenuCommand)
96 END_EVENT_TABLE()
97
98 //-----------------------------------------------------------------------------
99 // Public methods
100 //-----------------------------------------------------------------------------
101
102 // Constructor
103 MyFrame::MyFrame(wxWindow* parent)
104 {
105 // Load up this frame from XRC. [Note, instead of making a class's
106 // constructor take a wxWindow* parent with a default value of NULL,
107 // we could have just had designed MyFrame class with an empty
108 // constructor and then written here:
109 // wxXmlResource::Get()->LoadFrame(this, (wxWindow* )NULL, "main_frame");
110 // since this frame will always be the top window, and thus parentless.
111 // However, the current approach has source code that can be recycled
112 // for other frames that aren't the top level window.]
113 wxXmlResource::Get()->LoadFrame(this, parent, wxT("main_frame"));
114
115 // Set the icon for the frame.
116 SetIcon(wxICON(sample));
117
118 // Load the menubar from XRC and set this frame's menubar to it.
119 SetMenuBar(wxXmlResource::Get()->LoadMenuBar(wxT("main_menu")));
120 // Load the toolbar from XRC and set this frame's toolbar to it.
121 // NOTE: For toolbars you currently should do it exactly like this.
122 // With toolbars, you currently can't create one, and set it later. It
123 // needs to be all in one step.
124 wxSystemOptions::SetOption ( wxT("msw.remap"), 0 );
125 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar")));
126
127 #if wxUSE_STATUSBAR
128 // Give the frame an optional statusbar. The '1' just means one field.
129 // A gripsizer will automatically get put on into the corner, if that
130 // is the normal OS behaviour for frames on that platform. Helptext
131 // for menu items and toolbar tools will automatically get displayed
132 // here.
133 CreateStatusBar( 1 );
134 #endif // wxUSE_STATUSBAR
135 }
136
137 //-----------------------------------------------------------------------------
138 // Private methods
139 //-----------------------------------------------------------------------------
140
141 void MyFrame::OnUnloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
142 {
143 if ( wxXmlResource::Get()->Unload(wxT("rc/basicdlg.xrc")) )
144 {
145 wxLogMessage(wxT("Basic dialog resource has now been unloaded, you ")
146 wxT("won't be able to use it before loading it again"));
147 }
148 else
149 {
150 wxLogWarning(wxT("Failed to unload basic dialog resource"));
151 }
152 }
153
154 void MyFrame::OnReloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
155 {
156 if ( wxXmlResource::Get()->Load(wxT("rc/basicdlg.xrc")) )
157 {
158 wxLogStatus(wxT("Basic dialog resource has been loaded."));
159 }
160 else
161 {
162 wxLogError(wxT("Failed to load basic dialog resource"));
163 }
164 }
165
166 void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
167 {
168 // true is to force the frame to close.
169 Close(true);
170 }
171
172
173 void MyFrame::OnNonDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
174 {
175 wxDialog dlg;
176 // "non_derived_dialog" is the name of the wxDialog XRC node that should
177 // be loaded.
178 if ( wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("non_derived_dialog")) )
179 dlg.ShowModal();
180 }
181
182
183 void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
184 {
185 // Make an instance of our derived dialog, passing it "this" window
186 // (the main frame) as the parent of the dialog. This allows the dialog
187 // to be destructed automatically when the parent is destroyed.
188 PreferencesDialog preferencesDialog(this);
189 // Show the instance of the dialog, modally.
190 preferencesDialog.ShowModal();
191 }
192
193 void MyFrame::OnAnimationCtrlPlay(wxCommandEvent& event)
194 {
195 #if wxUSE_ANIMATIONCTRL
196 // get the pointers we need
197 wxButton *btn = wxDynamicCast(event.GetEventObject(), wxButton);
198 if (!btn || !btn->GetParent()) return;
199
200 wxWindow *win = btn->GetParent();
201 wxAnimationCtrl *ctrl = XRCCTRL(*win, "controls_animation_ctrl", wxAnimationCtrl);
202 if (ctrl->IsPlaying())
203 {
204 ctrl->Stop();
205 btn->SetLabel(wxT("Play"));
206 }
207 else
208 {
209 if (ctrl->Play())
210 btn->SetLabel(wxT("Stop"));
211 else
212 wxLogError(wxT("Cannot play the animation..."));
213 }
214 #endif
215 }
216
217 void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
218 {
219 wxDialog dlg;
220 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog"));
221
222 #if wxUSE_LISTCTRL
223 // The resource file specifies the columns of the control as they are
224 // typically static while the items themselves are added from here as
225 // usually they are not static (but if they are, they can be defined in the
226 // resources too, see the two other list controls definitions in
227 // controls.xrc)
228
229 // Insert some items into the listctrl: notice that we can access it using
230 // XRCCTRL
231 wxListCtrl * const list = XRCCTRL(dlg, "controls_listctrl", wxListCtrl);
232
233 list->InsertItem(0, "Athos", 0); list->SetItem(0, 1, "90", 2);
234 list->InsertItem(1, "Porthos", 5); list->SetItem(1, 1, "120", 3);
235 list->InsertItem(2, "Aramis", 1); list->SetItem(2, 1, "80", 4);
236 #endif // wxUSE_LISTCTRL
237
238 #if wxUSE_TREECTRL
239 // There is no data in the tree ctrl. These lines will add some.
240 // (1) Instead of having to write out
241 // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction()
242 // each time (which is also OK), this example code will shown how
243 // to make a pointer to the XRC control, so we can use
244 // treectrl->SomeFunction() as a short cut. This is useful if you
245 // will be referring to this control often in the code.
246 wxTreeCtrl* treectrl = XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl);
247 // (2) Add a root node
248 treectrl->AddRoot(_("Godfather"));
249 // (3)Append some items to the root node.
250 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 1"));
251 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 2"));
252 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil accountant"));
253 #endif
254
255 #if wxUSE_ANIMATIONCTRL
256 // dynamically connect our event handler for the "clicked" event of the "play" button
257 // in the animation ctrl page of our dialog
258 dlg.Connect(XRCID("controls_animation_button_play"), wxEVT_BUTTON,
259 wxCommandEventHandler(MyFrame::OnAnimationCtrlPlay));
260 #endif
261
262 // All done. Show the dialog.
263 dlg.ShowModal();
264 }
265
266
267 void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
268 {
269 wxDialog dlg;
270 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("uncentered_dialog"));
271 dlg.ShowModal();
272 }
273
274
275 void MyFrame::OnObjRefToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
276 {
277 // The dialog redirects log messages, so save the old log target first
278 wxLog* oldlogtarget = wxLog::SetActiveTarget(NULL);
279
280 // Make an instance of the dialog
281 ObjrefDialog* objrefDialog = new ObjrefDialog(this);
282 // Show the instance of the dialog, modally.
283 objrefDialog->ShowModal();
284 objrefDialog->Destroy();
285
286 // Restore the old log target
287 delete wxLog::SetActiveTarget(oldlogtarget);
288 }
289
290
291 void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
292 {
293 wxDialog dlg;
294 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("custom_class_dialog"));
295
296 // Make an instance of our new custom class.
297 MyResizableListCtrl* a_myResizableListCtrl = new MyResizableListCtrl(&dlg,
298 wxID_ANY,
299 wxDefaultPosition,
300 wxDefaultSize,
301 wxLC_REPORT,
302 wxDefaultValidator);
303
304 // "custom_control_placeholder" is the name of the "unknown" tag in the
305 // custctrl.xrc XRC file.
306 wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"),
307 a_myResizableListCtrl);
308 dlg.ShowModal();
309 }
310
311
312 void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
313 {
314 wxDialog dlg;
315 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("platform_property_dialog"));
316 dlg.ShowModal();
317 }
318
319
320 void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
321 {
322 wxDialog dlg;
323 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("art_provider_dialog"));
324 dlg.ShowModal();
325 }
326
327
328 void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
329 {
330 wxDialog dlg;
331 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variable_expansion_dialog"));
332 dlg.ShowModal();
333 }
334
335 void MyFrame::OnRecursiveLoad(wxCommandEvent& WXUNUSED(event))
336 {
337 // this dialog is created manually to show how you can inject a single
338 // control from XRC into an existing dialog
339 //
340 // this is a slightly contrived example, please keep in mind that it's done
341 // only to demonstrate LoadObjectRecursively() in action and is not the
342 // recommended to do this
343 wxDialog dlg(NULL, wxID_ANY, "Recursive Load Example",
344 wxDefaultPosition, wxDefaultSize,
345 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
346 wxSizer * const sizer = new wxBoxSizer(wxVERTICAL);
347 sizer->Add
348 (
349 new wxStaticText
350 (
351 &dlg,
352 wxID_ANY,
353 "The entire tree book control below is loaded from XRC"
354 ),
355 wxSizerFlags().Expand().Border()
356 );
357
358 sizer->Add
359 (
360 static_cast<wxWindow *>
361 (
362 // notice that controls_treebook is defined inside a notebook page
363 // inside a dialog defined in controls.xrc and so LoadObject()
364 // wouldn't find it -- but LoadObjectRecursively() does
365 wxXmlResource::Get()->
366 LoadObjectRecursively(&dlg, "controls_treebook", "wxTreebook")
367 ),
368 wxSizerFlags(1).Expand().Border()
369 );
370
371 dlg.SetSizer(sizer);
372 dlg.SetClientSize(400, 200);
373
374 dlg.ShowModal();
375 }
376
377 void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
378 {
379 wxString msg;
380 msg.Printf( wxT("This is the about dialog of XML resources demo.\n")
381 wxT("Welcome to %s"), wxVERSION_STRING);
382
383 wxMessageBox(msg, _("About XML resources demo"), wxOK | wxICON_INFORMATION, this);
384 }