1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dcclient.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Chris Breeze
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
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") );
219 return (GdkGC
*) NULL
;
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
) :
245 m_gdkwindow
= (GdkWindow
*) NULL
;
246 m_penGC
= (GdkGC
*) NULL
;
247 m_brushGC
= (GdkGC
*) NULL
;
248 m_textGC
= (GdkGC
*) NULL
;
249 m_bgGC
= (GdkGC
*) NULL
;
250 m_cmap
= (GdkColormap
*) NULL
;
251 m_isScreenDC
= false;
252 m_context
= (PangoContext
*)NULL
;
253 m_layout
= (PangoLayout
*)NULL
;
254 m_fontdesc
= (PangoFontDescription
*)NULL
;
257 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
260 wxASSERT_MSG( window
, wxT("DC needs a window") );
262 m_gdkwindow
= (GdkWindow
*) NULL
;
263 m_penGC
= (GdkGC
*) NULL
;
264 m_brushGC
= (GdkGC
*) NULL
;
265 m_textGC
= (GdkGC
*) NULL
;
266 m_bgGC
= (GdkGC
*) NULL
;
267 m_cmap
= (GdkColormap
*) NULL
;
268 m_isScreenDC
= false;
269 m_font
= window
->GetFont();
271 GtkWidget
*widget
= window
->m_wxwindow
;
273 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
274 // code should still be able to create wxClientDCs for them, so we will
275 // use the parent window here then.
278 window
= window
->GetParent();
279 widget
= window
->m_wxwindow
;
282 wxASSERT_MSG( widget
, wxT("DC needs a widget") );
284 m_context
= window
->GtkGetPangoDefaultContext();
285 m_layout
= pango_layout_new( m_context
);
286 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
288 m_gdkwindow
= widget
->window
;
290 // Window not realized ?
293 // Don't report problems as per MSW.
299 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
303 /* this must be done after SetUpDC, bacause SetUpDC calls the
304 repective SetBrush, SetPen, SetBackground etc functions
305 to set up the DC. SetBackground call m_owner->SetBackground
306 and this might not be desired as the standard dc background
307 is white whereas a window might assume gray to be the
308 standard (as e.g. wxStatusBar) */
312 if (m_window
&& m_window
->m_wxwindow
&&
313 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
318 // origin in the upper right corner
319 m_deviceOriginX
= m_window
->GetClientSize().x
;
323 wxWindowDCImpl::~wxWindowDCImpl()
328 g_object_unref (m_layout
);
330 pango_font_description_free( m_fontdesc
);
333 void wxWindowDCImpl::SetUpDC( bool isMemDC
)
337 wxASSERT_MSG( !m_penGC
, wxT("GCs already created") );
341 if ((isMemDC
) && (GetSelectedBitmap().IsOk()))
343 if (GetSelectedBitmap().GetDepth() == 1)
345 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_MONO
);
346 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_MONO
);
347 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_MONO
);
348 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_MONO
);
357 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_SCREEN
);
358 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_SCREEN
);
359 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_SCREEN
);
360 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_SCREEN
);
364 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR
);
365 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR
);
366 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR
);
367 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR
);
371 /* background colour */
372 m_backgroundBrush
= *wxWHITE_BRUSH
;
373 m_backgroundBrush
.GetColour().CalcPixel( m_cmap
);
374 const GdkColor
*bg_col
= m_backgroundBrush
.GetColour().GetColor();
377 m_textForegroundColour
.CalcPixel( m_cmap
);
378 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
380 m_textBackgroundColour
.CalcPixel( m_cmap
);
381 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
383 gdk_gc_set_fill( m_textGC
, GDK_SOLID
);
385 gdk_gc_set_colormap( m_textGC
, m_cmap
);
388 m_pen
.GetColour().CalcPixel( m_cmap
);
389 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
390 gdk_gc_set_background( m_penGC
, bg_col
);
392 gdk_gc_set_line_attributes( m_penGC
, 0, GDK_LINE_SOLID
, GDK_CAP_NOT_LAST
, GDK_JOIN_ROUND
);
395 m_brush
.GetColour().CalcPixel( m_cmap
);
396 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
397 gdk_gc_set_background( m_brushGC
, bg_col
);
399 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
402 gdk_gc_set_background( m_bgGC
, bg_col
);
403 gdk_gc_set_foreground( m_bgGC
, bg_col
);
405 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
408 gdk_gc_set_function( m_textGC
, GDK_COPY
);
409 gdk_gc_set_function( m_brushGC
, GDK_COPY
);
410 gdk_gc_set_function( m_penGC
, GDK_COPY
);
413 gdk_gc_set_clip_rectangle( m_penGC
, (GdkRectangle
*) NULL
);
414 gdk_gc_set_clip_rectangle( m_brushGC
, (GdkRectangle
*) NULL
);
415 gdk_gc_set_clip_rectangle( m_textGC
, (GdkRectangle
*) NULL
);
416 gdk_gc_set_clip_rectangle( m_bgGC
, (GdkRectangle
*) NULL
);
419 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
421 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
423 m_window
->GetSize(width
, height
);
426 bool wxWindowDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
427 const wxColour
& col
, int style
)
430 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
431 const wxColour
& col
, int style
);
433 return wxDoFloodFill( GetOwner(), x
, y
, col
, style
);
444 bool wxWindowDCImpl::DoGetPixel( wxCoord x1
, wxCoord y1
, wxColour
*col
) const
446 GdkImage
* image
= NULL
;
449 const int x
= LogicalToDeviceX(x1
);
450 const int y
= LogicalToDeviceY(y1
);
452 gdk_drawable_get_size(m_gdkwindow
, &rect
.width
, &rect
.height
);
453 if (rect
.Contains(x
, y
))
454 image
= gdk_drawable_get_image(m_gdkwindow
, x
, y
, 1, 1);
461 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
462 const unsigned pixel
= gdk_image_get_pixel(image
, 0, 0);
463 if (colormap
== NULL
)
464 *col
= pixel
? m_textForegroundColour
: m_textBackgroundColour
;
468 gdk_colormap_query_color(colormap
, pixel
, &c
);
469 col
->Set(c
.red
>> 8, c
.green
>> 8, c
.blue
>> 8);
471 g_object_unref(image
);
475 void wxWindowDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
477 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
479 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
482 gdk_draw_line( m_gdkwindow
, m_penGC
, XLOG2DEV(x1
), YLOG2DEV(y1
), XLOG2DEV(x2
), YLOG2DEV(y2
) );
484 CalcBoundingBox(x1
, y1
);
485 CalcBoundingBox(x2
, y2
);
489 void wxWindowDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
491 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
493 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
497 GetOwner()->GetSize( &w
, &h
);
498 wxCoord xx
= XLOG2DEV(x
);
499 wxCoord yy
= YLOG2DEV(y
);
502 gdk_draw_line( m_gdkwindow
, m_penGC
, 0, yy
, XLOG2DEVREL(w
), yy
);
503 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, 0, xx
, YLOG2DEVREL(h
) );
508 void wxWindowDCImpl::DrawingSetup(GdkGC
*& gc
, bool& originChanged
)
511 GdkPixmap
* pixmap
= NULL
;
512 const int style
= m_brush
.GetStyle();
514 if (style
== wxBRUSHSTYLE_STIPPLE
|| style
== wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
)
516 const wxBitmap
* stipple
= m_brush
.GetStipple();
519 if (style
== wxBRUSHSTYLE_STIPPLE
)
520 pixmap
= stipple
->GetPixmap();
521 else if (stipple
->GetMask())
523 pixmap
= stipple
->GetPixmap();
528 else if (m_brush
.IsHatch())
530 pixmap
= GetHatch(style
);
538 gdk_drawable_get_size(pixmap
, &w
, &h
);
539 origin_x
= m_deviceOriginX
% w
;
540 origin_y
= m_deviceOriginY
% h
;
543 originChanged
= origin_x
|| origin_y
;
545 gdk_gc_set_ts_origin(gc
, origin_x
, origin_y
);
548 void wxWindowDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
,
549 wxCoord xc
, wxCoord yc
)
551 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
553 wxCoord xx1
= XLOG2DEV(x1
);
554 wxCoord yy1
= YLOG2DEV(y1
);
555 wxCoord xx2
= XLOG2DEV(x2
);
556 wxCoord yy2
= YLOG2DEV(y2
);
557 wxCoord xxc
= XLOG2DEV(xc
);
558 wxCoord yyc
= YLOG2DEV(yc
);
559 double dx
= xx1
- xxc
;
560 double dy
= yy1
- yyc
;
561 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
562 wxCoord r
= (wxCoord
)radius
;
563 double radius1
, radius2
;
565 if (xx1
== xx2
&& yy1
== yy2
)
570 else if ( wxIsNullDouble(radius
) )
577 radius1
= (xx1
- xxc
== 0) ?
578 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
579 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
580 radius2
= (xx2
- xxc
== 0) ?
581 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
582 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
584 wxCoord alpha1
= wxCoord(radius1
* 64.0);
585 wxCoord alpha2
= wxCoord((radius2
- radius1
) * 64.0);
586 while (alpha2
<= 0) alpha2
+= 360*64;
587 while (alpha1
> 360*64) alpha1
-= 360*64;
591 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
595 DrawingSetup(gc
, originChanged
);
597 gdk_draw_arc(m_gdkwindow
, gc
, true, xxc
-r
, yyc
-r
, 2*r
, 2*r
, alpha1
, alpha2
);
600 gdk_gc_set_ts_origin(gc
, 0, 0);
603 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
605 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xxc
-r
, yyc
-r
, 2*r
,2*r
, alpha1
, alpha2
);
607 if ((m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
) && (alpha2
- alpha1
!= 360*64))
609 gdk_draw_line( m_gdkwindow
, m_penGC
, xx1
, yy1
, xxc
, yyc
);
610 gdk_draw_line( m_gdkwindow
, m_penGC
, xxc
, yyc
, xx2
, yy2
);
615 CalcBoundingBox (x1
, y1
);
616 CalcBoundingBox (x2
, y2
);
619 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double sa
, double ea
)
621 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
623 wxCoord xx
= XLOG2DEV(x
);
624 wxCoord yy
= YLOG2DEV(y
);
625 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
626 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
628 // CMB: handle -ve width and/or height
629 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
630 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
634 wxCoord start
= wxCoord(sa
* 64.0);
635 wxCoord end
= wxCoord((ea
-sa
) * 64.0);
637 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
641 DrawingSetup(gc
, originChanged
);
643 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, start
, end
);
646 gdk_gc_set_ts_origin(gc
, 0, 0);
649 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
650 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, start
, end
);
653 CalcBoundingBox (x
, y
);
654 CalcBoundingBox (x
+ width
, y
+ height
);
657 void wxWindowDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
659 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
661 if ((m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
) && m_gdkwindow
)
662 gdk_draw_point( m_gdkwindow
, m_penGC
, XLOG2DEV(x
), YLOG2DEV(y
) );
664 CalcBoundingBox (x
, y
);
667 void wxWindowDCImpl::DoDrawLines( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
669 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
671 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
) return;
674 //Check, if scaling is necessary
676 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
678 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
679 GdkPoint
* gpts
= reinterpret_cast<GdkPoint
*>(points
);
682 gpts
= new GdkPoint
[n
];
684 for (int i
= 0; i
< n
; i
++)
688 gpts
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
689 gpts
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
691 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
695 gdk_draw_lines( m_gdkwindow
, m_penGC
, gpts
, n
);
701 void wxWindowDCImpl::DoDrawPolygon( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int WXUNUSED(fillStyle
) )
703 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
707 //Check, if scaling is necessary
709 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
711 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
712 GdkPoint
* gdkpoints
= reinterpret_cast<GdkPoint
*>(points
);
715 gdkpoints
= new GdkPoint
[n
];
718 for (i
= 0 ; i
< n
; i
++)
722 gdkpoints
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
723 gdkpoints
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
725 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
730 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
734 DrawingSetup(gc
, originChanged
);
736 gdk_draw_polygon(m_gdkwindow
, gc
, true, gdkpoints
, n
);
739 gdk_gc_set_ts_origin(gc
, 0, 0);
742 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
745 for (i = 0 ; i < n ; i++)
747 gdk_draw_line( m_gdkwindow, m_penGC,
750 gdkpoints[(i+1)%n].x,
751 gdkpoints[(i+1)%n].y);
754 gdk_draw_polygon( m_gdkwindow
, m_penGC
, FALSE
, gdkpoints
, n
);
763 void wxWindowDCImpl::DoDrawRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
765 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
767 wxCoord xx
= XLOG2DEV(x
);
768 wxCoord yy
= YLOG2DEV(y
);
769 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
770 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
772 // CMB: draw nothing if transformed w or h is 0
773 if (ww
== 0 || hh
== 0) return;
775 // CMB: handle -ve width and/or height
776 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
777 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
781 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
785 DrawingSetup(gc
, originChanged
);
787 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
);
790 gdk_gc_set_ts_origin(gc
, 0, 0);
793 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
796 if ((m_pen
.GetWidth() == 2) && (m_pen
.GetCap() == wxCAP_ROUND
) &&
797 (m_pen
.GetJoin() == wxJOIN_ROUND
) && (m_pen
.GetStyle() == wxPENSTYLE_SOLID
))
799 // Use 2 1-line rects instead
800 gdk_gc_set_line_attributes( m_penGC
, 1, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
805 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
+1, yy
, ww
-2, hh
-2 );
806 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
-1, ww
, hh
);
810 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-2, hh
-2 );
811 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
-1, yy
-1, ww
, hh
);
815 gdk_gc_set_line_attributes( m_penGC
, 2, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
820 // Just use X11 for other cases
821 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-1, hh
-1 );
826 CalcBoundingBox( x
, y
);
827 CalcBoundingBox( x
+ width
, y
+ height
);
830 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
832 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
834 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
836 wxCoord xx
= XLOG2DEV(x
);
837 wxCoord yy
= YLOG2DEV(y
);
838 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
839 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
840 wxCoord rr
= XLOG2DEVREL((wxCoord
)radius
);
842 // CMB: handle -ve width and/or height
843 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
844 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
846 // CMB: if radius is zero use DrawRectangle() instead to avoid
847 // X drawing errors with small radii
850 DoDrawRectangle( x
, y
, width
, height
);
854 // CMB: draw nothing if transformed w or h is 0
855 if (ww
== 0 || hh
== 0) return;
857 // CMB: adjust size if outline is drawn otherwise the result is
858 // 1 pixel too wide and high
859 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
867 // CMB: ensure dd is not larger than rectangle otherwise we
868 // get an hour glass shape
870 if (dd
> ww
) dd
= ww
;
871 if (dd
> hh
) dd
= hh
;
874 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
878 DrawingSetup(gc
, originChanged
);
880 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
+rr
, yy
, ww
-dd
+1, hh
);
881 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
+rr
, ww
, hh
-dd
+1);
882 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, dd
, dd
, 90*64, 90*64);
883 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64);
884 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64);
885 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64);
888 gdk_gc_set_ts_origin(gc
, 0, 0);
891 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
893 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
, xx
+ww
-rr
, yy
);
894 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
+hh
, xx
+ww
-rr
, yy
+hh
);
895 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, yy
+rr
+1, xx
, yy
+hh
-rr
);
896 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+ww
, yy
+rr
+1, xx
+ww
, yy
+hh
-rr
);
897 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, dd
, dd
, 90*64, 90*64 );
898 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64 );
899 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64 );
900 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64 );
904 // this ignores the radius
905 CalcBoundingBox( x
, y
);
906 CalcBoundingBox( x
+ width
, y
+ height
);
909 void wxWindowDCImpl::DoDrawEllipse( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
911 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
913 wxCoord xx
= XLOG2DEV(x
);
914 wxCoord yy
= YLOG2DEV(y
);
915 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
916 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
918 // CMB: handle -ve width and/or height
919 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
920 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
924 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
928 DrawingSetup(gc
, originChanged
);
930 // If the pen is transparent pen we increase the size
931 // for better compatibility with other platforms.
932 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
938 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, 0, 360*64);
941 gdk_gc_set_ts_origin(gc
, 0, 0);
944 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
945 gdk_draw_arc( m_gdkwindow
, m_penGC
, false, xx
, yy
, ww
, hh
, 0, 360*64 );
948 CalcBoundingBox( x
, y
);
949 CalcBoundingBox( x
+ width
, y
+ height
);
952 void wxWindowDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
954 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
955 DoDrawBitmap( (const wxBitmap
&)icon
, x
, y
, true );
958 // scale a pixbuf, return new pixbuf, unref old one
960 Scale(GdkPixbuf
* pixbuf
, int dst_w
, int dst_h
, double sx
, double sy
)
962 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
963 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, dst_w
, dst_h
);
964 gdk_pixbuf_scale(pixbuf
, pixbuf_scaled
,
965 0, 0, dst_w
, dst_h
, 0, 0, sx
, sy
, GDK_INTERP_NEAREST
);
966 g_object_unref(pixbuf
);
967 return pixbuf_scaled
;
970 // scale part of a pixmap using pixbuf scaling, return pixbuf
972 Scale(GdkPixmap
* pixmap
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
974 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(
975 NULL
, pixmap
, NULL
, x
, y
, 0, 0, w
, h
);
976 return Scale(pixbuf
, dst_w
, dst_h
, sx
, sy
);
979 // scale part of a mask pixmap, return new mask, unref old one
981 ScaleMask(GdkPixmap
* mask
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
983 GdkPixbuf
* pixbuf
= Scale(mask
, x
, y
, w
, h
, dst_w
, dst_h
, sx
, sy
);
985 // convert black and white pixbuf back to a mono pixmap
986 const unsigned out_rowstride
= (dst_w
+ 7) / 8;
987 const size_t data_size
= out_rowstride
* size_t(dst_h
);
988 char* data
= new char[data_size
];
990 const guchar
* row
= gdk_pixbuf_get_pixels(pixbuf
);
991 const int rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
992 memset(data
, 0, data_size
);
993 for (int j
= 0; j
< dst_h
; j
++, row
+= rowstride
, out
+= out_rowstride
)
995 const guchar
* in
= row
;
996 for (int i
= 0; i
< dst_w
; i
++, in
+= 3)
998 out
[i
>> 3] |= 1 << (i
& 7);
1000 g_object_unref(pixbuf
);
1001 GdkPixmap
* pixmap
= gdk_bitmap_create_from_data(mask
, data
, dst_w
, dst_h
);
1003 g_object_unref(mask
);
1007 // Make a new mask from part of a mask and a clip region.
1008 // Return new mask, unref old one.
1010 ClipMask(GdkPixmap
* mask
, GdkRegion
* clipRegion
, int x
, int y
, int dst_x
, int dst_y
, int w
, int h
)
1012 GdkGCValues gcValues
;
1013 gcValues
.foreground
.pixel
= 0;
1014 GdkGC
* gc
= gdk_gc_new_with_values(mask
, &gcValues
, GDK_GC_FOREGROUND
);
1015 GdkPixmap
* pixmap
= gdk_pixmap_new(mask
, w
, h
, 1);
1016 // clear new mask, so clipped areas will be masked
1017 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1018 gdk_gc_set_clip_region(gc
, clipRegion
);
1019 gdk_gc_set_clip_origin(gc
, -dst_x
, -dst_y
);
1020 // draw old mask onto new one, with clip
1021 gdk_draw_drawable(pixmap
, gc
, mask
, x
, y
, 0, 0, w
, h
);
1023 g_object_unref(mask
);
1027 // make a color pixmap from part of a mono one, using text fg/bg colors
1029 wxWindowDCImpl::MonoToColor(GdkPixmap
* monoPixmap
, int x
, int y
, int w
, int h
) const
1031 GdkPixmap
* pixmap
= gdk_pixmap_new(m_gdkwindow
, w
, h
, -1);
1032 GdkGCValues gcValues
;
1033 gcValues
.foreground
.pixel
= m_textForegroundColour
.GetColor()->pixel
;
1034 gcValues
.background
.pixel
= m_textBackgroundColour
.GetColor()->pixel
;
1035 gcValues
.stipple
= monoPixmap
;
1036 gcValues
.fill
= GDK_OPAQUE_STIPPLED
;
1037 gcValues
.ts_x_origin
= -x
;
1038 gcValues
.ts_y_origin
= -y
;
1039 GdkGC
* gc
= gdk_gc_new_with_values(pixmap
, &gcValues
, GdkGCValuesMask(
1040 GDK_GC_FOREGROUND
| GDK_GC_BACKGROUND
| GDK_GC_STIPPLE
| GDK_GC_FILL
|
1041 GDK_GC_TS_X_ORIGIN
| GDK_GC_TS_Y_ORIGIN
));
1042 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1047 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap
&bitmap
,
1048 wxCoord x
, wxCoord y
,
1051 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1052 wxCHECK_RET( bitmap
.IsOk(), wxT("invalid bitmap") );
1054 if (!m_gdkwindow
) return;
1056 const int w
= bitmap
.GetWidth();
1057 const int h
= bitmap
.GetHeight();
1059 CalcBoundingBox(x
, y
);
1060 CalcBoundingBox(x
+ w
, y
+ h
);
1063 int xx
= LogicalToDeviceX(x
);
1064 const int yy
= LogicalToDeviceY(y
);
1065 const int ww
= LogicalToDeviceXRel(w
);
1066 const int hh
= LogicalToDeviceYRel(h
);
1068 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1071 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1072 // determine clip region overlap
1073 int overlap
= wxInRegion
;
1076 overlap
= m_currentClippingRegion
.Contains(xx
, yy
, ww
, hh
);
1077 if (overlap
== wxOutRegion
)
1081 const bool isScaled
= ww
!= w
|| hh
!= h
;
1082 const bool hasAlpha
= bitmap
.HasAlpha();
1083 GdkGC
* const use_gc
= m_penGC
;
1085 GdkPixmap
* mask
= NULL
;
1086 // mask does not work when drawing a pixbuf with alpha
1087 if (useMask
&& !hasAlpha
)
1089 wxMask
* m
= bitmap
.GetMask();
1091 mask
= m
->GetBitmap();
1097 mask
= ScaleMask(mask
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1098 if (overlap
== wxPartRegion
)
1100 // need a new mask that also masks the clipped area,
1101 // because gc can't have both a mask and a clip region
1102 mask
= ClipMask(mask
, clipRegion
, 0, 0, xx
, yy
, ww
, hh
);
1104 gdk_gc_set_clip_mask(use_gc
, mask
);
1105 gdk_gc_set_clip_origin(use_gc
, xx
, yy
);
1108 // determine whether to use pixmap or pixbuf
1109 GdkPixmap
* pixmap
= NULL
;
1110 GdkPixbuf
* pixbuf
= NULL
;
1111 if (bitmap
.HasPixmap())
1112 pixmap
= bitmap
.GetPixmap();
1113 if (pixmap
&& gdk_drawable_get_depth(pixmap
) == 1)
1115 // convert mono pixmap to color using text fg/bg colors
1116 pixmap
= MonoToColor(pixmap
, 0, 0, w
, h
);
1118 else if (hasAlpha
|| pixmap
== NULL
)
1121 pixbuf
= bitmap
.GetPixbuf();
1122 g_object_ref(pixbuf
);
1126 g_object_ref(pixmap
);
1132 pixbuf
= Scale(pixbuf
, ww
, hh
, m_scaleX
, m_scaleY
);
1134 pixbuf
= Scale(pixmap
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1139 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1140 0, 0, xx
, yy
, ww
, hh
, GDK_RGB_DITHER_NORMAL
, 0, 0);
1141 g_object_unref(pixbuf
);
1145 gdk_draw_drawable(m_gdkwindow
, use_gc
, pixmap
, 0, 0, xx
, yy
, ww
, hh
);
1149 g_object_unref(pixmap
);
1152 g_object_unref(mask
);
1153 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1157 bool wxWindowDCImpl::DoBlit( wxCoord xdest
, wxCoord ydest
,
1158 wxCoord width
, wxCoord height
,
1160 wxCoord xsrc
, wxCoord ysrc
,
1163 wxCoord xsrcMask
, wxCoord ysrcMask
)
1165 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1166 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1168 if (!m_gdkwindow
) return false;
1170 GdkDrawable
* srcDrawable
= NULL
;
1171 GdkPixmap
* mask
= NULL
;
1172 wxMemoryDC
* memDC
= wxDynamicCast(source
, wxMemoryDC
);
1175 const wxBitmap
& bitmap
= memDC
->GetSelectedBitmap();
1178 srcDrawable
= bitmap
.GetPixmap();
1181 wxMask
* m
= bitmap
.GetMask();
1183 mask
= m
->GetBitmap();
1188 wxDCImpl
* impl
= source
->GetImpl();
1189 wxWindowDCImpl
* gtk_impl
= wxDynamicCast(impl
, wxWindowDCImpl
);
1191 srcDrawable
= gtk_impl
->GetGDKWindow();
1192 if (srcDrawable
== NULL
)
1196 CalcBoundingBox(xdest
, ydest
);
1197 CalcBoundingBox(xdest
+ width
, ydest
+ height
);
1199 // source device coords
1200 int src_x
= source
->LogicalToDeviceX(xsrc
);
1201 int src_y
= source
->LogicalToDeviceY(ysrc
);
1202 int src_w
= source
->LogicalToDeviceXRel(width
);
1203 int src_h
= source
->LogicalToDeviceYRel(height
);
1205 // Clip source rect to source dc.
1206 // Only necessary when scaling, to avoid GDK errors when
1207 // converting to pixbuf, but no harm in always doing it.
1208 // If source rect changes, it also changes the dest rect.
1210 gdk_drawable_get_size(srcDrawable
, &clip
.width
, &clip
.height
);
1211 clip
.Intersect(wxRect(src_x
, src_y
, src_w
, src_h
));
1212 if (src_w
!= clip
.width
|| src_h
!= clip
.height
)
1214 if (clip
.width
== 0)
1218 src_h
= clip
.height
;
1219 width
= source
->DeviceToLogicalXRel(src_w
);
1220 height
= source
->DeviceToLogicalYRel(src_h
);
1221 if (src_x
!= clip
.x
|| src_y
!= clip
.y
)
1223 xdest
+= source
->DeviceToLogicalXRel(clip
.x
- src_x
);
1224 ydest
+= source
->DeviceToLogicalYRel(clip
.y
- src_y
);
1230 // destination device coords
1231 const int dst_x
= LogicalToDeviceX(xdest
);
1232 const int dst_y
= LogicalToDeviceY(ydest
);
1233 const int dst_w
= LogicalToDeviceXRel(width
);
1234 const int dst_h
= LogicalToDeviceYRel(height
);
1236 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1237 // determine dest clip region overlap
1238 int overlap
= wxInRegion
;
1241 overlap
= m_currentClippingRegion
.Contains(dst_x
, dst_y
, dst_w
, dst_h
);
1242 if (overlap
== wxOutRegion
)
1246 const bool isScaled
= src_w
!= dst_w
|| src_h
!= dst_h
;
1251 // get source to dest scale
1252 double usx
, usy
, lsx
, lsy
;
1253 source
->GetUserScale(&usx
, &usy
);
1254 source
->GetLogicalScale(&lsx
, &lsy
);
1255 scale_x
= m_scaleX
/ (usx
* lsx
);
1256 scale_y
= m_scaleY
/ (usy
* lsy
);
1259 GdkGC
* const use_gc
= m_penGC
;
1264 int srcMask_x
= src_x
;
1265 int srcMask_y
= src_y
;
1266 if (xsrcMask
!= -1 || ysrcMask
!= -1)
1268 srcMask_x
= source
->LogicalToDeviceX(xsrcMask
);
1269 srcMask_y
= source
->LogicalToDeviceY(ysrcMask
);
1273 mask
= ScaleMask(mask
, srcMask_x
, srcMask_y
,
1274 src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1278 if (overlap
== wxPartRegion
)
1280 // need a new mask that also masks the clipped area,
1281 // because gc can't have both a mask and a clip region
1282 mask
= ClipMask(mask
, clipRegion
,
1283 srcMask_x
, srcMask_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1287 gdk_gc_set_clip_mask(use_gc
, mask
);
1288 gdk_gc_set_clip_origin(use_gc
, dst_x
- srcMask_x
, dst_y
- srcMask_y
);
1291 GdkPixmap
* pixmap
= NULL
;
1292 if (gdk_drawable_get_depth(srcDrawable
) == 1)
1294 // Convert mono pixmap to color using text fg/bg colors.
1295 // Scaling/drawing is simpler if this is done first.
1296 pixmap
= MonoToColor(srcDrawable
, src_x
, src_y
, src_w
, src_h
);
1297 srcDrawable
= pixmap
;
1302 const int logical_func_save
= m_logicalFunction
;
1303 SetLogicalFunction(logical_func
);
1305 gdk_gc_set_subwindow(use_gc
, GDK_INCLUDE_INFERIORS
);
1309 GdkPixbuf
* pixbuf
= Scale(srcDrawable
,
1310 src_x
, src_y
, src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1311 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1312 0, 0, dst_x
, dst_y
, dst_w
, dst_h
, GDK_RGB_DITHER_NONE
, 0, 0);
1313 g_object_unref(pixbuf
);
1317 gdk_draw_drawable(m_gdkwindow
, use_gc
, srcDrawable
,
1318 src_x
, src_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1321 SetLogicalFunction(logical_func_save
);
1323 gdk_gc_set_subwindow(use_gc
, GDK_CLIP_BY_CHILDREN
);
1326 g_object_unref(pixmap
);
1329 g_object_unref(mask
);
1330 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1335 void wxWindowDCImpl::DoDrawText( const wxString
&text
, wxCoord x
, wxCoord y
)
1337 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1339 if (!m_gdkwindow
) return;
1341 if (text
.empty()) return;
1346 wxCHECK_RET( m_context
, wxT("no Pango context") );
1347 wxCHECK_RET( m_layout
, wxT("no Pango layout") );
1348 wxCHECK_RET( m_fontdesc
, wxT("no Pango font description") );
1350 gdk_pango_context_set_colormap( m_context
, m_cmap
); // not needed in gtk+ >= 2.6
1352 bool underlined
= m_font
.IsOk() && m_font
.GetUnderlined();
1354 wxCharBuffer data
= wxGTK_CONV(text
);
1357 size_t datalen
= strlen(data
);
1359 // in Pango >= 1.16 the "underline of leading/trailing spaces" bug
1360 // has been fixed and thus the hack implemented below should never be used
1361 static bool pangoOk
= !wx_pango_version_check(1, 16, 0);
1363 bool needshack
= underlined
&& !pangoOk
;
1367 // a PangoLayout which has leading/trailing spaces with underlined font
1368 // is not correctly drawn by this pango version: Pango won't underline the spaces.
1369 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
1370 // this behaviour. To workaround this problem, we use a special hack here
1371 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
1372 // empty space characters and give them a dummy colour attribute.
1373 // This will force Pango to underline the leading/trailing spaces, too.
1375 wxCharBuffer
data_tmp(datalen
+ 6);
1376 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1377 memcpy(data_tmp
.data(), "\342\200\214", 3);
1378 // copy the user string
1379 memcpy(data_tmp
.data() + 3, data
, datalen
);
1380 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1381 memcpy(data_tmp
.data() + 3 + datalen
, "\342\200\214", 3);
1387 pango_layout_set_text(m_layout
, data
, datalen
);
1391 PangoAttrList
*attrs
= pango_attr_list_new();
1392 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1394 a
->end_index
= datalen
;
1395 pango_attr_list_insert(attrs
, a
);
1399 // dummy colour for the leading space
1400 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1403 pango_attr_list_insert(attrs
, a
);
1405 // dummy colour for the trailing space
1406 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1407 a
->start_index
= datalen
- 1;
1408 a
->end_index
= datalen
;
1409 pango_attr_list_insert(attrs
, a
);
1412 pango_layout_set_attributes(m_layout
, attrs
);
1413 pango_attr_list_unref(attrs
);
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(x
+ int(w
/ m_scaleX
), y
+ int(h
/ m_scaleY
));
1460 CalcBoundingBox(x
, y
);
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);
1480 if (m_font
.GetUnderlined())
1482 PangoAttrList
*attrs
= pango_attr_list_new();
1483 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1484 pango_attr_list_insert(attrs
, a
);
1485 pango_layout_set_attributes(m_layout
, attrs
);
1486 pango_attr_list_unref(attrs
);
1490 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1493 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1494 // If there is a user or actually any scale applied to
1495 // the device context, scale the font.
1497 // scale font description
1498 oldSize
= pango_font_description_get_size(m_fontdesc
);
1499 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1501 // actually apply scaled font
1502 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1506 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1508 const GdkColor
* bg_col
= NULL
;
1509 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1510 bg_col
= m_textBackgroundColour
.GetColor();
1513 PangoMatrix matrix
= PANGO_MATRIX_INIT
;
1514 pango_matrix_rotate (&matrix
, angle
);
1515 pango_context_set_matrix (m_context
, &matrix
);
1516 pango_layout_context_changed (m_layout
);
1518 // To be compatible with MSW, the rotation axis must be in the old
1520 // Calculate the vertices of the rotated rectangle containing the text,
1521 // relative to the old top-left vertex.
1522 // We could use the matrix for this, but it's simpler with trignonometry.
1523 double rad
= DegToRad(angle
);
1524 // the rectangle vertices are counted clockwise with the first one
1526 double x2
= w
* cos(rad
);
1527 double y2
= -w
* sin(rad
); // y axis points to the bottom, hence minus
1528 double x4
= h
* sin(rad
);
1529 double y4
= h
* cos(rad
);
1530 double x3
= x4
+ x2
;
1531 double y3
= y4
+ y2
;
1532 // Then we calculate max and min of the rotated rectangle.
1533 wxCoord maxX
= (wxCoord
)(dmax(dmax(0, x2
), dmax(x3
, x4
)) + 0.5),
1534 maxY
= (wxCoord
)(dmax(dmax(0, y2
), dmax(y3
, y4
)) + 0.5),
1535 minX
= (wxCoord
)(dmin(dmin(0, x2
), dmin(x3
, x4
)) - 0.5),
1536 minY
= (wxCoord
)(dmin(dmin(0, y2
), dmin(y3
, y4
)) - 0.5);
1538 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x
+minX
, y
+minY
,
1539 m_layout
, NULL
, bg_col
);
1541 if (m_font
.GetUnderlined())
1542 pango_layout_set_attributes(m_layout
, NULL
);
1544 // clean up the transformation matrix
1545 pango_context_set_matrix(m_context
, NULL
);
1549 // reset unscaled size
1550 pango_font_description_set_size( m_fontdesc
, oldSize
);
1552 // actually apply unscaled font
1553 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1556 CalcBoundingBox(x
+minX
, y
+minY
);
1557 CalcBoundingBox(x
+maxX
, y
+maxY
);
1560 #endif //__WXGTK26__
1563 if ( wxIsNullDouble(angle
) )
1565 DoDrawText(text
, x
, y
);
1572 // TODO: implement later without GdkFont for GTK 2.0
1573 DoGetTextExtent(text
, &w
, &h
, NULL
,NULL
, &m_font
);
1575 // draw the string normally
1578 dc
.SelectObject(src
);
1579 dc
.SetFont(GetFont());
1580 dc
.SetBackground(*wxBLACK_BRUSH
);
1581 dc
.SetBrush(*wxBLACK_BRUSH
);
1583 dc
.SetTextForeground( *wxWHITE
);
1584 dc
.DrawText(text
, 0, 0);
1585 dc
.SelectObject(wxNullBitmap
);
1587 // Calculate the size of the rotated bounding box.
1588 double rad
= DegToRad(angle
);
1589 double dx
= cos(rad
),
1592 // the rectngle vertices are counted clockwise with the first one being at
1593 // (0, 0) (or, rather, at (x, y))
1595 y2
= -w
*dy
; // y axis points to the bottom, hence minus
1598 double x3
= x4
+ x2
,
1602 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5),
1603 maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5),
1604 minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5),
1605 minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1608 wxImage image
= src
.ConvertToImage();
1610 image
.ConvertColourToAlpha( m_textForegroundColour
.Red(),
1611 m_textForegroundColour
.Green(),
1612 m_textForegroundColour
.Blue() );
1613 image
= image
.Rotate( rad
, wxPoint(0,0) );
1615 int i_angle
= (int) angle
;
1616 i_angle
= i_angle
% 360;
1620 if ((i_angle
>= 90.0) && (i_angle
< 270.0))
1621 xoffset
= image
.GetWidth();
1623 if ((i_angle
>= 0.0) && (i_angle
< 180.0))
1624 yoffset
= image
.GetHeight();
1626 if ((i_angle
>= 0) && (i_angle
< 90))
1627 yoffset
-= (int)( cos(rad
)*h
);
1628 if ((i_angle
>= 90) && (i_angle
< 180))
1629 xoffset
-= (int)( sin(rad
)*h
);
1630 if ((i_angle
>= 180) && (i_angle
< 270))
1631 yoffset
-= (int)( cos(rad
)*h
);
1632 if ((i_angle
>= 270) && (i_angle
< 360))
1633 xoffset
-= (int)( sin(rad
)*h
);
1635 int i_x
= x
- xoffset
;
1636 int i_y
= y
- yoffset
;
1639 DoDrawBitmap( src
, i_x
, i_y
, true );
1642 // it would be better to draw with non underlined font and draw the line
1643 // manually here (it would be more straight...)
1645 if ( m_font
.GetUnderlined() )
1647 gdk_draw_line( m_gdkwindow
, m_textGC
,
1648 XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
),
1649 XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1653 // update the bounding box
1654 CalcBoundingBox(x
+ minX
, y
+ minY
);
1655 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1656 #else // !wxUSE_IMAGE
1661 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
1665 void wxWindowDCImpl::DoGetTextExtent(const wxString
&string
,
1666 wxCoord
*width
, wxCoord
*height
,
1667 wxCoord
*descent
, wxCoord
*externalLeading
,
1668 const wxFont
*theFont
) const
1676 if ( externalLeading
)
1677 *externalLeading
= 0;
1682 // ensure that theFont is always non-NULL
1683 if ( !theFont
|| !theFont
->IsOk() )
1686 // and use it if it's valid
1687 if ( theFont
->IsOk() )
1689 pango_layout_set_font_description
1692 theFont
->GetNativeFontInfo()->description
1696 // Set layout's text
1697 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *theFont
);
1700 // hardly ideal, but what else can we do if conversion failed?
1704 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1707 pango_layout_get_pixel_size(m_layout
, width
, &h
);
1710 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1711 int baseline
= pango_layout_iter_get_baseline(iter
);
1712 pango_layout_iter_free(iter
);
1713 *descent
= h
- PANGO_PIXELS(baseline
);
1718 // Reset old font description
1719 if (theFont
->IsOk())
1720 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1724 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString
& text
,
1725 wxArrayInt
& widths
) const
1727 const size_t len
= text
.length();
1734 // Set layout's text
1735 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, m_font
);
1738 // hardly ideal, but what else can we do if conversion failed?
1739 wxLogLastError(wxT("DoGetPartialTextExtents"));
1743 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1745 // Calculate the position of each character based on the widths of
1746 // the previous characters
1748 // Code borrowed from Scintilla's PlatGTK
1749 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1751 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1753 while (pango_layout_iter_next_cluster(iter
))
1755 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1756 int position
= PANGO_PIXELS(pos
.x
);
1757 widths
[i
++] = position
;
1760 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
1761 pango_layout_iter_free(iter
);
1767 wxCoord
wxWindowDCImpl::GetCharWidth() const
1769 pango_layout_set_text( m_layout
, "H", 1 );
1771 pango_layout_get_pixel_size( m_layout
, &w
, NULL
);
1775 wxCoord
wxWindowDCImpl::GetCharHeight() const
1777 PangoFontMetrics
*metrics
= pango_context_get_metrics (m_context
, m_fontdesc
, pango_context_get_language(m_context
));
1778 wxCHECK_MSG( metrics
, -1, _T("failed to get pango font metrics") );
1780 wxCoord h
= PANGO_PIXELS (pango_font_metrics_get_descent (metrics
) +
1781 pango_font_metrics_get_ascent (metrics
));
1782 pango_font_metrics_unref (metrics
);
1786 void wxWindowDCImpl::Clear()
1788 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1790 if (!m_gdkwindow
) return;
1793 DoGetSize( &width
, &height
);
1794 gdk_draw_rectangle( m_gdkwindow
, m_bgGC
, TRUE
, 0, 0, width
, height
);
1797 void wxWindowDCImpl::SetFont( const wxFont
&font
)
1804 pango_font_description_free( m_fontdesc
);
1806 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1811 PangoContext
*oldContext
= m_context
;
1813 m_context
= m_window
->GtkGetPangoDefaultContext();
1815 // If we switch back/forth between different contexts
1816 // we also have to create a new layout. I think so,
1817 // at least, and it doesn't hurt to do it.
1818 if (oldContext
!= m_context
)
1821 g_object_unref (m_layout
);
1823 m_layout
= pango_layout_new( m_context
);
1827 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1831 void wxWindowDCImpl::SetPen( const wxPen
&pen
)
1833 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1835 if (m_pen
== pen
) return;
1839 if (!m_pen
.IsOk()) return;
1841 if (!m_gdkwindow
) return;
1843 gint width
= m_pen
.GetWidth();
1846 // CMB: if width is non-zero scale it with the dc
1851 // X doesn't allow different width in x and y and so we take
1854 ( fabs((double) XLOG2DEVREL(width
)) +
1855 fabs((double) YLOG2DEVREL(width
)) ) / 2.0;
1859 // width can't be 0 or an internal GTK error occurs inside
1860 // gdk_gc_set_dashes() below
1865 static const wxGTKDash dotted
[] = {1, 1};
1866 static const wxGTKDash short_dashed
[] = {2, 2};
1867 static const wxGTKDash wxCoord_dashed
[] = {2, 4};
1868 static const wxGTKDash dotted_dashed
[] = {3, 3, 1, 3};
1870 // We express dash pattern in pen width unit, so we are
1871 // independent of zoom factor and so on...
1873 const wxGTKDash
*req_dash
;
1875 GdkLineStyle lineStyle
= GDK_LINE_ON_OFF_DASH
;
1876 switch (m_pen
.GetStyle())
1878 case wxPENSTYLE_USER_DASH
:
1879 req_nb_dash
= m_pen
.GetDashCount();
1880 req_dash
= (wxGTKDash
*)m_pen
.GetDash();
1882 case wxPENSTYLE_DOT
:
1886 case wxPENSTYLE_LONG_DASH
:
1888 req_dash
= wxCoord_dashed
;
1890 case wxPENSTYLE_SHORT_DASH
:
1892 req_dash
= short_dashed
;
1894 case wxPENSTYLE_DOT_DASH
:
1896 req_dash
= dotted_dashed
;
1899 case wxPENSTYLE_TRANSPARENT
:
1900 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
1901 case wxPENSTYLE_STIPPLE
:
1902 case wxPENSTYLE_SOLID
:
1904 lineStyle
= GDK_LINE_SOLID
;
1905 req_dash
= (wxGTKDash
*)NULL
;
1910 if (req_dash
&& req_nb_dash
)
1912 wxGTKDash
*real_req_dash
= new wxGTKDash
[req_nb_dash
];
1915 for (int i
= 0; i
< req_nb_dash
; i
++)
1916 real_req_dash
[i
] = req_dash
[i
] * width
;
1917 gdk_gc_set_dashes( m_penGC
, 0, real_req_dash
, req_nb_dash
);
1918 delete[] real_req_dash
;
1922 // No Memory. We use non-scaled dash pattern...
1923 gdk_gc_set_dashes( m_penGC
, 0, (wxGTKDash
*)req_dash
, req_nb_dash
);
1927 GdkCapStyle capStyle
= GDK_CAP_ROUND
;
1928 switch (m_pen
.GetCap())
1930 case wxCAP_PROJECTING
: { capStyle
= GDK_CAP_PROJECTING
; break; }
1931 case wxCAP_BUTT
: { capStyle
= GDK_CAP_BUTT
; break; }
1937 capStyle
= GDK_CAP_NOT_LAST
;
1942 GdkJoinStyle joinStyle
= GDK_JOIN_ROUND
;
1943 switch (m_pen
.GetJoin())
1945 case wxJOIN_BEVEL
: { joinStyle
= GDK_JOIN_BEVEL
; break; }
1946 case wxJOIN_MITER
: { joinStyle
= GDK_JOIN_MITER
; break; }
1948 default: { joinStyle
= GDK_JOIN_ROUND
; break; }
1951 gdk_gc_set_line_attributes( m_penGC
, width
, lineStyle
, capStyle
, joinStyle
);
1953 m_pen
.GetColour().CalcPixel( m_cmap
);
1954 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
1957 void wxWindowDCImpl::SetBrush( const wxBrush
&brush
)
1959 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1961 if (m_brush
== brush
) return;
1965 if (!m_brush
.IsOk()) return;
1967 if (!m_gdkwindow
) return;
1969 m_brush
.GetColour().CalcPixel( m_cmap
);
1970 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
1972 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
1974 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
) && (m_brush
.GetStipple()->IsOk()))
1976 if (m_brush
.GetStipple()->GetDepth() != 1)
1978 gdk_gc_set_fill( m_brushGC
, GDK_TILED
);
1979 gdk_gc_set_tile( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1983 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1984 gdk_gc_set_stipple( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1988 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
) && (m_brush
.GetStipple()->GetMask()))
1990 gdk_gc_set_fill( m_textGC
, GDK_OPAQUE_STIPPLED
);
1991 gdk_gc_set_stipple( m_textGC
, m_brush
.GetStipple()->GetMask()->GetBitmap() );
1994 if (m_brush
.IsHatch())
1996 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1997 gdk_gc_set_stipple(m_brushGC
, GetHatch(m_brush
.GetStyle()));
2001 void wxWindowDCImpl::SetBackground( const wxBrush
&brush
)
2003 /* CMB 21/7/98: Added SetBackground. Sets background brush
2004 * for Clear() and bg colour for shapes filled with cross-hatch brush */
2006 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2008 if (m_backgroundBrush
== brush
) return;
2010 m_backgroundBrush
= brush
;
2012 if (!m_backgroundBrush
.IsOk()) return;
2014 if (!m_gdkwindow
) return;
2016 wxColor color
= m_backgroundBrush
.GetColour();
2017 color
.CalcPixel(m_cmap
);
2018 const GdkColor
* gdkColor
= color
.GetColor();
2019 gdk_gc_set_background(m_brushGC
, gdkColor
);
2020 gdk_gc_set_background(m_penGC
, gdkColor
);
2021 gdk_gc_set_background(m_bgGC
, gdkColor
);
2022 gdk_gc_set_foreground(m_bgGC
, gdkColor
);
2025 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
2027 if (m_backgroundBrush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
)
2029 const wxBitmap
* stipple
= m_backgroundBrush
.GetStipple();
2030 if (stipple
->IsOk())
2032 if (stipple
->GetDepth() != 1)
2034 gdk_gc_set_fill(m_bgGC
, GDK_TILED
);
2035 gdk_gc_set_tile(m_bgGC
, stipple
->GetPixmap());
2039 gdk_gc_set_fill(m_bgGC
, GDK_STIPPLED
);
2040 gdk_gc_set_stipple(m_bgGC
, stipple
->GetPixmap());
2044 else if (m_backgroundBrush
.IsHatch())
2046 gdk_gc_set_fill( m_bgGC
, GDK_STIPPLED
);
2047 gdk_gc_set_stipple(m_bgGC
, GetHatch(m_backgroundBrush
.GetStyle()));
2051 void wxWindowDCImpl::SetLogicalFunction( int function
)
2053 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2055 if (m_logicalFunction
== function
)
2058 // VZ: shouldn't this be a CHECK?
2065 case wxXOR
: mode
= GDK_XOR
; break;
2066 case wxINVERT
: mode
= GDK_INVERT
; break;
2067 case wxOR_REVERSE
: mode
= GDK_OR_REVERSE
; break;
2068 case wxAND_REVERSE
: mode
= GDK_AND_REVERSE
; break;
2069 case wxCLEAR
: mode
= GDK_CLEAR
; break;
2070 case wxSET
: mode
= GDK_SET
; break;
2071 case wxOR_INVERT
: mode
= GDK_OR_INVERT
; break;
2072 case wxAND
: mode
= GDK_AND
; break;
2073 case wxOR
: mode
= GDK_OR
; break;
2074 case wxEQUIV
: mode
= GDK_EQUIV
; break;
2075 case wxNAND
: mode
= GDK_NAND
; break;
2076 case wxAND_INVERT
: mode
= GDK_AND_INVERT
; break;
2077 case wxCOPY
: mode
= GDK_COPY
; break;
2078 case wxNO_OP
: mode
= GDK_NOOP
; break;
2079 case wxSRC_INVERT
: mode
= GDK_COPY_INVERT
; break;
2080 case wxNOR
: mode
= GDK_NOR
; break;
2082 wxFAIL_MSG( wxT("unsupported logical function") );
2086 m_logicalFunction
= function
;
2088 gdk_gc_set_function( m_penGC
, mode
);
2089 gdk_gc_set_function( m_brushGC
, mode
);
2091 // to stay compatible with wxMSW, we don't apply ROPs to the text
2092 // operations (i.e. DrawText/DrawRotatedText).
2093 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2094 gdk_gc_set_function( m_textGC
, mode
);
2097 void wxWindowDCImpl::SetTextForeground( const wxColour
&col
)
2099 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2101 // don't set m_textForegroundColour to an invalid colour as we'd crash
2102 // later then (we use m_textForegroundColour.GetColor() without checking
2104 if ( !col
.IsOk() || (m_textForegroundColour
== col
) )
2107 m_textForegroundColour
= col
;
2111 m_textForegroundColour
.CalcPixel( m_cmap
);
2112 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
2116 void wxWindowDCImpl::SetTextBackground( const wxColour
&col
)
2118 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2121 if ( !col
.IsOk() || (m_textBackgroundColour
== col
) )
2124 m_textBackgroundColour
= col
;
2128 m_textBackgroundColour
.CalcPixel( m_cmap
);
2129 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
2133 void wxWindowDCImpl::SetBackgroundMode( int mode
)
2135 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2137 m_backgroundMode
= mode
;
2140 void wxWindowDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
2142 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
2145 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2147 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2149 if (!m_gdkwindow
) return;
2152 rect
.x
= XLOG2DEV(x
);
2153 rect
.y
= YLOG2DEV(y
);
2154 rect
.width
= XLOG2DEVREL(width
);
2155 rect
.height
= YLOG2DEVREL(height
);
2157 if (m_window
&& m_window
->m_wxwindow
&&
2158 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2160 rect
.x
-= rect
.width
;
2163 DoSetDeviceClippingRegion(wxRegion(rect
));
2166 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
2168 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2172 DestroyClippingRegion();
2176 if (!m_gdkwindow
) return;
2178 if (!m_currentClippingRegion
.IsNull())
2179 m_currentClippingRegion
.Intersect( region
);
2181 m_currentClippingRegion
.Union( region
);
2183 #if USE_PAINT_REGION
2184 if (!m_paintClippingRegion
.IsNull())
2185 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2188 wxCoord xx
, yy
, ww
, hh
;
2189 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2190 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2192 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2193 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2194 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2195 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2196 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2199 void wxWindowDCImpl::DestroyClippingRegion()
2201 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2203 wxDCImpl::DestroyClippingRegion();
2205 m_currentClippingRegion
.Clear();
2207 #if USE_PAINT_REGION
2208 if (!m_paintClippingRegion
.IsEmpty())
2209 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2212 if (!m_gdkwindow
) return;
2214 GdkRegion
* gdkRegion
= NULL
;
2215 if (!m_currentClippingRegion
.IsEmpty())
2216 gdkRegion
= m_currentClippingRegion
.GetRegion();
2218 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2219 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2220 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2221 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2224 void wxWindowDCImpl::Destroy()
2226 if (m_penGC
) wxFreePoolGC( m_penGC
);
2227 m_penGC
= (GdkGC
*) NULL
;
2228 if (m_brushGC
) wxFreePoolGC( m_brushGC
);
2229 m_brushGC
= (GdkGC
*) NULL
;
2230 if (m_textGC
) wxFreePoolGC( m_textGC
);
2231 m_textGC
= (GdkGC
*) NULL
;
2232 if (m_bgGC
) wxFreePoolGC( m_bgGC
);
2233 m_bgGC
= (GdkGC
*) NULL
;
2236 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
2238 m_deviceOriginX
= x
;
2239 m_deviceOriginY
= y
;
2241 ComputeScaleAndOrigin();
2244 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
2246 m_signX
= (xLeftRight
? 1 : -1);
2247 m_signY
= (yBottomUp
? -1 : 1);
2249 if (m_window
&& m_window
->m_wxwindow
&&
2250 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2253 ComputeScaleAndOrigin();
2256 void wxWindowDCImpl::ComputeScaleAndOrigin()
2258 const wxRealPoint
origScale(m_scaleX
, m_scaleY
);
2260 wxDCImpl::ComputeScaleAndOrigin();
2262 // if scale has changed call SetPen to recalulate the line width
2263 if ( wxRealPoint(m_scaleX
, m_scaleY
) != origScale
&& m_pen
.IsOk() )
2265 // this is a bit artificial, but we need to force wxDC to think the pen
2273 // Resolution in pixels per logical inch
2274 wxSize
wxWindowDCImpl::GetPPI() const
2276 return wxSize( (int) (m_mm_to_pix_x
* 25.4 + 0.5), (int) (m_mm_to_pix_y
* 25.4 + 0.5));
2279 int wxWindowDCImpl::GetDepth() const
2281 return gdk_drawable_get_depth(m_gdkwindow
);
2285 //-----------------------------------------------------------------------------
2287 //-----------------------------------------------------------------------------
2289 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
2291 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
2292 : wxWindowDCImpl( owner
)
2296 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*win
)
2297 : wxWindowDCImpl( owner
, win
)
2299 wxCHECK_RET( win
, _T("NULL window in wxClientDCImpl::wxClientDC") );
2301 #ifdef __WXUNIVERSAL__
2302 wxPoint ptOrigin
= win
->GetClientAreaOrigin();
2303 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
2304 wxSize size
= win
->GetClientSize();
2305 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
2310 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
2312 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
2314 m_window
->GetClientSize( width
, height
);
2317 //-----------------------------------------------------------------------------
2319 //-----------------------------------------------------------------------------
2321 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
2323 // Limit the paint region to the window size. Sometimes
2324 // the paint region is too big, and this risks X11 errors
2325 static void wxLimitRegionToSize(wxRegion
& region
, const wxSize
& sz
)
2327 wxRect originalRect
= region
.GetBox();
2328 wxRect
rect(originalRect
);
2329 if (rect
.width
+ rect
.x
> sz
.x
)
2330 rect
.width
= sz
.x
- rect
.x
;
2331 if (rect
.height
+ rect
.y
> sz
.y
)
2332 rect
.height
= sz
.y
- rect
.y
;
2333 if (rect
!= originalRect
)
2335 region
= wxRegion(rect
);
2336 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2337 originalRect
.x
, originalRect
.y
, originalRect
.width
, originalRect
.height
,
2338 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2342 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
2343 : wxClientDCImpl( owner
)
2347 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*win
)
2348 : wxClientDCImpl( owner
, win
)
2350 #if USE_PAINT_REGION
2351 if (!win
->m_clipPaintRegion
)
2354 wxSize sz
= win
->GetSize();
2355 m_paintClippingRegion
= win
->m_nativeUpdateRegion
;
2356 wxLimitRegionToSize(m_paintClippingRegion
, sz
);
2358 GdkRegion
*region
= m_paintClippingRegion
.GetRegion();
2361 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2362 wxLimitRegionToSize(m_currentClippingRegion
, sz
);
2364 if (sz
.x
<= 0 || sz
.y
<= 0)
2367 gdk_gc_set_clip_region( m_penGC
, region
);
2368 gdk_gc_set_clip_region( m_brushGC
, region
);
2369 gdk_gc_set_clip_region( m_textGC
, region
);
2370 gdk_gc_set_clip_region( m_bgGC
, region
);
2375 // ----------------------------------------------------------------------------
2377 // ----------------------------------------------------------------------------
2379 class wxDCModule
: public wxModule
2386 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2389 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2391 bool wxDCModule::OnInit()
2397 void wxDCModule::OnExit()
2401 for (int i
= wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
; i
--; )
2404 g_object_unref(hatches
[i
]);