]>
git.saurik.com Git - wxWidgets.git/blob - samples/erase/erase.cpp
932c2baccedc8b4d707382b82ee9e12a2d6ade8a
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(); }
65 void OnPaint( wxPaintEvent
&event
);
66 void OnChar( wxKeyEvent
&event
);
67 void OnEraseBackground( wxEraseEvent
&event
);
69 void DoPaint(wxDC
& dc
);
75 // use wxMemoryDC in OnPaint()?
81 class MyFrame
: public wxFrame
86 void OnUseBuffer(wxCommandEvent
& event
);
87 void OnQuit(wxCommandEvent
& event
);
88 void OnAbout(wxCommandEvent
& event
);
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
104 Erase_Menu_UseBuffer
= 100,
105 Erase_Menu_Exit
= wxID_EXIT
,
106 Erase_Menu_About
= wxID_ABOUT
110 // ----------------------------------------------------------------------------
111 // the application class
112 // ----------------------------------------------------------------------------
118 MyFrame
*frame
= new MyFrame
;
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
130 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
131 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
132 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
137 : wxFrame(NULL
, wxID_ANY
, _T("Erase sample"),
138 wxPoint(50, 50), wxSize(450, 340))
140 SetIcon(wxICON(mondrian
));
142 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
143 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, _T("&Use memory DC\tCtrl-M"));
144 menuFile
->AppendSeparator();
145 menuFile
->Append(Erase_Menu_Exit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
148 wxMenu
*helpMenu
= new wxMenu
;
149 helpMenu
->Append(Erase_Menu_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
151 wxMenuBar
*menuBar
= new wxMenuBar();
152 menuBar
->Append(menuFile
, _T("&File"));
153 menuBar
->Append(helpMenu
, _T("&Help"));
158 // create a status bar just for fun (by default with 1 pane only)
160 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
161 #endif // wxUSE_STATUSBAR
163 m_canvas
= new MyCanvas( this );
167 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
169 m_canvas
->UseBuffer(event
.IsChecked());
172 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
177 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
179 wxMessageBox(_T("This sample shows how you can draw custom background."),
180 _T("About Erase Sample"), wxOK
| wxICON_INFORMATION
, this);
184 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
185 EVT_PAINT( MyCanvas::OnPaint
)
186 EVT_CHAR( MyCanvas::OnChar
)
187 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground
)
190 MyCanvas::MyCanvas( wxFrame
*parent
)
191 : wxScrolledWindow( parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
192 wxScrolledWindowStyle
| wxSUNKEN_BORDER
)
196 SetScrollbars( 10, 10, 40, 100, 0, 0 );
198 m_bitmap
= wxBitmap( wxICON(mondrian
) );
200 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
202 SetFocusIgnoringChildren();
205 void MyCanvas::OnChar( wxKeyEvent
&event
)
210 m_text
+= event
.m_uniChar
;
217 switch (event
.m_keyCode
)
219 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
220 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
221 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
222 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
223 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
224 default: m_text
+= (wxChar
)event
.m_keyCode
; break;
228 void MyCanvas::DoPaint(wxDC
& dc
)
230 dc
.SetBrush( *wxBLACK_BRUSH
);
231 dc
.DrawRectangle( 10,10,200,50 );
233 dc
.DrawBitmap( m_bitmap
, 10, 20, true );
235 dc
.SetTextForeground(*wxBLUE
);
236 dc
.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
239 tmp
.Printf( _T("Hit any key to display more text: %s"), m_text
.c_str() );
241 dc
.GetTextExtent( tmp
, &w
, &h
);
242 dc
.SetBrush( *wxWHITE_BRUSH
);
243 dc
.DrawRectangle( 65, 85, w
, h
);
244 dc
.DrawText( tmp
, 65, 85 );
247 wxRegionIterator
upd( GetUpdateRegion() );
250 wxLogDebug( _T("Paint: %d %d %d %d"), upd
.GetX(), upd
.GetY(), upd
.GetWidth(), upd
.GetHeight() );
256 wxSize size
= GetSize();
257 wxSize client_size
= GetClientSize();
258 wxLogDebug( _T("size %d %d client_size %d %d"), size
.x
, size
.y
, client_size
.x
, client_size
.y
);
263 dc
.SetPen( *wxWHITE_PEN
);
264 for (i
= 0; i
< 20; i
+= 2)
265 dc
.DrawLine( i
,i
, i
+100,i
);
267 dc
.SetPen( *wxWHITE_PEN
);
268 for (i
= 200; i
< 220; i
+= 2)
269 dc
.DrawLine( i
-200,i
, i
-100,i
);
271 wxRegion
region( 110, 110, 80, 80 );
272 wxRegion
hole( 130, 130, 40, 1 );
273 region
.Intersect( hole
);
274 dc
.SetClippingRegion( region
);
276 dc
.SetBrush( *wxRED_BRUSH
);
277 dc
.DrawRectangle( 100, 100, 200, 200 );
279 dc
.DestroyClippingRegion();
281 dc
.SetPen( *wxTRANSPARENT_PEN
);
283 wxRegion
strip( 110, 200, 30, 1 );
284 wxRegionIterator
it( strip
);
287 dc
.DrawRectangle( it
.GetX(), it
.GetY(), it
.GetWidth(), it
.GetHeight() );
293 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
295 wxPaintDC
dcWin(this);
300 const wxSize size
= GetClientSize();
302 wxBitmap
bmp(size
.x
, size
.y
);
303 dc
.SelectObject(bmp
);
304 dc
.Blit(0, 0, size
.x
, size
.y
, &dcWin
, 0, 0);
305 dc
.DrawText(_T("(copy of background)"), 5, 120 );
309 dcWin
.Blit(0, 0, size
.x
, size
.y
, &dc
, 0, 0);
317 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
319 wxDC
& dc
= *event
.GetDC();
320 dc
.SetPen(*wxGREEN_PEN
);
324 // clear any junk currently displayed
327 const wxSize size
= GetClientSize();
328 for ( int x
= 0; x
< size
.x
; x
+= 15 )
330 dc
.DrawLine(x
, 0, x
, size
.y
);
333 for ( int y
= 0; y
< size
.y
; y
+= 15 )
335 dc
.DrawLine(0, y
, size
.x
, y
);
338 dc
.SetTextForeground(*wxRED
);
339 dc
.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160);