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 clip_x
, int clip_y
, int clip_width
, int clip_height
)
121 void wxCanvasObject::Recreate()
125 void wxCanvasObject::WriteSVG( wxTextOutputStream
&stream
)
129 //----------------------------------------------------------------------------
131 //----------------------------------------------------------------------------
133 wxCanvasRect::wxCanvasRect( double x
, double y
, double w
, double h
,
134 unsigned char red
, unsigned char green
, unsigned char blue
)
147 void wxCanvasRect::Recreate()
149 SetArea( m_owner
->GetDeviceX( m_x
),
150 m_owner
->GetDeviceY( m_y
),
151 m_owner
->GetDeviceWidth( m_width
),
152 m_owner
->GetDeviceHeight( m_height
) );
155 void wxCanvasRect::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
157 wxImage
*image
= m_owner
->GetBuffer();
159 for (int y
= clip_y
; y
< clip_y
+clip_height
; y
++)
160 for (int x
= clip_x
; x
< clip_x
+clip_width
; x
++)
161 image
->SetRGB( x
, y
, m_red
, m_green
, m_blue
);
164 void wxCanvasRect::WriteSVG( wxTextOutputStream
&stream
)
168 //----------------------------------------------------------------------------
170 //----------------------------------------------------------------------------
172 wxCanvasLine::wxCanvasLine( double x1
, double y1
, double x2
, double y2
,
173 unsigned char red
, unsigned char green
, unsigned char blue
)
186 void wxCanvasLine::Recreate()
188 int x1
= m_owner
->GetDeviceX( m_x1
);
189 int y1
= m_owner
->GetDeviceY( m_y1
);
190 int x2
= m_owner
->GetDeviceX( m_x2
);
191 int y2
= m_owner
->GetDeviceY( m_y2
);
204 SetArea( x1
, y1
, x2
-x1
+1, y2
-y1
+1 );
207 void wxCanvasLine::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
209 wxImage
*image
= m_owner
->GetBuffer();
211 if ((m_area
.width
== 0) && (m_area
.height
== 0))
213 image
->SetRGB( m_area
.x
, m_area
.y
, m_red
, m_green
, m_blue
);
217 int x1
= m_owner
->GetDeviceX( m_x1
);
218 int y1
= m_owner
->GetDeviceY( m_y1
);
219 int x2
= m_owner
->GetDeviceX( m_x2
);
220 int y2
= m_owner
->GetDeviceY( m_y2
);
222 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
225 si
= (di
< 0)? -1 : 1;
228 sj
= (dj
< 0)? -1 : 1;
240 if ((ii
>= clip_x
) && (ii
<= clip_x
+clip_width
) &&
241 (jj
>= clip_y
) && (jj
<= clip_y
+clip_height
))
243 image
->SetRGB( ii
, jj
, m_red
, m_blue
, m_green
);
261 if ((ii
>= clip_x
) && (ii
<= clip_x
+clip_width
) &&
262 (jj
>= clip_y
) && (jj
<= clip_y
+clip_height
))
264 image
->SetRGB( ii
, jj
, m_red
, m_blue
, m_green
);
278 void wxCanvasLine::WriteSVG( wxTextOutputStream
&stream
)
283 //----------------------------------------------------------------------------
285 //----------------------------------------------------------------------------
287 wxCanvasImage::wxCanvasImage( const wxImage
&image
, double x
, double y
, double w
, double h
)
299 void wxCanvasImage::Recreate()
301 SetArea( m_owner
->GetDeviceX( m_x
),
302 m_owner
->GetDeviceY( m_y
),
303 m_owner
->GetDeviceWidth( m_width
),
304 m_owner
->GetDeviceHeight( m_height
) );
306 if ((m_area
.width
== m_image
.GetWidth()) &&
307 (m_area
.width
== m_image
.GetWidth()))
310 m_tmp
= m_image
.Scale( m_area
.width
, m_area
.height
);
313 void wxCanvasImage::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
315 if ((clip_x
== m_area
.x
) &&
316 (clip_y
== m_area
.y
) &&
317 (clip_width
== m_area
.width
) &&
318 (clip_height
== m_area
.height
))
320 m_owner
->GetBuffer()->Paste( m_tmp
, clip_x
, clip_y
);
325 int start_x
= clip_x
- m_area
.x
;
326 int start_y
= clip_y
- m_area
.y
;
328 wxRect
rect( start_x
, start_y
, clip_width
, clip_height
);
329 wxImage
sub_image( m_tmp
.GetSubImage( rect
) );
330 m_owner
->GetBuffer()->Paste( sub_image
, clip_x
, clip_y
);
334 void wxCanvasImage::WriteSVG( wxTextOutputStream
&stream
)
339 //----------------------------------------------------------------------------
341 //----------------------------------------------------------------------------
343 wxCanvasControl::wxCanvasControl( wxWindow
*control
)
350 wxCanvasControl::~wxCanvasControl()
352 m_control
->Destroy();
355 void wxCanvasControl::Recreate()
357 m_control
->GetSize( &m_area
.width
, &m_area
.height
);
358 m_control
->GetPosition( &m_area
.x
, &m_area
.y
);
361 void wxCanvasControl::Move( int x
, int y
)
363 m_control
->Move( x
, y
);
366 //----------------------------------------------------------------------------
368 //----------------------------------------------------------------------------
380 wxCanvasText::wxCanvasText( const wxString
&text
, double x
, double y
, const wxString
&fontFile
, int size
)
384 m_fontFileName
= fontFile
;
397 wxFaceData
*data
= new wxFaceData
;
400 int error
= FT_New_Face( g_freetypeLibrary
,
405 error
= FT_Set_Char_Size( data
->m_face
,
413 wxCanvasText::~wxCanvasText()
416 wxFaceData
*data
= (wxFaceData
*) m_faceData
;
420 if (m_alpha
) delete [] m_alpha
;
423 void wxCanvasText::SetRGB( unsigned char red
, unsigned char green
, unsigned char blue
)
430 void wxCanvasText::SetFlag( int flag
)
435 void wxCanvasText::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
437 if (!m_alpha
) return;
439 wxImage
*image
= m_owner
->GetBuffer();
442 int start_x
= clip_x
- m_area
.x
;
443 int end_x
= clip_width
+ start_x
;
444 int start_y
= clip_y
- m_area
.y
;
445 int end_y
= clip_height
+ start_y
;
447 for (int y
= start_y
; y
< end_y
; y
++)
448 for (int x
= start_x
; x
< end_x
; x
++)
450 int alpha
= m_alpha
[y
*m_area
.width
+ x
];
453 int image_x
= m_area
.x
+x
;
454 int image_y
= m_area
.y
+y
;
457 image
->SetRGB( image_x
, image_y
, m_red
, m_green
, m_blue
);
460 int red1
= (m_red
* alpha
) / 255;
461 int green1
= (m_green
* alpha
) / 255;
462 int blue1
= (m_blue
* alpha
) / 255;
465 int red2
= image
->GetRed( image_x
, image_y
);
466 int green2
= image
->GetGreen( image_x
, image_y
);
467 int blue2
= image
->GetBlue( image_x
, image_y
);
468 red2
= (red2
* alpha
) / 255;
469 green2
= (green2
* alpha
) / 255;
470 blue2
= (blue2
* alpha
) / 255;
472 image
->SetRGB( image_x
, image_y
, red1
+red2
, green1
+green2
, blue1
+blue2
);
477 void wxCanvasText::WriteSVG( wxTextOutputStream
&stream
)
481 void wxCanvasText::Recreate()
483 if (m_alpha
) delete [] m_alpha
;
485 m_area
.x
= m_owner
->GetDeviceX( m_x
);
486 m_area
.y
= m_owner
->GetDeviceY( m_y
);
488 m_area
.width
= 100; // TODO, calculate length
489 m_area
.height
= m_size
;
490 m_alpha
= new unsigned char[100*m_size
];
491 memset( m_alpha
, 0, m_area
.width
*m_area
.height
);
494 FT_Face face
= ((wxFaceData
*)m_faceData
)->m_face
;
495 FT_GlyphSlot slot
= face
->glyph
;
499 for (int n
= 0; n
< (int)m_text
.Len(); n
++)
501 FT_UInt index
= FT_Get_Char_Index( face
, m_text
[n
] );
503 int error
= FT_Load_Glyph( face
, index
, FT_LOAD_DEFAULT
);
506 error
= FT_Render_Glyph( face
->glyph
, ft_render_mode_normal
);
509 FT_Bitmap
*bitmap
= &slot
->bitmap
;
510 unsigned char* buffer
= bitmap
->buffer
;
511 for (int y
= 0; y
< bitmap
->rows
; y
++)
512 for (int x
= 0; x
< bitmap
->width
; x
++)
514 unsigned char alpha
= buffer
[ y
*bitmap
->pitch
+ x
];
515 if (alpha
== 0) continue;
517 int xx
= pen_x
+ slot
->bitmap_left
+ x
;
518 int yy
= pen_y
- slot
->bitmap_top
+ y
;
519 m_alpha
[ yy
* m_area
.width
+ xx
] = alpha
;
522 pen_x
+= slot
->advance
.x
>> 6;
523 pen_y
+= slot
->advance
.y
>> 6;
528 //----------------------------------------------------------------------------
530 //----------------------------------------------------------------------------
532 IMPLEMENT_CLASS(wxCanvas
,wxScrolledWindow
)
534 BEGIN_EVENT_TABLE(wxCanvas
,wxScrolledWindow
)
535 EVT_CHAR( wxCanvas::OnChar
)
536 EVT_PAINT( wxCanvas::OnPaint
)
537 EVT_SIZE( wxCanvas::OnSize
)
538 EVT_IDLE( wxCanvas::OnIdle
)
539 EVT_MOUSE_EVENTS( wxCanvas::OnMouse
)
540 EVT_SET_FOCUS( wxCanvas::OnSetFocus
)
541 EVT_KILL_FOCUS( wxCanvas::OnKillFocus
)
544 wxCanvas::wxCanvas( wxWindow
*parent
, wxWindowID id
,
545 const wxPoint
&position
, const wxSize
& size
, long style
) :
546 wxScrolledWindow( parent
, id
, position
, size
, style
)
548 m_needUpdate
= FALSE
;
549 m_objects
.DeleteContents( TRUE
);
553 m_lastMouse
= (wxCanvasObject
*)NULL
;
554 m_captureMouse
= (wxCanvasObject
*)NULL
;
558 wxCanvas::~wxCanvas()
560 wxNode
*node
= m_updateRects
.First();
563 wxRect
*rect
= (wxRect
*) node
->Data();
565 m_updateRects
.DeleteNode( node
);
566 node
= m_updateRects
.First();
570 void wxCanvas::SetArea( int width
, int height
)
572 m_buffer
= wxImage( width
, height
);
573 SetScrollbars( 10, 10, width
/10, height
/10 );
576 void wxCanvas::SetColour( unsigned char red
, unsigned char green
, unsigned char blue
)
582 if (m_frozen
) return;
584 unsigned char *data
= m_buffer
.GetData();
586 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
587 for (int x
= 0; x
< m_buffer
.GetWidth(); x
++)
598 void wxCanvas::SetCaptureMouse( wxCanvasObject
*obj
)
602 wxWindow::CaptureMouse();
603 m_captureMouse
= obj
;
607 wxWindow::ReleaseMouse();
608 m_captureMouse
= NULL
;
612 void wxCanvas::Freeze()
617 void wxCanvas::Thaw()
619 wxNode
*node
= m_updateRects
.First();
622 wxRect
*rect
= (wxRect
*) node
->Data();
624 m_updateRects
.DeleteNode( node
);
625 node
= m_updateRects
.First();
630 Update( 0, 0, m_buffer
.GetWidth(), m_buffer
.GetHeight() );
633 void wxCanvas::Update( int x
, int y
, int width
, int height
)
635 if (m_frozen
) return;
643 if (width
< 0) return;
650 if (height
< 0) return;
652 if (x
+width
> m_buffer
.GetWidth())
654 width
= m_buffer
.GetWidth() - x
;
656 if (width
< 0) return;
658 if (y
+height
> m_buffer
.GetHeight())
660 height
= m_buffer
.GetHeight() - y
;
662 if (height
< 0) return;
664 // update is within the buffer
667 // has to be blitted to screen later
668 m_updateRects
.Append(
669 (wxObject
*) new wxRect( x
,y
,width
,height
) );
671 // speed up with direct access, maybe add wxImage::Clear(x,y,w,h,r,g,b)
672 for (int yy
= y
; yy
< y
+height
; yy
++)
673 for (int xx
= x
; xx
< x
+width
; xx
++)
674 m_buffer
.SetRGB( xx
, yy
, m_red
, m_green
, m_blue
);
676 // cycle through all objects
677 wxNode
*node
= m_objects
.First();
680 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
682 if (!obj
->IsControl())
684 // If we have 10.000 objects, we will go through
685 // this 10.000 times for each update, so we have
686 // to optimise carefully.
687 int clip_x
= obj
->GetX();
688 int clip_width
= obj
->GetWidth();
691 clip_width
-= x
-clip_x
;
696 if (clip_x
+ clip_width
> x
+ width
)
697 clip_width
= x
+width
-clip_x
;
701 int clip_y
= obj
->GetY();
702 int clip_height
= obj
->GetHeight();
705 clip_height
-= y
-clip_y
;
710 if (clip_y
+ clip_height
> y
+ height
)
711 clip_height
= y
+height
-clip_y
;
714 obj
->Render( clip_x
, clip_y
, clip_width
, clip_height
);
724 void wxCanvas::BlitBuffer( wxDC
&dc
)
726 wxNode
*node
= m_updateRects
.First();
729 wxRect
*rect
= (wxRect
*) node
->Data();
730 wxImage
sub_image( m_buffer
.GetSubImage( *rect
) );
732 // DirectDraw here, please
735 int bpp
= wxDisplayDepth();
738 // the init code is doubled in wxImage
739 static bool s_hasInitialized
= FALSE
;
741 if (!s_hasInitialized
)
744 s_hasInitialized
= TRUE
;
749 CalcScrolledPosition( x
, y
, &x
, &y
);
751 gdk_draw_rgb_image( GTK_PIZZA(m_wxwindow
)->bin_window
,
752 m_wxwindow
->style
->black_gc
,
754 sub_image
.GetWidth(), sub_image
.GetHeight(),
757 sub_image
.GetWidth()*3 );
761 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
762 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
767 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
768 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
772 m_updateRects
.DeleteNode( node
);
773 node
= m_updateRects
.First();
776 m_needUpdate
= FALSE
;
779 void wxCanvas::UpdateNow()
781 if (!m_needUpdate
) return;
783 wxClientDC
dc( this );
789 int wxCanvas::GetDeviceX( double x
)
794 int wxCanvas::GetDeviceY( double y
)
799 int wxCanvas::GetDeviceWidth( double width
)
804 int wxCanvas::GetDeviceHeight( double height
)
809 void wxCanvas::Recreate()
811 wxNode
*node
= m_objects
.First();
814 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
822 void wxCanvas::Prepend( wxCanvasObject
* obj
)
824 m_objects
.Insert( obj
);
826 obj
->SetOwner( this );
829 if (!obj
->IsControl())
830 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
833 void wxCanvas::Append( wxCanvasObject
* obj
)
835 m_objects
.Append( obj
);
837 obj
->SetOwner( this );
840 if (!obj
->IsControl())
841 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
844 void wxCanvas::Insert( size_t before
, wxCanvasObject
* obj
)
846 m_objects
.Insert( before
, obj
);
848 obj
->SetOwner( this );
851 if (!obj
->IsControl())
852 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
855 void wxCanvas::Remove( wxCanvasObject
* obj
)
859 int w
= obj
->GetWidth();
860 int h
= obj
->GetHeight();
861 bool ic
= obj
->IsControl();
863 m_objects
.DeleteObject( obj
);
866 Update( x
, y
, w
, h
);
869 void wxCanvas::OnPaint(wxPaintEvent
&event
)
876 wxRegionIterator
it( GetUpdateRegion() );
881 CalcUnscrolledPosition( x
, y
, &x
, &y
);
883 int w
= it
.GetWidth();
884 int h
= it
.GetHeight();
886 if (x
+w
> m_buffer
.GetWidth())
887 w
= m_buffer
.GetWidth() - x
;
888 if (y
+h
> m_buffer
.GetHeight())
889 h
= m_buffer
.GetHeight() - y
;
891 if ((w
> 0) && (h
> 0))
892 m_updateRects
.Append( (wxObject
*) new wxRect( x
, y
, w
, h
) );
900 void wxCanvas::OnMouse(wxMouseEvent
&event
)
902 int x
= event
.GetX();
903 int y
= event
.GetY();
904 CalcUnscrolledPosition( x
, y
, &x
, &y
);
906 if (event
.GetEventType() == wxEVT_MOTION
)
908 if (m_captureMouse
) //no matter what go to this one
910 wxMouseEvent
child_event( wxEVT_MOTION
);
911 child_event
.SetEventObject(m_captureMouse
);
912 child_event
.m_x
= x
- m_captureMouse
->GetX();
913 child_event
.m_y
= y
- m_captureMouse
->GetY();
914 child_event
.m_leftDown
= event
.m_leftDown
;
915 child_event
.m_rightDown
= event
.m_rightDown
;
916 child_event
.m_middleDown
= event
.m_middleDown
;
917 child_event
.m_controlDown
= event
.m_controlDown
;
918 child_event
.m_shiftDown
= event
.m_shiftDown
;
919 child_event
.m_altDown
= event
.m_altDown
;
920 child_event
.m_metaDown
= event
.m_metaDown
;
921 m_captureMouse
->ProcessEvent( child_event
);
925 wxNode
*node
= m_objects
.Last();
928 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
930 if (!obj
->IsControl())
934 wxMouseEvent
child_event( wxEVT_MOTION
);
935 child_event
.SetEventObject( obj
);
936 child_event
.m_x
= x
- obj
->GetX();
937 child_event
.m_y
= y
- obj
->GetY();
938 child_event
.m_leftDown
= event
.m_leftDown
;
939 child_event
.m_rightDown
= event
.m_rightDown
;
940 child_event
.m_middleDown
= event
.m_middleDown
;
941 child_event
.m_controlDown
= event
.m_controlDown
;
942 child_event
.m_shiftDown
= event
.m_shiftDown
;
943 child_event
.m_altDown
= event
.m_altDown
;
944 child_event
.m_metaDown
= event
.m_metaDown
;
946 if ((obj
!= m_lastMouse
) && (m_lastMouse
!= NULL
))
948 child_event
.SetEventType( wxEVT_LEAVE_WINDOW
);
949 child_event
.SetEventObject( m_lastMouse
);
950 child_event
.m_x
= x
- m_lastMouse
->GetX();
951 child_event
.m_y
= y
- m_lastMouse
->GetY();
952 m_lastMouse
->ProcessEvent( child_event
);
955 child_event
.SetEventType( wxEVT_ENTER_WINDOW
);
956 child_event
.SetEventObject( m_lastMouse
);
957 child_event
.m_x
= x
- m_lastMouse
->GetX();
958 child_event
.m_y
= y
- m_lastMouse
->GetY();
959 m_lastMouse
->ProcessEvent( child_event
);
961 child_event
.SetEventType( wxEVT_MOTION
);
962 child_event
.SetEventObject( obj
);
964 obj
->ProcessEvent( child_event
);
968 node
= node
->Previous();
973 wxMouseEvent
child_event( wxEVT_LEAVE_WINDOW
);
974 child_event
.SetEventObject( m_lastMouse
);
975 child_event
.m_x
= x
- m_lastMouse
->GetX();
976 child_event
.m_y
= y
- m_lastMouse
->GetY();
977 child_event
.m_leftDown
= event
.m_leftDown
;
978 child_event
.m_rightDown
= event
.m_rightDown
;
979 child_event
.m_middleDown
= event
.m_middleDown
;
980 child_event
.m_controlDown
= event
.m_controlDown
;
981 child_event
.m_shiftDown
= event
.m_shiftDown
;
982 child_event
.m_altDown
= event
.m_altDown
;
983 child_event
.m_metaDown
= event
.m_metaDown
;
984 m_lastMouse
->ProcessEvent( child_event
);
986 m_lastMouse
= (wxCanvasObject
*) NULL
;
993 void wxCanvas::OnSize(wxSizeEvent
&event
)
998 void wxCanvas::OnIdle(wxIdleEvent
&event
)
1004 void wxCanvas::OnSetFocus(wxFocusEvent
&event
)
1008 void wxCanvas::OnKillFocus(wxFocusEvent
&event
)
1012 void wxCanvas::OnChar(wxKeyEvent
&event
)
1017 //--------------------------------------------------------------------
1019 //--------------------------------------------------------------------
1021 class wxCanvasModule
: public wxModule
1024 virtual bool OnInit();
1025 virtual void OnExit();
1028 DECLARE_DYNAMIC_CLASS(wxCanvasModule
)
1031 IMPLEMENT_DYNAMIC_CLASS(wxCanvasModule
, wxModule
)
1033 bool wxCanvasModule::OnInit()
1036 int error
= FT_Init_FreeType( &g_freetypeLibrary
);
1037 if (error
) return FALSE
;
1043 void wxCanvasModule::OnExit()
1046 FT_Done_FreeType( g_freetypeLibrary
);