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
);
78 wxColour
SelectColour();
79 void PrepareDC(wxDC
& dc
);
89 wxColour m_colourForeground
, // these are _text_ colours
91 wxBrush m_backgroundBrush
;
95 // any class wishing to process wxWindows events must use this macro
99 // define a scrollable canvas for drawing onto
100 class MyCanvas
: public wxScrolledWindow
103 MyCanvas( MyFrame
*parent
);
105 void DrawTestLines( int x
, int y
, int width
, wxDC
&dc
);
106 void OnPaint(wxPaintEvent
&event
);
107 void OnMouseMove(wxMouseEvent
&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
)
202 EVT_MOTION (MyCanvas::OnMouseMove
)
205 MyCanvas::MyCanvas( MyFrame
*parent
)
206 : wxScrolledWindow( parent
)
211 void MyCanvas::DrawTestLines( int x
, int y
, int width
, wxDC
&dc
)
213 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
214 dc
.SetBrush( *wxRED_BRUSH
);
215 dc
.DrawRectangle( x
+10, y
+10, 110, 190 );
217 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
218 dc
.DrawLine( x
+20, y
+20, 100, y
+20 );
219 dc
.SetPen( wxPen( "black", width
, wxDOT
) );
220 dc
.DrawLine( x
+20, y
+30, 100, y
+30 );
221 dc
.SetPen( wxPen( "black", width
, wxSHORT_DASH
) );
222 dc
.DrawLine( x
+20, y
+40, 100, y
+40 );
223 dc
.SetPen( wxPen( "black", width
, wxLONG_DASH
) );
224 dc
.DrawLine( x
+20, y
+50, 100, y
+50 );
225 dc
.SetPen( wxPen( "black", width
, wxDOT_DASH
) );
226 dc
.DrawLine( x
+20, y
+60, 100, y
+60 );
228 dc
.SetPen( wxPen( "black", width
, wxBDIAGONAL_HATCH
) );
229 dc
.DrawLine( x
+20, y
+70, 100, y
+70 );
230 dc
.SetPen( wxPen( "black", width
, wxCROSSDIAG_HATCH
) );
231 dc
.DrawLine( x
+20, y
+80, 100, y
+80 );
232 dc
.SetPen( wxPen( "black", width
, wxFDIAGONAL_HATCH
) );
233 dc
.DrawLine( x
+20, y
+90, 100, y
+90 );
234 dc
.SetPen( wxPen( "black", width
, wxCROSS_HATCH
) );
235 dc
.DrawLine( x
+20, y
+100, 100, y
+100 );
236 dc
.SetPen( wxPen( "black", width
, wxHORIZONTAL_HATCH
) );
237 dc
.DrawLine( x
+20, y
+110, 100, y
+110 );
238 dc
.SetPen( wxPen( "black", width
, wxVERTICAL_HATCH
) );
239 dc
.DrawLine( x
+20, y
+120, 100, y
+120 );
241 wxPen
ud( "black", width
, wxUSER_DASH
);
244 ud
.SetDashes( 1, dash1
);
245 dc
.DrawLine( x
+20, y
+140, 100, y
+140 );
247 ud
.SetDashes( 1, dash1
);
248 dc
.DrawLine( x
+20, y
+150, 100, y
+150 );
250 ud
.SetDashes( 1, dash1
);
251 dc
.DrawLine( x
+20, y
+160, 100, y
+160 );
253 ud
.SetDashes( 1, dash1
);
254 dc
.DrawLine( x
+20, y
+170, 100, y
+170 );
258 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
262 m_owner
->PrepareDC(dc
);
264 dc
.SetBackgroundMode( m_owner
->m_backgroundMode
);
265 if ( m_owner
->m_backgroundBrush
.Ok() )
266 dc
.SetBackground( m_owner
->m_backgroundBrush
);
267 if ( m_owner
->m_colourForeground
.Ok() )
268 dc
.SetTextForeground( m_owner
->m_colourForeground
);
269 if ( m_owner
->m_colourBackground
.Ok() )
270 dc
.SetTextBackground( m_owner
->m_colourBackground
);
273 dc
.DrawCircle(0, 0, 10);
274 #if !(defined __WXGTK__) && !(defined __WXMOTIF__)
275 // not implemented in wxGTK or wxMOTIF :-(
276 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
279 dc
.DrawText( "This is text", 110, 10 );
282 dc
. SetBackgroundMode(wxSOLID
);
284 for ( int n
= -180; n
< 180; n
+= 30 )
286 text
.Printf(" %d rotated text", n
);
287 dc
.DrawRotatedText(text
, 400, 400, n
);
290 dc
.SetFont( wxFont( 18, wxSWISS
, wxNORMAL
, wxNORMAL
) );
292 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
297 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
298 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
299 dc
.DrawText( text
, 110, 80 );
301 dc
.DrawRectangle( 100, 40, 4, height
);
303 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
304 dc
.DrawText( text
, 110, 120 );
307 dc
.DrawIcon( wxICON(mondrian
), 310, 40 );
309 DrawTestLines( 0, 200, 0, dc
);
311 DrawTestLines( 0, 400, 1, dc
);
313 DrawTestLines( 0, 600, 2, dc
);
315 DrawTestLines( 0, 800, 6, dc
);
319 void MyCanvas::OnMouseMove(wxMouseEvent
&event
)
323 m_owner
->PrepareDC(dc
);
325 wxPoint pos
= event
.GetPosition();
326 long x
= dc
.DeviceToLogicalX( pos
.x
);
327 long y
= dc
.DeviceToLogicalY( pos
.y
);
329 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
330 m_owner
->SetStatusText( str
);
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 // the event tables connect the wxWindows events with the functions (event
338 // handlers) which process them. It can be also done at run-time, but for the
339 // simple menu events like this the static method is much simpler.
340 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
341 EVT_MENU (Minimal_Quit
, MyFrame::OnQuit
)
342 EVT_MENU (Minimal_About
, MyFrame::OnAbout
)
343 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
347 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
348 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
350 // set the frame icon
351 SetIcon(wxICON(mondrian
));
353 wxMenu
*menuFile
= new wxMenu
;
354 menuFile
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
355 menuFile
->AppendSeparator();
356 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
358 wxMenu
*menuMapMode
= new wxMenu
;
359 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
360 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
361 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
362 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
363 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
365 wxMenu
*menuUserScale
= new wxMenu
;
366 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
367 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
368 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
369 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
370 menuUserScale
->AppendSeparator();
371 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
373 wxMenu
*menuAxis
= new wxMenu
;
374 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-M", "", TRUE
);
375 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-N", "", TRUE
);
377 wxMenu
*menuLogical
= new wxMenu
;
378 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
379 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
380 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
381 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
383 wxMenu
*menuColour
= new wxMenu
;
384 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
385 menuColour
->Append( Colour_TextBackground
, "Text background..." );
386 menuColour
->Append( Colour_Background
, "Background colour..." );
387 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
389 // now append the freshly created menu to the menu bar...
390 wxMenuBar
*menuBar
= new wxMenuBar
;
391 menuBar
->Append(menuFile
, "&File");
392 menuBar
->Append(menuMapMode
, "&MapMode");
393 menuBar
->Append(menuUserScale
, "&UserScale");
394 menuBar
->Append(menuAxis
, "&Axis");
395 menuBar
->Append(menuLogical
, "&LogicalOrigin");
396 menuBar
->Append(menuColour
, "&Colours");
398 // ... and attach this menu bar to the frame
401 // create a status bar just for fun (by default with 1 pane only)
403 SetStatusText("Welcome to wxWindows!");
405 m_mapMode
= wxMM_TEXT
;
408 m_xLogicalOrigin
= 0;
409 m_yLogicalOrigin
= 0;
411 m_yAxisReversed
= FALSE
;
412 m_backgroundMode
= wxSOLID
;
414 m_canvas
= new MyCanvas( this );
415 m_canvas
->SetScrollbars( 10, 10, 100, 200 );
420 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
422 // TRUE is to force the frame to close
426 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
429 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
430 _T("Copyright (c) Robert Roebling 1999")
433 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
436 void MyFrame::OnOption(wxCommandEvent
&event
)
438 switch (event
.GetInt())
441 m_mapMode
= wxMM_TEXT
;
443 case MapMode_Lometric
:
444 m_mapMode
= wxMM_LOMETRIC
;
447 m_mapMode
= wxMM_TWIPS
;
450 m_mapMode
= wxMM_POINTS
;
453 m_mapMode
= wxMM_METRIC
;
456 case LogicalOrigin_MoveDown
:
457 m_yLogicalOrigin
+= 10;
459 case LogicalOrigin_MoveUp
:
460 m_yLogicalOrigin
-= 10;
462 case LogicalOrigin_MoveLeft
:
463 m_xLogicalOrigin
+= 10;
465 case LogicalOrigin_MoveRight
:
466 m_xLogicalOrigin
-= 10;
469 case UserScale_StretchHoriz
:
470 m_xUserScale
*= 1.10;
472 case UserScale_ShrinkHoriz
:
473 m_xUserScale
/= 1.10;
475 case UserScale_StretchVertic
:
476 m_yUserScale
*= 1.10;
478 case UserScale_ShrinkVertic
:
479 m_yUserScale
/= 1.10;
481 case UserScale_Restore
:
486 case AxisMirror_Vertic
:
487 m_yAxisReversed
= !m_yAxisReversed
;
489 case AxisMirror_Horiz
:
490 m_xAxisReversed
= !m_xAxisReversed
;
493 case Colour_TextForeground
:
494 m_colourForeground
= SelectColour();
496 case Colour_TextBackground
:
497 m_colourBackground
= SelectColour();
499 case Colour_Background
:
501 wxColour col
= SelectColour();
504 m_backgroundBrush
.SetColour(col
);
508 case Colour_BackgroundMode
:
509 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
521 void MyFrame::PrepareDC(wxDC
& dc
)
523 dc
.SetMapMode( m_mapMode
);
524 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
525 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
526 dc
.SetAxisOrientation( !m_xAxisReversed
, m_yAxisReversed
);
529 wxColour
MyFrame::SelectColour()
533 wxColourDialog
dialog(this, &data
);
535 if ( dialog
.ShowModal() == wxID_OK
)
537 col
= dialog
.GetColourData().GetColour();