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