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