]>
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 #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 MyCanvas
: public wxScrolledWindow
60 MyCanvas( wxFrame
*parent
);
62 void UseBuffer(bool useBuffer
) { m_useBuffer
= useBuffer
; Refresh(); }
63 void EraseBg(bool eraseBg
) { m_eraseBg
= eraseBg
; Refresh(); }
66 void OnPaint( wxPaintEvent
&event
);
67 void OnChar( wxKeyEvent
&event
);
68 void OnEraseBackground( wxEraseEvent
&event
);
70 void DoPaint(wxDC
& dc
);
76 // use wxMemoryDC in OnPaint()?
79 // paint custom background in OnEraseBackground()?
86 class MyFrame
: public wxFrame
91 void OnUseBuffer(wxCommandEvent
& event
);
92 void OnEraseBg(wxCommandEvent
& event
);
93 void OnQuit(wxCommandEvent
& event
);
94 void OnAbout(wxCommandEvent
& event
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
110 Erase_Menu_UseBuffer
= 100,
112 Erase_Menu_Exit
= wxID_EXIT
,
113 Erase_Menu_About
= wxID_ABOUT
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
125 MyFrame
*frame
= new MyFrame
;
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
137 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
138 EVT_MENU(Erase_Menu_EraseBg
, MyFrame::OnEraseBg
)
139 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
140 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
145 : wxFrame(NULL
, wxID_ANY
, _T("Erase sample"),
146 wxPoint(50, 50), wxSize(450, 340))
148 SetIcon(wxICON(mondrian
));
150 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
151 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, _T("&Use memory DC\tCtrl-M"));
152 menuFile
->AppendCheckItem(Erase_Menu_EraseBg
, _T("Custom &background\tCtrl-B"));
153 menuFile
->AppendSeparator();
154 menuFile
->Append(Erase_Menu_Exit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
157 wxMenu
*helpMenu
= new wxMenu
;
158 helpMenu
->Append(Erase_Menu_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
160 wxMenuBar
*menuBar
= new wxMenuBar();
161 menuBar
->Append(menuFile
, _T("&File"));
162 menuBar
->Append(helpMenu
, _T("&Help"));
167 // create a status bar just for fun (by default with 1 pane only)
169 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
170 #endif // wxUSE_STATUSBAR
172 m_canvas
= new MyCanvas( this );
176 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
178 m_canvas
->UseBuffer(event
.IsChecked());
181 void MyFrame::OnEraseBg(wxCommandEvent
& event
)
183 m_canvas
->EraseBg(event
.IsChecked());
186 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
191 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
193 wxMessageBox(_T("This sample shows how you can draw custom background."),
194 _T("About Erase Sample"), wxOK
| wxICON_INFORMATION
, this);
198 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
199 EVT_PAINT( MyCanvas::OnPaint
)
200 EVT_CHAR( MyCanvas::OnChar
)
201 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground
)
204 MyCanvas::MyCanvas( wxFrame
*parent
)
205 : wxScrolledWindow( parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
206 wxScrolledWindowStyle
| wxSUNKEN_BORDER
)
211 SetScrollbars( 10, 10, 40, 100, 0, 0 );
213 m_bitmap
= wxBitmap( wxICON(mondrian
) );
215 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
217 SetFocusIgnoringChildren();
220 void MyCanvas::OnChar( wxKeyEvent
&event
)
225 m_text
+= event
.m_uniChar
;
232 switch (event
.m_keyCode
)
234 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
235 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
236 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
237 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
238 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
239 default: m_text
+= (wxChar
)event
.m_keyCode
; break;
243 void MyCanvas::DoPaint(wxDC
& dc
)
245 dc
.SetBrush( *wxBLACK_BRUSH
);
246 dc
.DrawRectangle( 10,10,200,50 );
248 dc
.DrawBitmap( m_bitmap
, 10, 20, true );
250 dc
.SetTextForeground(*wxBLUE
);
251 dc
.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
254 tmp
.Printf( _T("Hit any key to display more text: %s"), m_text
.c_str() );
256 dc
.GetTextExtent( tmp
, &w
, &h
);
257 dc
.SetBrush( *wxWHITE_BRUSH
);
258 dc
.DrawRectangle( 65, 85, w
, h
);
259 dc
.DrawText( tmp
, 65, 85 );
262 wxRegionIterator
upd( GetUpdateRegion() );
265 wxLogDebug( _T("Paint: %d %d %d %d"), upd
.GetX(), upd
.GetY(), upd
.GetWidth(), upd
.GetHeight() );
271 wxSize size
= GetSize();
272 wxSize client_size
= GetClientSize();
273 wxLogDebug( _T("size %d %d client_size %d %d"), size
.x
, size
.y
, client_size
.x
, client_size
.y
);
278 dc
.SetPen( *wxWHITE_PEN
);
279 for (i
= 0; i
< 20; i
+= 2)
280 dc
.DrawLine( i
,i
, i
+100,i
);
282 dc
.SetPen( *wxWHITE_PEN
);
283 for (i
= 200; i
< 220; i
+= 2)
284 dc
.DrawLine( i
-200,i
, i
-100,i
);
286 wxRegion
region( 110, 110, 80, 80 );
287 wxRegion
hole( 130, 130, 40, 1 );
288 region
.Intersect( hole
);
289 dc
.SetClippingRegion( region
);
291 dc
.SetBrush( *wxRED_BRUSH
);
292 dc
.DrawRectangle( 100, 100, 200, 200 );
294 dc
.DestroyClippingRegion();
296 dc
.SetPen( *wxTRANSPARENT_PEN
);
298 wxRegion
strip( 110, 200, 30, 1 );
299 wxRegionIterator
it( strip
);
302 dc
.DrawRectangle( it
.GetX(), it
.GetY(), it
.GetWidth(), it
.GetHeight() );
308 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
310 wxPaintDC
dcWin(this);
315 const wxSize size
= GetClientSize();
317 wxBitmap
bmp(size
.x
, size
.y
);
318 dc
.SelectObject(bmp
);
319 dc
.Blit(0, 0, size
.x
, size
.y
, &dcWin
, 0, 0);
320 dc
.DrawText(_T("(copy of background)"), 5, 120 );
324 dcWin
.Blit(0, 0, size
.x
, size
.y
, &dc
, 0, 0);
332 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
340 wxDC
& dc
= *event
.GetDC();
341 dc
.SetPen(*wxGREEN_PEN
);
345 // clear any junk currently displayed
348 const wxSize size
= GetClientSize();
349 for ( int x
= 0; x
< size
.x
; x
+= 15 )
351 dc
.DrawLine(x
, 0, x
, size
.y
);
354 for ( int y
= 0; y
< size
.y
; y
+= 15 )
356 dc
.DrawLine(0, y
, size
.x
, y
);
359 dc
.SetTextForeground(*wxRED
);
360 dc
.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160);