1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dcclient.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Chris Breeze
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
14 #define XCopyPlane XCOPYPLANE
17 #include "wx/gtk/dcclient.h"
20 #include "wx/window.h"
22 #include "wx/dcmemory.h"
25 #include "wx/module.h"
28 #include "wx/fontutil.h"
30 #include "wx/gtk/private.h"
31 #include "wx/gtk/private/object.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 #define XLOG2DEV(x) LogicalToDeviceX(x)
40 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
41 #define YLOG2DEV(y) LogicalToDeviceY(y)
42 #define YLOG2DEVREL(y) LogicalToDeviceYRel(y)
44 #define USE_PAINT_REGION 1
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
57 static GdkPixmap
* hatches
[wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
+ 1];
59 extern GtkWidget
*wxGetRootWindow();
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static const double RAD2DEG
= 180.0 / M_PI
;
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
72 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
74 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
76 static GdkPixmap
* GetHatch(int style
)
78 wxASSERT(style
>= wxBRUSHSTYLE_FIRST_HATCH
&& style
<= wxBRUSHSTYLE_LAST_HATCH
);
79 const int i
= style
- wxBRUSHSTYLE_FIRST_HATCH
;
80 if (hatches
[i
] == NULL
)
84 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
85 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, bdiag_bits
, bdiag_width
, bdiag_height
);
87 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
88 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, cdiag_bits
, cdiag_width
, cdiag_height
);
90 case wxBRUSHSTYLE_CROSS_HATCH
:
91 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, cross_bits
, cross_width
, cross_height
);
93 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
94 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, fdiag_bits
, fdiag_width
, fdiag_height
);
96 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
97 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, horiz_bits
, horiz_width
, horiz_height
);
99 case wxBRUSHSTYLE_VERTICAL_HATCH
:
100 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, verti_bits
, verti_width
, verti_height
);
107 //-----------------------------------------------------------------------------
108 // temporary implementation of the missing GDK function
109 //-----------------------------------------------------------------------------
112 void gdk_wx_draw_bitmap(GdkDrawable
*drawable
,
118 wxCHECK_RET( drawable
, _T("NULL drawable in gdk_wx_draw_bitmap") );
119 wxCHECK_RET( src
, _T("NULL src in gdk_wx_draw_bitmap") );
120 wxCHECK_RET( gc
, _T("NULL gc in gdk_wx_draw_bitmap") );
122 gint src_width
, src_height
;
123 gdk_drawable_get_size(src
, &src_width
, &src_height
);
125 XCopyPlane( GDK_WINDOW_XDISPLAY(drawable
),
127 GDK_WINDOW_XID(drawable
),
130 src_width
, src_height
,
135 //-----------------------------------------------------------------------------
136 // Implement Pool of Graphic contexts. Creating them takes too much time.
137 //-----------------------------------------------------------------------------
163 #define GC_POOL_ALLOC_SIZE 100
165 static int wxGCPoolSize
= 0;
167 static wxGC
*wxGCPool
= NULL
;
169 static void wxInitGCPool()
171 // This really could wait until the first call to
172 // wxGetPoolGC, but we will make the first allocation
173 // now when other initialization is being performed.
175 // Set initial pool size.
176 wxGCPoolSize
= GC_POOL_ALLOC_SIZE
;
178 // Allocate initial pool.
179 wxGCPool
= (wxGC
*)malloc(wxGCPoolSize
* sizeof(wxGC
));
180 if (wxGCPool
== NULL
)
182 // If we cannot malloc, then fail with error
183 // when debug is enabled. If debug is not enabled,
184 // the problem will eventually get caught
186 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
190 // Zero initial pool.
191 memset(wxGCPool
, 0, wxGCPoolSize
* sizeof(wxGC
));
194 static void wxCleanUpGCPool()
196 for (int i
= 0; i
< wxGCPoolSize
; i
++)
198 if (wxGCPool
[i
].m_gc
)
199 g_object_unref (wxGCPool
[i
].m_gc
);
207 static GdkGC
* wxGetPoolGC( GdkWindow
*window
, wxPoolGCType type
)
211 // Look for an available GC.
212 for (int i
= 0; i
< wxGCPoolSize
; i
++)
214 if (!wxGCPool
[i
].m_gc
)
216 wxGCPool
[i
].m_gc
= gdk_gc_new( window
);
217 gdk_gc_set_exposures( wxGCPool
[i
].m_gc
, FALSE
);
218 wxGCPool
[i
].m_type
= type
;
219 wxGCPool
[i
].m_used
= false;
221 if ((!wxGCPool
[i
].m_used
) && (wxGCPool
[i
].m_type
== type
))
223 wxGCPool
[i
].m_used
= true;
224 return wxGCPool
[i
].m_gc
;
228 // We did not find an available GC.
229 // We need to grow the GC pool.
230 pptr
= (wxGC
*)realloc(wxGCPool
,
231 (wxGCPoolSize
+ GC_POOL_ALLOC_SIZE
)*sizeof(wxGC
));
234 // Initialize newly allocated pool.
236 memset(&wxGCPool
[wxGCPoolSize
], 0,
237 GC_POOL_ALLOC_SIZE
*sizeof(wxGC
));
239 // Initialize entry we will return.
240 wxGCPool
[wxGCPoolSize
].m_gc
= gdk_gc_new( window
);
241 gdk_gc_set_exposures( wxGCPool
[wxGCPoolSize
].m_gc
, FALSE
);
242 wxGCPool
[wxGCPoolSize
].m_type
= type
;
243 wxGCPool
[wxGCPoolSize
].m_used
= true;
245 // Set new value of pool size.
246 wxGCPoolSize
+= GC_POOL_ALLOC_SIZE
;
248 // Return newly allocated entry.
249 return wxGCPool
[wxGCPoolSize
-GC_POOL_ALLOC_SIZE
].m_gc
;
252 // The realloc failed. Fall through to error.
253 wxFAIL_MSG( wxT("No GC available") );
255 return (GdkGC
*) NULL
;
258 static void wxFreePoolGC( GdkGC
*gc
)
260 for (int i
= 0; i
< wxGCPoolSize
; i
++)
262 if (wxGCPool
[i
].m_gc
== gc
)
264 wxGCPool
[i
].m_used
= false;
269 wxFAIL_MSG( wxT("Wrong GC") );
272 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
276 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxGTKDCImpl
)
278 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
281 m_gdkwindow
= (GdkWindow
*) NULL
;
282 m_penGC
= (GdkGC
*) NULL
;
283 m_brushGC
= (GdkGC
*) NULL
;
284 m_textGC
= (GdkGC
*) NULL
;
285 m_bgGC
= (GdkGC
*) NULL
;
286 m_cmap
= (GdkColormap
*) NULL
;
287 m_isScreenDC
= false;
288 m_context
= (PangoContext
*)NULL
;
289 m_layout
= (PangoLayout
*)NULL
;
290 m_fontdesc
= (PangoFontDescription
*)NULL
;
293 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
296 wxASSERT_MSG( window
, wxT("DC needs a window") );
298 m_gdkwindow
= (GdkWindow
*) NULL
;
299 m_penGC
= (GdkGC
*) NULL
;
300 m_brushGC
= (GdkGC
*) NULL
;
301 m_textGC
= (GdkGC
*) NULL
;
302 m_bgGC
= (GdkGC
*) NULL
;
303 m_cmap
= (GdkColormap
*) NULL
;
304 m_isScreenDC
= false;
305 m_font
= window
->GetFont();
307 GtkWidget
*widget
= window
->m_wxwindow
;
309 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
310 // code should still be able to create wxClientDCs for them, so we will
311 // use the parent window here then.
314 window
= window
->GetParent();
315 widget
= window
->m_wxwindow
;
318 wxASSERT_MSG( widget
, wxT("DC needs a widget") );
320 m_context
= window
->GtkGetPangoDefaultContext();
321 m_layout
= pango_layout_new( m_context
);
322 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
324 m_gdkwindow
= widget
->window
;
326 // Window not realized ?
329 // Don't report problems as per MSW.
335 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
339 /* this must be done after SetUpDC, bacause SetUpDC calls the
340 repective SetBrush, SetPen, SetBackground etc functions
341 to set up the DC. SetBackground call m_owner->SetBackground
342 and this might not be desired as the standard dc background
343 is white whereas a window might assume gray to be the
344 standard (as e.g. wxStatusBar) */
348 if (m_window
&& m_window
->m_wxwindow
&&
349 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
354 // origin in the upper right corner
355 m_deviceOriginX
= m_window
->GetClientSize().x
;
359 wxWindowDCImpl::~wxWindowDCImpl()
364 g_object_unref (m_layout
);
366 pango_font_description_free( m_fontdesc
);
369 void wxWindowDCImpl::SetUpDC( bool isMemDC
)
373 wxASSERT_MSG( !m_penGC
, wxT("GCs already created") );
377 if ((isMemDC
) && (GetSelectedBitmap().IsOk()))
379 if (GetSelectedBitmap().GetDepth() == 1)
381 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_MONO
);
382 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_MONO
);
383 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_MONO
);
384 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_MONO
);
393 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_SCREEN
);
394 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_SCREEN
);
395 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_SCREEN
);
396 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_SCREEN
);
400 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR
);
401 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR
);
402 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR
);
403 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR
);
407 /* background colour */
408 m_backgroundBrush
= *wxWHITE_BRUSH
;
409 m_backgroundBrush
.GetColour().CalcPixel( m_cmap
);
410 const GdkColor
*bg_col
= m_backgroundBrush
.GetColour().GetColor();
413 m_textForegroundColour
.CalcPixel( m_cmap
);
414 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
416 m_textBackgroundColour
.CalcPixel( m_cmap
);
417 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
419 gdk_gc_set_fill( m_textGC
, GDK_SOLID
);
421 gdk_gc_set_colormap( m_textGC
, m_cmap
);
424 m_pen
.GetColour().CalcPixel( m_cmap
);
425 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
426 gdk_gc_set_background( m_penGC
, bg_col
);
428 gdk_gc_set_line_attributes( m_penGC
, 0, GDK_LINE_SOLID
, GDK_CAP_NOT_LAST
, GDK_JOIN_ROUND
);
431 m_brush
.GetColour().CalcPixel( m_cmap
);
432 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
433 gdk_gc_set_background( m_brushGC
, bg_col
);
435 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
438 gdk_gc_set_background( m_bgGC
, bg_col
);
439 gdk_gc_set_foreground( m_bgGC
, bg_col
);
441 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
444 gdk_gc_set_function( m_textGC
, GDK_COPY
);
445 gdk_gc_set_function( m_brushGC
, GDK_COPY
);
446 gdk_gc_set_function( m_penGC
, GDK_COPY
);
449 gdk_gc_set_clip_rectangle( m_penGC
, (GdkRectangle
*) NULL
);
450 gdk_gc_set_clip_rectangle( m_brushGC
, (GdkRectangle
*) NULL
);
451 gdk_gc_set_clip_rectangle( m_textGC
, (GdkRectangle
*) NULL
);
452 gdk_gc_set_clip_rectangle( m_bgGC
, (GdkRectangle
*) NULL
);
455 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
457 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
459 m_window
->GetSize(width
, height
);
462 bool wxWindowDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
463 const wxColour
& col
, int style
)
466 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
467 const wxColour
& col
, int style
);
469 return wxDoFloodFill( GetOwner(), x
, y
, col
, style
);
480 bool wxWindowDCImpl::DoGetPixel( wxCoord x1
, wxCoord y1
, wxColour
*col
) const
483 // Generic (and therefore rather inefficient) method.
484 // Could be improved.
486 wxBitmap
bitmap(1, 1);
487 memdc
.SelectObject(bitmap
);
488 memdc
.Blit(0, 0, 1, 1, GetOwner(), x1
, y1
);
489 memdc
.SelectObject(wxNullBitmap
);
491 wxImage image
= bitmap
.ConvertToImage();
492 col
->Set(image
.GetRed(0, 0), image
.GetGreen(0, 0), image
.GetBlue(0, 0));
494 #else // !wxUSE_IMAGE
500 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
503 void wxWindowDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
505 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
507 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
510 gdk_draw_line( m_gdkwindow
, m_penGC
, XLOG2DEV(x1
), YLOG2DEV(y1
), XLOG2DEV(x2
), YLOG2DEV(y2
) );
512 CalcBoundingBox(x1
, y1
);
513 CalcBoundingBox(x2
, y2
);
517 void wxWindowDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
519 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
521 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
525 GetOwner()->GetSize( &w
, &h
);
526 wxCoord xx
= XLOG2DEV(x
);
527 wxCoord yy
= YLOG2DEV(y
);
530 gdk_draw_line( m_gdkwindow
, m_penGC
, 0, yy
, XLOG2DEVREL(w
), yy
);
531 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, 0, xx
, YLOG2DEVREL(h
) );
536 void wxWindowDCImpl::DrawingSetup(GdkGC
*& gc
, bool& originChanged
)
539 GdkPixmap
* pixmap
= NULL
;
540 const int style
= m_brush
.GetStyle();
542 if (style
== wxBRUSHSTYLE_STIPPLE
|| style
== wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
)
544 const wxBitmap
* stipple
= m_brush
.GetStipple();
547 if (style
== wxBRUSHSTYLE_STIPPLE
)
548 pixmap
= stipple
->GetPixmap();
549 else if (stipple
->GetMask())
551 pixmap
= stipple
->GetPixmap();
556 else if (m_brush
.IsHatch())
558 pixmap
= GetHatch(style
);
566 gdk_drawable_get_size(pixmap
, &w
, &h
);
567 origin_x
= m_deviceOriginX
% w
;
568 origin_y
= m_deviceOriginY
% h
;
571 originChanged
= origin_x
|| origin_y
;
573 gdk_gc_set_ts_origin(gc
, origin_x
, origin_y
);
576 void wxWindowDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
,
577 wxCoord xc
, wxCoord yc
)
579 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
581 wxCoord xx1
= XLOG2DEV(x1
);
582 wxCoord yy1
= YLOG2DEV(y1
);
583 wxCoord xx2
= XLOG2DEV(x2
);
584 wxCoord yy2
= YLOG2DEV(y2
);
585 wxCoord xxc
= XLOG2DEV(xc
);
586 wxCoord yyc
= YLOG2DEV(yc
);
587 double dx
= xx1
- xxc
;
588 double dy
= yy1
- yyc
;
589 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
590 wxCoord r
= (wxCoord
)radius
;
591 double radius1
, radius2
;
593 if (xx1
== xx2
&& yy1
== yy2
)
598 else if ( wxIsNullDouble(radius
) )
605 radius1
= (xx1
- xxc
== 0) ?
606 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
607 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
608 radius2
= (xx2
- xxc
== 0) ?
609 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
610 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
612 wxCoord alpha1
= wxCoord(radius1
* 64.0);
613 wxCoord alpha2
= wxCoord((radius2
- radius1
) * 64.0);
614 while (alpha2
<= 0) alpha2
+= 360*64;
615 while (alpha1
> 360*64) alpha1
-= 360*64;
619 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
623 DrawingSetup(gc
, originChanged
);
625 gdk_draw_arc(m_gdkwindow
, gc
, true, xxc
-r
, yyc
-r
, 2*r
, 2*r
, alpha1
, alpha2
);
628 gdk_gc_set_ts_origin(gc
, 0, 0);
631 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
633 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xxc
-r
, yyc
-r
, 2*r
,2*r
, alpha1
, alpha2
);
635 if ((m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
) && (alpha2
- alpha1
!= 360*64))
637 gdk_draw_line( m_gdkwindow
, m_penGC
, xx1
, yy1
, xxc
, yyc
);
638 gdk_draw_line( m_gdkwindow
, m_penGC
, xxc
, yyc
, xx2
, yy2
);
643 CalcBoundingBox (x1
, y1
);
644 CalcBoundingBox (x2
, y2
);
647 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double sa
, double ea
)
649 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
651 wxCoord xx
= XLOG2DEV(x
);
652 wxCoord yy
= YLOG2DEV(y
);
653 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
654 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
656 // CMB: handle -ve width and/or height
657 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
658 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
662 wxCoord start
= wxCoord(sa
* 64.0);
663 wxCoord end
= wxCoord((ea
-sa
) * 64.0);
665 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
669 DrawingSetup(gc
, originChanged
);
671 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, start
, end
);
674 gdk_gc_set_ts_origin(gc
, 0, 0);
677 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
678 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, start
, end
);
681 CalcBoundingBox (x
, y
);
682 CalcBoundingBox (x
+ width
, y
+ height
);
685 void wxWindowDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
687 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
689 if ((m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
) && m_gdkwindow
)
690 gdk_draw_point( m_gdkwindow
, m_penGC
, XLOG2DEV(x
), YLOG2DEV(y
) );
692 CalcBoundingBox (x
, y
);
695 void wxWindowDCImpl::DoDrawLines( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
697 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
699 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
) return;
702 //Check, if scaling is necessary
704 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
706 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
707 GdkPoint
* gpts
= reinterpret_cast<GdkPoint
*>(points
);
710 gpts
= new GdkPoint
[n
];
712 for (int i
= 0; i
< n
; i
++)
716 gpts
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
717 gpts
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
719 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
723 gdk_draw_lines( m_gdkwindow
, m_penGC
, gpts
, n
);
729 void wxWindowDCImpl::DoDrawPolygon( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int WXUNUSED(fillStyle
) )
731 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
735 //Check, if scaling is necessary
737 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
739 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
740 GdkPoint
* gdkpoints
= reinterpret_cast<GdkPoint
*>(points
);
743 gdkpoints
= new GdkPoint
[n
];
746 for (i
= 0 ; i
< n
; i
++)
750 gdkpoints
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
751 gdkpoints
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
753 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
758 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
762 DrawingSetup(gc
, originChanged
);
764 gdk_draw_polygon(m_gdkwindow
, gc
, true, gdkpoints
, n
);
767 gdk_gc_set_ts_origin(gc
, 0, 0);
770 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
773 for (i = 0 ; i < n ; i++)
775 gdk_draw_line( m_gdkwindow, m_penGC,
778 gdkpoints[(i+1)%n].x,
779 gdkpoints[(i+1)%n].y);
782 gdk_draw_polygon( m_gdkwindow
, m_penGC
, FALSE
, gdkpoints
, n
);
791 void wxWindowDCImpl::DoDrawRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
793 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
795 wxCoord xx
= XLOG2DEV(x
);
796 wxCoord yy
= YLOG2DEV(y
);
797 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
798 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
800 // CMB: draw nothing if transformed w or h is 0
801 if (ww
== 0 || hh
== 0) return;
803 // CMB: handle -ve width and/or height
804 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
805 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
809 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
813 DrawingSetup(gc
, originChanged
);
815 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
);
818 gdk_gc_set_ts_origin(gc
, 0, 0);
821 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
824 if ((m_pen
.GetWidth() == 2) && (m_pen
.GetCap() == wxCAP_ROUND
) &&
825 (m_pen
.GetJoin() == wxJOIN_ROUND
) && (m_pen
.GetStyle() == wxPENSTYLE_SOLID
))
827 // Use 2 1-line rects instead
828 gdk_gc_set_line_attributes( m_penGC
, 1, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
833 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
+1, yy
, ww
-2, hh
-2 );
834 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
-1, ww
, hh
);
838 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-2, hh
-2 );
839 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
-1, yy
-1, ww
, hh
);
843 gdk_gc_set_line_attributes( m_penGC
, 2, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
848 // Just use X11 for other cases
849 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-1, hh
-1 );
854 CalcBoundingBox( x
, y
);
855 CalcBoundingBox( x
+ width
, y
+ height
);
858 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
860 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
862 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
864 wxCoord xx
= XLOG2DEV(x
);
865 wxCoord yy
= YLOG2DEV(y
);
866 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
867 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
868 wxCoord rr
= XLOG2DEVREL((wxCoord
)radius
);
870 // CMB: handle -ve width and/or height
871 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
872 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
874 // CMB: if radius is zero use DrawRectangle() instead to avoid
875 // X drawing errors with small radii
878 DoDrawRectangle( x
, y
, width
, height
);
882 // CMB: draw nothing if transformed w or h is 0
883 if (ww
== 0 || hh
== 0) return;
885 // CMB: adjust size if outline is drawn otherwise the result is
886 // 1 pixel too wide and high
887 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
895 // CMB: ensure dd is not larger than rectangle otherwise we
896 // get an hour glass shape
898 if (dd
> ww
) dd
= ww
;
899 if (dd
> hh
) dd
= hh
;
902 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
906 DrawingSetup(gc
, originChanged
);
908 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
+rr
, yy
, ww
-dd
+1, hh
);
909 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
+rr
, ww
, hh
-dd
+1);
910 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, dd
, dd
, 90*64, 90*64);
911 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64);
912 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64);
913 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64);
916 gdk_gc_set_ts_origin(gc
, 0, 0);
919 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
921 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
, xx
+ww
-rr
, yy
);
922 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
+hh
, xx
+ww
-rr
, yy
+hh
);
923 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, yy
+rr
+1, xx
, yy
+hh
-rr
);
924 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+ww
, yy
+rr
+1, xx
+ww
, yy
+hh
-rr
);
925 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, dd
, dd
, 90*64, 90*64 );
926 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64 );
927 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64 );
928 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64 );
932 // this ignores the radius
933 CalcBoundingBox( x
, y
);
934 CalcBoundingBox( x
+ width
, y
+ height
);
937 void wxWindowDCImpl::DoDrawEllipse( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
939 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
941 wxCoord xx
= XLOG2DEV(x
);
942 wxCoord yy
= YLOG2DEV(y
);
943 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
944 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
946 // CMB: handle -ve width and/or height
947 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
948 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
952 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
956 DrawingSetup(gc
, originChanged
);
958 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, 0, 360*64);
961 gdk_gc_set_ts_origin(gc
, 0, 0);
964 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
965 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, 0, 360*64 );
968 CalcBoundingBox( x
, y
);
969 CalcBoundingBox( x
+ width
, y
+ height
);
972 void wxWindowDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
974 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
975 DoDrawBitmap( (const wxBitmap
&)icon
, x
, y
, true );
978 // compare to current clipping region
979 bool wxWindowDCImpl::IsOutsideOfClippingRegion(int x
, int y
, int w
, int h
)
981 if (m_currentClippingRegion
.IsNull())
984 wxRegion
region(x
, y
, w
, h
);
985 region
.Intersect( m_currentClippingRegion
);
986 return region
.IsEmpty();
989 void wxWindowDCImpl::RemoveClipMask(GdkGC
*gc
)
991 gdk_gc_set_clip_mask(gc
, NULL
);
992 gdk_gc_set_clip_origin(gc
, 0, 0);
993 if (!m_currentClippingRegion
.IsNull())
994 gdk_gc_set_clip_region(gc
, m_currentClippingRegion
.GetRegion());
997 // For drawing a mono-bitmap (XBitmap) we use the current text GC
998 void wxWindowDCImpl::DoDrawMonoBitmap(const wxBitmap
& bitmap
,
999 int bmp_w
, int bmp_h
,
1001 int xdest
, int ydest
,
1002 int width
, int height
)
1004 wxGtkObject
<GdkPixmap
>
1005 bitmap2(gdk_pixmap_new( wxGetRootWindow()->window
, bmp_w
, bmp_h
, -1 ));
1006 wxGtkObject
<GdkGC
> gc(gdk_gc_new( bitmap2
));
1007 gdk_gc_set_foreground( gc
, m_textForegroundColour
.GetColor() );
1008 gdk_gc_set_background( gc
, m_textBackgroundColour
.GetColor() );
1009 gdk_wx_draw_bitmap(bitmap2
, gc
, bitmap
.GetPixmap(), 0, 0);
1011 gdk_draw_drawable(m_gdkwindow
, m_textGC
, bitmap2
, xsrc
, ysrc
, xdest
, ydest
,
1015 // Returns a new mask that is the intersection of the old mask
1016 // and m_currentClippingRegion with proper offsets
1017 GdkBitmap
* wxWindowDCImpl::GetClippedMask(GdkBitmap
* mask
, int w
, int h
,
1019 int xsrcMask
, int ysrcMask
)
1021 // create monochrome bitmap that will be used as the new mask
1022 GdkBitmap
*new_mask
= gdk_pixmap_new( wxGetRootWindow()->window
, w
, h
, 1 );
1027 wxGtkObject
<GdkGC
> gc(gdk_gc_new( new_mask
));
1029 // zero-ing new_mask
1030 gdk_gc_set_foreground( gc
, &c0
);
1031 gdk_draw_rectangle( new_mask
, gc
, TRUE
, 0, 0, w
, h
);
1034 gdk_gc_set_clip_region( gc
, m_currentClippingRegion
.GetRegion() );
1035 gdk_gc_set_clip_origin( gc
, -x
, -y
);
1037 // copy the old mask to the new_mask in the clip region area
1038 gdk_gc_set_background( gc
, &c0
);
1039 gdk_gc_set_foreground( gc
, &c1
);
1040 gdk_gc_set_fill( gc
, GDK_OPAQUE_STIPPLED
);
1041 gdk_gc_set_ts_origin( gc
, -xsrcMask
, -ysrcMask
);
1042 gdk_gc_set_stipple( gc
, mask
);
1043 gdk_draw_rectangle( new_mask
, gc
, TRUE
, 0, 0, w
, h
);
1048 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap
&bitmap
,
1049 wxCoord x
, wxCoord y
,
1052 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1054 wxCHECK_RET( bitmap
.IsOk(), wxT("invalid bitmap") );
1056 bool is_mono
= bitmap
.GetDepth() == 1;
1058 // scale/translate size and position
1059 int xx
= XLOG2DEV(x
);
1060 int yy
= YLOG2DEV(y
);
1062 int w
= bitmap
.GetWidth();
1063 int h
= bitmap
.GetHeight();
1065 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1068 CalcBoundingBox( x
, y
);
1069 CalcBoundingBox( x
+ w
, y
+ h
);
1071 if (!m_gdkwindow
) return;
1073 int ww
= XLOG2DEVREL(w
);
1074 int hh
= YLOG2DEVREL(h
);
1076 if (IsOutsideOfClippingRegion( xx
,yy
,ww
,hh
))
1079 // scale bitmap if required
1080 wxBitmap use_bitmap
= bitmap
;
1081 if ((w
!= ww
) || (h
!= hh
))
1082 use_bitmap
= use_bitmap
.Rescale( 0, 0, w
, h
, ww
, hh
);
1085 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1086 if (useMask
&& use_bitmap
.GetMask())
1087 mask
= use_bitmap
.GetMask()->GetBitmap();
1089 // for drawing a mono-bitmap we use the current text GC
1090 GdkGC
* use_gc
= is_mono
? m_textGC
: m_penGC
;
1092 bool mask_owned
= false;
1096 if (!m_currentClippingRegion
.IsNull())
1098 mask
= GetClippedMask(mask
, ww
, hh
, xx
, yy
, 0, 0);
1102 gdk_gc_set_clip_mask(use_gc
, mask
);
1103 gdk_gc_set_clip_origin(use_gc
, xx
, yy
);
1106 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains.
1109 DoDrawMonoBitmap(use_bitmap
, ww
, hh
, 0, 0, xx
, yy
, -1, -1);
1113 if (use_bitmap
.HasPixbuf())
1115 gdk_draw_pixbuf(m_gdkwindow
, use_gc
,
1116 use_bitmap
.GetPixbuf(),
1117 0, 0, xx
, yy
, -1, -1,
1118 GDK_RGB_DITHER_NORMAL
, xx
, yy
);
1122 gdk_draw_drawable(m_gdkwindow
, use_gc
,
1123 use_bitmap
.GetPixmap(),
1124 0, 0, xx
, yy
, -1, -1);
1128 // remove mask again if any
1131 RemoveClipMask(use_gc
);
1133 g_object_unref(mask
);
1137 bool wxWindowDCImpl::DoBlit( wxCoord xdest
, wxCoord ydest
,
1138 wxCoord width
, wxCoord height
,
1140 wxCoord xsrc
, wxCoord ysrc
,
1143 wxCoord xsrcMask
, wxCoord ysrcMask
)
1145 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1147 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1149 if (!m_gdkwindow
) return false;
1151 // transform the source DC coords to the device ones
1152 xsrc
= source
->LogicalToDeviceX(xsrc
);
1153 ysrc
= source
->LogicalToDeviceY(ysrc
);
1156 wxMemoryDC
*memDC
= wxDynamicCast(source
, wxMemoryDC
);
1159 selected
= memDC
->GetSelectedBitmap();
1160 if ( !selected
.IsOk() )
1164 bool use_bitmap_method
= false;
1165 bool is_mono
= false;
1167 if (xsrcMask
== -1 && ysrcMask
== -1)
1173 if (selected
.IsOk())
1175 is_mono
= (selected
.GetDepth() == 1);
1177 // we use the "XCopyArea" way to copy a memory dc into
1178 // a different window if the memory dc BOTH
1179 // a) doesn't have any mask or its mask isn't used
1183 if (useMask
&& (selected
.GetMask()))
1185 // we HAVE TO use the direct way for memory dcs
1186 // that have mask since the XCopyArea doesn't know
1188 use_bitmap_method
= true;
1192 // we HAVE TO use the direct way for memory dcs
1193 // that are bitmaps because XCopyArea doesn't cope
1194 // with different bit depths
1195 use_bitmap_method
= true;
1197 else if ((xsrc
== 0) && (ysrc
== 0) &&
1198 (width
== selected
.GetWidth()) &&
1199 (height
== selected
.GetHeight()))
1201 // we SHOULD use the direct way if all of the bitmap
1202 // in the memory dc is copied in which case XCopyArea
1203 // wouldn't be able able to boost performace by reducing
1204 // the area to be scaled
1205 use_bitmap_method
= true;
1209 CalcBoundingBox( xdest
, ydest
);
1210 CalcBoundingBox( xdest
+ width
, ydest
+ height
);
1212 // scale/translate size and position
1213 wxCoord xx
= XLOG2DEV(xdest
);
1214 wxCoord yy
= YLOG2DEV(ydest
);
1216 wxCoord ww
= XLOG2DEVREL(width
);
1217 wxCoord hh
= YLOG2DEVREL(height
);
1219 // compare to current clipping region
1220 if (IsOutsideOfClippingRegion( xx
,yy
,ww
,hh
))
1223 int old_logical_func
= m_logicalFunction
;
1224 SetLogicalFunction( logical_func
);
1226 if (use_bitmap_method
)
1228 // scale/translate bitmap size
1229 wxCoord bm_width
= selected
.GetWidth();
1230 wxCoord bm_height
= selected
.GetHeight();
1232 // Get clip coords for the bitmap. If we don't
1233 // use wxBitmap::Rescale(), which can clip the
1234 // bitmap, these are the same as the original
1241 // interpret userscale of src too
1243 memDC
->GetUserScale(&xsc
,&ysc
);
1244 bm_width
= (int) (bm_width
/ xsc
);
1245 bm_height
= (int) (bm_height
/ ysc
);
1247 wxCoord bm_ww
= XLOG2DEVREL( bm_width
);
1248 wxCoord bm_hh
= YLOG2DEVREL( bm_height
);
1250 // Scale bitmap if required
1251 wxBitmap use_bitmap
= selected
;
1252 if ((selected
.GetWidth()!= bm_ww
) || ( selected
.GetHeight()!= bm_hh
))
1254 // This indicates that the blitting code below will get
1255 // a clipped bitmap and therefore needs to move the origin
1257 wxRegion
tmp( xx
,yy
,ww
,hh
);
1258 if (!m_currentClippingRegion
.IsNull())
1259 tmp
.Intersect( m_currentClippingRegion
);
1260 tmp
.GetBox(cx
,cy
,cw
,ch
);
1262 // Scale and clipped bitmap
1263 use_bitmap
= selected
.Rescale(cx
-xx
,cy
-yy
,cw
,ch
,bm_ww
,bm_hh
);
1266 // apply mask if any
1267 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1268 if (useMask
&& use_bitmap
.GetMask())
1269 mask
= use_bitmap
.GetMask()->GetBitmap();
1271 GdkGC
* use_gc
= is_mono
? m_textGC
: m_penGC
;
1273 bool mask_owned
= false;
1277 if (!m_currentClippingRegion
.IsNull())
1279 mask
= GetClippedMask(mask
, bm_ww
, bm_hh
, cx
, cy
,
1280 xsrcMask
, ysrcMask
);
1284 gdk_gc_set_clip_mask(use_gc
, mask
);
1286 gdk_gc_set_clip_origin(use_gc
, cx
, cy
);
1288 gdk_gc_set_clip_origin(use_gc
, cx
- xsrcMask
, cy
- ysrcMask
);
1291 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains.
1294 DoDrawMonoBitmap(use_bitmap
, bm_ww
, bm_hh
,
1295 xsrc
, ysrc
, cx
, cy
, cw
, ch
);
1299 // was: gdk_draw_drawable( m_gdkwindow, m_penGC, use_bitmap.GetPixmap(), xsrc, ysrc, xx, yy, ww, hh );
1300 gdk_draw_drawable(m_gdkwindow
, use_gc
, use_bitmap
.GetPixmap(), xsrc
, ysrc
, cx
, cy
, cw
, ch
);
1303 // remove mask again if any
1306 RemoveClipMask(use_gc
);
1308 g_object_unref (mask
);
1311 else // use_bitmap_method
1313 if (selected
.IsOk() && ((width
!= ww
) || (height
!= hh
)))
1315 wxBitmap bitmap
= selected
.Rescale( xsrc
, ysrc
, width
, height
, ww
, hh
);
1316 // draw scaled bitmap
1317 gdk_draw_drawable( m_gdkwindow
, m_penGC
, bitmap
.GetPixmap(), 0, 0, xx
, yy
, -1, -1 );
1321 // No scaling and not a memory dc with a mask either
1322 GdkWindow
* window
= NULL
;
1323 wxDCImpl
*impl
= source
->GetImpl();
1324 wxWindowDCImpl
*gtk_impl
= wxDynamicCast(impl
, wxWindowDCImpl
);
1326 window
= gtk_impl
->GetGDKWindow();
1329 SetLogicalFunction( old_logical_func
);
1333 // copy including child window contents
1334 gdk_gc_set_subwindow( m_penGC
, GDK_INCLUDE_INFERIORS
);
1335 gdk_draw_drawable( m_gdkwindow
, m_penGC
,
1339 gdk_gc_set_subwindow( m_penGC
, GDK_CLIP_BY_CHILDREN
);
1343 SetLogicalFunction( old_logical_func
);
1348 void wxWindowDCImpl::DoDrawText( const wxString
&text
, wxCoord x
, wxCoord y
)
1350 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1352 if (!m_gdkwindow
) return;
1354 if (text
.empty()) return;
1359 wxCHECK_RET( m_context
, wxT("no Pango context") );
1360 wxCHECK_RET( m_layout
, wxT("no Pango layout") );
1361 wxCHECK_RET( m_fontdesc
, wxT("no Pango font description") );
1363 gdk_pango_context_set_colormap( m_context
, m_cmap
); // not needed in gtk+ >= 2.6
1365 bool underlined
= m_font
.IsOk() && m_font
.GetUnderlined();
1367 wxCharBuffer data
= wxGTK_CONV(text
);
1370 size_t datalen
= strlen(data
);
1372 // in Pango >= 1.16 the "underline of leading/trailing spaces" bug
1373 // has been fixed and thus the hack implemented below should never be used
1374 static bool pangoOk
= !wx_pango_version_check(1, 16, 0);
1376 bool needshack
= underlined
&& !pangoOk
;
1380 // a PangoLayout which has leading/trailing spaces with underlined font
1381 // is not correctly drawn by this pango version: Pango won't underline the spaces.
1382 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
1383 // this behaviour. To workaround this problem, we use a special hack here
1384 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
1385 // empty space characters and give them a dummy colour attribute.
1386 // This will force Pango to underline the leading/trailing spaces, too.
1388 wxCharBuffer
data_tmp(datalen
+ 6);
1389 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1390 memcpy(data_tmp
.data(), "\342\200\214", 3);
1391 // copy the user string
1392 memcpy(data_tmp
.data() + 3, data
, datalen
);
1393 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1394 memcpy(data_tmp
.data() + 3 + datalen
, "\342\200\214", 3);
1400 pango_layout_set_text(m_layout
, data
, datalen
);
1404 PangoAttrList
*attrs
= pango_attr_list_new();
1405 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1407 a
->end_index
= datalen
;
1408 pango_attr_list_insert(attrs
, a
);
1412 // dummy colour for the leading space
1413 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1416 pango_attr_list_insert(attrs
, a
);
1418 // dummy colour for the trailing space
1419 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1420 a
->start_index
= datalen
- 1;
1421 a
->end_index
= datalen
;
1422 pango_attr_list_insert(attrs
, a
);
1425 pango_layout_set_attributes(m_layout
, attrs
);
1426 pango_attr_list_unref(attrs
);
1430 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1433 // If there is a user or actually any scale applied to
1434 // the device context, scale the font.
1436 // scale font description
1437 oldSize
= pango_font_description_get_size(m_fontdesc
);
1438 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1440 // actually apply scaled font
1441 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1445 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1449 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1452 const GdkColor
* bg_col
= NULL
;
1453 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1454 bg_col
= m_textBackgroundColour
.GetColor();
1456 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x_rtl
, y
, m_layout
, NULL
, bg_col
);
1460 // reset unscaled size
1461 pango_font_description_set_size( m_fontdesc
, oldSize
);
1463 // actually apply unscaled font
1464 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1468 // undo underline attributes setting:
1469 pango_layout_set_attributes(m_layout
, NULL
);
1472 CalcBoundingBox(x
+ int(w
/ m_scaleX
), y
+ int(h
/ m_scaleY
));
1473 CalcBoundingBox(x
, y
);
1476 // TODO: When GTK2.6 is required, merge DoDrawText and DoDrawRotatedText to
1477 // avoid code duplication
1478 void wxWindowDCImpl::DoDrawRotatedText( const wxString
&text
, wxCoord x
, wxCoord y
, double angle
)
1480 if (!m_gdkwindow
|| text
.empty())
1483 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1486 if (!gtk_check_version(2,6,0))
1491 pango_layout_set_text(m_layout
, wxGTK_CONV(text
), -1);
1493 if (m_font
.GetUnderlined())
1495 PangoAttrList
*attrs
= pango_attr_list_new();
1496 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1497 pango_attr_list_insert(attrs
, a
);
1498 pango_layout_set_attributes(m_layout
, attrs
);
1499 pango_attr_list_unref(attrs
);
1503 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1506 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1507 // If there is a user or actually any scale applied to
1508 // the device context, scale the font.
1510 // scale font description
1511 oldSize
= pango_font_description_get_size(m_fontdesc
);
1512 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1514 // actually apply scaled font
1515 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1519 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1521 const GdkColor
* bg_col
= NULL
;
1522 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1523 bg_col
= m_textBackgroundColour
.GetColor();
1526 PangoMatrix matrix
= PANGO_MATRIX_INIT
;
1527 pango_matrix_rotate (&matrix
, angle
);
1528 pango_context_set_matrix (m_context
, &matrix
);
1529 pango_layout_context_changed (m_layout
);
1531 // To be compatible with MSW, the rotation axis must be in the old
1533 // Calculate the vertices of the rotated rectangle containing the text,
1534 // relative to the old top-left vertex.
1535 // We could use the matrix for this, but it's simpler with trignonometry.
1536 double rad
= DegToRad(angle
);
1537 // the rectangle vertices are counted clockwise with the first one
1539 double x2
= w
* cos(rad
);
1540 double y2
= -w
* sin(rad
); // y axis points to the bottom, hence minus
1541 double x4
= h
* sin(rad
);
1542 double y4
= h
* cos(rad
);
1543 double x3
= x4
+ x2
;
1544 double y3
= y4
+ y2
;
1545 // Then we calculate max and min of the rotated rectangle.
1546 wxCoord maxX
= (wxCoord
)(dmax(dmax(0, x2
), dmax(x3
, x4
)) + 0.5),
1547 maxY
= (wxCoord
)(dmax(dmax(0, y2
), dmax(y3
, y4
)) + 0.5),
1548 minX
= (wxCoord
)(dmin(dmin(0, x2
), dmin(x3
, x4
)) - 0.5),
1549 minY
= (wxCoord
)(dmin(dmin(0, y2
), dmin(y3
, y4
)) - 0.5);
1551 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x
+minX
, y
+minY
,
1552 m_layout
, NULL
, bg_col
);
1554 if (m_font
.GetUnderlined())
1555 pango_layout_set_attributes(m_layout
, NULL
);
1557 // clean up the transformation matrix
1558 pango_context_set_matrix(m_context
, NULL
);
1562 // reset unscaled size
1563 pango_font_description_set_size( m_fontdesc
, oldSize
);
1565 // actually apply unscaled font
1566 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1569 CalcBoundingBox(x
+minX
, y
+minY
);
1570 CalcBoundingBox(x
+maxX
, y
+maxY
);
1573 #endif //__WXGTK26__
1576 if ( wxIsNullDouble(angle
) )
1578 DoDrawText(text
, x
, y
);
1585 // TODO: implement later without GdkFont for GTK 2.0
1586 DoGetTextExtent(text
, &w
, &h
, NULL
,NULL
, &m_font
);
1588 // draw the string normally
1591 dc
.SelectObject(src
);
1592 dc
.SetFont(GetFont());
1593 dc
.SetBackground(*wxBLACK_BRUSH
);
1594 dc
.SetBrush(*wxBLACK_BRUSH
);
1596 dc
.SetTextForeground( *wxWHITE
);
1597 dc
.DrawText(text
, 0, 0);
1598 dc
.SelectObject(wxNullBitmap
);
1600 // Calculate the size of the rotated bounding box.
1601 double rad
= DegToRad(angle
);
1602 double dx
= cos(rad
),
1605 // the rectngle vertices are counted clockwise with the first one being at
1606 // (0, 0) (or, rather, at (x, y))
1608 y2
= -w
*dy
; // y axis points to the bottom, hence minus
1611 double x3
= x4
+ x2
,
1615 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5),
1616 maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5),
1617 minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5),
1618 minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1621 wxImage image
= src
.ConvertToImage();
1623 image
.ConvertColourToAlpha( m_textForegroundColour
.Red(),
1624 m_textForegroundColour
.Green(),
1625 m_textForegroundColour
.Blue() );
1626 image
= image
.Rotate( rad
, wxPoint(0,0) );
1628 int i_angle
= (int) angle
;
1629 i_angle
= i_angle
% 360;
1633 if ((i_angle
>= 90.0) && (i_angle
< 270.0))
1634 xoffset
= image
.GetWidth();
1636 if ((i_angle
>= 0.0) && (i_angle
< 180.0))
1637 yoffset
= image
.GetHeight();
1639 if ((i_angle
>= 0) && (i_angle
< 90))
1640 yoffset
-= (int)( cos(rad
)*h
);
1641 if ((i_angle
>= 90) && (i_angle
< 180))
1642 xoffset
-= (int)( sin(rad
)*h
);
1643 if ((i_angle
>= 180) && (i_angle
< 270))
1644 yoffset
-= (int)( cos(rad
)*h
);
1645 if ((i_angle
>= 270) && (i_angle
< 360))
1646 xoffset
-= (int)( sin(rad
)*h
);
1648 int i_x
= x
- xoffset
;
1649 int i_y
= y
- yoffset
;
1652 DoDrawBitmap( src
, i_x
, i_y
, true );
1655 // it would be better to draw with non underlined font and draw the line
1656 // manually here (it would be more straight...)
1658 if ( m_font
.GetUnderlined() )
1660 gdk_draw_line( m_gdkwindow
, m_textGC
,
1661 XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
),
1662 XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1666 // update the bounding box
1667 CalcBoundingBox(x
+ minX
, y
+ minY
);
1668 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1669 #else // !wxUSE_IMAGE
1674 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
1678 void wxWindowDCImpl::DoGetTextExtent(const wxString
&string
,
1679 wxCoord
*width
, wxCoord
*height
,
1680 wxCoord
*descent
, wxCoord
*externalLeading
,
1681 const wxFont
*theFont
) const
1689 if ( externalLeading
)
1690 *externalLeading
= 0;
1695 // ensure that theFont is always non-NULL
1696 if ( !theFont
|| !theFont
->IsOk() )
1699 // and use it if it's valid
1700 if ( theFont
->IsOk() )
1702 pango_layout_set_font_description
1705 theFont
->GetNativeFontInfo()->description
1709 // Set layout's text
1710 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *theFont
);
1713 // hardly ideal, but what else can we do if conversion failed?
1717 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1720 pango_layout_get_pixel_size(m_layout
, width
, &h
);
1723 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1724 int baseline
= pango_layout_iter_get_baseline(iter
);
1725 pango_layout_iter_free(iter
);
1726 *descent
= h
- PANGO_PIXELS(baseline
);
1731 // Reset old font description
1732 if (theFont
->IsOk())
1733 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1737 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString
& text
,
1738 wxArrayInt
& widths
) const
1740 const size_t len
= text
.length();
1747 // Set layout's text
1748 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, m_font
);
1751 // hardly ideal, but what else can we do if conversion failed?
1752 wxLogLastError(wxT("DoGetPartialTextExtents"));
1756 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1758 // Calculate the position of each character based on the widths of
1759 // the previous characters
1761 // Code borrowed from Scintilla's PlatGTK
1762 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1764 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1766 while (pango_layout_iter_next_cluster(iter
))
1768 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1769 int position
= PANGO_PIXELS(pos
.x
);
1770 widths
[i
++] = position
;
1773 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
1774 pango_layout_iter_free(iter
);
1780 wxCoord
wxWindowDCImpl::GetCharWidth() const
1782 pango_layout_set_text( m_layout
, "H", 1 );
1784 pango_layout_get_pixel_size( m_layout
, &w
, NULL
);
1788 wxCoord
wxWindowDCImpl::GetCharHeight() const
1790 PangoFontMetrics
*metrics
= pango_context_get_metrics (m_context
, m_fontdesc
, pango_context_get_language(m_context
));
1791 wxCHECK_MSG( metrics
, -1, _T("failed to get pango font metrics") );
1793 wxCoord h
= PANGO_PIXELS (pango_font_metrics_get_descent (metrics
) +
1794 pango_font_metrics_get_ascent (metrics
));
1795 pango_font_metrics_unref (metrics
);
1799 void wxWindowDCImpl::Clear()
1801 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1803 if (!m_gdkwindow
) return;
1806 DoGetSize( &width
, &height
);
1807 gdk_draw_rectangle( m_gdkwindow
, m_bgGC
, TRUE
, 0, 0, width
, height
);
1810 void wxWindowDCImpl::SetFont( const wxFont
&font
)
1817 pango_font_description_free( m_fontdesc
);
1819 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1824 PangoContext
*oldContext
= m_context
;
1826 m_context
= m_window
->GtkGetPangoDefaultContext();
1828 // If we switch back/forth between different contexts
1829 // we also have to create a new layout. I think so,
1830 // at least, and it doesn't hurt to do it.
1831 if (oldContext
!= m_context
)
1834 g_object_unref (m_layout
);
1836 m_layout
= pango_layout_new( m_context
);
1840 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1844 void wxWindowDCImpl::SetPen( const wxPen
&pen
)
1846 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1848 if (m_pen
== pen
) return;
1852 if (!m_pen
.IsOk()) return;
1854 if (!m_gdkwindow
) return;
1856 gint width
= m_pen
.GetWidth();
1859 // CMB: if width is non-zero scale it with the dc
1864 // X doesn't allow different width in x and y and so we take
1867 ( fabs((double) XLOG2DEVREL(width
)) +
1868 fabs((double) YLOG2DEVREL(width
)) ) / 2.0;
1872 // width can't be 0 or an internal GTK error occurs inside
1873 // gdk_gc_set_dashes() below
1878 static const wxGTKDash dotted
[] = {1, 1};
1879 static const wxGTKDash short_dashed
[] = {2, 2};
1880 static const wxGTKDash wxCoord_dashed
[] = {2, 4};
1881 static const wxGTKDash dotted_dashed
[] = {3, 3, 1, 3};
1883 // We express dash pattern in pen width unit, so we are
1884 // independent of zoom factor and so on...
1886 const wxGTKDash
*req_dash
;
1888 GdkLineStyle lineStyle
= GDK_LINE_ON_OFF_DASH
;
1889 switch (m_pen
.GetStyle())
1891 case wxPENSTYLE_USER_DASH
:
1892 req_nb_dash
= m_pen
.GetDashCount();
1893 req_dash
= (wxGTKDash
*)m_pen
.GetDash();
1895 case wxPENSTYLE_DOT
:
1899 case wxPENSTYLE_LONG_DASH
:
1901 req_dash
= wxCoord_dashed
;
1903 case wxPENSTYLE_SHORT_DASH
:
1905 req_dash
= short_dashed
;
1907 case wxPENSTYLE_DOT_DASH
:
1909 req_dash
= dotted_dashed
;
1912 case wxPENSTYLE_TRANSPARENT
:
1913 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
1914 case wxPENSTYLE_STIPPLE
:
1915 case wxPENSTYLE_SOLID
:
1917 lineStyle
= GDK_LINE_SOLID
;
1918 req_dash
= (wxGTKDash
*)NULL
;
1923 if (req_dash
&& req_nb_dash
)
1925 wxGTKDash
*real_req_dash
= new wxGTKDash
[req_nb_dash
];
1928 for (int i
= 0; i
< req_nb_dash
; i
++)
1929 real_req_dash
[i
] = req_dash
[i
] * width
;
1930 gdk_gc_set_dashes( m_penGC
, 0, real_req_dash
, req_nb_dash
);
1931 delete[] real_req_dash
;
1935 // No Memory. We use non-scaled dash pattern...
1936 gdk_gc_set_dashes( m_penGC
, 0, (wxGTKDash
*)req_dash
, req_nb_dash
);
1940 GdkCapStyle capStyle
= GDK_CAP_ROUND
;
1941 switch (m_pen
.GetCap())
1943 case wxCAP_PROJECTING
: { capStyle
= GDK_CAP_PROJECTING
; break; }
1944 case wxCAP_BUTT
: { capStyle
= GDK_CAP_BUTT
; break; }
1950 capStyle
= GDK_CAP_NOT_LAST
;
1955 GdkJoinStyle joinStyle
= GDK_JOIN_ROUND
;
1956 switch (m_pen
.GetJoin())
1958 case wxJOIN_BEVEL
: { joinStyle
= GDK_JOIN_BEVEL
; break; }
1959 case wxJOIN_MITER
: { joinStyle
= GDK_JOIN_MITER
; break; }
1961 default: { joinStyle
= GDK_JOIN_ROUND
; break; }
1964 gdk_gc_set_line_attributes( m_penGC
, width
, lineStyle
, capStyle
, joinStyle
);
1966 m_pen
.GetColour().CalcPixel( m_cmap
);
1967 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
1970 void wxWindowDCImpl::SetBrush( const wxBrush
&brush
)
1972 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1974 if (m_brush
== brush
) return;
1978 if (!m_brush
.IsOk()) return;
1980 if (!m_gdkwindow
) return;
1982 m_brush
.GetColour().CalcPixel( m_cmap
);
1983 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
1985 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
1987 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
) && (m_brush
.GetStipple()->IsOk()))
1989 if (m_brush
.GetStipple()->GetDepth() != 1)
1991 gdk_gc_set_fill( m_brushGC
, GDK_TILED
);
1992 gdk_gc_set_tile( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1996 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1997 gdk_gc_set_stipple( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
2001 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
) && (m_brush
.GetStipple()->GetMask()))
2003 gdk_gc_set_fill( m_textGC
, GDK_OPAQUE_STIPPLED
);
2004 gdk_gc_set_stipple( m_textGC
, m_brush
.GetStipple()->GetMask()->GetBitmap() );
2007 if (m_brush
.IsHatch())
2009 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
2010 gdk_gc_set_stipple(m_brushGC
, GetHatch(m_brush
.GetStyle()));
2014 void wxWindowDCImpl::SetBackground( const wxBrush
&brush
)
2016 /* CMB 21/7/98: Added SetBackground. Sets background brush
2017 * for Clear() and bg colour for shapes filled with cross-hatch brush */
2019 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2021 if (m_backgroundBrush
== brush
) return;
2023 m_backgroundBrush
= brush
;
2025 if (!m_backgroundBrush
.IsOk()) return;
2027 if (!m_gdkwindow
) return;
2029 wxColor color
= m_backgroundBrush
.GetColour();
2030 color
.CalcPixel(m_cmap
);
2031 const GdkColor
* gdkColor
= color
.GetColor();
2032 gdk_gc_set_background(m_brushGC
, gdkColor
);
2033 gdk_gc_set_background(m_penGC
, gdkColor
);
2034 gdk_gc_set_background(m_bgGC
, gdkColor
);
2035 gdk_gc_set_foreground(m_bgGC
, gdkColor
);
2038 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
2040 if (m_backgroundBrush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
)
2042 const wxBitmap
* stipple
= m_backgroundBrush
.GetStipple();
2043 if (stipple
->IsOk())
2045 if (stipple
->GetDepth() != 1)
2047 gdk_gc_set_fill(m_bgGC
, GDK_TILED
);
2048 gdk_gc_set_tile(m_bgGC
, stipple
->GetPixmap());
2052 gdk_gc_set_fill(m_bgGC
, GDK_STIPPLED
);
2053 gdk_gc_set_stipple(m_bgGC
, stipple
->GetPixmap());
2057 else if (m_backgroundBrush
.IsHatch())
2059 gdk_gc_set_fill( m_bgGC
, GDK_STIPPLED
);
2060 gdk_gc_set_stipple(m_bgGC
, GetHatch(m_backgroundBrush
.GetStyle()));
2064 void wxWindowDCImpl::SetLogicalFunction( int function
)
2066 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2068 if (m_logicalFunction
== function
)
2071 // VZ: shouldn't this be a CHECK?
2078 case wxXOR
: mode
= GDK_XOR
; break;
2079 case wxINVERT
: mode
= GDK_INVERT
; break;
2080 case wxOR_REVERSE
: mode
= GDK_OR_REVERSE
; break;
2081 case wxAND_REVERSE
: mode
= GDK_AND_REVERSE
; break;
2082 case wxCLEAR
: mode
= GDK_CLEAR
; break;
2083 case wxSET
: mode
= GDK_SET
; break;
2084 case wxOR_INVERT
: mode
= GDK_OR_INVERT
; break;
2085 case wxAND
: mode
= GDK_AND
; break;
2086 case wxOR
: mode
= GDK_OR
; break;
2087 case wxEQUIV
: mode
= GDK_EQUIV
; break;
2088 case wxNAND
: mode
= GDK_NAND
; break;
2089 case wxAND_INVERT
: mode
= GDK_AND_INVERT
; break;
2090 case wxCOPY
: mode
= GDK_COPY
; break;
2091 case wxNO_OP
: mode
= GDK_NOOP
; break;
2092 case wxSRC_INVERT
: mode
= GDK_COPY_INVERT
; break;
2093 case wxNOR
: mode
= GDK_NOR
; break;
2095 wxFAIL_MSG( wxT("unsupported logical function") );
2099 m_logicalFunction
= function
;
2101 gdk_gc_set_function( m_penGC
, mode
);
2102 gdk_gc_set_function( m_brushGC
, mode
);
2104 // to stay compatible with wxMSW, we don't apply ROPs to the text
2105 // operations (i.e. DrawText/DrawRotatedText).
2106 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2107 gdk_gc_set_function( m_textGC
, mode
);
2110 void wxWindowDCImpl::SetTextForeground( const wxColour
&col
)
2112 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2114 // don't set m_textForegroundColour to an invalid colour as we'd crash
2115 // later then (we use m_textForegroundColour.GetColor() without checking
2117 if ( !col
.IsOk() || (m_textForegroundColour
== col
) )
2120 m_textForegroundColour
= col
;
2124 m_textForegroundColour
.CalcPixel( m_cmap
);
2125 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
2129 void wxWindowDCImpl::SetTextBackground( const wxColour
&col
)
2131 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2134 if ( !col
.IsOk() || (m_textBackgroundColour
== col
) )
2137 m_textBackgroundColour
= col
;
2141 m_textBackgroundColour
.CalcPixel( m_cmap
);
2142 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
2146 void wxWindowDCImpl::SetBackgroundMode( int mode
)
2148 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2150 m_backgroundMode
= mode
;
2153 void wxWindowDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
2155 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
2158 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2160 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2162 if (!m_gdkwindow
) return;
2165 rect
.x
= XLOG2DEV(x
);
2166 rect
.y
= YLOG2DEV(y
);
2167 rect
.width
= XLOG2DEVREL(width
);
2168 rect
.height
= YLOG2DEVREL(height
);
2170 if (m_window
&& m_window
->m_wxwindow
&&
2171 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2173 rect
.x
-= rect
.width
;
2176 DoSetDeviceClippingRegion(wxRegion(rect
));
2179 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
2181 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2185 DestroyClippingRegion();
2189 if (!m_gdkwindow
) return;
2191 if (!m_currentClippingRegion
.IsNull())
2192 m_currentClippingRegion
.Intersect( region
);
2194 m_currentClippingRegion
.Union( region
);
2196 #if USE_PAINT_REGION
2197 if (!m_paintClippingRegion
.IsNull())
2198 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2201 wxCoord xx
, yy
, ww
, hh
;
2202 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2203 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2205 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2206 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2207 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2208 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2209 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2212 void wxWindowDCImpl::DestroyClippingRegion()
2214 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2216 wxDCImpl::DestroyClippingRegion();
2218 m_currentClippingRegion
.Clear();
2220 #if USE_PAINT_REGION
2221 if (!m_paintClippingRegion
.IsEmpty())
2222 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2225 if (!m_gdkwindow
) return;
2227 GdkRegion
* gdkRegion
= NULL
;
2228 if (!m_currentClippingRegion
.IsEmpty())
2229 gdkRegion
= m_currentClippingRegion
.GetRegion();
2231 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2232 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2233 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2234 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2237 void wxWindowDCImpl::Destroy()
2239 if (m_penGC
) wxFreePoolGC( m_penGC
);
2240 m_penGC
= (GdkGC
*) NULL
;
2241 if (m_brushGC
) wxFreePoolGC( m_brushGC
);
2242 m_brushGC
= (GdkGC
*) NULL
;
2243 if (m_textGC
) wxFreePoolGC( m_textGC
);
2244 m_textGC
= (GdkGC
*) NULL
;
2245 if (m_bgGC
) wxFreePoolGC( m_bgGC
);
2246 m_bgGC
= (GdkGC
*) NULL
;
2249 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
2251 m_deviceOriginX
= x
;
2252 m_deviceOriginY
= y
;
2254 ComputeScaleAndOrigin();
2257 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
2259 m_signX
= (xLeftRight
? 1 : -1);
2260 m_signY
= (yBottomUp
? -1 : 1);
2262 if (m_window
&& m_window
->m_wxwindow
&&
2263 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2266 ComputeScaleAndOrigin();
2269 void wxWindowDCImpl::ComputeScaleAndOrigin()
2271 const wxRealPoint
origScale(m_scaleX
, m_scaleY
);
2273 wxDCImpl::ComputeScaleAndOrigin();
2275 // if scale has changed call SetPen to recalulate the line width
2276 if ( wxRealPoint(m_scaleX
, m_scaleY
) != origScale
&& m_pen
.IsOk() )
2278 // this is a bit artificial, but we need to force wxDC to think the pen
2286 // Resolution in pixels per logical inch
2287 wxSize
wxWindowDCImpl::GetPPI() const
2289 return wxSize( (int) (m_mm_to_pix_x
* 25.4 + 0.5), (int) (m_mm_to_pix_y
* 25.4 + 0.5));
2292 int wxWindowDCImpl::GetDepth() const
2294 return gdk_drawable_get_depth(m_gdkwindow
);
2298 //-----------------------------------------------------------------------------
2300 //-----------------------------------------------------------------------------
2302 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
2304 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
2305 : wxWindowDCImpl( owner
)
2309 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*win
)
2310 : wxWindowDCImpl( owner
, win
)
2312 wxCHECK_RET( win
, _T("NULL window in wxClientDCImpl::wxClientDC") );
2314 #ifdef __WXUNIVERSAL__
2315 wxPoint ptOrigin
= win
->GetClientAreaOrigin();
2316 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
2317 wxSize size
= win
->GetClientSize();
2318 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
2323 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
2325 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
2327 m_window
->GetClientSize( width
, height
);
2330 //-----------------------------------------------------------------------------
2332 //-----------------------------------------------------------------------------
2334 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
2336 // Limit the paint region to the window size. Sometimes
2337 // the paint region is too big, and this risks X11 errors
2338 static void wxLimitRegionToSize(wxRegion
& region
, const wxSize
& sz
)
2340 wxRect originalRect
= region
.GetBox();
2341 wxRect
rect(originalRect
);
2342 if (rect
.width
+ rect
.x
> sz
.x
)
2343 rect
.width
= sz
.x
- rect
.x
;
2344 if (rect
.height
+ rect
.y
> sz
.y
)
2345 rect
.height
= sz
.y
- rect
.y
;
2346 if (rect
!= originalRect
)
2348 region
= wxRegion(rect
);
2349 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2350 originalRect
.x
, originalRect
.y
, originalRect
.width
, originalRect
.height
,
2351 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2355 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
2356 : wxClientDCImpl( owner
)
2360 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*win
)
2361 : wxClientDCImpl( owner
, win
)
2363 #if USE_PAINT_REGION
2364 if (!win
->m_clipPaintRegion
)
2367 wxSize sz
= win
->GetSize();
2368 m_paintClippingRegion
= win
->m_nativeUpdateRegion
;
2369 wxLimitRegionToSize(m_paintClippingRegion
, sz
);
2371 GdkRegion
*region
= m_paintClippingRegion
.GetRegion();
2374 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2375 wxLimitRegionToSize(m_currentClippingRegion
, sz
);
2377 if (sz
.x
<= 0 || sz
.y
<= 0)
2380 gdk_gc_set_clip_region( m_penGC
, region
);
2381 gdk_gc_set_clip_region( m_brushGC
, region
);
2382 gdk_gc_set_clip_region( m_textGC
, region
);
2383 gdk_gc_set_clip_region( m_bgGC
, region
);
2388 // ----------------------------------------------------------------------------
2390 // ----------------------------------------------------------------------------
2392 class wxDCModule
: public wxModule
2399 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2402 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2404 bool wxDCModule::OnInit()
2410 void wxDCModule::OnExit()
2414 for (int i
= wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
; i
--; )
2417 g_object_unref(hatches
[i
]);