minimal.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: minimal.cpp
  3. // Purpose: Minimal wxWidgets sample
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 04/01/98
  7. // RCS-ID: $Id$
  8. // Copyright: (c) Julian Smart
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. // ============================================================================
  12. // declarations
  13. // ============================================================================
  14. // ----------------------------------------------------------------------------
  15. // headers
  16. // ----------------------------------------------------------------------------
  17. // For compilers that support precompilation, includes "wx/wx.h".
  18. #include "wx/wxprec.h"
  19. #ifdef __BORLANDC__
  20. #pragma hdrstop
  21. #endif
  22. // for all others, include the necessary headers (this file is usually all you
  23. // need because it includes almost all "standard" wxWidgets headers)
  24. #ifndef WX_PRECOMP
  25. #include "wx/wx.h"
  26. #endif
  27. // ----------------------------------------------------------------------------
  28. // resources
  29. // ----------------------------------------------------------------------------
  30. // the application icon (under Windows and OS/2 it is in resources and even
  31. // though we could still include the XPM here it would be unused)
  32. #ifndef wxHAS_IMAGES_IN_RESOURCES
  33. #include "../sample.xpm"
  34. #endif
  35. // ----------------------------------------------------------------------------
  36. // private classes
  37. // ----------------------------------------------------------------------------
  38. // Define a new application type, each program should derive a class from wxApp
  39. class MyApp : public wxApp
  40. {
  41. public:
  42. // override base class virtuals
  43. // ----------------------------
  44. // this one is called on application startup and is a good place for the app
  45. // initialization (doing it here and not in the ctor allows to have an error
  46. // return: if OnInit() returns false, the application terminates)
  47. virtual bool OnInit();
  48. };
  49. // Define a new frame type: this is going to be our main frame
  50. class MyFrame : public wxFrame
  51. {
  52. public:
  53. // ctor(s)
  54. MyFrame(const wxString& title);
  55. // event handlers (these functions should _not_ be virtual)
  56. void OnQuit(wxCommandEvent& event);
  57. void OnAbout(wxCommandEvent& event);
  58. private:
  59. // any class wishing to process wxWidgets events must use this macro
  60. wxDECLARE_EVENT_TABLE();
  61. };
  62. // ----------------------------------------------------------------------------
  63. // constants
  64. // ----------------------------------------------------------------------------
  65. // IDs for the controls and the menu commands
  66. enum
  67. {
  68. // menu items
  69. Minimal_Quit = wxID_EXIT,
  70. // it is important for the id corresponding to the "About" command to have
  71. // this standard value as otherwise it won't be handled properly under Mac
  72. // (where it is special and put into the "Apple" menu)
  73. Minimal_About = wxID_ABOUT
  74. };
  75. // ----------------------------------------------------------------------------
  76. // event tables and other macros for wxWidgets
  77. // ----------------------------------------------------------------------------
  78. // the event tables connect the wxWidgets events with the functions (event
  79. // handlers) which process them. It can be also done at run-time, but for the
  80. // simple menu events like this the static method is much simpler.
  81. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  82. EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
  83. EVT_MENU(Minimal_About, MyFrame::OnAbout)
  84. wxEND_EVENT_TABLE()
  85. // Create a new application object: this macro will allow wxWidgets to create
  86. // the application object during program execution (it's better than using a
  87. // static object for many reasons) and also implements the accessor function
  88. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  89. // not wxApp)
  90. IMPLEMENT_APP(MyApp)
  91. // ============================================================================
  92. // implementation
  93. // ============================================================================
  94. // ----------------------------------------------------------------------------
  95. // the application class
  96. // ----------------------------------------------------------------------------
  97. // 'Main program' equivalent: the program execution "starts" here
  98. bool MyApp::OnInit()
  99. {
  100. // call the base class initialization method, currently it only parses a
  101. // few common command-line options but it could be do more in the future
  102. if ( !wxApp::OnInit() )
  103. return false;
  104. // create the main application window
  105. MyFrame *frame = new MyFrame("Minimal wxWidgets App");
  106. // and show it (the frames, unlike simple controls, are not shown when
  107. // created initially)
  108. frame->Show(true);
  109. // success: wxApp::OnRun() will be called which will enter the main message
  110. // loop and the application will run. If we returned false here, the
  111. // application would exit immediately.
  112. return true;
  113. }
  114. // ----------------------------------------------------------------------------
  115. // main frame
  116. // ----------------------------------------------------------------------------
  117. // frame constructor
  118. MyFrame::MyFrame(const wxString& title)
  119. : wxFrame(NULL, wxID_ANY, title)
  120. {
  121. // set the frame icon
  122. SetIcon(wxICON(sample));
  123. #if wxUSE_MENUS
  124. // create a menu bar
  125. wxMenu *fileMenu = new wxMenu;
  126. // the "About" item should be in the help menu
  127. wxMenu *helpMenu = new wxMenu;
  128. helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
  129. fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
  130. // now append the freshly created menu to the menu bar...
  131. wxMenuBar *menuBar = new wxMenuBar();
  132. menuBar->Append(fileMenu, "&File");
  133. menuBar->Append(helpMenu, "&Help");
  134. // ... and attach this menu bar to the frame
  135. SetMenuBar(menuBar);
  136. #endif // wxUSE_MENUS
  137. #if wxUSE_STATUSBAR
  138. // create a status bar just for fun (by default with 1 pane only)
  139. CreateStatusBar(2);
  140. SetStatusText("Welcome to wxWidgets!");
  141. #endif // wxUSE_STATUSBAR
  142. }
  143. // event handlers
  144. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  145. {
  146. // true is to force the frame to close
  147. Close(true);
  148. }
  149. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  150. {
  151. wxMessageBox(wxString::Format
  152. (
  153. "Welcome to %s!\n"
  154. "\n"
  155. "This is the minimal wxWidgets sample\n"
  156. "running under %s.",
  157. wxVERSION_STRING,
  158. wxGetOsDescription()
  159. ),
  160. "About wxWidgets minimal sample",
  161. wxOK | wxICON_INFORMATION,
  162. this);
  163. }