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