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