myframe.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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("recursive_load"), MyFrame::OnRecursiveLoad)
  77. EVT_MENU(wxID_ABOUT, MyFrame::OnAboutToolOrMenuCommand)
  78. wxEND_EVENT_TABLE()
  79. //-----------------------------------------------------------------------------
  80. // Public methods
  81. //-----------------------------------------------------------------------------
  82. // Constructor
  83. MyFrame::MyFrame(wxWindow* parent)
  84. {
  85. // Load up this frame from XRC. [Note, instead of making a class's
  86. // constructor take a wxWindow* parent with a default value of NULL,
  87. // we could have just had designed MyFrame class with an empty
  88. // constructor and then written here:
  89. // wxXmlResource::Get()->LoadFrame(this, (wxWindow* )NULL, "main_frame");
  90. // since this frame will always be the top window, and thus parentless.
  91. // However, the current approach has source code that can be recycled
  92. // for other frames that aren't the top level window.]
  93. wxXmlResource::Get()->LoadFrame(this, parent, wxT("main_frame"));
  94. // Set the icon for the frame.
  95. SetIcon(wxICON(sample));
  96. // Load the menubar from XRC and set this frame's menubar to it.
  97. SetMenuBar(wxXmlResource::Get()->LoadMenuBar(wxT("main_menu")));
  98. // Load the toolbar from XRC and set this frame's toolbar to it.
  99. // NOTE: For toolbars you currently should do it exactly like this.
  100. // With toolbars, you currently can't create one, and set it later. It
  101. // needs to be all in one step.
  102. wxSystemOptions::SetOption ( wxT("msw.remap"), 0 );
  103. SetToolBar(wxXmlResource::Get()->LoadToolBar(this, wxT("main_toolbar")));
  104. #if wxUSE_STATUSBAR
  105. // Give the frame an optional statusbar. The '1' just means one field.
  106. // A gripsizer will automatically get put on into the corner, if that
  107. // is the normal OS behaviour for frames on that platform. Helptext
  108. // for menu items and toolbar tools will automatically get displayed
  109. // here.
  110. CreateStatusBar( 1 );
  111. #endif // wxUSE_STATUSBAR
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Private methods
  115. //-----------------------------------------------------------------------------
  116. void MyFrame::OnUnloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
  117. {
  118. if ( wxXmlResource::Get()->Unload(wxT("rc/basicdlg.xrc")) )
  119. {
  120. wxLogMessage(wxT("Basic dialog resource has now been unloaded, you ")
  121. wxT("won't be able to use it before loading it again"));
  122. }
  123. else
  124. {
  125. wxLogWarning(wxT("Failed to unload basic dialog resource"));
  126. }
  127. }
  128. void MyFrame::OnReloadResourceMenuCommand(wxCommandEvent& WXUNUSED(event))
  129. {
  130. if ( wxXmlResource::Get()->Load(wxT("rc/basicdlg.xrc")) )
  131. {
  132. wxLogStatus(wxT("Basic dialog resource has been loaded."));
  133. }
  134. else
  135. {
  136. wxLogError(wxT("Failed to load basic dialog resource"));
  137. }
  138. }
  139. void MyFrame::OnExitToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  140. {
  141. // true is to force the frame to close.
  142. Close(true);
  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. if ( wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("non_derived_dialog")) )
  150. dlg.ShowModal();
  151. }
  152. void MyFrame::OnDerivedDialogToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  153. {
  154. // Make an instance of our derived dialog, passing it "this" window
  155. // (the main frame) as the parent of the dialog. This allows the dialog
  156. // to be destructed automatically when the parent is destroyed.
  157. PreferencesDialog preferencesDialog(this);
  158. // Show the instance of the dialog, modally.
  159. preferencesDialog.ShowModal();
  160. }
  161. void MyFrame::OnAnimationCtrlPlay(wxCommandEvent& event)
  162. {
  163. #if wxUSE_ANIMATIONCTRL
  164. // get the pointers we need
  165. wxButton *btn = wxDynamicCast(event.GetEventObject(), wxButton);
  166. if (!btn || !btn->GetParent()) return;
  167. wxWindow *win = btn->GetParent();
  168. wxAnimationCtrl *ctrl = XRCCTRL(*win, "controls_animation_ctrl", wxAnimationCtrl);
  169. if (ctrl->IsPlaying())
  170. {
  171. ctrl->Stop();
  172. btn->SetLabel(wxT("Play"));
  173. }
  174. else
  175. {
  176. if (ctrl->Play())
  177. btn->SetLabel(wxT("Stop"));
  178. else
  179. wxLogError(wxT("Cannot play the animation..."));
  180. }
  181. #endif
  182. }
  183. void MyFrame::OnControlsToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  184. {
  185. wxDialog dlg;
  186. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("controls_dialog"));
  187. #if wxUSE_LISTCTRL
  188. // The resource file specifies the columns of the control as they are
  189. // typically static while the items themselves are added from here as
  190. // usually they are not static (but if they are, they can be defined in the
  191. // resources too, see the two other list controls definitions in
  192. // controls.xrc)
  193. // Insert some items into the listctrl: notice that we can access it using
  194. // XRCCTRL
  195. wxListCtrl * const list = XRCCTRL(dlg, "controls_listctrl", wxListCtrl);
  196. list->InsertItem(0, "Athos", 0); list->SetItem(0, 1, "90", 2);
  197. list->InsertItem(1, "Porthos", 5); list->SetItem(1, 1, "120", 3);
  198. list->InsertItem(2, "Aramis", 1); list->SetItem(2, 1, "80", 4);
  199. #endif // wxUSE_LISTCTRL
  200. #if wxUSE_TREECTRL
  201. // There is no data in the tree ctrl. These lines will add some.
  202. // (1) Instead of having to write out
  203. // XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl)->SomeFunction()
  204. // each time (which is also OK), this example code will shown how
  205. // to make a pointer to the XRC control, so we can use
  206. // treectrl->SomeFunction() as a short cut. This is useful if you
  207. // will be referring to this control often in the code.
  208. wxTreeCtrl* treectrl = XRCCTRL(dlg, "controls_treectrl", wxTreeCtrl);
  209. // (2) Add a root node
  210. treectrl->AddRoot(_("Godfather"));
  211. // (3)Append some items to the root node.
  212. treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 1"));
  213. treectrl->AppendItem(treectrl->GetRootItem(), _("Evil henchman 2"));
  214. treectrl->AppendItem(treectrl->GetRootItem(), _("Evil accountant"));
  215. #endif
  216. #if wxUSE_ANIMATIONCTRL
  217. // dynamically connect our event handler for the "clicked" event of the "play" button
  218. // in the animation ctrl page of our dialog
  219. dlg.Connect(XRCID("controls_animation_button_play"), wxEVT_BUTTON,
  220. wxCommandEventHandler(MyFrame::OnAnimationCtrlPlay));
  221. #endif
  222. // All done. Show the dialog.
  223. dlg.ShowModal();
  224. }
  225. void MyFrame::OnUncenteredToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  226. {
  227. wxDialog dlg;
  228. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("uncentered_dialog"));
  229. dlg.ShowModal();
  230. }
  231. void MyFrame::OnObjRefToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  232. {
  233. // The dialog redirects log messages, so save the old log target first
  234. wxLog* oldlogtarget = wxLog::SetActiveTarget(NULL);
  235. // Make an instance of the dialog
  236. ObjrefDialog* objrefDialog = new ObjrefDialog(this);
  237. // Show the instance of the dialog, modally.
  238. objrefDialog->ShowModal();
  239. objrefDialog->Destroy();
  240. // Restore the old log target
  241. delete wxLog::SetActiveTarget(oldlogtarget);
  242. }
  243. void MyFrame::OnCustomClassToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  244. {
  245. wxDialog dlg;
  246. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("custom_class_dialog"));
  247. // Make an instance of our new custom class.
  248. MyResizableListCtrl* a_myResizableListCtrl = new MyResizableListCtrl(&dlg,
  249. wxID_ANY,
  250. wxDefaultPosition,
  251. wxDefaultSize,
  252. wxLC_REPORT,
  253. wxDefaultValidator);
  254. // "custom_control_placeholder" is the name of the "unknown" tag in the
  255. // custctrl.xrc XRC file.
  256. wxXmlResource::Get()->AttachUnknownControl(wxT("custom_control_placeholder"),
  257. a_myResizableListCtrl);
  258. dlg.ShowModal();
  259. }
  260. void MyFrame::OnPlatformPropertyToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  261. {
  262. wxDialog dlg;
  263. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("platform_property_dialog"));
  264. dlg.ShowModal();
  265. }
  266. void MyFrame::OnArtProviderToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  267. {
  268. wxDialog dlg;
  269. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("art_provider_dialog"));
  270. dlg.ShowModal();
  271. }
  272. void MyFrame::OnVariableExpansionToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  273. {
  274. wxDialog dlg;
  275. wxXmlResource::Get()->LoadDialog(&dlg, this, wxT("variable_expansion_dialog"));
  276. dlg.ShowModal();
  277. }
  278. void MyFrame::OnRecursiveLoad(wxCommandEvent& WXUNUSED(event))
  279. {
  280. // this dialog is created manually to show how you can inject a single
  281. // control from XRC into an existing dialog
  282. //
  283. // this is a slightly contrived example, please keep in mind that it's done
  284. // only to demonstrate LoadObjectRecursively() in action and is not the
  285. // recommended to do this
  286. wxDialog dlg(NULL, wxID_ANY, "Recursive Load Example",
  287. wxDefaultPosition, wxDefaultSize,
  288. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  289. wxSizer * const sizer = new wxBoxSizer(wxVERTICAL);
  290. sizer->Add
  291. (
  292. new wxStaticText
  293. (
  294. &dlg,
  295. wxID_ANY,
  296. "The entire tree book control below is loaded from XRC"
  297. ),
  298. wxSizerFlags().Expand().Border()
  299. );
  300. sizer->Add
  301. (
  302. static_cast<wxWindow *>
  303. (
  304. // notice that controls_treebook is defined inside a notebook page
  305. // inside a dialog defined in controls.xrc and so LoadObject()
  306. // wouldn't find it -- but LoadObjectRecursively() does
  307. wxXmlResource::Get()->
  308. LoadObjectRecursively(&dlg, "controls_treebook", "wxTreebook")
  309. ),
  310. wxSizerFlags(1).Expand().Border()
  311. );
  312. dlg.SetSizer(sizer);
  313. dlg.SetClientSize(400, 200);
  314. dlg.ShowModal();
  315. }
  316. void MyFrame::OnAboutToolOrMenuCommand(wxCommandEvent& WXUNUSED(event))
  317. {
  318. wxString msg;
  319. msg.Printf( wxT("This is the about dialog of XML resources demo.\n")
  320. wxT("Welcome to %s"), wxVERSION_STRING);
  321. wxMessageBox(msg, _("About XML resources demo"), wxOK | wxICON_INFORMATION, this);
  322. }