+}
+
+wxCanvasObject* wxCanvasObjectRef::IsHitWorld( double x, double y, double margin )
+{
+ //KKKfirst check if within bbox
+ //will only work if they are always uptodate
+ //if (!m_bbox.PointInBox(x,y,margin))
+ // return (wxCanvasObject*) NULL;
+
+ wxTransformMatrix inverse = lworld;
+ double xh,yh;
+ inverse.Invert();
+ inverse.TransformPoint(x,y,xh,yh);
+
+ if (m_obj->IsHitWorld(xh,yh,margin))
+ return this;
+
+ return (wxCanvasObject*) NULL;
+}
+
+
+
+//----------------------------------------------------------------------------
+// wxCanvasRect
+//----------------------------------------------------------------------------
+
+IMPLEMENT_CLASS(wxCanvasRect, wxCanvasObject)
+
+wxCanvasRect::wxCanvasRect( double x, double y, double w, double h , double radius )
+ : wxCanvasObject()
+{
+ m_x = x;
+ m_y = y;
+ m_width = w;
+ m_height = h;
+ m_radius = radius;
+
+ m_brush = *wxBLACK_BRUSH;
+ m_pen = *wxTRANSPARENT_PEN;
+ CalcBoundingBox();
+}
+
+void wxCanvasRect::TransLate( double x, double y )
+{
+ m_x += x;
+ m_y += y;
+ CalcBoundingBox();
+}
+
+void wxCanvasRect::CalcBoundingBox()
+{
+ m_bbox.SetMin( m_x , m_y);
+ m_bbox.SetMax( m_x + m_width ,m_y + m_height );
+
+ //include the pen width also
+//KKK m_bbox.EnLarge(m_pen.GetWidth()+m_radius);
+ m_bbox.EnLarge(m_pen.GetWidth()/2);
+}
+
+void wxCanvasRect::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, int clip_width, int clip_height )
+{
+ if (!m_visible) return;
+
+ if (cworld->GetRotation())
+ {
+ wxPoint *cpoints = new wxPoint[4];
+ double x;
+ double y;
+ cworld->TransformPoint( m_x, m_y, x, y );
+ cpoints[0].x = m_admin->LogicalToDeviceX(x);
+ cpoints[0].y = m_admin->LogicalToDeviceY(y);
+ cworld->TransformPoint( m_x , m_y + m_height, x, y );
+ cpoints[1].x = m_admin->LogicalToDeviceX(x);
+ cpoints[1].y = m_admin->LogicalToDeviceY(y);
+ cworld->TransformPoint( m_x + m_width, m_y + m_height, x, y );
+ cpoints[2].x = m_admin->LogicalToDeviceX(x);
+ cpoints[2].y = m_admin->LogicalToDeviceY(y);
+ cworld->TransformPoint( m_x + m_width, m_y , x, y );
+ cpoints[3].x = m_admin->LogicalToDeviceX(x);
+ cpoints[3].y = m_admin->LogicalToDeviceY(y);
+
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->SetClippingRegion(clip_x, clip_y, clip_width, clip_height);
+ dc->SetBrush(m_brush);
+ int pw=m_pen.GetWidth();
+ m_pen.SetWidth(m_admin->LogicalToDeviceXRel(pw));
+ dc->SetPen(m_pen);
+ dc->DrawPolygon(4, cpoints, 0,0,wxWINDING_RULE);
+ delete [] cpoints;
+ dc->SetBrush(wxNullBrush);
+ dc->SetPen(wxNullPen);
+ dc->DestroyClippingRegion();
+ m_pen.SetWidth(pw);
+ }
+ else
+ {
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->SetClippingRegion(clip_x, clip_y, clip_width, clip_height);
+ dc->SetBrush(m_brush);
+ int pw=m_pen.GetWidth();
+ m_pen.SetWidth(m_admin->LogicalToDeviceXRel(pw));
+ dc->SetPen(m_pen);
+ //yes the whole not only the clipping region, because we have a pen also
+ int x = m_admin->LogicalToDeviceX(cworld->GetValue(2,0) + m_x );
+ int y = m_admin->LogicalToDeviceY(cworld->GetValue(2,1) + m_y );
+ int w = m_admin->LogicalToDeviceXRel( m_width );
+ int h = m_admin->LogicalToDeviceYRel( m_height );
+ int r = m_admin->LogicalToDeviceYRel( m_radius );
+ if (w > 0 && w < 1) w=1;
+ if (w < 0 && w > -1) w=-1;
+ if (h > 0 && h < 1) h=1;
+ if (h < 0 && h > -1) h=-1;
+ if (m_radius)
+ dc->DrawRoundedRectangle( x,y,w,h,r);
+ else
+ dc->DrawRectangle( x,y,w,h);
+ dc->SetBrush(wxNullBrush);
+ dc->SetPen(wxNullPen);
+ dc->DestroyClippingRegion();
+ m_pen.SetWidth(pw);
+ }
+}
+
+void wxCanvasRect::WriteSVG( wxTextOutputStream &stream )
+{
+}
+
+//----------------------------------------------------------------------------
+// wxCanvasCircle
+//----------------------------------------------------------------------------
+
+wxCanvasCircle::wxCanvasCircle( double x, double y, double radius )
+ : wxCanvasObject()
+{
+ m_x = x;
+ m_y = y;
+ m_radius = radius;
+
+ m_brush = *wxBLACK_BRUSH;
+ m_pen = *wxTRANSPARENT_PEN;
+ CalcBoundingBox();
+}
+
+void wxCanvasCircle::TransLate( double x, double y )
+{
+ m_x += x;
+ m_y += y;
+ CalcBoundingBox();
+}
+
+void wxCanvasCircle::CalcBoundingBox()
+{
+ m_bbox.SetMin( m_x-m_radius , m_y-m_radius );
+ m_bbox.SetMax( m_x+m_radius , m_y+m_radius );
+
+ //include the pen width also
+ m_bbox.EnLarge(m_pen.GetWidth()/2);
+}
+
+void wxCanvasCircle::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, int clip_width, int clip_height )
+{
+ if (!m_visible) return;
+
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->SetClippingRegion(clip_x, clip_y, clip_width, clip_height);
+ dc->SetBrush(m_brush);
+ int pw=m_pen.GetWidth();
+ m_pen.SetWidth(m_admin->LogicalToDeviceXRel(pw));
+ dc->SetPen(m_pen);
+ //yes the whole not only the clipping region, because we have a pen also
+ //and rotation on a circle is not important so only a shift with cworld
+ int x = m_admin->LogicalToDeviceX(cworld->GetValue(2,0) + m_x );
+ int y = m_admin->LogicalToDeviceY(cworld->GetValue(2,1) + m_y );
+ int radius = m_admin->LogicalToDeviceXRel( m_radius );
+ if (radius < 1) radius=1;
+ dc->DrawCircle( x,y,radius);
+ dc->SetBrush(wxNullBrush);
+ dc->SetPen(wxNullPen);
+ dc->DestroyClippingRegion();
+ m_pen.SetWidth(pw);
+}
+
+void wxCanvasCircle::WriteSVG( wxTextOutputStream &stream )
+{
+}
+
+wxCanvasObject* wxCanvasCircle::IsHitWorld( double x, double y, double margin )
+{
+ if ((x >= m_bbox.GetMinX()-margin) &&
+ (x <= m_bbox.GetMaxX()+margin) &&
+ (y >= m_bbox.GetMinY()-margin) &&
+ (y <= m_bbox.GetMaxY()+margin)
+ )
+ {
+ if (m_radius+m_pen.GetWidth()/2+margin > sqrt(pow(m_x-x,2)+pow(m_y-y,2)))
+ return this;
+ else
+ return (wxCanvasObject*) NULL;
+ }
+ return (wxCanvasObject*) NULL;
+}
+
+//----------------------------------------------------------------------------
+// wxCanvasEllipse
+//----------------------------------------------------------------------------
+
+wxCanvasEllipse::wxCanvasEllipse( double x, double y, double width, double height )
+ : wxCanvasObject()
+{
+ m_x = x;
+ m_y = y;
+ m_width = width;
+ m_height = height;
+
+ m_brush = *wxBLACK_BRUSH;
+ m_pen = *wxTRANSPARENT_PEN;
+ CalcBoundingBox();
+}
+
+void wxCanvasEllipse::TransLate( double x, double y )
+{
+ m_x += x;
+ m_y += y;
+ CalcBoundingBox();
+}
+
+void wxCanvasEllipse::CalcBoundingBox()
+{
+ m_bbox.SetMin( m_x, m_y );
+ m_bbox.SetMax( m_x+m_width , m_y+m_height );
+
+ //include the pen width also
+ m_bbox.EnLarge(m_pen.GetWidth()/2);
+}
+
+void wxCanvasEllipse::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, int clip_width, int clip_height )
+{
+ if (!m_visible) return;
+
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->SetClippingRegion(clip_x, clip_y, clip_width, clip_height);
+ dc->SetBrush(m_brush);
+ int pw=m_pen.GetWidth();
+ m_pen.SetWidth(m_admin->LogicalToDeviceXRel(pw));
+ dc->SetPen(m_pen);
+ int x = m_admin->LogicalToDeviceX(cworld->GetValue(2,0) + m_x );
+ int y = m_admin->LogicalToDeviceY(cworld->GetValue(2,1) + m_y );
+ int w = m_admin->LogicalToDeviceXRel( m_width );
+ int h = m_admin->LogicalToDeviceYRel( m_height );
+ if (w > 0 && w < 1) w=1;
+ if (w < 0 && w > -1) w=-1;
+ if (h > 0 && h < 1) h=1;
+ if (h < 0 && h > -1) h=-1;
+ dc->DrawEllipse( x,y,w,h);
+ dc->SetBrush(wxNullBrush);
+ dc->SetPen(wxNullPen);
+ dc->DestroyClippingRegion();
+ m_pen.SetWidth(pw);
+}
+
+void wxCanvasEllipse::WriteSVG( wxTextOutputStream &stream )
+{
+}
+
+wxCanvasObject* wxCanvasEllipse::IsHitWorld( double x, double y, double margin )
+{
+ if ((x >= m_bbox.GetMinX()-margin) &&
+ (x <= m_bbox.GetMaxX()+margin) &&
+ (y >= m_bbox.GetMinY()-margin) &&
+ (y <= m_bbox.GetMaxY()+margin)
+ )
+ {
+ double a=(m_width+m_pen.GetWidth())/2+margin ;
+ double b=(m_height+m_pen.GetWidth())/2+margin;
+ double c=pow((m_x+m_width/2-x)/a,2)+pow((m_y+m_height/2-y)/b,2);
+ if ( 1 > c)
+ return this;
+ else
+ return (wxCanvasObject*) NULL;
+ }
+ return (wxCanvasObject*) NULL;
+}
+
+//----------------------------------------------------------------------------
+// wxCanvasEllipticArc
+//----------------------------------------------------------------------------
+
+wxCanvasEllipticArc::wxCanvasEllipticArc( double x, double y, double width, double height, double start, double end )
+ : wxCanvasObject()
+{
+ m_x = x;
+ m_y = y;
+ m_width = width;
+ m_height = height;
+ m_start = start;
+ m_end = end;
+
+ m_brush = *wxBLACK_BRUSH;
+ m_pen = *wxTRANSPARENT_PEN;
+ CalcBoundingBox();
+}
+
+void wxCanvasEllipticArc::TransLate( double x, double y )
+{
+ m_x += x;
+ m_y += y;
+ CalcBoundingBox();
+}
+
+void wxCanvasEllipticArc::CalcBoundingBox()
+{
+ m_bbox.SetMin( m_x, m_y );
+ m_bbox.SetMax( m_x+m_width , m_y+m_height );
+
+ //include the pen width also
+ m_bbox.EnLarge(m_pen.GetWidth()/2);
+}
+
+void wxCanvasEllipticArc::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, int clip_width, int clip_height )
+{
+ if (!m_visible) return;
+
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->SetClippingRegion(clip_x, clip_y, clip_width, clip_height);
+ dc->SetBrush(m_brush);
+ int pw=m_pen.GetWidth();
+ m_pen.SetWidth(m_admin->LogicalToDeviceXRel(pw));
+ dc->SetPen(m_pen);
+ int x = m_admin->LogicalToDeviceX(cworld->GetValue(2,0) + m_x );
+ int y = m_admin->LogicalToDeviceY(cworld->GetValue(2,1) + m_y );
+ int w = m_admin->LogicalToDeviceXRel( m_width );
+ int h = m_admin->LogicalToDeviceYRel( m_height );
+ if (w > 0 && w < 1) w=1;
+ if (w < 0 && w > -1) w=-1;
+ if (h > 0 && h < 1) h=1;
+ if (h < 0 && h > -1) h=-1;
+ if (m_admin->GetActive()->GetYaxis())
+ dc->DrawEllipticArc( x,y,w,h,-m_end,-m_start);
+ else
+ dc->DrawEllipticArc( x,y,w,h,m_start,m_end);
+ dc->SetBrush(wxNullBrush);
+ dc->SetPen(wxNullPen);
+ dc->DestroyClippingRegion();
+ m_pen.SetWidth(pw);
+}
+
+void wxCanvasEllipticArc::WriteSVG( wxTextOutputStream &stream )
+{
+}
+
+wxCanvasObject* wxCanvasEllipticArc::IsHitWorld( double x, double y, double margin )
+{
+ if ((x >= m_bbox.GetMinX()-margin) &&
+ (x <= m_bbox.GetMaxX()+margin) &&
+ (y >= m_bbox.GetMinY()-margin) &&
+ (y <= m_bbox.GetMaxY()+margin)
+ )
+ {
+ double a=(m_width+m_pen.GetWidth())/2+margin ;
+ double b=(m_height+m_pen.GetWidth())/2+margin;
+ double c=pow((m_x+m_width/2-x)/a,2)+pow((m_y+m_height/2-y)/b,2);
+ if ( 1 > c)
+ return this;
+ else
+ return (wxCanvasObject*) NULL;
+ }
+ return (wxCanvasObject*) NULL;
+}
+
+//----------------------------------------------------------------------------
+// wxCanvasLine
+//----------------------------------------------------------------------------
+
+wxCanvasLine::wxCanvasLine( double x1, double y1, double x2, double y2 )
+ : wxCanvasObject()
+{
+ m_x1 = x1;
+ m_y1 = y1;
+ m_x2 = x2;
+ m_y2 = y2;
+
+ m_pen = *wxBLACK_PEN;
+ CalcBoundingBox();
+}
+
+void wxCanvasLine::TransLate( double x, double y )
+{
+ m_x1 += x;
+ m_y1 += y;
+ m_x2 += x;
+ m_y2 += y;
+ CalcBoundingBox();
+}
+
+void wxCanvasLine::CalcBoundingBox()
+{
+ m_bbox.SetMin( m_x1 , m_y1);
+ m_bbox.SetMax( m_x2 , m_y2);
+
+ //include the pen width also
+ m_bbox.EnLarge(m_pen.GetWidth()/2);
+}
+
+void wxCanvasLine::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, int clip_width, int clip_height )
+{
+ if (!m_visible) return;
+
+ double x1,y1,x2,y2;
+ cworld->TransformPoint( m_x1, m_y1, x1, y1 );
+ cworld->TransformPoint( m_x2, m_y2, x2, y2 );
+ x1 = m_admin->LogicalToDeviceX( x1 );
+ y1 = m_admin->LogicalToDeviceY( y1 );
+ x2 = m_admin->LogicalToDeviceX( x2 );
+ y2 = m_admin->LogicalToDeviceY( y2 );
+
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->SetClippingRegion( clip_x, clip_y, clip_width, clip_height );
+ int pw=m_pen.GetWidth();
+ m_pen.SetWidth(m_admin->LogicalToDeviceXRel(pw));
+ dc->SetPen( m_pen );
+ dc->DrawLine( x1, y1, x2, y2 );
+
+ dc->DestroyClippingRegion();
+ m_pen.SetWidth(pw);
+}
+
+void wxCanvasLine::WriteSVG( wxTextOutputStream &stream )
+{
+ // no idea
+}
+
+wxCanvasObject* wxCanvasLine::IsHitWorld( double x, double y, double margin )
+{
+ if ((x >= m_bbox.GetMinX()-margin) &&
+ (x <= m_bbox.GetMaxX()+margin) &&
+ (y >= m_bbox.GetMinY()-margin) &&
+ (y <= m_bbox.GetMaxY()+margin)
+ )
+ {
+ wxLine line1(m_x1,m_y1,m_x2,m_y2);
+ wxPoint2DDouble P=wxPoint2DDouble(x,y);
+ double distance;
+ if (line1.PointInLine(P,distance,m_pen.GetWidth()/2+margin) == R_IN_AREA)
+ return this;
+ else
+ return (wxCanvasObject*) NULL;
+ }
+ return (wxCanvasObject*) NULL;
+}
+
+//----------------------------------------------------------------------------
+// wxCanvasImage
+//----------------------------------------------------------------------------
+
+wxCanvasImage::wxCanvasImage( const wxImage &image, double x, double y, double w, double h )
+ : wxCanvasObject()
+{
+ m_x = x;
+ m_y = y;
+ m_width = w;
+ m_height = h;
+
+ m_image = image;
+
+ m_orgw = m_image.GetWidth();
+ m_orgh = m_image.GetHeight();
+
+ m_isImage = TRUE;
+ CalcBoundingBox();
+}
+
+void wxCanvasImage::SetPosXY( double x, double y)
+{
+ m_x = x;
+ m_y = y;
+ CalcBoundingBox();
+}
+
+void wxCanvasImage::TransLate( double x, double y )
+{
+ m_x += x;
+ m_y += y;
+ CalcBoundingBox();
+}
+
+void wxCanvasImage::CalcBoundingBox()
+{
+ m_bbox.SetMin( m_x, m_y );
+ m_bbox.SetMax( m_x + m_width, m_y + m_height );
+}
+
+void wxCanvasImage::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, int clip_width, int clip_height )
+{
+ if (!m_visible) return;
+
+ wxRect tmparea;
+
+ tmparea.x = m_admin->LogicalToDeviceXRel( m_bbox.GetMinX());
+ tmparea.y = m_admin->LogicalToDeviceYRel( m_bbox.GetMinY());
+ tmparea.width = m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() );
+ tmparea.height = m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() );
+
+ double x;
+ double y;
+ cworld->TransformPoint( m_x, m_y, x, y );
+ x = m_admin->LogicalToDeviceX(x);
+ y = m_admin->LogicalToDeviceY(y);
+
+
+ // What is this???
+ if ( m_orgw*5 < m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) ||
+ m_orgw/5 > m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) ||
+ m_orgh*5 < m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() ) ||
+ m_orgh/5 > m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() )
+ )
+ {
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->SetClippingRegion(clip_x, clip_y, clip_width, clip_height);
+ dc->SetBrush(*wxTRANSPARENT_BRUSH);
+ dc->SetPen(*wxBLACK_PEN);
+ //yes the whole not only the clipping region, because we have a pen also
+ int x = m_admin->LogicalToDeviceX(cworld->GetValue(2,0) + m_x );
+ int y = m_admin->LogicalToDeviceY(cworld->GetValue(2,1) + m_y );
+ int w = m_admin->LogicalToDeviceXRel( m_width );
+ int h = m_admin->LogicalToDeviceYRel( m_height );
+ if (w < 1) w=1;
+ if (h < 1) h=1;
+ dc->DrawRectangle( x,y,w,h);
+ dc->SetBrush(wxNullBrush);
+ dc->SetPen(wxNullPen);
+ dc->DestroyClippingRegion();
+ return;
+ }
+
+ wxImage tmp;
+ bool is_cashed = FALSE;
+
+ if (m_cImage.Ok() && (m_cW == m_bbox.GetWidth()) && (m_cH == m_bbox.GetHeight()))
+ {
+ // use cached image
+ tmp = m_cImage;
+ is_cashed = TRUE;
+ }
+ else
+ {
+ if ((m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) == m_image.GetWidth()) &&
+ (m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() ) == m_image.GetHeight()))
+ {
+ tmp = m_image;
+ }
+ else
+ {
+ tmp = m_image.Scale( m_admin->LogicalToDeviceXRel( m_bbox.GetWidth()),
+ m_admin->LogicalToDeviceYRel( m_bbox.GetHeight()) );
+ }
+
+ // create cached image
+ m_cImage = tmp;
+ m_cW = tmp.GetWidth();
+ m_cH = tmp.GetHeight();
+ }
+
+// wxPoint centr(m_admin->LogicalToDeviceX(m_x),m_admin->LogicalToDeviceY(m_y));
+ wxPoint centr(0,0);
+
+ wxBitmap bmp;
+
+ if (m_cBitmap.Ok() && is_cashed && (m_cR == cworld->GetRotation()))
+ {
+ bmp = m_cBitmap;
+ }
+ else
+ {
+ if (cworld->GetRotation())
+ tmp = tmp.Rotate(-cworld->GetRotation()/180.0 * pi, centr, TRUE, NULL );
+
+ bmp = wxBitmap(tmp);
+
+ // create cached bitmap
+ m_cBitmap = bmp;
+ m_cR = cworld->GetRotation();
+ }
+
+ wxDC *dc = m_admin->GetActive()->GetDC();
+
+ wxPoint centr2;
+ if (cworld->GetRotation()> 0)
+ {
+ centr2.x= (int) (x+m_height*sin(-cworld->GetRotation()/180.0 * pi));
+ centr2.y= (int) y;
+ }
+ else
+ {
+ centr2.x= (int) x;
+ centr2.y= (int) (y-m_width*sin(-cworld->GetRotation()/180.0 * pi));
+ }
+
+ if (cworld->GetRotation() != 0)
+ {
+ //TODO clipping not right
+ dc->DrawBitmap(bmp,centr2,TRUE );
+// dc->DrawPoint(centr2);
+// dc->DrawPoint(x,y);
+ }
+ else
+ {
+ dc->SetClippingRegion( clip_x, clip_y, clip_width, clip_height );
+ dc->DrawBitmap( bmp, x, y, TRUE );
+ dc->DestroyClippingRegion();
+ }
+}
+
+void wxCanvasImage::WriteSVG( wxTextOutputStream &stream )
+{
+ // no idea
+}
+
+//----------------------------------------------------------------------------
+// wxCanvasCtrl
+//----------------------------------------------------------------------------
+
+wxCanvasControl::wxCanvasControl( wxWindow *control )
+ : wxCanvasObject()
+{
+ m_isControl = TRUE;
+ m_control = control;
+ CalcBoundingBox();
+}
+
+double wxCanvasControl::GetPosX()
+{
+ int x,y ;
+ m_control->GetPosition( &x, &y );
+ return m_admin->DeviceToLogicalX(x);
+}
+
+double wxCanvasControl::GetPosY()
+{
+ int x,y ;
+ m_control->GetPosition( &x, &y );
+ return m_admin->DeviceToLogicalY(y);
+}
+
+void wxCanvasControl::SetPosXY( double x, double y)
+{
+ int xd = m_admin->LogicalToDeviceX(x);
+ int yd = m_admin->LogicalToDeviceY(y);
+ m_control->Move(xd,yd);
+}
+
+
+void wxCanvasControl::TransLate( double x, double y )
+{
+ int xdo,ydo;
+ m_control->GetPosition( &xdo, &ydo );
+ int xd = m_admin->LogicalToDeviceX(x)-xdo;
+ int yd = m_admin->LogicalToDeviceY(y)-ydo;
+ m_control->Move(xd,yd);
+ CalcBoundingBox();
+}
+
+wxCanvasControl::~wxCanvasControl()
+{
+ m_control->Destroy();
+}
+
+void wxCanvasControl::CalcBoundingBox()
+{
+ wxRect tmparea;
+
+ m_control->GetSize( &tmparea.width, &tmparea.height );
+ m_control->GetPosition( &tmparea.x, &tmparea.y );
+
+ m_bbox.SetMin( tmparea.x , tmparea.y);
+ m_bbox.SetMax( tmparea.x + tmparea.width , tmparea.y + tmparea.height);
+
+}
+
+void wxCanvasControl::MoveRelative( double x, double y )
+{
+ m_control->Move( m_admin->LogicalToDeviceX(x), m_admin->LogicalToDeviceX(y) );
+}
+
+//----------------------------------------------------------------------------
+// wxCanvasText
+//----------------------------------------------------------------------------
+
+class wxFaceData
+{
+public:
+#if wxUSE_FREETYPE
+ FT_Face m_face;
+#else
+ void *m_dummy;
+#endif
+};
+
+wxCanvasText::wxCanvasText( const wxString &text, double x, double y, const wxString &fontFile, int size )
+ : wxCanvasObject()
+{
+ m_text = text;
+ m_fontFileName = fontFile;
+ m_size = size;
+
+ m_red = 0;
+ m_green = 0;
+ m_blue = 0;
+
+ m_alpha = NULL;
+
+ m_x = x;
+ m_y = y;
+
+#if wxUSE_FREETYPE
+ wxFaceData *data = new wxFaceData;
+ m_faceData = data;
+
+ int error = FT_New_Face( g_freetypeLibrary,
+ m_fontFileName,
+ 0,
+ &(data->m_face) );
+
+ error = FT_Set_Char_Size( data->m_face,
+ 0,
+ m_size*64,
+ 96, // screen dpi
+ 96 );
+#endif
+ CalcBoundingBox();
+}
+
+wxCanvasText::~wxCanvasText()
+{
+#if wxUSE_FREETYPE
+ wxFaceData *data = (wxFaceData*) m_faceData;
+ delete data;
+#endif
+
+ if (m_alpha) delete [] m_alpha;
+}
+
+void wxCanvasText::SetRGB( unsigned char red, unsigned char green, unsigned char blue )
+{
+ m_red = red;
+ m_green = green;
+ m_blue = blue;
+}
+
+void wxCanvasText::SetFlag( int flag )
+{
+ m_flag = flag;
+}
+
+void wxCanvasText::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, int clip_width, int clip_height )
+{
+ if (!m_visible) return;
+
+ wxRect tmparea;
+ tmparea.x = m_admin->LogicalToDeviceX( m_bbox.GetMinX());
+ tmparea.y = m_admin->LogicalToDeviceY( m_bbox.GetMinY());
+ tmparea.width = m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() );
+ tmparea.height = m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() );
+
+ m_alpha = new unsigned char[tmparea.width*tmparea.height];
+ memset( m_alpha, 0, tmparea.width*tmparea.height );
+
+ if (!m_alpha) return;
+
+#if wxUSE_FREETYPE
+ FT_Face face = ((wxFaceData*)m_faceData)->m_face;
+ FT_GlyphSlot slot = face->glyph;
+ int pen_x = 0;
+ int pen_y = m_size;
+
+ for (int n = 0; n < (int)m_text.Len(); n++)
+ {
+ FT_UInt index = FT_Get_Char_Index( face, m_text[(unsigned int)n] );
+
+ int error = FT_Load_Glyph( face, index, FT_LOAD_DEFAULT );
+ if (error) continue;
+
+ error = FT_Render_Glyph( face->glyph, ft_render_mode_normal );
+ if (error) continue;
+
+ FT_Bitmap *bitmap = &slot->bitmap;
+ unsigned char* buffer = bitmap->buffer;
+ for (int y = 0; y < bitmap->rows; y++)
+ for (int x = 0; x < bitmap->width; x++)
+ {
+ unsigned char alpha = buffer[ y*bitmap->pitch + x ];
+ if (alpha == 0) continue;
+
+ int xx = pen_x + slot->bitmap_left + x;
+ int yy = pen_y - slot->bitmap_top + y;
+ m_alpha[ yy * tmparea.width + xx ] = alpha;
+ }
+
+ pen_x += slot->advance.x >> 6;
+ pen_y += slot->advance.y >> 6;
+ }
+#endif
+
+ wxBitmap *bitmap = m_admin->GetActive()->GetBuffer();
+ wxRect sub_rect( clip_x, clip_y, clip_width, clip_height );
+ wxBitmap sub_bitmap( bitmap->GetSubBitmap( sub_rect ) );
+
+ wxImage image( sub_bitmap.ConvertToImage() );
+
+ // local coordinates
+ int start_x = clip_x - tmparea.x;
+ int end_x = clip_width + start_x;
+ int start_y = clip_y - tmparea.y;
+ int end_y = clip_height + start_y;
+
+ for (int y = start_y; y < end_y; y++)
+ for (int x = start_x; x < end_x; x++)
+ {
+ int alpha = m_alpha[y*tmparea.width + x];
+ if (alpha)
+ {
+ int image_x = x - start_x;
+ int image_y = y - start_y;
+ if (alpha == 255)
+ {
+ image.SetRGB( image_x, image_y, m_red, m_green, m_blue );
+ continue;
+ }
+ int red1 = (m_red * alpha) / 255;
+ int green1 = (m_green * alpha) / 255;
+ int blue1 = (m_blue * alpha) / 255;
+
+ alpha = 255-alpha;
+ int red2 = image.GetRed( image_x, image_y );
+ int green2 = image.GetGreen( image_x, image_y );
+ int blue2 = image.GetBlue( image_x, image_y );
+ red2 = (red2 * alpha) / 255;
+ green2 = (green2 * alpha) / 255;
+ blue2 = (blue2 * alpha) / 255;
+
+ image.SetRGB( image_x, image_y, red1+red2, green1+green2, blue1+blue2 );
+ }
+ }
+
+ sub_bitmap = wxBitmap(image);
+
+ wxDC *dc = m_admin->GetActive()->GetDC();
+ dc->DrawBitmap( sub_bitmap, clip_x, clip_y );
+}
+
+void wxCanvasText::WriteSVG( wxTextOutputStream &stream )
+{
+}
+
+void wxCanvasText::TransLate( double x, double y )
+{
+ m_x += x;
+ m_y += y;
+ CalcBoundingBox();
+}
+
+void wxCanvasText::CalcBoundingBox()
+{
+ if (m_alpha) delete [] m_alpha;
+
+ m_bbox.SetMin( m_x , m_y);
+ m_bbox.SetMax( m_x + 100 , m_y + m_size + (m_size/2));
+
+
+}
+
+//----------------------------------------------------------------------------
+// wxCanvas
+//----------------------------------------------------------------------------
+
+IMPLEMENT_CLASS(wxCanvas,wxScrolledWindow)
+
+BEGIN_EVENT_TABLE(wxCanvas,wxScrolledWindow)
+ EVT_PAINT( wxCanvas::OnPaint )
+ EVT_IDLE( wxCanvas::OnIdle )
+ EVT_SIZE( wxCanvas::OnSize )
+ EVT_MOUSE_EVENTS( wxCanvas::OnMouse )
+ EVT_SET_FOCUS( wxCanvas::OnSetFocus )
+ EVT_KILL_FOCUS( wxCanvas::OnKillFocus )
+ EVT_ERASE_BACKGROUND( wxCanvas::OnEraseBackground )
+END_EVENT_TABLE()
+
+wxCanvas::wxCanvas( wxCanvasAdmin* admin, wxWindow *parent, wxWindowID id,
+ const wxPoint &position, const wxSize& size, long style ) :
+ wxScrolledWindow( parent, id, position, size, style )
+{
+ // These are unused in wxVectorCanvas
+ m_bufferX = 0;
+ m_bufferY = 0;
+
+ m_admin = admin;
+ m_admin->Append( this );
+
+ m_needUpdate = FALSE;
+ m_background = *wxWHITE;
+ m_lastMouse = (wxCanvasObject*)NULL;
+ m_captureMouse = (wxCanvasObject*)NULL;
+ m_frozen = FALSE;
+ m_oldDeviceX = 0;
+ m_oldDeviceY = 0;
+ m_root = (wxCanvasObjectGroup*)NULL;
+}
+
+wxCanvas::~wxCanvas()
+{
+ wxNode *node = m_updateRects.First();
+ while (node)
+ {
+ wxRect *rect = (wxRect*) node->Data();
+ delete rect;
+ m_updateRects.DeleteNode( node );
+ node = m_updateRects.First();
+ }
+}
+
+double wxCanvas::GetMinX() const
+{
+ return 0.0;
+}
+
+double wxCanvas::GetMinY() const
+{
+ return 0.0;
+}
+
+double wxCanvas::GetMaxX() const
+{
+ int width;
+ GetVirtualSize( &width, NULL );
+ return width;
+}
+
+double wxCanvas::GetMaxY() const
+{
+ int height;
+ GetVirtualSize( NULL, &height );
+ return height;
+}
+
+void wxCanvas::SetColour( const wxColour& background )
+{
+ m_background = background;
+ SetBackgroundColour( m_background );
+
+ if (m_frozen) return;
+
+ wxMemoryDC dc;
+ dc.SelectObject( m_buffer );
+ dc.SetPen( *wxTRANSPARENT_PEN );
+ wxBrush brush( m_background, wxSOLID );
+ dc.SetBrush( brush );
+ dc.DrawRectangle( 0, 0, m_buffer.GetWidth(), m_buffer.GetHeight() );
+ dc.SelectObject( wxNullBitmap );
+}
+
+void wxCanvas::SetCaptureMouse( wxCanvasObject *obj )
+{
+ if (obj)
+ {
+ wxWindow::CaptureMouse();
+ m_captureMouse = obj;
+ }
+ else
+ {
+ wxWindow::ReleaseMouse();
+ m_captureMouse = NULL;
+ }
+}
+
+void wxCanvas::Freeze()
+{
+ m_frozen = TRUE;
+}
+
+void wxCanvas::Thaw()
+{
+ wxNode *node = m_updateRects.First();
+ while (node)
+ {
+ wxRect *rect = (wxRect*) node->Data();
+ delete rect;
+ m_updateRects.DeleteNode( node );
+ node = m_updateRects.First();
+ }
+
+ m_frozen = FALSE;
+
+ if (m_buffer.Ok())
+ Update( m_bufferX, m_bufferY, m_buffer.GetWidth(), m_buffer.GetHeight() );
+}
+
+void wxCanvas::Update( int x, int y, int width, int height, bool blit )
+{
+ CalcScrolledPosition( 0, 0, &m_oldDeviceX, &m_oldDeviceY );
+
+ m_admin->SetActive(this);
+
+ if (!m_root) return;
+
+ if (m_frozen) return;
+
+ // clip to buffer
+ if (x < m_bufferX)
+ {
+ width -= m_bufferX-x;
+ x = m_bufferX;
+ }
+ if (width <= 0) return;
+
+ if (y < m_bufferY)
+ {
+ height -= m_bufferY-y;
+ y = m_bufferY;
+ }
+ if (height <= 0) return;
+
+ if (x+width > m_bufferX+m_buffer.GetWidth())
+ {
+ width = m_bufferX+m_buffer.GetWidth() - x;
+ }
+ if (width <= 0) return;
+
+ if (y+height > m_bufferY+m_buffer.GetHeight())
+ {
+ height = m_bufferY+m_buffer.GetHeight() - y;
+ }
+ if (height <= 0) return;
+
+ // update is within the buffer
+ m_needUpdate = TRUE;
+
+ // has to be blitted to screen later
+ if (blit)
+ {
+ m_updateRects.Append(
+ (wxObject*) new wxRect( x,y,width,height ) );
+ }
+
+ wxTransformMatrix cworld;
+
+ wxMemoryDC dc;
+ dc.SelectObject( m_buffer );
+
+ dc.SetPen( *wxTRANSPARENT_PEN );
+ wxBrush brush( m_background, wxSOLID );
+ dc.SetBrush( brush );
+ dc.SetLogicalFunction(wxCOPY);
+
+#if 0
+ if (width != m_buffer.GetWidth() && height != m_buffer.GetHeight())
+ {
+ dc.SetClippingRegion(x,y,width,height);
+ dc.DrawRectangle(x-2,y-2,width+4,height+4);
+ dc.DestroyClippingRegion();
+ }
+ else
+ {
+ dc.Clear();
+ dc.DrawRectangle(0,0,m_buffer.GetWidth(),m_buffer.GetHeight());
+ }
+#else
+ // No idea, what the code up there does.
+ dc.DrawRectangle( x-m_bufferX, y-m_bufferY, width, height );
+#endif
+
+ dc.SetBrush(wxNullBrush);
+ dc.SetPen(wxNullPen);
+
+ dc.SetDeviceOrigin( m_oldDeviceX, m_oldDeviceY );
+ m_renderDC = &dc;
+
+ m_root->Render( &cworld, x, y, width, height );
+
+ m_renderDC = NULL;
+ dc.SelectObject( wxNullBitmap );
+}
+
+void wxCanvas::BlitBuffer( wxDC &dc )
+{
+ wxNode *node = m_updateRects.First();
+ while (node)
+ {
+ wxRect *rect = (wxRect*) node->Data();
+
+ wxMemoryDC mdc;
+ mdc.SelectObject( m_buffer );
+ dc.Blit( rect->x,
+ rect->y,
+ rect->width,
+ rect->height,
+ &mdc,
+ rect->x - m_bufferX,
+ rect->y - m_bufferY );
+ mdc.SelectObject( wxNullBitmap );
+
+ delete rect;
+ m_updateRects.DeleteNode( node );
+ node = m_updateRects.First();
+ }
+
+ m_needUpdate = FALSE;
+}
+
+void wxCanvas::UpdateNow()
+{
+ if (m_frozen) return;
+
+ if (!m_needUpdate) return;
+
+ wxClientDC dc( this );
+ PrepareDC( dc );
+
+ BlitBuffer( dc );
+}
+
+void wxCanvas::OnSize(wxSizeEvent &event)
+{
+ int w,h;
+ GetClientSize( &w, &h );
+ m_buffer = wxBitmap( w, h );
+
+ CalcUnscrolledPosition( 0, 0, &m_bufferX, &m_bufferY );
+
+ wxNode *node = m_updateRects.First();
+ while (node)
+ {
+ wxRect *rect = (wxRect*) node->Data();
+ delete rect;
+ m_updateRects.DeleteNode( node );
+ node = m_updateRects.First();
+ }
+
+ m_frozen = FALSE;
+
+ Update( m_bufferX, m_bufferY, m_buffer.GetWidth(), m_buffer.GetHeight(), FALSE );
+
+ event.Skip();
+}
+
+void wxCanvas::OnPaint(wxPaintEvent &event)
+{
+ wxPaintDC dc(this);
+ PrepareDC( dc );
+
+ if (!m_buffer.Ok()) return;
+
+ if (m_frozen) return;
+
+ m_needUpdate = TRUE;
+
+ wxRegionIterator it( GetUpdateRegion() );
+ while (it)
+ {
+ int x = it.GetX();
+ int y = it.GetY();
+
+ int w = it.GetWidth();
+ int h = it.GetHeight();
+
+ if (x+w > m_buffer.GetWidth())
+ w = m_buffer.GetWidth() - x;
+ if (y+h > m_buffer.GetHeight())
+ h = m_buffer.GetHeight() - y;
+
+ if ((w > 0) && (h > 0))
+ {
+ x += m_bufferX;
+ y += m_bufferY;
+ m_updateRects.Append( (wxObject*) new wxRect( x, y, w, h ) );
+ }
+
+ it++;
+ }
+
+ BlitBuffer( dc );
+}
+
+void wxCanvas::ScrollWindow( int dx, int dy, const wxRect* rect )
+{
+ // If any updates are pending, do them now since they will
+ // expect the previous m_bufferX and m_bufferY as well as
+ // the previous device origin values.
+ wxClientDC dc( this );
+ dc.SetDeviceOrigin( m_oldDeviceX, m_oldDeviceY );
+ BlitBuffer( dc );
+
+ // The buffer always starts at the top left corner of the
+ // client area. Indeed, it is the client area.
+ CalcUnscrolledPosition( 0, 0, &m_bufferX, &m_bufferY );
+
+ // Update everything.
+ Update( m_bufferX, m_bufferY, m_buffer.GetWidth(), m_buffer.GetHeight(), FALSE );
+
+ // Scroll, actually.
+ wxWindow::ScrollWindow( dx, dy, rect );
+}
+
+void wxCanvas::OnMouse(wxMouseEvent &event)
+{
+ m_admin->SetActive(this);
+ if (!m_root)
+ {
+ event.Skip();
+ return;
+ }
+
+ int x = event.GetX();
+ int y = event.GetY();
+
+ //to world coordinates to do hit test in world coordinates
+ double xw = DeviceToLogicalX( x );
+ double yw = DeviceToLogicalY( y );
+
+ //make a select margin of 2 pixels, so also zero line thickness will be hit
+ double margin = DeviceToLogicalXRel( 2 );