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