1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Erase wxWidgets sample
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "erase.cpp"
22 #pragma interface "erase.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" wxWidgets headers)
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
41 // the application icon
42 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
43 #include "mondrian.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 class MyApp
: public wxApp
53 virtual bool OnInit();
57 class MyFrame
: public wxFrame
60 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
62 void OnQuit(wxCommandEvent
& event
);
63 void OnAbout(wxCommandEvent
& event
);
70 class MyCanvas
: public wxScrolledWindow
73 MyCanvas( MyFrame
*parent
);
75 void OnPaint( wxPaintEvent
&event
);
76 void OnChar( wxKeyEvent
&event
);
77 void OnEraseBackground( wxEraseEvent
&event
);
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
98 // the application class
99 // ----------------------------------------------------------------------------
105 MyFrame
*frame
= new MyFrame(_T("Erase sample"),
106 wxPoint(50, 50), wxSize(450, 340));
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
118 EVT_MENU(ID_MENU_QUIT
, MyFrame::OnQuit
)
119 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
123 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
124 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
126 SetIcon(wxICON(mondrian
));
128 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
130 wxMenu
*helpMenu
= new wxMenu
;
131 helpMenu
->Append(wxID_ABOUT
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
133 menuFile
->Append(ID_MENU_QUIT
, _T("E&xit\tAlt-X"), _T("Quit this program"));
135 wxMenuBar
*menuBar
= new wxMenuBar();
136 menuBar
->Append(menuFile
, _T("&File"));
137 menuBar
->Append(helpMenu
, _T("&Help"));
142 // create a status bar just for fun (by default with 1 pane only)
144 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
145 #endif // wxUSE_STATUSBAR
147 (void)new MyCanvas( this );
151 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
156 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
158 wxMessageBox(_T("This sample shows how you can draw custom background."),
159 _T("About Erase Sample"), wxOK
| wxICON_INFORMATION
, this);
163 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
164 EVT_PAINT( MyCanvas::OnPaint
)
165 EVT_CHAR( MyCanvas::OnChar
)
166 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground
)
169 MyCanvas::MyCanvas( MyFrame
*parent
)
170 : wxScrolledWindow( parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
171 wxScrolledWindowStyle
|
172 wxNO_FULL_REPAINT_ON_RESIZE
|
175 SetScrollbars( 10, 10, 40, 100, 0, 0 );
177 m_bitmap
= wxBitmap( wxICON(mondrian
) );
179 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
182 void MyCanvas::OnChar( wxKeyEvent
&event
)
187 m_text
+= event
.m_uniChar
;
194 switch (event
.m_keyCode
)
196 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
197 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
198 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
199 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
200 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
201 default: m_text
+= event
.m_keyCode
; break;
206 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
211 dc
.SetBrush( *wxBLACK_BRUSH
);
212 dc
.DrawRectangle( 0,0,200,50 );
214 dc
.DrawBitmap( m_bitmap
, 10, 20, true );
216 dc
.SetTextForeground(*wxBLUE
);
217 dc
.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
220 tmp
.Printf( _T("Hit any key to display more text: %s"), m_text
.c_str() );
222 dc
.GetTextExtent( tmp
, &w
, &h
);
223 dc
.SetBrush( *wxWHITE_BRUSH
);
224 dc
.DrawRectangle( 65, 85, w
, h
);
225 dc
.DrawText( tmp
, 65, 85 );
228 wxRegionIterator
upd( GetUpdateRegion() );
231 wxLogDebug( _T("Paint: %d %d %d %d"), upd
.GetX(), upd
.GetY(), upd
.GetWidth(), upd
.GetHeight() );
237 wxSize size
= GetSize();
238 wxSize client_size
= GetClientSize();
239 wxLogDebug( _T("size %d %d client_size %d %d"), size
.x
, size
.y
, client_size
.x
, client_size
.y
);
244 dc
.SetPen( *wxWHITE_PEN
);
245 for (i
= 0; i
< 20; i
+= 2)
246 dc
.DrawLine( i
,i
, i
+100,i
);
248 dc
.SetPen( *wxWHITE_PEN
);
249 for (i
= 200; i
< 220; i
+= 2)
250 dc
.DrawLine( i
-200,i
, i
-100,i
);
252 wxRegion
region( 110, 110, 80, 80 );
253 wxRegion
hole( 130, 130, 40, 1 );
254 region
.Intersect( hole
);
255 dc
.SetClippingRegion( region
);
257 dc
.SetBrush( *wxRED_BRUSH
);
258 dc
.DrawRectangle( 100, 100, 200, 200 );
260 dc
.DestroyClippingRegion();
262 dc
.SetPen( *wxTRANSPARENT_PEN
);
264 wxRegion
strip( 110, 200, 30, 1 );
265 wxRegionIterator
it( strip
);
268 dc
.DrawRectangle( it
.GetX(), it
.GetY(), it
.GetWidth(), it
.GetHeight() );
274 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
276 wxDC
& dc
= *event
.GetDC();
277 dc
.SetPen(*wxGREEN_PEN
);
279 // this line is needed, otherwise the junk is left on win the background
282 wxSize size
= GetClientSize();
283 for ( int x
= 0; x
< size
.x
; x
+= 10 )
285 dc
.DrawLine(x
, 0, x
, size
.y
);
288 for ( int y
= 0; y
< size
.y
; y
+= 10 )
290 dc
.DrawLine(0, y
, size
.x
, y
);
293 dc
.SetTextForeground(*wxRED
);
294 dc
.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 60);