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 //-----------------------------------------------------------------------------
142 #define GC_POOL_ALLOC_SIZE 100
144 static int wxGCPoolSize
= 0;
146 static wxGC
*wxGCPool
= NULL
;
148 static void wxInitGCPool()
150 // This really could wait until the first call to
151 // wxGetPoolGC, but we will make the first allocation
152 // now when other initialization is being performed.
154 // Set initial pool size.
155 wxGCPoolSize
= GC_POOL_ALLOC_SIZE
;
157 // Allocate initial pool.
158 wxGCPool
= (wxGC
*)malloc(wxGCPoolSize
* sizeof(wxGC
));
159 if (wxGCPool
== NULL
)
161 // If we cannot malloc, then fail with error
162 // when debug is enabled. If debug is not enabled,
163 // the problem will eventually get caught
165 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
169 // Zero initial pool.
170 memset(wxGCPool
, 0, wxGCPoolSize
* sizeof(wxGC
));
173 static void wxCleanUpGCPool()
175 for (int i
= 0; i
< wxGCPoolSize
; i
++)
177 if (wxGCPool
[i
].m_gc
)
178 g_object_unref (wxGCPool
[i
].m_gc
);
186 static GdkGC
* wxGetPoolGC( GdkWindow
*window
, wxPoolGCType type
)
190 // Look for an available GC.
191 for (int i
= 0; i
< wxGCPoolSize
; i
++)
193 if (!wxGCPool
[i
].m_gc
)
195 wxGCPool
[i
].m_gc
= gdk_gc_new( window
);
196 gdk_gc_set_exposures( wxGCPool
[i
].m_gc
, FALSE
);
197 wxGCPool
[i
].m_type
= type
;
198 wxGCPool
[i
].m_used
= false;
200 if ((!wxGCPool
[i
].m_used
) && (wxGCPool
[i
].m_type
== type
))
202 wxGCPool
[i
].m_used
= true;
203 return wxGCPool
[i
].m_gc
;
207 // We did not find an available GC.
208 // We need to grow the GC pool.
209 pptr
= (wxGC
*)realloc(wxGCPool
,
210 (wxGCPoolSize
+ GC_POOL_ALLOC_SIZE
)*sizeof(wxGC
));
213 // Initialize newly allocated pool.
215 memset(&wxGCPool
[wxGCPoolSize
], 0,
216 GC_POOL_ALLOC_SIZE
*sizeof(wxGC
));
218 // Initialize entry we will return.
219 wxGCPool
[wxGCPoolSize
].m_gc
= gdk_gc_new( window
);
220 gdk_gc_set_exposures( wxGCPool
[wxGCPoolSize
].m_gc
, FALSE
);
221 wxGCPool
[wxGCPoolSize
].m_type
= type
;
222 wxGCPool
[wxGCPoolSize
].m_used
= true;
224 // Set new value of pool size.
225 wxGCPoolSize
+= GC_POOL_ALLOC_SIZE
;
227 // Return newly allocated entry.
228 return wxGCPool
[wxGCPoolSize
-GC_POOL_ALLOC_SIZE
].m_gc
;
231 // The realloc failed. Fall through to error.
232 wxFAIL_MSG( wxT("No GC available") );
237 static void wxFreePoolGC( GdkGC
*gc
)
239 for (int i
= 0; i
< wxGCPoolSize
; i
++)
241 if (wxGCPool
[i
].m_gc
== gc
)
243 wxGCPool
[i
].m_used
= false;
248 wxFAIL_MSG( wxT("Wrong GC") );
251 //-----------------------------------------------------------------------------
253 //-----------------------------------------------------------------------------
255 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxGTKDCImpl
)
257 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
266 m_isScreenDC
= false;
272 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
275 wxASSERT_MSG( window
, wxT("DC needs a window") );
283 m_isScreenDC
= false;
284 m_font
= window
->GetFont();
286 GtkWidget
*widget
= window
->m_wxwindow
;
287 m_gdkwindow
= window
->GTKGetDrawingWindow();
289 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
290 // code should still be able to create wxClientDCs for them
293 widget
= window
->m_widget
;
295 wxCHECK_RET(widget
, "DC needs a widget");
297 m_gdkwindow
= widget
->window
;
298 if (!gtk_widget_get_has_window(widget
))
299 SetDeviceLocalOrigin(widget
->allocation
.x
, widget
->allocation
.y
);
302 m_context
= window
->GTKGetPangoDefaultContext();
303 m_layout
= pango_layout_new( m_context
);
304 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
306 // Window not realized ?
309 // Don't report problems as per MSW.
315 m_cmap
= gtk_widget_get_colormap(widget
);
319 /* this must be done after SetUpDC, bacause SetUpDC calls the
320 repective SetBrush, SetPen, SetBackground etc functions
321 to set up the DC. SetBackground call m_owner->SetBackground
322 and this might not be desired as the standard dc background
323 is white whereas a window might assume gray to be the
324 standard (as e.g. wxStatusBar) */
328 if (m_window
&& m_window
->m_wxwindow
&&
329 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
334 // origin in the upper right corner
335 m_deviceOriginX
= m_window
->GetClientSize().x
;
339 wxWindowDCImpl::~wxWindowDCImpl()
344 g_object_unref (m_layout
);
346 pango_font_description_free( m_fontdesc
);
349 void wxWindowDCImpl::SetUpDC( bool isMemDC
)
353 wxASSERT_MSG( !m_penGC
, wxT("GCs already created") );
357 if ((isMemDC
) && (GetSelectedBitmap().IsOk()))
359 if (GetSelectedBitmap().GetDepth() == 1)
361 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_MONO
);
362 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_MONO
);
363 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_MONO
);
364 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_MONO
);
373 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_SCREEN
);
374 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_SCREEN
);
375 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_SCREEN
);
376 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_SCREEN
);
380 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR
);
381 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR
);
382 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR
);
383 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR
);
387 /* background colour */
388 m_backgroundBrush
= *wxWHITE_BRUSH
;
389 m_backgroundBrush
.GetColour().CalcPixel( m_cmap
);
390 const GdkColor
*bg_col
= m_backgroundBrush
.GetColour().GetColor();
393 m_textForegroundColour
.CalcPixel( m_cmap
);
394 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
396 m_textBackgroundColour
.CalcPixel( m_cmap
);
397 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
399 gdk_gc_set_fill( m_textGC
, GDK_SOLID
);
401 gdk_gc_set_colormap( m_textGC
, m_cmap
);
404 m_pen
.GetColour().CalcPixel( m_cmap
);
405 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
406 gdk_gc_set_background( m_penGC
, bg_col
);
408 gdk_gc_set_line_attributes( m_penGC
, 0, GDK_LINE_SOLID
, GDK_CAP_NOT_LAST
, GDK_JOIN_ROUND
);
411 m_brush
.GetColour().CalcPixel( m_cmap
);
412 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
413 gdk_gc_set_background( m_brushGC
, bg_col
);
415 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
418 gdk_gc_set_background( m_bgGC
, bg_col
);
419 gdk_gc_set_foreground( m_bgGC
, bg_col
);
421 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
424 gdk_gc_set_function( m_textGC
, GDK_COPY
);
425 gdk_gc_set_function( m_brushGC
, GDK_COPY
);
426 gdk_gc_set_function( m_penGC
, GDK_COPY
);
429 gdk_gc_set_clip_rectangle( m_penGC
, NULL
);
430 gdk_gc_set_clip_rectangle( m_brushGC
, NULL
);
431 gdk_gc_set_clip_rectangle( m_textGC
, NULL
);
432 gdk_gc_set_clip_rectangle( m_bgGC
, NULL
);
435 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
437 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
439 m_window
->GetSize(width
, height
);
442 bool wxWindowDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
443 const wxColour
& col
, wxFloodFillStyle style
)
446 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
447 const wxColour
& col
, wxFloodFillStyle style
);
449 return wxDoFloodFill( GetOwner(), x
, y
, col
, style
);
460 bool wxWindowDCImpl::DoGetPixel( wxCoord x1
, wxCoord y1
, wxColour
*col
) const
462 GdkImage
* image
= NULL
;
465 const int x
= LogicalToDeviceX(x1
);
466 const int y
= LogicalToDeviceY(y1
);
468 gdk_drawable_get_size(m_gdkwindow
, &rect
.width
, &rect
.height
);
469 if (rect
.Contains(x
, y
))
470 image
= gdk_drawable_get_image(m_gdkwindow
, x
, y
, 1, 1);
477 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
478 const unsigned pixel
= gdk_image_get_pixel(image
, 0, 0);
479 if (colormap
== NULL
)
480 *col
= pixel
? m_textForegroundColour
: m_textBackgroundColour
;
484 gdk_colormap_query_color(colormap
, pixel
, &c
);
485 col
->Set(c
.red
>> 8, c
.green
>> 8, c
.blue
>> 8);
487 g_object_unref(image
);
491 void wxWindowDCImpl::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
493 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
495 if ( m_pen
.IsNonTransparent() )
498 gdk_draw_line( m_gdkwindow
, m_penGC
, XLOG2DEV(x1
), YLOG2DEV(y1
), XLOG2DEV(x2
), YLOG2DEV(y2
) );
500 CalcBoundingBox(x1
, y1
);
501 CalcBoundingBox(x2
, y2
);
505 void wxWindowDCImpl::DoCrossHair( wxCoord x
, wxCoord y
)
507 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
509 if ( m_pen
.IsNonTransparent() )
513 GetOwner()->GetSize( &w
, &h
);
514 wxCoord xx
= XLOG2DEV(x
);
515 wxCoord yy
= YLOG2DEV(y
);
518 gdk_draw_line( m_gdkwindow
, m_penGC
, 0, yy
, XLOG2DEVREL(w
), yy
);
519 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, 0, xx
, YLOG2DEVREL(h
) );
524 void wxWindowDCImpl::DrawingSetup(GdkGC
*& gc
, bool& originChanged
)
527 GdkPixmap
* pixmap
= NULL
;
528 const int style
= m_brush
.GetStyle();
530 if (style
== wxBRUSHSTYLE_STIPPLE
|| style
== wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
)
532 const wxBitmap
* stipple
= m_brush
.GetStipple();
535 if (style
== wxBRUSHSTYLE_STIPPLE
)
536 pixmap
= stipple
->GetPixmap();
537 else if (stipple
->GetMask())
539 pixmap
= stipple
->GetPixmap();
544 else if (m_brush
.IsHatch())
546 pixmap
= GetHatch(style
);
554 gdk_drawable_get_size(pixmap
, &w
, &h
);
555 origin_x
= m_deviceOriginX
% w
;
556 origin_y
= m_deviceOriginY
% h
;
559 originChanged
= origin_x
|| origin_y
;
561 gdk_gc_set_ts_origin(gc
, origin_x
, origin_y
);
564 void wxWindowDCImpl::DoDrawArc( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
,
565 wxCoord xc
, wxCoord yc
)
567 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
569 wxCoord xx1
= XLOG2DEV(x1
);
570 wxCoord yy1
= YLOG2DEV(y1
);
571 wxCoord xx2
= XLOG2DEV(x2
);
572 wxCoord yy2
= YLOG2DEV(y2
);
573 wxCoord xxc
= XLOG2DEV(xc
);
574 wxCoord yyc
= YLOG2DEV(yc
);
575 double dx
= xx1
- xxc
;
576 double dy
= yy1
- yyc
;
577 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
578 wxCoord r
= (wxCoord
)radius
;
579 double radius1
, radius2
;
581 if (xx1
== xx2
&& yy1
== yy2
)
586 else if ( wxIsNullDouble(radius
) )
593 radius1
= (xx1
- xxc
== 0) ?
594 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
595 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
596 radius2
= (xx2
- xxc
== 0) ?
597 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
598 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
600 wxCoord alpha1
= wxCoord(radius1
* 64.0);
601 wxCoord alpha2
= wxCoord((radius2
- radius1
) * 64.0);
602 while (alpha2
<= 0) alpha2
+= 360*64;
603 while (alpha1
> 360*64) alpha1
-= 360*64;
607 if ( m_brush
.IsNonTransparent() )
611 DrawingSetup(gc
, originChanged
);
613 gdk_draw_arc(m_gdkwindow
, gc
, true, xxc
-r
, yyc
-r
, 2*r
, 2*r
, alpha1
, alpha2
);
616 gdk_gc_set_ts_origin(gc
, 0, 0);
619 if ( m_pen
.IsNonTransparent() )
621 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xxc
-r
, yyc
-r
, 2*r
,2*r
, alpha1
, alpha2
);
623 if ( m_brush
.IsNonTransparent() && (alpha2
- alpha1
!= 360*64) )
625 gdk_draw_line( m_gdkwindow
, m_penGC
, xx1
, yy1
, xxc
, yyc
);
626 gdk_draw_line( m_gdkwindow
, m_penGC
, xxc
, yyc
, xx2
, yy2
);
631 CalcBoundingBox (x1
, y1
);
632 CalcBoundingBox (x2
, y2
);
635 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double sa
, double ea
)
637 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
639 wxCoord xx
= XLOG2DEV(x
);
640 wxCoord yy
= YLOG2DEV(y
);
641 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
642 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
644 // CMB: handle -ve width and/or height
645 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
646 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
650 wxCoord start
= wxCoord(sa
* 64.0);
651 wxCoord end
= wxCoord((ea
-sa
) * 64.0);
653 if ( m_brush
.IsNonTransparent() )
657 DrawingSetup(gc
, originChanged
);
659 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, start
, end
);
662 gdk_gc_set_ts_origin(gc
, 0, 0);
665 if ( m_pen
.IsNonTransparent() )
666 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, start
, end
);
669 CalcBoundingBox (x
, y
);
670 CalcBoundingBox (x
+ width
, y
+ height
);
673 void wxWindowDCImpl::DoDrawPoint( wxCoord x
, wxCoord y
)
675 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
677 if ( m_pen
.IsNonTransparent() && m_gdkwindow
)
678 gdk_draw_point( m_gdkwindow
, m_penGC
, XLOG2DEV(x
), YLOG2DEV(y
) );
680 CalcBoundingBox (x
, y
);
683 void wxWindowDCImpl::DoDrawLines( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
685 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
689 if ( m_pen
.IsTransparent() )
692 //Check, if scaling is necessary
694 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
696 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
697 GdkPoint
* gpts
= reinterpret_cast<GdkPoint
*>(points
);
700 gpts
= new GdkPoint
[n
];
702 for (int i
= 0; i
< n
; i
++)
706 gpts
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
707 gpts
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
709 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
713 gdk_draw_lines( m_gdkwindow
, m_penGC
, gpts
, n
);
719 void wxWindowDCImpl::DoDrawPolygon( int n
, wxPoint points
[],
720 wxCoord xoffset
, wxCoord yoffset
,
721 wxPolygonFillMode
WXUNUSED(fillStyle
) )
723 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
727 //Check, if scaling is necessary
729 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
731 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
732 GdkPoint
* gdkpoints
= reinterpret_cast<GdkPoint
*>(points
);
735 gdkpoints
= new GdkPoint
[n
];
738 for (i
= 0 ; i
< n
; i
++)
742 gdkpoints
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
743 gdkpoints
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
745 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
750 if ( m_brush
.IsNonTransparent() )
754 DrawingSetup(gc
, originChanged
);
756 gdk_draw_polygon(m_gdkwindow
, gc
, true, gdkpoints
, n
);
759 gdk_gc_set_ts_origin(gc
, 0, 0);
762 if ( m_pen
.IsNonTransparent() )
765 for (i = 0 ; i < n ; i++)
767 gdk_draw_line( m_gdkwindow, m_penGC,
770 gdkpoints[(i+1)%n].x,
771 gdkpoints[(i+1)%n].y);
774 gdk_draw_polygon( m_gdkwindow
, m_penGC
, FALSE
, gdkpoints
, n
);
783 void wxWindowDCImpl::DoDrawRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
785 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
787 wxCoord xx
= XLOG2DEV(x
);
788 wxCoord yy
= YLOG2DEV(y
);
789 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
790 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
792 // CMB: draw nothing if transformed w or h is 0
793 if (ww
== 0 || hh
== 0) return;
795 // CMB: handle -ve width and/or height
796 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
797 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
801 if ( m_brush
.IsNonTransparent() )
805 DrawingSetup(gc
, originChanged
);
807 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
);
810 gdk_gc_set_ts_origin(gc
, 0, 0);
813 if ( m_pen
.IsNonTransparent() )
815 if ((m_pen
.GetWidth() == 2) && (m_pen
.GetCap() == wxCAP_ROUND
) &&
816 (m_pen
.GetJoin() == wxJOIN_ROUND
) && (m_pen
.GetStyle() == wxPENSTYLE_SOLID
))
818 // Use 2 1-line rects instead
819 gdk_gc_set_line_attributes( m_penGC
, 1, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
824 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
+1, yy
, ww
-2, hh
-2 );
825 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
-1, ww
, hh
);
829 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-2, hh
-2 );
830 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
-1, yy
-1, ww
, hh
);
834 gdk_gc_set_line_attributes( m_penGC
, 2, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
838 // Just use X11 for other cases
839 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-1, hh
-1 );
844 CalcBoundingBox( x
, y
);
845 CalcBoundingBox( x
+ width
, y
+ height
);
848 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
850 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
852 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
854 wxCoord xx
= XLOG2DEV(x
);
855 wxCoord yy
= YLOG2DEV(y
);
856 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
857 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
858 wxCoord rr
= XLOG2DEVREL((wxCoord
)radius
);
860 // CMB: handle -ve width and/or height
861 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
862 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
864 // CMB: if radius is zero use DrawRectangle() instead to avoid
865 // X drawing errors with small radii
868 DoDrawRectangle( x
, y
, width
, height
);
872 // CMB: draw nothing if transformed w or h is 0
873 if (ww
== 0 || hh
== 0) return;
875 // CMB: adjust size if outline is drawn otherwise the result is
876 // 1 pixel too wide and high
877 if ( m_pen
.IsNonTransparent() )
885 // CMB: ensure dd is not larger than rectangle otherwise we
886 // get an hour glass shape
888 if (dd
> ww
) dd
= ww
;
889 if (dd
> hh
) dd
= hh
;
892 if ( m_brush
.IsNonTransparent() )
896 DrawingSetup(gc
, originChanged
);
898 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
+rr
, yy
, ww
-dd
+1, hh
);
899 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
+rr
, ww
, hh
-dd
+1);
900 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, dd
, dd
, 90*64, 90*64);
901 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64);
902 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64);
903 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64);
906 gdk_gc_set_ts_origin(gc
, 0, 0);
909 if ( m_pen
.IsNonTransparent() )
911 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
, xx
+ww
-rr
, yy
);
912 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
+hh
, xx
+ww
-rr
, yy
+hh
);
913 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, yy
+rr
+1, xx
, yy
+hh
-rr
);
914 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+ww
, yy
+rr
+1, xx
+ww
, yy
+hh
-rr
);
915 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, dd
, dd
, 90*64, 90*64 );
916 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64 );
917 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64 );
918 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64 );
922 // this ignores the radius
923 CalcBoundingBox( x
, y
);
924 CalcBoundingBox( x
+ width
, y
+ height
);
927 void wxWindowDCImpl::DoDrawEllipse( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
929 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
931 wxCoord xx
= XLOG2DEV(x
);
932 wxCoord yy
= YLOG2DEV(y
);
933 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
934 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
936 // CMB: handle -ve width and/or height
937 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
938 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
942 if ( m_brush
.IsNonTransparent() )
946 DrawingSetup(gc
, originChanged
);
948 // If the pen is transparent pen we increase the size
949 // for better compatibility with other platforms.
950 if ( m_pen
.IsNonTransparent() )
956 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, 0, 360*64);
959 gdk_gc_set_ts_origin(gc
, 0, 0);
962 if ( m_pen
.IsNonTransparent() )
963 gdk_draw_arc( m_gdkwindow
, m_penGC
, false, xx
, yy
, ww
, hh
, 0, 360*64 );
966 CalcBoundingBox( x
, y
);
967 CalcBoundingBox( x
+ width
, y
+ height
);
970 void wxWindowDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
972 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
973 DoDrawBitmap( (const wxBitmap
&)icon
, x
, y
, true );
976 // scale a pixbuf, return new pixbuf, unref old one
978 Scale(GdkPixbuf
* pixbuf
, int dst_w
, int dst_h
, double sx
, double sy
)
980 GdkPixbuf
* pixbuf_scaled
= gdk_pixbuf_new(
981 GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, dst_w
, dst_h
);
982 gdk_pixbuf_scale(pixbuf
, pixbuf_scaled
,
983 0, 0, dst_w
, dst_h
, 0, 0, sx
, sy
, GDK_INTERP_NEAREST
);
984 g_object_unref(pixbuf
);
985 return pixbuf_scaled
;
988 // scale part of a pixmap using pixbuf scaling, return pixbuf
990 Scale(GdkPixmap
* pixmap
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
992 GdkPixbuf
* pixbuf
= gdk_pixbuf_get_from_drawable(
993 NULL
, pixmap
, NULL
, x
, y
, 0, 0, w
, h
);
994 return Scale(pixbuf
, dst_w
, dst_h
, sx
, sy
);
997 // scale part of a mask pixmap, return new mask, unref old one
999 ScaleMask(GdkPixmap
* mask
, int x
, int y
, int w
, int h
, int dst_w
, int dst_h
, double sx
, double sy
)
1001 GdkPixbuf
* pixbuf
= Scale(mask
, x
, y
, w
, h
, dst_w
, dst_h
, sx
, sy
);
1003 // convert black and white pixbuf back to a mono pixmap
1004 const unsigned out_rowstride
= (dst_w
+ 7) / 8;
1005 const size_t data_size
= out_rowstride
* size_t(dst_h
);
1006 char* data
= new char[data_size
];
1008 const guchar
* row
= gdk_pixbuf_get_pixels(pixbuf
);
1009 const int rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
1010 memset(data
, 0, data_size
);
1011 for (int j
= 0; j
< dst_h
; j
++, row
+= rowstride
, out
+= out_rowstride
)
1013 const guchar
* in
= row
;
1014 for (int i
= 0; i
< dst_w
; i
++, in
+= 3)
1016 out
[i
>> 3] |= 1 << (i
& 7);
1018 g_object_unref(pixbuf
);
1019 GdkPixmap
* pixmap
= gdk_bitmap_create_from_data(mask
, data
, dst_w
, dst_h
);
1021 g_object_unref(mask
);
1025 // Make a new mask from part of a mask and a clip region.
1026 // Return new mask, unref old one.
1028 ClipMask(GdkPixmap
* mask
, GdkRegion
* clipRegion
, int x
, int y
, int dst_x
, int dst_y
, int w
, int h
)
1030 GdkGCValues gcValues
;
1031 gcValues
.foreground
.pixel
= 0;
1032 GdkGC
* gc
= gdk_gc_new_with_values(mask
, &gcValues
, GDK_GC_FOREGROUND
);
1033 GdkPixmap
* pixmap
= gdk_pixmap_new(mask
, w
, h
, 1);
1034 // clear new mask, so clipped areas will be masked
1035 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1036 gdk_gc_set_clip_region(gc
, clipRegion
);
1037 gdk_gc_set_clip_origin(gc
, -dst_x
, -dst_y
);
1038 // draw old mask onto new one, with clip
1039 gdk_draw_drawable(pixmap
, gc
, mask
, x
, y
, 0, 0, w
, h
);
1041 g_object_unref(mask
);
1045 // make a color pixmap from part of a mono one, using text fg/bg colors
1047 wxWindowDCImpl::MonoToColor(GdkPixmap
* monoPixmap
, int x
, int y
, int w
, int h
) const
1049 GdkPixmap
* pixmap
= gdk_pixmap_new(m_gdkwindow
, w
, h
, -1);
1050 GdkGCValues gcValues
;
1051 gcValues
.foreground
.pixel
= m_textForegroundColour
.GetColor()->pixel
;
1052 gcValues
.background
.pixel
= m_textBackgroundColour
.GetColor()->pixel
;
1053 gcValues
.stipple
= monoPixmap
;
1054 gcValues
.fill
= GDK_OPAQUE_STIPPLED
;
1055 gcValues
.ts_x_origin
= -x
;
1056 gcValues
.ts_y_origin
= -y
;
1057 GdkGC
* gc
= gdk_gc_new_with_values(pixmap
, &gcValues
, GdkGCValuesMask(
1058 GDK_GC_FOREGROUND
| GDK_GC_BACKGROUND
| GDK_GC_STIPPLE
| GDK_GC_FILL
|
1059 GDK_GC_TS_X_ORIGIN
| GDK_GC_TS_Y_ORIGIN
));
1060 gdk_draw_rectangle(pixmap
, gc
, true, 0, 0, w
, h
);
1065 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap
&bitmap
,
1066 wxCoord x
, wxCoord y
,
1069 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1070 wxCHECK_RET( bitmap
.IsOk(), wxT("invalid bitmap") );
1072 if (!m_gdkwindow
) return;
1074 const int w
= bitmap
.GetWidth();
1075 const int h
= bitmap
.GetHeight();
1077 // notice that as the bitmap is not drawn upside down (or right to left)
1078 // even if the corresponding axis direction is inversed, we need to take it
1079 // into account when calculating its bounding box
1080 CalcBoundingBox(x
, y
);
1081 CalcBoundingBox(x
+ m_signX
*w
, y
+ m_signY
*h
);
1084 int xx
= LogicalToDeviceX(x
);
1085 const int yy
= LogicalToDeviceY(y
);
1086 const int ww
= LogicalToDeviceXRel(w
);
1087 const int hh
= LogicalToDeviceYRel(h
);
1089 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1092 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1093 // determine clip region overlap
1094 int overlap
= wxInRegion
;
1097 overlap
= m_currentClippingRegion
.Contains(xx
, yy
, ww
, hh
);
1098 if (overlap
== wxOutRegion
)
1102 const bool isScaled
= ww
!= w
|| hh
!= h
;
1103 const bool hasAlpha
= bitmap
.HasAlpha();
1104 GdkGC
* const use_gc
= m_penGC
;
1106 GdkPixmap
* mask
= NULL
;
1107 // mask does not work when drawing a pixbuf with alpha
1108 if (useMask
&& !hasAlpha
)
1110 wxMask
* m
= bitmap
.GetMask();
1112 mask
= m
->GetBitmap();
1118 mask
= ScaleMask(mask
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1119 if (overlap
== wxPartRegion
)
1121 // need a new mask that also masks the clipped area,
1122 // because gc can't have both a mask and a clip region
1123 mask
= ClipMask(mask
, clipRegion
, 0, 0, xx
, yy
, ww
, hh
);
1125 gdk_gc_set_clip_mask(use_gc
, mask
);
1126 gdk_gc_set_clip_origin(use_gc
, xx
, yy
);
1129 // determine whether to use pixmap or pixbuf
1130 GdkPixmap
* pixmap
= NULL
;
1131 GdkPixbuf
* pixbuf
= NULL
;
1132 if (bitmap
.HasPixmap())
1133 pixmap
= bitmap
.GetPixmap();
1134 if (pixmap
&& gdk_drawable_get_depth(pixmap
) == 1)
1136 // convert mono pixmap to color using text fg/bg colors
1137 pixmap
= MonoToColor(pixmap
, 0, 0, w
, h
);
1139 else if (hasAlpha
|| pixmap
== NULL
)
1142 pixbuf
= bitmap
.GetPixbuf();
1143 g_object_ref(pixbuf
);
1147 g_object_ref(pixmap
);
1153 pixbuf
= Scale(pixbuf
, ww
, hh
, m_scaleX
, m_scaleY
);
1155 pixbuf
= Scale(pixmap
, 0, 0, w
, h
, ww
, hh
, m_scaleX
, m_scaleY
);
1160 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1161 0, 0, xx
, yy
, ww
, hh
, GDK_RGB_DITHER_NORMAL
, 0, 0);
1162 g_object_unref(pixbuf
);
1166 gdk_draw_drawable(m_gdkwindow
, use_gc
, pixmap
, 0, 0, xx
, yy
, ww
, hh
);
1170 g_object_unref(pixmap
);
1173 g_object_unref(mask
);
1174 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1178 bool wxWindowDCImpl::DoBlit( wxCoord xdest
, wxCoord ydest
,
1179 wxCoord width
, wxCoord height
,
1181 wxCoord xsrc
, wxCoord ysrc
,
1182 wxRasterOperationMode logical_func
,
1184 wxCoord xsrcMask
, wxCoord ysrcMask
)
1186 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1187 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1189 if (!m_gdkwindow
) return false;
1191 GdkDrawable
* srcDrawable
= NULL
;
1192 GdkPixmap
* mask
= NULL
;
1193 wxMemoryDC
* memDC
= wxDynamicCast(source
, wxMemoryDC
);
1196 const wxBitmap
& bitmap
= memDC
->GetSelectedBitmap();
1199 srcDrawable
= bitmap
.GetPixmap();
1202 wxMask
* m
= bitmap
.GetMask();
1204 mask
= m
->GetBitmap();
1209 wxDCImpl
* impl
= source
->GetImpl();
1210 wxWindowDCImpl
* gtk_impl
= wxDynamicCast(impl
, wxWindowDCImpl
);
1212 srcDrawable
= gtk_impl
->GetGDKWindow();
1213 if (srcDrawable
== NULL
)
1217 CalcBoundingBox(xdest
, ydest
);
1218 CalcBoundingBox(xdest
+ width
, ydest
+ height
);
1220 // source device coords
1221 int src_x
= source
->LogicalToDeviceX(xsrc
);
1222 int src_y
= source
->LogicalToDeviceY(ysrc
);
1223 int src_w
= source
->LogicalToDeviceXRel(width
);
1224 int src_h
= source
->LogicalToDeviceYRel(height
);
1226 // Clip source rect to source dc.
1227 // Only necessary when scaling, to avoid GDK errors when
1228 // converting to pixbuf, but no harm in always doing it.
1229 // If source rect changes, it also changes the dest rect.
1231 gdk_drawable_get_size(srcDrawable
, &clip
.width
, &clip
.height
);
1232 clip
.Intersect(wxRect(src_x
, src_y
, src_w
, src_h
));
1233 if (src_w
!= clip
.width
|| src_h
!= clip
.height
)
1235 if (clip
.width
== 0)
1239 src_h
= clip
.height
;
1240 width
= source
->DeviceToLogicalXRel(src_w
);
1241 height
= source
->DeviceToLogicalYRel(src_h
);
1242 if (src_x
!= clip
.x
|| src_y
!= clip
.y
)
1244 xdest
+= source
->DeviceToLogicalXRel(clip
.x
- src_x
);
1245 ydest
+= source
->DeviceToLogicalYRel(clip
.y
- src_y
);
1251 // destination device coords
1252 const int dst_x
= LogicalToDeviceX(xdest
);
1253 const int dst_y
= LogicalToDeviceY(ydest
);
1254 const int dst_w
= LogicalToDeviceXRel(width
);
1255 const int dst_h
= LogicalToDeviceYRel(height
);
1257 GdkRegion
* const clipRegion
= m_currentClippingRegion
.GetRegion();
1258 // determine dest clip region overlap
1259 int overlap
= wxInRegion
;
1262 overlap
= m_currentClippingRegion
.Contains(dst_x
, dst_y
, dst_w
, dst_h
);
1263 if (overlap
== wxOutRegion
)
1267 const bool isScaled
= src_w
!= dst_w
|| src_h
!= dst_h
;
1272 // get source to dest scale
1273 double usx
, usy
, lsx
, lsy
;
1274 source
->GetUserScale(&usx
, &usy
);
1275 source
->GetLogicalScale(&lsx
, &lsy
);
1276 scale_x
= m_scaleX
/ (usx
* lsx
);
1277 scale_y
= m_scaleY
/ (usy
* lsy
);
1280 GdkGC
* const use_gc
= m_penGC
;
1285 int srcMask_x
= src_x
;
1286 int srcMask_y
= src_y
;
1287 if (xsrcMask
!= -1 || ysrcMask
!= -1)
1289 srcMask_x
= source
->LogicalToDeviceX(xsrcMask
);
1290 srcMask_y
= source
->LogicalToDeviceY(ysrcMask
);
1294 mask
= ScaleMask(mask
, srcMask_x
, srcMask_y
,
1295 src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1299 if (overlap
== wxPartRegion
)
1301 // need a new mask that also masks the clipped area,
1302 // because gc can't have both a mask and a clip region
1303 mask
= ClipMask(mask
, clipRegion
,
1304 srcMask_x
, srcMask_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1308 gdk_gc_set_clip_mask(use_gc
, mask
);
1309 gdk_gc_set_clip_origin(use_gc
, dst_x
- srcMask_x
, dst_y
- srcMask_y
);
1312 GdkPixmap
* pixmap
= NULL
;
1313 if (gdk_drawable_get_depth(srcDrawable
) == 1)
1315 // Convert mono pixmap to color using text fg/bg colors.
1316 // Scaling/drawing is simpler if this is done first.
1317 pixmap
= MonoToColor(srcDrawable
, src_x
, src_y
, src_w
, src_h
);
1318 srcDrawable
= pixmap
;
1323 const wxRasterOperationMode logical_func_save
= m_logicalFunction
;
1324 SetLogicalFunction(logical_func
);
1326 gdk_gc_set_subwindow(use_gc
, GDK_INCLUDE_INFERIORS
);
1330 GdkPixbuf
* pixbuf
= Scale(srcDrawable
,
1331 src_x
, src_y
, src_w
, src_h
, dst_w
, dst_h
, scale_x
, scale_y
);
1332 gdk_draw_pixbuf(m_gdkwindow
, use_gc
, pixbuf
,
1333 0, 0, dst_x
, dst_y
, dst_w
, dst_h
, GDK_RGB_DITHER_NONE
, 0, 0);
1334 g_object_unref(pixbuf
);
1338 gdk_draw_drawable(m_gdkwindow
, use_gc
, srcDrawable
,
1339 src_x
, src_y
, dst_x
, dst_y
, dst_w
, dst_h
);
1342 SetLogicalFunction(logical_func_save
);
1344 gdk_gc_set_subwindow(use_gc
, GDK_CLIP_BY_CHILDREN
);
1347 g_object_unref(pixmap
);
1350 g_object_unref(mask
);
1351 gdk_gc_set_clip_region(use_gc
, clipRegion
);
1356 void wxWindowDCImpl::DoDrawText(const wxString
& text
,
1360 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1362 if (!m_gdkwindow
) return;
1364 if (text
.empty()) return;
1366 wxCoord x
= XLOG2DEV(xLogical
),
1367 y
= YLOG2DEV(yLogical
);
1369 wxCHECK_RET( m_context
, wxT("no Pango context") );
1370 wxCHECK_RET( m_layout
, wxT("no Pango layout") );
1371 wxCHECK_RET( m_fontdesc
, wxT("no Pango font description") );
1373 gdk_pango_context_set_colormap( m_context
, m_cmap
); // not needed in gtk+ >= 2.6
1375 wxCharBuffer data
= wxGTK_CONV(text
);
1379 pango_layout_set_text(m_layout
, data
, data
.length());
1380 const bool setAttrs
= m_font
.GTKSetPangoAttrs(m_layout
);
1383 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1386 // If there is a user or actually any scale applied to
1387 // the device context, scale the font.
1389 // scale font description
1390 oldSize
= pango_font_description_get_size(m_fontdesc
);
1391 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1393 // actually apply scaled font
1394 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1398 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1402 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1405 const GdkColor
* bg_col
= NULL
;
1406 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1407 bg_col
= m_textBackgroundColour
.GetColor();
1409 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x_rtl
, y
, m_layout
, NULL
, bg_col
);
1413 // reset unscaled size
1414 pango_font_description_set_size( m_fontdesc
, oldSize
);
1416 // actually apply unscaled font
1417 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1421 // undo underline attributes setting:
1422 pango_layout_set_attributes(m_layout
, NULL
);
1425 CalcBoundingBox(xLogical
+ int(w
/ m_scaleX
), yLogical
+ int(h
/ m_scaleY
));
1426 CalcBoundingBox(xLogical
, yLogical
);
1429 // TODO: When GTK2.6 is required, merge DoDrawText and DoDrawRotatedText to
1430 // avoid code duplication
1431 void wxWindowDCImpl::DoDrawRotatedText( const wxString
&text
, wxCoord x
, wxCoord y
, double angle
)
1433 if (!m_gdkwindow
|| text
.empty())
1436 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1439 if (!gtk_check_version(2,6,0))
1444 pango_layout_set_text(m_layout
, wxGTK_CONV(text
), -1);
1445 const bool setAttrs
= m_font
.GTKSetPangoAttrs(m_layout
);
1447 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1450 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1451 // If there is a user or actually any scale applied to
1452 // the device context, scale the font.
1454 // scale font description
1455 oldSize
= pango_font_description_get_size(m_fontdesc
);
1456 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1458 // actually apply scaled font
1459 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1463 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1465 const GdkColor
* bg_col
= NULL
;
1466 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1467 bg_col
= m_textBackgroundColour
.GetColor();
1470 PangoMatrix matrix
= PANGO_MATRIX_INIT
;
1471 pango_matrix_rotate (&matrix
, angle
);
1472 pango_context_set_matrix (m_context
, &matrix
);
1473 pango_layout_context_changed (m_layout
);
1475 // To be compatible with MSW, the rotation axis must be in the old
1477 // Calculate the vertices of the rotated rectangle containing the text,
1478 // relative to the old top-left vertex.
1479 // We could use the matrix for this, but it's simpler with trignonometry.
1480 double rad
= DegToRad(angle
);
1481 // the rectangle vertices are counted clockwise with the first one
1483 double x2
= w
* cos(rad
);
1484 double y2
= -w
* sin(rad
); // y axis points to the bottom, hence minus
1485 double x4
= h
* sin(rad
);
1486 double y4
= h
* cos(rad
);
1487 double x3
= x4
+ x2
;
1488 double y3
= y4
+ y2
;
1489 // Then we calculate max and min of the rotated rectangle.
1490 wxCoord maxX
= (wxCoord
)(dmax(dmax(0, x2
), dmax(x3
, x4
)) + 0.5),
1491 maxY
= (wxCoord
)(dmax(dmax(0, y2
), dmax(y3
, y4
)) + 0.5),
1492 minX
= (wxCoord
)(dmin(dmin(0, x2
), dmin(x3
, x4
)) - 0.5),
1493 minY
= (wxCoord
)(dmin(dmin(0, y2
), dmin(y3
, y4
)) - 0.5);
1495 gdk_draw_layout_with_colors(m_gdkwindow
, m_textGC
, x
+minX
, y
+minY
,
1496 m_layout
, NULL
, bg_col
);
1499 pango_layout_set_attributes(m_layout
, NULL
);
1501 // clean up the transformation matrix
1502 pango_context_set_matrix(m_context
, NULL
);
1506 // reset unscaled size
1507 pango_font_description_set_size( m_fontdesc
, oldSize
);
1509 // actually apply unscaled font
1510 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1513 CalcBoundingBox(x
+minX
, y
+minY
);
1514 CalcBoundingBox(x
+maxX
, y
+maxY
);
1517 #endif //__WXGTK26__
1520 if ( wxIsNullDouble(angle
) )
1522 DoDrawText(text
, x
, y
);
1529 // TODO: implement later without GdkFont for GTK 2.0
1530 DoGetTextExtent(text
, &w
, &h
, NULL
,NULL
, &m_font
);
1532 // draw the string normally
1535 dc
.SelectObject(src
);
1536 dc
.SetFont(GetFont());
1537 dc
.SetBackground(*wxBLACK_BRUSH
);
1538 dc
.SetBrush(*wxBLACK_BRUSH
);
1540 dc
.SetTextForeground( *wxWHITE
);
1541 dc
.DrawText(text
, 0, 0);
1542 dc
.SelectObject(wxNullBitmap
);
1544 // Calculate the size of the rotated bounding box.
1545 double rad
= DegToRad(angle
);
1546 double dx
= cos(rad
),
1549 // the rectngle vertices are counted clockwise with the first one being at
1550 // (0, 0) (or, rather, at (x, y))
1552 y2
= -w
*dy
; // y axis points to the bottom, hence minus
1555 double x3
= x4
+ x2
,
1559 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5),
1560 maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5),
1561 minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5),
1562 minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1565 wxImage image
= src
.ConvertToImage();
1567 image
.ConvertColourToAlpha( m_textForegroundColour
.Red(),
1568 m_textForegroundColour
.Green(),
1569 m_textForegroundColour
.Blue() );
1570 image
= image
.Rotate( rad
, wxPoint(0,0) );
1572 int i_angle
= (int) angle
;
1573 i_angle
= i_angle
% 360;
1577 if ((i_angle
>= 90.0) && (i_angle
< 270.0))
1578 xoffset
= image
.GetWidth();
1580 if ((i_angle
>= 0.0) && (i_angle
< 180.0))
1581 yoffset
= image
.GetHeight();
1583 if ((i_angle
>= 0) && (i_angle
< 90))
1584 yoffset
-= (int)( cos(rad
)*h
);
1585 if ((i_angle
>= 90) && (i_angle
< 180))
1586 xoffset
-= (int)( sin(rad
)*h
);
1587 if ((i_angle
>= 180) && (i_angle
< 270))
1588 yoffset
-= (int)( cos(rad
)*h
);
1589 if ((i_angle
>= 270) && (i_angle
< 360))
1590 xoffset
-= (int)( sin(rad
)*h
);
1592 int i_x
= x
- xoffset
;
1593 int i_y
= y
- yoffset
;
1596 DoDrawBitmap( src
, i_x
, i_y
, true );
1599 // it would be better to draw with non underlined font and draw the line
1600 // manually here (it would be more straight...)
1602 if ( m_font
.GetUnderlined() )
1604 gdk_draw_line( m_gdkwindow
, m_textGC
,
1605 XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
),
1606 XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1610 // update the bounding box
1611 CalcBoundingBox(x
+ minX
, y
+ minY
);
1612 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1613 #else // !wxUSE_IMAGE
1618 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
1622 void wxWindowDCImpl::DoGetTextExtent(const wxString
&string
,
1623 wxCoord
*width
, wxCoord
*height
,
1624 wxCoord
*descent
, wxCoord
*externalLeading
,
1625 const wxFont
*theFont
) const
1633 if ( externalLeading
)
1634 *externalLeading
= 0;
1639 // ensure that theFont is always non-NULL
1640 if ( !theFont
|| !theFont
->IsOk() )
1643 // and use it if it's valid
1644 if ( theFont
->IsOk() )
1646 pango_layout_set_font_description
1649 theFont
->GetNativeFontInfo()->description
1653 // Set layout's text
1654 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *theFont
);
1657 // hardly ideal, but what else can we do if conversion failed?
1661 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1664 pango_layout_get_pixel_size(m_layout
, width
, &h
);
1667 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1668 int baseline
= pango_layout_iter_get_baseline(iter
);
1669 pango_layout_iter_free(iter
);
1670 *descent
= h
- PANGO_PIXELS(baseline
);
1675 // Reset old font description
1676 if (theFont
->IsOk())
1677 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1681 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString
& text
,
1682 wxArrayInt
& widths
) const
1684 const size_t len
= text
.length();
1691 // Set layout's text
1692 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, m_font
);
1695 // hardly ideal, but what else can we do if conversion failed?
1696 wxLogLastError(wxT("DoGetPartialTextExtents"));
1700 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1702 // Calculate the position of each character based on the widths of
1703 // the previous characters
1705 // Code borrowed from Scintilla's PlatGTK
1706 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1708 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1710 while (pango_layout_iter_next_cluster(iter
))
1712 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1713 int position
= PANGO_PIXELS(pos
.x
);
1714 widths
[i
++] = position
;
1717 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
1718 pango_layout_iter_free(iter
);
1724 wxCoord
wxWindowDCImpl::GetCharWidth() const
1726 pango_layout_set_text( m_layout
, "H", 1 );
1728 pango_layout_get_pixel_size( m_layout
, &w
, NULL
);
1732 wxCoord
wxWindowDCImpl::GetCharHeight() const
1734 PangoFontMetrics
*metrics
= pango_context_get_metrics (m_context
, m_fontdesc
, pango_context_get_language(m_context
));
1735 wxCHECK_MSG( metrics
, -1, wxT("failed to get pango font metrics") );
1737 wxCoord h
= PANGO_PIXELS (pango_font_metrics_get_descent (metrics
) +
1738 pango_font_metrics_get_ascent (metrics
));
1739 pango_font_metrics_unref (metrics
);
1743 void wxWindowDCImpl::Clear()
1745 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1747 if (!m_gdkwindow
) return;
1750 DoGetSize( &width
, &height
);
1751 gdk_draw_rectangle( m_gdkwindow
, m_bgGC
, TRUE
, 0, 0, width
, height
);
1754 void wxWindowDCImpl::SetFont( const wxFont
&font
)
1761 pango_font_description_free( m_fontdesc
);
1763 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1768 PangoContext
*oldContext
= m_context
;
1770 m_context
= m_window
->GTKGetPangoDefaultContext();
1772 // If we switch back/forth between different contexts
1773 // we also have to create a new layout. I think so,
1774 // at least, and it doesn't hurt to do it.
1775 if (oldContext
!= m_context
)
1778 g_object_unref (m_layout
);
1780 m_layout
= pango_layout_new( m_context
);
1784 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1788 void wxWindowDCImpl::SetPen( const wxPen
&pen
)
1790 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1792 if (m_pen
== pen
) return;
1796 if (!m_pen
.IsOk()) return;
1798 if (!m_gdkwindow
) return;
1800 gint width
= m_pen
.GetWidth();
1803 // CMB: if width is non-zero scale it with the dc
1808 // X doesn't allow different width in x and y and so we take
1811 ( fabs((double) XLOG2DEVREL(width
)) +
1812 fabs((double) YLOG2DEVREL(width
)) ) / 2.0;
1816 // width can't be 0 or an internal GTK error occurs inside
1817 // gdk_gc_set_dashes() below
1822 static const wxGTKDash dotted
[] = {1, 1};
1823 static const wxGTKDash short_dashed
[] = {2, 2};
1824 static const wxGTKDash wxCoord_dashed
[] = {2, 4};
1825 static const wxGTKDash dotted_dashed
[] = {3, 3, 1, 3};
1827 // We express dash pattern in pen width unit, so we are
1828 // independent of zoom factor and so on...
1830 const wxGTKDash
*req_dash
;
1832 GdkLineStyle lineStyle
= GDK_LINE_ON_OFF_DASH
;
1833 switch (m_pen
.GetStyle())
1835 case wxPENSTYLE_USER_DASH
:
1836 req_nb_dash
= m_pen
.GetDashCount();
1837 req_dash
= (wxGTKDash
*)m_pen
.GetDash();
1839 case wxPENSTYLE_DOT
:
1843 case wxPENSTYLE_LONG_DASH
:
1845 req_dash
= wxCoord_dashed
;
1847 case wxPENSTYLE_SHORT_DASH
:
1849 req_dash
= short_dashed
;
1851 case wxPENSTYLE_DOT_DASH
:
1853 req_dash
= dotted_dashed
;
1856 case wxPENSTYLE_TRANSPARENT
:
1857 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
1858 case wxPENSTYLE_STIPPLE
:
1859 case wxPENSTYLE_SOLID
:
1861 lineStyle
= GDK_LINE_SOLID
;
1867 if (req_dash
&& req_nb_dash
)
1869 wxGTKDash
*real_req_dash
= new wxGTKDash
[req_nb_dash
];
1872 for (int i
= 0; i
< req_nb_dash
; i
++)
1873 real_req_dash
[i
] = req_dash
[i
] * width
;
1874 gdk_gc_set_dashes( m_penGC
, 0, real_req_dash
, req_nb_dash
);
1875 delete[] real_req_dash
;
1879 // No Memory. We use non-scaled dash pattern...
1880 gdk_gc_set_dashes( m_penGC
, 0, (wxGTKDash
*)req_dash
, req_nb_dash
);
1884 GdkCapStyle capStyle
= GDK_CAP_ROUND
;
1885 switch (m_pen
.GetCap())
1887 case wxCAP_PROJECTING
: { capStyle
= GDK_CAP_PROJECTING
; break; }
1888 case wxCAP_BUTT
: { capStyle
= GDK_CAP_BUTT
; break; }
1894 capStyle
= GDK_CAP_NOT_LAST
;
1899 GdkJoinStyle joinStyle
= GDK_JOIN_ROUND
;
1900 switch (m_pen
.GetJoin())
1902 case wxJOIN_BEVEL
: { joinStyle
= GDK_JOIN_BEVEL
; break; }
1903 case wxJOIN_MITER
: { joinStyle
= GDK_JOIN_MITER
; break; }
1905 default: { joinStyle
= GDK_JOIN_ROUND
; break; }
1908 gdk_gc_set_line_attributes( m_penGC
, width
, lineStyle
, capStyle
, joinStyle
);
1910 m_pen
.GetColour().CalcPixel( m_cmap
);
1911 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
1914 void wxWindowDCImpl::SetBrush( const wxBrush
&brush
)
1916 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1918 if (m_brush
== brush
) return;
1922 if (!m_brush
.IsOk()) return;
1924 if (!m_gdkwindow
) return;
1926 m_brush
.GetColour().CalcPixel( m_cmap
);
1927 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
1929 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
1931 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
) && (m_brush
.GetStipple()->IsOk()))
1933 if (m_brush
.GetStipple()->GetDepth() != 1)
1935 gdk_gc_set_fill( m_brushGC
, GDK_TILED
);
1936 gdk_gc_set_tile( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1940 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1941 gdk_gc_set_stipple( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1945 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
) && (m_brush
.GetStipple()->GetMask()))
1947 gdk_gc_set_fill( m_textGC
, GDK_OPAQUE_STIPPLED
);
1948 gdk_gc_set_stipple( m_textGC
, m_brush
.GetStipple()->GetMask()->GetBitmap() );
1951 if (m_brush
.IsHatch())
1953 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1954 gdk_gc_set_stipple(m_brushGC
, GetHatch(m_brush
.GetStyle()));
1958 void wxWindowDCImpl::SetBackground( const wxBrush
&brush
)
1960 /* CMB 21/7/98: Added SetBackground. Sets background brush
1961 * for Clear() and bg colour for shapes filled with cross-hatch brush */
1963 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1965 if (m_backgroundBrush
== brush
) return;
1967 m_backgroundBrush
= brush
;
1969 if (!m_backgroundBrush
.IsOk()) return;
1971 if (!m_gdkwindow
) return;
1973 wxColor color
= m_backgroundBrush
.GetColour();
1974 color
.CalcPixel(m_cmap
);
1975 const GdkColor
* gdkColor
= color
.GetColor();
1976 gdk_gc_set_background(m_brushGC
, gdkColor
);
1977 gdk_gc_set_background(m_penGC
, gdkColor
);
1978 gdk_gc_set_background(m_bgGC
, gdkColor
);
1979 gdk_gc_set_foreground(m_bgGC
, gdkColor
);
1982 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
1984 if (m_backgroundBrush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
)
1986 const wxBitmap
* stipple
= m_backgroundBrush
.GetStipple();
1987 if (stipple
->IsOk())
1989 if (stipple
->GetDepth() != 1)
1991 gdk_gc_set_fill(m_bgGC
, GDK_TILED
);
1992 gdk_gc_set_tile(m_bgGC
, stipple
->GetPixmap());
1996 gdk_gc_set_fill(m_bgGC
, GDK_STIPPLED
);
1997 gdk_gc_set_stipple(m_bgGC
, stipple
->GetPixmap());
2001 else if (m_backgroundBrush
.IsHatch())
2003 gdk_gc_set_fill( m_bgGC
, GDK_STIPPLED
);
2004 gdk_gc_set_stipple(m_bgGC
, GetHatch(m_backgroundBrush
.GetStyle()));
2008 void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
2010 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2012 if (m_logicalFunction
== function
)
2015 // VZ: shouldn't this be a CHECK?
2022 case wxXOR
: mode
= GDK_XOR
; break;
2023 case wxINVERT
: mode
= GDK_INVERT
; break;
2024 case wxOR_REVERSE
: mode
= GDK_OR_REVERSE
; break;
2025 case wxAND_REVERSE
: mode
= GDK_AND_REVERSE
; break;
2026 case wxCLEAR
: mode
= GDK_CLEAR
; break;
2027 case wxSET
: mode
= GDK_SET
; break;
2028 case wxOR_INVERT
: mode
= GDK_OR_INVERT
; break;
2029 case wxAND
: mode
= GDK_AND
; break;
2030 case wxOR
: mode
= GDK_OR
; break;
2031 case wxEQUIV
: mode
= GDK_EQUIV
; break;
2032 case wxNAND
: mode
= GDK_NAND
; break;
2033 case wxAND_INVERT
: mode
= GDK_AND_INVERT
; break;
2034 case wxCOPY
: mode
= GDK_COPY
; break;
2035 case wxNO_OP
: mode
= GDK_NOOP
; break;
2036 case wxSRC_INVERT
: mode
= GDK_COPY_INVERT
; break;
2037 case wxNOR
: mode
= GDK_NOR
; break;
2039 wxFAIL_MSG("unknown mode");
2043 m_logicalFunction
= function
;
2045 gdk_gc_set_function( m_penGC
, mode
);
2046 gdk_gc_set_function( m_brushGC
, mode
);
2048 // to stay compatible with wxMSW, we don't apply ROPs to the text
2049 // operations (i.e. DrawText/DrawRotatedText).
2050 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2051 gdk_gc_set_function( m_textGC
, mode
);
2054 void wxWindowDCImpl::SetTextForeground( const wxColour
&col
)
2056 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2058 // don't set m_textForegroundColour to an invalid colour as we'd crash
2059 // later then (we use m_textForegroundColour.GetColor() without checking
2061 if ( !col
.IsOk() || (m_textForegroundColour
== col
) )
2064 m_textForegroundColour
= col
;
2068 m_textForegroundColour
.CalcPixel( m_cmap
);
2069 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
2073 void wxWindowDCImpl::SetTextBackground( const wxColour
&col
)
2075 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2078 if ( !col
.IsOk() || (m_textBackgroundColour
== col
) )
2081 m_textBackgroundColour
= col
;
2085 m_textBackgroundColour
.CalcPixel( m_cmap
);
2086 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
2090 void wxWindowDCImpl::SetBackgroundMode( int mode
)
2092 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2094 m_backgroundMode
= mode
;
2097 void wxWindowDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
2099 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
2102 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2104 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2106 if (!m_gdkwindow
) return;
2109 rect
.x
= XLOG2DEV(x
);
2110 rect
.y
= YLOG2DEV(y
);
2111 rect
.width
= XLOG2DEVREL(width
);
2112 rect
.height
= YLOG2DEVREL(height
);
2114 if (m_window
&& m_window
->m_wxwindow
&&
2115 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2117 rect
.x
-= rect
.width
;
2120 DoSetDeviceClippingRegion(wxRegion(rect
));
2123 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion
®ion
)
2125 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2129 DestroyClippingRegion();
2133 if (!m_gdkwindow
) return;
2135 if (!m_currentClippingRegion
.IsNull())
2136 m_currentClippingRegion
.Intersect( region
);
2138 m_currentClippingRegion
.Union( region
);
2140 #if USE_PAINT_REGION
2141 if (!m_paintClippingRegion
.IsNull())
2142 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2145 wxCoord xx
, yy
, ww
, hh
;
2146 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2147 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2149 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2150 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2151 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2152 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2153 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2156 void wxWindowDCImpl::DestroyClippingRegion()
2158 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2160 wxDCImpl::DestroyClippingRegion();
2162 m_currentClippingRegion
.Clear();
2164 #if USE_PAINT_REGION
2165 if (!m_paintClippingRegion
.IsEmpty())
2166 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2169 if (!m_gdkwindow
) return;
2171 GdkRegion
* gdkRegion
= NULL
;
2172 if (!m_currentClippingRegion
.IsEmpty())
2173 gdkRegion
= m_currentClippingRegion
.GetRegion();
2175 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2176 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2177 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2178 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2181 void wxWindowDCImpl::Destroy()
2183 if (m_penGC
) wxFreePoolGC( m_penGC
);
2185 if (m_brushGC
) wxFreePoolGC( m_brushGC
);
2187 if (m_textGC
) wxFreePoolGC( m_textGC
);
2189 if (m_bgGC
) wxFreePoolGC( m_bgGC
);
2193 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
2195 m_deviceOriginX
= x
;
2196 m_deviceOriginY
= y
;
2198 ComputeScaleAndOrigin();
2201 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
2203 m_signX
= (xLeftRight
? 1 : -1);
2204 m_signY
= (yBottomUp
? -1 : 1);
2206 if (m_window
&& m_window
->m_wxwindow
&&
2207 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2210 ComputeScaleAndOrigin();
2213 void wxWindowDCImpl::ComputeScaleAndOrigin()
2215 const wxRealPoint
origScale(m_scaleX
, m_scaleY
);
2217 wxDCImpl::ComputeScaleAndOrigin();
2219 // if scale has changed call SetPen to recalulate the line width
2220 if ( wxRealPoint(m_scaleX
, m_scaleY
) != origScale
&& m_pen
.IsOk() )
2222 // this is a bit artificial, but we need to force wxDC to think the pen
2230 // Resolution in pixels per logical inch
2231 wxSize
wxWindowDCImpl::GetPPI() const
2233 return wxSize( (int) (m_mm_to_pix_x
* 25.4 + 0.5), (int) (m_mm_to_pix_y
* 25.4 + 0.5));
2236 int wxWindowDCImpl::GetDepth() const
2238 return gdk_drawable_get_depth(m_gdkwindow
);
2241 //-----------------------------------------------------------------------------
2243 //-----------------------------------------------------------------------------
2245 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
2247 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
2248 : wxWindowDCImpl( owner
)
2252 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*win
)
2253 : wxWindowDCImpl( owner
, win
)
2255 wxCHECK_RET( win
, wxT("NULL window in wxClientDCImpl::wxClientDC") );
2257 #ifdef __WXUNIVERSAL__
2258 wxPoint ptOrigin
= win
->GetClientAreaOrigin();
2259 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
2260 wxSize size
= win
->GetClientSize();
2261 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
2266 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
2268 wxCHECK_RET( m_window
, wxT("GetSize() doesn't work without window") );
2270 m_window
->GetClientSize( width
, height
);
2273 //-----------------------------------------------------------------------------
2275 //-----------------------------------------------------------------------------
2277 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
2279 // Limit the paint region to the window size. Sometimes
2280 // the paint region is too big, and this risks X11 errors
2281 static void wxLimitRegionToSize(wxRegion
& region
, const wxSize
& sz
)
2283 wxRect originalRect
= region
.GetBox();
2284 wxRect
rect(originalRect
);
2285 if (rect
.width
+ rect
.x
> sz
.x
)
2286 rect
.width
= sz
.x
- rect
.x
;
2287 if (rect
.height
+ rect
.y
> sz
.y
)
2288 rect
.height
= sz
.y
- rect
.y
;
2289 if (rect
!= originalRect
)
2291 region
= wxRegion(rect
);
2292 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2293 originalRect
.x
, originalRect
.y
, originalRect
.width
, originalRect
.height
,
2294 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2298 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
2299 : wxClientDCImpl( owner
)
2303 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*win
)
2304 : wxClientDCImpl( owner
, win
)
2306 #if USE_PAINT_REGION
2307 if (!win
->m_clipPaintRegion
)
2310 wxSize sz
= win
->GetSize();
2311 m_paintClippingRegion
= win
->m_nativeUpdateRegion
;
2312 wxLimitRegionToSize(m_paintClippingRegion
, sz
);
2314 GdkRegion
*region
= m_paintClippingRegion
.GetRegion();
2317 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2318 wxLimitRegionToSize(m_currentClippingRegion
, sz
);
2320 if (sz
.x
<= 0 || sz
.y
<= 0)
2323 gdk_gc_set_clip_region( m_penGC
, region
);
2324 gdk_gc_set_clip_region( m_brushGC
, region
);
2325 gdk_gc_set_clip_region( m_textGC
, region
);
2326 gdk_gc_set_clip_region( m_bgGC
, region
);
2331 // ----------------------------------------------------------------------------
2333 // ----------------------------------------------------------------------------
2335 class wxDCModule
: public wxModule
2342 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2345 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2347 bool wxDCModule::OnInit()
2353 void wxDCModule::OnExit()
2357 for (int i
= wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
; i
--; )
2360 g_object_unref(hatches
[i
]);