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(double x
, double 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(double x
, double 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(m_x
);
372 yabs
+= m_owner
->GetDeviceY(m_y
);
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
)
426 int old_area_x
= m_area
.x
;
427 int old_area_y
= m_area
.y
;
429 m_area
.x
=m_owner
->GetDeviceX( m_x
+ m_minx
);
430 m_area
.y
=m_owner
->GetDeviceY( m_y
+ m_miny
);
432 int leftu
,rightu
,bottomu
,topu
;
433 leftu
= wxMin (m_area
.x
, old_area_x
) ;
434 rightu
= wxMax (old_area_x
+ m_area
.width
, m_area
.x
+ m_area
.width
) ;
435 topu
= wxMin (m_area
.y
,old_area_y
) ;
436 bottomu
= wxMax (old_area_y
+ m_area
.height
, m_area
.y
+ m_area
.height
) ;
438 if ( rightu
- leftu
< 2*m_area
.width
&& bottomu
- topu
< 2*m_area
.height
)
440 m_owner
->Update(leftu
,topu
,rightu
- leftu
,bottomu
- topu
);
444 m_owner
->Update(old_area_x
, old_area_y
, m_area
.width
, m_area
.height
);
445 m_owner
->Update( m_area
.x
, m_area
.y
, m_area
.width
, m_area
.height
);
449 //----------------------------------------------------------------------------
451 //----------------------------------------------------------------------------
453 wxCanvasPolyline::wxCanvasPolyline( int n
, wxPoint2DDouble points
[])
458 m_pen
= wxPen(wxColour(0,0,0),1,wxSOLID
);
461 wxCanvasPolyline::~wxCanvasPolyline()
466 void wxCanvasPolyline::ExtendArea(double x
, double y
)
470 if (x
< m_minx
) m_minx
= x
;
471 if (y
< m_miny
) m_miny
= y
;
472 if (x
> m_maxx
) m_maxx
= x
;
473 if (y
> m_maxy
) m_maxy
= y
;
477 m_validbounds
= TRUE
;
486 void wxCanvasPolyline::Recreate()
491 for (i
=0; i
< m_n
;i
++)
493 ExtendArea(m_points
[i
].m_x
,m_points
[i
].m_y
);
496 //include the pen width also
497 ExtendArea(m_minx
-m_pen
.GetWidth(),m_miny
-m_pen
.GetWidth());
498 ExtendArea(m_maxx
+m_pen
.GetWidth()*2,m_maxy
+m_pen
.GetWidth()*2);
500 //set the area in pixels relative to the parent
501 SetArea( m_owner
->GetDeviceX(m_minx
),
502 m_owner
->GetDeviceY(m_miny
),
503 m_owner
->GetDeviceWidth( m_maxx
-m_minx
),
504 m_owner
->GetDeviceHeight( m_maxy
-m_miny
) );
507 void wxCanvasPolyline::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
509 int buffer_x
= m_owner
->GetBufferX();
510 int buffer_y
= m_owner
->GetBufferY();
512 int start_y
= clip_y
- buffer_y
;
513 int end_y
= clip_y
+clip_height
- buffer_y
;
515 int start_x
= clip_x
- buffer_x
;
516 int end_x
= clip_x
+clip_width
- buffer_x
;
520 wxPoint
*cpoints
= new wxPoint
[m_n
];
522 for (i
= 0; i
< m_n
; i
++)
524 cpoints
[i
].x
= m_owner
->GetDeviceX(m_points
[i
].m_x
+xabs
);
525 cpoints
[i
].y
= m_owner
->GetDeviceY(m_points
[i
].m_y
+yabs
);
527 wxMemoryDC
*dc
= m_owner
->GetDC();
528 dc
->SetClippingRegion(start_x
,start_y
,end_x
-start_x
,end_y
-start_y
);
530 dc
->DrawLines(m_n
, cpoints
, 0,0);
532 dc
->SetPen(wxNullPen
);
533 dc
->DestroyClippingRegion();
537 void wxCanvasPolyline::WriteSVG( wxTextOutputStream
&stream
)
541 //----------------------------------------------------------------------------
543 //----------------------------------------------------------------------------
545 wxCanvasPolygon::wxCanvasPolygon( int n
, wxPoint2DDouble points
[])
550 m_brush
= wxBrush(wxColour(0,0,0),wxSOLID
);
551 m_pen
= wxPen(wxColour(0,0,0),1,wxSOLID
);
554 wxCanvasPolygon::~wxCanvasPolygon()
559 void wxCanvasPolygon::ExtendArea(double x
, double y
)
563 if (x
< m_minx
) m_minx
= x
;
564 if (y
< m_miny
) m_miny
= y
;
565 if (x
> m_maxx
) m_maxx
= x
;
566 if (y
> m_maxy
) m_maxy
= y
;
570 m_validbounds
= TRUE
;
579 void wxCanvasPolygon::Recreate()
584 for (i
=0; i
< m_n
;i
++)
586 ExtendArea(m_points
[i
].m_x
,m_points
[i
].m_y
);
589 //include the pen width also
590 ExtendArea(m_minx
-m_pen
.GetWidth(),m_miny
-m_pen
.GetWidth());
591 ExtendArea(m_maxx
+m_pen
.GetWidth()*2,m_maxy
+m_pen
.GetWidth()*2);
593 //set the area in pixels relative to the parent
594 SetArea( m_owner
->GetDeviceX( m_minx
),
595 m_owner
->GetDeviceY( m_miny
),
596 m_owner
->GetDeviceWidth( m_maxx
-m_minx
),
597 m_owner
->GetDeviceHeight( m_maxy
-m_miny
) );
600 void wxCanvasPolygon::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
602 int buffer_x
= m_owner
->GetBufferX();
603 int buffer_y
= m_owner
->GetBufferY();
605 int start_y
= clip_y
- buffer_y
;
606 int end_y
= clip_y
+clip_height
- buffer_y
;
608 int start_x
= clip_x
- buffer_x
;
609 int end_x
= clip_x
+clip_width
- buffer_x
;
613 wxPoint
*cpoints
= new wxPoint
[m_n
];
615 for (i
= 0; i
< m_n
; i
++)
617 cpoints
[i
].x
= m_owner
->GetDeviceX(m_points
[i
].m_x
+xabs
);
618 cpoints
[i
].y
= m_owner
->GetDeviceY(m_points
[i
].m_y
+yabs
);
620 wxMemoryDC
*dc
= m_owner
->GetDC();
621 dc
->SetClippingRegion(start_x
,start_y
,end_x
-start_x
,end_y
-start_y
);
622 dc
->SetBrush(m_brush
);
624 dc
->DrawPolygon(m_n
, cpoints
, 0,0,wxWINDING_RULE
);
626 dc
->SetBrush(wxNullBrush
);
627 dc
->SetPen(wxNullPen
);
628 dc
->DestroyClippingRegion();
632 void wxCanvasPolygon::WriteSVG( wxTextOutputStream
&stream
)
638 //----------------------------------------------------------------------------
640 //----------------------------------------------------------------------------
642 wxCanvasRect::wxCanvasRect( double x
, double y
, double w
, double h
,
643 unsigned char red
, unsigned char green
, unsigned char blue
)
656 void wxCanvasRect::Recreate()
658 SetArea( m_owner
->GetDeviceX( m_x
),
659 m_owner
->GetDeviceY( m_y
),
660 m_owner
->GetDeviceWidth( m_width
),
661 m_owner
->GetDeviceHeight( m_height
) );
664 void wxCanvasRect::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
666 int buffer_x
= m_owner
->GetBufferX();
667 int buffer_y
= m_owner
->GetBufferY();
670 wxImage
*image
= m_owner
->GetBuffer();
672 int start_y
= clip_y
- buffer_y
;
673 int end_y
= clip_y
+clip_height
- buffer_y
;
675 int start_x
= clip_x
- buffer_x
;
676 int end_x
= clip_x
+clip_width
- buffer_x
;
679 for (int y
= start_y
; y
< end_y
; y
++)
680 for (int x
= start_x
; x
< end_x
; x
++)
681 image
->SetRGB( x
, y
, m_red
, m_green
, m_blue
);
683 wxMemoryDC
*dc
= m_owner
->GetDC();
684 dc
->SetPen( *wxTRANSPARENT_PEN
);
685 wxBrush
brush( wxColour( m_red
,m_green
,m_blue
), wxSOLID
);
686 dc
->SetBrush( brush
);
688 dc
->DrawRectangle( clip_x
-buffer_x
, clip_y
-buffer_y
, clip_width
, clip_height
);
692 void wxCanvasRect::WriteSVG( wxTextOutputStream
&stream
)
696 //----------------------------------------------------------------------------
698 //----------------------------------------------------------------------------
700 wxCanvasLine::wxCanvasLine( double x1
, double y1
, double x2
, double y2
,
701 unsigned char red
, unsigned char green
, unsigned char blue
)
714 void wxCanvasLine::Recreate()
716 int x1
= m_owner
->GetDeviceX( m_x1
);
717 int y1
= m_owner
->GetDeviceY( m_y1
);
718 int x2
= m_owner
->GetDeviceX( m_x2
);
719 int y2
= m_owner
->GetDeviceY( m_y2
);
732 SetArea( x1
, y1
, x2
-x1
+1, y2
-y1
+1 );
735 void wxCanvasLine::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
737 int buffer_x
= m_owner
->GetBufferX();
738 int buffer_y
= m_owner
->GetBufferY();
740 int x1
= xabs
+ m_owner
->GetDeviceX( m_x1
);
741 int y1
= yabs
+ m_owner
->GetDeviceY( m_y1
);
742 int x2
= xabs
+ m_owner
->GetDeviceX( m_x2
);
743 int y2
= yabs
+ m_owner
->GetDeviceY( m_y2
);
746 wxImage
*image
= m_owner
->GetBuffer();
747 if ((m_area
.width
== 0) && (m_area
.height
== 0))
749 image
->SetRGB( m_area
.x
-buffer_x
, m_area
.y
-buffer_y
, m_red
, m_green
, m_blue
);
753 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
756 si
= (di
< 0)? -1 : 1;
759 sj
= (dj
< 0)? -1 : 1;
771 if ((ii
>= clip_x
) && (ii
< clip_x
+clip_width
) &&
772 (jj
>= clip_y
) && (jj
< clip_y
+clip_height
))
774 image
->SetRGB( ii
-buffer_x
, jj
-buffer_y
, m_red
, m_blue
, m_green
);
792 if ((ii
>= clip_x
) && (ii
< clip_x
+clip_width
) &&
793 (jj
>= clip_y
) && (jj
< clip_y
+clip_height
))
795 image
->SetRGB( ii
-buffer_x
, jj
-buffer_y
, m_red
, m_blue
, m_green
);
808 wxMemoryDC
*dc
= m_owner
->GetDC();
809 dc
->SetClippingRegion( clip_x
-buffer_x
, clip_y
-buffer_y
, clip_width
, clip_height
);
811 wxPen
pen( wxColour(m_red
,m_green
,m_blue
), 0, wxSOLID
);
813 dc
->DrawLine( x1
-buffer_x
, y1
-buffer_y
, x2
-buffer_x
, y2
-buffer_y
);
815 dc
->DestroyClippingRegion();
819 void wxCanvasLine::WriteSVG( wxTextOutputStream
&stream
)
824 //----------------------------------------------------------------------------
826 //----------------------------------------------------------------------------
828 wxCanvasImage::wxCanvasImage( const wxImage
&image
, double x
, double y
, double w
, double h
)
840 void wxCanvasImage::Recreate()
842 SetArea( m_owner
->GetDeviceX( m_x
),
843 m_owner
->GetDeviceY( m_y
),
844 m_owner
->GetDeviceWidth( m_width
),
845 m_owner
->GetDeviceHeight( m_height
) );
848 if ((m_area
.width
== m_image
.GetWidth()) &&
849 (m_area
.width
== m_image
.GetWidth()))
855 m_tmp
= m_image
.Scale( m_area
.width
, m_area
.height
);
858 if ((m_area
.width
== m_image
.GetWidth()) &&
859 (m_area
.width
== m_image
.GetWidth()))
861 m_tmp
= m_image
.ConvertToBitmap();
865 wxImage
tmp( m_image
.Scale( m_area
.width
, m_area
.height
) );
866 m_tmp
= tmp
.ConvertToBitmap();
871 void wxCanvasImage::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
873 int buffer_x
= m_owner
->GetBufferX();
874 int buffer_y
= m_owner
->GetBufferY();
877 if ((clip_x
== xabs
+ m_area
.x
) &&
878 (clip_y
== yabs
+ m_area
.y
) &&
879 (clip_width
== m_area
.width
) &&
880 (clip_height
== m_area
.height
))
882 m_owner
->GetBuffer()->Paste( m_tmp
, clip_x
-buffer_x
, clip_y
-buffer_y
);
887 int start_x
= clip_x
- (xabs
+ m_area
.x
);
888 int start_y
= clip_y
- (yabs
+ m_area
.y
);
890 wxRect
rect( start_x
, start_y
, clip_width
, clip_height
);
891 wxImage
sub_image( m_tmp
.GetSubImage( rect
) );
892 m_owner
->GetBuffer()->Paste( sub_image
, clip_x
-buffer_x
, clip_y
-buffer_y
);
895 wxMemoryDC
*dc
= m_owner
->GetDC();
897 if ((clip_x
== xabs
+ m_area
.x
) &&
898 (clip_y
== yabs
+ m_area
.y
) &&
899 (clip_width
== m_area
.width
) &&
900 (clip_height
== m_area
.height
))
902 dc
->DrawBitmap( m_tmp
, clip_x
-buffer_x
, clip_y
-buffer_y
, TRUE
);
907 int start_x
= clip_x
- (xabs
+ m_area
.x
);
908 int start_y
= clip_y
- (yabs
+ m_area
.y
);
910 // Clipping region faster ?
911 wxRect
rect( start_x
, start_y
, clip_width
, clip_height
);
912 wxBitmap
sub_bitmap( m_tmp
.GetSubBitmap( rect
) );
913 dc
->DrawBitmap( sub_bitmap
, clip_x
-buffer_x
, clip_y
-buffer_y
, TRUE
);
918 void wxCanvasImage::WriteSVG( wxTextOutputStream
&stream
)
923 //----------------------------------------------------------------------------
925 //----------------------------------------------------------------------------
927 wxCanvasControl::wxCanvasControl( wxWindow
*control
)
934 wxCanvasControl::~wxCanvasControl()
936 m_control
->Destroy();
939 void wxCanvasControl::Recreate()
941 m_control
->GetSize( &m_area
.width
, &m_area
.height
);
942 m_control
->GetPosition( &m_area
.x
, &m_area
.y
);
945 void wxCanvasControl::Move( int x
, int y
)
947 m_control
->Move( x
, y
);
950 //----------------------------------------------------------------------------
952 //----------------------------------------------------------------------------
964 wxCanvasText::wxCanvasText( const wxString
&text
, double x
, double y
, const wxString
&fontFile
, int size
)
968 m_fontFileName
= fontFile
;
981 wxFaceData
*data
= new wxFaceData
;
984 int error
= FT_New_Face( g_freetypeLibrary
,
989 error
= FT_Set_Char_Size( data
->m_face
,
997 wxCanvasText::~wxCanvasText()
1000 wxFaceData
*data
= (wxFaceData
*) m_faceData
;
1004 if (m_alpha
) delete [] m_alpha
;
1007 void wxCanvasText::SetRGB( unsigned char red
, unsigned char green
, unsigned char blue
)
1014 void wxCanvasText::SetFlag( int flag
)
1019 void wxCanvasText::Render(int xabs
, int yabs
, int clip_x
, int clip_y
, int clip_width
, int clip_height
)
1021 if (!m_alpha
) return;
1024 wxImage
*image
= m_owner
->GetBuffer();
1025 int buffer_x
= m_owner
->GetBufferX();
1026 int buffer_y
= m_owner
->GetBufferY();
1028 // local coordinates
1029 int start_x
= clip_x
- m_area
.x
;
1030 int end_x
= clip_width
+ start_x
;
1031 int start_y
= clip_y
- m_area
.y
;
1032 int end_y
= clip_height
+ start_y
;
1034 for (int y
= start_y
; y
< end_y
; y
++)
1035 for (int x
= start_x
; x
< end_x
; x
++)
1037 int alpha
= m_alpha
[y
*m_area
.width
+ x
];
1040 int image_x
= m_area
.x
+x
- buffer_x
;
1041 int image_y
= m_area
.y
+y
- buffer_y
;
1044 image
->SetRGB( image_x
, image_y
, m_red
, m_green
, m_blue
);
1047 int red1
= (m_red
* alpha
) / 255;
1048 int green1
= (m_green
* alpha
) / 255;
1049 int blue1
= (m_blue
* alpha
) / 255;
1052 int red2
= image
->GetRed( image_x
, image_y
);
1053 int green2
= image
->GetGreen( image_x
, image_y
);
1054 int blue2
= image
->GetBlue( image_x
, image_y
);
1055 red2
= (red2
* alpha
) / 255;
1056 green2
= (green2
* alpha
) / 255;
1057 blue2
= (blue2
* alpha
) / 255;
1059 image
->SetRGB( image_x
, image_y
, red1
+red2
, green1
+green2
, blue1
+blue2
);
1065 void wxCanvasText::WriteSVG( wxTextOutputStream
&stream
)
1069 void wxCanvasText::Recreate()
1071 if (m_alpha
) delete [] m_alpha
;
1073 m_area
.x
= m_owner
->GetDeviceX( m_x
);
1074 m_area
.y
= m_owner
->GetDeviceY( m_y
);
1076 m_area
.width
= 100; // TODO, calculate length
1077 m_area
.height
= m_size
;
1078 m_alpha
= new unsigned char[100*m_size
];
1079 memset( m_alpha
, 0, m_area
.width
*m_area
.height
);
1082 FT_Face face
= ((wxFaceData
*)m_faceData
)->m_face
;
1083 FT_GlyphSlot slot
= face
->glyph
;
1087 for (int n
= 0; n
< (int)m_text
.Len(); n
++)
1089 FT_UInt index
= FT_Get_Char_Index( face
, m_text
[n
] );
1091 int error
= FT_Load_Glyph( face
, index
, FT_LOAD_DEFAULT
);
1092 if (error
) continue;
1094 error
= FT_Render_Glyph( face
->glyph
, ft_render_mode_normal
);
1095 if (error
) continue;
1097 FT_Bitmap
*bitmap
= &slot
->bitmap
;
1098 unsigned char* buffer
= bitmap
->buffer
;
1099 for (int y
= 0; y
< bitmap
->rows
; y
++)
1100 for (int x
= 0; x
< bitmap
->width
; x
++)
1102 unsigned char alpha
= buffer
[ y
*bitmap
->pitch
+ x
];
1103 if (alpha
== 0) continue;
1105 int xx
= pen_x
+ slot
->bitmap_left
+ x
;
1106 int yy
= pen_y
- slot
->bitmap_top
+ y
;
1107 m_alpha
[ yy
* m_area
.width
+ xx
] = alpha
;
1110 pen_x
+= slot
->advance
.x
>> 6;
1111 pen_y
+= slot
->advance
.y
>> 6;
1116 //----------------------------------------------------------------------------
1118 //----------------------------------------------------------------------------
1120 IMPLEMENT_CLASS(wxCanvas
,wxScrolledWindow
)
1122 BEGIN_EVENT_TABLE(wxCanvas
,wxScrolledWindow
)
1123 EVT_CHAR( wxCanvas::OnChar
)
1124 EVT_PAINT( wxCanvas::OnPaint
)
1125 EVT_SIZE( wxCanvas::OnSize
)
1126 EVT_IDLE( wxCanvas::OnIdle
)
1127 EVT_MOUSE_EVENTS( wxCanvas::OnMouse
)
1128 EVT_SET_FOCUS( wxCanvas::OnSetFocus
)
1129 EVT_KILL_FOCUS( wxCanvas::OnKillFocus
)
1130 EVT_ERASE_BACKGROUND( wxCanvas::OnEraseBackground
)
1133 wxCanvas::wxCanvas( wxWindow
*parent
, wxWindowID id
,
1134 const wxPoint
&position
, const wxSize
& size
, long style
) :
1135 wxScrolledWindow( parent
, id
, position
, size
, style
)
1139 m_needUpdate
= FALSE
;
1143 m_lastMouse
= (wxCanvasObject
*)NULL
;
1144 m_captureMouse
= (wxCanvasObject
*)NULL
;
1147 //root group always at 0,0
1148 m_root
= new wxCanvasObjectGroup();
1149 m_root
->DeleteContents( TRUE
);
1150 m_root
->SetOwner(this);
1153 wxCanvas::~wxCanvas()
1155 wxNode
*node
= m_updateRects
.First();
1158 wxRect
*rect
= (wxRect
*) node
->Data();
1160 m_updateRects
.DeleteNode( node
);
1161 node
= m_updateRects
.First();
1165 void wxCanvas::SetArea( int width
, int height
)
1167 SetScrollbars( 10, 10, width
/10, height
/10 );
1170 void wxCanvas::SetColour( unsigned char red
, unsigned char green
, unsigned char blue
)
1176 SetBackgroundColour( wxColour( red
, green
, blue
) );
1178 if (m_frozen
) return;
1181 unsigned char *data
= m_buffer
.GetData();
1183 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
1184 for (int x
= 0; x
< m_buffer
.GetWidth(); x
++)
1195 dc
.SelectObject( m_buffer
);
1196 dc
.SetPen( *wxTRANSPARENT_PEN
);
1197 wxBrush
brush( wxColour( red
,green
,blue
), wxSOLID
);
1198 dc
.SetBrush( brush
);
1199 dc
.DrawRectangle( 0, 0, m_buffer
.GetWidth(), m_buffer
.GetHeight() );
1200 dc
.SelectObject( wxNullBitmap
);
1204 void wxCanvas::SetCaptureMouse( wxCanvasObject
*obj
)
1208 wxWindow::CaptureMouse();
1209 m_captureMouse
= obj
;
1213 wxWindow::ReleaseMouse();
1214 m_captureMouse
= NULL
;
1218 void wxCanvas::Freeze()
1223 void wxCanvas::Thaw()
1225 wxNode
*node
= m_updateRects
.First();
1228 wxRect
*rect
= (wxRect
*) node
->Data();
1230 m_updateRects
.DeleteNode( node
);
1231 node
= m_updateRects
.First();
1237 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), m_buffer
.GetHeight() );
1240 void wxCanvas::Update( int x
, int y
, int width
, int height
, bool blit
)
1242 if (m_frozen
) return;
1247 width
-= m_bufferX
-x
;
1250 if (width
<= 0) return;
1254 height
-= m_bufferY
-y
;
1257 if (height
<= 0) return;
1259 if (x
+width
> m_bufferX
+m_buffer
.GetWidth())
1261 width
= m_bufferX
+m_buffer
.GetWidth() - x
;
1263 if (width
<= 0) return;
1265 if (y
+height
> m_bufferY
+m_buffer
.GetHeight())
1267 height
= m_bufferY
+m_buffer
.GetHeight() - y
;
1269 if (height
<= 0) return;
1271 // update is within the buffer
1272 m_needUpdate
= TRUE
;
1274 // has to be blitted to screen later
1277 m_updateRects
.Append(
1278 (wxObject
*) new wxRect( x
,y
,width
,height
) );
1282 // speed up with direct access, maybe add wxImage::Clear(x,y,w,h,r,g,b)
1283 int start_y
= y
- m_bufferY
;
1284 int end_y
= y
+height
- m_bufferY
;
1285 int start_x
= x
- m_bufferX
;
1286 int end_x
= x
+width
- m_bufferX
;
1287 for (int yy
= start_y
; yy
< end_y
; yy
++)
1288 for (int xx
= start_x
; xx
< end_x
; xx
++)
1289 m_buffer
.SetRGB( xx
, yy
, m_red
, m_green
, m_blue
);
1291 m_root
->Render(0,0, x
, y
, width
, height
);
1294 dc
.SelectObject( m_buffer
);
1295 dc
.SetPen( *wxTRANSPARENT_PEN
);
1296 wxBrush
brush( wxColour( m_red
,m_green
,m_blue
), wxSOLID
);
1297 dc
.SetBrush( brush
);
1298 dc
.DrawRectangle( x
-m_bufferX
, y
-m_bufferY
, width
, height
);
1301 m_root
->Render(0,0, x
, y
, width
, height
);
1303 dc
.SelectObject( wxNullBitmap
);
1307 void wxCanvas::BlitBuffer( wxDC
&dc
)
1309 wxNode
*node
= m_updateRects
.First();
1312 wxRect
*rect
= (wxRect
*) node
->Data();
1314 wxRect
sub_rect( *rect
);
1315 sub_rect
.x
-= m_bufferX
;
1316 sub_rect
.y
-= m_bufferY
;
1320 wxImage
sub_image( m_buffer
.GetSubImage( sub_rect
) );
1322 int bpp
= wxDisplayDepth();
1325 // the init code is doubled in wxImage
1326 static bool s_hasInitialized
= FALSE
;
1328 if (!s_hasInitialized
)
1331 s_hasInitialized
= TRUE
;
1334 gdk_draw_rgb_image( GTK_PIZZA(m_wxwindow
)->bin_window
,
1335 m_wxwindow
->style
->black_gc
,
1336 sub_rect
.x
, sub_rect
.y
,
1337 sub_image
.GetWidth(), sub_image
.GetHeight(),
1338 GDK_RGB_DITHER_NONE
,
1339 sub_image
.GetData(),
1340 sub_image
.GetWidth()*3 );
1344 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
1345 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
1348 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
1349 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
1352 #else // IMAGE_CANVAS
1354 // Maybe clipping use SetClipping() is faster than
1355 // getting the subrect first and drawing it then?
1356 wxBitmap
sub_bitmap( m_buffer
.GetSubBitmap( sub_rect
) );
1357 dc
.DrawBitmap( sub_bitmap
, rect
->x
, rect
->y
);
1361 m_updateRects
.DeleteNode( node
);
1362 node
= m_updateRects
.First();
1365 m_needUpdate
= FALSE
;
1368 void wxCanvas::UpdateNow()
1370 if (m_frozen
) return;
1372 if (!m_needUpdate
) return;
1374 wxClientDC
dc( this );
1380 int wxCanvas::GetDeviceX( double x
)
1385 int wxCanvas::GetDeviceY( double y
)
1390 int wxCanvas::GetDeviceWidth( double width
)
1395 int wxCanvas::GetDeviceHeight( double height
)
1397 return (int) height
;
1400 void wxCanvas::Recreate()
1405 void wxCanvas::Prepend( wxCanvasObject
* obj
)
1407 m_root
->Prepend( obj
);
1408 obj
->SetOwner(this);
1412 if (!obj
->IsControl())
1413 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
1416 void wxCanvas::Append( wxCanvasObject
* obj
)
1418 m_root
->Append( obj
);
1419 obj
->SetOwner(this);
1423 if (!obj
->IsControl())
1424 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
1427 void wxCanvas::Insert( size_t before
, wxCanvasObject
* obj
)
1429 m_root
->Insert( before
, obj
);
1430 obj
->SetOwner(this);
1434 if (!obj
->IsControl())
1435 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
1438 void wxCanvas::Remove( wxCanvasObject
* obj
)
1440 int x
= obj
->GetX();
1441 int y
= obj
->GetY();
1442 int w
= obj
->GetWidth();
1443 int h
= obj
->GetHeight();
1444 bool ic
= obj
->IsControl();
1446 m_root
->Remove( obj
);
1449 Update( x
, y
, w
, h
);
1452 void wxCanvas::OnPaint(wxPaintEvent
&event
)
1457 if (!m_buffer
.Ok()) return;
1459 if (m_frozen
) return;
1461 m_needUpdate
= TRUE
;
1463 wxRegionIterator
it( GetUpdateRegion() );
1469 int w
= it
.GetWidth();
1470 int h
= it
.GetHeight();
1472 if (x
+w
> m_buffer
.GetWidth())
1473 w
= m_buffer
.GetWidth() - x
;
1474 if (y
+h
> m_buffer
.GetHeight())
1475 h
= m_buffer
.GetHeight() - y
;
1477 if ((w
> 0) && (h
> 0))
1479 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1480 m_updateRects
.Append( (wxObject
*) new wxRect( x
, y
, w
, h
) );
1489 void wxCanvas::ScrollWindow( int dx
, int dy
, const wxRect
* rect
)
1491 // If any updates are pending, do them now since they will
1492 // expect the previous m_bufferX and m_bufferY values.
1495 // The buffer always starts at the top left corner of the
1496 // client area. Indeed, it is the client area.
1497 CalcUnscrolledPosition( 0, 0, &m_bufferX
, &m_bufferY
);
1500 unsigned char* data
= m_buffer
.GetData();
1506 unsigned char *source
= data
;
1507 unsigned char *dest
= data
+ (dy
* m_buffer
.GetWidth() * 3);
1508 size_t count
= (size_t) (m_buffer
.GetWidth() * 3 * (m_buffer
.GetHeight()-dy
));
1509 memmove( dest
, source
, count
);
1511 // We update the new buffer area, but there is no need to
1512 // blit (last param FALSE) since the ensuing paint event will
1514 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), dy
, FALSE
);
1518 unsigned char *dest
= data
;
1519 unsigned char *source
= data
+ (-dy
* m_buffer
.GetWidth() * 3);
1520 size_t count
= (size_t) (m_buffer
.GetWidth() * 3 * (m_buffer
.GetHeight()+dy
));
1521 memmove( dest
, source
, count
);
1523 // We update the new buffer area, but there is no need to
1524 // blit (last param FALSE) since the ensuing paint event will
1526 Update( m_bufferX
, m_bufferY
+m_buffer
.GetHeight()+dy
, m_buffer
.GetWidth(), -dy
, FALSE
);
1534 unsigned char *source
= data
;
1535 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
1537 unsigned char *dest
= source
+ dx
*3;
1538 memmove( dest
, source
, (m_buffer
.GetWidth()-dx
) * 3 );
1539 source
+= m_buffer
.GetWidth()*3;
1542 // We update the new buffer area, but there is no need to
1543 // blit (last param FALSE) since the ensuing paint event will
1545 Update( m_bufferX
, m_bufferY
, dx
, m_buffer
.GetHeight(), FALSE
);
1549 unsigned char *dest
= data
;
1550 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
1552 unsigned char *source
= dest
- dx
*3;
1553 memmove( dest
, source
, (m_buffer
.GetWidth()+dx
) * 3 );
1554 dest
+= m_buffer
.GetWidth()*3;
1557 // We update the new buffer area, but there is no need to
1558 // blit (last param FALSE) since the ensuing paint event will
1560 Update( m_bufferX
+m_buffer
.GetWidth()+dx
, m_bufferY
, -dx
, m_buffer
.GetHeight(), FALSE
);
1564 // Update everything, TODO: scrolling
1565 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), m_buffer
.GetHeight(), FALSE
);
1568 wxWindow::ScrollWindow( dx
, dy
, rect
);
1571 void wxCanvas::OnMouse(wxMouseEvent
&event
)
1573 int x
= event
.GetX();
1574 int y
= event
.GetY();
1575 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1577 if (event
.GetEventType() == wxEVT_MOTION
)
1579 if (m_captureMouse
) //no matter what go to this one
1581 wxMouseEvent
child_event( wxEVT_MOTION
);
1582 child_event
.SetEventObject(m_captureMouse
);
1583 child_event
.m_x
= x
- m_captureMouse
->GetX();
1584 child_event
.m_y
= y
- m_captureMouse
->GetY();
1585 child_event
.m_leftDown
= event
.m_leftDown
;
1586 child_event
.m_rightDown
= event
.m_rightDown
;
1587 child_event
.m_middleDown
= event
.m_middleDown
;
1588 child_event
.m_controlDown
= event
.m_controlDown
;
1589 child_event
.m_shiftDown
= event
.m_shiftDown
;
1590 child_event
.m_altDown
= event
.m_altDown
;
1591 child_event
.m_metaDown
= event
.m_metaDown
;
1593 m_captureMouse
->ProcessEvent( child_event
);
1598 wxCanvasObject
*obj
= m_root
->IsHitObject(x
,y
,0);
1600 if (obj
&& !obj
->IsControl())
1602 wxMouseEvent
child_event( wxEVT_MOTION
);
1603 child_event
.SetEventObject( obj
);
1604 child_event
.m_x
= x
- obj
->GetX();
1605 child_event
.m_y
= y
- obj
->GetY();
1606 child_event
.m_leftDown
= event
.m_leftDown
;
1607 child_event
.m_rightDown
= event
.m_rightDown
;
1608 child_event
.m_middleDown
= event
.m_middleDown
;
1609 child_event
.m_controlDown
= event
.m_controlDown
;
1610 child_event
.m_shiftDown
= event
.m_shiftDown
;
1611 child_event
.m_altDown
= event
.m_altDown
;
1612 child_event
.m_metaDown
= event
.m_metaDown
;
1614 if ((obj
!= m_lastMouse
) && (m_lastMouse
!= NULL
))
1616 child_event
.SetEventType( wxEVT_LEAVE_WINDOW
);
1617 child_event
.SetEventObject( m_lastMouse
);
1618 child_event
.m_x
= x
- m_lastMouse
->GetX();
1619 child_event
.m_y
= y
- m_lastMouse
->GetY();
1620 m_lastMouse
->ProcessEvent( child_event
);
1623 child_event
.SetEventType( wxEVT_ENTER_WINDOW
);
1624 child_event
.SetEventObject( m_lastMouse
);
1625 child_event
.m_x
= x
- m_lastMouse
->GetX();
1626 child_event
.m_y
= y
- m_lastMouse
->GetY();
1627 m_lastMouse
->ProcessEvent( child_event
);
1629 child_event
.SetEventType( wxEVT_MOTION
);
1630 child_event
.SetEventObject( obj
);
1633 obj
->ProcessEvent( child_event
);
1639 wxMouseEvent
child_event( wxEVT_LEAVE_WINDOW
);
1640 child_event
.SetEventObject( m_lastMouse
);
1641 child_event
.m_x
= x
- m_lastMouse
->GetX();
1642 child_event
.m_y
= y
- m_lastMouse
->GetY();
1643 child_event
.m_leftDown
= event
.m_leftDown
;
1644 child_event
.m_rightDown
= event
.m_rightDown
;
1645 child_event
.m_middleDown
= event
.m_middleDown
;
1646 child_event
.m_controlDown
= event
.m_controlDown
;
1647 child_event
.m_shiftDown
= event
.m_shiftDown
;
1648 child_event
.m_altDown
= event
.m_altDown
;
1649 child_event
.m_metaDown
= event
.m_metaDown
;
1650 m_lastMouse
->ProcessEvent( child_event
);
1652 m_lastMouse
= (wxCanvasObject
*) NULL
;
1658 if (m_captureMouse
) //no matter what go to this one
1660 wxMouseEvent
child_event( event
.GetEventType() );
1661 child_event
.SetEventObject(m_captureMouse
);
1662 child_event
.m_x
= x
- m_captureMouse
->GetX();
1663 child_event
.m_y
= y
- m_captureMouse
->GetY();
1664 child_event
.m_leftDown
= event
.m_leftDown
;
1665 child_event
.m_rightDown
= event
.m_rightDown
;
1666 child_event
.m_middleDown
= event
.m_middleDown
;
1667 child_event
.m_controlDown
= event
.m_controlDown
;
1668 child_event
.m_shiftDown
= event
.m_shiftDown
;
1669 child_event
.m_altDown
= event
.m_altDown
;
1670 child_event
.m_metaDown
= event
.m_metaDown
;
1671 m_captureMouse
->ProcessEvent( child_event
);
1675 wxCanvasObject
*obj
= m_root
->IsHitObject(x
,y
,0);
1677 if (obj
&& !obj
->IsControl())
1679 wxMouseEvent
child_event( event
.GetEventType() );
1680 child_event
.SetEventObject( obj
);
1681 child_event
.m_x
= x
- obj
->GetX();
1682 child_event
.m_y
= y
- obj
->GetY();
1683 child_event
.m_leftDown
= event
.m_leftDown
;
1684 child_event
.m_rightDown
= event
.m_rightDown
;
1685 child_event
.m_middleDown
= event
.m_middleDown
;
1686 child_event
.m_controlDown
= event
.m_controlDown
;
1687 child_event
.m_shiftDown
= event
.m_shiftDown
;
1688 child_event
.m_altDown
= event
.m_altDown
;
1689 child_event
.m_metaDown
= event
.m_metaDown
;
1691 obj
->ProcessEvent( child_event
);
1700 void wxCanvas::OnSize(wxSizeEvent
&event
)
1703 GetClientSize( &w
, &h
);
1705 m_buffer
= wxImage( w
, h
);
1707 m_buffer
= wxBitmap( w
, h
);
1710 CalcUnscrolledPosition( 0, 0, &m_bufferX
, &m_bufferY
);
1712 wxNode
*node
= m_updateRects
.First();
1715 wxRect
*rect
= (wxRect
*) node
->Data();
1717 m_updateRects
.DeleteNode( node
);
1718 node
= m_updateRects
.First();
1723 Update( m_bufferX
, m_bufferY
, m_buffer
.GetWidth(), m_buffer
.GetHeight(), FALSE
);
1728 void wxCanvas::OnIdle(wxIdleEvent
&event
)
1734 void wxCanvas::OnSetFocus(wxFocusEvent
&event
)
1738 void wxCanvas::OnKillFocus(wxFocusEvent
&event
)
1742 void wxCanvas::OnChar(wxKeyEvent
&event
)
1747 void wxCanvas::OnEraseBackground(wxEraseEvent
&event
)
1751 //--------------------------------------------------------------------
1753 //--------------------------------------------------------------------
1755 class wxCanvasModule
: public wxModule
1758 virtual bool OnInit();
1759 virtual void OnExit();
1762 DECLARE_DYNAMIC_CLASS(wxCanvasModule
)
1765 IMPLEMENT_DYNAMIC_CLASS(wxCanvasModule
, wxModule
)
1767 bool wxCanvasModule::OnInit()
1770 int error
= FT_Init_FreeType( &g_freetypeLibrary
);
1771 if (error
) return FALSE
;
1777 void wxCanvasModule::OnExit()
1780 FT_Done_FreeType( g_freetypeLibrary
);