1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: shows and tests wxDC features
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "drawing.cpp"
21 #pragma interface "drawing.cpp"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers
37 #include "wx/colordlg.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
42 // the application icon
43 #if defined(__WXGTK__) || defined(__WXMOTIF__)
44 #include "mondrian.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // Define a new application type, each program should derive a class from wxApp
52 class MyApp
: public wxApp
55 // override base class virtuals
56 // ----------------------------
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();
66 // Define a new frame type: this is going to be our main frame
67 class MyFrame
: public wxFrame
71 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
79 wxColour
SelectColour();
80 void PrepareDC(wxDC
& dc
);
90 wxColour m_colourForeground
, // these are _text_ colours
92 wxBrush m_backgroundBrush
;
96 // any class wishing to process wxWindows events must use this macro
100 // define a scrollable canvas for drawing onto
101 class MyCanvas
: public wxScrolledWindow
104 MyCanvas( MyFrame
*parent
);
106 void DoDrawTests( int x
, int y
, wxDC
&dc
);
107 void OnPaint(wxPaintEvent
&event
);
113 DECLARE_EVENT_TABLE()
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // IDs for the controls and the menu commands
129 MapMode_Text
= MenuOption_First
,
135 UserScale_StretchHoriz
,
136 UserScale_ShrinkHoriz
,
137 UserScale_StretchVertic
,
138 UserScale_ShrinkVertic
,
144 LogicalOrigin_MoveDown
,
145 LogicalOrigin_MoveUp
,
146 LogicalOrigin_MoveLeft
,
147 LogicalOrigin_MoveRight
,
149 Colour_TextForeground
,
150 Colour_TextBackground
,
152 Colour_BackgroundMode
,
154 MenuOption_Last
= Colour_BackgroundMode
157 // ----------------------------------------------------------------------------
158 // event tables and other macros for wxWindows
159 // ----------------------------------------------------------------------------
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
169 // ============================================================================
171 // ============================================================================
173 // ----------------------------------------------------------------------------
174 // the application class
175 // ----------------------------------------------------------------------------
177 // `Main program' equivalent: the program execution "starts" here
180 // Create the main application window
181 MyFrame
*frame
= new MyFrame("Drawing sample",
182 wxPoint(50, 50), wxSize(550, 340));
184 // Show it and tell the application that it's our main window
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.
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
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
)
204 MyCanvas::MyCanvas( MyFrame
*parent
)
205 : wxScrolledWindow( parent
)
210 void MyCanvas::DoDrawTests( int x
, int y
, wxDC
&dc
)
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;
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 );
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 );
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 );
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);
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);
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);
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);
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);
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);
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);
312 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
316 m_owner
->PrepareDC(dc
);
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
);
327 dc
.DrawCircle(0, 0, 10);
328 #ifndef __WXGTK__ // not implemented in wxGTK :-(
329 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
332 dc
.DrawText( "This is text", 110, 10 );
334 dc
.DrawIcon( wxICON(mondrian
), 110, 40 );
336 dc
.SetBrush( *wxRED_BRUSH
);
342 dc
.SetPen( wxPen( "black", 1, 0) );
343 DoDrawTests( x
, y
, dc
);
347 dc
.SetPen( wxPen( "black", 1, wxDOT
) );
348 DoDrawTests( x
, y
, dc
);
352 dc
.SetPen( wxPen( "black", 1, wxSHORT_DASH
) );
353 DoDrawTests( x
, y
, dc
);
357 dc
.SetPen( wxPen( "black", 1, wxLONG_DASH
) );
358 DoDrawTests( x
, y
, dc
);
362 dc
.SetPen( wxPen( "black", 1, wxDOT_DASH
) );
363 DoDrawTests( x
, y
, dc
);
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
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
)
381 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
382 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
384 // set the frame icon
385 SetIcon(wxICON(mondrian
));
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");
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" );
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" );
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
);
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" );
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
);
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");
432 // ... and attach this menu bar to the frame
435 // create a status bar just for fun (by default with 1 pane only)
437 SetStatusText("Welcome to wxWindows!");
439 m_mapMode
= wxMM_TEXT
;
442 m_xLogicalOrigin
= 0;
443 m_yLogicalOrigin
= 0;
445 m_yAxisReversed
= FALSE
;
446 m_backgroundMode
= wxSOLID
;
448 m_canvas
= new MyCanvas( this );
449 m_canvas
->SetScrollbars( 10, 10, 100, 200 );
454 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
456 // TRUE is to force the frame to close
460 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
463 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
464 _T("Copyright (c) Robert Roebling 1999")
467 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
470 void MyFrame::OnOption(wxCommandEvent
&event
)
472 switch (event
.GetInt())
475 m_mapMode
= wxMM_TEXT
;
477 case MapMode_Lometric
:
478 m_mapMode
= wxMM_LOMETRIC
;
481 m_mapMode
= wxMM_TWIPS
;
484 m_mapMode
= wxMM_POINTS
;
487 m_mapMode
= wxMM_METRIC
;
490 case LogicalOrigin_MoveDown
:
491 m_yLogicalOrigin
+= 10;
493 case LogicalOrigin_MoveUp
:
494 m_yLogicalOrigin
-= 10;
496 case LogicalOrigin_MoveLeft
:
497 m_xLogicalOrigin
+= 10;
499 case LogicalOrigin_MoveRight
:
500 m_xLogicalOrigin
-= 10;
503 case UserScale_StretchHoriz
:
504 m_xUserScale
*= 1.10;
506 case UserScale_ShrinkHoriz
:
507 m_xUserScale
/= 1.10;
509 case UserScale_StretchVertic
:
510 m_yUserScale
*= 1.10;
512 case UserScale_ShrinkVertic
:
513 m_yUserScale
/= 1.10;
515 case UserScale_Restore
:
520 case AxisMirror_Vertic
:
521 m_yAxisReversed
= !m_yAxisReversed
;
523 case AxisMirror_Horiz
:
524 m_xAxisReversed
= !m_xAxisReversed
;
527 case Colour_TextForeground
:
528 m_colourForeground
= SelectColour();
530 case Colour_TextBackground
:
531 m_colourBackground
= SelectColour();
533 case Colour_Background
:
535 wxColour col
= SelectColour();
538 m_backgroundBrush
.SetColour(col
);
542 case Colour_BackgroundMode
:
543 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
555 void MyFrame::PrepareDC(wxDC
& dc
)
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
);
563 void MyFrame::OnMouseMove(wxMouseEvent
&event
)
568 wxPoint pos
= event
.GetPosition();
569 long x
= dc
.DeviceToLogicalX( pos
.x
);
570 long y
= dc
.DeviceToLogicalY( pos
.y
);
572 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
573 SetStatusText( str
);
576 wxColour
MyFrame::SelectColour()
580 wxColourDialog
dialog(this, &data
);
582 if ( dialog
.ShowModal() == wxID_OK
)
584 col
= dialog
.GetColourData().GetColour();