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