]> git.saurik.com Git - wxWidgets.git/blame - samples/drawing/drawing.cpp
Started drawing sample.
[wxWidgets.git] / samples / drawing / drawing.cpp
CommitLineData
aba99005
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: drawing.cpp
3// Purpose: Minimal wxWindows sample
4// Author: Robert Roebling
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
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
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();
60};
61
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)
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72 void OnPaint(wxPaintEvent &event);
73 void OnOption(wxCommandEvent &event);
74 void OnMouseMove(wxMouseEvent &event);
75
76 int m_mapMode;
77 double m_xUserScale;
78 double m_yUserScale;
79 int m_xLogicalOrigin;
80 int m_yLogicalOrigin;
81
82private:
83 // any class wishing to process wxWindows events must use this macro
84 DECLARE_EVENT_TABLE()
85};
86
87// ----------------------------------------------------------------------------
88// constants
89// ----------------------------------------------------------------------------
90
91// IDs for the controls and the menu commands
92enum
93{
94 // menu items
95 Minimal_Quit = 1,
96 Minimal_About,
97
98 MapMode_Text,
99 MapMode_Lometric,
100 MapMode_Twips,
101 MapMode_Points,
102 MapMode_Metric,
103
104 UserScale_StretchHoriz,
105 UserScale_ShrinkHoriz,
106 UserScale_StretchVertic,
107 UserScale_ShrinkVertic,
108
109 AxisMirror_Horiz,
110 AxisMirror_Vertic,
111
112 LogicalOrigin_MoveDown,
113 LogicalOrigin_MoveUp,
114 LogicalOrigin_MoveLeft,
115 LogicalOrigin_MoveRight,
116
117};
118
119// ----------------------------------------------------------------------------
120// event tables and other macros for wxWindows
121// ----------------------------------------------------------------------------
122
123// the event tables connect the wxWindows events with the functions (event
124// handlers) which process them. It can be also done at run-time, but for the
125// simple menu events like this the static method is much simpler.
126BEGIN_EVENT_TABLE(MyFrame, wxFrame)
127
128 EVT_MOTION (MyFrame::OnMouseMove)
129 EVT_PAINT (MyFrame::OnPaint)
130
131 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
132 EVT_MENU(Minimal_About, MyFrame::OnAbout)
133
134 EVT_MENU(MapMode_Text, MyFrame::OnOption)
135 EVT_MENU(MapMode_Lometric, MyFrame::OnOption)
136 EVT_MENU(MapMode_Twips, MyFrame::OnOption)
137 EVT_MENU(MapMode_Points, MyFrame::OnOption)
138 EVT_MENU(MapMode_Metric, MyFrame::OnOption)
139
140 EVT_MENU(UserScale_StretchHoriz, MyFrame::OnOption)
141 EVT_MENU(UserScale_ShrinkHoriz, MyFrame::OnOption)
142 EVT_MENU(UserScale_StretchVertic, MyFrame::OnOption)
143 EVT_MENU(UserScale_ShrinkVertic, MyFrame::OnOption)
144
145 EVT_MENU(AxisMirror_Horiz, MyFrame::OnOption)
146 EVT_MENU(AxisMirror_Vertic, MyFrame::OnOption)
147
148 EVT_MENU(LogicalOrigin_MoveDown, MyFrame::OnOption)
149 EVT_MENU(LogicalOrigin_MoveUp, MyFrame::OnOption)
150 EVT_MENU(LogicalOrigin_MoveLeft, MyFrame::OnOption)
151 EVT_MENU(LogicalOrigin_MoveRight, MyFrame::OnOption)
152END_EVENT_TABLE()
153
154// Create a new application object: this macro will allow wxWindows to create
155// the application object during program execution (it's better than using a
156// static object for many reasons) and also declares the accessor function
157// wxGetApp() which will return the reference of the right type (i.e. MyApp and
158// not wxApp)
159IMPLEMENT_APP(MyApp)
160
161// ============================================================================
162// implementation
163// ============================================================================
164
165// ----------------------------------------------------------------------------
166// the application class
167// ----------------------------------------------------------------------------
168
169// `Main program' equivalent: the program execution "starts" here
170bool MyApp::OnInit()
171{
172 // Create the main application window
173 MyFrame *frame = new MyFrame("Minimal wxWindows App",
174 wxPoint(50, 50), wxSize(450, 340));
175
176 // Show it and tell the application that it's our main window
177 // @@@ what does it do exactly, in fact? is it necessary here?
178 frame->Show(TRUE);
179 SetTopWindow(frame);
180
181 // success: wxApp::OnRun() will be called which will enter the main message
182 // loop and the application will run. If we returned FALSE here, the
183 // application would exit immediately.
184 return TRUE;
185}
186
187// ----------------------------------------------------------------------------
188// main frame
189// ----------------------------------------------------------------------------
190
191// frame constructor
192MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
193 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
194{
195 // set the frame icon
196 SetIcon(wxICON(mondrian));
197
198 wxMenu *menuFile = new wxMenu;
199 menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
200 menuFile->AppendSeparator();
201 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
202
203 wxMenu *menuMapMode = new wxMenu;
204 menuMapMode->Append( MapMode_Text, "&TEXT map mode" );
205 menuMapMode->Append( MapMode_Lometric, "&LOMETRIC map mode" );
206 menuMapMode->Append( MapMode_Twips, "T&WIPS map mode" );
207 menuMapMode->Append( MapMode_Points, "&POINTS map mode" );
208 menuMapMode->Append( MapMode_Metric, "&METRIC map mode" );
209
210 wxMenu *menuUserScale = new wxMenu;
211 menuUserScale->Append( UserScale_StretchHoriz, "Stretch horizontally\tAlt-P" );
212 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tAlt-M" );
213 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-P" );
214 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-M" );
215
216 wxMenu *menuAxis = new wxMenu;
217 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally" );
218 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically" );
219
220 wxMenu *menuLogical = new wxMenu;
221 menuLogical->Append( LogicalOrigin_MoveDown, "Move &down\tCtrl-D" );
222 menuLogical->Append( LogicalOrigin_MoveUp, "Move &up\tCtrl-U" );
223 menuLogical->Append( LogicalOrigin_MoveLeft, "Move &right\tCtrl-L" );
224 menuLogical->Append( LogicalOrigin_MoveRight, "Move &left\tCtrl-R" );
225
226 // now append the freshly created menu to the menu bar...
227 wxMenuBar *menuBar = new wxMenuBar;
228 menuBar->Append(menuFile, "&File");
229 menuBar->Append(menuMapMode, "&MapMode");
230 menuBar->Append(menuUserScale, "&UserScale");
231 menuBar->Append(menuAxis, "&Axis");
232 menuBar->Append(menuLogical, "&LogicalOrigin");
233
234 // ... and attach this menu bar to the frame
235 SetMenuBar(menuBar);
236
237 // create a status bar just for fun (by default with 1 pane only)
238 CreateStatusBar(2);
239 SetStatusText("Welcome to wxWindows!");
240
241 m_mapMode = wxMM_TEXT;
242 m_xUserScale = 1.0;
243 m_yUserScale = 1.0;
244 m_xLogicalOrigin = 0;
245 m_yLogicalOrigin = 0;
246}
247
248
249// event handlers
250
251void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
252{
253 // TRUE is to force the frame to close
254 Close(TRUE);
255}
256
257void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
258{
259 wxString msg;
260 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
261 _T("Welcome to %s")
262#ifdef wxBETA_NUMBER
263 _T(" (beta %d)!")
264#endif // wxBETA_NUMBER
265 , wxVERSION_STRING
266#ifdef wxBETA_NUMBER
267 , wxBETA_NUMBER
268#endif // wxBETA_NUMBER
269 );
270
271 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
272}
273
274void MyFrame::OnOption(wxCommandEvent &event)
275{
276 switch (event.GetInt())
277 {
278 case MapMode_Text:
279 m_mapMode = wxMM_LOMETRIC;
280 break;
281 case MapMode_Lometric:
282 m_mapMode = wxMM_LOMETRIC;
283 break;
284 case MapMode_Twips:
285 m_mapMode = wxMM_TWIPS;
286 break;
287 case MapMode_Points:
288 m_mapMode = wxMM_POINTS;
289 break;
290 case MapMode_Metric:
291 m_mapMode = wxMM_METRIC;
292 break;
293 }
294 Refresh();
295}
296
297void MyFrame::OnPaint(wxPaintEvent &WXUNUSED(event) )
298{
299 wxPaintDC dc(this);
300 dc.SetMapMode( m_mapMode );
301 dc.SetUserScale( m_xUserScale, m_yUserScale );
302 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
303
304 dc.DrawRectangle( 10, 10, 90, 90 );
305
306 dc.DrawRoundedRectangle( 10, 110, 90, 90, 5 );
307}
308
309void MyFrame::OnMouseMove(wxMouseEvent &event)
310{
311 wxClientDC dc(this);
312 dc.SetMapMode( m_mapMode );
313 dc.SetUserScale( m_xUserScale, m_yUserScale );
314 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
315
316 wxPoint pos = event.GetPosition();
317 long x = dc.DeviceToLogicalX( pos.x );
318 long y = dc.DeviceToLogicalY( pos.y );
319 wxString str;
320 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
321 SetStatusText( str );
322}