]> git.saurik.com Git - wxWidgets.git/blob - samples/drawing/drawing.cpp
some minor changes in controls/image, timings added to listtest
[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), 410, 40 );
574
575
576 dc.SetBrush( *wxBLACK_BRUSH );
577 dc.SetPen(*wxTRANSPARENT_PEN);
578 dc.DrawRectangle( 0, 160, 1000, 300 );
579
580 // draw lines
581 wxBitmap bitmap(20,70);
582 wxMemoryDC memdc;
583 memdc.SelectObject( bitmap );
584 memdc.SetBrush( *wxBLACK_BRUSH );
585 memdc.SetPen( *wxWHITE_PEN );
586 memdc.DrawRectangle(0,0,20,70);
587 memdc.DrawLine( 10,0,10,70 );
588
589 // to the right
590 wxPen pen = *wxRED_PEN;
591 pen.SetWidth(2);
592 memdc.SetPen(pen);
593 memdc.DrawLine( 10, 5,10, 5 );
594 memdc.DrawLine( 10,10,11,10 );
595 memdc.DrawLine( 10,15,12,15 );
596 memdc.DrawLine( 10,20,13,20 );
597
598 /*
599 memdc.SetPen(*wxRED_PEN);
600 memdc.DrawLine( 12, 5,12, 5 );
601 memdc.DrawLine( 12,10,13,10 );
602 memdc.DrawLine( 12,15,14,15 );
603 memdc.DrawLine( 12,20,15,20 );
604 */
605
606 // same to the left
607 memdc.DrawLine( 10,25,10,25 );
608 memdc.DrawLine( 10,30, 9,30 );
609 memdc.DrawLine( 10,35, 8,35 );
610 memdc.DrawLine( 10,40, 7,40 );
611
612 // XOR draw lines
613 dc.SetPen(*wxWHITE_PEN);
614 memdc.SetLogicalFunction( wxINVERT );
615 memdc.SetPen( *wxWHITE_PEN );
616 memdc.DrawLine( 10,50,10,50 );
617 memdc.DrawLine( 10,55,11,55 );
618 memdc.DrawLine( 10,60,12,60 );
619 memdc.DrawLine( 10,65,13,65 );
620
621 memdc.DrawLine( 12,50,12,50 );
622 memdc.DrawLine( 12,55,13,55 );
623 memdc.DrawLine( 12,60,14,60 );
624 memdc.DrawLine( 12,65,15,65 );
625
626 memdc.SelectObject( wxNullBitmap );
627 dc.DrawBitmap( bitmap, 10, 170 );
628 wxImage image( bitmap );
629 image.Rescale( 60,210 );
630 bitmap = image.ConvertToBitmap();
631 dc.DrawBitmap( bitmap, 50, 170 );
632
633 // test the rectangle outline drawing - there should be one pixel between
634 // the rect and the lines
635 dc.SetPen(*wxWHITE_PEN);
636 dc.SetBrush( *wxTRANSPARENT_BRUSH );
637 dc.DrawRectangle(150, 170, 49, 29);
638 dc.DrawRectangle(200, 170, 49, 29);
639 dc.SetPen(*wxWHITE_PEN);
640 dc.DrawLine(250, 210, 250, 170);
641 dc.DrawLine(260, 200, 150, 200);
642
643 // test the rectangle filled drawing - there should be one pixel between
644 // the rect and the lines
645 dc.SetPen(*wxTRANSPARENT_PEN);
646 dc.SetBrush( *wxWHITE_BRUSH );
647 dc.DrawRectangle(300, 170, 49, 29);
648 dc.DrawRectangle(350, 170, 49, 29);
649 dc.SetPen(*wxWHITE_PEN);
650 dc.DrawLine(400, 170, 400, 210);
651 dc.DrawLine(300, 200, 410, 200);
652
653 // and now for filled rect with outline
654 dc.SetPen(*wxRED_PEN);
655 dc.SetBrush( *wxWHITE_BRUSH );
656 dc.DrawRectangle(500, 170, 49, 29);
657 dc.DrawRectangle(550, 170, 49, 29);
658 dc.SetPen(*wxWHITE_PEN);
659 dc.DrawLine(600, 170, 600, 210);
660 dc.DrawLine(500, 200, 610, 200);
661
662 // test the rectangle outline drawing - there should be one pixel between
663 // the rect and the lines
664 dc.SetPen(*wxWHITE_PEN);
665 dc.SetBrush( *wxTRANSPARENT_BRUSH );
666 dc.DrawRoundedRectangle(150, 270, 49, 29, 6);
667 dc.DrawRoundedRectangle(200, 270, 49, 29, 6);
668 dc.SetPen(*wxWHITE_PEN);
669 dc.DrawLine(250, 270, 250, 310);
670 dc.DrawLine(150, 300, 260, 300);
671
672 // test the rectangle filled drawing - there should be one pixel between
673 // the rect and the lines
674 dc.SetPen(*wxTRANSPARENT_PEN);
675 dc.SetBrush( *wxWHITE_BRUSH );
676 dc.DrawRoundedRectangle(300, 270, 49, 29, 6);
677 dc.DrawRoundedRectangle(350, 270, 49, 29, 6);
678 dc.SetPen(*wxWHITE_PEN);
679 dc.DrawLine(400, 270, 400, 310);
680 dc.DrawLine(300, 300, 410, 300);
681
682 }
683
684 void MyCanvas::DrawText(wxDC& dc)
685 {
686 // set underlined font for testing
687 dc.SetFont( wxFont(12, wxMODERN, wxNORMAL, wxNORMAL, TRUE) );
688 dc.DrawText( "This is text", 110, 10 );
689 dc.DrawRotatedText( "That is text", 20, 10, -45 );
690
691 dc.SetFont( *wxNORMAL_FONT );
692
693 wxString text;
694 dc. SetBackgroundMode(wxTRANSPARENT);
695
696 for ( int n = -180; n < 180; n += 30 )
697 {
698 text.Printf(" %d rotated text", n);
699 dc.DrawRotatedText(text , 400, 400, n);
700 }
701
702 dc.SetFont( wxFont( 18, wxSWISS, wxNORMAL, wxNORMAL ) );
703
704 dc.DrawText( "This is Swiss 18pt text.", 110, 40 );
705
706 long length;
707 long height;
708 long descent;
709 dc.GetTextExtent( "This is Swiss 18pt text.", &length, &height, &descent );
710 text.Printf( "Dimensions are length %ld, height %ld, descent %ld", length, height, descent );
711 dc.DrawText( text, 110, 80 );
712
713 text.Printf( "CharHeight() returns: %d", dc.GetCharHeight() );
714 dc.DrawText( text, 110, 120 );
715
716 dc.DrawRectangle( 100, 40, 4, height );
717 }
718
719 static const struct
720 {
721 const wxChar *name;
722 int rop;
723 } rasterOperations[] =
724 {
725 { "wxAND", wxAND },
726 { "wxAND_INVERT", wxAND_INVERT },
727 { "wxAND_REVERSE", wxAND_REVERSE },
728 { "wxCLEAR", wxCLEAR },
729 { "wxCOPY", wxCOPY },
730 { "wxEQUIV", wxEQUIV },
731 { "wxINVERT", wxINVERT },
732 { "wxNAND", wxNAND },
733 { "wxNO_OP", wxNO_OP },
734 { "wxOR", wxOR },
735 { "wxOR_INVERT", wxOR_INVERT },
736 { "wxOR_REVERSE", wxOR_REVERSE },
737 { "wxSET", wxSET },
738 { "wxSRC_INVERT", wxSRC_INVERT },
739 { "wxXOR", wxXOR },
740 };
741
742 void MyCanvas::DrawImages(wxDC& dc)
743 {
744 dc.DrawText("original image", 0, 0);
745 dc.DrawBitmap(gs_bmpNoMask, 0, 20, 0);
746 dc.DrawText("with colour mask", 0, 100);
747 dc.DrawBitmap(gs_bmpWithColMask, 0, 120, TRUE);
748 dc.DrawText("the mask image", 0, 200);
749 dc.DrawBitmap(gs_bmpMask, 0, 220, 0);
750 dc.DrawText("masked image", 0, 300);
751 dc.DrawBitmap(gs_bmpWithMask, 0, 320, TRUE);
752
753 int cx = gs_bmpWithColMask.GetWidth(),
754 cy = gs_bmpWithColMask.GetHeight();
755
756 wxMemoryDC memDC;
757 for ( size_t n = 0; n < WXSIZEOF(rasterOperations); n++ )
758 {
759 wxCoord x = 120 + 150*(n%4),
760 y = 20 + 100*(n/4);
761
762 dc.DrawText(rasterOperations[n].name, x, y - 20);
763 memDC.SelectObject(gs_bmpWithColMask);
764 dc.Blit(x, y, cx, cy, &memDC, 0, 0, rasterOperations[n].rop, TRUE);
765 }
766 }
767
768 void MyCanvas::DrawWithLogicalOps(wxDC& dc)
769 {
770 static const wxCoord w = 60;
771 static const wxCoord h = 60;
772
773 // reuse the text colour here
774 dc.SetPen(wxPen(m_owner->m_colourForeground, 1, wxSOLID));
775
776 for ( size_t n = 0; n < WXSIZEOF(rasterOperations); n++ )
777 {
778 wxCoord x = 20 + 150*(n%4),
779 y = 20 + 100*(n/4);
780
781 dc.DrawText(rasterOperations[n].name, x, y - 20);
782 dc.SetLogicalFunction(rasterOperations[n].rop);
783 //dc.DrawRectangle(x, y, w, h);
784 dc.DrawLine(x, y, x + w, y + h);
785 dc.DrawLine(x + w, y, x, y + h);
786 }
787 }
788
789 void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
790 {
791 wxPaintDC dc(this);
792 PrepareDC(dc);
793 m_owner->PrepareDC(dc);
794
795 dc.SetBackgroundMode( m_owner->m_backgroundMode );
796 if ( m_owner->m_backgroundBrush.Ok() )
797 dc.SetBackground( m_owner->m_backgroundBrush );
798 if ( m_owner->m_colourForeground.Ok() )
799 dc.SetTextForeground( m_owner->m_colourForeground );
800 if ( m_owner->m_colourBackground.Ok() )
801 dc.SetTextBackground( m_owner->m_colourBackground );
802
803 switch ( m_show )
804 {
805 case Show_Default:
806 DrawDefault(dc);
807 break;
808
809 case Show_Text:
810 DrawText(dc);
811 break;
812
813 case Show_Lines:
814 DrawTestLines( 0, 100, 0, dc );
815 DrawTestLines( 0, 300, 1, dc );
816 DrawTestLines( 0, 500, 2, dc );
817 DrawTestLines( 0, 700, 6, dc );
818 break;
819
820 case Show_Polygons:
821 DrawTestPoly( 0, 100, dc, 0 );
822 DrawTestPoly( 33, 500, dc, 1 );
823 DrawTestPoly( 43, 1000, dc, 2 );
824 break;
825
826 case Show_Mask:
827 DrawImages(dc);
828 break;
829
830 case Show_Ops:
831 DrawWithLogicalOps(dc);
832 break;
833 }
834 }
835
836 void MyCanvas::OnMouseMove(wxMouseEvent &event)
837 {
838 wxClientDC dc(this);
839 PrepareDC(dc);
840 m_owner->PrepareDC(dc);
841
842 wxPoint pos = event.GetPosition();
843 long x = dc.DeviceToLogicalX( pos.x );
844 long y = dc.DeviceToLogicalY( pos.y );
845 wxString str;
846 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
847 m_owner->SetStatusText( str );
848 }
849
850 // ----------------------------------------------------------------------------
851 // MyFrame
852 // ----------------------------------------------------------------------------
853
854 // the event tables connect the wxWindows events with the functions (event
855 // handlers) which process them. It can be also done at run-time, but for the
856 // simple menu events like this the static method is much simpler.
857 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
858 EVT_MENU (File_Quit, MyFrame::OnQuit)
859 EVT_MENU (File_About, MyFrame::OnAbout)
860
861 EVT_MENU_RANGE(MenuShow_First, MenuShow_Last, MyFrame::OnShow)
862
863 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
864 END_EVENT_TABLE()
865
866 // frame constructor
867 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
868 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
869 {
870 // set the frame icon
871 SetIcon(wxICON(mondrian));
872
873 wxMenu *menuFile = new wxMenu;
874 menuFile->Append(File_ShowDefault, "&Default screen\tF1");
875 menuFile->Append(File_ShowText, "&Text screen\tF2");
876 menuFile->Append(File_ShowLines, "&Lines screen\tF3");
877 menuFile->Append(File_ShowPolygons, "&Polygons screen\tF4");
878 menuFile->Append(File_ShowMask, "wx&Mask screen\tF5");
879 menuFile->Append(File_ShowOps, "&ROP screen\tF6");
880 menuFile->AppendSeparator();
881 menuFile->Append(File_About, "&About...\tCtrl-A", "Show about dialog");
882 menuFile->AppendSeparator();
883 menuFile->Append(File_Quit, "E&xit\tAlt-X", "Quit this program");
884
885 wxMenu *menuMapMode = new wxMenu;
886 menuMapMode->Append( MapMode_Text, "&TEXT map mode" );
887 menuMapMode->Append( MapMode_Lometric, "&LOMETRIC map mode" );
888 menuMapMode->Append( MapMode_Twips, "T&WIPS map mode" );
889 menuMapMode->Append( MapMode_Points, "&POINTS map mode" );
890 menuMapMode->Append( MapMode_Metric, "&METRIC map mode" );
891
892 wxMenu *menuUserScale = new wxMenu;
893 menuUserScale->Append( UserScale_StretchHoriz, "Stretch horizontally\tCtrl-H" );
894 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tCtrl-G" );
895 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-V" );
896 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-W" );
897 menuUserScale->AppendSeparator();
898 menuUserScale->Append( UserScale_Restore, "Restore to normal\tCtrl-0" );
899
900 wxMenu *menuAxis = new wxMenu;
901 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-M", "", TRUE );
902 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-N", "", TRUE );
903
904 wxMenu *menuLogical = new wxMenu;
905 menuLogical->Append( LogicalOrigin_MoveDown, "Move &down\tCtrl-D" );
906 menuLogical->Append( LogicalOrigin_MoveUp, "Move &up\tCtrl-U" );
907 menuLogical->Append( LogicalOrigin_MoveLeft, "Move &right\tCtrl-L" );
908 menuLogical->Append( LogicalOrigin_MoveRight, "Move &left\tCtrl-R" );
909
910 wxMenu *menuColour = new wxMenu;
911 menuColour->Append( Colour_TextForeground, "Text foreground..." );
912 menuColour->Append( Colour_TextBackground, "Text background..." );
913 menuColour->Append( Colour_Background, "Background colour..." );
914 menuColour->Append( Colour_BackgroundMode, "Opaque/transparent\tCtrl-B", "", TRUE );
915
916 // now append the freshly created menu to the menu bar...
917 wxMenuBar *menuBar = new wxMenuBar;
918 menuBar->Append(menuFile, "&File");
919 menuBar->Append(menuMapMode, "&MapMode");
920 menuBar->Append(menuUserScale, "&UserScale");
921 menuBar->Append(menuAxis, "&Axis");
922 menuBar->Append(menuLogical, "&LogicalOrigin");
923 menuBar->Append(menuColour, "&Colours");
924
925 // ... and attach this menu bar to the frame
926 SetMenuBar(menuBar);
927
928 // create a status bar just for fun (by default with 1 pane only)
929 CreateStatusBar(2);
930 SetStatusText("Welcome to wxWindows!");
931
932 m_mapMode = wxMM_TEXT;
933 m_xUserScale = 1.0;
934 m_yUserScale = 1.0;
935 m_xLogicalOrigin = 0;
936 m_yLogicalOrigin = 0;
937 m_xAxisReversed =
938 m_yAxisReversed = FALSE;
939 m_backgroundMode = wxSOLID;
940 m_colourForeground = *wxRED;
941 m_colourBackground = *wxBLUE;
942
943 m_canvas = new MyCanvas( this );
944 m_canvas->SetScrollbars( 10, 10, 100, 240 );
945 }
946
947 // event handlers
948
949 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
950 {
951 // TRUE is to force the frame to close
952 Close(TRUE);
953 }
954
955 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
956 {
957 wxString msg;
958 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
959 _T("Copyright (c) Robert Roebling 1999")
960 );
961
962 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
963 }
964
965 void MyFrame::OnShow(wxCommandEvent& event)
966 {
967 m_canvas->Show((ScreenToShow)(event.GetInt() - MenuShow_First));
968 }
969
970 void MyFrame::OnOption(wxCommandEvent& event)
971 {
972 switch (event.GetInt())
973 {
974 case MapMode_Text:
975 m_mapMode = wxMM_TEXT;
976 break;
977 case MapMode_Lometric:
978 m_mapMode = wxMM_LOMETRIC;
979 break;
980 case MapMode_Twips:
981 m_mapMode = wxMM_TWIPS;
982 break;
983 case MapMode_Points:
984 m_mapMode = wxMM_POINTS;
985 break;
986 case MapMode_Metric:
987 m_mapMode = wxMM_METRIC;
988 break;
989
990 case LogicalOrigin_MoveDown:
991 m_yLogicalOrigin += 10;
992 break;
993 case LogicalOrigin_MoveUp:
994 m_yLogicalOrigin -= 10;
995 break;
996 case LogicalOrigin_MoveLeft:
997 m_xLogicalOrigin += 10;
998 break;
999 case LogicalOrigin_MoveRight:
1000 m_xLogicalOrigin -= 10;
1001 break;
1002
1003 case UserScale_StretchHoriz:
1004 m_xUserScale *= 1.10;
1005 break;
1006 case UserScale_ShrinkHoriz:
1007 m_xUserScale /= 1.10;
1008 break;
1009 case UserScale_StretchVertic:
1010 m_yUserScale *= 1.10;
1011 break;
1012 case UserScale_ShrinkVertic:
1013 m_yUserScale /= 1.10;
1014 break;
1015 case UserScale_Restore:
1016 m_xUserScale =
1017 m_yUserScale = 1.0;
1018 break;
1019
1020 case AxisMirror_Vertic:
1021 m_yAxisReversed = !m_yAxisReversed;
1022 break;
1023 case AxisMirror_Horiz:
1024 m_xAxisReversed = !m_xAxisReversed;
1025 break;
1026
1027 case Colour_TextForeground:
1028 m_colourForeground = SelectColour();
1029 break;
1030 case Colour_TextBackground:
1031 m_colourBackground = SelectColour();
1032 break;
1033 case Colour_Background:
1034 {
1035 wxColour col = SelectColour();
1036 if ( col.Ok() )
1037 {
1038 m_backgroundBrush.SetColour(col);
1039 }
1040 }
1041 break;
1042 case Colour_BackgroundMode:
1043 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
1044 : wxSOLID;
1045 break;
1046
1047 default:
1048 // skip Refresh()
1049 return;
1050 }
1051
1052 m_canvas->Refresh();
1053 }
1054
1055 void MyFrame::PrepareDC(wxDC& dc)
1056 {
1057 dc.SetMapMode( m_mapMode );
1058 dc.SetUserScale( m_xUserScale, m_yUserScale );
1059 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
1060 dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
1061 }
1062
1063 wxColour MyFrame::SelectColour()
1064 {
1065 wxColour col;
1066 wxColourData data;
1067 wxColourDialog dialog(this, &data);
1068
1069 if ( dialog.ShowModal() == wxID_OK )
1070 {
1071 col = dialog.GetColourData().GetColour();
1072 }
1073
1074 return col;
1075 }