]> git.saurik.com Git - wxWidgets.git/blame - samples/drawing/drawing.cpp
Added tests for buffered streams.
[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
b62c3631
RR
64class MyCanvas;
65
aba99005
RR
66// Define a new frame type: this is going to be our main frame
67class MyFrame : public wxFrame
68{
69public:
70 // ctor(s)
71 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
72
73 // event handlers (these functions should _not_ be virtual)
74 void OnQuit(wxCommandEvent& event);
75 void OnAbout(wxCommandEvent& event);
aba99005 76 void OnOption(wxCommandEvent &event);
aba99005 77
220af862
VZ
78 wxColour SelectColour();
79 void PrepareDC(wxDC& dc);
0f0c61d0 80
b62c3631
RR
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 MyCanvas *m_canvas;
aba99005
RR
93
94private:
95 // any class wishing to process wxWindows events must use this macro
96 DECLARE_EVENT_TABLE()
97};
98
b62c3631
RR
99// define a scrollable canvas for drawing onto
100class MyCanvas: public wxScrolledWindow
101{
102public:
103 MyCanvas( MyFrame *parent );
1e7fd311
RR
104
105 void DrawTestLines( int x, int y, int width, wxDC &dc );
b62c3631 106 void OnPaint(wxPaintEvent &event);
bf0c00c6 107 void OnMouseMove(wxMouseEvent &event);
b62c3631
RR
108
109protected:
110 MyFrame *m_owner;
111
112private:
113 DECLARE_EVENT_TABLE()
114};
115
aba99005
RR
116// ----------------------------------------------------------------------------
117// constants
118// ----------------------------------------------------------------------------
119
120// IDs for the controls and the menu commands
121enum
122{
123 // menu items
124 Minimal_Quit = 1,
125 Minimal_About,
0f0c61d0
VZ
126
127 MenuOption_First,
128
129 MapMode_Text = MenuOption_First,
aba99005
RR
130 MapMode_Lometric,
131 MapMode_Twips,
132 MapMode_Points,
133 MapMode_Metric,
0f0c61d0 134
aba99005
RR
135 UserScale_StretchHoriz,
136 UserScale_ShrinkHoriz,
137 UserScale_StretchVertic,
138 UserScale_ShrinkVertic,
0f0c61d0
VZ
139 UserScale_Restore,
140
aba99005
RR
141 AxisMirror_Horiz,
142 AxisMirror_Vertic,
0f0c61d0 143
aba99005
RR
144 LogicalOrigin_MoveDown,
145 LogicalOrigin_MoveUp,
146 LogicalOrigin_MoveLeft,
147 LogicalOrigin_MoveRight,
0f0c61d0
VZ
148
149 Colour_TextForeground,
150 Colour_TextBackground,
151 Colour_Background,
152 Colour_BackgroundMode,
153
154 MenuOption_Last = Colour_BackgroundMode
aba99005
RR
155};
156
157// ----------------------------------------------------------------------------
158// event tables and other macros for wxWindows
159// ----------------------------------------------------------------------------
160
aba99005
RR
161
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
166// not wxApp)
167IMPLEMENT_APP(MyApp)
168
169// ============================================================================
170// implementation
171// ============================================================================
172
173// ----------------------------------------------------------------------------
174// the application class
175// ----------------------------------------------------------------------------
176
177// `Main program' equivalent: the program execution "starts" here
178bool MyApp::OnInit()
179{
180 // Create the main application window
ed43910d 181 MyFrame *frame = new MyFrame("Drawing sample",
b62c3631 182 wxPoint(50, 50), wxSize(550, 340));
aba99005
RR
183
184 // Show it and tell the application that it's our main window
aba99005
RR
185 frame->Show(TRUE);
186 SetTopWindow(frame);
187
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.
191 return TRUE;
192}
193
194// ----------------------------------------------------------------------------
b62c3631 195// MyCanvas
aba99005
RR
196// ----------------------------------------------------------------------------
197
b62c3631
RR
198// the event tables connect the wxWindows events with the functions (event
199// handlers) which process them.
200BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
201 EVT_PAINT (MyCanvas::OnPaint)
bf0c00c6 202 EVT_MOTION (MyCanvas::OnMouseMove)
b62c3631
RR
203END_EVENT_TABLE()
204
205MyCanvas::MyCanvas( MyFrame *parent )
206 : wxScrolledWindow( parent )
207{
208 m_owner = parent;
209}
210
1e7fd311 211void MyCanvas::DrawTestLines( int x, int y, int width, wxDC &dc )
b62c3631 212{
1e7fd311
RR
213 dc.SetPen( wxPen( "black", width, wxSOLID) );
214 dc.SetBrush( *wxRED_BRUSH );
215 dc.DrawRectangle( x+10, y+10, 400, 190 );
216
217 dc.SetPen( wxPen( "black", width, wxSOLID) );
218 dc.DrawLine( x+20, y+20, 390, y+20 );
219 dc.SetPen( wxPen( "black", width, wxDOT) );
220 dc.DrawLine( x+20, y+30, 390, y+30 );
221 dc.SetPen( wxPen( "black", width, wxSHORT_DASH) );
222 dc.DrawLine( x+20, y+40, 390, y+40 );
223 dc.SetPen( wxPen( "black", width, wxLONG_DASH) );
224 dc.DrawLine( x+20, y+50, 390, y+50 );
225 dc.SetPen( wxPen( "black", width, wxDOT_DASH) );
226 dc.DrawLine( x+20, y+60, 390, y+60 );
227
228 dc.SetPen( wxPen( "black", width, wxBDIAGONAL_HATCH) );
229 dc.DrawLine( x+20, y+70, 390, y+70 );
230 dc.SetPen( wxPen( "black", width, wxCROSSDIAG_HATCH) );
231 dc.DrawLine( x+20, y+80, 390, y+80 );
232 dc.SetPen( wxPen( "black", width, wxFDIAGONAL_HATCH) );
233 dc.DrawLine( x+20, y+90, 390, y+90 );
234 dc.SetPen( wxPen( "black", width, wxCROSS_HATCH) );
235 dc.DrawLine( x+20, y+100, 390, y+100 );
236 dc.SetPen( wxPen( "black", width, wxHORIZONTAL_HATCH) );
237 dc.DrawLine( x+20, y+110, 390, y+110 );
238 dc.SetPen( wxPen( "black", width, wxVERTICAL_HATCH) );
239 dc.DrawLine( x+20, y+120, 390, y+120 );
240
241 wxPen ud( "black", width, wxUSER_DASH );
242 wxDash dash1[1];
243 dash1[0] = 0;
244 ud.SetDashes( 1, dash1 );
245 dc.DrawLine( x+20, y+140, 390, y+140 );
246 dash1[0] = 1;
247 ud.SetDashes( 1, dash1 );
248 dc.DrawLine( x+20, y+150, 390, y+150 );
249 dash1[0] = 2;
250 ud.SetDashes( 1, dash1 );
251 dc.DrawLine( x+20, y+160, 390, y+160 );
252 dash1[0] = 0xFF;
253 ud.SetDashes( 1, dash1 );
254 dc.DrawLine( x+20, y+170, 390, y+170 );
255
b62c3631
RR
256}
257
258void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
259{
260 wxPaintDC dc(this);
261 PrepareDC(dc);
262 m_owner->PrepareDC(dc);
263
264 dc.SetBackgroundMode( m_owner->m_backgroundMode );
265 if ( m_owner->m_backgroundBrush.Ok() )
266 dc.SetBackground( m_owner->m_backgroundBrush );
267 if ( m_owner->m_colourForeground.Ok() )
268 dc.SetTextForeground( m_owner->m_colourForeground );
269 if ( m_owner->m_colourBackground.Ok() )
270 dc.SetTextBackground( m_owner->m_colourBackground );
271
272 // mark the origin
273 dc.DrawCircle(0, 0, 10);
70bdacfc
MB
274#if !(defined __WXGTK__) && !(defined __WXMOTIF__)
275 // not implemented in wxGTK or wxMOTIF :-(
b62c3631 276 dc.FloodFill(0, 0, wxColour(255, 0, 0));
70bdacfc 277#endif //
b62c3631
RR
278
279 dc.DrawText( "This is text", 110, 10 );
280
95724b1a
VZ
281 dc.DrawRotatedText( "+90 rotated text", 30, 30, 90 );
282 dc.DrawRotatedText( "-90 rotated text", 30, 30, -90 );
283
196c87f4 284 dc.SetFont( wxFont( 18, wxSWISS, wxNORMAL, wxNORMAL ) );
c45a644e
RR
285
286 dc.DrawText( "This is Swiss 18pt text.", 110, 40 );
287
288 long length;
289 long height;
290 long descent;
291 dc.GetTextExtent( "This is Swiss 18pt text.", &length, &height, &descent );
292 wxString text;
293 text.Printf( "Dimensions are length %ld, height %ld, descent %ld", length, height, descent );
294 dc.DrawText( text, 110, 80 );
295
296 dc.DrawRectangle( 100, 40, 4, height );
297
298 text.Printf( "CharHeight() returns: %d", dc.GetCharHeight() );
299 dc.DrawText( text, 110, 120 );
300
301
302 dc.DrawIcon( wxICON(mondrian), 310, 40 );
b62c3631 303
c45a644e 304 DrawTestLines( 0, 200, 0, dc );
1e7fd311 305
c45a644e 306 DrawTestLines( 0, 400, 1, dc );
1e7fd311 307
c45a644e 308 DrawTestLines( 0, 600, 2, dc );
1e7fd311 309
c45a644e 310 DrawTestLines( 0, 800, 6, dc );
1e7fd311 311
b62c3631
RR
312}
313
bf0c00c6
RR
314void MyCanvas::OnMouseMove(wxMouseEvent &event)
315{
316 wxClientDC dc(this);
317 PrepareDC(dc);
318 m_owner->PrepareDC(dc);
319
320 wxPoint pos = event.GetPosition();
321 long x = dc.DeviceToLogicalX( pos.x );
322 long y = dc.DeviceToLogicalY( pos.y );
323 wxString str;
324 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
325 m_owner->SetStatusText( str );
326}
327
b62c3631
RR
328// ----------------------------------------------------------------------------
329// MyFrame
330// ----------------------------------------------------------------------------
331
332// the event tables connect the wxWindows events with the functions (event
333// handlers) which process them. It can be also done at run-time, but for the
334// simple menu events like this the static method is much simpler.
335BEGIN_EVENT_TABLE(MyFrame, wxFrame)
b62c3631
RR
336 EVT_MENU (Minimal_Quit, MyFrame::OnQuit)
337 EVT_MENU (Minimal_About, MyFrame::OnAbout)
338 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
339END_EVENT_TABLE()
340
aba99005
RR
341// frame constructor
342MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
343 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
344{
345 // set the frame icon
346 SetIcon(wxICON(mondrian));
347
348 wxMenu *menuFile = new wxMenu;
349 menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog");
350 menuFile->AppendSeparator();
351 menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
0f0c61d0 352
aba99005
RR
353 wxMenu *menuMapMode = new wxMenu;
354 menuMapMode->Append( MapMode_Text, "&TEXT map mode" );
355 menuMapMode->Append( MapMode_Lometric, "&LOMETRIC map mode" );
356 menuMapMode->Append( MapMode_Twips, "T&WIPS map mode" );
357 menuMapMode->Append( MapMode_Points, "&POINTS map mode" );
358 menuMapMode->Append( MapMode_Metric, "&METRIC map mode" );
0f0c61d0 359
aba99005 360 wxMenu *menuUserScale = new wxMenu;
ed43910d
RR
361 menuUserScale->Append( UserScale_StretchHoriz, "Stretch horizontally\tCtrl-H" );
362 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tCtrl-G" );
363 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-V" );
364 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-W" );
0f0c61d0
VZ
365 menuUserScale->AppendSeparator();
366 menuUserScale->Append( UserScale_Restore, "Restore to normal\tCtrl-0" );
367
aba99005 368 wxMenu *menuAxis = new wxMenu;
220af862
VZ
369 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-M", "", TRUE );
370 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-N", "", TRUE );
0f0c61d0 371
aba99005
RR
372 wxMenu *menuLogical = new wxMenu;
373 menuLogical->Append( LogicalOrigin_MoveDown, "Move &down\tCtrl-D" );
374 menuLogical->Append( LogicalOrigin_MoveUp, "Move &up\tCtrl-U" );
375 menuLogical->Append( LogicalOrigin_MoveLeft, "Move &right\tCtrl-L" );
376 menuLogical->Append( LogicalOrigin_MoveRight, "Move &left\tCtrl-R" );
377
0f0c61d0
VZ
378 wxMenu *menuColour = new wxMenu;
379 menuColour->Append( Colour_TextForeground, "Text foreground..." );
380 menuColour->Append( Colour_TextBackground, "Text background..." );
381 menuColour->Append( Colour_Background, "Background colour..." );
382 menuColour->Append( Colour_BackgroundMode, "Opaque/transparent\tCtrl-B", "", TRUE );
383
aba99005
RR
384 // now append the freshly created menu to the menu bar...
385 wxMenuBar *menuBar = new wxMenuBar;
386 menuBar->Append(menuFile, "&File");
387 menuBar->Append(menuMapMode, "&MapMode");
388 menuBar->Append(menuUserScale, "&UserScale");
389 menuBar->Append(menuAxis, "&Axis");
390 menuBar->Append(menuLogical, "&LogicalOrigin");
0f0c61d0 391 menuBar->Append(menuColour, "&Colours");
aba99005
RR
392
393 // ... and attach this menu bar to the frame
394 SetMenuBar(menuBar);
395
396 // create a status bar just for fun (by default with 1 pane only)
397 CreateStatusBar(2);
398 SetStatusText("Welcome to wxWindows!");
0f0c61d0 399
aba99005
RR
400 m_mapMode = wxMM_TEXT;
401 m_xUserScale = 1.0;
402 m_yUserScale = 1.0;
403 m_xLogicalOrigin = 0;
404 m_yLogicalOrigin = 0;
0f0c61d0
VZ
405 m_xAxisReversed =
406 m_yAxisReversed = FALSE;
407 m_backgroundMode = wxSOLID;
aba99005 408
b62c3631
RR
409 m_canvas = new MyCanvas( this );
410 m_canvas->SetScrollbars( 10, 10, 100, 200 );
411}
aba99005
RR
412
413// event handlers
414
415void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
416{
417 // TRUE is to force the frame to close
418 Close(TRUE);
419}
420
421void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
422{
423 wxString msg;
424 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
ed43910d 425 _T("Copyright (c) Robert Roebling 1999")
aba99005
RR
426 );
427
428 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
429}
430
431void MyFrame::OnOption(wxCommandEvent &event)
432{
433 switch (event.GetInt())
434 {
435 case MapMode_Text:
b9857632 436 m_mapMode = wxMM_TEXT;
0f0c61d0 437 break;
aba99005
RR
438 case MapMode_Lometric:
439 m_mapMode = wxMM_LOMETRIC;
0f0c61d0
VZ
440 break;
441 case MapMode_Twips:
aba99005 442 m_mapMode = wxMM_TWIPS;
0f0c61d0
VZ
443 break;
444 case MapMode_Points:
aba99005 445 m_mapMode = wxMM_POINTS;
0f0c61d0
VZ
446 break;
447 case MapMode_Metric:
aba99005 448 m_mapMode = wxMM_METRIC;
0f0c61d0
VZ
449 break;
450
451 case LogicalOrigin_MoveDown:
452 m_yLogicalOrigin += 10;
453 break;
454 case LogicalOrigin_MoveUp:
455 m_yLogicalOrigin -= 10;
456 break;
457 case LogicalOrigin_MoveLeft:
458 m_xLogicalOrigin += 10;
459 break;
460 case LogicalOrigin_MoveRight:
461 m_xLogicalOrigin -= 10;
462 break;
463
464 case UserScale_StretchHoriz:
465 m_xUserScale *= 1.10;
466 break;
467 case UserScale_ShrinkHoriz:
468 m_xUserScale /= 1.10;
469 break;
470 case UserScale_StretchVertic:
471 m_yUserScale *= 1.10;
472 break;
473 case UserScale_ShrinkVertic:
474 m_yUserScale /= 1.10;
475 break;
476 case UserScale_Restore:
477 m_xUserScale =
478 m_yUserScale = 1.0;
479 break;
480
481 case AxisMirror_Vertic:
482 m_yAxisReversed = !m_yAxisReversed;
483 break;
484 case AxisMirror_Horiz:
485 m_xAxisReversed = !m_xAxisReversed;
486 break;
487
488 case Colour_TextForeground:
489 m_colourForeground = SelectColour();
490 break;
491 case Colour_TextBackground:
492 m_colourBackground = SelectColour();
493 break;
494 case Colour_Background:
495 {
496 wxColour col = SelectColour();
497 if ( col.Ok() )
498 {
499 m_backgroundBrush.SetColour(col);
500 }
501 }
502 break;
503 case Colour_BackgroundMode:
504 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
505 : wxSOLID;
506 break;
507
508 default:
509 // skip Refresh()
510 return;
aba99005 511 }
0f0c61d0 512
1e7fd311 513 m_canvas->Refresh();
aba99005
RR
514}
515
220af862 516void MyFrame::PrepareDC(wxDC& dc)
aba99005 517{
0f0c61d0
VZ
518 dc.SetMapMode( m_mapMode );
519 dc.SetUserScale( m_xUserScale, m_yUserScale );
520 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
428db2ea 521 dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
220af862
VZ
522}
523
220af862 524wxColour MyFrame::SelectColour()
0f0c61d0
VZ
525{
526 wxColour col;
527 wxColourData data;
528 wxColourDialog dialog(this, &data);
529
530 if ( dialog.ShowModal() == wxID_OK )
531 {
428db2ea 532 col = dialog.GetColourData().GetColour();
0f0c61d0
VZ
533 }
534
535 return col;
aba99005 536}