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