1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dcclient.cpp
3 // Purpose: wxWindowDCImpl implementation
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"
13 #include "wx/gtk/dcclient.h"
16 #include "wx/window.h"
18 #include "wx/dcmemory.h"
21 #include "wx/module.h"
24 #include "wx/fontutil.h"
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/private/object.h"
29 using wxGTKPrivate::SetPangoAttrsForFont
;
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 #define XLOG2DEV(x) LogicalToDeviceX(x)
36 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
37 #define YLOG2DEV(y) LogicalToDeviceY(y)
38 #define YLOG2DEVREL(y) LogicalToDeviceYRel(y)
40 #define USE_PAINT_REGION 1
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
53 static GdkPixmap
* hatches
[wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
+ 1];
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 static const double RAD2DEG
= 180.0 / M_PI
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
66 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
68 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
70 static GdkPixmap
* GetHatch(int style
)
72 wxASSERT(style
>= wxBRUSHSTYLE_FIRST_HATCH
&& style
<= wxBRUSHSTYLE_LAST_HATCH
);
73 const int i
= style
- wxBRUSHSTYLE_FIRST_HATCH
;
74 if (hatches
[i
] == NULL
)
76 // This macro creates a bitmap from an XBM file included above. Notice
77 // the need for the cast because gdk_bitmap_create_from_data() doesn't
78 // accept unsigned data but the arrays in XBM need to be unsigned to
79 // avoid warnings (and even errors in C+0x mode) from g++.
80 #define CREATE_FROM_XBM_DATA(name) \
81 gdk_bitmap_create_from_data \
84 reinterpret_cast<gchar *>(name ## _bits), \
91 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
92 hatches
[i
] = CREATE_FROM_XBM_DATA(bdiag
);
94 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
95 hatches
[i
] = CREATE_FROM_XBM_DATA(cdiag
);
97 case wxBRUSHSTYLE_CROSS_HATCH
:
98 hatches
[i
] = CREATE_FROM_XBM_DATA(cross
);
100 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
101 hatches
[i
] = CREATE_FROM_XBM_DATA(fdiag
);
103 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
104 hatches
[i
] = CREATE_FROM_XBM_DATA(horiz
);
106 case wxBRUSHSTYLE_VERTICAL_HATCH
:
107 hatches
[i
] = CREATE_FROM_XBM_DATA(verti
);
111 #undef CREATE_FROM_XBM_DATA
116 //-----------------------------------------------------------------------------
117 // Implement Pool of Graphic contexts. Creating them takes too much time.
118 //-----------------------------------------------------------------------------
144 #define GC_POOL_ALLOC_SIZE 100
146 static int wxGCPoolSize
= 0;
148 static wxGC
*wxGCPool
= NULL
;
150 static void wxInitGCPool()
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.
156 // Set initial pool size.
157 wxGCPoolSize
= GC_POOL_ALLOC_SIZE
;
159 // Allocate initial pool.
160 wxGCPool
= (wxGC
*)malloc(wxGCPoolSize
* sizeof(wxGC
));
161 if (wxGCPool
== NULL
)
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
167 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
171 // Zero initial pool.
172 memset(wxGCPool
, 0, wxGCPoolSize
* sizeof(wxGC
));
175 static void wxCleanUpGCPool()
177 for (int i
= 0; i
< wxGCPoolSize
; i
++)
179 if (wxGCPool
[i
].m_gc
)
180 g_object_unref (wxGCPool
[i
].m_gc
);
188 static GdkGC
* wxGetPoolGC( GdkWindow
*window
, wxPoolGCType type
)
192 // Look for an available GC.
193 for (int i
= 0; i
< wxGCPoolSize
; i
++)
195 if (!wxGCPool
[i
].m_gc
)
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;
202 if ((!wxGCPool
[i
].m_used
) && (wxGCPool
[i
].m_type
== type
))
204 wxGCPool
[i
].m_used
= true;
205 return wxGCPool
[i
].m_gc
;
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
));
215 // Initialize newly allocated pool.
217 memset(&wxGCPool
[wxGCPoolSize
], 0,
218 GC_POOL_ALLOC_SIZE
*sizeof(wxGC
));
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;
226 // Set new value of pool size.
227 wxGCPoolSize
+= GC_POOL_ALLOC_SIZE
;
229 // Return newly allocated entry.
230 return wxGCPool
[wxGCPoolSize
-GC_POOL_ALLOC_SIZE
].m_gc
;
233 // The realloc failed. Fall through to error.
234 wxFAIL_MSG( wxT("No GC available") );
239 static void wxFreePoolGC( GdkGC
*gc
)
241 for (int i
= 0; i
< wxGCPoolSize
; i
++)
243 if (wxGCPool
[i
].m_gc
== gc
)
245 wxGCPool
[i
].m_used
= false;
250 wxFAIL_MSG( wxT("Wrong GC") );
253 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
257 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxGTKDCImpl
)
259 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
268 m_isScreenDC
= false;
274 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
277 wxASSERT_MSG( window
, wxT("DC needs a window") );
285 m_isScreenDC
= false;
286 m_font
= window
->GetFont();
288 GtkWidget
*widget
= window
->m_wxwindow
;
289 m_gdkwindow
= window
->GTKGetDrawingWindow();
291 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
292 // code should still be able to create wxClientDCs for them
295 widget
= window
->m_widget
;
297 wxCHECK_RET(widget
, "DC needs a widget");
299 m_gdkwindow
= widget
->window
;
300 if (!gtk_widget_get_has_window(widget
))
301 SetDeviceLocalOrigin(widget
->allocation
.x
, widget
->allocation
.y
);
304 m_context
= window
->GTKGetPangoDefaultContext();
305 m_layout
= pango_layout_new( m_context
);
306 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
308 // Window not realized ?
311 // Don't report problems as per MSW.
317 m_cmap
= gtk_widget_get_colormap(widget
);
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) */
330 if (m_window
&& m_window
->m_wxwindow
&&
331 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
336 // origin in the upper right corner
337 m_deviceOriginX
= m_window
->GetClientSize().x
;
341 wxWindowDCImpl::~wxWindowDCImpl()
346 g_object_unref (m_layout
);
348 pango_font_description_free( m_fontdesc
);
351 void wxWindowDCImpl::SetUpDC( bool isMemDC
)
355 wxASSERT_MSG( !m_penGC
, wxT("GCs already created") );
359 if ((isMemDC
) && (GetSelectedBitmap().IsOk()))
361 if (GetSelectedBitmap().GetDepth() == 1)
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
);
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
);
382 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR
);
383 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR
);
384 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR
);
385 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR
);
389 /* background colour */
390 m_backgroundBrush
= *wxWHITE_BRUSH
;
391 m_backgroundBrush
.GetColour().CalcPixel( m_cmap
);
392 const GdkColor
*bg_col
= m_backgroundBrush
.GetColour().GetColor();
395 m_textForegroundColour
.CalcPixel( m_cmap
);
396 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
398 m_textBackgroundColour
.CalcPixel( m_cmap
);
399 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
401 gdk_gc_set_fill( m_textGC
, GDK_SOLID
);
403 gdk_gc_set_colormap( m_textGC
, m_cmap
);
406 m_pen
.GetColour().CalcPixel( m_cmap
);
407 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
408 gdk_gc_set_background( m_penGC
, bg_col
);
410 gdk_gc_set_line_attributes( m_penGC
, 0, GDK_LINE_SOLID
, GDK_CAP_NOT_LAST
, GDK_JOIN_ROUND
);
413 m_brush
.GetColour().CalcPixel( m_cmap
);
414 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
415 gdk_gc_set_background( m_brushGC
, bg_col
);
417 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
420 gdk_gc_set_background( m_bgGC
, bg_col
);
421 gdk_gc_set_foreground( m_bgGC
, bg_col
);
423 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
426 gdk_gc_set_function( m_textGC
, GDK_COPY
);
427 gdk_gc_set_function( m_brushGC
, GDK_COPY
);
428 gdk_gc_set_function( m_penGC
, GDK_COPY
);
431 gdk_gc_set_clip_rectangle( m_penGC
, NULL
);
432 gdk_gc_set_clip_rectangle( m_brushGC
, NULL
);
433 gdk_gc_set_clip_rectangle( m_textGC
, NULL
);
434 gdk_gc_set_clip_rectangle( m_bgGC
, NULL
);
437 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
439 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
441 m_window
->GetSize(width
, height
);
444 bool wxWindowDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
445 const wxColour
& col
, wxFloodFillStyle style
)
448 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
449 const wxColour
& col
, wxFloodFillStyle style
);
451 return wxDoFloodFill( GetOwner(), x
, y
, col
, style
);
462 bool wxWindowDCImpl::DoGetPixel( wxCoord x1
, wxCoord y1
, wxColour
*col
) const
464 GdkImage
* image
= NULL
;
467 const int x
= LogicalToDeviceX(x1
);
468 const int y
= LogicalToDeviceY(y1
);
470 gdk_drawable_get_size(m_gdkwindow
, &rect
.width
, &rect
.height
);
471 if (rect
.Contains(x
, y
))
472 image
= gdk_drawable_get_image(m_gdkwindow
, x
, y
, 1, 1);
479 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
480 const unsigned pixel
= gdk_image_get_pixel(image
, 0, 0);
481 if (colormap
== NULL
)
482 *col
= pixel
? m_textForegroundColour
: m_textBackgroundColour
;
486 gdk_colormap_query_color(colormap
, pixel
, &c
);
487 col
->Set(c
.red
>> 8, c
.green
>> 8, c
.blue
>> 8);
489 g_object_unref(image
);
493 void wxWindowDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
495 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
497 if ( m_pen
.IsNonTransparent() )
500 gdk_draw_line( m_gdkwindow
, m_penGC
, XLOG2DEV(x1
), YLOG2DEV(y1
), XLOG2DEV(x2
), YLOG2DEV(y2
) );
502 CalcBoundingBox(x1
, y1
);
503 CalcBoundingBox(x2
, y2
);
507 void wxWindowDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
509 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
511 if ( m_pen
.IsNonTransparent() )
515 GetOwner()->GetSize( &w
, &h
);
516 wxCoord xx
= XLOG2DEV(x
);
517 wxCoord yy
= YLOG2DEV(y
);
520 gdk_draw_line( m_gdkwindow
, m_penGC
, 0, yy
, XLOG2DEVREL(w
), yy
);
521 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, 0, xx
, YLOG2DEVREL(h
) );
526 void wxWindowDCImpl::DrawingSetup(GdkGC
*& gc
, bool& originChanged
)
529 GdkPixmap
* pixmap
= NULL
;
530 const int style
= m_brush
.GetStyle();
532 if (style
== wxBRUSHSTYLE_STIPPLE
|| style
== wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
)
534 const wxBitmap
* stipple
= m_brush
.GetStipple();
537 if (style
== wxBRUSHSTYLE_STIPPLE
)
538 pixmap
= stipple
->GetPixmap();
539 else if (stipple
->GetMask())
541 pixmap
= stipple
->GetPixmap();
546 else if (m_brush
.IsHatch())
548 pixmap
= GetHatch(style
);
556 gdk_drawable_get_size(pixmap
, &w
, &h
);
557 origin_x
= m_deviceOriginX
% w
;
558 origin_y
= m_deviceOriginY
% h
;
561 originChanged
= origin_x
|| origin_y
;
563 gdk_gc_set_ts_origin(gc
, origin_x
, origin_y
);
566 void wxWindowDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
,
567 wxCoord xc
, wxCoord yc
)
569 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
571 wxCoord xx1
= XLOG2DEV(x1
);
572 wxCoord yy1
= YLOG2DEV(y1
);
573 wxCoord xx2
= XLOG2DEV(x2
);
574 wxCoord yy2
= YLOG2DEV(y2
);
575 wxCoord xxc
= XLOG2DEV(xc
);
576 wxCoord yyc
= YLOG2DEV(yc
);
577 double dx
= xx1
- xxc
;
578 double dy
= yy1
- yyc
;
579 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
580 wxCoord r
= (wxCoord
)radius
;
581 double radius1
, radius2
;
583 if (xx1
== xx2
&& yy1
== yy2
)
588 else if ( wxIsNullDouble(radius
) )
595 radius1
= (xx1
- xxc
== 0) ?
596 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
597 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
598 radius2
= (xx2
- xxc
== 0) ?
599 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
600 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
602 wxCoord alpha1
= wxCoord(radius1
* 64.0);
603 wxCoord alpha2
= wxCoord((radius2
- radius1
) * 64.0);
604 while (alpha2
<= 0) alpha2
+= 360*64;
605 while (alpha1
> 360*64) alpha1
-= 360*64;
609 if ( m_brush
.IsNonTransparent() )
613 DrawingSetup(gc
, originChanged
);
615 gdk_draw_arc(m_gdkwindow
, gc
, true, xxc
-r
, yyc
-r
, 2*r
, 2*r
, alpha1
, alpha2
);
618 gdk_gc_set_ts_origin(gc
, 0, 0);
621 if ( m_pen
.IsNonTransparent() )
623 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xxc
-r
, yyc
-r
, 2*r
,2*r
, alpha1
, alpha2
);
625 if ( m_brush
.IsNonTransparent() && (alpha2
- alpha1
!= 360*64) )
627 gdk_draw_line( m_gdkwindow
, m_penGC
, xx1
, yy1
, xxc
, yyc
);
628 gdk_draw_line( m_gdkwindow
, m_penGC
, xxc
, yyc
, xx2
, yy2
);
633 CalcBoundingBox (x1
, y1
);
634 CalcBoundingBox (x2
, y2
);
637 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double sa
, double ea
)
639 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
641 wxCoord xx
= XLOG2DEV(x
);
642 wxCoord yy
= YLOG2DEV(y
);
643 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
644 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
646 // CMB: handle -ve width and/or height
647 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
648 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
652 wxCoord start
= wxCoord(sa
* 64.0);
653 wxCoord end
= wxCoord((ea
-sa
) * 64.0);
655 if ( m_brush
.IsNonTransparent() )
659 DrawingSetup(gc
, originChanged
);
661 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, start
, end
);
664 gdk_gc_set_ts_origin(gc
, 0, 0);
667 if ( m_pen
.IsNonTransparent() )
668 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, start
, end
);
671 CalcBoundingBox (x
, y
);
672 CalcBoundingBox (x
+ width
, y
+ height
);
675 void wxWindowDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
677 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
679 if ( m_pen
.IsNonTransparent() && m_gdkwindow
)
680 gdk_draw_point( m_gdkwindow
, m_penGC
, XLOG2DEV(x
), YLOG2DEV(y
) );
682 CalcBoundingBox (x
, y
);
685 void wxWindowDCImpl::DoDrawLines( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
687 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
691 if ( m_pen
.IsTransparent() )
694 //Check, if scaling is necessary
696 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
698 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
699 GdkPoint
* gpts
= reinterpret_cast<GdkPoint
*>(points
);
702 gpts
= new GdkPoint
[n
];
704 for (int i
= 0; i
< n
; i
++)
708 gpts
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
709 gpts
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
711 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
715 gdk_draw_lines( m_gdkwindow
, m_penGC
, gpts
, n
);
721 void wxWindowDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
722 wxCoord xoffset
, wxCoord yoffset
,
723 wxPolygonFillMode
WXUNUSED(fillStyle
) )
725 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
729 //Check, if scaling is necessary
731 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
733 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
734 GdkPoint
* gdkpoints
= reinterpret_cast<GdkPoint
*>(points
);
737 gdkpoints
= new GdkPoint
[n
];
740 for (i
= 0 ; i
< n
; i
++)
744 gdkpoints
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
745 gdkpoints
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
747 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
752 if ( m_brush
.IsNonTransparent() )
756 DrawingSetup(gc
, originChanged
);
758 gdk_draw_polygon(m_gdkwindow
, gc
, true, gdkpoints
, n
);
761 gdk_gc_set_ts_origin(gc
, 0, 0);
764 if ( m_pen
.IsNonTransparent() )
767 for (i = 0 ; i < n ; i++)
769 gdk_draw_line( m_gdkwindow, m_penGC,
772 gdkpoints[(i+1)%n].x,
773 gdkpoints[(i+1)%n].y);
776 gdk_draw_polygon( m_gdkwindow
, m_penGC
, FALSE
, gdkpoints
, n
);
785 void wxWindowDCImpl::DoDrawRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
787 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
789 wxCoord xx
= XLOG2DEV(x
);
790 wxCoord yy
= YLOG2DEV(y
);
791 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
792 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
794 // CMB: draw nothing if transformed w or h is 0
795 if (ww
== 0 || hh
== 0) return;
797 // CMB: handle -ve width and/or height
798 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
799 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
803 if ( m_brush
.IsNonTransparent() )
807 DrawingSetup(gc
, originChanged
);
809 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
);
812 gdk_gc_set_ts_origin(gc
, 0, 0);
815 if ( m_pen
.IsNonTransparent() )
817 if ((m_pen
.GetWidth() == 2) && (m_pen
.GetCap() == wxCAP_ROUND
) &&
818 (m_pen
.GetJoin() == wxJOIN_ROUND
) && (m_pen
.GetStyle() == wxPENSTYLE_SOLID
))
820 // Use 2 1-line rects instead
821 gdk_gc_set_line_attributes( m_penGC
, 1, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
826 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
+1, yy
, ww
-2, hh
-2 );
827 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
-1, ww
, hh
);
831 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-2, hh
-2 );
832 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
-1, yy
-1, ww
, hh
);
836 gdk_gc_set_line_attributes( m_penGC
, 2, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
840 // Just use X11 for other cases
841 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-1, hh
-1 );
846 CalcBoundingBox( x
, y
);
847 CalcBoundingBox( x
+ width
, y
+ height
);
850 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
852 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
854 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
856 wxCoord xx
= XLOG2DEV(x
);
857 wxCoord yy
= YLOG2DEV(y
);
858 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
859 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
860 wxCoord rr
= XLOG2DEVREL((wxCoord
)radius
);
862 // CMB: handle -ve width and/or height
863 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
864 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
866 // CMB: if radius is zero use DrawRectangle() instead to avoid
867 // X drawing errors with small radii
870 DoDrawRectangle( x
, y
, width
, height
);
874 // CMB: draw nothing if transformed w or h is 0
875 if (ww
== 0 || hh
== 0) return;
877 // CMB: adjust size if outline is drawn otherwise the result is
878 // 1 pixel too wide and high
879 if ( m_pen
.IsNonTransparent() )
887 // CMB: ensure dd is not larger than rectangle otherwise we
888 // get an hour glass shape
890 if (dd
> ww
) dd
= ww
;
891 if (dd
> hh
) dd
= hh
;
894 if ( m_brush
.IsNonTransparent() )
898 DrawingSetup(gc
, originChanged
);
900 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
+rr
, yy
, ww
-dd
+1, hh
);
901 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
+rr
, ww
, hh
-dd
+1);
902 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, dd
, dd
, 90*64, 90*64);
903 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64);
904 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64);
905 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64);
908 gdk_gc_set_ts_origin(gc
, 0, 0);
911 if ( m_pen
.IsNonTransparent() )
913 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
, xx
+ww
-rr
, yy
);
914 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
+hh
, xx
+ww
-rr
, yy
+hh
);
915 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, yy
+rr
+1, xx
, yy
+hh
-rr
);
916 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+ww
, yy
+rr
+1, xx
+ww
, yy
+hh
-rr
);
917 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, dd
, dd
, 90*64, 90*64 );
918 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64 );
919 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64 );
920 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64 );
924 // this ignores the radius
925 CalcBoundingBox( x
, y
);
926 CalcBoundingBox( x
+ width
, y
+ height
);
929 void wxWindowDCImpl::DoDrawEllipse( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
931 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
933 wxCoord xx
= XLOG2DEV(x
);
934 wxCoord yy
= YLOG2DEV(y
);
935 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
936 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
938 // CMB: handle -ve width and/or height
939 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
940 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
944 if ( m_brush
.IsNonTransparent() )
948 DrawingSetup(gc
, originChanged
);
950 // If the pen is transparent pen we increase the size
951 // for better compatibility with other platforms.
952 if ( m_pen
.IsNonTransparent() )
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
.IsNonTransparent() )
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 // scale a pixbuf, return new pixbuf, unref old one
980 Scale(GdkPixbuf
* pixbuf
, int dst_w
, int dst_h
, double sx
, double sy
)
982 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
983 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, dst_w
, dst_h
);
984 gdk_pixbuf_scale(pixbuf
, pixbuf_scaled
,
985 0, 0, dst_w
, dst_h
, 0, 0, sx
, sy
, GDK_INTERP_NEAREST
);
986 g_object_unref(pixbuf
);
987 return pixbuf_scaled
;
990 // scale part of a pixmap using pixbuf scaling, return pixbuf
992 Scale(GdkPixmap
* pixmap
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
994 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(
995 NULL
, pixmap
, NULL
, x
, y
, 0, 0, w
, h
);
996 return Scale(pixbuf
, dst_w
, dst_h
, sx
, sy
);
999 // scale part of a mask pixmap, return new mask, unref old one
1001 ScaleMask(GdkPixmap
* mask
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
1003 GdkPixbuf
* pixbuf
= Scale(mask
, x
, y
, w
, h
, dst_w
, dst_h
, sx
, sy
);
1005 // convert black and white pixbuf back to a mono pixmap
1006 const unsigned out_rowstride
= (dst_w
+ 7) / 8;
1007 const size_t data_size
= out_rowstride
* size_t(dst_h
);
1008 char* data
= new char[data_size
];
1010 const guchar
* row
= gdk_pixbuf_get_pixels(pixbuf
);
1011 const int rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
1012 memset(data
, 0, data_size
);
1013 for (int j
= 0; j
< dst_h
; j
++, row
+= rowstride
, out
+= out_rowstride
)
1015 const guchar
* in
= row
;
1016 for (int i
= 0; i
< dst_w
; i
++, in
+= 3)
1018 out
[i
>> 3] |= 1 << (i
& 7);
1020 g_object_unref(pixbuf
);
1021 GdkPixmap
* pixmap
= gdk_bitmap_create_from_data(mask
, data
, dst_w
, dst_h
);
1023 g_object_unref(mask
);
1027 // Make a new mask from part of a mask and a clip region.
1028 // Return new mask, unref old one.
1030 ClipMask(GdkPixmap
* mask
, GdkRegion
* clipRegion
, int x
, int y
, int dst_x
, int dst_y
, int w
, int h
)
1032 GdkGCValues gcValues
;
1033 gcValues
.foreground
.pixel
= 0;
1034 GdkGC
* gc
= gdk_gc_new_with_values(mask
, &gcValues
, GDK_GC_FOREGROUND
);
1035 GdkPixmap
* pixmap
= gdk_pixmap_new(mask
, w
, h
, 1);
1036 // clear new mask, so clipped areas will be masked
1037 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1038 gdk_gc_set_clip_region(gc
, clipRegion
);
1039 gdk_gc_set_clip_origin(gc
, -dst_x
, -dst_y
);
1040 // draw old mask onto new one, with clip
1041 gdk_draw_drawable(pixmap
, gc
, mask
, x
, y
, 0, 0, w
, h
);
1043 g_object_unref(mask
);
1047 // make a color pixmap from part of a mono one, using text fg/bg colors
1049 wxWindowDCImpl::MonoToColor(GdkPixmap
* monoPixmap
, int x
, int y
, int w
, int h
) const
1051 GdkPixmap
* pixmap
= gdk_pixmap_new(m_gdkwindow
, w
, h
, -1);
1052 GdkGCValues gcValues
;
1053 gcValues
.foreground
.pixel
= m_textForegroundColour
.GetColor()->pixel
;
1054 gcValues
.background
.pixel
= m_textBackgroundColour
.GetColor()->pixel
;
1055 gcValues
.stipple
= monoPixmap
;
1056 gcValues
.fill
= GDK_OPAQUE_STIPPLED
;
1057 gcValues
.ts_x_origin
= -x
;
1058 gcValues
.ts_y_origin
= -y
;
1059 GdkGC
* gc
= gdk_gc_new_with_values(pixmap
, &gcValues
, GdkGCValuesMask(
1060 GDK_GC_FOREGROUND
| GDK_GC_BACKGROUND
| GDK_GC_STIPPLE
| GDK_GC_FILL
|
1061 GDK_GC_TS_X_ORIGIN
| GDK_GC_TS_Y_ORIGIN
));
1062 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1067 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap
&bitmap
,
1068 wxCoord x
, wxCoord y
,
1071 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1072 wxCHECK_RET( bitmap
.IsOk(), wxT("invalid bitmap") );
1074 if (!m_gdkwindow
) return;
1076 const int w
= bitmap
.GetWidth();
1077 const int h
= bitmap
.GetHeight();
1079 // notice that as the bitmap is not drawn upside down (or right to left)
1080 // even if the corresponding axis direction is inversed, we need to take it
1081 // into account when calculating its bounding box
1082 CalcBoundingBox(x
, y
);
1083 CalcBoundingBox(x
+ m_signX
*w
, y
+ m_signY
*h
);
1086 int xx
= LogicalToDeviceX(x
);
1087 const int yy
= LogicalToDeviceY(y
);
1088 const int ww
= LogicalToDeviceXRel(w
);
1089 const int hh
= LogicalToDeviceYRel(h
);
1091 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1094 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1095 // determine clip region overlap
1096 int overlap
= wxInRegion
;
1099 overlap
= m_currentClippingRegion
.Contains(xx
, yy
, ww
, hh
);
1100 if (overlap
== wxOutRegion
)
1104 const bool isScaled
= ww
!= w
|| hh
!= h
;
1105 const bool hasAlpha
= bitmap
.HasAlpha();
1106 GdkGC
* const use_gc
= m_penGC
;
1108 GdkPixmap
* mask
= NULL
;
1109 // mask does not work when drawing a pixbuf with alpha
1110 if (useMask
&& !hasAlpha
)
1112 wxMask
* m
= bitmap
.GetMask();
1114 mask
= m
->GetBitmap();
1120 mask
= ScaleMask(mask
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1121 if (overlap
== wxPartRegion
)
1123 // need a new mask that also masks the clipped area,
1124 // because gc can't have both a mask and a clip region
1125 mask
= ClipMask(mask
, clipRegion
, 0, 0, xx
, yy
, ww
, hh
);
1127 gdk_gc_set_clip_mask(use_gc
, mask
);
1128 gdk_gc_set_clip_origin(use_gc
, xx
, yy
);
1131 // determine whether to use pixmap or pixbuf
1132 GdkPixmap
* pixmap
= NULL
;
1133 GdkPixbuf
* pixbuf
= NULL
;
1134 if (bitmap
.HasPixmap())
1135 pixmap
= bitmap
.GetPixmap();
1136 if (pixmap
&& gdk_drawable_get_depth(pixmap
) == 1)
1138 // convert mono pixmap to color using text fg/bg colors
1139 pixmap
= MonoToColor(pixmap
, 0, 0, w
, h
);
1141 else if (hasAlpha
|| pixmap
== NULL
)
1144 pixbuf
= bitmap
.GetPixbuf();
1145 g_object_ref(pixbuf
);
1149 g_object_ref(pixmap
);
1155 pixbuf
= Scale(pixbuf
, ww
, hh
, m_scaleX
, m_scaleY
);
1157 pixbuf
= Scale(pixmap
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1162 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1163 0, 0, xx
, yy
, ww
, hh
, GDK_RGB_DITHER_NORMAL
, 0, 0);
1164 g_object_unref(pixbuf
);
1168 gdk_draw_drawable(m_gdkwindow
, use_gc
, pixmap
, 0, 0, xx
, yy
, ww
, hh
);
1172 g_object_unref(pixmap
);
1175 g_object_unref(mask
);
1176 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1180 bool wxWindowDCImpl::DoBlit( wxCoord xdest
, wxCoord ydest
,
1181 wxCoord width
, wxCoord height
,
1183 wxCoord xsrc
, wxCoord ysrc
,
1184 wxRasterOperationMode logical_func
,
1186 wxCoord xsrcMask
, wxCoord ysrcMask
)
1188 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1189 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1191 if (!m_gdkwindow
) return false;
1193 GdkDrawable
* srcDrawable
= NULL
;
1194 GdkPixmap
* mask
= NULL
;
1195 wxMemoryDC
* memDC
= wxDynamicCast(source
, wxMemoryDC
);
1198 const wxBitmap
& bitmap
= memDC
->GetSelectedBitmap();
1201 srcDrawable
= bitmap
.GetPixmap();
1204 wxMask
* m
= bitmap
.GetMask();
1206 mask
= m
->GetBitmap();
1211 wxDCImpl
* impl
= source
->GetImpl();
1212 wxWindowDCImpl
* gtk_impl
= wxDynamicCast(impl
, wxWindowDCImpl
);
1214 srcDrawable
= gtk_impl
->GetGDKWindow();
1215 if (srcDrawable
== NULL
)
1219 CalcBoundingBox(xdest
, ydest
);
1220 CalcBoundingBox(xdest
+ width
, ydest
+ height
);
1222 // source device coords
1223 int src_x
= source
->LogicalToDeviceX(xsrc
);
1224 int src_y
= source
->LogicalToDeviceY(ysrc
);
1225 int src_w
= source
->LogicalToDeviceXRel(width
);
1226 int src_h
= source
->LogicalToDeviceYRel(height
);
1228 // Clip source rect to source dc.
1229 // Only necessary when scaling, to avoid GDK errors when
1230 // converting to pixbuf, but no harm in always doing it.
1231 // If source rect changes, it also changes the dest rect.
1233 gdk_drawable_get_size(srcDrawable
, &clip
.width
, &clip
.height
);
1234 clip
.Intersect(wxRect(src_x
, src_y
, src_w
, src_h
));
1235 if (src_w
!= clip
.width
|| src_h
!= clip
.height
)
1237 if (clip
.width
== 0)
1241 src_h
= clip
.height
;
1242 width
= source
->DeviceToLogicalXRel(src_w
);
1243 height
= source
->DeviceToLogicalYRel(src_h
);
1244 if (src_x
!= clip
.x
|| src_y
!= clip
.y
)
1246 xdest
+= source
->DeviceToLogicalXRel(clip
.x
- src_x
);
1247 ydest
+= source
->DeviceToLogicalYRel(clip
.y
- src_y
);
1253 // destination device coords
1254 const int dst_x
= LogicalToDeviceX(xdest
);
1255 const int dst_y
= LogicalToDeviceY(ydest
);
1256 const int dst_w
= LogicalToDeviceXRel(width
);
1257 const int dst_h
= LogicalToDeviceYRel(height
);
1259 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1260 // determine dest clip region overlap
1261 int overlap
= wxInRegion
;
1264 overlap
= m_currentClippingRegion
.Contains(dst_x
, dst_y
, dst_w
, dst_h
);
1265 if (overlap
== wxOutRegion
)
1269 const bool isScaled
= src_w
!= dst_w
|| src_h
!= dst_h
;
1274 // get source to dest scale
1275 double usx
, usy
, lsx
, lsy
;
1276 source
->GetUserScale(&usx
, &usy
);
1277 source
->GetLogicalScale(&lsx
, &lsy
);
1278 scale_x
= m_scaleX
/ (usx
* lsx
);
1279 scale_y
= m_scaleY
/ (usy
* lsy
);
1282 GdkGC
* const use_gc
= m_penGC
;
1287 int srcMask_x
= src_x
;
1288 int srcMask_y
= src_y
;
1289 if (xsrcMask
!= -1 || ysrcMask
!= -1)
1291 srcMask_x
= source
->LogicalToDeviceX(xsrcMask
);
1292 srcMask_y
= source
->LogicalToDeviceY(ysrcMask
);
1296 mask
= ScaleMask(mask
, srcMask_x
, srcMask_y
,
1297 src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1301 if (overlap
== wxPartRegion
)
1303 // need a new mask that also masks the clipped area,
1304 // because gc can't have both a mask and a clip region
1305 mask
= ClipMask(mask
, clipRegion
,
1306 srcMask_x
, srcMask_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1310 gdk_gc_set_clip_mask(use_gc
, mask
);
1311 gdk_gc_set_clip_origin(use_gc
, dst_x
- srcMask_x
, dst_y
- srcMask_y
);
1314 GdkPixmap
* pixmap
= NULL
;
1315 if (gdk_drawable_get_depth(srcDrawable
) == 1)
1317 // Convert mono pixmap to color using text fg/bg colors.
1318 // Scaling/drawing is simpler if this is done first.
1319 pixmap
= MonoToColor(srcDrawable
, src_x
, src_y
, src_w
, src_h
);
1320 srcDrawable
= pixmap
;
1325 const wxRasterOperationMode logical_func_save
= m_logicalFunction
;
1326 SetLogicalFunction(logical_func
);
1328 gdk_gc_set_subwindow(use_gc
, GDK_INCLUDE_INFERIORS
);
1332 GdkPixbuf
* pixbuf
= Scale(srcDrawable
,
1333 src_x
, src_y
, src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1334 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1335 0, 0, dst_x
, dst_y
, dst_w
, dst_h
, GDK_RGB_DITHER_NONE
, 0, 0);
1336 g_object_unref(pixbuf
);
1340 gdk_draw_drawable(m_gdkwindow
, use_gc
, srcDrawable
,
1341 src_x
, src_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1344 SetLogicalFunction(logical_func_save
);
1346 gdk_gc_set_subwindow(use_gc
, GDK_CLIP_BY_CHILDREN
);
1349 g_object_unref(pixmap
);
1352 g_object_unref(mask
);
1353 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1358 void wxWindowDCImpl::DoDrawText(const wxString
& text
,
1362 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1364 if (!m_gdkwindow
) return;
1366 if (text
.empty()) return;
1368 wxCoord x
= XLOG2DEV(xLogical
),
1369 y
= YLOG2DEV(yLogical
);
1371 wxCHECK_RET( m_context
, wxT("no Pango context") );
1372 wxCHECK_RET( m_layout
, wxT("no Pango layout") );
1373 wxCHECK_RET( m_fontdesc
, wxT("no Pango font description") );
1375 gdk_pango_context_set_colormap( m_context
, m_cmap
); // not needed in gtk+ >= 2.6
1377 bool underlined
= m_font
.IsOk() && m_font
.GetUnderlined();
1379 wxCharBuffer data
= wxGTK_CONV(text
);
1382 size_t datalen
= strlen(data
);
1384 // in Pango >= 1.16 the "underline of leading/trailing spaces" bug
1385 // has been fixed and thus the hack implemented below should never be used
1386 static bool pangoOk
= !wx_pango_version_check(1, 16, 0);
1388 bool needshack
= underlined
&& !pangoOk
;
1392 // a PangoLayout which has leading/trailing spaces with underlined font
1393 // is not correctly drawn by this pango version: Pango won't underline the spaces.
1394 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
1395 // this behaviour. To workaround this problem, we use a special hack here
1396 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
1397 // empty space characters and give them a dummy colour attribute.
1398 // This will force Pango to underline the leading/trailing spaces, too.
1400 wxCharBuffer
data_tmp(datalen
+ 6);
1401 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1402 memcpy(data_tmp
.data(), "\342\200\214", 3);
1403 // copy the user string
1404 memcpy(data_tmp
.data() + 3, data
, datalen
);
1405 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1406 memcpy(data_tmp
.data() + 3 + datalen
, "\342\200\214", 3);
1412 pango_layout_set_text(m_layout
, data
, datalen
);
1414 setAttrs
= SetPangoAttrsForFont(m_font
, m_layout
, datalen
, needshack
);
1417 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1420 // If there is a user or actually any scale applied to
1421 // the device context, scale the font.
1423 // scale font description
1424 oldSize
= pango_font_description_get_size(m_fontdesc
);
1425 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1427 // actually apply scaled font
1428 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1432 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1436 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1439 const GdkColor
* bg_col
= NULL
;
1440 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1441 bg_col
= m_textBackgroundColour
.GetColor();
1443 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x_rtl
, y
, m_layout
, NULL
, bg_col
);
1447 // reset unscaled size
1448 pango_font_description_set_size( m_fontdesc
, oldSize
);
1450 // actually apply unscaled font
1451 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1455 // undo underline attributes setting:
1456 pango_layout_set_attributes(m_layout
, NULL
);
1459 CalcBoundingBox(xLogical
+ int(w
/ m_scaleX
), yLogical
+ int(h
/ m_scaleY
));
1460 CalcBoundingBox(xLogical
, yLogical
);
1463 // TODO: When GTK2.6 is required, merge DoDrawText and DoDrawRotatedText to
1464 // avoid code duplication
1465 void wxWindowDCImpl::DoDrawRotatedText( const wxString
&text
, wxCoord x
, wxCoord y
, double angle
)
1467 if (!m_gdkwindow
|| text
.empty())
1470 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1473 if (!gtk_check_version(2,6,0))
1478 pango_layout_set_text(m_layout
, wxGTK_CONV(text
), -1);
1479 SetPangoAttrsForFont( m_font
, m_layout
);
1481 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1484 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1485 // If there is a user or actually any scale applied to
1486 // the device context, scale the font.
1488 // scale font description
1489 oldSize
= pango_font_description_get_size(m_fontdesc
);
1490 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1492 // actually apply scaled font
1493 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1497 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1499 const GdkColor
* bg_col
= NULL
;
1500 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1501 bg_col
= m_textBackgroundColour
.GetColor();
1504 PangoMatrix matrix
= PANGO_MATRIX_INIT
;
1505 pango_matrix_rotate (&matrix
, angle
);
1506 pango_context_set_matrix (m_context
, &matrix
);
1507 pango_layout_context_changed (m_layout
);
1509 // To be compatible with MSW, the rotation axis must be in the old
1511 // Calculate the vertices of the rotated rectangle containing the text,
1512 // relative to the old top-left vertex.
1513 // We could use the matrix for this, but it's simpler with trignonometry.
1514 double rad
= DegToRad(angle
);
1515 // the rectangle vertices are counted clockwise with the first one
1517 double x2
= w
* cos(rad
);
1518 double y2
= -w
* sin(rad
); // y axis points to the bottom, hence minus
1519 double x4
= h
* sin(rad
);
1520 double y4
= h
* cos(rad
);
1521 double x3
= x4
+ x2
;
1522 double y3
= y4
+ y2
;
1523 // Then we calculate max and min of the rotated rectangle.
1524 wxCoord maxX
= (wxCoord
)(dmax(dmax(0, x2
), dmax(x3
, x4
)) + 0.5),
1525 maxY
= (wxCoord
)(dmax(dmax(0, y2
), dmax(y3
, y4
)) + 0.5),
1526 minX
= (wxCoord
)(dmin(dmin(0, x2
), dmin(x3
, x4
)) - 0.5),
1527 minY
= (wxCoord
)(dmin(dmin(0, y2
), dmin(y3
, y4
)) - 0.5);
1529 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x
+minX
, y
+minY
,
1530 m_layout
, NULL
, bg_col
);
1532 if (m_font
.GetUnderlined() || m_font
.GetStrikethrough())
1533 pango_layout_set_attributes(m_layout
, NULL
);
1535 // clean up the transformation matrix
1536 pango_context_set_matrix(m_context
, NULL
);
1540 // reset unscaled size
1541 pango_font_description_set_size( m_fontdesc
, oldSize
);
1543 // actually apply unscaled font
1544 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1547 CalcBoundingBox(x
+minX
, y
+minY
);
1548 CalcBoundingBox(x
+maxX
, y
+maxY
);
1551 #endif //__WXGTK26__
1554 if ( wxIsNullDouble(angle
) )
1556 DoDrawText(text
, x
, y
);
1563 // TODO: implement later without GdkFont for GTK 2.0
1564 DoGetTextExtent(text
, &w
, &h
, NULL
,NULL
, &m_font
);
1566 // draw the string normally
1569 dc
.SelectObject(src
);
1570 dc
.SetFont(GetFont());
1571 dc
.SetBackground(*wxBLACK_BRUSH
);
1572 dc
.SetBrush(*wxBLACK_BRUSH
);
1574 dc
.SetTextForeground( *wxWHITE
);
1575 dc
.DrawText(text
, 0, 0);
1576 dc
.SelectObject(wxNullBitmap
);
1578 // Calculate the size of the rotated bounding box.
1579 double rad
= DegToRad(angle
);
1580 double dx
= cos(rad
),
1583 // the rectngle vertices are counted clockwise with the first one being at
1584 // (0, 0) (or, rather, at (x, y))
1586 y2
= -w
*dy
; // y axis points to the bottom, hence minus
1589 double x3
= x4
+ x2
,
1593 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5),
1594 maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5),
1595 minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5),
1596 minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1599 wxImage image
= src
.ConvertToImage();
1601 image
.ConvertColourToAlpha( m_textForegroundColour
.Red(),
1602 m_textForegroundColour
.Green(),
1603 m_textForegroundColour
.Blue() );
1604 image
= image
.Rotate( rad
, wxPoint(0,0) );
1606 int i_angle
= (int) angle
;
1607 i_angle
= i_angle
% 360;
1611 if ((i_angle
>= 90.0) && (i_angle
< 270.0))
1612 xoffset
= image
.GetWidth();
1614 if ((i_angle
>= 0.0) && (i_angle
< 180.0))
1615 yoffset
= image
.GetHeight();
1617 if ((i_angle
>= 0) && (i_angle
< 90))
1618 yoffset
-= (int)( cos(rad
)*h
);
1619 if ((i_angle
>= 90) && (i_angle
< 180))
1620 xoffset
-= (int)( sin(rad
)*h
);
1621 if ((i_angle
>= 180) && (i_angle
< 270))
1622 yoffset
-= (int)( cos(rad
)*h
);
1623 if ((i_angle
>= 270) && (i_angle
< 360))
1624 xoffset
-= (int)( sin(rad
)*h
);
1626 int i_x
= x
- xoffset
;
1627 int i_y
= y
- yoffset
;
1630 DoDrawBitmap( src
, i_x
, i_y
, true );
1633 // it would be better to draw with non underlined font and draw the line
1634 // manually here (it would be more straight...)
1636 if ( m_font
.GetUnderlined() )
1638 gdk_draw_line( m_gdkwindow
, m_textGC
,
1639 XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
),
1640 XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1644 // update the bounding box
1645 CalcBoundingBox(x
+ minX
, y
+ minY
);
1646 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1647 #else // !wxUSE_IMAGE
1652 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
1656 void wxWindowDCImpl::DoGetTextExtent(const wxString
&string
,
1657 wxCoord
*width
, wxCoord
*height
,
1658 wxCoord
*descent
, wxCoord
*externalLeading
,
1659 const wxFont
*theFont
) const
1667 if ( externalLeading
)
1668 *externalLeading
= 0;
1673 // ensure that theFont is always non-NULL
1674 if ( !theFont
|| !theFont
->IsOk() )
1677 // and use it if it's valid
1678 if ( theFont
->IsOk() )
1680 pango_layout_set_font_description
1683 theFont
->GetNativeFontInfo()->description
1687 // Set layout's text
1688 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *theFont
);
1691 // hardly ideal, but what else can we do if conversion failed?
1695 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1698 pango_layout_get_pixel_size(m_layout
, width
, &h
);
1701 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1702 int baseline
= pango_layout_iter_get_baseline(iter
);
1703 pango_layout_iter_free(iter
);
1704 *descent
= h
- PANGO_PIXELS(baseline
);
1709 // Reset old font description
1710 if (theFont
->IsOk())
1711 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1715 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString
& text
,
1716 wxArrayInt
& widths
) const
1718 const size_t len
= text
.length();
1725 // Set layout's text
1726 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, m_font
);
1729 // hardly ideal, but what else can we do if conversion failed?
1730 wxLogLastError(wxT("DoGetPartialTextExtents"));
1734 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1736 // Calculate the position of each character based on the widths of
1737 // the previous characters
1739 // Code borrowed from Scintilla's PlatGTK
1740 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1742 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1744 while (pango_layout_iter_next_cluster(iter
))
1746 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1747 int position
= PANGO_PIXELS(pos
.x
);
1748 widths
[i
++] = position
;
1751 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
1752 pango_layout_iter_free(iter
);
1758 wxCoord
wxWindowDCImpl::GetCharWidth() const
1760 pango_layout_set_text( m_layout
, "H", 1 );
1762 pango_layout_get_pixel_size( m_layout
, &w
, NULL
);
1766 wxCoord
wxWindowDCImpl::GetCharHeight() const
1768 PangoFontMetrics
*metrics
= pango_context_get_metrics (m_context
, m_fontdesc
, pango_context_get_language(m_context
));
1769 wxCHECK_MSG( metrics
, -1, wxT("failed to get pango font metrics") );
1771 wxCoord h
= PANGO_PIXELS (pango_font_metrics_get_descent (metrics
) +
1772 pango_font_metrics_get_ascent (metrics
));
1773 pango_font_metrics_unref (metrics
);
1777 void wxWindowDCImpl::Clear()
1779 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1781 if (!m_gdkwindow
) return;
1784 DoGetSize( &width
, &height
);
1785 gdk_draw_rectangle( m_gdkwindow
, m_bgGC
, TRUE
, 0, 0, width
, height
);
1788 void wxWindowDCImpl::SetFont( const wxFont
&font
)
1795 pango_font_description_free( m_fontdesc
);
1797 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1802 PangoContext
*oldContext
= m_context
;
1804 m_context
= m_window
->GTKGetPangoDefaultContext();
1806 // If we switch back/forth between different contexts
1807 // we also have to create a new layout. I think so,
1808 // at least, and it doesn't hurt to do it.
1809 if (oldContext
!= m_context
)
1812 g_object_unref (m_layout
);
1814 m_layout
= pango_layout_new( m_context
);
1818 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1822 void wxWindowDCImpl::SetPen( const wxPen
&pen
)
1824 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1826 if (m_pen
== pen
) return;
1830 if (!m_pen
.IsOk()) return;
1832 if (!m_gdkwindow
) return;
1834 gint width
= m_pen
.GetWidth();
1837 // CMB: if width is non-zero scale it with the dc
1842 // X doesn't allow different width in x and y and so we take
1845 ( fabs((double) XLOG2DEVREL(width
)) +
1846 fabs((double) YLOG2DEVREL(width
)) ) / 2.0;
1850 // width can't be 0 or an internal GTK error occurs inside
1851 // gdk_gc_set_dashes() below
1856 static const wxGTKDash dotted
[] = {1, 1};
1857 static const wxGTKDash short_dashed
[] = {2, 2};
1858 static const wxGTKDash wxCoord_dashed
[] = {2, 4};
1859 static const wxGTKDash dotted_dashed
[] = {3, 3, 1, 3};
1861 // We express dash pattern in pen width unit, so we are
1862 // independent of zoom factor and so on...
1864 const wxGTKDash
*req_dash
;
1866 GdkLineStyle lineStyle
= GDK_LINE_ON_OFF_DASH
;
1867 switch (m_pen
.GetStyle())
1869 case wxPENSTYLE_USER_DASH
:
1870 req_nb_dash
= m_pen
.GetDashCount();
1871 req_dash
= (wxGTKDash
*)m_pen
.GetDash();
1873 case wxPENSTYLE_DOT
:
1877 case wxPENSTYLE_LONG_DASH
:
1879 req_dash
= wxCoord_dashed
;
1881 case wxPENSTYLE_SHORT_DASH
:
1883 req_dash
= short_dashed
;
1885 case wxPENSTYLE_DOT_DASH
:
1887 req_dash
= dotted_dashed
;
1890 case wxPENSTYLE_TRANSPARENT
:
1891 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
1892 case wxPENSTYLE_STIPPLE
:
1893 case wxPENSTYLE_SOLID
:
1895 lineStyle
= GDK_LINE_SOLID
;
1901 if (req_dash
&& req_nb_dash
)
1903 wxGTKDash
*real_req_dash
= new wxGTKDash
[req_nb_dash
];
1906 for (int i
= 0; i
< req_nb_dash
; i
++)
1907 real_req_dash
[i
] = req_dash
[i
] * width
;
1908 gdk_gc_set_dashes( m_penGC
, 0, real_req_dash
, req_nb_dash
);
1909 delete[] real_req_dash
;
1913 // No Memory. We use non-scaled dash pattern...
1914 gdk_gc_set_dashes( m_penGC
, 0, (wxGTKDash
*)req_dash
, req_nb_dash
);
1918 GdkCapStyle capStyle
= GDK_CAP_ROUND
;
1919 switch (m_pen
.GetCap())
1921 case wxCAP_PROJECTING
: { capStyle
= GDK_CAP_PROJECTING
; break; }
1922 case wxCAP_BUTT
: { capStyle
= GDK_CAP_BUTT
; break; }
1928 capStyle
= GDK_CAP_NOT_LAST
;
1933 GdkJoinStyle joinStyle
= GDK_JOIN_ROUND
;
1934 switch (m_pen
.GetJoin())
1936 case wxJOIN_BEVEL
: { joinStyle
= GDK_JOIN_BEVEL
; break; }
1937 case wxJOIN_MITER
: { joinStyle
= GDK_JOIN_MITER
; break; }
1939 default: { joinStyle
= GDK_JOIN_ROUND
; break; }
1942 gdk_gc_set_line_attributes( m_penGC
, width
, lineStyle
, capStyle
, joinStyle
);
1944 m_pen
.GetColour().CalcPixel( m_cmap
);
1945 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
1948 void wxWindowDCImpl::SetBrush( const wxBrush
&brush
)
1950 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1952 if (m_brush
== brush
) return;
1956 if (!m_brush
.IsOk()) return;
1958 if (!m_gdkwindow
) return;
1960 m_brush
.GetColour().CalcPixel( m_cmap
);
1961 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
1963 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
1965 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
) && (m_brush
.GetStipple()->IsOk()))
1967 if (m_brush
.GetStipple()->GetDepth() != 1)
1969 gdk_gc_set_fill( m_brushGC
, GDK_TILED
);
1970 gdk_gc_set_tile( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1974 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1975 gdk_gc_set_stipple( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1979 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
) && (m_brush
.GetStipple()->GetMask()))
1981 gdk_gc_set_fill( m_textGC
, GDK_OPAQUE_STIPPLED
);
1982 gdk_gc_set_stipple( m_textGC
, m_brush
.GetStipple()->GetMask()->GetBitmap() );
1985 if (m_brush
.IsHatch())
1987 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1988 gdk_gc_set_stipple(m_brushGC
, GetHatch(m_brush
.GetStyle()));
1992 void wxWindowDCImpl::SetBackground( const wxBrush
&brush
)
1994 /* CMB 21/7/98: Added SetBackground. Sets background brush
1995 * for Clear() and bg colour for shapes filled with cross-hatch brush */
1997 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1999 if (m_backgroundBrush
== brush
) return;
2001 m_backgroundBrush
= brush
;
2003 if (!m_backgroundBrush
.IsOk()) return;
2005 if (!m_gdkwindow
) return;
2007 wxColor color
= m_backgroundBrush
.GetColour();
2008 color
.CalcPixel(m_cmap
);
2009 const GdkColor
* gdkColor
= color
.GetColor();
2010 gdk_gc_set_background(m_brushGC
, gdkColor
);
2011 gdk_gc_set_background(m_penGC
, gdkColor
);
2012 gdk_gc_set_background(m_bgGC
, gdkColor
);
2013 gdk_gc_set_foreground(m_bgGC
, gdkColor
);
2016 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
2018 if (m_backgroundBrush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
)
2020 const wxBitmap
* stipple
= m_backgroundBrush
.GetStipple();
2021 if (stipple
->IsOk())
2023 if (stipple
->GetDepth() != 1)
2025 gdk_gc_set_fill(m_bgGC
, GDK_TILED
);
2026 gdk_gc_set_tile(m_bgGC
, stipple
->GetPixmap());
2030 gdk_gc_set_fill(m_bgGC
, GDK_STIPPLED
);
2031 gdk_gc_set_stipple(m_bgGC
, stipple
->GetPixmap());
2035 else if (m_backgroundBrush
.IsHatch())
2037 gdk_gc_set_fill( m_bgGC
, GDK_STIPPLED
);
2038 gdk_gc_set_stipple(m_bgGC
, GetHatch(m_backgroundBrush
.GetStyle()));
2042 void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
2044 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2046 if (m_logicalFunction
== function
)
2049 // VZ: shouldn't this be a CHECK?
2056 case wxXOR
: mode
= GDK_XOR
; break;
2057 case wxINVERT
: mode
= GDK_INVERT
; break;
2058 case wxOR_REVERSE
: mode
= GDK_OR_REVERSE
; break;
2059 case wxAND_REVERSE
: mode
= GDK_AND_REVERSE
; break;
2060 case wxCLEAR
: mode
= GDK_CLEAR
; break;
2061 case wxSET
: mode
= GDK_SET
; break;
2062 case wxOR_INVERT
: mode
= GDK_OR_INVERT
; break;
2063 case wxAND
: mode
= GDK_AND
; break;
2064 case wxOR
: mode
= GDK_OR
; break;
2065 case wxEQUIV
: mode
= GDK_EQUIV
; break;
2066 case wxNAND
: mode
= GDK_NAND
; break;
2067 case wxAND_INVERT
: mode
= GDK_AND_INVERT
; break;
2068 case wxCOPY
: mode
= GDK_COPY
; break;
2069 case wxNO_OP
: mode
= GDK_NOOP
; break;
2070 case wxSRC_INVERT
: mode
= GDK_COPY_INVERT
; break;
2071 case wxNOR
: mode
= GDK_NOR
; break;
2073 wxFAIL_MSG("unknown mode");
2077 m_logicalFunction
= function
;
2079 gdk_gc_set_function( m_penGC
, mode
);
2080 gdk_gc_set_function( m_brushGC
, mode
);
2082 // to stay compatible with wxMSW, we don't apply ROPs to the text
2083 // operations (i.e. DrawText/DrawRotatedText).
2084 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2085 gdk_gc_set_function( m_textGC
, mode
);
2088 void wxWindowDCImpl::SetTextForeground( const wxColour
&col
)
2090 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2092 // don't set m_textForegroundColour to an invalid colour as we'd crash
2093 // later then (we use m_textForegroundColour.GetColor() without checking
2095 if ( !col
.IsOk() || (m_textForegroundColour
== col
) )
2098 m_textForegroundColour
= col
;
2102 m_textForegroundColour
.CalcPixel( m_cmap
);
2103 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
2107 void wxWindowDCImpl::SetTextBackground( const wxColour
&col
)
2109 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2112 if ( !col
.IsOk() || (m_textBackgroundColour
== col
) )
2115 m_textBackgroundColour
= col
;
2119 m_textBackgroundColour
.CalcPixel( m_cmap
);
2120 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
2124 void wxWindowDCImpl::SetBackgroundMode( int mode
)
2126 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2128 m_backgroundMode
= mode
;
2131 void wxWindowDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
2133 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
2136 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2138 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2140 if (!m_gdkwindow
) return;
2143 rect
.x
= XLOG2DEV(x
);
2144 rect
.y
= YLOG2DEV(y
);
2145 rect
.width
= XLOG2DEVREL(width
);
2146 rect
.height
= YLOG2DEVREL(height
);
2148 if (m_window
&& m_window
->m_wxwindow
&&
2149 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2151 rect
.x
-= rect
.width
;
2154 DoSetDeviceClippingRegion(wxRegion(rect
));
2157 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
2159 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2163 DestroyClippingRegion();
2167 if (!m_gdkwindow
) return;
2169 if (!m_currentClippingRegion
.IsNull())
2170 m_currentClippingRegion
.Intersect( region
);
2172 m_currentClippingRegion
.Union( region
);
2174 #if USE_PAINT_REGION
2175 if (!m_paintClippingRegion
.IsNull())
2176 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2179 wxCoord xx
, yy
, ww
, hh
;
2180 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2181 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2183 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2184 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2185 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2186 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2187 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2190 void wxWindowDCImpl::DestroyClippingRegion()
2192 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2194 wxDCImpl::DestroyClippingRegion();
2196 m_currentClippingRegion
.Clear();
2198 #if USE_PAINT_REGION
2199 if (!m_paintClippingRegion
.IsEmpty())
2200 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2203 if (!m_gdkwindow
) return;
2205 GdkRegion
* gdkRegion
= NULL
;
2206 if (!m_currentClippingRegion
.IsEmpty())
2207 gdkRegion
= m_currentClippingRegion
.GetRegion();
2209 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2210 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2211 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2212 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2215 void wxWindowDCImpl::Destroy()
2217 if (m_penGC
) wxFreePoolGC( m_penGC
);
2219 if (m_brushGC
) wxFreePoolGC( m_brushGC
);
2221 if (m_textGC
) wxFreePoolGC( m_textGC
);
2223 if (m_bgGC
) wxFreePoolGC( m_bgGC
);
2227 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
2229 m_deviceOriginX
= x
;
2230 m_deviceOriginY
= y
;
2232 ComputeScaleAndOrigin();
2235 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
2237 m_signX
= (xLeftRight
? 1 : -1);
2238 m_signY
= (yBottomUp
? -1 : 1);
2240 if (m_window
&& m_window
->m_wxwindow
&&
2241 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2244 ComputeScaleAndOrigin();
2247 void wxWindowDCImpl::ComputeScaleAndOrigin()
2249 const wxRealPoint
origScale(m_scaleX
, m_scaleY
);
2251 wxDCImpl::ComputeScaleAndOrigin();
2253 // if scale has changed call SetPen to recalulate the line width
2254 if ( wxRealPoint(m_scaleX
, m_scaleY
) != origScale
&& m_pen
.IsOk() )
2256 // this is a bit artificial, but we need to force wxDC to think the pen
2264 // Resolution in pixels per logical inch
2265 wxSize
wxWindowDCImpl::GetPPI() const
2267 return wxSize( (int) (m_mm_to_pix_x
* 25.4 + 0.5), (int) (m_mm_to_pix_y
* 25.4 + 0.5));
2270 int wxWindowDCImpl::GetDepth() const
2272 return gdk_drawable_get_depth(m_gdkwindow
);
2276 wxGTKPrivate::SetPangoAttrsForFont(const wxFont
& font
,
2277 PangoLayout
*layout
,
2281 if ( !font
.IsOk() || !(font
.GetUnderlined() || font
.GetStrikethrough()) )
2284 PangoAttrList
* attrs
= pango_attr_list_new();
2286 if ( font
.GetUnderlined() )
2288 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
2294 pango_attr_list_insert(attrs
, a
);
2296 // Add dummy attributes (use colour as it's invisible anyhow for 0
2297 // width spaces) to ensure that the spaces in the beginning/end of the
2298 // string are underlined too.
2299 if ( addDummyAttrs
)
2301 wxASSERT_MSG( len
> 2, "Must have 0-width spaces at string ends" );
2303 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
2306 pango_attr_list_insert(attrs
, a
);
2308 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
2309 a
->start_index
= len
- 1;
2311 pango_attr_list_insert(attrs
, a
);
2315 if ( font
.GetStrikethrough() )
2317 PangoAttribute
*a
= pango_attr_strikethrough_new( TRUE
);
2323 pango_attr_list_insert(attrs
, a
);
2326 pango_layout_set_attributes(layout
, attrs
);
2327 pango_attr_list_unref(attrs
);
2332 //-----------------------------------------------------------------------------
2334 //-----------------------------------------------------------------------------
2336 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
2338 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
2339 : wxWindowDCImpl( owner
)
2343 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*win
)
2344 : wxWindowDCImpl( owner
, win
)
2346 wxCHECK_RET( win
, wxT("NULL window in wxClientDCImpl::wxClientDC") );
2348 #ifdef __WXUNIVERSAL__
2349 wxPoint ptOrigin
= win
->GetClientAreaOrigin();
2350 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
2351 wxSize size
= win
->GetClientSize();
2352 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
2357 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
2359 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
2361 m_window
->GetClientSize( width
, height
);
2364 //-----------------------------------------------------------------------------
2366 //-----------------------------------------------------------------------------
2368 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
2370 // Limit the paint region to the window size. Sometimes
2371 // the paint region is too big, and this risks X11 errors
2372 static void wxLimitRegionToSize(wxRegion
& region
, const wxSize
& sz
)
2374 wxRect originalRect
= region
.GetBox();
2375 wxRect
rect(originalRect
);
2376 if (rect
.width
+ rect
.x
> sz
.x
)
2377 rect
.width
= sz
.x
- rect
.x
;
2378 if (rect
.height
+ rect
.y
> sz
.y
)
2379 rect
.height
= sz
.y
- rect
.y
;
2380 if (rect
!= originalRect
)
2382 region
= wxRegion(rect
);
2383 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2384 originalRect
.x
, originalRect
.y
, originalRect
.width
, originalRect
.height
,
2385 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2389 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
2390 : wxClientDCImpl( owner
)
2394 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*win
)
2395 : wxClientDCImpl( owner
, win
)
2397 #if USE_PAINT_REGION
2398 if (!win
->m_clipPaintRegion
)
2401 wxSize sz
= win
->GetSize();
2402 m_paintClippingRegion
= win
->m_nativeUpdateRegion
;
2403 wxLimitRegionToSize(m_paintClippingRegion
, sz
);
2405 GdkRegion
*region
= m_paintClippingRegion
.GetRegion();
2408 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2409 wxLimitRegionToSize(m_currentClippingRegion
, sz
);
2411 if (sz
.x
<= 0 || sz
.y
<= 0)
2414 gdk_gc_set_clip_region( m_penGC
, region
);
2415 gdk_gc_set_clip_region( m_brushGC
, region
);
2416 gdk_gc_set_clip_region( m_textGC
, region
);
2417 gdk_gc_set_clip_region( m_bgGC
, region
);
2422 // ----------------------------------------------------------------------------
2424 // ----------------------------------------------------------------------------
2426 class wxDCModule
: public wxModule
2433 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2436 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2438 bool wxDCModule::OnInit()
2444 void wxDCModule::OnExit()
2448 for (int i
= wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
; i
--; )
2451 g_object_unref(hatches
[i
]);