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 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 #define XLOG2DEV(x) LogicalToDeviceX(x)
34 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
35 #define YLOG2DEV(y) LogicalToDeviceY(y)
36 #define YLOG2DEVREL(y) LogicalToDeviceYRel(y)
38 #define USE_PAINT_REGION 1
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
51 static GdkPixmap
* hatches
[wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
+ 1];
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 static const double RAD2DEG
= 180.0 / M_PI
;
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
64 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
66 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
68 static GdkPixmap
* GetHatch(int style
)
70 wxASSERT(style
>= wxBRUSHSTYLE_FIRST_HATCH
&& style
<= wxBRUSHSTYLE_LAST_HATCH
);
71 const int i
= style
- wxBRUSHSTYLE_FIRST_HATCH
;
72 if (hatches
[i
] == NULL
)
76 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
77 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, bdiag_bits
, bdiag_width
, bdiag_height
);
79 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
80 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, cdiag_bits
, cdiag_width
, cdiag_height
);
82 case wxBRUSHSTYLE_CROSS_HATCH
:
83 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, cross_bits
, cross_width
, cross_height
);
85 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
86 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, fdiag_bits
, fdiag_width
, fdiag_height
);
88 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
89 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, horiz_bits
, horiz_width
, horiz_height
);
91 case wxBRUSHSTYLE_VERTICAL_HATCH
:
92 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, verti_bits
, verti_width
, verti_height
);
99 //-----------------------------------------------------------------------------
100 // Implement Pool of Graphic contexts. Creating them takes too much time.
101 //-----------------------------------------------------------------------------
127 #define GC_POOL_ALLOC_SIZE 100
129 static int wxGCPoolSize
= 0;
131 static wxGC
*wxGCPool
= NULL
;
133 static void wxInitGCPool()
135 // This really could wait until the first call to
136 // wxGetPoolGC, but we will make the first allocation
137 // now when other initialization is being performed.
139 // Set initial pool size.
140 wxGCPoolSize
= GC_POOL_ALLOC_SIZE
;
142 // Allocate initial pool.
143 wxGCPool
= (wxGC
*)malloc(wxGCPoolSize
* sizeof(wxGC
));
144 if (wxGCPool
== NULL
)
146 // If we cannot malloc, then fail with error
147 // when debug is enabled. If debug is not enabled,
148 // the problem will eventually get caught
150 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
154 // Zero initial pool.
155 memset(wxGCPool
, 0, wxGCPoolSize
* sizeof(wxGC
));
158 static void wxCleanUpGCPool()
160 for (int i
= 0; i
< wxGCPoolSize
; i
++)
162 if (wxGCPool
[i
].m_gc
)
163 g_object_unref (wxGCPool
[i
].m_gc
);
171 static GdkGC
* wxGetPoolGC( GdkWindow
*window
, wxPoolGCType type
)
175 // Look for an available GC.
176 for (int i
= 0; i
< wxGCPoolSize
; i
++)
178 if (!wxGCPool
[i
].m_gc
)
180 wxGCPool
[i
].m_gc
= gdk_gc_new( window
);
181 gdk_gc_set_exposures( wxGCPool
[i
].m_gc
, FALSE
);
182 wxGCPool
[i
].m_type
= type
;
183 wxGCPool
[i
].m_used
= false;
185 if ((!wxGCPool
[i
].m_used
) && (wxGCPool
[i
].m_type
== type
))
187 wxGCPool
[i
].m_used
= true;
188 return wxGCPool
[i
].m_gc
;
192 // We did not find an available GC.
193 // We need to grow the GC pool.
194 pptr
= (wxGC
*)realloc(wxGCPool
,
195 (wxGCPoolSize
+ GC_POOL_ALLOC_SIZE
)*sizeof(wxGC
));
198 // Initialize newly allocated pool.
200 memset(&wxGCPool
[wxGCPoolSize
], 0,
201 GC_POOL_ALLOC_SIZE
*sizeof(wxGC
));
203 // Initialize entry we will return.
204 wxGCPool
[wxGCPoolSize
].m_gc
= gdk_gc_new( window
);
205 gdk_gc_set_exposures( wxGCPool
[wxGCPoolSize
].m_gc
, FALSE
);
206 wxGCPool
[wxGCPoolSize
].m_type
= type
;
207 wxGCPool
[wxGCPoolSize
].m_used
= true;
209 // Set new value of pool size.
210 wxGCPoolSize
+= GC_POOL_ALLOC_SIZE
;
212 // Return newly allocated entry.
213 return wxGCPool
[wxGCPoolSize
-GC_POOL_ALLOC_SIZE
].m_gc
;
216 // The realloc failed. Fall through to error.
217 wxFAIL_MSG( wxT("No GC available") );
222 static void wxFreePoolGC( GdkGC
*gc
)
224 for (int i
= 0; i
< wxGCPoolSize
; i
++)
226 if (wxGCPool
[i
].m_gc
== gc
)
228 wxGCPool
[i
].m_used
= false;
233 wxFAIL_MSG( wxT("Wrong GC") );
236 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
240 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxGTKDCImpl
)
242 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
251 m_isScreenDC
= false;
257 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
260 wxASSERT_MSG( window
, wxT("DC needs a window") );
268 m_isScreenDC
= false;
269 m_font
= window
->GetFont();
271 GtkWidget
*widget
= window
->m_wxwindow
;
272 m_gdkwindow
= window
->GTKGetDrawingWindow();
274 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
275 // code should still be able to create wxClientDCs for them
278 widget
= window
->m_widget
;
280 wxCHECK_RET(widget
, "DC needs a widget");
282 m_gdkwindow
= widget
->window
;
283 if (GTK_WIDGET_NO_WINDOW(widget
))
284 SetDeviceLocalOrigin(widget
->allocation
.x
, widget
->allocation
.y
);
287 m_context
= window
->GTKGetPangoDefaultContext();
288 m_layout
= pango_layout_new( m_context
);
289 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
291 // Window not realized ?
294 // Don't report problems as per MSW.
300 m_cmap
= gtk_widget_get_colormap(widget
);
304 /* this must be done after SetUpDC, bacause SetUpDC calls the
305 repective SetBrush, SetPen, SetBackground etc functions
306 to set up the DC. SetBackground call m_owner->SetBackground
307 and this might not be desired as the standard dc background
308 is white whereas a window might assume gray to be the
309 standard (as e.g. wxStatusBar) */
313 if (m_window
&& m_window
->m_wxwindow
&&
314 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
319 // origin in the upper right corner
320 m_deviceOriginX
= m_window
->GetClientSize().x
;
324 wxWindowDCImpl::~wxWindowDCImpl()
329 g_object_unref (m_layout
);
331 pango_font_description_free( m_fontdesc
);
334 void wxWindowDCImpl::SetUpDC( bool isMemDC
)
338 wxASSERT_MSG( !m_penGC
, wxT("GCs already created") );
342 if ((isMemDC
) && (GetSelectedBitmap().IsOk()))
344 if (GetSelectedBitmap().GetDepth() == 1)
346 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_MONO
);
347 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_MONO
);
348 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_MONO
);
349 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_MONO
);
358 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_SCREEN
);
359 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_SCREEN
);
360 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_SCREEN
);
361 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_SCREEN
);
365 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR
);
366 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR
);
367 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR
);
368 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR
);
372 /* background colour */
373 m_backgroundBrush
= *wxWHITE_BRUSH
;
374 m_backgroundBrush
.GetColour().CalcPixel( m_cmap
);
375 const GdkColor
*bg_col
= m_backgroundBrush
.GetColour().GetColor();
378 m_textForegroundColour
.CalcPixel( m_cmap
);
379 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
381 m_textBackgroundColour
.CalcPixel( m_cmap
);
382 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
384 gdk_gc_set_fill( m_textGC
, GDK_SOLID
);
386 gdk_gc_set_colormap( m_textGC
, m_cmap
);
389 m_pen
.GetColour().CalcPixel( m_cmap
);
390 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
391 gdk_gc_set_background( m_penGC
, bg_col
);
393 gdk_gc_set_line_attributes( m_penGC
, 0, GDK_LINE_SOLID
, GDK_CAP_NOT_LAST
, GDK_JOIN_ROUND
);
396 m_brush
.GetColour().CalcPixel( m_cmap
);
397 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
398 gdk_gc_set_background( m_brushGC
, bg_col
);
400 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
403 gdk_gc_set_background( m_bgGC
, bg_col
);
404 gdk_gc_set_foreground( m_bgGC
, bg_col
);
406 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
409 gdk_gc_set_function( m_textGC
, GDK_COPY
);
410 gdk_gc_set_function( m_brushGC
, GDK_COPY
);
411 gdk_gc_set_function( m_penGC
, GDK_COPY
);
414 gdk_gc_set_clip_rectangle( m_penGC
, NULL
);
415 gdk_gc_set_clip_rectangle( m_brushGC
, NULL
);
416 gdk_gc_set_clip_rectangle( m_textGC
, NULL
);
417 gdk_gc_set_clip_rectangle( m_bgGC
, NULL
);
420 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
422 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
424 m_window
->GetSize(width
, height
);
427 bool wxWindowDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
428 const wxColour
& col
, wxFloodFillStyle style
)
431 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
432 const wxColour
& col
, wxFloodFillStyle style
);
434 return wxDoFloodFill( GetOwner(), x
, y
, col
, style
);
445 bool wxWindowDCImpl::DoGetPixel( wxCoord x1
, wxCoord y1
, wxColour
*col
) const
447 GdkImage
* image
= NULL
;
450 const int x
= LogicalToDeviceX(x1
);
451 const int y
= LogicalToDeviceY(y1
);
453 gdk_drawable_get_size(m_gdkwindow
, &rect
.width
, &rect
.height
);
454 if (rect
.Contains(x
, y
))
455 image
= gdk_drawable_get_image(m_gdkwindow
, x
, y
, 1, 1);
462 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
463 const unsigned pixel
= gdk_image_get_pixel(image
, 0, 0);
464 if (colormap
== NULL
)
465 *col
= pixel
? m_textForegroundColour
: m_textBackgroundColour
;
469 gdk_colormap_query_color(colormap
, pixel
, &c
);
470 col
->Set(c
.red
>> 8, c
.green
>> 8, c
.blue
>> 8);
472 g_object_unref(image
);
476 void wxWindowDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
478 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
480 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
483 gdk_draw_line( m_gdkwindow
, m_penGC
, XLOG2DEV(x1
), YLOG2DEV(y1
), XLOG2DEV(x2
), YLOG2DEV(y2
) );
485 CalcBoundingBox(x1
, y1
);
486 CalcBoundingBox(x2
, y2
);
490 void wxWindowDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
492 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
494 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
498 GetOwner()->GetSize( &w
, &h
);
499 wxCoord xx
= XLOG2DEV(x
);
500 wxCoord yy
= YLOG2DEV(y
);
503 gdk_draw_line( m_gdkwindow
, m_penGC
, 0, yy
, XLOG2DEVREL(w
), yy
);
504 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, 0, xx
, YLOG2DEVREL(h
) );
509 void wxWindowDCImpl::DrawingSetup(GdkGC
*& gc
, bool& originChanged
)
512 GdkPixmap
* pixmap
= NULL
;
513 const int style
= m_brush
.GetStyle();
515 if (style
== wxBRUSHSTYLE_STIPPLE
|| style
== wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
)
517 const wxBitmap
* stipple
= m_brush
.GetStipple();
520 if (style
== wxBRUSHSTYLE_STIPPLE
)
521 pixmap
= stipple
->GetPixmap();
522 else if (stipple
->GetMask())
524 pixmap
= stipple
->GetPixmap();
529 else if (m_brush
.IsHatch())
531 pixmap
= GetHatch(style
);
539 gdk_drawable_get_size(pixmap
, &w
, &h
);
540 origin_x
= m_deviceOriginX
% w
;
541 origin_y
= m_deviceOriginY
% h
;
544 originChanged
= origin_x
|| origin_y
;
546 gdk_gc_set_ts_origin(gc
, origin_x
, origin_y
);
549 void wxWindowDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
,
550 wxCoord xc
, wxCoord yc
)
552 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
554 wxCoord xx1
= XLOG2DEV(x1
);
555 wxCoord yy1
= YLOG2DEV(y1
);
556 wxCoord xx2
= XLOG2DEV(x2
);
557 wxCoord yy2
= YLOG2DEV(y2
);
558 wxCoord xxc
= XLOG2DEV(xc
);
559 wxCoord yyc
= YLOG2DEV(yc
);
560 double dx
= xx1
- xxc
;
561 double dy
= yy1
- yyc
;
562 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
563 wxCoord r
= (wxCoord
)radius
;
564 double radius1
, radius2
;
566 if (xx1
== xx2
&& yy1
== yy2
)
571 else if ( wxIsNullDouble(radius
) )
578 radius1
= (xx1
- xxc
== 0) ?
579 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
580 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
581 radius2
= (xx2
- xxc
== 0) ?
582 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
583 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
585 wxCoord alpha1
= wxCoord(radius1
* 64.0);
586 wxCoord alpha2
= wxCoord((radius2
- radius1
) * 64.0);
587 while (alpha2
<= 0) alpha2
+= 360*64;
588 while (alpha1
> 360*64) alpha1
-= 360*64;
592 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
596 DrawingSetup(gc
, originChanged
);
598 gdk_draw_arc(m_gdkwindow
, gc
, true, xxc
-r
, yyc
-r
, 2*r
, 2*r
, alpha1
, alpha2
);
601 gdk_gc_set_ts_origin(gc
, 0, 0);
604 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
606 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xxc
-r
, yyc
-r
, 2*r
,2*r
, alpha1
, alpha2
);
608 if ((m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
) && (alpha2
- alpha1
!= 360*64))
610 gdk_draw_line( m_gdkwindow
, m_penGC
, xx1
, yy1
, xxc
, yyc
);
611 gdk_draw_line( m_gdkwindow
, m_penGC
, xxc
, yyc
, xx2
, yy2
);
616 CalcBoundingBox (x1
, y1
);
617 CalcBoundingBox (x2
, y2
);
620 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double sa
, double ea
)
622 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
624 wxCoord xx
= XLOG2DEV(x
);
625 wxCoord yy
= YLOG2DEV(y
);
626 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
627 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
629 // CMB: handle -ve width and/or height
630 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
631 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
635 wxCoord start
= wxCoord(sa
* 64.0);
636 wxCoord end
= wxCoord((ea
-sa
) * 64.0);
638 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
642 DrawingSetup(gc
, originChanged
);
644 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, start
, end
);
647 gdk_gc_set_ts_origin(gc
, 0, 0);
650 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
651 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, start
, end
);
654 CalcBoundingBox (x
, y
);
655 CalcBoundingBox (x
+ width
, y
+ height
);
658 void wxWindowDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
660 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
662 if ((m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
) && m_gdkwindow
)
663 gdk_draw_point( m_gdkwindow
, m_penGC
, XLOG2DEV(x
), YLOG2DEV(y
) );
665 CalcBoundingBox (x
, y
);
668 void wxWindowDCImpl::DoDrawLines( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
670 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
672 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
) return;
675 //Check, if scaling is necessary
677 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
679 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
680 GdkPoint
* gpts
= reinterpret_cast<GdkPoint
*>(points
);
683 gpts
= new GdkPoint
[n
];
685 for (int i
= 0; i
< n
; i
++)
689 gpts
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
690 gpts
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
692 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
696 gdk_draw_lines( m_gdkwindow
, m_penGC
, gpts
, n
);
702 void wxWindowDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
703 wxCoord xoffset
, wxCoord yoffset
,
704 wxPolygonFillMode
WXUNUSED(fillStyle
) )
706 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
710 //Check, if scaling is necessary
712 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
714 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
715 GdkPoint
* gdkpoints
= reinterpret_cast<GdkPoint
*>(points
);
718 gdkpoints
= new GdkPoint
[n
];
721 for (i
= 0 ; i
< n
; i
++)
725 gdkpoints
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
726 gdkpoints
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
728 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
733 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
737 DrawingSetup(gc
, originChanged
);
739 gdk_draw_polygon(m_gdkwindow
, gc
, true, gdkpoints
, n
);
742 gdk_gc_set_ts_origin(gc
, 0, 0);
745 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
748 for (i = 0 ; i < n ; i++)
750 gdk_draw_line( m_gdkwindow, m_penGC,
753 gdkpoints[(i+1)%n].x,
754 gdkpoints[(i+1)%n].y);
757 gdk_draw_polygon( m_gdkwindow
, m_penGC
, FALSE
, gdkpoints
, n
);
766 void wxWindowDCImpl::DoDrawRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
768 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
770 wxCoord xx
= XLOG2DEV(x
);
771 wxCoord yy
= YLOG2DEV(y
);
772 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
773 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
775 // CMB: draw nothing if transformed w or h is 0
776 if (ww
== 0 || hh
== 0) return;
778 // CMB: handle -ve width and/or height
779 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
780 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
784 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
788 DrawingSetup(gc
, originChanged
);
790 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
);
793 gdk_gc_set_ts_origin(gc
, 0, 0);
796 if ( m_pen
.IsOk() && m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
798 if ((m_pen
.GetWidth() == 2) && (m_pen
.GetCap() == wxCAP_ROUND
) &&
799 (m_pen
.GetJoin() == wxJOIN_ROUND
) && (m_pen
.GetStyle() == wxPENSTYLE_SOLID
))
801 // Use 2 1-line rects instead
802 gdk_gc_set_line_attributes( m_penGC
, 1, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
807 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
+1, yy
, ww
-2, hh
-2 );
808 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
-1, ww
, hh
);
812 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-2, hh
-2 );
813 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
-1, yy
-1, ww
, hh
);
817 gdk_gc_set_line_attributes( m_penGC
, 2, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
821 // Just use X11 for other cases
822 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-1, hh
-1 );
827 CalcBoundingBox( x
, y
);
828 CalcBoundingBox( x
+ width
, y
+ height
);
831 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
833 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
835 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
837 wxCoord xx
= XLOG2DEV(x
);
838 wxCoord yy
= YLOG2DEV(y
);
839 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
840 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
841 wxCoord rr
= XLOG2DEVREL((wxCoord
)radius
);
843 // CMB: handle -ve width and/or height
844 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
845 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
847 // CMB: if radius is zero use DrawRectangle() instead to avoid
848 // X drawing errors with small radii
851 DoDrawRectangle( x
, y
, width
, height
);
855 // CMB: draw nothing if transformed w or h is 0
856 if (ww
== 0 || hh
== 0) return;
858 // CMB: adjust size if outline is drawn otherwise the result is
859 // 1 pixel too wide and high
860 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
868 // CMB: ensure dd is not larger than rectangle otherwise we
869 // get an hour glass shape
871 if (dd
> ww
) dd
= ww
;
872 if (dd
> hh
) dd
= hh
;
875 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
879 DrawingSetup(gc
, originChanged
);
881 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
+rr
, yy
, ww
-dd
+1, hh
);
882 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
+rr
, ww
, hh
-dd
+1);
883 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, dd
, dd
, 90*64, 90*64);
884 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64);
885 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64);
886 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64);
889 gdk_gc_set_ts_origin(gc
, 0, 0);
892 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
894 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
, xx
+ww
-rr
, yy
);
895 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
+hh
, xx
+ww
-rr
, yy
+hh
);
896 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, yy
+rr
+1, xx
, yy
+hh
-rr
);
897 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+ww
, yy
+rr
+1, xx
+ww
, yy
+hh
-rr
);
898 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, dd
, dd
, 90*64, 90*64 );
899 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64 );
900 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64 );
901 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64 );
905 // this ignores the radius
906 CalcBoundingBox( x
, y
);
907 CalcBoundingBox( x
+ width
, y
+ height
);
910 void wxWindowDCImpl::DoDrawEllipse( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
912 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
914 wxCoord xx
= XLOG2DEV(x
);
915 wxCoord yy
= YLOG2DEV(y
);
916 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
917 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
919 // CMB: handle -ve width and/or height
920 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
921 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
925 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
929 DrawingSetup(gc
, originChanged
);
931 // If the pen is transparent pen we increase the size
932 // for better compatibility with other platforms.
933 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
939 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, 0, 360*64);
942 gdk_gc_set_ts_origin(gc
, 0, 0);
945 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
946 gdk_draw_arc( m_gdkwindow
, m_penGC
, false, xx
, yy
, ww
, hh
, 0, 360*64 );
949 CalcBoundingBox( x
, y
);
950 CalcBoundingBox( x
+ width
, y
+ height
);
953 void wxWindowDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
955 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
956 DoDrawBitmap( (const wxBitmap
&)icon
, x
, y
, true );
959 // scale a pixbuf, return new pixbuf, unref old one
961 Scale(GdkPixbuf
* pixbuf
, int dst_w
, int dst_h
, double sx
, double sy
)
963 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
964 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, dst_w
, dst_h
);
965 gdk_pixbuf_scale(pixbuf
, pixbuf_scaled
,
966 0, 0, dst_w
, dst_h
, 0, 0, sx
, sy
, GDK_INTERP_NEAREST
);
967 g_object_unref(pixbuf
);
968 return pixbuf_scaled
;
971 // scale part of a pixmap using pixbuf scaling, return pixbuf
973 Scale(GdkPixmap
* pixmap
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
975 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(
976 NULL
, pixmap
, NULL
, x
, y
, 0, 0, w
, h
);
977 return Scale(pixbuf
, dst_w
, dst_h
, sx
, sy
);
980 // scale part of a mask pixmap, return new mask, unref old one
982 ScaleMask(GdkPixmap
* mask
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
984 GdkPixbuf
* pixbuf
= Scale(mask
, x
, y
, w
, h
, dst_w
, dst_h
, sx
, sy
);
986 // convert black and white pixbuf back to a mono pixmap
987 const unsigned out_rowstride
= (dst_w
+ 7) / 8;
988 const size_t data_size
= out_rowstride
* size_t(dst_h
);
989 char* data
= new char[data_size
];
991 const guchar
* row
= gdk_pixbuf_get_pixels(pixbuf
);
992 const int rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
993 memset(data
, 0, data_size
);
994 for (int j
= 0; j
< dst_h
; j
++, row
+= rowstride
, out
+= out_rowstride
)
996 const guchar
* in
= row
;
997 for (int i
= 0; i
< dst_w
; i
++, in
+= 3)
999 out
[i
>> 3] |= 1 << (i
& 7);
1001 g_object_unref(pixbuf
);
1002 GdkPixmap
* pixmap
= gdk_bitmap_create_from_data(mask
, data
, dst_w
, dst_h
);
1004 g_object_unref(mask
);
1008 // Make a new mask from part of a mask and a clip region.
1009 // Return new mask, unref old one.
1011 ClipMask(GdkPixmap
* mask
, GdkRegion
* clipRegion
, int x
, int y
, int dst_x
, int dst_y
, int w
, int h
)
1013 GdkGCValues gcValues
;
1014 gcValues
.foreground
.pixel
= 0;
1015 GdkGC
* gc
= gdk_gc_new_with_values(mask
, &gcValues
, GDK_GC_FOREGROUND
);
1016 GdkPixmap
* pixmap
= gdk_pixmap_new(mask
, w
, h
, 1);
1017 // clear new mask, so clipped areas will be masked
1018 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1019 gdk_gc_set_clip_region(gc
, clipRegion
);
1020 gdk_gc_set_clip_origin(gc
, -dst_x
, -dst_y
);
1021 // draw old mask onto new one, with clip
1022 gdk_draw_drawable(pixmap
, gc
, mask
, x
, y
, 0, 0, w
, h
);
1024 g_object_unref(mask
);
1028 // make a color pixmap from part of a mono one, using text fg/bg colors
1030 wxWindowDCImpl::MonoToColor(GdkPixmap
* monoPixmap
, int x
, int y
, int w
, int h
) const
1032 GdkPixmap
* pixmap
= gdk_pixmap_new(m_gdkwindow
, w
, h
, -1);
1033 GdkGCValues gcValues
;
1034 gcValues
.foreground
.pixel
= m_textForegroundColour
.GetColor()->pixel
;
1035 gcValues
.background
.pixel
= m_textBackgroundColour
.GetColor()->pixel
;
1036 gcValues
.stipple
= monoPixmap
;
1037 gcValues
.fill
= GDK_OPAQUE_STIPPLED
;
1038 gcValues
.ts_x_origin
= -x
;
1039 gcValues
.ts_y_origin
= -y
;
1040 GdkGC
* gc
= gdk_gc_new_with_values(pixmap
, &gcValues
, GdkGCValuesMask(
1041 GDK_GC_FOREGROUND
| GDK_GC_BACKGROUND
| GDK_GC_STIPPLE
| GDK_GC_FILL
|
1042 GDK_GC_TS_X_ORIGIN
| GDK_GC_TS_Y_ORIGIN
));
1043 gdk_draw_rectangle(pixmap
, 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") );
1053 wxCHECK_RET( bitmap
.IsOk(), wxT("invalid bitmap") );
1055 if (!m_gdkwindow
) return;
1057 const int w
= bitmap
.GetWidth();
1058 const int h
= bitmap
.GetHeight();
1060 // notice that as the bitmap is not drawn upside down (or right to left)
1061 // even if the corresponding axis direction is inversed, we need to take it
1062 // into account when calculating its bounding box
1063 CalcBoundingBox(x
, y
);
1064 CalcBoundingBox(x
+ m_signX
*w
, y
+ m_signY
*h
);
1067 int xx
= LogicalToDeviceX(x
);
1068 const int yy
= LogicalToDeviceY(y
);
1069 const int ww
= LogicalToDeviceXRel(w
);
1070 const int hh
= LogicalToDeviceYRel(h
);
1072 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1075 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1076 // determine clip region overlap
1077 int overlap
= wxInRegion
;
1080 overlap
= m_currentClippingRegion
.Contains(xx
, yy
, ww
, hh
);
1081 if (overlap
== wxOutRegion
)
1085 const bool isScaled
= ww
!= w
|| hh
!= h
;
1086 const bool hasAlpha
= bitmap
.HasAlpha();
1087 GdkGC
* const use_gc
= m_penGC
;
1089 GdkPixmap
* mask
= NULL
;
1090 // mask does not work when drawing a pixbuf with alpha
1091 if (useMask
&& !hasAlpha
)
1093 wxMask
* m
= bitmap
.GetMask();
1095 mask
= m
->GetBitmap();
1101 mask
= ScaleMask(mask
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1102 if (overlap
== wxPartRegion
)
1104 // need a new mask that also masks the clipped area,
1105 // because gc can't have both a mask and a clip region
1106 mask
= ClipMask(mask
, clipRegion
, 0, 0, xx
, yy
, ww
, hh
);
1108 gdk_gc_set_clip_mask(use_gc
, mask
);
1109 gdk_gc_set_clip_origin(use_gc
, xx
, yy
);
1112 // determine whether to use pixmap or pixbuf
1113 GdkPixmap
* pixmap
= NULL
;
1114 GdkPixbuf
* pixbuf
= NULL
;
1115 if (bitmap
.HasPixmap())
1116 pixmap
= bitmap
.GetPixmap();
1117 if (pixmap
&& gdk_drawable_get_depth(pixmap
) == 1)
1119 // convert mono pixmap to color using text fg/bg colors
1120 pixmap
= MonoToColor(pixmap
, 0, 0, w
, h
);
1122 else if (hasAlpha
|| pixmap
== NULL
)
1125 pixbuf
= bitmap
.GetPixbuf();
1126 g_object_ref(pixbuf
);
1130 g_object_ref(pixmap
);
1136 pixbuf
= Scale(pixbuf
, ww
, hh
, m_scaleX
, m_scaleY
);
1138 pixbuf
= Scale(pixmap
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1143 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1144 0, 0, xx
, yy
, ww
, hh
, GDK_RGB_DITHER_NORMAL
, 0, 0);
1145 g_object_unref(pixbuf
);
1149 gdk_draw_drawable(m_gdkwindow
, use_gc
, pixmap
, 0, 0, xx
, yy
, ww
, hh
);
1153 g_object_unref(pixmap
);
1156 g_object_unref(mask
);
1157 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1161 bool wxWindowDCImpl::DoBlit( wxCoord xdest
, wxCoord ydest
,
1162 wxCoord width
, wxCoord height
,
1164 wxCoord xsrc
, wxCoord ysrc
,
1165 wxRasterOperationMode logical_func
,
1167 wxCoord xsrcMask
, wxCoord ysrcMask
)
1169 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1170 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1172 if (!m_gdkwindow
) return false;
1174 GdkDrawable
* srcDrawable
= NULL
;
1175 GdkPixmap
* mask
= NULL
;
1176 wxMemoryDC
* memDC
= wxDynamicCast(source
, wxMemoryDC
);
1179 const wxBitmap
& bitmap
= memDC
->GetSelectedBitmap();
1182 srcDrawable
= bitmap
.GetPixmap();
1185 wxMask
* m
= bitmap
.GetMask();
1187 mask
= m
->GetBitmap();
1192 wxDCImpl
* impl
= source
->GetImpl();
1193 wxWindowDCImpl
* gtk_impl
= wxDynamicCast(impl
, wxWindowDCImpl
);
1195 srcDrawable
= gtk_impl
->GetGDKWindow();
1196 if (srcDrawable
== NULL
)
1200 CalcBoundingBox(xdest
, ydest
);
1201 CalcBoundingBox(xdest
+ width
, ydest
+ height
);
1203 // source device coords
1204 int src_x
= source
->LogicalToDeviceX(xsrc
);
1205 int src_y
= source
->LogicalToDeviceY(ysrc
);
1206 int src_w
= source
->LogicalToDeviceXRel(width
);
1207 int src_h
= source
->LogicalToDeviceYRel(height
);
1209 // Clip source rect to source dc.
1210 // Only necessary when scaling, to avoid GDK errors when
1211 // converting to pixbuf, but no harm in always doing it.
1212 // If source rect changes, it also changes the dest rect.
1214 gdk_drawable_get_size(srcDrawable
, &clip
.width
, &clip
.height
);
1215 clip
.Intersect(wxRect(src_x
, src_y
, src_w
, src_h
));
1216 if (src_w
!= clip
.width
|| src_h
!= clip
.height
)
1218 if (clip
.width
== 0)
1222 src_h
= clip
.height
;
1223 width
= source
->DeviceToLogicalXRel(src_w
);
1224 height
= source
->DeviceToLogicalYRel(src_h
);
1225 if (src_x
!= clip
.x
|| src_y
!= clip
.y
)
1227 xdest
+= source
->DeviceToLogicalXRel(clip
.x
- src_x
);
1228 ydest
+= source
->DeviceToLogicalYRel(clip
.y
- src_y
);
1234 // destination device coords
1235 const int dst_x
= LogicalToDeviceX(xdest
);
1236 const int dst_y
= LogicalToDeviceY(ydest
);
1237 const int dst_w
= LogicalToDeviceXRel(width
);
1238 const int dst_h
= LogicalToDeviceYRel(height
);
1240 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1241 // determine dest clip region overlap
1242 int overlap
= wxInRegion
;
1245 overlap
= m_currentClippingRegion
.Contains(dst_x
, dst_y
, dst_w
, dst_h
);
1246 if (overlap
== wxOutRegion
)
1250 const bool isScaled
= src_w
!= dst_w
|| src_h
!= dst_h
;
1255 // get source to dest scale
1256 double usx
, usy
, lsx
, lsy
;
1257 source
->GetUserScale(&usx
, &usy
);
1258 source
->GetLogicalScale(&lsx
, &lsy
);
1259 scale_x
= m_scaleX
/ (usx
* lsx
);
1260 scale_y
= m_scaleY
/ (usy
* lsy
);
1263 GdkGC
* const use_gc
= m_penGC
;
1268 int srcMask_x
= src_x
;
1269 int srcMask_y
= src_y
;
1270 if (xsrcMask
!= -1 || ysrcMask
!= -1)
1272 srcMask_x
= source
->LogicalToDeviceX(xsrcMask
);
1273 srcMask_y
= source
->LogicalToDeviceY(ysrcMask
);
1277 mask
= ScaleMask(mask
, srcMask_x
, srcMask_y
,
1278 src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1282 if (overlap
== wxPartRegion
)
1284 // need a new mask that also masks the clipped area,
1285 // because gc can't have both a mask and a clip region
1286 mask
= ClipMask(mask
, clipRegion
,
1287 srcMask_x
, srcMask_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1291 gdk_gc_set_clip_mask(use_gc
, mask
);
1292 gdk_gc_set_clip_origin(use_gc
, dst_x
- srcMask_x
, dst_y
- srcMask_y
);
1295 GdkPixmap
* pixmap
= NULL
;
1296 if (gdk_drawable_get_depth(srcDrawable
) == 1)
1298 // Convert mono pixmap to color using text fg/bg colors.
1299 // Scaling/drawing is simpler if this is done first.
1300 pixmap
= MonoToColor(srcDrawable
, src_x
, src_y
, src_w
, src_h
);
1301 srcDrawable
= pixmap
;
1306 const wxRasterOperationMode logical_func_save
= m_logicalFunction
;
1307 SetLogicalFunction(logical_func
);
1309 gdk_gc_set_subwindow(use_gc
, GDK_INCLUDE_INFERIORS
);
1313 GdkPixbuf
* pixbuf
= Scale(srcDrawable
,
1314 src_x
, src_y
, src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1315 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1316 0, 0, dst_x
, dst_y
, dst_w
, dst_h
, GDK_RGB_DITHER_NONE
, 0, 0);
1317 g_object_unref(pixbuf
);
1321 gdk_draw_drawable(m_gdkwindow
, use_gc
, srcDrawable
,
1322 src_x
, src_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1325 SetLogicalFunction(logical_func_save
);
1327 gdk_gc_set_subwindow(use_gc
, GDK_CLIP_BY_CHILDREN
);
1330 g_object_unref(pixmap
);
1333 g_object_unref(mask
);
1334 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1339 void wxWindowDCImpl::DoDrawText(const wxString
& text
,
1343 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1345 if (!m_gdkwindow
) return;
1347 if (text
.empty()) return;
1349 wxCoord x
= XLOG2DEV(xLogical
),
1350 y
= YLOG2DEV(yLogical
);
1352 wxCHECK_RET( m_context
, wxT("no Pango context") );
1353 wxCHECK_RET( m_layout
, wxT("no Pango layout") );
1354 wxCHECK_RET( m_fontdesc
, wxT("no Pango font description") );
1356 gdk_pango_context_set_colormap( m_context
, m_cmap
); // not needed in gtk+ >= 2.6
1358 bool underlined
= m_font
.IsOk() && m_font
.GetUnderlined();
1360 wxCharBuffer data
= wxGTK_CONV(text
);
1363 size_t datalen
= strlen(data
);
1365 // in Pango >= 1.16 the "underline of leading/trailing spaces" bug
1366 // has been fixed and thus the hack implemented below should never be used
1367 static bool pangoOk
= !wx_pango_version_check(1, 16, 0);
1369 bool needshack
= underlined
&& !pangoOk
;
1373 // a PangoLayout which has leading/trailing spaces with underlined font
1374 // is not correctly drawn by this pango version: Pango won't underline the spaces.
1375 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
1376 // this behaviour. To workaround this problem, we use a special hack here
1377 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
1378 // empty space characters and give them a dummy colour attribute.
1379 // This will force Pango to underline the leading/trailing spaces, too.
1381 wxCharBuffer
data_tmp(datalen
+ 6);
1382 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1383 memcpy(data_tmp
.data(), "\342\200\214", 3);
1384 // copy the user string
1385 memcpy(data_tmp
.data() + 3, data
, datalen
);
1386 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1387 memcpy(data_tmp
.data() + 3 + datalen
, "\342\200\214", 3);
1393 pango_layout_set_text(m_layout
, data
, datalen
);
1397 PangoAttrList
*attrs
= pango_attr_list_new();
1398 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1400 a
->end_index
= datalen
;
1401 pango_attr_list_insert(attrs
, a
);
1405 // dummy colour for the leading space
1406 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1409 pango_attr_list_insert(attrs
, a
);
1411 // dummy colour for the trailing space
1412 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1413 a
->start_index
= datalen
- 1;
1414 a
->end_index
= datalen
;
1415 pango_attr_list_insert(attrs
, a
);
1418 pango_layout_set_attributes(m_layout
, attrs
);
1419 pango_attr_list_unref(attrs
);
1423 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1426 // If there is a user or actually any scale applied to
1427 // the device context, scale the font.
1429 // scale font description
1430 oldSize
= pango_font_description_get_size(m_fontdesc
);
1431 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1433 // actually apply scaled font
1434 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1438 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1442 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1445 const GdkColor
* bg_col
= NULL
;
1446 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1447 bg_col
= m_textBackgroundColour
.GetColor();
1449 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x_rtl
, y
, m_layout
, NULL
, bg_col
);
1453 // reset unscaled size
1454 pango_font_description_set_size( m_fontdesc
, oldSize
);
1456 // actually apply unscaled font
1457 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1461 // undo underline attributes setting:
1462 pango_layout_set_attributes(m_layout
, NULL
);
1465 CalcBoundingBox(xLogical
+ int(w
/ m_scaleX
), yLogical
+ int(h
/ m_scaleY
));
1466 CalcBoundingBox(xLogical
, yLogical
);
1469 // TODO: When GTK2.6 is required, merge DoDrawText and DoDrawRotatedText to
1470 // avoid code duplication
1471 void wxWindowDCImpl::DoDrawRotatedText( const wxString
&text
, wxCoord x
, wxCoord y
, double angle
)
1473 if (!m_gdkwindow
|| text
.empty())
1476 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1479 if (!gtk_check_version(2,6,0))
1484 pango_layout_set_text(m_layout
, wxGTK_CONV(text
), -1);
1486 if (m_font
.GetUnderlined())
1488 PangoAttrList
*attrs
= pango_attr_list_new();
1489 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1490 pango_attr_list_insert(attrs
, a
);
1491 pango_layout_set_attributes(m_layout
, attrs
);
1492 pango_attr_list_unref(attrs
);
1496 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1499 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1500 // If there is a user or actually any scale applied to
1501 // the device context, scale the font.
1503 // scale font description
1504 oldSize
= pango_font_description_get_size(m_fontdesc
);
1505 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1507 // actually apply scaled font
1508 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1512 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1514 const GdkColor
* bg_col
= NULL
;
1515 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1516 bg_col
= m_textBackgroundColour
.GetColor();
1519 PangoMatrix matrix
= PANGO_MATRIX_INIT
;
1520 pango_matrix_rotate (&matrix
, angle
);
1521 pango_context_set_matrix (m_context
, &matrix
);
1522 pango_layout_context_changed (m_layout
);
1524 // To be compatible with MSW, the rotation axis must be in the old
1526 // Calculate the vertices of the rotated rectangle containing the text,
1527 // relative to the old top-left vertex.
1528 // We could use the matrix for this, but it's simpler with trignonometry.
1529 double rad
= DegToRad(angle
);
1530 // the rectangle vertices are counted clockwise with the first one
1532 double x2
= w
* cos(rad
);
1533 double y2
= -w
* sin(rad
); // y axis points to the bottom, hence minus
1534 double x4
= h
* sin(rad
);
1535 double y4
= h
* cos(rad
);
1536 double x3
= x4
+ x2
;
1537 double y3
= y4
+ y2
;
1538 // Then we calculate max and min of the rotated rectangle.
1539 wxCoord maxX
= (wxCoord
)(dmax(dmax(0, x2
), dmax(x3
, x4
)) + 0.5),
1540 maxY
= (wxCoord
)(dmax(dmax(0, y2
), dmax(y3
, y4
)) + 0.5),
1541 minX
= (wxCoord
)(dmin(dmin(0, x2
), dmin(x3
, x4
)) - 0.5),
1542 minY
= (wxCoord
)(dmin(dmin(0, y2
), dmin(y3
, y4
)) - 0.5);
1544 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x
+minX
, y
+minY
,
1545 m_layout
, NULL
, bg_col
);
1547 if (m_font
.GetUnderlined())
1548 pango_layout_set_attributes(m_layout
, NULL
);
1550 // clean up the transformation matrix
1551 pango_context_set_matrix(m_context
, NULL
);
1555 // reset unscaled size
1556 pango_font_description_set_size( m_fontdesc
, oldSize
);
1558 // actually apply unscaled font
1559 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1562 CalcBoundingBox(x
+minX
, y
+minY
);
1563 CalcBoundingBox(x
+maxX
, y
+maxY
);
1566 #endif //__WXGTK26__
1569 if ( wxIsNullDouble(angle
) )
1571 DoDrawText(text
, x
, y
);
1578 // TODO: implement later without GdkFont for GTK 2.0
1579 DoGetTextExtent(text
, &w
, &h
, NULL
,NULL
, &m_font
);
1581 // draw the string normally
1584 dc
.SelectObject(src
);
1585 dc
.SetFont(GetFont());
1586 dc
.SetBackground(*wxBLACK_BRUSH
);
1587 dc
.SetBrush(*wxBLACK_BRUSH
);
1589 dc
.SetTextForeground( *wxWHITE
);
1590 dc
.DrawText(text
, 0, 0);
1591 dc
.SelectObject(wxNullBitmap
);
1593 // Calculate the size of the rotated bounding box.
1594 double rad
= DegToRad(angle
);
1595 double dx
= cos(rad
),
1598 // the rectngle vertices are counted clockwise with the first one being at
1599 // (0, 0) (or, rather, at (x, y))
1601 y2
= -w
*dy
; // y axis points to the bottom, hence minus
1604 double x3
= x4
+ x2
,
1608 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5),
1609 maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5),
1610 minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5),
1611 minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1614 wxImage image
= src
.ConvertToImage();
1616 image
.ConvertColourToAlpha( m_textForegroundColour
.Red(),
1617 m_textForegroundColour
.Green(),
1618 m_textForegroundColour
.Blue() );
1619 image
= image
.Rotate( rad
, wxPoint(0,0) );
1621 int i_angle
= (int) angle
;
1622 i_angle
= i_angle
% 360;
1626 if ((i_angle
>= 90.0) && (i_angle
< 270.0))
1627 xoffset
= image
.GetWidth();
1629 if ((i_angle
>= 0.0) && (i_angle
< 180.0))
1630 yoffset
= image
.GetHeight();
1632 if ((i_angle
>= 0) && (i_angle
< 90))
1633 yoffset
-= (int)( cos(rad
)*h
);
1634 if ((i_angle
>= 90) && (i_angle
< 180))
1635 xoffset
-= (int)( sin(rad
)*h
);
1636 if ((i_angle
>= 180) && (i_angle
< 270))
1637 yoffset
-= (int)( cos(rad
)*h
);
1638 if ((i_angle
>= 270) && (i_angle
< 360))
1639 xoffset
-= (int)( sin(rad
)*h
);
1641 int i_x
= x
- xoffset
;
1642 int i_y
= y
- yoffset
;
1645 DoDrawBitmap( src
, i_x
, i_y
, true );
1648 // it would be better to draw with non underlined font and draw the line
1649 // manually here (it would be more straight...)
1651 if ( m_font
.GetUnderlined() )
1653 gdk_draw_line( m_gdkwindow
, m_textGC
,
1654 XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
),
1655 XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1659 // update the bounding box
1660 CalcBoundingBox(x
+ minX
, y
+ minY
);
1661 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1662 #else // !wxUSE_IMAGE
1667 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
1671 void wxWindowDCImpl::DoGetTextExtent(const wxString
&string
,
1672 wxCoord
*width
, wxCoord
*height
,
1673 wxCoord
*descent
, wxCoord
*externalLeading
,
1674 const wxFont
*theFont
) const
1682 if ( externalLeading
)
1683 *externalLeading
= 0;
1688 // ensure that theFont is always non-NULL
1689 if ( !theFont
|| !theFont
->IsOk() )
1692 // and use it if it's valid
1693 if ( theFont
->IsOk() )
1695 pango_layout_set_font_description
1698 theFont
->GetNativeFontInfo()->description
1702 // Set layout's text
1703 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *theFont
);
1706 // hardly ideal, but what else can we do if conversion failed?
1710 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1713 pango_layout_get_pixel_size(m_layout
, width
, &h
);
1716 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1717 int baseline
= pango_layout_iter_get_baseline(iter
);
1718 pango_layout_iter_free(iter
);
1719 *descent
= h
- PANGO_PIXELS(baseline
);
1724 // Reset old font description
1725 if (theFont
->IsOk())
1726 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1730 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString
& text
,
1731 wxArrayInt
& widths
) const
1733 const size_t len
= text
.length();
1740 // Set layout's text
1741 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, m_font
);
1744 // hardly ideal, but what else can we do if conversion failed?
1745 wxLogLastError(wxT("DoGetPartialTextExtents"));
1749 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1751 // Calculate the position of each character based on the widths of
1752 // the previous characters
1754 // Code borrowed from Scintilla's PlatGTK
1755 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1757 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1759 while (pango_layout_iter_next_cluster(iter
))
1761 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1762 int position
= PANGO_PIXELS(pos
.x
);
1763 widths
[i
++] = position
;
1766 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
1767 pango_layout_iter_free(iter
);
1773 wxCoord
wxWindowDCImpl::GetCharWidth() const
1775 pango_layout_set_text( m_layout
, "H", 1 );
1777 pango_layout_get_pixel_size( m_layout
, &w
, NULL
);
1781 wxCoord
wxWindowDCImpl::GetCharHeight() const
1783 PangoFontMetrics
*metrics
= pango_context_get_metrics (m_context
, m_fontdesc
, pango_context_get_language(m_context
));
1784 wxCHECK_MSG( metrics
, -1, wxT("failed to get pango font metrics") );
1786 wxCoord h
= PANGO_PIXELS (pango_font_metrics_get_descent (metrics
) +
1787 pango_font_metrics_get_ascent (metrics
));
1788 pango_font_metrics_unref (metrics
);
1792 void wxWindowDCImpl::Clear()
1794 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1796 if (!m_gdkwindow
) return;
1799 DoGetSize( &width
, &height
);
1800 gdk_draw_rectangle( m_gdkwindow
, m_bgGC
, TRUE
, 0, 0, width
, height
);
1803 void wxWindowDCImpl::SetFont( const wxFont
&font
)
1810 pango_font_description_free( m_fontdesc
);
1812 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1817 PangoContext
*oldContext
= m_context
;
1819 m_context
= m_window
->GTKGetPangoDefaultContext();
1821 // If we switch back/forth between different contexts
1822 // we also have to create a new layout. I think so,
1823 // at least, and it doesn't hurt to do it.
1824 if (oldContext
!= m_context
)
1827 g_object_unref (m_layout
);
1829 m_layout
= pango_layout_new( m_context
);
1833 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1837 void wxWindowDCImpl::SetPen( const wxPen
&pen
)
1839 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1841 if (m_pen
== pen
) return;
1845 if (!m_pen
.IsOk()) return;
1847 if (!m_gdkwindow
) return;
1849 gint width
= m_pen
.GetWidth();
1852 // CMB: if width is non-zero scale it with the dc
1857 // X doesn't allow different width in x and y and so we take
1860 ( fabs((double) XLOG2DEVREL(width
)) +
1861 fabs((double) YLOG2DEVREL(width
)) ) / 2.0;
1865 // width can't be 0 or an internal GTK error occurs inside
1866 // gdk_gc_set_dashes() below
1871 static const wxGTKDash dotted
[] = {1, 1};
1872 static const wxGTKDash short_dashed
[] = {2, 2};
1873 static const wxGTKDash wxCoord_dashed
[] = {2, 4};
1874 static const wxGTKDash dotted_dashed
[] = {3, 3, 1, 3};
1876 // We express dash pattern in pen width unit, so we are
1877 // independent of zoom factor and so on...
1879 const wxGTKDash
*req_dash
;
1881 GdkLineStyle lineStyle
= GDK_LINE_ON_OFF_DASH
;
1882 switch (m_pen
.GetStyle())
1884 case wxPENSTYLE_USER_DASH
:
1885 req_nb_dash
= m_pen
.GetDashCount();
1886 req_dash
= (wxGTKDash
*)m_pen
.GetDash();
1888 case wxPENSTYLE_DOT
:
1892 case wxPENSTYLE_LONG_DASH
:
1894 req_dash
= wxCoord_dashed
;
1896 case wxPENSTYLE_SHORT_DASH
:
1898 req_dash
= short_dashed
;
1900 case wxPENSTYLE_DOT_DASH
:
1902 req_dash
= dotted_dashed
;
1905 case wxPENSTYLE_TRANSPARENT
:
1906 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
1907 case wxPENSTYLE_STIPPLE
:
1908 case wxPENSTYLE_SOLID
:
1910 lineStyle
= GDK_LINE_SOLID
;
1916 if (req_dash
&& req_nb_dash
)
1918 wxGTKDash
*real_req_dash
= new wxGTKDash
[req_nb_dash
];
1921 for (int i
= 0; i
< req_nb_dash
; i
++)
1922 real_req_dash
[i
] = req_dash
[i
] * width
;
1923 gdk_gc_set_dashes( m_penGC
, 0, real_req_dash
, req_nb_dash
);
1924 delete[] real_req_dash
;
1928 // No Memory. We use non-scaled dash pattern...
1929 gdk_gc_set_dashes( m_penGC
, 0, (wxGTKDash
*)req_dash
, req_nb_dash
);
1933 GdkCapStyle capStyle
= GDK_CAP_ROUND
;
1934 switch (m_pen
.GetCap())
1936 case wxCAP_PROJECTING
: { capStyle
= GDK_CAP_PROJECTING
; break; }
1937 case wxCAP_BUTT
: { capStyle
= GDK_CAP_BUTT
; break; }
1943 capStyle
= GDK_CAP_NOT_LAST
;
1948 GdkJoinStyle joinStyle
= GDK_JOIN_ROUND
;
1949 switch (m_pen
.GetJoin())
1951 case wxJOIN_BEVEL
: { joinStyle
= GDK_JOIN_BEVEL
; break; }
1952 case wxJOIN_MITER
: { joinStyle
= GDK_JOIN_MITER
; break; }
1954 default: { joinStyle
= GDK_JOIN_ROUND
; break; }
1957 gdk_gc_set_line_attributes( m_penGC
, width
, lineStyle
, capStyle
, joinStyle
);
1959 m_pen
.GetColour().CalcPixel( m_cmap
);
1960 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
1963 void wxWindowDCImpl::SetBrush( const wxBrush
&brush
)
1965 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1967 if (m_brush
== brush
) return;
1971 if (!m_brush
.IsOk()) return;
1973 if (!m_gdkwindow
) return;
1975 m_brush
.GetColour().CalcPixel( m_cmap
);
1976 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
1978 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
1980 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
) && (m_brush
.GetStipple()->IsOk()))
1982 if (m_brush
.GetStipple()->GetDepth() != 1)
1984 gdk_gc_set_fill( m_brushGC
, GDK_TILED
);
1985 gdk_gc_set_tile( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1989 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1990 gdk_gc_set_stipple( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1994 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
) && (m_brush
.GetStipple()->GetMask()))
1996 gdk_gc_set_fill( m_textGC
, GDK_OPAQUE_STIPPLED
);
1997 gdk_gc_set_stipple( m_textGC
, m_brush
.GetStipple()->GetMask()->GetBitmap() );
2000 if (m_brush
.IsHatch())
2002 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
2003 gdk_gc_set_stipple(m_brushGC
, GetHatch(m_brush
.GetStyle()));
2007 void wxWindowDCImpl::SetBackground( const wxBrush
&brush
)
2009 /* CMB 21/7/98: Added SetBackground. Sets background brush
2010 * for Clear() and bg colour for shapes filled with cross-hatch brush */
2012 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2014 if (m_backgroundBrush
== brush
) return;
2016 m_backgroundBrush
= brush
;
2018 if (!m_backgroundBrush
.IsOk()) return;
2020 if (!m_gdkwindow
) return;
2022 wxColor color
= m_backgroundBrush
.GetColour();
2023 color
.CalcPixel(m_cmap
);
2024 const GdkColor
* gdkColor
= color
.GetColor();
2025 gdk_gc_set_background(m_brushGC
, gdkColor
);
2026 gdk_gc_set_background(m_penGC
, gdkColor
);
2027 gdk_gc_set_background(m_bgGC
, gdkColor
);
2028 gdk_gc_set_foreground(m_bgGC
, gdkColor
);
2031 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
2033 if (m_backgroundBrush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
)
2035 const wxBitmap
* stipple
= m_backgroundBrush
.GetStipple();
2036 if (stipple
->IsOk())
2038 if (stipple
->GetDepth() != 1)
2040 gdk_gc_set_fill(m_bgGC
, GDK_TILED
);
2041 gdk_gc_set_tile(m_bgGC
, stipple
->GetPixmap());
2045 gdk_gc_set_fill(m_bgGC
, GDK_STIPPLED
);
2046 gdk_gc_set_stipple(m_bgGC
, stipple
->GetPixmap());
2050 else if (m_backgroundBrush
.IsHatch())
2052 gdk_gc_set_fill( m_bgGC
, GDK_STIPPLED
);
2053 gdk_gc_set_stipple(m_bgGC
, GetHatch(m_backgroundBrush
.GetStyle()));
2057 void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
2059 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2061 if (m_logicalFunction
== function
)
2064 // VZ: shouldn't this be a CHECK?
2071 case wxXOR
: mode
= GDK_XOR
; break;
2072 case wxINVERT
: mode
= GDK_INVERT
; break;
2073 case wxOR_REVERSE
: mode
= GDK_OR_REVERSE
; break;
2074 case wxAND_REVERSE
: mode
= GDK_AND_REVERSE
; break;
2075 case wxCLEAR
: mode
= GDK_CLEAR
; break;
2076 case wxSET
: mode
= GDK_SET
; break;
2077 case wxOR_INVERT
: mode
= GDK_OR_INVERT
; break;
2078 case wxAND
: mode
= GDK_AND
; break;
2079 case wxOR
: mode
= GDK_OR
; break;
2080 case wxEQUIV
: mode
= GDK_EQUIV
; break;
2081 case wxNAND
: mode
= GDK_NAND
; break;
2082 case wxAND_INVERT
: mode
= GDK_AND_INVERT
; break;
2083 case wxCOPY
: mode
= GDK_COPY
; break;
2084 case wxNO_OP
: mode
= GDK_NOOP
; break;
2085 case wxSRC_INVERT
: mode
= GDK_COPY_INVERT
; break;
2086 case wxNOR
: mode
= GDK_NOR
; break;
2088 wxFAIL_MSG("unknown mode");
2092 m_logicalFunction
= function
;
2094 gdk_gc_set_function( m_penGC
, mode
);
2095 gdk_gc_set_function( m_brushGC
, mode
);
2097 // to stay compatible with wxMSW, we don't apply ROPs to the text
2098 // operations (i.e. DrawText/DrawRotatedText).
2099 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2100 gdk_gc_set_function( m_textGC
, mode
);
2103 void wxWindowDCImpl::SetTextForeground( const wxColour
&col
)
2105 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2107 // don't set m_textForegroundColour to an invalid colour as we'd crash
2108 // later then (we use m_textForegroundColour.GetColor() without checking
2110 if ( !col
.IsOk() || (m_textForegroundColour
== col
) )
2113 m_textForegroundColour
= col
;
2117 m_textForegroundColour
.CalcPixel( m_cmap
);
2118 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
2122 void wxWindowDCImpl::SetTextBackground( const wxColour
&col
)
2124 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2127 if ( !col
.IsOk() || (m_textBackgroundColour
== col
) )
2130 m_textBackgroundColour
= col
;
2134 m_textBackgroundColour
.CalcPixel( m_cmap
);
2135 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
2139 void wxWindowDCImpl::SetBackgroundMode( int mode
)
2141 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2143 m_backgroundMode
= mode
;
2146 void wxWindowDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
2148 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
2151 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2153 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2155 if (!m_gdkwindow
) return;
2158 rect
.x
= XLOG2DEV(x
);
2159 rect
.y
= YLOG2DEV(y
);
2160 rect
.width
= XLOG2DEVREL(width
);
2161 rect
.height
= YLOG2DEVREL(height
);
2163 if (m_window
&& m_window
->m_wxwindow
&&
2164 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2166 rect
.x
-= rect
.width
;
2169 DoSetDeviceClippingRegion(wxRegion(rect
));
2172 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
2174 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2178 DestroyClippingRegion();
2182 if (!m_gdkwindow
) return;
2184 if (!m_currentClippingRegion
.IsNull())
2185 m_currentClippingRegion
.Intersect( region
);
2187 m_currentClippingRegion
.Union( region
);
2189 #if USE_PAINT_REGION
2190 if (!m_paintClippingRegion
.IsNull())
2191 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2194 wxCoord xx
, yy
, ww
, hh
;
2195 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2196 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2198 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2199 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2200 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2201 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2202 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2205 void wxWindowDCImpl::DestroyClippingRegion()
2207 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2209 wxDCImpl::DestroyClippingRegion();
2211 m_currentClippingRegion
.Clear();
2213 #if USE_PAINT_REGION
2214 if (!m_paintClippingRegion
.IsEmpty())
2215 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2218 if (!m_gdkwindow
) return;
2220 GdkRegion
* gdkRegion
= NULL
;
2221 if (!m_currentClippingRegion
.IsEmpty())
2222 gdkRegion
= m_currentClippingRegion
.GetRegion();
2224 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2225 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2226 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2227 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2230 void wxWindowDCImpl::Destroy()
2232 if (m_penGC
) wxFreePoolGC( m_penGC
);
2234 if (m_brushGC
) wxFreePoolGC( m_brushGC
);
2236 if (m_textGC
) wxFreePoolGC( m_textGC
);
2238 if (m_bgGC
) wxFreePoolGC( m_bgGC
);
2242 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
2244 m_deviceOriginX
= x
;
2245 m_deviceOriginY
= y
;
2247 ComputeScaleAndOrigin();
2250 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
2252 m_signX
= (xLeftRight
? 1 : -1);
2253 m_signY
= (yBottomUp
? -1 : 1);
2255 if (m_window
&& m_window
->m_wxwindow
&&
2256 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2259 ComputeScaleAndOrigin();
2262 void wxWindowDCImpl::ComputeScaleAndOrigin()
2264 const wxRealPoint
origScale(m_scaleX
, m_scaleY
);
2266 wxDCImpl::ComputeScaleAndOrigin();
2268 // if scale has changed call SetPen to recalulate the line width
2269 if ( wxRealPoint(m_scaleX
, m_scaleY
) != origScale
&& m_pen
.IsOk() )
2271 // this is a bit artificial, but we need to force wxDC to think the pen
2279 // Resolution in pixels per logical inch
2280 wxSize
wxWindowDCImpl::GetPPI() const
2282 return wxSize( (int) (m_mm_to_pix_x
* 25.4 + 0.5), (int) (m_mm_to_pix_y
* 25.4 + 0.5));
2285 int wxWindowDCImpl::GetDepth() const
2287 return gdk_drawable_get_depth(m_gdkwindow
);
2291 //-----------------------------------------------------------------------------
2293 //-----------------------------------------------------------------------------
2295 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
2297 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
2298 : wxWindowDCImpl( owner
)
2302 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*win
)
2303 : wxWindowDCImpl( owner
, win
)
2305 wxCHECK_RET( win
, wxT("NULL window in wxClientDCImpl::wxClientDC") );
2307 #ifdef __WXUNIVERSAL__
2308 wxPoint ptOrigin
= win
->GetClientAreaOrigin();
2309 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
2310 wxSize size
= win
->GetClientSize();
2311 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
2316 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
2318 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
2320 m_window
->GetClientSize( width
, height
);
2323 //-----------------------------------------------------------------------------
2325 //-----------------------------------------------------------------------------
2327 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
2329 // Limit the paint region to the window size. Sometimes
2330 // the paint region is too big, and this risks X11 errors
2331 static void wxLimitRegionToSize(wxRegion
& region
, const wxSize
& sz
)
2333 wxRect originalRect
= region
.GetBox();
2334 wxRect
rect(originalRect
);
2335 if (rect
.width
+ rect
.x
> sz
.x
)
2336 rect
.width
= sz
.x
- rect
.x
;
2337 if (rect
.height
+ rect
.y
> sz
.y
)
2338 rect
.height
= sz
.y
- rect
.y
;
2339 if (rect
!= originalRect
)
2341 region
= wxRegion(rect
);
2342 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2343 originalRect
.x
, originalRect
.y
, originalRect
.width
, originalRect
.height
,
2344 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2348 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
2349 : wxClientDCImpl( owner
)
2353 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*win
)
2354 : wxClientDCImpl( owner
, win
)
2356 #if USE_PAINT_REGION
2357 if (!win
->m_clipPaintRegion
)
2360 wxSize sz
= win
->GetSize();
2361 m_paintClippingRegion
= win
->m_nativeUpdateRegion
;
2362 wxLimitRegionToSize(m_paintClippingRegion
, sz
);
2364 GdkRegion
*region
= m_paintClippingRegion
.GetRegion();
2367 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2368 wxLimitRegionToSize(m_currentClippingRegion
, sz
);
2370 if (sz
.x
<= 0 || sz
.y
<= 0)
2373 gdk_gc_set_clip_region( m_penGC
, region
);
2374 gdk_gc_set_clip_region( m_brushGC
, region
);
2375 gdk_gc_set_clip_region( m_textGC
, region
);
2376 gdk_gc_set_clip_region( m_bgGC
, region
);
2381 // ----------------------------------------------------------------------------
2383 // ----------------------------------------------------------------------------
2385 class wxDCModule
: public wxModule
2392 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2395 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2397 bool wxDCModule::OnInit()
2403 void wxDCModule::OnExit()
2407 for (int i
= wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
; i
--; )
2410 g_object_unref(hatches
[i
]);