]> git.saurik.com Git - wxWidgets.git/blob - samples/drawing/drawing.cpp
Corrected TIFF,
[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
258 // This is so wrong, it hurts.
259 // gs_bmpMask.SetDepth(1);
260 // wxMask *mask = new wxMask(gs_bmpMask);
261
262 wxMask *mask = new wxMask(gs_bmpMask, *wxBLACK);
263 gs_bmpWithMask.SetMask(mask);
264
265 mask = new wxMask(gs_bmpWithColMask, *wxWHITE);
266 gs_bmpWithColMask.SetMask(mask);
267
268 return TRUE;
269 }
270
271 // `Main program' equivalent: the program execution "starts" here
272 bool MyApp::OnInit()
273 {
274 // Create the main application window
275 MyFrame *frame = new MyFrame("Drawing sample",
276 wxPoint(50, 50), wxSize(550, 340));
277
278 // Show it and tell the application that it's our main window
279 frame->Show(TRUE);
280 SetTopWindow(frame);
281
282 if ( !LoadImages() )
283 {
284 wxLogError("Can't load one of the bitmap files needed for this sample "
285 "from the current or parent directory, please copy them "
286 "there.");
287
288 // stop here
289 return FALSE;
290 }
291
292 // ok, continue
293 return TRUE;
294 }
295
296 // ----------------------------------------------------------------------------
297 // MyCanvas
298 // ----------------------------------------------------------------------------
299
300 // the event tables connect the wxWindows events with the functions (event
301 // handlers) which process them.
302 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
303 EVT_PAINT (MyCanvas::OnPaint)
304 EVT_MOTION (MyCanvas::OnMouseMove)
305 END_EVENT_TABLE()
306
307 MyCanvas::MyCanvas( MyFrame *parent ) : wxScrolledWindow( parent )
308 {
309 m_owner = parent;
310 m_show = Show_Default;
311 }
312
313 //draw a polygon and an overlapping rectangle
314 //is transparent is 1, the fill pattern are made transparent
315 //is transparent is 2, the fill pattern are made transparent but inversed
316 //is transparent is 0 the text for and background color will be used to represent/map
317 //the colors of the monochrome bitmap pixels to the fillpattern
318 //
319 //i miss_used the the menu items for setting so called back and fore ground color
320 //just to show how the those colors do influence the fillpatterns
321 //just play with those,
322 //and with the code
323 //variations are endless using other logical functions
324 void MyCanvas::DrawTestPoly( int x, int y,wxDC &dc,int transparent )
325 {
326 wxBrush* brush4 = new wxBrush(gs_bmp4);
327 wxBrush* brush36 = new wxBrush(gs_bmp36);
328
329 wxPoint todraw[5];
330 todraw[0].x=(long)x+100;
331 todraw[0].y=(long)y+100;
332 todraw[1].x=(long)x+300;
333 todraw[1].y=(long)y+100;
334 todraw[2].x=(long)x+300;
335 todraw[2].y=(long)y+300;
336 todraw[3].x=(long)x+150;
337 todraw[3].y=(long)y+350;
338 todraw[4].x=(long)x+100;
339 todraw[4].y=(long)y+300;
340
341 wxPoint todraw2[5];
342 todraw2[0].x=100;
343 todraw2[0].y=100;
344 todraw2[1].x=300;
345 todraw2[1].y=100;
346 todraw2[2].x=300;
347 todraw2[2].y=300;
348 todraw2[3].x=150;
349 todraw2[3].y=350;
350 todraw2[4].x=100;
351 todraw2[4].y=300;
352
353 switch (transparent)
354 {
355 case 0:
356 {
357 dc.SetPen( wxPen( "black", 4, wxSOLID) );
358 dc.SetBrush( *brush4 );
359 dc.SetTextForeground(*wxGREEN);
360 dc.SetTextBackground(m_owner->m_colourForeground);
361 dc.SetLogicalFunction(wxCOPY);
362 dc.DrawPolygon(5,todraw,0,0,wxWINDING_RULE);
363
364 //don't understand hwo but the outline is also depending on logicalfunction
365 dc.SetPen( wxPen( "red", 4, wxSOLID) );
366 dc.SetBrush( *brush36 );
367 dc.SetTextForeground(*wxCYAN);
368 dc.SetTextBackground(m_owner->m_colourBackground);
369 dc.SetLogicalFunction(wxCOPY);
370 dc.DrawRectangle( x+10, y+10, 200, 200 );
371 dc.SetBrush(wxNullBrush);
372 dc.SetPen(wxNullPen);
373 break;
374 }
375 case 1: //now with transparent fillpatterns
376 {
377
378 wxBitmap* bmpBlit = new wxBitmap(600,400);
379 wxMemoryDC* memDC= new wxMemoryDC();
380 // wxBrush _clearbrush(*wxGREEN,wxSOLID);
381 wxBrush _clearbrush(*wxBLACK,wxSOLID);
382 memDC->SelectObject(*bmpBlit);
383 memDC->BeginDrawing();
384 memDC->SetBackground(_clearbrush);
385 memDC->Clear();
386 memDC->SetBackground(wxNullBrush);
387
388 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
389 memDC->SetBrush( wxNullBrush);
390 memDC->SetBrush( *brush4 );
391 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
392 memDC->SetTextBackground(*wxWHITE); // 1s --> 0xFFFFFF (white)
393 memDC->SetLogicalFunction(wxAND_INVERT);
394
395 // BLACK OUT the opaque pixels and leave the rest as is
396 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
397
398 // Set background and foreground colors for fill pattern
399 //the previous blacked out pixels are now merged with the layer color
400 //while the non blacked out pixels stay as they are.
401 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
402
403 //now define what will be the color of the fillpattern parts that are not transparent
404 // memDC->SetTextBackground(*wxBLUE);
405 memDC->SetTextBackground(m_owner->m_colourForeground);
406 memDC->SetLogicalFunction(wxOR);
407
408
409 //don't understand how but the outline is also depending on logicalfunction
410 memDC->SetPen( wxPen( "red", 4, wxSOLID) );
411 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
412
413 memDC->SetLogicalFunction(wxCOPY);
414
415 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
416 memDC->SetBrush( wxNullBrush);
417 memDC->SetBrush( *brush36 );
418 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
419 memDC->SetTextBackground(*wxWHITE); // 1s --> 0xFFFFFF (white)
420 memDC->SetLogicalFunction(wxAND_INVERT);
421
422 memDC->DrawRectangle( 10, 10, 200, 200 );
423
424 // Set background and foreground colors for fill pattern
425 //the previous blacked out pixels are now merged with the layer color
426 //while the non blacked out pixels stay as they are.
427 memDC->SetTextForeground(*wxBLACK); // 0s --> 0x000000 (black)
428 //now define what will be the color of the fillpattern parts that are not transparent
429 // memDC->SetTextBackground(*wxRED);
430 memDC->SetTextBackground(m_owner->m_colourBackground);
431 memDC->SetLogicalFunction(wxOR);
432
433 //don't understand how but the outline is also depending on logicalfunction
434 memDC->SetPen( wxPen( "yellow", 4, wxSOLID) );
435 memDC->DrawRectangle( 10, 10, 200, 200 );
436
437 memDC->SetBrush(wxNullBrush);
438 memDC->SetPen(wxNullPen);
439
440 memDC->EndDrawing();
441 dc.Blit(x,y,600,400,memDC,0,0,wxCOPY);
442 delete bmpBlit;
443 delete memDC;
444 break;
445 }
446 case 2: //now with transparent inversed fillpatterns
447 {
448 wxBitmap* bmpBlit = new wxBitmap(600,400);
449 wxMemoryDC* memDC= new wxMemoryDC();
450 wxBrush _clearbrush(*wxWHITE,wxSOLID);
451 memDC->SelectObject(*bmpBlit);
452 memDC->BeginDrawing();
453 memDC->SetBackground(_clearbrush);
454 memDC->Clear();
455 memDC->SetBackground(wxNullBrush);
456
457 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
458 memDC->SetBrush( *brush4 );
459 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
460 memDC->SetTextForeground(*wxWHITE); // 1s --> 0xFFFFFF (white)
461 memDC->SetLogicalFunction(wxAND_INVERT);
462
463 // BLACK OUT the opaque pixels and leave the rest as is
464 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
465
466 // Set background and foreground colors for fill pattern
467 //the previous blacked out pixels are now merged with the layer color
468 //while the non blacked out pixels stay as they are.
469 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
470
471 //now define what will be the color of the fillpattern parts that are not transparent
472 memDC->SetTextForeground(m_owner->m_colourForeground);
473 memDC->SetLogicalFunction(wxOR);
474
475
476 //don't understand how but the outline is also depending on logicalfunction
477 memDC->SetPen( wxPen( "red", 4, wxSOLID) );
478 memDC->DrawPolygon(5,todraw2,0,0,wxWINDING_RULE);
479
480 memDC->SetLogicalFunction(wxCOPY);
481
482 memDC->SetPen( wxPen( "black", 4, wxSOLID) );
483 memDC->SetBrush( *brush36 );
484 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
485 memDC->SetTextForeground(*wxWHITE); // 1s --> 0xFFFFFF (white)
486 memDC->SetLogicalFunction(wxAND_INVERT);
487
488 memDC->DrawRectangle( 10,10, 200, 200 );
489
490 // Set background and foreground colors for fill pattern
491 //the previous blacked out pixels are now merged with the layer color
492 //while the non blacked out pixels stay as they are.
493 memDC->SetTextBackground(*wxBLACK); // 0s --> 0x000000 (black)
494 //now define what will be the color of the fillpattern parts that are not transparent
495 memDC->SetTextForeground(m_owner->m_colourBackground);
496 memDC->SetLogicalFunction(wxOR);
497
498 //don't understand how but the outline is also depending on logicalfunction
499 memDC->SetPen( wxPen( "yellow", 4, wxSOLID) );
500 memDC->DrawRectangle( 10, 10, 200, 200 );
501
502 memDC->SetBrush(wxNullBrush);
503 memDC->SetPen(wxNullPen);
504 dc.Blit(x,y,600,400,memDC,0,0,wxCOPY);
505 delete bmpBlit;
506 delete memDC;
507 }
508 }
509
510 delete brush4;
511 delete brush36;
512 }
513
514 void MyCanvas::DrawTestLines( int x, int y, int width, wxDC &dc )
515 {
516 dc.SetPen( wxPen( "black", width, wxSOLID) );
517 dc.SetBrush( *wxRED_BRUSH );
518 dc.DrawRectangle( x+10, y+10, 100, 190 );
519
520 dc.SetPen( wxPen( "black", width, wxSOLID) );
521 dc.DrawLine( x+20, y+20, 100, y+20 );
522 dc.SetPen( wxPen( "black", width, wxDOT) );
523 dc.DrawLine( x+20, y+30, 100, y+30 );
524 dc.SetPen( wxPen( "black", width, wxSHORT_DASH) );
525 dc.DrawLine( x+20, y+40, 100, y+40 );
526 dc.SetPen( wxPen( "black", width, wxLONG_DASH) );
527 dc.DrawLine( x+20, y+50, 100, y+50 );
528 dc.SetPen( wxPen( "black", width, wxDOT_DASH) );
529 dc.DrawLine( x+20, y+60, 100, y+60 );
530
531 dc.SetPen( wxPen( "black", width, wxBDIAGONAL_HATCH) );
532 dc.DrawLine( x+20, y+70, 100, y+70 );
533 dc.SetPen( wxPen( "black", width, wxCROSSDIAG_HATCH) );
534 dc.DrawLine( x+20, y+80, 100, y+80 );
535 dc.SetPen( wxPen( "black", width, wxFDIAGONAL_HATCH) );
536 dc.DrawLine( x+20, y+90, 100, y+90 );
537 dc.SetPen( wxPen( "black", width, wxCROSS_HATCH) );
538 dc.DrawLine( x+20, y+100, 100, y+100 );
539 dc.SetPen( wxPen( "black", width, wxHORIZONTAL_HATCH) );
540 dc.DrawLine( x+20, y+110, 100, y+110 );
541 dc.SetPen( wxPen( "black", width, wxVERTICAL_HATCH) );
542 dc.DrawLine( x+20, y+120, 100, y+120 );
543
544 wxPen ud( "black", width, wxUSER_DASH );
545 wxDash dash1[1];
546 dash1[0] = 0;
547 ud.SetDashes( 1, dash1 );
548 dc.DrawLine( x+20, y+140, 100, y+140 );
549 dash1[0] = 1;
550 ud.SetDashes( 1, dash1 );
551 dc.DrawLine( x+20, y+150, 100, y+150 );
552 dash1[0] = 2;
553 ud.SetDashes( 1, dash1 );
554 dc.DrawLine( x+20, y+160, 100, y+160 );
555 dash1[0] = 0xFF;
556 ud.SetDashes( 1, dash1 );
557 dc.DrawLine( x+20, y+170, 100, y+170 );
558
559 }
560
561 void MyCanvas::DrawDefault(wxDC& dc)
562 {
563 // mark the origin
564 dc.DrawCircle(0, 0, 10);
565 #if !(defined __WXGTK__) && !(defined __WXMOTIF__)
566 // not implemented in wxGTK or wxMOTIF :-(
567 dc.FloodFill(0, 0, wxColour(255, 0, 0));
568 #endif //
569
570 dc.DrawIcon( wxICON(mondrian), 410, 40 );
571
572 dc.SetBrush( *wxBLACK_BRUSH );
573 dc.SetPen(*wxTRANSPARENT_PEN);
574 dc.DrawRectangle( 0, 100, 1000, 300 );
575
576 // test the rectangle outline drawing - there should be one pixel between
577 // the rect and the lines
578 dc.SetPen(*wxWHITE_PEN);
579 dc.SetBrush( *wxTRANSPARENT_BRUSH );
580 dc.DrawRectangle(100, 170, 49, 29);
581 dc.DrawRectangle(150, 170, 49, 29);
582 dc.SetPen(*wxWHITE_PEN);
583 dc.DrawLine(200, 160, 200, 210);
584 dc.DrawLine(100, 200, 210, 200);
585
586 // test the rectangle filled drawing - there should be one pixel between
587 // the rect and the lines
588 dc.SetPen(*wxTRANSPARENT_PEN);
589 dc.SetBrush( *wxWHITE_BRUSH );
590 dc.DrawRectangle(300, 170, 49, 29);
591 dc.DrawRectangle(350, 170, 49, 29);
592 dc.SetPen(*wxWHITE_PEN);
593 dc.DrawLine(400, 160, 400, 210);
594 dc.DrawLine(300, 200, 410, 200);
595
596 // test the rectangle outline drawing - there should be one pixel between
597 // the rect and the lines
598 dc.SetPen(*wxWHITE_PEN);
599 dc.SetBrush( *wxTRANSPARENT_BRUSH );
600 dc.DrawRoundedRectangle(100, 270, 49, 29, 6);
601 dc.DrawRoundedRectangle(150, 270, 49, 29, 6);
602 dc.SetPen(*wxWHITE_PEN);
603 dc.DrawLine(200, 260, 200, 310);
604 dc.DrawLine(100, 300, 210, 300);
605
606 // test the rectangle filled drawing - there should be one pixel between
607 // the rect and the lines
608 dc.SetPen(*wxTRANSPARENT_PEN);
609 dc.SetBrush( *wxWHITE_BRUSH );
610 dc.DrawRoundedRectangle(300, 270, 49, 29, 6);
611 dc.DrawRoundedRectangle(350, 270, 49, 29, 6);
612 dc.SetPen(*wxWHITE_PEN);
613 dc.DrawLine(400, 260, 400, 310);
614 dc.DrawLine(300, 300, 410, 300);
615
616 }
617
618 void MyCanvas::DrawText(wxDC& dc)
619 {
620 // set underlined font for testing
621 dc.SetFont( wxFont(12, wxMODERN, wxNORMAL, wxNORMAL, TRUE) );
622 dc.DrawText( "This is text", 110, 10 );
623 dc.DrawRotatedText( "That is text", 20, 10, -45 );
624
625 dc.SetFont( *wxNORMAL_FONT );
626
627 wxString text;
628 dc. SetBackgroundMode(wxTRANSPARENT);
629
630 for ( int n = -180; n < 180; n += 30 )
631 {
632 text.Printf(" %d rotated text", n);
633 dc.DrawRotatedText(text , 400, 400, n);
634 }
635
636 dc.SetFont( wxFont( 18, wxSWISS, wxNORMAL, wxNORMAL ) );
637
638 dc.DrawText( "This is Swiss 18pt text.", 110, 40 );
639
640 long length;
641 long height;
642 long descent;
643 dc.GetTextExtent( "This is Swiss 18pt text.", &length, &height, &descent );
644 text.Printf( "Dimensions are length %ld, height %ld, descent %ld", length, height, descent );
645 dc.DrawText( text, 110, 80 );
646
647 text.Printf( "CharHeight() returns: %d", dc.GetCharHeight() );
648 dc.DrawText( text, 110, 120 );
649
650 dc.DrawRectangle( 100, 40, 4, height );
651 }
652
653 void MyCanvas::DrawImages(wxDC& dc)
654 {
655 static const struct
656 {
657 const wxChar *name;
658 int rop;
659 } rasterOperations[] =
660 {
661 { "wxAND", wxAND },
662 { "wxAND_INVERT", wxAND_INVERT },
663 { "wxAND_REVERSE", wxAND_REVERSE },
664 { "wxCLEAR", wxCLEAR },
665 { "wxCOPY", wxCOPY },
666 { "wxEQUIV", wxEQUIV },
667 { "wxINVERT", wxINVERT },
668 { "wxNAND", wxNAND },
669 { "wxNO_OP", wxNO_OP },
670 { "wxOR", wxOR },
671 { "wxOR_INVERT", wxOR_INVERT },
672 { "wxOR_REVERSE", wxOR_REVERSE },
673 { "wxSET", wxSET },
674 { "wxSRC_INVERT", wxSRC_INVERT },
675 { "wxXOR", wxXOR },
676 };
677
678 dc.DrawText("original image", 0, 0);
679 dc.DrawBitmap(gs_bmpNoMask, 0, 20, 0);
680 dc.DrawText("with colour mask", 0, 100);
681 dc.DrawBitmap(gs_bmpWithColMask, 0, 120, TRUE);
682 dc.DrawText("the mask image", 0, 200);
683 dc.DrawBitmap(gs_bmpMask, 0, 220, 0);
684 dc.DrawText("masked image", 0, 300);
685 dc.DrawBitmap(gs_bmpWithMask, 0, 320, TRUE);
686
687 int cx = gs_bmpWithColMask.GetWidth(),
688 cy = gs_bmpWithColMask.GetHeight();
689
690 wxMemoryDC memDC;
691 for ( size_t n = 0; n < WXSIZEOF(rasterOperations); n++ )
692 {
693 wxCoord x = 120 + 150*(n%4),
694 y = 20 + 100*(n/4);
695
696 dc.DrawText(rasterOperations[n].name, x, y - 20);
697 memDC.SelectObject(gs_bmpWithColMask);
698 dc.Blit(x, y, cx, cy, &memDC, 0, 0, rasterOperations[n].rop, TRUE);
699 }
700 }
701
702 void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
703 {
704 wxPaintDC dc(this);
705 PrepareDC(dc);
706 m_owner->PrepareDC(dc);
707
708 dc.SetBackgroundMode( m_owner->m_backgroundMode );
709 if ( m_owner->m_backgroundBrush.Ok() )
710 dc.SetBackground( m_owner->m_backgroundBrush );
711 if ( m_owner->m_colourForeground.Ok() )
712 dc.SetTextForeground( m_owner->m_colourForeground );
713 if ( m_owner->m_colourBackground.Ok() )
714 dc.SetTextBackground( m_owner->m_colourBackground );
715
716 switch ( m_show )
717 {
718 case Show_Default:
719 DrawDefault(dc);
720 break;
721
722 case Show_Text:
723 DrawText(dc);
724 break;
725
726 case Show_Lines:
727 DrawTestLines( 0, 100, 0, dc );
728 DrawTestLines( 0, 300, 1, dc );
729 DrawTestLines( 0, 500, 2, dc );
730 DrawTestLines( 0, 700, 6, dc );
731 break;
732
733 case Show_Polygons:
734 DrawTestPoly( 0, 100, dc, 0 );
735 DrawTestPoly( 33, 500, dc, 1 );
736 DrawTestPoly( 43, 1000, dc, 2 );
737 break;
738
739 case Show_Mask:
740 DrawImages(dc);
741 break;
742 }
743 }
744
745 void MyCanvas::OnMouseMove(wxMouseEvent &event)
746 {
747 wxClientDC dc(this);
748 PrepareDC(dc);
749 m_owner->PrepareDC(dc);
750
751 wxPoint pos = event.GetPosition();
752 long x = dc.DeviceToLogicalX( pos.x );
753 long y = dc.DeviceToLogicalY( pos.y );
754 wxString str;
755 str.Printf( "Current mouse position: %d,%d", (int)x, (int)y );
756 m_owner->SetStatusText( str );
757 }
758
759 // ----------------------------------------------------------------------------
760 // MyFrame
761 // ----------------------------------------------------------------------------
762
763 // the event tables connect the wxWindows events with the functions (event
764 // handlers) which process them. It can be also done at run-time, but for the
765 // simple menu events like this the static method is much simpler.
766 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
767 EVT_MENU (File_Quit, MyFrame::OnQuit)
768 EVT_MENU (File_About, MyFrame::OnAbout)
769
770 EVT_MENU_RANGE(MenuShow_First, MenuShow_Last, MyFrame::OnShow)
771
772 EVT_MENU_RANGE(MenuOption_First, MenuOption_Last, MyFrame::OnOption)
773 END_EVENT_TABLE()
774
775 // frame constructor
776 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
777 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
778 {
779 // set the frame icon
780 SetIcon(wxICON(mondrian));
781
782 wxMenu *menuFile = new wxMenu;
783 menuFile->Append(File_ShowDefault, "&Default screen\tF1");
784 menuFile->Append(File_ShowText, "&Text screen\tF2");
785 menuFile->Append(File_ShowLines, "&Lines screen\tF3");
786 menuFile->Append(File_ShowPolygons, "&Polygons screen\tF4");
787 menuFile->Append(File_ShowMask, "wx&Mask screen\tF5");
788 menuFile->AppendSeparator();
789 menuFile->Append(File_About, "&About...\tCtrl-A", "Show about dialog");
790 menuFile->AppendSeparator();
791 menuFile->Append(File_Quit, "E&xit\tAlt-X", "Quit this program");
792
793 wxMenu *menuMapMode = new wxMenu;
794 menuMapMode->Append( MapMode_Text, "&TEXT map mode" );
795 menuMapMode->Append( MapMode_Lometric, "&LOMETRIC map mode" );
796 menuMapMode->Append( MapMode_Twips, "T&WIPS map mode" );
797 menuMapMode->Append( MapMode_Points, "&POINTS map mode" );
798 menuMapMode->Append( MapMode_Metric, "&METRIC map mode" );
799
800 wxMenu *menuUserScale = new wxMenu;
801 menuUserScale->Append( UserScale_StretchHoriz, "Stretch horizontally\tCtrl-H" );
802 menuUserScale->Append( UserScale_ShrinkHoriz, "Shrink horizontally\tCtrl-G" );
803 menuUserScale->Append( UserScale_StretchVertic, "Stretch vertically\tCtrl-V" );
804 menuUserScale->Append( UserScale_ShrinkVertic, "Shrink vertically\tCtrl-W" );
805 menuUserScale->AppendSeparator();
806 menuUserScale->Append( UserScale_Restore, "Restore to normal\tCtrl-0" );
807
808 wxMenu *menuAxis = new wxMenu;
809 menuAxis->Append( AxisMirror_Horiz, "Mirror horizontally\tCtrl-M", "", TRUE );
810 menuAxis->Append( AxisMirror_Vertic, "Mirror vertically\tCtrl-N", "", TRUE );
811
812 wxMenu *menuLogical = new wxMenu;
813 menuLogical->Append( LogicalOrigin_MoveDown, "Move &down\tCtrl-D" );
814 menuLogical->Append( LogicalOrigin_MoveUp, "Move &up\tCtrl-U" );
815 menuLogical->Append( LogicalOrigin_MoveLeft, "Move &right\tCtrl-L" );
816 menuLogical->Append( LogicalOrigin_MoveRight, "Move &left\tCtrl-R" );
817
818 wxMenu *menuColour = new wxMenu;
819 menuColour->Append( Colour_TextForeground, "Text foreground..." );
820 menuColour->Append( Colour_TextBackground, "Text background..." );
821 menuColour->Append( Colour_Background, "Background colour..." );
822 menuColour->Append( Colour_BackgroundMode, "Opaque/transparent\tCtrl-B", "", TRUE );
823
824 // now append the freshly created menu to the menu bar...
825 wxMenuBar *menuBar = new wxMenuBar;
826 menuBar->Append(menuFile, "&File");
827 menuBar->Append(menuMapMode, "&MapMode");
828 menuBar->Append(menuUserScale, "&UserScale");
829 menuBar->Append(menuAxis, "&Axis");
830 menuBar->Append(menuLogical, "&LogicalOrigin");
831 menuBar->Append(menuColour, "&Colours");
832
833 // ... and attach this menu bar to the frame
834 SetMenuBar(menuBar);
835
836 // create a status bar just for fun (by default with 1 pane only)
837 CreateStatusBar(2);
838 SetStatusText("Welcome to wxWindows!");
839
840 m_mapMode = wxMM_TEXT;
841 m_xUserScale = 1.0;
842 m_yUserScale = 1.0;
843 m_xLogicalOrigin = 0;
844 m_yLogicalOrigin = 0;
845 m_xAxisReversed =
846 m_yAxisReversed = FALSE;
847 m_backgroundMode = wxSOLID;
848 m_colourForeground = *wxRED;
849 m_colourBackground = *wxBLUE;
850
851 m_canvas = new MyCanvas( this );
852 m_canvas->SetScrollbars( 10, 10, 100, 240 );
853 }
854
855 // event handlers
856
857 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
858 {
859 // TRUE is to force the frame to close
860 Close(TRUE);
861 }
862
863 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
864 {
865 wxString msg;
866 msg.Printf( _T("This is the about dialog of the drawing sample.\n")
867 _T("Copyright (c) Robert Roebling 1999")
868 );
869
870 wxMessageBox(msg, "About Drawing", wxOK | wxICON_INFORMATION, this);
871 }
872
873 void MyFrame::OnShow(wxCommandEvent& event)
874 {
875 m_canvas->Show((ScreenToShow)(event.GetInt() - MenuShow_First));
876 }
877
878 void MyFrame::OnOption(wxCommandEvent& event)
879 {
880 switch (event.GetInt())
881 {
882 case MapMode_Text:
883 m_mapMode = wxMM_TEXT;
884 break;
885 case MapMode_Lometric:
886 m_mapMode = wxMM_LOMETRIC;
887 break;
888 case MapMode_Twips:
889 m_mapMode = wxMM_TWIPS;
890 break;
891 case MapMode_Points:
892 m_mapMode = wxMM_POINTS;
893 break;
894 case MapMode_Metric:
895 m_mapMode = wxMM_METRIC;
896 break;
897
898 case LogicalOrigin_MoveDown:
899 m_yLogicalOrigin += 10;
900 break;
901 case LogicalOrigin_MoveUp:
902 m_yLogicalOrigin -= 10;
903 break;
904 case LogicalOrigin_MoveLeft:
905 m_xLogicalOrigin += 10;
906 break;
907 case LogicalOrigin_MoveRight:
908 m_xLogicalOrigin -= 10;
909 break;
910
911 case UserScale_StretchHoriz:
912 m_xUserScale *= 1.10;
913 break;
914 case UserScale_ShrinkHoriz:
915 m_xUserScale /= 1.10;
916 break;
917 case UserScale_StretchVertic:
918 m_yUserScale *= 1.10;
919 break;
920 case UserScale_ShrinkVertic:
921 m_yUserScale /= 1.10;
922 break;
923 case UserScale_Restore:
924 m_xUserScale =
925 m_yUserScale = 1.0;
926 break;
927
928 case AxisMirror_Vertic:
929 m_yAxisReversed = !m_yAxisReversed;
930 break;
931 case AxisMirror_Horiz:
932 m_xAxisReversed = !m_xAxisReversed;
933 break;
934
935 case Colour_TextForeground:
936 m_colourForeground = SelectColour();
937 break;
938 case Colour_TextBackground:
939 m_colourBackground = SelectColour();
940 break;
941 case Colour_Background:
942 {
943 wxColour col = SelectColour();
944 if ( col.Ok() )
945 {
946 m_backgroundBrush.SetColour(col);
947 }
948 }
949 break;
950 case Colour_BackgroundMode:
951 m_backgroundMode = m_backgroundMode == wxSOLID ? wxTRANSPARENT
952 : wxSOLID;
953 break;
954
955 default:
956 // skip Refresh()
957 return;
958 }
959
960 m_canvas->Refresh();
961 }
962
963 void MyFrame::PrepareDC(wxDC& dc)
964 {
965 dc.SetMapMode( m_mapMode );
966 dc.SetUserScale( m_xUserScale, m_yUserScale );
967 dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
968 dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
969 }
970
971 wxColour MyFrame::SelectColour()
972 {
973 wxColour col;
974 wxColourData data;
975 wxColourDialog dialog(this, &data);
976
977 if ( dialog.ShowModal() == wxID_OK )
978 {
979 col = dialog.GetColourData().GetColour();
980 }
981
982 return col;
983 }