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::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
105 void wxCanvasObject::Recreate()
109 void wxCanvasObject::WriteSVG( wxTextOutputStream
&stream
)
113 //----------------------------------------------------------------------------
115 //----------------------------------------------------------------------------
117 wxCanvasRect::wxCanvasRect( double x
, double y
, double w
, double h
,
118 unsigned char red
, unsigned char green
, unsigned char blue
)
131 void wxCanvasRect::Recreate()
133 SetArea( m_owner
->GetDeviceX( m_x
),
134 m_owner
->GetDeviceY( m_y
),
135 m_owner
->GetDeviceWidth( m_width
),
136 m_owner
->GetDeviceHeight( m_height
) );
139 void wxCanvasRect::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
141 wxImage
*image
= m_owner
->GetBuffer();
143 for (int y
= clip_y
; y
< clip_y
+clip_height
; y
++)
144 for (int x
= clip_x
; x
< clip_x
+clip_width
; x
++)
145 image
->SetRGB( x
, y
, m_red
, m_green
, m_blue
);
148 void wxCanvasRect::WriteSVG( wxTextOutputStream
&stream
)
152 //----------------------------------------------------------------------------
154 //----------------------------------------------------------------------------
156 wxCanvasLine::wxCanvasLine( double x1
, double y1
, double x2
, double y2
,
157 unsigned char red
, unsigned char green
, unsigned char blue
)
170 void wxCanvasLine::Recreate()
172 int x1
= m_owner
->GetDeviceX( m_x1
);
173 int y1
= m_owner
->GetDeviceY( m_y1
);
174 int x2
= m_owner
->GetDeviceX( m_x2
);
175 int y2
= m_owner
->GetDeviceY( m_y2
);
188 SetArea( x1
, y1
, x2
-x1
+1, y2
-y1
+1 );
191 void wxCanvasLine::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
193 wxImage
*image
= m_owner
->GetBuffer();
195 if ((m_area
.width
== 0) && (m_area
.height
== 0))
197 image
->SetRGB( m_area
.x
, m_area
.y
, m_red
, m_green
, m_blue
);
201 int x1
= m_owner
->GetDeviceX( m_x1
);
202 int y1
= m_owner
->GetDeviceY( m_y1
);
203 int x2
= m_owner
->GetDeviceX( m_x2
);
204 int y2
= m_owner
->GetDeviceY( m_y2
);
206 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
209 si
= (di
< 0)? -1 : 1;
212 sj
= (dj
< 0)? -1 : 1;
224 if ((ii
>= clip_x
) && (ii
<= clip_x
+clip_width
) &&
225 (jj
>= clip_y
) && (jj
<= clip_y
+clip_height
))
227 image
->SetRGB( ii
, jj
, m_red
, m_blue
, m_green
);
245 if ((ii
>= clip_x
) && (ii
<= clip_x
+clip_width
) &&
246 (jj
>= clip_y
) && (jj
<= clip_y
+clip_height
))
248 image
->SetRGB( ii
, jj
, m_red
, m_blue
, m_green
);
262 void wxCanvasLine::WriteSVG( wxTextOutputStream
&stream
)
267 //----------------------------------------------------------------------------
269 //----------------------------------------------------------------------------
271 wxCanvasImage::wxCanvasImage( const wxImage
&image
, double x
, double y
, double w
, double h
)
283 void wxCanvasImage::Recreate()
285 SetArea( m_owner
->GetDeviceX( m_x
),
286 m_owner
->GetDeviceY( m_y
),
287 m_owner
->GetDeviceWidth( m_width
),
288 m_owner
->GetDeviceHeight( m_height
) );
290 if ((m_area
.width
== m_image
.GetWidth()) &&
291 (m_area
.width
== m_image
.GetWidth()))
294 m_tmp
= m_image
.Scale( m_area
.width
, m_area
.height
);
297 void wxCanvasImage::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
299 if ((clip_x
== m_area
.x
) &&
300 (clip_y
== m_area
.y
) &&
301 (clip_width
== m_area
.width
) &&
302 (clip_height
== m_area
.height
))
304 m_owner
->GetBuffer()->Paste( m_tmp
, clip_x
, clip_y
);
309 int start_x
= clip_x
- m_area
.x
;
310 int start_y
= clip_y
- m_area
.y
;
312 wxRect
rect( start_x
, start_y
, clip_width
, clip_height
);
313 wxImage
sub_image( m_tmp
.GetSubImage( rect
) );
314 m_owner
->GetBuffer()->Paste( sub_image
, clip_x
, clip_y
);
318 void wxCanvasImage::WriteSVG( wxTextOutputStream
&stream
)
323 //----------------------------------------------------------------------------
325 //----------------------------------------------------------------------------
327 wxCanvasControl::wxCanvasControl( wxWindow
*control
)
334 wxCanvasControl::~wxCanvasControl()
336 m_control
->Destroy();
339 void wxCanvasControl::Recreate()
341 m_control
->GetSize( &m_area
.width
, &m_area
.height
);
342 m_control
->GetPosition( &m_area
.x
, &m_area
.y
);
345 void wxCanvasControl::Move( int x
, int y
)
347 m_control
->Move( x
, y
);
350 //----------------------------------------------------------------------------
352 //----------------------------------------------------------------------------
364 wxCanvasText::wxCanvasText( const wxString
&text
, double x
, double y
, const wxString
&fontFile
, int size
)
368 m_fontFileName
= fontFile
;
381 wxFaceData
*data
= new wxFaceData
;
384 int error
= FT_New_Face( g_freetypeLibrary
,
389 error
= FT_Set_Char_Size( data
->m_face
,
397 wxCanvasText::~wxCanvasText()
400 wxFaceData
*data
= (wxFaceData
*) m_faceData
;
404 if (m_alpha
) delete [] m_alpha
;
407 void wxCanvasText::SetRGB( unsigned char red
, unsigned char green
, unsigned char blue
)
414 void wxCanvasText::SetFlag( int flag
)
419 void wxCanvasText::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
421 if (!m_alpha
) return;
423 wxImage
*image
= m_owner
->GetBuffer();
426 int start_x
= clip_x
- m_area
.x
;
427 int end_x
= clip_width
+ start_x
;
428 int start_y
= clip_y
- m_area
.y
;
429 int end_y
= clip_height
+ start_y
;
431 for (int y
= start_y
; y
< end_y
; y
++)
432 for (int x
= start_x
; x
< end_x
; x
++)
434 int alpha
= m_alpha
[y
*m_area
.width
+ x
];
437 int image_x
= m_area
.x
+x
;
438 int image_y
= m_area
.y
+y
;
441 image
->SetRGB( image_x
, image_y
, m_red
, m_green
, m_blue
);
444 int red1
= (m_red
* alpha
) / 255;
445 int green1
= (m_green
* alpha
) / 255;
446 int blue1
= (m_blue
* alpha
) / 255;
449 int red2
= image
->GetRed( image_x
, image_y
);
450 int green2
= image
->GetGreen( image_x
, image_y
);
451 int blue2
= image
->GetBlue( image_x
, image_y
);
452 red2
= (red2
* alpha
) / 255;
453 green2
= (green2
* alpha
) / 255;
454 blue2
= (blue2
* alpha
) / 255;
456 image
->SetRGB( image_x
, image_y
, red1
+red2
, green1
+green2
, blue1
+blue2
);
461 void wxCanvasText::WriteSVG( wxTextOutputStream
&stream
)
465 void wxCanvasText::Recreate()
467 if (m_alpha
) delete [] m_alpha
;
469 m_area
.x
= m_owner
->GetDeviceX( m_x
);
470 m_area
.y
= m_owner
->GetDeviceY( m_y
);
472 m_area
.width
= 100; // TODO, calculate length
473 m_area
.height
= m_size
;
474 m_alpha
= new unsigned char[100*m_size
];
475 memset( m_alpha
, 0, m_area
.width
*m_area
.height
);
478 FT_Face face
= ((wxFaceData
*)m_faceData
)->m_face
;
479 FT_GlyphSlot slot
= face
->glyph
;
483 for (int n
= 0; n
< (int)m_text
.Len(); n
++)
485 FT_UInt index
= FT_Get_Char_Index( face
, m_text
[n
] );
487 int error
= FT_Load_Glyph( face
, index
, FT_LOAD_DEFAULT
);
490 error
= FT_Render_Glyph( face
->glyph
, ft_render_mode_normal
);
493 FT_Bitmap
*bitmap
= &slot
->bitmap
;
494 unsigned char* buffer
= bitmap
->buffer
;
495 for (int y
= 0; y
< bitmap
->rows
; y
++)
496 for (int x
= 0; x
< bitmap
->width
; x
++)
498 unsigned char alpha
= buffer
[ y
*bitmap
->pitch
+ x
];
499 if (alpha
== 0) continue;
501 int xx
= pen_x
+ slot
->bitmap_left
+ x
;
502 int yy
= pen_y
- slot
->bitmap_top
+ y
;
503 m_alpha
[ yy
* m_area
.width
+ xx
] = alpha
;
506 pen_x
+= slot
->advance
.x
>> 6;
507 pen_y
+= slot
->advance
.y
>> 6;
512 //----------------------------------------------------------------------------
514 //----------------------------------------------------------------------------
516 IMPLEMENT_CLASS(wxCanvas
,wxScrolledWindow
)
518 BEGIN_EVENT_TABLE(wxCanvas
,wxScrolledWindow
)
519 EVT_CHAR( wxCanvas::OnChar
)
520 EVT_PAINT( wxCanvas::OnPaint
)
521 EVT_SIZE( wxCanvas::OnSize
)
522 EVT_IDLE( wxCanvas::OnIdle
)
523 EVT_MOUSE_EVENTS( wxCanvas::OnMouse
)
524 EVT_SET_FOCUS( wxCanvas::OnSetFocus
)
525 EVT_KILL_FOCUS( wxCanvas::OnKillFocus
)
528 wxCanvas::wxCanvas( wxWindow
*parent
, wxWindowID id
,
529 const wxPoint
&position
, const wxSize
& size
, long style
) :
530 wxScrolledWindow( parent
, id
, position
, size
, style
)
532 m_needUpdate
= FALSE
;
533 m_objects
.DeleteContents( TRUE
);
537 m_lastMouse
= (wxCanvasObject
*)NULL
;
538 m_captureMouse
= (wxCanvasObject
*)NULL
;
542 wxCanvas::~wxCanvas()
544 wxNode
*node
= m_updateRects
.First();
547 wxRect
*rect
= (wxRect
*) node
->Data();
549 m_updateRects
.DeleteNode( node
);
550 node
= m_updateRects
.First();
554 void wxCanvas::SetArea( int width
, int height
)
556 m_buffer
= wxImage( width
, height
);
557 SetScrollbars( 10, 10, width
/10, height
/10 );
560 void wxCanvas::SetColour( unsigned char red
, unsigned char green
, unsigned char blue
)
566 if (m_frozen
) return;
568 unsigned char *data
= m_buffer
.GetData();
570 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
571 for (int x
= 0; x
< m_buffer
.GetWidth(); x
++)
582 void wxCanvas::CaptureMouse( wxCanvasObject
*obj
)
584 wxWindow::CaptureMouse();
585 m_captureMouse
= obj
;
588 void wxCanvas::ReleaseMouse()
590 m_captureMouse
= (wxCanvasObject
*) NULL
;
591 wxWindow::ReleaseMouse();
594 void wxCanvas::Freeze()
599 void wxCanvas::Thaw()
601 wxNode
*node
= m_updateRects
.First();
604 wxRect
*rect
= (wxRect
*) node
->Data();
606 m_updateRects
.DeleteNode( node
);
607 node
= m_updateRects
.First();
612 Update( 0, 0, m_buffer
.GetWidth(), m_buffer
.GetHeight() );
615 void wxCanvas::Update( int x
, int y
, int width
, int height
)
617 if (m_frozen
) return;
625 if (width
< 0) return;
632 if (height
< 0) return;
634 if (x
+width
> m_buffer
.GetWidth())
636 width
= m_buffer
.GetWidth() - x
;
638 if (width
< 0) return;
640 if (y
+height
> m_buffer
.GetHeight())
642 height
= m_buffer
.GetHeight() - y
;
644 if (height
< 0) return;
646 // update is within the buffer
649 // has to be blitted to screen later
650 m_updateRects
.Append(
651 (wxObject
*) new wxRect( x
,y
,width
,height
) );
653 // speed up with direct access, maybe add wxImage::Clear(x,y,w,h,r,g,b)
654 for (int yy
= y
; yy
< y
+height
; yy
++)
655 for (int xx
= x
; xx
< x
+width
; xx
++)
656 m_buffer
.SetRGB( xx
, yy
, m_red
, m_green
, m_blue
);
658 // cycle through all objects
659 wxNode
*node
= m_objects
.First();
662 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
664 if (!obj
->IsControl())
666 // If we have 10.000 objects, we will go through
667 // this 10.000 times for each update, so we have
668 // to optimise carefully.
669 int clip_x
= obj
->GetX();
670 int clip_width
= obj
->GetWidth();
673 clip_width
-= x
-clip_x
;
678 if (clip_x
+ clip_width
> x
+ width
)
679 clip_width
= x
+width
-clip_x
;
683 int clip_y
= obj
->GetY();
684 int clip_height
= obj
->GetHeight();
687 clip_height
-= y
-clip_y
;
692 if (clip_y
+ clip_height
> y
+ height
)
693 clip_height
= y
+height
-clip_y
;
696 obj
->Render( clip_x
, clip_y
, clip_width
, clip_height
);
706 void wxCanvas::BlitBuffer( wxDC
&dc
)
708 wxNode
*node
= m_updateRects
.First();
711 wxRect
*rect
= (wxRect
*) node
->Data();
712 wxImage
sub_image( m_buffer
.GetSubImage( *rect
) );
714 // DirectDraw here, please
717 int bpp
= wxDisplayDepth();
720 // the init code is doubled in wxImage
721 static bool s_hasInitialized
= FALSE
;
723 if (!s_hasInitialized
)
726 s_hasInitialized
= TRUE
;
731 CalcScrolledPosition( x
, y
, &x
, &y
);
733 gdk_draw_rgb_image( GTK_PIZZA(m_wxwindow
)->bin_window
,
734 m_wxwindow
->style
->black_gc
,
736 sub_image
.GetWidth(), sub_image
.GetHeight(),
739 sub_image
.GetWidth()*3 );
743 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
744 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
749 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
750 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
754 m_updateRects
.DeleteNode( node
);
755 node
= m_updateRects
.First();
758 m_needUpdate
= FALSE
;
761 void wxCanvas::UpdateNow()
763 if (!m_needUpdate
) return;
765 wxClientDC
dc( this );
771 int wxCanvas::GetDeviceX( double x
)
776 int wxCanvas::GetDeviceY( double y
)
781 int wxCanvas::GetDeviceWidth( double width
)
786 int wxCanvas::GetDeviceHeight( double height
)
791 void wxCanvas::Recreate()
793 wxNode
*node
= m_objects
.First();
796 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
804 void wxCanvas::Prepend( wxCanvasObject
* obj
)
806 m_objects
.Insert( obj
);
808 obj
->SetOwner( this );
811 if (!obj
->IsControl())
812 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
815 void wxCanvas::Append( wxCanvasObject
* obj
)
817 m_objects
.Append( obj
);
819 obj
->SetOwner( this );
822 if (!obj
->IsControl())
823 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
826 void wxCanvas::Insert( size_t before
, wxCanvasObject
* obj
)
828 m_objects
.Insert( before
, obj
);
830 obj
->SetOwner( this );
833 if (!obj
->IsControl())
834 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
837 void wxCanvas::Remove( wxCanvasObject
* obj
)
841 int w
= obj
->GetWidth();
842 int h
= obj
->GetHeight();
843 bool ic
= obj
->IsControl();
845 m_objects
.DeleteObject( obj
);
848 Update( x
, y
, w
, h
);
851 void wxCanvas::OnPaint(wxPaintEvent
&event
)
858 wxRegionIterator
it( GetUpdateRegion() );
863 CalcUnscrolledPosition( x
, y
, &x
, &y
);
865 int w
= it
.GetWidth();
866 int h
= it
.GetHeight();
868 if (x
+w
> m_buffer
.GetWidth())
869 w
= m_buffer
.GetWidth() - x
;
870 if (y
+h
> m_buffer
.GetHeight())
871 h
= m_buffer
.GetHeight() - y
;
873 if ((w
> 0) && (h
> 0))
874 m_updateRects
.Append( (wxObject
*) new wxRect( x
, y
, w
, h
) );
882 void wxCanvas::OnMouse(wxMouseEvent
&event
)
884 // should we implement mouse capture ?
886 int x
= event
.GetX();
887 int y
= event
.GetY();
888 CalcUnscrolledPosition( x
, y
, &x
, &y
);
890 if (event
.GetEventType() == wxEVT_MOTION
)
892 wxNode
*node
= m_objects
.Last();
895 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
897 if (!obj
->IsControl())
901 wxMouseEvent
child_event( wxEVT_MOTION
);
902 child_event
.SetEventObject( obj
);
903 child_event
.m_x
= x
- obj
->GetX();
904 child_event
.m_y
= y
- obj
->GetY();
905 child_event
.m_leftDown
= event
.m_leftDown
;
906 child_event
.m_rightDown
= event
.m_rightDown
;
907 child_event
.m_middleDown
= event
.m_middleDown
;
908 child_event
.m_controlDown
= event
.m_controlDown
;
909 child_event
.m_shiftDown
= event
.m_shiftDown
;
910 child_event
.m_altDown
= event
.m_altDown
;
911 child_event
.m_metaDown
= event
.m_metaDown
;
913 if ((obj
!= m_lastMouse
) && (m_lastMouse
!= NULL
))
915 child_event
.SetEventType( wxEVT_LEAVE_WINDOW
);
916 child_event
.SetEventObject( m_lastMouse
);
917 child_event
.m_x
= x
- m_lastMouse
->GetX();
918 child_event
.m_y
= y
- m_lastMouse
->GetY();
919 m_lastMouse
->ProcessEvent( child_event
);
922 child_event
.SetEventType( wxEVT_ENTER_WINDOW
);
923 child_event
.SetEventObject( m_lastMouse
);
924 child_event
.m_x
= x
- m_lastMouse
->GetX();
925 child_event
.m_y
= y
- m_lastMouse
->GetY();
926 m_lastMouse
->ProcessEvent( child_event
);
928 child_event
.SetEventType( wxEVT_MOTION
);
929 child_event
.SetEventObject( obj
);
931 obj
->ProcessEvent( child_event
);
935 node
= node
->Previous();
939 wxMouseEvent
child_event( wxEVT_LEAVE_WINDOW
);
940 child_event
.SetEventObject( m_lastMouse
);
941 child_event
.m_x
= x
- m_lastMouse
->GetX();
942 child_event
.m_y
= y
- m_lastMouse
->GetY();
943 child_event
.m_leftDown
= event
.m_leftDown
;
944 child_event
.m_rightDown
= event
.m_rightDown
;
945 child_event
.m_middleDown
= event
.m_middleDown
;
946 child_event
.m_controlDown
= event
.m_controlDown
;
947 child_event
.m_shiftDown
= event
.m_shiftDown
;
948 child_event
.m_altDown
= event
.m_altDown
;
949 child_event
.m_metaDown
= event
.m_metaDown
;
950 m_lastMouse
->ProcessEvent( child_event
);
952 m_lastMouse
= (wxCanvasObject
*) NULL
;
959 void wxCanvas::OnSize(wxSizeEvent
&event
)
964 void wxCanvas::OnIdle(wxIdleEvent
&event
)
970 void wxCanvas::OnSetFocus(wxFocusEvent
&event
)
974 void wxCanvas::OnKillFocus(wxFocusEvent
&event
)
978 void wxCanvas::OnChar(wxKeyEvent
&event
)
983 //--------------------------------------------------------------------
985 //--------------------------------------------------------------------
987 class wxCanvasModule
: public wxModule
990 virtual bool OnInit();
991 virtual void OnExit();
994 DECLARE_DYNAMIC_CLASS(wxCanvasModule
)
997 IMPLEMENT_DYNAMIC_CLASS(wxCanvasModule
, wxModule
)
999 bool wxCanvasModule::OnInit()
1002 int error
= FT_Init_FreeType( &g_freetypeLibrary
);
1003 if (error
) return FALSE
;
1009 void wxCanvasModule::OnExit()
1012 FT_Done_FreeType( g_freetypeLibrary
);