X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/da048e3d4c244a0d0b42e6e6970ad895ea948a53..5dd26b083956439f0147c134e9365c222fba3726:/src/gtk/dcclient.cpp diff --git a/src/gtk/dcclient.cpp b/src/gtk/dcclient.cpp index fd0105435a..be232b8848 100644 --- a/src/gtk/dcclient.cpp +++ b/src/gtk/dcclient.cpp @@ -15,10 +15,10 @@ #include "wx/dcmemory.h" #include "wx/image.h" #include "wx/gtk/win_gtk.h" -#include // for floating-point functions -#include "gdk/gdk.h" -#include "gtk/gtk.h" +#include // for floating-point functions +#include +#include //----------------------------------------------------------------------------- // local data @@ -39,7 +39,16 @@ static GdkPixmap **hatch_bitmap = (GdkPixmap **) NULL; // constants //----------------------------------------------------------------------------- -#define RAD2DEG 57.2957795131 +const double RAD2DEG = 180.0 / M_PI; + +// ---------------------------------------------------------------------------- +// private functions +// ---------------------------------------------------------------------------- + +static inline double dmax(double a, double b) { return a > b ? a : b; } +static inline double dmin(double a, double b) { return a < b ? a : b; } + +static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } //----------------------------------------------------------------------------- // temporary implementation of the missing GDK function @@ -491,6 +500,8 @@ void wxWindowDC::DoDrawBitmap( const wxBitmap &bitmap, wxCHECK_RET( bitmap.Ok(), wxT("invalid bitmap") ); + bool is_mono = (bitmap.GetBitmap()); + /* scale/translate size and position */ int xx = XLOG2DEV(x); @@ -514,9 +525,11 @@ void wxWindowDC::DoDrawBitmap( const wxBitmap &bitmap, if ((w != ww) || (h != hh)) { wxImage image( bitmap ); - image = image.Scale( ww, hh ); - - use_bitmap = image.ConvertToBitmap(); + image.Rescale( ww, hh ); + if (is_mono) + use_bitmap = image.ConvertToMonoBitmap(255,255,255); + else + use_bitmap = image.ConvertToBitmap(); } else { @@ -530,32 +543,39 @@ void wxWindowDC::DoDrawBitmap( const wxBitmap &bitmap, if (useMask && mask) { - gdk_gc_set_clip_mask( m_penGC, mask ); - gdk_gc_set_clip_origin( m_penGC, xx, yy ); - } - - /* draw XPixmap or XBitmap, depending on what the wxBitmap contains */ - - GdkPixmap *pm = use_bitmap.GetPixmap(); - if (pm) - { - gdk_draw_pixmap( m_window, m_penGC, pm, 0, 0, xx, yy, -1, -1 ); - } - else - { - GdkBitmap *bm = use_bitmap.GetBitmap(); - if (bm) + if (is_mono) { - gdk_draw_bitmap( m_window, m_penGC, bm, 0, 0, xx, yy, -1, -1 ); + gdk_gc_set_clip_mask( m_textGC, mask ); + gdk_gc_set_clip_origin( m_textGC, xx, yy ); + } + else + { + gdk_gc_set_clip_mask( m_penGC, mask ); + gdk_gc_set_clip_origin( m_penGC, xx, yy ); } } + /* 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) + gdk_draw_bitmap( m_window, m_textGC, use_bitmap.GetBitmap(), 0, 0, xx, yy, -1, -1 ); + else + gdk_draw_pixmap( m_window, m_penGC, use_bitmap.GetPixmap(), 0, 0, xx, yy, -1, -1 ); + /* remove mask again if any */ if (useMask && mask) { - gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL ); - gdk_gc_set_clip_origin( m_penGC, 0, 0 ); + if (is_mono) + { + gdk_gc_set_clip_mask( m_textGC, (GdkBitmap *) NULL ); + gdk_gc_set_clip_origin( m_textGC, 0, 0 ); + } + else + { + gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL ); + gdk_gc_set_clip_origin( m_penGC, 0, 0 ); + } } } @@ -578,6 +598,7 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord he wxMemoryDC *memDC = (wxMemoryDC*)source; bool use_bitmap_method = FALSE; + bool is_mono = FALSE; if (srcDC->m_isMemDC) { @@ -601,6 +622,7 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord he /* we HAVE TO use the direct way for memory dcs that are bitmaps because XCopyArea doesn't cope with different bit depths */ + is_mono = TRUE; use_bitmap_method = TRUE; } else if ((xsrc == 0) && (ysrc == 0) && @@ -644,7 +666,10 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord he wxImage image( memDC->m_selected ); image = image.Scale( bm_ww, bm_hh ); - use_bitmap = image.ConvertToBitmap(); + if (is_mono) + use_bitmap = image.ConvertToMonoBitmap(255,255,255); + else + use_bitmap = image.ConvertToBitmap(); } else { @@ -666,34 +691,39 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord he if (useMask && mask) { - gdk_gc_set_clip_mask( m_penGC, mask ); - gdk_gc_set_clip_origin( m_penGC, xx, yy ); - } - - /* draw XPixmap or XBitmap, depending on what the wxBitmap contains */ - - GdkPixmap *pm = use_bitmap.GetPixmap(); - if (pm) - { - gdk_draw_pixmap( m_window, m_penGC, pm, xsrc, ysrc, xx, yy, ww, hh ); - } - else - { - GdkBitmap *bm = use_bitmap.GetBitmap(); - if (bm) + if (is_mono) { - /* we use the textGC here because blitting a bitmap is done - using the current text colour */ - gdk_draw_bitmap( m_window, m_textGC, bm, xsrc, ysrc, xx, yy, ww, hh ); + gdk_gc_set_clip_mask( m_textGC, mask ); + gdk_gc_set_clip_origin( m_textGC, xx, yy ); + } + else + { + gdk_gc_set_clip_mask( m_penGC, mask ); + gdk_gc_set_clip_origin( m_penGC, xx, yy ); } } + /* 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) + gdk_draw_bitmap( m_window, m_textGC, use_bitmap.GetBitmap(), xsrc, ysrc, xx, yy, ww, hh ); + else + gdk_draw_pixmap( m_window, m_penGC, use_bitmap.GetPixmap(), xsrc, ysrc, xx, yy, ww, hh ); + /* remove mask again if any */ if (useMask && mask) { - gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL ); - gdk_gc_set_clip_origin( m_penGC, 0, 0 ); + if (is_mono) + { + gdk_gc_set_clip_mask( m_textGC, (GdkBitmap *) NULL ); + gdk_gc_set_clip_origin( m_textGC, 0, 0 ); + } + else + { + gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL ); + gdk_gc_set_clip_origin( m_penGC, 0, 0 ); + } } } else /* use_bitmap_method */ @@ -795,6 +825,119 @@ void wxWindowDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y ) CalcBoundingBox (x, y); } +void wxWindowDC::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, double angle ) +{ + if (angle == 0.0) + { + DrawText(text, x, y); + return; + } + + wxCHECK_RET( Ok(), wxT("invalid window dc") ); + + if (!m_window) return; + + GdkFont *font = m_font.GetInternalFont( m_scaleY ); + + wxCHECK_RET( font, wxT("invalid font") ); + + // the size of the text + wxCoord w = gdk_string_width( font, text.mbc_str() ); + wxCoord h = font->ascent + font->descent; + + // draw the string normally + wxBitmap src(w, h); + wxMemoryDC dc; + dc.SelectObject(src); + dc.SetFont(GetFont()); + dc.SetBackground(*wxWHITE_BRUSH); + dc.SetBrush(*wxBLACK_BRUSH); + dc.Clear(); + dc.DrawText(text, 0, 0); + dc.SetFont(wxNullFont); + 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); + + // prepare to blit-with-rotate the bitmap to the DC + wxImage image(src); + + GdkColor *colText = m_textForegroundColour.GetColor(), + *colBack = m_textBackgroundColour.GetColor(); + + bool textColSet = TRUE; + + unsigned char *data = image.GetData(); + + // paint pixel by pixel + for ( wxCoord srcX = 0; srcX < w; srcX++ ) + { + for ( wxCoord srcY = 0; srcY < h; srcY++ ) + { + // transform source coords to dest coords + double r = sqrt(srcX*srcX + srcY*srcY); + double angleOrig = atan2(srcY, srcX) - rad; + wxCoord dstX = (wxCoord)(r*cos(angleOrig) + 0.5), + dstY = (wxCoord)(r*sin(angleOrig) + 0.5); + + // black pixel? + bool textPixel = data[(srcY*w + srcX)*3] == 0; + if ( textPixel || (m_backgroundMode == wxSOLID) ) + { + // change colour if needed + if ( textPixel != textColSet ) + { + gdk_gc_set_foreground( m_textGC, textPixel ? colText + : colBack ); + + textColSet = textPixel; + } + + // don't use DrawPoint() because it uses the current pen + // colour, and we don't need it here + gdk_draw_point( m_window, m_textGC, + XLOG2DEV(x + dstX), YLOG2DEV(y + dstY) ); + } + } + } + + // 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 + + // restore the font colour + gdk_gc_set_foreground( m_textGC, colText ); + + // update the bounding box + CalcBoundingBox(x + minX, y + minY); + CalcBoundingBox(x + maxX, y + maxY); +} + void wxWindowDC::DoGetTextExtent(const wxString &string, wxCoord *width, wxCoord *height, wxCoord *descent, wxCoord *externalLeading, @@ -813,12 +956,16 @@ void wxWindowDC::DoGetTextExtent(const wxString &string, wxCoord wxWindowDC::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 wxWindowDC::GetCharHeight() const { GdkFont *font = m_font.GetInternalFont( m_scaleY ); + wxCHECK_MSG( font, -1, wxT("invalid font") ); + return wxCoord((font->ascent + font->descent) / m_scaleY); }