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