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
), 410, 40 );
576 dc
.SetBrush( *wxBLACK_BRUSH
);
577 dc
.SetPen(*wxTRANSPARENT_PEN
);
578 dc
.DrawRectangle( 0, 160, 1000, 300 );
581 wxBitmap
bitmap(20,70);
583 memdc
.SelectObject( bitmap
);
584 memdc
.SetBrush( *wxBLACK_BRUSH
);
585 memdc
.SetPen( *wxWHITE_PEN
);
586 memdc
.DrawRectangle(0,0,20,70);
587 memdc
.DrawLine( 10,0,10,70 );
590 wxPen pen
= *wxRED_PEN
;
593 memdc
.DrawLine( 10, 5,10, 5 );
594 memdc
.DrawLine( 10,10,11,10 );
595 memdc
.DrawLine( 10,15,12,15 );
596 memdc
.DrawLine( 10,20,13,20 );
599 memdc.SetPen(*wxRED_PEN);
600 memdc.DrawLine( 12, 5,12, 5 );
601 memdc.DrawLine( 12,10,13,10 );
602 memdc.DrawLine( 12,15,14,15 );
603 memdc.DrawLine( 12,20,15,20 );
607 memdc
.DrawLine( 10,25,10,25 );
608 memdc
.DrawLine( 10,30, 9,30 );
609 memdc
.DrawLine( 10,35, 8,35 );
610 memdc
.DrawLine( 10,40, 7,40 );
613 dc
.SetPen(*wxWHITE_PEN
);
614 memdc
.SetLogicalFunction( wxINVERT
);
615 memdc
.SetPen( *wxWHITE_PEN
);
616 memdc
.DrawLine( 10,50,10,50 );
617 memdc
.DrawLine( 10,55,11,55 );
618 memdc
.DrawLine( 10,60,12,60 );
619 memdc
.DrawLine( 10,65,13,65 );
621 memdc
.DrawLine( 12,50,12,50 );
622 memdc
.DrawLine( 12,55,13,55 );
623 memdc
.DrawLine( 12,60,14,60 );
624 memdc
.DrawLine( 12,65,15,65 );
626 memdc
.SelectObject( wxNullBitmap
);
627 dc
.DrawBitmap( bitmap
, 10, 170 );
628 wxImage
image( bitmap
);
629 image
.Rescale( 60,210 );
630 bitmap
= image
.ConvertToBitmap();
631 dc
.DrawBitmap( bitmap
, 50, 170 );
633 // test the rectangle outline drawing - there should be one pixel between
634 // the rect and the lines
635 dc
.SetPen(*wxWHITE_PEN
);
636 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
637 dc
.DrawRectangle(150, 170, 49, 29);
638 dc
.DrawRectangle(200, 170, 49, 29);
639 dc
.SetPen(*wxWHITE_PEN
);
640 dc
.DrawLine(250, 210, 250, 170);
641 dc
.DrawLine(260, 200, 150, 200);
643 // test the rectangle filled drawing - there should be one pixel between
644 // the rect and the lines
645 dc
.SetPen(*wxTRANSPARENT_PEN
);
646 dc
.SetBrush( *wxWHITE_BRUSH
);
647 dc
.DrawRectangle(300, 170, 49, 29);
648 dc
.DrawRectangle(350, 170, 49, 29);
649 dc
.SetPen(*wxWHITE_PEN
);
650 dc
.DrawLine(400, 170, 400, 210);
651 dc
.DrawLine(300, 200, 410, 200);
653 // and now for filled rect with outline
654 dc
.SetPen(*wxRED_PEN
);
655 dc
.SetBrush( *wxWHITE_BRUSH
);
656 dc
.DrawRectangle(500, 170, 49, 29);
657 dc
.DrawRectangle(550, 170, 49, 29);
658 dc
.SetPen(*wxWHITE_PEN
);
659 dc
.DrawLine(600, 170, 600, 210);
660 dc
.DrawLine(500, 200, 610, 200);
662 // test the rectangle outline drawing - there should be one pixel between
663 // the rect and the lines
664 dc
.SetPen(*wxWHITE_PEN
);
665 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
666 dc
.DrawRoundedRectangle(150, 270, 49, 29, 6);
667 dc
.DrawRoundedRectangle(200, 270, 49, 29, 6);
668 dc
.SetPen(*wxWHITE_PEN
);
669 dc
.DrawLine(250, 270, 250, 310);
670 dc
.DrawLine(150, 300, 260, 300);
672 // test the rectangle filled drawing - there should be one pixel between
673 // the rect and the lines
674 dc
.SetPen(*wxTRANSPARENT_PEN
);
675 dc
.SetBrush( *wxWHITE_BRUSH
);
676 dc
.DrawRoundedRectangle(300, 270, 49, 29, 6);
677 dc
.DrawRoundedRectangle(350, 270, 49, 29, 6);
678 dc
.SetPen(*wxWHITE_PEN
);
679 dc
.DrawLine(400, 270, 400, 310);
680 dc
.DrawLine(300, 300, 410, 300);
684 void MyCanvas::DrawText(wxDC
& dc
)
686 // set underlined font for testing
687 dc
.SetFont( wxFont(12, wxMODERN
, wxNORMAL
, wxNORMAL
, TRUE
) );
688 dc
.DrawText( "This is text", 110, 10 );
689 dc
.DrawRotatedText( "That is text", 20, 10, -45 );
691 dc
.SetFont( *wxNORMAL_FONT
);
694 dc
. SetBackgroundMode(wxTRANSPARENT
);
696 for ( int n
= -180; n
< 180; n
+= 30 )
698 text
.Printf(" %d rotated text", n
);
699 dc
.DrawRotatedText(text
, 400, 400, n
);
702 dc
.SetFont( wxFont( 18, wxSWISS
, wxNORMAL
, wxNORMAL
) );
704 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
709 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
710 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
711 dc
.DrawText( text
, 110, 80 );
713 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
714 dc
.DrawText( text
, 110, 120 );
716 dc
.DrawRectangle( 100, 40, 4, height
);
723 } rasterOperations
[] =
726 { "wxAND_INVERT", wxAND_INVERT
},
727 { "wxAND_REVERSE", wxAND_REVERSE
},
728 { "wxCLEAR", wxCLEAR
},
729 { "wxCOPY", wxCOPY
},
730 { "wxEQUIV", wxEQUIV
},
731 { "wxINVERT", wxINVERT
},
732 { "wxNAND", wxNAND
},
733 { "wxNO_OP", wxNO_OP
},
735 { "wxOR_INVERT", wxOR_INVERT
},
736 { "wxOR_REVERSE", wxOR_REVERSE
},
738 { "wxSRC_INVERT", wxSRC_INVERT
},
742 void MyCanvas::DrawImages(wxDC
& dc
)
744 dc
.DrawText("original image", 0, 0);
745 dc
.DrawBitmap(gs_bmpNoMask
, 0, 20, 0);
746 dc
.DrawText("with colour mask", 0, 100);
747 dc
.DrawBitmap(gs_bmpWithColMask
, 0, 120, TRUE
);
748 dc
.DrawText("the mask image", 0, 200);
749 dc
.DrawBitmap(gs_bmpMask
, 0, 220, 0);
750 dc
.DrawText("masked image", 0, 300);
751 dc
.DrawBitmap(gs_bmpWithMask
, 0, 320, TRUE
);
753 int cx
= gs_bmpWithColMask
.GetWidth(),
754 cy
= gs_bmpWithColMask
.GetHeight();
757 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
759 wxCoord x
= 120 + 150*(n%4
),
762 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
763 memDC
.SelectObject(gs_bmpWithColMask
);
764 dc
.Blit(x
, y
, cx
, cy
, &memDC
, 0, 0, rasterOperations
[n
].rop
, TRUE
);
768 void MyCanvas::DrawWithLogicalOps(wxDC
& dc
)
770 static const wxCoord w
= 60;
771 static const wxCoord h
= 60;
773 // reuse the text colour here
774 dc
.SetPen(wxPen(m_owner
->m_colourForeground
, 1, wxSOLID
));
776 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
778 wxCoord x
= 20 + 150*(n%4
),
781 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
782 dc
.SetLogicalFunction(rasterOperations
[n
].rop
);
783 //dc.DrawRectangle(x, y, w, h);
784 dc
.DrawLine(x
, y
, x
+ w
, y
+ h
);
785 dc
.DrawLine(x
+ w
, y
, x
, y
+ h
);
789 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
793 m_owner
->PrepareDC(dc
);
795 dc
.SetBackgroundMode( m_owner
->m_backgroundMode
);
796 if ( m_owner
->m_backgroundBrush
.Ok() )
797 dc
.SetBackground( m_owner
->m_backgroundBrush
);
798 if ( m_owner
->m_colourForeground
.Ok() )
799 dc
.SetTextForeground( m_owner
->m_colourForeground
);
800 if ( m_owner
->m_colourBackground
.Ok() )
801 dc
.SetTextBackground( m_owner
->m_colourBackground
);
814 DrawTestLines( 0, 100, 0, dc
);
815 DrawTestLines( 0, 300, 1, dc
);
816 DrawTestLines( 0, 500, 2, dc
);
817 DrawTestLines( 0, 700, 6, dc
);
821 DrawTestPoly( 0, 100, dc
, 0 );
822 DrawTestPoly( 33, 500, dc
, 1 );
823 DrawTestPoly( 43, 1000, dc
, 2 );
831 DrawWithLogicalOps(dc
);
836 void MyCanvas::OnMouseMove(wxMouseEvent
&event
)
840 m_owner
->PrepareDC(dc
);
842 wxPoint pos
= event
.GetPosition();
843 long x
= dc
.DeviceToLogicalX( pos
.x
);
844 long y
= dc
.DeviceToLogicalY( pos
.y
);
846 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
847 m_owner
->SetStatusText( str
);
850 // ----------------------------------------------------------------------------
852 // ----------------------------------------------------------------------------
854 // the event tables connect the wxWindows events with the functions (event
855 // handlers) which process them. It can be also done at run-time, but for the
856 // simple menu events like this the static method is much simpler.
857 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
858 EVT_MENU (File_Quit
, MyFrame::OnQuit
)
859 EVT_MENU (File_About
, MyFrame::OnAbout
)
861 EVT_MENU_RANGE(MenuShow_First
, MenuShow_Last
, MyFrame::OnShow
)
863 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
867 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
868 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
870 // set the frame icon
871 SetIcon(wxICON(mondrian
));
873 wxMenu
*menuFile
= new wxMenu
;
874 menuFile
->Append(File_ShowDefault
, "&Default screen\tF1");
875 menuFile
->Append(File_ShowText
, "&Text screen\tF2");
876 menuFile
->Append(File_ShowLines
, "&Lines screen\tF3");
877 menuFile
->Append(File_ShowPolygons
, "&Polygons screen\tF4");
878 menuFile
->Append(File_ShowMask
, "wx&Mask screen\tF5");
879 menuFile
->Append(File_ShowOps
, "&ROP screen\tF6");
880 menuFile
->AppendSeparator();
881 menuFile
->Append(File_About
, "&About...\tCtrl-A", "Show about dialog");
882 menuFile
->AppendSeparator();
883 menuFile
->Append(File_Quit
, "E&xit\tAlt-X", "Quit this program");
885 wxMenu
*menuMapMode
= new wxMenu
;
886 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
887 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
888 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
889 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
890 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
892 wxMenu
*menuUserScale
= new wxMenu
;
893 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
894 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
895 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
896 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
897 menuUserScale
->AppendSeparator();
898 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
900 wxMenu
*menuAxis
= new wxMenu
;
901 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-M", "", TRUE
);
902 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-N", "", TRUE
);
904 wxMenu
*menuLogical
= new wxMenu
;
905 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
906 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
907 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
908 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
910 wxMenu
*menuColour
= new wxMenu
;
911 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
912 menuColour
->Append( Colour_TextBackground
, "Text background..." );
913 menuColour
->Append( Colour_Background
, "Background colour..." );
914 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
916 // now append the freshly created menu to the menu bar...
917 wxMenuBar
*menuBar
= new wxMenuBar
;
918 menuBar
->Append(menuFile
, "&File");
919 menuBar
->Append(menuMapMode
, "&MapMode");
920 menuBar
->Append(menuUserScale
, "&UserScale");
921 menuBar
->Append(menuAxis
, "&Axis");
922 menuBar
->Append(menuLogical
, "&LogicalOrigin");
923 menuBar
->Append(menuColour
, "&Colours");
925 // ... and attach this menu bar to the frame
928 // create a status bar just for fun (by default with 1 pane only)
930 SetStatusText("Welcome to wxWindows!");
932 m_mapMode
= wxMM_TEXT
;
935 m_xLogicalOrigin
= 0;
936 m_yLogicalOrigin
= 0;
938 m_yAxisReversed
= FALSE
;
939 m_backgroundMode
= wxSOLID
;
940 m_colourForeground
= *wxRED
;
941 m_colourBackground
= *wxBLUE
;
943 m_canvas
= new MyCanvas( this );
944 m_canvas
->SetScrollbars( 10, 10, 100, 240 );
949 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
951 // TRUE is to force the frame to close
955 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
958 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
959 _T("Copyright (c) Robert Roebling 1999")
962 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
965 void MyFrame::OnShow(wxCommandEvent
& event
)
967 m_canvas
->Show((ScreenToShow
)(event
.GetInt() - MenuShow_First
));
970 void MyFrame::OnOption(wxCommandEvent
& event
)
972 switch (event
.GetInt())
975 m_mapMode
= wxMM_TEXT
;
977 case MapMode_Lometric
:
978 m_mapMode
= wxMM_LOMETRIC
;
981 m_mapMode
= wxMM_TWIPS
;
984 m_mapMode
= wxMM_POINTS
;
987 m_mapMode
= wxMM_METRIC
;
990 case LogicalOrigin_MoveDown
:
991 m_yLogicalOrigin
+= 10;
993 case LogicalOrigin_MoveUp
:
994 m_yLogicalOrigin
-= 10;
996 case LogicalOrigin_MoveLeft
:
997 m_xLogicalOrigin
+= 10;
999 case LogicalOrigin_MoveRight
:
1000 m_xLogicalOrigin
-= 10;
1003 case UserScale_StretchHoriz
:
1004 m_xUserScale
*= 1.10;
1006 case UserScale_ShrinkHoriz
:
1007 m_xUserScale
/= 1.10;
1009 case UserScale_StretchVertic
:
1010 m_yUserScale
*= 1.10;
1012 case UserScale_ShrinkVertic
:
1013 m_yUserScale
/= 1.10;
1015 case UserScale_Restore
:
1020 case AxisMirror_Vertic
:
1021 m_yAxisReversed
= !m_yAxisReversed
;
1023 case AxisMirror_Horiz
:
1024 m_xAxisReversed
= !m_xAxisReversed
;
1027 case Colour_TextForeground
:
1028 m_colourForeground
= SelectColour();
1030 case Colour_TextBackground
:
1031 m_colourBackground
= SelectColour();
1033 case Colour_Background
:
1035 wxColour col
= SelectColour();
1038 m_backgroundBrush
.SetColour(col
);
1042 case Colour_BackgroundMode
:
1043 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
1052 m_canvas
->Refresh();
1055 void MyFrame::PrepareDC(wxDC
& dc
)
1057 dc
.SetMapMode( m_mapMode
);
1058 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
1059 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
1060 dc
.SetAxisOrientation( !m_xAxisReversed
, m_yAxisReversed
);
1063 wxColour
MyFrame::SelectColour()
1067 wxColourDialog
dialog(this, &data
);
1069 if ( dialog
.ShowModal() == wxID_OK
)
1071 col
= dialog
.GetColourData().GetColour();