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 DrawTestLines( int x
, int y
, int width
, 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::DrawTestLines( int x
, int y
, int width
, wxDC
&dc
)
212 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
213 dc
.SetBrush( *wxRED_BRUSH
);
214 dc
.DrawRectangle( x
+10, y
+10, 400, 190 );
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 );
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 );
240 wxPen
ud( "black", width
, wxUSER_DASH
);
243 ud
.SetDashes( 1, dash1
);
244 dc
.DrawLine( x
+20, y
+140, 390, y
+140 );
246 ud
.SetDashes( 1, dash1
);
247 dc
.DrawLine( x
+20, y
+150, 390, y
+150 );
249 ud
.SetDashes( 1, dash1
);
250 dc
.DrawLine( x
+20, y
+160, 390, y
+160 );
252 ud
.SetDashes( 1, dash1
);
253 dc
.DrawLine( x
+20, y
+170, 390, y
+170 );
257 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
261 m_owner
->PrepareDC(dc
);
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
);
272 dc
.DrawCircle(0, 0, 10);
273 #ifndef __WXGTK__ // not implemented in wxGTK :-(
274 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
277 dc
.DrawText( "This is text", 110, 10 );
279 dc
.SetFont( wxFont( 18, wxSWISS
, 0, 0 ) );
281 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
286 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
288 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
289 dc
.DrawText( text
, 110, 80 );
291 dc
.DrawRectangle( 100, 40, 4, height
);
293 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
294 dc
.DrawText( text
, 110, 120 );
297 dc
.DrawIcon( wxICON(mondrian
), 310, 40 );
299 DrawTestLines( 0, 200, 0, dc
);
301 DrawTestLines( 0, 400, 1, dc
);
303 DrawTestLines( 0, 600, 2, dc
);
305 DrawTestLines( 0, 800, 6, dc
);
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
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
)
324 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
325 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
327 // set the frame icon
328 SetIcon(wxICON(mondrian
));
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");
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" );
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" );
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
);
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" );
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
);
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");
375 // ... and attach this menu bar to the frame
378 // create a status bar just for fun (by default with 1 pane only)
380 SetStatusText("Welcome to wxWindows!");
382 m_mapMode
= wxMM_TEXT
;
385 m_xLogicalOrigin
= 0;
386 m_yLogicalOrigin
= 0;
388 m_yAxisReversed
= FALSE
;
389 m_backgroundMode
= wxSOLID
;
391 m_canvas
= new MyCanvas( this );
392 m_canvas
->SetScrollbars( 10, 10, 100, 200 );
397 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
399 // TRUE is to force the frame to close
403 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
406 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
407 _T("Copyright (c) Robert Roebling 1999")
410 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
413 void MyFrame::OnOption(wxCommandEvent
&event
)
415 switch (event
.GetInt())
418 m_mapMode
= wxMM_TEXT
;
420 case MapMode_Lometric
:
421 m_mapMode
= wxMM_LOMETRIC
;
424 m_mapMode
= wxMM_TWIPS
;
427 m_mapMode
= wxMM_POINTS
;
430 m_mapMode
= wxMM_METRIC
;
433 case LogicalOrigin_MoveDown
:
434 m_yLogicalOrigin
+= 10;
436 case LogicalOrigin_MoveUp
:
437 m_yLogicalOrigin
-= 10;
439 case LogicalOrigin_MoveLeft
:
440 m_xLogicalOrigin
+= 10;
442 case LogicalOrigin_MoveRight
:
443 m_xLogicalOrigin
-= 10;
446 case UserScale_StretchHoriz
:
447 m_xUserScale
*= 1.10;
449 case UserScale_ShrinkHoriz
:
450 m_xUserScale
/= 1.10;
452 case UserScale_StretchVertic
:
453 m_yUserScale
*= 1.10;
455 case UserScale_ShrinkVertic
:
456 m_yUserScale
/= 1.10;
458 case UserScale_Restore
:
463 case AxisMirror_Vertic
:
464 m_yAxisReversed
= !m_yAxisReversed
;
466 case AxisMirror_Horiz
:
467 m_xAxisReversed
= !m_xAxisReversed
;
470 case Colour_TextForeground
:
471 m_colourForeground
= SelectColour();
473 case Colour_TextBackground
:
474 m_colourBackground
= SelectColour();
476 case Colour_Background
:
478 wxColour col
= SelectColour();
481 m_backgroundBrush
.SetColour(col
);
485 case Colour_BackgroundMode
:
486 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
498 void MyFrame::PrepareDC(wxDC
& dc
)
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
);
506 void MyFrame::OnMouseMove(wxMouseEvent
&event
)
511 wxPoint pos
= event
.GetPosition();
512 long x
= dc
.DeviceToLogicalX( pos
.x
);
513 long y
= dc
.DeviceToLogicalY( pos
.y
);
515 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
516 SetStatusText( str
);
519 wxColour
MyFrame::SelectColour()
523 wxColourDialog
dialog(this, &data
);
525 if ( dialog
.ShowModal() == wxID_OK
)
527 col
= dialog
.GetColourData().GetColour();