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