]> git.saurik.com Git - wxWidgets.git/blob - samples/drawing/drawing.cpp
cbb669192f3a256df3bfd1a8d3a7f2d79723bd0c
[wxWidgets.git] / samples / drawing / drawing.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: drawing.cpp
3 // Purpose: shows and tests wxDC features
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "drawing.cpp"
21 #pragma interface "drawing.cpp"
22 #endif
23
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers
33 #ifndef WX_PRECOMP
34 #include "wx/wx.h"
35 #endif
36
37 #include "wx/colordlg.h"
38
39 // ----------------------------------------------------------------------------
40 // ressources
41 // ----------------------------------------------------------------------------
42 // the application icon
43 #if defined(__WXGTK__) || defined(__WXMOTIF__)
44 #include "mondrian.xpm"
45 #endif
46
47 // ----------------------------------------------------------------------------
48 // private classes
49 // ----------------------------------------------------------------------------
50
51 // Define a new application type, each program should derive a class from wxApp
52 class MyApp : public wxApp
53 {
54 public:
55 // override base class virtuals
56 // ----------------------------
57
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();
62 };
63
64 // Define a new frame type: this is going to be our main frame
65 class MyFrame : public wxFrame
66 {
67 public:
68 // ctor(s)
69 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
70
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);
77
78 wxColour SelectColour() const;
79
80 protected:
81 int m_backgroundMode;
82 int m_mapMode;
83 double m_xUserScale;
84 double m_yUserScale;
85 int m_xLogicalOrigin;
86 int m_yLogicalOrigin;
87 bool m_xAxisReversed,
88 m_yAxisReversed;
89 wxColour m_colourForeground, // these are _text_ colours
90 m_colourBackground;
91 wxBrush m_backgroundBrush;
92
93 private:
94 // any class wishing to process wxWindows events must use this macro
95 DECLARE_EVENT_TABLE()
96 };
97
98 // ----------------------------------------------------------------------------
99 // constants
100 // ----------------------------------------------------------------------------
101
102 // IDs for the controls and the menu commands
103 enum
104 {
105 // menu items
106 Minimal_Quit = 1,
107 Minimal_About,
108
109 MenuOption_First,
110
111 MapMode_Text = MenuOption_First,
112 MapMode_Lometric,
113 MapMode_Twips,
114 MapMode_Points,
115 MapMode_Metric,
116
117 UserScale_StretchHoriz,
118 UserScale_ShrinkHoriz,
119 UserScale_StretchVertic,
120 UserScale_ShrinkVertic,
121 UserScale_Restore,
122
123 AxisMirror_Horiz,
124 AxisMirror_Vertic,
125
126 LogicalOrigin_MoveDown,
127 LogicalOrigin_MoveUp,
128 LogicalOrigin_MoveLeft,
129 LogicalOrigin_MoveRight,
130
131 Colour_TextForeground,
132 Colour_TextBackground,
133 Colour_Background,
134 Colour_BackgroundMode,
135
136 MenuOption_Last = Colour_BackgroundMode
137 };
138
139 // ----------------------------------------------------------------------------
140 // event tables and other macros for wxWindows
141 // ----------------------------------------------------------------------------
142
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)
147
148 EVT_MOTION (MyFrame::OnMouseMove)
149 EVT_PAINT (MyFrame::OnPaint)
150
151 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
152 EVT_MENU(Minimal_About, MyFrame::OnAbout)
153
154 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
155 END_EVENT_TABLE()
156
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
161 // not wxApp)
162 IMPLEMENT_APP(MyApp)
163
164 // ============================================================================
165 // implementation
166 // ============================================================================
167
168 // ----------------------------------------------------------------------------
169 // the application class
170 // ----------------------------------------------------------------------------
171
172 // `Main program' equivalent: the program execution "starts" here
173 bool MyApp::OnInit()
174 {
175 // Create the main application window
176 MyFrame *frame = new MyFrame("Drawing sample",
177 wxPoint(50, 50), wxSize(450, 340));
178
179 // Show it and tell the application that it's our main window
180 frame->Show(TRUE);
181 SetTopWindow(frame);
182
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.
186 return TRUE;
187 }
188
189 // ----------------------------------------------------------------------------
190 // main frame
191 // ----------------------------------------------------------------------------
192
193 // frame constructor
194 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
195 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
196 {
197 // set the frame icon
198 SetIcon(wxICON(mondrian));
199
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");
204
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" );
211
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" );
219
220 wxMenu *menuAxis = new wxMenu;
221 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-\\", "", TRUE );
222 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-/", "", TRUE );
223
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" );
229
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 );
235
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");
244
245 // ... and attach this menu bar to the frame
246 SetMenuBar(menuBar);
247
248 // create a status bar just for fun (by default with 1 pane only)
249 CreateStatusBar(2);
250 SetStatusText("Welcome to wxWindows!");
251
252 m_mapMode = wxMM_TEXT;
253 m_xUserScale = 1.0;
254 m_yUserScale = 1.0;
255 m_xLogicalOrigin = 0;
256 m_yLogicalOrigin = 0;
257 m_xAxisReversed =
258 m_yAxisReversed = FALSE;
259 m_backgroundMode = wxSOLID;
260 }
261
262
263 // event handlers
264
265 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
266 {
267 // TRUE is to force the frame to close
268 Close(TRUE);
269 }
270
271 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
272 {
273 wxString msg;
274 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
275 _T("Copyright (c) Robert Roebling 1999")
276 );
277
278 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
279 }
280
281 void MyFrame::OnOption(wxCommandEvent &event)
282 {
283 switch (event.GetInt())
284 {
285 case MapMode_Text:
286 m_mapMode = wxMM_TEXT;
287 break;
288 case MapMode_Lometric:
289 m_mapMode = wxMM_LOMETRIC;
290 break;
291 case MapMode_Twips:
292 m_mapMode = wxMM_TWIPS;
293 break;
294 case MapMode_Points:
295 m_mapMode = wxMM_POINTS;
296 break;
297 case MapMode_Metric:
298 m_mapMode = wxMM_METRIC;
299 break;
300
301 case LogicalOrigin_MoveDown:
302 m_yLogicalOrigin += 10;
303 break;
304 case LogicalOrigin_MoveUp:
305 m_yLogicalOrigin -= 10;
306 break;
307 case LogicalOrigin_MoveLeft:
308 m_xLogicalOrigin += 10;
309 break;
310 case LogicalOrigin_MoveRight:
311 m_xLogicalOrigin -= 10;
312 break;
313
314 case UserScale_StretchHoriz:
315 m_xUserScale *= 1.10;
316 break;
317 case UserScale_ShrinkHoriz:
318 m_xUserScale /= 1.10;
319 break;
320 case UserScale_StretchVertic:
321 m_yUserScale *= 1.10;
322 break;
323 case UserScale_ShrinkVertic:
324 m_yUserScale /= 1.10;
325 break;
326 case UserScale_Restore:
327 m_xUserScale =
328 m_yUserScale = 1.0;
329 break;
330
331 case AxisMirror_Vertic:
332 m_yAxisReversed = !m_yAxisReversed;
333 break;
334 case AxisMirror_Horiz:
335 m_xAxisReversed = !m_xAxisReversed;
336 break;
337
338 case Colour_TextForeground:
339 m_colourForeground = SelectColour();
340 break;
341 case Colour_TextBackground:
342 m_colourBackground = SelectColour();
343 break;
344 case Colour_Background:
345 {
346 wxColour col = SelectColour();
347 if ( col.Ok() )
348 {
349 m_backgroundBrush.SetColour(col);
350 }
351 }
352 break;
353 case Colour_BackgroundMode:
354 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
355 : wxSOLID;
356 break;
357
358 default:
359 // skip Refresh()
360 return;
361 }
362
363 Refresh();
364 }
365
366 void MyFrame::OnPaint(wxPaintEvent &WXUNUSED(event) )
367 {
368 wxPaintDC dc(this);
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 );
373
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 );
381
382 dc.DrawRectangle( 10, 10, 90, 90 );
383 dc.DrawRoundedRectangle( 10, 110, 90, 90, 5 );
384
385 dc.DrawText( "This is text\n(on multiple lines)", 110, 10 );
386
387 dc.DrawIcon( wxICON(mondrian), 110, 40 );
388 }
389
390 void MyFrame::OnMouseMove(wxMouseEvent &event)
391 {
392 wxClientDC dc(this);
393 dc.SetMapMode( m_mapMode );
394 dc.SetUserScale( m_xUserScale, m_yUserScale );
395 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
396
397 wxPoint pos = event.GetPosition();
398 long x = dc.DeviceToLogicalX( pos.x );
399 long y = dc.DeviceToLogicalY( pos.y );
400 wxString str;
401 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
402 SetStatusText( str );
403 }
404
405 wxColour MyFrame::SelectColour() const
406 {
407 wxColour col;
408 wxColourData data;
409 wxColourDialog dialog(this, &data);
410
411 if ( dialog.ShowModal() == wxID_OK )
412 {
413 col = data.GetColour();
414 }
415
416 return col;
417 }