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();
64 // Define a new frame type: this is going to be our main frame
65 class MyFrame
: public wxFrame
69 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
71 // event handlers (these functions should _not_ be virtual)
72 void OnQuit(wxCommandEvent
& event
);
73 void OnAbout(wxCommandEvent
& event
);
74 void OnPaint(wxPaintEvent
&event
);
75 void OnOption(wxCommandEvent
&event
);
76 void OnMouseMove(wxMouseEvent
&event
);
78 wxColour
SelectColour();
79 void PrepareDC(wxDC
& dc
);
90 wxColour m_colourForeground
, // these are _text_ colours
92 wxBrush m_backgroundBrush
;
95 // any class wishing to process wxWindows events must use this macro
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // IDs for the controls and the menu commands
112 MapMode_Text
= MenuOption_First
,
118 UserScale_StretchHoriz
,
119 UserScale_ShrinkHoriz
,
120 UserScale_StretchVertic
,
121 UserScale_ShrinkVertic
,
127 LogicalOrigin_MoveDown
,
128 LogicalOrigin_MoveUp
,
129 LogicalOrigin_MoveLeft
,
130 LogicalOrigin_MoveRight
,
132 Colour_TextForeground
,
133 Colour_TextBackground
,
135 Colour_BackgroundMode
,
137 MenuOption_Last
= Colour_BackgroundMode
140 // ----------------------------------------------------------------------------
141 // event tables and other macros for wxWindows
142 // ----------------------------------------------------------------------------
144 // the event tables connect the wxWindows events with the functions (event
145 // handlers) which process them. It can be also done at run-time, but for the
146 // simple menu events like this the static method is much simpler.
147 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
149 EVT_MOTION (MyFrame::OnMouseMove
)
150 EVT_PAINT (MyFrame::OnPaint
)
152 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
153 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
155 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
158 // Create a new application object: this macro will allow wxWindows to create
159 // the application object during program execution (it's better than using a
160 // static object for many reasons) and also declares the accessor function
161 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
165 // ============================================================================
167 // ============================================================================
169 // ----------------------------------------------------------------------------
170 // the application class
171 // ----------------------------------------------------------------------------
173 // `Main program' equivalent: the program execution "starts" here
176 // Create the main application window
177 MyFrame
*frame
= new MyFrame("Drawing sample",
178 wxPoint(50, 50), wxSize(450, 340));
180 // Show it and tell the application that it's our main window
184 // success: wxApp::OnRun() will be called which will enter the main message
185 // loop and the application will run. If we returned FALSE here, the
186 // application would exit immediately.
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
195 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
196 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
198 // set the frame icon
199 SetIcon(wxICON(mondrian
));
201 wxMenu
*menuFile
= new wxMenu
;
202 menuFile
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
203 menuFile
->AppendSeparator();
204 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
206 wxMenu
*menuMapMode
= new wxMenu
;
207 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
208 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
209 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
210 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
211 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
213 wxMenu
*menuUserScale
= new wxMenu
;
214 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
215 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
216 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
217 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
218 menuUserScale
->AppendSeparator();
219 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
221 wxMenu
*menuAxis
= new wxMenu
;
222 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-M", "", TRUE
);
223 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-N", "", TRUE
);
225 wxMenu
*menuLogical
= new wxMenu
;
226 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
227 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
228 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
229 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
231 wxMenu
*menuColour
= new wxMenu
;
232 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
233 menuColour
->Append( Colour_TextBackground
, "Text background..." );
234 menuColour
->Append( Colour_Background
, "Background colour..." );
235 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
237 // now append the freshly created menu to the menu bar...
238 wxMenuBar
*menuBar
= new wxMenuBar
;
239 menuBar
->Append(menuFile
, "&File");
240 menuBar
->Append(menuMapMode
, "&MapMode");
241 menuBar
->Append(menuUserScale
, "&UserScale");
242 menuBar
->Append(menuAxis
, "&Axis");
243 menuBar
->Append(menuLogical
, "&LogicalOrigin");
244 menuBar
->Append(menuColour
, "&Colours");
246 // ... and attach this menu bar to the frame
249 // create a status bar just for fun (by default with 1 pane only)
251 SetStatusText("Welcome to wxWindows!");
253 m_mapMode
= wxMM_TEXT
;
256 m_xLogicalOrigin
= 0;
257 m_yLogicalOrigin
= 0;
259 m_yAxisReversed
= FALSE
;
260 m_backgroundMode
= wxSOLID
;
266 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
268 // TRUE is to force the frame to close
272 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
275 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
276 _T("Copyright (c) Robert Roebling 1999")
279 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
282 void MyFrame::OnOption(wxCommandEvent
&event
)
284 switch (event
.GetInt())
287 m_mapMode
= wxMM_TEXT
;
289 case MapMode_Lometric
:
290 m_mapMode
= wxMM_LOMETRIC
;
293 m_mapMode
= wxMM_TWIPS
;
296 m_mapMode
= wxMM_POINTS
;
299 m_mapMode
= wxMM_METRIC
;
302 case LogicalOrigin_MoveDown
:
303 m_yLogicalOrigin
+= 10;
305 case LogicalOrigin_MoveUp
:
306 m_yLogicalOrigin
-= 10;
308 case LogicalOrigin_MoveLeft
:
309 m_xLogicalOrigin
+= 10;
311 case LogicalOrigin_MoveRight
:
312 m_xLogicalOrigin
-= 10;
315 case UserScale_StretchHoriz
:
316 m_xUserScale
*= 1.10;
318 case UserScale_ShrinkHoriz
:
319 m_xUserScale
/= 1.10;
321 case UserScale_StretchVertic
:
322 m_yUserScale
*= 1.10;
324 case UserScale_ShrinkVertic
:
325 m_yUserScale
/= 1.10;
327 case UserScale_Restore
:
332 case AxisMirror_Vertic
:
333 m_yAxisReversed
= !m_yAxisReversed
;
335 case AxisMirror_Horiz
:
336 m_xAxisReversed
= !m_xAxisReversed
;
339 case Colour_TextForeground
:
340 m_colourForeground
= SelectColour();
342 case Colour_TextBackground
:
343 m_colourBackground
= SelectColour();
345 case Colour_Background
:
347 wxColour col
= SelectColour();
350 m_backgroundBrush
.SetColour(col
);
354 case Colour_BackgroundMode
:
355 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
367 void MyFrame::PrepareDC(wxDC
& dc
)
369 dc
.SetMapMode( m_mapMode
);
370 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
371 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
372 dc
.SetAxisOrientation( !m_xAxisReversed
, m_yAxisReversed
);
375 void MyFrame::OnPaint(wxPaintEvent
&WXUNUSED(event
) )
380 dc
.SetBackgroundMode( m_backgroundMode
);
381 if ( m_backgroundBrush
.Ok() )
382 dc
.SetBackground( m_backgroundBrush
);
383 if ( m_colourForeground
.Ok() )
384 dc
.SetTextForeground( m_colourForeground
);
385 if ( m_colourBackground
.Ok() )
386 dc
.SetTextBackground( m_colourBackground
);
389 dc
.DrawCircle(0, 0, 10);
390 #ifndef __WXGTK__ // not implemented in wxGTK :-(
391 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
394 dc
.DrawRectangle( 10, 10, 90, 90 );
395 dc
.DrawRoundedRectangle( 110, 10, 90, 90, 5 );
397 dc
.SetPen( *wxWHITE_PEN
);
398 dc
.DrawLine( 10, 110, 100, 110 );
399 dc
.SetPen( *wxBLACK_PEN
);
400 dc
.DrawLine( 100, 110, 100, 200 );
401 dc
.SetPen( *wxWHITE_PEN
);
402 dc
.DrawLine( 100, 200, 10, 200 );
403 dc
.SetPen( *wxBLACK_PEN
);
404 dc
.DrawLine( 10, 200, 10, 110 );
406 wxPen
white_butt( "white", 1, wxSOLID
);
407 white_butt
.SetCap( wxCAP_BUTT
);
408 wxPen
black_butt( "black", 1, wxSOLID
);
409 black_butt
.SetCap( wxCAP_BUTT
);
411 dc
.SetPen( white_butt
);
412 dc
.DrawLine( 110, 110, 200, 110 );
413 dc
.SetPen( black_butt
);
414 dc
.DrawLine( 200, 110, 200, 200 );
415 dc
.SetPen( white_butt
);
416 dc
.DrawLine( 200, 200, 110, 200 );
417 dc
.SetPen( black_butt
);
418 dc
.DrawLine( 110, 200, 110, 110 );
420 wxPen
white_miter( "white", 1, wxSOLID
);
421 white_miter
.SetJoin( wxJOIN_MITER
);
422 wxPen
black_miter( "black", 1, wxSOLID
);
423 black_miter
.SetJoin( wxJOIN_MITER
);
425 dc
.SetPen( white_miter
);
426 dc
.DrawLine( 210, 110, 300, 110 );
427 dc
.SetPen( black_miter
);
428 dc
.DrawLine( 300, 110, 300, 200 );
429 dc
.SetPen( white_miter
);
430 dc
.DrawLine( 300, 200, 210, 200 );
431 dc
.SetPen( black_miter
);
432 dc
.DrawLine( 210, 200, 210, 110 );
435 dc
.DrawText( "This is text\n(on multiple lines)", 110, 10 );
437 dc
.DrawIcon( wxICON(mondrian
), 110, 40 );
440 void MyFrame::OnMouseMove(wxMouseEvent
&event
)
445 wxPoint pos
= event
.GetPosition();
446 long x
= dc
.DeviceToLogicalX( pos
.x
);
447 long y
= dc
.DeviceToLogicalY( pos
.y
);
449 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
450 SetStatusText( str
);
453 wxColour
MyFrame::SelectColour()
457 wxColourDialog
dialog(this, &data
);
459 if ( dialog
.ShowModal() == wxID_OK
)
461 col
= dialog
.GetColourData().GetColour();