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