]> git.saurik.com Git - wxWidgets.git/blob - samples/drawing/drawing.cpp
wxMenu compile fix
[wxWidgets.git] / samples / drawing / drawing.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: drawing.cpp
3 // Purpose: shows and tests wxDC features
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 "drawing.cpp"
21 #pragma interface "drawing.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 #include "wx/colordlg.h"
38
39 // ----------------------------------------------------------------------------
40 // ressources
41 // ----------------------------------------------------------------------------
42 // the application icon
43 #if defined(__WXGTK__) || defined(__WXMOTIF__)
44 #include "mondrian.xpm"
45 #endif
46
47 // ----------------------------------------------------------------------------
48 // private classes
49 // ----------------------------------------------------------------------------
50
51 // Define a new application type, each program should derive a class from wxApp
52 class MyApp : public wxApp
53 {
54 public:
55 // override base class virtuals
56 // ----------------------------
57
58 // this one is called on application startup and is a good place for the app
59 // initialization (doing it here and not in the ctor allows to have an error
60 // return: if OnInit() returns false, the application terminates)
61 virtual bool OnInit();
62 };
63
64 class MyCanvas;
65
66 // Define a new frame type: this is going to be our main frame
67 class MyFrame : public wxFrame
68 {
69 public:
70 // ctor(s)
71 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
72
73 // event handlers (these functions should _not_ be virtual)
74 void OnQuit(wxCommandEvent& event);
75 void OnAbout(wxCommandEvent& event);
76 void OnOption(wxCommandEvent &event);
77 void OnMouseMove(wxMouseEvent &event);
78
79 wxColour SelectColour();
80 void PrepareDC(wxDC& dc);
81
82 int m_backgroundMode;
83 int m_mapMode;
84 double m_xUserScale;
85 double m_yUserScale;
86 int m_xLogicalOrigin;
87 int m_yLogicalOrigin;
88 bool m_xAxisReversed,
89 m_yAxisReversed;
90 wxColour m_colourForeground, // these are _text_ colours
91 m_colourBackground;
92 wxBrush m_backgroundBrush;
93 MyCanvas *m_canvas;
94
95 private:
96 // any class wishing to process wxWindows events must use this macro
97 DECLARE_EVENT_TABLE()
98 };
99
100 // define a scrollable canvas for drawing onto
101 class MyCanvas: public wxScrolledWindow
102 {
103 public:
104 MyCanvas( MyFrame *parent );
105
106 void DoDrawTests( int x, int y, wxDC &dc );
107 void OnPaint(wxPaintEvent &event);
108
109 protected:
110 MyFrame *m_owner;
111
112 private:
113 DECLARE_EVENT_TABLE()
114 };
115
116 // ----------------------------------------------------------------------------
117 // constants
118 // ----------------------------------------------------------------------------
119
120 // IDs for the controls and the menu commands
121 enum
122 {
123 // menu items
124 Minimal_Quit = 1,
125 Minimal_About,
126
127 MenuOption_First,
128
129 MapMode_Text = MenuOption_First,
130 MapMode_Lometric,
131 MapMode_Twips,
132 MapMode_Points,
133 MapMode_Metric,
134
135 UserScale_StretchHoriz,
136 UserScale_ShrinkHoriz,
137 UserScale_StretchVertic,
138 UserScale_ShrinkVertic,
139 UserScale_Restore,
140
141 AxisMirror_Horiz,
142 AxisMirror_Vertic,
143
144 LogicalOrigin_MoveDown,
145 LogicalOrigin_MoveUp,
146 LogicalOrigin_MoveLeft,
147 LogicalOrigin_MoveRight,
148
149 Colour_TextForeground,
150 Colour_TextBackground,
151 Colour_Background,
152 Colour_BackgroundMode,
153
154 MenuOption_Last = Colour_BackgroundMode
155 };
156
157 // ----------------------------------------------------------------------------
158 // event tables and other macros for wxWindows
159 // ----------------------------------------------------------------------------
160
161
162 // Create a new application object: this macro will allow wxWindows to create
163 // the application object during program execution (it's better than using a
164 // static object for many reasons) and also declares the accessor function
165 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
166 // not wxApp)
167 IMPLEMENT_APP(MyApp)
168
169 // ============================================================================
170 // implementation
171 // ============================================================================
172
173 // ----------------------------------------------------------------------------
174 // the application class
175 // ----------------------------------------------------------------------------
176
177 // `Main program' equivalent: the program execution "starts" here
178 bool MyApp::OnInit()
179 {
180 // Create the main application window
181 MyFrame *frame = new MyFrame("Drawing sample",
182 wxPoint(50, 50), wxSize(550, 340));
183
184 // Show it and tell the application that it's our main window
185 frame->Show(TRUE);
186 SetTopWindow(frame);
187
188 // success: wxApp::OnRun() will be called which will enter the main message
189 // loop and the application will run. If we returned FALSE here, the
190 // application would exit immediately.
191 return TRUE;
192 }
193
194 // ----------------------------------------------------------------------------
195 // MyCanvas
196 // ----------------------------------------------------------------------------
197
198 // the event tables connect the wxWindows events with the functions (event
199 // handlers) which process them.
200 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
201 EVT_PAINT (MyCanvas::OnPaint)
202 END_EVENT_TABLE()
203
204 MyCanvas::MyCanvas( MyFrame *parent )
205 : wxScrolledWindow( parent )
206 {
207 m_owner = parent;
208 }
209
210 void MyCanvas::DoDrawTests( int x, int y, wxDC &dc )
211 {
212 wxRect rect;
213 rect.x = dc.LogicalToDeviceX( x-20 );
214 rect.y = dc.LogicalToDeviceY( y-20 );
215 rect.width = dc.LogicalToDeviceXRel( 500 );
216 rect.height = dc.LogicalToDeviceYRel( 200 );
217 if (!IsExposed(rect)) return;
218
219 dc.DrawLine( x , y, x , y );
220 dc.DrawLine( x+ 20, y, x+ 20, y+ 1 );
221 dc.DrawLine( x+ 40, y, x+ 40, y+ 2 );
222 dc.DrawLine( x+ 60, y, x+ 60, y+ 3 );
223 dc.DrawLine( x+ 80, y, x+ 80, y+ 4 );
224 dc.DrawLine( x+100, y, x+100, y+ 5 );
225 dc.DrawLine( x+120, y, x+120, y+ 6 );
226 dc.DrawLine( x+140, y, x+140, y+10 );
227 dc.DrawLine( x+160, y, x+160, y+20 );
228 dc.DrawLine( x+180, y, x+180, y+30 );
229
230 dc.DrawLine( x+200, y, x+200 , y );
231 dc.DrawLine( x+220, y, x+220+ 1, y+ 1 );
232 dc.DrawLine( x+240, y, x+240+ 2, y+ 2 );
233 dc.DrawLine( x+260, y, x+260+ 3, y+ 3 );
234 dc.DrawLine( x+280, y, x+280+ 4, y+ 4 );
235 dc.DrawLine( x+300, y, x+300+ 5, y+ 5 );
236 dc.DrawLine( x+320, y, x+320+ 6, y+ 6 );
237 dc.DrawLine( x+340, y, x+340+10, y+10 );
238 dc.DrawLine( x+360, y, x+360+20, y+20 );
239 dc.DrawLine( x+380, y, x+380+30, y+30 );
240
241 dc.DrawLine( x+420, y , x+420 , y );
242 dc.DrawLine( x+420, y+10, x+420+1, y+10 );
243 dc.DrawLine( x+420, y+20, x+420+2, y+20 );
244 dc.DrawLine( x+420, y+30, x+420+3, y+30 );
245 dc.DrawLine( x+420, y+40, x+420+4, y+40 );
246 dc.DrawLine( x+420, y+50, x+420+5, y+50 );
247 dc.DrawLine( x+420, y+60, x+420+6, y+60 );
248 dc.DrawLine( x+420, y+70, x+420+10, y+70 );
249 dc.DrawLine( x+420, y+80, x+420+20, y+80 );
250 dc.DrawLine( x+420, y+90, x+420+30, y+90 );
251
252 y -= 40;
253
254 dc.DrawCircle( x, y+100, 1);
255 dc.DrawCircle( x, y+110, 2);
256 dc.DrawCircle( x, y+120, 3);
257 dc.DrawCircle( x, y+130, 4);
258 dc.DrawCircle( x, y+140, 5);
259 dc.DrawCircle( x, y+160, 7);
260 dc.DrawCircle( x, y+180, 8);
261
262 dc.DrawRectangle( x+50, y+100, 1, 1);
263 dc.DrawRectangle( x+50, y+110, 2, 2);
264 dc.DrawRectangle( x+50, y+120, 3, 3);
265 dc.DrawRectangle( x+50, y+130, 4, 4);
266 dc.DrawRectangle( x+50, y+140, 5, 5);
267 dc.DrawRectangle( x+50, y+160, 10, 10);
268 dc.DrawRectangle( x+50, y+180, 20, 20);
269
270 dc.DrawRoundedRectangle( x+100, y+100, 1, 1, 1);
271 dc.DrawRoundedRectangle( x+100, y+110, 2, 2, 1);
272 dc.DrawRoundedRectangle( x+100, y+120, 3, 3, 1);
273 dc.DrawRoundedRectangle( x+100, y+130, 4, 4, 1);
274 dc.DrawRoundedRectangle( x+100, y+140, 5, 5, 1);
275 dc.DrawRoundedRectangle( x+100, y+160, 10, 10, 1);
276 dc.DrawRoundedRectangle( x+100, y+180, 20, 20, 1);
277
278 dc.DrawRoundedRectangle( x+150, y+100, 1, 1, 2);
279 dc.DrawRoundedRectangle( x+150, y+110, 2, 2, 2);
280 dc.DrawRoundedRectangle( x+150, y+120, 3, 3, 2);
281 dc.DrawRoundedRectangle( x+150, y+130, 4, 4, 2);
282 dc.DrawRoundedRectangle( x+150, y+140, 5, 5, 2);
283 dc.DrawRoundedRectangle( x+150, y+160, 10, 10, 2);
284 dc.DrawRoundedRectangle( x+150, y+180, 20, 20, 2);
285
286 dc.DrawRoundedRectangle( x+200, y+100, 1, 1, 3);
287 dc.DrawRoundedRectangle( x+200, y+110, 2, 2, 3);
288 dc.DrawRoundedRectangle( x+200, y+120, 3, 3, 3);
289 dc.DrawRoundedRectangle( x+200, y+130, 4, 4, 3);
290 dc.DrawRoundedRectangle( x+200, y+140, 5, 5, 3);
291 dc.DrawRoundedRectangle( x+200, y+160, 10, 10, 3);
292 dc.DrawRoundedRectangle( x+200, y+180, 20, 20, 3);
293
294 dc.DrawRoundedRectangle( x+250, y+100, 1, 1, 5);
295 dc.DrawRoundedRectangle( x+250, y+110, 2, 2, 5);
296 dc.DrawRoundedRectangle( x+250, y+120, 3, 3, 5);
297 dc.DrawRoundedRectangle( x+250, y+130, 4, 4, 5);
298 dc.DrawRoundedRectangle( x+250, y+140, 5, 5, 5);
299 dc.DrawRoundedRectangle( x+250, y+160, 10, 10, 5);
300 dc.DrawRoundedRectangle( x+250, y+180, 20, 20, 5);
301
302 dc.DrawRoundedRectangle( x+300, y+100, 1, 1, 10);
303 dc.DrawRoundedRectangle( x+300, y+110, 2, 2, 10);
304 dc.DrawRoundedRectangle( x+300, y+120, 3, 3, 10);
305 dc.DrawRoundedRectangle( x+300, y+130, 4, 4, 10);
306 dc.DrawRoundedRectangle( x+300, y+140, 5, 5, 10);
307 dc.DrawRoundedRectangle( x+300, y+160, 10, 10, 10);
308 dc.DrawRoundedRectangle( x+300, y+180, 20, 20, 10);
309
310 }
311
312 void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
313 {
314 wxPaintDC dc(this);
315 PrepareDC(dc);
316 m_owner->PrepareDC(dc);
317
318 dc.SetBackgroundMode( m_owner->m_backgroundMode );
319 if ( m_owner->m_backgroundBrush.Ok() )
320 dc.SetBackground( m_owner->m_backgroundBrush );
321 if ( m_owner->m_colourForeground.Ok() )
322 dc.SetTextForeground( m_owner->m_colourForeground );
323 if ( m_owner->m_colourBackground.Ok() )
324 dc.SetTextBackground( m_owner->m_colourBackground );
325
326 // mark the origin
327 dc.DrawCircle(0, 0, 10);
328 #ifndef __WXGTK__ // not implemented in wxGTK :-(
329 dc.FloodFill(0, 0, wxColour(255, 0, 0));
330 #endif // __WXGTK__
331
332 dc.DrawText( "This is text", 110, 10 );
333
334 dc.DrawIcon( wxICON(mondrian), 110, 40 );
335
336 dc.SetBrush( *wxRED_BRUSH );
337
338 int x = 20;
339 int y = 80;
340 int step = 200;
341
342 dc.SetPen( wxPen( "black", 1, 0) );
343 DoDrawTests( x, y, dc );
344
345 y += step;
346
347 dc.SetPen( wxPen( "black", 1, wxDOT) );
348 DoDrawTests( x, y, dc );
349
350 y += step;
351
352 dc.SetPen( wxPen( "black", 1, wxSHORT_DASH) );
353 DoDrawTests( x, y, dc );
354
355 y += step;
356
357 dc.SetPen( wxPen( "black", 1, wxLONG_DASH) );
358 DoDrawTests( x, y, dc );
359
360 y += step;
361
362 dc.SetPen( wxPen( "black", 1, wxDOT_DASH) );
363 DoDrawTests( x, y, dc );
364 }
365
366 // ----------------------------------------------------------------------------
367 // MyFrame
368 // ----------------------------------------------------------------------------
369
370 // the event tables connect the wxWindows events with the functions (event
371 // handlers) which process them. It can be also done at run-time, but for the
372 // simple menu events like this the static method is much simpler.
373 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
374 EVT_MOTION (MyFrame::OnMouseMove)
375 EVT_MENU (Minimal_Quit, MyFrame::OnQuit)
376 EVT_MENU (Minimal_About, MyFrame::OnAbout)
377 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
378 END_EVENT_TABLE()
379
380 // frame constructor
381 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
382 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
383 {
384 // set the frame icon
385 SetIcon(wxICON(mondrian));
386
387 wxMenu *menuFile = new wxMenu;
388 menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
389 menuFile->AppendSeparator();
390 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
391
392 wxMenu *menuMapMode = new wxMenu;
393 menuMapMode->Append( MapMode_Text, "&TEXT map mode" );
394 menuMapMode->Append( MapMode_Lometric, "&LOMETRIC map mode" );
395 menuMapMode->Append( MapMode_Twips, "T&WIPS map mode" );
396 menuMapMode->Append( MapMode_Points, "&POINTS map mode" );
397 menuMapMode->Append( MapMode_Metric, "&METRIC map mode" );
398
399 wxMenu *menuUserScale = new wxMenu;
400 menuUserScale->Append( UserScale_StretchHoriz, "Stretch horizontally\tCtrl-H" );
401 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tCtrl-G" );
402 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-V" );
403 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-W" );
404 menuUserScale->AppendSeparator();
405 menuUserScale->Append( UserScale_Restore, "Restore to normal\tCtrl-0" );
406
407 wxMenu *menuAxis = new wxMenu;
408 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-M", "", TRUE );
409 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-N", "", TRUE );
410
411 wxMenu *menuLogical = new wxMenu;
412 menuLogical->Append( LogicalOrigin_MoveDown, "Move &down\tCtrl-D" );
413 menuLogical->Append( LogicalOrigin_MoveUp, "Move &up\tCtrl-U" );
414 menuLogical->Append( LogicalOrigin_MoveLeft, "Move &right\tCtrl-L" );
415 menuLogical->Append( LogicalOrigin_MoveRight, "Move &left\tCtrl-R" );
416
417 wxMenu *menuColour = new wxMenu;
418 menuColour->Append( Colour_TextForeground, "Text foreground..." );
419 menuColour->Append( Colour_TextBackground, "Text background..." );
420 menuColour->Append( Colour_Background, "Background colour..." );
421 menuColour->Append( Colour_BackgroundMode, "Opaque/transparent\tCtrl-B", "", TRUE );
422
423 // now append the freshly created menu to the menu bar...
424 wxMenuBar *menuBar = new wxMenuBar;
425 menuBar->Append(menuFile, "&File");
426 menuBar->Append(menuMapMode, "&MapMode");
427 menuBar->Append(menuUserScale, "&UserScale");
428 menuBar->Append(menuAxis, "&Axis");
429 menuBar->Append(menuLogical, "&LogicalOrigin");
430 menuBar->Append(menuColour, "&Colours");
431
432 // ... and attach this menu bar to the frame
433 SetMenuBar(menuBar);
434
435 // create a status bar just for fun (by default with 1 pane only)
436 CreateStatusBar(2);
437 SetStatusText("Welcome to wxWindows!");
438
439 m_mapMode = wxMM_TEXT;
440 m_xUserScale = 1.0;
441 m_yUserScale = 1.0;
442 m_xLogicalOrigin = 0;
443 m_yLogicalOrigin = 0;
444 m_xAxisReversed =
445 m_yAxisReversed = FALSE;
446 m_backgroundMode = wxSOLID;
447
448 m_canvas = new MyCanvas( this );
449 m_canvas->SetScrollbars( 10, 10, 100, 200 );
450 }
451
452 // event handlers
453
454 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
455 {
456 // TRUE is to force the frame to close
457 Close(TRUE);
458 }
459
460 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
461 {
462 wxString msg;
463 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
464 _T("Copyright (c) Robert Roebling 1999")
465 );
466
467 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
468 }
469
470 void MyFrame::OnOption(wxCommandEvent &event)
471 {
472 switch (event.GetInt())
473 {
474 case MapMode_Text:
475 m_mapMode = wxMM_TEXT;
476 break;
477 case MapMode_Lometric:
478 m_mapMode = wxMM_LOMETRIC;
479 break;
480 case MapMode_Twips:
481 m_mapMode = wxMM_TWIPS;
482 break;
483 case MapMode_Points:
484 m_mapMode = wxMM_POINTS;
485 break;
486 case MapMode_Metric:
487 m_mapMode = wxMM_METRIC;
488 break;
489
490 case LogicalOrigin_MoveDown:
491 m_yLogicalOrigin += 10;
492 break;
493 case LogicalOrigin_MoveUp:
494 m_yLogicalOrigin -= 10;
495 break;
496 case LogicalOrigin_MoveLeft:
497 m_xLogicalOrigin += 10;
498 break;
499 case LogicalOrigin_MoveRight:
500 m_xLogicalOrigin -= 10;
501 break;
502
503 case UserScale_StretchHoriz:
504 m_xUserScale *= 1.10;
505 break;
506 case UserScale_ShrinkHoriz:
507 m_xUserScale /= 1.10;
508 break;
509 case UserScale_StretchVertic:
510 m_yUserScale *= 1.10;
511 break;
512 case UserScale_ShrinkVertic:
513 m_yUserScale /= 1.10;
514 break;
515 case UserScale_Restore:
516 m_xUserScale =
517 m_yUserScale = 1.0;
518 break;
519
520 case AxisMirror_Vertic:
521 m_yAxisReversed = !m_yAxisReversed;
522 break;
523 case AxisMirror_Horiz:
524 m_xAxisReversed = !m_xAxisReversed;
525 break;
526
527 case Colour_TextForeground:
528 m_colourForeground = SelectColour();
529 break;
530 case Colour_TextBackground:
531 m_colourBackground = SelectColour();
532 break;
533 case Colour_Background:
534 {
535 wxColour col = SelectColour();
536 if ( col.Ok() )
537 {
538 m_backgroundBrush.SetColour(col);
539 }
540 }
541 break;
542 case Colour_BackgroundMode:
543 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
544 : wxSOLID;
545 break;
546
547 default:
548 // skip Refresh()
549 return;
550 }
551
552 Refresh();
553 }
554
555 void MyFrame::PrepareDC(wxDC& dc)
556 {
557 dc.SetMapMode( m_mapMode );
558 dc.SetUserScale( m_xUserScale, m_yUserScale );
559 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
560 dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
561 }
562
563 void MyFrame::OnMouseMove(wxMouseEvent &event)
564 {
565 wxClientDC dc(this);
566 PrepareDC(dc);
567
568 wxPoint pos = event.GetPosition();
569 long x = dc.DeviceToLogicalX( pos.x );
570 long y = dc.DeviceToLogicalY( pos.y );
571 wxString str;
572 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
573 SetStatusText( str );
574 }
575
576 wxColour MyFrame::SelectColour()
577 {
578 wxColour col;
579 wxColourData data;
580 wxColourDialog dialog(this, &data);
581
582 if ( dialog.ShowModal() == wxID_OK )
583 {
584 col = dialog.GetColourData().GetColour();
585 }
586
587 return col;
588 }