make some reference handling a little more direct and less obscure
[wxWidgets.git] / src / gtk / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dcclient.cpp
3 // Purpose: wxWindowDCImpl implementation
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Chris Breeze
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/gtk/dcclient.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/window.h"
17 #include "wx/log.h"
18 #include "wx/dcmemory.h"
19 #include "wx/math.h"
20 #include "wx/image.h"
21 #include "wx/module.h"
22 #endif
23
24 #include "wx/fontutil.h"
25
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/private/object.h"
28 #include "wx/private/textmeasure.h"
29
30 //-----------------------------------------------------------------------------
31 // local defines
32 //-----------------------------------------------------------------------------
33
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
39 #define USE_PAINT_REGION 1
40
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"
51
52 static GdkPixmap* hatches[wxBRUSHSTYLE_LAST_HATCH - wxBRUSHSTYLE_FIRST_HATCH + 1];
53
54 //-----------------------------------------------------------------------------
55 // constants
56 //-----------------------------------------------------------------------------
57
58 static const double RAD2DEG = 180.0 / M_PI;
59
60 // ----------------------------------------------------------------------------
61 // private functions
62 // ----------------------------------------------------------------------------
63
64 static inline double dmax(double a, double b) { return a > b ? a : b; }
65 static inline double dmin(double a, double b) { return a < b ? a : b; }
66
67 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
68
69 static GdkPixmap* GetHatch(int style)
70 {
71 wxASSERT(style >= wxBRUSHSTYLE_FIRST_HATCH && style <= wxBRUSHSTYLE_LAST_HATCH);
72 const int i = style - wxBRUSHSTYLE_FIRST_HATCH;
73 if (hatches[i] == NULL)
74 {
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
88 switch (style)
89 {
90 case wxBRUSHSTYLE_BDIAGONAL_HATCH:
91 hatches[i] = CREATE_FROM_XBM_DATA(bdiag);
92 break;
93 case wxBRUSHSTYLE_CROSSDIAG_HATCH:
94 hatches[i] = CREATE_FROM_XBM_DATA(cdiag);
95 break;
96 case wxBRUSHSTYLE_CROSS_HATCH:
97 hatches[i] = CREATE_FROM_XBM_DATA(cross);
98 break;
99 case wxBRUSHSTYLE_FDIAGONAL_HATCH:
100 hatches[i] = CREATE_FROM_XBM_DATA(fdiag);
101 break;
102 case wxBRUSHSTYLE_HORIZONTAL_HATCH:
103 hatches[i] = CREATE_FROM_XBM_DATA(horiz);
104 break;
105 case wxBRUSHSTYLE_VERTICAL_HATCH:
106 hatches[i] = CREATE_FROM_XBM_DATA(verti);
107 break;
108 }
109
110 #undef CREATE_FROM_XBM_DATA
111 }
112 return hatches[i];
113 }
114
115 //-----------------------------------------------------------------------------
116 // Implement Pool of Graphic contexts. Creating them takes too much time.
117 //-----------------------------------------------------------------------------
118
119 enum 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,
129 wxBRUSH_COLOUR,
130 wxTEXT_SCREEN,
131 wxBG_SCREEN,
132 wxPEN_SCREEN,
133 wxBRUSH_SCREEN,
134 wxTEXT_COLOUR_ALPHA,
135 wxBG_COLOUR_ALPHA,
136 wxPEN_COLOUR_ALPHA,
137 wxBRUSH_COLOUR_ALPHA
138 };
139
140 struct wxGC
141 {
142 GdkGC *m_gc;
143 wxPoolGCType m_type;
144 bool m_used;
145 };
146
147 #define GC_POOL_ALLOC_SIZE 100
148
149 static int wxGCPoolSize = 0;
150
151 static wxGC *wxGCPool = NULL;
152
153 static void wxInitGCPool()
154 {
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.
158
159 // Set initial pool size.
160 wxGCPoolSize = GC_POOL_ALLOC_SIZE;
161
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
169 // in wxGetPoolGC.
170 wxFAIL_MSG( wxT("Cannot allocate GC pool") );
171 return;
172 }
173
174 // Zero initial pool.
175 memset(wxGCPool, 0, wxGCPoolSize * sizeof(wxGC));
176 }
177
178 static void wxCleanUpGCPool()
179 {
180 for (int i = 0; i < wxGCPoolSize; i++)
181 {
182 if (wxGCPool[i].m_gc)
183 g_object_unref (wxGCPool[i].m_gc);
184 }
185
186 free(wxGCPool);
187 wxGCPool = NULL;
188 wxGCPoolSize = 0;
189 }
190
191 static GdkGC* wxGetPoolGC( GdkWindow *window, wxPoolGCType type )
192 {
193 wxGC *pptr;
194
195 // Look for an available GC.
196 for (int i = 0; i < wxGCPoolSize; i++)
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 );
202 wxGCPool[i].m_type = type;
203 wxGCPool[i].m_used = false;
204 }
205 if ((!wxGCPool[i].m_used) && (wxGCPool[i].m_type == type))
206 {
207 wxGCPool[i].m_used = true;
208 return wxGCPool[i].m_gc;
209 }
210 }
211
212 // We did not find an available GC.
213 // We need to grow the GC pool.
214 pptr = (wxGC *)realloc(wxGCPool,
215 (wxGCPoolSize + GC_POOL_ALLOC_SIZE)*sizeof(wxGC));
216 if (pptr != NULL)
217 {
218 // Initialize newly allocated pool.
219 wxGCPool = pptr;
220 memset(&wxGCPool[wxGCPoolSize], 0,
221 GC_POOL_ALLOC_SIZE*sizeof(wxGC));
222
223 // Initialize entry we will return.
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;
227 wxGCPool[wxGCPoolSize].m_used = true;
228
229 // Set new value of pool size.
230 wxGCPoolSize += GC_POOL_ALLOC_SIZE;
231
232 // Return newly allocated entry.
233 return wxGCPool[wxGCPoolSize-GC_POOL_ALLOC_SIZE].m_gc;
234 }
235
236 // The realloc failed. Fall through to error.
237 wxFAIL_MSG( wxT("No GC available") );
238
239 return NULL;
240 }
241
242 static void wxFreePoolGC( GdkGC *gc )
243 {
244 for (int i = 0; i < wxGCPoolSize; i++)
245 {
246 if (wxGCPool[i].m_gc == gc)
247 {
248 wxGCPool[i].m_used = false;
249 return;
250 }
251 }
252
253 wxFAIL_MSG( wxT("Wrong GC") );
254 }
255
256 //-----------------------------------------------------------------------------
257 // wxWindowDC
258 //-----------------------------------------------------------------------------
259
260 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxGTKDCImpl)
261
262 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner ) :
263 wxGTKDCImpl( owner )
264 {
265 m_gdkwindow = NULL;
266 m_penGC = NULL;
267 m_brushGC = NULL;
268 m_textGC = NULL;
269 m_bgGC = NULL;
270 m_cmap = NULL;
271 m_isScreenDC = false;
272 m_context = NULL;
273 m_layout = NULL;
274 m_fontdesc = NULL;
275 }
276
277 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window ) :
278 wxGTKDCImpl( owner )
279 {
280 wxASSERT_MSG( window, wxT("DC needs a window") );
281
282 m_gdkwindow = NULL;
283 m_penGC = NULL;
284 m_brushGC = NULL;
285 m_textGC = NULL;
286 m_bgGC = NULL;
287 m_cmap = NULL;
288 m_isScreenDC = false;
289 m_font = window->GetFont();
290
291 GtkWidget *widget = window->m_wxwindow;
292 m_gdkwindow = window->GTKGetDrawingWindow();
293
294 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
295 // code should still be able to create wxClientDCs for them
296 if ( !widget )
297 {
298 widget = window->m_widget;
299
300 wxCHECK_RET(widget, "DC needs a widget");
301
302 m_gdkwindow = widget->window;
303 if (!gtk_widget_get_has_window(widget))
304 SetDeviceLocalOrigin(widget->allocation.x, widget->allocation.y);
305 }
306
307 m_context = window->GTKGetPangoDefaultContext();
308 m_layout = pango_layout_new( m_context );
309 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
310
311 // Window not realized ?
312 if (!m_gdkwindow)
313 {
314 // Don't report problems as per MSW.
315 m_ok = true;
316
317 return;
318 }
319
320 m_cmap = gtk_widget_get_colormap(widget);
321
322 SetUpDC();
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) */
330
331 m_window = window;
332
333 if (m_window && m_window->m_wxwindow &&
334 (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
335 {
336 // reverse sense
337 m_signX = -1;
338
339 // origin in the upper right corner
340 m_deviceOriginX = m_window->GetClientSize().x;
341 }
342 }
343
344 wxWindowDCImpl::~wxWindowDCImpl()
345 {
346 Destroy();
347
348 if (m_layout)
349 g_object_unref (m_layout);
350 if (m_fontdesc)
351 pango_font_description_free( m_fontdesc );
352 }
353
354 void wxWindowDCImpl::SetUpDC( bool isMemDC )
355 {
356 m_ok = true;
357
358 wxASSERT_MSG( !m_penGC, wxT("GCs already created") );
359
360 bool done = false;
361
362 if ((isMemDC) && (GetSelectedBitmap().IsOk()))
363 {
364 if (GetSelectedBitmap().GetDepth() == 1)
365 {
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 );
370 done = true;
371 }
372 }
373
374 if (!done)
375 {
376 if (m_isScreenDC)
377 {
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 );
382 }
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)))
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 }
394 #endif
395 else
396 {
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 );
401 }
402 }
403
404 /* background colour */
405 m_backgroundBrush = *wxWHITE_BRUSH;
406 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
407 const GdkColor *bg_col = m_backgroundBrush.GetColour().GetColor();
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 );
417
418 gdk_gc_set_colormap( m_textGC, m_cmap );
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 );
424
425 gdk_gc_set_line_attributes( m_penGC, 0, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_ROUND );
426
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 );
431
432 gdk_gc_set_fill( m_brushGC, GDK_SOLID );
433
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 );
439
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 );
444
445 /* clipping */
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 );
450 }
451
452 void wxWindowDCImpl::DoGetSize( int* width, int* height ) const
453 {
454 wxCHECK_RET( m_window, wxT("GetSize() doesn't work without window") );
455
456 m_window->GetSize(width, height);
457 }
458
459 bool wxWindowDCImpl::DoFloodFill(wxCoord x, wxCoord y,
460 const wxColour& col, wxFloodFillStyle style)
461 {
462 #if wxUSE_IMAGE
463 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
464 const wxColour & col, wxFloodFillStyle style);
465
466 return wxDoFloodFill( GetOwner(), x, y, col, style);
467 #else
468 wxUnusedVar(x);
469 wxUnusedVar(y);
470 wxUnusedVar(col);
471 wxUnusedVar(style);
472
473 return false;
474 #endif
475 }
476
477 bool wxWindowDCImpl::DoGetPixel( wxCoord x1, wxCoord y1, wxColour *col ) const
478 {
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);
505 return true;
506 }
507
508 void wxWindowDCImpl::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
509 {
510 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
511
512 if ( m_pen.IsNonTransparent() )
513 {
514 if (m_gdkwindow)
515 gdk_draw_line( m_gdkwindow, m_penGC, XLOG2DEV(x1), YLOG2DEV(y1), XLOG2DEV(x2), YLOG2DEV(y2) );
516
517 CalcBoundingBox(x1, y1);
518 CalcBoundingBox(x2, y2);
519 }
520 }
521
522 void wxWindowDCImpl::DoCrossHair( wxCoord x, wxCoord y )
523 {
524 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
525
526 if ( m_pen.IsNonTransparent() )
527 {
528 int w = 0;
529 int h = 0;
530 GetOwner()->GetSize( &w, &h );
531 wxCoord xx = XLOG2DEV(x);
532 wxCoord yy = YLOG2DEV(y);
533 if (m_gdkwindow)
534 {
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) );
537 }
538 }
539 }
540
541 void wxWindowDCImpl::DrawingSetup(GdkGC*& gc, bool& originChanged)
542 {
543 gc = m_brushGC;
544 GdkPixmap* pixmap = NULL;
545 const int style = m_brush.GetStyle();
546
547 if (style == wxBRUSHSTYLE_STIPPLE || style == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE)
548 {
549 const wxBitmap* stipple = m_brush.GetStipple();
550 if (stipple->IsOk())
551 {
552 if (style == wxBRUSHSTYLE_STIPPLE)
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
581 void wxWindowDCImpl::DoDrawArc( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2,
582 wxCoord xc, wxCoord yc )
583 {
584 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
585
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);
592 double dx = xx1 - xxc;
593 double dy = yy1 - yyc;
594 double radius = sqrt((double)(dx*dx+dy*dy));
595 wxCoord r = (wxCoord)radius;
596 double radius1, radius2;
597
598 if (xx1 == xx2 && yy1 == yy2)
599 {
600 radius1 = 0.0;
601 radius2 = 360.0;
602 }
603 else if ( wxIsNullDouble(radius) )
604 {
605 radius1 =
606 radius2 = 0.0;
607 }
608 else
609 {
610 radius1 = (xx1 - xxc == 0) ?
611 (yy1 - yyc < 0) ? 90.0 : -90.0 :
612 -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
613 radius2 = (xx2 - xxc == 0) ?
614 (yy2 - yyc < 0) ? 90.0 : -90.0 :
615 -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
616 }
617 wxCoord alpha1 = wxCoord(radius1 * 64.0);
618 wxCoord alpha2 = wxCoord((radius2 - radius1) * 64.0);
619 while (alpha2 <= 0) alpha2 += 360*64;
620 while (alpha1 > 360*64) alpha1 -= 360*64;
621
622 if (m_gdkwindow)
623 {
624 if ( m_brush.IsNonTransparent() )
625 {
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);
634 }
635
636 if ( m_pen.IsNonTransparent() )
637 {
638 gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
639
640 if ( m_brush.IsNonTransparent() && (alpha2 - alpha1 != 360*64) )
641 {
642 gdk_draw_line( m_gdkwindow, m_penGC, xx1, yy1, xxc, yyc );
643 gdk_draw_line( m_gdkwindow, m_penGC, xxc, yyc, xx2, yy2 );
644 }
645 }
646 }
647
648 CalcBoundingBox (x1, y1);
649 CalcBoundingBox (x2, y2);
650 }
651
652 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double sa, double ea )
653 {
654 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
655
656 wxCoord xx = XLOG2DEV(x);
657 wxCoord yy = YLOG2DEV(y);
658 wxCoord ww = m_signX * XLOG2DEVREL(width);
659 wxCoord hh = m_signY * YLOG2DEVREL(height);
660
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; }
664
665 if (m_gdkwindow)
666 {
667 wxCoord start = wxCoord(sa * 64.0);
668 wxCoord end = wxCoord((ea-sa) * 64.0);
669
670 if ( m_brush.IsNonTransparent() )
671 {
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);
680 }
681
682 if ( m_pen.IsNonTransparent() )
683 gdk_draw_arc( m_gdkwindow, m_penGC, FALSE, xx, yy, ww, hh, start, end );
684 }
685
686 CalcBoundingBox (x, y);
687 CalcBoundingBox (x + width, y + height);
688 }
689
690 void wxWindowDCImpl::DoDrawPoint( wxCoord x, wxCoord y )
691 {
692 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
693
694 if ( m_pen.IsNonTransparent() && m_gdkwindow )
695 gdk_draw_point( m_gdkwindow, m_penGC, XLOG2DEV(x), YLOG2DEV(y) );
696
697 CalcBoundingBox (x, y);
698 }
699
700 void wxWindowDCImpl::DoDrawLines( int n, const wxPoint points[], wxCoord xoffset, wxCoord yoffset )
701 {
702 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
703
704 if (n <= 0) return;
705
706 if ( m_pen.IsTransparent() )
707 return;
708
709 //Check, if scaling is necessary
710 const bool doScale =
711 xoffset != 0 || yoffset != 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
712
713 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
714 const GdkPoint* gpts = reinterpret_cast<const GdkPoint*>(points);
715 GdkPoint* gpts_alloc = NULL;
716
717 if (doScale)
718 {
719 gpts_alloc = new GdkPoint[n];
720 gpts = gpts_alloc;
721 }
722
723 for (int i = 0; i < n; i++)
724 {
725 if (doScale)
726 {
727 gpts_alloc[i].x = XLOG2DEV(points[i].x + xoffset);
728 gpts_alloc[i].y = YLOG2DEV(points[i].y + yoffset);
729 }
730 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
731 }
732
733 if (m_gdkwindow)
734 gdk_draw_lines( m_gdkwindow, m_penGC, gpts, n);
735
736 delete[] gpts_alloc;
737 }
738
739 void wxWindowDCImpl::DoDrawPolygon( int n, const wxPoint points[],
740 wxCoord xoffset, wxCoord yoffset,
741 wxPolygonFillMode WXUNUSED(fillStyle) )
742 {
743 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
744
745 if (n <= 0) return;
746
747 //Check, if scaling is necessary
748 const bool doScale =
749 xoffset != 0 || yoffset != 0 || XLOG2DEV(10) != 10 || YLOG2DEV(10) != 10;
750
751 // GdkPoint and wxPoint have the same memory layout, so we can cast one to the other
752 const GdkPoint* gdkpoints = reinterpret_cast<const GdkPoint*>(points);
753 GdkPoint* gdkpoints_alloc = NULL;
754
755 if (doScale)
756 {
757 gdkpoints_alloc = new GdkPoint[n];
758 gdkpoints = gdkpoints_alloc;
759 }
760
761 int i;
762 for (i = 0 ; i < n ; i++)
763 {
764 if (doScale)
765 {
766 gdkpoints_alloc[i].x = XLOG2DEV(points[i].x + xoffset);
767 gdkpoints_alloc[i].y = YLOG2DEV(points[i].y + yoffset);
768 }
769 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
770 }
771
772 if (m_gdkwindow)
773 {
774 if ( m_brush.IsNonTransparent() )
775 {
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);
784 }
785
786 if ( m_pen.IsNonTransparent() )
787 {
788 /*
789 for (i = 0 ; i < n ; i++)
790 {
791 gdk_draw_line( m_gdkwindow, m_penGC,
792 gdkpoints[i%n].x,
793 gdkpoints[i%n].y,
794 gdkpoints[(i+1)%n].x,
795 gdkpoints[(i+1)%n].y);
796 }
797 */
798 gdk_draw_polygon( m_gdkwindow, m_penGC, FALSE, gdkpoints, n );
799
800 }
801 }
802
803 delete[] gdkpoints_alloc;
804 }
805
806 void wxWindowDCImpl::DoDrawRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
807 {
808 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
809
810 wxCoord xx = XLOG2DEV(x);
811 wxCoord yy = YLOG2DEV(y);
812 wxCoord ww = m_signX * XLOG2DEVREL(width);
813 wxCoord hh = m_signY * YLOG2DEVREL(height);
814
815 // CMB: draw nothing if transformed w or h is 0
816 if (ww == 0 || hh == 0) return;
817
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; }
821
822 if (m_gdkwindow)
823 {
824 if ( m_brush.IsNonTransparent() )
825 {
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);
834 }
835
836 if ( m_pen.IsNonTransparent() )
837 {
838 if ((m_pen.GetWidth() == 2) && (m_pen.GetCap() == wxCAP_ROUND) &&
839 (m_pen.GetJoin() == wxJOIN_ROUND) && (m_pen.GetStyle() == wxPENSTYLE_SOLID))
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
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 );
849 }
850 else
851 {
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 );
854 }
855
856 // reset
857 gdk_gc_set_line_attributes( m_penGC, 2, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
858 }
859 else
860 {
861 // Just use X11 for other cases
862 gdk_draw_rectangle( m_gdkwindow, m_penGC, FALSE, xx, yy, ww-1, hh-1 );
863 }
864 }
865 }
866
867 CalcBoundingBox( x, y );
868 CalcBoundingBox( x + width, y + height );
869 }
870
871 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius )
872 {
873 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
874
875 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
876
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);
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 {
891 DoDrawRectangle( x, y, width, height );
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
900 if ( m_pen.IsNonTransparent() )
901 {
902 ww--;
903 hh--;
904 }
905
906 if (m_gdkwindow)
907 {
908 // CMB: ensure dd is not larger than rectangle otherwise we
909 // get an hour glass shape
910 wxCoord dd = 2 * rr;
911 if (dd > ww) dd = ww;
912 if (dd > hh) dd = hh;
913 rr = dd / 2;
914
915 if ( m_brush.IsNonTransparent() )
916 {
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);
930 }
931
932 if ( m_pen.IsNonTransparent() )
933 {
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 );
942 }
943 }
944
945 // this ignores the radius
946 CalcBoundingBox( x, y );
947 CalcBoundingBox( x + width, y + height );
948 }
949
950 void wxWindowDCImpl::DoDrawEllipse( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
951 {
952 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
953
954 wxCoord xx = XLOG2DEV(x);
955 wxCoord yy = YLOG2DEV(y);
956 wxCoord ww = m_signX * XLOG2DEVREL(width);
957 wxCoord hh = m_signY * YLOG2DEVREL(height);
958
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; }
962
963 if (m_gdkwindow)
964 {
965 if ( m_brush.IsNonTransparent() )
966 {
967 GdkGC* gc;
968 bool originChanged;
969 DrawingSetup(gc, originChanged);
970
971 // If the pen is transparent pen we increase the size
972 // for better compatibility with other platforms.
973 if (m_pen.IsTransparent())
974 {
975 ++ww;
976 ++hh;
977 }
978
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);
983 }
984
985 if ( m_pen.IsNonTransparent() )
986 gdk_draw_arc( m_gdkwindow, m_penGC, false, xx, yy, ww, hh, 0, 360*64 );
987 }
988
989 CalcBoundingBox( x, y );
990 CalcBoundingBox( x + width, y + height );
991 }
992
993 void wxWindowDCImpl::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
994 {
995 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
996 DoDrawBitmap( (const wxBitmap&)icon, x, y, true );
997 }
998
999 // scale a pixbuf
1000 static GdkPixbuf*
1001 Scale(GdkPixbuf* pixbuf, int dst_w, int dst_h, double sx, double sy)
1002 {
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);
1007 return pixbuf_scaled;
1008 }
1009
1010 // scale part of a pixmap using pixbuf scaling
1011 static GdkPixbuf*
1012 Scale(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);
1016 GdkPixbuf* pixbuf2 = Scale(pixbuf, dst_w, dst_h, sx, sy);
1017 g_object_unref(pixbuf);
1018 return pixbuf2;
1019 }
1020
1021 // scale part of a mask pixmap
1022 static GdkPixmap*
1023 ScaleMask(GdkPixmap* mask, int x, int y, int w, int h, int dst_w, int dst_h, double sx, double sy)
1024 {
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;
1045 return pixmap;
1046 }
1047
1048 // Make a new mask from part of a mask and a clip region.
1049 static GdkPixmap*
1050 ClipMask(GdkPixmap* mask, GdkRegion* clipRegion, int x, int y, int dst_x, int dst_y, int w, int h)
1051 {
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);
1063 return pixmap;
1064 }
1065
1066 // make a color pixmap from part of a mono one, using text fg/bg colors
1067 GdkPixmap*
1068 wxWindowDCImpl::MonoToColor(GdkPixmap* monoPixmap, int x, int y, int w, int h) const
1069 {
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;
1084 }
1085
1086 void wxWindowDCImpl::DoDrawBitmap( const wxBitmap &bitmap,
1087 wxCoord x, wxCoord y,
1088 bool useMask )
1089 {
1090 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1091 wxCHECK_RET( bitmap.IsOk(), wxT("invalid bitmap") );
1092
1093 if (!m_gdkwindow) return;
1094
1095 const int w = bitmap.GetWidth();
1096 const int h = bitmap.GetHeight();
1097
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
1101 CalcBoundingBox(x, y);
1102 CalcBoundingBox(x + m_signX*w, y + m_signY*h);
1103
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);
1109
1110 if (m_window && m_window->GetLayoutDirection() == wxLayout_RightToLeft)
1111 xx -= ww;
1112
1113 GdkRegion* const clipRegion = m_currentClippingRegion.GetRegion();
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 }
1122
1123 const bool isScaled = ww != w || hh != h;
1124 const bool hasAlpha = bitmap.HasAlpha();
1125 GdkGC* const use_gc = m_penGC;
1126
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)
1136 {
1137 GdkPixmap* mask_new = NULL;
1138 if (isScaled)
1139 {
1140 mask = ScaleMask(mask, 0, 0, w, h, ww, hh, m_scaleX, m_scaleY);
1141 mask_new = mask;
1142 }
1143 if (overlap == wxPartRegion)
1144 {
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);
1148 if (mask_new)
1149 g_object_unref(mask_new);
1150 mask_new = mask;
1151 }
1152 gdk_gc_set_clip_mask(use_gc, mask);
1153 gdk_gc_set_clip_origin(use_gc, xx, yy);
1154 if (mask_new)
1155 g_object_unref(mask_new);
1156 }
1157
1158 // determine whether to use pixmap or pixbuf
1159 GdkPixmap* pixmap = NULL;
1160 GdkPixmap* pixmap_new = NULL;
1161 GdkPixbuf* pixbuf = NULL;
1162 GdkPixbuf* pixbuf_new = NULL;
1163 if (bitmap.HasPixmap())
1164 pixmap = bitmap.GetPixmap();
1165 if (pixmap && gdk_drawable_get_depth(pixmap) == 1)
1166 {
1167 // convert mono pixmap to color using text fg/bg colors
1168 pixmap = MonoToColor(pixmap, 0, 0, w, h);
1169 pixmap_new = pixmap;
1170 }
1171 else if (hasAlpha || pixmap == NULL)
1172 pixbuf = bitmap.GetPixbuf();
1173
1174 if (isScaled)
1175 {
1176 if (pixbuf)
1177 pixbuf = Scale(pixbuf, ww, hh, m_scaleX, m_scaleY);
1178 else
1179 pixbuf = Scale(pixmap, 0, 0, w, h, ww, hh, m_scaleX, m_scaleY);
1180
1181 pixbuf_new = pixbuf;
1182 }
1183
1184 if (pixbuf)
1185 {
1186 gdk_draw_pixbuf(m_gdkwindow, use_gc, pixbuf,
1187 0, 0, xx, yy, ww, hh, GDK_RGB_DITHER_NORMAL, 0, 0);
1188 }
1189 else
1190 {
1191 gdk_draw_drawable(m_gdkwindow, use_gc, pixmap, 0, 0, xx, yy, ww, hh);
1192 }
1193
1194 if (pixbuf_new)
1195 g_object_unref(pixbuf_new);
1196 if (pixmap_new)
1197 g_object_unref(pixmap_new);
1198 if (mask)
1199 gdk_gc_set_clip_region(use_gc, clipRegion);
1200 }
1201
1202 bool wxWindowDCImpl::DoBlit( wxCoord xdest, wxCoord ydest,
1203 wxCoord width, wxCoord height,
1204 wxDC *source,
1205 wxCoord xsrc, wxCoord ysrc,
1206 wxRasterOperationMode logical_func,
1207 bool useMask,
1208 wxCoord xsrcMask, wxCoord ysrcMask )
1209 {
1210 wxCHECK_MSG( IsOk(), false, wxT("invalid window dc") );
1211 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1212
1213 if (!m_gdkwindow) return false;
1214
1215 GdkDrawable* srcDrawable = NULL;
1216 GdkPixmap* mask = NULL;
1217 wxMemoryDC* memDC = wxDynamicCast(source, wxMemoryDC);
1218 if (memDC)
1219 {
1220 const wxBitmap& bitmap = memDC->GetSelectedBitmap();
1221 if (!bitmap.IsOk())
1222 return false;
1223 srcDrawable = bitmap.GetPixmap();
1224 if (useMask)
1225 {
1226 wxMask* m = bitmap.GetMask();
1227 if (m)
1228 mask = m->GetBitmap();
1229 }
1230 }
1231 else
1232 {
1233 wxDCImpl* impl = source->GetImpl();
1234 wxWindowDCImpl* gtk_impl = wxDynamicCast(impl, wxWindowDCImpl);
1235 if (gtk_impl)
1236 srcDrawable = gtk_impl->GetGDKWindow();
1237 if (srcDrawable == NULL)
1238 return false;
1239 }
1240
1241 CalcBoundingBox(xdest, ydest);
1242 CalcBoundingBox(xdest + width, ydest + height);
1243
1244 // source device coords
1245 int src_x = source->LogicalToDeviceX(xsrc);
1246 int src_y = source->LogicalToDeviceY(ysrc);
1247 int src_w = source->LogicalToDeviceXRel(width);
1248 int src_h = source->LogicalToDeviceYRel(height);
1249
1250 // Clip source rect to source dc.
1251 // Only necessary when scaling, to avoid GDK errors when
1252 // converting to pixbuf, but no harm in always doing it.
1253 // If source rect changes, it also changes the dest rect.
1254 wxRect clip;
1255 gdk_drawable_get_size(srcDrawable, &clip.width, &clip.height);
1256 clip.Intersect(wxRect(src_x, src_y, src_w, src_h));
1257 if (src_w != clip.width || src_h != clip.height)
1258 {
1259 if (clip.width == 0)
1260 return true;
1261
1262 src_w = clip.width;
1263 src_h = clip.height;
1264 width = source->DeviceToLogicalXRel(src_w);
1265 height = source->DeviceToLogicalYRel(src_h);
1266 if (src_x != clip.x || src_y != clip.y)
1267 {
1268 xdest += source->DeviceToLogicalXRel(clip.x - src_x);
1269 ydest += source->DeviceToLogicalYRel(clip.y - src_y);
1270 src_x = clip.x;
1271 src_y = clip.y;
1272 }
1273 }
1274
1275 // destination device coords
1276 const int dst_x = LogicalToDeviceX(xdest);
1277 const int dst_y = LogicalToDeviceY(ydest);
1278 const int dst_w = LogicalToDeviceXRel(width);
1279 const int dst_h = LogicalToDeviceYRel(height);
1280
1281 GdkRegion* const clipRegion = m_currentClippingRegion.GetRegion();
1282 // determine dest clip region overlap
1283 int overlap = wxInRegion;
1284 if (clipRegion)
1285 {
1286 overlap = m_currentClippingRegion.Contains(dst_x, dst_y, dst_w, dst_h);
1287 if (overlap == wxOutRegion)
1288 return true;
1289 }
1290
1291 const bool isScaled = src_w != dst_w || src_h != dst_h;
1292 double scale_x = 0;
1293 double scale_y = 0;
1294 if (isScaled)
1295 {
1296 // get source to dest scale
1297 double usx, usy, lsx, lsy;
1298 source->GetUserScale(&usx, &usy);
1299 source->GetLogicalScale(&lsx, &lsy);
1300 scale_x = m_scaleX / (usx * lsx);
1301 scale_y = m_scaleY / (usy * lsy);
1302 }
1303
1304 GdkGC* const use_gc = m_penGC;
1305
1306 if (mask)
1307 {
1308 int srcMask_x = src_x;
1309 int srcMask_y = src_y;
1310 if (xsrcMask != -1 || ysrcMask != -1)
1311 {
1312 srcMask_x = source->LogicalToDeviceX(xsrcMask);
1313 srcMask_y = source->LogicalToDeviceY(ysrcMask);
1314 }
1315 GdkPixmap* mask_new = NULL;
1316 if (isScaled)
1317 {
1318 mask = ScaleMask(mask, srcMask_x, srcMask_y,
1319 src_w, src_h, dst_w, dst_h, scale_x, scale_y);
1320 mask_new = mask;
1321 srcMask_x = 0;
1322 srcMask_y = 0;
1323 }
1324 if (overlap == wxPartRegion)
1325 {
1326 // need a new mask that also masks the clipped area,
1327 // because gc can't have both a mask and a clip region
1328 mask = ClipMask(mask, clipRegion,
1329 srcMask_x, srcMask_y, dst_x, dst_y, dst_w, dst_h);
1330 if (mask_new)
1331 g_object_unref(mask_new);
1332 mask_new = mask;
1333 srcMask_x = 0;
1334 srcMask_y = 0;
1335 }
1336 gdk_gc_set_clip_mask(use_gc, mask);
1337 gdk_gc_set_clip_origin(use_gc, dst_x - srcMask_x, dst_y - srcMask_y);
1338 if (mask_new)
1339 g_object_unref(mask_new);
1340 }
1341
1342 GdkPixmap* pixmap = NULL;
1343 if (gdk_drawable_get_depth(srcDrawable) == 1)
1344 {
1345 // Convert mono pixmap to color using text fg/bg colors.
1346 // Scaling/drawing is simpler if this is done first.
1347 pixmap = MonoToColor(srcDrawable, src_x, src_y, src_w, src_h);
1348 srcDrawable = pixmap;
1349 src_x = 0;
1350 src_y = 0;
1351 }
1352
1353 const wxRasterOperationMode logical_func_save = m_logicalFunction;
1354 SetLogicalFunction(logical_func);
1355 if (memDC == NULL)
1356 gdk_gc_set_subwindow(use_gc, GDK_INCLUDE_INFERIORS);
1357
1358 if (isScaled)
1359 {
1360 GdkPixbuf* pixbuf = Scale(srcDrawable,
1361 src_x, src_y, src_w, src_h, dst_w, dst_h, scale_x, scale_y);
1362 gdk_draw_pixbuf(m_gdkwindow, use_gc, pixbuf,
1363 0, 0, dst_x, dst_y, dst_w, dst_h, GDK_RGB_DITHER_NONE, 0, 0);
1364 g_object_unref(pixbuf);
1365 }
1366 else
1367 {
1368 gdk_draw_drawable(m_gdkwindow, use_gc, srcDrawable,
1369 src_x, src_y, dst_x, dst_y, dst_w, dst_h);
1370 }
1371
1372 SetLogicalFunction(logical_func_save);
1373 if (memDC == NULL)
1374 gdk_gc_set_subwindow(use_gc, GDK_CLIP_BY_CHILDREN);
1375
1376 if (pixmap)
1377 g_object_unref(pixmap);
1378 if (mask)
1379 gdk_gc_set_clip_region(use_gc, clipRegion);
1380
1381 return true;
1382 }
1383
1384 void wxWindowDCImpl::DoDrawText(const wxString& text,
1385 wxCoord xLogical,
1386 wxCoord yLogical)
1387 {
1388 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1389
1390 if (!m_gdkwindow) return;
1391
1392 if (text.empty()) return;
1393
1394 wxCoord x = XLOG2DEV(xLogical),
1395 y = YLOG2DEV(yLogical);
1396
1397 wxCHECK_RET( m_context, wxT("no Pango context") );
1398 wxCHECK_RET( m_layout, wxT("no Pango layout") );
1399 wxCHECK_RET( m_fontdesc, wxT("no Pango font description") );
1400
1401 gdk_pango_context_set_colormap( m_context, m_cmap ); // not needed in gtk+ >= 2.6
1402
1403 wxCharBuffer data = wxGTK_CONV(text);
1404 if ( !data )
1405 return;
1406
1407 pango_layout_set_text(m_layout, data, data.length());
1408 const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout);
1409
1410 int oldSize = 0;
1411 const bool isScaled = fabs(m_scaleY - 1.0) > 0.00001;
1412 if (isScaled)
1413 {
1414 // If there is a user or actually any scale applied to
1415 // the device context, scale the font.
1416
1417 // scale font description
1418 oldSize = pango_font_description_get_size(m_fontdesc);
1419 pango_font_description_set_size(m_fontdesc, int(oldSize * m_scaleY));
1420
1421 // actually apply scaled font
1422 pango_layout_set_font_description( m_layout, m_fontdesc );
1423 }
1424
1425 int w, h;
1426 pango_layout_get_pixel_size(m_layout, &w, &h);
1427
1428 // Draw layout.
1429 int x_rtl = x;
1430 if (m_window && m_window->GetLayoutDirection() == wxLayout_RightToLeft)
1431 x_rtl -= w;
1432
1433 const GdkColor* bg_col = NULL;
1434 if (m_backgroundMode == wxBRUSHSTYLE_SOLID)
1435 bg_col = m_textBackgroundColour.GetColor();
1436
1437 gdk_draw_layout_with_colors(m_gdkwindow, m_textGC, x_rtl, y, m_layout, NULL, bg_col);
1438
1439 if (isScaled)
1440 {
1441 // reset unscaled size
1442 pango_font_description_set_size( m_fontdesc, oldSize );
1443
1444 // actually apply unscaled font
1445 pango_layout_set_font_description( m_layout, m_fontdesc );
1446 }
1447 if (setAttrs)
1448 {
1449 // undo underline attributes setting:
1450 pango_layout_set_attributes(m_layout, NULL);
1451 }
1452
1453 CalcBoundingBox(xLogical + int(w / m_scaleX), yLogical + int(h / m_scaleY));
1454 CalcBoundingBox(xLogical, yLogical);
1455 }
1456
1457 // TODO: When GTK2.6 is required, merge DoDrawText and DoDrawRotatedText to
1458 // avoid code duplication
1459 void wxWindowDCImpl::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, double angle )
1460 {
1461 if (!m_gdkwindow || text.empty())
1462 return;
1463
1464 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1465
1466 x = XLOG2DEV(x);
1467 y = YLOG2DEV(y);
1468
1469 pango_layout_set_text(m_layout, wxGTK_CONV(text), -1);
1470 const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout);
1471 int oldSize = 0;
1472 const bool isScaled = fabs(m_scaleY - 1.0) > 0.00001;
1473 if (isScaled)
1474 {
1475 //TODO: when Pango >= 1.6 is required, use pango_matrix_scale()
1476 // If there is a user or actually any scale applied to
1477 // the device context, scale the font.
1478
1479 // scale font description
1480 oldSize = pango_font_description_get_size(m_fontdesc);
1481 pango_font_description_set_size(m_fontdesc, int(oldSize * m_scaleY));
1482
1483 // actually apply scaled font
1484 pango_layout_set_font_description( m_layout, m_fontdesc );
1485 }
1486
1487 int w, h;
1488 pango_layout_get_pixel_size(m_layout, &w, &h);
1489
1490 const GdkColor* bg_col = NULL;
1491 if (m_backgroundMode == wxBRUSHSTYLE_SOLID)
1492 bg_col = m_textBackgroundColour.GetColor();
1493
1494 // rotate the text
1495 PangoMatrix matrix = PANGO_MATRIX_INIT;
1496 pango_matrix_rotate (&matrix, angle);
1497 pango_context_set_matrix (m_context, &matrix);
1498 pango_layout_context_changed (m_layout);
1499
1500 // To be compatible with MSW, the rotation axis must be in the old
1501 // top-left corner.
1502 // Calculate the vertices of the rotated rectangle containing the text,
1503 // relative to the old top-left vertex.
1504 // We could use the matrix for this, but it's simpler with trignonometry.
1505 double rad = DegToRad(angle);
1506 // the rectangle vertices are counted clockwise with the first one
1507 // being at (0, 0)
1508 double x2 = w * cos(rad);
1509 double y2 = -w * sin(rad); // y axis points to the bottom, hence minus
1510 double x4 = h * sin(rad);
1511 double y4 = h * cos(rad);
1512 double x3 = x4 + x2;
1513 double y3 = y4 + y2;
1514 // Then we calculate max and min of the rotated rectangle.
1515 wxCoord maxX = (wxCoord)(dmax(dmax(0, x2), dmax(x3, x4)) + 0.5),
1516 maxY = (wxCoord)(dmax(dmax(0, y2), dmax(y3, y4)) + 0.5),
1517 minX = (wxCoord)(dmin(dmin(0, x2), dmin(x3, x4)) - 0.5),
1518 minY = (wxCoord)(dmin(dmin(0, y2), dmin(y3, y4)) - 0.5);
1519
1520 gdk_draw_layout_with_colors(m_gdkwindow, m_textGC, x+minX, y+minY,
1521 m_layout, NULL, bg_col);
1522
1523 if (setAttrs)
1524 pango_layout_set_attributes(m_layout, NULL);
1525
1526 // clean up the transformation matrix
1527 pango_context_set_matrix(m_context, NULL);
1528
1529 if (isScaled)
1530 {
1531 // reset unscaled size
1532 pango_font_description_set_size( m_fontdesc, oldSize );
1533
1534 // actually apply unscaled font
1535 pango_layout_set_font_description( m_layout, m_fontdesc );
1536 }
1537
1538 CalcBoundingBox(x+minX, y+minY);
1539 CalcBoundingBox(x+maxX, y+maxY);
1540 }
1541
1542 void wxWindowDCImpl::DoGetTextExtent(const wxString &string,
1543 wxCoord *width, wxCoord *height,
1544 wxCoord *descent, wxCoord *externalLeading,
1545 const wxFont *theFont) const
1546 {
1547 // ensure we work with a valid font
1548 const wxFont *fontToUse;
1549 if ( !theFont || !theFont->IsOk() )
1550 fontToUse = &m_font;
1551 else
1552 fontToUse = theFont;
1553
1554 wxCHECK_RET( fontToUse->IsOk(), wxT("invalid font") );
1555
1556 wxTextMeasure txm(GetOwner(), fontToUse);
1557 txm.GetTextExtent(string, width, height, descent, externalLeading);
1558 }
1559
1560
1561 bool wxWindowDCImpl::DoGetPartialTextExtents(const wxString& text,
1562 wxArrayInt& widths) const
1563 {
1564 wxCHECK_MSG( m_font.IsOk(), false, wxT("Invalid font") );
1565
1566 wxTextMeasure txm(GetOwner(), &m_font);
1567 return txm.GetPartialTextExtents(text, widths, m_scaleX);
1568 }
1569
1570
1571 wxCoord wxWindowDCImpl::GetCharWidth() const
1572 {
1573 pango_layout_set_text( m_layout, "H", 1 );
1574 int w;
1575 pango_layout_get_pixel_size( m_layout, &w, NULL );
1576 return w;
1577 }
1578
1579 wxCoord wxWindowDCImpl::GetCharHeight() const
1580 {
1581 PangoFontMetrics *metrics = pango_context_get_metrics (m_context, m_fontdesc, pango_context_get_language(m_context));
1582 wxCHECK_MSG( metrics, -1, wxT("failed to get pango font metrics") );
1583
1584 wxCoord h = PANGO_PIXELS (pango_font_metrics_get_descent (metrics) +
1585 pango_font_metrics_get_ascent (metrics));
1586 pango_font_metrics_unref (metrics);
1587 return h;
1588 }
1589
1590 void wxWindowDCImpl::Clear()
1591 {
1592 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1593
1594 if (!m_gdkwindow) return;
1595
1596 int width,height;
1597 DoGetSize( &width, &height );
1598 gdk_draw_rectangle( m_gdkwindow, m_bgGC, TRUE, 0, 0, width, height );
1599 }
1600
1601 void wxWindowDCImpl::SetFont( const wxFont &font )
1602 {
1603 m_font = font;
1604
1605 if (m_font.IsOk())
1606 {
1607 if (m_fontdesc)
1608 pango_font_description_free( m_fontdesc );
1609
1610 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1611
1612
1613 if (m_window)
1614 {
1615 PangoContext *oldContext = m_context;
1616
1617 m_context = m_window->GTKGetPangoDefaultContext();
1618
1619 // If we switch back/forth between different contexts
1620 // we also have to create a new layout. I think so,
1621 // at least, and it doesn't hurt to do it.
1622 if (oldContext != m_context)
1623 {
1624 if (m_layout)
1625 g_object_unref (m_layout);
1626
1627 m_layout = pango_layout_new( m_context );
1628 }
1629 }
1630
1631 pango_layout_set_font_description( m_layout, m_fontdesc );
1632 }
1633 }
1634
1635 void wxWindowDCImpl::SetPen( const wxPen &pen )
1636 {
1637 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1638
1639 if (m_pen == pen) return;
1640
1641 m_pen = pen;
1642
1643 if (!m_pen.IsOk()) return;
1644
1645 if (!m_gdkwindow) return;
1646
1647 gint width = m_pen.GetWidth();
1648 if (width <= 0)
1649 {
1650 // CMB: if width is non-zero scale it with the dc
1651 width = 1;
1652 }
1653 else
1654 {
1655 // X doesn't allow different width in x and y and so we take
1656 // the average
1657 double w = 0.5 +
1658 ( fabs((double) XLOG2DEVREL(width)) +
1659 fabs((double) YLOG2DEVREL(width)) ) / 2.0;
1660 width = (int)w;
1661 if ( !width )
1662 {
1663 // width can't be 0 or an internal GTK error occurs inside
1664 // gdk_gc_set_dashes() below
1665 width = 1;
1666 }
1667 }
1668
1669 static const wxGTKDash dotted[] = {1, 1};
1670 static const wxGTKDash short_dashed[] = {2, 2};
1671 static const wxGTKDash wxCoord_dashed[] = {2, 4};
1672 static const wxGTKDash dotted_dashed[] = {3, 3, 1, 3};
1673
1674 // We express dash pattern in pen width unit, so we are
1675 // independent of zoom factor and so on...
1676 int req_nb_dash;
1677 const wxGTKDash *req_dash;
1678
1679 GdkLineStyle lineStyle = GDK_LINE_ON_OFF_DASH;
1680 switch (m_pen.GetStyle())
1681 {
1682 case wxPENSTYLE_USER_DASH:
1683 req_nb_dash = m_pen.GetDashCount();
1684 req_dash = (wxGTKDash*)m_pen.GetDash();
1685 break;
1686 case wxPENSTYLE_DOT:
1687 req_nb_dash = 2;
1688 req_dash = dotted;
1689 break;
1690 case wxPENSTYLE_LONG_DASH:
1691 req_nb_dash = 2;
1692 req_dash = wxCoord_dashed;
1693 break;
1694 case wxPENSTYLE_SHORT_DASH:
1695 req_nb_dash = 2;
1696 req_dash = short_dashed;
1697 break;
1698 case wxPENSTYLE_DOT_DASH:
1699 req_nb_dash = 4;
1700 req_dash = dotted_dashed;
1701 break;
1702
1703 case wxPENSTYLE_TRANSPARENT:
1704 case wxPENSTYLE_STIPPLE_MASK_OPAQUE:
1705 case wxPENSTYLE_STIPPLE:
1706 case wxPENSTYLE_SOLID:
1707 default:
1708 lineStyle = GDK_LINE_SOLID;
1709 req_dash = NULL;
1710 req_nb_dash = 0;
1711 break;
1712 }
1713
1714 if (req_dash && req_nb_dash)
1715 {
1716 wxGTKDash *real_req_dash = new wxGTKDash[req_nb_dash];
1717 if (real_req_dash)
1718 {
1719 for (int i = 0; i < req_nb_dash; i++)
1720 real_req_dash[i] = req_dash[i] * width;
1721 gdk_gc_set_dashes( m_penGC, 0, real_req_dash, req_nb_dash );
1722 delete[] real_req_dash;
1723 }
1724 else
1725 {
1726 // No Memory. We use non-scaled dash pattern...
1727 gdk_gc_set_dashes( m_penGC, 0, (wxGTKDash*)req_dash, req_nb_dash );
1728 }
1729 }
1730
1731 GdkCapStyle capStyle = GDK_CAP_ROUND;
1732 switch (m_pen.GetCap())
1733 {
1734 case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
1735 case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }
1736 case wxCAP_ROUND:
1737 default:
1738 if (width <= 1)
1739 {
1740 width = 0;
1741 capStyle = GDK_CAP_NOT_LAST;
1742 }
1743 break;
1744 }
1745
1746 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
1747 switch (m_pen.GetJoin())
1748 {
1749 case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
1750 case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
1751 case wxJOIN_ROUND:
1752 default: { joinStyle = GDK_JOIN_ROUND; break; }
1753 }
1754
1755 gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
1756
1757 m_pen.GetColour().CalcPixel( m_cmap );
1758 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
1759 }
1760
1761 void wxWindowDCImpl::SetBrush( const wxBrush &brush )
1762 {
1763 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1764
1765 if (m_brush == brush) return;
1766
1767 m_brush = brush;
1768
1769 if (!m_brush.IsOk()) return;
1770
1771 if (!m_gdkwindow) return;
1772
1773 m_brush.GetColour().CalcPixel( m_cmap );
1774 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
1775
1776 gdk_gc_set_fill( m_brushGC, GDK_SOLID );
1777
1778 if ((m_brush.GetStyle() == wxBRUSHSTYLE_STIPPLE) && (m_brush.GetStipple()->IsOk()))
1779 {
1780 if (m_brush.GetStipple()->GetDepth() != 1)
1781 {
1782 gdk_gc_set_fill( m_brushGC, GDK_TILED );
1783 gdk_gc_set_tile( m_brushGC, m_brush.GetStipple()->GetPixmap() );
1784 }
1785 else
1786 {
1787 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
1788 gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() );
1789 }
1790 }
1791
1792 if ((m_brush.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
1793 {
1794 gdk_gc_set_fill( m_textGC, GDK_OPAQUE_STIPPLED);
1795 gdk_gc_set_stipple( m_textGC, m_brush.GetStipple()->GetMask()->GetBitmap() );
1796 }
1797
1798 if (m_brush.IsHatch())
1799 {
1800 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
1801 gdk_gc_set_stipple(m_brushGC, GetHatch(m_brush.GetStyle()));
1802 }
1803 }
1804
1805 void wxWindowDCImpl::SetBackground( const wxBrush &brush )
1806 {
1807 /* CMB 21/7/98: Added SetBackground. Sets background brush
1808 * for Clear() and bg colour for shapes filled with cross-hatch brush */
1809
1810 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1811
1812 if (m_backgroundBrush == brush) return;
1813
1814 m_backgroundBrush = brush;
1815
1816 if (!m_backgroundBrush.IsOk()) return;
1817
1818 if (!m_gdkwindow) return;
1819
1820 wxColor color = m_backgroundBrush.GetColour();
1821 color.CalcPixel(m_cmap);
1822 const GdkColor* gdkColor = color.GetColor();
1823 gdk_gc_set_background(m_brushGC, gdkColor);
1824 gdk_gc_set_background(m_penGC, gdkColor);
1825 gdk_gc_set_background(m_bgGC, gdkColor);
1826 gdk_gc_set_foreground(m_bgGC, gdkColor);
1827
1828
1829 gdk_gc_set_fill( m_bgGC, GDK_SOLID );
1830
1831 if (m_backgroundBrush.GetStyle() == wxBRUSHSTYLE_STIPPLE)
1832 {
1833 const wxBitmap* stipple = m_backgroundBrush.GetStipple();
1834 if (stipple->IsOk())
1835 {
1836 if (stipple->GetDepth() != 1)
1837 {
1838 gdk_gc_set_fill(m_bgGC, GDK_TILED);
1839 gdk_gc_set_tile(m_bgGC, stipple->GetPixmap());
1840 }
1841 else
1842 {
1843 gdk_gc_set_fill(m_bgGC, GDK_STIPPLED);
1844 gdk_gc_set_stipple(m_bgGC, stipple->GetPixmap());
1845 }
1846 }
1847 }
1848 else if (m_backgroundBrush.IsHatch())
1849 {
1850 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
1851 gdk_gc_set_stipple(m_bgGC, GetHatch(m_backgroundBrush.GetStyle()));
1852 }
1853 }
1854
1855 void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function )
1856 {
1857 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1858
1859 if (m_logicalFunction == function)
1860 return;
1861
1862 // VZ: shouldn't this be a CHECK?
1863 if (!m_gdkwindow)
1864 return;
1865
1866 GdkFunction mode;
1867 switch (function)
1868 {
1869 case wxXOR: mode = GDK_XOR; break;
1870 case wxINVERT: mode = GDK_INVERT; break;
1871 case wxOR_REVERSE: mode = GDK_OR_REVERSE; break;
1872 case wxAND_REVERSE: mode = GDK_AND_REVERSE; break;
1873 case wxCLEAR: mode = GDK_CLEAR; break;
1874 case wxSET: mode = GDK_SET; break;
1875 case wxOR_INVERT: mode = GDK_OR_INVERT; break;
1876 case wxAND: mode = GDK_AND; break;
1877 case wxOR: mode = GDK_OR; break;
1878 case wxEQUIV: mode = GDK_EQUIV; break;
1879 case wxNAND: mode = GDK_NAND; break;
1880 case wxAND_INVERT: mode = GDK_AND_INVERT; break;
1881 case wxCOPY: mode = GDK_COPY; break;
1882 case wxNO_OP: mode = GDK_NOOP; break;
1883 case wxSRC_INVERT: mode = GDK_COPY_INVERT; break;
1884 case wxNOR: mode = GDK_NOR; break;
1885 default:
1886 wxFAIL_MSG("unknown mode");
1887 return;
1888 }
1889
1890 m_logicalFunction = function;
1891
1892 gdk_gc_set_function( m_penGC, mode );
1893 gdk_gc_set_function( m_brushGC, mode );
1894
1895 // to stay compatible with wxMSW, we don't apply ROPs to the text
1896 // operations (i.e. DrawText/DrawRotatedText).
1897 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
1898 gdk_gc_set_function( m_textGC, mode );
1899 }
1900
1901 void wxWindowDCImpl::SetTextForeground( const wxColour &col )
1902 {
1903 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1904
1905 // don't set m_textForegroundColour to an invalid colour as we'd crash
1906 // later then (we use m_textForegroundColour.GetColor() without checking
1907 // in a few places)
1908 if ( !col.IsOk() || (m_textForegroundColour == col) )
1909 return;
1910
1911 m_textForegroundColour = col;
1912
1913 if ( m_gdkwindow )
1914 {
1915 m_textForegroundColour.CalcPixel( m_cmap );
1916 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
1917 }
1918 }
1919
1920 void wxWindowDCImpl::SetTextBackground( const wxColour &col )
1921 {
1922 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1923
1924 // same as above
1925 if ( !col.IsOk() || (m_textBackgroundColour == col) )
1926 return;
1927
1928 m_textBackgroundColour = col;
1929
1930 if ( m_gdkwindow )
1931 {
1932 m_textBackgroundColour.CalcPixel( m_cmap );
1933 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
1934 }
1935 }
1936
1937 void wxWindowDCImpl::SetBackgroundMode( int mode )
1938 {
1939 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1940
1941 m_backgroundMode = mode;
1942 }
1943
1944 void wxWindowDCImpl::SetPalette( const wxPalette& WXUNUSED(palette) )
1945 {
1946 wxFAIL_MSG( wxT("wxWindowDCImpl::SetPalette not implemented") );
1947 }
1948
1949 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
1950 {
1951 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1952
1953 if (!m_gdkwindow) return;
1954
1955 wxRect rect;
1956 rect.x = XLOG2DEV(x);
1957 rect.y = YLOG2DEV(y);
1958 rect.width = XLOG2DEVREL(width);
1959 rect.height = YLOG2DEVREL(height);
1960
1961 if (m_window && m_window->m_wxwindow &&
1962 (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
1963 {
1964 rect.x -= rect.width;
1965 }
1966
1967 DoSetDeviceClippingRegion(wxRegion(rect));
1968 }
1969
1970 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion &region )
1971 {
1972 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
1973
1974 if (region.Empty())
1975 {
1976 DestroyClippingRegion();
1977 return;
1978 }
1979
1980 if (!m_gdkwindow) return;
1981
1982 if (!m_currentClippingRegion.IsNull())
1983 m_currentClippingRegion.Intersect( region );
1984 else
1985 m_currentClippingRegion.Union( region );
1986
1987 #if USE_PAINT_REGION
1988 if (!m_paintClippingRegion.IsNull())
1989 m_currentClippingRegion.Intersect( m_paintClippingRegion );
1990 #endif
1991
1992 wxCoord xx, yy, ww, hh;
1993 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
1994 wxGTKDCImpl::DoSetClippingRegion( xx, yy, ww, hh );
1995
1996 GdkRegion* gdkRegion = m_currentClippingRegion.GetRegion();
1997 gdk_gc_set_clip_region(m_penGC, gdkRegion);
1998 gdk_gc_set_clip_region(m_brushGC, gdkRegion);
1999 gdk_gc_set_clip_region(m_textGC, gdkRegion);
2000 gdk_gc_set_clip_region(m_bgGC, gdkRegion);
2001 }
2002
2003 void wxWindowDCImpl::DestroyClippingRegion()
2004 {
2005 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
2006
2007 wxDCImpl::DestroyClippingRegion();
2008
2009 m_currentClippingRegion.Clear();
2010
2011 #if USE_PAINT_REGION
2012 if (!m_paintClippingRegion.IsEmpty())
2013 m_currentClippingRegion.Union( m_paintClippingRegion );
2014 #endif
2015
2016 if (!m_gdkwindow) return;
2017
2018 GdkRegion* gdkRegion = NULL;
2019 if (!m_currentClippingRegion.IsEmpty())
2020 gdkRegion = m_currentClippingRegion.GetRegion();
2021
2022 gdk_gc_set_clip_region(m_penGC, gdkRegion);
2023 gdk_gc_set_clip_region(m_brushGC, gdkRegion);
2024 gdk_gc_set_clip_region(m_textGC, gdkRegion);
2025 gdk_gc_set_clip_region(m_bgGC, gdkRegion);
2026 }
2027
2028 void wxWindowDCImpl::Destroy()
2029 {
2030 if (m_penGC) wxFreePoolGC( m_penGC );
2031 m_penGC = NULL;
2032 if (m_brushGC) wxFreePoolGC( m_brushGC );
2033 m_brushGC = NULL;
2034 if (m_textGC) wxFreePoolGC( m_textGC );
2035 m_textGC = NULL;
2036 if (m_bgGC) wxFreePoolGC( m_bgGC );
2037 m_bgGC = NULL;
2038 }
2039
2040 void wxWindowDCImpl::SetDeviceOrigin( wxCoord x, wxCoord y )
2041 {
2042 m_deviceOriginX = x;
2043 m_deviceOriginY = y;
2044
2045 ComputeScaleAndOrigin();
2046 }
2047
2048 void wxWindowDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
2049 {
2050 m_signX = (xLeftRight ? 1 : -1);
2051 m_signY = (yBottomUp ? -1 : 1);
2052
2053 if (m_window && m_window->m_wxwindow &&
2054 (m_window->GetLayoutDirection() == wxLayout_RightToLeft))
2055 m_signX = -m_signX;
2056
2057 ComputeScaleAndOrigin();
2058 }
2059
2060 void wxWindowDCImpl::ComputeScaleAndOrigin()
2061 {
2062 const wxRealPoint origScale(m_scaleX, m_scaleY);
2063
2064 wxDCImpl::ComputeScaleAndOrigin();
2065
2066 // if scale has changed call SetPen to recalulate the line width
2067 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.IsOk() )
2068 {
2069 // this is a bit artificial, but we need to force wxDC to think the pen
2070 // has changed
2071 wxPen pen = m_pen;
2072 m_pen = wxNullPen;
2073 SetPen( pen );
2074 }
2075 }
2076
2077 // Resolution in pixels per logical inch
2078 wxSize wxWindowDCImpl::GetPPI() const
2079 {
2080 return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5));
2081 }
2082
2083 int wxWindowDCImpl::GetDepth() const
2084 {
2085 return gdk_drawable_get_depth(m_gdkwindow);
2086 }
2087
2088 //-----------------------------------------------------------------------------
2089 // wxClientDCImpl
2090 //-----------------------------------------------------------------------------
2091
2092 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
2093
2094 wxClientDCImpl::wxClientDCImpl( wxDC *owner )
2095 : wxWindowDCImpl( owner )
2096 {
2097 }
2098
2099 wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *win )
2100 : wxWindowDCImpl( owner, win )
2101 {
2102 wxCHECK_RET( win, wxT("NULL window in wxClientDCImpl::wxClientDC") );
2103
2104 #ifdef __WXUNIVERSAL__
2105 wxPoint ptOrigin = win->GetClientAreaOrigin();
2106 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
2107 wxSize size = win->GetClientSize();
2108 DoSetClippingRegion(0, 0, size.x, size.y);
2109 #endif
2110 // __WXUNIVERSAL__
2111 }
2112
2113 void wxClientDCImpl::DoGetSize(int *width, int *height) const
2114 {
2115 wxCHECK_RET( m_window, wxT("GetSize() doesn't work without window") );
2116
2117 m_window->GetClientSize(width, height);
2118 }
2119
2120 //-----------------------------------------------------------------------------
2121 // wxPaintDCImpl
2122 //-----------------------------------------------------------------------------
2123
2124 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxClientDCImpl)
2125
2126 // Limit the paint region to the window size. Sometimes
2127 // the paint region is too big, and this risks X11 errors
2128 static void wxLimitRegionToSize(wxRegion& region, const wxSize& sz)
2129 {
2130 wxRect originalRect = region.GetBox();
2131 wxRect rect(originalRect);
2132 if (rect.width + rect.x > sz.x)
2133 rect.width = sz.x - rect.x;
2134 if (rect.height + rect.y > sz.y)
2135 rect.height = sz.y - rect.y;
2136 if (rect != originalRect)
2137 {
2138 region = wxRegion(rect);
2139 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2140 originalRect.x, originalRect.y, originalRect.width, originalRect.height,
2141 rect.x, rect.y, rect.width, rect.height);
2142 }
2143 }
2144
2145 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner )
2146 : wxClientDCImpl( owner )
2147 {
2148 }
2149
2150 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *win )
2151 : wxClientDCImpl( owner, win )
2152 {
2153 #if USE_PAINT_REGION
2154 if (!win->m_clipPaintRegion)
2155 return;
2156
2157 wxSize sz = win->GetSize();
2158 m_paintClippingRegion = win->m_nativeUpdateRegion;
2159 wxLimitRegionToSize(m_paintClippingRegion, sz);
2160
2161 GdkRegion *region = m_paintClippingRegion.GetRegion();
2162 if ( region )
2163 {
2164 m_currentClippingRegion.Union( m_paintClippingRegion );
2165 wxLimitRegionToSize(m_currentClippingRegion, sz);
2166
2167 if (sz.x <= 0 || sz.y <= 0)
2168 return ;
2169
2170 gdk_gc_set_clip_region( m_penGC, region );
2171 gdk_gc_set_clip_region( m_brushGC, region );
2172 gdk_gc_set_clip_region( m_textGC, region );
2173 gdk_gc_set_clip_region( m_bgGC, region );
2174 }
2175 #endif
2176 }
2177
2178 // ----------------------------------------------------------------------------
2179 // wxDCModule
2180 // ----------------------------------------------------------------------------
2181
2182 class wxDCModule : public wxModule
2183 {
2184 public:
2185 bool OnInit();
2186 void OnExit();
2187
2188 private:
2189 DECLARE_DYNAMIC_CLASS(wxDCModule)
2190 };
2191
2192 IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
2193
2194 bool wxDCModule::OnInit()
2195 {
2196 wxInitGCPool();
2197 return true;
2198 }
2199
2200 void wxDCModule::OnExit()
2201 {
2202 wxCleanUpGCPool();
2203
2204 for (int i = wxBRUSHSTYLE_LAST_HATCH - wxBRUSHSTYLE_FIRST_HATCH; i--; )
2205 {
2206 if (hatches[i])
2207 g_object_unref(hatches[i]);
2208 }
2209 }