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