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