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