1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: shows and tests wxDC features
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "drawing.cpp"
22 #pragma interface "drawing.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
38 #include "wx/colordlg.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // the application icon
46 #if defined(__WXGTK__) || defined(__WXMOTIF__)
47 #include "mondrian.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // what do we show on screen (there are too many shapes to put them all on
55 // screen simultaneously)
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 static wxBitmap gs_bmpNoMask
,
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 // Define a new application type, each program should derive a class from wxApp
82 class MyApp
: public wxApp
85 // override base class virtuals
86 // ----------------------------
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();
99 // Define a new frame type: this is going to be our main frame
100 class MyFrame
: public wxFrame
104 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
112 wxColour
SelectColour();
113 void PrepareDC(wxDC
& dc
);
115 int m_backgroundMode
;
116 int m_textureBackground
;
120 int m_xLogicalOrigin
;
121 int m_yLogicalOrigin
;
122 bool m_xAxisReversed
,
124 wxColour m_colourForeground
, // these are _text_ colours
126 wxBrush m_backgroundBrush
;
130 // any class wishing to process wxWindows events must use this macro
131 DECLARE_EVENT_TABLE()
134 // define a scrollable canvas for drawing onto
135 class MyCanvas
: public wxScrolledWindow
138 MyCanvas( MyFrame
*parent
);
140 void OnPaint(wxPaintEvent
&event
);
141 void OnMouseMove(wxMouseEvent
&event
);
143 void Show(ScreenToShow show
) { m_show
= show
; Refresh(); }
146 void DrawTestPoly( int x
, int y
, wxDC
&dc
,int transparent
);
147 void DrawTestLines( int x
, int y
, int width
, wxDC
&dc
);
148 void DrawText(wxDC
& dc
);
149 void DrawImages(wxDC
& dc
);
150 void DrawWithLogicalOps(wxDC
& dc
);
151 void DrawDefault(wxDC
& dc
);
158 DECLARE_EVENT_TABLE()
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 // IDs for the controls and the menu commands
173 File_ShowDefault
= MenuShow_First
,
179 MenuShow_Last
= File_ShowOps
,
183 MapMode_Text
= MenuOption_First
,
189 UserScale_StretchHoriz
,
190 UserScale_ShrinkHoriz
,
191 UserScale_StretchVertic
,
192 UserScale_ShrinkVertic
,
198 LogicalOrigin_MoveDown
,
199 LogicalOrigin_MoveUp
,
200 LogicalOrigin_MoveLeft
,
201 LogicalOrigin_MoveRight
,
203 Colour_TextForeground
,
204 Colour_TextBackground
,
206 Colour_BackgroundMode
,
207 Colour_TextureBackgound
,
209 MenuOption_Last
= Colour_TextureBackgound
212 // ----------------------------------------------------------------------------
213 // event tables and other macros for wxWindows
214 // ----------------------------------------------------------------------------
217 // Create a new application object: this macro will allow wxWindows to create
218 // the application object during program execution (it's better than using a
219 // static object for many reasons) and also declares the accessor function
220 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
224 // ============================================================================
226 // ============================================================================
228 // ----------------------------------------------------------------------------
229 // the application class
230 // ----------------------------------------------------------------------------
232 bool MyApp::LoadImages()
238 wxString path
= pathList
.FindValidPath("pat4.bmp");
241 gs_bmp4
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
242 wxMask
* mask4
= new wxMask(gs_bmp4
, *wxBLACK
);
243 gs_bmp4
.SetMask(mask4
);
245 path
= pathList
.FindValidPath("pat36.bmp");
248 gs_bmp36
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
249 wxMask
* mask36
= new wxMask(gs_bmp36
, *wxBLACK
);
250 gs_bmp36
.SetMask(mask36
);
252 path
= pathList
.FindValidPath("image.bmp");
255 gs_bmpNoMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
256 gs_bmpWithMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
257 gs_bmpWithColMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
259 path
= pathList
.FindValidPath("mask.bmp");
262 gs_bmpMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
264 // This is so wrong, it hurts.
265 // gs_bmpMask.SetDepth(1);
266 // wxMask *mask = new wxMask(gs_bmpMask);
268 wxMask
*mask
= new wxMask(gs_bmpMask
, *wxBLACK
);
269 gs_bmpWithMask
.SetMask(mask
);
271 mask
= new wxMask(gs_bmpWithColMask
, *wxWHITE
);
272 gs_bmpWithColMask
.SetMask(mask
);
277 // `Main program' equivalent: the program execution "starts" here
280 // Create the main application window
281 MyFrame
*frame
= new MyFrame("Drawing sample",
282 wxPoint(50, 50), wxSize(550, 340));
284 // Show it and tell the application that it's our main window
290 wxLogError("Can't load one of the bitmap files needed for this sample "
291 "from the current or parent directory, please copy them "
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 // the event tables connect the wxWindows events with the functions (event
307 // handlers) which process them.
308 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
309 EVT_PAINT (MyCanvas::OnPaint
)
310 EVT_MOTION (MyCanvas::OnMouseMove
)
313 MyCanvas::MyCanvas( MyFrame
*parent
) : wxScrolledWindow( parent
)
316 m_show
= Show_Default
;
319 //draw a polygon and an overlapping rectangle
320 //is transparent is 1, the fill pattern are made transparent
321 //is transparent is 2, the fill pattern are made transparent but inversed
322 //is transparent is 0 the text for and background color will be used to represent/map
323 //the colors of the monochrome bitmap pixels to the fillpattern
325 //i miss_used the the menu items for setting so called back and fore ground color
326 //just to show how the those colors do influence the fillpatterns
327 //just play with those,
329 //variations are endless using other logical functions
330 void MyCanvas::DrawTestPoly( int x
, int y
,wxDC
&dc
,int transparent
)
332 wxBrush
* brush4
= new wxBrush(gs_bmp4
);
333 wxBrush
* brush36
= new wxBrush(gs_bmp36
);
336 todraw
[0].x
=(long)x
+100;
337 todraw
[0].y
=(long)y
+100;
338 todraw
[1].x
=(long)x
+300;
339 todraw
[1].y
=(long)y
+100;
340 todraw
[2].x
=(long)x
+300;
341 todraw
[2].y
=(long)y
+300;
342 todraw
[3].x
=(long)x
+150;
343 todraw
[3].y
=(long)y
+350;
344 todraw
[4].x
=(long)x
+100;
345 todraw
[4].y
=(long)y
+300;
363 dc
.SetPen( wxPen( "black", 4, wxSOLID
) );
364 dc
.SetBrush( *brush4
);
365 dc
.SetTextForeground(*wxGREEN
);
366 dc
.SetTextBackground(m_owner
->m_colourForeground
);
367 dc
.SetLogicalFunction(wxCOPY
);
368 dc
.DrawPolygon(5,todraw
,0,0,wxWINDING_RULE
);
370 //don't understand hwo but the outline is also depending on logicalfunction
371 dc
.SetPen( wxPen( "red", 4, wxSOLID
) );
372 dc
.SetBrush( *brush36
);
373 dc
.SetTextForeground(*wxCYAN
);
374 dc
.SetTextBackground(m_owner
->m_colourBackground
);
375 dc
.SetLogicalFunction(wxCOPY
);
376 dc
.DrawRectangle( x
+10, y
+10, 200, 200 );
377 dc
.SetBrush(wxNullBrush
);
378 dc
.SetPen(wxNullPen
);
381 case 1: //now with transparent fillpatterns
384 wxBitmap
* bmpBlit
= new wxBitmap(600,400);
385 wxMemoryDC
* memDC
= new wxMemoryDC();
386 // wxBrush _clearbrush(*wxGREEN,wxSOLID);
387 wxBrush
_clearbrush(*wxBLACK
,wxSOLID
);
388 memDC
->SelectObject(*bmpBlit
);
389 memDC
->BeginDrawing();
390 memDC
->SetBackground(_clearbrush
);
392 memDC
->SetBackground(wxNullBrush
);
394 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
395 memDC
->SetBrush( wxNullBrush
);
396 memDC
->SetBrush( *brush4
);
397 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
398 memDC
->SetTextBackground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
399 memDC
->SetLogicalFunction(wxAND_INVERT
);
401 // BLACK OUT the opaque pixels and leave the rest as is
402 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
404 // Set background and foreground colors for fill pattern
405 //the previous blacked out pixels are now merged with the layer color
406 //while the non blacked out pixels stay as they are.
407 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
409 //now define what will be the color of the fillpattern parts that are not transparent
410 // memDC->SetTextBackground(*wxBLUE);
411 memDC
->SetTextBackground(m_owner
->m_colourForeground
);
412 memDC
->SetLogicalFunction(wxOR
);
415 //don't understand how but the outline is also depending on logicalfunction
416 memDC
->SetPen( wxPen( "red", 4, wxSOLID
) );
417 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
419 memDC
->SetLogicalFunction(wxCOPY
);
421 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
422 memDC
->SetBrush( wxNullBrush
);
423 memDC
->SetBrush( *brush36
);
424 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
425 memDC
->SetTextBackground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
426 memDC
->SetLogicalFunction(wxAND_INVERT
);
428 memDC
->DrawRectangle( 10, 10, 200, 200 );
430 // Set background and foreground colors for fill pattern
431 //the previous blacked out pixels are now merged with the layer color
432 //while the non blacked out pixels stay as they are.
433 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
434 //now define what will be the color of the fillpattern parts that are not transparent
435 // memDC->SetTextBackground(*wxRED);
436 memDC
->SetTextBackground(m_owner
->m_colourBackground
);
437 memDC
->SetLogicalFunction(wxOR
);
439 //don't understand how but the outline is also depending on logicalfunction
440 memDC
->SetPen( wxPen( "yellow", 4, wxSOLID
) );
441 memDC
->DrawRectangle( 10, 10, 200, 200 );
443 memDC
->SetBrush(wxNullBrush
);
444 memDC
->SetPen(wxNullPen
);
447 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
452 case 2: //now with transparent inversed fillpatterns
454 wxBitmap
* bmpBlit
= new wxBitmap(600,400);
455 wxMemoryDC
* memDC
= new wxMemoryDC();
456 wxBrush
_clearbrush(*wxWHITE
,wxSOLID
);
457 memDC
->SelectObject(*bmpBlit
);
458 memDC
->BeginDrawing();
459 memDC
->SetBackground(_clearbrush
);
461 memDC
->SetBackground(wxNullBrush
);
463 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
464 memDC
->SetBrush( *brush4
);
465 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
466 memDC
->SetTextForeground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
467 memDC
->SetLogicalFunction(wxAND_INVERT
);
469 // BLACK OUT the opaque pixels and leave the rest as is
470 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
472 // Set background and foreground colors for fill pattern
473 //the previous blacked out pixels are now merged with the layer color
474 //while the non blacked out pixels stay as they are.
475 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
477 //now define what will be the color of the fillpattern parts that are not transparent
478 memDC
->SetTextForeground(m_owner
->m_colourForeground
);
479 memDC
->SetLogicalFunction(wxOR
);
482 //don't understand how but the outline is also depending on logicalfunction
483 memDC
->SetPen( wxPen( "red", 4, wxSOLID
) );
484 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
486 memDC
->SetLogicalFunction(wxCOPY
);
488 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
489 memDC
->SetBrush( *brush36
);
490 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
491 memDC
->SetTextForeground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
492 memDC
->SetLogicalFunction(wxAND_INVERT
);
494 memDC
->DrawRectangle( 10,10, 200, 200 );
496 // Set background and foreground colors for fill pattern
497 //the previous blacked out pixels are now merged with the layer color
498 //while the non blacked out pixels stay as they are.
499 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
500 //now define what will be the color of the fillpattern parts that are not transparent
501 memDC
->SetTextForeground(m_owner
->m_colourBackground
);
502 memDC
->SetLogicalFunction(wxOR
);
504 //don't understand how but the outline is also depending on logicalfunction
505 memDC
->SetPen( wxPen( "yellow", 4, wxSOLID
) );
506 memDC
->DrawRectangle( 10, 10, 200, 200 );
508 memDC
->SetBrush(wxNullBrush
);
509 memDC
->SetPen(wxNullPen
);
510 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
520 void MyCanvas::DrawTestLines( int x
, int y
, int width
, wxDC
&dc
)
522 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
523 dc
.SetBrush( *wxRED_BRUSH
);
524 dc
.DrawRectangle( x
+10, y
+10, 100, 190 );
526 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
527 dc
.DrawLine( x
+20, y
+20, 100, y
+20 );
528 dc
.SetPen( wxPen( "black", width
, wxDOT
) );
529 dc
.DrawLine( x
+20, y
+30, 100, y
+30 );
530 dc
.SetPen( wxPen( "black", width
, wxSHORT_DASH
) );
531 dc
.DrawLine( x
+20, y
+40, 100, y
+40 );
532 dc
.SetPen( wxPen( "black", width
, wxLONG_DASH
) );
533 dc
.DrawLine( x
+20, y
+50, 100, y
+50 );
534 dc
.SetPen( wxPen( "black", width
, wxDOT_DASH
) );
535 dc
.DrawLine( x
+20, y
+60, 100, y
+60 );
537 dc
.SetPen( wxPen( "black", width
, wxBDIAGONAL_HATCH
) );
538 dc
.DrawLine( x
+20, y
+70, 100, y
+70 );
539 dc
.SetPen( wxPen( "black", width
, wxCROSSDIAG_HATCH
) );
540 dc
.DrawLine( x
+20, y
+80, 100, y
+80 );
541 dc
.SetPen( wxPen( "black", width
, wxFDIAGONAL_HATCH
) );
542 dc
.DrawLine( x
+20, y
+90, 100, y
+90 );
543 dc
.SetPen( wxPen( "black", width
, wxCROSS_HATCH
) );
544 dc
.DrawLine( x
+20, y
+100, 100, y
+100 );
545 dc
.SetPen( wxPen( "black", width
, wxHORIZONTAL_HATCH
) );
546 dc
.DrawLine( x
+20, y
+110, 100, y
+110 );
547 dc
.SetPen( wxPen( "black", width
, wxVERTICAL_HATCH
) );
548 dc
.DrawLine( x
+20, y
+120, 100, y
+120 );
550 wxPen
ud( "black", width
, wxUSER_DASH
);
553 ud
.SetDashes( 1, dash1
);
554 dc
.DrawLine( x
+20, y
+140, 100, y
+140 );
556 ud
.SetDashes( 1, dash1
);
557 dc
.DrawLine( x
+20, y
+150, 100, y
+150 );
559 ud
.SetDashes( 1, dash1
);
560 dc
.DrawLine( x
+20, y
+160, 100, y
+160 );
562 ud
.SetDashes( 1, dash1
);
563 dc
.DrawLine( x
+20, y
+170, 100, y
+170 );
566 void MyCanvas::DrawDefault(wxDC
& dc
)
569 dc
.DrawCircle(0, 0, 10);
570 #if !(defined __WXGTK__) && !(defined __WXMOTIF__)
571 // not implemented in wxGTK or wxMOTIF :-(
572 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
575 dc
.DrawIcon( wxICON(mondrian
), 40, 40 );
577 // this is the test for "blitting bitmap into DC damages selected brush"
579 dc
.SetPen(*wxTRANSPARENT_PEN
);
580 dc
.SetBrush( *wxGREEN_BRUSH
);
581 dc
.DrawRectangle(100, 10, 40, 40);
582 dc
.DrawBitmap(wxTheApp
->GetStdIcon(wxICON_INFORMATION
), 102, 12, TRUE
);
583 dc
.DrawRectangle(150, 10, 40, 40);
584 dc
.DrawIcon(wxTheApp
->GetStdIcon(wxICON_INFORMATION
), 152, 12);
585 dc
.DrawRectangle(200, 10, 40, 40);
587 // test for "transparent" bitmap drawing (it intersects with the last
589 //dc.SetBrush( *wxTRANSPARENT_BRUSH );
590 #include "../image/smile.xpm"
591 wxBitmap
bmp(smile_xpm
);
592 dc
.DrawBitmap(bmp
, 210, 30, TRUE
);
594 dc
.SetBrush( *wxBLACK_BRUSH
);
595 dc
.DrawRectangle( 0, 160, 1000, 300 );
598 wxBitmap
bitmap(20,70);
600 memdc
.SelectObject( bitmap
);
601 memdc
.SetBrush( *wxBLACK_BRUSH
);
602 memdc
.SetPen( *wxWHITE_PEN
);
603 memdc
.DrawRectangle(0,0,20,70);
604 memdc
.DrawLine( 10,0,10,70 );
607 wxPen pen
= *wxRED_PEN
;
610 memdc
.DrawLine( 10, 5,10, 5 );
611 memdc
.DrawLine( 10,10,11,10 );
612 memdc
.DrawLine( 10,15,12,15 );
613 memdc
.DrawLine( 10,20,13,20 );
616 memdc.SetPen(*wxRED_PEN);
617 memdc.DrawLine( 12, 5,12, 5 );
618 memdc.DrawLine( 12,10,13,10 );
619 memdc.DrawLine( 12,15,14,15 );
620 memdc.DrawLine( 12,20,15,20 );
624 memdc
.DrawLine( 10,25,10,25 );
625 memdc
.DrawLine( 10,30, 9,30 );
626 memdc
.DrawLine( 10,35, 8,35 );
627 memdc
.DrawLine( 10,40, 7,40 );
630 dc
.SetPen(*wxWHITE_PEN
);
631 memdc
.SetLogicalFunction( wxINVERT
);
632 memdc
.SetPen( *wxWHITE_PEN
);
633 memdc
.DrawLine( 10,50,10,50 );
634 memdc
.DrawLine( 10,55,11,55 );
635 memdc
.DrawLine( 10,60,12,60 );
636 memdc
.DrawLine( 10,65,13,65 );
638 memdc
.DrawLine( 12,50,12,50 );
639 memdc
.DrawLine( 12,55,13,55 );
640 memdc
.DrawLine( 12,60,14,60 );
641 memdc
.DrawLine( 12,65,15,65 );
643 memdc
.SelectObject( wxNullBitmap
);
644 dc
.DrawBitmap( bitmap
, 10, 170 );
645 wxImage
image( bitmap
);
646 image
.Rescale( 60,210 );
647 bitmap
= image
.ConvertToBitmap();
648 dc
.DrawBitmap( bitmap
, 50, 170 );
650 // test the rectangle outline drawing - there should be one pixel between
651 // the rect and the lines
652 dc
.SetPen(*wxWHITE_PEN
);
653 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
654 dc
.DrawRectangle(150, 170, 49, 29);
655 dc
.DrawRectangle(200, 170, 49, 29);
656 dc
.SetPen(*wxWHITE_PEN
);
657 dc
.DrawLine(250, 210, 250, 170);
658 dc
.DrawLine(260, 200, 150, 200);
660 // test the rectangle filled drawing - there should be one pixel between
661 // the rect and the lines
662 dc
.SetPen(*wxTRANSPARENT_PEN
);
663 dc
.SetBrush( *wxWHITE_BRUSH
);
664 dc
.DrawRectangle(300, 170, 49, 29);
665 dc
.DrawRectangle(350, 170, 49, 29);
666 dc
.SetPen(*wxWHITE_PEN
);
667 dc
.DrawLine(400, 170, 400, 210);
668 dc
.DrawLine(300, 200, 410, 200);
670 // and now for filled rect with outline
671 dc
.SetPen(*wxRED_PEN
);
672 dc
.SetBrush( *wxWHITE_BRUSH
);
673 dc
.DrawRectangle(500, 170, 49, 29);
674 dc
.DrawRectangle(550, 170, 49, 29);
675 dc
.SetPen(*wxWHITE_PEN
);
676 dc
.DrawLine(600, 170, 600, 210);
677 dc
.DrawLine(500, 200, 610, 200);
679 // test the rectangle outline drawing - there should be one pixel between
680 // the rect and the lines
681 dc
.SetPen(*wxWHITE_PEN
);
682 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
683 dc
.DrawRoundedRectangle(150, 270, 49, 29, 6);
684 dc
.DrawRoundedRectangle(200, 270, 49, 29, 6);
685 dc
.SetPen(*wxWHITE_PEN
);
686 dc
.DrawLine(250, 270, 250, 310);
687 dc
.DrawLine(150, 300, 260, 300);
689 // test the rectangle filled drawing - there should be one pixel between
690 // the rect and the lines
691 dc
.SetPen(*wxTRANSPARENT_PEN
);
692 dc
.SetBrush( *wxWHITE_BRUSH
);
693 dc
.DrawRoundedRectangle(300, 270, 49, 29, 6);
694 dc
.DrawRoundedRectangle(350, 270, 49, 29, 6);
695 dc
.SetPen(*wxWHITE_PEN
);
696 dc
.DrawLine(400, 270, 400, 310);
697 dc
.DrawLine(300, 300, 410, 300);
701 void MyCanvas::DrawText(wxDC
& dc
)
703 // set underlined font for testing
704 dc
.SetFont( wxFont(12, wxMODERN
, wxNORMAL
, wxNORMAL
, TRUE
) );
705 dc
.DrawText( "This is text", 110, 10 );
706 dc
.DrawRotatedText( "That is text", 20, 10, -45 );
708 dc
.SetFont( *wxNORMAL_FONT
);
711 dc
. SetBackgroundMode(wxTRANSPARENT
);
713 for ( int n
= -180; n
< 180; n
+= 30 )
715 text
.Printf(" %d rotated text", n
);
716 dc
.DrawRotatedText(text
, 400, 400, n
);
719 dc
.SetFont( wxFont( 18, wxSWISS
, wxNORMAL
, wxNORMAL
) );
721 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
726 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
727 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
728 dc
.DrawText( text
, 110, 80 );
730 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
731 dc
.DrawText( text
, 110, 120 );
733 dc
.DrawRectangle( 100, 40, 4, height
);
740 } rasterOperations
[] =
743 { "wxAND_INVERT", wxAND_INVERT
},
744 { "wxAND_REVERSE", wxAND_REVERSE
},
745 { "wxCLEAR", wxCLEAR
},
746 { "wxCOPY", wxCOPY
},
747 { "wxEQUIV", wxEQUIV
},
748 { "wxINVERT", wxINVERT
},
749 { "wxNAND", wxNAND
},
750 { "wxNO_OP", wxNO_OP
},
752 { "wxOR_INVERT", wxOR_INVERT
},
753 { "wxOR_REVERSE", wxOR_REVERSE
},
755 { "wxSRC_INVERT", wxSRC_INVERT
},
759 void MyCanvas::DrawImages(wxDC
& dc
)
761 dc
.DrawText("original image", 0, 0);
762 dc
.DrawBitmap(gs_bmpNoMask
, 0, 20, 0);
763 dc
.DrawText("with colour mask", 0, 100);
764 dc
.DrawBitmap(gs_bmpWithColMask
, 0, 120, TRUE
);
765 dc
.DrawText("the mask image", 0, 200);
766 dc
.DrawBitmap(gs_bmpMask
, 0, 220, 0);
767 dc
.DrawText("masked image", 0, 300);
768 dc
.DrawBitmap(gs_bmpWithMask
, 0, 320, TRUE
);
770 int cx
= gs_bmpWithColMask
.GetWidth(),
771 cy
= gs_bmpWithColMask
.GetHeight();
774 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
776 wxCoord x
= 120 + 150*(n%4
),
779 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
780 memDC
.SelectObject(gs_bmpWithColMask
);
781 dc
.Blit(x
, y
, cx
, cy
, &memDC
, 0, 0, rasterOperations
[n
].rop
, TRUE
);
785 void MyCanvas::DrawWithLogicalOps(wxDC
& dc
)
787 static const wxCoord w
= 60;
788 static const wxCoord h
= 60;
790 // reuse the text colour here
791 dc
.SetPen(wxPen(m_owner
->m_colourForeground
, 1, wxSOLID
));
792 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
794 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
796 wxCoord x
= 20 + 150*(n%4
),
799 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
800 dc
.SetLogicalFunction(rasterOperations
[n
].rop
);
801 dc
.DrawRectangle(x
, y
, w
, h
);
802 dc
.DrawLine(x
, y
, x
+ w
, y
+ h
);
803 dc
.DrawLine(x
+ w
, y
, x
, y
+ h
);
807 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
811 m_owner
->PrepareDC(dc
);
813 dc
.SetBackgroundMode( m_owner
->m_backgroundMode
);
814 if ( m_owner
->m_backgroundBrush
.Ok() )
815 dc
.SetBackground( m_owner
->m_backgroundBrush
);
816 if ( m_owner
->m_colourForeground
.Ok() )
817 dc
.SetTextForeground( m_owner
->m_colourForeground
);
818 if ( m_owner
->m_colourBackground
.Ok() )
819 dc
.SetTextBackground( m_owner
->m_colourBackground
);
821 if ( m_owner
->m_textureBackground
) {
822 if ( ! m_owner
->m_backgroundBrush
.Ok() ) {
823 wxBrush
b(wxColour(0,128,0), wxSOLID
);
830 if ( m_owner
->m_textureBackground
) {
831 dc
.SetPen(*wxMEDIUM_GREY_PEN
);
832 for (int i
=0; i
<200; i
++)
833 dc
.DrawLine(0, i
*10, i
*10, 0);
847 DrawTestLines( 0, 100, 0, dc
);
848 DrawTestLines( 0, 300, 1, dc
);
849 DrawTestLines( 0, 500, 2, dc
);
850 DrawTestLines( 0, 700, 6, dc
);
854 DrawTestPoly( 0, 100, dc
, 0 );
855 DrawTestPoly( 33, 500, dc
, 1 );
856 DrawTestPoly( 43, 1000, dc
, 2 );
864 DrawWithLogicalOps(dc
);
869 void MyCanvas::OnMouseMove(wxMouseEvent
&event
)
873 m_owner
->PrepareDC(dc
);
875 wxPoint pos
= event
.GetPosition();
876 long x
= dc
.DeviceToLogicalX( pos
.x
);
877 long y
= dc
.DeviceToLogicalY( pos
.y
);
879 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
880 m_owner
->SetStatusText( str
);
883 // ----------------------------------------------------------------------------
885 // ----------------------------------------------------------------------------
887 // the event tables connect the wxWindows events with the functions (event
888 // handlers) which process them. It can be also done at run-time, but for the
889 // simple menu events like this the static method is much simpler.
890 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
891 EVT_MENU (File_Quit
, MyFrame::OnQuit
)
892 EVT_MENU (File_About
, MyFrame::OnAbout
)
894 EVT_MENU_RANGE(MenuShow_First
, MenuShow_Last
, MyFrame::OnShow
)
896 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
900 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
901 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
903 // set the frame icon
904 SetIcon(wxICON(mondrian
));
906 wxMenu
*menuFile
= new wxMenu
;
907 menuFile
->Append(File_ShowDefault
, "&Default screen\tF1");
908 menuFile
->Append(File_ShowText
, "&Text screen\tF2");
909 menuFile
->Append(File_ShowLines
, "&Lines screen\tF3");
910 menuFile
->Append(File_ShowPolygons
, "&Polygons screen\tF4");
911 menuFile
->Append(File_ShowMask
, "wx&Mask screen\tF5");
912 menuFile
->Append(File_ShowOps
, "&ROP screen\tF6");
913 menuFile
->AppendSeparator();
914 menuFile
->Append(File_About
, "&About...\tCtrl-A", "Show about dialog");
915 menuFile
->AppendSeparator();
916 menuFile
->Append(File_Quit
, "E&xit\tAlt-X", "Quit this program");
918 wxMenu
*menuMapMode
= new wxMenu
;
919 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
920 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
921 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
922 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
923 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
925 wxMenu
*menuUserScale
= new wxMenu
;
926 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
927 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
928 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
929 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
930 menuUserScale
->AppendSeparator();
931 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
933 wxMenu
*menuAxis
= new wxMenu
;
934 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-M", "", TRUE
);
935 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-N", "", TRUE
);
937 wxMenu
*menuLogical
= new wxMenu
;
938 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
939 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
940 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
941 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
943 wxMenu
*menuColour
= new wxMenu
;
944 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
945 menuColour
->Append( Colour_TextBackground
, "Text background..." );
946 menuColour
->Append( Colour_Background
, "Background colour..." );
947 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
948 menuColour
->Append( Colour_TextureBackgound
, "Draw textured background\tCtrl-T", "", TRUE
);
950 // now append the freshly created menu to the menu bar...
951 wxMenuBar
*menuBar
= new wxMenuBar
;
952 menuBar
->Append(menuFile
, "&File");
953 menuBar
->Append(menuMapMode
, "&MapMode");
954 menuBar
->Append(menuUserScale
, "&UserScale");
955 menuBar
->Append(menuAxis
, "&Axis");
956 menuBar
->Append(menuLogical
, "&LogicalOrigin");
957 menuBar
->Append(menuColour
, "&Colours");
959 // ... and attach this menu bar to the frame
962 // create a status bar just for fun (by default with 1 pane only)
964 SetStatusText("Welcome to wxWindows!");
966 m_mapMode
= wxMM_TEXT
;
969 m_xLogicalOrigin
= 0;
970 m_yLogicalOrigin
= 0;
972 m_yAxisReversed
= FALSE
;
973 m_backgroundMode
= wxSOLID
;
974 m_colourForeground
= *wxRED
;
975 m_colourBackground
= *wxBLUE
;
976 m_textureBackground
= FALSE
;
978 m_canvas
= new MyCanvas( this );
979 m_canvas
->SetScrollbars( 10, 10, 100, 240 );
984 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
986 // TRUE is to force the frame to close
990 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
993 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
994 _T("Copyright (c) Robert Roebling 1999")
997 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
1000 void MyFrame::OnShow(wxCommandEvent
& event
)
1002 m_canvas
->Show((ScreenToShow
)(event
.GetInt() - MenuShow_First
));
1005 void MyFrame::OnOption(wxCommandEvent
& event
)
1007 switch (event
.GetInt())
1010 m_mapMode
= wxMM_TEXT
;
1012 case MapMode_Lometric
:
1013 m_mapMode
= wxMM_LOMETRIC
;
1016 m_mapMode
= wxMM_TWIPS
;
1018 case MapMode_Points
:
1019 m_mapMode
= wxMM_POINTS
;
1021 case MapMode_Metric
:
1022 m_mapMode
= wxMM_METRIC
;
1025 case LogicalOrigin_MoveDown
:
1026 m_yLogicalOrigin
+= 10;
1028 case LogicalOrigin_MoveUp
:
1029 m_yLogicalOrigin
-= 10;
1031 case LogicalOrigin_MoveLeft
:
1032 m_xLogicalOrigin
+= 10;
1034 case LogicalOrigin_MoveRight
:
1035 m_xLogicalOrigin
-= 10;
1038 case UserScale_StretchHoriz
:
1039 m_xUserScale
*= 1.10;
1041 case UserScale_ShrinkHoriz
:
1042 m_xUserScale
/= 1.10;
1044 case UserScale_StretchVertic
:
1045 m_yUserScale
*= 1.10;
1047 case UserScale_ShrinkVertic
:
1048 m_yUserScale
/= 1.10;
1050 case UserScale_Restore
:
1055 case AxisMirror_Vertic
:
1056 m_yAxisReversed
= !m_yAxisReversed
;
1058 case AxisMirror_Horiz
:
1059 m_xAxisReversed
= !m_xAxisReversed
;
1062 case Colour_TextForeground
:
1063 m_colourForeground
= SelectColour();
1065 case Colour_TextBackground
:
1066 m_colourBackground
= SelectColour();
1068 case Colour_Background
:
1070 wxColour col
= SelectColour();
1073 m_backgroundBrush
.SetColour(col
);
1077 case Colour_BackgroundMode
:
1078 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
1082 case Colour_TextureBackgound
:
1083 m_textureBackground
= ! m_textureBackground
;
1091 m_canvas
->Refresh();
1094 void MyFrame::PrepareDC(wxDC
& dc
)
1096 dc
.SetMapMode( m_mapMode
);
1097 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
1098 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
1099 dc
.SetAxisOrientation( !m_xAxisReversed
, m_yAxisReversed
);
1102 wxColour
MyFrame::SelectColour()
1106 wxColourDialog
dialog(this, &data
);
1108 if ( dialog
.ShowModal() == wxID_OK
)
1110 col
= dialog
.GetColourData().GetColour();