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