]>
git.saurik.com Git - wxWidgets.git/blob - samples/erase/erase.cpp
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 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
36 // the application icon
37 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
38 #include "mondrian.xpm"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 class MyApp
: public wxApp
48 virtual bool OnInit();
52 class MyCanvas
: public wxScrolledWindow
55 MyCanvas( wxFrame
*parent
);
57 void UseBuffer(bool useBuffer
) { m_useBuffer
= useBuffer
; Refresh(); }
58 void EraseBg(bool eraseBg
) { m_eraseBg
= eraseBg
; Refresh(); }
61 void OnPaint( wxPaintEvent
&event
);
62 void OnChar( wxKeyEvent
&event
);
63 void OnEraseBackground( wxEraseEvent
&event
);
65 void DoPaint(wxDC
& dc
);
71 // use wxMemoryDC in OnPaint()?
74 // paint custom background in OnEraseBackground()?
81 class MyFrame
: public wxFrame
86 void OnUseBuffer(wxCommandEvent
& event
);
87 void OnEraseBg(wxCommandEvent
& event
);
88 void OnQuit(wxCommandEvent
& event
);
89 void OnAbout(wxCommandEvent
& event
);
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
105 Erase_Menu_UseBuffer
= 100,
107 Erase_Menu_Exit
= wxID_EXIT
,
108 Erase_Menu_About
= wxID_ABOUT
112 // ----------------------------------------------------------------------------
113 // the application class
114 // ----------------------------------------------------------------------------
120 if ( !wxApp::OnInit() )
123 MyFrame
*frame
= new MyFrame
;
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
135 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
136 EVT_MENU(Erase_Menu_EraseBg
, MyFrame::OnEraseBg
)
137 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
138 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
143 : wxFrame(NULL
, wxID_ANY
, _T("Erase sample"),
144 wxPoint(50, 50), wxSize(450, 340))
146 SetIcon(wxICON(mondrian
));
148 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
149 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, _T("&Use memory DC\tCtrl-M"));
150 menuFile
->AppendCheckItem(Erase_Menu_EraseBg
, _T("Custom &background\tCtrl-B"));
151 menuFile
->AppendSeparator();
152 menuFile
->Append(Erase_Menu_Exit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
155 wxMenu
*helpMenu
= new wxMenu
;
156 helpMenu
->Append(Erase_Menu_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
158 wxMenuBar
*menuBar
= new wxMenuBar();
159 menuBar
->Append(menuFile
, _T("&File"));
160 menuBar
->Append(helpMenu
, _T("&Help"));
165 // create a status bar just for fun (by default with 1 pane only)
167 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
168 #endif // wxUSE_STATUSBAR
170 m_canvas
= new MyCanvas( this );
174 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
176 m_canvas
->UseBuffer(event
.IsChecked());
179 void MyFrame::OnEraseBg(wxCommandEvent
& event
)
181 m_canvas
->EraseBg(event
.IsChecked());
184 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
189 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
191 wxMessageBox(_T("This sample shows how you can draw custom background."),
192 _T("About Erase Sample"), wxOK
| wxICON_INFORMATION
, this);
196 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
197 EVT_PAINT( MyCanvas::OnPaint
)
198 EVT_CHAR( MyCanvas::OnChar
)
199 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground
)
202 MyCanvas::MyCanvas( wxFrame
*parent
)
203 : wxScrolledWindow( parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
204 wxScrolledWindowStyle
| wxSUNKEN_BORDER
)
209 SetScrollbars( 10, 10, 40, 100, 0, 0 );
211 m_bitmap
= wxBitmap( wxICON(mondrian
) );
213 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
215 SetFocusIgnoringChildren();
218 void MyCanvas::OnChar( wxKeyEvent
&event
)
223 m_text
+= event
.m_uniChar
;
230 switch (event
.m_keyCode
)
232 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
233 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
234 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
235 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
236 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
237 default: m_text
+= (wxChar
)event
.m_keyCode
; break;
241 void MyCanvas::DoPaint(wxDC
& dc
)
243 dc
.SetBrush( *wxBLACK_BRUSH
);
244 dc
.DrawRectangle( 10,10,200,50 );
246 dc
.DrawBitmap( m_bitmap
, 10, 20, true );
248 dc
.SetTextForeground(*wxBLUE
);
249 dc
.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
252 tmp
.Printf( _T("Hit any key to display more text: %s"), m_text
.c_str() );
254 dc
.GetTextExtent( tmp
, &w
, &h
);
255 dc
.SetBrush( *wxWHITE_BRUSH
);
256 dc
.DrawRectangle( 65, 85, w
, h
);
257 dc
.DrawText( tmp
, 65, 85 );
260 wxRegionIterator
upd( GetUpdateRegion() );
263 wxLogDebug( _T("Paint: %d %d %d %d"), upd
.GetX(), upd
.GetY(), upd
.GetWidth(), upd
.GetHeight() );
269 wxSize size
= GetSize();
270 wxSize client_size
= GetClientSize();
271 wxLogDebug( _T("size %d %d client_size %d %d"), size
.x
, size
.y
, client_size
.x
, client_size
.y
);
276 dc
.SetPen( *wxWHITE_PEN
);
277 for (i
= 0; i
< 20; i
+= 2)
278 dc
.DrawLine( i
,i
, i
+100,i
);
280 dc
.SetPen( *wxWHITE_PEN
);
281 for (i
= 200; i
< 220; i
+= 2)
282 dc
.DrawLine( i
-200,i
, i
-100,i
);
284 wxRegion
region( 110, 110, 80, 80 );
285 wxRegion
hole( 130, 130, 40, 1 );
286 region
.Intersect( hole
);
287 dc
.SetClippingRegion( region
);
289 dc
.SetBrush( *wxRED_BRUSH
);
290 dc
.DrawRectangle( 100, 100, 200, 200 );
292 dc
.DestroyClippingRegion();
294 dc
.SetPen( *wxTRANSPARENT_PEN
);
296 wxRegion
strip( 110, 200, 30, 1 );
297 wxRegionIterator
it( strip
);
300 dc
.DrawRectangle( it
.GetX(), it
.GetY(), it
.GetWidth(), it
.GetHeight() );
306 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
308 wxPaintDC
dcWin(this);
313 const wxSize size
= GetClientSize();
315 wxBitmap
bmp(size
.x
, size
.y
);
316 dc
.SelectObject(bmp
);
317 dc
.Blit(0, 0, size
.x
, size
.y
, &dcWin
, 0, 0);
318 dc
.DrawText(_T("(copy of background)"), 5, 120 );
322 dcWin
.Blit(0, 0, size
.x
, size
.y
, &dc
, 0, 0);
330 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
338 wxDC
& dc
= *event
.GetDC();
339 dc
.SetPen(*wxGREEN_PEN
);
343 // clear any junk currently displayed
346 const wxSize size
= GetClientSize();
347 for ( int x
= 0; x
< size
.x
; x
+= 15 )
349 dc
.DrawLine(x
, 0, x
, size
.y
);
352 for ( int y
= 0; y
< size
.y
; y
+= 15 )
354 dc
.DrawLine(0, y
, size
.x
, y
);
357 dc
.SetTextForeground(*wxRED
);
358 dc
.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160);