Beginning to make wxDC code compile both before
[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 #if !GTK_CHECK_VERSION(2,2,0)
1117 // NB: We can't render pixbufs with GTK+ < 2.2, we need to use pixmaps code.
1118 // Pixbufs-based bitmaps with alpha channel don't have a mask, so we
1119 // have to call GetPixmap() here -- it converts the pixbuf into pixmap
1120 // and also creates the mask as a side-effect:
1121 use_bitmap.GetPixmap();
1122 #endif
1123
1124 // apply mask if any
1125 GdkBitmap *mask = (GdkBitmap *) NULL;
1126 if (useMask && use_bitmap.GetMask())
1127 mask = use_bitmap.GetMask()->GetBitmap();
1128
1129 GdkGC* use_gc = is_mono ? m_textGC : m_penGC;
1130
1131 GdkBitmap *new_mask = (GdkBitmap*) NULL;
1132
1133 if (mask != NULL)
1134 {
1135 if (!m_currentClippingRegion.IsNull())
1136 {
1137 GdkColor col;
1138 new_mask = gdk_pixmap_new( wxGetRootWindow()->window, ww, hh, 1 );
1139 GdkGC *gc = gdk_gc_new( new_mask );
1140 col.pixel = 0;
1141 gdk_gc_set_foreground( gc, &col );
1142 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, ww, hh );
1143 col.pixel = 0;
1144 gdk_gc_set_background( gc, &col );
1145 col.pixel = 1;
1146 gdk_gc_set_foreground( gc, &col );
1147 gdk_gc_set_clip_region( gc, m_currentClippingRegion.GetRegion() );
1148 gdk_gc_set_clip_origin( gc, -xx, -yy );
1149 gdk_gc_set_fill( gc, GDK_OPAQUE_STIPPLED );
1150 gdk_gc_set_stipple( gc, mask );
1151 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, ww, hh );
1152 mask = new_mask;
1153 g_object_unref (gc);
1154 }
1155
1156 gdk_gc_set_clip_mask(use_gc, mask);
1157 gdk_gc_set_clip_origin(use_gc, xx, yy);
1158 }
1159
1160 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains. For
1161 // drawing a mono-bitmap (XBitmap) we use the current text GC
1162 if (is_mono)
1163 {
1164 GdkPixmap *bitmap2 = gdk_pixmap_new( wxGetRootWindow()->window, ww, hh, -1 );
1165 GdkGC *gc = gdk_gc_new( bitmap2 );
1166 gdk_gc_set_foreground( gc, m_textForegroundColour.GetColor() );
1167 gdk_gc_set_background( gc, m_textBackgroundColour.GetColor() );
1168 gdk_wx_draw_bitmap( bitmap2, gc, use_bitmap.GetPixmap(), 0, 0, 0, 0, -1, -1 );
1169
1170 gdk_draw_drawable(m_window, use_gc, bitmap2, 0, 0, xx, yy, -1, -1);
1171
1172 g_object_unref (bitmap2);
1173 g_object_unref (gc);
1174 }
1175 else
1176 {
1177 #if GTK_CHECK_VERSION(2,2,0)
1178 if (!gtk_check_version(2,2,0) && use_bitmap.HasPixbuf())
1179 {
1180 gdk_draw_pixbuf(m_window, use_gc,
1181 use_bitmap.GetPixbuf(),
1182 0, 0, xx, yy, -1, -1,
1183 GDK_RGB_DITHER_NORMAL, xx, yy);
1184 }
1185 else
1186 #endif
1187 {
1188 gdk_draw_drawable(m_window, use_gc,
1189 use_bitmap.GetPixmap(),
1190 0, 0, xx, yy, -1, -1);
1191 }
1192 }
1193
1194 // remove mask again if any
1195 if (mask != NULL)
1196 {
1197 gdk_gc_set_clip_mask(use_gc, NULL);
1198 gdk_gc_set_clip_origin(use_gc, 0, 0);
1199 if (!m_currentClippingRegion.IsNull())
1200 gdk_gc_set_clip_region(use_gc, m_currentClippingRegion.GetRegion());
1201 if (new_mask != NULL)
1202 g_object_unref(new_mask);
1203 }
1204 }
1205
1206 bool wxGTKWindowImplDC::DoBlit( wxCoord xdest, wxCoord ydest,
1207 wxCoord width, wxCoord height,
1208 wxDC *source,
1209 wxCoord xsrc, wxCoord ysrc,
1210 int logical_func,
1211 bool useMask,
1212 wxCoord xsrcMask, wxCoord ysrcMask )
1213 {
1214 wxCHECK_MSG( Ok(), false, wxT("invalid window dc") );
1215
1216 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1217
1218 if (!m_window) return false;
1219
1220 // transform the source DC coords to the device ones
1221 xsrc = source->LogicalToDeviceX(xsrc);
1222 ysrc = source->LogicalToDeviceY(ysrc);
1223
1224 wxBitmap selected;
1225 wxMemoryDC *memDC = wxDynamicCast(source, wxMemoryDC);
1226 if ( memDC )
1227 {
1228 selected = memDC->GetSelectedBitmap();
1229 if ( !selected.IsOk() )
1230 return false;
1231 }
1232
1233 bool use_bitmap_method = false;
1234 bool is_mono = false;
1235
1236 if (xsrcMask == -1 && ysrcMask == -1)
1237 {
1238 xsrcMask = xsrc;
1239 ysrcMask = ysrc;
1240 }
1241
1242 if (selected.Ok())
1243 {
1244 is_mono = (selected.GetDepth() == 1);
1245
1246 // we use the "XCopyArea" way to copy a memory dc into
1247 // a different window if the memory dc BOTH
1248 // a) doesn't have any mask or its mask isn't used
1249 // b) it is clipped
1250 // c) is not 1-bit
1251
1252 if (useMask && (selected.GetMask()))
1253 {
1254 // we HAVE TO use the direct way for memory dcs
1255 // that have mask since the XCopyArea doesn't know
1256 // about masks
1257 use_bitmap_method = true;
1258 }
1259 else if (is_mono)
1260 {
1261 // we HAVE TO use the direct way for memory dcs
1262 // that are bitmaps because XCopyArea doesn't cope
1263 // with different bit depths
1264 use_bitmap_method = true;
1265 }
1266 else if ((xsrc == 0) && (ysrc == 0) &&
1267 (width == selected.GetWidth()) &&
1268 (height == selected.GetHeight()))
1269 {
1270 // we SHOULD use the direct way if all of the bitmap
1271 // in the memory dc is copied in which case XCopyArea
1272 // wouldn't be able able to boost performace by reducing
1273 // the area to be scaled
1274 use_bitmap_method = true;
1275 }
1276 }
1277
1278 CalcBoundingBox( xdest, ydest );
1279 CalcBoundingBox( xdest + width, ydest + height );
1280
1281 // scale/translate size and position
1282 wxCoord xx = XLOG2DEV(xdest);
1283 wxCoord yy = YLOG2DEV(ydest);
1284
1285 wxCoord ww = XLOG2DEVREL(width);
1286 wxCoord hh = YLOG2DEVREL(height);
1287
1288 // compare to current clipping region
1289 if (!m_currentClippingRegion.IsNull())
1290 {
1291 wxRegion tmp( xx,yy,ww,hh );
1292 tmp.Intersect( m_currentClippingRegion );
1293 if (tmp.IsEmpty())
1294 return true;
1295 }
1296
1297 int old_logical_func = m_logicalFunction;
1298 SetLogicalFunction( logical_func );
1299
1300 if (use_bitmap_method)
1301 {
1302 // scale/translate bitmap size
1303 wxCoord bm_width = selected.GetWidth();
1304 wxCoord bm_height = selected.GetHeight();
1305
1306 // Get clip coords for the bitmap. If we don't
1307 // use wxBitmap::Rescale(), which can clip the
1308 // bitmap, these are the same as the original
1309 // coordinates
1310 wxCoord cx = xx;
1311 wxCoord cy = yy;
1312 wxCoord cw = ww;
1313 wxCoord ch = hh;
1314
1315 // interpret userscale of src too
1316 double xsc,ysc;
1317 memDC->GetUserScale(&xsc,&ysc);
1318 bm_width = (int) (bm_width / xsc);
1319 bm_height = (int) (bm_height / ysc);
1320
1321 wxCoord bm_ww = XLOG2DEVREL( bm_width );
1322 wxCoord bm_hh = YLOG2DEVREL( bm_height );
1323
1324 // Scale bitmap if required
1325 wxBitmap use_bitmap = selected;
1326 if ((selected.GetWidth()!= bm_ww) || ( selected.GetHeight()!= bm_hh))
1327 {
1328 // This indicates that the blitting code below will get
1329 // a clipped bitmap and therefore needs to move the origin
1330 // accordingly
1331 wxRegion tmp( xx,yy,ww,hh );
1332 if (!m_currentClippingRegion.IsNull())
1333 tmp.Intersect( m_currentClippingRegion );
1334 tmp.GetBox(cx,cy,cw,ch);
1335
1336 // Scale and clipped bitmap
1337 use_bitmap = selected.Rescale(cx-xx,cy-yy,cw,ch,bm_ww,bm_hh);
1338 }
1339
1340 // apply mask if any
1341 GdkBitmap *mask = (GdkBitmap *) NULL;
1342 if (useMask && use_bitmap.GetMask())
1343 mask = use_bitmap.GetMask()->GetBitmap();
1344
1345 GdkGC* use_gc = is_mono ? m_textGC : m_penGC;
1346
1347 GdkBitmap *new_mask = (GdkBitmap*) NULL;
1348
1349 if (mask != NULL)
1350 {
1351 if (!m_currentClippingRegion.IsNull())
1352 {
1353 GdkColor col;
1354 new_mask = gdk_pixmap_new( wxGetRootWindow()->window, bm_ww, bm_hh, 1 );
1355 GdkGC *gc = gdk_gc_new( new_mask );
1356 col.pixel = 0;
1357 gdk_gc_set_foreground( gc, &col );
1358 gdk_gc_set_ts_origin( gc, -xsrcMask, -ysrcMask);
1359 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, bm_ww, bm_hh );
1360 col.pixel = 0;
1361 gdk_gc_set_background( gc, &col );
1362 col.pixel = 1;
1363 gdk_gc_set_foreground( gc, &col );
1364 gdk_gc_set_clip_region( gc, m_currentClippingRegion.GetRegion() );
1365 // was: gdk_gc_set_clip_origin( gc, -xx, -yy );
1366 gdk_gc_set_clip_origin( gc, -cx, -cy );
1367 gdk_gc_set_fill( gc, GDK_OPAQUE_STIPPLED );
1368 gdk_gc_set_stipple( gc, mask );
1369 gdk_draw_rectangle( new_mask, gc, TRUE, 0, 0, bm_ww, bm_hh );
1370 mask = new_mask;
1371 g_object_unref (gc);
1372 }
1373
1374 gdk_gc_set_clip_mask(use_gc, mask);
1375 if (new_mask != NULL)
1376 gdk_gc_set_clip_origin(use_gc, cx, cy);
1377 else
1378 gdk_gc_set_clip_origin(use_gc, cx - xsrcMask, cy - ysrcMask);
1379 }
1380
1381 // Draw XPixmap or XBitmap, depending on what the wxBitmap contains. For
1382 // drawing a mono-bitmap (XBitmap) we use the current text GC
1383
1384 if (is_mono)
1385 {
1386 GdkPixmap *bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bm_ww, bm_hh, -1 );
1387 GdkGC *gc = gdk_gc_new( bitmap );
1388 gdk_gc_set_foreground( gc, m_textForegroundColour.GetColor() );
1389 gdk_gc_set_background( gc, m_textBackgroundColour.GetColor() );
1390 gdk_wx_draw_bitmap( bitmap, gc, use_bitmap.GetPixmap(), 0, 0, 0, 0, -1, -1 );
1391
1392 gdk_draw_drawable(m_window, use_gc, bitmap, xsrc, ysrc, cx, cy, cw, ch);
1393
1394 g_object_unref (bitmap);
1395 g_object_unref (gc);
1396 }
1397 else
1398 {
1399 // was: gdk_draw_drawable( m_window, m_penGC, use_bitmap.GetPixmap(), xsrc, ysrc, xx, yy, ww, hh );
1400 gdk_draw_drawable(m_window, use_gc, use_bitmap.GetPixmap(), xsrc, ysrc, cx, cy, cw, ch);
1401 }
1402
1403 // remove mask again if any
1404 if (mask != NULL)
1405 {
1406 gdk_gc_set_clip_mask(use_gc, NULL);
1407 gdk_gc_set_clip_origin(use_gc, 0, 0);
1408 if (!m_currentClippingRegion.IsNull())
1409 gdk_gc_set_clip_region(use_gc, m_currentClippingRegion.GetRegion());
1410 }
1411
1412 if (new_mask)
1413 g_object_unref (new_mask);
1414 }
1415 else // use_bitmap_method
1416 {
1417 if (selected.Ok() && ((width != ww) || (height != hh)))
1418 {
1419 // get clip coords
1420 wxRegion tmp( xx,yy,ww,hh );
1421 tmp.Intersect( m_currentClippingRegion );
1422 wxCoord cx,cy,cw,ch;
1423 tmp.GetBox(cx,cy,cw,ch);
1424
1425 // rescale bitmap
1426 wxBitmap bitmap = selected.Rescale( cx-xx, cy-yy, cw, ch, ww, hh );
1427
1428 // draw scaled bitmap
1429 // was: gdk_draw_drawable( m_window, m_penGC, bitmap.GetPixmap(), 0, 0, xx, yy, -1, -1 );
1430 gdk_draw_drawable( m_window, m_penGC, bitmap.GetPixmap(), 0, 0, cx, cy, -1, -1 );
1431 }
1432 else
1433 {
1434 // No scaling and not a memory dc with a mask either
1435
1436 GdkWindow* window = source->GetGDKWindow();
1437 if ( !window )
1438 return false;
1439
1440 // copy including child window contents
1441 gdk_gc_set_subwindow( m_penGC, GDK_INCLUDE_INFERIORS );
1442 gdk_draw_drawable( m_window, m_penGC,
1443 window,
1444 xsrc, ysrc, xx, yy,
1445 width, height );
1446 gdk_gc_set_subwindow( m_penGC, GDK_CLIP_BY_CHILDREN );
1447 }
1448 }
1449
1450 SetLogicalFunction( old_logical_func );
1451
1452 return true;
1453 }
1454
1455 void wxGTKWindowImplDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y )
1456 {
1457 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1458
1459 if (!m_window) return;
1460
1461 if (text.empty()) return;
1462
1463 x = XLOG2DEV(x);
1464 y = YLOG2DEV(y);
1465
1466 wxCHECK_RET( m_context, wxT("no Pango context") );
1467 wxCHECK_RET( m_layout, wxT("no Pango layout") );
1468 wxCHECK_RET( m_fontdesc, wxT("no Pango font description") );
1469
1470 gdk_pango_context_set_colormap( m_context, m_cmap );
1471
1472 bool underlined = m_font.Ok() && m_font.GetUnderlined();
1473
1474 const wxCharBuffer data = wxGTK_CONV( text );
1475 if ( !data )
1476 return;
1477 size_t datalen = strlen(data);
1478
1479 // in Pango >= 1.16 the "underline of leading/trailing spaces" bug
1480 // has been fixed and thus the hack implemented below should never be used
1481 static bool pangoOk = !wx_pango_version_check(1, 16, 0);
1482
1483 bool needshack = underlined && !pangoOk;
1484 char *hackstring = NULL;
1485
1486 if (needshack)
1487 {
1488 // a PangoLayout which has leading/trailing spaces with underlined font
1489 // is not correctly drawn by this pango version: Pango won't underline the spaces.
1490 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
1491 // this behaviour. To workaround this problem, we use a special hack here
1492 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
1493 // empty space characters and give them a dummy colour attribute.
1494 // This will force Pango to underline the leading/trailing spaces, too.
1495
1496 // need to realloc the string to prepend & append our special characters
1497 hackstring = (char*)malloc((datalen+7)*sizeof(char));
1498
1499 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1500 strcpy(hackstring, "\342\200\214");
1501
1502 // copy the user string
1503 memcpy(&hackstring[3], data, datalen);
1504
1505 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
1506 strcpy(&hackstring[datalen+3], "\342\200\214");
1507
1508 // the special characters that we added require 6 additional bytes:
1509 datalen += 6;
1510
1511 pango_layout_set_text(m_layout, hackstring, datalen);
1512 }
1513 else
1514 {
1515 pango_layout_set_text(m_layout, data, datalen);
1516 }
1517
1518 if (underlined)
1519 {
1520 PangoAttrList *attrs = pango_attr_list_new();
1521 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1522 a->start_index = 0;
1523 a->end_index = datalen;
1524 pango_attr_list_insert(attrs, a);
1525
1526 if (needshack)
1527 {
1528 // dummy colour for the leading space
1529 a = pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1530 a->start_index = 0;
1531 a->end_index = 1;
1532 pango_attr_list_insert(attrs, a);
1533
1534 // dummy colour for the trailing space
1535 a = pango_attr_foreground_new (0x0057, 0x52A9, 0xD614);
1536 a->start_index = datalen - 1;
1537 a->end_index = datalen;
1538 pango_attr_list_insert(attrs, a);
1539 }
1540
1541 pango_layout_set_attributes(m_layout, attrs);
1542 pango_attr_list_unref(attrs);
1543 }
1544
1545 int w,h;
1546
1547 if (fabs(m_scaleY - 1.0) > 0.00001)
1548 {
1549 // If there is a user or actually any scale applied to
1550 // the device context, scale the font.
1551
1552 // scale font description
1553 gint oldSize = pango_font_description_get_size( m_fontdesc );
1554 double size = oldSize;
1555 size = size * m_scaleY;
1556 pango_font_description_set_size( m_fontdesc, (gint)size );
1557
1558 // actually apply scaled font
1559 pango_layout_set_font_description( m_layout, m_fontdesc );
1560
1561 pango_layout_get_pixel_size( m_layout, &w, &h );
1562 if ( m_backgroundMode == wxSOLID )
1563 {
1564 gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
1565 gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
1566 gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
1567 }
1568
1569 // Draw layout.
1570 if (m_owner && m_owner->GetLayoutDirection() == wxLayout_RightToLeft)
1571 gdk_draw_layout( m_window, m_textGC, x-w, y, m_layout );
1572 else
1573 gdk_draw_layout( m_window, m_textGC, x, y, m_layout );
1574
1575 // reset unscaled size
1576 pango_font_description_set_size( m_fontdesc, oldSize );
1577
1578 // actually apply unscaled font
1579 pango_layout_set_font_description( m_layout, m_fontdesc );
1580 }
1581 else
1582 {
1583 pango_layout_get_pixel_size( m_layout, &w, &h );
1584 if ( m_backgroundMode == wxSOLID )
1585 {
1586 gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
1587 gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
1588 gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
1589 }
1590
1591 // Draw layout.
1592 if (m_owner && m_owner->GetLayoutDirection() == wxLayout_RightToLeft)
1593 gdk_draw_layout( m_window, m_textGC, x-w, y, m_layout );
1594 else
1595 gdk_draw_layout( m_window, m_textGC, x, y, m_layout );
1596 }
1597
1598 if (underlined)
1599 {
1600 // undo underline attributes setting:
1601 pango_layout_set_attributes(m_layout, NULL);
1602 }
1603
1604 wxCoord width = w;
1605 wxCoord height = h;
1606
1607 width = wxCoord(width / m_scaleX);
1608 height = wxCoord(height / m_scaleY);
1609 CalcBoundingBox (x + width, y + height);
1610 CalcBoundingBox (x, y);
1611
1612 if (hackstring)
1613 free(hackstring);
1614 }
1615
1616
1617 // TODO: There is an example of rotating text with GTK2 that would probably be
1618 // a better approach here:
1619 // http://www.daa.com.au/pipermail/pygtk/2003-April/005052.html
1620
1621 void wxGTKWindowImplDC::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, double angle )
1622 {
1623 if (!m_window || text.empty())
1624 return;
1625
1626 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1627
1628 if ( wxIsNullDouble(angle) )
1629 {
1630 DrawText(text, x, y);
1631 return;
1632 }
1633
1634 wxCoord w;
1635 wxCoord h;
1636
1637 // TODO: implement later without GdkFont for GTK 2.0
1638 GetTextExtent(text, &w, &h, NULL,NULL, &m_font);
1639
1640 // draw the string normally
1641 wxBitmap src(w, h);
1642 wxMemoryDC dc;
1643 dc.SelectObject(src);
1644 dc.SetFont(GetFont());
1645 dc.SetBackground(*wxBLACK_BRUSH);
1646 dc.SetBrush(*wxBLACK_BRUSH);
1647 dc.Clear();
1648 dc.SetTextForeground( *wxWHITE );
1649 dc.DrawText(text, 0, 0);
1650 dc.SelectObject(wxNullBitmap);
1651
1652 // Calculate the size of the rotated bounding box.
1653 double rad = DegToRad(angle);
1654 double dx = cos(rad),
1655 dy = sin(rad);
1656
1657 // the rectngle vertices are counted clockwise with the first one being at
1658 // (0, 0) (or, rather, at (x, y))
1659 double x2 = w*dx,
1660 y2 = -w*dy; // y axis points to the bottom, hence minus
1661 double x4 = h*dy,
1662 y4 = h*dx;
1663 double x3 = x4 + x2,
1664 y3 = y4 + y2;
1665
1666 // calc max and min
1667 wxCoord maxX = (wxCoord)(dmax(x2, dmax(x3, x4)) + 0.5),
1668 maxY = (wxCoord)(dmax(y2, dmax(y3, y4)) + 0.5),
1669 minX = (wxCoord)(dmin(x2, dmin(x3, x4)) - 0.5),
1670 minY = (wxCoord)(dmin(y2, dmin(y3, y4)) - 0.5);
1671
1672
1673 wxImage image = src.ConvertToImage();
1674
1675 image.ConvertColourToAlpha( m_textForegroundColour.Red(),
1676 m_textForegroundColour.Green(),
1677 m_textForegroundColour.Blue() );
1678 image = image.Rotate( rad, wxPoint(0,0) );
1679
1680 int i_angle = (int) angle;
1681 i_angle = i_angle % 360;
1682 if (i_angle < 0)
1683 i_angle += 360;
1684 int xoffset = 0;
1685 if ((i_angle >= 90.0) && (i_angle < 270.0))
1686 xoffset = image.GetWidth();
1687 int yoffset = 0;
1688 if ((i_angle >= 0.0) && (i_angle < 180.0))
1689 yoffset = image.GetHeight();
1690
1691 if ((i_angle >= 0) && (i_angle < 90))
1692 yoffset -= (int)( cos(rad)*h );
1693 if ((i_angle >= 90) && (i_angle < 180))
1694 xoffset -= (int)( sin(rad)*h );
1695 if ((i_angle >= 180) && (i_angle < 270))
1696 yoffset -= (int)( cos(rad)*h );
1697 if ((i_angle >= 270) && (i_angle < 360))
1698 xoffset -= (int)( sin(rad)*h );
1699
1700 int i_x = x - xoffset;
1701 int i_y = y - yoffset;
1702
1703 src = image;
1704 DoDrawBitmap( src, i_x, i_y, true );
1705
1706
1707 // it would be better to draw with non underlined font and draw the line
1708 // manually here (it would be more straight...)
1709 #if 0
1710 if ( m_font.GetUnderlined() )
1711 {
1712 gdk_draw_line( m_window, m_textGC,
1713 XLOG2DEV(x + x4), YLOG2DEV(y + y4 + font->descent),
1714 XLOG2DEV(x + x3), YLOG2DEV(y + y3 + font->descent));
1715 }
1716 #endif // 0
1717
1718 // update the bounding box
1719 CalcBoundingBox(x + minX, y + minY);
1720 CalcBoundingBox(x + maxX, y + maxY);
1721 }
1722
1723 void wxGTKWindowImplDC::DoGetTextExtent(const wxString &string,
1724 wxCoord *width, wxCoord *height,
1725 wxCoord *descent, wxCoord *externalLeading,
1726 const wxFont *theFont) const
1727 {
1728 if ( width )
1729 *width = 0;
1730 if ( height )
1731 *height = 0;
1732 if ( descent )
1733 *descent = 0;
1734 if ( externalLeading )
1735 *externalLeading = 0;
1736
1737 if (string.empty())
1738 return;
1739
1740 // ensure that theFont is always non-NULL
1741 if ( !theFont || !theFont->Ok() )
1742 theFont = wx_const_cast(wxFont *, &m_font);
1743
1744 // and use it if it's valid
1745 if ( theFont->Ok() )
1746 {
1747 pango_layout_set_font_description
1748 (
1749 m_layout,
1750 theFont->GetNativeFontInfo()->description
1751 );
1752 }
1753
1754 // Set layout's text
1755 const wxCharBuffer dataUTF8 = wxGTK_CONV_FONT(string, *theFont);
1756 if ( !dataUTF8 )
1757 {
1758 // hardly ideal, but what else can we do if conversion failed?
1759 return;
1760 }
1761
1762 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
1763
1764 if (descent)
1765 {
1766 int h;
1767 pango_layout_get_pixel_size( m_layout, width, &h );
1768 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
1769 int baseline = pango_layout_iter_get_baseline(iter);
1770 pango_layout_iter_free(iter);
1771 *descent = h - PANGO_PIXELS(baseline);
1772
1773 if (height)
1774 *height = (wxCoord) h;
1775 }
1776 else
1777 {
1778 pango_layout_get_pixel_size( m_layout, width, height );
1779 }
1780
1781 // Reset old font description
1782 if (theFont->IsOk())
1783 pango_layout_set_font_description( m_layout, m_fontdesc );
1784 }
1785
1786
1787 bool wxGTKWindowImplDC::DoGetPartialTextExtents(const wxString& text,
1788 wxArrayInt& widths) const
1789 {
1790 const size_t len = text.length();
1791 widths.Empty();
1792 widths.Add(0, len);
1793
1794 if (text.empty())
1795 return true;
1796
1797 // Set layout's text
1798 const wxCharBuffer dataUTF8 = wxGTK_CONV_FONT(text, m_font);
1799 if ( !dataUTF8 )
1800 {
1801 // hardly ideal, but what else can we do if conversion failed?
1802 wxLogLastError(wxT("DoGetPartialTextExtents"));
1803 return false;
1804 }
1805
1806 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
1807
1808 // Calculate the position of each character based on the widths of
1809 // the previous characters
1810
1811 // Code borrowed from Scintilla's PlatGTK
1812 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
1813 PangoRectangle pos;
1814 pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
1815 size_t i = 0;
1816 while (pango_layout_iter_next_cluster(iter))
1817 {
1818 pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
1819 int position = PANGO_PIXELS(pos.x);
1820 widths[i++] = position;
1821 }
1822 while (i < len)
1823 widths[i++] = PANGO_PIXELS(pos.x + pos.width);
1824 pango_layout_iter_free(iter);
1825
1826 return true;
1827 }
1828
1829
1830 wxCoord wxGTKWindowImplDC::GetCharWidth() const
1831 {
1832 pango_layout_set_text( m_layout, "H", 1 );
1833 int w;
1834 pango_layout_get_pixel_size( m_layout, &w, NULL );
1835 return w;
1836 }
1837
1838 wxCoord wxGTKWindowImplDC::GetCharHeight() const
1839 {
1840 PangoFontMetrics *metrics = pango_context_get_metrics (m_context, m_fontdesc, pango_context_get_language(m_context));
1841 wxCHECK_MSG( metrics, -1, _T("failed to get pango font metrics") );
1842
1843 wxCoord h = PANGO_PIXELS (pango_font_metrics_get_descent (metrics) +
1844 pango_font_metrics_get_ascent (metrics));
1845 pango_font_metrics_unref (metrics);
1846 return h;
1847 }
1848
1849 void wxGTKWindowImplDC::Clear()
1850 {
1851 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1852
1853 if (!m_window) return;
1854
1855 // VZ: the code below results in infinite recursion and crashes when
1856 // dc.Clear() is done from OnPaint() so I disable it for now.
1857 // I don't know what the correct fix is but Clear() surely should not
1858 // reenter OnPaint()!
1859 #if 0
1860 /* - we either are a memory dc or have a window as the
1861 owner. anything else shouldn't happen.
1862 - we don't use gdk_window_clear() as we don't set
1863 the window's background colour anymore. it is too
1864 much pain to keep the DC's and the window's back-
1865 ground colour in synch. */
1866
1867 if (m_owner)
1868 {
1869 m_owner->Clear();
1870 return;
1871 }
1872
1873 if (m_isMemDC)
1874 {
1875 int width,height;
1876 GetSize( &width, &height );
1877 gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
1878 return;
1879 }
1880 #else // 1
1881 int width,height;
1882 GetSize( &width, &height );
1883 gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
1884 #endif // 0/1
1885 }
1886
1887 void wxGTKWindowImplDC::SetFont( const wxFont &font )
1888 {
1889 m_font = font;
1890
1891 if (m_font.Ok())
1892 {
1893 if (m_fontdesc)
1894 pango_font_description_free( m_fontdesc );
1895
1896 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1897
1898
1899 if (m_owner)
1900 {
1901 PangoContext *oldContext = m_context;
1902
1903 m_context = m_owner->GtkGetPangoDefaultContext();
1904
1905 // If we switch back/forth between different contexts
1906 // we also have to create a new layout. I think so,
1907 // at least, and it doesn't hurt to do it.
1908 if (oldContext != m_context)
1909 {
1910 if (m_layout)
1911 g_object_unref (m_layout);
1912
1913 m_layout = pango_layout_new( m_context );
1914 }
1915 }
1916
1917 pango_layout_set_font_description( m_layout, m_fontdesc );
1918 }
1919 }
1920
1921 void wxGTKWindowImplDC::SetPen( const wxPen &pen )
1922 {
1923 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1924
1925 if (m_pen == pen) return;
1926
1927 m_pen = pen;
1928
1929 if (!m_pen.Ok()) return;
1930
1931 if (!m_window) return;
1932
1933 gint width = m_pen.GetWidth();
1934 if (width <= 0)
1935 {
1936 // CMB: if width is non-zero scale it with the dc
1937 width = 1;
1938 }
1939 else
1940 {
1941 // X doesn't allow different width in x and y and so we take
1942 // the average
1943 double w = 0.5 +
1944 ( fabs((double) XLOG2DEVREL(width)) +
1945 fabs((double) YLOG2DEVREL(width)) ) / 2.0;
1946 width = (int)w;
1947 if ( !width )
1948 {
1949 // width can't be 0 or an internal GTK error occurs inside
1950 // gdk_gc_set_dashes() below
1951 width = 1;
1952 }
1953 }
1954
1955 static const wxGTKDash dotted[] = {1, 1};
1956 static const wxGTKDash short_dashed[] = {2, 2};
1957 static const wxGTKDash wxCoord_dashed[] = {2, 4};
1958 static const wxGTKDash dotted_dashed[] = {3, 3, 1, 3};
1959
1960 // We express dash pattern in pen width unit, so we are
1961 // independent of zoom factor and so on...
1962 int req_nb_dash;
1963 const wxGTKDash *req_dash;
1964
1965 GdkLineStyle lineStyle = GDK_LINE_SOLID;
1966 switch (m_pen.GetStyle())
1967 {
1968 case wxUSER_DASH:
1969 {
1970 lineStyle = GDK_LINE_ON_OFF_DASH;
1971 req_nb_dash = m_pen.GetDashCount();
1972 req_dash = (wxGTKDash*)m_pen.GetDash();
1973 break;
1974 }
1975 case wxDOT:
1976 {
1977 lineStyle = GDK_LINE_ON_OFF_DASH;
1978 req_nb_dash = 2;
1979 req_dash = dotted;
1980 break;
1981 }
1982 case wxLONG_DASH:
1983 {
1984 lineStyle = GDK_LINE_ON_OFF_DASH;
1985 req_nb_dash = 2;
1986 req_dash = wxCoord_dashed;
1987 break;
1988 }
1989 case wxSHORT_DASH:
1990 {
1991 lineStyle = GDK_LINE_ON_OFF_DASH;
1992 req_nb_dash = 2;
1993 req_dash = short_dashed;
1994 break;
1995 }
1996 case wxDOT_DASH:
1997 {
1998 // lineStyle = GDK_LINE_DOUBLE_DASH;
1999 lineStyle = GDK_LINE_ON_OFF_DASH;
2000 req_nb_dash = 4;
2001 req_dash = dotted_dashed;
2002 break;
2003 }
2004
2005 case wxTRANSPARENT:
2006 case wxSTIPPLE_MASK_OPAQUE:
2007 case wxSTIPPLE:
2008 case wxSOLID:
2009 default:
2010 {
2011 lineStyle = GDK_LINE_SOLID;
2012 req_dash = (wxGTKDash*)NULL;
2013 req_nb_dash = 0;
2014 break;
2015 }
2016 }
2017
2018 if (req_dash && req_nb_dash)
2019 {
2020 wxGTKDash *real_req_dash = new wxGTKDash[req_nb_dash];
2021 if (real_req_dash)
2022 {
2023 for (int i = 0; i < req_nb_dash; i++)
2024 real_req_dash[i] = req_dash[i] * width;
2025 gdk_gc_set_dashes( m_penGC, 0, real_req_dash, req_nb_dash );
2026 delete[] real_req_dash;
2027 }
2028 else
2029 {
2030 // No Memory. We use non-scaled dash pattern...
2031 gdk_gc_set_dashes( m_penGC, 0, (wxGTKDash*)req_dash, req_nb_dash );
2032 }
2033 }
2034
2035 GdkCapStyle capStyle = GDK_CAP_ROUND;
2036 switch (m_pen.GetCap())
2037 {
2038 case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
2039 case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }
2040 case wxCAP_ROUND:
2041 default:
2042 {
2043 if (width <= 1)
2044 {
2045 width = 0;
2046 capStyle = GDK_CAP_NOT_LAST;
2047 }
2048 else
2049 {
2050 capStyle = GDK_CAP_ROUND;
2051 }
2052 break;
2053 }
2054 }
2055
2056 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
2057 switch (m_pen.GetJoin())
2058 {
2059 case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
2060 case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
2061 case wxJOIN_ROUND:
2062 default: { joinStyle = GDK_JOIN_ROUND; break; }
2063 }
2064
2065 gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
2066
2067 m_pen.GetColour().CalcPixel( m_cmap );
2068 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
2069 }
2070
2071 void wxGTKWindowImplDC::SetBrush( const wxBrush &brush )
2072 {
2073 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2074
2075 if (m_brush == brush) return;
2076
2077 m_brush = brush;
2078
2079 if (!m_brush.Ok()) return;
2080
2081 if (!m_window) return;
2082
2083 m_brush.GetColour().CalcPixel( m_cmap );
2084 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
2085
2086 gdk_gc_set_fill( m_brushGC, GDK_SOLID );
2087
2088 if ((m_brush.GetStyle() == wxSTIPPLE) && (m_brush.GetStipple()->Ok()))
2089 {
2090 if (m_brush.GetStipple()->GetDepth() != 1)
2091 {
2092 gdk_gc_set_fill( m_brushGC, GDK_TILED );
2093 gdk_gc_set_tile( m_brushGC, m_brush.GetStipple()->GetPixmap() );
2094 }
2095 else
2096 {
2097 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
2098 gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() );
2099 }
2100 }
2101
2102 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
2103 {
2104 gdk_gc_set_fill( m_textGC, GDK_OPAQUE_STIPPLED);
2105 gdk_gc_set_stipple( m_textGC, m_brush.GetStipple()->GetMask()->GetBitmap() );
2106 }
2107
2108 if (m_brush.IsHatch())
2109 {
2110 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
2111 int num = m_brush.GetStyle() - wxBDIAGONAL_HATCH;
2112 gdk_gc_set_stipple( m_brushGC, hatches[num] );
2113 }
2114 }
2115
2116 void wxGTKWindowImplDC::SetBackground( const wxBrush &brush )
2117 {
2118 /* CMB 21/7/98: Added SetBackground. Sets background brush
2119 * for Clear() and bg colour for shapes filled with cross-hatch brush */
2120
2121 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2122
2123 if (m_backgroundBrush == brush) return;
2124
2125 m_backgroundBrush = brush;
2126
2127 if (!m_backgroundBrush.Ok()) return;
2128
2129 if (!m_window) return;
2130
2131 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
2132 gdk_gc_set_background( m_brushGC, m_backgroundBrush.GetColour().GetColor() );
2133 gdk_gc_set_background( m_penGC, m_backgroundBrush.GetColour().GetColor() );
2134 gdk_gc_set_background( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
2135 gdk_gc_set_foreground( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
2136
2137 gdk_gc_set_fill( m_bgGC, GDK_SOLID );
2138
2139 if ((m_backgroundBrush.GetStyle() == wxSTIPPLE) && (m_backgroundBrush.GetStipple()->Ok()))
2140 {
2141 if (m_backgroundBrush.GetStipple()->GetDepth() != 1)
2142 {
2143 gdk_gc_set_fill( m_bgGC, GDK_TILED );
2144 gdk_gc_set_tile( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
2145 }
2146 else
2147 {
2148 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
2149 gdk_gc_set_stipple( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
2150 }
2151 }
2152
2153 if (m_backgroundBrush.IsHatch())
2154 {
2155 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
2156 int num = m_backgroundBrush.GetStyle() - wxBDIAGONAL_HATCH;
2157 gdk_gc_set_stipple( m_bgGC, hatches[num] );
2158 }
2159 }
2160
2161 void wxGTKWindowImplDC::SetLogicalFunction( int function )
2162 {
2163 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2164
2165 if (m_logicalFunction == function)
2166 return;
2167
2168 // VZ: shouldn't this be a CHECK?
2169 if (!m_window)
2170 return;
2171
2172 GdkFunction mode;
2173 switch (function)
2174 {
2175 case wxXOR: mode = GDK_XOR; break;
2176 case wxINVERT: mode = GDK_INVERT; break;
2177 case wxOR_REVERSE: mode = GDK_OR_REVERSE; break;
2178 case wxAND_REVERSE: mode = GDK_AND_REVERSE; break;
2179 case wxCLEAR: mode = GDK_CLEAR; break;
2180 case wxSET: mode = GDK_SET; break;
2181 case wxOR_INVERT: mode = GDK_OR_INVERT; break;
2182 case wxAND: mode = GDK_AND; break;
2183 case wxOR: mode = GDK_OR; break;
2184 case wxEQUIV: mode = GDK_EQUIV; break;
2185 case wxNAND: mode = GDK_NAND; break;
2186 case wxAND_INVERT: mode = GDK_AND_INVERT; break;
2187 case wxCOPY: mode = GDK_COPY; break;
2188 case wxNO_OP: mode = GDK_NOOP; break;
2189 case wxSRC_INVERT: mode = GDK_COPY_INVERT; break;
2190
2191 // unsupported by GTK
2192 case wxNOR: mode = GDK_COPY; break;
2193 default:
2194 wxFAIL_MSG( wxT("unsupported logical function") );
2195 mode = GDK_COPY;
2196 }
2197
2198 m_logicalFunction = function;
2199
2200 gdk_gc_set_function( m_penGC, mode );
2201 gdk_gc_set_function( m_brushGC, mode );
2202
2203 // to stay compatible with wxMSW, we don't apply ROPs to the text
2204 // operations (i.e. DrawText/DrawRotatedText).
2205 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2206 gdk_gc_set_function( m_textGC, mode );
2207 }
2208
2209 void wxGTKWindowImplDC::SetTextForeground( const wxColour &col )
2210 {
2211 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2212
2213 // don't set m_textForegroundColour to an invalid colour as we'd crash
2214 // later then (we use m_textForegroundColour.GetColor() without checking
2215 // in a few places)
2216 if ( !col.Ok() || (m_textForegroundColour == col) )
2217 return;
2218
2219 m_textForegroundColour = col;
2220
2221 if ( m_window )
2222 {
2223 m_textForegroundColour.CalcPixel( m_cmap );
2224 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
2225 }
2226 }
2227
2228 void wxGTKWindowImplDC::SetTextBackground( const wxColour &col )
2229 {
2230 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2231
2232 // same as above
2233 if ( !col.Ok() || (m_textBackgroundColour == col) )
2234 return;
2235
2236 m_textBackgroundColour = col;
2237
2238 if ( m_window )
2239 {
2240 m_textBackgroundColour.CalcPixel( m_cmap );
2241 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
2242 }
2243 }
2244
2245 void wxGTKWindowImplDC::SetBackgroundMode( int mode )
2246 {
2247 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2248
2249 m_backgroundMode = mode;
2250
2251 if (!m_window) return;
2252
2253 // CMB 21/7/98: fill style of cross-hatch brushes is affected by
2254 // transparent/solid background mode
2255
2256 if (m_brush.GetStyle() != wxSOLID && m_brush.GetStyle() != wxTRANSPARENT)
2257 {
2258 gdk_gc_set_fill( m_brushGC,
2259 (m_backgroundMode == wxTRANSPARENT) ? GDK_STIPPLED : GDK_OPAQUE_STIPPLED);
2260 }
2261 }
2262
2263 void wxGTKWindowImplDC::SetPalette( const wxPalette& WXUNUSED(palette) )
2264 {
2265 wxFAIL_MSG( wxT("wxGTKWindowImplDC::SetPalette not implemented") );
2266 }
2267
2268 void wxGTKWindowImplDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
2269 {
2270 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2271
2272 if (!m_window) return;
2273
2274 wxRect rect;
2275 rect.x = XLOG2DEV(x);
2276 rect.y = YLOG2DEV(y);
2277 rect.width = XLOG2DEVREL(width);
2278 rect.height = YLOG2DEVREL(height);
2279
2280 if (m_owner && m_owner->m_wxwindow && (m_owner->GetLayoutDirection() == wxLayout_RightToLeft))
2281 {
2282 rect.x -= rect.width;
2283 }
2284
2285 if (!m_currentClippingRegion.IsNull())
2286 m_currentClippingRegion.Intersect( rect );
2287 else
2288 m_currentClippingRegion.Union( rect );
2289
2290 #if USE_PAINT_REGION
2291 if (!m_paintClippingRegion.IsNull())
2292 m_currentClippingRegion.Intersect( m_paintClippingRegion );
2293 #endif
2294
2295 wxCoord xx, yy, ww, hh;
2296 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
2297 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
2298
2299 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2300 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2301 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2302 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
2303 }
2304
2305 void wxGTKWindowImplDC::DoSetClippingRegionAsRegion( const wxRegion &region )
2306 {
2307 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2308
2309 if (region.Empty())
2310 {
2311 DestroyClippingRegion();
2312 return;
2313 }
2314
2315 if (!m_window) return;
2316
2317 if (!m_currentClippingRegion.IsNull())
2318 m_currentClippingRegion.Intersect( region );
2319 else
2320 m_currentClippingRegion.Union( region );
2321
2322 #if USE_PAINT_REGION
2323 if (!m_paintClippingRegion.IsNull())
2324 m_currentClippingRegion.Intersect( m_paintClippingRegion );
2325 #endif
2326
2327 wxCoord xx, yy, ww, hh;
2328 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
2329 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
2330
2331 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2332 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2333 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2334 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
2335 }
2336
2337 void wxGTKWindowImplDC::DestroyClippingRegion()
2338 {
2339 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2340
2341 wxDC::DestroyClippingRegion();
2342
2343 m_currentClippingRegion.Clear();
2344
2345 #if USE_PAINT_REGION
2346 if (!m_paintClippingRegion.IsEmpty())
2347 m_currentClippingRegion.Union( m_paintClippingRegion );
2348 #endif
2349
2350 if (!m_window) return;
2351
2352 if (m_currentClippingRegion.IsEmpty())
2353 {
2354 gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL );
2355 gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL );
2356 gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL );
2357 gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL );
2358 }
2359 else
2360 {
2361 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2362 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2363 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2364 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
2365 }
2366 }
2367
2368 void wxGTKWindowImplDC::Destroy()
2369 {
2370 if (m_penGC) wxFreePoolGC( m_penGC );
2371 m_penGC = (GdkGC*) NULL;
2372 if (m_brushGC) wxFreePoolGC( m_brushGC );
2373 m_brushGC = (GdkGC*) NULL;
2374 if (m_textGC) wxFreePoolGC( m_textGC );
2375 m_textGC = (GdkGC*) NULL;
2376 if (m_bgGC) wxFreePoolGC( m_bgGC );
2377 m_bgGC = (GdkGC*) NULL;
2378 }
2379
2380 void wxGTKWindowImplDC::SetDeviceOrigin( wxCoord x, wxCoord y )
2381 {
2382 m_deviceOriginX = x;
2383 m_deviceOriginY = y;
2384
2385 ComputeScaleAndOrigin();
2386 }
2387
2388 void wxGTKWindowImplDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
2389 {
2390 m_signX = (xLeftRight ? 1 : -1);
2391 m_signY = (yBottomUp ? -1 : 1);
2392
2393 if (m_owner && m_owner->m_wxwindow && (m_owner->GetLayoutDirection() == wxLayout_RightToLeft))
2394 m_signX = -m_signX;
2395
2396 ComputeScaleAndOrigin();
2397 }
2398
2399 void wxGTKWindowImplDC::ComputeScaleAndOrigin()
2400 {
2401 const wxRealPoint origScale(m_scaleX, m_scaleY);
2402
2403 wxDC::ComputeScaleAndOrigin();
2404
2405 // if scale has changed call SetPen to recalulate the line width
2406 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.Ok() )
2407 {
2408 // this is a bit artificial, but we need to force wxDC to think the pen
2409 // has changed
2410 wxPen pen = m_pen;
2411 m_pen = wxNullPen;
2412 SetPen( pen );
2413 }
2414 }
2415
2416 // Resolution in pixels per logical inch
2417 wxSize wxGTKWindowImplDC::GetPPI() const
2418 {
2419 return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5));
2420 }
2421
2422 int wxGTKWindowImplDC::GetDepth() const
2423 {
2424 return gdk_drawable_get_depth(m_window);
2425 }
2426
2427
2428 //-----------------------------------------------------------------------------
2429 // wxPaintDC
2430 //-----------------------------------------------------------------------------
2431
2432 #if wxUSE_NEW_DC
2433 IMPLEMENT_DYNAMIC_CLASS(wxGTKPaintImplDC, wxGTKClientImplDC)
2434 #else
2435 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC)
2436 #endif
2437
2438 // Limit the paint region to the window size. Sometimes
2439 // the paint region is too big, and this risks X11 errors
2440 static void wxLimitRegionToSize(wxRegion& region, const wxSize& sz)
2441 {
2442 wxRect originalRect = region.GetBox();
2443 wxRect rect(originalRect);
2444 if (rect.width + rect.x > sz.x)
2445 rect.width = sz.x - rect.x;
2446 if (rect.height + rect.y > sz.y)
2447 rect.height = sz.y - rect.y;
2448 if (rect != originalRect)
2449 {
2450 region = wxRegion(rect);
2451 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2452 originalRect.x, originalRect.y, originalRect.width, originalRect.height,
2453 rect.x, rect.y, rect.width, rect.height);
2454 }
2455 }
2456
2457 wxGTKPaintImplDC::wxGTKPaintImplDC( wxWindow *win )
2458 : wxGTKClientImplDC( win )
2459 {
2460 #if USE_PAINT_REGION
2461 if (!win->m_clipPaintRegion)
2462 return;
2463
2464 wxSize sz = win->GetSize();
2465 m_paintClippingRegion = win->m_nativeUpdateRegion;
2466 wxLimitRegionToSize(m_paintClippingRegion, sz);
2467
2468 GdkRegion *region = m_paintClippingRegion.GetRegion();
2469 if ( region )
2470 {
2471 m_currentClippingRegion.Union( m_paintClippingRegion );
2472 wxLimitRegionToSize(m_currentClippingRegion, sz);
2473
2474 if (sz.x <= 0 || sz.y <= 0)
2475 return ;
2476
2477 gdk_gc_set_clip_region( m_penGC, region );
2478 gdk_gc_set_clip_region( m_brushGC, region );
2479 gdk_gc_set_clip_region( m_textGC, region );
2480 gdk_gc_set_clip_region( m_bgGC, region );
2481 }
2482 #endif // USE_PAINT_REGION
2483 }
2484
2485 //-----------------------------------------------------------------------------
2486 // wxClientDC
2487 //-----------------------------------------------------------------------------
2488
2489 #if wxUSE_NEW_DC
2490 IMPLEMENT_DYNAMIC_CLASS(wxGTKClientImplDC, wxGTKWindowImplDC)
2491 #else
2492 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
2493 #endif
2494
2495 wxGTKClientImplDC::wxGTKClientImplDC( wxWindow *win )
2496 : wxGTKWindowImplDC( win )
2497 {
2498 wxCHECK_RET( win, _T("NULL window in wxGTKClientImplDC::wxClientDC") );
2499
2500 #ifdef __WXUNIVERSAL__
2501 wxPoint ptOrigin = win->GetClientAreaOrigin();
2502 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
2503 wxSize size = win->GetClientSize();
2504 SetClippingRegion(wxPoint(0, 0), size);
2505 #endif // __WXUNIVERSAL__
2506 }
2507
2508 void wxGTKClientImplDC::DoGetSize(int *width, int *height) const
2509 {
2510 wxCHECK_RET( m_owner, _T("GetSize() doesn't work without window") );
2511
2512 m_owner->GetClientSize( width, height );
2513 }
2514
2515 // ----------------------------------------------------------------------------
2516 // wxDCModule
2517 // ----------------------------------------------------------------------------
2518
2519 class wxDCModule : public wxModule
2520 {
2521 public:
2522 bool OnInit();
2523 void OnExit();
2524
2525 private:
2526 DECLARE_DYNAMIC_CLASS(wxDCModule)
2527 };
2528
2529 IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
2530
2531 bool wxDCModule::OnInit()
2532 {
2533 wxInitGCPool();
2534 return true;
2535 }
2536
2537 void wxDCModule::OnExit()
2538 {
2539 wxCleanUpGCPool();
2540 }