1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Minimal wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "minimal.cpp"
21 #pragma interface "minimal.cpp"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
40 // the application icon
42 #include "mondrian.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp
: public wxApp
53 // override base class virtuals
54 // ----------------------------
56 // this one is called on application startup and is a good place for the app
57 // initialization (doing it here and not in the ctor allows to have an error
58 // return: if OnInit() returns false, the application terminates)
59 virtual bool OnInit();
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame
: public wxFrame
67 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent
& event
);
71 void OnAbout(wxCommandEvent
& event
);
73 void OnPopupMenu(wxCommandEvent
& event
);
74 void OnRightDown(wxMouseEvent
& event
);
77 // any class wishing to process wxWindows events must use this macro
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // IDs for the controls and the menu commands
94 // controls start here (the numbers are, of course, arbitrary)
98 // ----------------------------------------------------------------------------
99 // event tables and other macros for wxWindows
100 // ----------------------------------------------------------------------------
102 // the event tables connect the wxWindows events with the functions (event
103 // handlers) which process them. It can be also done at run-time, but for the
104 // simple menu events like this the static method is much simpler.
105 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
106 EVT_MENU(Minimal_Quit
, MyFrame
::OnQuit
)
107 EVT_MENU(Minimal_About
, MyFrame
::OnAbout
)
109 EVT_MENU_RANGE(Minimal_Test1
, Minimal_Test2
, MyFrame
::OnPopupMenu
)
111 EVT_RIGHT_DOWN(MyFrame
::OnRightDown
)
114 // Create a new application object: this macro will allow wxWindows to create
115 // the application object during program execution (it's better than using a
116 // static object for many reasons) and also declares the accessor function
117 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
126 // the application class
127 // ----------------------------------------------------------------------------
129 // `Main program' equivalent: the program execution "starts" here
132 // Create the main application window
133 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
134 wxPoint(50, 50), wxSize(450, 340));
136 // Show it and tell the application that it's our main window
137 // @@@ what does it do exactly, in fact? is it necessary here?
141 // success: wxApp::OnRun() will be called which will enter the main message
142 // loop and the application will run. If we returned FALSE here, the
143 // application would exit immediately.
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
152 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
153 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
155 // set the frame icon
156 SetIcon(wxICON(mondrian
));
159 wxMenu
*menuFile
= new wxMenu
;
161 menuFile
->Append(Minimal_About
, "&About...");
162 menuFile
->AppendSeparator();
163 menuFile
->Append(Minimal_Quit
, "E&xit");
165 // now append the freshly created menu to the menu bar...
166 wxMenuBar
*menuBar
= new wxMenuBar
;
167 menuBar
->Append(menuFile
, "&File");
169 // ... and attach this menu bar to the frame
172 // create a status bar just for fun (by default with 1 pane only)
174 SetStatusText("Welcome to wxWindows!");
180 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
182 // TRUE is to force the frame to close
186 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
188 wxMessageBox("This is a minimal sample\nA second line in the message box",
189 "About Minimal", wxOK
| wxICON_INFORMATION
, this);
192 void MyFrame
::OnPopupMenu(wxCommandEvent
& event
)
195 str
.Printf("Test%d clicked.", event
.GetId() == Minimal_Test1 ?
1 : 2);
196 SetStatusText(str
, 1);
199 void MyFrame
::OnRightDown(wxMouseEvent
& event
)
201 class MyMenu
: public wxMenu
206 Append(Minimal_Test1
, "Test&1");
208 Append(Minimal_Test2
, "Test&2");
211 ~MyMenu() { printf("menu destroyed"); }
212 } *menu
= new MyMenu
;
214 PopupMenu(menu
, event
.GetX(), event
.GetY());