]> git.saurik.com Git - wxWidgets.git/blame - samples/minimal/minimal.cpp
fixed Stricmp ifdef to also check for __unix__
[wxWidgets.git] / samples / minimal / minimal.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: minimal.cpp
3// Purpose: Minimal wxWindows sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9fdf3c38 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
9fdf3c38
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
c801d85f 19#ifdef __GNUG__
9fdf3c38
VZ
20 #pragma implementation "minimal.cpp"
21 #pragma interface "minimal.cpp"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
9fdf3c38 28 #pragma hdrstop
c801d85f
KB
29#endif
30
9fdf3c38
VZ
31// for all others, include the necessary headers (this file is usually all you
32// need because it includes almost all "standard" wxWindows headers
c801d85f 33#ifndef WX_PRECOMP
9fdf3c38 34 #include "wx/wx.h"
c801d85f
KB
35#endif
36
9fdf3c38
VZ
37// ----------------------------------------------------------------------------
38// ressources
39// ----------------------------------------------------------------------------
40// the application icon
d355d3fe 41#ifdef __WXGTK__
9fdf3c38 42 #include "mondrian.xpm"
d355d3fe
RR
43#endif
44
9fdf3c38
VZ
45// ----------------------------------------------------------------------------
46// private classes
47// ----------------------------------------------------------------------------
48
49// Define a new application type, each program should derive a class from wxApp
50class MyApp : public wxApp
51{
52public:
53 // override base class virtuals
54 // ----------------------------
55
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();
c801d85f
KB
60};
61
9fdf3c38
VZ
62// Define a new frame type: this is going to be our main frame
63class MyFrame : public wxFrame
64{
65public:
66 // ctor(s)
67 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
68
69 // event handlers (these functions should _not_ be virtual)
c801d85f
KB
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
c801d85f 72
47d67540
VZ
73 void OnPopupMenu(wxCommandEvent& event);
74 void OnRightDown(wxMouseEvent& event);
75
9fdf3c38
VZ
76private:
77 // any class wishing to process wxWindows events must use this macro
78 DECLARE_EVENT_TABLE()
c801d85f
KB
79};
80
9fdf3c38
VZ
81// ----------------------------------------------------------------------------
82// constants
83// ----------------------------------------------------------------------------
c801d85f 84
9fdf3c38
VZ
85// IDs for the controls and the menu commands
86enum
87{
88 // menu items
89 Minimal_Quit = 1,
90 Minimal_About,
47d67540
VZ
91 Minimal_Test1,
92 Minimal_Test2,
9fdf3c38
VZ
93
94 // controls start here (the numbers are, of course, arbitrary)
95 Minimal_Text = 1000,
96};
97
98// ----------------------------------------------------------------------------
99// event tables and other macros for wxWindows
100// ----------------------------------------------------------------------------
101
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.
c801d85f 105BEGIN_EVENT_TABLE(MyFrame, wxFrame)
9fdf3c38
VZ
106 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
107 EVT_MENU(Minimal_About, MyFrame::OnAbout)
47d67540
VZ
108
109 EVT_MENU_RANGE(Minimal_Test1, Minimal_Test2, MyFrame::OnPopupMenu)
110
111 EVT_RIGHT_DOWN(MyFrame::OnRightDown)
c801d85f
KB
112END_EVENT_TABLE()
113
9fdf3c38
VZ
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
118// not wxApp)
119IMPLEMENT_APP(MyApp)
120
121// ============================================================================
122// implementation
123// ============================================================================
c801d85f 124
9fdf3c38
VZ
125// ----------------------------------------------------------------------------
126// the application class
127// ----------------------------------------------------------------------------
128
129// `Main program' equivalent: the program execution "starts" here
130bool MyApp::OnInit()
131{
132 // Create the main application window
133 MyFrame *frame = new MyFrame("Minimal wxWindows App",
134 wxPoint(50, 50), wxSize(450, 340));
135
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?
138 frame->Show(TRUE);
139 SetTopWindow(frame);
140
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.
144 return TRUE;
145}
146
147// ----------------------------------------------------------------------------
148// main frame
149// ----------------------------------------------------------------------------
150
151// frame constructor
152MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
153 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
c801d85f 154{
9fdf3c38
VZ
155 // set the frame icon
156 SetIcon(wxICON(mondrian));
c801d85f 157
9fdf3c38
VZ
158 // create a menu bar
159 wxMenu *menuFile = new wxMenu;
c801d85f 160
9fdf3c38
VZ
161 menuFile->Append(Minimal_About, "&About...");
162 menuFile->AppendSeparator();
163 menuFile->Append(Minimal_Quit, "E&xit");
c801d85f 164
9fdf3c38
VZ
165 // now append the freshly created menu to the menu bar...
166 wxMenuBar *menuBar = new wxMenuBar;
167 menuBar->Append(menuFile, "&File");
c801d85f 168
9fdf3c38
VZ
169 // ... and attach this menu bar to the frame
170 SetMenuBar(menuBar);
c801d85f 171
9fdf3c38 172 // create a status bar just for fun (by default with 1 pane only)
47d67540 173 CreateStatusBar(2);
9fdf3c38 174 SetStatusText("Welcome to wxWindows!");
c801d85f
KB
175}
176
c801d85f 177
9fdf3c38
VZ
178// event handlers
179
180void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 181{
9fdf3c38
VZ
182 // TRUE is to force the frame to close
183 Close(TRUE);
c801d85f
KB
184}
185
9fdf3c38 186void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 187{
9fdf3c38
VZ
188 wxMessageBox("This is a minimal sample\nA second line in the message box",
189 "About Minimal", wxOK | wxICON_INFORMATION, this);
c801d85f 190}
47d67540
VZ
191
192void MyFrame::OnPopupMenu(wxCommandEvent& event)
193{
194 wxString str;
195 str.Printf("Test%d clicked.", event.GetId() == Minimal_Test1 ? 1 : 2);
196 SetStatusText(str, 1);
197}
198
199void MyFrame::OnRightDown(wxMouseEvent& event)
200{
201 class MyMenu : public wxMenu
202 {
203 public:
204 MyMenu()
205 {
206 Append(Minimal_Test1, "Test&1");
207 AppendSeparator();
208 Append(Minimal_Test2, "Test&2");
209 }
210
211 ~MyMenu() { printf("menu destroyed"); }
212 } *menu = new MyMenu;
213
214 PopupMenu(menu, event.GetX(), event.GetY());
215}