]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dcclient.cpp
silence GCC warning "base class ‘class wxEvtHandler’ should be explicitly initialized...
[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
4787c92d 700void wxWindowDCImpl::DoDrawLines( int n, const 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 713 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
4787c92d
PC
714 const GdkPoint* gpts = reinterpret_cast<const GdkPoint*>(points);
715 GdkPoint* gpts_alloc = NULL;
e90411c2 716
280831d5 717 if (doScale)
4787c92d
PC
718 {
719 gpts_alloc = new GdkPoint[n];
720 gpts = gpts_alloc;
721 }
7d5af6fa 722
ab9d0a8c 723 for (int i = 0; i < n; i++)
265898fd 724 {
280831d5
PC
725 if (doScale)
726 {
4787c92d
PC
727 gpts_alloc[i].x = XLOG2DEV(points[i].x + xoffset);
728 gpts_alloc[i].y = YLOG2DEV(points[i].y + yoffset);
e90411c2 729 }
280831d5 730 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
e90411c2 731 }
6eb37239 732
888dde65
RR
733 if (m_gdkwindow)
734 gdk_draw_lines( m_gdkwindow, m_penGC, gpts, n);
6eb37239 735
4787c92d 736 delete[] gpts_alloc;
903f689b 737}
c801d85f 738
4787c92d 739void wxWindowDCImpl::DoDrawPolygon( int n, const wxPoint points[],
89efaf2b
FM
740 wxCoord xoffset, wxCoord yoffset,
741 wxPolygonFillMode WXUNUSED(fillStyle) )
cf7a7e13 742{
ab171e95 743 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 744
4bc67cc5 745 if (n <= 0) return;
7d5af6fa 746
e90411c2 747 //Check, if scaling is necessary
280831d5
PC
748 const bool doScale =
749 xoffset != 0 || yoffset != 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
e90411c2 750
280831d5 751 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
4787c92d
PC
752 const GdkPoint* gdkpoints = reinterpret_cast<const GdkPoint*>(points);
753 GdkPoint* gdkpoints_alloc = NULL;
e90411c2 754
280831d5 755 if (doScale)
4787c92d
PC
756 {
757 gdkpoints_alloc = new GdkPoint[n];
758 gdkpoints = gdkpoints_alloc;
759 }
e90411c2 760
265898fd
RR
761 int i;
762 for (i = 0 ; i < n ; i++)
763 {
280831d5
PC
764 if (doScale)
765 {
4787c92d
PC
766 gdkpoints_alloc[i].x = XLOG2DEV(points[i].x + xoffset);
767 gdkpoints_alloc[i].y = YLOG2DEV(points[i].y + yoffset);
e90411c2 768 }
280831d5 769 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
e90411c2 770 }
7d5af6fa 771
888dde65 772 if (m_gdkwindow)
72174350 773 {
e6777e65 774 if ( m_brush.IsNonTransparent() )
72174350 775 {
ea3a345f
PC
776 GdkGC* gc;
777 bool originChanged;
778 DrawingSetup(gc, originChanged);
779
780 gdk_draw_polygon(m_gdkwindow, gc, true, gdkpoints, n);
781
782 if (originChanged)
783 gdk_gc_set_ts_origin(gc, 0, 0);
72174350 784 }
7d5af6fa 785
e6777e65 786 if ( m_pen.IsNonTransparent() )
b0e0d661 787 {
bb56b0a8 788/*
e1208c31
RR
789 for (i = 0 ; i < n ; i++)
790 {
888dde65 791 gdk_draw_line( m_gdkwindow, m_penGC,
e1208c31
RR
792 gdkpoints[i%n].x,
793 gdkpoints[i%n].y,
794 gdkpoints[(i+1)%n].x,
795 gdkpoints[(i+1)%n].y);
796 }
bb56b0a8 797*/
888dde65 798 gdk_draw_polygon( m_gdkwindow, m_penGC, FALSE, gdkpoints, n );
ab9d0a8c 799
265898fd 800 }
6db90681 801 }
7d5af6fa 802
4787c92d 803 delete[] gdkpoints_alloc;
903f689b 804}
c801d85f 805
888dde65 806void wxWindowDCImpl::DoDrawRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 807{
ab171e95 808 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
c801d85f 809
72cdf4c9
VZ
810 wxCoord xx = XLOG2DEV(x);
811 wxCoord yy = YLOG2DEV(y);
812 wxCoord ww = m_signX * XLOG2DEVREL(width);
813 wxCoord hh = m_signY * YLOG2DEVREL(height);
7d5af6fa 814
265898fd
RR
815 // CMB: draw nothing if transformed w or h is 0
816 if (ww == 0 || hh == 0) return;
6f65e337 817
265898fd
RR
818 // CMB: handle -ve width and/or height
819 if (ww < 0) { ww = -ww; xx = xx - ww; }
820 if (hh < 0) { hh = -hh; yy = yy - hh; }
6f65e337 821
888dde65 822 if (m_gdkwindow)
6db90681 823 {
e6777e65 824 if ( m_brush.IsNonTransparent() )
72174350 825 {
ea3a345f
PC
826 GdkGC* gc;
827 bool originChanged;
828 DrawingSetup(gc, originChanged);
829
830 gdk_draw_rectangle(m_gdkwindow, gc, true, xx, yy, ww, hh);
831
832 if (originChanged)
833 gdk_gc_set_ts_origin(gc, 0, 0);
72174350 834 }
e1208c31 835
e6777e65 836 if ( m_pen.IsNonTransparent() )
3b5bf828 837 {
3b5bf828 838 if ((m_pen.GetWidth() == 2) && (m_pen.GetCap() == wxCAP_ROUND) &&
04ee05f9 839 (m_pen.GetJoin() == wxJOIN_ROUND) && (m_pen.GetStyle() == wxPENSTYLE_SOLID))
3b5bf828
RR
840 {
841 // Use 2 1-line rects instead
842 gdk_gc_set_line_attributes( m_penGC, 1, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
843
844 if (m_signX == -1)
845 {
846 // Different for RTL
888dde65
RR
847 gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx+1, yy, ww-2, hh-2 );
848 gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx, yy-1, ww, hh );
3b5bf828
RR
849 }
850 else
851 {
888dde65
RR
852 gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx, yy, ww-2, hh-2 );
853 gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx-1, yy-1, ww, hh );
3b5bf828 854 }
f4322df6 855
3b5bf828
RR
856 // reset
857 gdk_gc_set_line_attributes( m_penGC, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
858 }
859 else
3b5bf828
RR
860 {
861 // Just use X11 for other cases
888dde65 862 gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx, yy, ww-1, hh-1 );
3b5bf828
RR
863 }
864 }
6db90681 865 }
7d5af6fa 866
265898fd
RR
867 CalcBoundingBox( x, y );
868 CalcBoundingBox( x + width, y + height );
903f689b 869}
c801d85f 870
888dde65 871void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius )
c801d85f 872{
ab171e95 873 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 874
265898fd 875 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
7d5af6fa 876
72cdf4c9
VZ
877 wxCoord xx = XLOG2DEV(x);
878 wxCoord yy = YLOG2DEV(y);
879 wxCoord ww = m_signX * XLOG2DEVREL(width);
880 wxCoord hh = m_signY * YLOG2DEVREL(height);
881 wxCoord rr = XLOG2DEVREL((wxCoord)radius);
265898fd
RR
882
883 // CMB: handle -ve width and/or height
884 if (ww < 0) { ww = -ww; xx = xx - ww; }
885 if (hh < 0) { hh = -hh; yy = yy - hh; }
886
887 // CMB: if radius is zero use DrawRectangle() instead to avoid
888 // X drawing errors with small radii
889 if (rr == 0)
890 {
ab171e95 891 DoDrawRectangle( x, y, width, height );
265898fd
RR
892 return;
893 }
894
895 // CMB: draw nothing if transformed w or h is 0
896 if (ww == 0 || hh == 0) return;
897
898 // CMB: adjust size if outline is drawn otherwise the result is
899 // 1 pixel too wide and high
e6777e65 900 if ( m_pen.IsNonTransparent() )
265898fd
RR
901 {
902 ww--;
903 hh--;
904 }
905
888dde65 906 if (m_gdkwindow)
265898fd 907 {
6db90681
RR
908 // CMB: ensure dd is not larger than rectangle otherwise we
909 // get an hour glass shape
72cdf4c9 910 wxCoord dd = 2 * rr;
6db90681
RR
911 if (dd > ww) dd = ww;
912 if (dd > hh) dd = hh;
913 rr = dd / 2;
914
e6777e65 915 if ( m_brush.IsNonTransparent() )
6db90681 916 {
ea3a345f
PC
917 GdkGC* gc;
918 bool originChanged;
919 DrawingSetup(gc, originChanged);
920
921 gdk_draw_rectangle(m_gdkwindow, gc, true, xx+rr, yy, ww-dd+1, hh);
922 gdk_draw_rectangle(m_gdkwindow, gc, true, xx, yy+rr, ww, hh-dd+1);
923 gdk_draw_arc(m_gdkwindow, gc, true, xx, yy, dd, dd, 90*64, 90*64);
924 gdk_draw_arc(m_gdkwindow, gc, true, xx+ww-dd, yy, dd, dd, 0, 90*64);
925 gdk_draw_arc(m_gdkwindow, gc, true, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64);
926 gdk_draw_arc(m_gdkwindow, gc, true, xx, yy+hh-dd, dd, dd, 180*64, 90*64);
927
928 if (originChanged)
929 gdk_gc_set_ts_origin(gc, 0, 0);
6db90681 930 }
7d5af6fa 931
e6777e65 932 if ( m_pen.IsNonTransparent() )
7d5af6fa 933 {
888dde65
RR
934 gdk_draw_line( m_gdkwindow, m_penGC, xx+rr+1, yy, xx+ww-rr, yy );
935 gdk_draw_line( m_gdkwindow, m_penGC, xx+rr+1, yy+hh, xx+ww-rr, yy+hh );
936 gdk_draw_line( m_gdkwindow, m_penGC, xx, yy+rr+1, xx, yy+hh-rr );
937 gdk_draw_line( m_gdkwindow, m_penGC, xx+ww, yy+rr+1, xx+ww, yy+hh-rr );
938 gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx, yy, dd, dd, 90*64, 90*64 );
939 gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
940 gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
941 gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
7d5af6fa 942 }
265898fd 943 }
7d5af6fa 944
265898fd
RR
945 // this ignores the radius
946 CalcBoundingBox( x, y );
947 CalcBoundingBox( x + width, y + height );
903f689b 948}
c801d85f 949
888dde65 950void wxWindowDCImpl::DoDrawEllipse( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 951{
ab171e95 952 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 953
72cdf4c9
VZ
954 wxCoord xx = XLOG2DEV(x);
955 wxCoord yy = YLOG2DEV(y);
956 wxCoord ww = m_signX * XLOG2DEVREL(width);
957 wxCoord hh = m_signY * YLOG2DEVREL(height);
6f65e337 958
265898fd
RR
959 // CMB: handle -ve width and/or height
960 if (ww < 0) { ww = -ww; xx = xx - ww; }
961 if (hh < 0) { hh = -hh; yy = yy - hh; }
7d5af6fa 962
888dde65 963 if (m_gdkwindow)
6db90681 964 {
e6777e65 965 if ( m_brush.IsNonTransparent() )
a56fcaaf 966 {
ea3a345f
PC
967 GdkGC* gc;
968 bool originChanged;
969 DrawingSetup(gc, originChanged);
970
c11bdf68
VZ
971 // If the pen is transparent pen we increase the size
972 // for better compatibility with other platforms.
e0564c1c 973 if (m_pen.IsTransparent())
c11bdf68
VZ
974 {
975 ++ww;
976 ++hh;
977 }
978
ea3a345f
PC
979 gdk_draw_arc(m_gdkwindow, gc, true, xx, yy, ww, hh, 0, 360*64);
980
981 if (originChanged)
982 gdk_gc_set_ts_origin(gc, 0, 0);
a56fcaaf 983 }
7d5af6fa 984
e6777e65 985 if ( m_pen.IsNonTransparent() )
c11bdf68 986 gdk_draw_arc( m_gdkwindow, m_penGC, false, xx, yy, ww, hh, 0, 360*64 );
6db90681 987 }
7d5af6fa 988
44856297 989 CalcBoundingBox( x, y );
265898fd 990 CalcBoundingBox( x + width, y + height );
903f689b 991}
c801d85f 992
888dde65 993void wxWindowDCImpl::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
c801d85f 994{
b0e0d661 995 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
ab9d0a8c 996 DoDrawBitmap( (const wxBitmap&)icon, x, y, true );
903f689b 997}
c801d85f 998
eb6ee8ab 999// scale a pixbuf
02cecc4d
PC
1000static GdkPixbuf*
1001Scale(GdkPixbuf* pixbuf, int dst_w, int dst_h, double sx, double sy)
98d8a7ec 1002{
02cecc4d
PC
1003 GdkPixbuf* pixbuf_scaled = gdk_pixbuf_new(
1004 GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf), 8, dst_w, dst_h);
1005 gdk_pixbuf_scale(pixbuf, pixbuf_scaled,
1006 0, 0, dst_w, dst_h, 0, 0, sx, sy, GDK_INTERP_NEAREST);
02cecc4d
PC
1007 return pixbuf_scaled;
1008}
98d8a7ec 1009
eb6ee8ab 1010// scale part of a pixmap using pixbuf scaling
02cecc4d
PC
1011static GdkPixbuf*
1012Scale(GdkPixmap* pixmap, int x, int y, int w, int h, int dst_w, int dst_h, double sx, double sy)
1013{
1014 GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable(
1015 NULL, pixmap, NULL, x, y, 0, 0, w, h);
eb6ee8ab
PC
1016 GdkPixbuf* pixbuf2 = Scale(pixbuf, dst_w, dst_h, sx, sy);
1017 g_object_unref(pixbuf);
1018 return pixbuf2;
98d8a7ec
VZ
1019}
1020
eb6ee8ab 1021// scale part of a mask pixmap
02cecc4d
PC
1022static GdkPixmap*
1023ScaleMask(GdkPixmap* mask, int x, int y, int w, int h, int dst_w, int dst_h, double sx, double sy)
98d8a7ec 1024{
02cecc4d
PC
1025 GdkPixbuf* pixbuf = Scale(mask, x, y, w, h, dst_w, dst_h, sx, sy);
1026
1027 // convert black and white pixbuf back to a mono pixmap
1028 const unsigned out_rowstride = (dst_w + 7) / 8;
1029 const size_t data_size = out_rowstride * size_t(dst_h);
1030 char* data = new char[data_size];
1031 char* out = data;
1032 const guchar* row = gdk_pixbuf_get_pixels(pixbuf);
1033 const int rowstride = gdk_pixbuf_get_rowstride(pixbuf);
1034 memset(data, 0, data_size);
1035 for (int j = 0; j < dst_h; j++, row += rowstride, out += out_rowstride)
1036 {
1037 const guchar* in = row;
1038 for (int i = 0; i < dst_w; i++, in += 3)
1039 if (*in)
1040 out[i >> 3] |= 1 << (i & 7);
1041 }
1042 g_object_unref(pixbuf);
1043 GdkPixmap* pixmap = gdk_bitmap_create_from_data(mask, data, dst_w, dst_h);
1044 delete[] data;
02cecc4d 1045 return pixmap;
98d8a7ec
VZ
1046}
1047
02cecc4d 1048// Make a new mask from part of a mask and a clip region.
02cecc4d 1049static GdkPixmap*
a909ef16 1050ClipMask(GdkPixmap* mask, GdkRegion* clipRegion, int x, int y, int dst_x, int dst_y, int w, int h)
98d8a7ec 1051{
02cecc4d
PC
1052 GdkGCValues gcValues;
1053 gcValues.foreground.pixel = 0;
1054 GdkGC* gc = gdk_gc_new_with_values(mask, &gcValues, GDK_GC_FOREGROUND);
1055 GdkPixmap* pixmap = gdk_pixmap_new(mask, w, h, 1);
1056 // clear new mask, so clipped areas will be masked
1057 gdk_draw_rectangle(pixmap, gc, true, 0, 0, w, h);
1058 gdk_gc_set_clip_region(gc, clipRegion);
1059 gdk_gc_set_clip_origin(gc, -dst_x, -dst_y);
1060 // draw old mask onto new one, with clip
1061 gdk_draw_drawable(pixmap, gc, mask, x, y, 0, 0, w, h);
1062 g_object_unref(gc);
02cecc4d 1063 return pixmap;
98d8a7ec
VZ
1064}
1065
02cecc4d
PC
1066// make a color pixmap from part of a mono one, using text fg/bg colors
1067GdkPixmap*
1068wxWindowDCImpl::MonoToColor(GdkPixmap* monoPixmap, int x, int y, int w, int h) const
98d8a7ec 1069{
02cecc4d
PC
1070 GdkPixmap* pixmap = gdk_pixmap_new(m_gdkwindow, w, h, -1);
1071 GdkGCValues gcValues;
1072 gcValues.foreground.pixel = m_textForegroundColour.GetColor()->pixel;
1073 gcValues.background.pixel = m_textBackgroundColour.GetColor()->pixel;
1074 gcValues.stipple = monoPixmap;
1075 gcValues.fill = GDK_OPAQUE_STIPPLED;
1076 gcValues.ts_x_origin = -x;
1077 gcValues.ts_y_origin = -y;
1078 GdkGC* gc = gdk_gc_new_with_values(pixmap, &gcValues, GdkGCValuesMask(
1079 GDK_GC_FOREGROUND | GDK_GC_BACKGROUND | GDK_GC_STIPPLE | GDK_GC_FILL |
1080 GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN));
1081 gdk_draw_rectangle(pixmap, gc, true, 0, 0, w, h);
1082 g_object_unref(gc);
1083 return pixmap;
98d8a7ec
VZ
1084}
1085
888dde65 1086void wxWindowDCImpl::DoDrawBitmap( const wxBitmap &bitmap,
72cdf4c9 1087 wxCoord x, wxCoord y,
b0e0d661 1088 bool useMask )
4bc67cc5 1089{
ab171e95 1090 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
ab171e95 1091 wxCHECK_RET( bitmap.IsOk(), wxT("invalid bitmap") );
7d5af6fa 1092
02cecc4d 1093 if (!m_gdkwindow) return;
f4322df6 1094
02cecc4d
PC
1095 const int w = bitmap.GetWidth();
1096 const int h = bitmap.GetHeight();
7d5af6fa 1097
15b85007
VZ
1098 // notice that as the bitmap is not drawn upside down (or right to left)
1099 // even if the corresponding axis direction is inversed, we need to take it
1100 // into account when calculating its bounding box
02cecc4d 1101 CalcBoundingBox(x, y);
15b85007 1102 CalcBoundingBox(x + m_signX*w, y + m_signY*h);
7d5af6fa 1103
02cecc4d
PC
1104 // device coords
1105 int xx = LogicalToDeviceX(x);
1106 const int yy = LogicalToDeviceY(y);
1107 const int ww = LogicalToDeviceXRel(w);
1108 const int hh = LogicalToDeviceYRel(h);
7d5af6fa 1109
6226d1e9
PC
1110 if (m_window && m_window->GetLayoutDirection() == wxLayout_RightToLeft)
1111 xx -= ww;
1112
a909ef16 1113 GdkRegion* const clipRegion = m_currentClippingRegion.GetRegion();
02cecc4d
PC
1114 // determine clip region overlap
1115 int overlap = wxInRegion;
1116 if (clipRegion)
1117 {
1118 overlap = m_currentClippingRegion.Contains(xx, yy, ww, hh);
1119 if (overlap == wxOutRegion)
1120 return;
1121 }
7d5af6fa 1122
02cecc4d
PC
1123 const bool isScaled = ww != w || hh != h;
1124 const bool hasAlpha = bitmap.HasAlpha();
1125 GdkGC* const use_gc = m_penGC;
ab9d0a8c 1126
02cecc4d
PC
1127 GdkPixmap* mask = NULL;
1128 // mask does not work when drawing a pixbuf with alpha
1129 if (useMask && !hasAlpha)
1130 {
1131 wxMask* m = bitmap.GetMask();
1132 if (m)
1133 mask = m->GetBitmap();
1134 }
1135 if (mask)
d90c9596 1136 {
eb6ee8ab 1137 GdkPixmap* mask_new = NULL;
02cecc4d 1138 if (isScaled)
eb6ee8ab 1139 {
02cecc4d 1140 mask = ScaleMask(mask, 0, 0, w, h, ww, hh, m_scaleX, m_scaleY);
eb6ee8ab
PC
1141 mask_new = mask;
1142 }
02cecc4d 1143 if (overlap == wxPartRegion)
82ea63e6 1144 {
02cecc4d
PC
1145 // need a new mask that also masks the clipped area,
1146 // because gc can't have both a mask and a clip region
1147 mask = ClipMask(mask, clipRegion, 0, 0, xx, yy, ww, hh);
eb6ee8ab
PC
1148 if (mask_new)
1149 g_object_unref(mask_new);
1150 mask_new = mask;
d90c9596 1151 }
280831d5
PC
1152 gdk_gc_set_clip_mask(use_gc, mask);
1153 gdk_gc_set_clip_origin(use_gc, xx, yy);
eb6ee8ab
PC
1154 if (mask_new)
1155 g_object_unref(mask_new);
d90c9596 1156 }
7d5af6fa 1157
02cecc4d
PC
1158 // determine whether to use pixmap or pixbuf
1159 GdkPixmap* pixmap = NULL;
eb6ee8ab 1160 GdkPixmap* pixmap_new = NULL;
02cecc4d 1161 GdkPixbuf* pixbuf = NULL;
eb6ee8ab 1162 GdkPixbuf* pixbuf_new = NULL;
02cecc4d
PC
1163 if (bitmap.HasPixmap())
1164 pixmap = bitmap.GetPixmap();
1165 if (pixmap && gdk_drawable_get_depth(pixmap) == 1)
d90c9596 1166 {
d4ca3c5d
PC
1167 if (gdk_drawable_get_depth(m_gdkwindow) != 1)
1168 {
1169 // convert mono pixmap to color using text fg/bg colors
1170 pixmap = MonoToColor(pixmap, 0, 0, w, h);
1171 pixmap_new = pixmap;
1172 }
02cecc4d
PC
1173 }
1174 else if (hasAlpha || pixmap == NULL)
02cecc4d 1175 pixbuf = bitmap.GetPixbuf();
02cecc4d
PC
1176
1177 if (isScaled)
1178 {
1179 if (pixbuf)
1180 pixbuf = Scale(pixbuf, ww, hh, m_scaleX, m_scaleY);
feac7937 1181 else
02cecc4d 1182 pixbuf = Scale(pixmap, 0, 0, w, h, ww, hh, m_scaleX, m_scaleY);
eb6ee8ab
PC
1183
1184 pixbuf_new = pixbuf;
02cecc4d
PC
1185 }
1186
1187 if (pixbuf)
1188 {
1189 gdk_draw_pixbuf(m_gdkwindow, use_gc, pixbuf,
1190 0, 0, xx, yy, ww, hh, GDK_RGB_DITHER_NORMAL, 0, 0);
02cecc4d
PC
1191 }
1192 else
1193 {
1194 gdk_draw_drawable(m_gdkwindow, use_gc, pixmap, 0, 0, xx, yy, ww, hh);
d90c9596 1195 }
72174350 1196
eb6ee8ab
PC
1197 if (pixbuf_new)
1198 g_object_unref(pixbuf_new);
1199 if (pixmap_new)
1200 g_object_unref(pixmap_new);
02cecc4d 1201 if (mask)
02cecc4d 1202 gdk_gc_set_clip_region(use_gc, clipRegion);
463c1fa1
RR
1203}
1204
888dde65 1205bool wxWindowDCImpl::DoBlit( wxCoord xdest, wxCoord ydest,
1e6feb95
VZ
1206 wxCoord width, wxCoord height,
1207 wxDC *source,
1208 wxCoord xsrc, wxCoord ysrc,
89efaf2b 1209 wxRasterOperationMode logical_func,
0cbff120
JS
1210 bool useMask,
1211 wxCoord xsrcMask, wxCoord ysrcMask )
c801d85f 1212{
ab171e95 1213 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
ab9d0a8c 1214 wxCHECK_MSG( source, false, wxT("invalid source dc") );
7d5af6fa 1215
888dde65 1216 if (!m_gdkwindow) return false;
7d5af6fa 1217
02cecc4d
PC
1218 GdkDrawable* srcDrawable = NULL;
1219 GdkPixmap* mask = NULL;
1220 wxMemoryDC* memDC = wxDynamicCast(source, wxMemoryDC);
1221 if (memDC)
e19a8aea 1222 {
02cecc4d
PC
1223 const wxBitmap& bitmap = memDC->GetSelectedBitmap();
1224 if (!bitmap.IsOk())
e19a8aea 1225 return false;
02cecc4d
PC
1226 srcDrawable = bitmap.GetPixmap();
1227 if (useMask)
1228 {
1229 wxMask* m = bitmap.GetMask();
1230 if (m)
1231 mask = m->GetBitmap();
1232 }
e19a8aea 1233 }
02cecc4d 1234 else
0cbff120 1235 {
02cecc4d
PC
1236 wxDCImpl* impl = source->GetImpl();
1237 wxWindowDCImpl* gtk_impl = wxDynamicCast(impl, wxWindowDCImpl);
1238 if (gtk_impl)
1239 srcDrawable = gtk_impl->GetGDKWindow();
1240 if (srcDrawable == NULL)
1241 return false;
0cbff120
JS
1242 }
1243
02cecc4d
PC
1244 CalcBoundingBox(xdest, ydest);
1245 CalcBoundingBox(xdest + width, ydest + height);
1246
1247 // source device coords
1248 int src_x = source->LogicalToDeviceX(xsrc);
1249 int src_y = source->LogicalToDeviceY(ysrc);
1250 int src_w = source->LogicalToDeviceXRel(width);
1251 int src_h = source->LogicalToDeviceYRel(height);
1252
1253 // Clip source rect to source dc.
1254 // Only necessary when scaling, to avoid GDK errors when
1255 // converting to pixbuf, but no harm in always doing it.
1256 // If source rect changes, it also changes the dest rect.
1257 wxRect clip;
1258 gdk_drawable_get_size(srcDrawable, &clip.width, &clip.height);
1259 clip.Intersect(wxRect(src_x, src_y, src_w, src_h));
1260 if (src_w != clip.width || src_h != clip.height)
6e13c196 1261 {
02cecc4d
PC
1262 if (clip.width == 0)
1263 return true;
1264
1265 src_w = clip.width;
1266 src_h = clip.height;
1267 width = source->DeviceToLogicalXRel(src_w);
1268 height = source->DeviceToLogicalYRel(src_h);
1269 if (src_x != clip.x || src_y != clip.y)
b0e0d661 1270 {
02cecc4d
PC
1271 xdest += source->DeviceToLogicalXRel(clip.x - src_x);
1272 ydest += source->DeviceToLogicalYRel(clip.y - src_y);
1273 src_x = clip.x;
1274 src_y = clip.y;
b0e0d661 1275 }
6e13c196 1276 }
7d5af6fa 1277
02cecc4d
PC
1278 // destination device coords
1279 const int dst_x = LogicalToDeviceX(xdest);
1280 const int dst_y = LogicalToDeviceY(ydest);
1281 const int dst_w = LogicalToDeviceXRel(width);
1282 const int dst_h = LogicalToDeviceYRel(height);
3d2d8da1 1283
a909ef16 1284 GdkRegion* const clipRegion = m_currentClippingRegion.GetRegion();
02cecc4d
PC
1285 // determine dest clip region overlap
1286 int overlap = wxInRegion;
1287 if (clipRegion)
6f65e337 1288 {
02cecc4d
PC
1289 overlap = m_currentClippingRegion.Contains(dst_x, dst_y, dst_w, dst_h);
1290 if (overlap == wxOutRegion)
1291 return true;
1292 }
ab9d0a8c 1293
02cecc4d
PC
1294 const bool isScaled = src_w != dst_w || src_h != dst_h;
1295 double scale_x = 0;
1296 double scale_y = 0;
1297 if (isScaled)
1298 {
1299 // get source to dest scale
1300 double usx, usy, lsx, lsy;
1301 source->GetUserScale(&usx, &usy);
1302 source->GetLogicalScale(&lsx, &lsy);
1303 scale_x = m_scaleX / (usx * lsx);
1304 scale_y = m_scaleY / (usy * lsy);
1305 }
d90c9596 1306
02cecc4d 1307 GdkGC* const use_gc = m_penGC;
7d5af6fa 1308
02cecc4d
PC
1309 if (mask)
1310 {
02cecc4d
PC
1311 int srcMask_x = src_x;
1312 int srcMask_y = src_y;
1313 if (xsrcMask != -1 || ysrcMask != -1)
d90c9596 1314 {
02cecc4d
PC
1315 srcMask_x = source->LogicalToDeviceX(xsrcMask);
1316 srcMask_y = source->LogicalToDeviceY(ysrcMask);
d90c9596 1317 }
eb6ee8ab 1318 GdkPixmap* mask_new = NULL;
02cecc4d 1319 if (isScaled)
d90c9596 1320 {
02cecc4d
PC
1321 mask = ScaleMask(mask, srcMask_x, srcMask_y,
1322 src_w, src_h, dst_w, dst_h, scale_x, scale_y);
eb6ee8ab 1323 mask_new = mask;
02cecc4d
PC
1324 srcMask_x = 0;
1325 srcMask_y = 0;
d90c9596 1326 }
02cecc4d 1327 if (overlap == wxPartRegion)
6e13c196 1328 {
02cecc4d
PC
1329 // need a new mask that also masks the clipped area,
1330 // because gc can't have both a mask and a clip region
1331 mask = ClipMask(mask, clipRegion,
1332 srcMask_x, srcMask_y, dst_x, dst_y, dst_w, dst_h);
eb6ee8ab
PC
1333 if (mask_new)
1334 g_object_unref(mask_new);
1335 mask_new = mask;
02cecc4d
PC
1336 srcMask_x = 0;
1337 srcMask_y = 0;
265898fd 1338 }
02cecc4d
PC
1339 gdk_gc_set_clip_mask(use_gc, mask);
1340 gdk_gc_set_clip_origin(use_gc, dst_x - srcMask_x, dst_y - srcMask_y);
eb6ee8ab
PC
1341 if (mask_new)
1342 g_object_unref(mask_new);
6f65e337 1343 }
02cecc4d
PC
1344
1345 GdkPixmap* pixmap = NULL;
d4ca3c5d
PC
1346 if (gdk_drawable_get_depth(srcDrawable) == 1 &&
1347 (gdk_drawable_get_depth(m_gdkwindow) != 1 || isScaled))
6e13c196 1348 {
02cecc4d
PC
1349 // Convert mono pixmap to color using text fg/bg colors.
1350 // Scaling/drawing is simpler if this is done first.
1351 pixmap = MonoToColor(srcDrawable, src_x, src_y, src_w, src_h);
1352 srcDrawable = pixmap;
1353 src_x = 0;
1354 src_y = 0;
1355 }
f4322df6 1356
89efaf2b 1357 const wxRasterOperationMode logical_func_save = m_logicalFunction;
02cecc4d
PC
1358 SetLogicalFunction(logical_func);
1359 if (memDC == NULL)
1360 gdk_gc_set_subwindow(use_gc, GDK_INCLUDE_INFERIORS);
1361
1362 if (isScaled)
1363 {
1364 GdkPixbuf* pixbuf = Scale(srcDrawable,
1365 src_x, src_y, src_w, src_h, dst_w, dst_h, scale_x, scale_y);
1366 gdk_draw_pixbuf(m_gdkwindow, use_gc, pixbuf,
1367 0, 0, dst_x, dst_y, dst_w, dst_h, GDK_RGB_DITHER_NONE, 0, 0);
1368 g_object_unref(pixbuf);
1369 }
1370 else
1371 {
1372 gdk_draw_drawable(m_gdkwindow, use_gc, srcDrawable,
1373 src_x, src_y, dst_x, dst_y, dst_w, dst_h);
6e13c196 1374 }
c801d85f 1375
02cecc4d
PC
1376 SetLogicalFunction(logical_func_save);
1377 if (memDC == NULL)
1378 gdk_gc_set_subwindow(use_gc, GDK_CLIP_BY_CHILDREN);
ab9d0a8c 1379
02cecc4d
PC
1380 if (pixmap)
1381 g_object_unref(pixmap);
1382 if (mask)
02cecc4d 1383 gdk_gc_set_clip_region(use_gc, clipRegion);
eb6ee8ab 1384
ab9d0a8c 1385 return true;
903f689b 1386}
c801d85f 1387
2dcf60da
VZ
1388void wxWindowDCImpl::DoDrawText(const wxString& text,
1389 wxCoord xLogical,
1390 wxCoord yLogical)
c801d85f 1391{
ab171e95 1392 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
18a2fa37 1393
888dde65 1394 if (!m_gdkwindow) return;
ab9d0a8c 1395
2b5f62a0 1396 if (text.empty()) return;
7d5af6fa 1397
2dcf60da
VZ
1398 wxCoord x = XLOG2DEV(xLogical),
1399 y = YLOG2DEV(yLogical);
18a2fa37 1400
cfcc3932 1401 wxCHECK_RET( m_context, wxT("no Pango context") );
61850501 1402 wxCHECK_RET( m_layout, wxT("no Pango layout") );
cfcc3932 1403 wxCHECK_RET( m_fontdesc, wxT("no Pango font description") );
e4da1035 1404
5f22e8cc 1405 gdk_pango_context_set_colormap( m_context, m_cmap ); // not needed in gtk+ >= 2.6
24bbbfb1 1406
d6afc2c8 1407 wxCharBuffer data = wxGTK_CONV(text);
a3669332 1408 if ( !data )
7706daf3 1409 return;
ab9d0a8c 1410
99f8cf22
PC
1411 pango_layout_set_text(m_layout, data, data.length());
1412 const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout);
e4da1035 1413
46142dc9
PC
1414 int oldSize = 0;
1415 const bool isScaled = fabs(m_scaleY - 1.0) > 0.00001;
1416 if (isScaled)
e4da1035 1417 {
5f22e8cc
RR
1418 // If there is a user or actually any scale applied to
1419 // the device context, scale the font.
ab9d0a8c 1420
5f22e8cc 1421 // scale font description
46142dc9
PC
1422 oldSize = pango_font_description_get_size(m_fontdesc);
1423 pango_font_description_set_size(m_fontdesc, int(oldSize * m_scaleY));
ab9d0a8c 1424
5f22e8cc
RR
1425 // actually apply scaled font
1426 pango_layout_set_font_description( m_layout, m_fontdesc );
46142dc9 1427 }
ab9d0a8c 1428
46142dc9
PC
1429 int w, h;
1430 pango_layout_get_pixel_size(m_layout, &w, &h);
ab9d0a8c 1431
46142dc9
PC
1432 // Draw layout.
1433 int x_rtl = x;
1434 if (m_window && m_window->GetLayoutDirection() == wxLayout_RightToLeft)
1435 x_rtl -= w;
5f22e8cc
RR
1436
1437 const GdkColor* bg_col = NULL;
89efaf2b 1438 if (m_backgroundMode == wxBRUSHSTYLE_SOLID)
5f22e8cc 1439 bg_col = m_textBackgroundColour.GetColor();
89efaf2b 1440
5f22e8cc 1441 gdk_draw_layout_with_colors(m_gdkwindow, m_textGC, x_rtl, y, m_layout, NULL, bg_col);
ab9d0a8c 1442
46142dc9
PC
1443 if (isScaled)
1444 {
e4da1035
RR
1445 // reset unscaled size
1446 pango_font_description_set_size( m_fontdesc, oldSize );
ab9d0a8c 1447
61850501 1448 // actually apply unscaled font
e4da1035
RR
1449 pango_layout_set_font_description( m_layout, m_fontdesc );
1450 }
c7a49742 1451 if (setAttrs)
1dbeee57
VS
1452 {
1453 // undo underline attributes setting:
1454 pango_layout_set_attributes(m_layout, NULL);
1455 }
ab9d0a8c 1456
2dcf60da
VZ
1457 CalcBoundingBox(xLogical + int(w / m_scaleX), yLogical + int(h / m_scaleY));
1458 CalcBoundingBox(xLogical, yLogical);
903f689b 1459}
c801d85f 1460
89efaf2b 1461// TODO: When GTK2.6 is required, merge DoDrawText and DoDrawRotatedText to
e1bd1db2 1462// avoid code duplication
888dde65 1463void wxWindowDCImpl::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, double angle )
95724b1a 1464{
888dde65 1465 if (!m_gdkwindow || text.empty())
1c2d839f
PC
1466 return;
1467
ab171e95 1468 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1c2d839f 1469
c49ba211
PC
1470 x = XLOG2DEV(x);
1471 y = YLOG2DEV(y);
1472
1473 pango_layout_set_text(m_layout, wxGTK_CONV(text), -1);
1474 const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout);
1475 int oldSize = 0;
1476 const bool isScaled = fabs(m_scaleY - 1.0) > 0.00001;
1477 if (isScaled)
e1bd1db2 1478 {
c49ba211
PC
1479 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1480 // If there is a user or actually any scale applied to
1481 // the device context, scale the font.
e1bd1db2 1482
c49ba211
PC
1483 // scale font description
1484 oldSize = pango_font_description_get_size(m_fontdesc);
1485 pango_font_description_set_size(m_fontdesc, int(oldSize * m_scaleY));
e1bd1db2 1486
c49ba211
PC
1487 // actually apply scaled font
1488 pango_layout_set_font_description( m_layout, m_fontdesc );
1489 }
e1bd1db2 1490
c49ba211
PC
1491 int w, h;
1492 pango_layout_get_pixel_size(m_layout, &w, &h);
e1bd1db2 1493
c49ba211
PC
1494 const GdkColor* bg_col = NULL;
1495 if (m_backgroundMode == wxBRUSHSTYLE_SOLID)
1496 bg_col = m_textBackgroundColour.GetColor();
e1bd1db2 1497
c49ba211
PC
1498 // rotate the text
1499 PangoMatrix matrix = PANGO_MATRIX_INIT;
1500 pango_matrix_rotate (&matrix, angle);
1501 pango_context_set_matrix (m_context, &matrix);
1502 pango_layout_context_changed (m_layout);
1503
1504 // To be compatible with MSW, the rotation axis must be in the old
1505 // top-left corner.
1506 // Calculate the vertices of the rotated rectangle containing the text,
1507 // relative to the old top-left vertex.
1508 // We could use the matrix for this, but it's simpler with trignonometry.
1509 double rad = DegToRad(angle);
1510 // the rectangle vertices are counted clockwise with the first one
1511 // being at (0, 0)
1512 double x2 = w * cos(rad);
1513 double y2 = -w * sin(rad); // y axis points to the bottom, hence minus
1514 double x4 = h * sin(rad);
1515 double y4 = h * cos(rad);
1516 double x3 = x4 + x2;
1517 double y3 = y4 + y2;
1518 // Then we calculate max and min of the rotated rectangle.
1519 wxCoord maxX = (wxCoord)(dmax(dmax(0, x2), dmax(x3, x4)) + 0.5),
1520 maxY = (wxCoord)(dmax(dmax(0, y2), dmax(y3, y4)) + 0.5),
1521 minX = (wxCoord)(dmin(dmin(0, x2), dmin(x3, x4)) - 0.5),
1522 minY = (wxCoord)(dmin(dmin(0, y2), dmin(y3, y4)) - 0.5);
1523
1524 gdk_draw_layout_with_colors(m_gdkwindow, m_textGC, x+minX, y+minY,
1525 m_layout, NULL, bg_col);
e1bd1db2 1526
c49ba211
PC
1527 if (setAttrs)
1528 pango_layout_set_attributes(m_layout, NULL);
1529
1530 // clean up the transformation matrix
1531 pango_context_set_matrix(m_context, NULL);
e1bd1db2 1532
c49ba211
PC
1533 if (isScaled)
1534 {
1535 // reset unscaled size
1536 pango_font_description_set_size( m_fontdesc, oldSize );
1537
1538 // actually apply unscaled font
1539 pango_layout_set_font_description( m_layout, m_fontdesc );
e1bd1db2 1540 }
c49ba211
PC
1541
1542 CalcBoundingBox(x+minX, y+minY);
1543 CalcBoundingBox(x+maxX, y+maxY);
95724b1a
VZ
1544}
1545
888dde65 1546void wxWindowDCImpl::DoGetTextExtent(const wxString &string,
72cdf4c9
VZ
1547 wxCoord *width, wxCoord *height,
1548 wxCoord *descent, wxCoord *externalLeading,
c94f845b 1549 const wxFont *theFont) const
c801d85f 1550{
8cd79b7a
VZ
1551 // ensure we work with a valid font
1552 const wxFont *fontToUse;
ab171e95 1553 if ( !theFont || !theFont->IsOk() )
8cd79b7a
VZ
1554 fontToUse = &m_font;
1555 else
1556 fontToUse = theFont;
ab9d0a8c 1557
8cd79b7a 1558 wxCHECK_RET( fontToUse->IsOk(), wxT("invalid font") );
ab9d0a8c 1559
8cd79b7a
VZ
1560 wxTextMeasure txm(GetOwner(), fontToUse);
1561 txm.GetTextExtent(string, width, height, descent, externalLeading);
903f689b 1562}
c801d85f 1563
1f91072f 1564
888dde65 1565bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString& text,
1f91072f
VZ
1566 wxArrayInt& widths) const
1567{
8cd79b7a 1568 wxCHECK_MSG( m_font.IsOk(), false, wxT("Invalid font") );
1f91072f 1569
8cd79b7a
VZ
1570 wxTextMeasure txm(GetOwner(), &m_font);
1571 return txm.GetPartialTextExtents(text, widths, m_scaleX);
1f91072f
VZ
1572}
1573
1574
888dde65 1575wxCoord wxWindowDCImpl::GetCharWidth() const
c801d85f 1576{
cfcc3932 1577 pango_layout_set_text( m_layout, "H", 1 );
ace35c62
RR
1578 int w;
1579 pango_layout_get_pixel_size( m_layout, &w, NULL );
2b5f62a0 1580 return w;
903f689b 1581}
c801d85f 1582
888dde65 1583wxCoord wxWindowDCImpl::GetCharHeight() const
c801d85f 1584{
a8c5e1a9 1585 PangoFontMetrics *metrics = pango_context_get_metrics (m_context, m_fontdesc, pango_context_get_language(m_context));
9a83f860 1586 wxCHECK_MSG( metrics, -1, wxT("failed to get pango font metrics") );
2e61f681
VZ
1587
1588 wxCoord h = PANGO_PIXELS (pango_font_metrics_get_descent (metrics) +
b5791cc7 1589 pango_font_metrics_get_ascent (metrics));
2e61f681
VZ
1590 pango_font_metrics_unref (metrics);
1591 return h;
903f689b 1592}
c801d85f 1593
888dde65 1594void wxWindowDCImpl::Clear()
c801d85f 1595{
ab171e95 1596 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1597
888dde65 1598 if (!m_gdkwindow) return;
7d5af6fa 1599
f60e7fdd 1600 int width,height;
ab171e95 1601 DoGetSize( &width, &height );
888dde65 1602 gdk_draw_rectangle( m_gdkwindow, m_bgGC, TRUE, 0, 0, width, height );
903f689b 1603}
c801d85f 1604
888dde65 1605void wxWindowDCImpl::SetFont( const wxFont &font )
c801d85f 1606{
265898fd 1607 m_font = font;
ab9d0a8c 1608
ab171e95 1609 if (m_font.IsOk())
2b5f62a0 1610 {
cfcc3932
RR
1611 if (m_fontdesc)
1612 pango_font_description_free( m_fontdesc );
ab9d0a8c 1613
cfcc3932 1614 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
ab9d0a8c
WS
1615
1616
888dde65 1617 if (m_window)
77416abd 1618 {
cfcc3932 1619 PangoContext *oldContext = m_context;
ab9d0a8c 1620
496e7ec6 1621 m_context = m_window->GTKGetPangoDefaultContext();
ab9d0a8c 1622
cfcc3932
RR
1623 // If we switch back/forth between different contexts
1624 // we also have to create a new layout. I think so,
ab9d0a8c 1625 // at least, and it doesn't hurt to do it.
cfcc3932
RR
1626 if (oldContext != m_context)
1627 {
1628 if (m_layout)
3fe39b0c 1629 g_object_unref (m_layout);
ab9d0a8c 1630
cfcc3932
RR
1631 m_layout = pango_layout_new( m_context );
1632 }
77416abd 1633 }
ab9d0a8c 1634
cfcc3932 1635 pango_layout_set_font_description( m_layout, m_fontdesc );
2b5f62a0 1636 }
903f689b 1637}
c801d85f 1638
888dde65 1639void wxWindowDCImpl::SetPen( const wxPen &pen )
c801d85f 1640{
ab171e95 1641 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1642
265898fd 1643 if (m_pen == pen) return;
7d5af6fa 1644
265898fd 1645 m_pen = pen;
7d5af6fa 1646
ab171e95 1647 if (!m_pen.IsOk()) return;
7d5af6fa 1648
888dde65 1649 if (!m_gdkwindow) return;
7d5af6fa 1650
265898fd 1651 gint width = m_pen.GetWidth();
265898fd
RR
1652 if (width <= 0)
1653 {
112c5086 1654 // CMB: if width is non-zero scale it with the dc
265898fd
RR
1655 width = 1;
1656 }
1657 else
1658 {
1659 // X doesn't allow different width in x and y and so we take
1660 // the average
503f414e
VZ
1661 double w = 0.5 +
1662 ( fabs((double) XLOG2DEVREL(width)) +
1663 fabs((double) YLOG2DEVREL(width)) ) / 2.0;
265898fd 1664 width = (int)w;
375fc5a9
VZ
1665 if ( !width )
1666 {
1667 // width can't be 0 or an internal GTK error occurs inside
1668 // gdk_gc_set_dashes() below
1669 width = 1;
1670 }
265898fd 1671 }
7d5af6fa 1672
2eca425d
RL
1673 static const wxGTKDash dotted[] = {1, 1};
1674 static const wxGTKDash short_dashed[] = {2, 2};
1675 static const wxGTKDash wxCoord_dashed[] = {2, 4};
1676 static const wxGTKDash dotted_dashed[] = {3, 3, 1, 3};
7d5af6fa 1677
112c5086
RR
1678 // We express dash pattern in pen width unit, so we are
1679 // independent of zoom factor and so on...
1680 int req_nb_dash;
2eca425d 1681 const wxGTKDash *req_dash;
7d5af6fa 1682
86d58c90 1683 GdkLineStyle lineStyle = GDK_LINE_ON_OFF_DASH;
265898fd
RR
1684 switch (m_pen.GetStyle())
1685 {
04ee05f9 1686 case wxPENSTYLE_USER_DASH:
112c5086 1687 req_nb_dash = m_pen.GetDashCount();
2eca425d 1688 req_dash = (wxGTKDash*)m_pen.GetDash();
112c5086 1689 break;
04ee05f9 1690 case wxPENSTYLE_DOT:
112c5086
RR
1691 req_nb_dash = 2;
1692 req_dash = dotted;
7d5af6fa 1693 break;
04ee05f9 1694 case wxPENSTYLE_LONG_DASH:
112c5086 1695 req_nb_dash = 2;
72cdf4c9 1696 req_dash = wxCoord_dashed;
7d5af6fa 1697 break;
04ee05f9 1698 case wxPENSTYLE_SHORT_DASH:
112c5086
RR
1699 req_nb_dash = 2;
1700 req_dash = short_dashed;
7d5af6fa 1701 break;
04ee05f9 1702 case wxPENSTYLE_DOT_DASH:
112c5086
RR
1703 req_nb_dash = 4;
1704 req_dash = dotted_dashed;
7d5af6fa 1705 break;
7d5af6fa 1706
04ee05f9
PC
1707 case wxPENSTYLE_TRANSPARENT:
1708 case wxPENSTYLE_STIPPLE_MASK_OPAQUE:
1709 case wxPENSTYLE_STIPPLE:
1710 case wxPENSTYLE_SOLID:
7d5af6fa 1711 default:
7d5af6fa 1712 lineStyle = GDK_LINE_SOLID;
d3b9f782 1713 req_dash = NULL;
112c5086 1714 req_nb_dash = 0;
7d5af6fa 1715 break;
265898fd 1716 }
7d5af6fa 1717
112c5086
RR
1718 if (req_dash && req_nb_dash)
1719 {
2eca425d 1720 wxGTKDash *real_req_dash = new wxGTKDash[req_nb_dash];
112c5086
RR
1721 if (real_req_dash)
1722 {
1723 for (int i = 0; i < req_nb_dash; i++)
1724 real_req_dash[i] = req_dash[i] * width;
2eca425d 1725 gdk_gc_set_dashes( m_penGC, 0, real_req_dash, req_nb_dash );
112c5086
RR
1726 delete[] real_req_dash;
1727 }
1728 else
1729 {
1730 // No Memory. We use non-scaled dash pattern...
2eca425d 1731 gdk_gc_set_dashes( m_penGC, 0, (wxGTKDash*)req_dash, req_nb_dash );
112c5086
RR
1732 }
1733 }
7d5af6fa 1734
265898fd
RR
1735 GdkCapStyle capStyle = GDK_CAP_ROUND;
1736 switch (m_pen.GetCap())
1737 {
265898fd
RR
1738 case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
1739 case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }
d4aa3a4b
RR
1740 case wxCAP_ROUND:
1741 default:
d4aa3a4b
RR
1742 if (width <= 1)
1743 {
1744 width = 0;
1745 capStyle = GDK_CAP_NOT_LAST;
1746 }
72174350 1747 break;
265898fd 1748 }
7d5af6fa 1749
265898fd
RR
1750 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
1751 switch (m_pen.GetJoin())
1752 {
1753 case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
265898fd 1754 case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
d4aa3a4b
RR
1755 case wxJOIN_ROUND:
1756 default: { joinStyle = GDK_JOIN_ROUND; break; }
265898fd 1757 }
7d5af6fa 1758
265898fd 1759 gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
7d5af6fa 1760
265898fd
RR
1761 m_pen.GetColour().CalcPixel( m_cmap );
1762 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
903f689b 1763}
c801d85f 1764
888dde65 1765void wxWindowDCImpl::SetBrush( const wxBrush &brush )
c801d85f 1766{
ab171e95 1767 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1768
265898fd 1769 if (m_brush == brush) return;
7d5af6fa 1770
265898fd 1771 m_brush = brush;
7d5af6fa 1772
ab171e95 1773 if (!m_brush.IsOk()) return;
7d5af6fa 1774
888dde65 1775 if (!m_gdkwindow) return;
7d5af6fa 1776
265898fd
RR
1777 m_brush.GetColour().CalcPixel( m_cmap );
1778 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
7d5af6fa 1779
956dbab1 1780 gdk_gc_set_fill( m_brushGC, GDK_SOLID );
7d5af6fa 1781
04ee05f9 1782 if ((m_brush.GetStyle() == wxBRUSHSTYLE_STIPPLE) && (m_brush.GetStipple()->IsOk()))
265898fd 1783 {
b85229d1 1784 if (m_brush.GetStipple()->GetDepth() != 1)
7d5af6fa 1785 {
956dbab1 1786 gdk_gc_set_fill( m_brushGC, GDK_TILED );
cd25b18c 1787 gdk_gc_set_tile( m_brushGC, m_brush.GetStipple()->GetPixmap() );
7d5af6fa 1788 }
b0e0d661 1789 else
7d5af6fa 1790 {
956dbab1 1791 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
b85229d1 1792 gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() );
7d5af6fa 1793 }
265898fd 1794 }
7d5af6fa 1795
04ee05f9 1796 if ((m_brush.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
de2d2cdc 1797 {
e1208c31
RR
1798 gdk_gc_set_fill( m_textGC, GDK_OPAQUE_STIPPLED);
1799 gdk_gc_set_stipple( m_textGC, m_brush.GetStipple()->GetMask()->GetBitmap() );
de2d2cdc
VZ
1800 }
1801
ab9d0a8c 1802 if (m_brush.IsHatch())
265898fd 1803 {
956dbab1 1804 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
f7732fa5 1805 gdk_gc_set_stipple(m_brushGC, GetHatch(m_brush.GetStyle()));
265898fd 1806 }
903f689b 1807}
c801d85f 1808
888dde65 1809void wxWindowDCImpl::SetBackground( const wxBrush &brush )
46dc76ba 1810{
bbbbe360
RR
1811 /* CMB 21/7/98: Added SetBackground. Sets background brush
1812 * for Clear() and bg colour for shapes filled with cross-hatch brush */
7d5af6fa 1813
ab171e95 1814 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1815
265898fd 1816 if (m_backgroundBrush == brush) return;
7d5af6fa 1817
265898fd 1818 m_backgroundBrush = brush;
7d5af6fa 1819
ab171e95 1820 if (!m_backgroundBrush.IsOk()) return;
7d5af6fa 1821
888dde65 1822 if (!m_gdkwindow) return;
7d5af6fa 1823
86d58c90
PC
1824 wxColor color = m_backgroundBrush.GetColour();
1825 color.CalcPixel(m_cmap);
1826 const GdkColor* gdkColor = color.GetColor();
1827 gdk_gc_set_background(m_brushGC, gdkColor);
1828 gdk_gc_set_background(m_penGC, gdkColor);
1829 gdk_gc_set_background(m_bgGC, gdkColor);
1830 gdk_gc_set_foreground(m_bgGC, gdkColor);
1831
3417c2cd
RR
1832
1833 gdk_gc_set_fill( m_bgGC, GDK_SOLID );
7d5af6fa 1834
04ee05f9 1835 if (m_backgroundBrush.GetStyle() == wxBRUSHSTYLE_STIPPLE)
265898fd 1836 {
86d58c90
PC
1837 const wxBitmap* stipple = m_backgroundBrush.GetStipple();
1838 if (stipple->IsOk())
7d5af6fa 1839 {
86d58c90
PC
1840 if (stipple->GetDepth() != 1)
1841 {
1842 gdk_gc_set_fill(m_bgGC, GDK_TILED);
1843 gdk_gc_set_tile(m_bgGC, stipple->GetPixmap());
1844 }
1845 else
1846 {
1847 gdk_gc_set_fill(m_bgGC, GDK_STIPPLED);
1848 gdk_gc_set_stipple(m_bgGC, stipple->GetPixmap());
1849 }
7d5af6fa 1850 }
265898fd 1851 }
86d58c90 1852 else if (m_backgroundBrush.IsHatch())
265898fd 1853 {
3417c2cd 1854 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
f7732fa5 1855 gdk_gc_set_stipple(m_bgGC, GetHatch(m_backgroundBrush.GetStyle()));
bbbbe360 1856 }
903f689b 1857}
46dc76ba 1858
89efaf2b 1859void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function )
c801d85f 1860{
ab171e95 1861 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1862
72174350
VZ
1863 if (m_logicalFunction == function)
1864 return;
1865
1866 // VZ: shouldn't this be a CHECK?
888dde65 1867 if (!m_gdkwindow)
72174350 1868 return;
01eaf507 1869
2b5f62a0 1870 GdkFunction mode;
265898fd
RR
1871 switch (function)
1872 {
3c679789
RR
1873 case wxXOR: mode = GDK_XOR; break;
1874 case wxINVERT: mode = GDK_INVERT; break;
3c679789
RR
1875 case wxOR_REVERSE: mode = GDK_OR_REVERSE; break;
1876 case wxAND_REVERSE: mode = GDK_AND_REVERSE; break;
1877 case wxCLEAR: mode = GDK_CLEAR; break;
1878 case wxSET: mode = GDK_SET; break;
1879 case wxOR_INVERT: mode = GDK_OR_INVERT; break;
1880 case wxAND: mode = GDK_AND; break;
1881 case wxOR: mode = GDK_OR; break;
1882 case wxEQUIV: mode = GDK_EQUIV; break;
1883 case wxNAND: mode = GDK_NAND; break;
1884 case wxAND_INVERT: mode = GDK_AND_INVERT; break;
7d5af6fa
VZ
1885 case wxCOPY: mode = GDK_COPY; break;
1886 case wxNO_OP: mode = GDK_NOOP; break;
1887 case wxSRC_INVERT: mode = GDK_COPY_INVERT; break;
8e69bf8e 1888 case wxNOR: mode = GDK_NOR; break;
657a8a35
VZ
1889 default:
1890 wxFAIL_MSG("unknown mode");
1891 return;
265898fd 1892 }
7d5af6fa 1893
265898fd 1894 m_logicalFunction = function;
7d5af6fa 1895
265898fd
RR
1896 gdk_gc_set_function( m_penGC, mode );
1897 gdk_gc_set_function( m_brushGC, mode );
72174350
VZ
1898
1899 // to stay compatible with wxMSW, we don't apply ROPs to the text
3d2d8da1
RR
1900 // operations (i.e. DrawText/DrawRotatedText).
1901 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
1902 gdk_gc_set_function( m_textGC, mode );
903f689b 1903}
c801d85f 1904
888dde65 1905void wxWindowDCImpl::SetTextForeground( const wxColour &col )
c801d85f 1906{
ab171e95 1907 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1908
71ec83d2
VZ
1909 // don't set m_textForegroundColour to an invalid colour as we'd crash
1910 // later then (we use m_textForegroundColour.GetColor() without checking
1911 // in a few places)
ab171e95 1912 if ( !col.IsOk() || (m_textForegroundColour == col) )
71ec83d2 1913 return;
7d5af6fa 1914
265898fd 1915 m_textForegroundColour = col;
7d5af6fa 1916
888dde65 1917 if ( m_gdkwindow )
71ec83d2
VZ
1918 {
1919 m_textForegroundColour.CalcPixel( m_cmap );
1920 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
1921 }
903f689b 1922}
c801d85f 1923
888dde65 1924void wxWindowDCImpl::SetTextBackground( const wxColour &col )
c801d85f 1925{
ab171e95 1926 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1927
71ec83d2 1928 // same as above
ab171e95 1929 if ( !col.IsOk() || (m_textBackgroundColour == col) )
71ec83d2 1930 return;
7d5af6fa 1931
265898fd 1932 m_textBackgroundColour = col;
7d5af6fa 1933
888dde65 1934 if ( m_gdkwindow )
71ec83d2
VZ
1935 {
1936 m_textBackgroundColour.CalcPixel( m_cmap );
1937 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
1938 }
903f689b 1939}
c801d85f 1940
888dde65 1941void wxWindowDCImpl::SetBackgroundMode( int mode )
c801d85f 1942{
ab171e95 1943 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1944
265898fd 1945 m_backgroundMode = mode;
903f689b 1946}
c801d85f 1947
888dde65 1948void wxWindowDCImpl::SetPalette( const wxPalette& WXUNUSED(palette) )
c801d85f 1949{
888dde65 1950 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
903f689b 1951}
c801d85f 1952
888dde65 1953void wxWindowDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 1954{
ab171e95 1955 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1956
888dde65 1957 if (!m_gdkwindow) return;
7d5af6fa 1958
3d2d8da1 1959 wxRect rect;
265898fd
RR
1960 rect.x = XLOG2DEV(x);
1961 rect.y = YLOG2DEV(y);
1962 rect.width = XLOG2DEVREL(width);
1963 rect.height = YLOG2DEVREL(height);
5f170f33 1964
89efaf2b 1965 if (m_window && m_window->m_wxwindow &&
888dde65 1966 (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
49e74855
RR
1967 {
1968 rect.x -= rect.width;
1969 }
1970
98d8a7ec 1971 DoSetDeviceClippingRegion(wxRegion(rect));
903f689b 1972}
c801d85f 1973
fdaad94e 1974void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion &region )
463c1fa1 1975{
ab171e95 1976 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 1977
463c1fa1
RR
1978 if (region.Empty())
1979 {
1980 DestroyClippingRegion();
1981 return;
1982 }
7d5af6fa 1983
888dde65 1984 if (!m_gdkwindow) return;
5f170f33 1985
e1208c31 1986 if (!m_currentClippingRegion.IsNull())
993f97ee
RR
1987 m_currentClippingRegion.Intersect( region );
1988 else
1989 m_currentClippingRegion.Union( region );
5f170f33
VZ
1990
1991#if USE_PAINT_REGION
e1208c31 1992 if (!m_paintClippingRegion.IsNull())
3d2d8da1 1993 m_currentClippingRegion.Intersect( m_paintClippingRegion );
809934d2 1994#endif
3d2d8da1 1995
ee8dbe63
RL
1996 wxCoord xx, yy, ww, hh;
1997 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
888dde65 1998 wxGTKDCImpl::DoSetClippingRegion( xx, yy, ww, hh );
ee8dbe63 1999
86d58c90
PC
2000 GdkRegion* gdkRegion = m_currentClippingRegion.GetRegion();
2001 gdk_gc_set_clip_region(m_penGC, gdkRegion);
2002 gdk_gc_set_clip_region(m_brushGC, gdkRegion);
2003 gdk_gc_set_clip_region(m_textGC, gdkRegion);
2004 gdk_gc_set_clip_region(m_bgGC, gdkRegion);
463c1fa1
RR
2005}
2006
888dde65 2007void wxWindowDCImpl::DestroyClippingRegion()
c801d85f 2008{
ab171e95 2009 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
7d5af6fa 2010
888dde65 2011 wxDCImpl::DestroyClippingRegion();
7d5af6fa 2012
3d2d8da1 2013 m_currentClippingRegion.Clear();
5f170f33
VZ
2014
2015#if USE_PAINT_REGION
3d2d8da1
RR
2016 if (!m_paintClippingRegion.IsEmpty())
2017 m_currentClippingRegion.Union( m_paintClippingRegion );
993f97ee 2018#endif
3d2d8da1 2019
888dde65 2020 if (!m_gdkwindow) return;
7d5af6fa 2021
86d58c90
PC
2022 GdkRegion* gdkRegion = NULL;
2023 if (!m_currentClippingRegion.IsEmpty())
2024 gdkRegion = m_currentClippingRegion.GetRegion();
2025
2026 gdk_gc_set_clip_region(m_penGC, gdkRegion);
2027 gdk_gc_set_clip_region(m_brushGC, gdkRegion);
2028 gdk_gc_set_clip_region(m_textGC, gdkRegion);
2029 gdk_gc_set_clip_region(m_bgGC, gdkRegion);
903f689b 2030}
c801d85f 2031
888dde65 2032void wxWindowDCImpl::Destroy()
dbf858b5 2033{
3d2d8da1 2034 if (m_penGC) wxFreePoolGC( m_penGC );
d3b9f782 2035 m_penGC = NULL;
3d2d8da1 2036 if (m_brushGC) wxFreePoolGC( m_brushGC );
d3b9f782 2037 m_brushGC = NULL;
3d2d8da1 2038 if (m_textGC) wxFreePoolGC( m_textGC );
d3b9f782 2039 m_textGC = NULL;
3d2d8da1 2040 if (m_bgGC) wxFreePoolGC( m_bgGC );
d3b9f782 2041 m_bgGC = NULL;
dbf858b5
RR
2042}
2043
888dde65 2044void wxWindowDCImpl::SetDeviceOrigin( wxCoord x, wxCoord y )
847dfdb4
RR
2045{
2046 m_deviceOriginX = x;
2047 m_deviceOriginY = y;
f4322df6 2048
847dfdb4
RR
2049 ComputeScaleAndOrigin();
2050}
2051
888dde65 2052void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
847dfdb4
RR
2053{
2054 m_signX = (xLeftRight ? 1 : -1);
2055 m_signY = (yBottomUp ? -1 : 1);
f4322df6 2056
89efaf2b 2057 if (m_window && m_window->m_wxwindow &&
888dde65 2058 (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
f4322df6
VZ
2059 m_signX = -m_signX;
2060
847dfdb4
RR
2061 ComputeScaleAndOrigin();
2062}
2063
888dde65 2064void wxWindowDCImpl::ComputeScaleAndOrigin()
238d735d 2065{
c77a6796 2066 const wxRealPoint origScale(m_scaleX, m_scaleY);
238d735d 2067
888dde65 2068 wxDCImpl::ComputeScaleAndOrigin();
238d735d 2069
c77a6796 2070 // if scale has changed call SetPen to recalulate the line width
ab171e95 2071 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.IsOk() )
238d735d 2072 {
c77a6796
VZ
2073 // this is a bit artificial, but we need to force wxDC to think the pen
2074 // has changed
0a164d4c
WS
2075 wxPen pen = m_pen;
2076 m_pen = wxNullPen;
2077 SetPen( pen );
2078 }
238d735d
RR
2079}
2080
b0e0d661 2081// Resolution in pixels per logical inch
888dde65 2082wxSize wxWindowDCImpl::GetPPI() const
b0e0d661 2083{
7a5e6267 2084 return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5));
b0e0d661
VZ
2085}
2086
888dde65 2087int wxWindowDCImpl::GetDepth() const
c801d85f 2088{
888dde65 2089 return gdk_drawable_get_depth(m_gdkwindow);
903f689b 2090}
c801d85f 2091
ab171e95 2092//-----------------------------------------------------------------------------
888dde65 2093// wxClientDCImpl
ab171e95
RR
2094//-----------------------------------------------------------------------------
2095
888dde65 2096IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
ab171e95 2097
888dde65
RR
2098wxClientDCImpl::wxClientDCImpl( wxDC *owner )
2099 : wxWindowDCImpl( owner )
ab171e95
RR
2100{
2101}
2102
888dde65
RR
2103wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *win )
2104 : wxWindowDCImpl( owner, win )
ab171e95 2105{
9a83f860 2106 wxCHECK_RET( win, wxT("NULL window in wxClientDCImpl::wxClientDC") );
ab171e95
RR
2107
2108#ifdef __WXUNIVERSAL__
2109 wxPoint ptOrigin = win->GetClientAreaOrigin();
2110 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
2111 wxSize size = win->GetClientSize();
6267c3ea 2112 DoSetClippingRegion(0, 0, size.x, size.y);
89efaf2b 2113#endif
888dde65 2114 // __WXUNIVERSAL__
ab171e95
RR
2115}
2116
888dde65 2117void wxClientDCImpl::DoGetSize(int *width, int *height) const
ab171e95 2118{
9a83f860 2119 wxCHECK_RET( m_window, wxT("GetSize() doesn't work without window") );
ab171e95 2120
76274efd 2121 m_window->GetClientSize(width, height);
ab171e95
RR
2122}
2123
ec758a20 2124//-----------------------------------------------------------------------------
888dde65 2125// wxPaintDCImpl
ec758a20
RR
2126//-----------------------------------------------------------------------------
2127
888dde65 2128IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxClientDCImpl)
ec758a20 2129
2cfddfe7
JS
2130// Limit the paint region to the window size. Sometimes
2131// the paint region is too big, and this risks X11 errors
2132static void wxLimitRegionToSize(wxRegion& region, const wxSize& sz)
2133{
2134 wxRect originalRect = region.GetBox();
2135 wxRect rect(originalRect);
2136 if (rect.width + rect.x > sz.x)
2137 rect.width = sz.x - rect.x;
2138 if (rect.height + rect.y > sz.y)
2139 rect.height = sz.y - rect.y;
2140 if (rect != originalRect)
2141 {
2142 region = wxRegion(rect);
2143 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2144 originalRect.x, originalRect.y, originalRect.width, originalRect.height,
2145 rect.x, rect.y, rect.width, rect.height);
2146 }
2147}
2148
888dde65
RR
2149wxPaintDCImpl::wxPaintDCImpl( wxDC *owner )
2150 : wxClientDCImpl( owner )
ab171e95
RR
2151{
2152}
2153
888dde65
RR
2154wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *win )
2155 : wxClientDCImpl( owner, win )
ec758a20 2156{
b6fa52db
RR
2157#if USE_PAINT_REGION
2158 if (!win->m_clipPaintRegion)
2159 return;
f469b27c 2160
2cfddfe7 2161 wxSize sz = win->GetSize();
bcb614b3 2162 m_paintClippingRegion = win->m_nativeUpdateRegion;
2cfddfe7 2163 wxLimitRegionToSize(m_paintClippingRegion, sz);
ab9d0a8c 2164
5f170f33
VZ
2165 GdkRegion *region = m_paintClippingRegion.GetRegion();
2166 if ( region )
2167 {
823faac3 2168 m_currentClippingRegion.Union( m_paintClippingRegion );
2cfddfe7
JS
2169 wxLimitRegionToSize(m_currentClippingRegion, sz);
2170
2171 if (sz.x <= 0 || sz.y <= 0)
2172 return ;
5f170f33 2173
823faac3
JS
2174 gdk_gc_set_clip_region( m_penGC, region );
2175 gdk_gc_set_clip_region( m_brushGC, region );
2176 gdk_gc_set_clip_region( m_textGC, region );
2177 gdk_gc_set_clip_region( m_bgGC, region );
5f170f33 2178 }
89efaf2b 2179#endif
ec758a20
RR
2180}
2181
3d2d8da1
RR
2182// ----------------------------------------------------------------------------
2183// wxDCModule
2184// ----------------------------------------------------------------------------
2185
2186class wxDCModule : public wxModule
2187{
2188public:
2189 bool OnInit();
2190 void OnExit();
2191
2192private:
2193 DECLARE_DYNAMIC_CLASS(wxDCModule)
2194};
2195
2196IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
2197
2198bool wxDCModule::OnInit()
2199{
2200 wxInitGCPool();
ab9d0a8c 2201 return true;
3d2d8da1
RR
2202}
2203
2204void wxDCModule::OnExit()
2205{
2206 wxCleanUpGCPool();
f7732fa5 2207
04ee05f9 2208 for (int i = wxBRUSHSTYLE_LAST_HATCH - wxBRUSHSTYLE_FIRST_HATCH; i--; )
f7732fa5
PC
2209 {
2210 if (hatches[i])
2211 g_object_unref(hatches[i]);
2212 }
3d2d8da1 2213}