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
)
74 // This macro creates a bitmap from an XBM file included above. Notice
75 // the need for the cast because gdk_bitmap_create_from_data() doesn't
76 // accept unsigned data but the arrays in XBM need to be unsigned to
77 // avoid warnings (and even errors in C+0x mode) from g++.
78 #define CREATE_FROM_XBM_DATA(name) \
79 gdk_bitmap_create_from_data \
82 reinterpret_cast<gchar *>(name ## _bits), \
89 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
90 hatches
[i
] = CREATE_FROM_XBM_DATA(bdiag
);
92 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
93 hatches
[i
] = CREATE_FROM_XBM_DATA(cdiag
);
95 case wxBRUSHSTYLE_CROSS_HATCH
:
96 hatches
[i
] = CREATE_FROM_XBM_DATA(cross
);
98 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
99 hatches
[i
] = CREATE_FROM_XBM_DATA(fdiag
);
101 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
102 hatches
[i
] = CREATE_FROM_XBM_DATA(horiz
);
104 case wxBRUSHSTYLE_VERTICAL_HATCH
:
105 hatches
[i
] = CREATE_FROM_XBM_DATA(verti
);
109 #undef CREATE_FROM_XBM_DATA
114 //-----------------------------------------------------------------------------
115 // Implement Pool of Graphic contexts. Creating them takes too much time.
116 //-----------------------------------------------------------------------------
146 #define GC_POOL_ALLOC_SIZE 100
148 static int wxGCPoolSize
= 0;
150 static wxGC
*wxGCPool
= NULL
;
152 static void wxInitGCPool()
154 // This really could wait until the first call to
155 // wxGetPoolGC, but we will make the first allocation
156 // now when other initialization is being performed.
158 // Set initial pool size.
159 wxGCPoolSize
= GC_POOL_ALLOC_SIZE
;
161 // Allocate initial pool.
162 wxGCPool
= (wxGC
*)malloc(wxGCPoolSize
* sizeof(wxGC
));
163 if (wxGCPool
== NULL
)
165 // If we cannot malloc, then fail with error
166 // when debug is enabled. If debug is not enabled,
167 // the problem will eventually get caught
169 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
173 // Zero initial pool.
174 memset(wxGCPool
, 0, wxGCPoolSize
* sizeof(wxGC
));
177 static void wxCleanUpGCPool()
179 for (int i
= 0; i
< wxGCPoolSize
; i
++)
181 if (wxGCPool
[i
].m_gc
)
182 g_object_unref (wxGCPool
[i
].m_gc
);
190 static GdkGC
* wxGetPoolGC( GdkWindow
*window
, wxPoolGCType type
)
194 // Look for an available GC.
195 for (int i
= 0; i
< wxGCPoolSize
; i
++)
197 if (!wxGCPool
[i
].m_gc
)
199 wxGCPool
[i
].m_gc
= gdk_gc_new( window
);
200 gdk_gc_set_exposures( wxGCPool
[i
].m_gc
, FALSE
);
201 wxGCPool
[i
].m_type
= type
;
202 wxGCPool
[i
].m_used
= false;
204 if ((!wxGCPool
[i
].m_used
) && (wxGCPool
[i
].m_type
== type
))
206 wxGCPool
[i
].m_used
= true;
207 return wxGCPool
[i
].m_gc
;
211 // We did not find an available GC.
212 // We need to grow the GC pool.
213 pptr
= (wxGC
*)realloc(wxGCPool
,
214 (wxGCPoolSize
+ GC_POOL_ALLOC_SIZE
)*sizeof(wxGC
));
217 // Initialize newly allocated pool.
219 memset(&wxGCPool
[wxGCPoolSize
], 0,
220 GC_POOL_ALLOC_SIZE
*sizeof(wxGC
));
222 // Initialize entry we will return.
223 wxGCPool
[wxGCPoolSize
].m_gc
= gdk_gc_new( window
);
224 gdk_gc_set_exposures( wxGCPool
[wxGCPoolSize
].m_gc
, FALSE
);
225 wxGCPool
[wxGCPoolSize
].m_type
= type
;
226 wxGCPool
[wxGCPoolSize
].m_used
= true;
228 // Set new value of pool size.
229 wxGCPoolSize
+= GC_POOL_ALLOC_SIZE
;
231 // Return newly allocated entry.
232 return wxGCPool
[wxGCPoolSize
-GC_POOL_ALLOC_SIZE
].m_gc
;
235 // The realloc failed. Fall through to error.
236 wxFAIL_MSG( wxT("No GC available") );
241 static void wxFreePoolGC( GdkGC
*gc
)
243 for (int i
= 0; i
< wxGCPoolSize
; i
++)
245 if (wxGCPool
[i
].m_gc
== gc
)
247 wxGCPool
[i
].m_used
= false;
252 wxFAIL_MSG( wxT("Wrong GC") );
255 //-----------------------------------------------------------------------------
257 //-----------------------------------------------------------------------------
259 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxGTKDCImpl
)
261 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
270 m_isScreenDC
= false;
276 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
279 wxASSERT_MSG( window
, wxT("DC needs a window") );
287 m_isScreenDC
= false;
288 m_font
= window
->GetFont();
290 GtkWidget
*widget
= window
->m_wxwindow
;
291 m_gdkwindow
= window
->GTKGetDrawingWindow();
293 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
294 // code should still be able to create wxClientDCs for them
297 widget
= window
->m_widget
;
299 wxCHECK_RET(widget
, "DC needs a widget");
301 m_gdkwindow
= widget
->window
;
302 if (!gtk_widget_get_has_window(widget
))
303 SetDeviceLocalOrigin(widget
->allocation
.x
, widget
->allocation
.y
);
306 m_context
= window
->GTKGetPangoDefaultContext();
307 m_layout
= pango_layout_new( m_context
);
308 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
310 // Window not realized ?
313 // Don't report problems as per MSW.
319 m_cmap
= gtk_widget_get_colormap(widget
);
323 /* this must be done after SetUpDC, bacause SetUpDC calls the
324 repective SetBrush, SetPen, SetBackground etc functions
325 to set up the DC. SetBackground call m_owner->SetBackground
326 and this might not be desired as the standard dc background
327 is white whereas a window might assume gray to be the
328 standard (as e.g. wxStatusBar) */
332 if (m_window
&& m_window
->m_wxwindow
&&
333 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
338 // origin in the upper right corner
339 m_deviceOriginX
= m_window
->GetClientSize().x
;
343 wxWindowDCImpl::~wxWindowDCImpl()
348 g_object_unref (m_layout
);
350 pango_font_description_free( m_fontdesc
);
353 void wxWindowDCImpl::SetUpDC( bool isMemDC
)
357 wxASSERT_MSG( !m_penGC
, wxT("GCs already created") );
361 if ((isMemDC
) && (GetSelectedBitmap().IsOk()))
363 if (GetSelectedBitmap().GetDepth() == 1)
365 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_MONO
);
366 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_MONO
);
367 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_MONO
);
368 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_MONO
);
377 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_SCREEN
);
378 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_SCREEN
);
379 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_SCREEN
);
380 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_SCREEN
);
382 else if (m_cmap
== gdk_screen_get_rgba_colormap(gdk_colormap_get_screen(m_cmap
)))
384 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR_ALPHA
);
385 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR_ALPHA
);
386 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR_ALPHA
);
387 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR_ALPHA
);
391 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR
);
392 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR
);
393 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR
);
394 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR
);
398 /* background colour */
399 m_backgroundBrush
= *wxWHITE_BRUSH
;
400 m_backgroundBrush
.GetColour().CalcPixel( m_cmap
);
401 const GdkColor
*bg_col
= m_backgroundBrush
.GetColour().GetColor();
404 m_textForegroundColour
.CalcPixel( m_cmap
);
405 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
407 m_textBackgroundColour
.CalcPixel( m_cmap
);
408 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
410 gdk_gc_set_fill( m_textGC
, GDK_SOLID
);
412 gdk_gc_set_colormap( m_textGC
, m_cmap
);
415 m_pen
.GetColour().CalcPixel( m_cmap
);
416 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
417 gdk_gc_set_background( m_penGC
, bg_col
);
419 gdk_gc_set_line_attributes( m_penGC
, 0, GDK_LINE_SOLID
, GDK_CAP_NOT_LAST
, GDK_JOIN_ROUND
);
422 m_brush
.GetColour().CalcPixel( m_cmap
);
423 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
424 gdk_gc_set_background( m_brushGC
, bg_col
);
426 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
429 gdk_gc_set_background( m_bgGC
, bg_col
);
430 gdk_gc_set_foreground( m_bgGC
, bg_col
);
432 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
435 gdk_gc_set_function( m_textGC
, GDK_COPY
);
436 gdk_gc_set_function( m_brushGC
, GDK_COPY
);
437 gdk_gc_set_function( m_penGC
, GDK_COPY
);
440 gdk_gc_set_clip_rectangle( m_penGC
, NULL
);
441 gdk_gc_set_clip_rectangle( m_brushGC
, NULL
);
442 gdk_gc_set_clip_rectangle( m_textGC
, NULL
);
443 gdk_gc_set_clip_rectangle( m_bgGC
, NULL
);
446 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
448 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
450 m_window
->GetSize(width
, height
);
453 bool wxWindowDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
454 const wxColour
& col
, wxFloodFillStyle style
)
457 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
458 const wxColour
& col
, wxFloodFillStyle style
);
460 return wxDoFloodFill( GetOwner(), x
, y
, col
, style
);
471 bool wxWindowDCImpl::DoGetPixel( wxCoord x1
, wxCoord y1
, wxColour
*col
) const
473 GdkImage
* image
= NULL
;
476 const int x
= LogicalToDeviceX(x1
);
477 const int y
= LogicalToDeviceY(y1
);
479 gdk_drawable_get_size(m_gdkwindow
, &rect
.width
, &rect
.height
);
480 if (rect
.Contains(x
, y
))
481 image
= gdk_drawable_get_image(m_gdkwindow
, x
, y
, 1, 1);
488 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
489 const unsigned pixel
= gdk_image_get_pixel(image
, 0, 0);
490 if (colormap
== NULL
)
491 *col
= pixel
? m_textForegroundColour
: m_textBackgroundColour
;
495 gdk_colormap_query_color(colormap
, pixel
, &c
);
496 col
->Set(c
.red
>> 8, c
.green
>> 8, c
.blue
>> 8);
498 g_object_unref(image
);
502 void wxWindowDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
504 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
506 if ( m_pen
.IsNonTransparent() )
509 gdk_draw_line( m_gdkwindow
, m_penGC
, XLOG2DEV(x1
), YLOG2DEV(y1
), XLOG2DEV(x2
), YLOG2DEV(y2
) );
511 CalcBoundingBox(x1
, y1
);
512 CalcBoundingBox(x2
, y2
);
516 void wxWindowDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
518 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
520 if ( m_pen
.IsNonTransparent() )
524 GetOwner()->GetSize( &w
, &h
);
525 wxCoord xx
= XLOG2DEV(x
);
526 wxCoord yy
= YLOG2DEV(y
);
529 gdk_draw_line( m_gdkwindow
, m_penGC
, 0, yy
, XLOG2DEVREL(w
), yy
);
530 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, 0, xx
, YLOG2DEVREL(h
) );
535 void wxWindowDCImpl::DrawingSetup(GdkGC
*& gc
, bool& originChanged
)
538 GdkPixmap
* pixmap
= NULL
;
539 const int style
= m_brush
.GetStyle();
541 if (style
== wxBRUSHSTYLE_STIPPLE
|| style
== wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
)
543 const wxBitmap
* stipple
= m_brush
.GetStipple();
546 if (style
== wxBRUSHSTYLE_STIPPLE
)
547 pixmap
= stipple
->GetPixmap();
548 else if (stipple
->GetMask())
550 pixmap
= stipple
->GetPixmap();
555 else if (m_brush
.IsHatch())
557 pixmap
= GetHatch(style
);
565 gdk_drawable_get_size(pixmap
, &w
, &h
);
566 origin_x
= m_deviceOriginX
% w
;
567 origin_y
= m_deviceOriginY
% h
;
570 originChanged
= origin_x
|| origin_y
;
572 gdk_gc_set_ts_origin(gc
, origin_x
, origin_y
);
575 void wxWindowDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
,
576 wxCoord xc
, wxCoord yc
)
578 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
580 wxCoord xx1
= XLOG2DEV(x1
);
581 wxCoord yy1
= YLOG2DEV(y1
);
582 wxCoord xx2
= XLOG2DEV(x2
);
583 wxCoord yy2
= YLOG2DEV(y2
);
584 wxCoord xxc
= XLOG2DEV(xc
);
585 wxCoord yyc
= YLOG2DEV(yc
);
586 double dx
= xx1
- xxc
;
587 double dy
= yy1
- yyc
;
588 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
589 wxCoord r
= (wxCoord
)radius
;
590 double radius1
, radius2
;
592 if (xx1
== xx2
&& yy1
== yy2
)
597 else if ( wxIsNullDouble(radius
) )
604 radius1
= (xx1
- xxc
== 0) ?
605 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
606 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
607 radius2
= (xx2
- xxc
== 0) ?
608 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
609 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
611 wxCoord alpha1
= wxCoord(radius1
* 64.0);
612 wxCoord alpha2
= wxCoord((radius2
- radius1
) * 64.0);
613 while (alpha2
<= 0) alpha2
+= 360*64;
614 while (alpha1
> 360*64) alpha1
-= 360*64;
618 if ( m_brush
.IsNonTransparent() )
622 DrawingSetup(gc
, originChanged
);
624 gdk_draw_arc(m_gdkwindow
, gc
, true, xxc
-r
, yyc
-r
, 2*r
, 2*r
, alpha1
, alpha2
);
627 gdk_gc_set_ts_origin(gc
, 0, 0);
630 if ( m_pen
.IsNonTransparent() )
632 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xxc
-r
, yyc
-r
, 2*r
,2*r
, alpha1
, alpha2
);
634 if ( m_brush
.IsNonTransparent() && (alpha2
- alpha1
!= 360*64) )
636 gdk_draw_line( m_gdkwindow
, m_penGC
, xx1
, yy1
, xxc
, yyc
);
637 gdk_draw_line( m_gdkwindow
, m_penGC
, xxc
, yyc
, xx2
, yy2
);
642 CalcBoundingBox (x1
, y1
);
643 CalcBoundingBox (x2
, y2
);
646 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double sa
, double ea
)
648 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
650 wxCoord xx
= XLOG2DEV(x
);
651 wxCoord yy
= YLOG2DEV(y
);
652 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
653 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
655 // CMB: handle -ve width and/or height
656 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
657 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
661 wxCoord start
= wxCoord(sa
* 64.0);
662 wxCoord end
= wxCoord((ea
-sa
) * 64.0);
664 if ( m_brush
.IsNonTransparent() )
668 DrawingSetup(gc
, originChanged
);
670 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, start
, end
);
673 gdk_gc_set_ts_origin(gc
, 0, 0);
676 if ( m_pen
.IsNonTransparent() )
677 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, start
, end
);
680 CalcBoundingBox (x
, y
);
681 CalcBoundingBox (x
+ width
, y
+ height
);
684 void wxWindowDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
686 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
688 if ( m_pen
.IsNonTransparent() && m_gdkwindow
)
689 gdk_draw_point( m_gdkwindow
, m_penGC
, XLOG2DEV(x
), YLOG2DEV(y
) );
691 CalcBoundingBox (x
, y
);
694 void wxWindowDCImpl::DoDrawLines( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
696 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
700 if ( m_pen
.IsTransparent() )
703 //Check, if scaling is necessary
705 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
707 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
708 GdkPoint
* gpts
= reinterpret_cast<GdkPoint
*>(points
);
711 gpts
= new GdkPoint
[n
];
713 for (int i
= 0; i
< n
; i
++)
717 gpts
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
718 gpts
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
720 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
724 gdk_draw_lines( m_gdkwindow
, m_penGC
, gpts
, n
);
730 void wxWindowDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
731 wxCoord xoffset
, wxCoord yoffset
,
732 wxPolygonFillMode
WXUNUSED(fillStyle
) )
734 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
738 //Check, if scaling is necessary
740 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
742 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
743 GdkPoint
* gdkpoints
= reinterpret_cast<GdkPoint
*>(points
);
746 gdkpoints
= new GdkPoint
[n
];
749 for (i
= 0 ; i
< n
; i
++)
753 gdkpoints
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
754 gdkpoints
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
756 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
761 if ( m_brush
.IsNonTransparent() )
765 DrawingSetup(gc
, originChanged
);
767 gdk_draw_polygon(m_gdkwindow
, gc
, true, gdkpoints
, n
);
770 gdk_gc_set_ts_origin(gc
, 0, 0);
773 if ( m_pen
.IsNonTransparent() )
776 for (i = 0 ; i < n ; i++)
778 gdk_draw_line( m_gdkwindow, m_penGC,
781 gdkpoints[(i+1)%n].x,
782 gdkpoints[(i+1)%n].y);
785 gdk_draw_polygon( m_gdkwindow
, m_penGC
, FALSE
, gdkpoints
, n
);
794 void wxWindowDCImpl::DoDrawRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
796 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
798 wxCoord xx
= XLOG2DEV(x
);
799 wxCoord yy
= YLOG2DEV(y
);
800 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
801 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
803 // CMB: draw nothing if transformed w or h is 0
804 if (ww
== 0 || hh
== 0) return;
806 // CMB: handle -ve width and/or height
807 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
808 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
812 if ( m_brush
.IsNonTransparent() )
816 DrawingSetup(gc
, originChanged
);
818 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
);
821 gdk_gc_set_ts_origin(gc
, 0, 0);
824 if ( m_pen
.IsNonTransparent() )
826 if ((m_pen
.GetWidth() == 2) && (m_pen
.GetCap() == wxCAP_ROUND
) &&
827 (m_pen
.GetJoin() == wxJOIN_ROUND
) && (m_pen
.GetStyle() == wxPENSTYLE_SOLID
))
829 // Use 2 1-line rects instead
830 gdk_gc_set_line_attributes( m_penGC
, 1, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
835 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
+1, yy
, ww
-2, hh
-2 );
836 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
-1, ww
, hh
);
840 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-2, hh
-2 );
841 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
-1, yy
-1, ww
, hh
);
845 gdk_gc_set_line_attributes( m_penGC
, 2, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
849 // Just use X11 for other cases
850 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-1, hh
-1 );
855 CalcBoundingBox( x
, y
);
856 CalcBoundingBox( x
+ width
, y
+ height
);
859 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
861 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
863 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
865 wxCoord xx
= XLOG2DEV(x
);
866 wxCoord yy
= YLOG2DEV(y
);
867 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
868 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
869 wxCoord rr
= XLOG2DEVREL((wxCoord
)radius
);
871 // CMB: handle -ve width and/or height
872 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
873 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
875 // CMB: if radius is zero use DrawRectangle() instead to avoid
876 // X drawing errors with small radii
879 DoDrawRectangle( x
, y
, width
, height
);
883 // CMB: draw nothing if transformed w or h is 0
884 if (ww
== 0 || hh
== 0) return;
886 // CMB: adjust size if outline is drawn otherwise the result is
887 // 1 pixel too wide and high
888 if ( m_pen
.IsNonTransparent() )
896 // CMB: ensure dd is not larger than rectangle otherwise we
897 // get an hour glass shape
899 if (dd
> ww
) dd
= ww
;
900 if (dd
> hh
) dd
= hh
;
903 if ( m_brush
.IsNonTransparent() )
907 DrawingSetup(gc
, originChanged
);
909 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
+rr
, yy
, ww
-dd
+1, hh
);
910 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
+rr
, ww
, hh
-dd
+1);
911 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, dd
, dd
, 90*64, 90*64);
912 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64);
913 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64);
914 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64);
917 gdk_gc_set_ts_origin(gc
, 0, 0);
920 if ( m_pen
.IsNonTransparent() )
922 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
, xx
+ww
-rr
, yy
);
923 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
+hh
, xx
+ww
-rr
, yy
+hh
);
924 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, yy
+rr
+1, xx
, yy
+hh
-rr
);
925 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+ww
, yy
+rr
+1, xx
+ww
, yy
+hh
-rr
);
926 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, dd
, dd
, 90*64, 90*64 );
927 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64 );
928 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64 );
929 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64 );
933 // this ignores the radius
934 CalcBoundingBox( x
, y
);
935 CalcBoundingBox( x
+ width
, y
+ height
);
938 void wxWindowDCImpl::DoDrawEllipse( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
940 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
942 wxCoord xx
= XLOG2DEV(x
);
943 wxCoord yy
= YLOG2DEV(y
);
944 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
945 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
947 // CMB: handle -ve width and/or height
948 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
949 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
953 if ( m_brush
.IsNonTransparent() )
957 DrawingSetup(gc
, originChanged
);
959 // If the pen is transparent pen we increase the size
960 // for better compatibility with other platforms.
961 if ( m_pen
.IsNonTransparent() )
967 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, 0, 360*64);
970 gdk_gc_set_ts_origin(gc
, 0, 0);
973 if ( m_pen
.IsNonTransparent() )
974 gdk_draw_arc( m_gdkwindow
, m_penGC
, false, xx
, yy
, ww
, hh
, 0, 360*64 );
977 CalcBoundingBox( x
, y
);
978 CalcBoundingBox( x
+ width
, y
+ height
);
981 void wxWindowDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
983 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
984 DoDrawBitmap( (const wxBitmap
&)icon
, x
, y
, true );
987 // scale a pixbuf, return new pixbuf, unref old one
989 Scale(GdkPixbuf
* pixbuf
, int dst_w
, int dst_h
, double sx
, double sy
)
991 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
992 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, dst_w
, dst_h
);
993 gdk_pixbuf_scale(pixbuf
, pixbuf_scaled
,
994 0, 0, dst_w
, dst_h
, 0, 0, sx
, sy
, GDK_INTERP_NEAREST
);
995 g_object_unref(pixbuf
);
996 return pixbuf_scaled
;
999 // scale part of a pixmap using pixbuf scaling, return pixbuf
1001 Scale(GdkPixmap
* pixmap
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
1003 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(
1004 NULL
, pixmap
, NULL
, x
, y
, 0, 0, w
, h
);
1005 return Scale(pixbuf
, dst_w
, dst_h
, sx
, sy
);
1008 // scale part of a mask pixmap, return new mask, unref old one
1010 ScaleMask(GdkPixmap
* mask
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
1012 GdkPixbuf
* pixbuf
= Scale(mask
, x
, y
, w
, h
, dst_w
, dst_h
, sx
, sy
);
1014 // convert black and white pixbuf back to a mono pixmap
1015 const unsigned out_rowstride
= (dst_w
+ 7) / 8;
1016 const size_t data_size
= out_rowstride
* size_t(dst_h
);
1017 char* data
= new char[data_size
];
1019 const guchar
* row
= gdk_pixbuf_get_pixels(pixbuf
);
1020 const int rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
1021 memset(data
, 0, data_size
);
1022 for (int j
= 0; j
< dst_h
; j
++, row
+= rowstride
, out
+= out_rowstride
)
1024 const guchar
* in
= row
;
1025 for (int i
= 0; i
< dst_w
; i
++, in
+= 3)
1027 out
[i
>> 3] |= 1 << (i
& 7);
1029 g_object_unref(pixbuf
);
1030 GdkPixmap
* pixmap
= gdk_bitmap_create_from_data(mask
, data
, dst_w
, dst_h
);
1032 g_object_unref(mask
);
1036 // Make a new mask from part of a mask and a clip region.
1037 // Return new mask, unref old one.
1039 ClipMask(GdkPixmap
* mask
, GdkRegion
* clipRegion
, int x
, int y
, int dst_x
, int dst_y
, int w
, int h
)
1041 GdkGCValues gcValues
;
1042 gcValues
.foreground
.pixel
= 0;
1043 GdkGC
* gc
= gdk_gc_new_with_values(mask
, &gcValues
, GDK_GC_FOREGROUND
);
1044 GdkPixmap
* pixmap
= gdk_pixmap_new(mask
, w
, h
, 1);
1045 // clear new mask, so clipped areas will be masked
1046 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1047 gdk_gc_set_clip_region(gc
, clipRegion
);
1048 gdk_gc_set_clip_origin(gc
, -dst_x
, -dst_y
);
1049 // draw old mask onto new one, with clip
1050 gdk_draw_drawable(pixmap
, gc
, mask
, x
, y
, 0, 0, w
, h
);
1052 g_object_unref(mask
);
1056 // make a color pixmap from part of a mono one, using text fg/bg colors
1058 wxWindowDCImpl::MonoToColor(GdkPixmap
* monoPixmap
, int x
, int y
, int w
, int h
) const
1060 GdkPixmap
* pixmap
= gdk_pixmap_new(m_gdkwindow
, w
, h
, -1);
1061 GdkGCValues gcValues
;
1062 gcValues
.foreground
.pixel
= m_textForegroundColour
.GetColor()->pixel
;
1063 gcValues
.background
.pixel
= m_textBackgroundColour
.GetColor()->pixel
;
1064 gcValues
.stipple
= monoPixmap
;
1065 gcValues
.fill
= GDK_OPAQUE_STIPPLED
;
1066 gcValues
.ts_x_origin
= -x
;
1067 gcValues
.ts_y_origin
= -y
;
1068 GdkGC
* gc
= gdk_gc_new_with_values(pixmap
, &gcValues
, GdkGCValuesMask(
1069 GDK_GC_FOREGROUND
| GDK_GC_BACKGROUND
| GDK_GC_STIPPLE
| GDK_GC_FILL
|
1070 GDK_GC_TS_X_ORIGIN
| GDK_GC_TS_Y_ORIGIN
));
1071 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1076 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap
&bitmap
,
1077 wxCoord x
, wxCoord y
,
1080 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1081 wxCHECK_RET( bitmap
.IsOk(), wxT("invalid bitmap") );
1083 if (!m_gdkwindow
) return;
1085 const int w
= bitmap
.GetWidth();
1086 const int h
= bitmap
.GetHeight();
1088 // notice that as the bitmap is not drawn upside down (or right to left)
1089 // even if the corresponding axis direction is inversed, we need to take it
1090 // into account when calculating its bounding box
1091 CalcBoundingBox(x
, y
);
1092 CalcBoundingBox(x
+ m_signX
*w
, y
+ m_signY
*h
);
1095 int xx
= LogicalToDeviceX(x
);
1096 const int yy
= LogicalToDeviceY(y
);
1097 const int ww
= LogicalToDeviceXRel(w
);
1098 const int hh
= LogicalToDeviceYRel(h
);
1100 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1103 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1104 // determine clip region overlap
1105 int overlap
= wxInRegion
;
1108 overlap
= m_currentClippingRegion
.Contains(xx
, yy
, ww
, hh
);
1109 if (overlap
== wxOutRegion
)
1113 const bool isScaled
= ww
!= w
|| hh
!= h
;
1114 const bool hasAlpha
= bitmap
.HasAlpha();
1115 GdkGC
* const use_gc
= m_penGC
;
1117 GdkPixmap
* mask
= NULL
;
1118 // mask does not work when drawing a pixbuf with alpha
1119 if (useMask
&& !hasAlpha
)
1121 wxMask
* m
= bitmap
.GetMask();
1123 mask
= m
->GetBitmap();
1129 mask
= ScaleMask(mask
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1130 if (overlap
== wxPartRegion
)
1132 // need a new mask that also masks the clipped area,
1133 // because gc can't have both a mask and a clip region
1134 mask
= ClipMask(mask
, clipRegion
, 0, 0, xx
, yy
, ww
, hh
);
1136 gdk_gc_set_clip_mask(use_gc
, mask
);
1137 gdk_gc_set_clip_origin(use_gc
, xx
, yy
);
1140 // determine whether to use pixmap or pixbuf
1141 GdkPixmap
* pixmap
= NULL
;
1142 GdkPixbuf
* pixbuf
= NULL
;
1143 if (bitmap
.HasPixmap())
1144 pixmap
= bitmap
.GetPixmap();
1145 if (pixmap
&& gdk_drawable_get_depth(pixmap
) == 1)
1147 // convert mono pixmap to color using text fg/bg colors
1148 pixmap
= MonoToColor(pixmap
, 0, 0, w
, h
);
1150 else if (hasAlpha
|| pixmap
== NULL
)
1153 pixbuf
= bitmap
.GetPixbuf();
1154 g_object_ref(pixbuf
);
1158 g_object_ref(pixmap
);
1164 pixbuf
= Scale(pixbuf
, ww
, hh
, m_scaleX
, m_scaleY
);
1166 pixbuf
= Scale(pixmap
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1171 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1172 0, 0, xx
, yy
, ww
, hh
, GDK_RGB_DITHER_NORMAL
, 0, 0);
1173 g_object_unref(pixbuf
);
1177 gdk_draw_drawable(m_gdkwindow
, use_gc
, pixmap
, 0, 0, xx
, yy
, ww
, hh
);
1181 g_object_unref(pixmap
);
1184 g_object_unref(mask
);
1185 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1189 bool wxWindowDCImpl::DoBlit( wxCoord xdest
, wxCoord ydest
,
1190 wxCoord width
, wxCoord height
,
1192 wxCoord xsrc
, wxCoord ysrc
,
1193 wxRasterOperationMode logical_func
,
1195 wxCoord xsrcMask
, wxCoord ysrcMask
)
1197 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1198 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1200 if (!m_gdkwindow
) return false;
1202 GdkDrawable
* srcDrawable
= NULL
;
1203 GdkPixmap
* mask
= NULL
;
1204 wxMemoryDC
* memDC
= wxDynamicCast(source
, wxMemoryDC
);
1207 const wxBitmap
& bitmap
= memDC
->GetSelectedBitmap();
1210 srcDrawable
= bitmap
.GetPixmap();
1213 wxMask
* m
= bitmap
.GetMask();
1215 mask
= m
->GetBitmap();
1220 wxDCImpl
* impl
= source
->GetImpl();
1221 wxWindowDCImpl
* gtk_impl
= wxDynamicCast(impl
, wxWindowDCImpl
);
1223 srcDrawable
= gtk_impl
->GetGDKWindow();
1224 if (srcDrawable
== NULL
)
1228 CalcBoundingBox(xdest
, ydest
);
1229 CalcBoundingBox(xdest
+ width
, ydest
+ height
);
1231 // source device coords
1232 int src_x
= source
->LogicalToDeviceX(xsrc
);
1233 int src_y
= source
->LogicalToDeviceY(ysrc
);
1234 int src_w
= source
->LogicalToDeviceXRel(width
);
1235 int src_h
= source
->LogicalToDeviceYRel(height
);
1237 // Clip source rect to source dc.
1238 // Only necessary when scaling, to avoid GDK errors when
1239 // converting to pixbuf, but no harm in always doing it.
1240 // If source rect changes, it also changes the dest rect.
1242 gdk_drawable_get_size(srcDrawable
, &clip
.width
, &clip
.height
);
1243 clip
.Intersect(wxRect(src_x
, src_y
, src_w
, src_h
));
1244 if (src_w
!= clip
.width
|| src_h
!= clip
.height
)
1246 if (clip
.width
== 0)
1250 src_h
= clip
.height
;
1251 width
= source
->DeviceToLogicalXRel(src_w
);
1252 height
= source
->DeviceToLogicalYRel(src_h
);
1253 if (src_x
!= clip
.x
|| src_y
!= clip
.y
)
1255 xdest
+= source
->DeviceToLogicalXRel(clip
.x
- src_x
);
1256 ydest
+= source
->DeviceToLogicalYRel(clip
.y
- src_y
);
1262 // destination device coords
1263 const int dst_x
= LogicalToDeviceX(xdest
);
1264 const int dst_y
= LogicalToDeviceY(ydest
);
1265 const int dst_w
= LogicalToDeviceXRel(width
);
1266 const int dst_h
= LogicalToDeviceYRel(height
);
1268 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1269 // determine dest clip region overlap
1270 int overlap
= wxInRegion
;
1273 overlap
= m_currentClippingRegion
.Contains(dst_x
, dst_y
, dst_w
, dst_h
);
1274 if (overlap
== wxOutRegion
)
1278 const bool isScaled
= src_w
!= dst_w
|| src_h
!= dst_h
;
1283 // get source to dest scale
1284 double usx
, usy
, lsx
, lsy
;
1285 source
->GetUserScale(&usx
, &usy
);
1286 source
->GetLogicalScale(&lsx
, &lsy
);
1287 scale_x
= m_scaleX
/ (usx
* lsx
);
1288 scale_y
= m_scaleY
/ (usy
* lsy
);
1291 GdkGC
* const use_gc
= m_penGC
;
1296 int srcMask_x
= src_x
;
1297 int srcMask_y
= src_y
;
1298 if (xsrcMask
!= -1 || ysrcMask
!= -1)
1300 srcMask_x
= source
->LogicalToDeviceX(xsrcMask
);
1301 srcMask_y
= source
->LogicalToDeviceY(ysrcMask
);
1305 mask
= ScaleMask(mask
, srcMask_x
, srcMask_y
,
1306 src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1310 if (overlap
== wxPartRegion
)
1312 // need a new mask that also masks the clipped area,
1313 // because gc can't have both a mask and a clip region
1314 mask
= ClipMask(mask
, clipRegion
,
1315 srcMask_x
, srcMask_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1319 gdk_gc_set_clip_mask(use_gc
, mask
);
1320 gdk_gc_set_clip_origin(use_gc
, dst_x
- srcMask_x
, dst_y
- srcMask_y
);
1323 GdkPixmap
* pixmap
= NULL
;
1324 if (gdk_drawable_get_depth(srcDrawable
) == 1)
1326 // Convert mono pixmap to color using text fg/bg colors.
1327 // Scaling/drawing is simpler if this is done first.
1328 pixmap
= MonoToColor(srcDrawable
, src_x
, src_y
, src_w
, src_h
);
1329 srcDrawable
= pixmap
;
1334 const wxRasterOperationMode logical_func_save
= m_logicalFunction
;
1335 SetLogicalFunction(logical_func
);
1337 gdk_gc_set_subwindow(use_gc
, GDK_INCLUDE_INFERIORS
);
1341 GdkPixbuf
* pixbuf
= Scale(srcDrawable
,
1342 src_x
, src_y
, src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1343 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1344 0, 0, dst_x
, dst_y
, dst_w
, dst_h
, GDK_RGB_DITHER_NONE
, 0, 0);
1345 g_object_unref(pixbuf
);
1349 gdk_draw_drawable(m_gdkwindow
, use_gc
, srcDrawable
,
1350 src_x
, src_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1353 SetLogicalFunction(logical_func_save
);
1355 gdk_gc_set_subwindow(use_gc
, GDK_CLIP_BY_CHILDREN
);
1358 g_object_unref(pixmap
);
1361 g_object_unref(mask
);
1362 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1367 void wxWindowDCImpl::DoDrawText(const wxString
& text
,
1371 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1373 if (!m_gdkwindow
) return;
1375 if (text
.empty()) return;
1377 wxCoord x
= XLOG2DEV(xLogical
),
1378 y
= YLOG2DEV(yLogical
);
1380 wxCHECK_RET( m_context
, wxT("no Pango context") );
1381 wxCHECK_RET( m_layout
, wxT("no Pango layout") );
1382 wxCHECK_RET( m_fontdesc
, wxT("no Pango font description") );
1384 gdk_pango_context_set_colormap( m_context
, m_cmap
); // not needed in gtk+ >= 2.6
1386 wxCharBuffer data
= wxGTK_CONV(text
);
1390 pango_layout_set_text(m_layout
, data
, data
.length());
1391 const bool setAttrs
= m_font
.GTKSetPangoAttrs(m_layout
);
1394 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1397 // If there is a user or actually any scale applied to
1398 // the device context, scale the font.
1400 // scale font description
1401 oldSize
= pango_font_description_get_size(m_fontdesc
);
1402 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1404 // actually apply scaled font
1405 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1409 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1413 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1416 const GdkColor
* bg_col
= NULL
;
1417 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1418 bg_col
= m_textBackgroundColour
.GetColor();
1420 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x_rtl
, y
, m_layout
, NULL
, bg_col
);
1424 // reset unscaled size
1425 pango_font_description_set_size( m_fontdesc
, oldSize
);
1427 // actually apply unscaled font
1428 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1432 // undo underline attributes setting:
1433 pango_layout_set_attributes(m_layout
, NULL
);
1436 CalcBoundingBox(xLogical
+ int(w
/ m_scaleX
), yLogical
+ int(h
/ m_scaleY
));
1437 CalcBoundingBox(xLogical
, yLogical
);
1440 // TODO: When GTK2.6 is required, merge DoDrawText and DoDrawRotatedText to
1441 // avoid code duplication
1442 void wxWindowDCImpl::DoDrawRotatedText( const wxString
&text
, wxCoord x
, wxCoord y
, double angle
)
1444 if (!m_gdkwindow
|| text
.empty())
1447 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1450 if (!gtk_check_version(2,6,0))
1455 pango_layout_set_text(m_layout
, wxGTK_CONV(text
), -1);
1456 const bool setAttrs
= m_font
.GTKSetPangoAttrs(m_layout
);
1458 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1461 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1462 // If there is a user or actually any scale applied to
1463 // the device context, scale the font.
1465 // scale font description
1466 oldSize
= pango_font_description_get_size(m_fontdesc
);
1467 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1469 // actually apply scaled font
1470 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1474 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1476 const GdkColor
* bg_col
= NULL
;
1477 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1478 bg_col
= m_textBackgroundColour
.GetColor();
1481 PangoMatrix matrix
= PANGO_MATRIX_INIT
;
1482 pango_matrix_rotate (&matrix
, angle
);
1483 pango_context_set_matrix (m_context
, &matrix
);
1484 pango_layout_context_changed (m_layout
);
1486 // To be compatible with MSW, the rotation axis must be in the old
1488 // Calculate the vertices of the rotated rectangle containing the text,
1489 // relative to the old top-left vertex.
1490 // We could use the matrix for this, but it's simpler with trignonometry.
1491 double rad
= DegToRad(angle
);
1492 // the rectangle vertices are counted clockwise with the first one
1494 double x2
= w
* cos(rad
);
1495 double y2
= -w
* sin(rad
); // y axis points to the bottom, hence minus
1496 double x4
= h
* sin(rad
);
1497 double y4
= h
* cos(rad
);
1498 double x3
= x4
+ x2
;
1499 double y3
= y4
+ y2
;
1500 // Then we calculate max and min of the rotated rectangle.
1501 wxCoord maxX
= (wxCoord
)(dmax(dmax(0, x2
), dmax(x3
, x4
)) + 0.5),
1502 maxY
= (wxCoord
)(dmax(dmax(0, y2
), dmax(y3
, y4
)) + 0.5),
1503 minX
= (wxCoord
)(dmin(dmin(0, x2
), dmin(x3
, x4
)) - 0.5),
1504 minY
= (wxCoord
)(dmin(dmin(0, y2
), dmin(y3
, y4
)) - 0.5);
1506 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x
+minX
, y
+minY
,
1507 m_layout
, NULL
, bg_col
);
1510 pango_layout_set_attributes(m_layout
, NULL
);
1512 // clean up the transformation matrix
1513 pango_context_set_matrix(m_context
, NULL
);
1517 // reset unscaled size
1518 pango_font_description_set_size( m_fontdesc
, oldSize
);
1520 // actually apply unscaled font
1521 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1524 CalcBoundingBox(x
+minX
, y
+minY
);
1525 CalcBoundingBox(x
+maxX
, y
+maxY
);
1528 #endif //__WXGTK26__
1531 if ( wxIsNullDouble(angle
) )
1533 DoDrawText(text
, x
, y
);
1540 // TODO: implement later without GdkFont for GTK 2.0
1541 DoGetTextExtent(text
, &w
, &h
, NULL
,NULL
, &m_font
);
1543 // draw the string normally
1546 dc
.SelectObject(src
);
1547 dc
.SetFont(GetFont());
1548 dc
.SetBackground(*wxBLACK_BRUSH
);
1549 dc
.SetBrush(*wxBLACK_BRUSH
);
1551 dc
.SetTextForeground( *wxWHITE
);
1552 dc
.DrawText(text
, 0, 0);
1553 dc
.SelectObject(wxNullBitmap
);
1555 // Calculate the size of the rotated bounding box.
1556 double rad
= DegToRad(angle
);
1557 double dx
= cos(rad
),
1560 // the rectngle vertices are counted clockwise with the first one being at
1561 // (0, 0) (or, rather, at (x, y))
1563 y2
= -w
*dy
; // y axis points to the bottom, hence minus
1566 double x3
= x4
+ x2
,
1570 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5),
1571 maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5),
1572 minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5),
1573 minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1576 wxImage image
= src
.ConvertToImage();
1578 image
.ConvertColourToAlpha( m_textForegroundColour
.Red(),
1579 m_textForegroundColour
.Green(),
1580 m_textForegroundColour
.Blue() );
1581 image
= image
.Rotate( rad
, wxPoint(0,0) );
1583 int i_angle
= (int) angle
;
1584 i_angle
= i_angle
% 360;
1588 if ((i_angle
>= 90.0) && (i_angle
< 270.0))
1589 xoffset
= image
.GetWidth();
1591 if ((i_angle
>= 0.0) && (i_angle
< 180.0))
1592 yoffset
= image
.GetHeight();
1594 if ((i_angle
>= 0) && (i_angle
< 90))
1595 yoffset
-= (int)( cos(rad
)*h
);
1596 if ((i_angle
>= 90) && (i_angle
< 180))
1597 xoffset
-= (int)( sin(rad
)*h
);
1598 if ((i_angle
>= 180) && (i_angle
< 270))
1599 yoffset
-= (int)( cos(rad
)*h
);
1600 if ((i_angle
>= 270) && (i_angle
< 360))
1601 xoffset
-= (int)( sin(rad
)*h
);
1603 int i_x
= x
- xoffset
;
1604 int i_y
= y
- yoffset
;
1607 DoDrawBitmap( src
, i_x
, i_y
, true );
1610 // it would be better to draw with non underlined font and draw the line
1611 // manually here (it would be more straight...)
1613 if ( m_font
.GetUnderlined() )
1615 gdk_draw_line( m_gdkwindow
, m_textGC
,
1616 XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
),
1617 XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1621 // update the bounding box
1622 CalcBoundingBox(x
+ minX
, y
+ minY
);
1623 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1624 #else // !wxUSE_IMAGE
1629 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
1633 void wxWindowDCImpl::DoGetTextExtent(const wxString
&string
,
1634 wxCoord
*width
, wxCoord
*height
,
1635 wxCoord
*descent
, wxCoord
*externalLeading
,
1636 const wxFont
*theFont
) const
1644 if ( externalLeading
)
1645 *externalLeading
= 0;
1650 // ensure that theFont is always non-NULL
1651 if ( !theFont
|| !theFont
->IsOk() )
1654 // and use it if it's valid
1655 if ( theFont
->IsOk() )
1657 pango_layout_set_font_description
1660 theFont
->GetNativeFontInfo()->description
1664 // Set layout's text
1665 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *theFont
);
1668 // hardly ideal, but what else can we do if conversion failed?
1672 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1675 pango_layout_get_pixel_size(m_layout
, width
, &h
);
1678 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1679 int baseline
= pango_layout_iter_get_baseline(iter
);
1680 pango_layout_iter_free(iter
);
1681 *descent
= h
- PANGO_PIXELS(baseline
);
1686 // Reset old font description
1687 if (theFont
->IsOk())
1688 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1692 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString
& text
,
1693 wxArrayInt
& widths
) const
1695 const size_t len
= text
.length();
1702 // Set layout's text
1703 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, m_font
);
1706 // hardly ideal, but what else can we do if conversion failed?
1707 wxLogLastError(wxT("DoGetPartialTextExtents"));
1711 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1713 // Calculate the position of each character based on the widths of
1714 // the previous characters
1716 // Code borrowed from Scintilla's PlatGTK
1717 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1719 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1721 while (pango_layout_iter_next_cluster(iter
))
1723 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1724 int position
= PANGO_PIXELS(pos
.x
);
1725 widths
[i
++] = position
;
1728 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
1729 pango_layout_iter_free(iter
);
1735 wxCoord
wxWindowDCImpl::GetCharWidth() const
1737 pango_layout_set_text( m_layout
, "H", 1 );
1739 pango_layout_get_pixel_size( m_layout
, &w
, NULL
);
1743 wxCoord
wxWindowDCImpl::GetCharHeight() const
1745 PangoFontMetrics
*metrics
= pango_context_get_metrics (m_context
, m_fontdesc
, pango_context_get_language(m_context
));
1746 wxCHECK_MSG( metrics
, -1, wxT("failed to get pango font metrics") );
1748 wxCoord h
= PANGO_PIXELS (pango_font_metrics_get_descent (metrics
) +
1749 pango_font_metrics_get_ascent (metrics
));
1750 pango_font_metrics_unref (metrics
);
1754 void wxWindowDCImpl::Clear()
1756 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1758 if (!m_gdkwindow
) return;
1761 DoGetSize( &width
, &height
);
1762 gdk_draw_rectangle( m_gdkwindow
, m_bgGC
, TRUE
, 0, 0, width
, height
);
1765 void wxWindowDCImpl::SetFont( const wxFont
&font
)
1772 pango_font_description_free( m_fontdesc
);
1774 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1779 PangoContext
*oldContext
= m_context
;
1781 m_context
= m_window
->GTKGetPangoDefaultContext();
1783 // If we switch back/forth between different contexts
1784 // we also have to create a new layout. I think so,
1785 // at least, and it doesn't hurt to do it.
1786 if (oldContext
!= m_context
)
1789 g_object_unref (m_layout
);
1791 m_layout
= pango_layout_new( m_context
);
1795 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1799 void wxWindowDCImpl::SetPen( const wxPen
&pen
)
1801 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1803 if (m_pen
== pen
) return;
1807 if (!m_pen
.IsOk()) return;
1809 if (!m_gdkwindow
) return;
1811 gint width
= m_pen
.GetWidth();
1814 // CMB: if width is non-zero scale it with the dc
1819 // X doesn't allow different width in x and y and so we take
1822 ( fabs((double) XLOG2DEVREL(width
)) +
1823 fabs((double) YLOG2DEVREL(width
)) ) / 2.0;
1827 // width can't be 0 or an internal GTK error occurs inside
1828 // gdk_gc_set_dashes() below
1833 static const wxGTKDash dotted
[] = {1, 1};
1834 static const wxGTKDash short_dashed
[] = {2, 2};
1835 static const wxGTKDash wxCoord_dashed
[] = {2, 4};
1836 static const wxGTKDash dotted_dashed
[] = {3, 3, 1, 3};
1838 // We express dash pattern in pen width unit, so we are
1839 // independent of zoom factor and so on...
1841 const wxGTKDash
*req_dash
;
1843 GdkLineStyle lineStyle
= GDK_LINE_ON_OFF_DASH
;
1844 switch (m_pen
.GetStyle())
1846 case wxPENSTYLE_USER_DASH
:
1847 req_nb_dash
= m_pen
.GetDashCount();
1848 req_dash
= (wxGTKDash
*)m_pen
.GetDash();
1850 case wxPENSTYLE_DOT
:
1854 case wxPENSTYLE_LONG_DASH
:
1856 req_dash
= wxCoord_dashed
;
1858 case wxPENSTYLE_SHORT_DASH
:
1860 req_dash
= short_dashed
;
1862 case wxPENSTYLE_DOT_DASH
:
1864 req_dash
= dotted_dashed
;
1867 case wxPENSTYLE_TRANSPARENT
:
1868 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
1869 case wxPENSTYLE_STIPPLE
:
1870 case wxPENSTYLE_SOLID
:
1872 lineStyle
= GDK_LINE_SOLID
;
1878 if (req_dash
&& req_nb_dash
)
1880 wxGTKDash
*real_req_dash
= new wxGTKDash
[req_nb_dash
];
1883 for (int i
= 0; i
< req_nb_dash
; i
++)
1884 real_req_dash
[i
] = req_dash
[i
] * width
;
1885 gdk_gc_set_dashes( m_penGC
, 0, real_req_dash
, req_nb_dash
);
1886 delete[] real_req_dash
;
1890 // No Memory. We use non-scaled dash pattern...
1891 gdk_gc_set_dashes( m_penGC
, 0, (wxGTKDash
*)req_dash
, req_nb_dash
);
1895 GdkCapStyle capStyle
= GDK_CAP_ROUND
;
1896 switch (m_pen
.GetCap())
1898 case wxCAP_PROJECTING
: { capStyle
= GDK_CAP_PROJECTING
; break; }
1899 case wxCAP_BUTT
: { capStyle
= GDK_CAP_BUTT
; break; }
1905 capStyle
= GDK_CAP_NOT_LAST
;
1910 GdkJoinStyle joinStyle
= GDK_JOIN_ROUND
;
1911 switch (m_pen
.GetJoin())
1913 case wxJOIN_BEVEL
: { joinStyle
= GDK_JOIN_BEVEL
; break; }
1914 case wxJOIN_MITER
: { joinStyle
= GDK_JOIN_MITER
; break; }
1916 default: { joinStyle
= GDK_JOIN_ROUND
; break; }
1919 gdk_gc_set_line_attributes( m_penGC
, width
, lineStyle
, capStyle
, joinStyle
);
1921 m_pen
.GetColour().CalcPixel( m_cmap
);
1922 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
1925 void wxWindowDCImpl::SetBrush( const wxBrush
&brush
)
1927 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1929 if (m_brush
== brush
) return;
1933 if (!m_brush
.IsOk()) return;
1935 if (!m_gdkwindow
) return;
1937 m_brush
.GetColour().CalcPixel( m_cmap
);
1938 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
1940 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
1942 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
) && (m_brush
.GetStipple()->IsOk()))
1944 if (m_brush
.GetStipple()->GetDepth() != 1)
1946 gdk_gc_set_fill( m_brushGC
, GDK_TILED
);
1947 gdk_gc_set_tile( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1951 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1952 gdk_gc_set_stipple( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1956 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
) && (m_brush
.GetStipple()->GetMask()))
1958 gdk_gc_set_fill( m_textGC
, GDK_OPAQUE_STIPPLED
);
1959 gdk_gc_set_stipple( m_textGC
, m_brush
.GetStipple()->GetMask()->GetBitmap() );
1962 if (m_brush
.IsHatch())
1964 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1965 gdk_gc_set_stipple(m_brushGC
, GetHatch(m_brush
.GetStyle()));
1969 void wxWindowDCImpl::SetBackground( const wxBrush
&brush
)
1971 /* CMB 21/7/98: Added SetBackground. Sets background brush
1972 * for Clear() and bg colour for shapes filled with cross-hatch brush */
1974 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1976 if (m_backgroundBrush
== brush
) return;
1978 m_backgroundBrush
= brush
;
1980 if (!m_backgroundBrush
.IsOk()) return;
1982 if (!m_gdkwindow
) return;
1984 wxColor color
= m_backgroundBrush
.GetColour();
1985 color
.CalcPixel(m_cmap
);
1986 const GdkColor
* gdkColor
= color
.GetColor();
1987 gdk_gc_set_background(m_brushGC
, gdkColor
);
1988 gdk_gc_set_background(m_penGC
, gdkColor
);
1989 gdk_gc_set_background(m_bgGC
, gdkColor
);
1990 gdk_gc_set_foreground(m_bgGC
, gdkColor
);
1993 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
1995 if (m_backgroundBrush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
)
1997 const wxBitmap
* stipple
= m_backgroundBrush
.GetStipple();
1998 if (stipple
->IsOk())
2000 if (stipple
->GetDepth() != 1)
2002 gdk_gc_set_fill(m_bgGC
, GDK_TILED
);
2003 gdk_gc_set_tile(m_bgGC
, stipple
->GetPixmap());
2007 gdk_gc_set_fill(m_bgGC
, GDK_STIPPLED
);
2008 gdk_gc_set_stipple(m_bgGC
, stipple
->GetPixmap());
2012 else if (m_backgroundBrush
.IsHatch())
2014 gdk_gc_set_fill( m_bgGC
, GDK_STIPPLED
);
2015 gdk_gc_set_stipple(m_bgGC
, GetHatch(m_backgroundBrush
.GetStyle()));
2019 void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
2021 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2023 if (m_logicalFunction
== function
)
2026 // VZ: shouldn't this be a CHECK?
2033 case wxXOR
: mode
= GDK_XOR
; break;
2034 case wxINVERT
: mode
= GDK_INVERT
; break;
2035 case wxOR_REVERSE
: mode
= GDK_OR_REVERSE
; break;
2036 case wxAND_REVERSE
: mode
= GDK_AND_REVERSE
; break;
2037 case wxCLEAR
: mode
= GDK_CLEAR
; break;
2038 case wxSET
: mode
= GDK_SET
; break;
2039 case wxOR_INVERT
: mode
= GDK_OR_INVERT
; break;
2040 case wxAND
: mode
= GDK_AND
; break;
2041 case wxOR
: mode
= GDK_OR
; break;
2042 case wxEQUIV
: mode
= GDK_EQUIV
; break;
2043 case wxNAND
: mode
= GDK_NAND
; break;
2044 case wxAND_INVERT
: mode
= GDK_AND_INVERT
; break;
2045 case wxCOPY
: mode
= GDK_COPY
; break;
2046 case wxNO_OP
: mode
= GDK_NOOP
; break;
2047 case wxSRC_INVERT
: mode
= GDK_COPY_INVERT
; break;
2048 case wxNOR
: mode
= GDK_NOR
; break;
2050 wxFAIL_MSG("unknown mode");
2054 m_logicalFunction
= function
;
2056 gdk_gc_set_function( m_penGC
, mode
);
2057 gdk_gc_set_function( m_brushGC
, mode
);
2059 // to stay compatible with wxMSW, we don't apply ROPs to the text
2060 // operations (i.e. DrawText/DrawRotatedText).
2061 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2062 gdk_gc_set_function( m_textGC
, mode
);
2065 void wxWindowDCImpl::SetTextForeground( const wxColour
&col
)
2067 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2069 // don't set m_textForegroundColour to an invalid colour as we'd crash
2070 // later then (we use m_textForegroundColour.GetColor() without checking
2072 if ( !col
.IsOk() || (m_textForegroundColour
== col
) )
2075 m_textForegroundColour
= col
;
2079 m_textForegroundColour
.CalcPixel( m_cmap
);
2080 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
2084 void wxWindowDCImpl::SetTextBackground( const wxColour
&col
)
2086 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2089 if ( !col
.IsOk() || (m_textBackgroundColour
== col
) )
2092 m_textBackgroundColour
= col
;
2096 m_textBackgroundColour
.CalcPixel( m_cmap
);
2097 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
2101 void wxWindowDCImpl::SetBackgroundMode( int mode
)
2103 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2105 m_backgroundMode
= mode
;
2108 void wxWindowDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
2110 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
2113 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2115 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2117 if (!m_gdkwindow
) return;
2120 rect
.x
= XLOG2DEV(x
);
2121 rect
.y
= YLOG2DEV(y
);
2122 rect
.width
= XLOG2DEVREL(width
);
2123 rect
.height
= YLOG2DEVREL(height
);
2125 if (m_window
&& m_window
->m_wxwindow
&&
2126 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2128 rect
.x
-= rect
.width
;
2131 DoSetDeviceClippingRegion(wxRegion(rect
));
2134 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
2136 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2140 DestroyClippingRegion();
2144 if (!m_gdkwindow
) return;
2146 if (!m_currentClippingRegion
.IsNull())
2147 m_currentClippingRegion
.Intersect( region
);
2149 m_currentClippingRegion
.Union( region
);
2151 #if USE_PAINT_REGION
2152 if (!m_paintClippingRegion
.IsNull())
2153 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2156 wxCoord xx
, yy
, ww
, hh
;
2157 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2158 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2160 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2161 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2162 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2163 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2164 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2167 void wxWindowDCImpl::DestroyClippingRegion()
2169 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2171 wxDCImpl::DestroyClippingRegion();
2173 m_currentClippingRegion
.Clear();
2175 #if USE_PAINT_REGION
2176 if (!m_paintClippingRegion
.IsEmpty())
2177 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2180 if (!m_gdkwindow
) return;
2182 GdkRegion
* gdkRegion
= NULL
;
2183 if (!m_currentClippingRegion
.IsEmpty())
2184 gdkRegion
= m_currentClippingRegion
.GetRegion();
2186 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2187 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2188 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2189 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2192 void wxWindowDCImpl::Destroy()
2194 if (m_penGC
) wxFreePoolGC( m_penGC
);
2196 if (m_brushGC
) wxFreePoolGC( m_brushGC
);
2198 if (m_textGC
) wxFreePoolGC( m_textGC
);
2200 if (m_bgGC
) wxFreePoolGC( m_bgGC
);
2204 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
2206 m_deviceOriginX
= x
;
2207 m_deviceOriginY
= y
;
2209 ComputeScaleAndOrigin();
2212 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
2214 m_signX
= (xLeftRight
? 1 : -1);
2215 m_signY
= (yBottomUp
? -1 : 1);
2217 if (m_window
&& m_window
->m_wxwindow
&&
2218 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2221 ComputeScaleAndOrigin();
2224 void wxWindowDCImpl::ComputeScaleAndOrigin()
2226 const wxRealPoint
origScale(m_scaleX
, m_scaleY
);
2228 wxDCImpl::ComputeScaleAndOrigin();
2230 // if scale has changed call SetPen to recalulate the line width
2231 if ( wxRealPoint(m_scaleX
, m_scaleY
) != origScale
&& m_pen
.IsOk() )
2233 // this is a bit artificial, but we need to force wxDC to think the pen
2241 // Resolution in pixels per logical inch
2242 wxSize
wxWindowDCImpl::GetPPI() const
2244 return wxSize( (int) (m_mm_to_pix_x
* 25.4 + 0.5), (int) (m_mm_to_pix_y
* 25.4 + 0.5));
2247 int wxWindowDCImpl::GetDepth() const
2249 return gdk_drawable_get_depth(m_gdkwindow
);
2252 //-----------------------------------------------------------------------------
2254 //-----------------------------------------------------------------------------
2256 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
2258 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
2259 : wxWindowDCImpl( owner
)
2263 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*win
)
2264 : wxWindowDCImpl( owner
, win
)
2266 wxCHECK_RET( win
, wxT("NULL window in wxClientDCImpl::wxClientDC") );
2268 #ifdef __WXUNIVERSAL__
2269 wxPoint ptOrigin
= win
->GetClientAreaOrigin();
2270 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
2271 wxSize size
= win
->GetClientSize();
2272 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
2277 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
2279 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
2281 m_window
->GetClientSize( width
, height
);
2284 //-----------------------------------------------------------------------------
2286 //-----------------------------------------------------------------------------
2288 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
2290 // Limit the paint region to the window size. Sometimes
2291 // the paint region is too big, and this risks X11 errors
2292 static void wxLimitRegionToSize(wxRegion
& region
, const wxSize
& sz
)
2294 wxRect originalRect
= region
.GetBox();
2295 wxRect
rect(originalRect
);
2296 if (rect
.width
+ rect
.x
> sz
.x
)
2297 rect
.width
= sz
.x
- rect
.x
;
2298 if (rect
.height
+ rect
.y
> sz
.y
)
2299 rect
.height
= sz
.y
- rect
.y
;
2300 if (rect
!= originalRect
)
2302 region
= wxRegion(rect
);
2303 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2304 originalRect
.x
, originalRect
.y
, originalRect
.width
, originalRect
.height
,
2305 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2309 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
2310 : wxClientDCImpl( owner
)
2314 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*win
)
2315 : wxClientDCImpl( owner
, win
)
2317 #if USE_PAINT_REGION
2318 if (!win
->m_clipPaintRegion
)
2321 wxSize sz
= win
->GetSize();
2322 m_paintClippingRegion
= win
->m_nativeUpdateRegion
;
2323 wxLimitRegionToSize(m_paintClippingRegion
, sz
);
2325 GdkRegion
*region
= m_paintClippingRegion
.GetRegion();
2328 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2329 wxLimitRegionToSize(m_currentClippingRegion
, sz
);
2331 if (sz
.x
<= 0 || sz
.y
<= 0)
2334 gdk_gc_set_clip_region( m_penGC
, region
);
2335 gdk_gc_set_clip_region( m_brushGC
, region
);
2336 gdk_gc_set_clip_region( m_textGC
, region
);
2337 gdk_gc_set_clip_region( m_bgGC
, region
);
2342 // ----------------------------------------------------------------------------
2344 // ----------------------------------------------------------------------------
2346 class wxDCModule
: public wxModule
2353 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2356 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2358 bool wxDCModule::OnInit()
2364 void wxDCModule::OnExit()
2368 for (int i
= wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
; i
--; )
2371 g_object_unref(hatches
[i
]);