]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dcclient.cpp
correction to last commit: don't test unsetenv() return value, it's void under Darwin
[wxWidgets.git] / src / gtk / dcclient.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/gtk/dcclient.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
6c9a19aa 6// Copyright: (c) 1998 Robert Roebling, Chris Breeze
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
d02af7bb
JJ
13#ifdef __VMS
14#define XCopyPlane XCOPYPLANE
15#endif
16
c801d85f 17#include "wx/dcclient.h"
e4db172a
WS
18
19#ifndef WX_PRECOMP
772b3767 20 #include "wx/window.h"
e4db172a 21 #include "wx/log.h"
f38924e8 22 #include "wx/dcmemory.h"
18680f86 23 #include "wx/math.h" // for floating-point functions
155ecd4c 24 #include "wx/image.h"
02761f6c 25 #include "wx/module.h"
e4db172a
WS
26#endif
27
db16cab4 28#include "wx/fontutil.h"
5713b349 29#include "wx/scrolwin.h"
e5131165 30
ed673c6a 31#include "wx/gtk/win_gtk.h"
a3669332 32#include "wx/gtk/private.h"
c801d85f 33
993f97ee 34#include <gdk/gdkx.h>
83624f79 35
809934d2
RR
36//-----------------------------------------------------------------------------
37// local defines
38//-----------------------------------------------------------------------------
39
e1208c31 40#define USE_PAINT_REGION 1
809934d2 41
c801d85f
KB
42//-----------------------------------------------------------------------------
43// local data
44//-----------------------------------------------------------------------------
45
46#include "bdiag.xbm"
47#include "fdiag.xbm"
48#include "cdiag.xbm"
49#include "horiz.xbm"
50#include "verti.xbm"
51#include "cross.xbm"
52#define num_hatches 6
53
3ca6a5f0
BP
54#define IS_15_PIX_HATCH(s) ((s)==wxCROSSDIAG_HATCH || (s)==wxHORIZONTAL_HATCH || (s)==wxVERTICAL_HATCH)
55#define IS_16_PIX_HATCH(s) ((s)!=wxCROSSDIAG_HATCH && (s)!=wxHORIZONTAL_HATCH && (s)!=wxVERTICAL_HATCH)
56
57
c801d85f 58static GdkPixmap *hatches[num_hatches];
c67daf87 59static GdkPixmap **hatch_bitmap = (GdkPixmap **) NULL;
c801d85f 60
c2fa61e8 61extern GtkWidget *wxGetRootWindow();
3d2d8da1 62
c801d85f
KB
63//-----------------------------------------------------------------------------
64// constants
65//-----------------------------------------------------------------------------
66
95724b1a 67const double RAD2DEG = 180.0 / M_PI;
c801d85f 68
9a8c7620
VZ
69// ----------------------------------------------------------------------------
70// private functions
71// ----------------------------------------------------------------------------
72
73static inline double dmax(double a, double b) { return a > b ? a : b; }
74static inline double dmin(double a, double b) { return a < b ? a : b; }
75
76static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
77
6f65e337
JS
78//-----------------------------------------------------------------------------
79// temporary implementation of the missing GDK function
80//-----------------------------------------------------------------------------
b0e0d661 81
6f65e337 82#include "gdk/gdkprivate.h"
b0e0d661 83
9be3f755 84static
1e6feb95
VZ
85void gdk_wx_draw_bitmap(GdkDrawable *drawable,
86 GdkGC *gc,
87 GdkDrawable *src,
88 gint xsrc,
89 gint ysrc,
90 gint xdest,
91 gint ydest,
92 gint width,
93 gint height)
6f65e337 94{
17a1ebd1
VZ
95 wxCHECK_RET( drawable, _T("NULL drawable in gdk_wx_draw_bitmap") );
96 wxCHECK_RET( src, _T("NULL src in gdk_wx_draw_bitmap") );
97 wxCHECK_RET( gc, _T("NULL gc in gdk_wx_draw_bitmap") );
ab9d0a8c 98
d90c9596 99 gint src_width, src_height;
8943b403 100 gdk_drawable_get_size(src, &src_width, &src_height);
8943b403
OK
101 if (width == -1) width = src_width;
102 if (height == -1) height = src_height;
265898fd 103
8943b403
OK
104 XCopyPlane( GDK_WINDOW_XDISPLAY(drawable),
105 GDK_WINDOW_XID(src),
106 GDK_WINDOW_XID(drawable),
107 GDK_GC_XGC(gc),
108 xsrc, ysrc,
109 width, height,
d90c9596 110 0, 0,
8943b403 111 1 );
6f65e337
JS
112}
113
3d2d8da1
RR
114//-----------------------------------------------------------------------------
115// Implement Pool of Graphic contexts. Creating them takes too much time.
116//-----------------------------------------------------------------------------
117
0e09f76e
RR
118enum wxPoolGCType
119{
120 wxGC_ERROR = 0,
121 wxTEXT_MONO,
122 wxBG_MONO,
123 wxPEN_MONO,
124 wxBRUSH_MONO,
125 wxTEXT_COLOUR,
126 wxBG_COLOUR,
127 wxPEN_COLOUR,
f6bcfd97
BP
128 wxBRUSH_COLOUR,
129 wxTEXT_SCREEN,
130 wxBG_SCREEN,
131 wxPEN_SCREEN,
132 wxBRUSH_SCREEN
0e09f76e
RR
133};
134
3d2d8da1
RR
135struct wxGC
136{
0e09f76e
RR
137 GdkGC *m_gc;
138 wxPoolGCType m_type;
139 bool m_used;
3d2d8da1
RR
140};
141
696f36b7
JS
142#define GC_POOL_ALLOC_SIZE 100
143
144static int wxGCPoolSize = 0;
145
146static wxGC *wxGCPool = NULL;
3d2d8da1
RR
147
148static void wxInitGCPool()
149{
696f36b7
JS
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.
ab9d0a8c 153
696f36b7
JS
154 // Set initial pool size.
155 wxGCPoolSize = GC_POOL_ALLOC_SIZE;
ab9d0a8c 156
696f36b7
JS
157 // Allocate initial pool.
158 wxGCPool = (wxGC *)malloc(wxGCPoolSize * sizeof(wxGC));
159 if (wxGCPool == NULL)
160 {
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
551b5391 164 // in wxGetPoolGC.
696f36b7
JS
165 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
166 return;
167 }
ab9d0a8c 168
696f36b7
JS
169 // Zero initial pool.
170 memset(wxGCPool, 0, wxGCPoolSize * sizeof(wxGC));
3d2d8da1
RR
171}
172
173static void wxCleanUpGCPool()
174{
f6116855 175 for (int i = 0; i < wxGCPoolSize; i++)
3d2d8da1
RR
176 {
177 if (wxGCPool[i].m_gc)
3fe39b0c 178 g_object_unref (wxGCPool[i].m_gc);
3d2d8da1 179 }
696f36b7
JS
180
181 free(wxGCPool);
182 wxGCPool = NULL;
183 wxGCPoolSize = 0;
3d2d8da1
RR
184}
185
0e09f76e 186static GdkGC* wxGetPoolGC( GdkWindow *window, wxPoolGCType type )
3d2d8da1 187{
696f36b7 188 wxGC *pptr;
ab9d0a8c 189
696f36b7
JS
190 // Look for an available GC.
191 for (int i = 0; i < wxGCPoolSize; i++)
3d2d8da1
RR
192 {
193 if (!wxGCPool[i].m_gc)
194 {
195 wxGCPool[i].m_gc = gdk_gc_new( window );
196 gdk_gc_set_exposures( wxGCPool[i].m_gc, FALSE );
0e09f76e 197 wxGCPool[i].m_type = type;
ab9d0a8c 198 wxGCPool[i].m_used = false;
3d2d8da1 199 }
0e09f76e 200 if ((!wxGCPool[i].m_used) && (wxGCPool[i].m_type == type))
3d2d8da1 201 {
ab9d0a8c 202 wxGCPool[i].m_used = true;
3d2d8da1
RR
203 return wxGCPool[i].m_gc;
204 }
205 }
5f170f33 206
696f36b7
JS
207 // We did not find an available GC.
208 // We need to grow the GC pool.
209 pptr = (wxGC *)realloc(wxGCPool,
551b5391 210 (wxGCPoolSize + GC_POOL_ALLOC_SIZE)*sizeof(wxGC));
696f36b7
JS
211 if (pptr != NULL)
212 {
213 // Initialize newly allocated pool.
214 wxGCPool = pptr;
551b5391
RR
215 memset(&wxGCPool[wxGCPoolSize], 0,
216 GC_POOL_ALLOC_SIZE*sizeof(wxGC));
ab9d0a8c
WS
217
218 // Initialize entry we will return.
696f36b7
JS
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;
ab9d0a8c
WS
222 wxGCPool[wxGCPoolSize].m_used = true;
223
696f36b7
JS
224 // Set new value of pool size.
225 wxGCPoolSize += GC_POOL_ALLOC_SIZE;
ab9d0a8c 226
696f36b7
JS
227 // Return newly allocated entry.
228 return wxGCPool[wxGCPoolSize-GC_POOL_ALLOC_SIZE].m_gc;
229 }
ab9d0a8c 230
696f36b7 231 // The realloc failed. Fall through to error.
3d2d8da1 232 wxFAIL_MSG( wxT("No GC available") );
5f170f33 233
3d2d8da1
RR
234 return (GdkGC*) NULL;
235}
236
237static void wxFreePoolGC( GdkGC *gc )
238{
696f36b7 239 for (int i = 0; i < wxGCPoolSize; i++)
3d2d8da1
RR
240 {
241 if (wxGCPool[i].m_gc == gc)
242 {
ab9d0a8c 243 wxGCPool[i].m_used = false;
3d2d8da1
RR
244 return;
245 }
246 }
5f170f33 247
3d2d8da1
RR
248 wxFAIL_MSG( wxT("Wrong GC") );
249}
250
c801d85f 251//-----------------------------------------------------------------------------
ec758a20 252// wxWindowDC
c801d85f
KB
253//-----------------------------------------------------------------------------
254
2970ae54
RR
255#if wxUSE_NEW_DC
256IMPLEMENT_DYNAMIC_CLASS(wxGTKWindowImplDC, wxGTKImplDC)
257#else
b0e0d661 258IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
2970ae54 259#endif
c801d85f 260
2970ae54 261wxGTKWindowImplDC::wxGTKWindowImplDC()
c801d85f 262{
265898fd
RR
263 m_penGC = (GdkGC *) NULL;
264 m_brushGC = (GdkGC *) NULL;
265 m_textGC = (GdkGC *) NULL;
266 m_bgGC = (GdkGC *) NULL;
267 m_cmap = (GdkColormap *) NULL;
ab9d0a8c
WS
268 m_isMemDC = false;
269 m_isScreenDC = false;
bbbbe360 270 m_owner = (wxWindow *)NULL;
8943b403 271 m_context = (PangoContext *)NULL;
cfcc3932 272 m_layout = (PangoLayout *)NULL;
8943b403 273 m_fontdesc = (PangoFontDescription *)NULL;
903f689b 274}
c801d85f 275
2970ae54 276wxGTKWindowImplDC::wxGTKWindowImplDC( wxWindow *window )
c801d85f 277{
3cd0b8c5
RR
278 wxASSERT_MSG( window, wxT("DC needs a window") );
279
265898fd
RR
280 m_penGC = (GdkGC *) NULL;
281 m_brushGC = (GdkGC *) NULL;
282 m_textGC = (GdkGC *) NULL;
283 m_bgGC = (GdkGC *) NULL;
284 m_cmap = (GdkColormap *) NULL;
bbbbe360 285 m_owner = (wxWindow *)NULL;
ab9d0a8c
WS
286 m_isMemDC = false;
287 m_isScreenDC = false;
3b245d60 288 m_font = window->GetFont();
7d5af6fa 289
a2053b27 290 GtkWidget *widget = window->m_wxwindow;
7d5af6fa 291
cfcc3932 292 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
b7f1f77f 293 // code should still be able to create wxClientDCs for them, so we will
cfcc3932 294 // use the parent window here then.
b7f1f77f
VZ
295 if ( !widget )
296 {
297 window = window->GetParent();
298 widget = window->m_wxwindow;
299 }
300
223d09f6 301 wxASSERT_MSG( widget, wxT("DC needs a widget") );
7d5af6fa 302
2b5f62a0 303 m_context = window->GtkGetPangoDefaultContext();
cfcc3932
RR
304 m_layout = pango_layout_new( m_context );
305 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
8943b403 306
da048e3d
RR
307 GtkPizza *pizza = GTK_PIZZA( widget );
308 m_window = pizza->bin_window;
7d5af6fa 309
cfcc3932 310 // Window not realized ?
1e133b7d
RR
311 if (!m_window)
312 {
cfcc3932 313 // Don't report problems as per MSW.
ab9d0a8c 314 m_ok = true;
7d5af6fa
VZ
315
316 return;
1e133b7d 317 }
7d5af6fa 318
b7f1f77f 319 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
7d5af6fa 320
265898fd 321 SetUpDC();
bbbbe360
RR
322
323 /* this must be done after SetUpDC, bacause SetUpDC calls the
324 repective SetBrush, SetPen, SetBackground etc functions
325 to set up the DC. SetBackground call m_owner->SetBackground
326 and this might not be desired as the standard dc background
327 is white whereas a window might assume gray to be the
328 standard (as e.g. wxStatusBar) */
7d5af6fa 329
bbbbe360 330 m_owner = window;
f4322df6 331
847dfdb4
RR
332 if (m_owner && m_owner->m_wxwindow && (m_owner->GetLayoutDirection() == wxLayout_RightToLeft))
333 {
5713b349 334 // reverse sense
847dfdb4 335 m_signX = -1;
807a572e 336
5713b349 337 // origin in the upper right corner
807a572e 338 m_deviceOriginX = m_owner->GetClientSize().x;
847dfdb4 339 }
903f689b 340}
c801d85f 341
2970ae54 342wxGTKWindowImplDC::~wxGTKWindowImplDC()
c801d85f 343{
265898fd 344 Destroy();
ab9d0a8c 345
cfcc3932 346 if (m_layout)
3fe39b0c 347 g_object_unref (m_layout);
cfcc3932
RR
348 if (m_fontdesc)
349 pango_font_description_free( m_fontdesc );
903f689b 350}
c801d85f 351
2970ae54 352void wxGTKWindowImplDC::SetUpDC()
809934d2 353{
ab9d0a8c 354 m_ok = true;
5f170f33 355
809934d2 356 wxASSERT_MSG( !m_penGC, wxT("GCs already created") );
5f170f33 357
f6bcfd97
BP
358 if (m_isScreenDC)
359 {
360 m_penGC = wxGetPoolGC( m_window, wxPEN_SCREEN );
361 m_brushGC = wxGetPoolGC( m_window, wxBRUSH_SCREEN );
362 m_textGC = wxGetPoolGC( m_window, wxTEXT_SCREEN );
363 m_bgGC = wxGetPoolGC( m_window, wxBG_SCREEN );
364 }
365 else
809934d2
RR
366 if (m_isMemDC && (((wxMemoryDC*)this)->m_selected.GetDepth() == 1))
367 {
368 m_penGC = wxGetPoolGC( m_window, wxPEN_MONO );
369 m_brushGC = wxGetPoolGC( m_window, wxBRUSH_MONO );
370 m_textGC = wxGetPoolGC( m_window, wxTEXT_MONO );
371 m_bgGC = wxGetPoolGC( m_window, wxBG_MONO );
372 }
373 else
374 {
375 m_penGC = wxGetPoolGC( m_window, wxPEN_COLOUR );
376 m_brushGC = wxGetPoolGC( m_window, wxBRUSH_COLOUR );
377 m_textGC = wxGetPoolGC( m_window, wxTEXT_COLOUR );
378 m_bgGC = wxGetPoolGC( m_window, wxBG_COLOUR );
379 }
380
381 /* background colour */
382 m_backgroundBrush = *wxWHITE_BRUSH;
383 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
2796f657 384#ifdef __WXGTK24__
c6685317 385 const GdkColor *bg_col = m_backgroundBrush.GetColour().GetColor();
2796f657
PC
386#else
387 GdkColor *bg_col = m_backgroundBrush.GetColour().GetColor();
388#endif
809934d2
RR
389
390 /* m_textGC */
391 m_textForegroundColour.CalcPixel( m_cmap );
392 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
393
394 m_textBackgroundColour.CalcPixel( m_cmap );
395 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
396
397 gdk_gc_set_fill( m_textGC, GDK_SOLID );
f4322df6 398
4ba1c184 399 gdk_gc_set_colormap( m_textGC, m_cmap );
809934d2
RR
400
401 /* m_penGC */
402 m_pen.GetColour().CalcPixel( m_cmap );
403 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
404 gdk_gc_set_background( m_penGC, bg_col );
5f170f33 405
809934d2 406 gdk_gc_set_line_attributes( m_penGC, 0, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_ROUND );
5f170f33 407
809934d2
RR
408 /* m_brushGC */
409 m_brush.GetColour().CalcPixel( m_cmap );
410 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
411 gdk_gc_set_background( m_brushGC, bg_col );
5f170f33 412
809934d2 413 gdk_gc_set_fill( m_brushGC, GDK_SOLID );
5f170f33 414
809934d2
RR
415 /* m_bgGC */
416 gdk_gc_set_background( m_bgGC, bg_col );
417 gdk_gc_set_foreground( m_bgGC, bg_col );
418
419 gdk_gc_set_fill( m_bgGC, GDK_SOLID );
5f170f33 420
809934d2
RR
421 /* ROPs */
422 gdk_gc_set_function( m_textGC, GDK_COPY );
423 gdk_gc_set_function( m_brushGC, GDK_COPY );
424 gdk_gc_set_function( m_penGC, GDK_COPY );
5f170f33 425
809934d2
RR
426 /* clipping */
427 gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL );
428 gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL );
429 gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL );
430 gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL );
431
432 if (!hatch_bitmap)
433 {
434 hatch_bitmap = hatches;
435 hatch_bitmap[0] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, bdiag_bits, bdiag_width, bdiag_height );
436 hatch_bitmap[1] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cdiag_bits, cdiag_width, cdiag_height );
437 hatch_bitmap[2] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, fdiag_bits, fdiag_width, fdiag_height );
438 hatch_bitmap[3] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cross_bits, cross_width, cross_height );
439 hatch_bitmap[4] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, horiz_bits, horiz_width, horiz_height );
440 hatch_bitmap[5] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, verti_bits, verti_width, verti_height );
441 }
442}
443
2970ae54 444void wxGTKWindowImplDC::DoGetSize( int* width, int* height ) const
376aa62a
VZ
445{
446 wxCHECK_RET( m_owner, _T("GetSize() doesn't work without window") );
447
448 m_owner->GetSize(width, height);
449}
450
2970ae54 451bool wxGTKWindowImplDC::DoFloodFill(wxCoord x, wxCoord y,
06cf7c08
VS
452 const wxColour& col, int style)
453{
c0521644
VZ
454#if wxUSE_IMAGE
455 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
456 const wxColour & col, int style);
457
387ebd3e 458 return wxDoFloodFill(this, x, y, col, style);
c0521644
VZ
459#else
460 return false;
461#endif
903f689b 462}
c801d85f 463
2970ae54 464bool wxGTKWindowImplDC::DoGetPixel( wxCoord x1, wxCoord y1, wxColour *col ) const
c801d85f 465{
c0521644 466#if wxUSE_IMAGE
dc1efb1d
JS
467 // Generic (and therefore rather inefficient) method.
468 // Could be improved.
469 wxMemoryDC memdc;
470 wxBitmap bitmap(1, 1);
471 memdc.SelectObject(bitmap);
472 memdc.Blit(0, 0, 1, 1, (wxDC*) this, x1, y1);
473 memdc.SelectObject(wxNullBitmap);
c2fa61e8 474
368d59f0 475 wxImage image = bitmap.ConvertToImage();
dc1efb1d 476 col->Set(image.GetRed(0, 0), image.GetGreen(0, 0), image.GetBlue(0, 0));
ab9d0a8c 477 return true;
c0521644
VZ
478#else // !wxUSE_IMAGE
479 return false;
480#endif // wxUSE_IMAGE/!wxUSE_IMAGE
903f689b 481}
c801d85f 482
2970ae54 483void wxGTKWindowImplDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
c801d85f 484{
223d09f6 485 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 486
265898fd
RR
487 if (m_pen.GetStyle() != wxTRANSPARENT)
488 {
6db90681
RR
489 if (m_window)
490 gdk_draw_line( m_window, m_penGC, XLOG2DEV(x1), YLOG2DEV(y1), XLOG2DEV(x2), YLOG2DEV(y2) );
7d5af6fa 491
ed880dd4
RR
492 CalcBoundingBox(x1, y1);
493 CalcBoundingBox(x2, y2);
265898fd 494 }
903f689b 495}
c801d85f 496
2970ae54 497void wxGTKWindowImplDC::DoCrossHair( wxCoord x, wxCoord y )
c801d85f 498{
223d09f6 499 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 500
265898fd
RR
501 if (m_pen.GetStyle() != wxTRANSPARENT)
502 {
503 int w = 0;
504 int h = 0;
505 GetSize( &w, &h );
72cdf4c9
VZ
506 wxCoord xx = XLOG2DEV(x);
507 wxCoord yy = YLOG2DEV(y);
6db90681 508 if (m_window)
7d5af6fa 509 {
6db90681
RR
510 gdk_draw_line( m_window, m_penGC, 0, yy, XLOG2DEVREL(w), yy );
511 gdk_draw_line( m_window, m_penGC, xx, 0, xx, YLOG2DEVREL(h) );
7d5af6fa 512 }
265898fd 513 }
903f689b 514}
c801d85f 515
2970ae54 516void wxGTKWindowImplDC::DoDrawArc( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2,
72cdf4c9 517 wxCoord xc, wxCoord yc )
c801d85f 518{
223d09f6 519 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 520
72cdf4c9
VZ
521 wxCoord xx1 = XLOG2DEV(x1);
522 wxCoord yy1 = YLOG2DEV(y1);
523 wxCoord xx2 = XLOG2DEV(x2);
524 wxCoord yy2 = YLOG2DEV(y2);
525 wxCoord xxc = XLOG2DEV(xc);
526 wxCoord yyc = YLOG2DEV(yc);
7d5af6fa 527 double dx = xx1 - xxc;
265898fd 528 double dy = yy1 - yyc;
de87c353 529 double radius = sqrt((double)(dx*dx+dy*dy));
72cdf4c9 530 wxCoord r = (wxCoord)radius;
265898fd
RR
531 double radius1, radius2;
532
7d5af6fa 533 if (xx1 == xx2 && yy1 == yy2)
265898fd
RR
534 {
535 radius1 = 0.0;
536 radius2 = 360.0;
7d5af6fa 537 }
c77a6796 538 else if ( wxIsNullDouble(radius) )
265898fd 539 {
c77a6796
VZ
540 radius1 =
541 radius2 = 0.0;
7d5af6fa
VZ
542 }
543 else
265898fd
RR
544 {
545 radius1 = (xx1 - xxc == 0) ?
b0e0d661
VZ
546 (yy1 - yyc < 0) ? 90.0 : -90.0 :
547 -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
265898fd 548 radius2 = (xx2 - xxc == 0) ?
b0e0d661
VZ
549 (yy2 - yyc < 0) ? 90.0 : -90.0 :
550 -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
265898fd 551 }
72cdf4c9
VZ
552 wxCoord alpha1 = wxCoord(radius1 * 64.0);
553 wxCoord alpha2 = wxCoord((radius2 - radius1) * 64.0);
265898fd
RR
554 while (alpha2 <= 0) alpha2 += 360*64;
555 while (alpha1 > 360*64) alpha1 -= 360*64;
556
6db90681
RR
557 if (m_window)
558 {
559 if (m_brush.GetStyle() != wxTRANSPARENT)
e9f88d04
VZ
560 {
561 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
562 {
563 gdk_gc_set_ts_origin( m_textGC,
564 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
565 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
566 gdk_draw_arc( m_window, m_textGC, TRUE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
567 gdk_gc_set_ts_origin( m_textGC, 0, 0 );
568 } else
3ca6a5f0
BP
569 if (IS_15_PIX_HATCH(m_brush.GetStyle()))
570 {
571 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 15, m_deviceOriginY % 15 );
572 gdk_draw_arc( m_window, m_brushGC, TRUE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
573 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
574 } else
575 if (IS_16_PIX_HATCH(m_brush.GetStyle()))
576 {
577 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 16, m_deviceOriginY % 16 );
578 gdk_draw_arc( m_window, m_brushGC, TRUE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
579 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
580 } else
e9f88d04
VZ
581 if (m_brush.GetStyle() == wxSTIPPLE)
582 {
583 gdk_gc_set_ts_origin( m_brushGC,
584 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
585 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
586 gdk_draw_arc( m_window, m_brushGC, TRUE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
587 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
588 }
589 else
590 {
591 gdk_draw_arc( m_window, m_brushGC, TRUE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
592 }
593 }
7d5af6fa 594
6db90681 595 if (m_pen.GetStyle() != wxTRANSPARENT)
3d0c4d2e 596 {
6db90681 597 gdk_draw_arc( m_window, m_penGC, FALSE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
f469b27c 598
9707fd13
RR
599 if ((m_brush.GetStyle() != wxTRANSPARENT) && (alpha2 - alpha1 != 360*64))
600 {
601 gdk_draw_line( m_window, m_penGC, xx1, yy1, xxc, yyc );
602 gdk_draw_line( m_window, m_penGC, xxc, yyc, xx2, yy2 );
603 }
3d0c4d2e 604 }
6db90681 605 }
7d5af6fa 606
265898fd
RR
607 CalcBoundingBox (x1, y1);
608 CalcBoundingBox (x2, y2);
903f689b 609}
c801d85f 610
2970ae54 611void wxGTKWindowImplDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double sa, double ea )
c801d85f 612{
223d09f6 613 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 614
72cdf4c9
VZ
615 wxCoord xx = XLOG2DEV(x);
616 wxCoord yy = YLOG2DEV(y);
617 wxCoord ww = m_signX * XLOG2DEVREL(width);
618 wxCoord hh = m_signY * YLOG2DEVREL(height);
7d5af6fa 619
265898fd
RR
620 // CMB: handle -ve width and/or height
621 if (ww < 0) { ww = -ww; xx = xx - ww; }
622 if (hh < 0) { hh = -hh; yy = yy - hh; }
7d5af6fa 623
6db90681
RR
624 if (m_window)
625 {
72cdf4c9 626 wxCoord start = wxCoord(sa * 64.0);
3d0c4d2e 627 wxCoord end = wxCoord((ea-sa) * 64.0);
7d5af6fa 628
6db90681 629 if (m_brush.GetStyle() != wxTRANSPARENT)
e9f88d04
VZ
630 {
631 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
632 {
633 gdk_gc_set_ts_origin( m_textGC,
634 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
635 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
636 gdk_draw_arc( m_window, m_textGC, TRUE, xx, yy, ww, hh, start, end );
637 gdk_gc_set_ts_origin( m_textGC, 0, 0 );
638 } else
3ca6a5f0
BP
639 if (IS_15_PIX_HATCH(m_brush.GetStyle()))
640 {
641 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 15, m_deviceOriginY % 15 );
642 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, start, end );
643 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
644 } else
645 if (IS_16_PIX_HATCH(m_brush.GetStyle()))
646 {
647 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 16, m_deviceOriginY % 16 );
648 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, start, end );
649 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
650 } else
e9f88d04
VZ
651 if (m_brush.GetStyle() == wxSTIPPLE)
652 {
653 gdk_gc_set_ts_origin( m_brushGC,
654 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
655 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
656 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, start, end );
657 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
658 }
659 else
660 {
661 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, start, end );
662 }
663 }
7d5af6fa 664
6db90681
RR
665 if (m_pen.GetStyle() != wxTRANSPARENT)
666 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, ww, hh, start, end );
667 }
7d5af6fa 668
265898fd
RR
669 CalcBoundingBox (x, y);
670 CalcBoundingBox (x + width, y + height);
903f689b 671}
c801d85f 672
2970ae54 673void wxGTKWindowImplDC::DoDrawPoint( wxCoord x, wxCoord y )
c801d85f 674{
223d09f6 675 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 676
6db90681 677 if ((m_pen.GetStyle() != wxTRANSPARENT) && m_window)
265898fd 678 gdk_draw_point( m_window, m_penGC, XLOG2DEV(x), YLOG2DEV(y) );
7d5af6fa 679
265898fd 680 CalcBoundingBox (x, y);
903f689b 681}
c801d85f 682
2970ae54 683void wxGTKWindowImplDC::DoDrawLines( int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset )
c801d85f 684{
223d09f6 685 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 686
265898fd
RR
687 if (m_pen.GetStyle() == wxTRANSPARENT) return;
688 if (n <= 0) return;
7d5af6fa 689
e90411c2 690 //Check, if scaling is necessary
280831d5
PC
691 const bool doScale =
692 xoffset != 0 || yoffset != 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
e90411c2 693
280831d5
PC
694 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
695 GdkPoint* gpts = reinterpret_cast<GdkPoint*>(points);
e90411c2 696
280831d5 697 if (doScale)
e90411c2 698 gpts = new GdkPoint[n];
7d5af6fa 699
ab9d0a8c 700 for (int i = 0; i < n; i++)
265898fd 701 {
280831d5
PC
702 if (doScale)
703 {
704 gpts[i].x = XLOG2DEV(points[i].x + xoffset);
705 gpts[i].y = YLOG2DEV(points[i].y + yoffset);
e90411c2 706 }
280831d5 707 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
e90411c2 708 }
6eb37239
VZ
709
710 if (m_window)
711 gdk_draw_lines( m_window, m_penGC, gpts, n);
712
e90411c2 713 if (doScale)
280831d5 714 delete[] gpts;
903f689b 715}
c801d85f 716
2970ae54 717void wxGTKWindowImplDC::DoDrawPolygon( int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int WXUNUSED(fillStyle) )
cf7a7e13 718{
223d09f6 719 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 720
4bc67cc5 721 if (n <= 0) return;
7d5af6fa 722
e90411c2 723 //Check, if scaling is necessary
280831d5
PC
724 const bool doScale =
725 xoffset != 0 || yoffset != 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
e90411c2 726
280831d5
PC
727 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
728 GdkPoint* gdkpoints = reinterpret_cast<GdkPoint*>(points);
e90411c2 729
280831d5
PC
730 if (doScale)
731 gdkpoints = new GdkPoint[n];
e90411c2 732
265898fd
RR
733 int i;
734 for (i = 0 ; i < n ; i++)
735 {
280831d5
PC
736 if (doScale)
737 {
738 gdkpoints[i].x = XLOG2DEV(points[i].x + xoffset);
739 gdkpoints[i].y = YLOG2DEV(points[i].y + yoffset);
e90411c2 740 }
280831d5 741 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
e90411c2 742 }
7d5af6fa 743
de2d2cdc 744 if (m_window)
72174350 745 {
e1208c31 746 if (m_brush.GetStyle() != wxTRANSPARENT)
72174350 747 {
e1208c31
RR
748 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
749 {
5f170f33
VZ
750 gdk_gc_set_ts_origin( m_textGC,
751 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
e1208c31
RR
752 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
753 gdk_draw_polygon( m_window, m_textGC, TRUE, gdkpoints, n );
754 gdk_gc_set_ts_origin( m_textGC, 0, 0 );
755 } else
3ca6a5f0
BP
756 if (IS_15_PIX_HATCH(m_brush.GetStyle()))
757 {
758 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 15, m_deviceOriginY % 15 );
759 gdk_draw_polygon( m_window, m_brushGC, TRUE, gdkpoints, n );
760 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
761 } else
762 if (IS_16_PIX_HATCH(m_brush.GetStyle()))
763 {
764 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 16, m_deviceOriginY % 16 );
765 gdk_draw_polygon( m_window, m_brushGC, TRUE, gdkpoints, n );
766 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
767 } else
e1208c31
RR
768 if (m_brush.GetStyle() == wxSTIPPLE)
769 {
5f170f33
VZ
770 gdk_gc_set_ts_origin( m_brushGC,
771 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
e1208c31
RR
772 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
773 gdk_draw_polygon( m_window, m_brushGC, TRUE, gdkpoints, n );
774 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
775 }
776 else
777 {
778 gdk_draw_polygon( m_window, m_brushGC, TRUE, gdkpoints, n );
779 }
72174350 780 }
7d5af6fa 781
e1208c31 782 if (m_pen.GetStyle() != wxTRANSPARENT)
b0e0d661 783 {
bb56b0a8 784/*
e1208c31
RR
785 for (i = 0 ; i < n ; i++)
786 {
787 gdk_draw_line( m_window, m_penGC,
788 gdkpoints[i%n].x,
789 gdkpoints[i%n].y,
790 gdkpoints[(i+1)%n].x,
791 gdkpoints[(i+1)%n].y);
792 }
bb56b0a8
JS
793*/
794 gdk_draw_polygon( m_window, m_penGC, FALSE, gdkpoints, n );
ab9d0a8c 795
265898fd 796 }
6db90681 797 }
7d5af6fa 798
e90411c2 799 if (doScale)
280831d5 800 delete[] gdkpoints;
903f689b 801}
c801d85f 802
2970ae54 803void wxGTKWindowImplDC::DoDrawRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 804{
223d09f6 805 wxCHECK_RET( Ok(), wxT("invalid window dc") );
c801d85f 806
72cdf4c9
VZ
807 wxCoord xx = XLOG2DEV(x);
808 wxCoord yy = YLOG2DEV(y);
809 wxCoord ww = m_signX * XLOG2DEVREL(width);
810 wxCoord hh = m_signY * YLOG2DEVREL(height);
7d5af6fa 811
265898fd
RR
812 // CMB: draw nothing if transformed w or h is 0
813 if (ww == 0 || hh == 0) return;
6f65e337 814
265898fd
RR
815 // CMB: handle -ve width and/or height
816 if (ww < 0) { ww = -ww; xx = xx - ww; }
817 if (hh < 0) { hh = -hh; yy = yy - hh; }
6f65e337 818
6db90681
RR
819 if (m_window)
820 {
e1208c31 821 if (m_brush.GetStyle() != wxTRANSPARENT)
72174350 822 {
e1208c31
RR
823 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
824 {
5f170f33
VZ
825 gdk_gc_set_ts_origin( m_textGC,
826 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
e1208c31
RR
827 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
828 gdk_draw_rectangle( m_window, m_textGC, TRUE, xx, yy, ww, hh );
829 gdk_gc_set_ts_origin( m_textGC, 0, 0 );
3ca6a5f0
BP
830 } else
831 if (IS_15_PIX_HATCH(m_brush.GetStyle()))
832 {
833 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 15, m_deviceOriginY % 15 );
834 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh );
835 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
836 } else
837 if (IS_16_PIX_HATCH(m_brush.GetStyle()))
838 {
839 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 16, m_deviceOriginY % 16 );
840 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh );
841 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
842 } else
843 if (m_brush.GetStyle() == wxSTIPPLE)
e1208c31 844 {
5f170f33
VZ
845 gdk_gc_set_ts_origin( m_brushGC,
846 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
e1208c31 847 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
72174350 848 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh );
e1208c31
RR
849 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
850 }
851 else
852 {
853 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh );
854 }
72174350 855 }
e1208c31
RR
856
857 if (m_pen.GetStyle() != wxTRANSPARENT)
3b5bf828
RR
858 {
859#if 1
860 if ((m_pen.GetWidth() == 2) && (m_pen.GetCap() == wxCAP_ROUND) &&
861 (m_pen.GetJoin() == wxJOIN_ROUND) && (m_pen.GetStyle() == wxSOLID))
862 {
863 // Use 2 1-line rects instead
864 gdk_gc_set_line_attributes( m_penGC, 1, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
865
866 if (m_signX == -1)
867 {
868 // Different for RTL
869 gdk_draw_rectangle( m_window, m_penGC, FALSE, xx+1, yy, ww-2, hh-2 );
870 gdk_draw_rectangle( m_window, m_penGC, FALSE, xx, yy-1, ww, hh );
871 }
872 else
873 {
874 gdk_draw_rectangle( m_window, m_penGC, FALSE, xx, yy, ww-2, hh-2 );
875 gdk_draw_rectangle( m_window, m_penGC, FALSE, xx-1, yy-1, ww, hh );
876 }
f4322df6 877
3b5bf828
RR
878 // reset
879 gdk_gc_set_line_attributes( m_penGC, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
880 }
881 else
882#endif
883 {
884 // Just use X11 for other cases
885 gdk_draw_rectangle( m_window, m_penGC, FALSE, xx, yy, ww-1, hh-1 );
886 }
887 }
6db90681 888 }
7d5af6fa 889
265898fd
RR
890 CalcBoundingBox( x, y );
891 CalcBoundingBox( x + width, y + height );
903f689b 892}
c801d85f 893
2970ae54 894void wxGTKWindowImplDC::DoDrawRoundedRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius )
c801d85f 895{
223d09f6 896 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 897
265898fd 898 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
7d5af6fa 899
72cdf4c9
VZ
900 wxCoord xx = XLOG2DEV(x);
901 wxCoord yy = YLOG2DEV(y);
902 wxCoord ww = m_signX * XLOG2DEVREL(width);
903 wxCoord hh = m_signY * YLOG2DEVREL(height);
904 wxCoord rr = XLOG2DEVREL((wxCoord)radius);
265898fd
RR
905
906 // CMB: handle -ve width and/or height
907 if (ww < 0) { ww = -ww; xx = xx - ww; }
908 if (hh < 0) { hh = -hh; yy = yy - hh; }
909
910 // CMB: if radius is zero use DrawRectangle() instead to avoid
911 // X drawing errors with small radii
912 if (rr == 0)
913 {
914 DrawRectangle( x, y, width, height );
915 return;
916 }
917
918 // CMB: draw nothing if transformed w or h is 0
919 if (ww == 0 || hh == 0) return;
920
921 // CMB: adjust size if outline is drawn otherwise the result is
922 // 1 pixel too wide and high
923 if (m_pen.GetStyle() != wxTRANSPARENT)
924 {
925 ww--;
926 hh--;
927 }
928
6db90681 929 if (m_window)
265898fd 930 {
6db90681
RR
931 // CMB: ensure dd is not larger than rectangle otherwise we
932 // get an hour glass shape
72cdf4c9 933 wxCoord dd = 2 * rr;
6db90681
RR
934 if (dd > ww) dd = ww;
935 if (dd > hh) dd = hh;
936 rr = dd / 2;
937
938 if (m_brush.GetStyle() != wxTRANSPARENT)
939 {
a56fcaaf
RR
940 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
941 {
5f170f33
VZ
942 gdk_gc_set_ts_origin( m_textGC,
943 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
a56fcaaf
RR
944 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
945 gdk_draw_rectangle( m_window, m_textGC, TRUE, xx+rr, yy, ww-dd+1, hh );
946 gdk_draw_rectangle( m_window, m_textGC, TRUE, xx, yy+rr, ww, hh-dd+1 );
947 gdk_draw_arc( m_window, m_textGC, TRUE, xx, yy, dd, dd, 90*64, 90*64 );
948 gdk_draw_arc( m_window, m_textGC, TRUE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
949 gdk_draw_arc( m_window, m_textGC, TRUE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
950 gdk_draw_arc( m_window, m_textGC, TRUE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
951 gdk_gc_set_ts_origin( m_textGC, 0, 0 );
3ca6a5f0
BP
952 } else
953 if (IS_15_PIX_HATCH(m_brush.GetStyle()))
954 {
955 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 15, m_deviceOriginY % 15 );
956 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx+rr, yy, ww-dd+1, hh );
957 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy+rr, ww, hh-dd+1 );
958 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, dd, dd, 90*64, 90*64 );
959 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
960 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
961 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
962 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
963 } else
964 if (IS_16_PIX_HATCH(m_brush.GetStyle()))
965 {
966 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 16, m_deviceOriginY % 16 );
967 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx+rr, yy, ww-dd+1, hh );
968 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy+rr, ww, hh-dd+1 );
969 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, dd, dd, 90*64, 90*64 );
970 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
971 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
972 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
973 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
974 } else
975 if (m_brush.GetStyle() == wxSTIPPLE)
a56fcaaf 976 {
5f170f33
VZ
977 gdk_gc_set_ts_origin( m_brushGC,
978 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
a56fcaaf
RR
979 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
980 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx+rr, yy, ww-dd+1, hh );
981 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy+rr, ww, hh-dd+1 );
982 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, dd, dd, 90*64, 90*64 );
983 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
984 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
985 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
986 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
987 }
988 else
989 {
990 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx+rr, yy, ww-dd+1, hh );
991 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy+rr, ww, hh-dd+1 );
992 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, dd, dd, 90*64, 90*64 );
993 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
994 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
995 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
996 }
6db90681 997 }
7d5af6fa 998
6db90681 999 if (m_pen.GetStyle() != wxTRANSPARENT)
7d5af6fa 1000 {
3ca6a5f0
BP
1001 gdk_draw_line( m_window, m_penGC, xx+rr+1, yy, xx+ww-rr, yy );
1002 gdk_draw_line( m_window, m_penGC, xx+rr+1, yy+hh, xx+ww-rr, yy+hh );
1003 gdk_draw_line( m_window, m_penGC, xx, yy+rr+1, xx, yy+hh-rr );
1004 gdk_draw_line( m_window, m_penGC, xx+ww, yy+rr+1, xx+ww, yy+hh-rr );
6db90681
RR
1005 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, dd, dd, 90*64, 90*64 );
1006 gdk_draw_arc( m_window, m_penGC, FALSE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
1007 gdk_draw_arc( m_window, m_penGC, FALSE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
1008 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
7d5af6fa 1009 }
265898fd 1010 }
7d5af6fa 1011
265898fd
RR
1012 // this ignores the radius
1013 CalcBoundingBox( x, y );
1014 CalcBoundingBox( x + width, y + height );
903f689b 1015}
c801d85f 1016
2970ae54 1017void wxGTKWindowImplDC::DoDrawEllipse( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 1018{
223d09f6 1019 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 1020
72cdf4c9
VZ
1021 wxCoord xx = XLOG2DEV(x);
1022 wxCoord yy = YLOG2DEV(y);
1023 wxCoord ww = m_signX * XLOG2DEVREL(width);
1024 wxCoord hh = m_signY * YLOG2DEVREL(height);
6f65e337 1025
265898fd
RR
1026 // CMB: handle -ve width and/or height
1027 if (ww < 0) { ww = -ww; xx = xx - ww; }
1028 if (hh < 0) { hh = -hh; yy = yy - hh; }
7d5af6fa 1029
6db90681
RR
1030 if (m_window)
1031 {
1032 if (m_brush.GetStyle() != wxTRANSPARENT)
a56fcaaf
RR
1033 {
1034 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
1035 {
5f170f33
VZ
1036 gdk_gc_set_ts_origin( m_textGC,
1037 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
a56fcaaf
RR
1038 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
1039 gdk_draw_arc( m_window, m_textGC, TRUE, xx, yy, ww, hh, 0, 360*64 );
1040 gdk_gc_set_ts_origin( m_textGC, 0, 0 );
3ca6a5f0
BP
1041 } else
1042 if (IS_15_PIX_HATCH(m_brush.GetStyle()))
1043 {
1044 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 15, m_deviceOriginY % 15 );
1045 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, 0, 360*64 );
1046 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
1047 } else
1048 if (IS_16_PIX_HATCH(m_brush.GetStyle()))
1049 {
1050 gdk_gc_set_ts_origin( m_brushGC, m_deviceOriginX % 16, m_deviceOriginY % 16 );
1051 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, 0, 360*64 );
1052 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
1053 } else
1054 if (m_brush.GetStyle() == wxSTIPPLE)
a56fcaaf 1055 {
5f170f33
VZ
1056 gdk_gc_set_ts_origin( m_brushGC,
1057 m_deviceOriginX % m_brush.GetStipple()->GetWidth(),
a56fcaaf
RR
1058 m_deviceOriginY % m_brush.GetStipple()->GetHeight() );
1059 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, 0, 360*64 );
1060 gdk_gc_set_ts_origin( m_brushGC, 0, 0 );
1061 }
1062 else
1063 {
1064 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, 0, 360*64 );
1065 }
1066 }
7d5af6fa 1067
6db90681
RR
1068 if (m_pen.GetStyle() != wxTRANSPARENT)
1069 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, ww, hh, 0, 360*64 );
1070 }
7d5af6fa 1071
44856297 1072 CalcBoundingBox( x, y );
265898fd 1073 CalcBoundingBox( x + width, y + height );
903f689b 1074}
c801d85f 1075
2970ae54 1076void wxGTKWindowImplDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
c801d85f 1077{
b0e0d661 1078 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
ab9d0a8c 1079 DoDrawBitmap( (const wxBitmap&)icon, x, y, true );
903f689b 1080}
c801d85f 1081
2970ae54 1082void wxGTKWindowImplDC::DoDrawBitmap( const wxBitmap &bitmap,
72cdf4c9 1083 wxCoord x, wxCoord y,
b0e0d661 1084 bool useMask )
4bc67cc5 1085{
223d09f6 1086 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 1087
223d09f6 1088 wxCHECK_RET( bitmap.Ok(), wxT("invalid bitmap") );
7d5af6fa 1089
b85229d1 1090 bool is_mono = bitmap.GetDepth() == 1;
82ea63e6 1091
d90c9596 1092 // scale/translate size and position
265898fd
RR
1093 int xx = XLOG2DEV(x);
1094 int yy = YLOG2DEV(y);
7d5af6fa 1095
4bc67cc5
RR
1096 int w = bitmap.GetWidth();
1097 int h = bitmap.GetHeight();
f4322df6 1098
847dfdb4
RR
1099 if (m_owner && m_owner->GetLayoutDirection() == wxLayout_RightToLeft)
1100 xx -= w;
f4322df6 1101
6453876e
RR
1102 CalcBoundingBox( x, y );
1103 CalcBoundingBox( x + w, y + h );
7d5af6fa 1104
6453876e 1105 if (!m_window) return;
7d5af6fa 1106
4bc67cc5
RR
1107 int ww = XLOG2DEVREL(w);
1108 int hh = YLOG2DEVREL(h);
7d5af6fa 1109
d90c9596 1110 // compare to current clipping region
e1208c31 1111 if (!m_currentClippingRegion.IsNull())
3d2d8da1
RR
1112 {
1113 wxRegion tmp( xx,yy,ww,hh );
1114 tmp.Intersect( m_currentClippingRegion );
1115 if (tmp.IsEmpty())
1116 return;
1117 }
5f170f33 1118
ab9d0a8c 1119 // scale bitmap if required
d90c9596 1120 wxBitmap use_bitmap = bitmap;
4bc67cc5 1121 if ((w != ww) || (h != hh))
d90c9596 1122 use_bitmap = use_bitmap.Rescale( 0, 0, ww, hh, ww, hh );
ab9d0a8c 1123
1abedc32
VS
1124 // NB: We can't render pixbufs with GTK+ < 2.2, we need to use pixmaps code.
1125 // Pixbufs-based bitmaps with alpha channel don't have a mask, so we
1126 // have to call GetPixmap() here -- it converts the pixbuf into pixmap
1127 // and also creates the mask as a side-effect:
93de3784
PC
1128 if (gtk_check_version(2,2,0))
1129 use_bitmap.GetPixmap();
ab9d0a8c 1130
d90c9596 1131 // apply mask if any
463c1fa1 1132 GdkBitmap *mask = (GdkBitmap *) NULL;
280831d5
PC
1133 if (useMask && use_bitmap.GetMask())
1134 mask = use_bitmap.GetMask()->GetBitmap();
1135
1136 GdkGC* use_gc = is_mono ? m_textGC : m_penGC;
7d5af6fa 1137
d90c9596 1138 GdkBitmap *new_mask = (GdkBitmap*) NULL;
ab9d0a8c 1139
280831d5 1140 if (mask != NULL)
d90c9596
RR
1141 {
1142 if (!m_currentClippingRegion.IsNull())
82ea63e6 1143 {
d90c9596
RR
1144 GdkColor col;
1145 new_mask = gdk_pixmap_new( wxGetRootWindow()->window, ww, hh, 1 );
1146 GdkGC *gc = gdk_gc_new( new_mask );
1147 col.pixel = 0;
1148 gdk_gc_set_foreground( gc, &col );
1149 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, ww, hh );
1150 col.pixel = 0;
1151 gdk_gc_set_background( gc, &col );
1152 col.pixel = 1;
1153 gdk_gc_set_foreground( gc, &col );
1154 gdk_gc_set_clip_region( gc, m_currentClippingRegion.GetRegion() );
1155 gdk_gc_set_clip_origin( gc, -xx, -yy );
1156 gdk_gc_set_fill( gc, GDK_OPAQUE_STIPPLED );
1157 gdk_gc_set_stipple( gc, mask );
1158 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, ww, hh );
280831d5 1159 mask = new_mask;
3fe39b0c 1160 g_object_unref (gc);
d90c9596 1161 }
ab9d0a8c 1162
280831d5
PC
1163 gdk_gc_set_clip_mask(use_gc, mask);
1164 gdk_gc_set_clip_origin(use_gc, xx, yy);
d90c9596 1165 }
7d5af6fa 1166
d90c9596
RR
1167 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains. For
1168 // drawing a mono-bitmap (XBitmap) we use the current text GC
82ea63e6 1169 if (is_mono)
d90c9596 1170 {
4e115ed2
VZ
1171 GdkPixmap *bitmap2 = gdk_pixmap_new( wxGetRootWindow()->window, ww, hh, -1 );
1172 GdkGC *gc = gdk_gc_new( bitmap2 );
d90c9596
RR
1173 gdk_gc_set_foreground( gc, m_textForegroundColour.GetColor() );
1174 gdk_gc_set_background( gc, m_textBackgroundColour.GetColor() );
b85229d1 1175 gdk_wx_draw_bitmap( bitmap2, gc, use_bitmap.GetPixmap(), 0, 0, 0, 0, -1, -1 );
ab9d0a8c 1176
280831d5 1177 gdk_draw_drawable(m_window, use_gc, bitmap2, 0, 0, xx, yy, -1, -1);
ab9d0a8c 1178
3fe39b0c
MR
1179 g_object_unref (bitmap2);
1180 g_object_unref (gc);
d90c9596 1181 }
82ea63e6 1182 else
d90c9596 1183 {
feac7937 1184#if GTK_CHECK_VERSION(2,2,0)
cc35003a 1185 if (!gtk_check_version(2,2,0) && use_bitmap.HasPixbuf())
feac7937 1186 {
280831d5 1187 gdk_draw_pixbuf(m_window, use_gc,
feac7937
VS
1188 use_bitmap.GetPixbuf(),
1189 0, 0, xx, yy, -1, -1,
1190 GDK_RGB_DITHER_NORMAL, xx, yy);
1191 }
1192 else
1193#endif
1194 {
280831d5 1195 gdk_draw_drawable(m_window, use_gc,
15f0ad70
MR
1196 use_bitmap.GetPixmap(),
1197 0, 0, xx, yy, -1, -1);
feac7937 1198 }
d90c9596 1199 }
72174350 1200
d90c9596 1201 // remove mask again if any
280831d5 1202 if (mask != NULL)
463c1fa1 1203 {
280831d5
PC
1204 gdk_gc_set_clip_mask(use_gc, NULL);
1205 gdk_gc_set_clip_origin(use_gc, 0, 0);
1206 if (!m_currentClippingRegion.IsNull())
1207 gdk_gc_set_clip_region(use_gc, m_currentClippingRegion.GetRegion());
1208 if (new_mask != NULL)
1209 g_object_unref(new_mask);
463c1fa1 1210 }
463c1fa1
RR
1211}
1212
2970ae54 1213bool wxGTKWindowImplDC::DoBlit( wxCoord xdest, wxCoord ydest,
1e6feb95
VZ
1214 wxCoord width, wxCoord height,
1215 wxDC *source,
1216 wxCoord xsrc, wxCoord ysrc,
1217 int logical_func,
0cbff120
JS
1218 bool useMask,
1219 wxCoord xsrcMask, wxCoord ysrcMask )
c801d85f 1220{
ab9d0a8c 1221 wxCHECK_MSG( Ok(), false, wxT("invalid window dc") );
7d5af6fa 1222
ab9d0a8c 1223 wxCHECK_MSG( source, false, wxT("invalid source dc") );
7d5af6fa 1224
ab9d0a8c 1225 if (!m_window) return false;
7d5af6fa 1226
1e6feb95 1227 // transform the source DC coords to the device ones
772b3767
PC
1228 xsrc = source->LogicalToDeviceX(xsrc);
1229 ysrc = source->LogicalToDeviceY(ysrc);
1e6feb95 1230
e19a8aea 1231 wxBitmap selected;
f98bd6d6 1232 wxMemoryDC *memDC = wxDynamicCast(source, wxMemoryDC);
e19a8aea
VZ
1233 if ( memDC )
1234 {
1235 selected = memDC->GetSelectedBitmap();
1236 if ( !selected.IsOk() )
1237 return false;
1238 }
f4322df6 1239
ab9d0a8c
WS
1240 bool use_bitmap_method = false;
1241 bool is_mono = false;
7d5af6fa 1242
0cbff120
JS
1243 if (xsrcMask == -1 && ysrcMask == -1)
1244 {
d90c9596
RR
1245 xsrcMask = xsrc;
1246 ysrcMask = ysrc;
0cbff120
JS
1247 }
1248
f98bd6d6 1249 if (selected.Ok())
6e13c196 1250 {
f98bd6d6 1251 is_mono = (selected.GetDepth() == 1);
d90c9596
RR
1252
1253 // we use the "XCopyArea" way to copy a memory dc into
1254 // a different window if the memory dc BOTH
1255 // a) doesn't have any mask or its mask isn't used
1256 // b) it is clipped
1257 // c) is not 1-bit
7d5af6fa 1258
f98bd6d6 1259 if (useMask && (selected.GetMask()))
b0e0d661 1260 {
d90c9596
RR
1261 // we HAVE TO use the direct way for memory dcs
1262 // that have mask since the XCopyArea doesn't know
1263 // about masks
ab9d0a8c 1264 use_bitmap_method = true;
b0e0d661 1265 }
d90c9596 1266 else if (is_mono)
b0e0d661 1267 {
d90c9596
RR
1268 // we HAVE TO use the direct way for memory dcs
1269 // that are bitmaps because XCopyArea doesn't cope
1270 // with different bit depths
ab9d0a8c 1271 use_bitmap_method = true;
b0e0d661
VZ
1272 }
1273 else if ((xsrc == 0) && (ysrc == 0) &&
f98bd6d6
RD
1274 (width == selected.GetWidth()) &&
1275 (height == selected.GetHeight()))
b0e0d661 1276 {
d90c9596
RR
1277 // we SHOULD use the direct way if all of the bitmap
1278 // in the memory dc is copied in which case XCopyArea
1279 // wouldn't be able able to boost performace by reducing
1280 // the area to be scaled
ab9d0a8c 1281 use_bitmap_method = true;
b0e0d661 1282 }
6e13c196 1283 }
7d5af6fa 1284
265898fd
RR
1285 CalcBoundingBox( xdest, ydest );
1286 CalcBoundingBox( xdest + width, ydest + height );
7d5af6fa 1287
d90c9596 1288 // scale/translate size and position
3d2d8da1
RR
1289 wxCoord xx = XLOG2DEV(xdest);
1290 wxCoord yy = YLOG2DEV(ydest);
1291
1292 wxCoord ww = XLOG2DEVREL(width);
1293 wxCoord hh = YLOG2DEVREL(height);
1294
d90c9596 1295 // compare to current clipping region
e1208c31 1296 if (!m_currentClippingRegion.IsNull())
3d2d8da1
RR
1297 {
1298 wxRegion tmp( xx,yy,ww,hh );
1299 tmp.Intersect( m_currentClippingRegion );
1300 if (tmp.IsEmpty())
ab9d0a8c 1301 return true;
3d2d8da1
RR
1302 }
1303
4bc67cc5
RR
1304 int old_logical_func = m_logicalFunction;
1305 SetLogicalFunction( logical_func );
5f170f33 1306
6e13c196 1307 if (use_bitmap_method)
6f65e337 1308 {
d90c9596 1309 // scale/translate bitmap size
f98bd6d6
RD
1310 wxCoord bm_width = selected.GetWidth();
1311 wxCoord bm_height = selected.GetHeight();
7d5af6fa 1312
551b5391
RR
1313 // Get clip coords for the bitmap. If we don't
1314 // use wxBitmap::Rescale(), which can clip the
1315 // bitmap, these are the same as the original
1316 // coordinates
1317 wxCoord cx = xx;
1318 wxCoord cy = yy;
1319 wxCoord cw = ww;
1320 wxCoord ch = hh;
ab9d0a8c 1321
d90c9596
RR
1322 // interpret userscale of src too
1323 double xsc,ysc;
1324 memDC->GetUserScale(&xsc,&ysc);
1325 bm_width = (int) (bm_width / xsc);
1326 bm_height = (int) (bm_height / ysc);
1327
72cdf4c9
VZ
1328 wxCoord bm_ww = XLOG2DEVREL( bm_width );
1329 wxCoord bm_hh = YLOG2DEVREL( bm_height );
7d5af6fa 1330
551b5391 1331 // Scale bitmap if required
f98bd6d6
RD
1332 wxBitmap use_bitmap = selected;
1333 if ((selected.GetWidth()!= bm_ww) || ( selected.GetHeight()!= bm_hh))
6e13c196 1334 {
551b5391
RR
1335 // This indicates that the blitting code below will get
1336 // a clipped bitmap and therefore needs to move the origin
1337 // accordingly
1338 wxRegion tmp( xx,yy,ww,hh );
93da796d
RR
1339 if (!m_currentClippingRegion.IsNull())
1340 tmp.Intersect( m_currentClippingRegion );
551b5391 1341 tmp.GetBox(cx,cy,cw,ch);
ab9d0a8c 1342
551b5391 1343 // Scale and clipped bitmap
f98bd6d6 1344 use_bitmap = selected.Rescale(cx-xx,cy-yy,cw,ch,bm_ww,bm_hh);
6e13c196 1345 }
7d5af6fa 1346
d90c9596 1347 // apply mask if any
6e13c196 1348 GdkBitmap *mask = (GdkBitmap *) NULL;
280831d5
PC
1349 if (useMask && use_bitmap.GetMask())
1350 mask = use_bitmap.GetMask()->GetBitmap();
1351
1352 GdkGC* use_gc = is_mono ? m_textGC : m_penGC;
7d5af6fa 1353
d90c9596 1354 GdkBitmap *new_mask = (GdkBitmap*) NULL;
ab9d0a8c 1355
280831d5 1356 if (mask != NULL)
6e13c196 1357 {
e1208c31 1358 if (!m_currentClippingRegion.IsNull())
3d2d8da1
RR
1359 {
1360 GdkColor col;
c2fa61e8 1361 new_mask = gdk_pixmap_new( wxGetRootWindow()->window, bm_ww, bm_hh, 1 );
3d2d8da1
RR
1362 GdkGC *gc = gdk_gc_new( new_mask );
1363 col.pixel = 0;
1364 gdk_gc_set_foreground( gc, &col );
b7a49654 1365 gdk_gc_set_ts_origin( gc, -xsrcMask, -ysrcMask);
3d2d8da1
RR
1366 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, bm_ww, bm_hh );
1367 col.pixel = 0;
1368 gdk_gc_set_background( gc, &col );
1369 col.pixel = 1;
1370 gdk_gc_set_foreground( gc, &col );
1371 gdk_gc_set_clip_region( gc, m_currentClippingRegion.GetRegion() );
d90c9596
RR
1372 // was: gdk_gc_set_clip_origin( gc, -xx, -yy );
1373 gdk_gc_set_clip_origin( gc, -cx, -cy );
3d2d8da1
RR
1374 gdk_gc_set_fill( gc, GDK_OPAQUE_STIPPLED );
1375 gdk_gc_set_stipple( gc, mask );
1376 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, bm_ww, bm_hh );
280831d5 1377 mask = new_mask;
3fe39b0c 1378 g_object_unref (gc);
3d2d8da1 1379 }
d90c9596 1380
280831d5
PC
1381 gdk_gc_set_clip_mask(use_gc, mask);
1382 if (new_mask != NULL)
1383 gdk_gc_set_clip_origin(use_gc, cx, cy);
82ea63e6 1384 else
280831d5 1385 gdk_gc_set_clip_origin(use_gc, cx - xsrcMask, cy - ysrcMask);
6e13c196 1386 }
7d5af6fa 1387
d90c9596
RR
1388 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains. For
1389 // drawing a mono-bitmap (XBitmap) we use the current text GC
5f170f33 1390
82ea63e6 1391 if (is_mono)
d90c9596 1392 {
d90c9596
RR
1393 GdkPixmap *bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bm_ww, bm_hh, -1 );
1394 GdkGC *gc = gdk_gc_new( bitmap );
1395 gdk_gc_set_foreground( gc, m_textForegroundColour.GetColor() );
1396 gdk_gc_set_background( gc, m_textBackgroundColour.GetColor() );
b85229d1 1397 gdk_wx_draw_bitmap( bitmap, gc, use_bitmap.GetPixmap(), 0, 0, 0, 0, -1, -1 );
ab9d0a8c 1398
280831d5 1399 gdk_draw_drawable(m_window, use_gc, bitmap, xsrc, ysrc, cx, cy, cw, ch);
ab9d0a8c 1400
3fe39b0c
MR
1401 g_object_unref (bitmap);
1402 g_object_unref (gc);
d90c9596 1403 }
82ea63e6 1404 else
d90c9596 1405 {
15f0ad70 1406 // was: gdk_draw_drawable( m_window, m_penGC, use_bitmap.GetPixmap(), xsrc, ysrc, xx, yy, ww, hh );
280831d5 1407 gdk_draw_drawable(m_window, use_gc, use_bitmap.GetPixmap(), xsrc, ysrc, cx, cy, cw, ch);
d90c9596 1408 }
82ea63e6 1409
d90c9596 1410 // remove mask again if any
280831d5 1411 if (mask != NULL)
6e13c196 1412 {
280831d5
PC
1413 gdk_gc_set_clip_mask(use_gc, NULL);
1414 gdk_gc_set_clip_origin(use_gc, 0, 0);
1415 if (!m_currentClippingRegion.IsNull())
1416 gdk_gc_set_clip_region(use_gc, m_currentClippingRegion.GetRegion());
265898fd 1417 }
ab9d0a8c 1418
d90c9596 1419 if (new_mask)
3fe39b0c 1420 g_object_unref (new_mask);
6f65e337 1421 }
d90c9596 1422 else // use_bitmap_method
6e13c196 1423 {
f98bd6d6 1424 if (selected.Ok() && ((width != ww) || (height != hh)))
b0e0d661 1425 {
d90c9596
RR
1426 // get clip coords
1427 wxRegion tmp( xx,yy,ww,hh );
1428 tmp.Intersect( m_currentClippingRegion );
1429 wxCoord cx,cy,cw,ch;
1430 tmp.GetBox(cx,cy,cw,ch);
ab9d0a8c
WS
1431
1432 // rescale bitmap
f98bd6d6 1433 wxBitmap bitmap = selected.Rescale( cx-xx, cy-yy, cw, ch, ww, hh );
7d5af6fa 1434
d90c9596 1435 // draw scaled bitmap
15f0ad70
MR
1436 // was: gdk_draw_drawable( m_window, m_penGC, bitmap.GetPixmap(), 0, 0, xx, yy, -1, -1 );
1437 gdk_draw_drawable( m_window, m_penGC, bitmap.GetPixmap(), 0, 0, cx, cy, -1, -1 );
b0e0d661
VZ
1438 }
1439 else
1440 {
ab9d0a8c 1441 // No scaling and not a memory dc with a mask either
993f97ee 1442
f98bd6d6
RD
1443 GdkWindow* window = source->GetGDKWindow();
1444 if ( !window )
1445 return false;
f4322df6 1446
d90c9596 1447 // copy including child window contents
a56fcaaf 1448 gdk_gc_set_subwindow( m_penGC, GDK_INCLUDE_INFERIORS );
15f0ad70 1449 gdk_draw_drawable( m_window, m_penGC,
f98bd6d6 1450 window,
15f0ad70
MR
1451 xsrc, ysrc, xx, yy,
1452 width, height );
a56fcaaf 1453 gdk_gc_set_subwindow( m_penGC, GDK_CLIP_BY_CHILDREN );
b0e0d661 1454 }
6e13c196 1455 }
c801d85f 1456
4bc67cc5 1457 SetLogicalFunction( old_logical_func );
ab9d0a8c
WS
1458
1459 return true;
903f689b 1460}
c801d85f 1461
2970ae54 1462void wxGTKWindowImplDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y )
c801d85f 1463{
223d09f6 1464 wxCHECK_RET( Ok(), wxT("invalid window dc") );
18a2fa37 1465
6db90681 1466 if (!m_window) return;
ab9d0a8c 1467
2b5f62a0 1468 if (text.empty()) return;
7d5af6fa 1469
265898fd
RR
1470 x = XLOG2DEV(x);
1471 y = YLOG2DEV(y);
18a2fa37 1472
cfcc3932 1473 wxCHECK_RET( m_context, wxT("no Pango context") );
61850501 1474 wxCHECK_RET( m_layout, wxT("no Pango layout") );
cfcc3932 1475 wxCHECK_RET( m_fontdesc, wxT("no Pango font description") );
e4da1035 1476
24bbbfb1
RR
1477 gdk_pango_context_set_colormap( m_context, m_cmap );
1478
1dbeee57
VS
1479 bool underlined = m_font.Ok() && m_font.GetUnderlined();
1480
a3669332
VZ
1481 const wxCharBuffer data = wxGTK_CONV( text );
1482 if ( !data )
7706daf3 1483 return;
67a083dd
RR
1484 size_t datalen = strlen(data);
1485
5f4d1820
VZ
1486 // in Pango >= 1.16 the "underline of leading/trailing spaces" bug
1487 // has been fixed and thus the hack implemented below should never be used
1488 static bool pangoOk = !wx_pango_version_check(1, 16, 0);
1489
1490 bool needshack = underlined && !pangoOk;
67a083dd
RR
1491 char *hackstring = NULL;
1492
1493 if (needshack)
1494 {
1495 // a PangoLayout which has leading/trailing spaces with underlined font
1496 // is not correctly drawn by this pango version: Pango won't underline the spaces.
1497 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
1498 // this behaviour. To workaround this problem, we use a special hack here
1499 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
1500 // empty space characters and give them a dummy colour attribute.
1501 // This will force Pango to underline the leading/trailing spaces, too.
1502
1503 // need to realloc the string to prepend & append our special characters
1504 hackstring = (char*)malloc((datalen+7)*sizeof(char));
1505
1506 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1507 strcpy(hackstring, "\342\200\214");
1508
1509 // copy the user string
1510 memcpy(&hackstring[3], data, datalen);
1511
1512 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1513 strcpy(&hackstring[datalen+3], "\342\200\214");
1514
1515 // the special characters that we added require 6 additional bytes:
1516 datalen += 6;
1517
1518 pango_layout_set_text(m_layout, hackstring, datalen);
1519 }
1520 else
1521 {
1522 pango_layout_set_text(m_layout, data, datalen);
1523 }
ab9d0a8c 1524
1dbeee57
VS
1525 if (underlined)
1526 {
1527 PangoAttrList *attrs = pango_attr_list_new();
1528 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1529 a->start_index = 0;
1530 a->end_index = datalen;
1531 pango_attr_list_insert(attrs, a);
67a083dd
RR
1532
1533 if (needshack)
1534 {
1535 // dummy colour for the leading space
1536 a = pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1537 a->start_index = 0;
1538 a->end_index = 1;
1539 pango_attr_list_insert(attrs, a);
1540
1541 // dummy colour for the trailing space
1542 a = pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1543 a->start_index = datalen - 1;
1544 a->end_index = datalen;
1545 pango_attr_list_insert(attrs, a);
1546 }
1547
1dbeee57
VS
1548 pango_layout_set_attributes(m_layout, attrs);
1549 pango_attr_list_unref(attrs);
1550 }
e4da1035 1551
76b20467 1552 int w,h;
fa499247
RR
1553
1554 if (fabs(m_scaleY - 1.0) > 0.00001)
e4da1035
RR
1555 {
1556 // If there is a user or actually any scale applied to
1557 // the device context, scale the font.
ab9d0a8c 1558
e4da1035
RR
1559 // scale font description
1560 gint oldSize = pango_font_description_get_size( m_fontdesc );
1561 double size = oldSize;
1562 size = size * m_scaleY;
1563 pango_font_description_set_size( m_fontdesc, (gint)size );
ab9d0a8c 1564
e4da1035
RR
1565 // actually apply scaled font
1566 pango_layout_set_font_description( m_layout, m_fontdesc );
ab9d0a8c 1567
76b20467
VS
1568 pango_layout_get_pixel_size( m_layout, &w, &h );
1569 if ( m_backgroundMode == wxSOLID )
1570 {
1571 gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
1572 gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
1573 gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
1574 }
ab9d0a8c 1575
e4da1035 1576 // Draw layout.
847dfdb4
RR
1577 if (m_owner && m_owner->GetLayoutDirection() == wxLayout_RightToLeft)
1578 gdk_draw_layout( m_window, m_textGC, x-w, y, m_layout );
1579 else
1580 gdk_draw_layout( m_window, m_textGC, x, y, m_layout );
ab9d0a8c 1581
e4da1035
RR
1582 // reset unscaled size
1583 pango_font_description_set_size( m_fontdesc, oldSize );
ab9d0a8c 1584
61850501 1585 // actually apply unscaled font
e4da1035
RR
1586 pango_layout_set_font_description( m_layout, m_fontdesc );
1587 }
1588 else
1589 {
76b20467
VS
1590 pango_layout_get_pixel_size( m_layout, &w, &h );
1591 if ( m_backgroundMode == wxSOLID )
1592 {
1593 gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
1594 gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
1595 gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
1596 }
f4322df6 1597
76b20467 1598 // Draw layout.
847dfdb4
RR
1599 if (m_owner && m_owner->GetLayoutDirection() == wxLayout_RightToLeft)
1600 gdk_draw_layout( m_window, m_textGC, x-w, y, m_layout );
1601 else
1602 gdk_draw_layout( m_window, m_textGC, x, y, m_layout );
8943b403 1603 }
1dbeee57
VS
1604
1605 if (underlined)
1606 {
1607 // undo underline attributes setting:
1608 pango_layout_set_attributes(m_layout, NULL);
1609 }
ab9d0a8c 1610
2b5f62a0
VZ
1611 wxCoord width = w;
1612 wxCoord height = h;
ab9d0a8c 1613
8943b403
OK
1614 width = wxCoord(width / m_scaleX);
1615 height = wxCoord(height / m_scaleY);
1616 CalcBoundingBox (x + width, y + height);
265898fd 1617 CalcBoundingBox (x, y);
67a083dd
RR
1618
1619 if (hackstring)
1620 free(hackstring);
903f689b 1621}
c801d85f 1622
bf47e398
RD
1623
1624// TODO: There is an example of rotating text with GTK2 that would probably be
1625// a better approach here:
1626// http://www.daa.com.au/pipermail/pygtk/2003-April/005052.html
1627
2970ae54 1628void wxGTKWindowImplDC::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, double angle )
95724b1a 1629{
c0521644 1630#if wxUSE_IMAGE
1c2d839f
PC
1631 if (!m_window || text.empty())
1632 return;
1633
1634 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1635
c77a6796 1636 if ( wxIsNullDouble(angle) )
95724b1a
VZ
1637 {
1638 DrawText(text, x, y);
1639 return;
1640 }
1641
bf47e398
RD
1642 wxCoord w;
1643 wxCoord h;
1644
68567a96 1645 // TODO: implement later without GdkFont for GTK 2.0
bf47e398 1646 GetTextExtent(text, &w, &h, NULL,NULL, &m_font);
ab9d0a8c 1647
9a8c7620
VZ
1648 // draw the string normally
1649 wxBitmap src(w, h);
95724b1a
VZ
1650 wxMemoryDC dc;
1651 dc.SelectObject(src);
1652 dc.SetFont(GetFont());
f1c72f0c 1653 dc.SetBackground(*wxBLACK_BRUSH);
95724b1a
VZ
1654 dc.SetBrush(*wxBLACK_BRUSH);
1655 dc.Clear();
f1c72f0c 1656 dc.SetTextForeground( *wxWHITE );
95724b1a 1657 dc.DrawText(text, 0, 0);
9a8c7620 1658 dc.SelectObject(wxNullBitmap);
95724b1a
VZ
1659
1660 // Calculate the size of the rotated bounding box.
9a8c7620
VZ
1661 double rad = DegToRad(angle);
1662 double dx = cos(rad),
1663 dy = sin(rad);
1664
1665 // the rectngle vertices are counted clockwise with the first one being at
1666 // (0, 0) (or, rather, at (x, y))
1667 double x2 = w*dx,
1668 y2 = -w*dy; // y axis points to the bottom, hence minus
1669 double x4 = h*dy,
1670 y4 = h*dx;
1671 double x3 = x4 + x2,
1672 y3 = y4 + y2;
ab9d0a8c 1673
72174350 1674 // calc max and min
9a8c7620
VZ
1675 wxCoord maxX = (wxCoord)(dmax(x2, dmax(x3, x4)) + 0.5),
1676 maxY = (wxCoord)(dmax(y2, dmax(y3, y4)) + 0.5),
1677 minX = (wxCoord)(dmin(x2, dmin(x3, x4)) - 0.5),
1678 minY = (wxCoord)(dmin(y2, dmin(y3, y4)) - 0.5);
1679
72174350 1680
f1c72f0c 1681 wxImage image = src.ConvertToImage();
9a8c7620 1682
f1c72f0c
RR
1683 image.ConvertColourToAlpha( m_textForegroundColour.Red(),
1684 m_textForegroundColour.Green(),
1685 m_textForegroundColour.Blue() );
1686 image = image.Rotate( rad, wxPoint(0,0) );
ab9d0a8c 1687
1d65e535
RR
1688 int i_angle = (int) angle;
1689 i_angle = i_angle % 360;
c2cb80c8
KH
1690 if (i_angle < 0)
1691 i_angle += 360;
1d65e535
RR
1692 int xoffset = 0;
1693 if ((i_angle >= 90.0) && (i_angle < 270.0))
1694 xoffset = image.GetWidth();
1695 int yoffset = 0;
1696 if ((i_angle >= 0.0) && (i_angle < 180.0))
1697 yoffset = image.GetHeight();
ab9d0a8c 1698
1d65e535
RR
1699 if ((i_angle >= 0) && (i_angle < 90))
1700 yoffset -= (int)( cos(rad)*h );
1701 if ((i_angle >= 90) && (i_angle < 180))
ab9d0a8c 1702 xoffset -= (int)( sin(rad)*h );
1d65e535
RR
1703 if ((i_angle >= 180) && (i_angle < 270))
1704 yoffset -= (int)( cos(rad)*h );
1705 if ((i_angle >= 270) && (i_angle < 360))
1706 xoffset -= (int)( sin(rad)*h );
ab9d0a8c 1707
1d65e535
RR
1708 int i_x = x - xoffset;
1709 int i_y = y - yoffset;
ab9d0a8c 1710
f1c72f0c 1711 src = image;
1d65e535 1712 DoDrawBitmap( src, i_x, i_y, true );
9a8c7620 1713
95724b1a 1714
9a8c7620
VZ
1715 // it would be better to draw with non underlined font and draw the line
1716 // manually here (it would be more straight...)
1717#if 0
1718 if ( m_font.GetUnderlined() )
1719 {
1720 gdk_draw_line( m_window, m_textGC,
1721 XLOG2DEV(x + x4), YLOG2DEV(y + y4 + font->descent),
1722 XLOG2DEV(x + x3), YLOG2DEV(y + y3 + font->descent));
1723 }
1724#endif // 0
1725
9a8c7620
VZ
1726 // update the bounding box
1727 CalcBoundingBox(x + minX, y + minY);
1728 CalcBoundingBox(x + maxX, y + maxY);
c0521644 1729#endif // wxUSE_IMAGE
95724b1a
VZ
1730}
1731
2970ae54 1732void wxGTKWindowImplDC::DoGetTextExtent(const wxString &string,
72cdf4c9
VZ
1733 wxCoord *width, wxCoord *height,
1734 wxCoord *descent, wxCoord *externalLeading,
c94f845b 1735 const wxFont *theFont) const
c801d85f 1736{
b1f0abe4
VZ
1737 if ( width )
1738 *width = 0;
1739 if ( height )
1740 *height = 0;
1741 if ( descent )
1742 *descent = 0;
1743 if ( externalLeading )
1744 *externalLeading = 0;
1745
ab9d0a8c 1746 if (string.empty())
48d011c8 1747 return;
ab9d0a8c 1748
bf5752a4
VZ
1749 // ensure that theFont is always non-NULL
1750 if ( !theFont || !theFont->Ok() )
1751 theFont = wx_const_cast(wxFont *, &m_font);
1752
1753 // and use it if it's valid
1754 if ( theFont->Ok() )
1755 {
1756 pango_layout_set_font_description
1757 (
1758 m_layout,
1759 theFont->GetNativeFontInfo()->description
1760 );
1761 }
ab9d0a8c 1762
2b5f62a0 1763 // Set layout's text
bf5752a4 1764 const wxCharBuffer dataUTF8 = wxGTK_CONV_FONT(string, *theFont);
a1e101d0
VZ
1765 if ( !dataUTF8 )
1766 {
1767 // hardly ideal, but what else can we do if conversion failed?
1768 return;
1769 }
1770
1771 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
ab9d0a8c 1772
48d011c8
RR
1773 if (descent)
1774 {
ace35c62
RR
1775 int h;
1776 pango_layout_get_pixel_size( m_layout, width, &h );
f69e2009
VS
1777 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
1778 int baseline = pango_layout_iter_get_baseline(iter);
1779 pango_layout_iter_free(iter);
1780 *descent = h - PANGO_PIXELS(baseline);
ace35c62
RR
1781
1782 if (height)
1783 *height = (wxCoord) h;
1784 }
1785 else
1786 {
1787 pango_layout_get_pixel_size( m_layout, width, height );
48d011c8 1788 }
ab9d0a8c 1789
cfcc3932 1790 // Reset old font description
6fbe4b24 1791 if (theFont->IsOk())
cfcc3932 1792 pango_layout_set_font_description( m_layout, m_fontdesc );
903f689b 1793}
c801d85f 1794
1f91072f 1795
2970ae54 1796bool wxGTKWindowImplDC::DoGetPartialTextExtents(const wxString& text,
1f91072f
VZ
1797 wxArrayInt& widths) const
1798{
1799 const size_t len = text.length();
1800 widths.Empty();
1801 widths.Add(0, len);
1802
1803 if (text.empty())
1804 return true;
1805
1806 // Set layout's text
1807 const wxCharBuffer dataUTF8 = wxGTK_CONV_FONT(text, m_font);
1808 if ( !dataUTF8 )
1809 {
1810 // hardly ideal, but what else can we do if conversion failed?
1811 wxLogLastError(wxT("DoGetPartialTextExtents"));
1812 return false;
1813 }
1814
1815 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
f4322df6 1816
1f91072f
VZ
1817 // Calculate the position of each character based on the widths of
1818 // the previous characters
1819
1820 // Code borrowed from Scintilla's PlatGTK
1821 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
1822 PangoRectangle pos;
1823 pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
1824 size_t i = 0;
1825 while (pango_layout_iter_next_cluster(iter))
1826 {
1827 pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
1828 int position = PANGO_PIXELS(pos.x);
1f91072f
VZ
1829 widths[i++] = position;
1830 }
1831 while (i < len)
1832 widths[i++] = PANGO_PIXELS(pos.x + pos.width);
1833 pango_layout_iter_free(iter);
1834
1835 return true;
1836}
1837
1838
2970ae54 1839wxCoord wxGTKWindowImplDC::GetCharWidth() const
c801d85f 1840{
cfcc3932 1841 pango_layout_set_text( m_layout, "H", 1 );
ace35c62
RR
1842 int w;
1843 pango_layout_get_pixel_size( m_layout, &w, NULL );
2b5f62a0 1844 return w;
903f689b 1845}
c801d85f 1846
2970ae54 1847wxCoord wxGTKWindowImplDC::GetCharHeight() const
c801d85f 1848{
a8c5e1a9 1849 PangoFontMetrics *metrics = pango_context_get_metrics (m_context, m_fontdesc, pango_context_get_language(m_context));
2e61f681
VZ
1850 wxCHECK_MSG( metrics, -1, _T("failed to get pango font metrics") );
1851
1852 wxCoord h = PANGO_PIXELS (pango_font_metrics_get_descent (metrics) +
1853 pango_font_metrics_get_ascent (metrics));
1854 pango_font_metrics_unref (metrics);
1855 return h;
903f689b 1856}
c801d85f 1857
2970ae54 1858void wxGTKWindowImplDC::Clear()
c801d85f 1859{
223d09f6 1860 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 1861
6db90681 1862 if (!m_window) return;
7d5af6fa 1863
f60e7fdd
VZ
1864 // VZ: the code below results in infinite recursion and crashes when
1865 // dc.Clear() is done from OnPaint() so I disable it for now.
1866 // I don't know what the correct fix is but Clear() surely should not
1867 // reenter OnPaint()!
1868#if 0
f234c60c
RR
1869 /* - we either are a memory dc or have a window as the
1870 owner. anything else shouldn't happen.
1871 - we don't use gdk_window_clear() as we don't set
1872 the window's background colour anymore. it is too
1873 much pain to keep the DC's and the window's back-
1874 ground colour in synch. */
7d5af6fa 1875
f234c60c 1876 if (m_owner)
265898fd 1877 {
f90566f5 1878 m_owner->Clear();
b0e0d661 1879 return;
265898fd 1880 }
f234c60c
RR
1881
1882 if (m_isMemDC)
265898fd
RR
1883 {
1884 int width,height;
1885 GetSize( &width, &height );
1886 gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
b0e0d661 1887 return;
265898fd 1888 }
f60e7fdd
VZ
1889#else // 1
1890 int width,height;
1891 GetSize( &width, &height );
1892 gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
1893#endif // 0/1
903f689b 1894}
c801d85f 1895
2970ae54 1896void wxGTKWindowImplDC::SetFont( const wxFont &font )
c801d85f 1897{
265898fd 1898 m_font = font;
ab9d0a8c 1899
77416abd 1900 if (m_font.Ok())
2b5f62a0 1901 {
cfcc3932
RR
1902 if (m_fontdesc)
1903 pango_font_description_free( m_fontdesc );
ab9d0a8c 1904
cfcc3932 1905 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
ab9d0a8c
WS
1906
1907
77416abd
JS
1908 if (m_owner)
1909 {
cfcc3932 1910 PangoContext *oldContext = m_context;
ab9d0a8c 1911
f26623c8 1912 m_context = m_owner->GtkGetPangoDefaultContext();
ab9d0a8c 1913
cfcc3932
RR
1914 // If we switch back/forth between different contexts
1915 // we also have to create a new layout. I think so,
ab9d0a8c 1916 // at least, and it doesn't hurt to do it.
cfcc3932
RR
1917 if (oldContext != m_context)
1918 {
1919 if (m_layout)
3fe39b0c 1920 g_object_unref (m_layout);
ab9d0a8c 1921
cfcc3932
RR
1922 m_layout = pango_layout_new( m_context );
1923 }
77416abd 1924 }
ab9d0a8c 1925
cfcc3932 1926 pango_layout_set_font_description( m_layout, m_fontdesc );
2b5f62a0 1927 }
903f689b 1928}
c801d85f 1929
2970ae54 1930void wxGTKWindowImplDC::SetPen( const wxPen &pen )
c801d85f 1931{
223d09f6 1932 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 1933
265898fd 1934 if (m_pen == pen) return;
7d5af6fa 1935
265898fd 1936 m_pen = pen;
7d5af6fa 1937
265898fd 1938 if (!m_pen.Ok()) return;
7d5af6fa 1939
6db90681 1940 if (!m_window) return;
7d5af6fa 1941
265898fd 1942 gint width = m_pen.GetWidth();
265898fd
RR
1943 if (width <= 0)
1944 {
112c5086 1945 // CMB: if width is non-zero scale it with the dc
265898fd
RR
1946 width = 1;
1947 }
1948 else
1949 {
1950 // X doesn't allow different width in x and y and so we take
1951 // the average
503f414e
VZ
1952 double w = 0.5 +
1953 ( fabs((double) XLOG2DEVREL(width)) +
1954 fabs((double) YLOG2DEVREL(width)) ) / 2.0;
265898fd 1955 width = (int)w;
375fc5a9
VZ
1956 if ( !width )
1957 {
1958 // width can't be 0 or an internal GTK error occurs inside
1959 // gdk_gc_set_dashes() below
1960 width = 1;
1961 }
265898fd 1962 }
7d5af6fa 1963
2eca425d
RL
1964 static const wxGTKDash dotted[] = {1, 1};
1965 static const wxGTKDash short_dashed[] = {2, 2};
1966 static const wxGTKDash wxCoord_dashed[] = {2, 4};
1967 static const wxGTKDash dotted_dashed[] = {3, 3, 1, 3};
7d5af6fa 1968
112c5086
RR
1969 // We express dash pattern in pen width unit, so we are
1970 // independent of zoom factor and so on...
1971 int req_nb_dash;
2eca425d 1972 const wxGTKDash *req_dash;
7d5af6fa 1973
265898fd
RR
1974 GdkLineStyle lineStyle = GDK_LINE_SOLID;
1975 switch (m_pen.GetStyle())
1976 {
112c5086 1977 case wxUSER_DASH:
7d5af6fa
VZ
1978 {
1979 lineStyle = GDK_LINE_ON_OFF_DASH;
112c5086 1980 req_nb_dash = m_pen.GetDashCount();
2eca425d 1981 req_dash = (wxGTKDash*)m_pen.GetDash();
112c5086 1982 break;
7d5af6fa
VZ
1983 }
1984 case wxDOT:
1985 {
1986 lineStyle = GDK_LINE_ON_OFF_DASH;
112c5086
RR
1987 req_nb_dash = 2;
1988 req_dash = dotted;
7d5af6fa
VZ
1989 break;
1990 }
1991 case wxLONG_DASH:
1992 {
1993 lineStyle = GDK_LINE_ON_OFF_DASH;
112c5086 1994 req_nb_dash = 2;
72cdf4c9 1995 req_dash = wxCoord_dashed;
7d5af6fa
VZ
1996 break;
1997 }
1998 case wxSHORT_DASH:
1999 {
2000 lineStyle = GDK_LINE_ON_OFF_DASH;
112c5086
RR
2001 req_nb_dash = 2;
2002 req_dash = short_dashed;
7d5af6fa
VZ
2003 break;
2004 }
2005 case wxDOT_DASH:
2006 {
2007// lineStyle = GDK_LINE_DOUBLE_DASH;
2008 lineStyle = GDK_LINE_ON_OFF_DASH;
112c5086
RR
2009 req_nb_dash = 4;
2010 req_dash = dotted_dashed;
7d5af6fa
VZ
2011 break;
2012 }
2013
2014 case wxTRANSPARENT:
72174350 2015 case wxSTIPPLE_MASK_OPAQUE:
7d5af6fa
VZ
2016 case wxSTIPPLE:
2017 case wxSOLID:
2018 default:
2019 {
2020 lineStyle = GDK_LINE_SOLID;
2eca425d 2021 req_dash = (wxGTKDash*)NULL;
112c5086 2022 req_nb_dash = 0;
7d5af6fa
VZ
2023 break;
2024 }
265898fd 2025 }
7d5af6fa 2026
112c5086
RR
2027 if (req_dash && req_nb_dash)
2028 {
2eca425d 2029 wxGTKDash *real_req_dash = new wxGTKDash[req_nb_dash];
112c5086
RR
2030 if (real_req_dash)
2031 {
2032 for (int i = 0; i < req_nb_dash; i++)
2033 real_req_dash[i] = req_dash[i] * width;
2eca425d 2034 gdk_gc_set_dashes( m_penGC, 0, real_req_dash, req_nb_dash );
112c5086
RR
2035 delete[] real_req_dash;
2036 }
2037 else
2038 {
2039 // No Memory. We use non-scaled dash pattern...
2eca425d 2040 gdk_gc_set_dashes( m_penGC, 0, (wxGTKDash*)req_dash, req_nb_dash );
112c5086
RR
2041 }
2042 }
7d5af6fa 2043
265898fd
RR
2044 GdkCapStyle capStyle = GDK_CAP_ROUND;
2045 switch (m_pen.GetCap())
2046 {
265898fd
RR
2047 case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
2048 case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }
d4aa3a4b
RR
2049 case wxCAP_ROUND:
2050 default:
72174350 2051 {
d4aa3a4b
RR
2052 if (width <= 1)
2053 {
2054 width = 0;
2055 capStyle = GDK_CAP_NOT_LAST;
2056 }
2057 else
2058 {
2059 capStyle = GDK_CAP_ROUND;
2060 }
72174350 2061 break;
d4aa3a4b 2062 }
265898fd 2063 }
7d5af6fa 2064
265898fd
RR
2065 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
2066 switch (m_pen.GetJoin())
2067 {
2068 case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
265898fd 2069 case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
d4aa3a4b
RR
2070 case wxJOIN_ROUND:
2071 default: { joinStyle = GDK_JOIN_ROUND; break; }
265898fd 2072 }
7d5af6fa 2073
265898fd 2074 gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
7d5af6fa 2075
265898fd
RR
2076 m_pen.GetColour().CalcPixel( m_cmap );
2077 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
903f689b 2078}
c801d85f 2079
2970ae54 2080void wxGTKWindowImplDC::SetBrush( const wxBrush &brush )
c801d85f 2081{
223d09f6 2082 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2083
265898fd 2084 if (m_brush == brush) return;
7d5af6fa 2085
265898fd 2086 m_brush = brush;
7d5af6fa 2087
265898fd 2088 if (!m_brush.Ok()) return;
7d5af6fa 2089
6db90681 2090 if (!m_window) return;
7d5af6fa 2091
265898fd
RR
2092 m_brush.GetColour().CalcPixel( m_cmap );
2093 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
7d5af6fa 2094
956dbab1 2095 gdk_gc_set_fill( m_brushGC, GDK_SOLID );
7d5af6fa 2096
4fcd73bd 2097 if ((m_brush.GetStyle() == wxSTIPPLE) && (m_brush.GetStipple()->Ok()))
265898fd 2098 {
b85229d1 2099 if (m_brush.GetStipple()->GetDepth() != 1)
7d5af6fa 2100 {
956dbab1 2101 gdk_gc_set_fill( m_brushGC, GDK_TILED );
cd25b18c 2102 gdk_gc_set_tile( m_brushGC, m_brush.GetStipple()->GetPixmap() );
7d5af6fa 2103 }
b0e0d661 2104 else
7d5af6fa 2105 {
956dbab1 2106 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
b85229d1 2107 gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() );
7d5af6fa 2108 }
265898fd 2109 }
7d5af6fa 2110
de2d2cdc
VZ
2111 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
2112 {
e1208c31
RR
2113 gdk_gc_set_fill( m_textGC, GDK_OPAQUE_STIPPLED);
2114 gdk_gc_set_stipple( m_textGC, m_brush.GetStipple()->GetMask()->GetBitmap() );
de2d2cdc
VZ
2115 }
2116
ab9d0a8c 2117 if (m_brush.IsHatch())
265898fd 2118 {
956dbab1 2119 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
265898fd
RR
2120 int num = m_brush.GetStyle() - wxBDIAGONAL_HATCH;
2121 gdk_gc_set_stipple( m_brushGC, hatches[num] );
2122 }
903f689b 2123}
c801d85f 2124
2970ae54 2125void wxGTKWindowImplDC::SetBackground( const wxBrush &brush )
46dc76ba 2126{
bbbbe360
RR
2127 /* CMB 21/7/98: Added SetBackground. Sets background brush
2128 * for Clear() and bg colour for shapes filled with cross-hatch brush */
7d5af6fa 2129
223d09f6 2130 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2131
265898fd 2132 if (m_backgroundBrush == brush) return;
7d5af6fa 2133
265898fd 2134 m_backgroundBrush = brush;
7d5af6fa 2135
265898fd 2136 if (!m_backgroundBrush.Ok()) return;
7d5af6fa 2137
6db90681 2138 if (!m_window) return;
7d5af6fa 2139
265898fd
RR
2140 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
2141 gdk_gc_set_background( m_brushGC, m_backgroundBrush.GetColour().GetColor() );
a802c3a1 2142 gdk_gc_set_background( m_penGC, m_backgroundBrush.GetColour().GetColor() );
031b2a7b 2143 gdk_gc_set_background( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
265898fd 2144 gdk_gc_set_foreground( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
3417c2cd
RR
2145
2146 gdk_gc_set_fill( m_bgGC, GDK_SOLID );
7d5af6fa 2147
cd25b18c 2148 if ((m_backgroundBrush.GetStyle() == wxSTIPPLE) && (m_backgroundBrush.GetStipple()->Ok()))
265898fd 2149 {
b85229d1 2150 if (m_backgroundBrush.GetStipple()->GetDepth() != 1)
7d5af6fa 2151 {
3417c2cd 2152 gdk_gc_set_fill( m_bgGC, GDK_TILED );
fd128b0c 2153 gdk_gc_set_tile( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
7d5af6fa 2154 }
cd25b18c 2155 else
7d5af6fa 2156 {
3417c2cd 2157 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
b85229d1 2158 gdk_gc_set_stipple( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
7d5af6fa 2159 }
265898fd 2160 }
7d5af6fa 2161
ab9d0a8c 2162 if (m_backgroundBrush.IsHatch())
265898fd 2163 {
3417c2cd 2164 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
265898fd
RR
2165 int num = m_backgroundBrush.GetStyle() - wxBDIAGONAL_HATCH;
2166 gdk_gc_set_stipple( m_bgGC, hatches[num] );
bbbbe360 2167 }
903f689b 2168}
46dc76ba 2169
2970ae54 2170void wxGTKWindowImplDC::SetLogicalFunction( int function )
c801d85f 2171{
223d09f6 2172 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2173
72174350
VZ
2174 if (m_logicalFunction == function)
2175 return;
2176
2177 // VZ: shouldn't this be a CHECK?
2178 if (!m_window)
2179 return;
01eaf507 2180
2b5f62a0 2181 GdkFunction mode;
265898fd
RR
2182 switch (function)
2183 {
3c679789
RR
2184 case wxXOR: mode = GDK_XOR; break;
2185 case wxINVERT: mode = GDK_INVERT; break;
3c679789
RR
2186 case wxOR_REVERSE: mode = GDK_OR_REVERSE; break;
2187 case wxAND_REVERSE: mode = GDK_AND_REVERSE; break;
2188 case wxCLEAR: mode = GDK_CLEAR; break;
2189 case wxSET: mode = GDK_SET; break;
2190 case wxOR_INVERT: mode = GDK_OR_INVERT; break;
2191 case wxAND: mode = GDK_AND; break;
2192 case wxOR: mode = GDK_OR; break;
2193 case wxEQUIV: mode = GDK_EQUIV; break;
2194 case wxNAND: mode = GDK_NAND; break;
2195 case wxAND_INVERT: mode = GDK_AND_INVERT; break;
7d5af6fa
VZ
2196 case wxCOPY: mode = GDK_COPY; break;
2197 case wxNO_OP: mode = GDK_NOOP; break;
2198 case wxSRC_INVERT: mode = GDK_COPY_INVERT; break;
72174350 2199
2d52841d
RR
2200 // unsupported by GTK
2201 case wxNOR: mode = GDK_COPY; break;
01eaf507 2202 default:
223d09f6 2203 wxFAIL_MSG( wxT("unsupported logical function") );
2b5f62a0 2204 mode = GDK_COPY;
265898fd 2205 }
7d5af6fa 2206
265898fd 2207 m_logicalFunction = function;
7d5af6fa 2208
265898fd
RR
2209 gdk_gc_set_function( m_penGC, mode );
2210 gdk_gc_set_function( m_brushGC, mode );
72174350
VZ
2211
2212 // to stay compatible with wxMSW, we don't apply ROPs to the text
3d2d8da1
RR
2213 // operations (i.e. DrawText/DrawRotatedText).
2214 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2215 gdk_gc_set_function( m_textGC, mode );
903f689b 2216}
c801d85f 2217
2970ae54 2218void wxGTKWindowImplDC::SetTextForeground( const wxColour &col )
c801d85f 2219{
223d09f6 2220 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2221
71ec83d2
VZ
2222 // don't set m_textForegroundColour to an invalid colour as we'd crash
2223 // later then (we use m_textForegroundColour.GetColor() without checking
2224 // in a few places)
2225 if ( !col.Ok() || (m_textForegroundColour == col) )
2226 return;
7d5af6fa 2227
265898fd 2228 m_textForegroundColour = col;
7d5af6fa 2229
71ec83d2
VZ
2230 if ( m_window )
2231 {
2232 m_textForegroundColour.CalcPixel( m_cmap );
2233 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
2234 }
903f689b 2235}
c801d85f 2236
2970ae54 2237void wxGTKWindowImplDC::SetTextBackground( const wxColour &col )
c801d85f 2238{
223d09f6 2239 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2240
71ec83d2
VZ
2241 // same as above
2242 if ( !col.Ok() || (m_textBackgroundColour == col) )
2243 return;
7d5af6fa 2244
265898fd 2245 m_textBackgroundColour = col;
7d5af6fa 2246
71ec83d2
VZ
2247 if ( m_window )
2248 {
2249 m_textBackgroundColour.CalcPixel( m_cmap );
2250 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
2251 }
903f689b 2252}
c801d85f 2253
2970ae54 2254void wxGTKWindowImplDC::SetBackgroundMode( int mode )
c801d85f 2255{
223d09f6 2256 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2257
265898fd 2258 m_backgroundMode = mode;
46dc76ba 2259
6db90681 2260 if (!m_window) return;
7d5af6fa 2261
265898fd
RR
2262 // CMB 21/7/98: fill style of cross-hatch brushes is affected by
2263 // transparent/solid background mode
7d5af6fa 2264
265898fd
RR
2265 if (m_brush.GetStyle() != wxSOLID && m_brush.GetStyle() != wxTRANSPARENT)
2266 {
2267 gdk_gc_set_fill( m_brushGC,
2268 (m_backgroundMode == wxTRANSPARENT) ? GDK_STIPPLED : GDK_OPAQUE_STIPPLED);
2269 }
903f689b 2270}
c801d85f 2271
2970ae54 2272void wxGTKWindowImplDC::SetPalette( const wxPalette& WXUNUSED(palette) )
c801d85f 2273{
2970ae54 2274 wxFAIL_MSG( wxT("wxGTKWindowImplDC::SetPalette not implemented") );
903f689b 2275}
c801d85f 2276
2970ae54 2277void wxGTKWindowImplDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 2278{
223d09f6 2279 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2280
6db90681 2281 if (!m_window) return;
7d5af6fa 2282
3d2d8da1 2283 wxRect rect;
265898fd
RR
2284 rect.x = XLOG2DEV(x);
2285 rect.y = YLOG2DEV(y);
2286 rect.width = XLOG2DEVREL(width);
2287 rect.height = YLOG2DEVREL(height);
5f170f33 2288
49e74855
RR
2289 if (m_owner && m_owner->m_wxwindow && (m_owner->GetLayoutDirection() == wxLayout_RightToLeft))
2290 {
2291 rect.x -= rect.width;
2292 }
2293
e1208c31 2294 if (!m_currentClippingRegion.IsNull())
993f97ee
RR
2295 m_currentClippingRegion.Intersect( rect );
2296 else
2297 m_currentClippingRegion.Union( rect );
5f170f33
VZ
2298
2299#if USE_PAINT_REGION
e1208c31 2300 if (!m_paintClippingRegion.IsNull())
3d2d8da1 2301 m_currentClippingRegion.Intersect( m_paintClippingRegion );
809934d2 2302#endif
3d2d8da1 2303
ee8dbe63
RL
2304 wxCoord xx, yy, ww, hh;
2305 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
2306 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
2307
3d2d8da1
RR
2308 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2309 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2310 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2311 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
903f689b 2312}
c801d85f 2313
2970ae54 2314void wxGTKWindowImplDC::DoSetClippingRegionAsRegion( const wxRegion &region )
463c1fa1 2315{
223d09f6 2316 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2317
463c1fa1
RR
2318 if (region.Empty())
2319 {
2320 DestroyClippingRegion();
2321 return;
2322 }
7d5af6fa 2323
6db90681 2324 if (!m_window) return;
5f170f33 2325
e1208c31 2326 if (!m_currentClippingRegion.IsNull())
993f97ee
RR
2327 m_currentClippingRegion.Intersect( region );
2328 else
2329 m_currentClippingRegion.Union( region );
5f170f33
VZ
2330
2331#if USE_PAINT_REGION
e1208c31 2332 if (!m_paintClippingRegion.IsNull())
3d2d8da1 2333 m_currentClippingRegion.Intersect( m_paintClippingRegion );
809934d2 2334#endif
3d2d8da1 2335
ee8dbe63
RL
2336 wxCoord xx, yy, ww, hh;
2337 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
2338 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
2339
3d2d8da1
RR
2340 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2341 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2342 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2343 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
463c1fa1
RR
2344}
2345
2970ae54 2346void wxGTKWindowImplDC::DestroyClippingRegion()
c801d85f 2347{
223d09f6 2348 wxCHECK_RET( Ok(), wxT("invalid window dc") );
7d5af6fa 2349
265898fd 2350 wxDC::DestroyClippingRegion();
7d5af6fa 2351
3d2d8da1 2352 m_currentClippingRegion.Clear();
5f170f33
VZ
2353
2354#if USE_PAINT_REGION
3d2d8da1
RR
2355 if (!m_paintClippingRegion.IsEmpty())
2356 m_currentClippingRegion.Union( m_paintClippingRegion );
993f97ee 2357#endif
3d2d8da1 2358
6db90681 2359 if (!m_window) return;
7d5af6fa 2360
3d2d8da1
RR
2361 if (m_currentClippingRegion.IsEmpty())
2362 {
2363 gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL );
2364 gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL );
2365 gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL );
2366 gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL );
2367 }
2368 else
2369 {
2370 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2371 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2372 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2373 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
2374 }
903f689b 2375}
c801d85f 2376
2970ae54 2377void wxGTKWindowImplDC::Destroy()
dbf858b5 2378{
3d2d8da1 2379 if (m_penGC) wxFreePoolGC( m_penGC );
265898fd 2380 m_penGC = (GdkGC*) NULL;
3d2d8da1 2381 if (m_brushGC) wxFreePoolGC( m_brushGC );
265898fd 2382 m_brushGC = (GdkGC*) NULL;
3d2d8da1 2383 if (m_textGC) wxFreePoolGC( m_textGC );
265898fd 2384 m_textGC = (GdkGC*) NULL;
3d2d8da1 2385 if (m_bgGC) wxFreePoolGC( m_bgGC );
265898fd 2386 m_bgGC = (GdkGC*) NULL;
dbf858b5
RR
2387}
2388
2970ae54 2389void wxGTKWindowImplDC::SetDeviceOrigin( wxCoord x, wxCoord y )
847dfdb4
RR
2390{
2391 m_deviceOriginX = x;
2392 m_deviceOriginY = y;
f4322df6 2393
847dfdb4
RR
2394 ComputeScaleAndOrigin();
2395}
2396
2970ae54 2397void wxGTKWindowImplDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
847dfdb4
RR
2398{
2399 m_signX = (xLeftRight ? 1 : -1);
2400 m_signY = (yBottomUp ? -1 : 1);
f4322df6 2401
847dfdb4 2402 if (m_owner && m_owner->m_wxwindow && (m_owner->GetLayoutDirection() == wxLayout_RightToLeft))
f4322df6
VZ
2403 m_signX = -m_signX;
2404
847dfdb4
RR
2405 ComputeScaleAndOrigin();
2406}
2407
2970ae54 2408void wxGTKWindowImplDC::ComputeScaleAndOrigin()
238d735d 2409{
c77a6796 2410 const wxRealPoint origScale(m_scaleX, m_scaleY);
238d735d
RR
2411
2412 wxDC::ComputeScaleAndOrigin();
2413
c77a6796
VZ
2414 // if scale has changed call SetPen to recalulate the line width
2415 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.Ok() )
238d735d 2416 {
c77a6796
VZ
2417 // this is a bit artificial, but we need to force wxDC to think the pen
2418 // has changed
0a164d4c
WS
2419 wxPen pen = m_pen;
2420 m_pen = wxNullPen;
2421 SetPen( pen );
2422 }
238d735d
RR
2423}
2424
b0e0d661 2425// Resolution in pixels per logical inch
2970ae54 2426wxSize wxGTKWindowImplDC::GetPPI() const
b0e0d661 2427{
7a5e6267 2428 return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5));
b0e0d661
VZ
2429}
2430
2970ae54 2431int wxGTKWindowImplDC::GetDepth() const
c801d85f 2432{
ace35c62 2433 return gdk_drawable_get_depth(m_window);
903f689b 2434}
c801d85f 2435
ec758a20
RR
2436
2437//-----------------------------------------------------------------------------
2438// wxPaintDC
2439//-----------------------------------------------------------------------------
2440
2970ae54
RR
2441#if wxUSE_NEW_DC
2442IMPLEMENT_DYNAMIC_CLASS(wxGTKPaintImplDC, wxGTKClientImplDC)
2443#else
f469b27c 2444IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC)
2970ae54 2445#endif
ec758a20 2446
2cfddfe7
JS
2447// Limit the paint region to the window size. Sometimes
2448// the paint region is too big, and this risks X11 errors
2449static void wxLimitRegionToSize(wxRegion& region, const wxSize& sz)
2450{
2451 wxRect originalRect = region.GetBox();
2452 wxRect rect(originalRect);
2453 if (rect.width + rect.x > sz.x)
2454 rect.width = sz.x - rect.x;
2455 if (rect.height + rect.y > sz.y)
2456 rect.height = sz.y - rect.y;
2457 if (rect != originalRect)
2458 {
2459 region = wxRegion(rect);
2460 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2461 originalRect.x, originalRect.y, originalRect.width, originalRect.height,
2462 rect.x, rect.y, rect.width, rect.height);
2463 }
2464}
2465
2970ae54
RR
2466wxGTKPaintImplDC::wxGTKPaintImplDC( wxWindow *win )
2467 : wxGTKClientImplDC( win )
ec758a20 2468{
b6fa52db
RR
2469#if USE_PAINT_REGION
2470 if (!win->m_clipPaintRegion)
2471 return;
f469b27c 2472
2cfddfe7 2473 wxSize sz = win->GetSize();
bcb614b3 2474 m_paintClippingRegion = win->m_nativeUpdateRegion;
2cfddfe7 2475 wxLimitRegionToSize(m_paintClippingRegion, sz);
ab9d0a8c 2476
5f170f33
VZ
2477 GdkRegion *region = m_paintClippingRegion.GetRegion();
2478 if ( region )
2479 {
823faac3 2480 m_currentClippingRegion.Union( m_paintClippingRegion );
2cfddfe7
JS
2481 wxLimitRegionToSize(m_currentClippingRegion, sz);
2482
2483 if (sz.x <= 0 || sz.y <= 0)
2484 return ;
5f170f33 2485
823faac3
JS
2486 gdk_gc_set_clip_region( m_penGC, region );
2487 gdk_gc_set_clip_region( m_brushGC, region );
2488 gdk_gc_set_clip_region( m_textGC, region );
2489 gdk_gc_set_clip_region( m_bgGC, region );
5f170f33 2490 }
1e6feb95 2491#endif // USE_PAINT_REGION
ec758a20
RR
2492}
2493
2494//-----------------------------------------------------------------------------
2495// wxClientDC
2496//-----------------------------------------------------------------------------
2497
2970ae54
RR
2498#if wxUSE_NEW_DC
2499IMPLEMENT_DYNAMIC_CLASS(wxGTKClientImplDC, wxGTKWindowImplDC)
2500#else
f469b27c 2501IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
2970ae54 2502#endif
ec758a20 2503
2970ae54
RR
2504wxGTKClientImplDC::wxGTKClientImplDC( wxWindow *win )
2505 : wxGTKWindowImplDC( win )
ec758a20 2506{
2970ae54 2507 wxCHECK_RET( win, _T("NULL window in wxGTKClientImplDC::wxClientDC") );
4f55a07f 2508
1e6feb95
VZ
2509#ifdef __WXUNIVERSAL__
2510 wxPoint ptOrigin = win->GetClientAreaOrigin();
2511 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
2512 wxSize size = win->GetClientSize();
2513 SetClippingRegion(wxPoint(0, 0), size);
2514#endif // __WXUNIVERSAL__
ec758a20
RR
2515}
2516
2970ae54 2517void wxGTKClientImplDC::DoGetSize(int *width, int *height) const
4f55a07f
VZ
2518{
2519 wxCHECK_RET( m_owner, _T("GetSize() doesn't work without window") );
2520
2521 m_owner->GetClientSize( width, height );
2522}
2523
3d2d8da1
RR
2524// ----------------------------------------------------------------------------
2525// wxDCModule
2526// ----------------------------------------------------------------------------
2527
2528class wxDCModule : public wxModule
2529{
2530public:
2531 bool OnInit();
2532 void OnExit();
2533
2534private:
2535 DECLARE_DYNAMIC_CLASS(wxDCModule)
2536};
2537
2538IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
2539
2540bool wxDCModule::OnInit()
2541{
2542 wxInitGCPool();
ab9d0a8c 2543 return true;
3d2d8da1
RR
2544}
2545
2546void wxDCModule::OnExit()
2547{
2548 wxCleanUpGCPool();
2549}