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() const;
89 wxColour m_colourForeground
, // these are _text_ colours
91 wxBrush m_backgroundBrush
;
94 // any class wishing to process wxWindows events must use this macro
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // IDs for the controls and the menu commands
111 MapMode_Text
= MenuOption_First
,
117 UserScale_StretchHoriz
,
118 UserScale_ShrinkHoriz
,
119 UserScale_StretchVertic
,
120 UserScale_ShrinkVertic
,
126 LogicalOrigin_MoveDown
,
127 LogicalOrigin_MoveUp
,
128 LogicalOrigin_MoveLeft
,
129 LogicalOrigin_MoveRight
,
131 Colour_TextForeground
,
132 Colour_TextBackground
,
134 Colour_BackgroundMode
,
136 MenuOption_Last
= Colour_BackgroundMode
139 // ----------------------------------------------------------------------------
140 // event tables and other macros for wxWindows
141 // ----------------------------------------------------------------------------
143 // the event tables connect the wxWindows events with the functions (event
144 // handlers) which process them. It can be also done at run-time, but for the
145 // simple menu events like this the static method is much simpler.
146 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
148 EVT_MOTION (MyFrame::OnMouseMove
)
149 EVT_PAINT (MyFrame::OnPaint
)
151 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
152 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
154 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
157 // Create a new application object: this macro will allow wxWindows to create
158 // the application object during program execution (it's better than using a
159 // static object for many reasons) and also declares the accessor function
160 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
164 // ============================================================================
166 // ============================================================================
168 // ----------------------------------------------------------------------------
169 // the application class
170 // ----------------------------------------------------------------------------
172 // `Main program' equivalent: the program execution "starts" here
175 // Create the main application window
176 MyFrame
*frame
= new MyFrame("Drawing sample",
177 wxPoint(50, 50), wxSize(450, 340));
179 // Show it and tell the application that it's our main window
183 // success: wxApp::OnRun() will be called which will enter the main message
184 // loop and the application will run. If we returned FALSE here, the
185 // application would exit immediately.
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
194 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
195 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
197 // set the frame icon
198 SetIcon(wxICON(mondrian
));
200 wxMenu
*menuFile
= new wxMenu
;
201 menuFile
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
202 menuFile
->AppendSeparator();
203 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
205 wxMenu
*menuMapMode
= new wxMenu
;
206 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
207 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
208 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
209 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
210 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
212 wxMenu
*menuUserScale
= new wxMenu
;
213 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
214 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
215 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
216 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
217 menuUserScale
->AppendSeparator();
218 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
220 wxMenu
*menuAxis
= new wxMenu
;
221 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-\\", "", TRUE
);
222 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-/", "", TRUE
);
224 wxMenu
*menuLogical
= new wxMenu
;
225 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
226 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
227 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
228 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
230 wxMenu
*menuColour
= new wxMenu
;
231 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
232 menuColour
->Append( Colour_TextBackground
, "Text background..." );
233 menuColour
->Append( Colour_Background
, "Background colour..." );
234 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
236 // now append the freshly created menu to the menu bar...
237 wxMenuBar
*menuBar
= new wxMenuBar
;
238 menuBar
->Append(menuFile
, "&File");
239 menuBar
->Append(menuMapMode
, "&MapMode");
240 menuBar
->Append(menuUserScale
, "&UserScale");
241 menuBar
->Append(menuAxis
, "&Axis");
242 menuBar
->Append(menuLogical
, "&LogicalOrigin");
243 menuBar
->Append(menuColour
, "&Colours");
245 // ... and attach this menu bar to the frame
248 // create a status bar just for fun (by default with 1 pane only)
250 SetStatusText("Welcome to wxWindows!");
252 m_mapMode
= wxMM_TEXT
;
255 m_xLogicalOrigin
= 0;
256 m_yLogicalOrigin
= 0;
258 m_yAxisReversed
= FALSE
;
259 m_backgroundMode
= wxSOLID
;
265 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
267 // TRUE is to force the frame to close
271 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
274 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
275 _T("Copyright (c) Robert Roebling 1999")
278 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
281 void MyFrame::OnOption(wxCommandEvent
&event
)
283 switch (event
.GetInt())
286 m_mapMode
= wxMM_TEXT
;
288 case MapMode_Lometric
:
289 m_mapMode
= wxMM_LOMETRIC
;
292 m_mapMode
= wxMM_TWIPS
;
295 m_mapMode
= wxMM_POINTS
;
298 m_mapMode
= wxMM_METRIC
;
301 case LogicalOrigin_MoveDown
:
302 m_yLogicalOrigin
+= 10;
304 case LogicalOrigin_MoveUp
:
305 m_yLogicalOrigin
-= 10;
307 case LogicalOrigin_MoveLeft
:
308 m_xLogicalOrigin
+= 10;
310 case LogicalOrigin_MoveRight
:
311 m_xLogicalOrigin
-= 10;
314 case UserScale_StretchHoriz
:
315 m_xUserScale
*= 1.10;
317 case UserScale_ShrinkHoriz
:
318 m_xUserScale
/= 1.10;
320 case UserScale_StretchVertic
:
321 m_yUserScale
*= 1.10;
323 case UserScale_ShrinkVertic
:
324 m_yUserScale
/= 1.10;
326 case UserScale_Restore
:
331 case AxisMirror_Vertic
:
332 m_yAxisReversed
= !m_yAxisReversed
;
334 case AxisMirror_Horiz
:
335 m_xAxisReversed
= !m_xAxisReversed
;
338 case Colour_TextForeground
:
339 m_colourForeground
= SelectColour();
341 case Colour_TextBackground
:
342 m_colourBackground
= SelectColour();
344 case Colour_Background
:
346 wxColour col
= SelectColour();
349 m_backgroundBrush
.SetColour(col
);
353 case Colour_BackgroundMode
:
354 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
366 void MyFrame::OnPaint(wxPaintEvent
&WXUNUSED(event
) )
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
);
374 dc
.SetBackgroundMode( m_backgroundMode
);
375 if ( m_backgroundBrush
.Ok() )
376 dc
.SetBackground( m_backgroundBrush
);
377 if ( m_colourForeground
.Ok() )
378 dc
.SetTextForeground( m_colourForeground
);
379 if ( m_colourBackground
.Ok() )
380 dc
.SetTextBackground( m_colourBackground
);
382 dc
.DrawRectangle( 10, 10, 90, 90 );
383 dc
.DrawRoundedRectangle( 10, 110, 90, 90, 5 );
385 dc
.DrawText( "This is text\n(on multiple lines)", 110, 10 );
387 dc
.DrawIcon( wxICON(mondrian
), 110, 40 );
390 void MyFrame::OnMouseMove(wxMouseEvent
&event
)
393 dc
.SetMapMode( m_mapMode
);
394 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
395 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
397 wxPoint pos
= event
.GetPosition();
398 long x
= dc
.DeviceToLogicalX( pos
.x
);
399 long y
= dc
.DeviceToLogicalY( pos
.y
);
401 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
402 SetStatusText( str
);
405 wxColour
MyFrame::SelectColour() const
409 wxColourDialog
dialog(this, &data
);
411 if ( dialog
.ShowModal() == wxID_OK
)
413 col
= data
.GetColour();