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