]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/canvas/canvas.cpp
3556034ad6ceda4f41d122d126cacd8f3c494f82
1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Robert Roebling
5 // Copyright: 2000 (c) Robert Roebling
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "canvas.cpp"
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/canvas/canvas.h"
24 #include <gdk/gdkrgb.h>
25 #include "wx/gtk/win_gtk.h"
28 // WDR: class implementations
30 //----------------------------------------------------------------------------
32 //----------------------------------------------------------------------------
34 wxCanvasObject::wxCanvasObject( int x
, int y
, int width
, int height
)
40 m_area
.height
= height
;
46 void wxCanvasObject::Move( int x
, int y
)
56 // TODO: sometimes faster to merge into 1 Update or
57 // to break up into four
58 m_owner
->Update( old_x
, old_y
, m_area
.width
, m_area
.height
);
59 m_owner
->Update( x
, y
, m_area
.width
, m_area
.height
);
63 void wxCanvasObject::WriteSVG( wxTextOutputStream
&stream
)
67 void wxCanvasObject::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
71 //----------------------------------------------------------------------------
73 //----------------------------------------------------------------------------
75 wxCanvasImage::wxCanvasImage( const wxImage
&image
, int x
, int y
)
76 : wxCanvasObject( x
, y
, image
.GetWidth(), image
.GetHeight() )
82 void wxCanvasImage::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
84 m_owner
->GetBuffer()->Paste( m_image
, m_area
.x
, m_area
.y
);
87 void wxCanvasImage::WriteSVG( wxTextOutputStream
&stream
)
92 //----------------------------------------------------------------------------
94 //----------------------------------------------------------------------------
96 wxCanvasControl::wxCanvasControl( wxWindow
*control
)
97 : wxCanvasObject( -1, -1, -1, -1 )
103 wxCanvasControl::~wxCanvasControl()
105 m_control
->Destroy();
108 void wxCanvasControl::Move( int x
, int y
)
110 m_control
->Move( x
, y
);
113 void wxCanvasControl::UpdateSize()
115 m_control
->GetSize( &m_area
.width
, &m_area
.height
);
116 m_control
->GetPosition( &m_area
.x
, &m_area
.y
);
119 //----------------------------------------------------------------------------
121 //----------------------------------------------------------------------------
123 IMPLEMENT_CLASS(wxCanvas
,wxScrolledWindow
)
125 BEGIN_EVENT_TABLE(wxCanvas
,wxScrolledWindow
)
126 EVT_CHAR( wxCanvas::OnChar
)
127 EVT_PAINT( wxCanvas::OnPaint
)
128 EVT_SIZE( wxCanvas::OnSize
)
129 EVT_IDLE( wxCanvas::OnIdle
)
130 EVT_MOUSE_EVENTS( wxCanvas::OnMouse
)
131 EVT_SET_FOCUS( wxCanvas::OnSetFocus
)
132 EVT_KILL_FOCUS( wxCanvas::OnKillFocus
)
135 wxCanvas::wxCanvas( wxWindow
*parent
, wxWindowID id
,
136 const wxPoint
&position
, const wxSize
& size
, long style
) :
137 wxScrolledWindow( parent
, id
, position
, size
, style
)
139 m_needUpdate
= FALSE
;
140 m_objects
.DeleteContents( TRUE
);
143 wxCanvas::~wxCanvas()
145 wxNode
*node
= m_updateRects
.First();
148 wxRect
*rect
= (wxRect
*) node
->Data();
150 m_updateRects
.DeleteNode( node
);
151 node
= m_updateRects
.First();
155 void wxCanvas::SetArea( int width
, int height
)
157 m_buffer
= wxImage( width
, height
);
158 SetScrollbars( 10, 10, width
/10, height
/10 );
161 void wxCanvas::Update( int x
, int y
, int width
, int height
)
165 m_updateRects
.Append(
166 (wxObject
*) new wxRect( x
,y
,width
,height
) );
168 // speed up with direct access
170 for (yy
= y
; yy
< y
+height
; yy
++)
171 for (xx
= x
; xx
< x
+width
; xx
++)
172 m_buffer
.SetRGB( xx
, yy
, 0, 0, 0 );
174 wxNode
*node
= m_objects
.First();
177 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
180 ww
= obj
->GetWidth();
181 hh
= obj
->GetHeight();
183 if (!obj
->IsControl())
185 obj
->Render( x
, y
, width
, height
);
192 void wxCanvas::BlitBuffer( wxDC
&dc
)
194 wxNode
*node
= m_updateRects
.First();
197 wxRect
*rect
= (wxRect
*) node
->Data();
198 wxImage
sub_image( m_buffer
.GetSubImage( *rect
) );
200 // DirectDraw here, please
203 int bpp
= wxDisplayDepth();
206 // the init code is doubled in wxImage
207 static bool s_hasInitialized
= FALSE
;
209 if (!s_hasInitialized
)
212 s_hasInitialized
= TRUE
;
217 CalcScrolledPosition( x
, y
, &x
, &y
);
219 gdk_draw_rgb_image( GTK_PIZZA(m_wxwindow
)->bin_window
,
220 m_wxwindow
->style
->black_gc
,
222 sub_image
.GetWidth(), sub_image
.GetHeight(),
225 sub_image
.GetWidth()*3 );
229 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
230 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
235 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
236 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
240 m_updateRects
.DeleteNode( node
);
241 node
= m_updateRects
.First();
244 m_needUpdate
= FALSE
;
247 void wxCanvas::UpdateNow()
249 if (!m_needUpdate
) return;
251 wxClientDC
dc( this );
257 void wxCanvas::Prepend( wxCanvasObject
* obj
)
259 m_objects
.Insert( obj
);
261 obj
->SetOwner( this );
263 if (!obj
->IsControl())
264 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
267 void wxCanvas::Append( wxCanvasObject
* obj
)
269 m_objects
.Append( obj
);
271 obj
->SetOwner( this );
273 if (!obj
->IsControl())
274 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
277 void wxCanvas::Insert( size_t before
, wxCanvasObject
* obj
)
279 m_objects
.Insert( before
, obj
);
281 obj
->SetOwner( this );
283 if (!obj
->IsControl())
284 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
287 void wxCanvas::Remove( wxCanvasObject
* obj
)
291 int w
= obj
->GetWidth();
292 int h
= obj
->GetHeight();
293 bool ic
= obj
->IsControl();
295 m_objects
.DeleteObject( obj
);
298 Update( x
, y
, w
, h
);
301 void wxCanvas::OnPaint(wxPaintEvent
&event
)
308 wxRegionIterator
it( GetUpdateRegion() );
313 CalcUnscrolledPosition( x
, y
, &x
, &y
);
315 int w
= it
.GetWidth();
316 int h
= it
.GetHeight();
317 if (x
+ w
> m_buffer
.GetWidth())
318 w
= m_buffer
.GetWidth()-x
;
319 if (y
+ h
> m_buffer
.GetHeight())
320 h
= m_buffer
.GetHeight()-y
;
322 m_updateRects
.Append( (wxObject
*) new wxRect( x
, y
, w
, h
) );
330 void wxCanvas::OnMouse(wxMouseEvent
&event
)
332 // Propagate to objects here
335 void wxCanvas::OnSize(wxSizeEvent
&event
)
340 void wxCanvas::OnIdle(wxIdleEvent
&event
)
346 void wxCanvas::OnSetFocus(wxFocusEvent
&event
)
350 void wxCanvas::OnKillFocus(wxFocusEvent
&event
)
354 void wxCanvas::OnChar(wxKeyEvent
&event
)