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 #if !(defined __WXGTK__) && !(defined __WXMOTIF__)
274 // not implemented in wxGTK or wxMOTIF :-(
275 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
278 dc
.DrawText( "This is text", 110, 10 );
280 dc
.SetFont( wxFont( 18, wxSWISS
, 0, 0 ) );
282 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
287 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
289 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
290 dc
.DrawText( text
, 110, 80 );
292 dc
.DrawRectangle( 100, 40, 4, height
);
294 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
295 dc
.DrawText( text
, 110, 120 );
298 dc
.DrawIcon( wxICON(mondrian
), 310, 40 );
300 DrawTestLines( 0, 200, 0, dc
);
302 DrawTestLines( 0, 400, 1, dc
);
304 DrawTestLines( 0, 600, 2, dc
);
306 DrawTestLines( 0, 800, 6, dc
);
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
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
)
325 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
326 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
328 // set the frame icon
329 SetIcon(wxICON(mondrian
));
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");
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" );
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" );
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
);
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" );
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
);
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");
376 // ... and attach this menu bar to the frame
379 // create a status bar just for fun (by default with 1 pane only)
381 SetStatusText("Welcome to wxWindows!");
383 m_mapMode
= wxMM_TEXT
;
386 m_xLogicalOrigin
= 0;
387 m_yLogicalOrigin
= 0;
389 m_yAxisReversed
= FALSE
;
390 m_backgroundMode
= wxSOLID
;
392 m_canvas
= new MyCanvas( this );
393 m_canvas
->SetScrollbars( 10, 10, 100, 200 );
398 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
400 // TRUE is to force the frame to close
404 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
407 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
408 _T("Copyright (c) Robert Roebling 1999")
411 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
414 void MyFrame::OnOption(wxCommandEvent
&event
)
416 switch (event
.GetInt())
419 m_mapMode
= wxMM_TEXT
;
421 case MapMode_Lometric
:
422 m_mapMode
= wxMM_LOMETRIC
;
425 m_mapMode
= wxMM_TWIPS
;
428 m_mapMode
= wxMM_POINTS
;
431 m_mapMode
= wxMM_METRIC
;
434 case LogicalOrigin_MoveDown
:
435 m_yLogicalOrigin
+= 10;
437 case LogicalOrigin_MoveUp
:
438 m_yLogicalOrigin
-= 10;
440 case LogicalOrigin_MoveLeft
:
441 m_xLogicalOrigin
+= 10;
443 case LogicalOrigin_MoveRight
:
444 m_xLogicalOrigin
-= 10;
447 case UserScale_StretchHoriz
:
448 m_xUserScale
*= 1.10;
450 case UserScale_ShrinkHoriz
:
451 m_xUserScale
/= 1.10;
453 case UserScale_StretchVertic
:
454 m_yUserScale
*= 1.10;
456 case UserScale_ShrinkVertic
:
457 m_yUserScale
/= 1.10;
459 case UserScale_Restore
:
464 case AxisMirror_Vertic
:
465 m_yAxisReversed
= !m_yAxisReversed
;
467 case AxisMirror_Horiz
:
468 m_xAxisReversed
= !m_xAxisReversed
;
471 case Colour_TextForeground
:
472 m_colourForeground
= SelectColour();
474 case Colour_TextBackground
:
475 m_colourBackground
= SelectColour();
477 case Colour_Background
:
479 wxColour col
= SelectColour();
482 m_backgroundBrush
.SetColour(col
);
486 case Colour_BackgroundMode
:
487 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
499 void MyFrame::PrepareDC(wxDC
& dc
)
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
);
507 void MyFrame::OnMouseMove(wxMouseEvent
&event
)
512 wxPoint pos
= event
.GetPosition();
513 long x
= dc
.DeviceToLogicalX( pos
.x
);
514 long y
= dc
.DeviceToLogicalY( pos
.y
);
516 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
517 SetStatusText( str
);
520 wxColour
MyFrame::SelectColour()
524 wxColourDialog
dialog(this, &data
);
526 if ( dialog
.ShowModal() == wxID_OK
)
528 col
= dialog
.GetColourData().GetColour();