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