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