Added wxUSE_DC_CACHEING and associated code to wxMSW
[wxWidgets.git] / src / mgl / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose: wxDC class
4 // Author: Vaclav Slavik
5 // Created: 2001/03/09
6 // RCS-ID: $Id$
7 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ===========================================================================
12 // declarations
13 // ===========================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma implementation "dc.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/dc.h"
32 #include "wx/dcmemory.h"
33 #endif
34
35 #include "wx/fontutil.h"
36 #include "wx/fontmap.h"
37 #include "wx/mgl/private.h"
38 #include "wx/log.h"
39
40 #include <string.h>
41 #include <math.h>
42 #include <mgraph.hpp>
43
44
45
46 //-----------------------------------------------------------------------------
47 // constants
48 //-----------------------------------------------------------------------------
49
50 const double mm2inches = 0.0393700787402;
51 const double inches2mm = 25.4;
52 const double mm2twips = 56.6929133859;
53 const double twips2mm = 0.0176388888889;
54 const double mm2pt = 2.83464566929;
55 const double pt2mm = 0.352777777778;
56 const double RAD2DEG = 180.0 / M_PI;
57
58
59 //-----------------------------------------------------------------------------
60 // pens data:
61 //-----------------------------------------------------------------------------
62
63 const ushort STIPPLE_wxDOT = 0x5555/* - - - - - - - -*/;
64 const ushort STIPPLE_wxLONG_DASH = 0xF0F0/* ---- ----*/;
65 const ushort STIPPLE_wxSHORT_DASH = 0xCCCC/*-- -- -- -- */;
66 const ushort STIPPLE_wxDOT_DASH = 0x3939/* --- - --- -*/;
67 const ushort STIPPLE_wxSOLID = 0xFFFF/*----------------*/;
68
69 #define PATTERN_ROW(b7,b6,b5,b4,b3,b2,b1,b0) \
70 ((b7 << 7) | (b6 << 6) | (b5 << 5) | (b4 << 4) | \
71 (b3 << 3) | (b2 << 2) | (b1 << 1) | b0)
72
73 static pattern_t PATTERN_wxFDIAGONAL_HATCH = {{
74 PATTERN_ROW(1,0,0,0,0,0,0,0),
75 PATTERN_ROW(0,1,0,0,0,0,0,0),
76 PATTERN_ROW(0,0,1,0,0,0,0,0),
77 PATTERN_ROW(0,0,0,1,0,0,0,0),
78 PATTERN_ROW(0,0,0,0,1,0,0,0),
79 PATTERN_ROW(0,0,0,0,0,1,0,0),
80 PATTERN_ROW(0,0,0,0,0,0,1,0),
81 PATTERN_ROW(0,0,0,0,0,0,0,1),
82 }};
83
84 static pattern_t PATTERN_wxCROSSDIAG_HATCH = {{
85 PATTERN_ROW(1,0,0,0,0,0,0,1),
86 PATTERN_ROW(0,1,0,0,0,0,1,0),
87 PATTERN_ROW(0,0,1,0,0,1,0,0),
88 PATTERN_ROW(0,0,0,1,1,0,0,0),
89 PATTERN_ROW(0,0,0,1,1,0,0,0),
90 PATTERN_ROW(0,0,1,0,0,1,0,0),
91 PATTERN_ROW(0,1,0,0,0,0,1,0),
92 PATTERN_ROW(1,0,0,0,0,0,0,1),
93 }};
94
95 static pattern_t PATTERN_wxBDIAGONAL_HATCH = {{
96 PATTERN_ROW(0,0,0,0,0,0,0,1),
97 PATTERN_ROW(0,0,0,0,0,0,1,0),
98 PATTERN_ROW(0,0,0,0,0,1,0,0),
99 PATTERN_ROW(0,0,0,0,1,0,0,0),
100 PATTERN_ROW(0,0,0,1,0,0,0,0),
101 PATTERN_ROW(0,0,1,0,0,0,0,0),
102 PATTERN_ROW(0,1,0,0,0,0,0,0),
103 PATTERN_ROW(1,0,0,0,0,0,0,0),
104 }};
105
106 static pattern_t PATTERN_wxCROSS_HATCH = {{
107 PATTERN_ROW(0,0,0,1,0,0,0,0),
108 PATTERN_ROW(0,0,0,1,0,0,0,0),
109 PATTERN_ROW(0,0,0,1,0,0,0,0),
110 PATTERN_ROW(1,1,1,1,1,1,1,1),
111 PATTERN_ROW(0,0,0,1,0,0,0,0),
112 PATTERN_ROW(0,0,0,1,0,0,0,0),
113 PATTERN_ROW(0,0,0,1,0,0,0,0),
114 PATTERN_ROW(0,0,0,1,0,0,0,0),
115 }};
116
117 static pattern_t PATTERN_wxHORIZONTAL_HATCH = {{
118 PATTERN_ROW(0,0,0,0,0,0,0,0),
119 PATTERN_ROW(0,0,0,0,0,0,0,0),
120 PATTERN_ROW(0,0,0,0,0,0,0,0),
121 PATTERN_ROW(1,1,1,1,1,1,1,1),
122 PATTERN_ROW(0,0,0,0,0,0,0,0),
123 PATTERN_ROW(0,0,0,0,0,0,0,0),
124 PATTERN_ROW(0,0,0,0,0,0,0,0),
125 PATTERN_ROW(0,0,0,0,0,0,0,0),
126 }};
127
128 static pattern_t PATTERN_wxVERTICAL_HATCH = {{
129 PATTERN_ROW(0,0,0,1,0,0,0,0),
130 PATTERN_ROW(0,0,0,1,0,0,0,0),
131 PATTERN_ROW(0,0,0,1,0,0,0,0),
132 PATTERN_ROW(0,0,0,1,0,0,0,0),
133 PATTERN_ROW(0,0,0,1,0,0,0,0),
134 PATTERN_ROW(0,0,0,1,0,0,0,0),
135 PATTERN_ROW(0,0,0,1,0,0,0,0),
136 PATTERN_ROW(0,0,0,1,0,0,0,0),
137 }};
138
139 #undef PATTERN_ROW
140
141 //-----------------------------------------------------------------------------
142 // wxDC
143 //-----------------------------------------------------------------------------
144
145 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxDCBase)
146
147 // Default constructor
148 wxDC::wxDC()
149 {
150 m_isMemDC = FALSE;
151 m_MGLDC = NULL;
152 m_OwnsMGLDC = FALSE;
153 m_ok = FALSE; // must call SetMGLDevCtx() before using it
154
155 #if 0
156 m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
157 (double)wxGetDisplaySizeMM().GetWidth();
158 m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
159 (double)wxGetDisplaySizeMM().GetHeight();
160 #endif
161 // FIXME_MGL -- not in wxUniversal branch (and not implementend anyway,
162 // hardcode it for 75dpi for now)
163 m_mm_to_pix_x = ((double)wxGetDisplaySize().GetWidth() / 75) * inches2mm;
164 m_mm_to_pix_y = ((double)wxGetDisplaySize().GetHeight() / 75) * inches2mm;
165
166 m_pen = *wxBLACK_PEN;
167 m_font = *wxNORMAL_FONT;
168 m_brush = *wxWHITE_BRUSH;
169 m_penOfsX = m_penOfsY = 0;
170
171 m_penSelected = m_brushSelected = FALSE;
172 m_downloadedPatterns[0] = m_downloadedPatterns[1] = FALSE;
173
174 m_mglFont = NULL;
175 }
176
177
178 wxDC::~wxDC()
179 {
180 if (m_OwnsMGLDC)
181 delete m_MGLDC;
182 }
183
184 void wxDC::SetMGLDC(MGLDevCtx *mgldc, bool OwnsMGLDC)
185 {
186 if ( m_OwnsMGLDC && m_MGLDC )
187 delete m_MGLDC;
188 m_MGLDC = mgldc;
189 m_OwnsMGLDC = OwnsMGLDC;
190 m_ok = TRUE;
191 InitializeMGLDC();
192 }
193
194 void wxDC::InitializeMGLDC()
195 {
196 if ( GetDepth() > 8 )
197 {
198 wxCurrentDCSwitcher switcher(m_MGLDC); // will go away with MGL6
199 m_MGLDC->setFontBlendMode(MGL_AA_RGBBLEND);
200 }
201 }
202
203
204 // ---------------------------------------------------------------------------
205 // clipping
206 // ---------------------------------------------------------------------------
207
208
209 #define DO_SET_CLIPPING_BOX(rg) \
210 { \
211 wxRect rect = rg.GetBox(); \
212 m_clipX1 = (wxCoord) XDEV2LOG(rect.GetLeft()); \
213 m_clipY1 = (wxCoord) YDEV2LOG(rect.GetTop()); \
214 m_clipX2 = (wxCoord) XDEV2LOG(rect.GetRight()); \
215 m_clipY2 = (wxCoord) YDEV2LOG(rect.GetBottom()); \
216 }
217
218 void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
219 {
220 wxCHECK_RET( Ok(), wxT("invalid dc") );
221
222 wxRect rect(XLOG2DEV(cx), YLOG2DEV(cy), XLOG2DEVREL(cw), YLOG2DEVREL(ch));
223
224 if ( !m_currentClippingRegion.IsNull() )
225 m_currentClippingRegion.Intersect(rect);
226 else
227 m_currentClippingRegion.Union(rect);
228
229 m_MGLDC->setClipRegion(m_currentClippingRegion.GetMGLRegion());
230
231 m_clipping = TRUE;
232 DO_SET_CLIPPING_BOX(m_currentClippingRegion)
233 }
234
235 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
236 {
237 wxCHECK_RET( Ok(), wxT("invalid dc") );
238
239 if ( region.Empty() )
240 {
241 DestroyClippingRegion();
242 return;
243 }
244
245 wxRegion rg(region);
246
247 // check if the DC is scaled or moved, and if yes, then
248 // convert rg to device coordinates:
249 if ( m_deviceOriginX != 0 || m_deviceOriginY != 0 ||
250 XLOG2DEVREL(500) != 500 || YLOG2DEVREL(500) != 500 )
251 {
252 region_t *mrg = rg.GetMGLRegion().rgnPointer();
253 span_t *s;
254 segment_t *p;
255 for (s = mrg->spans; s; s = s->next)
256 {
257 s->y = YLOG2DEV(s->y);
258 for (p = s->seg; p; p = p->next)
259 p->x = XLOG2DEV(p->x);
260 }
261 }
262
263 if ( !m_currentClippingRegion.IsNull() )
264 m_currentClippingRegion.Intersect(rg);
265 else
266 m_currentClippingRegion.Union(rg);
267
268 m_MGLDC->setClipRegion(m_currentClippingRegion.GetMGLRegion());
269
270 m_clipping = TRUE;
271 DO_SET_CLIPPING_BOX(m_currentClippingRegion)
272 }
273
274 void wxDC::DestroyClippingRegion()
275 {
276 wxCHECK_RET( Ok(), wxT("invalid dc") );
277
278 m_MGLDC->setClipRect(MGLRect(0, 0, m_MGLDC->sizex(), m_MGLDC->sizey()));
279 m_clipping = FALSE;
280 m_currentClippingRegion.Clear();
281 }
282
283 // ---------------------------------------------------------------------------
284 // query capabilities
285 // ---------------------------------------------------------------------------
286
287 bool wxDC::CanDrawBitmap() const
288 {
289 return TRUE;
290 }
291
292 bool wxDC::CanGetTextExtent() const
293 {
294 return TRUE;
295 }
296
297 int wxDC::GetDepth() const
298 {
299 return m_MGLDC->getBitsPerPixel();
300 }
301
302 // ---------------------------------------------------------------------------
303 // drawing
304 // ---------------------------------------------------------------------------
305
306 void wxDC::Clear()
307 {
308 wxCHECK_RET( Ok(), wxT("invalid dc") );
309
310 m_MGLDC->makeCurrent(); // will go away with MGL6.0
311 m_MGLDC->clearDevice();
312 if ( m_backgroundBrush.GetStyle() != wxSOLID &&
313 m_backgroundBrush.GetStyle() != wxTRANSPARENT )
314 {
315 int w, h;
316 wxBrush oldb = m_brush;
317 SetBrush(m_backgroundBrush);
318 SelectBrush();
319 GetSize(&w, &h);
320 m_MGLDC->fillRect(0, 0, w-1, h-1);
321 SetBrush(oldb);
322 }
323 }
324
325 void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
326 {
327 wxFAIL_MSG( wxT("wxDC::DoFloodFill not implemented") );
328 }
329
330 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
331 {
332 wxCHECK_MSG( col, FALSE, _T("NULL colour parameter in wxDC::GetPixel"));
333
334 uchar r, g, b;
335 m_MGLDC->unpackColorFast(m_MGLDC->getPixel(XLOG2DEV(x), XLOG2DEV(y)),
336 r, g, b);
337 col->Set(r, g, b);
338 return TRUE;
339 }
340
341 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
342 {
343 wxCHECK_RET( Ok(), wxT("invalid dc") );
344
345 if ( m_pen.GetStyle() != wxTRANSPARENT )
346 {
347 int w = 0;
348 int h = 0;
349 GetSize(&w, &h);
350 m_MGLDC->makeCurrent(); // will go away with MGL6.0
351 if ( !m_penSelected )
352 SelectPen();
353 wxCoord xx = XLOG2DEV(x);
354 wxCoord yy = YLOG2DEV(y);
355 m_MGLDC->line(m_penOfsX, yy + m_penOfsY, w-1 + m_penOfsX, yy + m_penOfsY);
356 m_MGLDC->line(xx + m_penOfsX, m_penOfsY, x + m_penOfsX, h-1 + m_penOfsY);
357 CalcBoundingBox(0, 0);
358 CalcBoundingBox(w, h);
359 }
360 }
361
362 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
363 {
364 wxCHECK_RET( Ok(), wxT("invalid dc") );
365
366 if ( m_pen.GetStyle() != wxTRANSPARENT )
367 {
368 m_MGLDC->makeCurrent(); // will go away with MGL6.0
369 if ( !m_penSelected )
370 SelectPen();
371 m_MGLDC->line(XLOG2DEV(x1) + m_penOfsX, XLOG2DEV(y1) + m_penOfsY,
372 XLOG2DEV(x2) + m_penOfsX, XLOG2DEV(y2) + m_penOfsY);
373 CalcBoundingBox(x1, y1);
374 CalcBoundingBox(x2, y2);
375 }
376 }
377
378 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
379 // and ending at (x2, y2)
380 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1,
381 wxCoord x2, wxCoord y2,
382 wxCoord xc, wxCoord yc)
383 {
384 wxCHECK_RET( Ok(), wxT("invalid dc") );
385
386 wxCoord xx1 = XLOG2DEV(x1);
387 wxCoord yy1 = YLOG2DEV(y1);
388 wxCoord xx2 = XLOG2DEV(x2);
389 wxCoord yy2 = YLOG2DEV(y2);
390 wxCoord xxc = XLOG2DEV(xc);
391 wxCoord yyc = YLOG2DEV(yc);
392 double dx = xx1 - xxc;
393 double dy = yy1 - yyc;
394 double radius = sqrt((double)(dx*dx+dy*dy));
395 wxCoord r = (wxCoord)radius;
396 double radius1, radius2;
397
398
399 if (xx1 == xx2 && yy1 == yy2)
400 {
401 radius1 = 0.0;
402 radius2 = 360.0;
403 }
404 else if (radius == 0.0)
405 {
406 radius1 = radius2 = 0.0;
407 }
408 else
409 {
410 radius1 = (xx1 - xxc == 0) ?
411 (yy1 - yyc < 0) ? 90.0 : -90.0 :
412 -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
413 radius2 = (xx2 - xxc == 0) ?
414 (yy2 - yyc < 0) ? 90.0 : -90.0 :
415 -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
416 }
417 wxCoord alpha1 = wxCoord(radius1);
418 wxCoord alpha2 = alpha1 + wxCoord(radius2 - radius1);
419 while (alpha2 <= 0) alpha2 += 360;
420 while (alpha1 > 360) alpha1 -= 360;
421
422 m_MGLDC->makeCurrent(); // will go away with MGL6.0
423 if ( m_brush.GetStyle() != wxTRANSPARENT )
424 {
425 if ( !m_brushSelected )
426 SelectBrush();
427 m_MGLDC->fillEllipseArc(xxc, yyc, r, r, alpha1, alpha2);
428 }
429
430 if ( m_pen.GetStyle() != wxTRANSPARENT )
431 {
432 if ( !m_penSelected )
433 SelectPen();
434 m_MGLDC->ellipseArc(xxc + m_penOfsX, yyc + m_penOfsY, r, r, alpha1, alpha2);
435 }
436
437 CalcBoundingBox(xc - r, yc - r);
438 CalcBoundingBox(xc + r, yc + r);
439 }
440
441 void wxDC::DoDrawPoint(wxCoord x, wxCoord y)
442 {
443 wxCHECK_RET( Ok(), wxT("invalid dc") );
444
445 if ( m_pen.GetStyle() != wxTRANSPARENT )
446 {
447 m_MGLDC->makeCurrent(); // will go away with MGL6.0
448 if ( !m_penSelected )
449 SelectPen();
450 m_MGLDC->pixel(XLOG2DEV(x), YLOG2DEV(y));
451 CalcBoundingBox(x, y);
452 }
453 }
454
455 void wxDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset,int fillStyle)
456 {
457 wxCHECK_RET( Ok(), wxT("invalid dc") );
458
459 wxCoord xxoffset = XLOG2DEVREL(xoffset),
460 yyoffset = YLOG2DEVREL(yoffset);
461 MGLPoint *cpoints = new MGLPoint[n+1];
462 for (int i = 0; i < n; i++)
463 {
464 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
465 cpoints[i].x = (int)(XLOG2DEV(points[i].x));
466 cpoints[i].y = (int)(YLOG2DEV(points[i].y));
467 }
468 cpoints[n] = cpoints[0];
469
470 m_MGLDC->makeCurrent(); // will go away with MGL6.0
471 if ( m_brush.GetStyle() != wxTRANSPARENT )
472 {
473 if ( !m_brushSelected )
474 SelectBrush();
475 m_MGLDC->fillPolygon(n, cpoints, xxoffset, yyoffset);
476 }
477
478 if ( m_pen.GetStyle() != wxTRANSPARENT )
479 {
480 if ( !m_penSelected )
481 SelectPen();
482 if (m_penOfsX != 0 || m_penOfsY != 0)
483 {
484 for (int i = 0; i <= n; i++)
485 {
486 cpoints[i].x += m_penOfsX;
487 cpoints[i].y += m_penOfsY;
488 }
489 }
490 m_MGLDC->polyLine(n+1, cpoints);
491 }
492
493 delete[] cpoints;
494 }
495
496 void wxDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
497 {
498 wxCHECK_RET( Ok(), wxT("invalid dc") );
499
500 if ( m_pen.GetStyle() != wxTRANSPARENT )
501 {
502 MGLPoint *cpoints = new MGLPoint[n];
503 m_MGLDC->makeCurrent(); // will go away with MGL6.0
504 if ( !m_penSelected )
505 SelectPen();
506 for (int i = 0; i < n; i++)
507 {
508 CalcBoundingBox(points[i].x + xoffset, points[i].y + yoffset);
509 cpoints[i].x = (int)(XLOG2DEV(points[i].x + xoffset) /*+ m_penOfsX*/);
510 cpoints[i].y = (int)(YLOG2DEV(points[i].y + yoffset) /*+ m_penOfsY*/);
511 }
512 m_MGLDC->polyLine(n, cpoints);
513 delete[] cpoints;
514 }
515 }
516
517 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
518 {
519 wxCHECK_RET( Ok(), wxT("invalid dc") );
520
521 wxCoord xx = XLOG2DEV(x);
522 wxCoord yy = YLOG2DEV(y);
523 wxCoord ww = m_signX * XLOG2DEVREL(width);
524 wxCoord hh = m_signY * YLOG2DEVREL(height);
525
526 if ( ww == 0 || hh == 0 ) return;
527
528 if ( ww < 0 )
529 {
530 ww = -ww;
531 xx = xx - ww;
532 }
533 if ( hh < 0 )
534 {
535 hh = -hh;
536 yy = yy - hh;
537 }
538
539 m_MGLDC->makeCurrent(); // will go away with MGL6.0
540 if ( m_brush.GetStyle() != wxTRANSPARENT )
541 {
542 if ( !m_brushSelected )
543 SelectBrush();
544 m_MGLDC->fillRect(xx, yy, xx + ww, yy + hh);
545 }
546
547 if ( m_pen.GetStyle() != wxTRANSPARENT )
548 {
549 if ( !m_penSelected )
550 SelectPen();
551 m_MGLDC->rect(xx + m_penOfsX, yy + m_penOfsY,
552 xx + ww + m_penOfsX, yy + hh + m_penOfsY);
553 }
554
555 CalcBoundingBox(x, y);
556 CalcBoundingBox(x + width, y + height);
557 }
558
559 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
560 {
561 wxCHECK_RET( Ok(), wxT("invalid dc") );
562
563 if ( radius < 0.0 )
564 radius = -radius * ((width < height) ? width : height);
565
566 wxCoord xx = XLOG2DEV(x);
567 wxCoord yy = YLOG2DEV(y);
568 wxCoord ww = m_signX * XLOG2DEVREL(width);
569 wxCoord hh = m_signY * YLOG2DEVREL(height);
570 wxCoord rr = XLOG2DEVREL((wxCoord)radius);
571
572 // CMB: handle -ve width and/or height
573 if ( ww < 0 )
574 {
575 ww = -ww;
576 xx = xx - ww;
577 }
578 if ( hh < 0 )
579 {
580 hh = -hh;
581 yy = yy - hh;
582 }
583
584 // CMB: if radius is zero use DrawRectangle() instead to avoid
585 // X drawing errors with small radii
586 if ( rr == 0 )
587 {
588 DrawRectangle(x, y, width, height);
589 return;
590 }
591
592 // CMB: draw nothing if transformed w or h is 0
593 if ( ww == 0 || hh == 0 ) return;
594
595 // CMB: ensure dd is not larger than rectangle otherwise we
596 // get an hour glass shape
597 wxCoord dd = 2 * rr;
598 if ( dd > ww ) dd = ww;
599 if ( dd > hh ) dd = hh;
600 rr = dd / 2;
601
602 m_MGLDC->makeCurrent(); // will go away with MGL6.0
603 if ( m_brush.GetStyle() != wxTRANSPARENT )
604 {
605 if (!m_brushSelected)
606 SelectBrush();
607 m_MGLDC->fillRect(xx+rr, yy, xx+ww-rr, yy+hh);
608 m_MGLDC->fillRect(xx, yy+rr, xx+ww, yy+hh-rr);
609 m_MGLDC->fillEllipseArc(xx+rr, yy+rr, rr, rr, 90, 180);
610 m_MGLDC->fillEllipseArc(xx+ww-rr, yy+rr, rr, rr, 0, 90);
611 m_MGLDC->fillEllipseArc(xx+rr, yy+hh-rr, rr, rr, 180, 270);
612 m_MGLDC->fillEllipseArc(xx+ww-rr, yy+hh-rr, rr, rr, 270, 0);
613 }
614
615 if ( m_pen.GetStyle() != wxTRANSPARENT )
616 {
617 if ( !m_penSelected )
618 SelectPen();
619 xx += m_penOfsX;
620 yy += m_penOfsY;
621 m_MGLDC->line(xx+rr+1, yy, xx+ww-rr, yy);
622 m_MGLDC->ellipseArc(xx+ww-rr, yy+rr, rr, rr, 0, 90);
623 m_MGLDC->line(xx+ww, yy+rr+1, xx+ww, yy+hh-rr);
624 m_MGLDC->ellipseArc(xx+ww-rr, yy+hh-rr, rr, rr, 270, 0);
625 m_MGLDC->line(xx+ww-rr, yy+hh, xx+rr+1, yy+hh);
626 m_MGLDC->ellipseArc(xx+rr, yy+hh-rr, rr, rr, 180, 270);
627 m_MGLDC->line(xx, yy+hh-rr, xx, yy+rr+1);
628 m_MGLDC->ellipseArc(xx+rr, yy+rr, rr, rr, 90, 180);
629 }
630
631 CalcBoundingBox(x, y);
632 CalcBoundingBox(x + width, y + height);
633 }
634
635 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
636 {
637 wxCHECK_RET( Ok(), wxT("invalid dc") );
638
639 wxCoord x2 = (x+width);
640 wxCoord y2 = (y+height);
641
642 m_MGLDC->makeCurrent(); // will go away with MGL6.0
643 if ( m_brush.GetStyle() != wxTRANSPARENT )
644 {
645 if ( !m_brushSelected )
646 SelectBrush();
647 MGLRect rect(XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2));
648 m_MGLDC->fillEllipse(rect);
649 }
650
651 if ( m_pen.GetStyle() != wxTRANSPARENT )
652 {
653 if ( !m_penSelected )
654 SelectPen();
655 MGLRect rect(XLOG2DEV(x) + m_penOfsX, YLOG2DEV(y) + m_penOfsY,
656 XLOG2DEV(x2) + m_penOfsX, YLOG2DEV(y2) + m_penOfsY);
657 m_MGLDC->ellipse(rect);
658 }
659
660 CalcBoundingBox(x, y);
661 CalcBoundingBox(x2, y2);
662 }
663
664 void wxDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
665 {
666 wxCHECK_RET( Ok(), wxT("invalid dc") );
667
668 wxCoord x2 = (x+w);
669 wxCoord y2 = (y+h);
670
671 m_MGLDC->makeCurrent(); // will go away with MGL6.0
672 if ( m_brush.GetStyle() != wxTRANSPARENT )
673 {
674 if (!m_brushSelected) SelectBrush();
675 MGLRect rect(XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2));
676 m_MGLDC->fillEllipseArc(rect, (int)sa, (int)ea);
677 }
678
679 if ( m_pen.GetStyle() != wxTRANSPARENT )
680 {
681 if ( !m_penSelected )
682 SelectPen();
683 MGLRect rect(XLOG2DEV(x) + m_penOfsX, YLOG2DEV(y) + m_penOfsY,
684 XLOG2DEV(x2) + m_penOfsX, YLOG2DEV(y2) + m_penOfsY);
685 m_MGLDC->ellipseArc(rect, (int)sa, (int)ea);
686 }
687
688 CalcBoundingBox(x, y);
689 CalcBoundingBox(x2, y2);
690 }
691
692 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
693 {
694 if ( m_pen.GetStyle() == wxTRANSPARENT ) return;
695 m_MGLDC->makeCurrent(); // will go away with MGL6.0
696 DrawAnyText(text, x, y);
697
698 // update the bounding box
699 wxCoord w, h;
700 CalcBoundingBox(x, y);
701 GetTextExtent(text, &w, &h);
702 CalcBoundingBox(x + w, y + h);
703 }
704
705 bool wxDC::SelectMGLFont()
706 {
707 if ( m_mglFont == NULL )
708 {
709 float scale = m_scaleY;
710 bool antialiased = (GetDepth() > 8);
711
712 m_mglFont = m_font.GetMGLfont_t(scale, antialiased);
713 wxCHECK_MSG( m_mglFont, FALSE, wxT("invalid font") );
714
715 m_MGLDC->useFont(m_mglFont);
716 wxLogTrace("mgl_font", "useFont(%p)", m_mglFont);
717
718 #if !wxUSE_UNICODE
719 wxNativeEncodingInfo nativeEnc;
720 wxFontEncoding encoding = m_font.GetEncoding();
721 if ( !wxGetNativeFontEncoding(encoding, &nativeEnc) ||
722 !wxTestFontEncoding(nativeEnc) )
723 {
724 #if wxUSE_FONTMAP
725 if ( !wxTheFontMapper->GetAltForEncoding(encoding, &nativeEnc) )
726 #endif
727 {
728 nativeEnc.mglEncoding = MGL_ENCODING_ASCII;
729 }
730 }
731 m_MGLDC->setTextEncoding(nativeEnc.mglEncoding);
732 #endif
733 }
734 return TRUE;
735 }
736
737 void wxDC::DrawAnyText(const wxString& text, wxCoord x, wxCoord y)
738 {
739 wxCHECK_RET( Ok(), wxT("invalid dc") );
740
741 SelectMGLFont();
742
743 m_MGLDC->setColor(m_MGLDC->packColorFast(m_textForegroundColour.Red(),
744 m_textForegroundColour.Green(), m_textForegroundColour.Blue()));
745 m_MGLDC->setBackColor(m_MGLDC->packColorFast(m_textBackgroundColour.Red(),
746 m_textBackgroundColour.Green(), m_textBackgroundColour.Blue()));
747
748 // Render the text:
749 wxCoord xx = XLOG2DEV(x);
750 wxCoord yy = YLOG2DEV(y);
751
752 m_MGLDC->setLineStyle(MGL_LINE_STIPPLE);
753 m_MGLDC->setLineStipple(0xFFFF);
754 m_MGLDC->setPenSize(1, 1);
755 m_MGLDC->setPenStyle(MGL_BITMAP_SOLID);
756
757 #if wxUSE_UNICODE
758 const wchar_t *c_text = text.c_str();
759 #else
760 const char *c_text = text.c_str();
761 #endif
762 m_MGLDC->drawStr(xx, yy, c_text);
763
764 // Render underline:
765 if ( m_font.GetUnderlined() )
766 {
767 int x1 = xx, y1 = yy;
768 int x2, y2;
769 int w = m_MGLDC->textWidth(c_text);
770 m_MGLDC->underScoreLocation(x1, y1, c_text);
771 switch (m_MGLDC->getTextDirection())
772 {
773 case MGL_RIGHT_DIR: x2 = x1 + w, y2 = y1; break;
774 case MGL_LEFT_DIR: x2 = x1 - w, y2 = y1; break;
775 case MGL_UP_DIR: x2 = x1, y2 = y1 - w; break;
776 case MGL_DOWN_DIR: x2 = x1, y2 = y1 + w; break;
777 }
778 m_MGLDC->line(x1, y1, x2, y2);
779 }
780
781 m_penSelected = m_brushSelected = FALSE;
782 }
783
784 void wxDC::DoDrawRotatedText(const wxString& text,
785 wxCoord x, wxCoord y,
786 double angle)
787 {
788 if ( m_pen.GetStyle() == wxTRANSPARENT ) return;
789 m_MGLDC->makeCurrent(); // will go away with MGL6.0
790
791 if ( angle == 0 )
792 {
793 DoDrawText(text, x, y);
794 return;
795 }
796 else if ( angle == 90.0 )
797 m_MGLDC->setTextDirection(MGL_UP_DIR);
798 else if ( angle == 180.0 )
799 m_MGLDC->setTextDirection(MGL_LEFT_DIR);
800 else if ( angle == 270.0 )
801 m_MGLDC->setTextDirection(MGL_DOWN_DIR);
802 else
803 {
804 // FIXME_MGL -- implement once MGL supports it
805 wxFAIL_MSG(wxT("wxMGL only supports rotated text with angle 0,90,180 or 270"));
806 return;
807 }
808
809 DrawAnyText(text, x, y);
810
811 // Restore default:
812 m_MGLDC->setTextDirection(MGL_RIGHT_DIR);
813 }
814
815 // ---------------------------------------------------------------------------
816 // set GDI objects
817 // ---------------------------------------------------------------------------
818
819 void wxDC::SelectMGLStipplePen(int style)
820 {
821 ushort stipple;
822
823 switch (style)
824 {
825 case wxDOT: stipple = STIPPLE_wxDOT; break;
826 case wxLONG_DASH: stipple = STIPPLE_wxLONG_DASH; break;
827 case wxSHORT_DASH: stipple = STIPPLE_wxSHORT_DASH; break;
828 case wxDOT_DASH: stipple = STIPPLE_wxDOT_DASH; break;
829 default: stipple = STIPPLE_wxSOLID; break;
830 }
831
832 m_MGLDC->setLineStyle(MGL_LINE_STIPPLE);
833 m_MGLDC->setLineStipple(stipple);
834 m_MGLDC->setPenSize(1, 1);
835 m_MGLDC->setPenStyle(MGL_BITMAP_SOLID);
836 m_penOfsY = m_penOfsX = 0;
837 }
838
839 // Accepted valus of SelectMGLFatPen's 2nd argument
840 enum {
841 wxMGL_SELECT_FROM_PEN,
842 wxMGL_SELECT_FROM_BRUSH
843 };
844
845 void wxDC::SelectMGLFatPen(int style, int flag)
846 {
847 MGL_penStyleType penstyle;
848 const pattern_t *pattern = NULL;
849 pixpattern24_t *pixPattern = NULL;
850 int wx, wy;
851 int slot;
852
853 // Since MGL pens may be created from wxBrush or wxPen and we often
854 // switch between pens and brushes, we take advantage of MGL's ability
855 // to have multiple (pix)pattern_t's loaded. We always download pen
856 // to 0th slot and brush to 1st slot.
857 if ( flag == wxMGL_SELECT_FROM_PEN )
858 slot = 0;
859 else
860 slot = 1;
861
862 // compute pen's width:
863 if ( m_pen.GetWidth() <= 1 )
864 {
865 wx = wy = 1;
866 m_penOfsX = m_penOfsY = 0;
867 }
868 else
869 {
870 wx = (int)(0.5 + fabs((double) XLOG2DEVREL(m_pen.GetWidth())));
871 wy = (int)(0.5 + fabs((double) YLOG2DEVREL(m_pen.GetWidth())));
872 m_penOfsX = -wx/2;
873 m_penOfsY = -wy/2;
874 }
875
876 // find pen's type:
877 penstyle = MGL_BITMAP_TRANSPARENT;
878 switch (style)
879 {
880 case wxBDIAGONAL_HATCH: pattern = &PATTERN_wxBDIAGONAL_HATCH;
881 penstyle = MGL_BITMAP_TRANSPARENT;
882 break;
883 case wxCROSSDIAG_HATCH: pattern = &PATTERN_wxCROSSDIAG_HATCH;
884 penstyle = MGL_BITMAP_TRANSPARENT;
885 break;
886 case wxFDIAGONAL_HATCH: pattern = &PATTERN_wxFDIAGONAL_HATCH;
887 penstyle = MGL_BITMAP_TRANSPARENT;
888 break;
889 case wxCROSS_HATCH: pattern = &PATTERN_wxCROSS_HATCH;
890 penstyle = MGL_BITMAP_TRANSPARENT;
891 break;
892 case wxHORIZONTAL_HATCH: pattern = &PATTERN_wxHORIZONTAL_HATCH;
893 penstyle = MGL_BITMAP_TRANSPARENT;
894 break;
895 case wxVERTICAL_HATCH: pattern = &PATTERN_wxVERTICAL_HATCH;
896 penstyle = MGL_BITMAP_TRANSPARENT;
897 break;
898
899 case wxSTIPPLE:
900 if ( flag == wxMGL_SELECT_FROM_PEN )
901 pixPattern = (pixpattern24_t*) m_pen.GetPixPattern();
902 else
903 pixPattern = (pixpattern24_t*) m_brush.GetPixPattern();
904 penstyle = MGL_PIXMAP;
905 break;
906
907 case wxSTIPPLE_MASK_OPAQUE:
908 pattern = (pattern_t*) m_brush.GetMaskPattern();
909 penstyle = MGL_BITMAP_OPAQUE;
910 break;
911
912 case wxSOLID:
913 default:
914 penstyle = MGL_BITMAP_SOLID; break;
915 }
916
917 // ...and finally, pass the pen to MGL:
918
919 if ( pattern )
920 {
921 if ( !m_downloadedPatterns[slot] )
922 {
923 m_MGLDC->setPenBitmapPattern(slot, pattern);
924 m_downloadedPatterns[slot] = TRUE;
925 }
926 m_MGLDC->usePenBitmapPattern(slot);
927 }
928
929 if ( pixPattern )
930 {
931 if ( !m_downloadedPatterns[slot] )
932 {
933 pixpattern_t pix;
934 int x, y, c;
935
936 switch (GetDepth())
937 {
938 case 8:
939 for (y = 0; y < 8; y++)
940 for (x = 0; x < 8; x++)
941 pix.b8.p[x][y] = m_MGLDC->packColorFast(
942 pixPattern->p[x][y][0],
943 pixPattern->p[x][y][1],
944 pixPattern->p[x][y][2]);
945 break;
946 case 15:
947 case 16:
948 for (y = 0; y < 8; y++)
949 for (x = 0; x < 8; x++)
950 pix.b16.p[x][y] = m_MGLDC->packColorFast(
951 pixPattern->p[x][y][0],
952 pixPattern->p[x][y][1],
953 pixPattern->p[x][y][2]);
954 break;
955 case 24:
956 for (y = 0; y < 8; y++)
957 for (x = 0; x < 8; x++)
958 for (c = 0; c < 3; c++)
959 pix.b24.p[x][y][c] = pixPattern->p[x][y][c];
960 break;
961 case 32:
962 for (y = 0; y < 8; y++)
963 for (x = 0; x < 8; x++)
964 pix.b32.p[x][y] = m_MGLDC->packColorFast(
965 pixPattern->p[x][y][0],
966 pixPattern->p[x][y][1],
967 pixPattern->p[x][y][2]);
968 break;
969 default:
970 wxFAIL_MSG(_T("invalid DC depth"));
971 break;
972 }
973 m_MGLDC->setPenPixmapPattern(slot, &pix);
974 m_downloadedPatterns[slot] = TRUE;
975 }
976 m_MGLDC->usePenPixmapPattern(slot);
977 }
978
979 m_MGLDC->setLineStyle(MGL_LINE_PENSTYLE);
980 m_MGLDC->setPenStyle(penstyle);
981 m_MGLDC->setPenSize(wy, wx);
982 }
983
984 void wxDC::SelectPen()
985 {
986 wxCHECK_RET( Ok(), wxT("invalid dc") );
987
988 wxColour& clr = m_pen.GetColour();
989 m_MGLDC->makeCurrent(); // will go away with MGL6.0
990 m_MGLDC->setColorRGB(clr.Red(), clr.Green(), clr.Blue());
991
992 switch (m_pen.GetStyle())
993 {
994 case wxTRANSPARENT:
995 break;
996
997 case wxDOT:
998 case wxLONG_DASH:
999 case wxSHORT_DASH:
1000 case wxDOT_DASH:
1001 SelectMGLStipplePen(m_pen.GetStyle());
1002 break;
1003
1004 case wxBDIAGONAL_HATCH:
1005 case wxCROSSDIAG_HATCH:
1006 case wxFDIAGONAL_HATCH:
1007 case wxCROSS_HATCH:
1008 case wxHORIZONTAL_HATCH:
1009 case wxVERTICAL_HATCH:
1010 SelectMGLFatPen(m_pen.GetStyle(), wxMGL_SELECT_FROM_PEN);
1011 break;
1012
1013 case wxSTIPPLE:
1014 SelectMGLFatPen(m_pen.GetStyle(), wxMGL_SELECT_FROM_PEN);
1015 break;
1016
1017 case wxSOLID:
1018 case wxUSER_DASH:
1019 default:
1020 if ( m_pen.GetWidth() <= 1 )
1021 SelectMGLStipplePen(wxSOLID);
1022 else
1023 SelectMGLFatPen(wxSOLID, wxMGL_SELECT_FROM_PEN);
1024 break;
1025 }
1026 m_penSelected = TRUE;
1027 m_brushSelected = FALSE;
1028 }
1029
1030 void wxDC::SelectBrush()
1031 {
1032 wxCHECK_RET( Ok(), wxT("invalid dc") );
1033
1034 wxColour fg, bg;
1035 m_MGLDC->makeCurrent(); // will go away with MGL6.0
1036
1037 if ( m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
1038 {
1039 fg = m_textForegroundColour;
1040 bg = m_textBackgroundColour;
1041 }
1042 else
1043 {
1044 fg = m_brush.GetColour();
1045 bg = m_backgroundBrush.GetColour();
1046 }
1047
1048 m_MGLDC->setColorRGB(fg.Red(), fg.Green(), fg.Blue());
1049 m_MGLDC->setBackColor(m_MGLDC->packColorFast(bg.Red(), bg.Green(), bg.Blue()));
1050 m_penSelected = FALSE;
1051 m_brushSelected = TRUE;
1052
1053 SelectMGLFatPen(m_brush.GetStyle(), wxMGL_SELECT_FROM_BRUSH);
1054 }
1055
1056 void wxDC::SetPen(const wxPen& pen)
1057 {
1058 if ( !pen.Ok() ) return;
1059 if ( m_pen == pen ) return;
1060 m_pen = pen;
1061 m_penSelected = FALSE;
1062 m_downloadedPatterns[0] = FALSE;
1063 }
1064
1065 void wxDC::SetBrush(const wxBrush& brush)
1066 {
1067 if ( !brush.Ok() ) return;
1068 if ( m_brush == brush ) return;
1069 m_brush = brush;
1070 m_brushSelected = FALSE;
1071 m_downloadedPatterns[1] = FALSE;
1072 }
1073
1074 void wxDC::SetPalette(const wxPalette& palette)
1075 {
1076 wxCHECK_RET( Ok(), wxT("invalid dc") );
1077
1078 if ( palette == wxNullPalette )
1079 {
1080 SetPalette(m_oldPalette);
1081 return;
1082 }
1083
1084 if ( !palette.Ok() ) return;
1085 if ( m_palette == palette ) return;
1086 m_oldPalette = m_palette;
1087 m_palette = palette;
1088
1089 int cnt = m_palette.GetColoursCount();
1090 palette_t *pal = m_palette.GetMGLpalette_t();
1091 m_MGLDC->setPalette(pal, cnt, 0);
1092 m_MGLDC->realizePalette(cnt, 0, TRUE);
1093 }
1094
1095 void wxDC::SetFont(const wxFont& font)
1096 {
1097 wxCHECK_RET( font.Ok(), wxT("invalid font") );
1098 m_font = font;
1099 m_mglFont = NULL;
1100 }
1101
1102 void wxDC::SetBackground(const wxBrush& brush)
1103 {
1104 wxCHECK_RET( Ok(), wxT("invalid dc") );
1105
1106 if (!m_backgroundBrush.Ok()) return;
1107
1108 m_backgroundBrush = brush;
1109 wxColour &clr = m_backgroundBrush.GetColour();
1110 m_MGLDC->makeCurrent(); // will go away with MGL6.0
1111 m_MGLDC->setBackColor(
1112 m_MGLDC->packColorFast(clr.Red(), clr.Green(), clr.Blue()));
1113 }
1114
1115 void wxDC::SetBackgroundMode(int mode)
1116 {
1117 m_backgroundMode = mode;
1118 if ( mode == wxSOLID )
1119 m_MGLDC->setBackMode(MGL_OPAQUE_BACKGROUND);
1120 else
1121 m_MGLDC->setBackMode(MGL_TRANSPARENT_BACKGROUND);
1122 }
1123
1124 void wxDC::SetLogicalFunction(int function)
1125 {
1126 wxCHECK_RET( Ok(), wxT("invalid dc") );
1127
1128 m_logicalFunction = function;
1129
1130 m_MGLDC->makeCurrent(); // will go away with MGL6.0
1131 m_MGLDC->setWriteMode(LogicalFunctionToMGLRop(m_logicalFunction));
1132 }
1133
1134 int wxDC::LogicalFunctionToMGLRop(int logFunc) const
1135 {
1136 MGL_writeModeType rop;
1137
1138 switch (logFunc)
1139 {
1140 case wxCLEAR: rop = MGL_R2_BLACK; break;
1141 case wxXOR: rop = MGL_R2_XORSRC; break;
1142 case wxINVERT: rop = MGL_R2_NOT; break;
1143 case wxOR_REVERSE: rop = MGL_R2_MERGESRCNOT; break;
1144 case wxAND_REVERSE: rop = MGL_R2_MASKSRCNOT; break;
1145 case wxCOPY: rop = MGL_R2_COPYSRC; break;
1146 case wxAND: rop = MGL_R2_MASKSRC; break;
1147 case wxAND_INVERT: rop = MGL_R2_MASKNOTSRC; break;
1148 case wxNO_OP: rop = MGL_R2_NOP; break;
1149 case wxNOR: rop = MGL_R2_NOTMERGESRC; break;
1150 case wxEQUIV: rop = MGL_R2_NOTXORSRC; break;
1151 case wxSRC_INVERT: rop = MGL_R2_NOTCOPYSRC; break;
1152 case wxOR_INVERT: rop = MGL_R2_MERGENOTSRC; break;
1153 case wxNAND: rop = MGL_R2_NOTMASKSRC; break;
1154 case wxOR: rop = MGL_R2_MERGESRC; break;
1155 case wxSET: rop = MGL_R2_WHITE; break;
1156 default:
1157 wxFAIL_MSG( wxT("unsupported logical function") );
1158 return MGL_REPLACE_MODE;
1159 }
1160 return (int)rop;
1161 }
1162
1163 bool wxDC::StartDoc(const wxString& message)
1164 {
1165 // We might be previewing, so return TRUE to let it continue.
1166 return TRUE;
1167 }
1168
1169 void wxDC::EndDoc()
1170 {
1171 }
1172
1173 void wxDC::StartPage()
1174 {
1175 }
1176
1177 void wxDC::EndPage()
1178 {
1179 }
1180
1181 // ---------------------------------------------------------------------------
1182 // text metrics
1183 // ---------------------------------------------------------------------------
1184
1185 wxCoord wxDC::GetCharHeight() const
1186 {
1187 wxCurrentDCSwitcher switcher(m_MGLDC);
1188 if ( !wxConstCast(this, wxDC)->SelectMGLFont() ) return -1;
1189 return YDEV2LOGREL(m_mglFont->fontHeight);
1190 }
1191
1192 wxCoord wxDC::GetCharWidth() const
1193 {
1194 wxCurrentDCSwitcher switcher(m_MGLDC);
1195 if ( !wxConstCast(this, wxDC)->SelectMGLFont() ) return -1;
1196 // VS: wxT() is intentional, charWidth() has both char and wchar_t version
1197 // VS: YDEV is corrent, it should *not* be XDEV, because font's are only
1198 // scaled according to m_scaleY
1199 return YDEV2LOGREL(m_mglFont->fontWidth);
1200 }
1201
1202 void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
1203 wxCoord *descent, wxCoord *externalLeading,
1204 wxFont *theFont) const
1205 {
1206 wxFont oldFont;
1207
1208 if ( theFont != NULL )
1209 {
1210 oldFont = m_font;
1211 wxConstCast(this, wxDC)->SetFont(*theFont);
1212 }
1213
1214 wxCurrentDCSwitcher switcher(m_MGLDC);
1215 if ( !wxConstCast(this, wxDC)->SelectMGLFont() ) return;
1216
1217 if ( x )
1218 // VS: YDEV is corrent, it should *not* be XDEV, because font's are
1219 // only scaled according to m_scaleY
1220 *x = YDEV2LOGREL(m_MGLDC->textWidth(string.c_str()));
1221 if ( y )
1222 *y = YDEV2LOGREL(m_MGLDC->textHeight());
1223 if ( descent )
1224 *descent = YDEV2LOGREL(m_mglFont->descent);
1225 if ( externalLeading )
1226 *externalLeading = YDEV2LOGREL(m_mglFont->leading);
1227
1228 if ( theFont != NULL )
1229 wxConstCast(this, wxDC)->SetFont(oldFont);
1230 }
1231
1232
1233
1234 // ---------------------------------------------------------------------------
1235 // mapping modes
1236 // ---------------------------------------------------------------------------
1237
1238 void wxDC::ComputeScaleAndOrigin()
1239 {
1240 double newX = m_logicalScaleX * m_userScaleX;
1241 double newY = m_logicalScaleY * m_userScaleY;
1242
1243 // make sure font will be reloaded before drawing:
1244 if ( newY != m_scaleY )
1245 m_mglFont = NULL;
1246 // make sure m_penOfs{X,Y} will be reevaluated before drawing:
1247 if ( newY != m_scaleY || newX != m_scaleX )
1248 m_penSelected = FALSE;
1249
1250 m_scaleX = newX, m_scaleY = newY;
1251 }
1252
1253 void wxDC::SetMapMode(int mode)
1254 {
1255 switch (mode)
1256 {
1257 case wxMM_TWIPS:
1258 SetLogicalScale(twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y);
1259 break;
1260 case wxMM_POINTS:
1261 SetLogicalScale(pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y);
1262 break;
1263 case wxMM_METRIC:
1264 SetLogicalScale(m_mm_to_pix_x, m_mm_to_pix_y);
1265 break;
1266 case wxMM_LOMETRIC:
1267 SetLogicalScale(m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0);
1268 break;
1269 default:
1270 case wxMM_TEXT:
1271 SetLogicalScale(1.0, 1.0);
1272 break;
1273 }
1274 m_mappingMode = mode;
1275 }
1276
1277 void wxDC::SetUserScale( double x, double y )
1278 {
1279 // allow negative ? -> no
1280 m_userScaleX = x;
1281 m_userScaleY = y;
1282 ComputeScaleAndOrigin();
1283 }
1284
1285 void wxDC::SetLogicalScale( double x, double y )
1286 {
1287 // allow negative ?
1288 m_logicalScaleX = x;
1289 m_logicalScaleY = y;
1290 ComputeScaleAndOrigin();
1291 }
1292
1293 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
1294 {
1295 m_logicalOriginX = x * m_signX; // is this still correct ?
1296 m_logicalOriginY = y * m_signY;
1297 ComputeScaleAndOrigin();
1298 }
1299
1300 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1301 {
1302 // only wxPostScripDC has m_signX = -1, we override SetDeviceOrigin there
1303 m_deviceOriginX = x;
1304 m_deviceOriginY = y;
1305 ComputeScaleAndOrigin();
1306 }
1307
1308 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1309 {
1310 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
1311 m_signX = (xLeftRight ? 1 : -1);
1312 m_signY = (yBottomUp ? -1 : 1);
1313 ComputeScaleAndOrigin();
1314 }
1315
1316 // ---------------------------------------------------------------------------
1317 // coordinates transformations
1318 // ---------------------------------------------------------------------------
1319
1320 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1321 {
1322 return ((wxDC *)this)->XDEV2LOG(x);
1323 }
1324
1325 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1326 {
1327 return ((wxDC *)this)->YDEV2LOG(y);
1328 }
1329
1330 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1331 {
1332 return ((wxDC *)this)->XDEV2LOGREL(x);
1333 }
1334
1335 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1336 {
1337 return ((wxDC *)this)->YDEV2LOGREL(y);
1338 }
1339
1340 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1341 {
1342 return ((wxDC *)this)->XLOG2DEV(x);
1343 }
1344
1345 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1346 {
1347 return ((wxDC *)this)->YLOG2DEV(y);
1348 }
1349
1350 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1351 {
1352 return ((wxDC *)this)->XLOG2DEVREL(x);
1353 }
1354
1355 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1356 {
1357 return ((wxDC *)this)->YLOG2DEVREL(y);
1358 }
1359
1360
1361 void wxDC::DoGetSize(int *w, int *h) const
1362 {
1363 if (w) *w = m_MGLDC->sizex();
1364 if (h) *h = m_MGLDC->sizey();
1365 }
1366
1367 void wxDC::DoGetSizeMM(int *width, int *height) const
1368 {
1369 int w = 0;
1370 int h = 0;
1371 GetSize(&w, &h);
1372 if ( width ) *width = int(double(w) / (m_userScaleX*m_mm_to_pix_x));
1373 if ( height ) *height = int(double(h) / (m_userScaleY*m_mm_to_pix_y));
1374 }
1375
1376 wxSize wxDC::GetPPI() const
1377 {
1378 return wxSize(int(double(m_mm_to_pix_x) * inches2mm),
1379 int(double(m_mm_to_pix_y) * inches2mm));
1380 }
1381
1382
1383 // ---------------------------------------------------------------------------
1384 // Blitting
1385 // ---------------------------------------------------------------------------
1386
1387 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
1388 wxCoord width, wxCoord height,
1389 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1390 int rop, bool useMask,
1391 wxCoord xsrcMask, wxCoord ysrcMask)
1392 {
1393 wxCHECK_MSG( Ok(), FALSE, wxT("invalid dc") );
1394 wxCHECK_MSG( source, FALSE, wxT("invalid source dc") );
1395
1396 // transform the source DC coords to the device ones
1397 xsrc = source->LogicalToDeviceX(xsrc);
1398 ysrc = source->LogicalToDeviceY(ysrc);
1399
1400 /* TODO: use the mask origin when drawing transparently */
1401 if (xsrcMask == -1 && ysrcMask == -1)
1402 {
1403 xsrcMask = xsrc; ysrcMask = ysrc;
1404 }
1405 else
1406 {
1407 xsrcMask = source->LogicalToDeviceX(xsrcMask);
1408 ysrcMask = source->LogicalToDeviceY(ysrcMask);
1409 }
1410
1411 CalcBoundingBox(xdest, ydest);
1412 CalcBoundingBox(xdest + width, ydest + height);
1413
1414 /* scale/translate size and position */
1415 wxCoord xx = XLOG2DEV(xdest);
1416 wxCoord yy = YLOG2DEV(ydest);
1417 wxCoord ww = XLOG2DEVREL(width);
1418 wxCoord hh = YLOG2DEVREL(height);
1419
1420 if ( source->m_isMemDC )
1421 {
1422 wxMemoryDC *memDC = (wxMemoryDC*) source;
1423 DoDrawSubBitmap(memDC->GetSelectedObject(), xsrc, ysrc, ww, hh,
1424 xdest, ydest, rop, useMask);
1425 }
1426 else
1427 {
1428 m_MGLDC->makeCurrent(); // will go away with MGL6.0
1429 m_MGLDC->bitBlt(*source->GetMGLDC(),
1430 xsrc, ysrc, xsrc + ww, ysrc + hh,
1431 xx, yy, LogicalFunctionToMGLRop(rop));
1432 }
1433
1434 return TRUE;
1435 }
1436
1437 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
1438 {
1439 wxCHECK_RET( Ok(), wxT("invalid dc") );
1440 wxCHECK_RET( bmp.Ok(), wxT("invalid bitmap") );
1441
1442 wxCoord w = bmp.GetWidth();
1443 wxCoord h = bmp.GetHeight();
1444
1445 DoDrawSubBitmap(bmp, 0, 0, w, h, x, y, m_logicalFunction, useMask);
1446 }
1447
1448 void wxDC::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y)
1449 {
1450 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
1451 DoDrawBitmap((const wxBitmap&)icon, x, y, (bool)TRUE);
1452 }
1453
1454
1455 static inline void DoBitBlt(const wxBitmap& src, MGLDevCtx *dst,
1456 int sx, int sy, int sw, int sh,
1457 int dx, int dy, int dw, int dh,
1458 int rop, bool useStretching, bool putSection)
1459 {
1460 bitmap_t *bmp = src.GetMGLbitmap_t();
1461 if (!useStretching)
1462 {
1463 if (!putSection)
1464 dst->putBitmap(dx, dy, bmp, rop);
1465 else
1466 dst->putBitmapSection(sx, sy, sx + sw, sy + sh, dx, dy, bmp, rop);
1467 }
1468 else
1469 {
1470 if (!putSection)
1471 dst->stretchBitmap(dx, dy, dx + dw, dy + dh, bmp, rop);
1472 else
1473 dst->stretchBitmapSection(sx, sy, sx + sw, sy + sh,
1474 dx, dy, dx + dw, dy + dh, bmp, rop);
1475 }
1476 }
1477
1478 void wxDC::DoDrawSubBitmap(const wxBitmap &bmp,
1479 wxCoord x, wxCoord y, wxCoord w, wxCoord h,
1480 wxCoord destx, wxCoord desty, int rop, bool useMask)
1481 {
1482 wxCHECK_RET( Ok(), wxT("invalid dc") );
1483 wxCHECK_RET( bmp.Ok(), wxT("invalid bitmap") );
1484
1485 CalcBoundingBox(x, y);
1486 CalcBoundingBox(x + w, y + h);
1487
1488 wxCoord dx = XLOG2DEV(destx);
1489 wxCoord dy = YLOG2DEV(desty);
1490 wxCoord dw = XLOG2DEVREL(w);
1491 wxCoord dh = YLOG2DEVREL(h);
1492
1493 m_MGLDC->makeCurrent(); // will go away with MGL6.0
1494
1495 bool useStretching = ((w != dw) || (h != dh));
1496 bool putSection = (w != bmp.GetWidth() || h != bmp.GetHeight());
1497 MGL_writeModeType mglRop = (MGL_writeModeType)LogicalFunctionToMGLRop(rop);
1498
1499 if ( bmp.GetDepth() == 1 )
1500 {
1501 // Mono bitmaps are handled in special way -- all 1s are drawn in
1502 // foreground colours, all 0s in background colour.
1503
1504 ((wxBitmap&)bmp).SetMonoPalette(m_textForegroundColour, m_textBackgroundColour);
1505 }
1506
1507 if ( useMask && bmp.GetMask() )
1508 {
1509 // Since MGL does not support masks directly (in MGL, mask is handled
1510 // in same way as in wxImage, i.e. there is one "key" color), we
1511 // simulate masked bitblt in 6 steps (same as in MSW):
1512 //
1513 // 1. Create a temporary bitmap and copy the destination area into it.
1514 // 2. Copy the source area into the temporary bitmap using the
1515 // specified logical function.
1516 // 3. Set the masked area in the temporary bitmap to BLACK by ANDing
1517 // the mask bitmap with the temp bitmap with the foreground colour
1518 // set to WHITE and the bg colour set to BLACK.
1519 // 4. Set the unmasked area in the destination area to BLACK by
1520 // ANDing the mask bitmap with the destination area with the
1521 // foreground colour set to BLACK and the background colour set
1522 // to WHITE.
1523 // 5. OR the temporary bitmap with the destination area.
1524 // 6. Delete the temporary bitmap.
1525 //
1526 // This sequence of operations ensures that the source's transparent
1527 // area need not be black, and logical functions are supported.
1528
1529 wxBitmap *mask = bmp.GetMask()->GetBitmap();
1530
1531 MGLMemoryDC *temp;
1532
1533 if ( GetDepth() <= 8 )
1534 {
1535 temp = new MGLMemoryDC(dw, dh, GetDepth(), NULL);
1536 wxDC tempdc;
1537 tempdc.SetMGLDC(temp, FALSE);
1538 tempdc.SetPalette(m_palette);
1539 }
1540 else
1541 {
1542 pixel_format_t pf;
1543 m_MGLDC->getPixelFormat(pf);
1544 temp = new MGLMemoryDC(dw, dh, GetDepth(), &pf);
1545 }
1546
1547 wxCHECK_RET( temp->isValid(), wxT("cannot create temporary dc") );
1548
1549 temp->bitBlt(*m_MGLDC, dx, dy, dx + dw, dy + dh, 0, 0, MGL_REPLACE_MODE);
1550
1551 DoBitBlt(bmp, temp, x, y, w, h, 0, 0, dw, dh, mglRop,
1552 useStretching, putSection);
1553
1554 mask->SetMonoPalette(wxColour(0,0,0), wxColour(255,255,255));
1555 DoBitBlt(*mask, temp, x, y, w, h, 0, 0, dw, dh, MGL_R2_MASKSRC,
1556 useStretching, putSection);
1557 DoBitBlt(*mask, m_MGLDC, x, y, w, h, dx, dy, dw, dh, MGL_R2_MASKNOTSRC,
1558 useStretching, putSection);
1559
1560 m_MGLDC->bitBlt(*temp, 0, 0, dw, dh, dx, dy, MGL_OR_MODE);
1561
1562 delete temp;
1563 }
1564
1565 else
1566 {
1567 DoBitBlt(bmp, m_MGLDC, x, y, w, h, dx, dy, dw, dh, mglRop,
1568 useStretching, putSection);
1569 }
1570 }