]> git.saurik.com Git - wxWidgets.git/blob - samples/drawing/drawing.cpp
b0d1b3d91ac77814d3036d138a8d4dfe74f9153d
[wxWidgets.git] / samples / drawing / drawing.cpp
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
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 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
82 private:
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
92 enum
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.
126 BEGIN_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)
152 END_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)
159 IMPLEMENT_APP(MyApp)
160
161 // ============================================================================
162 // implementation
163 // ============================================================================
164
165 // ----------------------------------------------------------------------------
166 // the application class
167 // ----------------------------------------------------------------------------
168
169 // `Main program' equivalent: the program execution "starts" here
170 bool MyApp::OnInit()
171 {
172 // Create the main application window
173 MyFrame *frame = new MyFrame("Drawing sample",
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
192 MyFrame::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\tCtrl-H" );
212 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tCtrl-G" );
213 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-V" );
214 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-W" );
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
251 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
252 {
253 // TRUE is to force the frame to close
254 Close(TRUE);
255 }
256
257 void 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("Copyright (c) Robert Roebling 1999")
262 );
263
264 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
265 }
266
267 void MyFrame::OnOption(wxCommandEvent &event)
268 {
269 switch (event.GetInt())
270 {
271 case MapMode_Text:
272 m_mapMode = wxMM_LOMETRIC;
273 break;
274 case MapMode_Lometric:
275 m_mapMode = wxMM_LOMETRIC;
276 break;
277 case MapMode_Twips:
278 m_mapMode = wxMM_TWIPS;
279 break;
280 case MapMode_Points:
281 m_mapMode = wxMM_POINTS;
282 break;
283 case MapMode_Metric:
284 m_mapMode = wxMM_METRIC;
285 break;
286 case LogicalOrigin_MoveDown:
287 m_yLogicalOrigin += 10;
288 break;
289 case LogicalOrigin_MoveUp:
290 m_yLogicalOrigin -= 10;
291 break;
292 case LogicalOrigin_MoveLeft:
293 m_xLogicalOrigin += 10;
294 break;
295 case LogicalOrigin_MoveRight:
296 m_xLogicalOrigin -= 10;
297 break;
298 case UserScale_StretchHoriz:
299 m_xUserScale *= 1.10;
300 break;
301 case UserScale_ShrinkHoriz:
302 m_xUserScale /= 1.10;
303 break;
304 case UserScale_StretchVertic:
305 m_yUserScale *= 1.10;
306 break;
307 case UserScale_ShrinkVertic:
308 m_yUserScale /= 1.10;
309 break;
310 }
311
312 Refresh();
313 }
314
315 void MyFrame::OnPaint(wxPaintEvent &WXUNUSED(event) )
316 {
317 wxPaintDC dc(this);
318 dc.SetMapMode( m_mapMode );
319 dc.SetUserScale( m_xUserScale, m_yUserScale );
320 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
321
322 dc.DrawRectangle( 10, 10, 90, 90 );
323 dc.DrawRoundedRectangle( 10, 110, 90, 90, 5 );
324
325 dc.DrawText( "This is text.", 110, 10 );
326
327 dc.DrawIcon( wxICON(mondrian), 110, 40 );
328 }
329
330 void MyFrame::OnMouseMove(wxMouseEvent &event)
331 {
332 wxClientDC dc(this);
333 dc.SetMapMode( m_mapMode );
334 dc.SetUserScale( m_xUserScale, m_yUserScale );
335 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
336
337 wxPoint pos = event.GetPosition();
338 long x = dc.DeviceToLogicalX( pos.x );
339 long y = dc.DeviceToLogicalY( pos.y );
340 wxString str;
341 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
342 SetStatusText( str );
343 }