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