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