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