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 #ifndef wxUSE_FREETYPE
29 #define wxUSE_FREETYPE 1
33 #include <freetype/freetype.h>
36 //----------------------------------------------------------------------------
38 //----------------------------------------------------------------------------
41 FT_Library g_freetypeLibrary
;
44 //----------------------------------------------------------------------------
46 //----------------------------------------------------------------------------
48 wxCanvasObject::wxCanvasObject()
60 void wxCanvasObject::SetArea( int x
, int y
, int width
, int height
)
65 m_area
.height
= height
;
68 void wxCanvasObject::SetArea( wxRect rect
)
72 m_area
.width
= rect
.width
;
73 m_area
.height
= rect
.height
;
76 void wxCanvasObject::Move( int x
, int y
)
86 // TODO: sometimes faster to merge into 1 Update or
87 // to break up into four
88 m_owner
->Update( old_x
, old_y
, m_area
.width
, m_area
.height
);
89 m_owner
->Update( x
, y
, m_area
.width
, m_area
.height
);
93 bool wxCanvasObject::IsHit( int x
, int y
, int margin
)
95 return ((x
>= m_area
.x
-margin
) &&
96 (x
<= m_area
.x
+m_area
.width
+margin
) &&
97 (y
>= m_area
.y
-margin
) &&
98 (y
<= m_area
.y
+m_area
.height
+margin
));
101 void wxCanvasObject::CaptureMouse()
103 m_owner
->SetCaptureMouse( this );
106 void wxCanvasObject::ReleaseMouse()
108 m_owner
->SetCaptureMouse( NULL
);
111 bool wxCanvasObject::IsCapturedMouse()
113 return m_owner
->m_captureMouse
==this;
117 void wxCanvasObject::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
121 void wxCanvasObject::Recreate()
125 void wxCanvasObject::WriteSVG( wxTextOutputStream
&stream
)
129 //----------------------------------------------------------------------------
130 // wxCanvasObjectGroup
131 //----------------------------------------------------------------------------
133 wxCanvasObjectGroup::wxCanvasObjectGroup()
135 m_validbounds
= FALSE
;
138 wxCanvasObjectGroup::~wxCanvasObjectGroup()
142 void wxCanvasObjectGroup::SetOwner(wxCanvas
* canvas
)
145 wxNode
*node
= m_objects
.First();
148 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
150 obj
->SetOwner(canvas
);
156 void wxCanvasObjectGroup::ExtendArea(int x
, int y
)
160 if (x
< m_minx
) m_minx
= x
;
161 if (y
< m_miny
) m_miny
= y
;
162 if (x
> m_maxx
) m_maxx
= x
;
163 if (y
> m_maxy
) m_maxy
= y
;
167 m_validbounds
= TRUE
;
176 void wxCanvasObjectGroup::DeleteContents( bool flag
)
178 m_objects
.DeleteContents( flag
);
179 m_validbounds
= FALSE
;
182 void wxCanvasObjectGroup::Prepend( wxCanvasObject
* obj
)
184 m_objects
.Insert( obj
);
185 m_validbounds
= FALSE
;
188 void wxCanvasObjectGroup::Append( wxCanvasObject
* obj
)
190 m_objects
.Append( obj
);
191 m_validbounds
= FALSE
;
194 void wxCanvasObjectGroup::Insert( size_t before
, wxCanvasObject
* obj
)
196 m_objects
.Insert( before
, obj
);
197 m_validbounds
= FALSE
;
200 void wxCanvasObjectGroup::Remove( wxCanvasObject
* obj
)
202 m_objects
.DeleteObject( obj
);
203 m_validbounds
= FALSE
;
206 void wxCanvasObjectGroup::Recreate()
208 m_validbounds
= FALSE
;
209 wxNode
*node
= m_objects
.First();
212 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
215 ExtendArea(obj
->GetX(),obj
->GetY());
216 ExtendArea(obj
->GetX()+obj
->GetWidth(),obj
->GetY()+obj
->GetHeight());
222 void wxCanvasObjectGroup::Render(int xabs
, int yabs
, int x
, int y
, int width
, int height
)
224 // cycle through all objects
225 wxNode
*node
= m_objects
.First();
228 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
230 if (!obj
->IsControl())
232 // If we have 10.000 objects, we will go through
233 // this 10.000 times for each update, so we have
234 // to optimise carefully.
235 int clip_x
= xabs
+ obj
->GetX();
236 int clip_width
= obj
->GetWidth();
239 clip_width
-= x
-clip_x
;
244 if (clip_x
+ clip_width
> x
+ width
)
245 clip_width
= x
+width
-clip_x
;
249 int clip_y
= yabs
+ obj
->GetY();
250 int clip_height
= obj
->GetHeight();
253 clip_height
-= y
-clip_y
;
258 if (clip_y
+ clip_height
> y
+ height
)
259 clip_height
= y
+height
-clip_y
;
262 obj
->Render(xabs
,yabs
, clip_x
, clip_y
, clip_width
, clip_height
);
272 void wxCanvasObjectGroup::WriteSVG( wxTextOutputStream
&stream
)
276 bool wxCanvasObjectGroup::IsHit( int x
, int y
, int margin
)
278 wxNode
*node
= m_objects
.Last();
281 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
283 if (!obj
->IsControl())
285 if (obj
->IsHit(x
,y
,margin
))
290 node
= node
->Previous();
295 wxCanvasObject
* wxCanvasObjectGroup::IsHitObject( int x
, int y
, int margin
)
297 wxCanvasObject
*obj
=0;
298 wxNode
*node
= m_objects
.Last();
301 obj
=(wxCanvasObject
*) node
->Data();
303 if (!obj
->IsControl())
305 if (obj
->IsHit(x
,y
,margin
))
310 node
= node
->Previous();
313 return (wxCanvasObject
*) NULL
;
316 //----------------------------------------------------------------------------
317 // wxCanvasObjectGroupRef
318 //----------------------------------------------------------------------------
320 wxCanvasObjectGroupRef::wxCanvasObjectGroupRef(double x
, double y
, wxCanvasObjectGroup
* group
)
325 m_validbounds
= FALSE
;
329 void wxCanvasObjectGroupRef::SetOwner(wxCanvas
* canvas
)
332 m_group
->SetOwner(canvas
);
335 void wxCanvasObjectGroupRef::ExtendArea(int x
, int y
)
339 if (x
< m_minx
) m_minx
= x
;
340 if (y
< m_miny
) m_miny
= y
;
341 if (x
> m_maxx
) m_maxx
= x
;
342 if (y
> m_maxy
) m_maxy
= y
;
346 m_validbounds
= TRUE
;
355 void wxCanvasObjectGroupRef::Recreate()
357 m_validbounds
= FALSE
;
359 ExtendArea(m_group
->GetXMin(),m_group
->GetYMin());
360 ExtendArea(m_group
->GetXMax(),m_group
->GetYMax());
362 //set the area in pixels relative to the parent
363 SetArea( m_owner
->GetDeviceX( m_x
+ m_minx
),
364 m_owner
->GetDeviceY( m_y
+ m_miny
),
365 m_owner
->GetDeviceWidth( m_maxx
-m_minx
),
366 m_owner
->GetDeviceHeight( m_maxy
-m_miny
) );
369 void wxCanvasObjectGroupRef::Render(int xabs
, int yabs
, int x
, int y
, int width
, int height
)
371 xabs
+= m_owner
->GetDeviceX(GetPosX());
372 yabs
+= m_owner
->GetDeviceY(GetPosY());
374 int clip_x
= xabs
+ m_group
->GetXMin();
375 int clip_width
= m_group
->GetXMax()-m_group
->GetXMin();
378 clip_width
-= x
-clip_x
;
383 if (clip_x
+ clip_width
> x
+ width
)
384 clip_width
= x
+width
-clip_x
;
388 int clip_y
= yabs
+ m_group
->GetYMin();
389 int clip_height
= m_group
->GetYMax()-m_group
->GetYMin();
392 clip_height
-= y
-clip_y
;
397 if (clip_y
+ clip_height
> y
+ height
)
398 clip_height
= y
+height
-clip_y
;
401 m_group
->Render(xabs
,yabs
, clip_x
, clip_y
, clip_width
, clip_height
);
407 void wxCanvasObjectGroupRef::WriteSVG( wxTextOutputStream
&stream
)
411 bool wxCanvasObjectGroupRef::IsHit( int x
, int y
, int margin
)
413 return m_group
->IsHit(x
-GetPosX(),y
-GetPosY(),margin
);
416 wxCanvasObject
* wxCanvasObjectGroupRef::IsHitObject( int x
, int y
, int margin
)
418 return m_group
->IsHitObject(x
-GetPosX(),y
-GetPosY(),margin
);
421 void wxCanvasObjectGroupRef::Move( int x
, int y
)
428 // TODO: sometimes faster to merge into 1 Update or
429 // to break up into four
430 m_owner
->Update(m_area
.x
, m_area
.y
, m_area
.width
, m_area
.height
);
431 //calculate the new area in pixels relative to the parent
432 SetArea( m_owner
->GetDeviceX( m_x
+ m_minx
),
433 m_owner
->GetDeviceY( m_y
+ m_miny
),
434 m_owner
->GetDeviceWidth( m_maxx
-m_minx
),
435 m_owner
->GetDeviceHeight( m_maxy
-m_miny
) );
436 m_owner
->Update( m_area
.x
, m_area
.y
, m_area
.width
, m_area
.height
);
441 //----------------------------------------------------------------------------
443 //----------------------------------------------------------------------------
445 wxCanvasRect::wxCanvasRect( double x
, double y
, double w
, double h
,
446 unsigned char red
, unsigned char green
, unsigned char blue
)
459 void wxCanvasRect::Recreate()
461 SetArea( m_owner
->GetDeviceX( m_x
),
462 m_owner
->GetDeviceY( m_y
),
463 m_owner
->GetDeviceWidth( m_width
),
464 m_owner
->GetDeviceHeight( m_height
) );
467 void wxCanvasRect::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
469 int buffer_x
= m_owner
->GetBufferX();
470 int buffer_y
= m_owner
->GetBufferY();
473 wxImage
*image
= m_owner
->GetBuffer();
475 int start_y
= clip_y
- buffer_y
;
476 int end_y
= clip_y
+clip_height
- buffer_y
;
478 int start_x
= clip_x
- buffer_x
;
479 int end_x
= clip_x
+clip_width
- buffer_x
;
482 for (int y
= start_y
; y
< end_y
; y
++)
483 for (int x
= start_x
; x
< end_x
; x
++)
484 image
->SetRGB( x
, y
, m_red
, m_green
, m_blue
);
486 wxMemoryDC
*dc
= m_owner
->GetDC();
487 dc
->SetPen( *wxTRANSPARENT_PEN
);
488 wxBrush
brush( wxColour( m_red
,m_green
,m_blue
), wxSOLID
);
489 dc
->SetBrush( brush
);
491 dc
->DrawRectangle( clip_x
-buffer_x
, clip_y
-buffer_y
, clip_width
, clip_height
);
495 void wxCanvasRect::WriteSVG( wxTextOutputStream
&stream
)
499 //----------------------------------------------------------------------------
501 //----------------------------------------------------------------------------
503 wxCanvasLine::wxCanvasLine( double x1
, double y1
, double x2
, double y2
,
504 unsigned char red
, unsigned char green
, unsigned char blue
)
517 void wxCanvasLine::Recreate()
519 int x1
= m_owner
->GetDeviceX( m_x1
);
520 int y1
= m_owner
->GetDeviceY( m_y1
);
521 int x2
= m_owner
->GetDeviceX( m_x2
);
522 int y2
= m_owner
->GetDeviceY( m_y2
);
535 SetArea( x1
, y1
, x2
-x1
+1, y2
-y1
+1 );
538 void wxCanvasLine::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
540 int buffer_x
= m_owner
->GetBufferX();
541 int buffer_y
= m_owner
->GetBufferY();
543 int x1
= xabs
+ m_owner
->GetDeviceX( m_x1
);
544 int y1
= yabs
+ m_owner
->GetDeviceY( m_y1
);
545 int x2
= xabs
+ m_owner
->GetDeviceX( m_x2
);
546 int y2
= yabs
+ m_owner
->GetDeviceY( m_y2
);
549 wxImage
*image
= m_owner
->GetBuffer();
550 if ((m_area
.width
== 0) && (m_area
.height
== 0))
552 image
->SetRGB( m_area
.x
-buffer_x
, m_area
.y
-buffer_y
, m_red
, m_green
, m_blue
);
556 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
559 si
= (di
< 0)? -1 : 1;
562 sj
= (dj
< 0)? -1 : 1;
574 if ((ii
>= clip_x
) && (ii
< clip_x
+clip_width
) &&
575 (jj
>= clip_y
) && (jj
< clip_y
+clip_height
))
577 image
->SetRGB( ii
-buffer_x
, jj
-buffer_y
, m_red
, m_blue
, m_green
);
595 if ((ii
>= clip_x
) && (ii
< clip_x
+clip_width
) &&
596 (jj
>= clip_y
) && (jj
< clip_y
+clip_height
))
598 image
->SetRGB( ii
-buffer_x
, jj
-buffer_y
, m_red
, m_blue
, m_green
);
611 wxMemoryDC
*dc
= m_owner
->GetDC();
612 dc
->SetClippingRegion( clip_x
-buffer_x
, clip_y
-buffer_y
, clip_width
, clip_height
);
614 wxPen
pen( wxColour(m_red
,m_green
,m_blue
), 0, wxSOLID
);
616 dc
->DrawLine( x1
-buffer_x
, y1
-buffer_y
, x2
-buffer_x
, y2
-buffer_y
);
618 dc
->DestroyClippingRegion();
622 void wxCanvasLine::WriteSVG( wxTextOutputStream
&stream
)
627 //----------------------------------------------------------------------------
629 //----------------------------------------------------------------------------
631 wxCanvasImage::wxCanvasImage( const wxImage
&image
, double x
, double y
, double w
, double h
)
643 void wxCanvasImage::Recreate()
645 SetArea( m_owner
->GetDeviceX( m_x
),
646 m_owner
->GetDeviceY( m_y
),
647 m_owner
->GetDeviceWidth( m_width
),
648 m_owner
->GetDeviceHeight( m_height
) );
651 if ((m_area
.width
== m_image
.GetWidth()) &&
652 (m_area
.width
== m_image
.GetWidth()))
658 m_tmp
= m_image
.Scale( m_area
.width
, m_area
.height
);
661 if ((m_area
.width
== m_image
.GetWidth()) &&
662 (m_area
.width
== m_image
.GetWidth()))
664 m_tmp
= m_image
.ConvertToBitmap();
668 wxImage
tmp( m_image
.Scale( m_area
.width
, m_area
.height
) );
669 m_tmp
= tmp
.ConvertToBitmap();
674 void wxCanvasImage::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
676 int buffer_x
= m_owner
->GetBufferX();
677 int buffer_y
= m_owner
->GetBufferY();
680 if ((clip_x
== xabs
+ m_area
.x
) &&
681 (clip_y
== yabs
+ m_area
.y
) &&
682 (clip_width
== m_area
.width
) &&
683 (clip_height
== m_area
.height
))
685 m_owner
->GetBuffer()->Paste( m_tmp
, clip_x
-buffer_x
, clip_y
-buffer_y
);
690 int start_x
= clip_x
- (xabs
+ m_area
.x
);
691 int start_y
= clip_y
- (yabs
+ m_area
.y
);
693 wxRect
rect( start_x
, start_y
, clip_width
, clip_height
);
694 wxImage
sub_image( m_tmp
.GetSubImage( rect
) );
695 m_owner
->GetBuffer()->Paste( sub_image
, clip_x
-buffer_x
, clip_y
-buffer_y
);
698 wxMemoryDC
*dc
= m_owner
->GetDC();
700 if ((clip_x
== xabs
+ m_area
.x
) &&
701 (clip_y
== yabs
+ m_area
.y
) &&
702 (clip_width
== m_area
.width
) &&
703 (clip_height
== m_area
.height
))
705 dc
->DrawBitmap( m_tmp
, clip_x
-buffer_x
, clip_y
-buffer_y
, TRUE
);
710 int start_x
= clip_x
- (xabs
+ m_area
.x
);
711 int start_y
= clip_y
- (yabs
+ m_area
.y
);
713 // Clipping region faster ?
714 wxRect
rect( start_x
, start_y
, clip_width
, clip_height
);
715 wxBitmap
sub_bitmap( m_tmp
.GetSubBitmap( rect
) );
716 dc
->DrawBitmap( sub_bitmap
, clip_x
-buffer_x
, clip_y
-buffer_y
, TRUE
);
721 void wxCanvasImage::WriteSVG( wxTextOutputStream
&stream
)
726 //----------------------------------------------------------------------------
728 //----------------------------------------------------------------------------
730 wxCanvasControl::wxCanvasControl( wxWindow
*control
)
737 wxCanvasControl::~wxCanvasControl()
739 m_control
->Destroy();
742 void wxCanvasControl::Recreate()
744 m_control
->GetSize( &m_area
.width
, &m_area
.height
);
745 m_control
->GetPosition( &m_area
.x
, &m_area
.y
);
748 void wxCanvasControl::Move( int x
, int y
)
750 m_control
->Move( x
, y
);
753 //----------------------------------------------------------------------------
755 //----------------------------------------------------------------------------
767 wxCanvasText::wxCanvasText( const wxString
&text
, double x
, double y
, const wxString
&fontFile
, int size
)
771 m_fontFileName
= fontFile
;
784 wxFaceData
*data
= new wxFaceData
;
787 int error
= FT_New_Face( g_freetypeLibrary
,
792 error
= FT_Set_Char_Size( data
->m_face
,
800 wxCanvasText::~wxCanvasText()
803 wxFaceData
*data
= (wxFaceData
*) m_faceData
;
807 if (m_alpha
) delete [] m_alpha
;
810 void wxCanvasText::SetRGB( unsigned char red
, unsigned char green
, unsigned char blue
)
817 void wxCanvasText::SetFlag( int flag
)
822 void wxCanvasText::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
824 if (!m_alpha
) return;
827 wxImage
*image
= m_owner
->GetBuffer();
828 int buffer_x
= m_owner
->GetBufferX();
829 int buffer_y
= m_owner
->GetBufferY();
832 int start_x
= clip_x
- m_area
.x
;
833 int end_x
= clip_width
+ start_x
;
834 int start_y
= clip_y
- m_area
.y
;
835 int end_y
= clip_height
+ start_y
;
837 for (int y
= start_y
; y
< end_y
; y
++)
838 for (int x
= start_x
; x
< end_x
; x
++)
840 int alpha
= m_alpha
[y
*m_area
.width
+ x
];
843 int image_x
= m_area
.x
+x
- buffer_x
;
844 int image_y
= m_area
.y
+y
- buffer_y
;
847 image
->SetRGB( image_x
, image_y
, m_red
, m_green
, m_blue
);
850 int red1
= (m_red
* alpha
) / 255;
851 int green1
= (m_green
* alpha
) / 255;
852 int blue1
= (m_blue
* alpha
) / 255;
855 int red2
= image
->GetRed( image_x
, image_y
);
856 int green2
= image
->GetGreen( image_x
, image_y
);
857 int blue2
= image
->GetBlue( image_x
, image_y
);
858 red2
= (red2
* alpha
) / 255;
859 green2
= (green2
* alpha
) / 255;
860 blue2
= (blue2
* alpha
) / 255;
862 image
->SetRGB( image_x
, image_y
, red1
+red2
, green1
+green2
, blue1
+blue2
);
868 void wxCanvasText::WriteSVG( wxTextOutputStream
&stream
)
872 void wxCanvasText::Recreate()
874 if (m_alpha
) delete [] m_alpha
;
876 m_area
.x
= m_owner
->GetDeviceX( m_x
);
877 m_area
.y
= m_owner
->GetDeviceY( m_y
);
879 m_area
.width
= 100; // TODO, calculate length
880 m_area
.height
= m_size
;
881 m_alpha
= new unsigned char[100*m_size
];
882 memset( m_alpha
, 0, m_area
.width
*m_area
.height
);
885 FT_Face face
= ((wxFaceData
*)m_faceData
)->m_face
;
886 FT_GlyphSlot slot
= face
->glyph
;
890 for (int n
= 0; n
< (int)m_text
.Len(); n
++)
892 FT_UInt index
= FT_Get_Char_Index( face
, m_text
[n
] );
894 int error
= FT_Load_Glyph( face
, index
, FT_LOAD_DEFAULT
);
897 error
= FT_Render_Glyph( face
->glyph
, ft_render_mode_normal
);
900 FT_Bitmap
*bitmap
= &slot
->bitmap
;
901 unsigned char* buffer
= bitmap
->buffer
;
902 for (int y
= 0; y
< bitmap
->rows
; y
++)
903 for (int x
= 0; x
< bitmap
->width
; x
++)
905 unsigned char alpha
= buffer
[ y
*bitmap
->pitch
+ x
];
906 if (alpha
== 0) continue;
908 int xx
= pen_x
+ slot
->bitmap_left
+ x
;
909 int yy
= pen_y
- slot
->bitmap_top
+ y
;
910 m_alpha
[ yy
* m_area
.width
+ xx
] = alpha
;
913 pen_x
+= slot
->advance
.x
>> 6;
914 pen_y
+= slot
->advance
.y
>> 6;
919 //----------------------------------------------------------------------------
921 //----------------------------------------------------------------------------
923 IMPLEMENT_CLASS(wxCanvas
,wxScrolledWindow
)
925 BEGIN_EVENT_TABLE(wxCanvas
,wxScrolledWindow
)
926 EVT_CHAR( wxCanvas::OnChar
)
927 EVT_PAINT( wxCanvas::OnPaint
)
928 EVT_SIZE( wxCanvas::OnSize
)
929 EVT_IDLE( wxCanvas::OnIdle
)
930 EVT_MOUSE_EVENTS( wxCanvas::OnMouse
)
931 EVT_SET_FOCUS( wxCanvas::OnSetFocus
)
932 EVT_KILL_FOCUS( wxCanvas::OnKillFocus
)
933 EVT_ERASE_BACKGROUND( wxCanvas::OnEraseBackground
)
936 wxCanvas::wxCanvas( wxWindow
*parent
, wxWindowID id
,
937 const wxPoint
&position
, const wxSize
& size
, long style
) :
938 wxScrolledWindow( parent
, id
, position
, size
, style
)
942 m_needUpdate
= FALSE
;
946 m_lastMouse
= (wxCanvasObject
*)NULL
;
947 m_captureMouse
= (wxCanvasObject
*)NULL
;
950 //root group always at 0,0
951 m_root
= new wxCanvasObjectGroup();
952 m_root
->DeleteContents( TRUE
);
953 m_root
->SetOwner(this);
956 wxCanvas::~wxCanvas()
958 wxNode
*node
= m_updateRects
.First();
961 wxRect
*rect
= (wxRect
*) node
->Data();
963 m_updateRects
.DeleteNode( node
);
964 node
= m_updateRects
.First();
968 void wxCanvas::SetArea( int width
, int height
)
970 SetScrollbars( 10, 10, width
/10, height
/10 );
973 void wxCanvas::SetColour( unsigned char red
, unsigned char green
, unsigned char blue
)
979 SetBackgroundColour( wxColour( red
, green
, blue
) );
981 if (m_frozen
) return;
984 unsigned char *data
= m_buffer
.GetData();
986 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
987 for (int x
= 0; x
< m_buffer
.GetWidth(); x
++)
998 dc
.SelectObject( m_buffer
);
999 dc
.SetPen( *wxTRANSPARENT_PEN
);
1000 wxBrush
brush( wxColour( red
,green
,blue
), wxSOLID
);
1001 dc
.SetBrush( brush
);
1002 dc
.DrawRectangle( 0, 0, m_buffer
.GetWidth(), m_buffer
.GetHeight() );
1003 dc
.SelectObject( wxNullBitmap
);
1007 void wxCanvas::SetCaptureMouse( wxCanvasObject
*obj
)
1011 wxWindow::CaptureMouse();
1012 m_captureMouse
= obj
;
1016 wxWindow::ReleaseMouse();
1017 m_captureMouse
= NULL
;
1021 void wxCanvas::Freeze()
1026 void wxCanvas::Thaw()
1028 wxNode
*node
= m_updateRects
.First();
1031 wxRect
*rect
= (wxRect
*) node
->Data();
1033 m_updateRects
.DeleteNode( node
);
1034 node
= m_updateRects
.First();
1040 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), m_buffer
.GetHeight() );
1043 void wxCanvas::Update( int x
, int y
, int width
, int height
, bool blit
)
1045 if (m_frozen
) return;
1050 width
-= m_bufferX
-x
;
1053 if (width
<= 0) return;
1057 height
-= m_bufferY
-y
;
1060 if (height
<= 0) return;
1062 if (x
+width
> m_bufferX
+m_buffer
.GetWidth())
1064 width
= m_bufferX
+m_buffer
.GetWidth() - x
;
1066 if (width
<= 0) return;
1068 if (y
+height
> m_bufferY
+m_buffer
.GetHeight())
1070 height
= m_bufferY
+m_buffer
.GetHeight() - y
;
1072 if (height
<= 0) return;
1074 // update is within the buffer
1075 m_needUpdate
= TRUE
;
1077 // has to be blitted to screen later
1080 m_updateRects
.Append(
1081 (wxObject
*) new wxRect( x
,y
,width
,height
) );
1085 // speed up with direct access, maybe add wxImage::Clear(x,y,w,h,r,g,b)
1086 int start_y
= y
- m_bufferY
;
1087 int end_y
= y
+height
- m_bufferY
;
1088 int start_x
= x
- m_bufferX
;
1089 int end_x
= x
+width
- m_bufferX
;
1090 for (int yy
= start_y
; yy
< end_y
; yy
++)
1091 for (int xx
= start_x
; xx
< end_x
; xx
++)
1092 m_buffer
.SetRGB( xx
, yy
, m_red
, m_green
, m_blue
);
1094 m_root
->Render(0,0, x
, y
, width
, height
);
1097 dc
.SelectObject( m_buffer
);
1098 dc
.SetPen( *wxTRANSPARENT_PEN
);
1099 wxBrush
brush( wxColour( m_red
,m_green
,m_blue
), wxSOLID
);
1100 dc
.SetBrush( brush
);
1101 dc
.DrawRectangle( x
-m_bufferX
, y
-m_bufferY
, width
, height
);
1104 m_root
->Render(0,0, x
, y
, width
, height
);
1106 dc
.SelectObject( wxNullBitmap
);
1110 void wxCanvas::BlitBuffer( wxDC
&dc
)
1112 wxNode
*node
= m_updateRects
.First();
1115 wxRect
*rect
= (wxRect
*) node
->Data();
1117 wxRect
sub_rect( *rect
);
1118 sub_rect
.x
-= m_bufferX
;
1119 sub_rect
.y
-= m_bufferY
;
1123 wxImage
sub_image( m_buffer
.GetSubImage( sub_rect
) );
1125 int bpp
= wxDisplayDepth();
1128 // the init code is doubled in wxImage
1129 static bool s_hasInitialized
= FALSE
;
1131 if (!s_hasInitialized
)
1134 s_hasInitialized
= TRUE
;
1137 gdk_draw_rgb_image( GTK_PIZZA(m_wxwindow
)->bin_window
,
1138 m_wxwindow
->style
->black_gc
,
1139 sub_rect
.x
, sub_rect
.y
,
1140 sub_image
.GetWidth(), sub_image
.GetHeight(),
1141 GDK_RGB_DITHER_NONE
,
1142 sub_image
.GetData(),
1143 sub_image
.GetWidth()*3 );
1147 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
1148 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
1151 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
1152 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
1155 #else // IMAGE_CANVAS
1157 // Maybe clipping use SetClipping() is faster than
1158 // getting the subrect first and drawing it then?
1159 wxBitmap
sub_bitmap( m_buffer
.GetSubBitmap( sub_rect
) );
1160 dc
.DrawBitmap( sub_bitmap
, rect
->x
, rect
->y
);
1164 m_updateRects
.DeleteNode( node
);
1165 node
= m_updateRects
.First();
1168 m_needUpdate
= FALSE
;
1171 void wxCanvas::UpdateNow()
1173 if (m_frozen
) return;
1175 if (!m_needUpdate
) return;
1177 wxClientDC
dc( this );
1183 int wxCanvas::GetDeviceX( double x
)
1188 int wxCanvas::GetDeviceY( double y
)
1193 int wxCanvas::GetDeviceWidth( double width
)
1198 int wxCanvas::GetDeviceHeight( double height
)
1200 return (int) height
;
1203 void wxCanvas::Recreate()
1208 void wxCanvas::Prepend( wxCanvasObject
* obj
)
1210 m_root
->Prepend( obj
);
1211 obj
->SetOwner(this);
1215 if (!obj
->IsControl())
1216 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
1219 void wxCanvas::Append( wxCanvasObject
* obj
)
1221 m_root
->Append( obj
);
1222 obj
->SetOwner(this);
1226 if (!obj
->IsControl())
1227 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
1230 void wxCanvas::Insert( size_t before
, wxCanvasObject
* obj
)
1232 m_root
->Insert( before
, obj
);
1233 obj
->SetOwner(this);
1237 if (!obj
->IsControl())
1238 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
1241 void wxCanvas::Remove( wxCanvasObject
* obj
)
1243 int x
= obj
->GetX();
1244 int y
= obj
->GetY();
1245 int w
= obj
->GetWidth();
1246 int h
= obj
->GetHeight();
1247 bool ic
= obj
->IsControl();
1249 m_root
->Remove( obj
);
1252 Update( x
, y
, w
, h
);
1255 void wxCanvas::OnPaint(wxPaintEvent
&event
)
1260 if (!m_buffer
.Ok()) return;
1262 if (m_frozen
) return;
1264 m_needUpdate
= TRUE
;
1266 wxRegionIterator
it( GetUpdateRegion() );
1272 int w
= it
.GetWidth();
1273 int h
= it
.GetHeight();
1275 if (x
+w
> m_buffer
.GetWidth())
1276 w
= m_buffer
.GetWidth() - x
;
1277 if (y
+h
> m_buffer
.GetHeight())
1278 h
= m_buffer
.GetHeight() - y
;
1280 if ((w
> 0) && (h
> 0))
1282 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1283 m_updateRects
.Append( (wxObject
*) new wxRect( x
, y
, w
, h
) );
1292 void wxCanvas::ScrollWindow( int dx
, int dy
, const wxRect
* rect
)
1294 // If any updates are pending, do them now since they will
1295 // expect the previous m_bufferX and m_bufferY values.
1298 // The buffer always starts at the top left corner of the
1299 // client area. Indeed, it is the client area.
1300 CalcUnscrolledPosition( 0, 0, &m_bufferX
, &m_bufferY
);
1303 unsigned char* data
= m_buffer
.GetData();
1309 unsigned char *source
= data
;
1310 unsigned char *dest
= data
+ (dy
* m_buffer
.GetWidth() * 3);
1311 size_t count
= (size_t) (m_buffer
.GetWidth() * 3 * (m_buffer
.GetHeight()-dy
));
1312 memmove( dest
, source
, count
);
1314 // We update the new buffer area, but there is no need to
1315 // blit (last param FALSE) since the ensuing paint event will
1317 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), dy
, FALSE
);
1321 unsigned char *dest
= data
;
1322 unsigned char *source
= data
+ (-dy
* m_buffer
.GetWidth() * 3);
1323 size_t count
= (size_t) (m_buffer
.GetWidth() * 3 * (m_buffer
.GetHeight()+dy
));
1324 memmove( dest
, source
, count
);
1326 // We update the new buffer area, but there is no need to
1327 // blit (last param FALSE) since the ensuing paint event will
1329 Update( m_bufferX
, m_bufferY
+m_buffer
.GetHeight()+dy
, m_buffer
.GetWidth(), -dy
, FALSE
);
1337 unsigned char *source
= data
;
1338 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
1340 unsigned char *dest
= source
+ dx
*3;
1341 memmove( dest
, source
, (m_buffer
.GetWidth()-dx
) * 3 );
1342 source
+= m_buffer
.GetWidth()*3;
1345 // We update the new buffer area, but there is no need to
1346 // blit (last param FALSE) since the ensuing paint event will
1348 Update( m_bufferX
, m_bufferY
, dx
, m_buffer
.GetHeight(), FALSE
);
1352 unsigned char *dest
= data
;
1353 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
1355 unsigned char *source
= dest
- dx
*3;
1356 memmove( dest
, source
, (m_buffer
.GetWidth()+dx
) * 3 );
1357 dest
+= m_buffer
.GetWidth()*3;
1360 // We update the new buffer area, but there is no need to
1361 // blit (last param FALSE) since the ensuing paint event will
1363 Update( m_bufferX
+m_buffer
.GetWidth()+dx
, m_bufferY
, -dx
, m_buffer
.GetHeight(), FALSE
);
1367 // Update everything, TODO: scrolling
1368 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), m_buffer
.GetHeight(), FALSE
);
1371 wxWindow::ScrollWindow( dx
, dy
, rect
);
1374 void wxCanvas::OnMouse(wxMouseEvent
&event
)
1376 int x
= event
.GetX();
1377 int y
= event
.GetY();
1378 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1380 if (event
.GetEventType() == wxEVT_MOTION
)
1382 if (m_captureMouse
) //no matter what go to this one
1384 wxMouseEvent
child_event( wxEVT_MOTION
);
1385 child_event
.SetEventObject(m_captureMouse
);
1386 child_event
.m_x
= x
- m_captureMouse
->GetX();
1387 child_event
.m_y
= y
- m_captureMouse
->GetY();
1388 child_event
.m_leftDown
= event
.m_leftDown
;
1389 child_event
.m_rightDown
= event
.m_rightDown
;
1390 child_event
.m_middleDown
= event
.m_middleDown
;
1391 child_event
.m_controlDown
= event
.m_controlDown
;
1392 child_event
.m_shiftDown
= event
.m_shiftDown
;
1393 child_event
.m_altDown
= event
.m_altDown
;
1394 child_event
.m_metaDown
= event
.m_metaDown
;
1396 m_captureMouse
->ProcessEvent( child_event
);
1401 wxCanvasObject
*obj
= m_root
->IsHitObject(x
,y
,0);
1403 if (obj
&& !obj
->IsControl())
1405 wxMouseEvent
child_event( wxEVT_MOTION
);
1406 child_event
.SetEventObject( obj
);
1407 child_event
.m_x
= x
- obj
->GetX();
1408 child_event
.m_y
= y
- obj
->GetY();
1409 child_event
.m_leftDown
= event
.m_leftDown
;
1410 child_event
.m_rightDown
= event
.m_rightDown
;
1411 child_event
.m_middleDown
= event
.m_middleDown
;
1412 child_event
.m_controlDown
= event
.m_controlDown
;
1413 child_event
.m_shiftDown
= event
.m_shiftDown
;
1414 child_event
.m_altDown
= event
.m_altDown
;
1415 child_event
.m_metaDown
= event
.m_metaDown
;
1417 if ((obj
!= m_lastMouse
) && (m_lastMouse
!= NULL
))
1419 child_event
.SetEventType( wxEVT_LEAVE_WINDOW
);
1420 child_event
.SetEventObject( m_lastMouse
);
1421 child_event
.m_x
= x
- m_lastMouse
->GetX();
1422 child_event
.m_y
= y
- m_lastMouse
->GetY();
1423 m_lastMouse
->ProcessEvent( child_event
);
1426 child_event
.SetEventType( wxEVT_ENTER_WINDOW
);
1427 child_event
.SetEventObject( m_lastMouse
);
1428 child_event
.m_x
= x
- m_lastMouse
->GetX();
1429 child_event
.m_y
= y
- m_lastMouse
->GetY();
1430 m_lastMouse
->ProcessEvent( child_event
);
1432 child_event
.SetEventType( wxEVT_MOTION
);
1433 child_event
.SetEventObject( obj
);
1436 obj
->ProcessEvent( child_event
);
1442 wxMouseEvent
child_event( wxEVT_LEAVE_WINDOW
);
1443 child_event
.SetEventObject( m_lastMouse
);
1444 child_event
.m_x
= x
- m_lastMouse
->GetX();
1445 child_event
.m_y
= y
- m_lastMouse
->GetY();
1446 child_event
.m_leftDown
= event
.m_leftDown
;
1447 child_event
.m_rightDown
= event
.m_rightDown
;
1448 child_event
.m_middleDown
= event
.m_middleDown
;
1449 child_event
.m_controlDown
= event
.m_controlDown
;
1450 child_event
.m_shiftDown
= event
.m_shiftDown
;
1451 child_event
.m_altDown
= event
.m_altDown
;
1452 child_event
.m_metaDown
= event
.m_metaDown
;
1453 m_lastMouse
->ProcessEvent( child_event
);
1455 m_lastMouse
= (wxCanvasObject
*) NULL
;
1461 if (m_captureMouse
) //no matter what go to this one
1463 wxMouseEvent
child_event( event
.GetEventType() );
1464 child_event
.SetEventObject(m_captureMouse
);
1465 child_event
.m_x
= x
- m_captureMouse
->GetX();
1466 child_event
.m_y
= y
- m_captureMouse
->GetY();
1467 child_event
.m_leftDown
= event
.m_leftDown
;
1468 child_event
.m_rightDown
= event
.m_rightDown
;
1469 child_event
.m_middleDown
= event
.m_middleDown
;
1470 child_event
.m_controlDown
= event
.m_controlDown
;
1471 child_event
.m_shiftDown
= event
.m_shiftDown
;
1472 child_event
.m_altDown
= event
.m_altDown
;
1473 child_event
.m_metaDown
= event
.m_metaDown
;
1474 m_captureMouse
->ProcessEvent( child_event
);
1478 wxCanvasObject
*obj
= m_root
->IsHitObject(x
,y
,0);
1480 if (obj
&& !obj
->IsControl())
1482 wxMouseEvent
child_event( event
.GetEventType() );
1483 child_event
.SetEventObject( obj
);
1484 child_event
.m_x
= x
- obj
->GetX();
1485 child_event
.m_y
= y
- obj
->GetY();
1486 child_event
.m_leftDown
= event
.m_leftDown
;
1487 child_event
.m_rightDown
= event
.m_rightDown
;
1488 child_event
.m_middleDown
= event
.m_middleDown
;
1489 child_event
.m_controlDown
= event
.m_controlDown
;
1490 child_event
.m_shiftDown
= event
.m_shiftDown
;
1491 child_event
.m_altDown
= event
.m_altDown
;
1492 child_event
.m_metaDown
= event
.m_metaDown
;
1494 obj
->ProcessEvent( child_event
);
1503 void wxCanvas::OnSize(wxSizeEvent
&event
)
1506 GetClientSize( &w
, &h
);
1508 m_buffer
= wxImage( w
, h
);
1510 m_buffer
= wxBitmap( w
, h
);
1513 CalcUnscrolledPosition( 0, 0, &m_bufferX
, &m_bufferY
);
1515 wxNode
*node
= m_updateRects
.First();
1518 wxRect
*rect
= (wxRect
*) node
->Data();
1520 m_updateRects
.DeleteNode( node
);
1521 node
= m_updateRects
.First();
1526 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), m_buffer
.GetHeight(), FALSE
);
1531 void wxCanvas::OnIdle(wxIdleEvent
&event
)
1537 void wxCanvas::OnSetFocus(wxFocusEvent
&event
)
1541 void wxCanvas::OnKillFocus(wxFocusEvent
&event
)
1545 void wxCanvas::OnChar(wxKeyEvent
&event
)
1550 void wxCanvas::OnEraseBackground(wxEraseEvent
&event
)
1554 //--------------------------------------------------------------------
1556 //--------------------------------------------------------------------
1558 class wxCanvasModule
: public wxModule
1561 virtual bool OnInit();
1562 virtual void OnExit();
1565 DECLARE_DYNAMIC_CLASS(wxCanvasModule
)
1568 IMPLEMENT_DYNAMIC_CLASS(wxCanvasModule
, wxModule
)
1570 bool wxCanvasModule::OnInit()
1573 int error
= FT_Init_FreeType( &g_freetypeLibrary
);
1574 if (error
) return FALSE
;
1580 void wxCanvasModule::OnExit()
1583 FT_Done_FreeType( g_freetypeLibrary
);