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