]> git.saurik.com Git - wxWidgets.git/blob - src/motif/dcclient.cpp
fixed test failures under wxMSW when using our wxVsnprintf implementation (remains...
[wxWidgets.git] / src / motif / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/dcclient.cpp
3 // Purpose: wxClientDCImpl class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 About pens, brushes, and the m_autoSetting flag:
14
15 Under X, pens and brushes control some of the same X drawing
16 parameters. Therefore, it is impossible to independently maintain
17 the current pen and the current brush. Also, some settings depend on
18 the current logical function. The m_currentFill, etc. instance
19 variables remember state across the brush and pen.
20
21 Since pens are used more than brushes, the m_autoSetting flag is used to
22 indicate that a brush was recently used, and SetPen must be called to
23 reinstall the current pen's parameters. If m_autoSetting includes 0x2, then the
24 pens color may need to be set based on XOR.
25
26 There is, unfortunately, some confusion between setting the current pen/brush
27 and actually installing the brush/pen parameters. Both functionalies are
28 perform by SetPen and SetBrush. C'est la vie.
29 */
30
31 // ============================================================================
32 // declarations
33 // ============================================================================
34
35 // ----------------------------------------------------------------------------
36 // headers
37 // ----------------------------------------------------------------------------
38
39 // For compilers that support precompilation, includes "wx.h".
40 #include "wx/wxprec.h"
41
42 #ifndef WX_PRECOMP
43 #include "wx/log.h"
44 #include "wx/app.h"
45 #include "wx/window.h"
46 #include "wx/dcmemory.h"
47 #include "wx/math.h"
48 #include "wx/image.h"
49 #include "wx/dcclient.h"
50 #endif
51
52 #ifdef __VMS__
53 #pragma message disable nosimpint
54 #endif
55 #include <Xm/Xm.h>
56 #ifdef __VMS__
57 #pragma message enable nosimpint
58 #endif
59
60 #include "wx/motif/private.h"
61 #include "wx/motif/dcclient.h"
62
63 #ifdef __EMX__
64 #include <float.h> // for M_PI
65 #endif // __EMX__
66
67 #include "bdiag.xbm"
68 #include "fdiag.xbm"
69 #include "cdiag.xbm"
70 #include "horiz.xbm"
71 #include "verti.xbm"
72 #include "cross.xbm"
73
74 static Pixmap bdiag, cdiag, fdiag, cross, horiz, verti;
75
76 // ----------------------------------------------------------------------------
77 // constants
78 // ----------------------------------------------------------------------------
79
80 // Fudge factor (VZ: what??)
81 #define WX_GC_CF 1
82
83 // ----------------------------------------------------------------------------
84 // macros
85 // ----------------------------------------------------------------------------
86
87 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
88 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
89 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxMotifDCImpl)
90
91 #define IS_HATCH(s) ((s)>=wxFIRST_HATCH && (s)<=wxLAST_HATCH)
92
93 // FIXME: left over after removal of wxDC::GetOptimization()
94 #define GET_OPTIMIZATION false
95
96 // ----------------------------------------------------------------------------
97 // prototypes
98 // ----------------------------------------------------------------------------
99
100 static void XCopyRemote(Display *src_display, Display *dest_display,
101 Drawable src, Drawable dest,
102 GC destgc,
103 int srcx, int srcy,
104 unsigned int w, unsigned int h,
105 int destx, int desty,
106 bool more, XImage **cache);
107
108 // ============================================================================
109 // implementation
110 // ============================================================================
111
112 /*
113 * compare two doubles and return the larger rounded
114 * to the nearest int
115 */
116 static int roundmax(double a, double b)
117 {
118 return (int)((a > b ? a : b) + 0.5);
119 }
120
121 /*
122 * compare two doubles and return the smaller rounded
123 * to the nearest int
124 */
125 static int roundmin(double a, double b)
126 {
127 return (int)((a < b ? a : b) - 0.5);
128 }
129
130
131 // ----------------------------------------------------------------------------
132 // wxWindowDCImpl
133 // ----------------------------------------------------------------------------
134
135 void wxWindowDCImpl::Init()
136 {
137 m_gc = (WXGC) 0;
138 m_gcBacking = (WXGC) 0;
139 m_window = NULL;
140 m_backgroundPixel = -1;
141 m_currentPenWidth = 1;
142 m_currentPenJoin = -1;
143 m_currentPenDashCount = -1;
144 m_currentPenDash = NULL;
145 m_currentStyle = -1;
146 m_currentFill = -1;
147 m_colour = wxColourDisplay();
148 m_display = NULL;
149 m_pixmap = (WXPixmap) 0;
150 m_autoSetting = 0;
151 m_ok = false;
152 m_clipRegion = (WXRegion) 0;
153 }
154
155 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner)
156 : wxMotifDCImpl(owner)
157 {
158 Init();
159 }
160
161 wxWindowDCImpl::wxWindowDCImpl(wxDC *owner, wxWindow *window)
162 : wxMotifDCImpl(owner)
163 {
164 wxASSERT_MSG( (window != NULL), "You must pass a valid wxWindow to wxWindowDCImpl/wxClientDCImpl/wxPaintDCImpl constructor." );
165
166 Init();
167
168 m_window = window;
169 m_font = window->GetFont();
170 m_ok = true;
171
172 m_display = window->GetXDisplay();
173 m_pixmap = window->GetXWindow();
174 Display* display = (Display*) m_display;
175
176 XSetWindowColormap (display, (Pixmap) m_pixmap, (Colormap) wxTheApp->GetMainColormap(m_display));
177
178 XGCValues gcvalues;
179 gcvalues.foreground = BlackPixel (display, DefaultScreen (display));
180 gcvalues.background = WhitePixel (display, DefaultScreen (display));
181 gcvalues.graphics_exposures = False;
182 gcvalues.subwindow_mode = IncludeInferiors;
183 gcvalues.line_width = 1;
184 #if !wxMOTIF_NEW_FONT_HANDLING
185 WXFontStructPtr pFontStruct = m_font.GetFontStruct(m_userScaleY*m_logicalScaleY, m_display);
186 gcvalues.font = ((XFontStruct*)pFontStruct)->fid;
187 #endif
188 m_gc = (WXGC) XCreateGC (display, RootWindow (display, DefaultScreen (display)),
189 GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode
190 #if !wxMOTIF_NEW_FONT_HANDLING
191 | GCFont
192 #endif
193 ,
194 &gcvalues);
195
196 if (m_window->GetBackingPixmap())
197 {
198 m_gcBacking = (WXGC) XCreateGC (display, RootWindow (display,
199 DefaultScreen (display)),
200 GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
201 &gcvalues);
202 }
203
204 m_backgroundPixel = gcvalues.background;
205
206 SetBackground(wxBrush(m_window->GetBackgroundColour(), wxSOLID));
207 }
208
209 wxWindowDCImpl::~wxWindowDCImpl()
210 {
211 if (m_gc)
212 XFreeGC ((Display*) m_display, (GC) m_gc);
213 m_gc = (WXGC) 0;
214
215 if (m_gcBacking)
216 XFreeGC ((Display*) m_display, (GC) m_gcBacking);
217 m_gcBacking = (WXGC) 0;
218
219 if (m_clipRegion)
220 XDestroyRegion ((Region) m_clipRegion);
221 m_clipRegion = (WXRegion) 0;
222 }
223
224 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
225 const wxColour & col, wxFloodFillStyle style);
226
227 bool wxWindowDCImpl::DoFloodFill(wxCoord x, wxCoord y,
228 const wxColour& col, wxFloodFillStyle style)
229 {
230 return wxDoFloodFill(GetOwner(), x, y, col, style);
231 }
232
233 bool wxWindowDCImpl::DoGetPixel( wxCoord x1, wxCoord y1, wxColour *col ) const
234 {
235 // Generic (and therefore rather inefficient) method.
236 // Could be improved.
237 wxMemoryDC memdc;
238 wxBitmap bitmap(1, 1);
239 memdc.SelectObject(bitmap);
240 memdc.Blit(0, 0, 1, 1, GetOwner(), x1, y1);
241 memdc.SelectObject(wxNullBitmap);
242 wxImage image = bitmap.ConvertToImage();
243 col->Set(image.GetRed(0, 0), image.GetGreen(0, 0), image.GetBlue(0, 0));
244 return true;
245 }
246
247 void wxWindowDCImpl::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
248 {
249 wxCHECK_RET( IsOk(), "invalid dc" );
250
251 int x1d, y1d, x2d, y2d;
252
253 x1d = XLOG2DEV(x1);
254 y1d = YLOG2DEV(y1);
255 x2d = XLOG2DEV(x2);
256 y2d = YLOG2DEV(y2);
257
258 if (m_autoSetting)
259 SetPen (m_pen);
260
261 XDrawLine ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, x1d, y1d, x2d, y2d);
262
263 if (m_window && m_window->GetBackingPixmap())
264 XDrawLine ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
265 XLOG2DEV_2(x1), YLOG2DEV_2(y1),
266 XLOG2DEV_2(x2), YLOG2DEV_2(y2));
267
268 CalcBoundingBox(x1, y1);
269 CalcBoundingBox(x2, y2);
270 }
271
272 void wxWindowDCImpl::DoCrossHair( wxCoord x, wxCoord y )
273 {
274 wxCHECK_RET( IsOk(), "invalid dc" );
275
276 if (m_autoSetting)
277 SetPen (m_pen);
278
279 int xx = XLOG2DEV (x);
280 int yy = YLOG2DEV (y);
281 int ww, hh;
282 wxDisplaySize (&ww, &hh);
283 XDrawLine ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, 0, yy,
284 ww, yy);
285 XDrawLine ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xx, 0,
286 xx, hh);
287
288 if (m_window && m_window->GetBackingPixmap())
289 {
290 xx = XLOG2DEV_2 (x);
291 yy = YLOG2DEV_2 (y);
292 XDrawLine ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
293 0, yy,
294 ww, yy);
295 XDrawLine ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
296 xx, 0,
297 xx, hh);
298 }
299 }
300
301 void wxWindowDCImpl::DoDrawArc( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc )
302 {
303 wxCHECK_RET( IsOk(), "invalid dc" );
304
305 int xx1 = XLOG2DEV (x1);
306 int yy1 = YLOG2DEV (y1);
307 int xx2 = XLOG2DEV (x2);
308 int yy2 = YLOG2DEV (y2);
309 int xxc = XLOG2DEV (xc);
310 int yyc = YLOG2DEV (yc);
311 int xxc_2 = XLOG2DEV_2 (xc);
312 int yyc_2 = YLOG2DEV_2 (yc);
313
314 wxCoord dx = xx1 - xxc;
315 wxCoord dy = yy1 - yyc;
316 double radius = sqrt ((double)(dx * dx + dy * dy));
317 wxCoord r = (wxCoord) radius;
318
319 double radius1, radius2;
320
321 if (xx1 == xx2 && yy1 == yy2)
322 {
323 radius1 = 0.0;
324 radius2 = 360.0;
325 }
326 else if (radius == 0.0)
327 radius1 = radius2 = 0.0;
328 else
329 {
330 if (xx1 - xxc == 0)
331 if (yy1 - yyc < 0)
332 radius1 = 90.0;
333 else
334 radius1 = -90.0;
335 else
336 radius1 = -atan2 ((double) (yy1 - yyc), (double) (xx1 - xxc)) * 360.0 / (2 * M_PI);
337
338 if (xx2 - xxc == 0)
339 if (yy2 - yyc < 0)
340 radius2 = 90.0;
341 else
342 radius2 = -90.0;
343 else
344 radius2 = -atan2 ((double) (yy2 - yyc), (double) (xx2 - xxc)) * 360.0 / (2 * M_PI);
345 }
346 radius1 *= 64.0;
347 radius2 *= 64.0;
348 int alpha1 = (int) radius1;
349 int alpha2 = (int) (radius2 - radius1);
350 while (alpha2 <= 0)
351 alpha2 += 360 * 64;
352 while (alpha2 > 360 * 64)
353 alpha2 -= 360 * 64;
354
355 if (m_brush.IsOk() && m_brush.GetStyle () != wxTRANSPARENT)
356 {
357 SetBrush (m_brush);
358 XFillArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) (GC) m_gc,
359 xxc - r, yyc - r, 2 * r, 2 * r, alpha1, alpha2);
360
361 if (m_window && m_window->GetBackingPixmap())
362 XFillArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
363 xxc_2 - r, yyc_2 - r, 2 * r, 2 * r, alpha1, alpha2);
364
365 }
366
367 if (m_pen.IsOk() && m_pen.GetStyle () != wxTRANSPARENT)
368 {
369 if (m_autoSetting)
370 SetPen (m_pen);
371 XDrawArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc,
372 xxc - r, yyc - r, 2 * r, 2 * r, alpha1, alpha2);
373
374 if (m_window && m_window->GetBackingPixmap())
375 XDrawArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
376 xxc_2 - r, yyc_2 - r, 2 * r, 2 * r, alpha1, alpha2);
377 }
378 CalcBoundingBox (x1, y1);
379 CalcBoundingBox (x2, y2);
380 }
381
382 void wxWindowDCImpl::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double sa, double ea )
383 {
384 wxCHECK_RET( IsOk(), "invalid dc" );
385
386 int xd, yd, wd, hd;
387
388 xd = XLOG2DEV(x);
389 yd = YLOG2DEV(y);
390 wd = XLOG2DEVREL(width);
391 hd = YLOG2DEVREL(height);
392
393 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
394 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
395 int start = int(sa*64);
396 int end = int(ea*64);
397 if (start<0) start+=360*64;
398 if (end <0) end +=360*64;
399 if (end>start) end-=start;
400 else end+=360*64-start;
401
402 if (m_brush.IsOk() && m_brush.GetStyle () != wxTRANSPARENT)
403 {
404 m_autoSetting = true; // must be reset
405
406 SetBrush (m_brush);
407 XFillArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd, wd, hd, start, end);
408
409 if (m_window && m_window->GetBackingPixmap())
410 XFillArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
411 XLOG2DEV_2 (x), YLOG2DEV_2 (y),wd,hd,start,end);
412 }
413
414 if (m_pen.IsOk() && m_pen.GetStyle () != wxTRANSPARENT)
415 {
416 if (m_autoSetting)
417 SetPen (m_pen);
418 XDrawArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd, wd, hd, start,end);
419 if (m_window && m_window->GetBackingPixmap())
420 XDrawArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
421 XLOG2DEV_2 (x), YLOG2DEV_2 (y),wd,hd,start,end);
422 }
423 CalcBoundingBox (x, y);
424 CalcBoundingBox (x + width, y + height);
425 }
426
427 void wxWindowDCImpl::DoDrawPoint( wxCoord x, wxCoord y )
428 {
429 wxCHECK_RET( IsOk(), "invalid dc" );
430
431 if (m_pen.IsOk() && m_autoSetting)
432 SetPen (m_pen);
433
434 XDrawPoint ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, XLOG2DEV (x), YLOG2DEV (y));
435 if (m_window && m_window->GetBackingPixmap())
436 XDrawPoint ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking, XLOG2DEV_2 (x), YLOG2DEV_2 (y));
437
438 CalcBoundingBox (x, y);
439 }
440
441 void wxWindowDCImpl::DoDrawLines( int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset )
442 {
443 wxCHECK_RET( IsOk(), "invalid dc" );
444
445 if (m_pen.IsOk() && m_pen.GetStyle () != wxTRANSPARENT)
446 {
447 if (m_autoSetting)
448 SetPen (m_pen);
449
450 XPoint *xpoints = new XPoint[n];
451 int i;
452
453 for (i = 0; i < n; i++)
454 {
455 xpoints[i].x = (short)XLOG2DEV (points[i].x + xoffset);
456 xpoints[i].y = (short)YLOG2DEV (points[i].y + yoffset);
457 }
458 XDrawLines ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xpoints, n, 0);
459
460 if (m_window && m_window->GetBackingPixmap())
461 {
462 for (i = 0; i < n; i++)
463 {
464 xpoints[i].x = (short)XLOG2DEV_2 (points[i].x + xoffset);
465 xpoints[i].y = (short)YLOG2DEV_2 (points[i].y + yoffset);
466 }
467 XDrawLines ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking, xpoints, n, 0);
468 }
469 delete[]xpoints;
470 }
471 }
472
473 void wxWindowDCImpl::DoDrawPolygon( int n, wxPoint points[],
474 wxCoord xoffset, wxCoord yoffset, wxPolygonFillMode fillStyle )
475 {
476 wxCHECK_RET( IsOk(), "invalid dc" );
477
478 XPoint *xpoints1 = new XPoint[n + 1];
479 XPoint *xpoints2 = new XPoint[n + 1];
480 int i;
481 for (i = 0; i < n; i++)
482 {
483 xpoints1[i].x = (short)XLOG2DEV (points[i].x + xoffset);
484 xpoints1[i].y = (short)YLOG2DEV (points[i].y + yoffset);
485 xpoints2[i].x = (short)XLOG2DEV_2 (points[i].x + xoffset);
486 xpoints2[i].y = (short)YLOG2DEV_2 (points[i].y + yoffset);
487 CalcBoundingBox (points[i].x + xoffset, points[i].y + yoffset);
488 }
489
490 // Close figure for XDrawLines (not needed for XFillPolygon)
491 xpoints1[i].x = xpoints1[0].x;
492 xpoints1[i].y = xpoints1[0].y;
493 xpoints2[i].x = xpoints2[0].x;
494 xpoints2[i].y = xpoints2[0].y;
495
496 if (m_brush.IsOk() && m_brush.GetStyle () != wxTRANSPARENT)
497 {
498 SetBrush (m_brush);
499 XSetFillRule ((Display*) m_display, (GC) m_gc, fillStyle == wxODDEVEN_RULE ? EvenOddRule : WindingRule);
500 XFillPolygon ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xpoints1, n, Complex, 0);
501 XSetFillRule ((Display*) m_display, (GC) m_gc, EvenOddRule); // default mode
502 if (m_window && m_window->GetBackingPixmap())
503 {
504 XSetFillRule ((Display*) m_display,(GC) m_gcBacking,
505 fillStyle == wxODDEVEN_RULE ? EvenOddRule : WindingRule);
506 XFillPolygon ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking, xpoints2, n, Complex, 0);
507 XSetFillRule ((Display*) m_display,(GC) m_gcBacking, EvenOddRule); // default mode
508 }
509 }
510
511 if (m_pen.IsOk() && m_pen.GetStyle () != wxTRANSPARENT)
512 {
513 if (m_autoSetting)
514 SetPen (m_pen);
515 XDrawLines ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xpoints1, n + 1, 0);
516
517 if (m_window && m_window->GetBackingPixmap())
518 XDrawLines ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking, xpoints2, n + 1, 0);
519 }
520
521 delete[]xpoints1;
522 delete[]xpoints2;
523 }
524
525 void wxWindowDCImpl::DoDrawRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
526 {
527 wxCHECK_RET( IsOk(), "invalid dc" );
528
529 int xd, yd, wfd, hfd, wd, hd;
530
531 xd = XLOG2DEV(x);
532 yd = YLOG2DEV(y);
533 wfd = XLOG2DEVREL(width);
534 wd = wfd - WX_GC_CF;
535 hfd = YLOG2DEVREL(height);
536 hd = hfd - WX_GC_CF;
537
538 if (wfd == 0 || hfd == 0) return;
539 if (wd < 0) { wd = - wd; xd = xd - wd; }
540 if (hd < 0) { hd = - hd; yd = yd - hd; }
541
542 if (m_brush.IsOk() && m_brush.GetStyle () != wxTRANSPARENT)
543 {
544 SetBrush (m_brush);
545 XFillRectangle ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd, wfd, hfd);
546
547 if (m_window && m_window->GetBackingPixmap())
548 XFillRectangle ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
549 XLOG2DEV_2 (x), YLOG2DEV_2 (y),
550 wfd, hfd);
551 }
552
553 if (m_pen.IsOk() && m_pen.GetStyle () != wxTRANSPARENT)
554 {
555 if (m_autoSetting)
556 SetPen (m_pen);
557 XDrawRectangle ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd, wd, hd);
558
559 if (m_window && m_window->GetBackingPixmap())
560 XDrawRectangle ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
561 XLOG2DEV_2 (x), YLOG2DEV_2 (y),
562 wd, hd);
563 }
564 CalcBoundingBox (x, y);
565 CalcBoundingBox (x + width, y + height);
566 }
567
568 void wxWindowDCImpl::DoDrawRoundedRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius )
569 {
570 wxCHECK_RET( IsOk(), "invalid dc" );
571
572 // If radius is negative, it's a proportion of the smaller dimension.
573
574 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
575
576 int xd = XLOG2DEV (x);
577 int yd = YLOG2DEV (y);
578 int rd = XLOG2DEVREL ((long) radius);
579 int wd = XLOG2DEVREL (width) - WX_GC_CF;
580 int hd = YLOG2DEVREL (height) - WX_GC_CF;
581
582 int rw_d = rd * 2;
583 int rh_d = rw_d;
584
585 // If radius is zero use DrawRectangle() instead to avoid
586 // X drawing errors with small radii
587 if (rd == 0)
588 {
589 DoDrawRectangle( x, y, width, height );
590 return;
591 }
592
593 // Draw nothing if transformed w or h is 0
594 if (wd == 0 || hd == 0) return;
595
596 // CMB: adjust size if outline is drawn otherwise the result is
597 // 1 pixel too wide and high
598 if (m_pen.GetStyle() != wxTRANSPARENT)
599 {
600 wd--;
601 hd--;
602 }
603
604 // CMB: ensure dd is not larger than rectangle otherwise we
605 // get an hour glass shape
606 if (rw_d > wd) rw_d = wd;
607 if (rw_d > hd) rw_d = hd;
608 rd = rw_d / 2;
609
610 // For backing pixmap
611 int xd2 = XLOG2DEV_2 (x);
612 int yd2 = YLOG2DEV_2 (y);
613 int rd2 = XLOG2DEVREL ((long) radius);
614 int wd2 = XLOG2DEVREL (width) ;
615 int hd2 = YLOG2DEVREL (height) ;
616
617 int rw_d2 = rd2 * 2;
618 int rh_d2 = rw_d2;
619
620 if (m_brush.IsOk() && m_brush.GetStyle () != wxTRANSPARENT)
621 {
622 SetBrush (m_brush);
623
624 XFillRectangle ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + rd, yd,
625 wd - rw_d, hd);
626 XFillRectangle ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd + rd,
627 wd, hd - rh_d);
628
629 // Arcs start from 3 o'clock, positive angles anticlockwise
630 // Top-left
631 XFillArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd,
632 rw_d, rh_d, 90 * 64, 90 * 64);
633 // Top-right
634 XFillArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + wd - rw_d, yd,
635 // rw_d, rh_d, 0, 90 * 64);
636 rw_d, rh_d, 0, 91 * 64);
637 // Bottom-right
638 XFillArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + wd - rw_d,
639 yd + hd - rh_d,
640 // rw_d, rh_d, 270 * 64, 90 * 64);
641 rw_d, rh_d, 269 * 64, 92 * 64);
642 // Bottom-left
643 XFillArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd + hd - rh_d,
644 rw_d, rh_d, 180 * 64, 90 * 64);
645
646 if (m_window && m_window->GetBackingPixmap())
647 {
648 XFillRectangle ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
649 xd2 + rd2, yd2, wd2 - rw_d2, hd2);
650 XFillRectangle ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
651 xd2, yd2 + rd2, wd2, hd2 - rh_d2);
652
653 XFillArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
654 xd2, yd2, rw_d2, rh_d2, 90 * 64, 90 * 64);
655 XFillArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
656 xd2 + wd2 - rw_d2, yd2,
657 // rw_d2, rh_d2, 0, 90 * 64);
658 rw_d2, rh_d2, 0, 91 * 64);
659 XFillArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
660 xd2 + wd2 - rw_d2,
661 yd2 + hd2 - rh_d2,
662 // rw_d2, rh_d2, 270 * 64, 90 * 64);
663 rw_d2, rh_d2, 269 * 64, 92 * 64);
664 XFillArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
665 xd2, yd2 + hd2 - rh_d2,
666 rw_d2, rh_d2, 180 * 64, 90 * 64);
667 }
668 }
669
670 if (m_pen.IsOk() && m_pen.GetStyle () != wxTRANSPARENT)
671 {
672 SetPen (m_pen);
673 XDrawLine ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + rd, yd,
674 xd + wd - rd + 1, yd);
675 XDrawLine ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + rd, yd + hd,
676 xd + wd - rd, yd + hd);
677
678 XDrawLine ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd + rd,
679 xd, yd + hd - rd);
680 XDrawLine ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + wd, yd + rd,
681 xd + wd, yd + hd - rd + 1);
682 XDrawArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd,
683 rw_d, rh_d, 90 * 64, 90 * 64);
684 XDrawArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + wd - rw_d, yd,
685 // rw_d, rh_d, 0, 90 * 64);
686 rw_d, rh_d, 0, 91 * 64);
687 XDrawArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd + wd - rw_d,
688 yd + hd - rh_d,
689 rw_d, rh_d, 269 * 64, 92 * 64);
690 XDrawArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd + hd - rh_d,
691 rw_d, rh_d, 180 * 64, 90 * 64);
692
693 if (m_window && m_window->GetBackingPixmap())
694 {
695 XDrawLine ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
696 xd2 + rd2, yd2,
697 xd2 + wd2 - rd2 + 1, yd2);
698 XDrawLine ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
699 xd2 + rd2, yd2 + hd2,
700 xd2 + wd2 - rd2, yd2 + hd2);
701
702 XDrawLine ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
703 xd2, yd2 + rd2,
704 xd2, yd2 + hd2 - rd2);
705 XDrawLine ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
706 xd2 + wd2, yd2 + rd2,
707 xd2 + wd2, yd2 + hd2 - rd2 + 1);
708 XDrawArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
709 xd2, yd2,
710 rw_d2, rh_d2, 90 * 64, 90 * 64);
711 XDrawArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
712 xd2 + wd2 - rw_d2, yd2,
713 // rw_d2, rh_d2, 0, 90 * 64);
714 rw_d2, rh_d2, 0, 91 * 64);
715 XDrawArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
716 xd2 + wd2 - rw_d2,
717 yd2 + hd2 - rh_d2,
718 rw_d2, rh_d2, 269 * 64, 92 * 64);
719 XDrawArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
720 xd2, yd2 + hd2 - rh_d2,
721 rw_d2, rh_d2, 180 * 64, 90 * 64);
722 }
723 }
724 CalcBoundingBox (x, y);
725 CalcBoundingBox (x + width, y + height);
726 }
727
728 void wxWindowDCImpl::DoDrawEllipse( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
729 {
730 wxCHECK_RET( IsOk(), "invalid dc" );
731
732 // Check for negative width and height
733 if (height < 0)
734 {
735 y = y + height;
736 height = - height ;
737 }
738
739 if (width < 0)
740 {
741 x = x + width;
742 width = - width ;
743 }
744
745 static const int angle = 23040;
746
747 int xd, yd, wd, hd;
748
749 xd = XLOG2DEV(x);
750 yd = YLOG2DEV(y);
751 wd = XLOG2DEVREL(width) ;
752 hd = YLOG2DEVREL(height) ;
753
754 if (m_brush.IsOk() && m_brush.GetStyle () != wxTRANSPARENT)
755 {
756 SetBrush (m_brush);
757 XFillArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd, wd, hd, 0, angle);
758 if (m_window && m_window->GetBackingPixmap())
759 XFillArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
760 XLOG2DEV_2 (x), YLOG2DEV_2 (y),
761 XLOG2DEVREL (width) - WX_GC_CF,
762 YLOG2DEVREL (height) - WX_GC_CF, 0, angle);
763 }
764
765 if (m_pen.IsOk() && m_pen.GetStyle () != wxTRANSPARENT)
766 {
767 if (m_autoSetting)
768 SetPen (m_pen);
769 XDrawArc ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, xd, yd, wd, hd, 0, angle);
770 if (m_window && m_window->GetBackingPixmap())
771 XDrawArc ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
772 XLOG2DEV_2 (x), YLOG2DEV_2 (y),
773 XLOG2DEVREL (width) - WX_GC_CF,
774 YLOG2DEVREL (height) - WX_GC_CF, 0, angle);
775 }
776 CalcBoundingBox (x, y);
777 CalcBoundingBox (x + width, y + height);
778
779 }
780
781 bool wxWindowDCImpl::CanDrawBitmap() const
782 {
783 wxCHECK_MSG( IsOk(), false, "invalid dc" );
784
785 return true;
786 }
787
788 // TODO: use scaled Blit e.g. as per John Price's implementation
789 // in Contrib/Utilities
790 bool wxWindowDCImpl::DoBlit( wxCoord xdest, wxCoord ydest,
791 wxCoord width, wxCoord height,
792 wxDC *source, wxCoord xsrc, wxCoord ysrc,
793 wxRasterOperationMode rop, bool useMask,
794 wxCoord xsrcMask, wxCoord ysrcMask )
795 {
796 wxCHECK_MSG( IsOk(), false, "invalid dc" );
797
798 wxWindowDC const * sourceDC = wxDynamicCast(source, wxWindowDC);
799 wxCHECK_MSG( sourceDC, false, "Blit source DC must be wxWindowDCImpl or derived class." );
800
801 // cast is safe in virtue of the above wxCHECK_MSG()
802 const wxWindowDCImpl * const
803 srcImpl = static_cast<const wxWindowDCImpl *>(sourceDC->GetImpl());
804 WXDisplay * const srcDpy = srcImpl->m_display;
805
806
807 // Be sure that foreground pixels (1) of the Icon will be painted with
808 // foreground colour. [m_textForegroundColour] Background pixels (0)
809 // will be painted with backgound colour (m_textBackgroundColour)
810 // Using ::SetPen is horribly slow, so avoid doing it
811 WXPixel oldBackgroundPixel = -1;
812 WXPixel oldForegroundPixel = -1;
813
814 if (m_textBackgroundColour.IsOk())
815 {
816 oldBackgroundPixel = m_backgroundPixel;
817 WXPixel pixel = m_textBackgroundColour.AllocColour(m_display);
818
819 XSetBackground ((Display*) m_display, (GC) m_gc, pixel);
820 if (m_window && m_window->GetBackingPixmap())
821 XSetBackground ((Display*) m_display,(GC) m_gcBacking,
822 pixel);
823 }
824 if (m_textForegroundColour.IsOk())
825 {
826 oldForegroundPixel = m_currentColour.GetPixel();
827
828 if( m_textForegroundColour.GetPixel() <= -1 )
829 CalculatePixel( m_textForegroundColour,
830 m_textForegroundColour, true);
831
832 WXPixel pixel = m_textForegroundColour.GetPixel();
833 if (pixel > -1)
834 SetForegroundPixelWithLogicalFunction(pixel);
835 }
836
837 // Do bitmap scaling if necessary
838
839 wxBitmap *scaledBitmap = NULL;
840 Pixmap sourcePixmap = (Pixmap) NULL;
841 double scaleX, scaleY;
842 GetUserScale(& scaleX, & scaleY);
843 bool retVal = false;
844
845 /* TODO: use the mask origin when drawing transparently */
846 if (xsrcMask == -1 && ysrcMask == -1)
847 {
848 xsrcMask = xsrc; ysrcMask = ysrc;
849 }
850
851 // Sorry, can't scale masks just yet
852 if (!useMask && (scaleX != 1.0 || scaleY != 1.0) && sourceDC->IsKindOf(CLASSINFO(wxMemoryDC)))
853 {
854 wxMemoryDC* memDC = (wxMemoryDC*) sourceDC;
855 wxBitmap& bitmap = memDC->GetSelectedBitmap();
856
857 wxASSERT_MSG( (bitmap.IsOk()), "Bad source bitmap in wxWindowDCImpl::Blit");
858
859 wxImage image = bitmap.ConvertToImage();
860 if (!image.IsOk())
861 {
862 sourcePixmap = (Pixmap) bitmap.GetDrawable();
863 }
864 else
865 {
866 int scaledW = (int) (bitmap.GetWidth() * scaleX);
867 int scaledH = (int) (bitmap.GetHeight() * scaleY);
868
869 image = image.Scale(scaledW, scaledH);
870 scaledBitmap = new wxBitmap(image);
871 sourcePixmap = (Pixmap) scaledBitmap->GetDrawable();
872 }
873 }
874 else
875 {
876 sourcePixmap = (Pixmap) srcImpl->m_pixmap;
877 }
878
879 if (m_pixmap && sourcePixmap)
880 {
881 /* MATTHEW: [9] */
882 wxRasterOperationMode orig = m_logicalFunction;
883
884 SetLogicalFunction (rop);
885
886 if (m_display != srcDpy)
887 {
888 XImage *cache = NULL;
889
890 if (m_window && m_window->GetBackingPixmap())
891 XCopyRemote((Display*) srcDpy, (Display*) m_display,
892 (Pixmap) sourcePixmap, (Pixmap) m_window->GetBackingPixmap(),
893 (GC) m_gcBacking,
894 source->LogicalToDeviceX (xsrc),
895 source->LogicalToDeviceY (ysrc),
896 source->LogicalToDeviceXRel(width),
897 source->LogicalToDeviceYRel(height),
898 XLOG2DEV_2 (xdest), YLOG2DEV_2 (ydest),
899 True, &cache);
900
901 if ( useMask && source->IsKindOf(CLASSINFO(wxMemoryDC)) )
902 {
903 wxMemoryDC *memDC = (wxMemoryDC *)source;
904 wxBitmap& sel = memDC->GetSelectedBitmap();
905 if ( sel.IsOk() && sel.GetMask() && sel.GetMask()->GetBitmap() )
906 {
907 XSetClipMask ((Display*) m_display, (GC) m_gc, (Pixmap) sel.GetMask()->GetBitmap());
908 XSetClipOrigin ((Display*) m_display, (GC) m_gc, XLOG2DEV (xdest), YLOG2DEV (ydest));
909 }
910 }
911
912 XCopyRemote((Display*) srcDpy, (Display*) m_display, (Pixmap) sourcePixmap, (Pixmap) m_pixmap, (GC) m_gc,
913 source->LogicalToDeviceX (xsrc),
914 source->LogicalToDeviceY (ysrc),
915 source->LogicalToDeviceXRel(width),
916 source->LogicalToDeviceYRel(height),
917 XLOG2DEV (xdest), YLOG2DEV (ydest),
918 False, &cache);
919
920 if ( useMask )
921 {
922 if ( m_clipRegion )
923 XSetRegion ((Display*) m_display, (GC) m_gc,
924 (Region) m_clipRegion);
925 else
926 XSetClipMask ((Display*) m_display, (GC) m_gc, None);
927
928 XSetClipOrigin ((Display*) m_display, (GC) m_gc, 0, 0);
929 }
930
931 } else
932 { //XGCValues values;
933 //XGetGCValues((Display*)m_display, (GC)m_gc, GCForeground, &values);
934
935 if (m_window && m_window->GetBackingPixmap())
936 {
937 // +++ MARKUS (mho@comnets.rwth-aachen): error on blitting bitmaps with depth 1
938 if (source->IsKindOf(CLASSINFO(wxMemoryDC)) && ((wxMemoryDC*) source)->GetSelectedBitmap().GetDepth() == 1)
939 {
940 XCopyPlane ((Display*) m_display, (Pixmap) sourcePixmap, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
941 source->LogicalToDeviceX (xsrc),
942 source->LogicalToDeviceY (ysrc),
943 source->LogicalToDeviceXRel(width),
944 source->LogicalToDeviceYRel(height),
945 XLOG2DEV_2 (xdest), YLOG2DEV_2 (ydest), 1);
946 }
947 else
948 {
949 XCopyArea ((Display*) m_display, (Pixmap) sourcePixmap, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
950 source->LogicalToDeviceX (xsrc),
951 source->LogicalToDeviceY (ysrc),
952 source->LogicalToDeviceXRel(width),
953 source->LogicalToDeviceYRel(height),
954 XLOG2DEV_2 (xdest), YLOG2DEV_2 (ydest));
955 }
956 }
957 if ( useMask && source->IsKindOf(CLASSINFO(wxMemoryDC)) )
958 {
959 wxMemoryDC *memDC = (wxMemoryDC *)source;
960 wxBitmap& sel = memDC->GetSelectedBitmap();
961 if ( sel.IsOk() && sel.GetMask() && sel.GetMask()->GetBitmap() )
962 {
963 XSetClipMask ((Display*) m_display, (GC) m_gc, (Pixmap) sel.GetMask()->GetBitmap());
964 XSetClipOrigin ((Display*) m_display, (GC) m_gc, XLOG2DEV (xdest), YLOG2DEV (ydest));
965 }
966 }
967
968 // Check if we're copying from a mono bitmap
969 if (source->IsKindOf(CLASSINFO(wxMemoryDC)) &&
970 ((wxMemoryDC*)source)->GetSelectedBitmap().IsOk() &&
971 (((wxMemoryDC*)source)->GetSelectedBitmap().GetDepth () == 1))
972 {
973 XCopyPlane ((Display*) m_display, (Pixmap) sourcePixmap, (Pixmap) m_pixmap, (GC) m_gc,
974 source->LogicalToDeviceX (xsrc),
975 source->LogicalToDeviceY (ysrc),
976 source->LogicalToDeviceXRel(width),
977 source->LogicalToDeviceYRel(height),
978 XLOG2DEV (xdest), YLOG2DEV (ydest), 1);
979 }
980 else
981 {
982 XCopyArea ((Display*) m_display, (Pixmap) sourcePixmap, (Pixmap) m_pixmap, (GC) m_gc,
983 source->LogicalToDeviceX (xsrc),
984 source->LogicalToDeviceY (ysrc),
985 source->LogicalToDeviceXRel(width),
986 source->LogicalToDeviceYRel(height),
987 XLOG2DEV (xdest), YLOG2DEV (ydest));
988
989 }
990 if ( useMask )
991 {
992 if ( m_clipRegion )
993 XSetRegion ((Display*) m_display, (GC) m_gc,
994 (Region) m_clipRegion);
995 else
996 XSetClipMask ((Display*) m_display, (GC) m_gc, None);
997
998 XSetClipOrigin ((Display*) m_display, (GC) m_gc, 0, 0);
999 }
1000
1001 } /* Remote/local (Display*) m_display */
1002 CalcBoundingBox (xdest, ydest);
1003 CalcBoundingBox (xdest + width, ydest + height);
1004
1005 SetLogicalFunction(orig);
1006
1007 retVal = true;
1008 }
1009 if (scaledBitmap) delete scaledBitmap;
1010
1011 if (oldBackgroundPixel > -1)
1012 {
1013 XSetBackground ((Display*) m_display, (GC) m_gc, oldBackgroundPixel);
1014 if (m_window && m_window->GetBackingPixmap())
1015 XSetBackground ((Display*) m_display,(GC) m_gcBacking,
1016 oldBackgroundPixel);
1017 }
1018 if (oldForegroundPixel > -1)
1019 {
1020 XSetForeground ((Display*) m_display, (GC) m_gc, oldForegroundPixel);
1021 if (m_window && m_window->GetBackingPixmap())
1022 XSetForeground ((Display*) m_display,(GC) m_gcBacking,
1023 oldForegroundPixel);
1024 }
1025
1026 return retVal;
1027 }
1028
1029 void wxWindowDCImpl::DoDrawText( const wxString &text, wxCoord x, wxCoord y )
1030 {
1031 wxCHECK_RET( IsOk(), "invalid dc" );
1032
1033 // Since X draws from the baseline of the text, must add the text height
1034 int cx = 0;
1035 int cy = 0;
1036 int ascent = 0;
1037 int slen = text.length();
1038
1039 // Set FillStyle, otherwise X will use current stipple!
1040 XGCValues gcV, gcBackingV;
1041
1042 XGetGCValues ((Display*) m_display, (GC)m_gc, GCFillStyle, &gcV);
1043 XSetFillStyle ((Display*) m_display, (GC) m_gc, FillSolid);
1044 if (m_window && m_window->GetBackingPixmap())
1045 {
1046 XGetGCValues ((Display*) m_display, (GC)m_gcBacking, GCFillStyle,
1047 &gcBackingV );
1048 XSetFillStyle ((Display*) m_display, (GC) m_gcBacking, FillSolid);
1049 }
1050
1051 if (m_font.IsOk())
1052 wxGetTextExtent (m_display, m_font, m_userScaleY * m_logicalScaleY,
1053 text, &cx, &cy, &ascent, NULL);
1054
1055 // First draw a rectangle representing the text background, if a text
1056 // background is specified
1057 if (m_textBackgroundColour.IsOk () && (m_backgroundMode != wxTRANSPARENT))
1058 {
1059 wxColour oldPenColour = m_currentColour;
1060 m_currentColour = m_textBackgroundColour;
1061 bool sameColour = (oldPenColour.IsOk () && m_textBackgroundColour.IsOk () &&
1062 (oldPenColour.Red () == m_textBackgroundColour.Red ()) &&
1063 (oldPenColour.Blue () == m_textBackgroundColour.Blue ()) &&
1064 (oldPenColour.Green () == m_textBackgroundColour.Green ()));
1065
1066 // This separation of the big && test required for gcc2.7/HP UX 9.02
1067 // or pixel value can be corrupted!
1068 sameColour = (sameColour &&
1069 (oldPenColour.GetPixel() == m_textBackgroundColour.GetPixel()));
1070
1071 if (!sameColour || !GET_OPTIMIZATION)
1072 {
1073 WXPixel pixel = m_textBackgroundColour.AllocColour(m_display);
1074 m_currentColour = m_textBackgroundColour;
1075
1076 // Set the GC to the required colour
1077 if (pixel > -1)
1078 {
1079 XSetForeground ((Display*) m_display, (GC) m_gc, pixel);
1080 if (m_window && m_window->GetBackingPixmap())
1081 XSetForeground ((Display*) m_display,(GC) m_gcBacking, pixel);
1082 }
1083 }
1084 else
1085 m_textBackgroundColour = oldPenColour ;
1086
1087 XFillRectangle ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, XLOG2DEV (x), YLOG2DEV (y), cx, cy);
1088 if (m_window && m_window->GetBackingPixmap())
1089 XFillRectangle ((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
1090 XLOG2DEV_2 (x), YLOG2DEV_2 (y), cx, cy);
1091 }
1092
1093 // Now set the text foreground and draw the text
1094 if (m_textForegroundColour.IsOk ())
1095 {
1096 wxColour oldPenColour = m_currentColour;
1097 m_currentColour = m_textForegroundColour;
1098 bool sameColour = (oldPenColour.IsOk () && m_currentColour.IsOk () &&
1099 (oldPenColour.Red () == m_currentColour.Red ()) &&
1100 (oldPenColour.Blue () == m_currentColour.Blue ()) &&
1101 (oldPenColour.Green () == m_currentColour.Green ()) &&
1102 (oldPenColour.GetPixel() == m_currentColour.GetPixel()));
1103
1104 if (!sameColour || !GET_OPTIMIZATION)
1105 {
1106 WXPixel pixel = CalculatePixel(m_textForegroundColour,
1107 m_currentColour, false);
1108
1109 // Set the GC to the required colour
1110 if (pixel > -1)
1111 {
1112 XSetForeground ((Display*) m_display, (GC) m_gc, pixel);
1113 if (m_window && m_window->GetBackingPixmap())
1114 XSetForeground ((Display*) m_display,(GC) m_gcBacking, pixel);
1115 }
1116 }
1117 else
1118 m_textForegroundColour = oldPenColour;
1119 }
1120
1121 // We need to add the ascent, not the whole height, since X draws at the
1122 // point above the descender.
1123 #if 0
1124 if (use16)
1125 XDrawString16((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, XLOG2DEV (x), YLOG2DEV (y) + ascent,
1126 (XChar2b *)(char*) (const char*) text, slen);
1127 else
1128 #endif // 0
1129 #if wxMOTIF_NEW_FONT_HANDLING
1130 XFontSet fset = (XFontSet) m_font.GetFontSet (m_userScaleY * m_logicalScaleY, m_display);
1131 XmbDrawString((Display*) m_display, (Pixmap) m_pixmap, fset, (GC) m_gc, XLOG2DEV (x), YLOG2DEV (y) + ascent, text, slen);
1132 #else
1133 XDrawString((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc, XLOG2DEV (x), YLOG2DEV (y) + ascent, text, slen);
1134 #endif
1135
1136 if (m_window && m_window->GetBackingPixmap()) {
1137 #if 0
1138 if (use16)
1139 XDrawString16((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
1140 XLOG2DEV_2 (x), YLOG2DEV_2 (y) + ascent,
1141 (XChar2b *)(char*) (const char*) text, slen);
1142 else
1143 #endif // 0
1144 #if wxMOTIF_NEW_FONT_HANDLING
1145 XmbDrawString((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), fset, (GC) m_gcBacking,
1146 XLOG2DEV_2 (x), YLOG2DEV_2 (y) + ascent,
1147 text.mb_str(), slen);
1148 #else
1149 XDrawString((Display*) m_display, (Pixmap) m_window->GetBackingPixmap(), (GC) m_gcBacking,
1150 XLOG2DEV_2 (x), YLOG2DEV_2 (y) + ascent,
1151 text.mb_str(), slen);
1152 #endif
1153 }
1154
1155 // restore fill style
1156 XSetFillStyle ((Display*) m_display, (GC) m_gc, gcV.fill_style);
1157 if (m_window && m_window->GetBackingPixmap())
1158 XSetFillStyle ((Display*) m_display, (GC) m_gcBacking,
1159 gcBackingV.fill_style);
1160
1161 wxCoord w, h;
1162 DoGetTextExtent (text, &w, &h);
1163 CalcBoundingBox (x + w, y + h);
1164 CalcBoundingBox (x, y);
1165 }
1166
1167 void wxWindowDCImpl::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y,
1168 double angle )
1169 {
1170 if (angle == 0.0)
1171 {
1172 DoDrawText(text, x, y);
1173 return;
1174 }
1175
1176 wxCHECK_RET( IsOk(), "invalid dc" );
1177
1178 WXPixel oldBackgroundPixel = -1;
1179 WXPixel oldForegroundPixel = -1;
1180 WXPixel foregroundPixel = -1;
1181 WXPixel backgroundPixel = -1;
1182
1183 if (m_textBackgroundColour.IsOk())
1184 {
1185 oldBackgroundPixel = m_backgroundPixel;
1186 backgroundPixel = m_textBackgroundColour.AllocColour(m_display);
1187 }
1188 if (m_textForegroundColour.IsOk())
1189 {
1190 oldForegroundPixel = m_currentColour.GetPixel();
1191
1192 if( m_textForegroundColour.GetPixel() <= -1 )
1193 CalculatePixel( m_textForegroundColour,
1194 m_textForegroundColour, true);
1195
1196 foregroundPixel = m_textForegroundColour.GetPixel();
1197 }
1198
1199 // Since X draws from the baseline of the text, must add the text height
1200 int cx = 0;
1201 int cy = 0;
1202 int ascent = 0;
1203
1204 if (m_font.IsOk())
1205 wxGetTextExtent (m_display, m_font, m_userScaleY * m_logicalScaleY,
1206 text, &cx, &cy, &ascent, NULL);
1207
1208 wxBitmap src(cx, cy);
1209 wxMemoryDC dc;
1210 dc.SelectObject(src);
1211 dc.SetFont(GetFont());
1212 dc.SetBackground(*wxWHITE_BRUSH);
1213 dc.SetBrush(*wxBLACK_BRUSH);
1214 dc.Clear();
1215 dc.DrawText(text, 0, 0);
1216 dc.SetFont(wxNullFont);
1217
1218 // Calculate the size of the rotated bounding box.
1219 double dx = cos(angle / 180.0 * M_PI);
1220 double dy = sin(angle / 180.0 * M_PI);
1221 double x4 = cy * dy;
1222 double y4 = cy * dx;
1223 double x3 = cx * dx;
1224 double y3 = -cx * dy;
1225 double x2 = x3 + x4;
1226 double y2 = y3 + y4;
1227 double x1 = x;
1228 double y1 = y;
1229
1230 // Create image from the source bitmap after writing the text into it.
1231 wxImage image = src.ConvertToImage();
1232
1233 int minx = roundmin(0, roundmin(x4, roundmin(x2, x3)));
1234 int miny = roundmin(0, roundmin(y4, roundmin(y2, y3)));
1235 int maxx = roundmax(0, roundmax(x4, roundmax(x2, x3)));
1236 int maxy = roundmax(0, roundmax(y4, roundmax(y2, y3)));
1237
1238 bool lastFore = false, lastBack = false;
1239
1240 // This rotates counterclockwise around the top left corner.
1241 for (int rx = minx; rx < maxx; rx++)
1242 {
1243 for (int ry = miny; ry < maxy; ry++)
1244 {
1245 // transform dest coords to source coords
1246 int sx = (int) (rx * dx - ry * dy + 0.5);
1247 int sy = - (int) (-ry * dx - rx * dy + 0.5);
1248 if (sx >= 0 && sx < cx && sy >= 0 && sy < cy)
1249 {
1250 bool textPixel = image.GetRed(sx, sy) == 0;
1251
1252 if (!textPixel && m_backgroundMode != wxSOLID)
1253 continue;
1254
1255 wxCoord ox = (wxCoord) (x1 + rx),
1256 oy = (wxCoord) (y1 + ry);
1257 // draw black pixels, ignore white ones (i.e. transparent b/g)
1258 if (textPixel && !lastFore)
1259 {
1260 XSetForeground ((Display*) m_display, (GC) m_gc,
1261 foregroundPixel);
1262 lastFore = true;
1263 lastBack = false;
1264 }
1265 else if (!textPixel && !lastBack)
1266 {
1267 XSetForeground ((Display*) m_display, (GC) m_gc,
1268 backgroundPixel);
1269 lastFore = false;
1270 lastBack = true;
1271 }
1272
1273 XDrawPoint ((Display*) m_display, (Pixmap) m_pixmap,
1274 (GC) m_gc, XLOG2DEV (ox), YLOG2DEV (oy));
1275 if (m_window && m_window->GetBackingPixmap())
1276 XDrawPoint ((Display*) m_display,
1277 (Pixmap) m_window->GetBackingPixmap(),
1278 (GC) m_gcBacking,
1279 XLOG2DEV_2 (ox), YLOG2DEV_2 (oy));
1280 }
1281 }
1282 }
1283
1284 if (oldBackgroundPixel > -1)
1285 {
1286 XSetBackground ((Display*) m_display, (GC) m_gc, oldBackgroundPixel);
1287 if (m_window && m_window->GetBackingPixmap())
1288 XSetBackground ((Display*) m_display,(GC) m_gcBacking,
1289 oldBackgroundPixel);
1290 }
1291 if (oldForegroundPixel > -1)
1292 {
1293 XSetForeground ((Display*) m_display, (GC) m_gc, oldForegroundPixel);
1294 if (m_window && m_window->GetBackingPixmap())
1295 XSetForeground ((Display*) m_display,(GC) m_gcBacking,
1296 oldForegroundPixel);
1297 }
1298
1299 CalcBoundingBox (minx, miny);
1300 CalcBoundingBox (maxx, maxy);
1301 }
1302
1303 bool wxWindowDCImpl::CanGetTextExtent() const
1304 {
1305 return true;
1306 }
1307
1308 void wxWindowDCImpl::DoGetTextExtent( const wxString &string, wxCoord *width, wxCoord *height,
1309 wxCoord *descent, wxCoord *externalLeading,
1310 const wxFont *font ) const
1311 {
1312 wxCHECK_RET( IsOk(), "invalid dc" );
1313
1314 const wxFont* theFont = font ? font : &m_font;
1315
1316 if (!theFont->IsOk())
1317 {
1318 // TODO: this should be an error log function
1319 wxFAIL_MSG("set a valid font before calling GetTextExtent!");
1320
1321 if (width) *width = -1;
1322 if (height) *height = -1;
1323 return;
1324 }
1325
1326 wxGetTextExtent(m_display, *theFont, m_userScaleY * m_logicalScaleY,
1327 string, width, height, NULL, descent);
1328
1329 if (width) *width = XDEV2LOGREL (*width);
1330 if (height) *height = YDEV2LOGREL (*height);
1331 if (externalLeading)
1332 *externalLeading = 0;
1333 }
1334
1335 wxCoord wxWindowDCImpl::GetCharWidth() const
1336 {
1337 wxCHECK_MSG( IsOk(), 0, "invalid dc" );
1338 wxCHECK_MSG( m_font.IsOk(), 0, "invalid font" );
1339
1340 int width;
1341
1342 wxGetTextExtent (m_display, m_font, m_userScaleY * m_logicalScaleY,
1343 "x", &width, NULL, NULL, NULL);
1344
1345 return XDEV2LOGREL(width);
1346 }
1347
1348 wxCoord wxWindowDCImpl::GetCharHeight() const
1349 {
1350 wxCHECK_MSG( IsOk(), 0, "invalid dc" );
1351 wxCHECK_MSG( m_font.IsOk(), 0, "invalid font" );
1352
1353 int height;
1354
1355 wxGetTextExtent (m_display, m_font, m_userScaleY * m_logicalScaleY,
1356 "x", NULL, &height, NULL, NULL);
1357
1358 return XDEV2LOGREL(height);
1359 }
1360
1361 void wxWindowDCImpl::DoGetSize( int *width, int *height ) const
1362 {
1363 int w = 0, h = 0;
1364
1365 if( m_window )
1366 {
1367 if( m_window->GetBackingPixmap() )
1368 {
1369 w = m_window->GetPixmapWidth();
1370 h = m_window->GetPixmapHeight();
1371 }
1372 else
1373 m_window->GetSize( &w, &h );
1374 }
1375
1376 if( width ) *width = w;
1377 if( height ) *height = h;
1378 }
1379
1380 void wxWindowDCImpl::Clear()
1381 {
1382 wxCHECK_RET( IsOk(), "invalid dc" );
1383
1384 wxRect rect( GetSize() );
1385 Clear( rect );
1386 }
1387
1388 void wxWindowDCImpl::Clear(const wxRect& rect)
1389 {
1390 wxCHECK_RET( IsOk(), "invalid dc" );
1391
1392 int x = rect.x; int y = rect.y;
1393 int w = rect.width; int h = rect.height;
1394
1395 wxBrush saveBrush = m_brush;
1396 SetBrush (m_backgroundBrush);
1397
1398 XFillRectangle ((Display*) m_display, (Pixmap) m_pixmap, (GC) m_gc,
1399 x, y, w, h);
1400
1401 if (m_window && m_window->GetBackingPixmap())
1402 XFillRectangle ((Display*) m_display,
1403 (Pixmap) m_window->GetBackingPixmap(),(GC) m_gcBacking,
1404 x, y, w, h);
1405
1406 m_brush = saveBrush;
1407 }
1408
1409 void wxWindowDCImpl::SetFont( const wxFont &font )
1410 {
1411 wxCHECK_RET( IsOk(), "invalid dc" );
1412
1413 m_font = font;
1414
1415 if (!m_font.IsOk())
1416 {
1417 return;
1418 }
1419
1420 #if !wxMOTIF_NEW_FONT_HANDLING
1421 WXFontStructPtr pFontStruct = m_font.GetFontStruct(m_userScaleY*m_logicalScaleY, m_display);
1422
1423 Font fontId = ((XFontStruct*)pFontStruct)->fid;
1424 XSetFont ((Display*) m_display, (GC) m_gc, fontId);
1425
1426 if (m_window && m_window->GetBackingPixmap())
1427 XSetFont ((Display*) m_display,(GC) m_gcBacking, fontId);
1428 #endif
1429 }
1430
1431 void wxWindowDCImpl::SetForegroundPixelWithLogicalFunction(WXPixel pixel)
1432 {
1433 if (m_logicalFunction == wxXOR)
1434 {
1435 XGCValues values;
1436 XGetGCValues ((Display*) m_display, (GC) m_gc, GCBackground, &values);
1437 XSetForeground ((Display*) m_display, (GC) m_gc,
1438 pixel ^ values.background);
1439 if (m_window && m_window->GetBackingPixmap())
1440 XSetForeground ((Display*) m_display,(GC) m_gcBacking,
1441 pixel ^ values.background);
1442 }
1443 else
1444 {
1445 XSetForeground ((Display*) m_display, (GC) m_gc, pixel);
1446 if (m_window && m_window->GetBackingPixmap())
1447 XSetForeground ((Display*) m_display,(GC) m_gcBacking, pixel);
1448 }
1449 }
1450
1451 WXPixel wxWindowDCImpl::CalculatePixel(wxColour& colour, wxColour& curCol,
1452 bool roundToWhite) const
1453 {
1454 const unsigned char wp = (unsigned char)255;
1455
1456 WXPixel pixel = -1;
1457 if(!m_colour) // Mono display
1458 {
1459 unsigned char red = colour.Red ();
1460 unsigned char blue = colour.Blue ();
1461 unsigned char green = colour.Green ();
1462 // white
1463 if((red == wp && blue == wp && green == wp) ||
1464 // not black and roundToWhite was specified
1465 ((red != 0 || blue != 0 || green != 0) && roundToWhite))
1466 {
1467 curCol = *wxWHITE;
1468 pixel = WhitePixel((Display*) m_display,
1469 DefaultScreen((Display*) m_display));
1470 curCol.SetPixel(pixel);
1471 colour.SetPixel(pixel);
1472 }
1473 else
1474 {
1475 curCol = *wxBLACK;
1476 pixel = BlackPixel((Display*) m_display,
1477 DefaultScreen((Display*) m_display));
1478 curCol.SetPixel(pixel);
1479 colour.SetPixel(pixel);
1480 }
1481 }
1482 else
1483 {
1484 curCol = colour;
1485 pixel = colour.AllocColour((Display*) m_display);
1486 curCol.SetPixel(pixel);
1487 }
1488
1489 return pixel;
1490 }
1491
1492 void wxWindowDCImpl::SetPen( const wxPen &pen )
1493 {
1494 wxCHECK_RET( IsOk(), "invalid dc" );
1495
1496 m_pen = pen;
1497 if (!m_pen.IsOk())
1498 return;
1499
1500 wxBitmap oldStipple = m_currentStipple;
1501 int oldStyle = m_currentStyle;
1502 int oldFill = m_currentFill;
1503 int old_pen_width = m_currentPenWidth;
1504 int old_pen_join = m_currentPenJoin;
1505 int old_pen_cap = m_currentPenCap;
1506 int old_pen_nb_dash = m_currentPenDashCount;
1507 wxX11Dash *old_pen_dash = m_currentPenDash;
1508
1509 wxColour oldPenColour = m_currentColour;
1510 m_currentColour = m_pen.GetColour ();
1511 m_currentStyle = m_pen.GetStyle ();
1512 m_currentFill = m_pen.GetStyle (); // TODO?
1513 m_currentPenWidth = m_pen.GetWidth ();
1514 m_currentPenJoin = m_pen.GetJoin ();
1515 m_currentPenCap = m_pen.GetCap ();
1516 m_currentPenDashCount = m_pen.GetDashCount();
1517 m_currentPenDash = (wxX11Dash*)m_pen.GetDash();
1518
1519 if (m_currentStyle == wxSTIPPLE)
1520 m_currentStipple = * m_pen.GetStipple ();
1521
1522 bool sameStyle = (oldStyle == m_currentStyle &&
1523 oldFill == m_currentFill &&
1524 old_pen_join == m_currentPenJoin &&
1525 old_pen_cap == m_currentPenCap &&
1526 old_pen_nb_dash == m_currentPenDashCount &&
1527 old_pen_dash == m_currentPenDash &&
1528 old_pen_width == m_currentPenWidth);
1529
1530 bool sameColour = (oldPenColour.IsOk () &&
1531 (oldPenColour.Red () == m_currentColour.Red ()) &&
1532 (oldPenColour.Blue () == m_currentColour.Blue ()) &&
1533 (oldPenColour.Green () == m_currentColour.Green ()) &&
1534 (oldPenColour.GetPixel() == m_currentColour.GetPixel()));
1535
1536 if (!sameStyle || !GET_OPTIMIZATION)
1537 {
1538 int scaled_width = (int) XLOG2DEVREL (m_pen.GetWidth ());
1539 if (scaled_width < 0)
1540 scaled_width = 0;
1541
1542 int style;
1543 int join;
1544 int cap;
1545 static const wxX11Dash dotted[] = {2, 5};
1546 static const wxX11Dash short_dashed[] = {4, 4};
1547 static const wxX11Dash long_dashed[] = {4, 8};
1548 static const wxX11Dash dotted_dashed[] = {6, 6, 2, 6};
1549
1550 // We express dash pattern in pen width unit, so we are
1551 // independent of zoom factor and so on...
1552 int req_nb_dash;
1553 const wxX11Dash *req_dash;
1554
1555 switch (m_pen.GetStyle ())
1556 {
1557 case wxUSER_DASH:
1558 req_nb_dash = m_currentPenDashCount;
1559 req_dash = m_currentPenDash;
1560 style = LineOnOffDash;
1561 break;
1562 case wxDOT:
1563 req_nb_dash = 2;
1564 req_dash = dotted;
1565 style = LineOnOffDash;
1566 break;
1567 case wxSHORT_DASH:
1568 req_nb_dash = 2;
1569 req_dash = short_dashed;
1570 style = LineOnOffDash;
1571 break;
1572 case wxLONG_DASH:
1573 req_nb_dash = 2;
1574 req_dash = long_dashed;
1575 style = LineOnOffDash;
1576 break;
1577 case wxDOT_DASH:
1578 req_nb_dash = 4;
1579 req_dash = dotted_dashed;
1580 style = LineOnOffDash;
1581 break;
1582 case wxSTIPPLE:
1583 case wxSOLID:
1584 case wxTRANSPARENT:
1585 default:
1586 style = LineSolid;
1587 req_dash = NULL;
1588 req_nb_dash = 0;
1589 }
1590
1591 if (req_dash && req_nb_dash)
1592 {
1593 wxX11Dash *real_req_dash = new wxX11Dash[req_nb_dash];
1594 if (real_req_dash)
1595 {
1596 int factor = scaled_width == 0 ? 1 : scaled_width;
1597 for (int i = 0; i < req_nb_dash; i++)
1598 real_req_dash[i] = (wxX11Dash)(req_dash[i] * factor);
1599 XSetDashes ((Display*) m_display, (GC) m_gc, 0, real_req_dash, req_nb_dash);
1600
1601 if (m_window && m_window->GetBackingPixmap())
1602 XSetDashes ((Display*) m_display,(GC) m_gcBacking, 0, real_req_dash, req_nb_dash);
1603 delete[]real_req_dash;
1604 }
1605 else
1606 {
1607 // No Memory. We use non-scaled dash pattern...
1608 XSetDashes ((Display*) m_display, (GC) m_gc, 0, req_dash, req_nb_dash);
1609
1610 if (m_window && m_window->GetBackingPixmap())
1611 XSetDashes ((Display*) m_display,(GC) m_gcBacking, 0, req_dash, req_nb_dash);
1612 }
1613 }
1614
1615 switch (m_pen.GetCap ())
1616 {
1617 case wxCAP_PROJECTING:
1618 cap = CapProjecting;
1619 break;
1620 case wxCAP_BUTT:
1621 cap = CapButt;
1622 break;
1623 case wxCAP_ROUND:
1624 default:
1625 cap = (scaled_width <= 1) ? CapNotLast : CapRound;
1626 break;
1627 }
1628
1629 switch (m_pen.GetJoin ())
1630 {
1631 case wxJOIN_BEVEL:
1632 join = JoinBevel;
1633 break;
1634 case wxJOIN_MITER:
1635 join = JoinMiter;
1636 break;
1637 case wxJOIN_ROUND:
1638 default:
1639 join = JoinRound;
1640 break;
1641 }
1642
1643 XSetLineAttributes ((Display*) m_display, (GC) m_gc, scaled_width, style, cap, join);
1644
1645 if (m_window && m_window->GetBackingPixmap())
1646 XSetLineAttributes ((Display*) m_display,(GC) m_gcBacking, scaled_width, style, cap, join);
1647 }
1648
1649 if (IS_HATCH(m_currentFill) && ((m_currentFill != oldFill) || !GET_OPTIMIZATION))
1650 {
1651 Pixmap myStipple;
1652
1653 oldStipple = wxNullBitmap; // For later reset!!
1654
1655 switch (m_currentFill)
1656 {
1657 case wxBDIAGONAL_HATCH:
1658 if (bdiag == (Pixmap) 0)
1659 bdiag = XCreateBitmapFromData ((Display*) m_display,
1660 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1661 bdiag_bits, bdiag_width, bdiag_height);
1662 myStipple = bdiag;
1663 break;
1664 case wxFDIAGONAL_HATCH:
1665 if (fdiag == (Pixmap) 0)
1666 fdiag = XCreateBitmapFromData ((Display*) m_display,
1667 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1668 fdiag_bits, fdiag_width, fdiag_height);
1669 myStipple = fdiag;
1670 break;
1671 case wxCROSS_HATCH:
1672 if (cross == (Pixmap) 0)
1673 cross = XCreateBitmapFromData ((Display*) m_display,
1674 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1675 cross_bits, cross_width, cross_height);
1676 myStipple = cross;
1677 break;
1678 case wxHORIZONTAL_HATCH:
1679 if (horiz == (Pixmap) 0)
1680 horiz = XCreateBitmapFromData ((Display*) m_display,
1681 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1682 horiz_bits, horiz_width, horiz_height);
1683 myStipple = horiz;
1684 break;
1685 case wxVERTICAL_HATCH:
1686 if (verti == (Pixmap) 0)
1687 verti = XCreateBitmapFromData ((Display*) m_display,
1688 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1689 verti_bits, verti_width, verti_height);
1690 myStipple = verti;
1691 break;
1692 case wxCROSSDIAG_HATCH:
1693 default:
1694 if (cdiag == (Pixmap) 0)
1695 cdiag = XCreateBitmapFromData ((Display*) m_display,
1696 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1697 cdiag_bits, cdiag_width, cdiag_height);
1698 myStipple = cdiag;
1699 break;
1700 }
1701 XSetStipple ((Display*) m_display, (GC) m_gc, myStipple);
1702
1703 if (m_window && m_window->GetBackingPixmap())
1704 XSetStipple ((Display*) m_display,(GC) m_gcBacking, myStipple);
1705 }
1706 else if (m_currentStyle == wxSTIPPLE && m_currentStipple.IsOk()
1707 && ((!m_currentStipple.IsSameAs(oldStipple)) || !GET_OPTIMIZATION))
1708 {
1709 XSetStipple ((Display*) m_display, (GC) m_gc, (Pixmap) m_currentStipple.GetDrawable());
1710
1711 if (m_window && m_window->GetBackingPixmap())
1712 XSetStipple ((Display*) m_display,(GC) m_gcBacking, (Pixmap) m_currentStipple.GetDrawable());
1713 }
1714
1715 if ((m_currentFill != oldFill) || !GET_OPTIMIZATION)
1716 {
1717 int fill_style;
1718
1719 if (m_currentFill == wxSTIPPLE)
1720 fill_style = FillStippled;
1721 else if (IS_HATCH (m_currentFill))
1722 fill_style = FillStippled;
1723 else
1724 fill_style = FillSolid;
1725 XSetFillStyle ((Display*) m_display, (GC) m_gc, fill_style);
1726 if (m_window && m_window->GetBackingPixmap())
1727 XSetFillStyle ((Display*) m_display,(GC) m_gcBacking, fill_style);
1728 }
1729
1730 // must test m_logicalFunction, because it involves background!
1731 if (!sameColour || !GET_OPTIMIZATION
1732 || ((m_logicalFunction == wxXOR) || (m_autoSetting & 0x2)))
1733 {
1734 WXPixel pixel = -1;
1735 if (m_pen.GetStyle () == wxTRANSPARENT)
1736 pixel = m_backgroundPixel;
1737 else
1738 {
1739 wxColour penClr = m_pen.GetColour();
1740 pixel = CalculatePixel( penClr, m_currentColour, false);
1741 m_pen.SetColour(penClr);
1742 }
1743
1744 // Finally, set the GC to the required colour
1745 if (pixel > -1)
1746 SetForegroundPixelWithLogicalFunction(pixel);
1747 }
1748 else
1749 m_pen.GetColour().SetPixel(oldPenColour.GetPixel());
1750
1751 m_autoSetting = 0;
1752 }
1753
1754 void wxWindowDCImpl::SetBrush( const wxBrush &brush )
1755 {
1756 wxCHECK_RET( IsOk(), "invalid dc" );
1757
1758 m_brush = brush;
1759
1760 if (!m_brush.IsOk() || m_brush.GetStyle () == wxTRANSPARENT)
1761 return;
1762
1763 int oldFill = m_currentFill;
1764 wxBitmap oldStipple = m_currentStipple;
1765
1766 m_autoSetting |= 0x1;
1767
1768 m_currentFill = m_brush.GetStyle ();
1769 if (m_currentFill == wxSTIPPLE)
1770 m_currentStipple = * m_brush.GetStipple ();
1771
1772 wxColour oldBrushColour(m_currentColour);
1773 m_currentColour = m_brush.GetColour ();
1774
1775 bool sameColour = (oldBrushColour.IsOk () &&
1776 (oldBrushColour.Red () == m_currentColour.Red ()) &&
1777 (oldBrushColour.Blue () == m_currentColour.Blue ()) &&
1778 (oldBrushColour.Green () == m_currentColour.Green ()) &&
1779 (oldBrushColour.GetPixel() == m_currentColour.GetPixel()));
1780
1781 int stippleDepth = -1;
1782
1783 if ((oldFill != m_brush.GetStyle ()) || !GET_OPTIMIZATION)
1784 {
1785 switch (brush.GetStyle ())
1786 {
1787 case wxTRANSPARENT:
1788 break;
1789 case wxSTIPPLE:
1790 stippleDepth = m_currentStipple.GetDepth();
1791 // fall through!
1792 case wxBDIAGONAL_HATCH:
1793 case wxCROSSDIAG_HATCH:
1794 case wxFDIAGONAL_HATCH:
1795 case wxCROSS_HATCH:
1796 case wxHORIZONTAL_HATCH:
1797 case wxVERTICAL_HATCH:
1798 {
1799 if (stippleDepth == -1) stippleDepth = 1;
1800
1801 // Chris Breeze 23/07/97: use background mode to
1802 // determine whether fill style should be solid or
1803 // transparent
1804 int style = stippleDepth == 1 ?
1805 (m_backgroundMode == wxSOLID ?
1806 FillOpaqueStippled : FillStippled) :
1807 FillTiled;
1808 XSetFillStyle ((Display*) m_display, (GC) m_gc, style);
1809 if (m_window && m_window->GetBackingPixmap())
1810 XSetFillStyle ((Display*) m_display,(GC) m_gcBacking, style);
1811 }
1812 break;
1813 case wxSOLID:
1814 default:
1815 XSetFillStyle ((Display*) m_display, (GC) m_gc, FillSolid);
1816 if (m_window && m_window->GetBackingPixmap())
1817 XSetFillStyle ((Display*) m_display,(GC) m_gcBacking,
1818 FillSolid);
1819 }
1820 }
1821
1822 if (IS_HATCH(m_currentFill) && ((m_currentFill != oldFill) || !GET_OPTIMIZATION))
1823 {
1824 Pixmap myStipple;
1825
1826 switch (m_currentFill)
1827 {
1828 case wxBDIAGONAL_HATCH:
1829 if (bdiag == (Pixmap) 0)
1830 bdiag = XCreateBitmapFromData ((Display*) m_display,
1831 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1832 bdiag_bits, bdiag_width, bdiag_height);
1833 myStipple = bdiag;
1834 break;
1835 case wxFDIAGONAL_HATCH:
1836 if (fdiag == (Pixmap) 0)
1837 fdiag = XCreateBitmapFromData ((Display*) m_display,
1838 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1839 fdiag_bits, fdiag_width, fdiag_height);
1840 myStipple = fdiag;
1841 break;
1842 case wxCROSS_HATCH:
1843 if (cross == (Pixmap) 0)
1844 cross = XCreateBitmapFromData ((Display*) m_display,
1845 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1846 cross_bits, cross_width, cross_height);
1847 myStipple = cross;
1848 break;
1849 case wxHORIZONTAL_HATCH:
1850 if (horiz == (Pixmap) 0)
1851 horiz = XCreateBitmapFromData ((Display*) m_display,
1852 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1853 horiz_bits, horiz_width, horiz_height);
1854 myStipple = horiz;
1855 break;
1856 case wxVERTICAL_HATCH:
1857 if (verti == (Pixmap) 0)
1858 verti = XCreateBitmapFromData ((Display*) m_display,
1859 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1860 verti_bits, verti_width, verti_height);
1861 myStipple = verti;
1862 break;
1863 case wxCROSSDIAG_HATCH:
1864 default:
1865 if (cdiag == (Pixmap) 0)
1866 cdiag = XCreateBitmapFromData ((Display*) m_display,
1867 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1868 cdiag_bits, cdiag_width, cdiag_height);
1869 myStipple = cdiag;
1870 break;
1871 }
1872 XSetStipple ((Display*) m_display, (GC) m_gc, myStipple);
1873
1874 if (m_window && m_window->GetBackingPixmap())
1875 XSetStipple ((Display*) m_display,(GC) m_gcBacking, myStipple);
1876 }
1877 // X can forget the stipple value when resizing a window (apparently)
1878 // so always set the stipple.
1879 else if (m_currentFill != wxSOLID && m_currentFill != wxTRANSPARENT &&
1880 m_currentStipple.IsOk()) // && m_currentStipple != oldStipple)
1881 {
1882 if (m_currentStipple.GetDepth() == 1)
1883 {
1884 XSetStipple ((Display*) m_display, (GC) m_gc,
1885 (Pixmap) m_currentStipple.GetDrawable());
1886 if (m_window && m_window->GetBackingPixmap())
1887 XSetStipple ((Display*) m_display,(GC) m_gcBacking,
1888 (Pixmap) m_currentStipple.GetDrawable());
1889 }
1890 else
1891 {
1892 XSetTile ((Display*) m_display, (GC) m_gc,
1893 (Pixmap) m_currentStipple.GetDrawable());
1894 if (m_window && m_window->GetBackingPixmap())
1895 XSetTile ((Display*) m_display,(GC) m_gcBacking,
1896 (Pixmap) m_currentStipple.GetDrawable());
1897 }
1898 }
1899
1900 // must test m_logicalFunction, because it involves background!
1901 if (!sameColour || !GET_OPTIMIZATION || m_logicalFunction == wxXOR)
1902 {
1903 wxColour brushClr = m_brush.GetColour();
1904 WXPixel pixel = CalculatePixel( brushClr, m_currentColour, true);
1905 m_brush.SetColour(brushClr);
1906
1907 if (pixel > -1)
1908 SetForegroundPixelWithLogicalFunction(pixel);
1909 }
1910 else
1911 m_brush.GetColour().SetPixel(oldBrushColour.GetPixel());
1912 }
1913
1914 void wxWindowDCImpl::SetBackground( const wxBrush &brush )
1915 {
1916 wxCHECK_RET( IsOk(), "invalid dc" );
1917
1918 m_backgroundBrush = brush;
1919
1920 if (!m_backgroundBrush.IsOk())
1921 return;
1922
1923 m_backgroundPixel = m_backgroundBrush.GetColour().AllocColour(m_display);
1924
1925 // Necessary for ::DrawIcon, which use fg/bg pixel or the GC.
1926 // And Blit,... (Any fct that use XCopyPlane, in fact.)
1927 XSetBackground ((Display*) m_display, (GC) m_gc, m_backgroundPixel);
1928 if (m_window && m_window->GetBackingPixmap())
1929 XSetBackground ((Display*) m_display,(GC) m_gcBacking,
1930 m_backgroundPixel);
1931 }
1932
1933 void wxWindowDCImpl::SetLogicalFunction( wxRasterOperationMode function )
1934 {
1935 wxCHECK_RET( IsOk(), "invalid dc" );
1936
1937 int x_function;
1938
1939 /* MATTHEW: [9] */
1940 if (m_logicalFunction == function)
1941 return;
1942
1943 switch (function)
1944 {
1945 case wxCLEAR:
1946 x_function = GXclear;
1947 break;
1948 case wxXOR:
1949 x_function = GXxor;
1950 break;
1951 case wxINVERT:
1952 x_function = GXinvert;
1953 break;
1954 case wxOR_REVERSE:
1955 x_function = GXorReverse;
1956 break;
1957 case wxAND_REVERSE:
1958 x_function = GXandReverse;
1959 break;
1960 case wxAND:
1961 x_function = GXand;
1962 break;
1963 case wxOR:
1964 x_function = GXor;
1965 break;
1966 case wxAND_INVERT:
1967 x_function = GXandInverted;
1968 break;
1969 case wxNO_OP:
1970 x_function = GXnoop;
1971 break;
1972 case wxNOR:
1973 x_function = GXnor;
1974 break;
1975 case wxEQUIV:
1976 x_function = GXequiv;
1977 break;
1978 case wxSRC_INVERT:
1979 x_function = GXcopyInverted;
1980 break;
1981 case wxOR_INVERT:
1982 x_function = GXorInverted;
1983 break;
1984 case wxNAND:
1985 x_function = GXnand;
1986 break;
1987 case wxSET:
1988 x_function = GXset;
1989 break;
1990 case wxCOPY:
1991 default:
1992 x_function = GXcopy;
1993 break;
1994 }
1995
1996 XSetFunction((Display*) m_display, (GC) m_gc, x_function);
1997 if (m_window && m_window->GetBackingPixmap())
1998 XSetFunction((Display*) m_display, (GC) m_gcBacking, x_function);
1999
2000 if ((m_logicalFunction == wxXOR) != (function == wxXOR))
2001 /* MATTHEW: [9] Need to redo pen simply */
2002 m_autoSetting |= 0x2;
2003
2004 m_logicalFunction = function;
2005
2006 }
2007
2008 void wxWindowDCImpl::SetTextForeground( const wxColour &col )
2009 {
2010 wxCHECK_RET( IsOk(), "invalid dc" );
2011
2012 m_textForegroundColour = col;
2013 }
2014
2015 void wxWindowDCImpl::SetTextBackground( const wxColour &col )
2016 {
2017 wxCHECK_RET( IsOk(), "invalid dc" );
2018
2019 m_textBackgroundColour = col;
2020 }
2021
2022 void wxWindowDCImpl::SetBackgroundMode( int mode )
2023 {
2024 m_backgroundMode = mode;
2025 }
2026
2027 void wxWindowDCImpl::SetPalette( const wxPalette& palette )
2028 {
2029 if (m_window)
2030 {
2031 if (palette.IsOk())
2032 /* Use GetXColormap */
2033 XSetWindowColormap ((Display*) m_display, (Window) m_window->GetXWindow(),
2034 (Colormap) palette.GetXColormap());
2035 else
2036 /* Use wxGetMainColormap */
2037 XSetWindowColormap ((Display*) m_display, (Window) m_window->GetXWindow(),
2038 (Colormap) wxTheApp->GetMainColormap(m_display));
2039 }
2040 }
2041
2042 static void wxCopyRegion( WXRegion src, WXRegion& dst )
2043 {
2044 if( !dst )
2045 dst = XCreateRegion();
2046 XUnionRegion( (Region)src, (Region)src, (Region)dst );
2047 }
2048
2049 // Helper function; userRegion is the region set by calling SetClippingRegion
2050 void wxWindowDCImpl::SetDCClipping( WXRegion userRegion )
2051 {
2052 bool hasUpdateRegion = m_window && m_window->GetUpdateRegion().IsOk();
2053 // this means that we should start the clip region from scratch,
2054 // or from the update region, if any
2055 if( !userRegion )
2056 {
2057 if( m_clipRegion )
2058 XDestroyRegion( (Region)m_clipRegion );
2059 m_clipRegion = (WXRegion)NULL;
2060
2061 if( hasUpdateRegion )
2062 wxCopyRegion( m_window->GetUpdateRegion().GetX11Region(),
2063 m_clipRegion );
2064 }
2065 // intersect the user region, if any, with the
2066 // exisiting clip region
2067 else // if( userRegion )
2068 {
2069 if( !m_clipRegion )
2070 wxCopyRegion( userRegion, m_clipRegion );
2071 else
2072 XIntersectRegion( (Region)m_clipRegion,
2073 (Region)userRegion, (Region)m_clipRegion );
2074 }
2075
2076 if( m_clipRegion )
2077 XSetRegion( (Display*)m_display, (GC)m_gc, (Region)m_clipRegion );
2078 else
2079 XSetClipMask( (Display*)m_display, (GC)m_gc, None );
2080 }
2081
2082 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y,
2083 wxCoord width, wxCoord height )
2084 {
2085 wxMotifDCImpl::DoSetClippingRegion( x, y, width, height );
2086
2087 wxRegion temp(XLOG2DEV(x), YLOG2DEV(y),
2088 XLOG2DEVREL(width), YLOG2DEVREL(height));
2089
2090 SetDCClipping(temp.GetX11Region());
2091
2092 // Needs to work differently for Pixmap: without this,
2093 // there's a nasty (Display*) m_display bug. 8/12/94
2094 if (m_window && m_window->GetBackingPixmap())
2095 {
2096 XRectangle rects[1];
2097 rects[0].x = (short)XLOG2DEV_2(x);
2098 rects[0].y = (short)YLOG2DEV_2(y);
2099 rects[0].width = (unsigned short)XLOG2DEVREL(width);
2100 rects[0].height = (unsigned short)YLOG2DEVREL(height);
2101 XSetClipRectangles((Display*) m_display, (GC) m_gcBacking,
2102 0, 0, rects, 1, Unsorted);
2103 }
2104 }
2105
2106 void wxWindowDCImpl::DoSetDeviceClippingRegion( const wxRegion& region )
2107 {
2108 SetDCClipping(region.GetX11Region());
2109
2110 // Needs to work differently for Pixmap: without this,
2111 // there's a nasty (Display*) m_display bug. 8/12/94
2112 if (m_window && m_window->GetBackingPixmap())
2113 {
2114 wxRect box = region.GetBox();
2115
2116 XRectangle rects[1];
2117 rects[0].x = (short)box.x;
2118 rects[0].y = (short)box.y;
2119 rects[0].width = (unsigned short)box.width;
2120 rects[0].height = (unsigned short)box.height;
2121 XSetClipRectangles((Display*) m_display, (GC) m_gcBacking,
2122 0, 0, rects, 1, Unsorted);
2123 }
2124 }
2125
2126
2127 void wxWindowDCImpl::DestroyClippingRegion()
2128 {
2129 wxMotifDCImpl::DestroyClippingRegion();
2130
2131 SetDCClipping(NULL);
2132
2133 if (m_window && m_window->GetBackingPixmap())
2134 XSetClipMask ((Display*) m_display, (GC) m_gcBacking, None);
2135 }
2136
2137 // Resolution in pixels per logical inch
2138 wxSize wxWindowDCImpl::GetPPI() const
2139 {
2140 // TODO
2141 return wxSize(100, 100);
2142 }
2143
2144 int wxWindowDCImpl::GetDepth() const
2145 {
2146 // TODO
2147 return 24;
2148 }
2149
2150
2151
2152
2153 // ----------------------------------------------------------------------------
2154 // wxPaintDCImpl
2155 // ----------------------------------------------------------------------------
2156
2157 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner, wxWindow* win)
2158 : wxWindowDCImpl(owner, win)
2159 {
2160 // Set the clipping region.to the update region
2161 SetDCClipping((WXRegion)NULL);
2162 }
2163
2164 wxPaintDCImpl::~wxPaintDCImpl()
2165 {
2166 if (m_window)
2167 m_window->ClearUpdateRegion();
2168 SetDCClipping((WXRegion)NULL);
2169 }
2170
2171 // ----------------------------------------------------------------------------
2172 // private functions
2173 // ----------------------------------------------------------------------------
2174
2175 /*
2176 Used when copying between drawables on different (Display*) m_displays. Not
2177 very fast, but better than giving up.
2178 */
2179
2180 static void XCopyRemote(Display *src_display, Display *dest_display,
2181 Drawable src, Drawable dest,
2182 GC destgc,
2183 int srcx, int srcy,
2184 unsigned int w, unsigned int h,
2185 int destx, int desty,
2186 bool more, XImage **cache)
2187 {
2188 XImage *image, *destimage;
2189 Colormap destcm, srccm;
2190 static const int CACHE_SIZE = 256;
2191
2192 unsigned int i, j;
2193 Pixel cachesrc[CACHE_SIZE], cachedest[CACHE_SIZE];
2194 int k, cache_pos, all_cache;
2195
2196 if (!cache || !*cache)
2197 image = XGetImage(src_display, src, srcx, srcy, w, h, AllPlanes, ZPixmap);
2198 else
2199 image = *cache;
2200
2201 destimage = XGetImage(dest_display, dest, destx, desty, w, h, AllPlanes, ZPixmap);
2202
2203 srccm = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) src_display);
2204 destcm = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dest_display);
2205
2206 cache_pos = 0;
2207 all_cache = False;
2208
2209 for (i = 0; i < w; i++)
2210 for (j = 0; j < h; j++) {
2211 Pixel pixel;
2212 XColor xcol;
2213
2214 pixel = XGetPixel(image, i, j);
2215 for (k = cache_pos; k--; )
2216 if (cachesrc[k] == pixel) {
2217 pixel = cachedest[k];
2218 goto install;
2219 }
2220 if (all_cache)
2221 for (k = CACHE_SIZE; k-- > cache_pos; )
2222 if (cachesrc[k] == pixel) {
2223 pixel = cachedest[k];
2224 goto install;
2225 }
2226
2227 cachesrc[cache_pos] = xcol.pixel = pixel;
2228 XQueryColor(src_display, srccm, &xcol);
2229 if (!XAllocColor(dest_display, destcm, &xcol))
2230 xcol.pixel = 0;
2231 cachedest[cache_pos] = pixel = xcol.pixel;
2232
2233 if (++cache_pos >= CACHE_SIZE) {
2234 cache_pos = 0;
2235 all_cache = true;
2236 }
2237
2238 install:
2239 XPutPixel(destimage, i, j, pixel);
2240 }
2241
2242 XPutImage(dest_display, dest, destgc, destimage, 0, 0, destx, desty, w, h);
2243 XDestroyImage(destimage);
2244
2245 if (more && cache)
2246 *cache = image;
2247 else
2248 XDestroyImage(image);
2249 }
2250