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