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