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