1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/dcclient.cpp
3 // Purpose:     wxWindowDCImpl implementation
4 // Author:      Robert Roebling
5 // Copyright:   (c) 1998 Robert Roebling, Chris Breeze
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 #include "wx/gtk/dcclient.h"
13 
14 #ifndef WX_PRECOMP
15     #include "wx/window.h"
16     #include "wx/log.h"
17     #include "wx/dcmemory.h"
18     #include "wx/math.h"
19     #include "wx/image.h"
20     #include "wx/module.h"
21 #endif
22 
23 #include "wx/fontutil.h"
24 
25 #include "wx/gtk/private.h"
26 #include "wx/gtk/private/object.h"
27 #include "wx/private/textmeasure.h"
28 
29 //-----------------------------------------------------------------------------
30 // local defines
31 //-----------------------------------------------------------------------------
32 
33 #define XLOG2DEV(x)    LogicalToDeviceX(x)
34 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
35 #define YLOG2DEV(y)    LogicalToDeviceY(y)
36 #define YLOG2DEVREL(y) LogicalToDeviceYRel(y)
37 
38 #define USE_PAINT_REGION 1
39 
40 //-----------------------------------------------------------------------------
41 // local data
42 //-----------------------------------------------------------------------------
43 
44 #include "bdiag.xbm"
45 #include "fdiag.xbm"
46 #include "cdiag.xbm"
47 #include "horiz.xbm"
48 #include "verti.xbm"
49 #include "cross.xbm"
50 
51 static GdkPixmap* hatches[wxBRUSHSTYLE_LAST_HATCH - wxBRUSHSTYLE_FIRST_HATCH + 1];
52 
53 //-----------------------------------------------------------------------------
54 // constants
55 //-----------------------------------------------------------------------------
56 
57 static const double RAD2DEG  = 180.0 / M_PI;
58 
59 // ----------------------------------------------------------------------------
60 // private functions
61 // ----------------------------------------------------------------------------
62 
dmax(double a,double b)63 static inline double dmax(double a, double b) { return a > b ? a : b; }
dmin(double a,double b)64 static inline double dmin(double a, double b) { return a < b ? a : b; }
65 
GetHatch(int style)66 static GdkPixmap* GetHatch(int style)
67 {
68     wxASSERT(style >= wxBRUSHSTYLE_FIRST_HATCH && style <= wxBRUSHSTYLE_LAST_HATCH);
69     const int i = style - wxBRUSHSTYLE_FIRST_HATCH;
70     if (hatches[i] == NULL)
71     {
72         // This macro creates a bitmap from an XBM file included above. Notice
73         // the need for the cast because gdk_bitmap_create_from_data() doesn't
74         // accept unsigned data but the arrays in XBM need to be unsigned to
75         // avoid warnings (and even errors in C+0x mode) from g++.
76 #define CREATE_FROM_XBM_DATA(name) \
77         gdk_bitmap_create_from_data \
78         ( \
79             NULL, \
80             reinterpret_cast<gchar *>(name ## _bits), \
81             name ## _width, \
82             name ## _height \
83         )
84 
85         switch (style)
86         {
87         case wxBRUSHSTYLE_BDIAGONAL_HATCH:
88             hatches[i] = CREATE_FROM_XBM_DATA(bdiag);
89             break;
90         case wxBRUSHSTYLE_CROSSDIAG_HATCH:
91             hatches[i] = CREATE_FROM_XBM_DATA(cdiag);
92             break;
93         case wxBRUSHSTYLE_CROSS_HATCH:
94             hatches[i] = CREATE_FROM_XBM_DATA(cross);
95             break;
96         case wxBRUSHSTYLE_FDIAGONAL_HATCH:
97             hatches[i] = CREATE_FROM_XBM_DATA(fdiag);
98             break;
99         case wxBRUSHSTYLE_HORIZONTAL_HATCH:
100             hatches[i] = CREATE_FROM_XBM_DATA(horiz);
101             break;
102         case wxBRUSHSTYLE_VERTICAL_HATCH:
103             hatches[i] = CREATE_FROM_XBM_DATA(verti);
104             break;
105         }
106 
107 #undef CREATE_FROM_XBM_DATA
108     }
109     return hatches[i];
110 }
111 
112 //-----------------------------------------------------------------------------
113 // Implement Pool of Graphic contexts. Creating them takes too much time.
114 //-----------------------------------------------------------------------------
115 
116 enum wxPoolGCType
117 {
118    wxGC_ERROR = 0,
119    wxTEXT_MONO,
120    wxBG_MONO,
121    wxPEN_MONO,
122    wxBRUSH_MONO,
123    wxTEXT_COLOUR,
124    wxBG_COLOUR,
125    wxPEN_COLOUR,
126    wxBRUSH_COLOUR,
127    wxTEXT_SCREEN,
128    wxBG_SCREEN,
129    wxPEN_SCREEN,
130    wxBRUSH_SCREEN,
131    wxTEXT_COLOUR_ALPHA,
132    wxBG_COLOUR_ALPHA,
133    wxPEN_COLOUR_ALPHA,
134    wxBRUSH_COLOUR_ALPHA
135 };
136 
137 struct wxGC
138 {
139     GdkGC        *m_gc;
140     wxPoolGCType  m_type;
141     bool          m_used;
142 };
143 
144 #define GC_POOL_ALLOC_SIZE 100
145 
146 static int wxGCPoolSize = 0;
147 
148 static wxGC *wxGCPool = NULL;
149 
wxInitGCPool()150 static void wxInitGCPool()
151 {
152     // This really could wait until the first call to
153     // wxGetPoolGC, but we will make the first allocation
154     // now when other initialization is being performed.
155 
156     // Set initial pool size.
157     wxGCPoolSize = GC_POOL_ALLOC_SIZE;
158 
159     // Allocate initial pool.
160     wxGCPool = (wxGC *)malloc(wxGCPoolSize * sizeof(wxGC));
161     if (wxGCPool == NULL)
162     {
163         // If we cannot malloc, then fail with error
164         // when debug is enabled.  If debug is not enabled,
165         // the problem will eventually get caught
166         // in wxGetPoolGC.
167         wxFAIL_MSG( wxT("Cannot allocate GC pool") );
168         return;
169     }
170 
171     // Zero initial pool.
172     memset(wxGCPool, 0, wxGCPoolSize * sizeof(wxGC));
173 }
174 
wxCleanUpGCPool()175 static void wxCleanUpGCPool()
176 {
177     for (int i = 0; i < wxGCPoolSize; i++)
178     {
179         if (wxGCPool[i].m_gc)
180             g_object_unref (wxGCPool[i].m_gc);
181     }
182 
183     free(wxGCPool);
184     wxGCPool = NULL;
185     wxGCPoolSize = 0;
186 }
187 
wxGetPoolGC(GdkWindow * window,wxPoolGCType type)188 static GdkGC* wxGetPoolGC( GdkWindow *window, wxPoolGCType type )
189 {
190     wxGC *pptr;
191 
192     // Look for an available GC.
193     for (int i = 0; i < wxGCPoolSize; i++)
194     {
195         if (!wxGCPool[i].m_gc)
196         {
197             wxGCPool[i].m_gc = gdk_gc_new( window );
198             gdk_gc_set_exposures( wxGCPool[i].m_gc, FALSE );
199             wxGCPool[i].m_type = type;
200             wxGCPool[i].m_used = false;
201         }
202         if ((!wxGCPool[i].m_used) && (wxGCPool[i].m_type == type))
203         {
204             wxGCPool[i].m_used = true;
205             return wxGCPool[i].m_gc;
206         }
207     }
208 
209     // We did not find an available GC.
210     // We need to grow the GC pool.
211     pptr = (wxGC *)realloc(wxGCPool,
212         (wxGCPoolSize + GC_POOL_ALLOC_SIZE)*sizeof(wxGC));
213     if (pptr != NULL)
214     {
215         // Initialize newly allocated pool.
216         wxGCPool = pptr;
217         memset(&wxGCPool[wxGCPoolSize], 0,
218             GC_POOL_ALLOC_SIZE*sizeof(wxGC));
219 
220         // Initialize entry we will return.
221         wxGCPool[wxGCPoolSize].m_gc = gdk_gc_new( window );
222         gdk_gc_set_exposures( wxGCPool[wxGCPoolSize].m_gc, FALSE );
223         wxGCPool[wxGCPoolSize].m_type = type;
224         wxGCPool[wxGCPoolSize].m_used = true;
225 
226         // Set new value of pool size.
227         wxGCPoolSize += GC_POOL_ALLOC_SIZE;
228 
229         // Return newly allocated entry.
230         return wxGCPool[wxGCPoolSize-GC_POOL_ALLOC_SIZE].m_gc;
231     }
232 
233     // The realloc failed.  Fall through to error.
234     wxFAIL_MSG( wxT("No GC available") );
235 
236     return NULL;
237 }
238 
wxFreePoolGC(GdkGC * gc)239 static void wxFreePoolGC( GdkGC *gc )
240 {
241     for (int i = 0; i < wxGCPoolSize; i++)
242     {
243         if (wxGCPool[i].m_gc == gc)
244         {
245             wxGCPool[i].m_used = false;
246             return;
247         }
248     }
249 
250     wxFAIL_MSG( wxT("Wrong GC") );
251 }
252 
253 //-----------------------------------------------------------------------------
254 // wxWindowDC
255 //-----------------------------------------------------------------------------
256 
IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl,wxGTKDCImpl)257 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxGTKDCImpl)
258 
259 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner ) :
260    wxGTKDCImpl( owner )
261 {
262     m_gdkwindow = NULL;
263     m_penGC = NULL;
264     m_brushGC = NULL;
265     m_textGC = NULL;
266     m_bgGC = NULL;
267     m_cmap = NULL;
268     m_isScreenDC = false;
269     m_context = NULL;
270     m_layout = NULL;
271     m_fontdesc = NULL;
272 }
273 
wxWindowDCImpl(wxDC * owner,wxWindow * window)274 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window ) :
275    wxGTKDCImpl( owner )
276 {
277     wxASSERT_MSG( window, wxT("DC needs a window") );
278 
279     m_gdkwindow = NULL;
280     m_penGC = NULL;
281     m_brushGC = NULL;
282     m_textGC = NULL;
283     m_bgGC = NULL;
284     m_cmap = NULL;
285     m_isScreenDC = false;
286     m_font = window->GetFont();
287 
288     GtkWidget *widget = window->m_wxwindow;
289     m_gdkwindow = window->GTKGetDrawingWindow();
290 
291     // Some controls don't have m_wxwindow - like wxStaticBox, but the user
292     // code should still be able to create wxClientDCs for them
293     if ( !widget )
294     {
295         widget = window->m_widget;
296 
297         wxCHECK_RET(widget, "DC needs a widget");
298 
299         m_gdkwindow = widget->window;
300         if (!gtk_widget_get_has_window(widget))
301             SetDeviceLocalOrigin(widget->allocation.x, widget->allocation.y);
302     }
303 
304     m_context = window->GTKGetPangoDefaultContext();
305     m_layout = pango_layout_new( m_context );
306     m_fontdesc = pango_font_description_copy( widget->style->font_desc );
307 
308     // Window not realized ?
309     if (!m_gdkwindow)
310     {
311          // Don't report problems as per MSW.
312          m_ok = true;
313 
314          return;
315     }
316 
317     m_cmap = gtk_widget_get_colormap(widget);
318 
319     SetUpDC();
320 
321     /* this must be done after SetUpDC, bacause SetUpDC calls the
322        repective SetBrush, SetPen, SetBackground etc functions
323        to set up the DC. SetBackground call m_owner->SetBackground
324        and this might not be desired as the standard dc background
325        is white whereas a window might assume gray to be the
326        standard (as e.g. wxStatusBar) */
327 
328     m_window = window;
329 
330     if (m_window && m_window->m_wxwindow &&
331         (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
332     {
333         // reverse sense
334         m_signX = -1;
335 
336         // origin in the upper right corner
337         m_deviceOriginX = m_window->GetClientSize().x;
338     }
339 }
340 
~wxWindowDCImpl()341 wxWindowDCImpl::~wxWindowDCImpl()
342 {
343     Destroy();
344 
345     if (m_layout)
346         g_object_unref (m_layout);
347     if (m_fontdesc)
348         pango_font_description_free( m_fontdesc );
349 }
350 
SetUpDC(bool isMemDC)351 void wxWindowDCImpl::SetUpDC( bool isMemDC )
352 {
353     m_ok = true;
354 
355     wxASSERT_MSG( !m_penGC, wxT("GCs already created") );
356 
357     bool done = false;
358 
359     if ((isMemDC) && (GetSelectedBitmap().IsOk()))
360     {
361         if (GetSelectedBitmap().GetDepth() == 1)
362         {
363             m_penGC = wxGetPoolGC( m_gdkwindow, wxPEN_MONO );
364             m_brushGC = wxGetPoolGC( m_gdkwindow, wxBRUSH_MONO );
365             m_textGC = wxGetPoolGC( m_gdkwindow, wxTEXT_MONO );
366             m_bgGC = wxGetPoolGC( m_gdkwindow, wxBG_MONO );
367             done = true;
368         }
369     }
370 
371     if (!done)
372     {
373         if (m_isScreenDC)
374         {
375             m_penGC = wxGetPoolGC( m_gdkwindow, wxPEN_SCREEN );
376             m_brushGC = wxGetPoolGC( m_gdkwindow, wxBRUSH_SCREEN );
377             m_textGC = wxGetPoolGC( m_gdkwindow, wxTEXT_SCREEN );
378             m_bgGC = wxGetPoolGC( m_gdkwindow, wxBG_SCREEN );
379         }
380 #if GTK_CHECK_VERSION(2,12,0)
381         // gdk_screen_get_rgba_colormap was added in 2.8, but this code is for
382         // compositing which requires 2.12
383         else if (gtk_check_version(2,12,0) == NULL &&
384             m_cmap == gdk_screen_get_rgba_colormap(gdk_colormap_get_screen(m_cmap)))
385         {
386             m_penGC = wxGetPoolGC( m_gdkwindow, wxPEN_COLOUR_ALPHA );
387             m_brushGC = wxGetPoolGC( m_gdkwindow, wxBRUSH_COLOUR_ALPHA );
388             m_textGC = wxGetPoolGC( m_gdkwindow, wxTEXT_COLOUR_ALPHA );
389             m_bgGC = wxGetPoolGC( m_gdkwindow, wxBG_COLOUR_ALPHA );
390         }
391 #endif
392         else
393         {
394             m_penGC = wxGetPoolGC( m_gdkwindow, wxPEN_COLOUR );
395             m_brushGC = wxGetPoolGC( m_gdkwindow, wxBRUSH_COLOUR );
396             m_textGC = wxGetPoolGC( m_gdkwindow, wxTEXT_COLOUR );
397             m_bgGC = wxGetPoolGC( m_gdkwindow, wxBG_COLOUR );
398         }
399     }
400 
401     /* background colour */
402     m_backgroundBrush = *wxWHITE_BRUSH;
403     m_backgroundBrush.GetColour().CalcPixel( m_cmap );
404     const GdkColor *bg_col = m_backgroundBrush.GetColour().GetColor();
405 
406     /* m_textGC */
407     m_textForegroundColour.CalcPixel( m_cmap );
408     gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
409 
410     m_textBackgroundColour.CalcPixel( m_cmap );
411     gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
412 
413     gdk_gc_set_fill( m_textGC, GDK_SOLID );
414 
415     gdk_gc_set_colormap( m_textGC, m_cmap );
416 
417     /* m_penGC */
418     m_pen.GetColour().CalcPixel( m_cmap );
419     gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
420     gdk_gc_set_background( m_penGC, bg_col );
421 
422     gdk_gc_set_line_attributes( m_penGC, 0, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_ROUND );
423 
424     /* m_brushGC */
425     m_brush.GetColour().CalcPixel( m_cmap );
426     gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
427     gdk_gc_set_background( m_brushGC, bg_col );
428 
429     gdk_gc_set_fill( m_brushGC, GDK_SOLID );
430 
431     /* m_bgGC */
432     gdk_gc_set_background( m_bgGC, bg_col );
433     gdk_gc_set_foreground( m_bgGC, bg_col );
434 
435     gdk_gc_set_fill( m_bgGC, GDK_SOLID );
436 
437     /* ROPs */
438     gdk_gc_set_function( m_textGC, GDK_COPY );
439     gdk_gc_set_function( m_brushGC, GDK_COPY );
440     gdk_gc_set_function( m_penGC, GDK_COPY );
441 
442     /* clipping */
443     gdk_gc_set_clip_rectangle( m_penGC, NULL );
444     gdk_gc_set_clip_rectangle( m_brushGC, NULL );
445     gdk_gc_set_clip_rectangle( m_textGC, NULL );
446     gdk_gc_set_clip_rectangle( m_bgGC, NULL );
447 }
448 
DoGetSize(int * width,int * height) const449 void wxWindowDCImpl::DoGetSize( int* width, int* height ) const
450 {
451     wxCHECK_RET( m_window, wxT("GetSize() doesn't work without window") );
452 
453     m_window->GetSize(width, height);
454 }
455 
DoFloodFill(wxCoord x,wxCoord y,const wxColour & col,wxFloodFillStyle style)456 bool wxWindowDCImpl::DoFloodFill(wxCoord x, wxCoord y,
457                                  const wxColour& col, wxFloodFillStyle style)
458 {
459 #if wxUSE_IMAGE
460     extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
461                               const wxColour & col, wxFloodFillStyle style);
462 
463     return wxDoFloodFill( GetOwner(), x, y, col, style);
464 #else
465     wxUnusedVar(x);
466     wxUnusedVar(y);
467     wxUnusedVar(col);
468     wxUnusedVar(style);
469 
470     return false;
471 #endif
472 }
473 
DoGetPixel(wxCoord x1,wxCoord y1,wxColour * col) const474 bool wxWindowDCImpl::DoGetPixel( wxCoord x1, wxCoord y1, wxColour *col ) const
475 {
476     GdkImage* image = NULL;
477     if (m_gdkwindow)
478     {
479         const int x = LogicalToDeviceX(x1);
480         const int y = LogicalToDeviceY(y1);
481         wxRect rect;
482         gdk_drawable_get_size(m_gdkwindow, &rect.width, &rect.height);
483         if (rect.Contains(x, y))
484             image = gdk_drawable_get_image(m_gdkwindow, x, y, 1, 1);
485     }
486     if (image == NULL)
487     {
488         *col = wxColour();
489         return false;
490     }
491     GdkColormap* colormap = gdk_image_get_colormap(image);
492     const unsigned pixel = gdk_image_get_pixel(image, 0, 0);
493     if (colormap == NULL)
494         *col = pixel ? m_textForegroundColour : m_textBackgroundColour;
495     else
496     {
497         GdkColor c;
498         gdk_colormap_query_color(colormap, pixel, &c);
499         col->Set(c.red >> 8, c.green >> 8, c.blue >> 8);
500     }
501     g_object_unref(image);
502     return true;
503 }
504 
DoDrawLine(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2)505 void wxWindowDCImpl::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
506 {
507     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
508 
509     if ( m_pen.IsNonTransparent() )
510     {
511         if (m_gdkwindow)
512             gdk_draw_line( m_gdkwindow, m_penGC, XLOG2DEV(x1), YLOG2DEV(y1), XLOG2DEV(x2), YLOG2DEV(y2) );
513 
514         CalcBoundingBox(x1, y1);
515         CalcBoundingBox(x2, y2);
516     }
517 }
518 
DoCrossHair(wxCoord x,wxCoord y)519 void wxWindowDCImpl::DoCrossHair( wxCoord x, wxCoord y )
520 {
521     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
522 
523     if ( m_pen.IsNonTransparent() )
524     {
525         int w = 0;
526         int h = 0;
527         GetOwner()->GetSize( &w, &h );
528         wxCoord xx = XLOG2DEV(x);
529         wxCoord yy = YLOG2DEV(y);
530         if (m_gdkwindow)
531         {
532             gdk_draw_line( m_gdkwindow, m_penGC, 0, yy, XLOG2DEVREL(w), yy );
533             gdk_draw_line( m_gdkwindow, m_penGC, xx, 0, xx, YLOG2DEVREL(h) );
534         }
535     }
536 }
537 
DrawingSetup(GdkGC * & gc,bool & originChanged)538 void wxWindowDCImpl::DrawingSetup(GdkGC*& gc, bool& originChanged)
539 {
540     gc = m_brushGC;
541     GdkPixmap* pixmap = NULL;
542     const int style = m_brush.GetStyle();
543 
544     if (style == wxBRUSHSTYLE_STIPPLE || style == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE)
545     {
546         const wxBitmap* stipple = m_brush.GetStipple();
547         if (stipple->IsOk())
548         {
549             if (style == wxBRUSHSTYLE_STIPPLE)
550                 pixmap = stipple->GetPixmap();
551             else if (stipple->GetMask())
552             {
553                 pixmap = stipple->GetPixmap();
554                 gc = m_textGC;
555             }
556         }
557     }
558     else if (m_brush.IsHatch())
559     {
560         pixmap = GetHatch(style);
561     }
562 
563     int origin_x = 0;
564     int origin_y = 0;
565     if (pixmap)
566     {
567         int w, h;
568         gdk_drawable_get_size(pixmap, &w, &h);
569         origin_x = m_deviceOriginX % w;
570         origin_y = m_deviceOriginY % h;
571     }
572 
573     originChanged = origin_x || origin_y;
574     if (originChanged)
575         gdk_gc_set_ts_origin(gc, origin_x, origin_y);
576 }
577 
DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)578 void wxWindowDCImpl::DoDrawArc( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2,
579                             wxCoord xc, wxCoord yc )
580 {
581     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
582 
583     wxCoord xx1 = XLOG2DEV(x1);
584     wxCoord yy1 = YLOG2DEV(y1);
585     wxCoord xx2 = XLOG2DEV(x2);
586     wxCoord yy2 = YLOG2DEV(y2);
587     wxCoord xxc = XLOG2DEV(xc);
588     wxCoord yyc = YLOG2DEV(yc);
589     double dx = xx1 - xxc;
590     double dy = yy1 - yyc;
591     double radius = sqrt((double)(dx*dx+dy*dy));
592     wxCoord   r      = (wxCoord)radius;
593     double radius1, radius2;
594 
595     if (xx1 == xx2 && yy1 == yy2)
596     {
597         radius1 = 0.0;
598         radius2 = 360.0;
599     }
600     else if ( wxIsNullDouble(radius) )
601     {
602         radius1 =
603         radius2 = 0.0;
604     }
605     else
606     {
607         radius1 = (xx1 - xxc == 0) ?
608             (yy1 - yyc < 0) ? 90.0 : -90.0 :
609             -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
610         radius2 = (xx2 - xxc == 0) ?
611             (yy2 - yyc < 0) ? 90.0 : -90.0 :
612             -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
613     }
614     wxCoord alpha1 = wxCoord(radius1 * 64.0);
615     wxCoord alpha2 = wxCoord((radius2 - radius1) * 64.0);
616     while (alpha2 <= 0) alpha2 += 360*64;
617     while (alpha1 > 360*64) alpha1 -= 360*64;
618 
619     if (m_gdkwindow)
620     {
621         if ( m_brush.IsNonTransparent() )
622         {
623             GdkGC* gc;
624             bool originChanged;
625             DrawingSetup(gc, originChanged);
626 
627             gdk_draw_arc(m_gdkwindow, gc, true, xxc-r, yyc-r, 2*r, 2*r, alpha1, alpha2);
628 
629             if (originChanged)
630                 gdk_gc_set_ts_origin(gc, 0, 0);
631         }
632 
633         if ( m_pen.IsNonTransparent() )
634         {
635             gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
636 
637             if ( m_brush.IsNonTransparent() && (alpha2 - alpha1 != 360*64) )
638             {
639                 gdk_draw_line( m_gdkwindow, m_penGC, xx1, yy1, xxc, yyc );
640                 gdk_draw_line( m_gdkwindow, m_penGC, xxc, yyc, xx2, yy2 );
641             }
642         }
643     }
644 
645     CalcBoundingBox (x1, y1);
646     CalcBoundingBox (x2, y2);
647 }
648 
DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord width,wxCoord height,double sa,double ea)649 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double sa, double ea )
650 {
651     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
652 
653     wxCoord xx = XLOG2DEV(x);
654     wxCoord yy = YLOG2DEV(y);
655     wxCoord ww = m_signX * XLOG2DEVREL(width);
656     wxCoord hh = m_signY * YLOG2DEVREL(height);
657 
658     // CMB: handle -ve width and/or height
659     if (ww < 0) { ww = -ww; xx = xx - ww; }
660     if (hh < 0) { hh = -hh; yy = yy - hh; }
661 
662     if (m_gdkwindow)
663     {
664         wxCoord start = wxCoord(sa * 64.0);
665         wxCoord end = wxCoord((ea-sa) * 64.0);
666 
667         if ( m_brush.IsNonTransparent() )
668         {
669             GdkGC* gc;
670             bool originChanged;
671             DrawingSetup(gc, originChanged);
672 
673             gdk_draw_arc(m_gdkwindow, gc, true, xx, yy, ww, hh, start, end);
674 
675             if (originChanged)
676                 gdk_gc_set_ts_origin(gc, 0, 0);
677         }
678 
679         if ( m_pen.IsNonTransparent() )
680             gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx, yy, ww, hh, start, end );
681     }
682 
683     CalcBoundingBox (x, y);
684     CalcBoundingBox (x + width, y + height);
685 }
686 
DoDrawPoint(wxCoord x,wxCoord y)687 void wxWindowDCImpl::DoDrawPoint( wxCoord x, wxCoord y )
688 {
689     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
690 
691     if ( m_pen.IsNonTransparent() && m_gdkwindow )
692         gdk_draw_point( m_gdkwindow, m_penGC, XLOG2DEV(x), YLOG2DEV(y) );
693 
694     CalcBoundingBox (x, y);
695 }
696 
DoDrawLines(int n,const wxPoint points[],wxCoord xoffset,wxCoord yoffset)697 void wxWindowDCImpl::DoDrawLines( int n, const wxPoint points[], wxCoord xoffset, wxCoord yoffset )
698 {
699     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
700 
701     if (n <= 0) return;
702 
703     if ( m_pen.IsTransparent() )
704         return;
705 
706     //Check, if scaling is necessary
707     const bool doScale =
708         xoffset != 0 || yoffset != 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
709 
710     // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
711     const GdkPoint* gpts = reinterpret_cast<const GdkPoint*>(points);
712     GdkPoint* gpts_alloc = NULL;
713 
714     if (doScale)
715     {
716         gpts_alloc = new GdkPoint[n];
717         gpts = gpts_alloc;
718     }
719 
720     for (int i = 0; i < n; i++)
721     {
722         if (doScale)
723         {
724             gpts_alloc[i].x = XLOG2DEV(points[i].x + xoffset);
725             gpts_alloc[i].y = YLOG2DEV(points[i].y + yoffset);
726         }
727         CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
728     }
729 
730     if (m_gdkwindow)
731         gdk_draw_lines( m_gdkwindow, m_penGC, (GdkPoint*) gpts, n);
732 
733     delete[] gpts_alloc;
734 }
735 
DoDrawPolygon(int n,const wxPoint points[],wxCoord xoffset,wxCoord yoffset,wxPolygonFillMode WXUNUSED (fillStyle))736 void wxWindowDCImpl::DoDrawPolygon( int n, const wxPoint points[],
737                                     wxCoord xoffset, wxCoord yoffset,
738                                     wxPolygonFillMode WXUNUSED(fillStyle) )
739 {
740     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
741 
742     if (n <= 0) return;
743 
744     //Check, if scaling is necessary
745     const bool doScale =
746         xoffset != 0 || yoffset != 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
747 
748     // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
749     const GdkPoint* gdkpoints = reinterpret_cast<const GdkPoint*>(points);
750     GdkPoint* gdkpoints_alloc = NULL;
751 
752     if (doScale)
753     {
754         gdkpoints_alloc = new GdkPoint[n];
755         gdkpoints = gdkpoints_alloc;
756     }
757 
758     int i;
759     for (i = 0 ; i < n ; i++)
760     {
761         if (doScale)
762         {
763             gdkpoints_alloc[i].x = XLOG2DEV(points[i].x + xoffset);
764             gdkpoints_alloc[i].y = YLOG2DEV(points[i].y + yoffset);
765         }
766         CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
767     }
768 
769     if (m_gdkwindow)
770     {
771         if ( m_brush.IsNonTransparent() )
772         {
773             GdkGC* gc;
774             bool originChanged;
775             DrawingSetup(gc, originChanged);
776 
777             gdk_draw_polygon(m_gdkwindow, gc, true, (GdkPoint*) gdkpoints, n);
778 
779             if (originChanged)
780                 gdk_gc_set_ts_origin(gc, 0, 0);
781         }
782 
783         if ( m_pen.IsNonTransparent() )
784         {
785 /*
786             for (i = 0 ; i < n ; i++)
787             {
788                 gdk_draw_line( m_gdkwindow, m_penGC,
789                                gdkpoints[i%n].x,
790                                gdkpoints[i%n].y,
791                                gdkpoints[(i+1)%n].x,
792                                gdkpoints[(i+1)%n].y);
793             }
794 */
795             gdk_draw_polygon( m_gdkwindow, m_penGC, FALSE, (GdkPoint*) gdkpoints, n );
796 
797         }
798     }
799 
800     delete[] gdkpoints_alloc;
801 }
802 
DoDrawRectangle(wxCoord x,wxCoord y,wxCoord width,wxCoord height)803 void wxWindowDCImpl::DoDrawRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
804 {
805     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
806 
807     wxCoord xx = XLOG2DEV(x);
808     wxCoord yy = YLOG2DEV(y);
809     wxCoord ww = m_signX * XLOG2DEVREL(width);
810     wxCoord hh = m_signY * YLOG2DEVREL(height);
811 
812     // CMB: draw nothing if transformed w or h is 0
813     if (ww == 0 || hh == 0) return;
814 
815     // CMB: handle -ve width and/or height
816     if (ww < 0) { ww = -ww; xx = xx - ww; }
817     if (hh < 0) { hh = -hh; yy = yy - hh; }
818 
819     if (m_gdkwindow)
820     {
821         if ( m_brush.IsNonTransparent() )
822         {
823             GdkGC* gc;
824             bool originChanged;
825             DrawingSetup(gc, originChanged);
826 
827             gdk_draw_rectangle(m_gdkwindow, gc, true, xx, yy, ww, hh);
828 
829             if (originChanged)
830                 gdk_gc_set_ts_origin(gc, 0, 0);
831         }
832 
833         if ( m_pen.IsNonTransparent() )
834         {
835             if ((m_pen.GetWidth() == 2) && (m_pen.GetCap() == wxCAP_ROUND) &&
836                 (m_pen.GetJoin() == wxJOIN_ROUND) && (m_pen.GetStyle() == wxPENSTYLE_SOLID))
837             {
838                 // Use 2 1-line rects instead
839                 gdk_gc_set_line_attributes( m_penGC, 1, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
840 
841                 if (m_signX == -1)
842                 {
843                     // Different for RTL
844                     gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx+1, yy, ww-2, hh-2 );
845                     gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx, yy-1, ww, hh );
846                 }
847                 else
848                 {
849                     gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx, yy, ww-2, hh-2 );
850                     gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx-1, yy-1, ww, hh );
851                 }
852 
853                 // reset
854                 gdk_gc_set_line_attributes( m_penGC, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
855             }
856             else
857             {
858                 // Just use X11 for other cases
859                 gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx, yy, ww-1, hh-1 );
860             }
861         }
862     }
863 
864     CalcBoundingBox( x, y );
865     CalcBoundingBox( x + width, y + height );
866 }
867 
DoDrawRoundedRectangle(wxCoord x,wxCoord y,wxCoord width,wxCoord height,double radius)868 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius )
869 {
870     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
871 
872     if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
873 
874     wxCoord xx = XLOG2DEV(x);
875     wxCoord yy = YLOG2DEV(y);
876     wxCoord ww = m_signX * XLOG2DEVREL(width);
877     wxCoord hh = m_signY * YLOG2DEVREL(height);
878     wxCoord rr = XLOG2DEVREL((wxCoord)radius);
879 
880     // CMB: handle -ve width and/or height
881     if (ww < 0) { ww = -ww; xx = xx - ww; }
882     if (hh < 0) { hh = -hh; yy = yy - hh; }
883 
884     // CMB: if radius is zero use DrawRectangle() instead to avoid
885     // X drawing errors with small radii
886     if (rr == 0)
887     {
888         DoDrawRectangle( x, y, width, height );
889         return;
890     }
891 
892     // CMB: draw nothing if transformed w or h is 0
893     if (ww == 0 || hh == 0) return;
894 
895     // CMB: adjust size if outline is drawn otherwise the result is
896     // 1 pixel too wide and high
897     if ( m_pen.IsNonTransparent() )
898     {
899         ww--;
900         hh--;
901     }
902 
903     if (m_gdkwindow)
904     {
905         // CMB: ensure dd is not larger than rectangle otherwise we
906         // get an hour glass shape
907         wxCoord dd = 2 * rr;
908         if (dd > ww) dd = ww;
909         if (dd > hh) dd = hh;
910         rr = dd / 2;
911 
912         if ( m_brush.IsNonTransparent() )
913         {
914             GdkGC* gc;
915             bool originChanged;
916             DrawingSetup(gc, originChanged);
917 
918             gdk_draw_rectangle(m_gdkwindow, gc, true, xx+rr, yy, ww-dd+1, hh);
919             gdk_draw_rectangle(m_gdkwindow, gc, true, xx, yy+rr, ww, hh-dd+1);
920             gdk_draw_arc(m_gdkwindow, gc, true, xx, yy, dd, dd, 90*64, 90*64);
921             gdk_draw_arc(m_gdkwindow, gc, true, xx+ww-dd, yy, dd, dd, 0, 90*64);
922             gdk_draw_arc(m_gdkwindow, gc, true, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64);
923             gdk_draw_arc(m_gdkwindow, gc, true, xx, yy+hh-dd, dd, dd, 180*64, 90*64);
924 
925             if (originChanged)
926                 gdk_gc_set_ts_origin(gc, 0, 0);
927         }
928 
929         if ( m_pen.IsNonTransparent() )
930         {
931             gdk_draw_line( m_gdkwindow, m_penGC, xx+rr+1, yy, xx+ww-rr, yy );
932             gdk_draw_line( m_gdkwindow, m_penGC, xx+rr+1, yy+hh, xx+ww-rr, yy+hh );
933             gdk_draw_line( m_gdkwindow, m_penGC, xx, yy+rr+1, xx, yy+hh-rr );
934             gdk_draw_line( m_gdkwindow, m_penGC, xx+ww, yy+rr+1, xx+ww, yy+hh-rr );
935             gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx, yy, dd, dd, 90*64, 90*64 );
936             gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
937             gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
938             gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
939         }
940     }
941 
942     // this ignores the radius
943     CalcBoundingBox( x, y );
944     CalcBoundingBox( x + width, y + height );
945 }
946 
DoDrawEllipse(wxCoord x,wxCoord y,wxCoord width,wxCoord height)947 void wxWindowDCImpl::DoDrawEllipse( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
948 {
949     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
950 
951     wxCoord xx = XLOG2DEV(x);
952     wxCoord yy = YLOG2DEV(y);
953     wxCoord ww = m_signX * XLOG2DEVREL(width);
954     wxCoord hh = m_signY * YLOG2DEVREL(height);
955 
956     // CMB: handle -ve width and/or height
957     if (ww < 0) { ww = -ww; xx = xx - ww; }
958     if (hh < 0) { hh = -hh; yy = yy - hh; }
959 
960     if (m_gdkwindow)
961     {
962         if ( m_brush.IsNonTransparent() )
963         {
964             GdkGC* gc;
965             bool originChanged;
966             DrawingSetup(gc, originChanged);
967 
968             // If the pen is transparent pen we increase the size
969             // for better compatibility with other platforms.
970             if (m_pen.IsTransparent())
971             {
972                 ++ww;
973                 ++hh;
974             }
975 
976             gdk_draw_arc(m_gdkwindow, gc, true, xx, yy, ww, hh, 0, 360*64);
977 
978             if (originChanged)
979                 gdk_gc_set_ts_origin(gc, 0, 0);
980         }
981 
982         if ( m_pen.IsNonTransparent() )
983             gdk_draw_arc( m_gdkwindow, m_penGC, false, xx, yy, ww, hh, 0, 360*64 );
984     }
985 
986     CalcBoundingBox( x, y );
987     CalcBoundingBox( x + width, y + height );
988 }
989 
DoDrawIcon(const wxIcon & icon,wxCoord x,wxCoord y)990 void wxWindowDCImpl::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
991 {
992     // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
993     DoDrawBitmap( (const wxBitmap&)icon, x, y, true );
994 }
995 
996 // scale a pixbuf
997 static GdkPixbuf*
Scale(GdkPixbuf * pixbuf,int dst_w,int dst_h,double sx,double sy)998 Scale(GdkPixbuf* pixbuf, int dst_w, int dst_h, double sx, double sy)
999 {
1000     GdkPixbuf* pixbuf_scaled = gdk_pixbuf_new(
1001         GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf), 8, dst_w, dst_h);
1002     gdk_pixbuf_scale(pixbuf, pixbuf_scaled,
1003         0, 0, dst_w, dst_h, 0, 0, sx, sy, GDK_INTERP_NEAREST);
1004     return pixbuf_scaled;
1005 }
1006 
1007 // scale part of a pixmap using pixbuf scaling
1008 static GdkPixbuf*
Scale(GdkPixmap * pixmap,int x,int y,int w,int h,int dst_w,int dst_h,double sx,double sy)1009 Scale(GdkPixmap* pixmap, int x, int y, int w, int h, int dst_w, int dst_h, double sx, double sy)
1010 {
1011     GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable(
1012         NULL, pixmap, NULL, x, y, 0, 0, w, h);
1013     GdkPixbuf* pixbuf2 = Scale(pixbuf, dst_w, dst_h, sx, sy);
1014     g_object_unref(pixbuf);
1015     return pixbuf2;
1016 }
1017 
1018 // scale part of a mask pixmap
1019 static GdkPixmap*
ScaleMask(GdkPixmap * mask,int x,int y,int w,int h,int dst_w,int dst_h,double sx,double sy)1020 ScaleMask(GdkPixmap* mask, int x, int y, int w, int h, int dst_w, int dst_h, double sx, double sy)
1021 {
1022     GdkPixbuf* pixbuf = Scale(mask, x, y, w, h, dst_w, dst_h, sx, sy);
1023 
1024     // convert black and white pixbuf back to a mono pixmap
1025     const unsigned out_rowstride = (dst_w + 7) / 8;
1026     const size_t data_size = out_rowstride * size_t(dst_h);
1027     char* data = new char[data_size];
1028     char* out = data;
1029     const guchar* row = gdk_pixbuf_get_pixels(pixbuf);
1030     const int rowstride = gdk_pixbuf_get_rowstride(pixbuf);
1031     memset(data, 0, data_size);
1032     for (int j = 0; j < dst_h; j++, row += rowstride, out += out_rowstride)
1033     {
1034         const guchar* in = row;
1035         for (int i = 0; i < dst_w; i++, in += 3)
1036             if (*in)
1037                 out[i >> 3] |= 1 << (i & 7);
1038     }
1039     g_object_unref(pixbuf);
1040     GdkPixmap* pixmap = gdk_bitmap_create_from_data(mask, data, dst_w, dst_h);
1041     delete[] data;
1042     return pixmap;
1043 }
1044 
1045 // Make a new mask from part of a mask and a clip region.
1046 static GdkPixmap*
ClipMask(GdkPixmap * mask,GdkRegion * clipRegion,int x,int y,int dst_x,int dst_y,int w,int h)1047 ClipMask(GdkPixmap* mask, GdkRegion* clipRegion, int x, int y, int dst_x, int dst_y, int w, int h)
1048 {
1049     GdkGCValues gcValues;
1050     gcValues.foreground.pixel = 0;
1051     GdkGC* gc = gdk_gc_new_with_values(mask, &gcValues, GDK_GC_FOREGROUND);
1052     GdkPixmap* pixmap = gdk_pixmap_new(mask, w, h, 1);
1053     // clear new mask, so clipped areas will be masked
1054     gdk_draw_rectangle(pixmap, gc, true, 0, 0, w, h);
1055     gdk_gc_set_clip_region(gc, clipRegion);
1056     gdk_gc_set_clip_origin(gc, -dst_x, -dst_y);
1057     // draw old mask onto new one, with clip
1058     gdk_draw_drawable(pixmap, gc, mask, x, y, 0, 0, w, h);
1059     g_object_unref(gc);
1060     return pixmap;
1061 }
1062 
1063 // make a color pixmap from part of a mono one, using text fg/bg colors
1064 GdkPixmap*
MonoToColor(GdkPixmap * monoPixmap,int x,int y,int w,int h) const1065 wxWindowDCImpl::MonoToColor(GdkPixmap* monoPixmap, int x, int y, int w, int h) const
1066 {
1067     GdkPixmap* pixmap = gdk_pixmap_new(m_gdkwindow, w, h, -1);
1068     GdkGCValues gcValues;
1069     gcValues.foreground.pixel = m_textForegroundColour.GetColor()->pixel;
1070     gcValues.background.pixel = m_textBackgroundColour.GetColor()->pixel;
1071     gcValues.stipple = monoPixmap;
1072     gcValues.fill = GDK_OPAQUE_STIPPLED;
1073     gcValues.ts_x_origin = -x;
1074     gcValues.ts_y_origin = -y;
1075     GdkGC* gc = gdk_gc_new_with_values(pixmap, &gcValues, GdkGCValuesMask(
1076         GDK_GC_FOREGROUND | GDK_GC_BACKGROUND | GDK_GC_STIPPLE | GDK_GC_FILL |
1077         GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN));
1078     gdk_draw_rectangle(pixmap, gc, true, 0, 0, w, h);
1079     g_object_unref(gc);
1080     return pixmap;
1081 }
1082 
DoDrawBitmap(const wxBitmap & bitmap,wxCoord x,wxCoord y,bool useMask)1083 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap &bitmap,
1084                                wxCoord x, wxCoord y,
1085                                bool useMask )
1086 {
1087     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1088     wxCHECK_RET( bitmap.IsOk(), wxT("invalid bitmap") );
1089 
1090     if (!m_gdkwindow) return;
1091 
1092     const int w = bitmap.GetWidth();
1093     const int h = bitmap.GetHeight();
1094 
1095     // notice that as the bitmap is not drawn upside down (or right to left)
1096     // even if the corresponding axis direction is inversed, we need to take it
1097     // into account when calculating its bounding box
1098     CalcBoundingBox(x, y);
1099     CalcBoundingBox(x + m_signX*w, y + m_signY*h);
1100 
1101     // device coords
1102     int xx = LogicalToDeviceX(x);
1103     const int yy = LogicalToDeviceY(y);
1104     const int ww = LogicalToDeviceXRel(w);
1105     const int hh = LogicalToDeviceYRel(h);
1106 
1107     if (m_window && m_window->GetLayoutDirection() == wxLayout_RightToLeft)
1108         xx -= ww;
1109 
1110     GdkRegion* const clipRegion = m_currentClippingRegion.GetRegion();
1111     // determine clip region overlap
1112     int overlap = wxInRegion;
1113     if (clipRegion)
1114     {
1115         overlap = m_currentClippingRegion.Contains(xx, yy, ww, hh);
1116         if (overlap == wxOutRegion)
1117             return;
1118     }
1119 
1120     const bool isScaled = ww != w || hh != h;
1121     const bool hasAlpha = bitmap.HasAlpha();
1122     GdkGC* const use_gc = m_penGC;
1123 
1124     GdkPixmap* mask = NULL;
1125     // mask does not work when drawing a pixbuf with alpha
1126     if (useMask && !hasAlpha)
1127     {
1128         wxMask* m = bitmap.GetMask();
1129         if (m)
1130             mask = *m;
1131     }
1132 
1133     GdkPixmap* mask_new = NULL;
1134     if (mask)
1135     {
1136         if (isScaled)
1137         {
1138             mask = ScaleMask(mask, 0, 0, w, h, ww, hh, m_scaleX, m_scaleY);
1139             mask_new = mask;
1140         }
1141         if (overlap == wxPartRegion)
1142         {
1143             // need a new mask that also masks the clipped area,
1144             // because gc can't have both a mask and a clip region
1145             mask = ClipMask(mask, clipRegion, 0, 0, xx, yy, ww, hh);
1146             if (mask_new)
1147                 g_object_unref(mask_new);
1148             mask_new = mask;
1149         }
1150         gdk_gc_set_clip_mask(use_gc, mask);
1151         gdk_gc_set_clip_origin(use_gc, xx, yy);
1152     }
1153 
1154     // determine whether to use pixmap or pixbuf
1155     GdkPixmap* pixmap = NULL;
1156     GdkPixmap* pixmap_new = NULL;
1157     GdkPixbuf* pixbuf = NULL;
1158     GdkPixbuf* pixbuf_new = NULL;
1159     if (bitmap.HasPixmap())
1160         pixmap = bitmap.GetPixmap();
1161     if (pixmap && gdk_drawable_get_depth(pixmap) == 1)
1162     {
1163         if (gdk_drawable_get_depth(m_gdkwindow) != 1)
1164         {
1165             // convert mono pixmap to color using text fg/bg colors
1166             pixmap = MonoToColor(pixmap, 0, 0, w, h);
1167             pixmap_new = pixmap;
1168         }
1169     }
1170     else if (hasAlpha || pixmap == NULL)
1171         pixbuf = bitmap.GetPixbuf();
1172 
1173     if (isScaled)
1174     {
1175         if (pixbuf)
1176             pixbuf = Scale(pixbuf, ww, hh, m_scaleX, m_scaleY);
1177         else
1178             pixbuf = Scale(pixmap, 0, 0, w, h, ww, hh, m_scaleX, m_scaleY);
1179 
1180         pixbuf_new = pixbuf;
1181     }
1182 
1183     if (pixbuf)
1184     {
1185         gdk_draw_pixbuf(m_gdkwindow, use_gc, pixbuf,
1186             0, 0, xx, yy, ww, hh, GDK_RGB_DITHER_NORMAL, 0, 0);
1187     }
1188     else
1189     {
1190         gdk_draw_drawable(m_gdkwindow, use_gc, pixmap, 0, 0, xx, yy, ww, hh);
1191     }
1192 
1193     if (pixbuf_new)
1194         g_object_unref(pixbuf_new);
1195     if (pixmap_new)
1196         g_object_unref(pixmap_new);
1197     if (mask)
1198     {
1199         gdk_gc_set_clip_region(use_gc, clipRegion);
1200 
1201         // Notice that we can only release the mask now, we can't do it before
1202         // the calls to gdk_draw_xxx() above as they crash with BadPixmap X
1203         // error with GTK+ 2.16 and earlier.
1204         if (mask_new)
1205             g_object_unref(mask_new);
1206     }
1207 }
1208 
DoBlit(wxCoord xdest,wxCoord ydest,wxCoord width,wxCoord height,wxDC * source,wxCoord xsrc,wxCoord ysrc,wxRasterOperationMode logical_func,bool useMask,wxCoord xsrcMask,wxCoord ysrcMask)1209 bool wxWindowDCImpl::DoBlit( wxCoord xdest, wxCoord ydest,
1210                          wxCoord width, wxCoord height,
1211                          wxDC *source,
1212                          wxCoord xsrc, wxCoord ysrc,
1213                          wxRasterOperationMode logical_func,
1214                          bool useMask,
1215                          wxCoord xsrcMask, wxCoord ysrcMask )
1216 {
1217     wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1218     wxCHECK_MSG( source, false, wxT("invalid source dc") );
1219 
1220     if (!m_gdkwindow) return false;
1221 
1222     GdkDrawable* srcDrawable = NULL;
1223     GdkPixmap* mask = NULL;
1224     wxMemoryDC* memDC = wxDynamicCast(source, wxMemoryDC);
1225     if (memDC)
1226     {
1227         const wxBitmap& bitmap = memDC->GetSelectedBitmap();
1228         if (!bitmap.IsOk())
1229             return false;
1230         srcDrawable = bitmap.GetPixmap();
1231         if (useMask)
1232         {
1233             wxMask* m = bitmap.GetMask();
1234             if (m)
1235                 mask = *m;
1236         }
1237     }
1238     else
1239     {
1240         wxDCImpl* impl = source->GetImpl();
1241         wxWindowDCImpl* gtk_impl = wxDynamicCast(impl, wxWindowDCImpl);
1242         if (gtk_impl)
1243             srcDrawable = gtk_impl->GetGDKWindow();
1244         if (srcDrawable == NULL)
1245             return false;
1246     }
1247 
1248     CalcBoundingBox(xdest, ydest);
1249     CalcBoundingBox(xdest + width, ydest + height);
1250 
1251     // source device coords
1252     int src_x = source->LogicalToDeviceX(xsrc);
1253     int src_y = source->LogicalToDeviceY(ysrc);
1254     int src_w = source->LogicalToDeviceXRel(width);
1255     int src_h = source->LogicalToDeviceYRel(height);
1256 
1257     // Clip source rect to source dc.
1258     // Only necessary when scaling, to avoid GDK errors when
1259     // converting to pixbuf, but no harm in always doing it.
1260     // If source rect changes, it also changes the dest rect.
1261     wxRect clip;
1262     gdk_drawable_get_size(srcDrawable, &clip.width, &clip.height);
1263     clip.Intersect(wxRect(src_x, src_y, src_w, src_h));
1264     if (src_w != clip.width || src_h != clip.height)
1265     {
1266         if (clip.width == 0)
1267             return true;
1268 
1269         src_w = clip.width;
1270         src_h = clip.height;
1271         width  = source->DeviceToLogicalXRel(src_w);
1272         height = source->DeviceToLogicalYRel(src_h);
1273         if (src_x != clip.x || src_y != clip.y)
1274         {
1275             xdest += source->DeviceToLogicalXRel(clip.x - src_x);
1276             ydest += source->DeviceToLogicalYRel(clip.y - src_y);
1277             src_x = clip.x;
1278             src_y = clip.y;
1279         }
1280     }
1281 
1282     // destination device coords
1283     const int dst_x = LogicalToDeviceX(xdest);
1284     const int dst_y = LogicalToDeviceY(ydest);
1285     const int dst_w = LogicalToDeviceXRel(width);
1286     const int dst_h = LogicalToDeviceYRel(height);
1287 
1288     GdkRegion* const clipRegion = m_currentClippingRegion.GetRegion();
1289     // determine dest clip region overlap
1290     int overlap = wxInRegion;
1291     if (clipRegion)
1292     {
1293         overlap = m_currentClippingRegion.Contains(dst_x, dst_y, dst_w, dst_h);
1294         if (overlap == wxOutRegion)
1295             return true;
1296     }
1297 
1298     const bool isScaled = src_w != dst_w || src_h != dst_h;
1299     double scale_x = 0;
1300     double scale_y = 0;
1301     if (isScaled)
1302     {
1303         // get source to dest scale
1304         double usx, usy, lsx, lsy;
1305         source->GetUserScale(&usx, &usy);
1306         source->GetLogicalScale(&lsx, &lsy);
1307         scale_x = m_scaleX / (usx * lsx);
1308         scale_y = m_scaleY / (usy * lsy);
1309     }
1310 
1311     GdkGC* const use_gc = m_penGC;
1312 
1313     GdkPixmap* mask_new = NULL;
1314     if (mask)
1315     {
1316         int srcMask_x = src_x;
1317         int srcMask_y = src_y;
1318         if (xsrcMask != -1 || ysrcMask != -1)
1319         {
1320             srcMask_x = source->LogicalToDeviceX(xsrcMask);
1321             srcMask_y = source->LogicalToDeviceY(ysrcMask);
1322         }
1323         if (isScaled)
1324         {
1325             mask = ScaleMask(mask, srcMask_x, srcMask_y,
1326                 src_w, src_h, dst_w, dst_h, scale_x, scale_y);
1327             mask_new = mask;
1328             srcMask_x = 0;
1329             srcMask_y = 0;
1330         }
1331         if (overlap == wxPartRegion)
1332         {
1333             // need a new mask that also masks the clipped area,
1334             // because gc can't have both a mask and a clip region
1335             mask = ClipMask(mask, clipRegion,
1336                 srcMask_x, srcMask_y, dst_x, dst_y, dst_w, dst_h);
1337             if (mask_new)
1338                 g_object_unref(mask_new);
1339             mask_new = mask;
1340             srcMask_x = 0;
1341             srcMask_y = 0;
1342         }
1343         gdk_gc_set_clip_mask(use_gc, mask);
1344         gdk_gc_set_clip_origin(use_gc, dst_x - srcMask_x, dst_y - srcMask_y);
1345     }
1346 
1347     GdkPixmap* pixmap = NULL;
1348     if (gdk_drawable_get_depth(srcDrawable) == 1 &&
1349         (gdk_drawable_get_depth(m_gdkwindow) != 1 || isScaled))
1350     {
1351         // Convert mono pixmap to color using text fg/bg colors.
1352         // Scaling/drawing is simpler if this is done first.
1353         pixmap = MonoToColor(srcDrawable, src_x, src_y, src_w, src_h);
1354         srcDrawable = pixmap;
1355         src_x = 0;
1356         src_y = 0;
1357     }
1358 
1359     const wxRasterOperationMode logical_func_save = m_logicalFunction;
1360     SetLogicalFunction(logical_func);
1361     if (memDC == NULL)
1362         gdk_gc_set_subwindow(use_gc, GDK_INCLUDE_INFERIORS);
1363 
1364     if (isScaled)
1365     {
1366         GdkPixbuf* pixbuf = Scale(srcDrawable,
1367             src_x, src_y, src_w, src_h, dst_w, dst_h, scale_x, scale_y);
1368         gdk_draw_pixbuf(m_gdkwindow, use_gc, pixbuf,
1369             0, 0, dst_x, dst_y, dst_w, dst_h, GDK_RGB_DITHER_NONE, 0, 0);
1370         g_object_unref(pixbuf);
1371     }
1372     else
1373     {
1374         gdk_draw_drawable(m_gdkwindow, use_gc, srcDrawable,
1375             src_x, src_y, dst_x, dst_y, dst_w, dst_h);
1376     }
1377 
1378     SetLogicalFunction(logical_func_save);
1379     if (memDC == NULL)
1380         gdk_gc_set_subwindow(use_gc, GDK_CLIP_BY_CHILDREN);
1381 
1382     if (pixmap)
1383         g_object_unref(pixmap);
1384     if (mask)
1385     {
1386         gdk_gc_set_clip_region(use_gc, clipRegion);
1387         // see comment at end of DoDrawBitmap()
1388         if (mask_new)
1389             g_object_unref(mask_new);
1390     }
1391 
1392     return true;
1393 }
1394 
DoDrawText(const wxString & text,wxCoord xLogical,wxCoord yLogical)1395 void wxWindowDCImpl::DoDrawText(const wxString& text,
1396                                 wxCoord xLogical,
1397                                 wxCoord yLogical)
1398 {
1399     DoDrawRotatedText(text, xLogical, yLogical, 0);
1400 }
1401 
DoDrawRotatedText(const wxString & str,int xLogical,int yLogical,double angle)1402 void wxWindowDCImpl::DoDrawRotatedText(const wxString& str, int xLogical, int yLogical, double angle)
1403 {
1404     if (!m_gdkwindow || str.empty())
1405         return;
1406 
1407     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1408 
1409     pango_layout_set_text(m_layout, wxGTK_CONV(str), -1);
1410     const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout);
1411 
1412     const GdkColor* bg_col = NULL;
1413     if (m_backgroundMode == wxBRUSHSTYLE_SOLID)
1414         bg_col = m_textBackgroundColour.GetColor();
1415 
1416     PangoMatrix matrix = PANGO_MATRIX_INIT;
1417     if (!wxIsSameDouble(m_scaleX, 1) || !wxIsSameDouble(m_scaleY, 1) || !wxIsNullDouble(angle))
1418     {
1419         pango_matrix_scale(&matrix, m_scaleX, m_scaleY);
1420         pango_matrix_rotate(&matrix, angle);
1421         pango_context_set_matrix(m_context, &matrix);
1422         pango_layout_context_changed(m_layout);
1423     }
1424 
1425     int w, h;
1426     pango_layout_get_pixel_size(m_layout, &w, &h);
1427 
1428     int x = LogicalToDeviceX(xLogical);
1429     int y = LogicalToDeviceY(yLogical);
1430     if (m_window && m_window->GetLayoutDirection() == wxLayout_RightToLeft)
1431         x -= LogicalToDeviceXRel(w);
1432 
1433     if (wxIsNullDouble(angle))
1434     {
1435         CalcBoundingBox(xLogical, yLogical);
1436         CalcBoundingBox(xLogical + w, yLogical + h);
1437     }
1438     else
1439     {
1440         // To be compatible with MSW, the rotation axis must be in the old
1441         // top-left corner.
1442         // Calculate the vertices of the rotated rectangle containing the text,
1443         // relative to the old top-left vertex.
1444         // the rectangle vertices are counted clockwise with the first one
1445         // being at (0, 0)
1446         double x2 = w * matrix.xx;
1447         double y2 = w * matrix.yx;
1448         double x4 = h * matrix.xy;
1449         double y4 = h * matrix.yy;
1450         double x3 = x4 + x2;
1451         double y3 = y4 + y2;
1452         // Then we calculate max and min of the rotated rectangle.
1453         wxCoord maxX = (wxCoord)(dmax(dmax(0, x2), dmax(x3, x4)) + 0.5),
1454                 maxY = (wxCoord)(dmax(dmax(0, y2), dmax(y3, y4)) + 0.5),
1455                 minX = (wxCoord)(dmin(dmin(0, x2), dmin(x3, x4)) - 0.5),
1456                 minY = (wxCoord)(dmin(dmin(0, y2), dmin(y3, y4)) - 0.5);
1457         x += minX;
1458         y += minY;
1459         CalcBoundingBox(DeviceToLogicalX(x), DeviceToLogicalY(y));
1460         CalcBoundingBox(DeviceToLogicalX(x + maxX - minX), DeviceToLogicalY(y + maxY - minY));
1461     }
1462 
1463     gdk_draw_layout_with_colors(m_gdkwindow, m_textGC, x, y, m_layout, NULL, bg_col);
1464 
1465     pango_context_set_matrix(m_context, NULL);
1466     if (setAttrs)
1467         pango_layout_set_attributes(m_layout, NULL);
1468 }
1469 
DoGetTextExtent(const wxString & string,wxCoord * width,wxCoord * height,wxCoord * descent,wxCoord * externalLeading,const wxFont * theFont) const1470 void wxWindowDCImpl::DoGetTextExtent(const wxString &string,
1471                                  wxCoord *width, wxCoord *height,
1472                                  wxCoord *descent, wxCoord *externalLeading,
1473                                  const wxFont *theFont) const
1474 {
1475     // ensure we work with a valid font
1476     const wxFont *fontToUse;
1477     if ( !theFont || !theFont->IsOk() )
1478         fontToUse = &m_font;
1479     else
1480         fontToUse = theFont;
1481 
1482     wxCHECK_RET( fontToUse->IsOk(), wxT("invalid font") );
1483 
1484     wxTextMeasure txm(GetOwner(), fontToUse);
1485     txm.GetTextExtent(string, width, height, descent, externalLeading);
1486 }
1487 
1488 
DoGetPartialTextExtents(const wxString & text,wxArrayInt & widths) const1489 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString& text,
1490                                          wxArrayInt& widths) const
1491 {
1492     wxCHECK_MSG( m_font.IsOk(), false, wxT("Invalid font") );
1493 
1494     wxTextMeasure txm(GetOwner(), &m_font);
1495     return txm.GetPartialTextExtents(text, widths, m_scaleX);
1496 }
1497 
1498 
GetCharWidth() const1499 wxCoord wxWindowDCImpl::GetCharWidth() const
1500 {
1501     pango_layout_set_text( m_layout, "H", 1 );
1502     int w;
1503     pango_layout_get_pixel_size( m_layout, &w, NULL );
1504     return w;
1505 }
1506 
GetCharHeight() const1507 wxCoord wxWindowDCImpl::GetCharHeight() const
1508 {
1509     PangoFontMetrics *metrics = pango_context_get_metrics (m_context, m_fontdesc, pango_context_get_language(m_context));
1510     wxCHECK_MSG( metrics, -1, wxT("failed to get pango font metrics") );
1511 
1512     wxCoord h = PANGO_PIXELS (pango_font_metrics_get_descent (metrics) +
1513                               pango_font_metrics_get_ascent (metrics));
1514     pango_font_metrics_unref (metrics);
1515     return h;
1516 }
1517 
Clear()1518 void wxWindowDCImpl::Clear()
1519 {
1520     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1521 
1522     if (!m_gdkwindow) return;
1523 
1524     int width,height;
1525     DoGetSize( &width, &height );
1526     gdk_draw_rectangle( m_gdkwindow, m_bgGC, TRUE, 0, 0, width, height );
1527 }
1528 
SetFont(const wxFont & font)1529 void wxWindowDCImpl::SetFont( const wxFont &font )
1530 {
1531     m_font = font;
1532 
1533     if (m_font.IsOk())
1534     {
1535         if (m_fontdesc)
1536             pango_font_description_free( m_fontdesc );
1537 
1538         m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1539 
1540 
1541         if (m_window)
1542         {
1543             PangoContext *oldContext = m_context;
1544 
1545             m_context = m_window->GTKGetPangoDefaultContext();
1546 
1547             // If we switch back/forth between different contexts
1548             // we also have to create a new layout. I think so,
1549             // at least, and it doesn't hurt to do it.
1550             if (oldContext != m_context)
1551             {
1552                 if (m_layout)
1553                     g_object_unref (m_layout);
1554 
1555                 m_layout = pango_layout_new( m_context );
1556             }
1557         }
1558 
1559         pango_layout_set_font_description( m_layout, m_fontdesc );
1560     }
1561 }
1562 
SetPen(const wxPen & pen)1563 void wxWindowDCImpl::SetPen( const wxPen &pen )
1564 {
1565     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1566 
1567     if (m_pen == pen && (!pen.IsOk() || pen.GetStyle() != wxPENSTYLE_USER_DASH))
1568         return;
1569 
1570     m_pen = pen;
1571 
1572     if (!m_pen.IsOk()) return;
1573 
1574     if (!m_gdkwindow) return;
1575 
1576     gint width = m_pen.GetWidth();
1577     if (width <= 0)
1578     {
1579         // CMB: if width is non-zero scale it with the dc
1580         width = 1;
1581     }
1582     else
1583     {
1584         // X doesn't allow different width in x and y and so we take
1585         // the average
1586         double w = 0.5 +
1587                    ( fabs((double) XLOG2DEVREL(width)) +
1588                      fabs((double) YLOG2DEVREL(width)) ) / 2.0;
1589         width = (int)w;
1590         if ( !width )
1591         {
1592             // width can't be 0 or an internal GTK error occurs inside
1593             // gdk_gc_set_dashes() below
1594             width = 1;
1595         }
1596     }
1597 
1598     static const wxGTKDash dotted[] = {1, 1};
1599     static const wxGTKDash short_dashed[] = {2, 2};
1600     static const wxGTKDash wxCoord_dashed[] = {2, 4};
1601     static const wxGTKDash dotted_dashed[] = {3, 3, 1, 3};
1602 
1603     // We express dash pattern in pen width unit, so we are
1604     // independent of zoom factor and so on...
1605     int req_nb_dash;
1606     const wxGTKDash *req_dash;
1607 
1608     GdkLineStyle lineStyle = GDK_LINE_ON_OFF_DASH;
1609     switch (m_pen.GetStyle())
1610     {
1611         case wxPENSTYLE_USER_DASH:
1612             req_nb_dash = m_pen.GetDashCount();
1613             req_dash = (wxGTKDash*)m_pen.GetDash();
1614             break;
1615         case wxPENSTYLE_DOT:
1616             req_nb_dash = 2;
1617             req_dash = dotted;
1618             break;
1619         case wxPENSTYLE_LONG_DASH:
1620             req_nb_dash = 2;
1621             req_dash = wxCoord_dashed;
1622             break;
1623         case wxPENSTYLE_SHORT_DASH:
1624             req_nb_dash = 2;
1625             req_dash = short_dashed;
1626             break;
1627         case wxPENSTYLE_DOT_DASH:
1628             req_nb_dash = 4;
1629             req_dash = dotted_dashed;
1630             break;
1631 
1632         case wxPENSTYLE_TRANSPARENT:
1633         case wxPENSTYLE_STIPPLE_MASK_OPAQUE:
1634         case wxPENSTYLE_STIPPLE:
1635         case wxPENSTYLE_SOLID:
1636         default:
1637             lineStyle = GDK_LINE_SOLID;
1638             req_dash = NULL;
1639             req_nb_dash = 0;
1640             break;
1641     }
1642 
1643     if (req_dash && req_nb_dash)
1644     {
1645         wxGTKDash *real_req_dash = new wxGTKDash[req_nb_dash];
1646         if (real_req_dash)
1647         {
1648             for (int i = 0; i < req_nb_dash; i++)
1649                 real_req_dash[i] = req_dash[i] * width;
1650             gdk_gc_set_dashes( m_penGC, 0, real_req_dash, req_nb_dash );
1651             delete[] real_req_dash;
1652         }
1653         else
1654         {
1655             // No Memory. We use non-scaled dash pattern...
1656             gdk_gc_set_dashes( m_penGC, 0, (wxGTKDash*)req_dash, req_nb_dash );
1657         }
1658     }
1659 
1660     GdkCapStyle capStyle = GDK_CAP_ROUND;
1661     switch (m_pen.GetCap())
1662     {
1663         case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
1664         case wxCAP_BUTT:       { capStyle = GDK_CAP_BUTT;       break; }
1665         case wxCAP_ROUND:
1666         default:
1667             if (width <= 1)
1668             {
1669                 width = 0;
1670                 capStyle = GDK_CAP_NOT_LAST;
1671             }
1672             break;
1673     }
1674 
1675     GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
1676     switch (m_pen.GetJoin())
1677     {
1678         case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
1679         case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
1680         case wxJOIN_ROUND:
1681         default:           { joinStyle = GDK_JOIN_ROUND; break; }
1682     }
1683 
1684     gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
1685 
1686     m_pen.GetColour().CalcPixel( m_cmap );
1687     gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
1688 }
1689 
SetBrush(const wxBrush & brush)1690 void wxWindowDCImpl::SetBrush( const wxBrush &brush )
1691 {
1692     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1693 
1694     if (m_brush == brush) return;
1695 
1696     m_brush = brush;
1697 
1698     if (!m_brush.IsOk()) return;
1699 
1700     if (!m_gdkwindow) return;
1701 
1702     m_brush.GetColour().CalcPixel( m_cmap );
1703     gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
1704 
1705     gdk_gc_set_fill( m_brushGC, GDK_SOLID );
1706 
1707     if ((m_brush.GetStyle() == wxBRUSHSTYLE_STIPPLE) && (m_brush.GetStipple()->IsOk()))
1708     {
1709         if (m_brush.GetStipple()->GetDepth() != 1)
1710         {
1711             gdk_gc_set_fill( m_brushGC, GDK_TILED );
1712             gdk_gc_set_tile( m_brushGC, m_brush.GetStipple()->GetPixmap() );
1713         }
1714         else
1715         {
1716             gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
1717             gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() );
1718         }
1719     }
1720 
1721     if ((m_brush.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
1722     {
1723         gdk_gc_set_fill( m_textGC, GDK_OPAQUE_STIPPLED);
1724         gdk_gc_set_stipple( m_textGC, *m_brush.GetStipple()->GetMask() );
1725     }
1726 
1727     if (m_brush.IsHatch())
1728     {
1729         gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
1730         gdk_gc_set_stipple(m_brushGC, GetHatch(m_brush.GetStyle()));
1731     }
1732 }
1733 
SetBackground(const wxBrush & brush)1734 void wxWindowDCImpl::SetBackground( const wxBrush &brush )
1735 {
1736    /* CMB 21/7/98: Added SetBackground. Sets background brush
1737     * for Clear() and bg colour for shapes filled with cross-hatch brush */
1738 
1739     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1740 
1741     if (m_backgroundBrush == brush) return;
1742 
1743     m_backgroundBrush = brush;
1744 
1745     if (!m_backgroundBrush.IsOk()) return;
1746 
1747     if (!m_gdkwindow) return;
1748 
1749     wxColor color = m_backgroundBrush.GetColour();
1750     color.CalcPixel(m_cmap);
1751     const GdkColor* gdkColor = color.GetColor();
1752     gdk_gc_set_background(m_brushGC, gdkColor);
1753     gdk_gc_set_background(m_penGC,   gdkColor);
1754     gdk_gc_set_background(m_bgGC,    gdkColor);
1755     gdk_gc_set_foreground(m_bgGC,    gdkColor);
1756 
1757 
1758     gdk_gc_set_fill( m_bgGC, GDK_SOLID );
1759 
1760     if (m_backgroundBrush.GetStyle() == wxBRUSHSTYLE_STIPPLE)
1761     {
1762         const wxBitmap* stipple = m_backgroundBrush.GetStipple();
1763         if (stipple->IsOk())
1764         {
1765             if (stipple->GetDepth() != 1)
1766             {
1767                 gdk_gc_set_fill(m_bgGC, GDK_TILED);
1768                 gdk_gc_set_tile(m_bgGC, stipple->GetPixmap());
1769             }
1770             else
1771             {
1772                 gdk_gc_set_fill(m_bgGC, GDK_STIPPLED);
1773                 gdk_gc_set_stipple(m_bgGC, stipple->GetPixmap());
1774             }
1775         }
1776     }
1777     else if (m_backgroundBrush.IsHatch())
1778     {
1779         gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
1780         gdk_gc_set_stipple(m_bgGC, GetHatch(m_backgroundBrush.GetStyle()));
1781     }
1782 }
1783 
SetLogicalFunction(wxRasterOperationMode function)1784 void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function )
1785 {
1786     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1787 
1788     if (m_logicalFunction == function)
1789         return;
1790 
1791     // VZ: shouldn't this be a CHECK?
1792     if (!m_gdkwindow)
1793         return;
1794 
1795     GdkFunction mode;
1796     switch (function)
1797     {
1798         case wxXOR:          mode = GDK_XOR;           break;
1799         case wxINVERT:       mode = GDK_INVERT;        break;
1800         case wxOR_REVERSE:   mode = GDK_OR_REVERSE;    break;
1801         case wxAND_REVERSE:  mode = GDK_AND_REVERSE;   break;
1802         case wxCLEAR:        mode = GDK_CLEAR;         break;
1803         case wxSET:          mode = GDK_SET;           break;
1804         case wxOR_INVERT:    mode = GDK_OR_INVERT;     break;
1805         case wxAND:          mode = GDK_AND;           break;
1806         case wxOR:           mode = GDK_OR;            break;
1807         case wxEQUIV:        mode = GDK_EQUIV;         break;
1808         case wxNAND:         mode = GDK_NAND;          break;
1809         case wxAND_INVERT:   mode = GDK_AND_INVERT;    break;
1810         case wxCOPY:         mode = GDK_COPY;          break;
1811         case wxNO_OP:        mode = GDK_NOOP;          break;
1812         case wxSRC_INVERT:   mode = GDK_COPY_INVERT;   break;
1813         case wxNOR:          mode = GDK_NOR;           break;
1814         default:
1815             wxFAIL_MSG("unknown mode");
1816             return;
1817     }
1818 
1819     m_logicalFunction = function;
1820 
1821     gdk_gc_set_function( m_penGC, mode );
1822     gdk_gc_set_function( m_brushGC, mode );
1823 
1824     // to stay compatible with wxMSW, we don't apply ROPs to the text
1825     // operations (i.e. DrawText/DrawRotatedText).
1826     // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
1827     gdk_gc_set_function( m_textGC, mode );
1828 }
1829 
SetTextForeground(const wxColour & col)1830 void wxWindowDCImpl::SetTextForeground( const wxColour &col )
1831 {
1832     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1833 
1834     // don't set m_textForegroundColour to an invalid colour as we'd crash
1835     // later then (we use m_textForegroundColour.GetColor() without checking
1836     // in a few places)
1837     if ( !col.IsOk() || (m_textForegroundColour == col) )
1838         return;
1839 
1840     m_textForegroundColour = col;
1841 
1842     if ( m_gdkwindow )
1843     {
1844         m_textForegroundColour.CalcPixel( m_cmap );
1845         gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
1846     }
1847 }
1848 
SetTextBackground(const wxColour & col)1849 void wxWindowDCImpl::SetTextBackground( const wxColour &col )
1850 {
1851     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1852 
1853     // same as above
1854     if ( !col.IsOk() || (m_textBackgroundColour == col) )
1855         return;
1856 
1857     m_textBackgroundColour = col;
1858 
1859     if ( m_gdkwindow )
1860     {
1861         m_textBackgroundColour.CalcPixel( m_cmap );
1862         gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
1863     }
1864 }
1865 
SetBackgroundMode(int mode)1866 void wxWindowDCImpl::SetBackgroundMode( int mode )
1867 {
1868     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1869 
1870     m_backgroundMode = mode;
1871 }
1872 
SetPalette(const wxPalette & WXUNUSED (palette))1873 void wxWindowDCImpl::SetPalette( const wxPalette& WXUNUSED(palette) )
1874 {
1875     wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
1876 }
1877 
DoSetClippingRegion(wxCoord x,wxCoord y,wxCoord width,wxCoord height)1878 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
1879 {
1880     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1881 
1882     if (!m_gdkwindow) return;
1883 
1884     wxRect rect;
1885     rect.x = XLOG2DEV(x);
1886     rect.y = YLOG2DEV(y);
1887     rect.width = XLOG2DEVREL(width);
1888     rect.height = YLOG2DEVREL(height);
1889 
1890     if (m_window && m_window->m_wxwindow &&
1891         (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
1892     {
1893         rect.x -= rect.width;
1894     }
1895 
1896     DoSetDeviceClippingRegion(wxRegion(rect));
1897 }
1898 
DoSetDeviceClippingRegion(const wxRegion & region)1899 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion &region  )
1900 {
1901     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1902 
1903     if (!m_gdkwindow) return;
1904 
1905     if (!m_currentClippingRegion.IsNull())
1906         m_currentClippingRegion.Intersect( region );
1907     else
1908         m_currentClippingRegion.Union( region );
1909 
1910 #if USE_PAINT_REGION
1911     if (!m_paintClippingRegion.IsNull())
1912         m_currentClippingRegion.Intersect( m_paintClippingRegion );
1913 #endif
1914 
1915     wxCoord xx, yy, ww, hh;
1916     m_currentClippingRegion.GetBox( xx, yy, ww, hh );
1917     wxGTKDCImpl::DoSetClippingRegion( xx, yy, ww, hh );
1918 
1919     GdkRegion* gdkRegion = m_currentClippingRegion.GetRegion();
1920     gdk_gc_set_clip_region(m_penGC,   gdkRegion);
1921     gdk_gc_set_clip_region(m_brushGC, gdkRegion);
1922     gdk_gc_set_clip_region(m_textGC,  gdkRegion);
1923     gdk_gc_set_clip_region(m_bgGC,    gdkRegion);
1924 }
1925 
DestroyClippingRegion()1926 void wxWindowDCImpl::DestroyClippingRegion()
1927 {
1928     wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1929 
1930     wxDCImpl::DestroyClippingRegion();
1931 
1932     m_currentClippingRegion.Clear();
1933 
1934 #if USE_PAINT_REGION
1935     if (!m_paintClippingRegion.IsEmpty())
1936         m_currentClippingRegion.Union( m_paintClippingRegion );
1937 #endif
1938 
1939     if (!m_gdkwindow) return;
1940 
1941     GdkRegion* gdkRegion = NULL;
1942     if (!m_currentClippingRegion.IsEmpty())
1943         gdkRegion = m_currentClippingRegion.GetRegion();
1944 
1945     gdk_gc_set_clip_region(m_penGC,   gdkRegion);
1946     gdk_gc_set_clip_region(m_brushGC, gdkRegion);
1947     gdk_gc_set_clip_region(m_textGC,  gdkRegion);
1948     gdk_gc_set_clip_region(m_bgGC,    gdkRegion);
1949 }
1950 
Destroy()1951 void wxWindowDCImpl::Destroy()
1952 {
1953     if (m_penGC) wxFreePoolGC( m_penGC );
1954     m_penGC = NULL;
1955     if (m_brushGC) wxFreePoolGC( m_brushGC );
1956     m_brushGC = NULL;
1957     if (m_textGC) wxFreePoolGC( m_textGC );
1958     m_textGC = NULL;
1959     if (m_bgGC) wxFreePoolGC( m_bgGC );
1960     m_bgGC = NULL;
1961 }
1962 
SetDeviceOrigin(wxCoord x,wxCoord y)1963 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x, wxCoord y )
1964 {
1965     m_deviceOriginX = x;
1966     m_deviceOriginY = y;
1967 
1968     ComputeScaleAndOrigin();
1969 }
1970 
SetAxisOrientation(bool xLeftRight,bool yBottomUp)1971 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1972 {
1973     m_signX = (xLeftRight ?  1 : -1);
1974     m_signY = (yBottomUp  ? -1 :  1);
1975 
1976     if (m_window && m_window->m_wxwindow &&
1977         (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
1978         m_signX = -m_signX;
1979 
1980     ComputeScaleAndOrigin();
1981 }
1982 
ComputeScaleAndOrigin()1983 void wxWindowDCImpl::ComputeScaleAndOrigin()
1984 {
1985     const wxRealPoint origScale(m_scaleX, m_scaleY);
1986 
1987     wxDCImpl::ComputeScaleAndOrigin();
1988 
1989     // if scale has changed call SetPen to recalculate the line width
1990     if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.IsOk() )
1991     {
1992         // this is a bit artificial, but we need to force wxDC to think the pen
1993         // has changed
1994         wxPen pen = m_pen;
1995         m_pen = wxNullPen;
1996         SetPen( pen );
1997     }
1998 }
1999 
2000 // Resolution in pixels per logical inch
GetPPI() const2001 wxSize wxWindowDCImpl::GetPPI() const
2002 {
2003     return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5));
2004 }
2005 
GetDepth() const2006 int wxWindowDCImpl::GetDepth() const
2007 {
2008     return gdk_drawable_get_depth(m_gdkwindow);
2009 }
2010 
2011 //-----------------------------------------------------------------------------
2012 // wxClientDCImpl
2013 //-----------------------------------------------------------------------------
2014 
IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl,wxWindowDCImpl)2015 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
2016 
2017 wxClientDCImpl::wxClientDCImpl( wxDC *owner )
2018           : wxWindowDCImpl( owner )
2019 {
2020 }
2021 
wxClientDCImpl(wxDC * owner,wxWindow * win)2022 wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *win )
2023           : wxWindowDCImpl( owner, win )
2024 {
2025     wxCHECK_RET( win, wxT("NULL window in wxClientDCImpl::wxClientDC") );
2026 
2027 #ifdef __WXUNIVERSAL__
2028     wxPoint ptOrigin = win->GetClientAreaOrigin();
2029     SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
2030     wxSize size = win->GetClientSize();
2031     DoSetClippingRegion(0, 0, size.x, size.y);
2032 #endif
2033     // __WXUNIVERSAL__
2034 }
2035 
DoGetSize(int * width,int * height) const2036 void wxClientDCImpl::DoGetSize(int *width, int *height) const
2037 {
2038     wxCHECK_RET( m_window, wxT("GetSize() doesn't work without window") );
2039 
2040     m_window->GetClientSize(width, height);
2041 }
2042 
2043 //-----------------------------------------------------------------------------
2044 // wxPaintDCImpl
2045 //-----------------------------------------------------------------------------
2046 
IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl,wxClientDCImpl)2047 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxClientDCImpl)
2048 
2049 // Limit the paint region to the window size. Sometimes
2050 // the paint region is too big, and this risks X11 errors
2051 static void wxLimitRegionToSize(wxRegion& region, const wxSize& sz)
2052 {
2053     wxRect originalRect = region.GetBox();
2054     wxRect rect(originalRect);
2055     if (rect.width + rect.x > sz.x)
2056         rect.width = sz.x - rect.x;
2057     if (rect.height + rect.y > sz.y)
2058         rect.height = sz.y - rect.y;
2059     if (rect != originalRect)
2060     {
2061         region = wxRegion(rect);
2062         wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2063                    originalRect.x, originalRect.y, originalRect.width, originalRect.height,
2064                    rect.x, rect.y, rect.width, rect.height);
2065     }
2066 }
2067 
wxPaintDCImpl(wxDC * owner)2068 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner )
2069          : wxClientDCImpl( owner )
2070 {
2071 }
2072 
wxPaintDCImpl(wxDC * owner,wxWindow * win)2073 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *win )
2074          : wxClientDCImpl( owner, win )
2075 {
2076 #if USE_PAINT_REGION
2077     if (!win->m_clipPaintRegion)
2078         return;
2079 
2080     wxSize sz = win->GetSize();
2081     m_paintClippingRegion = win->m_nativeUpdateRegion;
2082     wxLimitRegionToSize(m_paintClippingRegion, sz);
2083 
2084     GdkRegion *region = m_paintClippingRegion.GetRegion();
2085     if ( region )
2086     {
2087         m_currentClippingRegion.Union( m_paintClippingRegion );
2088         wxLimitRegionToSize(m_currentClippingRegion, sz);
2089 
2090         if (sz.x <= 0 || sz.y <= 0)
2091             return ;
2092 
2093         gdk_gc_set_clip_region( m_penGC, region );
2094         gdk_gc_set_clip_region( m_brushGC, region );
2095         gdk_gc_set_clip_region( m_textGC, region );
2096         gdk_gc_set_clip_region( m_bgGC, region );
2097     }
2098 #endif
2099 }
2100 
2101 // ----------------------------------------------------------------------------
2102 // wxDCModule
2103 // ----------------------------------------------------------------------------
2104 
2105 class wxDCModule : public wxModule
2106 {
2107 public:
2108     bool OnInit();
2109     void OnExit();
2110 
2111 private:
2112     DECLARE_DYNAMIC_CLASS(wxDCModule)
2113 };
2114 
IMPLEMENT_DYNAMIC_CLASS(wxDCModule,wxModule)2115 IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
2116 
2117 bool wxDCModule::OnInit()
2118 {
2119     wxInitGCPool();
2120     return true;
2121 }
2122 
OnExit()2123 void wxDCModule::OnExit()
2124 {
2125     wxCleanUpGCPool();
2126 
2127     for (int i = wxBRUSHSTYLE_LAST_HATCH - wxBRUSHSTYLE_FIRST_HATCH; i--; )
2128     {
2129         if (hatches[i])
2130             g_object_unref(hatches[i]);
2131     }
2132 }
2133