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