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