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