]> git.saurik.com Git - wxWidgets.git/blob - src/motif/dcclient.cpp
[ 1936700 ] wxCAL_SHOW_WEEK_NUMBERS, slightly modified
[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 = (wxX11Dash*) NULL;
145 m_currentStyle = -1;
146 m_currentFill = -1;
147 m_colour = wxColourDisplay();
148 m_display = (WXDisplay*) 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 != (wxWindow*) 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, int style);
226
227 bool wxWindowDCImpl::DoFloodFill(wxCoord x, wxCoord y,
228 const wxColour& col, int 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, int 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 int 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 = (wxBitmap*) 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 int 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 = (wxX11Dash*)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 pixel = CalculatePixel( (wxColour&) m_pen.GetColour(), m_currentColour, false);
1740 }
1741
1742 // Finally, set the GC to the required colour
1743 if (pixel > -1)
1744 SetForegroundPixelWithLogicalFunction(pixel);
1745 }
1746 else
1747 m_pen.GetColour().SetPixel(oldPenColour.GetPixel());
1748
1749 m_autoSetting = 0;
1750 }
1751
1752 void wxWindowDCImpl::SetBrush( const wxBrush &brush )
1753 {
1754 wxCHECK_RET( IsOk(), "invalid dc" );
1755
1756 m_brush = brush;
1757
1758 if (!m_brush.IsOk() || m_brush.GetStyle () == wxTRANSPARENT)
1759 return;
1760
1761 int oldFill = m_currentFill;
1762 wxBitmap oldStipple = m_currentStipple;
1763
1764 m_autoSetting |= 0x1;
1765
1766 m_currentFill = m_brush.GetStyle ();
1767 if (m_currentFill == wxSTIPPLE)
1768 m_currentStipple = * m_brush.GetStipple ();
1769
1770 wxColour oldBrushColour(m_currentColour);
1771 m_currentColour = m_brush.GetColour ();
1772
1773 bool sameColour = (oldBrushColour.IsOk () &&
1774 (oldBrushColour.Red () == m_currentColour.Red ()) &&
1775 (oldBrushColour.Blue () == m_currentColour.Blue ()) &&
1776 (oldBrushColour.Green () == m_currentColour.Green ()) &&
1777 (oldBrushColour.GetPixel() == m_currentColour.GetPixel()));
1778
1779 int stippleDepth = -1;
1780
1781 if ((oldFill != m_brush.GetStyle ()) || !GET_OPTIMIZATION)
1782 {
1783 switch (brush.GetStyle ())
1784 {
1785 case wxTRANSPARENT:
1786 break;
1787 case wxSTIPPLE:
1788 stippleDepth = m_currentStipple.GetDepth();
1789 // fall through!
1790 case wxBDIAGONAL_HATCH:
1791 case wxCROSSDIAG_HATCH:
1792 case wxFDIAGONAL_HATCH:
1793 case wxCROSS_HATCH:
1794 case wxHORIZONTAL_HATCH:
1795 case wxVERTICAL_HATCH:
1796 {
1797 if (stippleDepth == -1) stippleDepth = 1;
1798
1799 // Chris Breeze 23/07/97: use background mode to
1800 // determine whether fill style should be solid or
1801 // transparent
1802 int style = stippleDepth == 1 ?
1803 (m_backgroundMode == wxSOLID ?
1804 FillOpaqueStippled : FillStippled) :
1805 FillTiled;
1806 XSetFillStyle ((Display*) m_display, (GC) m_gc, style);
1807 if (m_window && m_window->GetBackingPixmap())
1808 XSetFillStyle ((Display*) m_display,(GC) m_gcBacking, style);
1809 }
1810 break;
1811 case wxSOLID:
1812 default:
1813 XSetFillStyle ((Display*) m_display, (GC) m_gc, FillSolid);
1814 if (m_window && m_window->GetBackingPixmap())
1815 XSetFillStyle ((Display*) m_display,(GC) m_gcBacking,
1816 FillSolid);
1817 }
1818 }
1819
1820 if (IS_HATCH(m_currentFill) && ((m_currentFill != oldFill) || !GET_OPTIMIZATION))
1821 {
1822 Pixmap myStipple;
1823
1824 switch (m_currentFill)
1825 {
1826 case wxBDIAGONAL_HATCH:
1827 if (bdiag == (Pixmap) 0)
1828 bdiag = XCreateBitmapFromData ((Display*) m_display,
1829 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1830 bdiag_bits, bdiag_width, bdiag_height);
1831 myStipple = bdiag;
1832 break;
1833 case wxFDIAGONAL_HATCH:
1834 if (fdiag == (Pixmap) 0)
1835 fdiag = XCreateBitmapFromData ((Display*) m_display,
1836 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1837 fdiag_bits, fdiag_width, fdiag_height);
1838 myStipple = fdiag;
1839 break;
1840 case wxCROSS_HATCH:
1841 if (cross == (Pixmap) 0)
1842 cross = XCreateBitmapFromData ((Display*) m_display,
1843 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1844 cross_bits, cross_width, cross_height);
1845 myStipple = cross;
1846 break;
1847 case wxHORIZONTAL_HATCH:
1848 if (horiz == (Pixmap) 0)
1849 horiz = XCreateBitmapFromData ((Display*) m_display,
1850 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1851 horiz_bits, horiz_width, horiz_height);
1852 myStipple = horiz;
1853 break;
1854 case wxVERTICAL_HATCH:
1855 if (verti == (Pixmap) 0)
1856 verti = XCreateBitmapFromData ((Display*) m_display,
1857 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1858 verti_bits, verti_width, verti_height);
1859 myStipple = verti;
1860 break;
1861 case wxCROSSDIAG_HATCH:
1862 default:
1863 if (cdiag == (Pixmap) 0)
1864 cdiag = XCreateBitmapFromData ((Display*) m_display,
1865 RootWindow ((Display*) m_display, DefaultScreen ((Display*) m_display)),
1866 cdiag_bits, cdiag_width, cdiag_height);
1867 myStipple = cdiag;
1868 break;
1869 }
1870 XSetStipple ((Display*) m_display, (GC) m_gc, myStipple);
1871
1872 if (m_window && m_window->GetBackingPixmap())
1873 XSetStipple ((Display*) m_display,(GC) m_gcBacking, myStipple);
1874 }
1875 // X can forget the stipple value when resizing a window (apparently)
1876 // so always set the stipple.
1877 else if (m_currentFill != wxSOLID && m_currentFill != wxTRANSPARENT &&
1878 m_currentStipple.IsOk()) // && m_currentStipple != oldStipple)
1879 {
1880 if (m_currentStipple.GetDepth() == 1)
1881 {
1882 XSetStipple ((Display*) m_display, (GC) m_gc,
1883 (Pixmap) m_currentStipple.GetDrawable());
1884 if (m_window && m_window->GetBackingPixmap())
1885 XSetStipple ((Display*) m_display,(GC) m_gcBacking,
1886 (Pixmap) m_currentStipple.GetDrawable());
1887 }
1888 else
1889 {
1890 XSetTile ((Display*) m_display, (GC) m_gc,
1891 (Pixmap) m_currentStipple.GetDrawable());
1892 if (m_window && m_window->GetBackingPixmap())
1893 XSetTile ((Display*) m_display,(GC) m_gcBacking,
1894 (Pixmap) m_currentStipple.GetDrawable());
1895 }
1896 }
1897
1898 // must test m_logicalFunction, because it involves background!
1899 if (!sameColour || !GET_OPTIMIZATION || m_logicalFunction == wxXOR)
1900 {
1901 WXPixel pixel = CalculatePixel( (wxColour&) m_brush.GetColour(), m_currentColour, true);
1902
1903 if (pixel > -1)
1904 SetForegroundPixelWithLogicalFunction(pixel);
1905 }
1906 else
1907 m_brush.GetColour().SetPixel(oldBrushColour.GetPixel());
1908 }
1909
1910 void wxWindowDCImpl::SetBackground( const wxBrush &brush )
1911 {
1912 wxCHECK_RET( IsOk(), "invalid dc" );
1913
1914 m_backgroundBrush = brush;
1915
1916 if (!m_backgroundBrush.IsOk())
1917 return;
1918
1919 m_backgroundPixel = m_backgroundBrush.GetColour().AllocColour(m_display);
1920
1921 // Necessary for ::DrawIcon, which use fg/bg pixel or the GC.
1922 // And Blit,... (Any fct that use XCopyPlane, in fact.)
1923 XSetBackground ((Display*) m_display, (GC) m_gc, m_backgroundPixel);
1924 if (m_window && m_window->GetBackingPixmap())
1925 XSetBackground ((Display*) m_display,(GC) m_gcBacking,
1926 m_backgroundPixel);
1927 }
1928
1929 void wxWindowDCImpl::SetLogicalFunction( int function )
1930 {
1931 wxCHECK_RET( IsOk(), "invalid dc" );
1932
1933 int x_function;
1934
1935 /* MATTHEW: [9] */
1936 if (m_logicalFunction == function)
1937 return;
1938
1939 switch (function)
1940 {
1941 case wxCLEAR:
1942 x_function = GXclear;
1943 break;
1944 case wxXOR:
1945 x_function = GXxor;
1946 break;
1947 case wxINVERT:
1948 x_function = GXinvert;
1949 break;
1950 case wxOR_REVERSE:
1951 x_function = GXorReverse;
1952 break;
1953 case wxAND_REVERSE:
1954 x_function = GXandReverse;
1955 break;
1956 case wxAND:
1957 x_function = GXand;
1958 break;
1959 case wxOR:
1960 x_function = GXor;
1961 break;
1962 case wxAND_INVERT:
1963 x_function = GXandInverted;
1964 break;
1965 case wxNO_OP:
1966 x_function = GXnoop;
1967 break;
1968 case wxNOR:
1969 x_function = GXnor;
1970 break;
1971 case wxEQUIV:
1972 x_function = GXequiv;
1973 break;
1974 case wxSRC_INVERT:
1975 x_function = GXcopyInverted;
1976 break;
1977 case wxOR_INVERT:
1978 x_function = GXorInverted;
1979 break;
1980 case wxNAND:
1981 x_function = GXnand;
1982 break;
1983 case wxSET:
1984 x_function = GXset;
1985 break;
1986 case wxCOPY:
1987 default:
1988 x_function = GXcopy;
1989 break;
1990 }
1991
1992 XSetFunction((Display*) m_display, (GC) m_gc, x_function);
1993 if (m_window && m_window->GetBackingPixmap())
1994 XSetFunction((Display*) m_display, (GC) m_gcBacking, x_function);
1995
1996 if ((m_logicalFunction == wxXOR) != (function == wxXOR))
1997 /* MATTHEW: [9] Need to redo pen simply */
1998 m_autoSetting |= 0x2;
1999
2000 m_logicalFunction = function;
2001
2002 }
2003
2004 void wxWindowDCImpl::SetTextForeground( const wxColour &col )
2005 {
2006 wxCHECK_RET( IsOk(), "invalid dc" );
2007
2008 m_textForegroundColour = col;
2009 }
2010
2011 void wxWindowDCImpl::SetTextBackground( const wxColour &col )
2012 {
2013 wxCHECK_RET( IsOk(), "invalid dc" );
2014
2015 m_textBackgroundColour = col;
2016 }
2017
2018 void wxWindowDCImpl::SetBackgroundMode( int mode )
2019 {
2020 m_backgroundMode = mode;
2021 }
2022
2023 void wxWindowDCImpl::SetPalette( const wxPalette& palette )
2024 {
2025 if (m_window)
2026 {
2027 if (palette.IsOk())
2028 /* Use GetXColormap */
2029 XSetWindowColormap ((Display*) m_display, (Window) m_window->GetXWindow(),
2030 (Colormap) palette.GetXColormap());
2031 else
2032 /* Use wxGetMainColormap */
2033 XSetWindowColormap ((Display*) m_display, (Window) m_window->GetXWindow(),
2034 (Colormap) wxTheApp->GetMainColormap(m_display));
2035 }
2036 }
2037
2038 static void wxCopyRegion( WXRegion src, WXRegion& dst )
2039 {
2040 if( !dst )
2041 dst = XCreateRegion();
2042 XUnionRegion( (Region)src, (Region)src, (Region)dst );
2043 }
2044
2045 // Helper function; userRegion is the region set by calling SetClippingRegion
2046 void wxWindowDCImpl::SetDCClipping( WXRegion userRegion )
2047 {
2048 bool hasUpdateRegion = m_window && m_window->GetUpdateRegion().IsOk();
2049 // this means that we should start the clip region from scratch,
2050 // or from the update region, if any
2051 if( !userRegion )
2052 {
2053 if( m_clipRegion )
2054 XDestroyRegion( (Region)m_clipRegion );
2055 m_clipRegion = (WXRegion)NULL;
2056
2057 if( hasUpdateRegion )
2058 wxCopyRegion( m_window->GetUpdateRegion().GetX11Region(),
2059 m_clipRegion );
2060 }
2061 // intersect the user region, if any, with the
2062 // exisiting clip region
2063 else // if( userRegion )
2064 {
2065 if( !m_clipRegion )
2066 wxCopyRegion( userRegion, m_clipRegion );
2067 else
2068 XIntersectRegion( (Region)m_clipRegion,
2069 (Region)userRegion, (Region)m_clipRegion );
2070 }
2071
2072 if( m_clipRegion )
2073 XSetRegion( (Display*)m_display, (GC)m_gc, (Region)m_clipRegion );
2074 else
2075 XSetClipMask( (Display*)m_display, (GC)m_gc, None );
2076 }
2077
2078 void wxWindowDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y,
2079 wxCoord width, wxCoord height )
2080 {
2081 wxMotifDCImpl::DoSetClippingRegion( x, y, width, height );
2082
2083 wxRegion temp(XLOG2DEV(x), YLOG2DEV(y),
2084 XLOG2DEVREL(width), YLOG2DEVREL(height));
2085
2086 SetDCClipping(temp.GetX11Region());
2087
2088 // Needs to work differently for Pixmap: without this,
2089 // there's a nasty (Display*) m_display bug. 8/12/94
2090 if (m_window && m_window->GetBackingPixmap())
2091 {
2092 XRectangle rects[1];
2093 rects[0].x = (short)XLOG2DEV_2(x);
2094 rects[0].y = (short)YLOG2DEV_2(y);
2095 rects[0].width = (unsigned short)XLOG2DEVREL(width);
2096 rects[0].height = (unsigned short)YLOG2DEVREL(height);
2097 XSetClipRectangles((Display*) m_display, (GC) m_gcBacking,
2098 0, 0, rects, 1, Unsorted);
2099 }
2100 }
2101
2102 void wxWindowDCImpl::DoSetClippingRegionAsRegion( const wxRegion& region )
2103 {
2104 SetDCClipping(region.GetX11Region());
2105
2106 // Needs to work differently for Pixmap: without this,
2107 // there's a nasty (Display*) m_display bug. 8/12/94
2108 if (m_window && m_window->GetBackingPixmap())
2109 {
2110 wxRect box = region.GetBox();
2111
2112 XRectangle rects[1];
2113 rects[0].x = (short)XLOG2DEV_2(box.x);
2114 rects[0].y = (short)YLOG2DEV_2(box.y);
2115 rects[0].width = (unsigned short)XLOG2DEVREL(box.width);
2116 rects[0].height = (unsigned short)YLOG2DEVREL(box.height);
2117 XSetClipRectangles((Display*) m_display, (GC) m_gcBacking,
2118 0, 0, rects, 1, Unsorted);
2119 }
2120 }
2121
2122
2123 void wxWindowDCImpl::DestroyClippingRegion()
2124 {
2125 wxMotifDCImpl::DestroyClippingRegion();
2126
2127 SetDCClipping(NULL);
2128
2129 if (m_window && m_window->GetBackingPixmap())
2130 XSetClipMask ((Display*) m_display, (GC) m_gcBacking, None);
2131 }
2132
2133 // Resolution in pixels per logical inch
2134 wxSize wxWindowDCImpl::GetPPI() const
2135 {
2136 // TODO
2137 return wxSize(100, 100);
2138 }
2139
2140 int wxWindowDCImpl::GetDepth() const
2141 {
2142 // TODO
2143 return 24;
2144 }
2145
2146
2147
2148
2149 // ----------------------------------------------------------------------------
2150 // wxPaintDCImpl
2151 // ----------------------------------------------------------------------------
2152
2153 wxPaintDCImpl::wxPaintDCImpl(wxDC *owner, wxWindow* win)
2154 : wxWindowDCImpl(owner, win)
2155 {
2156 // Set the clipping region.to the update region
2157 SetDCClipping((WXRegion)NULL);
2158 }
2159
2160 wxPaintDCImpl::~wxPaintDCImpl()
2161 {
2162 if (m_window)
2163 m_window->ClearUpdateRegion();
2164 SetDCClipping((WXRegion)NULL);
2165 }
2166
2167 // ----------------------------------------------------------------------------
2168 // private functions
2169 // ----------------------------------------------------------------------------
2170
2171 /*
2172 Used when copying between drawables on different (Display*) m_displays. Not
2173 very fast, but better than giving up.
2174 */
2175
2176 static void XCopyRemote(Display *src_display, Display *dest_display,
2177 Drawable src, Drawable dest,
2178 GC destgc,
2179 int srcx, int srcy,
2180 unsigned int w, unsigned int h,
2181 int destx, int desty,
2182 bool more, XImage **cache)
2183 {
2184 XImage *image, *destimage;
2185 Colormap destcm, srccm;
2186 static const int CACHE_SIZE = 256;
2187
2188 unsigned int i, j;
2189 Pixel cachesrc[CACHE_SIZE], cachedest[CACHE_SIZE];
2190 int k, cache_pos, all_cache;
2191
2192 if (!cache || !*cache)
2193 image = XGetImage(src_display, src, srcx, srcy, w, h, AllPlanes, ZPixmap);
2194 else
2195 image = *cache;
2196
2197 destimage = XGetImage(dest_display, dest, destx, desty, w, h, AllPlanes, ZPixmap);
2198
2199 srccm = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) src_display);
2200 destcm = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dest_display);
2201
2202 cache_pos = 0;
2203 all_cache = False;
2204
2205 for (i = 0; i < w; i++)
2206 for (j = 0; j < h; j++) {
2207 Pixel pixel;
2208 XColor xcol;
2209
2210 pixel = XGetPixel(image, i, j);
2211 for (k = cache_pos; k--; )
2212 if (cachesrc[k] == pixel) {
2213 pixel = cachedest[k];
2214 goto install;
2215 }
2216 if (all_cache)
2217 for (k = CACHE_SIZE; k-- > cache_pos; )
2218 if (cachesrc[k] == pixel) {
2219 pixel = cachedest[k];
2220 goto install;
2221 }
2222
2223 cachesrc[cache_pos] = xcol.pixel = pixel;
2224 XQueryColor(src_display, srccm, &xcol);
2225 if (!XAllocColor(dest_display, destcm, &xcol))
2226 xcol.pixel = 0;
2227 cachedest[cache_pos] = pixel = xcol.pixel;
2228
2229 if (++cache_pos >= CACHE_SIZE) {
2230 cache_pos = 0;
2231 all_cache = true;
2232 }
2233
2234 install:
2235 XPutPixel(destimage, i, j, pixel);
2236 }
2237
2238 XPutImage(dest_display, dest, destgc, destimage, 0, 0, destx, desty, w, h);
2239 XDestroyImage(destimage);
2240
2241 if (more && cache)
2242 *cache = image;
2243 else
2244 XDestroyImage(image);
2245 }
2246