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