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