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