]> git.saurik.com Git - wxWidgets.git/blame - samples/drawing/drawing.cpp
Using the best visual is now an option.
[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// ----------------------------------------------------------------------------
568708e2 19
aba99005 20#ifdef __GNUG__
0f0c61d0
VZ
21 #pragma implementation "drawing.cpp"
22 #pragma interface "drawing.cpp"
aba99005
RR
23#endif
24
25// For compilers that support precompilation, includes "wx/wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
29 #pragma hdrstop
30#endif
31
32// for all others, include the necessary headers (this file is usually all you
33// need because it includes almost all "standard" wxWindows headers
34#ifndef WX_PRECOMP
35 #include "wx/wx.h"
36#endif
37
0f0c61d0 38#include "wx/colordlg.h"
107a1787 39#include "wx/image.h"
0f0c61d0 40
aba99005
RR
41// ----------------------------------------------------------------------------
42// ressources
43// ----------------------------------------------------------------------------
568708e2 44
aba99005
RR
45// the application icon
46#if defined(__WXGTK__) || defined(__WXMOTIF__)
47 #include "mondrian.xpm"
48#endif
49
568708e2
VZ
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54// what do we show on screen (there are too many shapes to put them all on
55// screen simultaneously)
56enum ScreenToShow
57{
58 Show_Default,
59 Show_Text,
60 Show_Lines,
61 Show_Polygons,
62 Show_Mask
63};
64
65// ----------------------------------------------------------------------------
66// global variables
67// ----------------------------------------------------------------------------
68
69static wxBitmap gs_bmpNoMask,
70 gs_bmpWithColMask,
71 gs_bmpMask,
72 gs_bmpWithMask,
73 gs_bmp4,
74 gs_bmp36;
75
aba99005
RR
76// ----------------------------------------------------------------------------
77// private classes
78// ----------------------------------------------------------------------------
79
80// Define a new application type, each program should derive a class from wxApp
81class MyApp : public wxApp
82{
83public:
84 // override base class virtuals
85 // ----------------------------
86
87 // this one is called on application startup and is a good place for the app
88 // initialization (doing it here and not in the ctor allows to have an error
89 // return: if OnInit() returns false, the application terminates)
90 virtual bool OnInit();
568708e2
VZ
91
92protected:
93 bool LoadImages();
aba99005
RR
94};
95
b62c3631
RR
96class MyCanvas;
97
aba99005
RR
98// Define a new frame type: this is going to be our main frame
99class MyFrame : public wxFrame
100{
101public:
102 // ctor(s)
103 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
104
105 // event handlers (these functions should _not_ be virtual)
106 void OnQuit(wxCommandEvent& event);
107 void OnAbout(wxCommandEvent& event);
568708e2 108 void OnShow(wxCommandEvent &event);
aba99005 109 void OnOption(wxCommandEvent &event);
aba99005 110
220af862
VZ
111 wxColour SelectColour();
112 void PrepareDC(wxDC& dc);
0f0c61d0 113
b62c3631
RR
114 int m_backgroundMode;
115 int m_mapMode;
116 double m_xUserScale;
117 double m_yUserScale;
118 int m_xLogicalOrigin;
119 int m_yLogicalOrigin;
120 bool m_xAxisReversed,
121 m_yAxisReversed;
122 wxColour m_colourForeground, // these are _text_ colours
123 m_colourBackground;
124 wxBrush m_backgroundBrush;
125 MyCanvas *m_canvas;
aba99005
RR
126
127private:
128 // any class wishing to process wxWindows events must use this macro
129 DECLARE_EVENT_TABLE()
130};
131
b62c3631
RR
132// define a scrollable canvas for drawing onto
133class MyCanvas: public wxScrolledWindow
134{
135public:
136 MyCanvas( MyFrame *parent );
1e7fd311 137
b62c3631 138 void OnPaint(wxPaintEvent &event);
bf0c00c6 139 void OnMouseMove(wxMouseEvent &event);
4786aabd 140
568708e2
VZ
141 void Show(ScreenToShow show) { m_show = show; Refresh(); }
142
b62c3631 143protected:
568708e2
VZ
144 void DrawTestPoly( int x, int y, wxDC &dc ,int transparent );
145 void DrawTestLines( int x, int y, int width, wxDC &dc );
146 void DrawText(wxDC& dc);
147 void DrawImages(wxDC& dc);
148 void DrawDefault(wxDC& dc);
4786aabd 149
b62c3631 150private:
568708e2
VZ
151 MyFrame *m_owner;
152
153 ScreenToShow m_show;
154
b62c3631
RR
155 DECLARE_EVENT_TABLE()
156};
157
aba99005
RR
158// ----------------------------------------------------------------------------
159// constants
160// ----------------------------------------------------------------------------
161
162// IDs for the controls and the menu commands
163enum
164{
165 // menu items
568708e2
VZ
166 File_Quit = 1,
167 File_About,
168
169 MenuShow_First,
170 File_ShowDefault = MenuShow_First,
171 File_ShowText,
172 File_ShowLines,
173 File_ShowPolygons,
174 File_ShowMask,
175 MenuShow_Last = File_ShowMask,
0f0c61d0
VZ
176
177 MenuOption_First,
178
179 MapMode_Text = MenuOption_First,
aba99005
RR
180 MapMode_Lometric,
181 MapMode_Twips,
182 MapMode_Points,
183 MapMode_Metric,
0f0c61d0 184
aba99005
RR
185 UserScale_StretchHoriz,
186 UserScale_ShrinkHoriz,
187 UserScale_StretchVertic,
188 UserScale_ShrinkVertic,
0f0c61d0
VZ
189 UserScale_Restore,
190
aba99005
RR
191 AxisMirror_Horiz,
192 AxisMirror_Vertic,
0f0c61d0 193
aba99005
RR
194 LogicalOrigin_MoveDown,
195 LogicalOrigin_MoveUp,
196 LogicalOrigin_MoveLeft,
197 LogicalOrigin_MoveRight,
0f0c61d0
VZ
198
199 Colour_TextForeground,
200 Colour_TextBackground,
201 Colour_Background,
202 Colour_BackgroundMode,
203
204 MenuOption_Last = Colour_BackgroundMode
aba99005
RR
205};
206
207// ----------------------------------------------------------------------------
208// event tables and other macros for wxWindows
209// ----------------------------------------------------------------------------
210
aba99005
RR
211
212// Create a new application object: this macro will allow wxWindows to create
213// the application object during program execution (it's better than using a
214// static object for many reasons) and also declares the accessor function
215// wxGetApp() which will return the reference of the right type (i.e. MyApp and
216// not wxApp)
217IMPLEMENT_APP(MyApp)
218
219// ============================================================================
220// implementation
221// ============================================================================
222
223// ----------------------------------------------------------------------------
224// the application class
225// ----------------------------------------------------------------------------
226
568708e2
VZ
227bool MyApp::LoadImages()
228{
229 wxPathList pathList;
230 pathList.Add(".");
231 pathList.Add("..");
232
233 wxString path = pathList.FindValidPath("pat4.bmp");
234 if ( !path )
235 return FALSE;
236 gs_bmp4.LoadFile(path, wxBITMAP_TYPE_BMP);
237 wxMask* mask4 = new wxMask(gs_bmp4, *wxBLACK);
238 gs_bmp4.SetMask(mask4);
239
240 path = pathList.FindValidPath("pat36.bmp");
241 if ( !path )
242 return FALSE;
243 gs_bmp36.LoadFile(path, wxBITMAP_TYPE_BMP);
244 wxMask* mask36 = new wxMask(gs_bmp36, *wxBLACK);
245 gs_bmp36.SetMask(mask36);
246
247 path = pathList.FindValidPath("image.bmp");
248 if ( !path )
249 return FALSE;
250 gs_bmpNoMask.LoadFile(path, wxBITMAP_TYPE_BMP);
251 gs_bmpWithMask.LoadFile(path, wxBITMAP_TYPE_BMP);
252 gs_bmpWithColMask.LoadFile(path, wxBITMAP_TYPE_BMP);
253
254 path = pathList.FindValidPath("mask.bmp");
255 if ( !path )
256 return FALSE;
257 gs_bmpMask.LoadFile(path, wxBITMAP_TYPE_BMP);
b9de1315 258
ff7c6c9c
RR
259// This is so wrong, it hurts.
260// gs_bmpMask.SetDepth(1);
261// wxMask *mask = new wxMask(gs_bmpMask);
568708e2 262
ff7c6c9c 263 wxMask *mask = new wxMask(gs_bmpMask, *wxBLACK);
568708e2
VZ
264 gs_bmpWithMask.SetMask(mask);
265
266 mask = new wxMask(gs_bmpWithColMask, *wxWHITE);
267 gs_bmpWithColMask.SetMask(mask);
268
269 return TRUE;
270}
271
aba99005
RR
272// `Main program' equivalent: the program execution "starts" here
273bool MyApp::OnInit()
274{
275 // Create the main application window
ed43910d 276 MyFrame *frame = new MyFrame("Drawing sample",
b62c3631 277 wxPoint(50, 50), wxSize(550, 340));
aba99005
RR
278
279 // Show it and tell the application that it's our main window
aba99005
RR
280 frame->Show(TRUE);
281 SetTopWindow(frame);
282
568708e2
VZ
283 if ( !LoadImages() )
284 {
285 wxLogError("Can't load one of the bitmap files needed for this sample "
286 "from the current or parent directory, please copy them "
287 "there.");
288
289 // stop here
290 return FALSE;
291 }
292
293 // ok, continue
aba99005
RR
294 return TRUE;
295}
296
297// ----------------------------------------------------------------------------
b62c3631 298// MyCanvas
aba99005
RR
299// ----------------------------------------------------------------------------
300
b62c3631
RR
301// the event tables connect the wxWindows events with the functions (event
302// handlers) which process them.
303BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
304 EVT_PAINT (MyCanvas::OnPaint)
bf0c00c6 305 EVT_MOTION (MyCanvas::OnMouseMove)
b62c3631
RR
306END_EVENT_TABLE()
307
b97fa7cf 308MyCanvas::MyCanvas( MyFrame *parent ) : wxScrolledWindow( parent )
b62c3631 309{
b97fa7cf 310 m_owner = parent;
568708e2 311 m_show = Show_Default;
b97fa7cf
VZ
312}
313
314//draw a polygon and an overlapping rectangle
315//is transparent is 1, the fill pattern are made transparent
316//is transparent is 2, the fill pattern are made transparent but inversed
317//is transparent is 0 the text for and background color will be used to represent/map
318//the colors of the monochrome bitmap pixels to the fillpattern
319//
320//i miss_used the the menu items for setting so called back and fore ground color
321//just to show how the those colors do influence the fillpatterns
322//just play with those,
323//and with the code
324//variations are endless using other logical functions
325void MyCanvas::DrawTestPoly( int x, int y,wxDC &dc,int transparent )
326{
568708e2
VZ
327 wxBrush* brush4 = new wxBrush(gs_bmp4);
328 wxBrush* brush36 = new wxBrush(gs_bmp36);
b97fa7cf
VZ
329
330 wxPoint todraw[5];
331 todraw[0].x=(long)x+100;
332 todraw[0].y=(long)y+100;
333 todraw[1].x=(long)x+300;
334 todraw[1].y=(long)y+100;
335 todraw[2].x=(long)x+300;
336 todraw[2].y=(long)y+300;
337 todraw[3].x=(long)x+150;
338 todraw[3].y=(long)y+350;
339 todraw[4].x=(long)x+100;
340 todraw[4].y=(long)y+300;
341
342 wxPoint todraw2[5];
343 todraw2[0].x=100;
344 todraw2[0].y=100;
345 todraw2[1].x=300;
346 todraw2[1].y=100;
347 todraw2[2].x=300;
348 todraw2[2].y=300;
349 todraw2[3].x=150;
350 todraw2[3].y=350;
351 todraw2[4].x=100;
352 todraw2[4].y=300;
353
354 switch (transparent)
355 {
356 case 0:
357 {
358 dc.SetPen( wxPen( "black", 4, wxSOLID) );
568708e2 359 dc.SetBrush( *brush4 );
b97fa7cf 360 dc.SetTextForeground(*wxGREEN);
b97fa7cf
VZ
361 dc.SetTextBackground(m_owner->m_colourForeground);
362 dc.SetLogicalFunction(wxCOPY);
363 dc.DrawPolygon(5,todraw,0,0,wxWINDING_RULE);
364
365 //don't understand hwo but the outline is also depending on logicalfunction
366 dc.SetPen( wxPen( "red", 4, wxSOLID) );
568708e2 367 dc.SetBrush( *brush36 );
b97fa7cf 368 dc.SetTextForeground(*wxCYAN);
b97fa7cf
VZ
369 dc.SetTextBackground(m_owner->m_colourBackground);
370 dc.SetLogicalFunction(wxCOPY);
371 dc.DrawRectangle( x+10, y+10, 200, 200 );
372 dc.SetBrush(wxNullBrush);
373 dc.SetPen(wxNullPen);
374 break;
375 }
376 case 1: //now with transparent fillpatterns
377 {
378
568708e2
VZ
379 wxBitmap* bmpBlit = new wxBitmap(600,400);
380 wxMemoryDC* memDC= new wxMemoryDC();
b97fa7cf
VZ
381 // wxBrush _clearbrush(*wxGREEN,wxSOLID);
382 wxBrush _clearbrush(*wxBLACK,wxSOLID);
568708e2
VZ
383 memDC->SelectObject(*bmpBlit);
384 memDC->BeginDrawing();
385 memDC->SetBackground(_clearbrush);
386 memDC->Clear();
387 memDC->SetBackground(wxNullBrush);
388
389 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
390 memDC->SetBrush( wxNullBrush);
391 memDC->SetBrush( *brush4 );
392 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
393 memDC->SetTextBackground(*wxWHITE); // 1s --> 0xFFFFFF (white)
394 memDC->SetLogicalFunction(wxAND_INVERT);
b97fa7cf
VZ
395
396 // BLACK OUT the opaque pixels and leave the rest as is
568708e2 397 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
b97fa7cf
VZ
398
399 // Set background and foreground colors for fill pattern
400 //the previous blacked out pixels are now merged with the layer color
401 //while the non blacked out pixels stay as they are.
568708e2 402 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
b97fa7cf
VZ
403
404 //now define what will be the color of the fillpattern parts that are not transparent
568708e2
VZ
405 // memDC->SetTextBackground(*wxBLUE);
406 memDC->SetTextBackground(m_owner->m_colourForeground);
407 memDC->SetLogicalFunction(wxOR);
b97fa7cf
VZ
408
409
410 //don't understand how but the outline is also depending on logicalfunction
568708e2
VZ
411 memDC->SetPen( wxPen( "red", 4, wxSOLID) );
412 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
b97fa7cf 413
568708e2 414 memDC->SetLogicalFunction(wxCOPY);
b97fa7cf 415
568708e2
VZ
416 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
417 memDC->SetBrush( wxNullBrush);
418 memDC->SetBrush( *brush36 );
419 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
420 memDC->SetTextBackground(*wxWHITE); // 1s --> 0xFFFFFF (white)
421 memDC->SetLogicalFunction(wxAND_INVERT);
b97fa7cf 422
568708e2 423 memDC->DrawRectangle( 10, 10, 200, 200 );
b97fa7cf
VZ
424
425 // Set background and foreground colors for fill pattern
426 //the previous blacked out pixels are now merged with the layer color
427 //while the non blacked out pixels stay as they are.
568708e2 428 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
b97fa7cf 429 //now define what will be the color of the fillpattern parts that are not transparent
568708e2
VZ
430 // memDC->SetTextBackground(*wxRED);
431 memDC->SetTextBackground(m_owner->m_colourBackground);
432 memDC->SetLogicalFunction(wxOR);
b97fa7cf
VZ
433
434 //don't understand how but the outline is also depending on logicalfunction
568708e2
VZ
435 memDC->SetPen( wxPen( "yellow", 4, wxSOLID) );
436 memDC->DrawRectangle( 10, 10, 200, 200 );
b97fa7cf 437
568708e2
VZ
438 memDC->SetBrush(wxNullBrush);
439 memDC->SetPen(wxNullPen);
b97fa7cf 440
568708e2
VZ
441 memDC->EndDrawing();
442 dc.Blit(x,y,600,400,memDC,0,0,wxCOPY);
443 delete bmpBlit;
444 delete memDC;
b97fa7cf
VZ
445 break;
446 }
447 case 2: //now with transparent inversed fillpatterns
448 {
568708e2
VZ
449 wxBitmap* bmpBlit = new wxBitmap(600,400);
450 wxMemoryDC* memDC= new wxMemoryDC();
b97fa7cf 451 wxBrush _clearbrush(*wxWHITE,wxSOLID);
568708e2
VZ
452 memDC->SelectObject(*bmpBlit);
453 memDC->BeginDrawing();
454 memDC->SetBackground(_clearbrush);
455 memDC->Clear();
456 memDC->SetBackground(wxNullBrush);
457
458 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
459 memDC->SetBrush( *brush4 );
460 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
461 memDC->SetTextForeground(*wxWHITE); // 1s --> 0xFFFFFF (white)
462 memDC->SetLogicalFunction(wxAND_INVERT);
b97fa7cf
VZ
463
464 // BLACK OUT the opaque pixels and leave the rest as is
568708e2 465 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
b97fa7cf
VZ
466
467 // Set background and foreground colors for fill pattern
468 //the previous blacked out pixels are now merged with the layer color
469 //while the non blacked out pixels stay as they are.
568708e2 470 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
b97fa7cf
VZ
471
472 //now define what will be the color of the fillpattern parts that are not transparent
568708e2
VZ
473 memDC->SetTextForeground(m_owner->m_colourForeground);
474 memDC->SetLogicalFunction(wxOR);
b97fa7cf
VZ
475
476
477 //don't understand how but the outline is also depending on logicalfunction
568708e2
VZ
478 memDC->SetPen( wxPen( "red", 4, wxSOLID) );
479 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
b97fa7cf 480
568708e2 481 memDC->SetLogicalFunction(wxCOPY);
b97fa7cf 482
568708e2
VZ
483 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
484 memDC->SetBrush( *brush36 );
485 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
486 memDC->SetTextForeground(*wxWHITE); // 1s --> 0xFFFFFF (white)
487 memDC->SetLogicalFunction(wxAND_INVERT);
b97fa7cf 488
568708e2 489 memDC->DrawRectangle( 10,10, 200, 200 );
b97fa7cf
VZ
490
491 // Set background and foreground colors for fill pattern
492 //the previous blacked out pixels are now merged with the layer color
493 //while the non blacked out pixels stay as they are.
568708e2 494 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
b97fa7cf 495 //now define what will be the color of the fillpattern parts that are not transparent
568708e2
VZ
496 memDC->SetTextForeground(m_owner->m_colourBackground);
497 memDC->SetLogicalFunction(wxOR);
b97fa7cf
VZ
498
499 //don't understand how but the outline is also depending on logicalfunction
568708e2
VZ
500 memDC->SetPen( wxPen( "yellow", 4, wxSOLID) );
501 memDC->DrawRectangle( 10, 10, 200, 200 );
502
503 memDC->SetBrush(wxNullBrush);
504 memDC->SetPen(wxNullPen);
505 dc.Blit(x,y,600,400,memDC,0,0,wxCOPY);
506 delete bmpBlit;
507 delete memDC;
b97fa7cf
VZ
508 }
509 }
510
568708e2
VZ
511 delete brush4;
512 delete brush36;
b62c3631
RR
513}
514
1e7fd311 515void MyCanvas::DrawTestLines( int x, int y, int width, wxDC &dc )
b62c3631 516{
1e7fd311
RR
517 dc.SetPen( wxPen( "black", width, wxSOLID) );
518 dc.SetBrush( *wxRED_BRUSH );
9a8c7620 519 dc.DrawRectangle( x+10, y+10, 100, 190 );
4786aabd 520
1e7fd311 521 dc.SetPen( wxPen( "black", width, wxSOLID) );
696e1ea0 522 dc.DrawLine( x+20, y+20, 100, y+20 );
1e7fd311 523 dc.SetPen( wxPen( "black", width, wxDOT) );
696e1ea0 524 dc.DrawLine( x+20, y+30, 100, y+30 );
1e7fd311 525 dc.SetPen( wxPen( "black", width, wxSHORT_DASH) );
696e1ea0 526 dc.DrawLine( x+20, y+40, 100, y+40 );
1e7fd311 527 dc.SetPen( wxPen( "black", width, wxLONG_DASH) );
696e1ea0 528 dc.DrawLine( x+20, y+50, 100, y+50 );
1e7fd311 529 dc.SetPen( wxPen( "black", width, wxDOT_DASH) );
696e1ea0 530 dc.DrawLine( x+20, y+60, 100, y+60 );
1e7fd311
RR
531
532 dc.SetPen( wxPen( "black", width, wxBDIAGONAL_HATCH) );
696e1ea0 533 dc.DrawLine( x+20, y+70, 100, y+70 );
1e7fd311 534 dc.SetPen( wxPen( "black", width, wxCROSSDIAG_HATCH) );
696e1ea0 535 dc.DrawLine( x+20, y+80, 100, y+80 );
1e7fd311 536 dc.SetPen( wxPen( "black", width, wxFDIAGONAL_HATCH) );
696e1ea0 537 dc.DrawLine( x+20, y+90, 100, y+90 );
1e7fd311 538 dc.SetPen( wxPen( "black", width, wxCROSS_HATCH) );
696e1ea0 539 dc.DrawLine( x+20, y+100, 100, y+100 );
1e7fd311 540 dc.SetPen( wxPen( "black", width, wxHORIZONTAL_HATCH) );
696e1ea0 541 dc.DrawLine( x+20, y+110, 100, y+110 );
1e7fd311 542 dc.SetPen( wxPen( "black", width, wxVERTICAL_HATCH) );
696e1ea0 543 dc.DrawLine( x+20, y+120, 100, y+120 );
1e7fd311
RR
544
545 wxPen ud( "black", width, wxUSER_DASH );
546 wxDash dash1[1];
547 dash1[0] = 0;
548 ud.SetDashes( 1, dash1 );
696e1ea0 549 dc.DrawLine( x+20, y+140, 100, y+140 );
1e7fd311
RR
550 dash1[0] = 1;
551 ud.SetDashes( 1, dash1 );
696e1ea0 552 dc.DrawLine( x+20, y+150, 100, y+150 );
1e7fd311
RR
553 dash1[0] = 2;
554 ud.SetDashes( 1, dash1 );
696e1ea0 555 dc.DrawLine( x+20, y+160, 100, y+160 );
1e7fd311
RR
556 dash1[0] = 0xFF;
557 ud.SetDashes( 1, dash1 );
696e1ea0 558 dc.DrawLine( x+20, y+170, 100, y+170 );
1e7fd311 559
b62c3631
RR
560}
561
568708e2 562void MyCanvas::DrawDefault(wxDC& dc)
b62c3631 563{
b62c3631
RR
564 // mark the origin
565 dc.DrawCircle(0, 0, 10);
70bdacfc
MB
566#if !(defined __WXGTK__) && !(defined __WXMOTIF__)
567 // not implemented in wxGTK or wxMOTIF :-(
b62c3631 568 dc.FloodFill(0, 0, wxColour(255, 0, 0));
70bdacfc 569#endif //
b62c3631 570
568708e2
VZ
571 dc.DrawIcon( wxICON(mondrian), 410, 40 );
572
107a1787 573
ff7c6c9c 574 dc.SetBrush( *wxBLACK_BRUSH );
568708e2 575 dc.SetPen(*wxTRANSPARENT_PEN);
107a1787
RR
576 dc.DrawRectangle( 0, 160, 1000, 300 );
577
578 // draw lines
579 wxBitmap bitmap(20,70);
580 wxMemoryDC memdc;
581 memdc.SelectObject( bitmap );
582 memdc.SetBrush( *wxBLACK_BRUSH );
583 memdc.SetPen( *wxWHITE_PEN );
584 memdc.DrawRectangle(0,0,20,70);
585 memdc.DrawLine( 10,0,10,70 );
586
d4aa3a4b 587 // to the right
4c45f240
VZ
588 wxPen pen = *wxRED_PEN;
589 pen.SetWidth(2);
590 memdc.SetPen(pen);
107a1787
RR
591 memdc.DrawLine( 10, 5,10, 5 );
592 memdc.DrawLine( 10,10,11,10 );
593 memdc.DrawLine( 10,15,12,15 );
594 memdc.DrawLine( 10,20,13,20 );
595
d4aa3a4b
RR
596/*
597 memdc.SetPen(*wxRED_PEN);
107a1787
RR
598 memdc.DrawLine( 12, 5,12, 5 );
599 memdc.DrawLine( 12,10,13,10 );
600 memdc.DrawLine( 12,15,14,15 );
601 memdc.DrawLine( 12,20,15,20 );
d4aa3a4b 602*/
107a1787
RR
603
604 // same to the left
605 memdc.DrawLine( 10,25,10,25 );
606 memdc.DrawLine( 10,30, 9,30 );
607 memdc.DrawLine( 10,35, 8,35 );
608 memdc.DrawLine( 10,40, 7,40 );
609
610 // XOR draw lines
d4aa3a4b 611 dc.SetPen(*wxWHITE_PEN);
107a1787
RR
612 memdc.SetLogicalFunction( wxINVERT );
613 memdc.SetPen( *wxWHITE_PEN );
614 memdc.DrawLine( 10,50,10,50 );
615 memdc.DrawLine( 10,55,11,55 );
616 memdc.DrawLine( 10,60,12,60 );
617 memdc.DrawLine( 10,65,13,65 );
618
619 memdc.DrawLine( 12,50,12,50 );
620 memdc.DrawLine( 12,55,13,55 );
621 memdc.DrawLine( 12,60,14,60 );
622 memdc.DrawLine( 12,65,15,65 );
623
624 memdc.SelectObject( wxNullBitmap );
625 dc.DrawBitmap( bitmap, 10, 170 );
626 wxImage image( bitmap );
627 image.Rescale( 60,210 );
628 bitmap = image.ConvertToBitmap();
629 dc.DrawBitmap( bitmap, 50, 170 );
630
631
b9de1315 632 // test the rectangle outline drawing - there should be one pixel between
ff7c6c9c
RR
633 // the rect and the lines
634 dc.SetPen(*wxWHITE_PEN);
635 dc.SetBrush( *wxTRANSPARENT_BRUSH );
ff7c6c9c 636 dc.DrawRectangle(150, 170, 49, 29);
107a1787 637 dc.DrawRectangle(200, 170, 49, 29);
ff7c6c9c 638 dc.SetPen(*wxWHITE_PEN);
107a1787
RR
639 dc.DrawLine(250, 210, 250, 170);
640 dc.DrawLine(260, 200, 150, 200);
b9de1315
VZ
641
642 // test the rectangle filled drawing - there should be one pixel between
ff7c6c9c
RR
643 // the rect and the lines
644 dc.SetPen(*wxTRANSPARENT_PEN);
645 dc.SetBrush( *wxWHITE_BRUSH );
646 dc.DrawRectangle(300, 170, 49, 29);
568708e2 647 dc.DrawRectangle(350, 170, 49, 29);
ff7c6c9c 648 dc.SetPen(*wxWHITE_PEN);
b9de1315
VZ
649 dc.DrawLine(400, 170, 400, 210);
650 dc.DrawLine(300, 200, 410, 200);
651
652 // and now for filled rect with outline
653 dc.SetPen(*wxRED_PEN);
654 dc.SetBrush( *wxWHITE_BRUSH );
655 dc.DrawRectangle(500, 170, 49, 29);
656 dc.DrawRectangle(550, 170, 49, 29);
657 dc.SetPen(*wxWHITE_PEN);
658 dc.DrawLine(600, 170, 600, 210);
659 dc.DrawLine(500, 200, 610, 200);
660
661 // test the rectangle outline drawing - there should be one pixel between
ff7c6c9c
RR
662 // the rect and the lines
663 dc.SetPen(*wxWHITE_PEN);
664 dc.SetBrush( *wxTRANSPARENT_BRUSH );
ff7c6c9c 665 dc.DrawRoundedRectangle(150, 270, 49, 29, 6);
107a1787 666 dc.DrawRoundedRectangle(200, 270, 49, 29, 6);
ff7c6c9c 667 dc.SetPen(*wxWHITE_PEN);
107a1787
RR
668 dc.DrawLine(250, 270, 250, 310);
669 dc.DrawLine(150, 300, 260, 300);
b9de1315
VZ
670
671 // test the rectangle filled drawing - there should be one pixel between
ff7c6c9c
RR
672 // the rect and the lines
673 dc.SetPen(*wxTRANSPARENT_PEN);
674 dc.SetBrush( *wxWHITE_BRUSH );
675 dc.DrawRoundedRectangle(300, 270, 49, 29, 6);
676 dc.DrawRoundedRectangle(350, 270, 49, 29, 6);
677 dc.SetPen(*wxWHITE_PEN);
b9de1315
VZ
678 dc.DrawLine(400, 270, 400, 310);
679 dc.DrawLine(300, 300, 410, 300);
ff7c6c9c 680
568708e2
VZ
681}
682
683void MyCanvas::DrawText(wxDC& dc)
684{
9a8c7620
VZ
685 // set underlined font for testing
686 dc.SetFont( wxFont(12, wxMODERN, wxNORMAL, wxNORMAL, TRUE) );
b62c3631 687 dc.DrawText( "This is text", 110, 10 );
4786aabd 688 dc.DrawRotatedText( "That is text", 20, 10, -45 );
9a8c7620
VZ
689
690 dc.SetFont( *wxNORMAL_FONT );
b62c3631 691
696e1ea0 692 wxString text;
9a8c7620 693 dc. SetBackgroundMode(wxTRANSPARENT);
696e1ea0
VZ
694
695 for ( int n = -180; n < 180; n += 30 )
696 {
697 text.Printf(" %d rotated text", n);
698 dc.DrawRotatedText(text , 400, 400, n);
699 }
95724b1a 700
196c87f4 701 dc.SetFont( wxFont( 18, wxSWISS, wxNORMAL, wxNORMAL ) );
c45a644e
RR
702
703 dc.DrawText( "This is Swiss 18pt text.", 110, 40 );
704
705 long length;
706 long height;
707 long descent;
708 dc.GetTextExtent( "This is Swiss 18pt text.", &length, &height, &descent );
c45a644e
RR
709 text.Printf( "Dimensions are length %ld, height %ld, descent %ld", length, height, descent );
710 dc.DrawText( text, 110, 80 );
711
c45a644e
RR
712 text.Printf( "CharHeight() returns: %d", dc.GetCharHeight() );
713 dc.DrawText( text, 110, 120 );
714
568708e2
VZ
715 dc.DrawRectangle( 100, 40, 4, height );
716}
717
718void MyCanvas::DrawImages(wxDC& dc)
719{
720 static const struct
721 {
722 const wxChar *name;
723 int rop;
724 } rasterOperations[] =
725 {
726 { "wxAND", wxAND },
727 { "wxAND_INVERT", wxAND_INVERT },
728 { "wxAND_REVERSE", wxAND_REVERSE },
729 { "wxCLEAR", wxCLEAR },
730 { "wxCOPY", wxCOPY },
731 { "wxEQUIV", wxEQUIV },
732 { "wxINVERT", wxINVERT },
733 { "wxNAND", wxNAND },
734 { "wxNO_OP", wxNO_OP },
735 { "wxOR", wxOR },
736 { "wxOR_INVERT", wxOR_INVERT },
737 { "wxOR_REVERSE", wxOR_REVERSE },
738 { "wxSET", wxSET },
739 { "wxSRC_INVERT", wxSRC_INVERT },
740 { "wxXOR", wxXOR },
741 };
742
743 dc.DrawText("original image", 0, 0);
744 dc.DrawBitmap(gs_bmpNoMask, 0, 20, 0);
745 dc.DrawText("with colour mask", 0, 100);
746 dc.DrawBitmap(gs_bmpWithColMask, 0, 120, TRUE);
747 dc.DrawText("the mask image", 0, 200);
748 dc.DrawBitmap(gs_bmpMask, 0, 220, 0);
749 dc.DrawText("masked image", 0, 300);
750 dc.DrawBitmap(gs_bmpWithMask, 0, 320, TRUE);
751
752 int cx = gs_bmpWithColMask.GetWidth(),
753 cy = gs_bmpWithColMask.GetHeight();
754
755 wxMemoryDC memDC;
756 for ( size_t n = 0; n < WXSIZEOF(rasterOperations); n++ )
757 {
758 wxCoord x = 120 + 150*(n%4),
759 y = 20 + 100*(n/4);
4786aabd 760
568708e2
VZ
761 dc.DrawText(rasterOperations[n].name, x, y - 20);
762 memDC.SelectObject(gs_bmpWithColMask);
763 dc.Blit(x, y, cx, cy, &memDC, 0, 0, rasterOperations[n].rop, TRUE);
764 }
765}
766
767void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
768{
769 wxPaintDC dc(this);
770 PrepareDC(dc);
771 m_owner->PrepareDC(dc);
772
773 dc.SetBackgroundMode( m_owner->m_backgroundMode );
774 if ( m_owner->m_backgroundBrush.Ok() )
775 dc.SetBackground( m_owner->m_backgroundBrush );
776 if ( m_owner->m_colourForeground.Ok() )
777 dc.SetTextForeground( m_owner->m_colourForeground );
778 if ( m_owner->m_colourBackground.Ok() )
779 dc.SetTextBackground( m_owner->m_colourBackground );
4786aabd 780
568708e2
VZ
781 switch ( m_show )
782 {
783 case Show_Default:
784 DrawDefault(dc);
785 break;
1e7fd311 786
568708e2
VZ
787 case Show_Text:
788 DrawText(dc);
789 break;
1e7fd311 790
568708e2
VZ
791 case Show_Lines:
792 DrawTestLines( 0, 100, 0, dc );
793 DrawTestLines( 0, 300, 1, dc );
794 DrawTestLines( 0, 500, 2, dc );
795 DrawTestLines( 0, 700, 6, dc );
796 break;
1e7fd311 797
568708e2
VZ
798 case Show_Polygons:
799 DrawTestPoly( 0, 100, dc, 0 );
800 DrawTestPoly( 33, 500, dc, 1 );
801 DrawTestPoly( 43, 1000, dc, 2 );
802 break;
1e7fd311 803
568708e2
VZ
804 case Show_Mask:
805 DrawImages(dc);
806 break;
807 }
b62c3631
RR
808}
809
bf0c00c6
RR
810void MyCanvas::OnMouseMove(wxMouseEvent &event)
811{
812 wxClientDC dc(this);
813 PrepareDC(dc);
814 m_owner->PrepareDC(dc);
815
816 wxPoint pos = event.GetPosition();
817 long x = dc.DeviceToLogicalX( pos.x );
818 long y = dc.DeviceToLogicalY( pos.y );
819 wxString str;
820 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
821 m_owner->SetStatusText( str );
822}
823
b62c3631
RR
824// ----------------------------------------------------------------------------
825// MyFrame
826// ----------------------------------------------------------------------------
827
828// the event tables connect the wxWindows events with the functions (event
829// handlers) which process them. It can be also done at run-time, but for the
830// simple menu events like this the static method is much simpler.
831BEGIN_EVENT_TABLE(MyFrame, wxFrame)
568708e2
VZ
832 EVT_MENU (File_Quit, MyFrame::OnQuit)
833 EVT_MENU (File_About, MyFrame::OnAbout)
834
835 EVT_MENU_RANGE(MenuShow_First, MenuShow_Last, MyFrame::OnShow)
836
b62c3631
RR
837 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
838END_EVENT_TABLE()
839
aba99005
RR
840// frame constructor
841MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
842 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
843{
844 // set the frame icon
845 SetIcon(wxICON(mondrian));
846
847 wxMenu *menuFile = new wxMenu;
568708e2
VZ
848 menuFile->Append(File_ShowDefault, "&Default screen\tF1");
849 menuFile->Append(File_ShowText, "&Text screen\tF2");
850 menuFile->Append(File_ShowLines, "&Lines screen\tF3");
851 menuFile->Append(File_ShowPolygons, "&Polygons screen\tF4");
852 menuFile->Append(File_ShowMask, "wx&Mask screen\tF5");
853 menuFile->AppendSeparator();
854 menuFile->Append(File_About, "&About...\tCtrl-A", "Show about dialog");
aba99005 855 menuFile->AppendSeparator();
568708e2 856 menuFile->Append(File_Quit, "E&xit\tAlt-X", "Quit this program");
0f0c61d0 857
aba99005
RR
858 wxMenu *menuMapMode = new wxMenu;
859 menuMapMode->Append( MapMode_Text, "&TEXT map mode" );
860 menuMapMode->Append( MapMode_Lometric, "&LOMETRIC map mode" );
861 menuMapMode->Append( MapMode_Twips, "T&WIPS map mode" );
862 menuMapMode->Append( MapMode_Points, "&POINTS map mode" );
863 menuMapMode->Append( MapMode_Metric, "&METRIC map mode" );
0f0c61d0 864
aba99005 865 wxMenu *menuUserScale = new wxMenu;
ed43910d
RR
866 menuUserScale->Append( UserScale_StretchHoriz, "Stretch horizontally\tCtrl-H" );
867 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tCtrl-G" );
868 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-V" );
869 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-W" );
0f0c61d0
VZ
870 menuUserScale->AppendSeparator();
871 menuUserScale->Append( UserScale_Restore, "Restore to normal\tCtrl-0" );
872
aba99005 873 wxMenu *menuAxis = new wxMenu;
220af862
VZ
874 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-M", "", TRUE );
875 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-N", "", TRUE );
0f0c61d0 876
aba99005
RR
877 wxMenu *menuLogical = new wxMenu;
878 menuLogical->Append( LogicalOrigin_MoveDown, "Move &down\tCtrl-D" );
879 menuLogical->Append( LogicalOrigin_MoveUp, "Move &up\tCtrl-U" );
880 menuLogical->Append( LogicalOrigin_MoveLeft, "Move &right\tCtrl-L" );
881 menuLogical->Append( LogicalOrigin_MoveRight, "Move &left\tCtrl-R" );
882
0f0c61d0
VZ
883 wxMenu *menuColour = new wxMenu;
884 menuColour->Append( Colour_TextForeground, "Text foreground..." );
885 menuColour->Append( Colour_TextBackground, "Text background..." );
886 menuColour->Append( Colour_Background, "Background colour..." );
887 menuColour->Append( Colour_BackgroundMode, "Opaque/transparent\tCtrl-B", "", TRUE );
888
aba99005
RR
889 // now append the freshly created menu to the menu bar...
890 wxMenuBar *menuBar = new wxMenuBar;
891 menuBar->Append(menuFile, "&File");
892 menuBar->Append(menuMapMode, "&MapMode");
893 menuBar->Append(menuUserScale, "&UserScale");
894 menuBar->Append(menuAxis, "&Axis");
895 menuBar->Append(menuLogical, "&LogicalOrigin");
0f0c61d0 896 menuBar->Append(menuColour, "&Colours");
aba99005
RR
897
898 // ... and attach this menu bar to the frame
899 SetMenuBar(menuBar);
900
901 // create a status bar just for fun (by default with 1 pane only)
902 CreateStatusBar(2);
903 SetStatusText("Welcome to wxWindows!");
0f0c61d0 904
aba99005
RR
905 m_mapMode = wxMM_TEXT;
906 m_xUserScale = 1.0;
907 m_yUserScale = 1.0;
908 m_xLogicalOrigin = 0;
909 m_yLogicalOrigin = 0;
0f0c61d0
VZ
910 m_xAxisReversed =
911 m_yAxisReversed = FALSE;
912 m_backgroundMode = wxSOLID;
b97fa7cf
VZ
913 m_colourForeground = *wxRED;
914 m_colourBackground = *wxBLUE;
aba99005 915
b62c3631 916 m_canvas = new MyCanvas( this );
b97fa7cf 917 m_canvas->SetScrollbars( 10, 10, 100, 240 );
b62c3631 918}
aba99005
RR
919
920// event handlers
921
922void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
923{
924 // TRUE is to force the frame to close
925 Close(TRUE);
926}
927
928void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
929{
930 wxString msg;
931 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
ed43910d 932 _T("Copyright (c) Robert Roebling 1999")
aba99005
RR
933 );
934
935 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
936}
937
568708e2
VZ
938void MyFrame::OnShow(wxCommandEvent& event)
939{
940 m_canvas->Show((ScreenToShow)(event.GetInt() - MenuShow_First));
941}
942
943void MyFrame::OnOption(wxCommandEvent& event)
aba99005
RR
944{
945 switch (event.GetInt())
946 {
947 case MapMode_Text:
b9857632 948 m_mapMode = wxMM_TEXT;
0f0c61d0 949 break;
aba99005
RR
950 case MapMode_Lometric:
951 m_mapMode = wxMM_LOMETRIC;
0f0c61d0
VZ
952 break;
953 case MapMode_Twips:
aba99005 954 m_mapMode = wxMM_TWIPS;
0f0c61d0
VZ
955 break;
956 case MapMode_Points:
aba99005 957 m_mapMode = wxMM_POINTS;
0f0c61d0
VZ
958 break;
959 case MapMode_Metric:
aba99005 960 m_mapMode = wxMM_METRIC;
0f0c61d0
VZ
961 break;
962
963 case LogicalOrigin_MoveDown:
964 m_yLogicalOrigin += 10;
965 break;
966 case LogicalOrigin_MoveUp:
967 m_yLogicalOrigin -= 10;
968 break;
969 case LogicalOrigin_MoveLeft:
970 m_xLogicalOrigin += 10;
971 break;
972 case LogicalOrigin_MoveRight:
973 m_xLogicalOrigin -= 10;
974 break;
975
976 case UserScale_StretchHoriz:
977 m_xUserScale *= 1.10;
978 break;
979 case UserScale_ShrinkHoriz:
980 m_xUserScale /= 1.10;
981 break;
982 case UserScale_StretchVertic:
983 m_yUserScale *= 1.10;
984 break;
985 case UserScale_ShrinkVertic:
986 m_yUserScale /= 1.10;
987 break;
988 case UserScale_Restore:
989 m_xUserScale =
990 m_yUserScale = 1.0;
991 break;
992
993 case AxisMirror_Vertic:
994 m_yAxisReversed = !m_yAxisReversed;
995 break;
996 case AxisMirror_Horiz:
997 m_xAxisReversed = !m_xAxisReversed;
998 break;
999
1000 case Colour_TextForeground:
1001 m_colourForeground = SelectColour();
1002 break;
1003 case Colour_TextBackground:
1004 m_colourBackground = SelectColour();
1005 break;
1006 case Colour_Background:
1007 {
1008 wxColour col = SelectColour();
1009 if ( col.Ok() )
1010 {
1011 m_backgroundBrush.SetColour(col);
1012 }
1013 }
1014 break;
1015 case Colour_BackgroundMode:
1016 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
1017 : wxSOLID;
1018 break;
1019
1020 default:
1021 // skip Refresh()
1022 return;
aba99005 1023 }
0f0c61d0 1024
1e7fd311 1025 m_canvas->Refresh();
aba99005
RR
1026}
1027
220af862 1028void MyFrame::PrepareDC(wxDC& dc)
aba99005 1029{
0f0c61d0
VZ
1030 dc.SetMapMode( m_mapMode );
1031 dc.SetUserScale( m_xUserScale, m_yUserScale );
1032 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
428db2ea 1033 dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
220af862
VZ
1034}
1035
220af862 1036wxColour MyFrame::SelectColour()
0f0c61d0
VZ
1037{
1038 wxColour col;
1039 wxColourData data;
1040 wxColourDialog dialog(this, &data);
1041
1042 if ( dialog.ShowModal() == wxID_OK )
1043 {
428db2ea 1044 col = dialog.GetColourData().GetColour();
0f0c61d0
VZ
1045 }
1046
1047 return col;
aba99005 1048}