]> git.saurik.com Git - wxWidgets.git/blame - samples/minimal/minimal.cpp
wxFileSelector should have file name only, not path, passed to 3rd arg
[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$
e2a6f233
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
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 31// for all others, include the necessary headers (this file is usually all you
f6bcfd97 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 37// ----------------------------------------------------------------------------
f6bcfd97 38// resources
9fdf3c38
VZ
39// ----------------------------------------------------------------------------
40// the application icon
3dd4e4e0 41#if defined(__WXGTK__) || defined(__WXMOTIF__)
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)
d6c9c1b7
VZ
70 void OnPaint(wxPaintEvent& event)
71 {
72 wxPaintDC dc(this);
73 dc.DrawRectangle(20, 20, 100, 100);
74 dc.SetPen(*wxRED_PEN);
75 dc.SetDeviceOrigin(20, 20);
76 dc.SetClippingRegion(0, 0, 100, 100);
77 dc.DrawLine(0, 0, 1000, 1000);
78 }
79
c801d85f
KB
80 void OnQuit(wxCommandEvent& event);
81 void OnAbout(wxCommandEvent& event);
c50f1fb9 82
9fdf3c38
VZ
83private:
84 // any class wishing to process wxWindows events must use this macro
85 DECLARE_EVENT_TABLE()
c801d85f
KB
86};
87
9fdf3c38
VZ
88// ----------------------------------------------------------------------------
89// constants
90// ----------------------------------------------------------------------------
c801d85f 91
9fdf3c38
VZ
92// IDs for the controls and the menu commands
93enum
94{
95 // menu items
96 Minimal_Quit = 1,
d6d26e04 97 Minimal_About
9fdf3c38
VZ
98};
99
100// ----------------------------------------------------------------------------
101// event tables and other macros for wxWindows
102// ----------------------------------------------------------------------------
103
104// the event tables connect the wxWindows events with the functions (event
105// handlers) which process them. It can be also done at run-time, but for the
106// simple menu events like this the static method is much simpler.
c801d85f 107BEGIN_EVENT_TABLE(MyFrame, wxFrame)
d6c9c1b7 108 EVT_PAINT(MyFrame::OnPaint)
9fdf3c38
VZ
109 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
110 EVT_MENU(Minimal_About, MyFrame::OnAbout)
c801d85f
KB
111END_EVENT_TABLE()
112
9fdf3c38
VZ
113// Create a new application object: this macro will allow wxWindows to create
114// the application object during program execution (it's better than using a
115// static object for many reasons) and also declares the accessor function
116// wxGetApp() which will return the reference of the right type (i.e. MyApp and
117// not wxApp)
118IMPLEMENT_APP(MyApp)
119
120// ============================================================================
121// implementation
122// ============================================================================
c801d85f 123
9fdf3c38
VZ
124// ----------------------------------------------------------------------------
125// the application class
126// ----------------------------------------------------------------------------
127
f6bcfd97 128// 'Main program' equivalent: the program execution "starts" here
9fdf3c38
VZ
129bool MyApp::OnInit()
130{
01ca9e8e 131 // create the main application window
9fdf3c38
VZ
132 MyFrame *frame = new MyFrame("Minimal wxWindows App",
133 wxPoint(50, 50), wxSize(450, 340));
134
01ca9e8e
VZ
135 // and show it (the frames, unlike simple controls, are not shown when
136 // created initially)
9fdf3c38 137 frame->Show(TRUE);
9fdf3c38
VZ
138
139 // success: wxApp::OnRun() will be called which will enter the main message
140 // loop and the application will run. If we returned FALSE here, the
141 // application would exit immediately.
142 return TRUE;
143}
144
145// ----------------------------------------------------------------------------
146// main frame
147// ----------------------------------------------------------------------------
148
149// frame constructor
150MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
151 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
c801d85f 152{
8208e181 153#ifdef __WXMAC__
01ca9e8e
VZ
154 // we need this in order to allow the about menu relocation, since ABOUT is
155 // not the default id of the about menu
156 wxApp::s_macAboutMenuItemId = Minimal_About;
8208e181
SC
157#endif
158
9fdf3c38
VZ
159 // set the frame icon
160 SetIcon(wxICON(mondrian));
c801d85f 161
d6d26e04
RR
162 // create a menu bar
163 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
c801d85f 164
d6d26e04
RR
165 // the "About" item should be in the help menu
166 wxMenu *helpMenu = new wxMenu;
167 helpMenu->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
c801d85f 168
d6d26e04 169 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
01ca9e8e 170
d6d26e04
RR
171 // now append the freshly created menu to the menu bar...
172 wxMenuBar *menuBar = new wxMenuBar();
173 menuBar->Append(menuFile, "&File");
174 menuBar->Append(helpMenu, "&Help");
c801d85f 175
9fdf3c38 176 // ... and attach this menu bar to the frame
d6d26e04 177 SetMenuBar(menuBar);
c801d85f 178
c50f1fb9 179#if wxUSE_STATUSBAR
9fdf3c38 180 // create a status bar just for fun (by default with 1 pane only)
47d67540 181 CreateStatusBar(2);
9fdf3c38 182 SetStatusText("Welcome to wxWindows!");
c50f1fb9 183#endif // wxUSE_STATUSBAR
c801d85f
KB
184}
185
c801d85f 186
9fdf3c38
VZ
187// event handlers
188
189void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 190{
9fdf3c38
VZ
191 // TRUE is to force the frame to close
192 Close(TRUE);
c801d85f
KB
193}
194
9fdf3c38 195void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 196{
2f877010 197 wxString msg;
9f06bcb3 198 msg.Printf( _T("This is the about dialog of minimal sample.\n")
01ca9e8e 199 _T("Welcome to %s"), wxVERSION_STRING);
2f877010
VZ
200
201 wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
c801d85f 202}