+ // remove mask again if any
+ if (useMask && mask)
+ {
+ if (is_mono)
+ {
+ gdk_gc_set_clip_mask( m_textGC, NULL );
+ gdk_gc_set_clip_origin( m_textGC, 0, 0 );
+ if (!m_currentClippingRegion.IsNull())
+ gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
+ }
+ else
+ {
+ gdk_gc_set_clip_mask( m_penGC, NULL );
+ gdk_gc_set_clip_origin( m_penGC, 0, 0 );
+ if (!m_currentClippingRegion.IsNull())
+ gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
+ }
+ }
+
+ if (new_mask)
+ gdk_bitmap_unref( new_mask );
+}
+
+bool wxWindowDCImpl::DoBlit( wxCoord xdest, wxCoord ydest,
+ wxCoord width, wxCoord height,
+ wxDC *source,
+ wxCoord xsrc, wxCoord ysrc,
+ wxRasterOperationMode logical_func,
+ bool useMask,
+ wxCoord xsrcMask, wxCoord ysrcMask )
+{
+ wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
+
+ wxCHECK_MSG( source, false, wxT("invalid source dc") );
+
+ if (!m_window) return false;
+
+ // transform the source DC coords to the device ones
+ xsrc = source->LogicalToDeviceX(xsrc);
+ ysrc = source->LogicalToDeviceY(ysrc);
+
+ wxWindowDCImpl *srcDC = wxDynamicCast(source->GetImpl(), wxWindowDCImpl);
+ wxCHECK_MSG( srcDC, false, "source must be a window DC" );
+
+ // FIXME: this cast is not always valid, see the code using m_isMemDC
+ wxMemoryDCImpl *memDC = static_cast<wxMemoryDCImpl *>(srcDC);
+
+ bool use_bitmap_method = false;
+ bool is_mono = false;
+
+ if (xsrcMask == -1 && ysrcMask == -1)
+ {
+ xsrcMask = xsrc;
+ ysrcMask = ysrc;
+ }
+
+ if (srcDC->m_isMemDC)
+ {
+ if (!memDC->m_selected.Ok()) return false;
+
+ is_mono = (memDC->m_selected.GetDepth() == 1);
+
+ // we use the "XCopyArea" way to copy a memory dc into
+ // a different window if the memory dc BOTH
+ // a) doesn't have any mask or its mask isn't used
+ // b) it is clipped
+ // c) is not 1-bit
+
+ if (useMask && (memDC->m_selected.GetMask()))
+ {
+ // we HAVE TO use the direct way for memory dcs
+ // that have mask since the XCopyArea doesn't know
+ // about masks
+ use_bitmap_method = true;
+ }
+ else if (is_mono)
+ {
+ // we HAVE TO use the direct way for memory dcs
+ // that are bitmaps because XCopyArea doesn't cope
+ // with different bit depths
+ use_bitmap_method = true;
+ }
+ else if ((xsrc == 0) && (ysrc == 0) &&
+ (width == memDC->m_selected.GetWidth()) &&
+ (height == memDC->m_selected.GetHeight()))
+ {
+ // we SHOULD use the direct way if all of the bitmap
+ // in the memory dc is copied in which case XCopyArea
+ // wouldn't be able able to boost performace by reducing
+ // the area to be scaled
+ use_bitmap_method = true;
+ }
+ else
+ {
+ use_bitmap_method = false;
+ }
+ }
+
+ CalcBoundingBox( xdest, ydest );
+ CalcBoundingBox( xdest + width, ydest + height );
+
+ // scale/translate size and position
+ wxCoord xx = XLOG2DEV(xdest);
+ wxCoord yy = YLOG2DEV(ydest);
+
+ wxCoord ww = XLOG2DEVREL(width);
+ wxCoord hh = YLOG2DEVREL(height);
+
+ // compare to current clipping region
+ if (!m_currentClippingRegion.IsNull())
+ {
+ wxRegion tmp( xx,yy,ww,hh );
+ tmp.Intersect( m_currentClippingRegion );
+ if (tmp.IsEmpty())
+ return true;
+ }
+
+ wxRasterOperationMode old_logical_func = m_logicalFunction;
+ SetLogicalFunction( logical_func );
+
+ if (use_bitmap_method)
+ {
+ // scale/translate bitmap size
+ wxCoord bm_width = memDC->m_selected.GetWidth();
+ wxCoord bm_height = memDC->m_selected.GetHeight();
+
+ // Get clip coords for the bitmap. If we don't
+ // use wxBitmap::Rescale(), which can clip the
+ // bitmap, these are the same as the original
+ // coordinates
+ wxCoord cx = xx;
+ wxCoord cy = yy;
+ wxCoord cw = ww;
+ wxCoord ch = hh;
+
+ // interpret userscale of src too
+ double xsc,ysc;
+ memDC->GetUserScale(&xsc,&ysc);
+ bm_width = (int) (bm_width / xsc);
+ bm_height = (int) (bm_height / ysc);
+
+ wxCoord bm_ww = XLOG2DEVREL( bm_width );
+ wxCoord bm_hh = YLOG2DEVREL( bm_height );
+
+ // Scale bitmap if required
+ wxBitmap use_bitmap;
+ if ((memDC->m_selected.GetWidth()!= bm_ww) || ( memDC->m_selected.GetHeight()!= bm_hh))
+ {
+ // This indicates that the blitting code below will get
+ // a clipped bitmap and therefore needs to move the origin
+ // accordingly
+ wxRegion tmp( xx,yy,ww,hh );
+ tmp.Intersect( m_currentClippingRegion );
+ tmp.GetBox(cx,cy,cw,ch);
+
+ // Scale and clipped bitmap
+ use_bitmap = memDC->m_selected.Rescale(cx-xx,cy-yy,cw,ch,bm_ww,bm_hh);
+ }
+ else
+ {
+ // Don't scale bitmap
+ use_bitmap = memDC->m_selected;
+ }
+
+ // apply mask if any
+ GdkBitmap *mask = NULL;
+ if (use_bitmap.GetMask()) mask = use_bitmap.GetMask()->GetBitmap();
+
+ GdkBitmap *new_mask = NULL;
+
+ if (useMask && mask)
+ {
+ if (!m_currentClippingRegion.IsNull())
+ {
+ GdkColor col;
+ new_mask = gdk_pixmap_new( wxGetRootWindow()->window, bm_ww, bm_hh, 1 );
+ GdkGC *gc = gdk_gc_new( new_mask );
+ col.pixel = 0;
+ gdk_gc_set_foreground( gc, &col );
+ gdk_gc_set_ts_origin( gc, -xsrcMask, -ysrcMask);
+ gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, bm_ww, bm_hh );
+ col.pixel = 0;
+ gdk_gc_set_background( gc, &col );
+ col.pixel = 1;
+ gdk_gc_set_foreground( gc, &col );
+ gdk_gc_set_clip_region( gc, m_currentClippingRegion.GetRegion() );
+ // was: gdk_gc_set_clip_origin( gc, -xx, -yy );
+ gdk_gc_set_clip_origin( gc, -cx, -cy );
+ gdk_gc_set_fill( gc, GDK_OPAQUE_STIPPLED );
+ gdk_gc_set_stipple( gc, mask );
+ gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, bm_ww, bm_hh );
+ gdk_gc_unref( gc );
+ }
+
+ if (is_mono)
+ {
+ if (new_mask)
+ {
+ gdk_gc_set_clip_mask( m_textGC, new_mask );
+ gdk_gc_set_clip_origin( m_textGC, cx, cy );
+ }
+ else
+ {
+ gdk_gc_set_clip_mask( m_textGC, mask );
+ gdk_gc_set_clip_origin( m_textGC, cx-xsrcMask, cy-ysrcMask );
+ }
+ }
+ else
+ {
+ if (new_mask)
+ {
+ gdk_gc_set_clip_mask( m_penGC, new_mask );
+ gdk_gc_set_clip_origin( m_penGC, cx, cy );
+ }
+ else
+ {
+ gdk_gc_set_clip_mask( m_penGC, mask );
+ gdk_gc_set_clip_origin( m_penGC, cx-xsrcMask, cy-ysrcMask );
+ }
+ }
+ }
+
+ // Draw XPixmap or XBitmap, depending on what the wxBitmap contains. For
+ // drawing a mono-bitmap (XBitmap) we use the current text GC
+
+ if (is_mono)
+ {
+ // was: gdk_wx_draw_bitmap( m_window, m_textGC, use_bitmap.GetBitmap(), xsrc, ysrc, xx, yy, ww, hh );
+ gdk_wx_draw_bitmap( m_window, m_textGC, use_bitmap.GetBitmap(), xsrc, ysrc, cx, cy, cw, ch );
+ }
+ else
+ {
+ // was: gdk_draw_pixmap( m_window, m_penGC, use_bitmap.GetPixmap(), xsrc, ysrc, xx, yy, ww, hh );
+ gdk_draw_pixmap( m_window, m_penGC, use_bitmap.GetPixmap(), xsrc, ysrc, cx, cy, cw, ch );
+ }
+
+ // remove mask again if any
+ if (useMask && mask)
+ {
+ if (is_mono)
+ {
+ gdk_gc_set_clip_mask( m_textGC, NULL );
+ gdk_gc_set_clip_origin( m_textGC, 0, 0 );
+ if (!m_currentClippingRegion.IsNull())
+ gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
+ }
+ else
+ {
+ gdk_gc_set_clip_mask( m_penGC, NULL );
+ gdk_gc_set_clip_origin( m_penGC, 0, 0 );
+ if (!m_currentClippingRegion.IsNull())
+ gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
+ }
+ }
+
+ if (new_mask)
+ gdk_bitmap_unref( new_mask );
+ }
+ else // use_bitmap_method
+ {
+ if ((width != ww) || (height != hh))
+ {
+ // get clip coords
+ wxRegion tmp( xx,yy,ww,hh );
+ tmp.Intersect( m_currentClippingRegion );
+ wxCoord cx,cy,cw,ch;
+ tmp.GetBox(cx,cy,cw,ch);
+
+ // rescale bitmap
+ wxBitmap bitmap = memDC->m_selected.Rescale( cx-xx, cy-yy, cw, ch, ww, hh );
+
+ // draw scaled bitmap
+ // was: gdk_draw_pixmap( m_window, m_penGC, bitmap.GetPixmap(), 0, 0, xx, yy, -1, -1 );
+ gdk_draw_pixmap( m_window, m_penGC, bitmap.GetPixmap(), 0, 0, cx, cy, -1, -1 );
+ }
+ else
+ {
+ // No scaling and not a memory dc with a mask either
+
+ // copy including child window contents
+ gdk_gc_set_subwindow( m_penGC, GDK_INCLUDE_INFERIORS );
+ gdk_window_copy_area( m_window, m_penGC, xx, yy,
+ srcDC->GetWindow(),
+ xsrc, ysrc, width, height );
+ gdk_gc_set_subwindow( m_penGC, GDK_CLIP_BY_CHILDREN );
+ }
+ }
+
+ SetLogicalFunction( old_logical_func );
+
+ return true;
+}
+
+void wxWindowDCImpl::DoDrawText( const wxString &text, wxCoord x, wxCoord y )
+{
+ wxCHECK_RET( IsOk(), wxT("invalid window dc") );
+
+ if (!m_window) return;
+
+ if (text.empty()) return;
+
+ GdkFont *font = m_font.GetInternalFont( m_scaleY );
+
+ wxCHECK_RET( font, wxT("invalid font") );
+
+ x = XLOG2DEV(x);
+ y = YLOG2DEV(y);
+
+ wxCoord width = gdk_string_width( font, text.mbc_str() );
+ wxCoord height = font->ascent + font->descent;
+
+ if ( m_backgroundMode == wxSOLID )
+ {
+ gdk_gc_set_foreground( m_textGC, m_textBackgroundColour.GetColor() );
+ gdk_draw_rectangle( m_window, m_textGC, TRUE, x, y, width, height );
+ gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
+ }
+ gdk_draw_string( m_window, font, m_textGC, x, y + font->ascent, text.mbc_str() );
+
+ /* CMB 17/7/98: simple underline: ignores scaling and underlying
+ X font's XA_UNDERLINE_POSITION and XA_UNDERLINE_THICKNESS
+ properties (see wxXt implementation) */
+ if (m_font.GetUnderlined())
+ {
+ wxCoord ul_y = y + font->ascent;
+ if (font->descent > 0) ul_y++;
+ gdk_draw_line( m_window, m_textGC, x, ul_y, x + width, ul_y);
+ }
+
+ width = wxCoord(width / m_scaleX);
+ height = wxCoord(height / m_scaleY);
+ CalcBoundingBox (x + width, y + height);
+ CalcBoundingBox (x, y);
+}
+
+
+// TODO: There is an example of rotating text with GTK2 that would probably be
+// a better approach here:
+// http://www.daa.com.au/pipermail/pygtk/2003-April/005052.html
+
+void wxWindowDCImpl::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, double angle )
+{
+ if ( wxIsNullDouble(angle) )
+ {
+ DoDrawText(text, x, y);
+ return;
+ }
+
+ wxCHECK_RET( IsOk(), wxT("invalid window dc") );
+
+ if (!m_window) return;
+
+ wxCoord w;
+ wxCoord h;
+
+ GdkFont *font = m_font.GetInternalFont( m_scaleY );
+
+ wxCHECK_RET( font, wxT("invalid font") );
+
+ // the size of the text
+ w = gdk_string_width( font, text.mbc_str() );
+ h = font->ascent + font->descent;
+
+ // draw the string normally
+ wxBitmap src(w, h);
+ wxMemoryDC dc;
+ dc.SelectObject(src);
+ dc.SetFont(GetFont());
+ dc.SetBackground(*wxBLACK_BRUSH);
+ dc.SetBrush(*wxBLACK_BRUSH);
+ dc.Clear();
+ dc.SetTextForeground( *wxWHITE );
+ dc.DrawText(text, 0, 0);
+ dc.SelectObject(wxNullBitmap);
+
+ // Calculate the size of the rotated bounding box.
+ double rad = DegToRad(angle);
+ double dx = cos(rad),
+ dy = sin(rad);
+
+ // the rectngle vertices are counted clockwise with the first one being at
+ // (0, 0) (or, rather, at (x, y))
+ double x2 = w*dx,
+ y2 = -w*dy; // y axis points to the bottom, hence minus
+ double x4 = h*dy,
+ y4 = h*dx;
+ double x3 = x4 + x2,
+ y3 = y4 + y2;
+
+ // calc max and min
+ wxCoord maxX = (wxCoord)(dmax(x2, dmax(x3, x4)) + 0.5),
+ maxY = (wxCoord)(dmax(y2, dmax(y3, y4)) + 0.5),
+ minX = (wxCoord)(dmin(x2, dmin(x3, x4)) - 0.5),
+ minY = (wxCoord)(dmin(y2, dmin(y3, y4)) - 0.5);
+
+
+ wxImage image = src.ConvertToImage();
+
+ image.ConvertColourToAlpha( m_textForegroundColour.Red(),
+ m_textForegroundColour.Green(),
+ m_textForegroundColour.Blue() );
+ image = image.Rotate( rad, wxPoint(0,0) );
+
+ int i_angle = (int) angle;
+ i_angle = i_angle % 360;
+ if (i_angle < 0)
+ i_angle += 360;
+ int xoffset = 0;
+ if ((i_angle >= 90.0) && (i_angle < 270.0))
+ xoffset = image.GetWidth();
+ int yoffset = 0;
+ if ((i_angle >= 0.0) && (i_angle < 180.0))
+ yoffset = image.GetHeight();
+
+ if ((i_angle >= 0) && (i_angle < 90))
+ yoffset -= (int)( cos(rad)*h );
+ if ((i_angle >= 90) && (i_angle < 180))
+ xoffset -= (int)( sin(rad)*h );
+ if ((i_angle >= 180) && (i_angle < 270))
+ yoffset -= (int)( cos(rad)*h );
+ if ((i_angle >= 270) && (i_angle < 360))
+ xoffset -= (int)( sin(rad)*h );
+
+ int i_x = x - xoffset;
+ int i_y = y - yoffset;
+
+ src = image;
+ DoDrawBitmap( src, i_x, i_y, true );
+
+
+ // it would be better to draw with non underlined font and draw the line
+ // manually here (it would be more straight...)
+#if 0
+ if ( m_font.GetUnderlined() )
+ {
+ gdk_draw_line( m_window, m_textGC,
+ XLOG2DEV(x + x4), YLOG2DEV(y + y4 + font->descent),
+ XLOG2DEV(x + x3), YLOG2DEV(y + y3 + font->descent));
+ }
+#endif // 0
+
+ // update the bounding box
+ CalcBoundingBox(x + minX, y + minY);
+ CalcBoundingBox(x + maxX, y + maxY);
+}
+
+void wxWindowDCImpl::DoGetTextExtent(const wxString &string,
+ wxCoord *width, wxCoord *height,
+ wxCoord *descent, wxCoord *externalLeading,
+ const wxFont *theFont) const
+{
+ if ( width )
+ *width = 0;
+ if ( height )
+ *height = 0;
+ if ( descent )
+ *descent = 0;
+ if ( externalLeading )
+ *externalLeading = 0;
+
+ if (string.empty())
+ {
+ return;
+ }
+
+ wxFont fontToUse = m_font;
+ if (theFont)
+ fontToUse = *theFont;
+
+ GdkFont *font = fontToUse.GetInternalFont( m_scaleY );
+ if ( !font )
+ return;
+
+ if (width)
+ *width = wxCoord(gdk_string_width( font, string.mbc_str() ) / m_scaleX);
+ if (height)
+ *height = wxCoord((font->ascent + font->descent) / m_scaleY);
+ if (descent)
+ *descent = wxCoord(font->descent / m_scaleY);
+}
+
+wxCoord wxWindowDCImpl::GetCharWidth() const
+{
+ GdkFont *font = m_font.GetInternalFont( m_scaleY );
+ wxCHECK_MSG( font, -1, wxT("invalid font") );
+
+ return wxCoord(gdk_string_width( font, "H" ) / m_scaleX);
+}
+
+wxCoord wxWindowDCImpl::GetCharHeight() const
+{
+ GdkFont *font = m_font.GetInternalFont( m_scaleY );
+ wxCHECK_MSG( font, -1, wxT("invalid font") );
+
+ return wxCoord((font->ascent + font->descent) / m_scaleY);
+}
+
+void wxWindowDCImpl::Clear()
+{
+ wxCHECK_RET( IsOk(), wxT("invalid window dc") );
+
+ if (!m_window) return;
+
+ // VZ: the code below results in infinite recursion and crashes when
+ // dc.Clear() is done from OnPaint() so I disable it for now.
+ // I don't know what the correct fix is but Clear() surely should not
+ // reenter OnPaint()!
+#if 0
+ /* - we either are a memory dc or have a window as the
+ owner. anything else shouldn't happen.
+ - we don't use gdk_window_clear() as we don't set
+ the window's background colour anymore. it is too
+ much pain to keep the DC's and the window's back-
+ ground colour in synch. */
+
+ if (m_owner)
+ {
+ m_owner->Clear();
+ return;
+ }
+
+ if (m_isMemDC)
+ {
+ int width,height;
+ GetSize( &width, &height );
+ gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
+ return;
+ }
+#else // 1
+ int width,height;