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