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