myframe.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. // Copyright: (c) Robert O'Connor and Vaclav Slavik
  6. // Licence: wxWindows licence
  7. //-----------------------------------------------------------------------------
  8. //-----------------------------------------------------------------------------
  9. // Standard wxWidgets headers
  10. //-----------------------------------------------------------------------------
  11. // For compilers that support precompilation, includes "wx/wx.h".
  12. #include "wx/wxprec.h"
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. // For all others, include the necessary headers (this file is usually all you
  17. // need because it includes almost all "standard" wxWidgets headers)
  18. #ifndef WX_PRECOMP
  19. #include "wx/wx.h"
  20. #endif
  21. #include "wx/sysopt.h"
  22. //-----------------------------------------------------------------------------
  23. // Header of this .cpp file
  24. //-----------------------------------------------------------------------------
  25. #include "myframe.h"
  26. //-----------------------------------------------------------------------------
  27. // Remaining headers: Needed wx headers, then wx/contrib headers, then application headers
  28. //-----------------------------------------------------------------------------
  29. // Since setting an icon
  30. #include "wx/image.h"
  31. //-----------------------------------------------------------------------------
  32. #include "wx/xrc/xmlres.h" // XRC XML resouces
  33. //-----------------------------------------------------------------------------
  34. // Our derived dialog for the derived dialog example.
  35. #include "derivdlg.h"
  36. // Our custom class, for the custom class example.
  37. #include "custclas.h"
  38. // And our objref dialog, for the object reference and ID range example.
  39. #include "objrefdlg.h"
  40. // For functions to manipulate the corresponding controls.
  41. #include "wx/animate.h"
  42. #include "wx/treectrl.h"
  43. #include "wx/listctrl.h"
  44. //-----------------------------------------------------------------------------
  45. // Regular resources (the non-XRC kind).
  46. //-----------------------------------------------------------------------------
  47. // the application icon (under Windows and OS/2 it is in resources and even
  48. // though we could still include the XPM here it would be unused)
  49. #ifndef wxHAS_IMAGES_IN_RESOURCES
  50. #include "../sample.xpm"
  51. #endif
  52. //-----------------------------------------------------------------------------
  53. // Event table: connect the events to the handler functions to process them
  54. //-----------------------------------------------------------------------------
  55. // The event tables connect the wxWidgets events with the functions (event
  56. // handlers) which process them. It can be also done at run-time, but for the
  57. // simple menu events like this the static method is much simpler.
  58. // The reason why the menuitems and tools are given the same name in the
  59. // XRC file, is that both a tool (a toolbar item) and a menuitem are designed
  60. // to fire the same kind of event (an EVT_MENU) and thus I give them the same
  61. // ID name to help new users emphasize this point which is often overlooked
  62. // when starting out with wxWidgets.
  63. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  64. EVT_MENU(XRCID("unload_resource_menuitem"), MyFrame::OnUnloadResourceMenuCommand)
  65. EVT_MENU(XRCID("reload_resource_menuitem"), MyFrame::OnReloadResourceMenuCommand)
  66. EVT_MENU(wxID_EXIT, MyFrame::OnExitToolOrMenuCommand)
  67. EVT_MENU(XRCID("non_derived_dialog_tool_or_menuitem"), MyFrame::OnNonDerivedDialogToolOrMenuCommand)
  68. EVT_MENU(XRCID("derived_tool_or_menuitem"), MyFrame::OnDerivedDialogToolOrMenuCommand)
  69. EVT_MENU(XRCID("controls_tool_or_menuitem"), MyFrame::OnControlsToolOrMenuCommand)
  70. EVT_MENU(XRCID("uncentered_tool_or_menuitem"), MyFrame::OnUncenteredToolOrMenuCommand)
  71. EVT_MENU(XRCID("obj_ref_tool_or_menuitem"), MyFrame::OnObjRefToolOrMenuCommand)
  72. EVT_MENU(XRCID("custom_class_tool_or_menuitem"), MyFrame::OnCustomClassToolOrMenuCommand)
  73. EVT_MENU(XRCID("platform_property_tool_or_menuitem"), MyFrame::OnPlatformPropertyToolOrMenuCommand)
  74. EVT_MENU(XRCID("art_provider_tool_or_menuitem"), MyFrame::OnArtProviderToolOrMenuCommand)
  75. EVT_MENU(XRCID("variable_expansion_tool_or_menuitem"), MyFrame::OnVariableExpansionToolOrMenuCommand)
  76. EVT_MENU(XRCID("variants"), MyFrame::OnVariants)
  77. EVT_MENU(XRCID("recursive_load"), MyFrame::OnRecursiveLoad)
  78. EVT_MENU(wxID_ABOUT, MyFrame::OnAboutToolOrMenuCommand)
  79. wxEND_EVENT_TABLE()
  80. //-----------------------------------------------------------------------------
  81. // Public methods
  82. //-----------------------------------------------------------------------------
  83. // Constructor
  84. MyFrame::MyFrame(wxWindow* parent)
  85. {
  86. // Load up this frame from XRC. [Note, instead of making a class's
  87. // constructor take a wxWindow* parent with a default value of NULL,
  88. // we could have just had designed MyFrame class with an empty
  89. // constructor and then written here:
  90. // wxXmlResource::Get()->LoadFrame(this, (wxWindow* )NULL, "main_frame");
  91. // since this frame will always be the top window, and thus parentless.
  92. // However, the current approach has source code that can be recycled
  93. // for other frames that aren't the top level window.]
  94. wxXmlResource::Get()->LoadFrame(this, parent, wxT("main_frame"));
  95. // Set the icon for the frame.
  96. SetIcon(wxICON(sample));
  97. // Load the menubar from XRC and set this frame's menubar to it.
  98. SetMenuBar(wxXmlResource::Get()->LoadMenuBar(wxT("main_menu")));
  99. // Load the toolbar from XRC and set this frame's toolbar to it.
  100. // NOTE: For toolbars you currently should do it exactly like this.
  101. // With toolbars, you currently can't create one, and set it later. It
  102. // needs to be all in one step.
  103. wxSystemOptions::SetOption ( wxT("msw.remap"), 0 );
  104. SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar")));
  105. #if wxUSE_STATUSBAR
  106. // Give the frame an optional statusbar. The '1' just means one field.
  107. // A gripsizer will automatically get put on into the corner, if that
  108. // is the normal OS behaviour for frames on that platform. Helptext
  109. // for menu items and toolbar tools will automatically get displayed
  110. // here.
  111. CreateStatusBar( 1 );
  112. #endif // wxUSE_STATUSBAR
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Private methods
  116. //-----------------------------------------------------------------------------
  117. void MyFrame::OnUnloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
  118. {
  119. if ( wxXmlResource::Get()->Unload(wxT("rc/basicdlg.xrc")) )
  120. {
  121. wxLogMessage(wxT("Basic dialog resource has now been unloaded, you ")
  122. wxT("won't be able to use it before loading it again"));
  123. }
  124. else
  125. {
  126. wxLogWarning(wxT("Failed to unload basic dialog resource"));
  127. }
  128. }
  129. void MyFrame::OnReloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
  130. {
  131. if ( wxXmlResource::Get()->Load(wxT("rc/basicdlg.xrc")) )
  132. {
  133. wxLogStatus(wxT("Basic dialog resource has been loaded."));
  134. }
  135. else
  136. {
  137. wxLogError(wxT("Failed to load basic dialog resource"));
  138. }
  139. }
  140. void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  141. {
  142. // true is to force the frame to close.
  143. Close(true);
  144. }
  145. void MyFrame::OnNonDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  146. {
  147. wxDialog dlg;
  148. // "non_derived_dialog" is the name of the wxDialog XRC node that should
  149. // be loaded.
  150. if ( wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("non_derived_dialog")) )
  151. dlg.ShowModal();
  152. }
  153. void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  154. {
  155. // Make an instance of our derived dialog, passing it "this" window
  156. // (the main frame) as the parent of the dialog. This allows the dialog
  157. // to be destructed automatically when the parent is destroyed.
  158. PreferencesDialog preferencesDialog(this);
  159. // Show the instance of the dialog, modally.
  160. preferencesDialog.ShowModal();
  161. }
  162. void MyFrame::OnAnimationCtrlPlay(wxCommandEvent& event)
  163. {
  164. #if wxUSE_ANIMATIONCTRL
  165. // get the pointers we need
  166. wxButton *btn = wxDynamicCast(event.GetEventObject(), wxButton);
  167. if (!btn || !btn->GetParent()) return;
  168. wxWindow *win = btn->GetParent();
  169. wxAnimationCtrl *ctrl = XRCCTRL(*win, "controls_animation_ctrl", wxAnimationCtrl);
  170. if (ctrl->IsPlaying())
  171. {
  172. ctrl->Stop();
  173. btn->SetLabel(wxT("Play"));
  174. }
  175. else
  176. {
  177. if (ctrl->Play())
  178. btn->SetLabel(wxT("Stop"));
  179. else
  180. wxLogError(wxT("Cannot play the animation..."));
  181. }
  182. #endif
  183. }
  184. void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  185. {
  186. wxDialog dlg;
  187. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog"));
  188. #if wxUSE_LISTCTRL
  189. // The resource file specifies the columns of the control as they are
  190. // typically static while the items themselves are added from here as
  191. // usually they are not static (but if they are, they can be defined in the
  192. // resources too, see the two other list controls definitions in
  193. // controls.xrc)
  194. // Insert some items into the listctrl: notice that we can access it using
  195. // XRCCTRL
  196. wxListCtrl * const list = XRCCTRL(dlg, "controls_listctrl", wxListCtrl);
  197. list->InsertItem(0, "Athos", 0); list->SetItem(0, 1, "90", 2);
  198. list->InsertItem(1, "Porthos", 5); list->SetItem(1, 1, "120", 3);
  199. list->InsertItem(2, "Aramis", 1); list->SetItem(2, 1, "80", 4);
  200. #endif // wxUSE_LISTCTRL
  201. #if wxUSE_TREECTRL
  202. // There is no data in the tree ctrl. These lines will add some.
  203. // (1) Instead of having to write out
  204. // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction()
  205. // each time (which is also OK), this example code will shown how
  206. // to make a pointer to the XRC control, so we can use
  207. // treectrl->SomeFunction() as a short cut. This is useful if you
  208. // will be referring to this control often in the code.
  209. wxTreeCtrl* treectrl = XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl);
  210. // (2) Add a root node
  211. treectrl->AddRoot(_("Godfather"));
  212. // (3)Append some items to the root node.
  213. treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 1"));
  214. treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 2"));
  215. treectrl->AppendItem(treectrl->GetRootItem(), _("Evil accountant"));
  216. #endif
  217. #if wxUSE_ANIMATIONCTRL
  218. // dynamically connect our event handler for the "clicked" event of the "play" button
  219. // in the animation ctrl page of our dialog
  220. dlg.Connect(XRCID("controls_animation_button_play"), wxEVT_BUTTON,
  221. wxCommandEventHandler(MyFrame::OnAnimationCtrlPlay));
  222. #endif
  223. // All done. Show the dialog.
  224. dlg.ShowModal();
  225. }
  226. void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  227. {
  228. wxDialog dlg;
  229. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("uncentered_dialog"));
  230. dlg.ShowModal();
  231. }
  232. void MyFrame::OnObjRefToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  233. {
  234. // The dialog redirects log messages, so save the old log target first
  235. wxLog* oldlogtarget = wxLog::SetActiveTarget(NULL);
  236. // Make an instance of the dialog
  237. ObjrefDialog* objrefDialog = new ObjrefDialog(this);
  238. // Show the instance of the dialog, modally.
  239. objrefDialog->ShowModal();
  240. objrefDialog->Destroy();
  241. // Restore the old log target
  242. delete wxLog::SetActiveTarget(oldlogtarget);
  243. }
  244. void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  245. {
  246. wxDialog dlg;
  247. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("custom_class_dialog"));
  248. // Make an instance of our new custom class.
  249. MyResizableListCtrl* a_myResizableListCtrl = new MyResizableListCtrl(&dlg,
  250. wxID_ANY,
  251. wxDefaultPosition,
  252. wxDefaultSize,
  253. wxLC_REPORT,
  254. wxDefaultValidator);
  255. // "custom_control_placeholder" is the name of the "unknown" tag in the
  256. // custctrl.xrc XRC file.
  257. wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"),
  258. a_myResizableListCtrl);
  259. dlg.ShowModal();
  260. }
  261. void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  262. {
  263. wxDialog dlg;
  264. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("platform_property_dialog"));
  265. dlg.ShowModal();
  266. }
  267. void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  268. {
  269. wxDialog dlg;
  270. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("art_provider_dialog"));
  271. dlg.ShowModal();
  272. }
  273. void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  274. {
  275. wxDialog dlg;
  276. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variable_expansion_dialog"));
  277. dlg.ShowModal();
  278. }
  279. void MyFrame::OnVariants(wxCommandEvent& WXUNUSED(event))
  280. {
  281. wxDialog dlg;
  282. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variants_dialog"));
  283. dlg.ShowModal();
  284. }
  285. void MyFrame::OnRecursiveLoad(wxCommandEvent& WXUNUSED(event))
  286. {
  287. // this dialog is created manually to show how you can inject a single
  288. // control from XRC into an existing dialog
  289. //
  290. // this is a slightly contrived example, please keep in mind that it's done
  291. // only to demonstrate LoadObjectRecursively() in action and is not the
  292. // recommended to do this
  293. wxDialog dlg(NULL, wxID_ANY, "Recursive Load Example",
  294. wxDefaultPosition, wxDefaultSize,
  295. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  296. wxSizer * const sizer = new wxBoxSizer(wxVERTICAL);
  297. sizer->Add
  298. (
  299. new wxStaticText
  300. (
  301. &dlg,
  302. wxID_ANY,
  303. "The entire tree book control below is loaded from XRC"
  304. ),
  305. wxSizerFlags().Expand().Border()
  306. );
  307. sizer->Add
  308. (
  309. static_cast<wxWindow *>
  310. (
  311. // notice that controls_treebook is defined inside a notebook page
  312. // inside a dialog defined in controls.xrc and so LoadObject()
  313. // wouldn't find it -- but LoadObjectRecursively() does
  314. wxXmlResource::Get()->
  315. LoadObjectRecursively(&dlg, "controls_treebook", "wxTreebook")
  316. ),
  317. wxSizerFlags(1).Expand().Border()
  318. );
  319. dlg.SetSizer(sizer);
  320. dlg.SetClientSize(400, 200);
  321. dlg.ShowModal();
  322. }
  323. void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  324. {
  325. wxString msg;
  326. msg.Printf( wxT("This is the about dialog of XML resources demo.\n")
  327. wxT("Welcome to %s"), wxVERSION_STRING);
  328. wxMessageBox(msg, _("About XML resources demo"), wxOK | wxICON_INFORMATION, this);
  329. }