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