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