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"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // the application icon
45 #if defined(__WXGTK__) || defined(__WXMOTIF__)
46 #include "mondrian.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // what do we show on screen (there are too many shapes to put them all on
54 // screen simultaneously)
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 static wxBitmap gs_bmpNoMask
,
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // Define a new application type, each program should derive a class from wxApp
80 class MyApp
: public wxApp
83 // override base class virtuals
84 // ----------------------------
86 // this one is called on application startup and is a good place for the app
87 // initialization (doing it here and not in the ctor allows to have an error
88 // return: if OnInit() returns false, the application terminates)
89 virtual bool OnInit();
97 // Define a new frame type: this is going to be our main frame
98 class MyFrame
: public wxFrame
102 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
104 // event handlers (these functions should _not_ be virtual)
105 void OnQuit(wxCommandEvent
& event
);
106 void OnAbout(wxCommandEvent
& event
);
107 void OnShow(wxCommandEvent
&event
);
108 void OnOption(wxCommandEvent
&event
);
110 wxColour
SelectColour();
111 void PrepareDC(wxDC
& dc
);
113 int m_backgroundMode
;
117 int m_xLogicalOrigin
;
118 int m_yLogicalOrigin
;
119 bool m_xAxisReversed
,
121 wxColour m_colourForeground
, // these are _text_ colours
123 wxBrush m_backgroundBrush
;
127 // any class wishing to process wxWindows events must use this macro
128 DECLARE_EVENT_TABLE()
131 // define a scrollable canvas for drawing onto
132 class MyCanvas
: public wxScrolledWindow
135 MyCanvas( MyFrame
*parent
);
137 void OnPaint(wxPaintEvent
&event
);
138 void OnMouseMove(wxMouseEvent
&event
);
140 void Show(ScreenToShow show
) { m_show
= show
; Refresh(); }
143 void DrawTestPoly( int x
, int y
, wxDC
&dc
,int transparent
);
144 void DrawTestLines( int x
, int y
, int width
, wxDC
&dc
);
145 void DrawText(wxDC
& dc
);
146 void DrawImages(wxDC
& dc
);
147 void DrawDefault(wxDC
& dc
);
154 DECLARE_EVENT_TABLE()
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // IDs for the controls and the menu commands
169 File_ShowDefault
= MenuShow_First
,
174 MenuShow_Last
= File_ShowMask
,
178 MapMode_Text
= MenuOption_First
,
184 UserScale_StretchHoriz
,
185 UserScale_ShrinkHoriz
,
186 UserScale_StretchVertic
,
187 UserScale_ShrinkVertic
,
193 LogicalOrigin_MoveDown
,
194 LogicalOrigin_MoveUp
,
195 LogicalOrigin_MoveLeft
,
196 LogicalOrigin_MoveRight
,
198 Colour_TextForeground
,
199 Colour_TextBackground
,
201 Colour_BackgroundMode
,
203 MenuOption_Last
= Colour_BackgroundMode
206 // ----------------------------------------------------------------------------
207 // event tables and other macros for wxWindows
208 // ----------------------------------------------------------------------------
211 // Create a new application object: this macro will allow wxWindows to create
212 // the application object during program execution (it's better than using a
213 // static object for many reasons) and also declares the accessor function
214 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
218 // ============================================================================
220 // ============================================================================
222 // ----------------------------------------------------------------------------
223 // the application class
224 // ----------------------------------------------------------------------------
226 bool MyApp::LoadImages()
232 wxString path
= pathList
.FindValidPath("pat4.bmp");
235 gs_bmp4
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
236 wxMask
* mask4
= new wxMask(gs_bmp4
, *wxBLACK
);
237 gs_bmp4
.SetMask(mask4
);
239 path
= pathList
.FindValidPath("pat36.bmp");
242 gs_bmp36
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
243 wxMask
* mask36
= new wxMask(gs_bmp36
, *wxBLACK
);
244 gs_bmp36
.SetMask(mask36
);
246 path
= pathList
.FindValidPath("image.bmp");
249 gs_bmpNoMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
250 gs_bmpWithMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
251 gs_bmpWithColMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
253 path
= pathList
.FindValidPath("mask.bmp");
256 gs_bmpMask
.LoadFile(path
, wxBITMAP_TYPE_BMP
);
258 // This is so wrong, it hurts.
259 // gs_bmpMask.SetDepth(1);
260 // wxMask *mask = new wxMask(gs_bmpMask);
262 wxMask
*mask
= new wxMask(gs_bmpMask
, *wxBLACK
);
263 gs_bmpWithMask
.SetMask(mask
);
265 mask
= new wxMask(gs_bmpWithColMask
, *wxWHITE
);
266 gs_bmpWithColMask
.SetMask(mask
);
271 // `Main program' equivalent: the program execution "starts" here
274 // Create the main application window
275 MyFrame
*frame
= new MyFrame("Drawing sample",
276 wxPoint(50, 50), wxSize(550, 340));
278 // Show it and tell the application that it's our main window
284 wxLogError("Can't load one of the bitmap files needed for this sample "
285 "from the current or parent directory, please copy them "
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 // the event tables connect the wxWindows events with the functions (event
301 // handlers) which process them.
302 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
303 EVT_PAINT (MyCanvas::OnPaint
)
304 EVT_MOTION (MyCanvas::OnMouseMove
)
307 MyCanvas::MyCanvas( MyFrame
*parent
) : wxScrolledWindow( parent
)
310 m_show
= Show_Default
;
313 //draw a polygon and an overlapping rectangle
314 //is transparent is 1, the fill pattern are made transparent
315 //is transparent is 2, the fill pattern are made transparent but inversed
316 //is transparent is 0 the text for and background color will be used to represent/map
317 //the colors of the monochrome bitmap pixels to the fillpattern
319 //i miss_used the the menu items for setting so called back and fore ground color
320 //just to show how the those colors do influence the fillpatterns
321 //just play with those,
323 //variations are endless using other logical functions
324 void MyCanvas::DrawTestPoly( int x
, int y
,wxDC
&dc
,int transparent
)
326 wxBrush
* brush4
= new wxBrush(gs_bmp4
);
327 wxBrush
* brush36
= new wxBrush(gs_bmp36
);
330 todraw
[0].x
=(long)x
+100;
331 todraw
[0].y
=(long)y
+100;
332 todraw
[1].x
=(long)x
+300;
333 todraw
[1].y
=(long)y
+100;
334 todraw
[2].x
=(long)x
+300;
335 todraw
[2].y
=(long)y
+300;
336 todraw
[3].x
=(long)x
+150;
337 todraw
[3].y
=(long)y
+350;
338 todraw
[4].x
=(long)x
+100;
339 todraw
[4].y
=(long)y
+300;
357 dc
.SetPen( wxPen( "black", 4, wxSOLID
) );
358 dc
.SetBrush( *brush4
);
359 dc
.SetTextForeground(*wxGREEN
);
360 dc
.SetTextBackground(m_owner
->m_colourForeground
);
361 dc
.SetLogicalFunction(wxCOPY
);
362 dc
.DrawPolygon(5,todraw
,0,0,wxWINDING_RULE
);
364 //don't understand hwo but the outline is also depending on logicalfunction
365 dc
.SetPen( wxPen( "red", 4, wxSOLID
) );
366 dc
.SetBrush( *brush36
);
367 dc
.SetTextForeground(*wxCYAN
);
368 dc
.SetTextBackground(m_owner
->m_colourBackground
);
369 dc
.SetLogicalFunction(wxCOPY
);
370 dc
.DrawRectangle( x
+10, y
+10, 200, 200 );
371 dc
.SetBrush(wxNullBrush
);
372 dc
.SetPen(wxNullPen
);
375 case 1: //now with transparent fillpatterns
378 wxBitmap
* bmpBlit
= new wxBitmap(600,400);
379 wxMemoryDC
* memDC
= new wxMemoryDC();
380 // wxBrush _clearbrush(*wxGREEN,wxSOLID);
381 wxBrush
_clearbrush(*wxBLACK
,wxSOLID
);
382 memDC
->SelectObject(*bmpBlit
);
383 memDC
->BeginDrawing();
384 memDC
->SetBackground(_clearbrush
);
386 memDC
->SetBackground(wxNullBrush
);
388 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
389 memDC
->SetBrush( wxNullBrush
);
390 memDC
->SetBrush( *brush4
);
391 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
392 memDC
->SetTextBackground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
393 memDC
->SetLogicalFunction(wxAND_INVERT
);
395 // BLACK OUT the opaque pixels and leave the rest as is
396 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
398 // Set background and foreground colors for fill pattern
399 //the previous blacked out pixels are now merged with the layer color
400 //while the non blacked out pixels stay as they are.
401 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
403 //now define what will be the color of the fillpattern parts that are not transparent
404 // memDC->SetTextBackground(*wxBLUE);
405 memDC
->SetTextBackground(m_owner
->m_colourForeground
);
406 memDC
->SetLogicalFunction(wxOR
);
409 //don't understand how but the outline is also depending on logicalfunction
410 memDC
->SetPen( wxPen( "red", 4, wxSOLID
) );
411 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
413 memDC
->SetLogicalFunction(wxCOPY
);
415 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
416 memDC
->SetBrush( wxNullBrush
);
417 memDC
->SetBrush( *brush36
);
418 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
419 memDC
->SetTextBackground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
420 memDC
->SetLogicalFunction(wxAND_INVERT
);
422 memDC
->DrawRectangle( 10, 10, 200, 200 );
424 // Set background and foreground colors for fill pattern
425 //the previous blacked out pixels are now merged with the layer color
426 //while the non blacked out pixels stay as they are.
427 memDC
->SetTextForeground(*wxBLACK
); // 0s --> 0x000000 (black)
428 //now define what will be the color of the fillpattern parts that are not transparent
429 // memDC->SetTextBackground(*wxRED);
430 memDC
->SetTextBackground(m_owner
->m_colourBackground
);
431 memDC
->SetLogicalFunction(wxOR
);
433 //don't understand how but the outline is also depending on logicalfunction
434 memDC
->SetPen( wxPen( "yellow", 4, wxSOLID
) );
435 memDC
->DrawRectangle( 10, 10, 200, 200 );
437 memDC
->SetBrush(wxNullBrush
);
438 memDC
->SetPen(wxNullPen
);
441 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
446 case 2: //now with transparent inversed fillpatterns
448 wxBitmap
* bmpBlit
= new wxBitmap(600,400);
449 wxMemoryDC
* memDC
= new wxMemoryDC();
450 wxBrush
_clearbrush(*wxWHITE
,wxSOLID
);
451 memDC
->SelectObject(*bmpBlit
);
452 memDC
->BeginDrawing();
453 memDC
->SetBackground(_clearbrush
);
455 memDC
->SetBackground(wxNullBrush
);
457 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
458 memDC
->SetBrush( *brush4
);
459 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
460 memDC
->SetTextForeground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
461 memDC
->SetLogicalFunction(wxAND_INVERT
);
463 // BLACK OUT the opaque pixels and leave the rest as is
464 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
466 // Set background and foreground colors for fill pattern
467 //the previous blacked out pixels are now merged with the layer color
468 //while the non blacked out pixels stay as they are.
469 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
471 //now define what will be the color of the fillpattern parts that are not transparent
472 memDC
->SetTextForeground(m_owner
->m_colourForeground
);
473 memDC
->SetLogicalFunction(wxOR
);
476 //don't understand how but the outline is also depending on logicalfunction
477 memDC
->SetPen( wxPen( "red", 4, wxSOLID
) );
478 memDC
->DrawPolygon(5,todraw2
,0,0,wxWINDING_RULE
);
480 memDC
->SetLogicalFunction(wxCOPY
);
482 memDC
->SetPen( wxPen( "black", 4, wxSOLID
) );
483 memDC
->SetBrush( *brush36
);
484 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
485 memDC
->SetTextForeground(*wxWHITE
); // 1s --> 0xFFFFFF (white)
486 memDC
->SetLogicalFunction(wxAND_INVERT
);
488 memDC
->DrawRectangle( 10,10, 200, 200 );
490 // Set background and foreground colors for fill pattern
491 //the previous blacked out pixels are now merged with the layer color
492 //while the non blacked out pixels stay as they are.
493 memDC
->SetTextBackground(*wxBLACK
); // 0s --> 0x000000 (black)
494 //now define what will be the color of the fillpattern parts that are not transparent
495 memDC
->SetTextForeground(m_owner
->m_colourBackground
);
496 memDC
->SetLogicalFunction(wxOR
);
498 //don't understand how but the outline is also depending on logicalfunction
499 memDC
->SetPen( wxPen( "yellow", 4, wxSOLID
) );
500 memDC
->DrawRectangle( 10, 10, 200, 200 );
502 memDC
->SetBrush(wxNullBrush
);
503 memDC
->SetPen(wxNullPen
);
504 dc
.Blit(x
,y
,600,400,memDC
,0,0,wxCOPY
);
514 void MyCanvas::DrawTestLines( int x
, int y
, int width
, wxDC
&dc
)
516 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
517 dc
.SetBrush( *wxRED_BRUSH
);
518 dc
.DrawRectangle( x
+10, y
+10, 100, 190 );
520 dc
.SetPen( wxPen( "black", width
, wxSOLID
) );
521 dc
.DrawLine( x
+20, y
+20, 100, y
+20 );
522 dc
.SetPen( wxPen( "black", width
, wxDOT
) );
523 dc
.DrawLine( x
+20, y
+30, 100, y
+30 );
524 dc
.SetPen( wxPen( "black", width
, wxSHORT_DASH
) );
525 dc
.DrawLine( x
+20, y
+40, 100, y
+40 );
526 dc
.SetPen( wxPen( "black", width
, wxLONG_DASH
) );
527 dc
.DrawLine( x
+20, y
+50, 100, y
+50 );
528 dc
.SetPen( wxPen( "black", width
, wxDOT_DASH
) );
529 dc
.DrawLine( x
+20, y
+60, 100, y
+60 );
531 dc
.SetPen( wxPen( "black", width
, wxBDIAGONAL_HATCH
) );
532 dc
.DrawLine( x
+20, y
+70, 100, y
+70 );
533 dc
.SetPen( wxPen( "black", width
, wxCROSSDIAG_HATCH
) );
534 dc
.DrawLine( x
+20, y
+80, 100, y
+80 );
535 dc
.SetPen( wxPen( "black", width
, wxFDIAGONAL_HATCH
) );
536 dc
.DrawLine( x
+20, y
+90, 100, y
+90 );
537 dc
.SetPen( wxPen( "black", width
, wxCROSS_HATCH
) );
538 dc
.DrawLine( x
+20, y
+100, 100, y
+100 );
539 dc
.SetPen( wxPen( "black", width
, wxHORIZONTAL_HATCH
) );
540 dc
.DrawLine( x
+20, y
+110, 100, y
+110 );
541 dc
.SetPen( wxPen( "black", width
, wxVERTICAL_HATCH
) );
542 dc
.DrawLine( x
+20, y
+120, 100, y
+120 );
544 wxPen
ud( "black", width
, wxUSER_DASH
);
547 ud
.SetDashes( 1, dash1
);
548 dc
.DrawLine( x
+20, y
+140, 100, y
+140 );
550 ud
.SetDashes( 1, dash1
);
551 dc
.DrawLine( x
+20, y
+150, 100, y
+150 );
553 ud
.SetDashes( 1, dash1
);
554 dc
.DrawLine( x
+20, y
+160, 100, y
+160 );
556 ud
.SetDashes( 1, dash1
);
557 dc
.DrawLine( x
+20, y
+170, 100, y
+170 );
561 void MyCanvas::DrawDefault(wxDC
& dc
)
564 dc
.DrawCircle(0, 0, 10);
565 #if !(defined __WXGTK__) && !(defined __WXMOTIF__)
566 // not implemented in wxGTK or wxMOTIF :-(
567 dc
.FloodFill(0, 0, wxColour(255, 0, 0));
570 dc
.DrawIcon( wxICON(mondrian
), 410, 40 );
572 dc
.SetBrush( *wxBLACK_BRUSH
);
573 dc
.SetPen(*wxTRANSPARENT_PEN
);
574 dc
.DrawRectangle( 0, 100, 1000, 300 );
576 // test the rectangle outline drawing - there should be one pixel between
577 // the rect and the lines
578 dc
.SetPen(*wxWHITE_PEN
);
579 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
580 dc
.DrawRectangle(100, 170, 49, 29);
581 dc
.DrawRectangle(150, 170, 49, 29);
582 dc
.SetPen(*wxWHITE_PEN
);
583 dc
.DrawLine(200, 160, 200, 210);
584 dc
.DrawLine(100, 200, 210, 200);
586 // test the rectangle filled drawing - there should be one pixel between
587 // the rect and the lines
588 dc
.SetPen(*wxTRANSPARENT_PEN
);
589 dc
.SetBrush( *wxWHITE_BRUSH
);
590 dc
.DrawRectangle(300, 170, 49, 29);
591 dc
.DrawRectangle(350, 170, 49, 29);
592 dc
.SetPen(*wxWHITE_PEN
);
593 dc
.DrawLine(400, 160, 400, 210);
594 dc
.DrawLine(300, 200, 410, 200);
596 // test the rectangle outline drawing - there should be one pixel between
597 // the rect and the lines
598 dc
.SetPen(*wxWHITE_PEN
);
599 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
600 dc
.DrawRoundedRectangle(100, 270, 49, 29, 6);
601 dc
.DrawRoundedRectangle(150, 270, 49, 29, 6);
602 dc
.SetPen(*wxWHITE_PEN
);
603 dc
.DrawLine(200, 260, 200, 310);
604 dc
.DrawLine(100, 300, 210, 300);
606 // test the rectangle filled drawing - there should be one pixel between
607 // the rect and the lines
608 dc
.SetPen(*wxTRANSPARENT_PEN
);
609 dc
.SetBrush( *wxWHITE_BRUSH
);
610 dc
.DrawRoundedRectangle(300, 270, 49, 29, 6);
611 dc
.DrawRoundedRectangle(350, 270, 49, 29, 6);
612 dc
.SetPen(*wxWHITE_PEN
);
613 dc
.DrawLine(400, 260, 400, 310);
614 dc
.DrawLine(300, 300, 410, 300);
618 void MyCanvas::DrawText(wxDC
& dc
)
620 // set underlined font for testing
621 dc
.SetFont( wxFont(12, wxMODERN
, wxNORMAL
, wxNORMAL
, TRUE
) );
622 dc
.DrawText( "This is text", 110, 10 );
623 dc
.DrawRotatedText( "That is text", 20, 10, -45 );
625 dc
.SetFont( *wxNORMAL_FONT
);
628 dc
. SetBackgroundMode(wxTRANSPARENT
);
630 for ( int n
= -180; n
< 180; n
+= 30 )
632 text
.Printf(" %d rotated text", n
);
633 dc
.DrawRotatedText(text
, 400, 400, n
);
636 dc
.SetFont( wxFont( 18, wxSWISS
, wxNORMAL
, wxNORMAL
) );
638 dc
.DrawText( "This is Swiss 18pt text.", 110, 40 );
643 dc
.GetTextExtent( "This is Swiss 18pt text.", &length
, &height
, &descent
);
644 text
.Printf( "Dimensions are length %ld, height %ld, descent %ld", length
, height
, descent
);
645 dc
.DrawText( text
, 110, 80 );
647 text
.Printf( "CharHeight() returns: %d", dc
.GetCharHeight() );
648 dc
.DrawText( text
, 110, 120 );
650 dc
.DrawRectangle( 100, 40, 4, height
);
653 void MyCanvas::DrawImages(wxDC
& dc
)
659 } rasterOperations
[] =
662 { "wxAND_INVERT", wxAND_INVERT
},
663 { "wxAND_REVERSE", wxAND_REVERSE
},
664 { "wxCLEAR", wxCLEAR
},
665 { "wxCOPY", wxCOPY
},
666 { "wxEQUIV", wxEQUIV
},
667 { "wxINVERT", wxINVERT
},
668 { "wxNAND", wxNAND
},
669 { "wxNO_OP", wxNO_OP
},
671 { "wxOR_INVERT", wxOR_INVERT
},
672 { "wxOR_REVERSE", wxOR_REVERSE
},
674 { "wxSRC_INVERT", wxSRC_INVERT
},
678 dc
.DrawText("original image", 0, 0);
679 dc
.DrawBitmap(gs_bmpNoMask
, 0, 20, 0);
680 dc
.DrawText("with colour mask", 0, 100);
681 dc
.DrawBitmap(gs_bmpWithColMask
, 0, 120, TRUE
);
682 dc
.DrawText("the mask image", 0, 200);
683 dc
.DrawBitmap(gs_bmpMask
, 0, 220, 0);
684 dc
.DrawText("masked image", 0, 300);
685 dc
.DrawBitmap(gs_bmpWithMask
, 0, 320, TRUE
);
687 int cx
= gs_bmpWithColMask
.GetWidth(),
688 cy
= gs_bmpWithColMask
.GetHeight();
691 for ( size_t n
= 0; n
< WXSIZEOF(rasterOperations
); n
++ )
693 wxCoord x
= 120 + 150*(n%4
),
696 dc
.DrawText(rasterOperations
[n
].name
, x
, y
- 20);
697 memDC
.SelectObject(gs_bmpWithColMask
);
698 dc
.Blit(x
, y
, cx
, cy
, &memDC
, 0, 0, rasterOperations
[n
].rop
, TRUE
);
702 void MyCanvas::OnPaint(wxPaintEvent
&WXUNUSED(event
))
706 m_owner
->PrepareDC(dc
);
708 dc
.SetBackgroundMode( m_owner
->m_backgroundMode
);
709 if ( m_owner
->m_backgroundBrush
.Ok() )
710 dc
.SetBackground( m_owner
->m_backgroundBrush
);
711 if ( m_owner
->m_colourForeground
.Ok() )
712 dc
.SetTextForeground( m_owner
->m_colourForeground
);
713 if ( m_owner
->m_colourBackground
.Ok() )
714 dc
.SetTextBackground( m_owner
->m_colourBackground
);
727 DrawTestLines( 0, 100, 0, dc
);
728 DrawTestLines( 0, 300, 1, dc
);
729 DrawTestLines( 0, 500, 2, dc
);
730 DrawTestLines( 0, 700, 6, dc
);
734 DrawTestPoly( 0, 100, dc
, 0 );
735 DrawTestPoly( 33, 500, dc
, 1 );
736 DrawTestPoly( 43, 1000, dc
, 2 );
745 void MyCanvas::OnMouseMove(wxMouseEvent
&event
)
749 m_owner
->PrepareDC(dc
);
751 wxPoint pos
= event
.GetPosition();
752 long x
= dc
.DeviceToLogicalX( pos
.x
);
753 long y
= dc
.DeviceToLogicalY( pos
.y
);
755 str
.Printf( "Current mouse position: %d,%d", (int)x
, (int)y
);
756 m_owner
->SetStatusText( str
);
759 // ----------------------------------------------------------------------------
761 // ----------------------------------------------------------------------------
763 // the event tables connect the wxWindows events with the functions (event
764 // handlers) which process them. It can be also done at run-time, but for the
765 // simple menu events like this the static method is much simpler.
766 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
767 EVT_MENU (File_Quit
, MyFrame::OnQuit
)
768 EVT_MENU (File_About
, MyFrame::OnAbout
)
770 EVT_MENU_RANGE(MenuShow_First
, MenuShow_Last
, MyFrame::OnShow
)
772 EVT_MENU_RANGE(MenuOption_First
, MenuOption_Last
, MyFrame::OnOption
)
776 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
777 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
779 // set the frame icon
780 SetIcon(wxICON(mondrian
));
782 wxMenu
*menuFile
= new wxMenu
;
783 menuFile
->Append(File_ShowDefault
, "&Default screen\tF1");
784 menuFile
->Append(File_ShowText
, "&Text screen\tF2");
785 menuFile
->Append(File_ShowLines
, "&Lines screen\tF3");
786 menuFile
->Append(File_ShowPolygons
, "&Polygons screen\tF4");
787 menuFile
->Append(File_ShowMask
, "wx&Mask screen\tF5");
788 menuFile
->AppendSeparator();
789 menuFile
->Append(File_About
, "&About...\tCtrl-A", "Show about dialog");
790 menuFile
->AppendSeparator();
791 menuFile
->Append(File_Quit
, "E&xit\tAlt-X", "Quit this program");
793 wxMenu
*menuMapMode
= new wxMenu
;
794 menuMapMode
->Append( MapMode_Text
, "&TEXT map mode" );
795 menuMapMode
->Append( MapMode_Lometric
, "&LOMETRIC map mode" );
796 menuMapMode
->Append( MapMode_Twips
, "T&WIPS map mode" );
797 menuMapMode
->Append( MapMode_Points
, "&POINTS map mode" );
798 menuMapMode
->Append( MapMode_Metric
, "&METRIC map mode" );
800 wxMenu
*menuUserScale
= new wxMenu
;
801 menuUserScale
->Append( UserScale_StretchHoriz
, "Stretch horizontally\tCtrl-H" );
802 menuUserScale
->Append( UserScale_ShrinkHoriz
, "Shrink horizontally\tCtrl-G" );
803 menuUserScale
->Append( UserScale_StretchVertic
, "Stretch vertically\tCtrl-V" );
804 menuUserScale
->Append( UserScale_ShrinkVertic
, "Shrink vertically\tCtrl-W" );
805 menuUserScale
->AppendSeparator();
806 menuUserScale
->Append( UserScale_Restore
, "Restore to normal\tCtrl-0" );
808 wxMenu
*menuAxis
= new wxMenu
;
809 menuAxis
->Append( AxisMirror_Horiz
, "Mirror horizontally\tCtrl-M", "", TRUE
);
810 menuAxis
->Append( AxisMirror_Vertic
, "Mirror vertically\tCtrl-N", "", TRUE
);
812 wxMenu
*menuLogical
= new wxMenu
;
813 menuLogical
->Append( LogicalOrigin_MoveDown
, "Move &down\tCtrl-D" );
814 menuLogical
->Append( LogicalOrigin_MoveUp
, "Move &up\tCtrl-U" );
815 menuLogical
->Append( LogicalOrigin_MoveLeft
, "Move &right\tCtrl-L" );
816 menuLogical
->Append( LogicalOrigin_MoveRight
, "Move &left\tCtrl-R" );
818 wxMenu
*menuColour
= new wxMenu
;
819 menuColour
->Append( Colour_TextForeground
, "Text foreground..." );
820 menuColour
->Append( Colour_TextBackground
, "Text background..." );
821 menuColour
->Append( Colour_Background
, "Background colour..." );
822 menuColour
->Append( Colour_BackgroundMode
, "Opaque/transparent\tCtrl-B", "", TRUE
);
824 // now append the freshly created menu to the menu bar...
825 wxMenuBar
*menuBar
= new wxMenuBar
;
826 menuBar
->Append(menuFile
, "&File");
827 menuBar
->Append(menuMapMode
, "&MapMode");
828 menuBar
->Append(menuUserScale
, "&UserScale");
829 menuBar
->Append(menuAxis
, "&Axis");
830 menuBar
->Append(menuLogical
, "&LogicalOrigin");
831 menuBar
->Append(menuColour
, "&Colours");
833 // ... and attach this menu bar to the frame
836 // create a status bar just for fun (by default with 1 pane only)
838 SetStatusText("Welcome to wxWindows!");
840 m_mapMode
= wxMM_TEXT
;
843 m_xLogicalOrigin
= 0;
844 m_yLogicalOrigin
= 0;
846 m_yAxisReversed
= FALSE
;
847 m_backgroundMode
= wxSOLID
;
848 m_colourForeground
= *wxRED
;
849 m_colourBackground
= *wxBLUE
;
851 m_canvas
= new MyCanvas( this );
852 m_canvas
->SetScrollbars( 10, 10, 100, 240 );
857 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
859 // TRUE is to force the frame to close
863 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
866 msg
.Printf( _T("This is the about dialog of the drawing sample.\n")
867 _T("Copyright (c) Robert Roebling 1999")
870 wxMessageBox(msg
, "About Drawing", wxOK
| wxICON_INFORMATION
, this);
873 void MyFrame::OnShow(wxCommandEvent
& event
)
875 m_canvas
->Show((ScreenToShow
)(event
.GetInt() - MenuShow_First
));
878 void MyFrame::OnOption(wxCommandEvent
& event
)
880 switch (event
.GetInt())
883 m_mapMode
= wxMM_TEXT
;
885 case MapMode_Lometric
:
886 m_mapMode
= wxMM_LOMETRIC
;
889 m_mapMode
= wxMM_TWIPS
;
892 m_mapMode
= wxMM_POINTS
;
895 m_mapMode
= wxMM_METRIC
;
898 case LogicalOrigin_MoveDown
:
899 m_yLogicalOrigin
+= 10;
901 case LogicalOrigin_MoveUp
:
902 m_yLogicalOrigin
-= 10;
904 case LogicalOrigin_MoveLeft
:
905 m_xLogicalOrigin
+= 10;
907 case LogicalOrigin_MoveRight
:
908 m_xLogicalOrigin
-= 10;
911 case UserScale_StretchHoriz
:
912 m_xUserScale
*= 1.10;
914 case UserScale_ShrinkHoriz
:
915 m_xUserScale
/= 1.10;
917 case UserScale_StretchVertic
:
918 m_yUserScale
*= 1.10;
920 case UserScale_ShrinkVertic
:
921 m_yUserScale
/= 1.10;
923 case UserScale_Restore
:
928 case AxisMirror_Vertic
:
929 m_yAxisReversed
= !m_yAxisReversed
;
931 case AxisMirror_Horiz
:
932 m_xAxisReversed
= !m_xAxisReversed
;
935 case Colour_TextForeground
:
936 m_colourForeground
= SelectColour();
938 case Colour_TextBackground
:
939 m_colourBackground
= SelectColour();
941 case Colour_Background
:
943 wxColour col
= SelectColour();
946 m_backgroundBrush
.SetColour(col
);
950 case Colour_BackgroundMode
:
951 m_backgroundMode
= m_backgroundMode
== wxSOLID
? wxTRANSPARENT
963 void MyFrame::PrepareDC(wxDC
& dc
)
965 dc
.SetMapMode( m_mapMode
);
966 dc
.SetUserScale( m_xUserScale
, m_yUserScale
);
967 dc
.SetLogicalOrigin( m_xLogicalOrigin
, m_yLogicalOrigin
);
968 dc
.SetAxisOrientation( !m_xAxisReversed
, m_yAxisReversed
);
971 wxColour
MyFrame::SelectColour()
975 wxColourDialog
dialog(this, &data
);
977 if ( dialog
.ShowModal() == wxID_OK
)
979 col
= dialog
.GetColourData().GetColour();