X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7ec2881ac5954bad751dbaceedf91fb050f7cee5..8b089c5e8c8a2140258d150ca1ce42afd3b79e1b:/src/gtk1/dcclient.cpp diff --git a/src/gtk1/dcclient.cpp b/src/gtk1/dcclient.cpp index 7adf096ae7..84ad260e39 100644 --- a/src/gtk1/dcclient.cpp +++ b/src/gtk1/dcclient.cpp @@ -1,10 +1,10 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: dcclient.cpp +// Name: gtk/dcclient.cpp // Purpose: // Author: Robert Roebling // RCS-ID: $Id$ // Copyright: (c) 1998 Robert Roebling, Markus Holzem, Chris Breeze -// Licence: wxWindows licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ @@ -14,12 +14,21 @@ #include "wx/dcclient.h" #include "wx/dcmemory.h" #include "wx/image.h" +#include "wx/module.h" + #include "wx/gtk/win_gtk.h" #include // for floating-point functions + #include #include +//----------------------------------------------------------------------------- +// local defines +//----------------------------------------------------------------------------- + +#define USE_PAINT_REGION 0 + //----------------------------------------------------------------------------- // local data //----------------------------------------------------------------------------- @@ -35,12 +44,23 @@ static GdkPixmap *hatches[num_hatches]; static GdkPixmap **hatch_bitmap = (GdkPixmap **) NULL; +extern GtkWidget *wxRootWindow; + //----------------------------------------------------------------------------- // constants //----------------------------------------------------------------------------- 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 //----------------------------------------------------------------------------- @@ -85,24 +105,82 @@ void gdk_draw_bitmap (GdkDrawable *drawable, 1 ); } -/* - * compare two doubles and return the larger rounded - * to the nearest int - */ -static int roundmax(double a, double b) +//----------------------------------------------------------------------------- +// Implement Pool of Graphic contexts. Creating them takes too much time. +//----------------------------------------------------------------------------- + +enum wxPoolGCType +{ + wxGC_ERROR = 0, + wxTEXT_MONO, + wxBG_MONO, + wxPEN_MONO, + wxBRUSH_MONO, + wxTEXT_COLOUR, + wxBG_COLOUR, + wxPEN_COLOUR, + wxBRUSH_COLOUR +}; + +struct wxGC +{ + GdkGC *m_gc; + wxPoolGCType m_type; + bool m_used; +}; + +static wxGC wxGCPool[200]; + +static void wxInitGCPool() +{ + memset( wxGCPool, 0, 200*sizeof(wxGC) ); +} + +static void wxCleanUpGCPool() { - return (int)((a > b ? a : b) + 0.5); + for (int i = 0; i < 200; i++) + { + if (wxGCPool[i].m_gc) + gdk_gc_unref( wxGCPool[i].m_gc ); + } } -/* - * compare two doubles and return the smaller rounded - * to the nearest int - */ -static int roundmin(double a, double b) +static GdkGC* wxGetPoolGC( GdkWindow *window, wxPoolGCType type ) { - return (int)((a < b ? a : b) - 0.5); + for (int i = 0; i < 200; i++) + { + if (!wxGCPool[i].m_gc) + { + wxGCPool[i].m_gc = gdk_gc_new( window ); + gdk_gc_set_exposures( wxGCPool[i].m_gc, FALSE ); + wxGCPool[i].m_type = type; + wxGCPool[i].m_used = FALSE; + } + if ((!wxGCPool[i].m_used) && (wxGCPool[i].m_type == type)) + { + wxGCPool[i].m_used = TRUE; + return wxGCPool[i].m_gc; + } + } + + wxFAIL_MSG( wxT("No GC available") ); + + return (GdkGC*) NULL; } +static void wxFreePoolGC( GdkGC *gc ) +{ + for (int i = 0; i < 200; i++) + { + if (wxGCPool[i].m_gc == gc) + { + wxGCPool[i].m_used = FALSE; + return; + } + } + + wxFAIL_MSG( wxT("Wrong GC") ); +} //----------------------------------------------------------------------------- // wxWindowDC @@ -136,6 +214,15 @@ wxWindowDC::wxWindowDC( wxWindow *window ) GtkWidget *widget = window->m_wxwindow; + // some controls don't have m_wxwindow - like wxStaticBox, but the user + // code should still be able to create wxClientDCs for them, so we will + // use the parent window here then + if ( !widget ) + { + window = window->GetParent(); + widget = window->m_wxwindow; + } + wxASSERT_MSG( widget, wxT("DC needs a widget") ); GtkPizza *pizza = GTK_PIZZA( widget ); @@ -150,10 +237,7 @@ wxWindowDC::wxWindowDC( wxWindow *window ) return; } - if (window->m_wxwindow) - m_cmap = gtk_widget_get_colormap( window->m_wxwindow ); - else - m_cmap = gtk_widget_get_colormap( window->m_widget ); + m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget ); SetUpDC(); @@ -172,16 +256,104 @@ wxWindowDC::~wxWindowDC() Destroy(); } +void wxWindowDC::SetUpDC() +{ + m_ok = TRUE; + + wxASSERT_MSG( !m_penGC, wxT("GCs already created") ); + + if (m_isMemDC && (((wxMemoryDC*)this)->m_selected.GetDepth() == 1)) + { + m_penGC = wxGetPoolGC( m_window, wxPEN_MONO ); + m_brushGC = wxGetPoolGC( m_window, wxBRUSH_MONO ); + m_textGC = wxGetPoolGC( m_window, wxTEXT_MONO ); + m_bgGC = wxGetPoolGC( m_window, wxBG_MONO ); + } + else + { + m_penGC = wxGetPoolGC( m_window, wxPEN_COLOUR ); + m_brushGC = wxGetPoolGC( m_window, wxBRUSH_COLOUR ); + m_textGC = wxGetPoolGC( m_window, wxTEXT_COLOUR ); + m_bgGC = wxGetPoolGC( m_window, wxBG_COLOUR ); + } + + /* background colour */ + m_backgroundBrush = *wxWHITE_BRUSH; + m_backgroundBrush.GetColour().CalcPixel( m_cmap ); + GdkColor *bg_col = m_backgroundBrush.GetColour().GetColor(); + + /* m_textGC */ + m_textForegroundColour.CalcPixel( m_cmap ); + gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() ); + + m_textBackgroundColour.CalcPixel( m_cmap ); + gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() ); + + gdk_gc_set_fill( m_textGC, GDK_SOLID ); + + /* m_penGC */ + m_pen.GetColour().CalcPixel( m_cmap ); + gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() ); + gdk_gc_set_background( m_penGC, bg_col ); + + gdk_gc_set_line_attributes( m_penGC, 0, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_ROUND ); + + + /* m_brushGC */ + m_brush.GetColour().CalcPixel( m_cmap ); + gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() ); + gdk_gc_set_background( m_brushGC, bg_col ); + + gdk_gc_set_fill( m_brushGC, GDK_SOLID ); + + + /* m_bgGC */ + gdk_gc_set_background( m_bgGC, bg_col ); + gdk_gc_set_foreground( m_bgGC, bg_col ); + + gdk_gc_set_fill( m_bgGC, GDK_SOLID ); + + /* ROPs */ + gdk_gc_set_function( m_textGC, GDK_COPY ); + gdk_gc_set_function( m_brushGC, GDK_COPY ); + gdk_gc_set_function( m_penGC, GDK_COPY ); + + /* clipping */ + gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL ); + gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL ); + gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL ); + gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL ); + + if (!hatch_bitmap) + { + hatch_bitmap = hatches; + hatch_bitmap[0] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, bdiag_bits, bdiag_width, bdiag_height ); + hatch_bitmap[1] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cdiag_bits, cdiag_width, cdiag_height ); + hatch_bitmap[2] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, fdiag_bits, fdiag_width, fdiag_height ); + hatch_bitmap[3] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cross_bits, cross_width, cross_height ); + hatch_bitmap[4] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, horiz_bits, horiz_width, horiz_height ); + hatch_bitmap[5] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, verti_bits, verti_width, verti_height ); + } +} + void wxWindowDC::DoFloodFill( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style) ) { wxFAIL_MSG( wxT("wxWindowDC::DoFloodFill not implemented") ); } -bool wxWindowDC::DoGetPixel( wxCoord WXUNUSED(x1), wxCoord WXUNUSED(y1), wxColour *WXUNUSED(col) ) const +bool wxWindowDC::DoGetPixel( wxCoord x1, wxCoord y1, wxColour *col ) const { - wxFAIL_MSG( wxT("wxWindowDC::DoGetPixel not implemented") ); - return FALSE; + // Generic (and therefore rather inefficient) method. + // Could be improved. + wxMemoryDC memdc; + wxBitmap bitmap(1, 1); + memdc.SelectObject(bitmap); + memdc.Blit(0, 0, 1, 1, (wxDC*) this, x1, y1); + memdc.SelectObject(wxNullBitmap); + wxImage image(bitmap); + col->Set(image.GetRed(0, 0), image.GetGreen(0, 0), image.GetBlue(0, 0)); + return TRUE; } void wxWindowDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 ) @@ -349,8 +521,16 @@ void wxWindowDC::DoDrawPolygon( int n, wxPoint points[], wxCoord xoffset, wxCoor CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset ); } - if ((m_brush.GetStyle() != wxTRANSPARENT) && m_window) - gdk_draw_polygon (m_window, m_brushGC, TRUE, gdkpoints, n); + if (m_window) + { + if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask())) + gdk_draw_polygon (m_window, m_textGC, TRUE, gdkpoints, n); + else + { + if ((m_brush.GetStyle() != wxTRANSPARENT)) + gdk_draw_polygon (m_window, m_brushGC, TRUE, gdkpoints, n); + } + } // To do: Fillstyle @@ -387,11 +567,19 @@ void wxWindowDC::DoDrawRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord h if (m_window) { - if (m_brush.GetStyle() != wxTRANSPARENT) - gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh ); - - if (m_pen.GetStyle() != wxTRANSPARENT) + if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask())) + { + gdk_draw_rectangle( m_window, m_textGC, TRUE, xx, yy, ww, hh ); gdk_draw_rectangle( m_window, m_penGC, FALSE, xx, yy, ww-1, hh-1 ); + } + else + { + if (m_brush.GetStyle() != wxTRANSPARENT) + gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh ); + + if (m_pen.GetStyle() != wxTRANSPARENT) + gdk_draw_rectangle( m_window, m_penGC, FALSE, xx, yy, ww-1, hh-1 ); + } } CalcBoundingBox( x, y ); @@ -510,8 +698,9 @@ void wxWindowDC::DoDrawBitmap( const wxBitmap &bitmap, wxCHECK_RET( bitmap.Ok(), wxT("invalid bitmap") ); - /* scale/translate size and position */ + bool is_mono = (bitmap.GetBitmap() != NULL); + /* scale/translate size and position */ int xx = XLOG2DEV(x); int yy = YLOG2DEV(y); @@ -526,16 +715,25 @@ void wxWindowDC::DoDrawBitmap( const wxBitmap &bitmap, int ww = XLOG2DEVREL(w); int hh = YLOG2DEVREL(h); + /* compare to current clipping region */ + if (!m_currentClippingRegion.IsEmpty()) + { + wxRegion tmp( xx,yy,ww,hh ); + tmp.Intersect( m_currentClippingRegion ); + if (tmp.IsEmpty()) + return; + } + /* scale bitmap if required */ - wxBitmap use_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 { @@ -543,38 +741,84 @@ void wxWindowDC::DoDrawBitmap( const wxBitmap &bitmap, } /* apply mask if any */ - GdkBitmap *mask = (GdkBitmap *) NULL; if (use_bitmap.GetMask()) mask = use_bitmap.GetMask()->GetBitmap(); - 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 (useMask && mask) { - gdk_draw_bitmap( m_window, m_penGC, bm, 0, 0, xx, yy, -1, -1 ); + GdkBitmap *new_mask = (GdkBitmap*) NULL; + if (!m_currentClippingRegion.IsEmpty()) + { + GdkColor col; + new_mask = gdk_pixmap_new( wxRootWindow->window, ww, hh, 1 ); + GdkGC *gc = gdk_gc_new( new_mask ); + col.pixel = 0; + gdk_gc_set_foreground( gc, &col ); + gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, ww, 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() ); + gdk_gc_set_clip_origin( gc, -xx, -yy ); + gdk_gc_set_fill( gc, GDK_OPAQUE_STIPPLED ); + gdk_gc_set_stipple( gc, mask ); + gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, ww, hh ); +/* + gdk_gc_set_clip_mask( m_brushGC, NULL ); + gdk_gc_set_clip_mask( m_textGC, NULL ); + SetBrush( *wxRED_BRUSH ); + DrawRectangle( 70, 0, 70, 1000 ); + gdk_draw_bitmap( m_window, m_textGC, new_mask, 0, 0, 100, 5, ww, hh ); + gdk_draw_bitmap( m_window, m_textGC, mask, 0, 0, 80, 5, ww, hh ); +*/ + gdk_gc_unref( gc ); + } + + if (is_mono) + { + if (new_mask) + gdk_gc_set_clip_mask( m_textGC, new_mask ); + else + gdk_gc_set_clip_mask( m_textGC, mask ); + gdk_gc_set_clip_origin( m_textGC, xx, yy ); + } + else + { + if (new_mask) + gdk_gc_set_clip_mask( m_penGC, new_mask ); + else + gdk_gc_set_clip_mask( m_penGC, mask ); + gdk_gc_set_clip_origin( m_penGC, xx, yy ); + } + if (new_mask) + gdk_bitmap_unref( new_mask ); } - } - /* remove mask again if any */ + /* 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 ); + if (!m_currentClippingRegion.IsEmpty()) + gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() ); + } + else + { + gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL ); + gdk_gc_set_clip_origin( m_penGC, 0, 0 ); + if (!m_currentClippingRegion.IsEmpty()) + gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() ); + } } } @@ -597,6 +841,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) { @@ -620,6 +865,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) && @@ -641,13 +887,28 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord he 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.IsEmpty()) + { + wxRegion tmp( xx,yy,ww,hh ); + tmp.Intersect( m_currentClippingRegion ); + if (tmp.IsEmpty()) + return TRUE; + } + int 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(); @@ -655,7 +916,6 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord he wxCoord bm_hh = YLOG2DEVREL( bm_height ); /* scale bitmap if required */ - wxBitmap use_bitmap; if ((bm_width != bm_ww) || (bm_height != bm_hh)) @@ -663,68 +923,91 @@ 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 { use_bitmap = memDC->m_selected; } - /* scale/translate size and position */ - - wxCoord xx = XLOG2DEV(xdest); - wxCoord yy = YLOG2DEV(ydest); - - wxCoord ww = XLOG2DEVREL(width); - wxCoord hh = YLOG2DEVREL(height); - /* apply mask if any */ - GdkBitmap *mask = (GdkBitmap *) NULL; if (use_bitmap.GetMask()) mask = use_bitmap.GetMask()->GetBitmap(); 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) + GdkBitmap *new_mask = (GdkBitmap*) NULL; + if (!m_currentClippingRegion.IsEmpty()) + { + GdkColor col; + new_mask = gdk_pixmap_new( wxRootWindow->window, bm_ww, bm_hh, 1 ); + GdkGC *gc = gdk_gc_new( new_mask ); + col.pixel = 0; + gdk_gc_set_foreground( gc, &col ); + 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() ); + gdk_gc_set_clip_origin( gc, -xx, -yy ); + 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) { - /* 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 ); + if (new_mask) + gdk_gc_set_clip_mask( m_textGC, new_mask ); + else + gdk_gc_set_clip_mask( m_textGC, mask ); + gdk_gc_set_clip_origin( m_textGC, xx, yy ); } + else + { + if (new_mask) + gdk_gc_set_clip_mask( m_penGC, new_mask ); + else + gdk_gc_set_clip_mask( m_penGC, mask ); + gdk_gc_set_clip_origin( m_penGC, xx, yy ); + } + if (new_mask) + gdk_bitmap_unref( new_mask ); } - /* remove mask again if any */ + /* 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 ); + if (!m_currentClippingRegion.IsEmpty()) + gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() ); + } + else + { + gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL ); + gdk_gc_set_clip_origin( m_penGC, 0, 0 ); + if (!m_currentClippingRegion.IsEmpty()) + gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() ); + } } } else /* use_bitmap_method */ { - /* scale/translate size and position */ - - wxCoord xx = XLOG2DEV(xdest); - wxCoord yy = YLOG2DEV(ydest); - - wxCoord ww = XLOG2DEVREL(width); - wxCoord hh = YLOG2DEVREL(height); - if ((width != ww) || (height != hh)) { /* draw source window into a bitmap as we cannot scale @@ -802,13 +1085,13 @@ void wxWindowDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y ) properties (see wxXt implementation) */ if (m_font.GetUnderlined()) { - long width = gdk_string_width( font, text.mbc_str() ); - long ul_y = y + font->ascent; + wxCoord width = gdk_string_width( font, text.mbc_str() ); + 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); } - long w, h; + wxCoord w, h; GetTextExtent (text, &w, &h); CalcBoundingBox (x + w, y + h); CalcBoundingBox (x, y); @@ -830,13 +1113,12 @@ void wxWindowDC::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, wxCHECK_RET( font, wxT("invalid font") ); -// x = XLOG2DEV(x); -// y = YLOG2DEV(y); + // the size of the text + wxCoord w = gdk_string_width( font, text.mbc_str() ); + wxCoord h = font->ascent + font->descent; - int cx = gdk_string_width( font, text.mbc_str() ); - int cy = font->ascent + font->descent; - - wxBitmap src(cx, cy); + // draw the string normally + wxBitmap src(w, h); wxMemoryDC dc; dc.SelectObject(src); dc.SetFont(GetFont()); @@ -845,52 +1127,87 @@ void wxWindowDC::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, dc.Clear(); dc.DrawText(text, 0, 0); dc.SetFont(wxNullFont); + dc.SelectObject(wxNullBitmap); // Calculate the size of the rotated bounding box. - double dx = cos(angle / 180.0 * M_PI); - double dy = sin(angle / 180.0 * M_PI); - double x4 = -cy * dy; - double y4 = cy * dx; - double x3 = cx * dx; - double y3 = cx * dy; - double x2 = x3 + x4; - double y2 = y3 + y4; - double x1 = x; - double y1 = y; - - // Create image from the source bitmap after writing the text into it. - wxImage image(src); - - int minx = roundmin(0, roundmin(x4, roundmin(x2, x3))); - int miny = roundmin(0, roundmin(y4, roundmin(y2, y3))); - int maxx = roundmax(0, roundmax(x4, roundmax(x2, x3))); - int maxy = roundmax(0, roundmax(y4, roundmax(y2, y3))); - - // This rotates counterclockwise around the top left corner. - for (int rx = minx; rx < maxx; rx++) + 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 (int ry = miny; ry < maxy; ry++) + for ( wxCoord srcY = 0; srcY < h; srcY++ ) { - // transform dest coords to source coords - int sx = (int) (rx * dx + ry * dy + 0.5); - int sy = (int) (ry * dx - rx * dy + 0.5); - if (sx >= 0 && sx < cx && sy >= 0 && sy < cy) + // 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) ) { - // draw black pixels, ignore white ones (i.e. transparent b/g) - if (image.GetRed(sx, sy) == 0) + // change colour if needed + if ( textPixel != textColSet ) { - DrawPoint(x1 + maxx - rx, cy + y1 - ry); - } - else - { - // Background - //DrawPoint(x1 + maxx - rx, cy + y1 + maxy - ry); + 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) ); } } } - // TODO call CalcBoundingBox() + // 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, @@ -1038,6 +1355,7 @@ void wxWindowDC::SetPen( const wxPen &pen ) } case wxTRANSPARENT: + case wxSTIPPLE_MASK_OPAQUE: case wxSTIPPLE: case wxSOLID: default: @@ -1071,17 +1389,31 @@ void wxWindowDC::SetPen( const wxPen &pen ) GdkCapStyle capStyle = GDK_CAP_ROUND; switch (m_pen.GetCap()) { - case wxCAP_ROUND: { capStyle = (width <= 1) ? GDK_CAP_NOT_LAST : GDK_CAP_ROUND; break; } case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; } case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; } + case wxCAP_ROUND: + default: + { + if (width <= 1) + { + width = 0; + capStyle = GDK_CAP_NOT_LAST; + } + else + { + capStyle = GDK_CAP_ROUND; + } + break; + } } GdkJoinStyle joinStyle = GDK_JOIN_ROUND; switch (m_pen.GetJoin()) { case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; } - case wxJOIN_ROUND: { joinStyle = GDK_JOIN_ROUND; break; } case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; } + case wxJOIN_ROUND: + default: { joinStyle = GDK_JOIN_ROUND; break; } } gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle ); @@ -1121,6 +1453,12 @@ void wxWindowDC::SetBrush( const wxBrush &brush ) } } + if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask())) + { + gdk_gc_set_fill( m_textGC, GDK_OPAQUE_STIPPLED); + gdk_gc_set_stipple( m_textGC, m_brush.GetStipple()->GetMask()->GetBitmap() ); + } + if (IS_HATCH(m_brush.GetStyle())) { gdk_gc_set_fill( m_brushGC, GDK_STIPPLED ); @@ -1178,7 +1516,12 @@ void wxWindowDC::SetLogicalFunction( int function ) { wxCHECK_RET( Ok(), wxT("invalid window dc") ); - if (m_logicalFunction == function) return; + if (m_logicalFunction == function) + return; + + // VZ: shouldn't this be a CHECK? + if (!m_window) + return; GdkFunction mode = GDK_COPY; switch (function) @@ -1191,9 +1534,7 @@ void wxWindowDC::SetLogicalFunction( int function ) case wxCLEAR: mode = GDK_CLEAR; break; case wxSET: mode = GDK_SET; break; case wxOR_INVERT: mode = GDK_OR_INVERT; break; - case wxSRC_AND: case wxAND: mode = GDK_AND; break; - case wxSRC_OR: case wxOR: mode = GDK_OR; break; case wxEQUIV: mode = GDK_EQUIV; break; case wxNAND: mode = GDK_NAND; break; @@ -1201,6 +1542,9 @@ void wxWindowDC::SetLogicalFunction( int function ) case wxCOPY: mode = GDK_COPY; break; case wxNO_OP: mode = GDK_NOOP; break; case wxSRC_INVERT: mode = GDK_COPY_INVERT; break; + + // unsupported by GTK + case wxNOR: mode = GDK_COPY; break; #endif default: { @@ -1211,10 +1555,12 @@ void wxWindowDC::SetLogicalFunction( int function ) m_logicalFunction = function; - if (!m_window) return; - gdk_gc_set_function( m_penGC, mode ); gdk_gc_set_function( m_brushGC, mode ); + + // to stay compatible with wxMSW, we don't apply ROPs to the text + // operations (i.e. DrawText/DrawRotatedText). + // True, but mono-bitmaps use the m_textGC and they use ROPs as well. gdk_gc_set_function( m_textGC, mode ); } @@ -1279,15 +1625,23 @@ void wxWindowDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoo if (!m_window) return; - GdkRectangle rect; + wxRect rect; rect.x = XLOG2DEV(x); rect.y = YLOG2DEV(y); rect.width = XLOG2DEVREL(width); rect.height = YLOG2DEVREL(height); - gdk_gc_set_clip_rectangle( m_penGC, &rect ); - gdk_gc_set_clip_rectangle( m_brushGC, &rect ); - gdk_gc_set_clip_rectangle( m_textGC, &rect ); - gdk_gc_set_clip_rectangle( m_bgGC, &rect ); + + m_currentClippingRegion.Clear(); + m_currentClippingRegion.Union( rect ); +#if USE_PAINT_REGION + if (!m_paintClippingRegion.IsEmpty()) + m_currentClippingRegion.Intersect( m_paintClippingRegion ); +#endif + + gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() ); } void wxWindowDC::DoSetClippingRegionAsRegion( const wxRegion ®ion ) @@ -1300,12 +1654,24 @@ void wxWindowDC::DoSetClippingRegionAsRegion( const wxRegion ®ion ) return; } + wxCoord x,y,w,h; + region.GetBox( x, y, w, h ); + + wxDC::DoSetClippingRegion( x, y, w, h ); + if (!m_window) return; + + m_currentClippingRegion.Clear(); + m_currentClippingRegion.Union( region ); +#if USE_PAINT_REGION + if (!m_paintClippingRegion.IsEmpty()) + m_currentClippingRegion.Intersect( m_paintClippingRegion ); +#endif - gdk_gc_set_clip_region( m_penGC, region.GetRegion() ); - gdk_gc_set_clip_region( m_brushGC, region.GetRegion() ); - gdk_gc_set_clip_region( m_textGC, region.GetRegion() ); - gdk_gc_set_clip_region( m_bgGC, region.GetRegion() ); + gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() ); } void wxWindowDC::DestroyClippingRegion() @@ -1314,74 +1680,38 @@ void wxWindowDC::DestroyClippingRegion() wxDC::DestroyClippingRegion(); - if (!m_window) return; + m_currentClippingRegion.Clear(); + + if (!m_paintClippingRegion.IsEmpty()) + m_currentClippingRegion.Union( m_paintClippingRegion ); - gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL ); - gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL ); - gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL ); - gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL ); -} - -void wxWindowDC::SetUpDC() -{ - Destroy(); - m_ok = TRUE; - m_logicalFunction = wxCOPY; - m_penGC = gdk_gc_new( m_window ); - m_brushGC = gdk_gc_new( m_window ); - m_textGC = gdk_gc_new( m_window ); - m_bgGC = gdk_gc_new( m_window ); - - wxColour tmp_col( m_textForegroundColour ); - m_textForegroundColour = wxNullColour; - SetTextForeground( tmp_col ); - tmp_col = m_textBackgroundColour; - m_textBackgroundColour = wxNullColour; - SetTextBackground( tmp_col ); - - wxPen tmp_pen( m_pen ); - m_pen = wxNullPen; - SetPen( tmp_pen ); - - wxFont tmp_font( m_font ); - m_font = wxNullFont; - SetFont( tmp_font ); - - wxBrush tmp_brush( m_brush ); - m_brush = wxNullBrush; - SetBrush( tmp_brush ); - -/* - tmp_brush = m_backgroundBrush; - m_backgroundBrush = wxNullBrush; - SetBackground( tmp_brush ); -*/ - tmp_brush = m_backgroundBrush; - m_backgroundBrush = wxNullBrush; - SetBackground( *wxWHITE_BRUSH ); - m_backgroundBrush = tmp_brush; + if (!m_window) return; - if (!hatch_bitmap) + if (m_currentClippingRegion.IsEmpty()) { - hatch_bitmap = hatches; - hatch_bitmap[0] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, bdiag_bits, bdiag_width, bdiag_height ); - hatch_bitmap[1] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cdiag_bits, cdiag_width, cdiag_height ); - hatch_bitmap[2] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, fdiag_bits, fdiag_width, fdiag_height ); - hatch_bitmap[3] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cross_bits, cross_width, cross_height ); - hatch_bitmap[4] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, horiz_bits, horiz_width, horiz_height ); - hatch_bitmap[5] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, verti_bits, verti_width, verti_height ); + gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL ); + gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL ); + gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL ); + gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL ); + } + else + { + gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() ); } } void wxWindowDC::Destroy() { - if (m_penGC) gdk_gc_unref( m_penGC ); + if (m_penGC) wxFreePoolGC( m_penGC ); m_penGC = (GdkGC*) NULL; - if (m_brushGC) gdk_gc_unref( m_brushGC ); + if (m_brushGC) wxFreePoolGC( m_brushGC ); m_brushGC = (GdkGC*) NULL; - if (m_textGC) gdk_gc_unref( m_textGC ); + if (m_textGC) wxFreePoolGC( m_textGC ); m_textGC = (GdkGC*) NULL; - if (m_bgGC) gdk_gc_unref( m_bgGC ); + if (m_bgGC) wxFreePoolGC( m_bgGC ); m_bgGC = (GdkGC*) NULL; } @@ -1602,6 +1932,18 @@ wxPaintDC::wxPaintDC() wxPaintDC::wxPaintDC( wxWindow *win ) : wxWindowDC( win ) { +#if USE_PAINT_REGION + if (!win->GetUpdateRegion().IsEmpty()) + { + m_paintClippingRegion = win->GetUpdateRegion(); + m_currentClippingRegion.Union( m_paintClippingRegion ); + + gdk_gc_set_clip_region( m_penGC, m_paintClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_brushGC, m_paintClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_textGC, m_paintClippingRegion.GetRegion() ); + gdk_gc_set_clip_region( m_bgGC, m_paintClippingRegion.GetRegion() ); + } +#endif } //----------------------------------------------------------------------------- @@ -1620,3 +1962,29 @@ wxClientDC::wxClientDC( wxWindow *win ) { } +// ---------------------------------------------------------------------------- +// wxDCModule +// ---------------------------------------------------------------------------- + +class wxDCModule : public wxModule +{ +public: + bool OnInit(); + void OnExit(); + +private: + DECLARE_DYNAMIC_CLASS(wxDCModule) +}; + +IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule) + +bool wxDCModule::OnInit() +{ + wxInitGCPool(); + return TRUE; +} + +void wxDCModule::OnExit() +{ + wxCleanUpGCPool(); +}