]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dcclient.cpp
Silence warning about truncation since the comment says it ok
[wxWidgets.git] / src / gtk1 / 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 // We might want to use the X11 context for faster
1887 // rendering on screen.
1888 // MR: Lets not want to do this, as this introduces libpangox dependancy.
1889 #if 0
1890 if (m_font.GetNoAntiAliasing())
1891 m_context = m_owner->GtkGetPangoX11Context();
1892 else
1893 #endif
1894 m_context = m_owner->GtkGetPangoDefaultContext();
1895
1896 // If we switch back/forth between different contexts
1897 // we also have to create a new layout. I think so,
1898 // at least, and it doesn't hurt to do it.
1899 if (oldContext != m_context)
1900 {
1901 if (m_layout)
1902 g_object_unref( G_OBJECT( m_layout ) );
1903
1904 m_layout = pango_layout_new( m_context );
1905 }
1906 }
1907
1908 pango_layout_set_font_description( m_layout, m_fontdesc );
1909 }
1910 #endif
1911 }
1912
1913 void wxWindowDC::SetPen( const wxPen &pen )
1914 {
1915 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1916
1917 if (m_pen == pen) return;
1918
1919 m_pen = pen;
1920
1921 if (!m_pen.Ok()) return;
1922
1923 if (!m_window) return;
1924
1925 gint width = m_pen.GetWidth();
1926 if (width <= 0)
1927 {
1928 // CMB: if width is non-zero scale it with the dc
1929 width = 1;
1930 }
1931 else
1932 {
1933 // X doesn't allow different width in x and y and so we take
1934 // the average
1935 double w = 0.5 +
1936 ( fabs((double) XLOG2DEVREL(width)) +
1937 fabs((double) YLOG2DEVREL(width)) ) / 2.0;
1938 width = (int)w;
1939 if ( !width )
1940 {
1941 // width can't be 0 or an internal GTK error occurs inside
1942 // gdk_gc_set_dashes() below
1943 width = 1;
1944 }
1945 }
1946
1947 static const wxGTKDash dotted[] = {1, 1};
1948 static const wxGTKDash short_dashed[] = {2, 2};
1949 static const wxGTKDash wxCoord_dashed[] = {2, 4};
1950 static const wxGTKDash dotted_dashed[] = {3, 3, 1, 3};
1951
1952 // We express dash pattern in pen width unit, so we are
1953 // independent of zoom factor and so on...
1954 int req_nb_dash;
1955 const wxGTKDash *req_dash;
1956
1957 GdkLineStyle lineStyle = GDK_LINE_SOLID;
1958 switch (m_pen.GetStyle())
1959 {
1960 case wxUSER_DASH:
1961 {
1962 lineStyle = GDK_LINE_ON_OFF_DASH;
1963 req_nb_dash = m_pen.GetDashCount();
1964 req_dash = (wxGTKDash*)m_pen.GetDash();
1965 break;
1966 }
1967 case wxDOT:
1968 {
1969 lineStyle = GDK_LINE_ON_OFF_DASH;
1970 req_nb_dash = 2;
1971 req_dash = dotted;
1972 break;
1973 }
1974 case wxLONG_DASH:
1975 {
1976 lineStyle = GDK_LINE_ON_OFF_DASH;
1977 req_nb_dash = 2;
1978 req_dash = wxCoord_dashed;
1979 break;
1980 }
1981 case wxSHORT_DASH:
1982 {
1983 lineStyle = GDK_LINE_ON_OFF_DASH;
1984 req_nb_dash = 2;
1985 req_dash = short_dashed;
1986 break;
1987 }
1988 case wxDOT_DASH:
1989 {
1990 // lineStyle = GDK_LINE_DOUBLE_DASH;
1991 lineStyle = GDK_LINE_ON_OFF_DASH;
1992 req_nb_dash = 4;
1993 req_dash = dotted_dashed;
1994 break;
1995 }
1996
1997 case wxTRANSPARENT:
1998 case wxSTIPPLE_MASK_OPAQUE:
1999 case wxSTIPPLE:
2000 case wxSOLID:
2001 default:
2002 {
2003 lineStyle = GDK_LINE_SOLID;
2004 req_dash = (wxGTKDash*)NULL;
2005 req_nb_dash = 0;
2006 break;
2007 }
2008 }
2009
2010 if (req_dash && req_nb_dash)
2011 {
2012 wxGTKDash *real_req_dash = new wxGTKDash[req_nb_dash];
2013 if (real_req_dash)
2014 {
2015 for (int i = 0; i < req_nb_dash; i++)
2016 real_req_dash[i] = req_dash[i] * width;
2017 gdk_gc_set_dashes( m_penGC, 0, real_req_dash, req_nb_dash );
2018 delete[] real_req_dash;
2019 }
2020 else
2021 {
2022 // No Memory. We use non-scaled dash pattern...
2023 gdk_gc_set_dashes( m_penGC, 0, (wxGTKDash*)req_dash, req_nb_dash );
2024 }
2025 }
2026
2027 GdkCapStyle capStyle = GDK_CAP_ROUND;
2028 switch (m_pen.GetCap())
2029 {
2030 case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
2031 case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }
2032 case wxCAP_ROUND:
2033 default:
2034 {
2035 if (width <= 1)
2036 {
2037 width = 0;
2038 capStyle = GDK_CAP_NOT_LAST;
2039 }
2040 else
2041 {
2042 capStyle = GDK_CAP_ROUND;
2043 }
2044 break;
2045 }
2046 }
2047
2048 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
2049 switch (m_pen.GetJoin())
2050 {
2051 case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
2052 case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
2053 case wxJOIN_ROUND:
2054 default: { joinStyle = GDK_JOIN_ROUND; break; }
2055 }
2056
2057 gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
2058
2059 m_pen.GetColour().CalcPixel( m_cmap );
2060 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
2061 }
2062
2063 void wxWindowDC::SetBrush( const wxBrush &brush )
2064 {
2065 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2066
2067 if (m_brush == brush) return;
2068
2069 m_brush = brush;
2070
2071 if (!m_brush.Ok()) return;
2072
2073 if (!m_window) return;
2074
2075 m_brush.GetColour().CalcPixel( m_cmap );
2076 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
2077
2078 gdk_gc_set_fill( m_brushGC, GDK_SOLID );
2079
2080 if ((m_brush.GetStyle() == wxSTIPPLE) && (m_brush.GetStipple()->Ok()))
2081 {
2082 if (m_brush.GetStipple()->GetPixmap())
2083 {
2084 gdk_gc_set_fill( m_brushGC, GDK_TILED );
2085 gdk_gc_set_tile( m_brushGC, m_brush.GetStipple()->GetPixmap() );
2086 }
2087 else
2088 {
2089 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
2090 gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetBitmap() );
2091 }
2092 }
2093
2094 if ((m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE) && (m_brush.GetStipple()->GetMask()))
2095 {
2096 gdk_gc_set_fill( m_textGC, GDK_OPAQUE_STIPPLED);
2097 gdk_gc_set_stipple( m_textGC, m_brush.GetStipple()->GetMask()->GetBitmap() );
2098 }
2099
2100 if (m_brush.IsHatch())
2101 {
2102 gdk_gc_set_fill( m_brushGC, GDK_STIPPLED );
2103 int num = m_brush.GetStyle() - wxBDIAGONAL_HATCH;
2104 gdk_gc_set_stipple( m_brushGC, hatches[num] );
2105 }
2106 }
2107
2108 void wxWindowDC::SetBackground( const wxBrush &brush )
2109 {
2110 /* CMB 21/7/98: Added SetBackground. Sets background brush
2111 * for Clear() and bg colour for shapes filled with cross-hatch brush */
2112
2113 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2114
2115 if (m_backgroundBrush == brush) return;
2116
2117 m_backgroundBrush = brush;
2118
2119 if (!m_backgroundBrush.Ok()) return;
2120
2121 if (!m_window) return;
2122
2123 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
2124 gdk_gc_set_background( m_brushGC, m_backgroundBrush.GetColour().GetColor() );
2125 gdk_gc_set_background( m_penGC, m_backgroundBrush.GetColour().GetColor() );
2126 gdk_gc_set_background( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
2127 gdk_gc_set_foreground( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
2128
2129 gdk_gc_set_fill( m_bgGC, GDK_SOLID );
2130
2131 if ((m_backgroundBrush.GetStyle() == wxSTIPPLE) && (m_backgroundBrush.GetStipple()->Ok()))
2132 {
2133 if (m_backgroundBrush.GetStipple()->GetPixmap())
2134 {
2135 gdk_gc_set_fill( m_bgGC, GDK_TILED );
2136 gdk_gc_set_tile( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
2137 }
2138 else
2139 {
2140 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
2141 gdk_gc_set_stipple( m_bgGC, m_backgroundBrush.GetStipple()->GetBitmap() );
2142 }
2143 }
2144
2145 if (m_backgroundBrush.IsHatch())
2146 {
2147 gdk_gc_set_fill( m_bgGC, GDK_STIPPLED );
2148 int num = m_backgroundBrush.GetStyle() - wxBDIAGONAL_HATCH;
2149 gdk_gc_set_stipple( m_bgGC, hatches[num] );
2150 }
2151 }
2152
2153 void wxWindowDC::SetLogicalFunction( int function )
2154 {
2155 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2156
2157 if (m_logicalFunction == function)
2158 return;
2159
2160 // VZ: shouldn't this be a CHECK?
2161 if (!m_window)
2162 return;
2163
2164 GdkFunction mode;
2165 switch (function)
2166 {
2167 case wxXOR: mode = GDK_XOR; break;
2168 case wxINVERT: mode = GDK_INVERT; break;
2169 case wxOR_REVERSE: mode = GDK_OR_REVERSE; break;
2170 case wxAND_REVERSE: mode = GDK_AND_REVERSE; break;
2171 case wxCLEAR: mode = GDK_CLEAR; break;
2172 case wxSET: mode = GDK_SET; break;
2173 case wxOR_INVERT: mode = GDK_OR_INVERT; break;
2174 case wxAND: mode = GDK_AND; break;
2175 case wxOR: mode = GDK_OR; break;
2176 case wxEQUIV: mode = GDK_EQUIV; break;
2177 case wxNAND: mode = GDK_NAND; break;
2178 case wxAND_INVERT: mode = GDK_AND_INVERT; break;
2179 case wxCOPY: mode = GDK_COPY; break;
2180 case wxNO_OP: mode = GDK_NOOP; break;
2181 case wxSRC_INVERT: mode = GDK_COPY_INVERT; break;
2182
2183 // unsupported by GTK
2184 case wxNOR: mode = GDK_COPY; break;
2185 default:
2186 wxFAIL_MSG( wxT("unsupported logical function") );
2187 mode = GDK_COPY;
2188 }
2189
2190 m_logicalFunction = function;
2191
2192 gdk_gc_set_function( m_penGC, mode );
2193 gdk_gc_set_function( m_brushGC, mode );
2194
2195 // to stay compatible with wxMSW, we don't apply ROPs to the text
2196 // operations (i.e. DrawText/DrawRotatedText).
2197 // True, but mono-bitmaps use the m_textGC and they use ROPs as well.
2198 gdk_gc_set_function( m_textGC, mode );
2199 }
2200
2201 void wxWindowDC::SetTextForeground( const wxColour &col )
2202 {
2203 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2204
2205 // don't set m_textForegroundColour to an invalid colour as we'd crash
2206 // later then (we use m_textForegroundColour.GetColor() without checking
2207 // in a few places)
2208 if ( !col.Ok() || (m_textForegroundColour == col) )
2209 return;
2210
2211 m_textForegroundColour = col;
2212
2213 if ( m_window )
2214 {
2215 m_textForegroundColour.CalcPixel( m_cmap );
2216 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
2217 }
2218 }
2219
2220 void wxWindowDC::SetTextBackground( const wxColour &col )
2221 {
2222 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2223
2224 // same as above
2225 if ( !col.Ok() || (m_textBackgroundColour == col) )
2226 return;
2227
2228 m_textBackgroundColour = col;
2229
2230 if ( m_window )
2231 {
2232 m_textBackgroundColour.CalcPixel( m_cmap );
2233 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
2234 }
2235 }
2236
2237 void wxWindowDC::SetBackgroundMode( int mode )
2238 {
2239 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2240
2241 m_backgroundMode = mode;
2242
2243 if (!m_window) return;
2244
2245 // CMB 21/7/98: fill style of cross-hatch brushes is affected by
2246 // transparent/solid background mode
2247
2248 if (m_brush.GetStyle() != wxSOLID && m_brush.GetStyle() != wxTRANSPARENT)
2249 {
2250 gdk_gc_set_fill( m_brushGC,
2251 (m_backgroundMode == wxTRANSPARENT) ? GDK_STIPPLED : GDK_OPAQUE_STIPPLED);
2252 }
2253 }
2254
2255 void wxWindowDC::SetPalette( const wxPalette& WXUNUSED(palette) )
2256 {
2257 wxFAIL_MSG( wxT("wxWindowDC::SetPalette not implemented") );
2258 }
2259
2260 void wxWindowDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
2261 {
2262 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2263
2264 if (!m_window) return;
2265
2266 wxRect rect;
2267 rect.x = XLOG2DEV(x);
2268 rect.y = YLOG2DEV(y);
2269 rect.width = XLOG2DEVREL(width);
2270 rect.height = YLOG2DEVREL(height);
2271
2272 if (!m_currentClippingRegion.IsNull())
2273 m_currentClippingRegion.Intersect( rect );
2274 else
2275 m_currentClippingRegion.Union( rect );
2276
2277 #if USE_PAINT_REGION
2278 if (!m_paintClippingRegion.IsNull())
2279 m_currentClippingRegion.Intersect( m_paintClippingRegion );
2280 #endif
2281
2282 wxCoord xx, yy, ww, hh;
2283 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
2284 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
2285
2286 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2287 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2288 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2289 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
2290 }
2291
2292 void wxWindowDC::DoSetClippingRegionAsRegion( const wxRegion &region )
2293 {
2294 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2295
2296 if (region.Empty())
2297 {
2298 DestroyClippingRegion();
2299 return;
2300 }
2301
2302 if (!m_window) return;
2303
2304 if (!m_currentClippingRegion.IsNull())
2305 m_currentClippingRegion.Intersect( region );
2306 else
2307 m_currentClippingRegion.Union( region );
2308
2309 #if USE_PAINT_REGION
2310 if (!m_paintClippingRegion.IsNull())
2311 m_currentClippingRegion.Intersect( m_paintClippingRegion );
2312 #endif
2313
2314 wxCoord xx, yy, ww, hh;
2315 m_currentClippingRegion.GetBox( xx, yy, ww, hh );
2316 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
2317
2318 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2319 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2320 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2321 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
2322 }
2323
2324 void wxWindowDC::DestroyClippingRegion()
2325 {
2326 wxCHECK_RET( Ok(), wxT("invalid window dc") );
2327
2328 wxDC::DestroyClippingRegion();
2329
2330 m_currentClippingRegion.Clear();
2331
2332 #if USE_PAINT_REGION
2333 if (!m_paintClippingRegion.IsEmpty())
2334 m_currentClippingRegion.Union( m_paintClippingRegion );
2335 #endif
2336
2337 if (!m_window) return;
2338
2339 if (m_currentClippingRegion.IsEmpty())
2340 {
2341 gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL );
2342 gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL );
2343 gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL );
2344 gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL );
2345 }
2346 else
2347 {
2348 gdk_gc_set_clip_region( m_penGC, m_currentClippingRegion.GetRegion() );
2349 gdk_gc_set_clip_region( m_brushGC, m_currentClippingRegion.GetRegion() );
2350 gdk_gc_set_clip_region( m_textGC, m_currentClippingRegion.GetRegion() );
2351 gdk_gc_set_clip_region( m_bgGC, m_currentClippingRegion.GetRegion() );
2352 }
2353 }
2354
2355 void wxWindowDC::Destroy()
2356 {
2357 if (m_penGC) wxFreePoolGC( m_penGC );
2358 m_penGC = (GdkGC*) NULL;
2359 if (m_brushGC) wxFreePoolGC( m_brushGC );
2360 m_brushGC = (GdkGC*) NULL;
2361 if (m_textGC) wxFreePoolGC( m_textGC );
2362 m_textGC = (GdkGC*) NULL;
2363 if (m_bgGC) wxFreePoolGC( m_bgGC );
2364 m_bgGC = (GdkGC*) NULL;
2365 }
2366
2367 void wxWindowDC::ComputeScaleAndOrigin()
2368 {
2369 const wxRealPoint origScale(m_scaleX, m_scaleY);
2370
2371 wxDC::ComputeScaleAndOrigin();
2372
2373 // if scale has changed call SetPen to recalulate the line width
2374 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.Ok() )
2375 {
2376 // this is a bit artificial, but we need to force wxDC to think the pen
2377 // has changed
2378 wxPen pen = m_pen;
2379 m_pen = wxNullPen;
2380 SetPen( pen );
2381 }
2382 }
2383
2384 // Resolution in pixels per logical inch
2385 wxSize wxWindowDC::GetPPI() const
2386 {
2387 return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5));
2388 }
2389
2390 int wxWindowDC::GetDepth() const
2391 {
2392 #ifdef __WXGTK20__
2393 return gdk_drawable_get_depth(m_window);
2394 #else
2395 wxFAIL_MSG(wxT("not implemented"));
2396
2397 return -1;
2398 #endif
2399 }
2400
2401
2402 //-----------------------------------------------------------------------------
2403 // wxPaintDC
2404 //-----------------------------------------------------------------------------
2405
2406 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxClientDC)
2407
2408 // Limit the paint region to the window size. Sometimes
2409 // the paint region is too big, and this risks X11 errors
2410 static void wxLimitRegionToSize(wxRegion& region, const wxSize& sz)
2411 {
2412 wxRect originalRect = region.GetBox();
2413 wxRect rect(originalRect);
2414 if (rect.width + rect.x > sz.x)
2415 rect.width = sz.x - rect.x;
2416 if (rect.height + rect.y > sz.y)
2417 rect.height = sz.y - rect.y;
2418 if (rect != originalRect)
2419 {
2420 region = wxRegion(rect);
2421 wxLogTrace(wxT("painting"), wxT("Limiting region from %d, %d, %d, %d to %d, %d, %d, %d\n"),
2422 originalRect.x, originalRect.y, originalRect.width, originalRect.height,
2423 rect.x, rect.y, rect.width, rect.height);
2424 }
2425 }
2426
2427 wxPaintDC::wxPaintDC( wxWindow *win )
2428 : wxClientDC( win )
2429 {
2430 #if USE_PAINT_REGION
2431 if (!win->m_clipPaintRegion)
2432 return;
2433
2434 wxSize sz = win->GetSize();
2435 m_paintClippingRegion = win->GetUpdateRegion();
2436 wxLimitRegionToSize(m_paintClippingRegion, sz);
2437
2438 GdkRegion *region = m_paintClippingRegion.GetRegion();
2439 if ( region )
2440 {
2441 m_currentClippingRegion.Union( m_paintClippingRegion );
2442 wxLimitRegionToSize(m_currentClippingRegion, sz);
2443
2444 if (sz.x <= 0 || sz.y <= 0)
2445 return ;
2446
2447 gdk_gc_set_clip_region( m_penGC, region );
2448 gdk_gc_set_clip_region( m_brushGC, region );
2449 gdk_gc_set_clip_region( m_textGC, region );
2450 gdk_gc_set_clip_region( m_bgGC, region );
2451 }
2452 #endif // USE_PAINT_REGION
2453 }
2454
2455 //-----------------------------------------------------------------------------
2456 // wxClientDC
2457 //-----------------------------------------------------------------------------
2458
2459 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
2460
2461 wxClientDC::wxClientDC( wxWindow *win )
2462 : wxWindowDC( win )
2463 {
2464 wxCHECK_RET( win, _T("NULL window in wxClientDC::wxClientDC") );
2465
2466 #ifdef __WXUNIVERSAL__
2467 wxPoint ptOrigin = win->GetClientAreaOrigin();
2468 SetDeviceOrigin(ptOrigin.x, ptOrigin.y);
2469 wxSize size = win->GetClientSize();
2470 SetClippingRegion(wxPoint(0, 0), size);
2471 #endif // __WXUNIVERSAL__
2472 }
2473
2474 void wxClientDC::DoGetSize(int *width, int *height) const
2475 {
2476 wxCHECK_RET( m_owner, _T("GetSize() doesn't work without window") );
2477
2478 m_owner->GetClientSize( width, height );
2479 }
2480
2481 // ----------------------------------------------------------------------------
2482 // wxDCModule
2483 // ----------------------------------------------------------------------------
2484
2485 class wxDCModule : public wxModule
2486 {
2487 public:
2488 bool OnInit();
2489 void OnExit();
2490
2491 private:
2492 DECLARE_DYNAMIC_CLASS(wxDCModule)
2493 };
2494
2495 IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
2496
2497 bool wxDCModule::OnInit()
2498 {
2499 wxInitGCPool();
2500 return true;
2501 }
2502
2503 void wxDCModule::OnExit()
2504 {
2505 wxCleanUpGCPool();
2506 }