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