PageRenderTime 58ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/ExtLibs/wxWidgets/docs/doxygen/overviews/helloworld.h

https://bitbucket.org/lennonchan/cafu
C Header | 288 lines | 0 code | 2 blank | 286 comment | 0 complexity | 4abcf66d46e8dcf5a6709b9678235a08 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: helloworld.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // RCS-ID: $Id$
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. /**
  9. @page overview_helloworld Hello World Example
  10. This page shows a very simple wxWidgets program that can be used as a skeleton
  11. for your own code. While it does nothing very useful, it introduces a couple of
  12. important concepts and explains how to write a working wxWidgets application.
  13. First, you have to include wxWidgets' header files, of course. This can
  14. be done on a file by file basis (such as @c wx/window.h</tt>) or using one
  15. global include (@c wx/wx.h) which includes most of the commonly needed headers
  16. (although not all of them as there are simply too many wxWidgets headers to
  17. pull in all of them). For the platforms with support for precompiled headers,
  18. as indicated by @c WX_PRECOMP, this global header is already included by @c
  19. wx/wxprec.h so we only include it for the other ones:
  20. @code
  21. // wxWidgets "Hello world" Program
  22. // For compilers that support precompilation, includes "wx/wx.h".
  23. #include <wx/wxprec.h>
  24. #ifndef WX_PRECOMP
  25. #include <wx/wx.h>
  26. #endif
  27. @endcode
  28. Practically every app should define a new class derived from wxApp. By
  29. overriding wxApp's OnInit() virtual method the program can be initialized, e.g.
  30. by creating a new main window.
  31. @code
  32. class MyApp: public wxApp
  33. {
  34. public:
  35. virtual bool OnInit();
  36. };
  37. @endcode
  38. The main window is created by deriving a class from wxFrame and
  39. giving it a menu and a status bar in its constructor. Also, any class
  40. that wishes to respond to any "event" (such as mouse clicks or
  41. messages from the menu or a button) must declare an event table
  42. using the macro below.
  43. Finally, the way to react to such events must be done in "handlers".
  44. In our sample, we react to three menu items, one for our custom menu
  45. command and two for the standard "Exit" and "About" commands (any program
  46. should normally implement the latter two). Notice that these handlers
  47. don't need to be neither virtual nor public.
  48. @code
  49. class MyFrame: public wxFrame
  50. {
  51. public:
  52. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  53. private:
  54. void OnHello(wxCommandEvent& event);
  55. void OnExit(wxCommandEvent& event);
  56. void OnAbout(wxCommandEvent& event);
  57. wxDECLARE_EVENT_TABLE();
  58. };
  59. @endcode
  60. In order to be able to react to a menu command, it must be given a unique
  61. identifier which can be defined as a const variable or an enum element. The
  62. latter is often used because typically many such constants will be needed:
  63. @code
  64. enum
  65. {
  66. ID_Hello = 1
  67. };
  68. @endcode
  69. Notice that you don't need to define identifiers for the "About" and "Exit"
  70. We then proceed to actually implement an event table in which the events
  71. are routed to their respective handler functions in the class MyFrame.
  72. There are predefined macros for routing all common events, ranging from
  73. the selection of a list box entry to a resize event when a user resizes
  74. a window on the screen. If @c wxID_ANY is given as the ID, the given handler will be
  75. invoked for any event of the specified type, so that you could add just
  76. one entry in the event table for all menu commands or all button commands etc.
  77. The origin of the event can still be distinguished in the event handler as
  78. the (only) parameter in an event handler is a reference to a wxEvent object,
  79. which holds various information about the event (such as the ID of and a
  80. pointer to the class, which emitted the event).
  81. @code
  82. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  83. EVT_MENU(ID_Hello, MyFrame::OnHello)
  84. EVT_MENU(wxID_EXIT, MyFrame::OnExit)
  85. EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  86. wxEND_EVENT_TABLE()
  87. @endcode
  88. As in all programs there must be a "main" function. Under wxWidgets main is implemented
  89. using this macro, which creates an application instance and starts the program.
  90. @code
  91. wxIMPLEMENT_APP(MyApp)
  92. @endcode
  93. As mentioned above, wxApp::OnInit() is called upon startup and should be
  94. used to initialize the program, maybe showing a "splash screen" and creating
  95. the main window (or several). The frame should get a title bar text ("Hello World")
  96. and a position and start-up size. One frame can also be declared to be the
  97. top window. Returning @true indicates a successful initialization.
  98. @code
  99. bool MyApp::OnInit()
  100. {
  101. MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
  102. frame->Show( true );
  103. return true;
  104. }
  105. @endcode
  106. In the constructor of the main window (or later on) we create a menu with our menu
  107. items as well as a status bar to be shown at the bottom of the main window. Both have
  108. to be associated with the frame with respective calls.
  109. @code
  110. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  111. : wxFrame(NULL, wxID_ANY, title, pos, size)
  112. {
  113. wxMenu *menuFile = new wxMenu;
  114. menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
  115. "Help string shown in status bar for this menu item");
  116. menuFile->AppendSeparator();
  117. menuFile->Append(wxID_EXIT);
  118. wxMenu *menuHelp = new wxMenu;
  119. menuHelp->Append(wxID_ABOUT);
  120. wxMenuBar *menuBar = new wxMenuBar;
  121. menuBar->Append( menuFile, "&File" );
  122. menuBar->Append( menuHelp, "&Help" );
  123. SetMenuBar( menuBar );
  124. CreateStatusBar();
  125. SetStatusText( "Welcome to wxWidgets!" );
  126. }
  127. @endcode
  128. Notice that we don't need to specify the labels for the standard menu items
  129. @c wxID_ABOUT and @c wxID_EXIT, they will be given standard (even correctly
  130. translated) labels and also standard accelerators correct for the current
  131. platform making your program behaviour more native. For this reason you should
  132. prefer reusing the standard ids (see @ref page_stockitems) if possible.
  133. Here are the standard event handlers implementations. MyFrame::OnExit() closes
  134. the main window by calling Close(). The parameter @true indicates that other
  135. windows have no veto power such as after asking "Do you really want to close?".
  136. If there is no other main window left, the application will quit.
  137. @code
  138. void MyFrame::OnExit(wxCommandEvent& event)
  139. {
  140. Close( true );
  141. }
  142. @endcode
  143. MyFrame::OnAbout() will display a small window with some text in it. In this
  144. case a typical "About" window with information about the program.
  145. @code
  146. void MyFrame::OnAbout(wxCommandEvent& event)
  147. {
  148. wxMessageBox( "This is a wxWidgets' Hello world sample",
  149. "About Hello World", wxOK | wxICON_INFORMATION );
  150. }
  151. @endcode
  152. The implementation of custom menu command handler may perform whatever task
  153. your program needs to do, in this case we will simply show a message from it as
  154. befits a hello world example:
  155. @code
  156. void MyFrame::OnHello(wxCommandEvent& event)
  157. {
  158. wxLogMessage("Hello world from wxWidgets!");
  159. }
  160. @endcode
  161. Here is the entire program that can be copied and pasted:
  162. @code
  163. // wxWidgets "Hello world" Program
  164. // For compilers that support precompilation, includes "wx/wx.h".
  165. #include <wx/wxprec.h>
  166. #ifndef WX_PRECOMP
  167. #include <wx/wx.h>
  168. #endif
  169. class MyApp: public wxApp
  170. {
  171. public:
  172. virtual bool OnInit();
  173. };
  174. class MyFrame: public wxFrame
  175. {
  176. public:
  177. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  178. private:
  179. void OnHello(wxCommandEvent& event);
  180. void OnExit(wxCommandEvent& event);
  181. void OnAbout(wxCommandEvent& event);
  182. wxDECLARE_EVENT_TABLE();
  183. };
  184. enum
  185. {
  186. ID_Hello = 1
  187. };
  188. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  189. EVT_MENU(ID_Hello, MyFrame::OnHello)
  190. EVT_MENU(wxID_EXIT, MyFrame::OnExit)
  191. EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  192. wxEND_EVENT_TABLE()
  193. wxIMPLEMENT_APP(MyApp);
  194. bool MyApp::OnInit()
  195. {
  196. MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
  197. frame->Show( true );
  198. return true;
  199. }
  200. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  201. : wxFrame(NULL, wxID_ANY, title, pos, size)
  202. {
  203. wxMenu *menuFile = new wxMenu;
  204. menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
  205. "Help string shown in status bar for this menu item");
  206. menuFile->AppendSeparator();
  207. menuFile->Append(wxID_EXIT);
  208. wxMenu *menuHelp = new wxMenu;
  209. menuHelp->Append(wxID_ABOUT);
  210. wxMenuBar *menuBar = new wxMenuBar;
  211. menuBar->Append( menuFile, "&File" );
  212. menuBar->Append( menuHelp, "&Help" );
  213. SetMenuBar( menuBar );
  214. CreateStatusBar();
  215. SetStatusText( "Welcome to wxWidgets!" );
  216. }
  217. void MyFrame::OnExit(wxCommandEvent& event)
  218. {
  219. Close( true );
  220. }
  221. void MyFrame::OnAbout(wxCommandEvent& event)
  222. {
  223. wxMessageBox( "This is a wxWidgets' Hello world sample",
  224. "About Hello World", wxOK | wxICON_INFORMATION );
  225. }
  226. void MyFrame::OnHello(wxCommandEvent& event)
  227. {
  228. wxLogMessage("Hello world from wxWidgets!");
  229. }
  230. @endcode
  231. */