minimal.cpp 6.0 KB

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