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
;
119 int m_xLogicalOrigin
;
120 int m_yLogicalOrigin
;
121 bool m_xAxisReversed
,
123 wxColour m_colourForeground
, // these are _text_ colours
125 wxBrush m_backgroundBrush
;
129 // any class wishing to process wxWindows events must use this macro
130 DECLARE_EVENT_TABLE()
133 // define a scrollable canvas for drawing onto
134 class MyCanvas
: public wxScrolledWindow
137 MyCanvas( MyFrame
*parent
);
139 void OnPaint(wxPaintEvent
&event
);
140 void OnMouseMove(wxMouseEvent
&event
);
142 void Show(ScreenToShow show
) { m_show
= show
; Refresh(); }
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
);
157 DECLARE_EVENT_TABLE()
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 // IDs for the controls and the menu commands
172 File_ShowDefault
= MenuShow_First
,
178 MenuShow_Last
= File_ShowOps
,
182 MapMode_Text
= MenuOption_First
,
188 UserScale_StretchHoriz
,
189 UserScale_ShrinkHoriz
,
190 UserScale_StretchVertic
,
191 UserScale_ShrinkVertic
,
197 LogicalOrigin_MoveDown
,
198 LogicalOrigin_MoveUp
,
199 LogicalOrigin_MoveLeft
,
200 LogicalOrigin_MoveRight
,
202 Colour_TextForeground
,
203 Colour_TextBackground
,
205 Colour_BackgroundMode
,
207 MenuOption_Last
= Colour_BackgroundMode
210 // ----------------------------------------------------------------------------
211 // event tables and other macros for wxWindows
212 // ----------------------------------------------------------------------------
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
222 // ============================================================================
224 // ============================================================================
226 // ----------------------------------------------------------------------------
227 // the application class
228 // ----------------------------------------------------------------------------
230 bool MyApp::LoadImages()
236 wxString path
= pathList
.FindValidPath("pat4.bmp");
239 gs_bmp4
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
240 wxMask
* mask4
= new wxMask(gs_bmp4
, *wxBLACK
);
241 gs_bmp4
.SetMask(mask4
);
243 path
= pathList
.FindValidPath("pat36.bmp");
246 gs_bmp36
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
247 wxMask
* mask36
= new wxMask(gs_bmp36
, *wxBLACK
);
248 gs_bmp36
.SetMask(mask36
);
250 path
= pathList
.FindValidPath("image.bmp");
253 gs_bmpNoMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
254 gs_bmpWithMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
255 gs_bmpWithColMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
257 path
= pathList
.FindValidPath("mask.bmp");
260 gs_bmpMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
262 // This is so wrong, it hurts.
263 // gs_bmpMask.SetDepth(1);
264 // wxMask *mask = new wxMask(gs_bmpMask);
266 wxMask
*mask
= new wxMask(gs_bmpMask
, *wxBLACK
);
267 gs_bmpWithMask
.SetMask(mask
);
269 mask
= new wxMask(gs_bmpWithColMask
, *wxWHITE
);
270 gs_bmpWithColMask
.SetMask(mask
);
275 // `Main program' equivalent: the program execution "starts" here
278 // Create the main application window
279 MyFrame
*frame
= new MyFrame("Drawing sample",
280 wxPoint(50, 50), wxSize(550, 340));
282 // Show it and tell the application that it's our main window
288 wxLogError("Can't load one of the bitmap files needed for this sample "
289 "from the current or parent directory, please copy them "
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
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
)
311 MyCanvas::MyCanvas( MyFrame
*parent
) : wxScrolledWindow( parent
)
314 m_show
= Show_Default
;
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
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,
327 //variations are endless using other logical functions
328 void MyCanvas::DrawTestPoly( int x
, int y
,wxDC
&dc
,int transparent
)
330 wxBrush
* brush4
= new wxBrush(gs_bmp4
);
331 wxBrush
* brush36
= new wxBrush(gs_bmp36
);
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;
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
);
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
);
379 case 1: //now with transparent fillpatterns
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
);
390 memDC
->SetBackground(wxNullBrush
);
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
);
399 // BLACK OUT the opaque pixels and leave the rest as is
400 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
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)
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
);
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
);
417 memDC
->SetLogicalFunction(wxCOPY
);
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
);
426 memDC
->DrawRectangle( 10, 10, 200, 200 );
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
);
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 );
441 memDC
->SetBrush(wxNullBrush
);
442 memDC
->SetPen(wxNullPen
);
445 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
450 case 2: //now with transparent inversed fillpatterns
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
);
459 memDC
->SetBackground(wxNullBrush
);
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
);
467 // BLACK OUT the opaque pixels and leave the rest as is
468 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
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)
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
);
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
);
484 memDC
->SetLogicalFunction(wxCOPY
);
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
);
492 memDC
->DrawRectangle( 10,10, 200, 200 );
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
);
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 );
506 memDC
->SetBrush(wxNullBrush
);
507 memDC
->SetPen(wxNullPen
);
508 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
518 void MyCanvas::DrawTestLines( int x
, int y
, int width
, wxDC
&dc
)
520 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
521 dc
.SetBrush( *wxRED_BRUSH
);
522 dc
.DrawRectangle( x
+10, y
+10, 100, 190 );
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 );
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 );
548 wxPen
ud( "black", width
, wxUSER_DASH
);
551 ud
.SetDashes( 1, dash1
);
552 dc
.DrawLine( x
+20, y
+140, 100, y
+140 );
554 ud
.SetDashes( 1, dash1
);
555 dc
.DrawLine( x
+20, y
+150, 100, y
+150 );
557 ud
.SetDashes( 1, dash1
);
558 dc
.DrawLine( x
+20, y
+160, 100, y
+160 );
560 ud
.SetDashes( 1, dash1
);
561 dc
.DrawLine( x
+20, y
+170, 100, y
+170 );
564 void MyCanvas::DrawDefault(wxDC
& dc
)
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));
573 dc
.DrawIcon( wxICON(mondrian
), 40, 40 );
575 // this is the test for "blitting bitmap into DC damages selected brush"
577 dc
.SetPen(*wxTRANSPARENT_PEN
);
578 dc
.SetBrush( *wxGREEN_BRUSH
);
579 dc
.DrawRectangle(100, 10, 40, 40);
580 dc
.DrawBitmap(wxTheApp
->GetStdIcon(wxICON_INFORMATION
), 102, 12, TRUE
);
581 dc
.DrawRectangle(150, 10, 40, 40);
582 dc
.DrawIcon(wxTheApp
->GetStdIcon(wxICON_INFORMATION
), 152, 12);
583 dc
.DrawRectangle(200, 10, 40, 40);
585 // test for "transparent" bitmap drawing (it intersects with the last
587 //dc.SetBrush( *wxTRANSPARENT_BRUSH );
588 #include "../image/smile.xpm"
589 wxBitmap
bmp(smile_xpm
);
590 dc
.DrawBitmap(bmp
, 210, 30, TRUE
);
592 dc
.SetBrush( *wxBLACK_BRUSH
);
593 dc
.DrawRectangle( 0, 160, 1000, 300 );
596 wxBitmap
bitmap(20,70);
598 memdc
.SelectObject( bitmap
);
599 memdc
.SetBrush( *wxBLACK_BRUSH
);
600 memdc
.SetPen( *wxWHITE_PEN
);
601 memdc
.DrawRectangle(0,0,20,70);
602 memdc
.DrawLine( 10,0,10,70 );
605 wxPen pen
= *wxRED_PEN
;
608 memdc
.DrawLine( 10, 5,10, 5 );
609 memdc
.DrawLine( 10,10,11,10 );
610 memdc
.DrawLine( 10,15,12,15 );
611 memdc
.DrawLine( 10,20,13,20 );
614 memdc.SetPen(*wxRED_PEN);
615 memdc.DrawLine( 12, 5,12, 5 );
616 memdc.DrawLine( 12,10,13,10 );
617 memdc.DrawLine( 12,15,14,15 );
618 memdc.DrawLine( 12,20,15,20 );
622 memdc
.DrawLine( 10,25,10,25 );
623 memdc
.DrawLine( 10,30, 9,30 );
624 memdc
.DrawLine( 10,35, 8,35 );
625 memdc
.DrawLine( 10,40, 7,40 );
628 dc
.SetPen(*wxWHITE_PEN
);
629 memdc
.SetLogicalFunction( wxINVERT
);
630 memdc
.SetPen( *wxWHITE_PEN
);
631 memdc
.DrawLine( 10,50,10,50 );
632 memdc
.DrawLine( 10,55,11,55 );
633 memdc
.DrawLine( 10,60,12,60 );
634 memdc
.DrawLine( 10,65,13,65 );
636 memdc
.DrawLine( 12,50,12,50 );
637 memdc
.DrawLine( 12,55,13,55 );
638 memdc
.DrawLine( 12,60,14,60 );
639 memdc
.DrawLine( 12,65,15,65 );
641 memdc
.SelectObject( wxNullBitmap
);
642 dc
.DrawBitmap( bitmap
, 10, 170 );
643 wxImage
image( bitmap
);
644 image
.Rescale( 60,210 );
645 bitmap
= image
.ConvertToBitmap();
646 dc
.DrawBitmap( bitmap
, 50, 170 );
648 // test the rectangle outline drawing - there should be one pixel between
649 // the rect and the lines
650 dc
.SetPen(*wxWHITE_PEN
);
651 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
652 dc
.DrawRectangle(150, 170, 49, 29);
653 dc
.DrawRectangle(200, 170, 49, 29);
654 dc
.SetPen(*wxWHITE_PEN
);
655 dc
.DrawLine(250, 210, 250, 170);
656 dc
.DrawLine(260, 200, 150, 200);
658 // test the rectangle filled drawing - there should be one pixel between
659 // the rect and the lines
660 dc
.SetPen(*wxTRANSPARENT_PEN
);
661 dc
.SetBrush( *wxWHITE_BRUSH
);
662 dc
.DrawRectangle(300, 170, 49, 29);
663 dc
.DrawRectangle(350, 170, 49, 29);
664 dc
.SetPen(*wxWHITE_PEN
);
665 dc
.DrawLine(400, 170, 400, 210);
666 dc
.DrawLine(300, 200, 410, 200);
668 // and now for filled rect with outline
669 dc
.SetPen(*wxRED_PEN
);
670 dc
.SetBrush( *wxWHITE_BRUSH
);
671 dc
.DrawRectangle(500, 170, 49, 29);
672 dc
.DrawRectangle(550, 170, 49, 29);
673 dc
.SetPen(*wxWHITE_PEN
);
674 dc
.DrawLine(600, 170, 600, 210);
675 dc
.DrawLine(500, 200, 610, 200);
677 // test the rectangle outline drawing - there should be one pixel between
678 // the rect and the lines
679 dc
.SetPen(*wxWHITE_PEN
);
680 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
681 dc
.DrawRoundedRectangle(150, 270, 49, 29, 6);
682 dc
.DrawRoundedRectangle(200, 270, 49, 29, 6);
683 dc
.SetPen(*wxWHITE_PEN
);
684 dc
.DrawLine(250, 270, 250, 310);
685 dc
.DrawLine(150, 300, 260, 300);
687 // test the rectangle filled drawing - there should be one pixel between
688 // the rect and the lines
689 dc
.SetPen(*wxTRANSPARENT_PEN
);
690 dc
.SetBrush( *wxWHITE_BRUSH
);
691 dc
.DrawRoundedRectangle(300, 270, 49, 29, 6);
692 dc
.DrawRoundedRectangle(350, 270, 49, 29, 6);
693 dc
.SetPen(*wxWHITE_PEN
);
694 dc
.DrawLine(400, 270, 400, 310);
695 dc
.DrawLine(300, 300, 410, 300);
699 void MyCanvas::DrawText(wxDC
& dc
)
701 // set underlined font for testing
702 dc
.SetFont( wxFont(12, wxMODERN
, wxNORMAL
, wxNORMAL
, TRUE
) );
703 dc
.DrawText( "This is text", 110, 10 );
704 dc
.DrawRotatedText( "That is text", 20, 10, -45 );
706 dc
.SetFont( *wxNORMAL_FONT
);
709 dc
. SetBackgroundMode(wxTRANSPARENT
);
711 for ( int n
= -180; n
< 180; n
+= 30 )
713 text
.Printf(" %d rotated text", n
);
714 dc
.DrawRotatedText(text
, 400, 400, n
);
717 dc
.SetFont( wxFont( 18, wxSWISS
, wxNORMAL
, wxNORMAL
) );
719 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
724 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
725 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
726 dc
.DrawText( text
, 110, 80 );
728 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
729 dc
.DrawText( text
, 110, 120 );
731 dc
.DrawRectangle( 100, 40, 4, height
);
738 } rasterOperations
[] =
741 { "wxAND_INVERT", wxAND_INVERT
},
742 { "wxAND_REVERSE", wxAND_REVERSE
},
743 { "wxCLEAR", wxCLEAR
},
744 { "wxCOPY", wxCOPY
},
745 { "wxEQUIV", wxEQUIV
},
746 { "wxINVERT", wxINVERT
},
747 { "wxNAND", wxNAND
},
748 { "wxNO_OP", wxNO_OP
},
750 { "wxOR_INVERT", wxOR_INVERT
},
751 { "wxOR_REVERSE", wxOR_REVERSE
},
753 { "wxSRC_INVERT", wxSRC_INVERT
},
757 void MyCanvas::DrawImages(wxDC
& dc
)
759 dc
.DrawText("original image", 0, 0);
760 dc
.DrawBitmap(gs_bmpNoMask
, 0, 20, 0);
761 dc
.DrawText("with colour mask", 0, 100);
762 dc
.DrawBitmap(gs_bmpWithColMask
, 0, 120, TRUE
);
763 dc
.DrawText("the mask image", 0, 200);
764 dc
.DrawBitmap(gs_bmpMask
, 0, 220, 0);
765 dc
.DrawText("masked image", 0, 300);
766 dc
.DrawBitmap(gs_bmpWithMask
, 0, 320, TRUE
);
768 int cx
= gs_bmpWithColMask
.GetWidth(),
769 cy
= gs_bmpWithColMask
.GetHeight();
772 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
774 wxCoord x
= 120 + 150*(n%4
),
777 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
778 memDC
.SelectObject(gs_bmpWithColMask
);
779 dc
.Blit(x
, y
, cx
, cy
, &memDC
, 0, 0, rasterOperations
[n
].rop
, TRUE
);
783 void MyCanvas::DrawWithLogicalOps(wxDC
& dc
)
785 static const wxCoord w
= 60;
786 static const wxCoord h
= 60;
788 // reuse the text colour here
789 dc
.SetPen(wxPen(m_owner
->m_colourForeground
, 1, wxSOLID
));
790 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
792 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
794 wxCoord x
= 20 + 150*(n%4
),
797 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
798 dc
.SetLogicalFunction(rasterOperations
[n
].rop
);
799 dc
.DrawRectangle(x
, y
, w
, h
);
800 dc
.DrawLine(x
, y
, x
+ w
, y
+ h
);
801 dc
.DrawLine(x
+ w
, y
, x
, y
+ h
);
805 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
809 m_owner
->PrepareDC(dc
);
811 dc
.SetBackgroundMode( m_owner
->m_backgroundMode
);
812 if ( m_owner
->m_backgroundBrush
.Ok() )
813 dc
.SetBackground( m_owner
->m_backgroundBrush
);
814 if ( m_owner
->m_colourForeground
.Ok() )
815 dc
.SetTextForeground( m_owner
->m_colourForeground
);
816 if ( m_owner
->m_colourBackground
.Ok() )
817 dc
.SetTextBackground( m_owner
->m_colourBackground
);
830 DrawTestLines( 0, 100, 0, dc
);
831 DrawTestLines( 0, 300, 1, dc
);
832 DrawTestLines( 0, 500, 2, dc
);
833 DrawTestLines( 0, 700, 6, dc
);
837 DrawTestPoly( 0, 100, dc
, 0 );
838 DrawTestPoly( 33, 500, dc
, 1 );
839 DrawTestPoly( 43, 1000, dc
, 2 );
847 DrawWithLogicalOps(dc
);
852 void MyCanvas::OnMouseMove(wxMouseEvent
&event
)
856 m_owner
->PrepareDC(dc
);
858 wxPoint pos
= event
.GetPosition();
859 long x
= dc
.DeviceToLogicalX( pos
.x
);
860 long y
= dc
.DeviceToLogicalY( pos
.y
);
862 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
863 m_owner
->SetStatusText( str
);
866 // ----------------------------------------------------------------------------
868 // ----------------------------------------------------------------------------
870 // the event tables connect the wxWindows events with the functions (event
871 // handlers) which process them. It can be also done at run-time, but for the
872 // simple menu events like this the static method is much simpler.
873 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
874 EVT_MENU (File_Quit
, MyFrame::OnQuit
)
875 EVT_MENU (File_About
, MyFrame::OnAbout
)
877 EVT_MENU_RANGE(MenuShow_First
, MenuShow_Last
, MyFrame::OnShow
)
879 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
883 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
884 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
886 // set the frame icon
887 SetIcon(wxICON(mondrian
));
889 wxMenu
*menuFile
= new wxMenu
;
890 menuFile
->Append(File_ShowDefault
, "&Default screen\tF1");
891 menuFile
->Append(File_ShowText
, "&Text screen\tF2");
892 menuFile
->Append(File_ShowLines
, "&Lines screen\tF3");
893 menuFile
->Append(File_ShowPolygons
, "&Polygons screen\tF4");
894 menuFile
->Append(File_ShowMask
, "wx&Mask screen\tF5");
895 menuFile
->Append(File_ShowOps
, "&ROP screen\tF6");
896 menuFile
->AppendSeparator();
897 menuFile
->Append(File_About
, "&About...\tCtrl-A", "Show about dialog");
898 menuFile
->AppendSeparator();
899 menuFile
->Append(File_Quit
, "E&xit\tAlt-X", "Quit this program");
901 wxMenu
*menuMapMode
= new wxMenu
;
902 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
903 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
904 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
905 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
906 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
908 wxMenu
*menuUserScale
= new wxMenu
;
909 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
910 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
911 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
912 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
913 menuUserScale
->AppendSeparator();
914 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
916 wxMenu
*menuAxis
= new wxMenu
;
917 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-M", "", TRUE
);
918 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-N", "", TRUE
);
920 wxMenu
*menuLogical
= new wxMenu
;
921 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
922 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
923 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
924 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
926 wxMenu
*menuColour
= new wxMenu
;
927 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
928 menuColour
->Append( Colour_TextBackground
, "Text background..." );
929 menuColour
->Append( Colour_Background
, "Background colour..." );
930 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
932 // now append the freshly created menu to the menu bar...
933 wxMenuBar
*menuBar
= new wxMenuBar
;
934 menuBar
->Append(menuFile
, "&File");
935 menuBar
->Append(menuMapMode
, "&MapMode");
936 menuBar
->Append(menuUserScale
, "&UserScale");
937 menuBar
->Append(menuAxis
, "&Axis");
938 menuBar
->Append(menuLogical
, "&LogicalOrigin");
939 menuBar
->Append(menuColour
, "&Colours");
941 // ... and attach this menu bar to the frame
944 // create a status bar just for fun (by default with 1 pane only)
946 SetStatusText("Welcome to wxWindows!");
948 m_mapMode
= wxMM_TEXT
;
951 m_xLogicalOrigin
= 0;
952 m_yLogicalOrigin
= 0;
954 m_yAxisReversed
= FALSE
;
955 m_backgroundMode
= wxSOLID
;
956 m_colourForeground
= *wxRED
;
957 m_colourBackground
= *wxBLUE
;
959 m_canvas
= new MyCanvas( this );
960 m_canvas
->SetScrollbars( 10, 10, 100, 240 );
965 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
967 // TRUE is to force the frame to close
971 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
974 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
975 _T("Copyright (c) Robert Roebling 1999")
978 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
981 void MyFrame::OnShow(wxCommandEvent
& event
)
983 m_canvas
->Show((ScreenToShow
)(event
.GetInt() - MenuShow_First
));
986 void MyFrame::OnOption(wxCommandEvent
& event
)
988 switch (event
.GetInt())
991 m_mapMode
= wxMM_TEXT
;
993 case MapMode_Lometric
:
994 m_mapMode
= wxMM_LOMETRIC
;
997 m_mapMode
= wxMM_TWIPS
;
1000 m_mapMode
= wxMM_POINTS
;
1002 case MapMode_Metric
:
1003 m_mapMode
= wxMM_METRIC
;
1006 case LogicalOrigin_MoveDown
:
1007 m_yLogicalOrigin
+= 10;
1009 case LogicalOrigin_MoveUp
:
1010 m_yLogicalOrigin
-= 10;
1012 case LogicalOrigin_MoveLeft
:
1013 m_xLogicalOrigin
+= 10;
1015 case LogicalOrigin_MoveRight
:
1016 m_xLogicalOrigin
-= 10;
1019 case UserScale_StretchHoriz
:
1020 m_xUserScale
*= 1.10;
1022 case UserScale_ShrinkHoriz
:
1023 m_xUserScale
/= 1.10;
1025 case UserScale_StretchVertic
:
1026 m_yUserScale
*= 1.10;
1028 case UserScale_ShrinkVertic
:
1029 m_yUserScale
/= 1.10;
1031 case UserScale_Restore
:
1036 case AxisMirror_Vertic
:
1037 m_yAxisReversed
= !m_yAxisReversed
;
1039 case AxisMirror_Horiz
:
1040 m_xAxisReversed
= !m_xAxisReversed
;
1043 case Colour_TextForeground
:
1044 m_colourForeground
= SelectColour();
1046 case Colour_TextBackground
:
1047 m_colourBackground
= SelectColour();
1049 case Colour_Background
:
1051 wxColour col
= SelectColour();
1054 m_backgroundBrush
.SetColour(col
);
1058 case Colour_BackgroundMode
:
1059 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
1068 m_canvas
->Refresh();
1071 void MyFrame::PrepareDC(wxDC
& dc
)
1073 dc
.SetMapMode( m_mapMode
);
1074 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
1075 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
1076 dc
.SetAxisOrientation( !m_xAxisReversed
, m_yAxisReversed
);
1079 wxColour
MyFrame::SelectColour()
1083 wxColourDialog
dialog(this, &data
);
1085 if ( dialog
.ShowModal() == wxID_OK
)
1087 col
= dialog
.GetColourData().GetColour();