1 //-----------------------------------------------------------------------------
3 // Purpose: XML resources sample: A derived frame, called MyFrame
4 // Author: Robert O'Connor (rob@medicalmnemonics.com), Vaclav Slavik
6 // Copyright: (c) Robert O'Connor and Vaclav Slavik
7 // Licence: wxWindows licence
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
11 // Standard wxWidgets headers
12 //-----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
21 // For all others, include the necessary headers (this file is usually all you
22 // need because it includes almost all "standard" wxWidgets headers)
27 #include "wx/sysopt.h"
29 //-----------------------------------------------------------------------------
30 // Header of this .cpp file
31 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
36 // Remaining headers: Needed wx headers, then wx/contrib headers, then application headers
37 //-----------------------------------------------------------------------------
39 // Since setting an icon
42 //-----------------------------------------------------------------------------
44 #include "wx/xrc/xmlres.h" // XRC XML resouces
46 //-----------------------------------------------------------------------------
48 // Our derived dialog for the derived dialog example.
50 // Our custom class, for the custom class example.
52 // For functions to manipulate our wxTreeCtrl and wxListCtrl
53 #include "wx/treectrl.h"
54 #include "wx/listctrl.h"
56 //-----------------------------------------------------------------------------
57 // Regular resources (the non-XRC kind).
58 //-----------------------------------------------------------------------------
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"
66 //-----------------------------------------------------------------------------
67 // Event table: connect the events to the handler functions to process them
68 //-----------------------------------------------------------------------------
70 // The event tables connect the wxWidgets events with the functions (event
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.
73 // The reason why the menuitems and tools are given the same name in the
74 // XRC file, is that both a tool (a toolbar item) and a menuitem are designed
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
77 // when starting out with wxWidgets.
78 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
79 EVT_MENU(XRCID("unload_resource_menuitem"), MyFrame::OnUnloadResourceMenuCommand
)
80 EVT_MENU(XRCID("reload_resource_menuitem"), MyFrame::OnReloadResourceMenuCommand
)
81 EVT_MENU(XRCID("exit_tool_or_menuitem"), MyFrame::OnExitToolOrMenuCommand
)
82 EVT_MENU(XRCID("exit_tool_or_menuitem"), MyFrame::OnExitToolOrMenuCommand
)
83 EVT_MENU(XRCID("non_derived_dialog_tool_or_menuitem"), MyFrame::OnNonDerivedDialogToolOrMenuCommand
)
84 EVT_MENU(XRCID("derived_tool_or_menuitem"), MyFrame::OnDerivedDialogToolOrMenuCommand
)
85 EVT_MENU(XRCID("controls_tool_or_menuitem"), MyFrame::OnControlsToolOrMenuCommand
)
86 EVT_MENU(XRCID("uncentered_tool_or_menuitem"), MyFrame::OnUncenteredToolOrMenuCommand
)
87 EVT_MENU(XRCID("custom_class_tool_or_menuitem"), MyFrame::OnCustomClassToolOrMenuCommand
)
88 EVT_MENU(XRCID("platform_property_tool_or_menuitem"), MyFrame::OnPlatformPropertyToolOrMenuCommand
)
89 EVT_MENU(XRCID("art_provider_tool_or_menuitem"), MyFrame::OnArtProviderToolOrMenuCommand
)
90 EVT_MENU(XRCID("variable_expansion_tool_or_menuitem"), MyFrame::OnVariableExpansionToolOrMenuCommand
)
91 EVT_MENU(XRCID("about_tool_or_menuitem"), MyFrame::OnAboutToolOrMenuCommand
)
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
99 MyFrame::MyFrame(wxWindow
* parent
)
101 // Load up this frame from XRC. [Note, instead of making a class's
102 // constructor take a wxWindow* parent with a default value of NULL,
103 // we could have just had designed MyFrame class with an empty
104 // constructor and then written here:
105 // wxXmlResource::Get()->LoadFrame(this, (wxWindow* )NULL, "main_frame");
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
108 // for other frames that aren't the top level window.]
109 wxXmlResource::Get()->LoadFrame(this, parent
, wxT("main_frame"));
111 // Set the icon for the frame.
112 SetIcon(wxICON(appicon
));
114 // Load the menubar from XRC and set this frame's menubar to it.
115 SetMenuBar(wxXmlResource::Get()->LoadMenuBar(wxT("main_menu")));
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.
120 wxSystemOptions::SetOption ( wxT("msw.remap"), 0 );
121 SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar")));
124 // Give the frame an optional statusbar. The '1' just means one field.
125 // A gripsizer will automatically get put on into the corner, if that
126 // is the normal OS behaviour for frames on that platform. Helptext
127 // for menu items and toolbar tools will automatically get displayed
129 CreateStatusBar( 1 );
130 #endif // wxUSE_STATUSBAR
133 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
137 void MyFrame::OnUnloadResourceMenuCommand(wxCommandEvent
& WXUNUSED(event
))
139 if ( wxXmlResource::Get()->Unload(wxT("rc/basicdlg.xrc")) )
140 wxLogMessage(_T("Basic dialog resource has now been unloaded, you ")
141 _T("won't be able to use it before loading it again"));
143 wxLogWarning(_T("Failed to unload basic dialog resource"));
146 void MyFrame::OnReloadResourceMenuCommand(wxCommandEvent
& WXUNUSED(event
))
148 if ( wxXmlResource::Get()->Load(wxT("rc/basicdlg.xrc")) )
149 wxLogStatus(_T("Basic dialog resource has been loaded."));
151 wxLogError(_T("Failed to load basic dialog resource"));
154 void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
156 // true is to force the frame to close.
161 void MyFrame::OnNonDerivedDialogToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
164 // "non_derived_dialog" is the name of the wxDialog XRC node that should
166 if ( wxXmlResource::Get()->LoadDialog(&dlg
, this, wxT("non_derived_dialog")) )
171 void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
173 // Make an instance of our derived dialog, passing it "this" window
174 // (the main frame) as the parent of the dialog. This allows the dialog
175 // to be destructed automatically when the parent is destroyed.
176 PreferencesDialog
preferencesDialog(this);
177 // Show the instance of the dialog, modally.
178 preferencesDialog
.ShowModal();
181 void MyFrame::OnAnimationCtrlPlay(wxCommandEvent
& event
)
183 #if wxUSE_ANIMATIONCTRL
184 // get the pointers we need
185 wxButton
*btn
= wxDynamicCast(event
.GetEventObject(), wxButton
);
186 if (!btn
|| !btn
->GetParent()) return;
188 wxWindow
*win
= btn
->GetParent();
189 wxAnimationCtrl
*ctrl
= XRCCTRL(*win
, "controls_animation_ctrl", wxAnimationCtrl
);
190 if (ctrl
->IsPlaying())
193 btn
->SetLabel(wxT("Play"));
198 btn
->SetLabel(wxT("Stop"));
200 wxLogError(wxT("Cannot play the animation..."));
205 void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
208 wxXmlResource::Get()->LoadDialog(&dlg
, this, wxT("controls_dialog"));
211 // There is no data in the listctrl. This will add some columns
212 // and some data. You don't need to use any pointers
213 // at all to manipulate the controls, just simply use the XRCCTL(...) macros.
214 // "controls_treectrl" is the name of this control in the XRC.
215 // (1) Insert a column, with the column header of "Name"
216 // (The '_' function around "Name" marks this string as one to translate).
217 XRCCTRL(dlg
, "controls_listctrl", wxListCtrl
)->InsertColumn( 0,
222 // (2) Insert some items into the listctrl
223 XRCCTRL(dlg
, "controls_listctrl", wxListCtrl
)->InsertItem(0,wxT("Todd Hope"));
224 XRCCTRL(dlg
, "controls_listctrl", wxListCtrl
)->InsertItem(1,wxT("Kim Wynd"));
225 XRCCTRL(dlg
, "controls_listctrl", wxListCtrl
)->InsertItem(2,wxT("Leon Li"));
229 // There is no data in the tree ctrl. These lines will add some.
230 // (1) Instead of having to write out
231 // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction()
232 // each time (which is also OK), this example code will shown how
233 // to make a pointer to the XRC control, so we can use
234 // treectrl->SomeFunction() as a short cut. This is useful if you
235 // will be referring to this control often in the code.
236 wxTreeCtrl
* treectrl
= XRCCTRL(dlg
, "controls_treectrl", wxTreeCtrl
);
237 // (2) Add a root node
238 treectrl
->AddRoot(_("Godfather"));
239 // (3)Append some items to the root node.
240 treectrl
->AppendItem(treectrl
->GetRootItem(), _("Evil henchman 1"));
241 treectrl
->AppendItem(treectrl
->GetRootItem(), _("Evil henchman 2"));
242 treectrl
->AppendItem(treectrl
->GetRootItem(), _("Evil accountant"));
245 #if wxUSE_ANIMATIONCTRL
246 // dynamically connect our event handler for the "clicked" event of the "play" button
247 // in the animation ctrl page of our dialog
248 dlg
.Connect(XRCID("controls_animation_button_play"), wxEVT_COMMAND_BUTTON_CLICKED
,
249 wxCommandEventHandler(MyFrame::OnAnimationCtrlPlay
));
252 // All done. Show the dialog.
257 void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
260 wxXmlResource::Get()->LoadDialog(&dlg
, this, wxT("uncentered_dialog"));
265 void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
268 wxXmlResource::Get()->LoadDialog(&dlg
, this, wxT("custom_class_dialog"));
270 // Make an instance of our new custom class.
271 MyResizableListCtrl
* a_myResizableListCtrl
= new MyResizableListCtrl(&dlg
,
278 // "custom_control_placeholder" is the name of the "unknown" tag in the
279 // custctrl.xrc XRC file.
280 wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"),
281 a_myResizableListCtrl
);
286 void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
289 wxXmlResource::Get()->LoadDialog(&dlg
, this, wxT("platform_property_dialog"));
294 void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
297 wxXmlResource::Get()->LoadDialog(&dlg
, this, wxT("art_provider_dialog"));
302 void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
305 wxXmlResource::Get()->LoadDialog(&dlg
, this, wxT("variable_expansion_dialog"));
310 void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent
& WXUNUSED(event
))
313 msg
.Printf( _T("This is the about dialog of XML resources demo.\n")
314 _T("Welcome to %s"), wxVERSION_STRING
);
316 wxMessageBox(msg
, _("About XML resources demo"), wxOK
| wxICON_INFORMATION
, this);