mac updates and msw bitmapdrawing on printer
[wxWidgets.git] / samples / minimal / minimal.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "minimal.cpp"
21 #pragma interface "minimal.cpp"
22 #endif
23
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers
33 #ifndef WX_PRECOMP
34 #include "wx/wx.h"
35 #endif
36
37 // ----------------------------------------------------------------------------
38 // ressources
39 // ----------------------------------------------------------------------------
40 // the application icon
41 #if defined(__WXGTK__) || defined(__WXMOTIF__)
42 #include "mondrian.xpm"
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp : public wxApp
51 {
52 public:
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();
60 };
61
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame : public wxFrame
64 {
65 public:
66 // ctor(s)
67 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
68
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72
73 private:
74 // any class wishing to process wxWindows events must use this macro
75 DECLARE_EVENT_TABLE()
76 };
77
78 // ----------------------------------------------------------------------------
79 // constants
80 // ----------------------------------------------------------------------------
81
82 // IDs for the controls and the menu commands
83 enum
84 {
85 // menu items
86 Minimal_Quit = 1,
87 Minimal_About
88 };
89
90 // ----------------------------------------------------------------------------
91 // event tables and other macros for wxWindows
92 // ----------------------------------------------------------------------------
93
94 // the event tables connect the wxWindows events with the functions (event
95 // handlers) which process them. It can be also done at run-time, but for the
96 // simple menu events like this the static method is much simpler.
97 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
98 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
99 EVT_MENU(Minimal_About, MyFrame::OnAbout)
100 END_EVENT_TABLE()
101
102 // Create a new application object: this macro will allow wxWindows to create
103 // the application object during program execution (it's better than using a
104 // static object for many reasons) and also declares the accessor function
105 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
106 // not wxApp)
107 IMPLEMENT_APP(MyApp)
108
109 // ============================================================================
110 // implementation
111 // ============================================================================
112
113 // ----------------------------------------------------------------------------
114 // the application class
115 // ----------------------------------------------------------------------------
116
117 // `Main program' equivalent: the program execution "starts" here
118 bool MyApp::OnInit()
119 {
120 // Create the main application window
121 MyFrame *frame = new MyFrame("Minimal wxWindows App",
122 wxPoint(50, 50), wxSize(450, 340));
123
124 // Show it and tell the application that it's our main window
125 // @@@ what does it do exactly, in fact? is it necessary here?
126 frame->Show(TRUE);
127 SetTopWindow(frame);
128
129 // success: wxApp::OnRun() will be called which will enter the main message
130 // loop and the application will run. If we returned FALSE here, the
131 // application would exit immediately.
132 return TRUE;
133 }
134
135 // ----------------------------------------------------------------------------
136 // main frame
137 // ----------------------------------------------------------------------------
138
139 // frame constructor
140 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
141 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
142 {
143 #ifdef __WXMAC__
144 // we need this in order to allow the about menu relocation, since ABOUT is not the default id of the about menu
145 wxApp::s_macAboutMenuItemId = Minimal_About ;
146 #endif
147
148 // set the frame icon
149 SetIcon(wxICON(mondrian));
150
151 // create a menu bar
152 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
153
154 #ifdef __WXMAC__
155 // since the about should be in the help menu for auto-relocation we have to do a little more...
156 wxMenu *helpMenu = new wxMenu("", wxMENU_TEAROFF);
157 helpMenu->Append(Minimal_About, "&About...\tCntrl+A", "Show about dialog");
158 #else
159 menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
160 menuFile->AppendSeparator();
161 #endif
162
163 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
164 // now append the freshly created menu to the menu bar...
165 wxMenuBar *menuBar = new wxMenuBar();
166 menuBar->Append(menuFile, "&File");
167 #ifdef __WXMAC__
168 menuBar->Append(helpMenu, "&Help");
169 #endif
170
171 // ... and attach this menu bar to the frame
172 SetMenuBar(menuBar);
173
174 #if wxUSE_STATUSBAR
175 // create a status bar just for fun (by default with 1 pane only)
176 CreateStatusBar(2);
177 SetStatusText("Welcome to wxWindows!");
178 #endif // wxUSE_STATUSBAR
179 }
180
181
182 // event handlers
183
184 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
185 {
186 // TRUE is to force the frame to close
187 Close(TRUE);
188 }
189
190 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
191 {
192 wxString msg;
193 msg.Printf( _T("This is the about dialog of minimal sample.\n")
194 _T("Welcome to %s")
195 #ifdef wxBETA_NUMBER
196 _T(" (beta %d)!")
197 #endif // wxBETA_NUMBER
198 , wxVERSION_STRING
199 #ifdef wxBETA_NUMBER
200 , wxBETA_NUMBER
201 #endif // wxBETA_NUMBER
202 );
203
204 wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
205 }