]>
Commit | Line | Data |
---|---|---|
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 | 78 | BEGIN_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) | |
af0ac990 | 90 | EVT_MENU(XRCID("recursive_load"), MyFrame::OnRecursiveLoad) |
80a06ad2 | 91 | EVT_MENU(wxID_ABOUT, MyFrame::OnAboutToolOrMenuCommand) |
af1337b0 JS |
92 | END_EVENT_TABLE() |
93 | ||
94 | //----------------------------------------------------------------------------- | |
95 | // Public methods | |
96 | //----------------------------------------------------------------------------- | |
97 | ||
98 | // Constructor | |
99 | MyFrame::MyFrame(wxWindow* parent) | |
100 | { | |
101 | // Load up this frame from XRC. [Note, instead of making a class's | |
f80ea77b WS |
102 | // constructor take a wxWindow* parent with a default value of NULL, |
103 | // we could have just had designed MyFrame class with an empty | |
af1337b0 JS |
104 | // constructor and then written here: |
105 | // wxXmlResource::Get()->LoadFrame(this, (wxWindow* )NULL, "main_frame"); | |
f80ea77b WS |
106 | // since this frame will always be the top window, and thus parentless. |
107 | // However, the current approach has source code that can be recycled | |
af1337b0 | 108 | // for other frames that aren't the top level window.] |
2b5f62a0 | 109 | wxXmlResource::Get()->LoadFrame(this, parent, wxT("main_frame")); |
af1337b0 JS |
110 | |
111 | // Set the icon for the frame. | |
112 | SetIcon(wxICON(appicon)); | |
113 | ||
114 | // Load the menubar from XRC and set this frame's menubar to it. | |
2b5f62a0 | 115 | SetMenuBar(wxXmlResource::Get()->LoadMenuBar(wxT("main_menu"))); |
af1337b0 JS |
116 | // Load the toolbar from XRC and set this frame's toolbar to it. |
117 | // NOTE: For toolbars you currently should do it exactly like this. | |
118 | // With toolbars, you currently can't create one, and set it later. It | |
119 | // needs to be all in one step. | |
188cb2af | 120 | wxSystemOptions::SetOption ( wxT("msw.remap"), 0 ); |
2b5f62a0 | 121 | SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar"))); |
f80ea77b | 122 | |
d96cdd4a | 123 | #if wxUSE_STATUSBAR |
43e8916f | 124 | // Give the frame an optional statusbar. The '1' just means one field. |
f80ea77b | 125 | // A gripsizer will automatically get put on into the corner, if that |
af1337b0 JS |
126 | // is the normal OS behaviour for frames on that platform. Helptext |
127 | // for menu items and toolbar tools will automatically get displayed | |
128 | // here. | |
129 | CreateStatusBar( 1 ); | |
d96cdd4a | 130 | #endif // wxUSE_STATUSBAR |
af1337b0 JS |
131 | } |
132 | ||
133 | //----------------------------------------------------------------------------- | |
134 | // Private methods | |
135 | //----------------------------------------------------------------------------- | |
136 | ||
60982dea VZ |
137 | void MyFrame::OnUnloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event)) |
138 | { | |
139 | if ( wxXmlResource::Get()->Unload(wxT("rc/basicdlg.xrc")) ) | |
43b2d5e7 | 140 | { |
9a83f860 VZ |
141 | wxLogMessage(wxT("Basic dialog resource has now been unloaded, you ") |
142 | wxT("won't be able to use it before loading it again")); | |
43b2d5e7 | 143 | } |
60982dea | 144 | else |
43b2d5e7 | 145 | { |
9a83f860 | 146 | wxLogWarning(wxT("Failed to unload basic dialog resource")); |
43b2d5e7 | 147 | } |
60982dea VZ |
148 | } |
149 | ||
150 | void MyFrame::OnReloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event)) | |
151 | { | |
152 | if ( wxXmlResource::Get()->Load(wxT("rc/basicdlg.xrc")) ) | |
43b2d5e7 | 153 | { |
9a83f860 | 154 | wxLogStatus(wxT("Basic dialog resource has been loaded.")); |
43b2d5e7 | 155 | } |
60982dea | 156 | else |
43b2d5e7 | 157 | { |
9a83f860 | 158 | wxLogError(wxT("Failed to load basic dialog resource")); |
43b2d5e7 | 159 | } |
60982dea VZ |
160 | } |
161 | ||
af1337b0 JS |
162 | void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) |
163 | { | |
f80ea77b WS |
164 | // true is to force the frame to close. |
165 | Close(true); | |
af1337b0 JS |
166 | } |
167 | ||
168 | ||
169 | void MyFrame::OnNonDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) | |
170 | { | |
171 | wxDialog dlg; | |
172 | // "non_derived_dialog" is the name of the wxDialog XRC node that should | |
173 | // be loaded. | |
60982dea VZ |
174 | if ( wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("non_derived_dialog")) ) |
175 | dlg.ShowModal(); | |
af1337b0 JS |
176 | } |
177 | ||
178 | ||
f80ea77b | 179 | void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) |
af1337b0 JS |
180 | { |
181 | // Make an instance of our derived dialog, passing it "this" window | |
f80ea77b | 182 | // (the main frame) as the parent of the dialog. This allows the dialog |
af1337b0 JS |
183 | // to be destructed automatically when the parent is destroyed. |
184 | PreferencesDialog preferencesDialog(this); | |
185 | // Show the instance of the dialog, modally. | |
186 | preferencesDialog.ShowModal(); | |
187 | } | |
188 | ||
d956f3e7 VZ |
189 | void MyFrame::OnAnimationCtrlPlay(wxCommandEvent& event) |
190 | { | |
191 | #if wxUSE_ANIMATIONCTRL | |
192 | // get the pointers we need | |
193 | wxButton *btn = wxDynamicCast(event.GetEventObject(), wxButton); | |
194 | if (!btn || !btn->GetParent()) return; | |
195 | ||
196 | wxWindow *win = btn->GetParent(); | |
197 | wxAnimationCtrl *ctrl = XRCCTRL(*win, "controls_animation_ctrl", wxAnimationCtrl); | |
198 | if (ctrl->IsPlaying()) | |
199 | { | |
200 | ctrl->Stop(); | |
201 | btn->SetLabel(wxT("Play")); | |
202 | } | |
203 | else | |
204 | { | |
863d562d VZ |
205 | if (ctrl->Play()) |
206 | btn->SetLabel(wxT("Stop")); | |
207 | else | |
208 | wxLogError(wxT("Cannot play the animation...")); | |
d956f3e7 VZ |
209 | } |
210 | #endif | |
211 | } | |
af1337b0 | 212 | |
f80ea77b | 213 | void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) |
af1337b0 JS |
214 | { |
215 | wxDialog dlg; | |
2b5f62a0 | 216 | wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog")); |
af1337b0 JS |
217 | |
218 | #if wxUSE_LISTCTRL | |
ef18e792 VZ |
219 | // The resource file specifies the columns of the control as they are |
220 | // typically static while the items themselves are added from here as | |
221 | // usually they are not static (but if they are, they can be defined in the | |
222 | // resources too, see the two other list controls definitions in | |
223 | // controls.xrc) | |
224 | ||
225 | // Insert some items into the listctrl: notice that we can access it using | |
226 | // XRCCTRL | |
227 | wxListCtrl * const list = XRCCTRL(dlg, "controls_listctrl", wxListCtrl); | |
228 | ||
229 | list->InsertItem(0, "Athos"); list->SetItem(0, 1, "90"); | |
230 | list->InsertItem(1, "Porthos"); list->SetItem(1, 1, "120"); | |
231 | list->InsertItem(2, "Aramis"); list->SetItem(2, 1, "80"); | |
232 | #endif // wxUSE_LISTCTRL | |
af1337b0 | 233 | |
f80ea77b | 234 | #if wxUSE_TREECTRL |
af1337b0 | 235 | // There is no data in the tree ctrl. These lines will add some. |
f80ea77b WS |
236 | // (1) Instead of having to write out |
237 | // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction() | |
af1337b0 | 238 | // each time (which is also OK), this example code will shown how |
f80ea77b | 239 | // to make a pointer to the XRC control, so we can use |
af1337b0 JS |
240 | // treectrl->SomeFunction() as a short cut. This is useful if you |
241 | // will be referring to this control often in the code. | |
242 | wxTreeCtrl* treectrl = XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl); | |
243 | // (2) Add a root node | |
244 | treectrl->AddRoot(_("Godfather")); | |
245 | // (3)Append some items to the root node. | |
246 | treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 1")); | |
247 | treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 2")); | |
f80ea77b | 248 | treectrl->AppendItem(treectrl->GetRootItem(), _("Evil accountant")); |
af1337b0 | 249 | #endif |
f80ea77b | 250 | |
d956f3e7 VZ |
251 | #if wxUSE_ANIMATIONCTRL |
252 | // dynamically connect our event handler for the "clicked" event of the "play" button | |
253 | // in the animation ctrl page of our dialog | |
254 | dlg.Connect(XRCID("controls_animation_button_play"), wxEVT_COMMAND_BUTTON_CLICKED, | |
255 | wxCommandEventHandler(MyFrame::OnAnimationCtrlPlay)); | |
256 | #endif | |
257 | ||
af1337b0 JS |
258 | // All done. Show the dialog. |
259 | dlg.ShowModal(); | |
260 | } | |
261 | ||
262 | ||
f80ea77b | 263 | void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) |
af1337b0 JS |
264 | { |
265 | wxDialog dlg; | |
2b5f62a0 | 266 | wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("uncentered_dialog")); |
af1337b0 JS |
267 | dlg.ShowModal(); |
268 | } | |
269 | ||
270 | ||
271 | void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) | |
272 | { | |
273 | wxDialog dlg; | |
2b5f62a0 | 274 | wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("custom_class_dialog")); |
f80ea77b WS |
275 | |
276 | // Make an instance of our new custom class. | |
277 | MyResizableListCtrl* a_myResizableListCtrl = new MyResizableListCtrl(&dlg, | |
278 | wxID_ANY, | |
af1337b0 JS |
279 | wxDefaultPosition, |
280 | wxDefaultSize, | |
281 | wxLC_REPORT, | |
282 | wxDefaultValidator); | |
f80ea77b WS |
283 | |
284 | // "custom_control_placeholder" is the name of the "unknown" tag in the | |
af1337b0 | 285 | // custctrl.xrc XRC file. |
2b5f62a0 | 286 | wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"), |
af1337b0 JS |
287 | a_myResizableListCtrl); |
288 | dlg.ShowModal(); | |
289 | } | |
290 | ||
291 | ||
292 | void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) | |
293 | { | |
294 | wxDialog dlg; | |
2b5f62a0 | 295 | wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("platform_property_dialog")); |
af1337b0 JS |
296 | dlg.ShowModal(); |
297 | } | |
298 | ||
299 | ||
300 | void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) | |
301 | { | |
302 | wxDialog dlg; | |
2b5f62a0 | 303 | wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("art_provider_dialog")); |
af1337b0 JS |
304 | dlg.ShowModal(); |
305 | } | |
306 | ||
307 | ||
308 | void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) | |
309 | { | |
310 | wxDialog dlg; | |
2b5f62a0 | 311 | wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variable_expansion_dialog")); |
af1337b0 JS |
312 | dlg.ShowModal(); |
313 | } | |
314 | ||
af0ac990 VZ |
315 | void MyFrame::OnRecursiveLoad(wxCommandEvent& WXUNUSED(event)) |
316 | { | |
317 | // this dialog is created manually to show how you can inject a single | |
318 | // control from XRC into an existing dialog | |
319 | // | |
320 | // this is a slightly contrived example, please keep in mind that it's done | |
321 | // only to demonstrate LoadObjectRecursively() in action and is not the | |
322 | // recommended to do this | |
323 | wxDialog dlg(NULL, wxID_ANY, "Recursive Load Example", | |
324 | wxDefaultPosition, wxDefaultSize, | |
325 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER); | |
326 | wxSizer * const sizer = new wxBoxSizer(wxVERTICAL); | |
327 | sizer->Add | |
328 | ( | |
329 | new wxStaticText | |
330 | ( | |
331 | &dlg, | |
332 | wxID_ANY, | |
333 | "The entire tree book control below is loaded from XRC" | |
334 | ), | |
335 | wxSizerFlags().Expand().Border() | |
336 | ); | |
337 | ||
338 | sizer->Add | |
339 | ( | |
340 | static_cast<wxWindow *> | |
341 | ( | |
342 | // notice that controls_treebook is defined inside a notebook page | |
343 | // inside a dialog defined in controls.xrc and so LoadObject() | |
344 | // wouldn't find it -- but LoadObjectRecursively() does | |
345 | wxXmlResource::Get()-> | |
346 | LoadObjectRecursively(&dlg, "controls_treebook", "wxTreebook") | |
347 | ), | |
348 | wxSizerFlags(1).Expand().Border() | |
349 | ); | |
350 | ||
351 | dlg.SetSizer(sizer); | |
352 | dlg.SetClientSize(400, 200); | |
353 | ||
354 | dlg.ShowModal(); | |
355 | } | |
af1337b0 JS |
356 | |
357 | void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent& WXUNUSED(event)) | |
358 | { | |
359 | wxString msg; | |
9a83f860 VZ |
360 | msg.Printf( wxT("This is the about dialog of XML resources demo.\n") |
361 | wxT("Welcome to %s"), wxVERSION_STRING); | |
af1337b0 | 362 | |
2b5f62a0 | 363 | wxMessageBox(msg, _("About XML resources demo"), wxOK | wxICON_INFORMATION, this); |
af1337b0 | 364 | } |