]> git.saurik.com Git - wxWidgets.git/blame - samples/drawing/drawing.cpp
wxProgressDialg works again.
[wxWidgets.git] / samples / drawing / drawing.cpp
CommitLineData
aba99005
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: drawing.cpp
0f0c61d0 3// Purpose: shows and tests wxDC features
aba99005
RR
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__
0f0c61d0
VZ
20 #pragma implementation "drawing.cpp"
21 #pragma interface "drawing.cpp"
aba99005
RR
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
0f0c61d0
VZ
37#include "wx/colordlg.h"
38
aba99005
RR
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
52class MyApp : public wxApp
53{
54public:
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
65class MyFrame : public wxFrame
66{
67public:
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
220af862
VZ
78 wxColour SelectColour();
79 void PrepareDC(wxDC& dc);
0f0c61d0
VZ
80
81protected:
82 int m_backgroundMode;
aba99005
RR
83 int m_mapMode;
84 double m_xUserScale;
85 double m_yUserScale;
86 int m_xLogicalOrigin;
87 int m_yLogicalOrigin;
0f0c61d0
VZ
88 bool m_xAxisReversed,
89 m_yAxisReversed;
90 wxColour m_colourForeground, // these are _text_ colours
91 m_colourBackground;
92 wxBrush m_backgroundBrush;
aba99005
RR
93
94private:
95 // any class wishing to process wxWindows events must use this macro
96 DECLARE_EVENT_TABLE()
97};
98
99// ----------------------------------------------------------------------------
100// constants
101// ----------------------------------------------------------------------------
102
103// IDs for the controls and the menu commands
104enum
105{
106 // menu items
107 Minimal_Quit = 1,
108 Minimal_About,
0f0c61d0
VZ
109
110 MenuOption_First,
111
112 MapMode_Text = MenuOption_First,
aba99005
RR
113 MapMode_Lometric,
114 MapMode_Twips,
115 MapMode_Points,
116 MapMode_Metric,
0f0c61d0 117
aba99005
RR
118 UserScale_StretchHoriz,
119 UserScale_ShrinkHoriz,
120 UserScale_StretchVertic,
121 UserScale_ShrinkVertic,
0f0c61d0
VZ
122 UserScale_Restore,
123
aba99005
RR
124 AxisMirror_Horiz,
125 AxisMirror_Vertic,
0f0c61d0 126
aba99005
RR
127 LogicalOrigin_MoveDown,
128 LogicalOrigin_MoveUp,
129 LogicalOrigin_MoveLeft,
130 LogicalOrigin_MoveRight,
0f0c61d0
VZ
131
132 Colour_TextForeground,
133 Colour_TextBackground,
134 Colour_Background,
135 Colour_BackgroundMode,
136
137 MenuOption_Last = Colour_BackgroundMode
aba99005
RR
138};
139
140// ----------------------------------------------------------------------------
141// event tables and other macros for wxWindows
142// ----------------------------------------------------------------------------
143
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.
147BEGIN_EVENT_TABLE(MyFrame, wxFrame)
148
149 EVT_MOTION (MyFrame::OnMouseMove)
150 EVT_PAINT (MyFrame::OnPaint)
0f0c61d0 151
aba99005
RR
152 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
153 EVT_MENU(Minimal_About, MyFrame::OnAbout)
0f0c61d0
VZ
154
155 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
aba99005
RR
156END_EVENT_TABLE()
157
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
162// not wxApp)
163IMPLEMENT_APP(MyApp)
164
165// ============================================================================
166// implementation
167// ============================================================================
168
169// ----------------------------------------------------------------------------
170// the application class
171// ----------------------------------------------------------------------------
172
173// `Main program' equivalent: the program execution "starts" here
174bool MyApp::OnInit()
175{
176 // Create the main application window
ed43910d 177 MyFrame *frame = new MyFrame("Drawing sample",
aba99005
RR
178 wxPoint(50, 50), wxSize(450, 340));
179
180 // Show it and tell the application that it's our main window
aba99005
RR
181 frame->Show(TRUE);
182 SetTopWindow(frame);
183
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.
187 return TRUE;
188}
189
190// ----------------------------------------------------------------------------
191// main frame
192// ----------------------------------------------------------------------------
193
194// frame constructor
195MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
196 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
197{
198 // set the frame icon
199 SetIcon(wxICON(mondrian));
200
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");
0f0c61d0 205
aba99005
RR
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" );
0f0c61d0 212
aba99005 213 wxMenu *menuUserScale = new wxMenu;
ed43910d
RR
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" );
0f0c61d0
VZ
218 menuUserScale->AppendSeparator();
219 menuUserScale->Append( UserScale_Restore, "Restore to normal\tCtrl-0" );
220
aba99005 221 wxMenu *menuAxis = new wxMenu;
220af862
VZ
222 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-M", "", TRUE );
223 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-N", "", TRUE );
0f0c61d0 224
aba99005
RR
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" );
230
0f0c61d0
VZ
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 );
236
aba99005
RR
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");
0f0c61d0 244 menuBar->Append(menuColour, "&Colours");
aba99005
RR
245
246 // ... and attach this menu bar to the frame
247 SetMenuBar(menuBar);
248
249 // create a status bar just for fun (by default with 1 pane only)
250 CreateStatusBar(2);
251 SetStatusText("Welcome to wxWindows!");
0f0c61d0 252
aba99005
RR
253 m_mapMode = wxMM_TEXT;
254 m_xUserScale = 1.0;
255 m_yUserScale = 1.0;
256 m_xLogicalOrigin = 0;
257 m_yLogicalOrigin = 0;
0f0c61d0
VZ
258 m_xAxisReversed =
259 m_yAxisReversed = FALSE;
260 m_backgroundMode = wxSOLID;
aba99005
RR
261}
262
263
264// event handlers
265
266void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
267{
268 // TRUE is to force the frame to close
269 Close(TRUE);
270}
271
272void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
273{
274 wxString msg;
275 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
ed43910d 276 _T("Copyright (c) Robert Roebling 1999")
aba99005
RR
277 );
278
279 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
280}
281
282void MyFrame::OnOption(wxCommandEvent &event)
283{
284 switch (event.GetInt())
285 {
286 case MapMode_Text:
b9857632 287 m_mapMode = wxMM_TEXT;
0f0c61d0 288 break;
aba99005
RR
289 case MapMode_Lometric:
290 m_mapMode = wxMM_LOMETRIC;
0f0c61d0
VZ
291 break;
292 case MapMode_Twips:
aba99005 293 m_mapMode = wxMM_TWIPS;
0f0c61d0
VZ
294 break;
295 case MapMode_Points:
aba99005 296 m_mapMode = wxMM_POINTS;
0f0c61d0
VZ
297 break;
298 case MapMode_Metric:
aba99005 299 m_mapMode = wxMM_METRIC;
0f0c61d0
VZ
300 break;
301
302 case LogicalOrigin_MoveDown:
303 m_yLogicalOrigin += 10;
304 break;
305 case LogicalOrigin_MoveUp:
306 m_yLogicalOrigin -= 10;
307 break;
308 case LogicalOrigin_MoveLeft:
309 m_xLogicalOrigin += 10;
310 break;
311 case LogicalOrigin_MoveRight:
312 m_xLogicalOrigin -= 10;
313 break;
314
315 case UserScale_StretchHoriz:
316 m_xUserScale *= 1.10;
317 break;
318 case UserScale_ShrinkHoriz:
319 m_xUserScale /= 1.10;
320 break;
321 case UserScale_StretchVertic:
322 m_yUserScale *= 1.10;
323 break;
324 case UserScale_ShrinkVertic:
325 m_yUserScale /= 1.10;
326 break;
327 case UserScale_Restore:
328 m_xUserScale =
329 m_yUserScale = 1.0;
330 break;
331
332 case AxisMirror_Vertic:
333 m_yAxisReversed = !m_yAxisReversed;
334 break;
335 case AxisMirror_Horiz:
336 m_xAxisReversed = !m_xAxisReversed;
337 break;
338
339 case Colour_TextForeground:
340 m_colourForeground = SelectColour();
341 break;
342 case Colour_TextBackground:
343 m_colourBackground = SelectColour();
344 break;
345 case Colour_Background:
346 {
347 wxColour col = SelectColour();
348 if ( col.Ok() )
349 {
350 m_backgroundBrush.SetColour(col);
351 }
352 }
353 break;
354 case Colour_BackgroundMode:
355 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
356 : wxSOLID;
357 break;
358
359 default:
360 // skip Refresh()
361 return;
aba99005 362 }
0f0c61d0 363
aba99005
RR
364 Refresh();
365}
366
220af862 367void MyFrame::PrepareDC(wxDC& dc)
aba99005 368{
0f0c61d0
VZ
369 dc.SetMapMode( m_mapMode );
370 dc.SetUserScale( m_xUserScale, m_yUserScale );
371 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
428db2ea 372 dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
220af862
VZ
373}
374
375void MyFrame::OnPaint(wxPaintEvent &WXUNUSED(event) )
376{
377 wxPaintDC dc(this);
378 PrepareDC(dc);
0f0c61d0
VZ
379
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 );
387
220af862
VZ
388 // mark the origin
389 dc.DrawCircle(0, 0, 10);
390#ifndef __WXGTK__ // not implemented in wxGTK :-(
391 dc.FloodFill(0, 0, wxColour(255, 0, 0));
392#endif // __WXGTK__
393
0f0c61d0 394 dc.DrawRectangle( 10, 10, 90, 90 );
c17bcb84
RR
395 dc.DrawRoundedRectangle( 110, 10, 90, 90, 5 );
396
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 );
405
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 );
410
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 );
419
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 );
424
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 );
433
0f0c61d0
VZ
434
435 dc.DrawText( "This is text\n(on multiple lines)", 110, 10 );
436
437 dc.DrawIcon( wxICON(mondrian), 110, 40 );
aba99005
RR
438}
439
440void MyFrame::OnMouseMove(wxMouseEvent &event)
441{
0f0c61d0 442 wxClientDC dc(this);
220af862 443 PrepareDC(dc);
0f0c61d0
VZ
444
445 wxPoint pos = event.GetPosition();
446 long x = dc.DeviceToLogicalX( pos.x );
447 long y = dc.DeviceToLogicalY( pos.y );
448 wxString str;
449 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
450 SetStatusText( str );
451}
452
220af862 453wxColour MyFrame::SelectColour()
0f0c61d0
VZ
454{
455 wxColour col;
456 wxColourData data;
457 wxColourDialog dialog(this, &data);
458
459 if ( dialog.ShowModal() == wxID_OK )
460 {
428db2ea 461 col = dialog.GetColourData().GetColour();
0f0c61d0
VZ
462 }
463
464 return col;
aba99005 465}