]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/xrc/myframe.cpp
moved XML classes to the core
[wxWidgets.git] / contrib / 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 // GCC implementation
12 //-----------------------------------------------------------------------------
13
14 #ifdef __GNUG__
15 #pragma implementation "myframe.h"
16 #endif
17
18 //-----------------------------------------------------------------------------
19 // Standard wxWindows headers
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
30 // need because it includes almost all "standard" wxWindows headers)
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
76 // The event tables connect the wxWindows events with the functions (event
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.
79 // The reason why the menuitems and tools are given the same name in the
80 // XRC file, is that both a tool (a toolbar item) and a menuitem are designed
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
83 // when starting out with wxWindows.
84 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
85 EVT_MENU(XRCID("exit_tool_or_menuitem"), MyFrame::OnExitToolOrMenuCommand)
86 EVT_MENU(XRCID("non_derived_dialog_tool_or_menuitem"), MyFrame::OnNonDerivedDialogToolOrMenuCommand)
87 EVT_MENU(XRCID("derived_tool_or_menuitem"), MyFrame::OnDerivedDialogToolOrMenuCommand)
88 EVT_MENU(XRCID("controls_tool_or_menuitem"), MyFrame::OnControlsToolOrMenuCommand)
89 EVT_MENU(XRCID("uncentered_tool_or_menuitem"), MyFrame::OnUncenteredToolOrMenuCommand)
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("about_tool_or_menuitem"), 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(appicon));
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 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar")));
124
125 // Give the frame a optional statusbar. The '1' just means one field.
126 // A gripsizer will automatically get put on into the corner, if that
127 // is the normal OS behaviour for frames on that platform. Helptext
128 // for menu items and toolbar tools will automatically get displayed
129 // here.
130 CreateStatusBar( 1 );
131 }
132
133 //-----------------------------------------------------------------------------
134 // Private methods
135 //-----------------------------------------------------------------------------
136
137 void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
138 {
139 // TRUE is to force the frame to close.
140 Close(TRUE);
141 }
142
143
144 void MyFrame::OnNonDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
145 {
146 wxDialog dlg;
147 // "non_derived_dialog" is the name of the wxDialog XRC node that should
148 // be loaded.
149 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("non_derived_dialog"));
150 dlg.ShowModal();
151
152 }
153
154
155 void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
156 {
157 // Make an instance of our derived dialog, passing it "this" window
158 // (the main frame) as the parent of the dialog. This allows the dialog
159 // to be destructed automatically when the parent is destroyed.
160 PreferencesDialog preferencesDialog(this);
161 // Show the instance of the dialog, modally.
162 preferencesDialog.ShowModal();
163 }
164
165
166 void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
167 {
168 wxDialog dlg;
169 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog"));
170
171 #if wxUSE_LISTCTRL
172 // There is no data in the listctrl. This will add some columns
173 // and some data. You don't need use any pointers
174 // at all to manipulate the controls, just simply use the XRCCTL(...) macros.
175 // "controls_treectrl" is the name of this control in the XRC.
176 // (1) Insert a column, with the column header of "Name"
177 // (The '_' function around "Name" marks this string as one to translate).
178 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertColumn( 0,
179 _("Name"),
180 wxLIST_FORMAT_LEFT,
181 ( 200 )
182 );
183 // (2) Insert some items into the listctrl
184 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(0,wxT("Todd Hope"));
185 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(1,wxT("Kim Wynd"));
186 XRCCTRL(dlg, "controls_listctrl", wxListCtrl)->InsertItem(2,wxT("Leon Li"));
187 #endif
188
189 #if wxUSE_TREECTRL
190 // There is no data in the tree ctrl. These lines will add some.
191 // (1) Instead of having to write out
192 // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction()
193 // each time (which is also OK), this example code will shown how
194 // to make a pointer to the XRC control, so we can use
195 // treectrl->SomeFunction() as a short cut. This is useful if you
196 // will be referring to this control often in the code.
197 wxTreeCtrl* treectrl = XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl);
198 // (2) Add a root node
199 treectrl->AddRoot(_("Godfather"));
200 // (3)Append some items to the root node.
201 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 1"));
202 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 2"));
203 treectrl->AppendItem(treectrl->GetRootItem(), _("Evil accountant"));
204 #endif
205
206 // All done. Show the dialog.
207 dlg.ShowModal();
208 }
209
210
211 void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
212 {
213 wxDialog dlg;
214 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("uncentered_dialog"));
215 dlg.ShowModal();
216 }
217
218
219 void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
220 {
221 wxDialog dlg;
222 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("custom_class_dialog"));
223
224 // Make an instance of our new custom class.
225 MyResizableListCtrl* a_myResizableListCtrl = new MyResizableListCtrl(&dlg,
226 -1,
227 wxDefaultPosition,
228 wxDefaultSize,
229 wxLC_REPORT,
230 wxDefaultValidator);
231
232 // "custom_control_placeholder" is the name of the "unknown" tag in the
233 // custctrl.xrc XRC file.
234 wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"),
235 a_myResizableListCtrl);
236 dlg.ShowModal();
237 }
238
239
240 void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
241 {
242 wxDialog dlg;
243 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("platform_property_dialog"));
244 dlg.ShowModal();
245 }
246
247
248 void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
249 {
250 wxDialog dlg;
251 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("art_provider_dialog"));
252 dlg.ShowModal();
253 }
254
255
256 void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
257 {
258 wxDialog dlg;
259 wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variable_expansion_dialog"));
260 dlg.ShowModal();
261 }
262
263
264 void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
265 {
266 wxString msg;
267 msg.Printf( _T("This is the about dialog of XML resources demo.\n")
268 _T("Welcome to %s"), wxVERSION_STRING);
269
270 wxMessageBox(msg, _("About XML resources demo"), wxOK | wxICON_INFORMATION, this);
271 }