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