]> git.saurik.com Git - wxWidgets.git/blob - samples/drawing/drawing.cpp
Augmented beta to 9,
[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 DrawTestLines( int x, int y, int width, 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::DrawTestLines( int x, int y, int width, wxDC &dc )
211 {
212 dc.SetPen( wxPen( "black", width, wxSOLID) );
213 dc.SetBrush( *wxRED_BRUSH );
214 dc.DrawRectangle( x+10, y+10, 400, 190 );
215
216 dc.SetPen( wxPen( "black", width, wxSOLID) );
217 dc.DrawLine( x+20, y+20, 390, y+20 );
218 dc.SetPen( wxPen( "black", width, wxDOT) );
219 dc.DrawLine( x+20, y+30, 390, y+30 );
220 dc.SetPen( wxPen( "black", width, wxSHORT_DASH) );
221 dc.DrawLine( x+20, y+40, 390, y+40 );
222 dc.SetPen( wxPen( "black", width, wxLONG_DASH) );
223 dc.DrawLine( x+20, y+50, 390, y+50 );
224 dc.SetPen( wxPen( "black", width, wxDOT_DASH) );
225 dc.DrawLine( x+20, y+60, 390, y+60 );
226
227 dc.SetPen( wxPen( "black", width, wxBDIAGONAL_HATCH) );
228 dc.DrawLine( x+20, y+70, 390, y+70 );
229 dc.SetPen( wxPen( "black", width, wxCROSSDIAG_HATCH) );
230 dc.DrawLine( x+20, y+80, 390, y+80 );
231 dc.SetPen( wxPen( "black", width, wxFDIAGONAL_HATCH) );
232 dc.DrawLine( x+20, y+90, 390, y+90 );
233 dc.SetPen( wxPen( "black", width, wxCROSS_HATCH) );
234 dc.DrawLine( x+20, y+100, 390, y+100 );
235 dc.SetPen( wxPen( "black", width, wxHORIZONTAL_HATCH) );
236 dc.DrawLine( x+20, y+110, 390, y+110 );
237 dc.SetPen( wxPen( "black", width, wxVERTICAL_HATCH) );
238 dc.DrawLine( x+20, y+120, 390, y+120 );
239
240 wxPen ud( "black", width, wxUSER_DASH );
241 wxDash dash1[1];
242 dash1[0] = 0;
243 ud.SetDashes( 1, dash1 );
244 dc.DrawLine( x+20, y+140, 390, y+140 );
245 dash1[0] = 1;
246 ud.SetDashes( 1, dash1 );
247 dc.DrawLine( x+20, y+150, 390, y+150 );
248 dash1[0] = 2;
249 ud.SetDashes( 1, dash1 );
250 dc.DrawLine( x+20, y+160, 390, y+160 );
251 dash1[0] = 0xFF;
252 ud.SetDashes( 1, dash1 );
253 dc.DrawLine( x+20, y+170, 390, y+170 );
254
255 }
256
257 void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
258 {
259 wxPaintDC dc(this);
260 PrepareDC(dc);
261 m_owner->PrepareDC(dc);
262
263 dc.SetBackgroundMode( m_owner->m_backgroundMode );
264 if ( m_owner->m_backgroundBrush.Ok() )
265 dc.SetBackground( m_owner->m_backgroundBrush );
266 if ( m_owner->m_colourForeground.Ok() )
267 dc.SetTextForeground( m_owner->m_colourForeground );
268 if ( m_owner->m_colourBackground.Ok() )
269 dc.SetTextBackground( m_owner->m_colourBackground );
270
271 // mark the origin
272 dc.DrawCircle(0, 0, 10);
273 #ifndef __WXGTK__ // not implemented in wxGTK :-(
274 dc.FloodFill(0, 0, wxColour(255, 0, 0));
275 #endif // __WXGTK__
276
277 dc.DrawText( "This is text", 110, 10 );
278
279 dc.SetFont( wxFont( 18, wxSWISS, 0, 0 ) );
280
281 dc.DrawText( "This is Swiss 18pt text.", 110, 40 );
282
283 long length;
284 long height;
285 long descent;
286 dc.GetTextExtent( "This is Swiss 18pt text.", &length, &height, &descent );
287 wxString text;
288 text.Printf( "Dimensions are length %ld, height %ld, descent %ld", length, height, descent );
289 dc.DrawText( text, 110, 80 );
290
291 dc.DrawRectangle( 100, 40, 4, height );
292
293 text.Printf( "CharHeight() returns: %d", dc.GetCharHeight() );
294 dc.DrawText( text, 110, 120 );
295
296
297 dc.DrawIcon( wxICON(mondrian), 310, 40 );
298
299 DrawTestLines( 0, 200, 0, dc );
300
301 DrawTestLines( 0, 400, 1, dc );
302
303 DrawTestLines( 0, 600, 2, dc );
304
305 DrawTestLines( 0, 800, 6, dc );
306
307 }
308
309 // ----------------------------------------------------------------------------
310 // MyFrame
311 // ----------------------------------------------------------------------------
312
313 // the event tables connect the wxWindows events with the functions (event
314 // handlers) which process them. It can be also done at run-time, but for the
315 // simple menu events like this the static method is much simpler.
316 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
317 EVT_MOTION (MyFrame::OnMouseMove)
318 EVT_MENU (Minimal_Quit, MyFrame::OnQuit)
319 EVT_MENU (Minimal_About, MyFrame::OnAbout)
320 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
321 END_EVENT_TABLE()
322
323 // frame constructor
324 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
325 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
326 {
327 // set the frame icon
328 SetIcon(wxICON(mondrian));
329
330 wxMenu *menuFile = new wxMenu;
331 menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
332 menuFile->AppendSeparator();
333 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
334
335 wxMenu *menuMapMode = new wxMenu;
336 menuMapMode->Append( MapMode_Text, "&TEXT map mode" );
337 menuMapMode->Append( MapMode_Lometric, "&LOMETRIC map mode" );
338 menuMapMode->Append( MapMode_Twips, "T&WIPS map mode" );
339 menuMapMode->Append( MapMode_Points, "&POINTS map mode" );
340 menuMapMode->Append( MapMode_Metric, "&METRIC map mode" );
341
342 wxMenu *menuUserScale = new wxMenu;
343 menuUserScale->Append( UserScale_StretchHoriz, "Stretch horizontally\tCtrl-H" );
344 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tCtrl-G" );
345 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-V" );
346 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-W" );
347 menuUserScale->AppendSeparator();
348 menuUserScale->Append( UserScale_Restore, "Restore to normal\tCtrl-0" );
349
350 wxMenu *menuAxis = new wxMenu;
351 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-M", "", TRUE );
352 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-N", "", TRUE );
353
354 wxMenu *menuLogical = new wxMenu;
355 menuLogical->Append( LogicalOrigin_MoveDown, "Move &down\tCtrl-D" );
356 menuLogical->Append( LogicalOrigin_MoveUp, "Move &up\tCtrl-U" );
357 menuLogical->Append( LogicalOrigin_MoveLeft, "Move &right\tCtrl-L" );
358 menuLogical->Append( LogicalOrigin_MoveRight, "Move &left\tCtrl-R" );
359
360 wxMenu *menuColour = new wxMenu;
361 menuColour->Append( Colour_TextForeground, "Text foreground..." );
362 menuColour->Append( Colour_TextBackground, "Text background..." );
363 menuColour->Append( Colour_Background, "Background colour..." );
364 menuColour->Append( Colour_BackgroundMode, "Opaque/transparent\tCtrl-B", "", TRUE );
365
366 // now append the freshly created menu to the menu bar...
367 wxMenuBar *menuBar = new wxMenuBar;
368 menuBar->Append(menuFile, "&File");
369 menuBar->Append(menuMapMode, "&MapMode");
370 menuBar->Append(menuUserScale, "&UserScale");
371 menuBar->Append(menuAxis, "&Axis");
372 menuBar->Append(menuLogical, "&LogicalOrigin");
373 menuBar->Append(menuColour, "&Colours");
374
375 // ... and attach this menu bar to the frame
376 SetMenuBar(menuBar);
377
378 // create a status bar just for fun (by default with 1 pane only)
379 CreateStatusBar(2);
380 SetStatusText("Welcome to wxWindows!");
381
382 m_mapMode = wxMM_TEXT;
383 m_xUserScale = 1.0;
384 m_yUserScale = 1.0;
385 m_xLogicalOrigin = 0;
386 m_yLogicalOrigin = 0;
387 m_xAxisReversed =
388 m_yAxisReversed = FALSE;
389 m_backgroundMode = wxSOLID;
390
391 m_canvas = new MyCanvas( this );
392 m_canvas->SetScrollbars( 10, 10, 100, 200 );
393 }
394
395 // event handlers
396
397 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
398 {
399 // TRUE is to force the frame to close
400 Close(TRUE);
401 }
402
403 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
404 {
405 wxString msg;
406 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
407 _T("Copyright (c) Robert Roebling 1999")
408 );
409
410 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
411 }
412
413 void MyFrame::OnOption(wxCommandEvent &event)
414 {
415 switch (event.GetInt())
416 {
417 case MapMode_Text:
418 m_mapMode = wxMM_TEXT;
419 break;
420 case MapMode_Lometric:
421 m_mapMode = wxMM_LOMETRIC;
422 break;
423 case MapMode_Twips:
424 m_mapMode = wxMM_TWIPS;
425 break;
426 case MapMode_Points:
427 m_mapMode = wxMM_POINTS;
428 break;
429 case MapMode_Metric:
430 m_mapMode = wxMM_METRIC;
431 break;
432
433 case LogicalOrigin_MoveDown:
434 m_yLogicalOrigin += 10;
435 break;
436 case LogicalOrigin_MoveUp:
437 m_yLogicalOrigin -= 10;
438 break;
439 case LogicalOrigin_MoveLeft:
440 m_xLogicalOrigin += 10;
441 break;
442 case LogicalOrigin_MoveRight:
443 m_xLogicalOrigin -= 10;
444 break;
445
446 case UserScale_StretchHoriz:
447 m_xUserScale *= 1.10;
448 break;
449 case UserScale_ShrinkHoriz:
450 m_xUserScale /= 1.10;
451 break;
452 case UserScale_StretchVertic:
453 m_yUserScale *= 1.10;
454 break;
455 case UserScale_ShrinkVertic:
456 m_yUserScale /= 1.10;
457 break;
458 case UserScale_Restore:
459 m_xUserScale =
460 m_yUserScale = 1.0;
461 break;
462
463 case AxisMirror_Vertic:
464 m_yAxisReversed = !m_yAxisReversed;
465 break;
466 case AxisMirror_Horiz:
467 m_xAxisReversed = !m_xAxisReversed;
468 break;
469
470 case Colour_TextForeground:
471 m_colourForeground = SelectColour();
472 break;
473 case Colour_TextBackground:
474 m_colourBackground = SelectColour();
475 break;
476 case Colour_Background:
477 {
478 wxColour col = SelectColour();
479 if ( col.Ok() )
480 {
481 m_backgroundBrush.SetColour(col);
482 }
483 }
484 break;
485 case Colour_BackgroundMode:
486 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
487 : wxSOLID;
488 break;
489
490 default:
491 // skip Refresh()
492 return;
493 }
494
495 m_canvas->Refresh();
496 }
497
498 void MyFrame::PrepareDC(wxDC& dc)
499 {
500 dc.SetMapMode( m_mapMode );
501 dc.SetUserScale( m_xUserScale, m_yUserScale );
502 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
503 dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
504 }
505
506 void MyFrame::OnMouseMove(wxMouseEvent &event)
507 {
508 wxClientDC dc(this);
509 PrepareDC(dc);
510
511 wxPoint pos = event.GetPosition();
512 long x = dc.DeviceToLogicalX( pos.x );
513 long y = dc.DeviceToLogicalY( pos.y );
514 wxString str;
515 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
516 SetStatusText( str );
517 }
518
519 wxColour MyFrame::SelectColour()
520 {
521 wxColour col;
522 wxColourData data;
523 wxColourDialog dialog(this, &data);
524
525 if ( dialog.ShowModal() == wxID_OK )
526 {
527 col = dialog.GetColourData().GetColour();
528 }
529
530 return col;
531 }