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 #define USE_FREETYPE 1
31 #include <freetype/freetype.h>
34 //----------------------------------------------------------------------------
36 //----------------------------------------------------------------------------
39 FT_Library g_freetypeLibrary
;
42 //----------------------------------------------------------------------------
44 //----------------------------------------------------------------------------
46 wxCanvasObject::wxCanvasObject( int x
, int y
, int width
, int height
)
52 m_area
.height
= height
;
58 void wxCanvasObject::Move( int x
, int y
)
68 // TODO: sometimes faster to merge into 1 Update or
69 // to break up into four
70 m_owner
->Update( old_x
, old_y
, m_area
.width
, m_area
.height
);
71 m_owner
->Update( x
, y
, m_area
.width
, m_area
.height
);
75 void wxCanvasObject::WriteSVG( wxTextOutputStream
&stream
)
79 void wxCanvasObject::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
83 //----------------------------------------------------------------------------
85 //----------------------------------------------------------------------------
87 wxCanvasImage::wxCanvasImage( const wxImage
&image
, int x
, int y
)
88 : wxCanvasObject( x
, y
, image
.GetWidth(), image
.GetHeight() )
94 void wxCanvasImage::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
96 int start_x
= wxMax( 0, clip_x
-m_area
.x
);
97 int end_x
= wxMin( m_area
.width
, clip_width
+clip_x
-m_area
.x
);
98 int start_y
= wxMax( 0, clip_y
-m_area
.y
);
99 int end_y
= wxMin( m_area
.height
, clip_height
+clip_y
-m_area
.y
);
101 if (end_x
< start_x
) return;
102 if (end_y
< start_y
) return;
104 if ((start_x
== 0) &&
106 (end_x
== m_area
.width
) &&
107 (end_y
== m_area
.height
))
109 m_owner
->GetBuffer()->Paste( m_image
, m_area
.x
, m_area
.y
);
113 wxRect
rect( start_x
, start_y
, end_x
-start_x
, end_y
-start_y
);
114 wxImage
sub_image( m_image
.GetSubImage( rect
) );
115 m_owner
->GetBuffer()->Paste( sub_image
, m_area
.x
+start_x
, m_area
.y
+start_y
);
119 void wxCanvasImage::WriteSVG( wxTextOutputStream
&stream
)
124 //----------------------------------------------------------------------------
126 //----------------------------------------------------------------------------
128 wxCanvasControl::wxCanvasControl( wxWindow
*control
)
129 : wxCanvasObject( -1, -1, -1, -1 )
135 wxCanvasControl::~wxCanvasControl()
137 m_control
->Destroy();
140 void wxCanvasControl::Move( int x
, int y
)
142 m_control
->Move( x
, y
);
145 void wxCanvasControl::UpdateSize()
147 m_control
->GetSize( &m_area
.width
, &m_area
.height
);
148 m_control
->GetPosition( &m_area
.x
, &m_area
.y
);
151 //----------------------------------------------------------------------------
153 //----------------------------------------------------------------------------
165 wxCanvasText::wxCanvasText( const wxString
&text
, int x
, int y
, const wxString
&fontFile
, int size
)
166 : wxCanvasObject( x
, y
, -1, -1 )
169 m_fontFileName
= fontFile
;
178 m_area
.height
= m_size
;
179 m_alpha
= new unsigned char[100*m_size
];
180 memset( m_alpha
, 0, m_area
.width
*m_area
.height
);
183 wxFaceData
*data
= new wxFaceData
;
186 int error
= FT_New_Face( g_freetypeLibrary
,
191 error
= FT_Set_Char_Size( data
->m_face
,
200 wxCanvasText::~wxCanvasText()
203 wxFaceData
*data
= (wxFaceData
*) m_faceData
;
207 if (m_alpha
) delete [] m_alpha
;
210 void wxCanvasText::SetRGB( unsigned char red
, unsigned char green
, unsigned char blue
)
217 void wxCanvasText::SetFlag( int flag
)
222 void wxCanvasText::Render( int clip_x
, int clip_y
, int clip_width
, int clip_height
)
224 if (!m_alpha
) return;
226 wxImage
*image
= m_owner
->GetBuffer();
228 int start_x
= wxMax( 0, clip_x
-m_area
.x
);
229 int end_x
= wxMin( m_area
.width
, clip_width
+clip_x
-m_area
.x
);
230 int start_y
= wxMax( 0, clip_y
-m_area
.y
);
231 int end_y
= wxMin( m_area
.height
, clip_height
+clip_y
-m_area
.y
);
233 for (int y
= start_y
; y
< end_y
; y
++)
234 for (int x
= start_x
; x
< end_x
; x
++)
236 int alpha
= m_alpha
[y
*m_area
.width
+ x
];
239 int image_x
= m_area
.x
+x
;
240 int image_y
= m_area
.y
+y
;
243 image
->SetRGB( image_x
, image_y
, m_red
, m_green
, m_blue
);
246 int red1
= (m_red
* alpha
) / 255;
247 int green1
= (m_green
* alpha
) / 255;
248 int blue1
= (m_blue
* alpha
) / 255;
251 int red2
= image
->GetRed( image_x
, image_y
);
252 int green2
= image
->GetGreen( image_x
, image_y
);
253 int blue2
= image
->GetBlue( image_x
, image_y
);
254 red2
= (red2
* alpha
) / 255;
255 green2
= (green2
* alpha
) / 255;
256 blue2
= (blue2
* alpha
) / 255;
258 image
->SetRGB( image_x
, image_y
, red1
+red2
, green1
+green2
, blue1
+blue2
);
263 void wxCanvasText::WriteSVG( wxTextOutputStream
&stream
)
267 void wxCanvasText::CreateBuffer()
270 FT_Face face
= ((wxFaceData
*)m_faceData
)->m_face
;
271 FT_GlyphSlot slot
= face
->glyph
;
275 for (int n
= 0; n
< (int)m_text
.Len(); n
++)
277 FT_UInt index
= FT_Get_Char_Index( face
, m_text
[n
] );
279 int error
= FT_Load_Glyph( face
, index
, FT_LOAD_DEFAULT
);
282 error
= FT_Render_Glyph( face
->glyph
, ft_render_mode_normal
);
285 FT_Bitmap
*bitmap
= &slot
->bitmap
;
286 unsigned char* buffer
= bitmap
->buffer
;
287 for (int y
= 0; y
< bitmap
->rows
; y
++)
288 for (int x
= 0; x
< bitmap
->width
; x
++)
290 unsigned char alpha
= buffer
[ y
*bitmap
->pitch
+ x
];
291 if (alpha
== 0) continue;
293 int xx
= pen_x
+ slot
->bitmap_left
+ x
;
294 int yy
= pen_y
- slot
->bitmap_top
+ y
;
295 m_alpha
[ yy
* m_area
.width
+ xx
] = alpha
;
298 pen_x
+= slot
->advance
.x
>> 6;
299 pen_y
+= slot
->advance
.y
>> 6;
304 //----------------------------------------------------------------------------
306 //----------------------------------------------------------------------------
308 IMPLEMENT_CLASS(wxCanvas
,wxScrolledWindow
)
310 BEGIN_EVENT_TABLE(wxCanvas
,wxScrolledWindow
)
311 EVT_CHAR( wxCanvas::OnChar
)
312 EVT_PAINT( wxCanvas::OnPaint
)
313 EVT_SIZE( wxCanvas::OnSize
)
314 EVT_IDLE( wxCanvas::OnIdle
)
315 EVT_MOUSE_EVENTS( wxCanvas::OnMouse
)
316 EVT_SET_FOCUS( wxCanvas::OnSetFocus
)
317 EVT_KILL_FOCUS( wxCanvas::OnKillFocus
)
320 wxCanvas::wxCanvas( wxWindow
*parent
, wxWindowID id
,
321 const wxPoint
&position
, const wxSize
& size
, long style
) :
322 wxScrolledWindow( parent
, id
, position
, size
, style
)
324 m_needUpdate
= FALSE
;
325 m_objects
.DeleteContents( TRUE
);
331 wxCanvas::~wxCanvas()
333 wxNode
*node
= m_updateRects
.First();
336 wxRect
*rect
= (wxRect
*) node
->Data();
338 m_updateRects
.DeleteNode( node
);
339 node
= m_updateRects
.First();
343 void wxCanvas::SetArea( int width
, int height
)
345 m_buffer
= wxImage( width
, height
);
346 SetScrollbars( 10, 10, width
/10, height
/10 );
349 void wxCanvas::SetColour( unsigned char red
, unsigned char green
, unsigned char blue
)
355 unsigned char *data
= m_buffer
.GetData();
357 for (int y
= 0; y
< m_buffer
.GetHeight(); y
++)
358 for (int x
= 0; x
< m_buffer
.GetWidth(); x
++)
369 void wxCanvas::Update( int x
, int y
, int width
, int height
)
373 m_updateRects
.Append(
374 (wxObject
*) new wxRect( x
,y
,width
,height
) );
376 // speed up with direct access, maybe add wxImage::Clear(x,y,w,h)
378 for (yy
= y
; yy
< y
+height
; yy
++)
379 for (xx
= x
; xx
< x
+width
; xx
++)
380 m_buffer
.SetRGB( xx
, yy
, m_red
, m_green
, m_blue
);
382 wxNode
*node
= m_objects
.First();
385 wxCanvasObject
*obj
= (wxCanvasObject
*) node
->Data();
388 ww
= obj
->GetWidth();
389 hh
= obj
->GetHeight();
391 if (!obj
->IsControl()) // calc intersection !
393 obj
->Render( x
, y
, width
, height
);
400 void wxCanvas::BlitBuffer( wxDC
&dc
)
402 wxNode
*node
= m_updateRects
.First();
405 wxRect
*rect
= (wxRect
*) node
->Data();
406 wxImage
sub_image( m_buffer
.GetSubImage( *rect
) );
408 // DirectDraw here, please
411 int bpp
= wxDisplayDepth();
414 // the init code is doubled in wxImage
415 static bool s_hasInitialized
= FALSE
;
417 if (!s_hasInitialized
)
420 s_hasInitialized
= TRUE
;
425 CalcScrolledPosition( x
, y
, &x
, &y
);
427 gdk_draw_rgb_image( GTK_PIZZA(m_wxwindow
)->bin_window
,
428 m_wxwindow
->style
->black_gc
,
430 sub_image
.GetWidth(), sub_image
.GetHeight(),
433 sub_image
.GetWidth()*3 );
437 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
438 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
443 wxBitmap
bitmap( sub_image
.ConvertToBitmap() );
444 dc
.DrawBitmap( bitmap
, rect
->x
, rect
->y
);
448 m_updateRects
.DeleteNode( node
);
449 node
= m_updateRects
.First();
452 m_needUpdate
= FALSE
;
455 void wxCanvas::UpdateNow()
457 if (!m_needUpdate
) return;
459 wxClientDC
dc( this );
465 void wxCanvas::Prepend( wxCanvasObject
* obj
)
467 m_objects
.Insert( obj
);
469 obj
->SetOwner( this );
471 if (!obj
->IsControl())
472 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
475 void wxCanvas::Append( wxCanvasObject
* obj
)
477 m_objects
.Append( obj
);
479 obj
->SetOwner( this );
481 if (!obj
->IsControl())
482 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
485 void wxCanvas::Insert( size_t before
, wxCanvasObject
* obj
)
487 m_objects
.Insert( before
, obj
);
489 obj
->SetOwner( this );
491 if (!obj
->IsControl())
492 Update( obj
->GetX(), obj
->GetY(), obj
->GetWidth(), obj
->GetHeight() );
495 void wxCanvas::Remove( wxCanvasObject
* obj
)
499 int w
= obj
->GetWidth();
500 int h
= obj
->GetHeight();
501 bool ic
= obj
->IsControl();
503 m_objects
.DeleteObject( obj
);
506 Update( x
, y
, w
, h
);
509 void wxCanvas::OnPaint(wxPaintEvent
&event
)
516 wxRegionIterator
it( GetUpdateRegion() );
521 CalcUnscrolledPosition( x
, y
, &x
, &y
);
523 int w
= it
.GetWidth();
524 int h
= it
.GetHeight();
525 if (x
+ w
> m_buffer
.GetWidth())
526 w
= m_buffer
.GetWidth()-x
;
527 if (y
+ h
> m_buffer
.GetHeight())
528 h
= m_buffer
.GetHeight()-y
;
530 m_updateRects
.Append( (wxObject
*) new wxRect( x
, y
, w
, h
) );
538 void wxCanvas::OnMouse(wxMouseEvent
&event
)
540 // Propagate to objects here
543 void wxCanvas::OnSize(wxSizeEvent
&event
)
548 void wxCanvas::OnIdle(wxIdleEvent
&event
)
554 void wxCanvas::OnSetFocus(wxFocusEvent
&event
)
558 void wxCanvas::OnKillFocus(wxFocusEvent
&event
)
562 void wxCanvas::OnChar(wxKeyEvent
&event
)
567 //--------------------------------------------------------------------
569 //--------------------------------------------------------------------
571 class wxCanvasModule
: public wxModule
574 virtual bool OnInit();
575 virtual void OnExit();
578 DECLARE_DYNAMIC_CLASS(wxCanvasModule
)
581 IMPLEMENT_DYNAMIC_CLASS(wxCanvasModule
, wxModule
)
583 bool wxCanvasModule::OnInit()
586 int error
= FT_Init_FreeType( &g_freetypeLibrary
);
587 if (error
) return FALSE
;
593 void wxCanvasModule::OnExit()
596 FT_Done_FreeType( g_freetypeLibrary
);