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)
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 static wxBitmap gs_bmpNoMask
,
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // Define a new application type, each program should derive a class from wxApp
81 class MyApp
: public wxApp
84 // override base class virtuals
85 // ----------------------------
87 // this one is called on application startup and is a good place for the app
88 // initialization (doing it here and not in the ctor allows to have an error
89 // return: if OnInit() returns false, the application terminates)
90 virtual bool OnInit();
98 // Define a new frame type: this is going to be our main frame
99 class MyFrame
: public wxFrame
103 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
105 // event handlers (these functions should _not_ be virtual)
106 void OnQuit(wxCommandEvent
& event
);
107 void OnAbout(wxCommandEvent
& event
);
108 void OnShow(wxCommandEvent
&event
);
109 void OnOption(wxCommandEvent
&event
);
111 wxColour
SelectColour();
112 void PrepareDC(wxDC
& dc
);
114 int m_backgroundMode
;
118 int m_xLogicalOrigin
;
119 int m_yLogicalOrigin
;
120 bool m_xAxisReversed
,
122 wxColour m_colourForeground
, // these are _text_ colours
124 wxBrush m_backgroundBrush
;
128 // any class wishing to process wxWindows events must use this macro
129 DECLARE_EVENT_TABLE()
132 // define a scrollable canvas for drawing onto
133 class MyCanvas
: public wxScrolledWindow
136 MyCanvas( MyFrame
*parent
);
138 void OnPaint(wxPaintEvent
&event
);
139 void OnMouseMove(wxMouseEvent
&event
);
141 void Show(ScreenToShow show
) { m_show
= show
; Refresh(); }
144 void DrawTestPoly( int x
, int y
, wxDC
&dc
,int transparent
);
145 void DrawTestLines( int x
, int y
, int width
, wxDC
&dc
);
146 void DrawText(wxDC
& dc
);
147 void DrawImages(wxDC
& dc
);
148 void DrawDefault(wxDC
& dc
);
155 DECLARE_EVENT_TABLE()
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // IDs for the controls and the menu commands
170 File_ShowDefault
= MenuShow_First
,
175 MenuShow_Last
= File_ShowMask
,
179 MapMode_Text
= MenuOption_First
,
185 UserScale_StretchHoriz
,
186 UserScale_ShrinkHoriz
,
187 UserScale_StretchVertic
,
188 UserScale_ShrinkVertic
,
194 LogicalOrigin_MoveDown
,
195 LogicalOrigin_MoveUp
,
196 LogicalOrigin_MoveLeft
,
197 LogicalOrigin_MoveRight
,
199 Colour_TextForeground
,
200 Colour_TextBackground
,
202 Colour_BackgroundMode
,
204 MenuOption_Last
= Colour_BackgroundMode
207 // ----------------------------------------------------------------------------
208 // event tables and other macros for wxWindows
209 // ----------------------------------------------------------------------------
212 // Create a new application object: this macro will allow wxWindows to create
213 // the application object during program execution (it's better than using a
214 // static object for many reasons) and also declares the accessor function
215 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
219 // ============================================================================
221 // ============================================================================
223 // ----------------------------------------------------------------------------
224 // the application class
225 // ----------------------------------------------------------------------------
227 bool MyApp::LoadImages()
233 wxString path
= pathList
.FindValidPath("pat4.bmp");
236 gs_bmp4
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
237 wxMask
* mask4
= new wxMask(gs_bmp4
, *wxBLACK
);
238 gs_bmp4
.SetMask(mask4
);
240 path
= pathList
.FindValidPath("pat36.bmp");
243 gs_bmp36
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
244 wxMask
* mask36
= new wxMask(gs_bmp36
, *wxBLACK
);
245 gs_bmp36
.SetMask(mask36
);
247 path
= pathList
.FindValidPath("image.bmp");
250 gs_bmpNoMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
251 gs_bmpWithMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
252 gs_bmpWithColMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
254 path
= pathList
.FindValidPath("mask.bmp");
257 gs_bmpMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
259 // This is so wrong, it hurts.
260 // gs_bmpMask.SetDepth(1);
261 // wxMask *mask = new wxMask(gs_bmpMask);
263 wxMask
*mask
= new wxMask(gs_bmpMask
, *wxBLACK
);
264 gs_bmpWithMask
.SetMask(mask
);
266 mask
= new wxMask(gs_bmpWithColMask
, *wxWHITE
);
267 gs_bmpWithColMask
.SetMask(mask
);
272 // `Main program' equivalent: the program execution "starts" here
275 // Create the main application window
276 MyFrame
*frame
= new MyFrame("Drawing sample",
277 wxPoint(50, 50), wxSize(550, 340));
279 // Show it and tell the application that it's our main window
285 wxLogError("Can't load one of the bitmap files needed for this sample "
286 "from the current or parent directory, please copy them "
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
301 // the event tables connect the wxWindows events with the functions (event
302 // handlers) which process them.
303 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
304 EVT_PAINT (MyCanvas::OnPaint
)
305 EVT_MOTION (MyCanvas::OnMouseMove
)
308 MyCanvas::MyCanvas( MyFrame
*parent
) : wxScrolledWindow( parent
)
311 m_show
= Show_Default
;
314 //draw a polygon and an overlapping rectangle
315 //is transparent is 1, the fill pattern are made transparent
316 //is transparent is 2, the fill pattern are made transparent but inversed
317 //is transparent is 0 the text for and background color will be used to represent/map
318 //the colors of the monochrome bitmap pixels to the fillpattern
320 //i miss_used the the menu items for setting so called back and fore ground color
321 //just to show how the those colors do influence the fillpatterns
322 //just play with those,
324 //variations are endless using other logical functions
325 void MyCanvas::DrawTestPoly( int x
, int y
,wxDC
&dc
,int transparent
)
327 wxBrush
* brush4
= new wxBrush(gs_bmp4
);
328 wxBrush
* brush36
= new wxBrush(gs_bmp36
);
331 todraw
[0].x
=(long)x
+100;
332 todraw
[0].y
=(long)y
+100;
333 todraw
[1].x
=(long)x
+300;
334 todraw
[1].y
=(long)y
+100;
335 todraw
[2].x
=(long)x
+300;
336 todraw
[2].y
=(long)y
+300;
337 todraw
[3].x
=(long)x
+150;
338 todraw
[3].y
=(long)y
+350;
339 todraw
[4].x
=(long)x
+100;
340 todraw
[4].y
=(long)y
+300;
358 dc
.SetPen( wxPen( "black", 4, wxSOLID
) );
359 dc
.SetBrush( *brush4
);
360 dc
.SetTextForeground(*wxGREEN
);
361 dc
.SetTextBackground(m_owner
->m_colourForeground
);
362 dc
.SetLogicalFunction(wxCOPY
);
363 dc
.DrawPolygon(5,todraw
,0,0,wxWINDING_RULE
);
365 //don't understand hwo but the outline is also depending on logicalfunction
366 dc
.SetPen( wxPen( "red", 4, wxSOLID
) );
367 dc
.SetBrush( *brush36
);
368 dc
.SetTextForeground(*wxCYAN
);
369 dc
.SetTextBackground(m_owner
->m_colourBackground
);
370 dc
.SetLogicalFunction(wxCOPY
);
371 dc
.DrawRectangle( x
+10, y
+10, 200, 200 );
372 dc
.SetBrush(wxNullBrush
);
373 dc
.SetPen(wxNullPen
);
376 case 1: //now with transparent fillpatterns
379 wxBitmap
* bmpBlit
= new wxBitmap(600,400);
380 wxMemoryDC
* memDC
= new wxMemoryDC();
381 // wxBrush _clearbrush(*wxGREEN,wxSOLID);
382 wxBrush
_clearbrush(*wxBLACK
,wxSOLID
);
383 memDC
->SelectObject(*bmpBlit
);
384 memDC
->BeginDrawing();
385 memDC
->SetBackground(_clearbrush
);
387 memDC
->SetBackground(wxNullBrush
);
389 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
390 memDC
->SetBrush( wxNullBrush
);
391 memDC
->SetBrush( *brush4
);
392 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
393 memDC
->SetTextBackground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
394 memDC
->SetLogicalFunction(wxAND_INVERT
);
396 // BLACK OUT the opaque pixels and leave the rest as is
397 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
399 // Set background and foreground colors for fill pattern
400 //the previous blacked out pixels are now merged with the layer color
401 //while the non blacked out pixels stay as they are.
402 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
404 //now define what will be the color of the fillpattern parts that are not transparent
405 // memDC->SetTextBackground(*wxBLUE);
406 memDC
->SetTextBackground(m_owner
->m_colourForeground
);
407 memDC
->SetLogicalFunction(wxOR
);
410 //don't understand how but the outline is also depending on logicalfunction
411 memDC
->SetPen( wxPen( "red", 4, wxSOLID
) );
412 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
414 memDC
->SetLogicalFunction(wxCOPY
);
416 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
417 memDC
->SetBrush( wxNullBrush
);
418 memDC
->SetBrush( *brush36
);
419 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
420 memDC
->SetTextBackground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
421 memDC
->SetLogicalFunction(wxAND_INVERT
);
423 memDC
->DrawRectangle( 10, 10, 200, 200 );
425 // Set background and foreground colors for fill pattern
426 //the previous blacked out pixels are now merged with the layer color
427 //while the non blacked out pixels stay as they are.
428 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
429 //now define what will be the color of the fillpattern parts that are not transparent
430 // memDC->SetTextBackground(*wxRED);
431 memDC
->SetTextBackground(m_owner
->m_colourBackground
);
432 memDC
->SetLogicalFunction(wxOR
);
434 //don't understand how but the outline is also depending on logicalfunction
435 memDC
->SetPen( wxPen( "yellow", 4, wxSOLID
) );
436 memDC
->DrawRectangle( 10, 10, 200, 200 );
438 memDC
->SetBrush(wxNullBrush
);
439 memDC
->SetPen(wxNullPen
);
442 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
447 case 2: //now with transparent inversed fillpatterns
449 wxBitmap
* bmpBlit
= new wxBitmap(600,400);
450 wxMemoryDC
* memDC
= new wxMemoryDC();
451 wxBrush
_clearbrush(*wxWHITE
,wxSOLID
);
452 memDC
->SelectObject(*bmpBlit
);
453 memDC
->BeginDrawing();
454 memDC
->SetBackground(_clearbrush
);
456 memDC
->SetBackground(wxNullBrush
);
458 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
459 memDC
->SetBrush( *brush4
);
460 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
461 memDC
->SetTextForeground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
462 memDC
->SetLogicalFunction(wxAND_INVERT
);
464 // BLACK OUT the opaque pixels and leave the rest as is
465 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
467 // Set background and foreground colors for fill pattern
468 //the previous blacked out pixels are now merged with the layer color
469 //while the non blacked out pixels stay as they are.
470 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
472 //now define what will be the color of the fillpattern parts that are not transparent
473 memDC
->SetTextForeground(m_owner
->m_colourForeground
);
474 memDC
->SetLogicalFunction(wxOR
);
477 //don't understand how but the outline is also depending on logicalfunction
478 memDC
->SetPen( wxPen( "red", 4, wxSOLID
) );
479 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
481 memDC
->SetLogicalFunction(wxCOPY
);
483 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
484 memDC
->SetBrush( *brush36
);
485 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
486 memDC
->SetTextForeground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
487 memDC
->SetLogicalFunction(wxAND_INVERT
);
489 memDC
->DrawRectangle( 10,10, 200, 200 );
491 // Set background and foreground colors for fill pattern
492 //the previous blacked out pixels are now merged with the layer color
493 //while the non blacked out pixels stay as they are.
494 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
495 //now define what will be the color of the fillpattern parts that are not transparent
496 memDC
->SetTextForeground(m_owner
->m_colourBackground
);
497 memDC
->SetLogicalFunction(wxOR
);
499 //don't understand how but the outline is also depending on logicalfunction
500 memDC
->SetPen( wxPen( "yellow", 4, wxSOLID
) );
501 memDC
->DrawRectangle( 10, 10, 200, 200 );
503 memDC
->SetBrush(wxNullBrush
);
504 memDC
->SetPen(wxNullPen
);
505 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
515 void MyCanvas::DrawTestLines( int x
, int y
, int width
, wxDC
&dc
)
517 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
518 dc
.SetBrush( *wxRED_BRUSH
);
519 dc
.DrawRectangle( x
+10, y
+10, 100, 190 );
521 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
522 dc
.DrawLine( x
+20, y
+20, 100, y
+20 );
523 dc
.SetPen( wxPen( "black", width
, wxDOT
) );
524 dc
.DrawLine( x
+20, y
+30, 100, y
+30 );
525 dc
.SetPen( wxPen( "black", width
, wxSHORT_DASH
) );
526 dc
.DrawLine( x
+20, y
+40, 100, y
+40 );
527 dc
.SetPen( wxPen( "black", width
, wxLONG_DASH
) );
528 dc
.DrawLine( x
+20, y
+50, 100, y
+50 );
529 dc
.SetPen( wxPen( "black", width
, wxDOT_DASH
) );
530 dc
.DrawLine( x
+20, y
+60, 100, y
+60 );
532 dc
.SetPen( wxPen( "black", width
, wxBDIAGONAL_HATCH
) );
533 dc
.DrawLine( x
+20, y
+70, 100, y
+70 );
534 dc
.SetPen( wxPen( "black", width
, wxCROSSDIAG_HATCH
) );
535 dc
.DrawLine( x
+20, y
+80, 100, y
+80 );
536 dc
.SetPen( wxPen( "black", width
, wxFDIAGONAL_HATCH
) );
537 dc
.DrawLine( x
+20, y
+90, 100, y
+90 );
538 dc
.SetPen( wxPen( "black", width
, wxCROSS_HATCH
) );
539 dc
.DrawLine( x
+20, y
+100, 100, y
+100 );
540 dc
.SetPen( wxPen( "black", width
, wxHORIZONTAL_HATCH
) );
541 dc
.DrawLine( x
+20, y
+110, 100, y
+110 );
542 dc
.SetPen( wxPen( "black", width
, wxVERTICAL_HATCH
) );
543 dc
.DrawLine( x
+20, y
+120, 100, y
+120 );
545 wxPen
ud( "black", width
, wxUSER_DASH
);
548 ud
.SetDashes( 1, dash1
);
549 dc
.DrawLine( x
+20, y
+140, 100, y
+140 );
551 ud
.SetDashes( 1, dash1
);
552 dc
.DrawLine( x
+20, y
+150, 100, y
+150 );
554 ud
.SetDashes( 1, dash1
);
555 dc
.DrawLine( x
+20, y
+160, 100, y
+160 );
557 ud
.SetDashes( 1, dash1
);
558 dc
.DrawLine( x
+20, y
+170, 100, y
+170 );
562 void MyCanvas::DrawDefault(wxDC
& dc
)
565 dc
.DrawCircle(0, 0, 10);
566 #if !(defined __WXGTK__) && !(defined __WXMOTIF__)
567 // not implemented in wxGTK or wxMOTIF :-(
568 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
571 dc
.DrawIcon( wxICON(mondrian
), 410, 40 );
574 dc
.SetBrush( *wxBLACK_BRUSH
);
575 dc
.SetPen(*wxTRANSPARENT_PEN
);
576 dc
.DrawRectangle( 0, 160, 1000, 300 );
579 wxBitmap
bitmap(20,70);
581 memdc
.SelectObject( bitmap
);
582 memdc
.SetBrush( *wxBLACK_BRUSH
);
583 memdc
.SetPen( *wxWHITE_PEN
);
584 memdc
.DrawRectangle(0,0,20,70);
585 memdc
.DrawLine( 10,0,10,70 );
587 memdc
.DrawLine( 10, 5,10, 5 );
588 memdc
.DrawLine( 10,10,11,10 );
589 memdc
.DrawLine( 10,15,12,15 );
590 memdc
.DrawLine( 10,20,13,20 );
592 memdc
.DrawLine( 12, 5,12, 5 );
593 memdc
.DrawLine( 12,10,13,10 );
594 memdc
.DrawLine( 12,15,14,15 );
595 memdc
.DrawLine( 12,20,15,20 );
598 memdc
.DrawLine( 10,25,10,25 );
599 memdc
.DrawLine( 10,30, 9,30 );
600 memdc
.DrawLine( 10,35, 8,35 );
601 memdc
.DrawLine( 10,40, 7,40 );
604 memdc
.SetLogicalFunction( wxINVERT
);
605 memdc
.SetPen( *wxWHITE_PEN
);
606 memdc
.DrawLine( 10,50,10,50 );
607 memdc
.DrawLine( 10,55,11,55 );
608 memdc
.DrawLine( 10,60,12,60 );
609 memdc
.DrawLine( 10,65,13,65 );
611 memdc
.DrawLine( 12,50,12,50 );
612 memdc
.DrawLine( 12,55,13,55 );
613 memdc
.DrawLine( 12,60,14,60 );
614 memdc
.DrawLine( 12,65,15,65 );
616 memdc
.SelectObject( wxNullBitmap
);
617 dc
.DrawBitmap( bitmap
, 10, 170 );
618 wxImage
image( bitmap
);
619 image
.Rescale( 60,210 );
620 bitmap
= image
.ConvertToBitmap();
621 dc
.DrawBitmap( bitmap
, 50, 170 );
624 // test the rectangle outline drawing - there should be one pixel between
625 // the rect and the lines
626 dc
.SetPen(*wxWHITE_PEN
);
627 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
628 dc
.DrawRectangle(150, 170, 49, 29);
629 dc
.DrawRectangle(200, 170, 49, 29);
630 dc
.SetPen(*wxWHITE_PEN
);
631 dc
.DrawLine(250, 210, 250, 170);
632 dc
.DrawLine(260, 200, 150, 200);
634 // test the rectangle filled drawing - there should be one pixel between
635 // the rect and the lines
636 dc
.SetPen(*wxTRANSPARENT_PEN
);
637 dc
.SetBrush( *wxWHITE_BRUSH
);
638 dc
.DrawRectangle(300, 170, 49, 29);
639 dc
.DrawRectangle(350, 170, 49, 29);
640 dc
.SetPen(*wxWHITE_PEN
);
641 dc
.DrawLine(400, 170, 400, 210);
642 dc
.DrawLine(300, 200, 410, 200);
644 // and now for filled rect with outline
645 dc
.SetPen(*wxRED_PEN
);
646 dc
.SetBrush( *wxWHITE_BRUSH
);
647 dc
.DrawRectangle(500, 170, 49, 29);
648 dc
.DrawRectangle(550, 170, 49, 29);
649 dc
.SetPen(*wxWHITE_PEN
);
650 dc
.DrawLine(600, 170, 600, 210);
651 dc
.DrawLine(500, 200, 610, 200);
653 // test the rectangle outline drawing - there should be one pixel between
654 // the rect and the lines
655 dc
.SetPen(*wxWHITE_PEN
);
656 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
657 dc
.DrawRoundedRectangle(150, 270, 49, 29, 6);
658 dc
.DrawRoundedRectangle(200, 270, 49, 29, 6);
659 dc
.SetPen(*wxWHITE_PEN
);
660 dc
.DrawLine(250, 270, 250, 310);
661 dc
.DrawLine(150, 300, 260, 300);
663 // test the rectangle filled drawing - there should be one pixel between
664 // the rect and the lines
665 dc
.SetPen(*wxTRANSPARENT_PEN
);
666 dc
.SetBrush( *wxWHITE_BRUSH
);
667 dc
.DrawRoundedRectangle(300, 270, 49, 29, 6);
668 dc
.DrawRoundedRectangle(350, 270, 49, 29, 6);
669 dc
.SetPen(*wxWHITE_PEN
);
670 dc
.DrawLine(400, 270, 400, 310);
671 dc
.DrawLine(300, 300, 410, 300);
675 void MyCanvas::DrawText(wxDC
& dc
)
677 // set underlined font for testing
678 dc
.SetFont( wxFont(12, wxMODERN
, wxNORMAL
, wxNORMAL
, TRUE
) );
679 dc
.DrawText( "This is text", 110, 10 );
680 dc
.DrawRotatedText( "That is text", 20, 10, -45 );
682 dc
.SetFont( *wxNORMAL_FONT
);
685 dc
. SetBackgroundMode(wxTRANSPARENT
);
687 for ( int n
= -180; n
< 180; n
+= 30 )
689 text
.Printf(" %d rotated text", n
);
690 dc
.DrawRotatedText(text
, 400, 400, n
);
693 dc
.SetFont( wxFont( 18, wxSWISS
, wxNORMAL
, wxNORMAL
) );
695 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
700 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
701 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
702 dc
.DrawText( text
, 110, 80 );
704 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
705 dc
.DrawText( text
, 110, 120 );
707 dc
.DrawRectangle( 100, 40, 4, height
);
710 void MyCanvas::DrawImages(wxDC
& dc
)
716 } rasterOperations
[] =
719 { "wxAND_INVERT", wxAND_INVERT
},
720 { "wxAND_REVERSE", wxAND_REVERSE
},
721 { "wxCLEAR", wxCLEAR
},
722 { "wxCOPY", wxCOPY
},
723 { "wxEQUIV", wxEQUIV
},
724 { "wxINVERT", wxINVERT
},
725 { "wxNAND", wxNAND
},
726 { "wxNO_OP", wxNO_OP
},
728 { "wxOR_INVERT", wxOR_INVERT
},
729 { "wxOR_REVERSE", wxOR_REVERSE
},
731 { "wxSRC_INVERT", wxSRC_INVERT
},
735 dc
.DrawText("original image", 0, 0);
736 dc
.DrawBitmap(gs_bmpNoMask
, 0, 20, 0);
737 dc
.DrawText("with colour mask", 0, 100);
738 dc
.DrawBitmap(gs_bmpWithColMask
, 0, 120, TRUE
);
739 dc
.DrawText("the mask image", 0, 200);
740 dc
.DrawBitmap(gs_bmpMask
, 0, 220, 0);
741 dc
.DrawText("masked image", 0, 300);
742 dc
.DrawBitmap(gs_bmpWithMask
, 0, 320, TRUE
);
744 int cx
= gs_bmpWithColMask
.GetWidth(),
745 cy
= gs_bmpWithColMask
.GetHeight();
748 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
750 wxCoord x
= 120 + 150*(n%4
),
753 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
754 memDC
.SelectObject(gs_bmpWithColMask
);
755 dc
.Blit(x
, y
, cx
, cy
, &memDC
, 0, 0, rasterOperations
[n
].rop
, TRUE
);
759 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
763 m_owner
->PrepareDC(dc
);
765 dc
.SetBackgroundMode( m_owner
->m_backgroundMode
);
766 if ( m_owner
->m_backgroundBrush
.Ok() )
767 dc
.SetBackground( m_owner
->m_backgroundBrush
);
768 if ( m_owner
->m_colourForeground
.Ok() )
769 dc
.SetTextForeground( m_owner
->m_colourForeground
);
770 if ( m_owner
->m_colourBackground
.Ok() )
771 dc
.SetTextBackground( m_owner
->m_colourBackground
);
784 DrawTestLines( 0, 100, 0, dc
);
785 DrawTestLines( 0, 300, 1, dc
);
786 DrawTestLines( 0, 500, 2, dc
);
787 DrawTestLines( 0, 700, 6, dc
);
791 DrawTestPoly( 0, 100, dc
, 0 );
792 DrawTestPoly( 33, 500, dc
, 1 );
793 DrawTestPoly( 43, 1000, dc
, 2 );
802 void MyCanvas::OnMouseMove(wxMouseEvent
&event
)
806 m_owner
->PrepareDC(dc
);
808 wxPoint pos
= event
.GetPosition();
809 long x
= dc
.DeviceToLogicalX( pos
.x
);
810 long y
= dc
.DeviceToLogicalY( pos
.y
);
812 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
813 m_owner
->SetStatusText( str
);
816 // ----------------------------------------------------------------------------
818 // ----------------------------------------------------------------------------
820 // the event tables connect the wxWindows events with the functions (event
821 // handlers) which process them. It can be also done at run-time, but for the
822 // simple menu events like this the static method is much simpler.
823 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
824 EVT_MENU (File_Quit
, MyFrame::OnQuit
)
825 EVT_MENU (File_About
, MyFrame::OnAbout
)
827 EVT_MENU_RANGE(MenuShow_First
, MenuShow_Last
, MyFrame::OnShow
)
829 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
833 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
834 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
836 // set the frame icon
837 SetIcon(wxICON(mondrian
));
839 wxMenu
*menuFile
= new wxMenu
;
840 menuFile
->Append(File_ShowDefault
, "&Default screen\tF1");
841 menuFile
->Append(File_ShowText
, "&Text screen\tF2");
842 menuFile
->Append(File_ShowLines
, "&Lines screen\tF3");
843 menuFile
->Append(File_ShowPolygons
, "&Polygons screen\tF4");
844 menuFile
->Append(File_ShowMask
, "wx&Mask screen\tF5");
845 menuFile
->AppendSeparator();
846 menuFile
->Append(File_About
, "&About...\tCtrl-A", "Show about dialog");
847 menuFile
->AppendSeparator();
848 menuFile
->Append(File_Quit
, "E&xit\tAlt-X", "Quit this program");
850 wxMenu
*menuMapMode
= new wxMenu
;
851 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
852 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
853 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
854 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
855 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
857 wxMenu
*menuUserScale
= new wxMenu
;
858 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
859 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
860 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
861 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
862 menuUserScale
->AppendSeparator();
863 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
865 wxMenu
*menuAxis
= new wxMenu
;
866 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-M", "", TRUE
);
867 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-N", "", TRUE
);
869 wxMenu
*menuLogical
= new wxMenu
;
870 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
871 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
872 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
873 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
875 wxMenu
*menuColour
= new wxMenu
;
876 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
877 menuColour
->Append( Colour_TextBackground
, "Text background..." );
878 menuColour
->Append( Colour_Background
, "Background colour..." );
879 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
881 // now append the freshly created menu to the menu bar...
882 wxMenuBar
*menuBar
= new wxMenuBar
;
883 menuBar
->Append(menuFile
, "&File");
884 menuBar
->Append(menuMapMode
, "&MapMode");
885 menuBar
->Append(menuUserScale
, "&UserScale");
886 menuBar
->Append(menuAxis
, "&Axis");
887 menuBar
->Append(menuLogical
, "&LogicalOrigin");
888 menuBar
->Append(menuColour
, "&Colours");
890 // ... and attach this menu bar to the frame
893 // create a status bar just for fun (by default with 1 pane only)
895 SetStatusText("Welcome to wxWindows!");
897 m_mapMode
= wxMM_TEXT
;
900 m_xLogicalOrigin
= 0;
901 m_yLogicalOrigin
= 0;
903 m_yAxisReversed
= FALSE
;
904 m_backgroundMode
= wxSOLID
;
905 m_colourForeground
= *wxRED
;
906 m_colourBackground
= *wxBLUE
;
908 m_canvas
= new MyCanvas( this );
909 m_canvas
->SetScrollbars( 10, 10, 100, 240 );
914 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
916 // TRUE is to force the frame to close
920 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
923 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
924 _T("Copyright (c) Robert Roebling 1999")
927 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
930 void MyFrame::OnShow(wxCommandEvent
& event
)
932 m_canvas
->Show((ScreenToShow
)(event
.GetInt() - MenuShow_First
));
935 void MyFrame::OnOption(wxCommandEvent
& event
)
937 switch (event
.GetInt())
940 m_mapMode
= wxMM_TEXT
;
942 case MapMode_Lometric
:
943 m_mapMode
= wxMM_LOMETRIC
;
946 m_mapMode
= wxMM_TWIPS
;
949 m_mapMode
= wxMM_POINTS
;
952 m_mapMode
= wxMM_METRIC
;
955 case LogicalOrigin_MoveDown
:
956 m_yLogicalOrigin
+= 10;
958 case LogicalOrigin_MoveUp
:
959 m_yLogicalOrigin
-= 10;
961 case LogicalOrigin_MoveLeft
:
962 m_xLogicalOrigin
+= 10;
964 case LogicalOrigin_MoveRight
:
965 m_xLogicalOrigin
-= 10;
968 case UserScale_StretchHoriz
:
969 m_xUserScale
*= 1.10;
971 case UserScale_ShrinkHoriz
:
972 m_xUserScale
/= 1.10;
974 case UserScale_StretchVertic
:
975 m_yUserScale
*= 1.10;
977 case UserScale_ShrinkVertic
:
978 m_yUserScale
/= 1.10;
980 case UserScale_Restore
:
985 case AxisMirror_Vertic
:
986 m_yAxisReversed
= !m_yAxisReversed
;
988 case AxisMirror_Horiz
:
989 m_xAxisReversed
= !m_xAxisReversed
;
992 case Colour_TextForeground
:
993 m_colourForeground
= SelectColour();
995 case Colour_TextBackground
:
996 m_colourBackground
= SelectColour();
998 case Colour_Background
:
1000 wxColour col
= SelectColour();
1003 m_backgroundBrush
.SetColour(col
);
1007 case Colour_BackgroundMode
:
1008 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
1017 m_canvas
->Refresh();
1020 void MyFrame::PrepareDC(wxDC
& dc
)
1022 dc
.SetMapMode( m_mapMode
);
1023 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
1024 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
1025 dc
.SetAxisOrientation( !m_xAxisReversed
, m_yAxisReversed
);
1028 wxColour
MyFrame::SelectColour()
1032 wxColourDialog
dialog(this, &data
);
1034 if ( dialog
.ShowModal() == wxID_OK
)
1036 col
= dialog
.GetColourData().GetColour();