1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dcclient.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Chris Breeze
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
14 #define XCopyPlane XCOPYPLANE
17 #include "wx/gtk/dcclient.h"
20 #include "wx/window.h"
22 #include "wx/dcmemory.h"
25 #include "wx/module.h"
28 #include "wx/fontutil.h"
30 #include "wx/gtk/private.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 #define XLOG2DEV(x) LogicalToDeviceX(x)
39 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
40 #define YLOG2DEV(y) LogicalToDeviceY(y)
41 #define YLOG2DEVREL(y) LogicalToDeviceYRel(y)
43 #define USE_PAINT_REGION 1
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
56 static GdkPixmap
* hatches
[wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
+ 1];
58 extern GtkWidget
*wxGetRootWindow();
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 static const double RAD2DEG
= 180.0 / M_PI
;
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
71 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
73 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
75 static GdkPixmap
* GetHatch(int style
)
77 wxASSERT(style
>= wxBRUSHSTYLE_FIRST_HATCH
&& style
<= wxBRUSHSTYLE_LAST_HATCH
);
78 const int i
= style
- wxBRUSHSTYLE_FIRST_HATCH
;
79 if (hatches
[i
] == NULL
)
83 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
84 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, bdiag_bits
, bdiag_width
, bdiag_height
);
86 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
87 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, cdiag_bits
, cdiag_width
, cdiag_height
);
89 case wxBRUSHSTYLE_CROSS_HATCH
:
90 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, cross_bits
, cross_width
, cross_height
);
92 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
93 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, fdiag_bits
, fdiag_width
, fdiag_height
);
95 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
96 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, horiz_bits
, horiz_width
, horiz_height
);
98 case wxBRUSHSTYLE_VERTICAL_HATCH
:
99 hatches
[i
] = gdk_bitmap_create_from_data(NULL
, verti_bits
, verti_width
, verti_height
);
106 //-----------------------------------------------------------------------------
107 // temporary implementation of the missing GDK function
108 //-----------------------------------------------------------------------------
111 void gdk_wx_draw_bitmap(GdkDrawable
*drawable
,
117 wxCHECK_RET( drawable
, _T("NULL drawable in gdk_wx_draw_bitmap") );
118 wxCHECK_RET( src
, _T("NULL src in gdk_wx_draw_bitmap") );
119 wxCHECK_RET( gc
, _T("NULL gc in gdk_wx_draw_bitmap") );
121 gint src_width
, src_height
;
122 gdk_drawable_get_size(src
, &src_width
, &src_height
);
124 XCopyPlane( GDK_WINDOW_XDISPLAY(drawable
),
126 GDK_WINDOW_XID(drawable
),
129 src_width
, src_height
,
134 //-----------------------------------------------------------------------------
135 // Implement Pool of Graphic contexts. Creating them takes too much time.
136 //-----------------------------------------------------------------------------
162 #define GC_POOL_ALLOC_SIZE 100
164 static int wxGCPoolSize
= 0;
166 static wxGC
*wxGCPool
= NULL
;
168 static void wxInitGCPool()
170 // This really could wait until the first call to
171 // wxGetPoolGC, but we will make the first allocation
172 // now when other initialization is being performed.
174 // Set initial pool size.
175 wxGCPoolSize
= GC_POOL_ALLOC_SIZE
;
177 // Allocate initial pool.
178 wxGCPool
= (wxGC
*)malloc(wxGCPoolSize
* sizeof(wxGC
));
179 if (wxGCPool
== NULL
)
181 // If we cannot malloc, then fail with error
182 // when debug is enabled. If debug is not enabled,
183 // the problem will eventually get caught
185 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
189 // Zero initial pool.
190 memset(wxGCPool
, 0, wxGCPoolSize
* sizeof(wxGC
));
193 static void wxCleanUpGCPool()
195 for (int i
= 0; i
< wxGCPoolSize
; i
++)
197 if (wxGCPool
[i
].m_gc
)
198 g_object_unref (wxGCPool
[i
].m_gc
);
206 static GdkGC
* wxGetPoolGC( GdkWindow
*window
, wxPoolGCType type
)
210 // Look for an available GC.
211 for (int i
= 0; i
< wxGCPoolSize
; i
++)
213 if (!wxGCPool
[i
].m_gc
)
215 wxGCPool
[i
].m_gc
= gdk_gc_new( window
);
216 gdk_gc_set_exposures( wxGCPool
[i
].m_gc
, FALSE
);
217 wxGCPool
[i
].m_type
= type
;
218 wxGCPool
[i
].m_used
= false;
220 if ((!wxGCPool
[i
].m_used
) && (wxGCPool
[i
].m_type
== type
))
222 wxGCPool
[i
].m_used
= true;
223 return wxGCPool
[i
].m_gc
;
227 // We did not find an available GC.
228 // We need to grow the GC pool.
229 pptr
= (wxGC
*)realloc(wxGCPool
,
230 (wxGCPoolSize
+ GC_POOL_ALLOC_SIZE
)*sizeof(wxGC
));
233 // Initialize newly allocated pool.
235 memset(&wxGCPool
[wxGCPoolSize
], 0,
236 GC_POOL_ALLOC_SIZE
*sizeof(wxGC
));
238 // Initialize entry we will return.
239 wxGCPool
[wxGCPoolSize
].m_gc
= gdk_gc_new( window
);
240 gdk_gc_set_exposures( wxGCPool
[wxGCPoolSize
].m_gc
, FALSE
);
241 wxGCPool
[wxGCPoolSize
].m_type
= type
;
242 wxGCPool
[wxGCPoolSize
].m_used
= true;
244 // Set new value of pool size.
245 wxGCPoolSize
+= GC_POOL_ALLOC_SIZE
;
247 // Return newly allocated entry.
248 return wxGCPool
[wxGCPoolSize
-GC_POOL_ALLOC_SIZE
].m_gc
;
251 // The realloc failed. Fall through to error.
252 wxFAIL_MSG( wxT("No GC available") );
254 return (GdkGC
*) NULL
;
257 static void wxFreePoolGC( GdkGC
*gc
)
259 for (int i
= 0; i
< wxGCPoolSize
; i
++)
261 if (wxGCPool
[i
].m_gc
== gc
)
263 wxGCPool
[i
].m_used
= false;
268 wxFAIL_MSG( wxT("Wrong GC") );
271 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
275 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl
, wxGTKDCImpl
)
277 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
) :
280 m_gdkwindow
= (GdkWindow
*) NULL
;
281 m_penGC
= (GdkGC
*) NULL
;
282 m_brushGC
= (GdkGC
*) NULL
;
283 m_textGC
= (GdkGC
*) NULL
;
284 m_bgGC
= (GdkGC
*) NULL
;
285 m_cmap
= (GdkColormap
*) NULL
;
286 m_isScreenDC
= false;
287 m_context
= (PangoContext
*)NULL
;
288 m_layout
= (PangoLayout
*)NULL
;
289 m_fontdesc
= (PangoFontDescription
*)NULL
;
292 wxWindowDCImpl::wxWindowDCImpl( wxDC
*owner
, wxWindow
*window
) :
295 wxASSERT_MSG( window
, wxT("DC needs a window") );
297 m_gdkwindow
= (GdkWindow
*) NULL
;
298 m_penGC
= (GdkGC
*) NULL
;
299 m_brushGC
= (GdkGC
*) NULL
;
300 m_textGC
= (GdkGC
*) NULL
;
301 m_bgGC
= (GdkGC
*) NULL
;
302 m_cmap
= (GdkColormap
*) NULL
;
303 m_isScreenDC
= false;
304 m_font
= window
->GetFont();
306 GtkWidget
*widget
= window
->m_wxwindow
;
308 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
309 // code should still be able to create wxClientDCs for them, so we will
310 // use the parent window here then.
313 window
= window
->GetParent();
314 widget
= window
->m_wxwindow
;
317 wxASSERT_MSG( widget
, wxT("DC needs a widget") );
319 m_context
= window
->GtkGetPangoDefaultContext();
320 m_layout
= pango_layout_new( m_context
);
321 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
323 m_gdkwindow
= widget
->window
;
325 // Window not realized ?
328 // Don't report problems as per MSW.
334 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
338 /* this must be done after SetUpDC, bacause SetUpDC calls the
339 repective SetBrush, SetPen, SetBackground etc functions
340 to set up the DC. SetBackground call m_owner->SetBackground
341 and this might not be desired as the standard dc background
342 is white whereas a window might assume gray to be the
343 standard (as e.g. wxStatusBar) */
347 if (m_window
&& m_window
->m_wxwindow
&&
348 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
353 // origin in the upper right corner
354 m_deviceOriginX
= m_window
->GetClientSize().x
;
358 wxWindowDCImpl::~wxWindowDCImpl()
363 g_object_unref (m_layout
);
365 pango_font_description_free( m_fontdesc
);
368 void wxWindowDCImpl::SetUpDC( bool isMemDC
)
372 wxASSERT_MSG( !m_penGC
, wxT("GCs already created") );
376 if ((isMemDC
) && (GetSelectedBitmap().IsOk()))
378 if (GetSelectedBitmap().GetDepth() == 1)
380 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_MONO
);
381 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_MONO
);
382 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_MONO
);
383 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_MONO
);
392 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_SCREEN
);
393 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_SCREEN
);
394 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_SCREEN
);
395 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_SCREEN
);
399 m_penGC
= wxGetPoolGC( m_gdkwindow
, wxPEN_COLOUR
);
400 m_brushGC
= wxGetPoolGC( m_gdkwindow
, wxBRUSH_COLOUR
);
401 m_textGC
= wxGetPoolGC( m_gdkwindow
, wxTEXT_COLOUR
);
402 m_bgGC
= wxGetPoolGC( m_gdkwindow
, wxBG_COLOUR
);
406 /* background colour */
407 m_backgroundBrush
= *wxWHITE_BRUSH
;
408 m_backgroundBrush
.GetColour().CalcPixel( m_cmap
);
409 const GdkColor
*bg_col
= m_backgroundBrush
.GetColour().GetColor();
412 m_textForegroundColour
.CalcPixel( m_cmap
);
413 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
415 m_textBackgroundColour
.CalcPixel( m_cmap
);
416 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
418 gdk_gc_set_fill( m_textGC
, GDK_SOLID
);
420 gdk_gc_set_colormap( m_textGC
, m_cmap
);
423 m_pen
.GetColour().CalcPixel( m_cmap
);
424 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
425 gdk_gc_set_background( m_penGC
, bg_col
);
427 gdk_gc_set_line_attributes( m_penGC
, 0, GDK_LINE_SOLID
, GDK_CAP_NOT_LAST
, GDK_JOIN_ROUND
);
430 m_brush
.GetColour().CalcPixel( m_cmap
);
431 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
432 gdk_gc_set_background( m_brushGC
, bg_col
);
434 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
437 gdk_gc_set_background( m_bgGC
, bg_col
);
438 gdk_gc_set_foreground( m_bgGC
, bg_col
);
440 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
443 gdk_gc_set_function( m_textGC
, GDK_COPY
);
444 gdk_gc_set_function( m_brushGC
, GDK_COPY
);
445 gdk_gc_set_function( m_penGC
, GDK_COPY
);
448 gdk_gc_set_clip_rectangle( m_penGC
, (GdkRectangle
*) NULL
);
449 gdk_gc_set_clip_rectangle( m_brushGC
, (GdkRectangle
*) NULL
);
450 gdk_gc_set_clip_rectangle( m_textGC
, (GdkRectangle
*) NULL
);
451 gdk_gc_set_clip_rectangle( m_bgGC
, (GdkRectangle
*) NULL
);
454 void wxWindowDCImpl::DoGetSize( int* width
, int* height
) const
456 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
458 m_window
->GetSize(width
, height
);
461 bool wxWindowDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
462 const wxColour
& col
, int style
)
465 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
466 const wxColour
& col
, int style
);
468 return wxDoFloodFill( GetOwner(), x
, y
, col
, style
);
479 bool wxWindowDCImpl::DoGetPixel( wxCoord x1
, wxCoord y1
, wxColour
*col
) const
482 // Generic (and therefore rather inefficient) method.
483 // Could be improved.
485 wxBitmap
bitmap(1, 1);
486 memdc
.SelectObject(bitmap
);
487 memdc
.Blit(0, 0, 1, 1, GetOwner(), x1
, y1
);
488 memdc
.SelectObject(wxNullBitmap
);
490 wxImage image
= bitmap
.ConvertToImage();
491 col
->Set(image
.GetRed(0, 0), image
.GetGreen(0, 0), image
.GetBlue(0, 0));
493 #else // !wxUSE_IMAGE
499 #endif // wxUSE_IMAGE/!wxUSE_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
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
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
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
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
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
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
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
632 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xxc
-r
, yyc
-r
, 2*r
,2*r
, alpha1
, alpha2
);
634 if ((m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
) && (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
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
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
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
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
.GetStyle() != wxPENSTYLE_TRANSPARENT
) && 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") );
698 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
) return;
701 //Check, if scaling is necessary
703 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
705 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
706 GdkPoint
* gpts
= reinterpret_cast<GdkPoint
*>(points
);
709 gpts
= new GdkPoint
[n
];
711 for (int i
= 0; i
< n
; i
++)
715 gpts
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
716 gpts
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
718 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
722 gdk_draw_lines( m_gdkwindow
, m_penGC
, gpts
, n
);
728 void wxWindowDCImpl::DoDrawPolygon( int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int WXUNUSED(fillStyle
) )
730 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
734 //Check, if scaling is necessary
736 xoffset
!= 0 || yoffset
!= 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
738 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
739 GdkPoint
* gdkpoints
= reinterpret_cast<GdkPoint
*>(points
);
742 gdkpoints
= new GdkPoint
[n
];
745 for (i
= 0 ; i
< n
; i
++)
749 gdkpoints
[i
].x
= XLOG2DEV(points
[i
].x
+ xoffset
);
750 gdkpoints
[i
].y
= YLOG2DEV(points
[i
].y
+ yoffset
);
752 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
757 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
761 DrawingSetup(gc
, originChanged
);
763 gdk_draw_polygon(m_gdkwindow
, gc
, true, gdkpoints
, n
);
766 gdk_gc_set_ts_origin(gc
, 0, 0);
769 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
772 for (i = 0 ; i < n ; i++)
774 gdk_draw_line( m_gdkwindow, m_penGC,
777 gdkpoints[(i+1)%n].x,
778 gdkpoints[(i+1)%n].y);
781 gdk_draw_polygon( m_gdkwindow
, m_penGC
, FALSE
, gdkpoints
, n
);
790 void wxWindowDCImpl::DoDrawRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
792 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
794 wxCoord xx
= XLOG2DEV(x
);
795 wxCoord yy
= YLOG2DEV(y
);
796 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
797 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
799 // CMB: draw nothing if transformed w or h is 0
800 if (ww
== 0 || hh
== 0) return;
802 // CMB: handle -ve width and/or height
803 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
804 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
808 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
812 DrawingSetup(gc
, originChanged
);
814 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
);
817 gdk_gc_set_ts_origin(gc
, 0, 0);
820 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
823 if ((m_pen
.GetWidth() == 2) && (m_pen
.GetCap() == wxCAP_ROUND
) &&
824 (m_pen
.GetJoin() == wxJOIN_ROUND
) && (m_pen
.GetStyle() == wxPENSTYLE_SOLID
))
826 // Use 2 1-line rects instead
827 gdk_gc_set_line_attributes( m_penGC
, 1, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
832 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
+1, yy
, ww
-2, hh
-2 );
833 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
-1, ww
, hh
);
837 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-2, hh
-2 );
838 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
-1, yy
-1, ww
, hh
);
842 gdk_gc_set_line_attributes( m_penGC
, 2, GDK_LINE_SOLID
, GDK_CAP_ROUND
, GDK_JOIN_ROUND
);
847 // Just use X11 for other cases
848 gdk_draw_rectangle( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
-1, hh
-1 );
853 CalcBoundingBox( x
, y
);
854 CalcBoundingBox( x
+ width
, y
+ height
);
857 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
859 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
861 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
863 wxCoord xx
= XLOG2DEV(x
);
864 wxCoord yy
= YLOG2DEV(y
);
865 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
866 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
867 wxCoord rr
= XLOG2DEVREL((wxCoord
)radius
);
869 // CMB: handle -ve width and/or height
870 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
871 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
873 // CMB: if radius is zero use DrawRectangle() instead to avoid
874 // X drawing errors with small radii
877 DoDrawRectangle( x
, y
, width
, height
);
881 // CMB: draw nothing if transformed w or h is 0
882 if (ww
== 0 || hh
== 0) return;
884 // CMB: adjust size if outline is drawn otherwise the result is
885 // 1 pixel too wide and high
886 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
894 // CMB: ensure dd is not larger than rectangle otherwise we
895 // get an hour glass shape
897 if (dd
> ww
) dd
= ww
;
898 if (dd
> hh
) dd
= hh
;
901 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
905 DrawingSetup(gc
, originChanged
);
907 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
+rr
, yy
, ww
-dd
+1, hh
);
908 gdk_draw_rectangle(m_gdkwindow
, gc
, true, xx
, yy
+rr
, ww
, hh
-dd
+1);
909 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, dd
, dd
, 90*64, 90*64);
910 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64);
911 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64);
912 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64);
915 gdk_gc_set_ts_origin(gc
, 0, 0);
918 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
920 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
, xx
+ww
-rr
, yy
);
921 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+rr
+1, yy
+hh
, xx
+ww
-rr
, yy
+hh
);
922 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
, yy
+rr
+1, xx
, yy
+hh
-rr
);
923 gdk_draw_line( m_gdkwindow
, m_penGC
, xx
+ww
, yy
+rr
+1, xx
+ww
, yy
+hh
-rr
);
924 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, dd
, dd
, 90*64, 90*64 );
925 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
, dd
, dd
, 0, 90*64 );
926 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
+ww
-dd
, yy
+hh
-dd
, dd
, dd
, 270*64, 90*64 );
927 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
+hh
-dd
, dd
, dd
, 180*64, 90*64 );
931 // this ignores the radius
932 CalcBoundingBox( x
, y
);
933 CalcBoundingBox( x
+ width
, y
+ height
);
936 void wxWindowDCImpl::DoDrawEllipse( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
938 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
940 wxCoord xx
= XLOG2DEV(x
);
941 wxCoord yy
= YLOG2DEV(y
);
942 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
943 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
945 // CMB: handle -ve width and/or height
946 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
947 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
951 if (m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
955 DrawingSetup(gc
, originChanged
);
957 gdk_draw_arc(m_gdkwindow
, gc
, true, xx
, yy
, ww
, hh
, 0, 360*64);
960 gdk_gc_set_ts_origin(gc
, 0, 0);
963 if (m_pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
964 gdk_draw_arc( m_gdkwindow
, m_penGC
, FALSE
, xx
, yy
, ww
, hh
, 0, 360*64 );
967 CalcBoundingBox( x
, y
);
968 CalcBoundingBox( x
+ width
, y
+ height
);
971 void wxWindowDCImpl::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
973 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
974 DoDrawBitmap( (const wxBitmap
&)icon
, x
, y
, true );
977 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap
&bitmap
,
978 wxCoord x
, wxCoord y
,
981 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
983 wxCHECK_RET( bitmap
.IsOk(), wxT("invalid bitmap") );
985 bool is_mono
= bitmap
.GetDepth() == 1;
987 // scale/translate size and position
988 int xx
= XLOG2DEV(x
);
989 int yy
= YLOG2DEV(y
);
991 int w
= bitmap
.GetWidth();
992 int h
= bitmap
.GetHeight();
994 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
997 CalcBoundingBox( x
, y
);
998 CalcBoundingBox( x
+ w
, y
+ h
);
1000 if (!m_gdkwindow
) return;
1002 int ww
= XLOG2DEVREL(w
);
1003 int hh
= YLOG2DEVREL(h
);
1005 // compare to current clipping region
1006 if (!m_currentClippingRegion
.IsNull())
1008 wxRegion
tmp( xx
,yy
,ww
,hh
);
1009 tmp
.Intersect( m_currentClippingRegion
);
1014 // scale bitmap if required
1015 wxBitmap use_bitmap
= bitmap
;
1016 if ((w
!= ww
) || (h
!= hh
))
1017 use_bitmap
= use_bitmap
.Rescale( 0, 0, ww
, hh
, ww
, hh
);
1019 // apply mask if any
1020 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1021 if (useMask
&& use_bitmap
.GetMask())
1022 mask
= use_bitmap
.GetMask()->GetBitmap();
1024 GdkGC
* use_gc
= is_mono
? m_textGC
: m_penGC
;
1026 GdkBitmap
*new_mask
= (GdkBitmap
*) NULL
;
1030 if (!m_currentClippingRegion
.IsNull())
1033 new_mask
= gdk_pixmap_new( wxGetRootWindow()->window
, ww
, hh
, 1 );
1034 GdkGC
*gc
= gdk_gc_new( new_mask
);
1036 gdk_gc_set_foreground( gc
, &col
);
1037 gdk_draw_rectangle( new_mask
, gc
, TRUE
, 0, 0, ww
, hh
);
1039 gdk_gc_set_background( gc
, &col
);
1041 gdk_gc_set_foreground( gc
, &col
);
1042 gdk_gc_set_clip_region( gc
, m_currentClippingRegion
.GetRegion() );
1043 gdk_gc_set_clip_origin( gc
, -xx
, -yy
);
1044 gdk_gc_set_fill( gc
, GDK_OPAQUE_STIPPLED
);
1045 gdk_gc_set_stipple( gc
, mask
);
1046 gdk_draw_rectangle( new_mask
, gc
, TRUE
, 0, 0, ww
, hh
);
1048 g_object_unref (gc
);
1051 gdk_gc_set_clip_mask(use_gc
, mask
);
1052 gdk_gc_set_clip_origin(use_gc
, xx
, yy
);
1055 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains. For
1056 // drawing a mono-bitmap (XBitmap) we use the current text GC
1059 GdkPixmap
*bitmap2
= gdk_pixmap_new( wxGetRootWindow()->window
, ww
, hh
, -1 );
1060 GdkGC
*gc
= gdk_gc_new( bitmap2
);
1061 gdk_gc_set_foreground( gc
, m_textForegroundColour
.GetColor() );
1062 gdk_gc_set_background( gc
, m_textBackgroundColour
.GetColor() );
1063 gdk_wx_draw_bitmap(bitmap2
, gc
, use_bitmap
.GetPixmap(), 0, 0);
1065 gdk_draw_drawable(m_gdkwindow
, use_gc
, bitmap2
, 0, 0, xx
, yy
, -1, -1);
1067 g_object_unref (bitmap2
);
1068 g_object_unref (gc
);
1072 if (use_bitmap
.HasPixbuf())
1074 gdk_draw_pixbuf(m_gdkwindow
, use_gc
,
1075 use_bitmap
.GetPixbuf(),
1076 0, 0, xx
, yy
, -1, -1,
1077 GDK_RGB_DITHER_NORMAL
, xx
, yy
);
1081 gdk_draw_drawable(m_gdkwindow
, use_gc
,
1082 use_bitmap
.GetPixmap(),
1083 0, 0, xx
, yy
, -1, -1);
1087 // remove mask again if any
1090 gdk_gc_set_clip_mask(use_gc
, NULL
);
1091 gdk_gc_set_clip_origin(use_gc
, 0, 0);
1092 if (!m_currentClippingRegion
.IsNull())
1093 gdk_gc_set_clip_region(use_gc
, m_currentClippingRegion
.GetRegion());
1094 if (new_mask
!= NULL
)
1095 g_object_unref(new_mask
);
1099 bool wxWindowDCImpl::DoBlit( wxCoord xdest
, wxCoord ydest
,
1100 wxCoord width
, wxCoord height
,
1102 wxCoord xsrc
, wxCoord ysrc
,
1105 wxCoord xsrcMask
, wxCoord ysrcMask
)
1107 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1109 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1111 if (!m_gdkwindow
) return false;
1113 // transform the source DC coords to the device ones
1114 xsrc
= source
->LogicalToDeviceX(xsrc
);
1115 ysrc
= source
->LogicalToDeviceY(ysrc
);
1118 wxMemoryDC
*memDC
= wxDynamicCast(source
, wxMemoryDC
);
1121 selected
= memDC
->GetSelectedBitmap();
1122 if ( !selected
.IsOk() )
1126 bool use_bitmap_method
= false;
1127 bool is_mono
= false;
1129 if (xsrcMask
== -1 && ysrcMask
== -1)
1135 if (selected
.IsOk())
1137 is_mono
= (selected
.GetDepth() == 1);
1139 // we use the "XCopyArea" way to copy a memory dc into
1140 // a different window if the memory dc BOTH
1141 // a) doesn't have any mask or its mask isn't used
1145 if (useMask
&& (selected
.GetMask()))
1147 // we HAVE TO use the direct way for memory dcs
1148 // that have mask since the XCopyArea doesn't know
1150 use_bitmap_method
= true;
1154 // we HAVE TO use the direct way for memory dcs
1155 // that are bitmaps because XCopyArea doesn't cope
1156 // with different bit depths
1157 use_bitmap_method
= true;
1159 else if ((xsrc
== 0) && (ysrc
== 0) &&
1160 (width
== selected
.GetWidth()) &&
1161 (height
== selected
.GetHeight()))
1163 // we SHOULD use the direct way if all of the bitmap
1164 // in the memory dc is copied in which case XCopyArea
1165 // wouldn't be able able to boost performace by reducing
1166 // the area to be scaled
1167 use_bitmap_method
= true;
1171 CalcBoundingBox( xdest
, ydest
);
1172 CalcBoundingBox( xdest
+ width
, ydest
+ height
);
1174 // scale/translate size and position
1175 wxCoord xx
= XLOG2DEV(xdest
);
1176 wxCoord yy
= YLOG2DEV(ydest
);
1178 wxCoord ww
= XLOG2DEVREL(width
);
1179 wxCoord hh
= YLOG2DEVREL(height
);
1181 // compare to current clipping region
1182 if (!m_currentClippingRegion
.IsNull())
1184 wxRegion
tmp( xx
,yy
,ww
,hh
);
1185 tmp
.Intersect( m_currentClippingRegion
);
1190 int old_logical_func
= m_logicalFunction
;
1191 SetLogicalFunction( logical_func
);
1193 if (use_bitmap_method
)
1195 // scale/translate bitmap size
1196 wxCoord bm_width
= selected
.GetWidth();
1197 wxCoord bm_height
= selected
.GetHeight();
1199 // Get clip coords for the bitmap. If we don't
1200 // use wxBitmap::Rescale(), which can clip the
1201 // bitmap, these are the same as the original
1208 // interpret userscale of src too
1210 memDC
->GetUserScale(&xsc
,&ysc
);
1211 bm_width
= (int) (bm_width
/ xsc
);
1212 bm_height
= (int) (bm_height
/ ysc
);
1214 wxCoord bm_ww
= XLOG2DEVREL( bm_width
);
1215 wxCoord bm_hh
= YLOG2DEVREL( bm_height
);
1217 // Scale bitmap if required
1218 wxBitmap use_bitmap
= selected
;
1219 if ((selected
.GetWidth()!= bm_ww
) || ( selected
.GetHeight()!= bm_hh
))
1221 // This indicates that the blitting code below will get
1222 // a clipped bitmap and therefore needs to move the origin
1224 wxRegion
tmp( xx
,yy
,ww
,hh
);
1225 if (!m_currentClippingRegion
.IsNull())
1226 tmp
.Intersect( m_currentClippingRegion
);
1227 tmp
.GetBox(cx
,cy
,cw
,ch
);
1229 // Scale and clipped bitmap
1230 use_bitmap
= selected
.Rescale(cx
-xx
,cy
-yy
,cw
,ch
,bm_ww
,bm_hh
);
1233 // apply mask if any
1234 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1235 if (useMask
&& use_bitmap
.GetMask())
1236 mask
= use_bitmap
.GetMask()->GetBitmap();
1238 GdkGC
* use_gc
= is_mono
? m_textGC
: m_penGC
;
1240 GdkBitmap
*new_mask
= (GdkBitmap
*) NULL
;
1244 if (!m_currentClippingRegion
.IsNull())
1247 new_mask
= gdk_pixmap_new( wxGetRootWindow()->window
, bm_ww
, bm_hh
, 1 );
1248 GdkGC
*gc
= gdk_gc_new( new_mask
);
1250 gdk_gc_set_foreground( gc
, &col
);
1251 gdk_gc_set_ts_origin( gc
, -xsrcMask
, -ysrcMask
);
1252 gdk_draw_rectangle( new_mask
, gc
, TRUE
, 0, 0, bm_ww
, bm_hh
);
1254 gdk_gc_set_background( gc
, &col
);
1256 gdk_gc_set_foreground( gc
, &col
);
1257 gdk_gc_set_clip_region( gc
, m_currentClippingRegion
.GetRegion() );
1258 // was: gdk_gc_set_clip_origin( gc, -xx, -yy );
1259 gdk_gc_set_clip_origin( gc
, -cx
, -cy
);
1260 gdk_gc_set_fill( gc
, GDK_OPAQUE_STIPPLED
);
1261 gdk_gc_set_stipple( gc
, mask
);
1262 gdk_draw_rectangle( new_mask
, gc
, TRUE
, 0, 0, bm_ww
, bm_hh
);
1264 g_object_unref (gc
);
1267 gdk_gc_set_clip_mask(use_gc
, mask
);
1268 if (new_mask
!= NULL
)
1269 gdk_gc_set_clip_origin(use_gc
, cx
, cy
);
1271 gdk_gc_set_clip_origin(use_gc
, cx
- xsrcMask
, cy
- ysrcMask
);
1274 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains. For
1275 // drawing a mono-bitmap (XBitmap) we use the current text GC
1279 GdkPixmap
*bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bm_ww
, bm_hh
, -1 );
1280 GdkGC
*gc
= gdk_gc_new( bitmap
);
1281 gdk_gc_set_foreground( gc
, m_textForegroundColour
.GetColor() );
1282 gdk_gc_set_background( gc
, m_textBackgroundColour
.GetColor() );
1283 gdk_wx_draw_bitmap(bitmap
, gc
, use_bitmap
.GetPixmap(), 0, 0);
1285 gdk_draw_drawable(m_gdkwindow
, use_gc
, bitmap
, xsrc
, ysrc
, cx
, cy
, cw
, ch
);
1287 g_object_unref (bitmap
);
1288 g_object_unref (gc
);
1292 // was: gdk_draw_drawable( m_gdkwindow, m_penGC, use_bitmap.GetPixmap(), xsrc, ysrc, xx, yy, ww, hh );
1293 gdk_draw_drawable(m_gdkwindow
, use_gc
, use_bitmap
.GetPixmap(), xsrc
, ysrc
, cx
, cy
, cw
, ch
);
1296 // remove mask again if any
1299 gdk_gc_set_clip_mask(use_gc
, NULL
);
1300 gdk_gc_set_clip_origin(use_gc
, 0, 0);
1301 if (!m_currentClippingRegion
.IsNull())
1302 gdk_gc_set_clip_region(use_gc
, m_currentClippingRegion
.GetRegion());
1306 g_object_unref (new_mask
);
1308 else // use_bitmap_method
1310 if (selected
.IsOk() && ((width
!= ww
) || (height
!= hh
)))
1313 wxRegion
tmp( xx
,yy
,ww
,hh
);
1314 tmp
.Intersect( m_currentClippingRegion
);
1315 wxCoord cx
,cy
,cw
,ch
;
1316 tmp
.GetBox(cx
,cy
,cw
,ch
);
1319 wxBitmap bitmap
= selected
.Rescale( cx
-xx
, cy
-yy
, cw
, ch
, ww
, hh
);
1321 // draw scaled bitmap
1322 // was: gdk_draw_drawable( m_gdkwindow, m_penGC, bitmap.GetPixmap(), 0, 0, xx, yy, -1, -1 );
1323 gdk_draw_drawable( m_gdkwindow
, m_penGC
, bitmap
.GetPixmap(), 0, 0, cx
, cy
, -1, -1 );
1327 // No scaling and not a memory dc with a mask either
1328 GdkWindow
* window
= NULL
;
1329 wxDCImpl
*impl
= source
->GetImpl();
1330 wxWindowDCImpl
*gtk_impl
= wxDynamicCast(impl
, wxWindowDCImpl
);
1332 window
= gtk_impl
->GetGDKWindow();
1335 SetLogicalFunction( old_logical_func
);
1339 // copy including child window contents
1340 gdk_gc_set_subwindow( m_penGC
, GDK_INCLUDE_INFERIORS
);
1341 gdk_draw_drawable( m_gdkwindow
, m_penGC
,
1345 gdk_gc_set_subwindow( m_penGC
, GDK_CLIP_BY_CHILDREN
);
1349 SetLogicalFunction( old_logical_func
);
1354 void wxWindowDCImpl::DoDrawText( const wxString
&text
, wxCoord x
, wxCoord y
)
1356 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1358 if (!m_gdkwindow
) return;
1360 if (text
.empty()) return;
1365 wxCHECK_RET( m_context
, wxT("no Pango context") );
1366 wxCHECK_RET( m_layout
, wxT("no Pango layout") );
1367 wxCHECK_RET( m_fontdesc
, wxT("no Pango font description") );
1369 gdk_pango_context_set_colormap( m_context
, m_cmap
);
1371 bool underlined
= m_font
.IsOk() && m_font
.GetUnderlined();
1373 wxCharBuffer data
= wxGTK_CONV(text
);
1376 size_t datalen
= strlen(data
);
1378 // in Pango >= 1.16 the "underline of leading/trailing spaces" bug
1379 // has been fixed and thus the hack implemented below should never be used
1380 static bool pangoOk
= !wx_pango_version_check(1, 16, 0);
1382 bool needshack
= underlined
&& !pangoOk
;
1386 // a PangoLayout which has leading/trailing spaces with underlined font
1387 // is not correctly drawn by this pango version: Pango won't underline the spaces.
1388 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
1389 // this behaviour. To workaround this problem, we use a special hack here
1390 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
1391 // empty space characters and give them a dummy colour attribute.
1392 // This will force Pango to underline the leading/trailing spaces, too.
1394 wxCharBuffer
data_tmp(datalen
+ 6);
1395 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1396 memcpy(data_tmp
.data(), "\342\200\214", 3);
1397 // copy the user string
1398 memcpy(data_tmp
.data() + 3, data
, datalen
);
1399 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1400 memcpy(data_tmp
.data() + 3 + datalen
, "\342\200\214", 3);
1406 pango_layout_set_text(m_layout
, data
, datalen
);
1410 PangoAttrList
*attrs
= pango_attr_list_new();
1411 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1413 a
->end_index
= datalen
;
1414 pango_attr_list_insert(attrs
, a
);
1418 // dummy colour for the leading space
1419 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1422 pango_attr_list_insert(attrs
, a
);
1424 // dummy colour for the trailing space
1425 a
= pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1426 a
->start_index
= datalen
- 1;
1427 a
->end_index
= datalen
;
1428 pango_attr_list_insert(attrs
, a
);
1431 pango_layout_set_attributes(m_layout
, attrs
);
1432 pango_attr_list_unref(attrs
);
1436 const bool isScaled
= fabs(m_scaleY
- 1.0) > 0.00001;
1439 // If there is a user or actually any scale applied to
1440 // the device context, scale the font.
1442 // scale font description
1443 oldSize
= pango_font_description_get_size(m_fontdesc
);
1444 pango_font_description_set_size(m_fontdesc
, int(oldSize
* m_scaleY
));
1446 // actually apply scaled font
1447 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1451 pango_layout_get_pixel_size(m_layout
, &w
, &h
);
1452 if (m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1454 gdk_gc_set_foreground(m_textGC
, m_textBackgroundColour
.GetColor());
1455 gdk_draw_rectangle(m_gdkwindow
, m_textGC
, true, x
, y
, w
, h
);
1456 gdk_gc_set_foreground(m_textGC
, m_textForegroundColour
.GetColor());
1461 if (m_window
&& m_window
->GetLayoutDirection() == wxLayout_RightToLeft
)
1463 gdk_draw_layout(m_gdkwindow
, m_textGC
, x_rtl
, y
, m_layout
);
1467 // reset unscaled size
1468 pango_font_description_set_size( m_fontdesc
, oldSize
);
1470 // actually apply unscaled font
1471 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1475 // undo underline attributes setting:
1476 pango_layout_set_attributes(m_layout
, NULL
);
1479 CalcBoundingBox(x
+ int(w
/ m_scaleX
), y
+ int(h
/ m_scaleY
));
1480 CalcBoundingBox(x
, y
);
1484 // TODO: There is an example of rotating text with GTK2 that would probably be
1485 // a better approach here:
1486 // http://www.daa.com.au/pipermail/pygtk/2003-April/005052.html
1488 void wxWindowDCImpl::DoDrawRotatedText( const wxString
&text
, wxCoord x
, wxCoord y
, double angle
)
1491 if (!m_gdkwindow
|| text
.empty())
1494 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1496 if ( wxIsNullDouble(angle
) )
1498 DoDrawText(text
, x
, y
);
1505 // TODO: implement later without GdkFont for GTK 2.0
1506 DoGetTextExtent(text
, &w
, &h
, NULL
,NULL
, &m_font
);
1508 // draw the string normally
1511 dc
.SelectObject(src
);
1512 dc
.SetFont(GetFont());
1513 dc
.SetBackground(*wxBLACK_BRUSH
);
1514 dc
.SetBrush(*wxBLACK_BRUSH
);
1516 dc
.SetTextForeground( *wxWHITE
);
1517 dc
.DrawText(text
, 0, 0);
1518 dc
.SelectObject(wxNullBitmap
);
1520 // Calculate the size of the rotated bounding box.
1521 double rad
= DegToRad(angle
);
1522 double dx
= cos(rad
),
1525 // the rectngle vertices are counted clockwise with the first one being at
1526 // (0, 0) (or, rather, at (x, y))
1528 y2
= -w
*dy
; // y axis points to the bottom, hence minus
1531 double x3
= x4
+ x2
,
1535 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5),
1536 maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5),
1537 minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5),
1538 minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1541 wxImage image
= src
.ConvertToImage();
1543 image
.ConvertColourToAlpha( m_textForegroundColour
.Red(),
1544 m_textForegroundColour
.Green(),
1545 m_textForegroundColour
.Blue() );
1546 image
= image
.Rotate( rad
, wxPoint(0,0) );
1548 int i_angle
= (int) angle
;
1549 i_angle
= i_angle
% 360;
1553 if ((i_angle
>= 90.0) && (i_angle
< 270.0))
1554 xoffset
= image
.GetWidth();
1556 if ((i_angle
>= 0.0) && (i_angle
< 180.0))
1557 yoffset
= image
.GetHeight();
1559 if ((i_angle
>= 0) && (i_angle
< 90))
1560 yoffset
-= (int)( cos(rad
)*h
);
1561 if ((i_angle
>= 90) && (i_angle
< 180))
1562 xoffset
-= (int)( sin(rad
)*h
);
1563 if ((i_angle
>= 180) && (i_angle
< 270))
1564 yoffset
-= (int)( cos(rad
)*h
);
1565 if ((i_angle
>= 270) && (i_angle
< 360))
1566 xoffset
-= (int)( sin(rad
)*h
);
1568 int i_x
= x
- xoffset
;
1569 int i_y
= y
- yoffset
;
1572 DoDrawBitmap( src
, i_x
, i_y
, true );
1575 // it would be better to draw with non underlined font and draw the line
1576 // manually here (it would be more straight...)
1578 if ( m_font
.GetUnderlined() )
1580 gdk_draw_line( m_gdkwindow
, m_textGC
,
1581 XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
),
1582 XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1586 // update the bounding box
1587 CalcBoundingBox(x
+ minX
, y
+ minY
);
1588 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1589 #else // !wxUSE_IMAGE
1594 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
1597 void wxWindowDCImpl::DoGetTextExtent(const wxString
&string
,
1598 wxCoord
*width
, wxCoord
*height
,
1599 wxCoord
*descent
, wxCoord
*externalLeading
,
1600 const wxFont
*theFont
) const
1608 if ( externalLeading
)
1609 *externalLeading
= 0;
1614 // ensure that theFont is always non-NULL
1615 if ( !theFont
|| !theFont
->IsOk() )
1618 // and use it if it's valid
1619 if ( theFont
->IsOk() )
1621 pango_layout_set_font_description
1624 theFont
->GetNativeFontInfo()->description
1628 // Set layout's text
1629 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, *theFont
);
1632 // hardly ideal, but what else can we do if conversion failed?
1636 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1639 pango_layout_get_pixel_size(m_layout
, width
, &h
);
1642 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1643 int baseline
= pango_layout_iter_get_baseline(iter
);
1644 pango_layout_iter_free(iter
);
1645 *descent
= h
- PANGO_PIXELS(baseline
);
1650 // Reset old font description
1651 if (theFont
->IsOk())
1652 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1656 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString
& text
,
1657 wxArrayInt
& widths
) const
1659 const size_t len
= text
.length();
1666 // Set layout's text
1667 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, m_font
);
1670 // hardly ideal, but what else can we do if conversion failed?
1671 wxLogLastError(wxT("DoGetPartialTextExtents"));
1675 pango_layout_set_text(m_layout
, dataUTF8
, -1);
1677 // Calculate the position of each character based on the widths of
1678 // the previous characters
1680 // Code borrowed from Scintilla's PlatGTK
1681 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
1683 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1685 while (pango_layout_iter_next_cluster(iter
))
1687 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
1688 int position
= PANGO_PIXELS(pos
.x
);
1689 widths
[i
++] = position
;
1692 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
1693 pango_layout_iter_free(iter
);
1699 wxCoord
wxWindowDCImpl::GetCharWidth() const
1701 pango_layout_set_text( m_layout
, "H", 1 );
1703 pango_layout_get_pixel_size( m_layout
, &w
, NULL
);
1707 wxCoord
wxWindowDCImpl::GetCharHeight() const
1709 PangoFontMetrics
*metrics
= pango_context_get_metrics (m_context
, m_fontdesc
, pango_context_get_language(m_context
));
1710 wxCHECK_MSG( metrics
, -1, _T("failed to get pango font metrics") );
1712 wxCoord h
= PANGO_PIXELS (pango_font_metrics_get_descent (metrics
) +
1713 pango_font_metrics_get_ascent (metrics
));
1714 pango_font_metrics_unref (metrics
);
1718 void wxWindowDCImpl::Clear()
1720 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1722 if (!m_gdkwindow
) return;
1725 DoGetSize( &width
, &height
);
1726 gdk_draw_rectangle( m_gdkwindow
, m_bgGC
, TRUE
, 0, 0, width
, height
);
1729 void wxWindowDCImpl::SetFont( const wxFont
&font
)
1736 pango_font_description_free( m_fontdesc
);
1738 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1743 PangoContext
*oldContext
= m_context
;
1745 m_context
= m_window
->GtkGetPangoDefaultContext();
1747 // If we switch back/forth between different contexts
1748 // we also have to create a new layout. I think so,
1749 // at least, and it doesn't hurt to do it.
1750 if (oldContext
!= m_context
)
1753 g_object_unref (m_layout
);
1755 m_layout
= pango_layout_new( m_context
);
1759 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1763 void wxWindowDCImpl::SetPen( const wxPen
&pen
)
1765 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1767 if (m_pen
== pen
) return;
1771 if (!m_pen
.IsOk()) return;
1773 if (!m_gdkwindow
) return;
1775 gint width
= m_pen
.GetWidth();
1778 // CMB: if width is non-zero scale it with the dc
1783 // X doesn't allow different width in x and y and so we take
1786 ( fabs((double) XLOG2DEVREL(width
)) +
1787 fabs((double) YLOG2DEVREL(width
)) ) / 2.0;
1791 // width can't be 0 or an internal GTK error occurs inside
1792 // gdk_gc_set_dashes() below
1797 static const wxGTKDash dotted
[] = {1, 1};
1798 static const wxGTKDash short_dashed
[] = {2, 2};
1799 static const wxGTKDash wxCoord_dashed
[] = {2, 4};
1800 static const wxGTKDash dotted_dashed
[] = {3, 3, 1, 3};
1802 // We express dash pattern in pen width unit, so we are
1803 // independent of zoom factor and so on...
1805 const wxGTKDash
*req_dash
;
1807 GdkLineStyle lineStyle
= GDK_LINE_ON_OFF_DASH
;
1808 switch (m_pen
.GetStyle())
1810 case wxPENSTYLE_USER_DASH
:
1811 req_nb_dash
= m_pen
.GetDashCount();
1812 req_dash
= (wxGTKDash
*)m_pen
.GetDash();
1814 case wxPENSTYLE_DOT
:
1818 case wxPENSTYLE_LONG_DASH
:
1820 req_dash
= wxCoord_dashed
;
1822 case wxPENSTYLE_SHORT_DASH
:
1824 req_dash
= short_dashed
;
1826 case wxPENSTYLE_DOT_DASH
:
1828 req_dash
= dotted_dashed
;
1831 case wxPENSTYLE_TRANSPARENT
:
1832 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
1833 case wxPENSTYLE_STIPPLE
:
1834 case wxPENSTYLE_SOLID
:
1836 lineStyle
= GDK_LINE_SOLID
;
1837 req_dash
= (wxGTKDash
*)NULL
;
1842 if (req_dash
&& req_nb_dash
)
1844 wxGTKDash
*real_req_dash
= new wxGTKDash
[req_nb_dash
];
1847 for (int i
= 0; i
< req_nb_dash
; i
++)
1848 real_req_dash
[i
] = req_dash
[i
] * width
;
1849 gdk_gc_set_dashes( m_penGC
, 0, real_req_dash
, req_nb_dash
);
1850 delete[] real_req_dash
;
1854 // No Memory. We use non-scaled dash pattern...
1855 gdk_gc_set_dashes( m_penGC
, 0, (wxGTKDash
*)req_dash
, req_nb_dash
);
1859 GdkCapStyle capStyle
= GDK_CAP_ROUND
;
1860 switch (m_pen
.GetCap())
1862 case wxCAP_PROJECTING
: { capStyle
= GDK_CAP_PROJECTING
; break; }
1863 case wxCAP_BUTT
: { capStyle
= GDK_CAP_BUTT
; break; }
1869 capStyle
= GDK_CAP_NOT_LAST
;
1874 GdkJoinStyle joinStyle
= GDK_JOIN_ROUND
;
1875 switch (m_pen
.GetJoin())
1877 case wxJOIN_BEVEL
: { joinStyle
= GDK_JOIN_BEVEL
; break; }
1878 case wxJOIN_MITER
: { joinStyle
= GDK_JOIN_MITER
; break; }
1880 default: { joinStyle
= GDK_JOIN_ROUND
; break; }
1883 gdk_gc_set_line_attributes( m_penGC
, width
, lineStyle
, capStyle
, joinStyle
);
1885 m_pen
.GetColour().CalcPixel( m_cmap
);
1886 gdk_gc_set_foreground( m_penGC
, m_pen
.GetColour().GetColor() );
1889 void wxWindowDCImpl::SetBrush( const wxBrush
&brush
)
1891 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1893 if (m_brush
== brush
) return;
1897 if (!m_brush
.IsOk()) return;
1899 if (!m_gdkwindow
) return;
1901 m_brush
.GetColour().CalcPixel( m_cmap
);
1902 gdk_gc_set_foreground( m_brushGC
, m_brush
.GetColour().GetColor() );
1904 gdk_gc_set_fill( m_brushGC
, GDK_SOLID
);
1906 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
) && (m_brush
.GetStipple()->IsOk()))
1908 if (m_brush
.GetStipple()->GetDepth() != 1)
1910 gdk_gc_set_fill( m_brushGC
, GDK_TILED
);
1911 gdk_gc_set_tile( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1915 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1916 gdk_gc_set_stipple( m_brushGC
, m_brush
.GetStipple()->GetPixmap() );
1920 if ((m_brush
.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
) && (m_brush
.GetStipple()->GetMask()))
1922 gdk_gc_set_fill( m_textGC
, GDK_OPAQUE_STIPPLED
);
1923 gdk_gc_set_stipple( m_textGC
, m_brush
.GetStipple()->GetMask()->GetBitmap() );
1926 if (m_brush
.IsHatch())
1928 gdk_gc_set_fill( m_brushGC
, GDK_STIPPLED
);
1929 gdk_gc_set_stipple(m_brushGC
, GetHatch(m_brush
.GetStyle()));
1933 void wxWindowDCImpl::SetBackground( const wxBrush
&brush
)
1935 /* CMB 21/7/98: Added SetBackground. Sets background brush
1936 * for Clear() and bg colour for shapes filled with cross-hatch brush */
1938 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1940 if (m_backgroundBrush
== brush
) return;
1942 m_backgroundBrush
= brush
;
1944 if (!m_backgroundBrush
.IsOk()) return;
1946 if (!m_gdkwindow
) return;
1948 wxColor color
= m_backgroundBrush
.GetColour();
1949 color
.CalcPixel(m_cmap
);
1950 const GdkColor
* gdkColor
= color
.GetColor();
1951 gdk_gc_set_background(m_brushGC
, gdkColor
);
1952 gdk_gc_set_background(m_penGC
, gdkColor
);
1953 gdk_gc_set_background(m_bgGC
, gdkColor
);
1954 gdk_gc_set_foreground(m_bgGC
, gdkColor
);
1957 gdk_gc_set_fill( m_bgGC
, GDK_SOLID
);
1959 if (m_backgroundBrush
.GetStyle() == wxBRUSHSTYLE_STIPPLE
)
1961 const wxBitmap
* stipple
= m_backgroundBrush
.GetStipple();
1962 if (stipple
->IsOk())
1964 if (stipple
->GetDepth() != 1)
1966 gdk_gc_set_fill(m_bgGC
, GDK_TILED
);
1967 gdk_gc_set_tile(m_bgGC
, stipple
->GetPixmap());
1971 gdk_gc_set_fill(m_bgGC
, GDK_STIPPLED
);
1972 gdk_gc_set_stipple(m_bgGC
, stipple
->GetPixmap());
1976 else if (m_backgroundBrush
.IsHatch())
1978 gdk_gc_set_fill( m_bgGC
, GDK_STIPPLED
);
1979 gdk_gc_set_stipple(m_bgGC
, GetHatch(m_backgroundBrush
.GetStyle()));
1983 void wxWindowDCImpl::SetLogicalFunction( int function
)
1985 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1987 if (m_logicalFunction
== function
)
1990 // VZ: shouldn't this be a CHECK?
1997 case wxXOR
: mode
= GDK_XOR
; break;
1998 case wxINVERT
: mode
= GDK_INVERT
; break;
1999 case wxOR_REVERSE
: mode
= GDK_OR_REVERSE
; break;
2000 case wxAND_REVERSE
: mode
= GDK_AND_REVERSE
; break;
2001 case wxCLEAR
: mode
= GDK_CLEAR
; break;
2002 case wxSET
: mode
= GDK_SET
; break;
2003 case wxOR_INVERT
: mode
= GDK_OR_INVERT
; break;
2004 case wxAND
: mode
= GDK_AND
; break;
2005 case wxOR
: mode
= GDK_OR
; break;
2006 case wxEQUIV
: mode
= GDK_EQUIV
; break;
2007 case wxNAND
: mode
= GDK_NAND
; break;
2008 case wxAND_INVERT
: mode
= GDK_AND_INVERT
; break;
2009 case wxCOPY
: mode
= GDK_COPY
; break;
2010 case wxNO_OP
: mode
= GDK_NOOP
; break;
2011 case wxSRC_INVERT
: mode
= GDK_COPY_INVERT
; break;
2013 // unsupported by GTK
2014 case wxNOR
: mode
= GDK_COPY
; break;
2016 wxFAIL_MSG( wxT("unsupported logical function") );
2020 m_logicalFunction
= function
;
2022 gdk_gc_set_function( m_penGC
, mode
);
2023 gdk_gc_set_function( m_brushGC
, mode
);
2025 // to stay compatible with wxMSW, we don't apply ROPs to the text
2026 // operations (i.e. DrawText/DrawRotatedText).
2027 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2028 gdk_gc_set_function( m_textGC
, mode
);
2031 void wxWindowDCImpl::SetTextForeground( const wxColour
&col
)
2033 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2035 // don't set m_textForegroundColour to an invalid colour as we'd crash
2036 // later then (we use m_textForegroundColour.GetColor() without checking
2038 if ( !col
.IsOk() || (m_textForegroundColour
== col
) )
2041 m_textForegroundColour
= col
;
2045 m_textForegroundColour
.CalcPixel( m_cmap
);
2046 gdk_gc_set_foreground( m_textGC
, m_textForegroundColour
.GetColor() );
2050 void wxWindowDCImpl::SetTextBackground( const wxColour
&col
)
2052 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2055 if ( !col
.IsOk() || (m_textBackgroundColour
== col
) )
2058 m_textBackgroundColour
= col
;
2062 m_textBackgroundColour
.CalcPixel( m_cmap
);
2063 gdk_gc_set_background( m_textGC
, m_textBackgroundColour
.GetColor() );
2067 void wxWindowDCImpl::SetBackgroundMode( int mode
)
2069 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2071 m_backgroundMode
= mode
;
2073 if (!m_gdkwindow
) return;
2075 // CMB 21/7/98: fill style of cross-hatch brushes is affected by
2076 // transparent/solid background mode
2078 if (m_brush
.GetStyle() != wxBRUSHSTYLE_SOLID
&& m_brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
2080 gdk_gc_set_fill( m_brushGC
,
2081 (m_backgroundMode
== wxBRUSHSTYLE_TRANSPARENT
) ? GDK_STIPPLED
: GDK_OPAQUE_STIPPLED
);
2085 void wxWindowDCImpl::SetPalette( const wxPalette
& WXUNUSED(palette
) )
2087 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
2090 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2092 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2094 if (!m_gdkwindow
) return;
2097 rect
.x
= XLOG2DEV(x
);
2098 rect
.y
= YLOG2DEV(y
);
2099 rect
.width
= XLOG2DEVREL(width
);
2100 rect
.height
= YLOG2DEVREL(height
);
2102 if (m_window
&& m_window
->m_wxwindow
&&
2103 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2105 rect
.x
-= rect
.width
;
2108 if (!m_currentClippingRegion
.IsNull())
2109 m_currentClippingRegion
.Intersect( rect
);
2111 m_currentClippingRegion
.Union( rect
);
2113 #if USE_PAINT_REGION
2114 if (!m_paintClippingRegion
.IsNull())
2115 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2118 wxCoord xx
, yy
, ww
, hh
;
2119 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2121 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2123 wxDC::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2126 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2127 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2128 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2129 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2130 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2133 void wxWindowDCImpl::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
2135 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2139 DestroyClippingRegion();
2143 if (!m_gdkwindow
) return;
2145 if (!m_currentClippingRegion
.IsNull())
2146 m_currentClippingRegion
.Intersect( region
);
2148 m_currentClippingRegion
.Union( region
);
2150 #if USE_PAINT_REGION
2151 if (!m_paintClippingRegion
.IsNull())
2152 m_currentClippingRegion
.Intersect( m_paintClippingRegion
);
2155 wxCoord xx
, yy
, ww
, hh
;
2156 m_currentClippingRegion
.GetBox( xx
, yy
, ww
, hh
);
2158 wxGTKDCImpl::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2160 wxDC::DoSetClippingRegion( xx
, yy
, ww
, hh
);
2163 GdkRegion
* gdkRegion
= m_currentClippingRegion
.GetRegion();
2164 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2165 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2166 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2167 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2170 void wxWindowDCImpl::DestroyClippingRegion()
2172 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2175 wxDCImpl::DestroyClippingRegion();
2177 wxDC::DestroyClippingRegion();
2180 m_currentClippingRegion
.Clear();
2182 #if USE_PAINT_REGION
2183 if (!m_paintClippingRegion
.IsEmpty())
2184 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2187 if (!m_gdkwindow
) return;
2189 GdkRegion
* gdkRegion
= NULL
;
2190 if (!m_currentClippingRegion
.IsEmpty())
2191 gdkRegion
= m_currentClippingRegion
.GetRegion();
2193 gdk_gc_set_clip_region(m_penGC
, gdkRegion
);
2194 gdk_gc_set_clip_region(m_brushGC
, gdkRegion
);
2195 gdk_gc_set_clip_region(m_textGC
, gdkRegion
);
2196 gdk_gc_set_clip_region(m_bgGC
, gdkRegion
);
2199 void wxWindowDCImpl::Destroy()
2201 if (m_penGC
) wxFreePoolGC( m_penGC
);
2202 m_penGC
= (GdkGC
*) NULL
;
2203 if (m_brushGC
) wxFreePoolGC( m_brushGC
);
2204 m_brushGC
= (GdkGC
*) NULL
;
2205 if (m_textGC
) wxFreePoolGC( m_textGC
);
2206 m_textGC
= (GdkGC
*) NULL
;
2207 if (m_bgGC
) wxFreePoolGC( m_bgGC
);
2208 m_bgGC
= (GdkGC
*) NULL
;
2211 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
2213 m_deviceOriginX
= x
;
2214 m_deviceOriginY
= y
;
2216 ComputeScaleAndOrigin();
2219 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
2221 m_signX
= (xLeftRight
? 1 : -1);
2222 m_signY
= (yBottomUp
? -1 : 1);
2224 if (m_window
&& m_window
->m_wxwindow
&&
2225 (m_window
->GetLayoutDirection() == wxLayout_RightToLeft
))
2228 ComputeScaleAndOrigin();
2231 void wxWindowDCImpl::ComputeScaleAndOrigin()
2233 const wxRealPoint
origScale(m_scaleX
, m_scaleY
);
2236 wxDCImpl::ComputeScaleAndOrigin();
2238 wxDC::ComputeScaleAndOrigin();
2241 // if scale has changed call SetPen to recalulate the line width
2242 if ( wxRealPoint(m_scaleX
, m_scaleY
) != origScale
&& m_pen
.IsOk() )
2244 // this is a bit artificial, but we need to force wxDC to think the pen
2252 // Resolution in pixels per logical inch
2253 wxSize
wxWindowDCImpl::GetPPI() const
2255 return wxSize( (int) (m_mm_to_pix_x
* 25.4 + 0.5), (int) (m_mm_to_pix_y
* 25.4 + 0.5));
2258 int wxWindowDCImpl::GetDepth() const
2260 return gdk_drawable_get_depth(m_gdkwindow
);
2264 //-----------------------------------------------------------------------------
2266 //-----------------------------------------------------------------------------
2268 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl
, wxWindowDCImpl
)
2270 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
)
2271 : wxWindowDCImpl( owner
)
2275 wxClientDCImpl::wxClientDCImpl( wxDC
*owner
, wxWindow
*win
)
2276 : wxWindowDCImpl( owner
, win
)
2278 wxCHECK_RET( win
, _T("NULL window in wxClientDCImpl::wxClientDC") );
2280 #ifdef __WXUNIVERSAL__
2281 wxPoint ptOrigin
= win
->GetClientAreaOrigin();
2282 SetDeviceOrigin(ptOrigin
.x
, ptOrigin
.y
);
2283 wxSize size
= win
->GetClientSize();
2284 DoSetClippingRegion(0, 0, size
.x
, size
.y
);
2289 void wxClientDCImpl::DoGetSize(int *width
, int *height
) const
2291 wxCHECK_RET( m_window
, _T("GetSize() doesn't work without window") );
2293 m_window
->GetClientSize( width
, height
);
2296 //-----------------------------------------------------------------------------
2298 //-----------------------------------------------------------------------------
2300 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl
, wxClientDCImpl
)
2302 // Limit the paint region to the window size. Sometimes
2303 // the paint region is too big, and this risks X11 errors
2304 static void wxLimitRegionToSize(wxRegion
& region
, const wxSize
& sz
)
2306 wxRect originalRect
= region
.GetBox();
2307 wxRect
rect(originalRect
);
2308 if (rect
.width
+ rect
.x
> sz
.x
)
2309 rect
.width
= sz
.x
- rect
.x
;
2310 if (rect
.height
+ rect
.y
> sz
.y
)
2311 rect
.height
= sz
.y
- rect
.y
;
2312 if (rect
!= originalRect
)
2314 region
= wxRegion(rect
);
2315 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2316 originalRect
.x
, originalRect
.y
, originalRect
.width
, originalRect
.height
,
2317 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
2321 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
)
2322 : wxClientDCImpl( owner
)
2326 wxPaintDCImpl::wxPaintDCImpl( wxDC
*owner
, wxWindow
*win
)
2327 : wxClientDCImpl( owner
, win
)
2329 #if USE_PAINT_REGION
2330 if (!win
->m_clipPaintRegion
)
2333 wxSize sz
= win
->GetSize();
2334 m_paintClippingRegion
= win
->m_nativeUpdateRegion
;
2335 wxLimitRegionToSize(m_paintClippingRegion
, sz
);
2337 GdkRegion
*region
= m_paintClippingRegion
.GetRegion();
2340 m_currentClippingRegion
.Union( m_paintClippingRegion
);
2341 wxLimitRegionToSize(m_currentClippingRegion
, sz
);
2343 if (sz
.x
<= 0 || sz
.y
<= 0)
2346 gdk_gc_set_clip_region( m_penGC
, region
);
2347 gdk_gc_set_clip_region( m_brushGC
, region
);
2348 gdk_gc_set_clip_region( m_textGC
, region
);
2349 gdk_gc_set_clip_region( m_bgGC
, region
);
2354 // ----------------------------------------------------------------------------
2356 // ----------------------------------------------------------------------------
2358 class wxDCModule
: public wxModule
2365 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2368 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2370 bool wxDCModule::OnInit()
2376 void wxDCModule::OnExit()
2380 for (int i
= wxBRUSHSTYLE_LAST_HATCH
- wxBRUSHSTYLE_FIRST_HATCH
; i
--; )
2383 g_object_unref(hatches
[i
]);