]> git.saurik.com Git - wxWidgets.git/blame - samples/minimal/minimal.cpp
don't call wxYield() from EnsureVisible(), this is too dangerous - and unnecessary...
[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// ----------------------------------------------------------------------------
0731c594 19
c801d85f 20#ifdef __GNUG__
71908213
JS
21// #pragma implementation "minimal.cpp"
22// #pragma interface "minimal.cpp"
c801d85f
KB
23#endif
24
25// For compilers that support precompilation, includes "wx/wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
9fdf3c38 29 #pragma hdrstop
c801d85f
KB
30#endif
31
9fdf3c38 32// for all others, include the necessary headers (this file is usually all you
f6bcfd97 33// need because it includes almost all "standard" wxWindows headers)
c801d85f 34#ifndef WX_PRECOMP
9fdf3c38 35 #include "wx/wx.h"
c801d85f
KB
36#endif
37
9fdf3c38 38// ----------------------------------------------------------------------------
f6bcfd97 39// resources
9fdf3c38
VZ
40// ----------------------------------------------------------------------------
41// the application icon
83661a13 42#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
9fdf3c38 43 #include "mondrian.xpm"
d355d3fe
RR
44#endif
45
9fdf3c38
VZ
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
50// Define a new application type, each program should derive a class from wxApp
51class MyApp : public wxApp
52{
53public:
54 // override base class virtuals
55 // ----------------------------
56
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
c801d85f
KB
61};
62
9fdf3c38
VZ
63// Define a new frame type: this is going to be our main frame
64class MyFrame : public wxFrame
65{
66public:
67 // ctor(s)
68 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
69
70 // event handlers (these functions should _not_ be virtual)
c801d85f
KB
71 void OnQuit(wxCommandEvent& event);
72 void OnAbout(wxCommandEvent& event);
c50f1fb9 73
9fdf3c38
VZ
74private:
75 // any class wishing to process wxWindows events must use this macro
76 DECLARE_EVENT_TABLE()
c801d85f
KB
77};
78
9fdf3c38
VZ
79// ----------------------------------------------------------------------------
80// constants
81// ----------------------------------------------------------------------------
c801d85f 82
9fdf3c38
VZ
83// IDs for the controls and the menu commands
84enum
85{
86 // menu items
87 Minimal_Quit = 1,
d6d26e04 88 Minimal_About
9fdf3c38
VZ
89};
90
91// ----------------------------------------------------------------------------
92// event tables and other macros for wxWindows
93// ----------------------------------------------------------------------------
94
95// the event tables connect the wxWindows events with the functions (event
96// handlers) which process them. It can be also done at run-time, but for the
97// simple menu events like this the static method is much simpler.
c801d85f 98BEGIN_EVENT_TABLE(MyFrame, wxFrame)
9fdf3c38
VZ
99 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
100 EVT_MENU(Minimal_About, MyFrame::OnAbout)
c801d85f
KB
101END_EVENT_TABLE()
102
9fdf3c38
VZ
103// Create a new application object: this macro will allow wxWindows to create
104// the application object during program execution (it's better than using a
105// static object for many reasons) and also declares the accessor function
106// wxGetApp() which will return the reference of the right type (i.e. MyApp and
107// not wxApp)
108IMPLEMENT_APP(MyApp)
109
1a77875b
JS
110#ifdef __WXUNIVERSAL__
111 #include "wx/univ/theme.h"
112
113 WX_USE_THEME(win32);
114 WX_USE_THEME(gtk);
115#endif // __WXUNIVERSAL__
116
9fdf3c38
VZ
117// ============================================================================
118// implementation
119// ============================================================================
c801d85f 120
9fdf3c38
VZ
121// ----------------------------------------------------------------------------
122// the application class
123// ----------------------------------------------------------------------------
124
f6bcfd97 125// 'Main program' equivalent: the program execution "starts" here
9fdf3c38
VZ
126bool MyApp::OnInit()
127{
01ca9e8e 128 // create the main application window
9fdf3c38
VZ
129 MyFrame *frame = new MyFrame("Minimal wxWindows App",
130 wxPoint(50, 50), wxSize(450, 340));
131
01ca9e8e
VZ
132 // and show it (the frames, unlike simple controls, are not shown when
133 // created initially)
9fdf3c38 134 frame->Show(TRUE);
9fdf3c38
VZ
135
136 // success: wxApp::OnRun() will be called which will enter the main message
137 // loop and the application will run. If we returned FALSE here, the
138 // application would exit immediately.
139 return TRUE;
140}
141
142// ----------------------------------------------------------------------------
143// main frame
144// ----------------------------------------------------------------------------
145
146// frame constructor
147MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
148 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
c801d85f 149{
8208e181 150#ifdef __WXMAC__
01ca9e8e
VZ
151 // we need this in order to allow the about menu relocation, since ABOUT is
152 // not the default id of the about menu
153 wxApp::s_macAboutMenuItemId = Minimal_About;
8208e181
SC
154#endif
155
9fdf3c38
VZ
156 // set the frame icon
157 SetIcon(wxICON(mondrian));
c801d85f 158
3379ed37 159#if wxUSE_MENUS
d6d26e04
RR
160 // create a menu bar
161 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
c801d85f 162
d6d26e04
RR
163 // the "About" item should be in the help menu
164 wxMenu *helpMenu = new wxMenu;
165 helpMenu->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
c801d85f 166
d6d26e04 167 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
01ca9e8e 168
d6d26e04
RR
169 // now append the freshly created menu to the menu bar...
170 wxMenuBar *menuBar = new wxMenuBar();
171 menuBar->Append(menuFile, "&File");
172 menuBar->Append(helpMenu, "&Help");
c801d85f 173
9fdf3c38 174 // ... and attach this menu bar to the frame
d6d26e04 175 SetMenuBar(menuBar);
3379ed37 176#endif // wxUSE_MENUS
c801d85f 177
c50f1fb9 178#if wxUSE_STATUSBAR
9fdf3c38 179 // create a status bar just for fun (by default with 1 pane only)
47d67540 180 CreateStatusBar(2);
9fdf3c38 181 SetStatusText("Welcome to wxWindows!");
c50f1fb9 182#endif // wxUSE_STATUSBAR
c801d85f
KB
183}
184
c801d85f 185
9fdf3c38
VZ
186// event handlers
187
188void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 189{
9fdf3c38
VZ
190 // TRUE is to force the frame to close
191 Close(TRUE);
c801d85f
KB
192}
193
9fdf3c38 194void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 195{
2f877010 196 wxString msg;
9f06bcb3 197 msg.Printf( _T("This is the about dialog of minimal sample.\n")
01ca9e8e 198 _T("Welcome to %s"), wxVERSION_STRING);
2f877010
VZ
199
200 wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
c801d85f 201}