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