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