]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dc.cpp
moved TARGET_CARBON define to private include for MacOS X
[wxWidgets.git] / src / os2 / dc.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: dc.cpp
3// Purpose: wxDC class
fb46a9a6 4// Author: David Webster
0e320a79 5// Modified by:
fb46a9a6 6// Created: 10/14/99
0e320a79 7// RCS-ID: $Id$
fb46a9a6
DW
8// Copyright: (c) David Webster
9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
fb46a9a6
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/window.h"
17 #include "wx/dc.h"
18 #include "wx/utils.h"
19 #include "wx/dialog.h"
20 #include "wx/app.h"
21 #include "wx/bitmap.h"
22 #include "wx/dcmemory.h"
23 #include "wx/log.h"
24 #include "wx/icon.h"
b9c15d10 25 #include "wx/msgdlg.h"
19193a2c 26 #include "wx/module.h"
0e320a79
DW
27#endif
28
fb46a9a6
DW
29#include "wx/dcprint.h"
30
31#include <string.h>
32#include <math.h>
33
34#include "wx/os2/private.h"
0e320a79 35
fb46a9a6 36 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
0e320a79 37
5fd2b2c6
DW
38//
39// wxWindows uses the Microsoft convention that the origin is the UPPER left.
40// Native OS/2 however in the GPI and PM define the origin as the LOWER left.
41// In order to map OS/2 GPI/PM y coordinates to wxWindows coordinates we must
42// perform the following transformation:
43//
44// Parent object height: POBJHEIGHT
45// Desried origin: WXORIGINY
46// Object to place's height: OBJHEIGHT
47//
48// To get the OS2 position from the wxWindows one:
49//
50// OS2Y = POBJHEIGHT - (WXORIGINY + OBJHEIGHT)
51//
52// For OS/2 wxDC's we will always determine m_vRclPaint as the size of the
53// OS/2 Presentation Space associated with the device context. y is the
54// desired application's y coordinate of the origin in wxWindows space.
55// objy is the height of the object we are going to draw.
56//
57#define OS2Y(y, objy) ((m_vRclPaint.yTop - m_vRclPaint.yBottom) - (y + objy))
58
fb46a9a6 59// ---------------------------------------------------------------------------
0e320a79 60// constants
fb46a9a6 61// ---------------------------------------------------------------------------
1408104d 62
fb46a9a6 63static const int VIEWPORT_EXTENT = 1000;
1408104d 64
fb46a9a6
DW
65static const int MM_POINTS = 9;
66static const int MM_METRIC = 10;
1408104d 67
f6bcfd97
BP
68// usually this is defined in math.h
69#ifndef M_PI
70 static const double M_PI = 3.14159265358979323846;
71#endif // M_PI
72
73// ---------------------------------------------------------------------------
74// private functions
75// ---------------------------------------------------------------------------
76
77// convert degrees to radians
78static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
79
7e99520b
DW
80int SetTextColor(
81 HPS hPS
82, int nForegroundColour
83)
84{
85 CHARBUNDLE vCbnd;
86
87 vCbnd.lColor = nForegroundColour;
88 ::GpiSetAttrs( hPS // presentation-space handle
89 ,PRIM_CHAR // Char primitive.
90 ,CBB_COLOR // sets color.
91 ,0 //
92 ,&vCbnd // buffer for attributes.
93 );
94 return 0;
95}
96
97int QueryTextBkColor(
98 HPS hPS
99)
100{
101 CHARBUNDLE vCbnd;
102
f44fdfb0
DW
103 ::GpiQueryAttrs( hPS // presentation-space handle
104 ,PRIM_CHAR // Char primitive.
105 ,CBB_BACK_COLOR // Background color.
106 ,&vCbnd // buffer for attributes.
107 );
7e99520b
DW
108 return vCbnd.lBackColor;
109}
110
111
112int SetTextBkColor(
113 HPS hPS
114, int nBackgroundColour
115)
116{
117 CHARBUNDLE vCbnd;
118 int rc;
119
120 rc = QueryTextBkColor(hPS);
121
122 vCbnd.lBackColor = nBackgroundColour;
123 ::GpiSetAttrs(hPS, // presentation-space handle
124 PRIM_CHAR, // Char primitive.
125 CBB_BACK_COLOR, // sets color.
126 0,
127 &vCbnd // buffer for attributes.
128 );
129 return rc;
130}
131
132int SetBkMode(
133 HPS hPS
134, int nBackgroundMode
135)
136{
137 if(nBackgroundMode == wxTRANSPARENT)
138 ::GpiSetBackMix( hPS
139 ,BM_LEAVEALONE
140 );
141 else
142 // the background of the primitive takes over whatever is underneath.
143 ::GpiSetBackMix( hPS
144 ,BM_OVERPAINT
145 );
146 return 0;
147}
148
fb46a9a6
DW
149// ===========================================================================
150// implementation
151// ===========================================================================
1408104d 152
893758d5
DW
153#if wxUSE_DC_CACHEING
154
155/*
156 * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will
157 * improve it in due course, either using arrays, or simply storing pointers to one
158 * entry for the bitmap, and two for the DCs. -- JACS
159 */
160
161// ---------------------------------------------------------------------------
162// wxDCCacheEntry
163// ---------------------------------------------------------------------------
164
165wxList wxDC::m_svBitmapCache;
166wxList wxDC::m_svDCCache;
167
168wxDCCacheEntry::wxDCCacheEntry(
169 WXHBITMAP hBitmap
170, int nWidth
171, int nHeight
172, int nDepth
173)
174{
175 m_hBitmap = hBitmap;
176 m_hPS = NULLHANDLE;
177 m_nWidth = nWidth;
178 m_nHeight = nHeight;
179 m_nDepth = nDepth;
180} // end of wxDCCacheEntry::wxDCCacheEntry
181
182wxDCCacheEntry::wxDCCacheEntry(
183 HPS hPS
184, int nDepth
185)
186{
187 m_hBitmap = NULLHANDLE;
188 m_hPS = hPS;
189 m_nWidth = 0;
190 m_nHeight = 0;
191 m_nDepth = nDepth;
192} // end of wxDCCacheEntry::wxDCCacheEntry
193
194wxDCCacheEntry::~wxDCCacheEntry()
195{
196 if (m_hBitmap)
197 ::GpiDeleteBitmap(m_hBitmap);
198 if (m_hPS)
199 ::GpiDestroyPS(m_hPS);
200} // end of wxDCCacheEntry::~wxDCCacheEntry
201
202wxDCCacheEntry* wxDC::FindBitmapInCache(
203 HPS hPS
204, int nWidth
205, int nHeight
206)
207{
19193a2c 208 int nDepth = 24; // we'll fix this later ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
893758d5
DW
209 wxNode* pNode = m_svBitmapCache.First();
210 BITMAPINFOHEADER2 vBmpHdr;
211
212 while(pNode)
213 {
214 wxDCCacheEntry* pEntry = (wxDCCacheEntry*)pNode->Data();
215
216 if (pEntry->m_nDepth == nDepth)
217 {
218 memset(&vBmpHdr, 0, sizeof(BITMAPINFOHEADER2));
219
220 if (pEntry->m_nWidth < nWidth || pEntry->m_nHeight < nHeight)
221 {
222 ::GpiDeleteBitmap((HBITMAP)pEntry->m_hBitmap);
223 vBmpHdr.cbFix = sizeof(BITMAPINFOHEADER2);
224 vBmpHdr.cx = nWidth;
225 vBmpHdr.cy = nHeight;
226 vBmpHdr.cPlanes = 1;
227 vBmpHdr.cBitCount = nDepth;
228
229 pEntry->m_hBitmap = (WXHBITMAP) ::GpiCreateBitmap( hPS
230 ,&vBmpHdr
231 ,0L, NULL, NULL
232 );
233 if (!pEntry->m_hBitmap)
234 {
235 wxLogLastError(wxT("CreateCompatibleBitmap"));
236 }
237 pEntry->m_nWidth = nWidth;
238 pEntry->m_nHeight = nHeight;
239 return pEntry;
240 }
241 return pEntry;
242 }
243 pNode = pNode->Next();
244 }
245 memset(&vBmpHdr, 0, sizeof(BITMAPINFOHEADER2));
246 vBmpHdr.cbFix = sizeof(BITMAPINFOHEADER2);
247 vBmpHdr.cx = nWidth;
248 vBmpHdr.cy = nHeight;
249 vBmpHdr.cPlanes = 1;
250 vBmpHdr.cBitCount = nDepth;
251
252 WXHBITMAP hBitmap = (WXHBITMAP) ::GpiCreateBitmap( hPS
253 ,&vBmpHdr
254 ,0L, NULL, NULL
255 );
256 if (!hBitmap)
257 {
258 wxLogLastError(wxT("CreateCompatibleBitmap"));
259 }
260 wxDCCacheEntry* pEntry = new wxDCCacheEntry( hBitmap
261 ,nWidth
262 ,nHeight
263 ,nDepth
264 );
265 AddToBitmapCache(pEntry);
266 return pEntry;
267} // end of FindBitmapInCache
268
269wxDCCacheEntry* wxDC::FindDCInCache(
270 wxDCCacheEntry* pNotThis
271, HPS hPS
272)
273{
274 int nDepth = 24; // we'll fix this up later ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
275 wxNode* pNode = m_svDCCache.First();
276
277 while(pNode)
278 {
279 wxDCCacheEntry* pEntry = (wxDCCacheEntry*)pNode->Data();
280
281 //
282 // Don't return the same one as we already have
283 //
284 if (!pNotThis || (pNotThis != pEntry))
285 {
286 if (pEntry->m_nDepth == nDepth)
287 {
288 return pEntry;
289 }
290 }
291 pNode = pNode->Next();
292 }
293 wxDCCacheEntry* pEntry = new wxDCCacheEntry( hPS
294 ,nDepth
295 );
296 AddToDCCache(pEntry);
297 return pEntry;
298} // end of wxDC::FindDCInCache
299
300void wxDC::AddToBitmapCache(
301 wxDCCacheEntry* pEntry
302)
303{
304 m_svBitmapCache.Append(pEntry);
305} // end of wxDC::AddToBitmapCache
306
307void wxDC::AddToDCCache(
308 wxDCCacheEntry* pEntry
309)
310{
311 m_svDCCache.Append(pEntry);
312} // end of wxDC::AddToDCCache
313
314void wxDC::ClearCache()
315{
316 m_svBitmapCache.DeleteContents(TRUE);
317 m_svBitmapCache.Clear();
318 m_svBitmapCache.DeleteContents(FALSE);
319 m_svDCCache.DeleteContents(TRUE);
320 m_svDCCache.Clear();
321 m_svDCCache.DeleteContents(FALSE);
322} // end of wxDC::ClearCache
323
324// Clean up cache at app exit
325class wxDCModule : public wxModule
326{
327public:
328 virtual bool OnInit() { return TRUE; }
329 virtual void OnExit() { wxDC::ClearCache(); }
330
331private:
332 DECLARE_DYNAMIC_CLASS(wxDCModule)
333}; // end of CLASS wxDCModule
334
335IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
336
337#endif // ndef for wxUSE_DC_CACHEING
338
fb46a9a6 339// ---------------------------------------------------------------------------
0e320a79 340// wxDC
fb46a9a6 341// ---------------------------------------------------------------------------
0e320a79
DW
342
343wxDC::wxDC(void)
344{
2b0ec34b
DW
345 wxColour vColor;
346
7e99520b 347 m_pCanvas = NULL;
c3d43472 348
7e99520b
DW
349 m_hOldBitmap = 0;
350 m_hOldPen = 0;
351 m_hOldBrush = 0;
352 m_hOldFont = 0;
353 m_hOldPalette = 0;
ce44c50e 354
7e99520b
DW
355 m_bOwnsDC = FALSE;
356 m_hDC = 0;
7e99520b
DW
357 m_hOldPS = NULL;
358 m_hPS = NULL;
e99762c0 359 m_bIsPaintTime = FALSE; // True at Paint Time
2b0ec34b
DW
360
361 vColor.InitFromName("BLACK");
362 m_pen.SetColour(vColor);
363 vColor.Set("WHITE");
364 m_brush.SetColour(vColor);
e1a688e4 365} // end of wxDC::wxDC
0e320a79
DW
366
367wxDC::~wxDC(void)
368{
e1a688e4
DW
369 if ( m_hDC != 0 )
370 {
371 SelectOldObjects(m_hDC);
372
373 // if we own the HDC, we delete it, otherwise we just release it
374
375 if (m_bOwnsDC)
376 {
377 if(m_hPS)
378 {
379 ::GpiAssociate(m_hPS, NULLHANDLE);
380 ::GpiDestroyPS(m_hPS);
381 }
382 m_hPS = NULLHANDLE;
383 ::DevCloseDC((HDC)m_hDC);
384 }
385 else
386 {
387 //
388 // Just Dissacociate, not destroy if we don't own the DC
389 //
390 if(m_hPS)
391 {
392 ::GpiAssociate(m_hPS, NULLHANDLE);
393 }
394 }
395 }
396} // end of wxDC::~wxDC
0e320a79 397
fb46a9a6
DW
398// This will select current objects out of the DC,
399// which is what you have to do before deleting the
400// DC.
f07bb01b
DW
401void wxDC::SelectOldObjects(
402 WXHDC hPS
403)
0e320a79 404{
f07bb01b 405 if (hPS)
fb46a9a6 406 {
f6bcfd97 407 if (m_hOldBitmap)
fb46a9a6 408 {
f07bb01b 409 ::GpiSetBitmap(hPS, (HBITMAP) m_hOldBitmap);
f6bcfd97 410 if (m_vSelectedBitmap.Ok())
fb46a9a6 411 {
f6bcfd97 412 m_vSelectedBitmap.SetSelectedInto(NULL);
fb46a9a6
DW
413 }
414 }
f6bcfd97 415 m_hOldBitmap = 0;
f07bb01b
DW
416 //
417 // OS/2 has no other native GDI objects to set in a PS/DC like windows
418 //
f6bcfd97 419 m_hOldPen = 0;
f6bcfd97 420 m_hOldBrush = 0;
f6bcfd97 421 m_hOldFont = 0;
f6bcfd97 422 m_hOldPalette = 0;
fb46a9a6 423 }
0e320a79 424
f6bcfd97
BP
425 m_brush = wxNullBrush;
426 m_pen = wxNullPen;
427 m_palette = wxNullPalette;
428 m_font = wxNullFont;
fb46a9a6 429 m_backgroundBrush = wxNullBrush;
f6bcfd97 430 m_vSelectedBitmap = wxNullBitmap;
e1a688e4 431} // end of wxDC::SelectOldObjects
0e320a79 432
fb46a9a6
DW
433// ---------------------------------------------------------------------------
434// clipping
435// ---------------------------------------------------------------------------
0e320a79 436
fa5593ac
DW
437#define DO_SET_CLIPPING_BOX() \
438{ \
439 RECTL rect; \
440 \
441 ::GpiQueryClipBox(m_hPS, &rect); \
442 \
443 m_clipX1 = (wxCoord) XDEV2LOG(rect.xLeft); \
444 m_clipY1 = (wxCoord) YDEV2LOG(rect.yTop); \
445 m_clipX2 = (wxCoord) XDEV2LOG(rect.xRight); \
446 m_clipY2 = (wxCoord) YDEV2LOG(rect.yBottom); \
fb46a9a6 447}
0e320a79 448
fa5593ac 449void wxDC::DoSetClippingRegion(
5fd2b2c6
DW
450 wxCoord vX
451, wxCoord vY
452, wxCoord vWidth
453, wxCoord vHeight
fa5593ac 454)
0e320a79 455{
fa5593ac
DW
456 RECTL vRect;
457
5fd2b2c6 458 vY = OS2Y(vY,vHeight);
fa5593ac 459 m_clipping = TRUE;
5fd2b2c6
DW
460 vRect.xLeft = vX;
461 vRect.yTop = vY + vHeight;
462 vRect.xRight = vX + vWidth;
463 vRect.yBottom = vY;
fa5593ac
DW
464 ::GpiIntersectClipRectangle(m_hPS, &vRect);
465 DO_SET_CLIPPING_BOX()
466} // end of wxDC::DoSetClippingRegion
467
468void wxDC::DoSetClippingRegionAsRegion(
469 const wxRegion& rRegion
470)
0e320a79 471{
fa5593ac
DW
472 wxCHECK_RET(rRegion.GetHRGN(), wxT("invalid clipping region"));
473 HRGN hRgnOld;
474
475 m_clipping = TRUE;
476 ::GpiSetClipRegion( m_hPS
477 ,(HRGN)rRegion.GetHRGN()
478 ,&hRgnOld
479 );
480 DO_SET_CLIPPING_BOX()
481} // end of wxDC::DoSetClippingRegionAsRegion
0e320a79 482
fb46a9a6 483void wxDC::DestroyClippingRegion(void)
0e320a79 484{
fa5593ac
DW
485 if (m_clipping && m_hPS)
486 {
487 HRGN hRgnOld;
488 RECTL vRect;
489
490 // TODO: this should restore the previous clipped region
491 // so that OnPaint processing works correctly, and
492 // the update doesn't get destroyed after the first
493 // DestroyClippingRegion
494 vRect.xLeft = XLOG2DEV(0);
495 vRect.yTop = YLOG2DEV(32000);
496 vRect.xRight = XLOG2DEV(32000);
497 vRect.yBottom = YLOG2DEV(0);
498
499 HRGN hRgn = ::GpiCreateRegion(m_hPS, 1, &vRect);
500
501 ::GpiSetClipRegion(m_hPS, hRgn, &hRgnOld);
502 }
503 m_clipping = FALSE;
504} // end of wxDC::DestroyClippingRegion
0e320a79 505
fb46a9a6
DW
506// ---------------------------------------------------------------------------
507// query capabilities
508// ---------------------------------------------------------------------------
0e320a79 509
fb46a9a6 510bool wxDC::CanDrawBitmap() const
0e320a79 511{
fb46a9a6
DW
512 return TRUE;
513}
0e320a79 514
fb46a9a6 515bool wxDC::CanGetTextExtent() const
0e320a79 516{
f07bb01b 517 LONG lTechnology = 0L;
0e320a79 518
f07bb01b
DW
519 ::DevQueryCaps(GetHDC(), CAPS_TECHNOLOGY, 1L, &lTechnology);
520 return (lTechnology == CAPS_TECH_RASTER_DISPLAY) || (lTechnology == CAPS_TECH_RASTER_PRINTER);
521} // end of wxDC::CanGetTextExtent
1408104d
DW
522
523int wxDC::GetDepth() const
0e320a79 524{
f07bb01b
DW
525 LONG lArray[CAPS_COLOR_BITCOUNT];
526 int nBitsPerPixel;
527
528 if(::DevQueryCaps( GetHDC()
529 ,CAPS_FAMILY
530 ,CAPS_COLOR_BITCOUNT
531 ,lArray
532 ))
533 {
534 nBitsPerPixel = (int)lArray[CAPS_COLOR_BITCOUNT];
535 }
536 return nBitsPerPixel;
537} // end of wxDC::GetDepth
0e320a79 538
fb46a9a6
DW
539// ---------------------------------------------------------------------------
540// drawing
541// ---------------------------------------------------------------------------
0e320a79 542
1408104d 543void wxDC::Clear()
0e320a79 544{
445c7bca 545 ::GpiErase(m_hPS);
5fd2b2c6 546} // end of wxDC::Clear
0e320a79 547
51c1d535
DW
548void wxDC::DoFloodFill(
549 wxCoord vX
550, wxCoord vY
551, const wxColour& rCol
552, int nStyle
553)
0e320a79 554{
51c1d535
DW
555 POINTL vPtlPos;
556 LONG lColor;
557 LONG lOptions;
558
559 vPtlPos.x = vX; // Loads x-coordinate
5fd2b2c6 560 vPtlPos.y = OS2Y(vY,0); // Loads y-coordinate
51c1d535
DW
561 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
562 lColor = rCol.GetPixel();
563 lOptions = FF_BOUNDARY;
564 if(wxFLOOD_SURFACE == nStyle)
565 lOptions = FF_SURFACE;
566
567 ::GpiFloodFill(m_hPS, lOptions, lColor);
5fd2b2c6 568} // end of wxDC::DoFloodFill
0e320a79 569
51c1d535
DW
570bool wxDC::DoGetPixel(
571 wxCoord vX
572, wxCoord vY
573, wxColour* pCol
574) const
1408104d 575{
51c1d535
DW
576 POINTL vPoint;
577 LONG lColor;
578
579 vPoint.x = vX;
5fd2b2c6 580 vPoint.y = OS2Y(vY,0);
51c1d535 581 lColor = ::GpiSetPel(m_hPS, &vPoint);
5fd2b2c6
DW
582
583 //
584 // Get the color of the pen
585 //
586 LONG lPencolor = 0x00ffffff;
587
588 if (m_pen.Ok())
589 {
590 lPencolor = m_pen.GetColour().GetPixel();
591 }
592
593 //
594 // return the color of the pixel
595 //
596 if(pCol)
597 pCol->Set( GetRValue(lColor)
598 ,GetGValue(lColor)
599 ,GetBValue(lColor)
600 );
601 return(lColor == lPencolor);
602} // end of wxDC::DoGetPixel
0e320a79 603
f07bb01b
DW
604void wxDC::DoCrossHair(
605 wxCoord vX
606, wxCoord vY
607)
0e320a79 608{
5fd2b2c6
DW
609 vY = OS2Y(vY,0);
610
f07bb01b
DW
611 wxCoord vX1 = vX - VIEWPORT_EXTENT;
612 wxCoord vY1 = vY - VIEWPORT_EXTENT;
613 wxCoord vX2 = vX + VIEWPORT_EXTENT;
614 wxCoord vY2 = vY + VIEWPORT_EXTENT;
615 POINTL vPoint[4];
616
617 vPoint[0].x = vX1;
5fd2b2c6 618 vPoint[0].y = vY;
f07bb01b
DW
619
620 vPoint[1].x = vX2;
5fd2b2c6 621 vPoint[1].y = vY;
f07bb01b
DW
622
623 ::GpiMove(m_hPS, &vPoint[0]);
624 ::GpiLine(m_hPS, &vPoint[1]);
625
626 vPoint[2].x = vX;
5fd2b2c6 627 vPoint[2].y = vY1;
f07bb01b
DW
628
629 vPoint[3].x = vX;
5fd2b2c6 630 vPoint[3].y = vY2;
f07bb01b
DW
631
632 ::GpiMove(m_hPS, &vPoint[2]);
633 ::GpiLine(m_hPS, &vPoint[3]);
5fd2b2c6
DW
634 CalcBoundingBox(vX1, vY1);
635 CalcBoundingBox(vX2, vY2);
f07bb01b 636} // end of wxDC::DoCrossHair
1408104d 637
7e99520b
DW
638void wxDC::DoDrawLine(
639 wxCoord vX1
640, wxCoord vY1
641, wxCoord vX2
642, wxCoord vY2
643)
0e320a79 644{
7e99520b
DW
645 POINTL vPoint[2];
646
5fd2b2c6
DW
647 vY1 = OS2Y(vY1,0);
648 vY2 = OS2Y(vY2,0);
649
7e99520b 650 vPoint[0].x = vX1;
5fd2b2c6 651 vPoint[0].y = vY1;
7e99520b 652 vPoint[1].x = vX2;
5fd2b2c6 653 vPoint[1].y = vY2;
7e99520b
DW
654 ::GpiMove(m_hPS, &vPoint[0]);
655 ::GpiLine(m_hPS, &vPoint[1]);
5fd2b2c6
DW
656 CalcBoundingBox(vX1, vY1);
657 CalcBoundingBox(vX2, vY2);
f07bb01b 658} // end of wxDC::DoDrawLine
1408104d 659
51c1d535
DW
660//////////////////////////////////////////////////////////////////////////////
661// Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
662// and ending at (x2, y2). The current pen is used for the outline and the
663// current brush for filling the shape. The arc is drawn in an anticlockwise
664// direction from the start point to the end point.
665//////////////////////////////////////////////////////////////////////////////
666void wxDC::DoDrawArc(
667 wxCoord vX1
668, wxCoord vY1
669, wxCoord vX2
670, wxCoord vY2
671, wxCoord vXc
672, wxCoord vYc
673)
1408104d 674{
51c1d535
DW
675 POINTL vPtlPos;
676 POINTL vPtlArc[2]; // Structure for current position
677 int nDx;
678 int nDy;
679 double dRadius;
680 double dAngl1;
681 double dAngl2;
682 double dAnglmid;
683 wxCoord vXm;
684 wxCoord vYm;
685 ARCPARAMS vArcp; // Structure for arc parameters
686
687 if((vX1 == vXc && vY1 == vXc) || (vX2 == vXc && vY2 == vXc))
688 return; // Draw point ??
689 dRadius = 0.5 * ( hypot( (double)(vY1 - vYc)
690 ,(double)(vX1 - vXc)
691 ) +
692 hypot( (double)(vY2 - vYc)
693 ,(double)(vX2 - vXc)
694 )
695 );
696
697 dAngl1 = atan2( (double)(vY1 - vYc)
698 ,(double)(vX1 - vXc)
699 );
700 dAngl2 = atan2( (double)(vY2 - vYc)
701 ,(double)(vX2 - vXc)
702 );
703 if(dAngl2 < dAngl1)
704 dAngl2 += M_PI * 2;
705
706 //
707 // GpiPointArc can't draw full arc
708 //
709 if(dAngl2 == dAngl1 || (vX1 == vX2 && vY1 == vY2) )
710 {
711 //
712 // Medium point
713 //
714 dAnglmid = (dAngl1 + dAngl2)/2. + M_PI;
715 vXm = vXc + dRadius * cos(dAnglmid);
716 vYm = vYc + dRadius * sin(dAnglmid);
e99762c0
DW
717 DoDrawArc( vX1, vY1
718 ,vXm, vYm
719 ,vXc, vYc
51c1d535 720 );
e99762c0
DW
721 DoDrawArc( vXm, vYm
722 ,vX2, vY2
723 ,vXc, vYc
51c1d535
DW
724 );
725 return;
726 }
727
728 //
729 // Medium point
730 //
731 dAnglmid = (dAngl1 + dAngl2)/2.;
732 vXm = vXc + dRadius * cos(dAnglmid);
733 vYm = vYc + dRadius * sin(dAnglmid);
734
735 //
736 // Ellipse main axis (r,q), (p,s) with center at (0,0) */
737 //
738 vArcp.lR = 0;
739 vArcp.lQ = 1;
740 vArcp.lP = 1;
741 vArcp.lS = 0;
742 ::GpiSetArcParams(m_hPS, &vArcp); // Sets parameters to default
743
744 vPtlPos.x = vX1; // Loads x-coordinate
745 vPtlPos.y = vY1; // Loads y-coordinate
746 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
e99762c0
DW
747 vPtlArc[0].x = vXm;
748 vPtlArc[0].y = vYm;
51c1d535
DW
749 vPtlArc[1].x = vX2;
750 vPtlArc[1].y = vY2;
751 ::GpiPointArc(m_hPS, vPtlArc); // Draws the arc
5fd2b2c6
DW
752 CalcBoundingBox( (vXc - dRadius)
753 ,(vYc - dRadius)
754 );
755 CalcBoundingBox( (vXc + dRadius)
756 ,(vYc + dRadius)
757 );
f07bb01b 758} // end of wxDC::DoDrawArc
1408104d 759
51c1d535
DW
760void wxDC::DoDrawCheckMark(
761 wxCoord vX1
762, wxCoord vY1
763, wxCoord vWidth
764, wxCoord vHeight
765)
f6bcfd97 766{
51c1d535
DW
767 POINTL vPoint[2];
768
5fd2b2c6
DW
769 vY1 = OS2Y(vY1,vHeight);
770
51c1d535
DW
771 vPoint[0].x = vX1;
772 vPoint[0].y = vY1;
773 vPoint[1].x = vX1 + vWidth;
774 vPoint[1].y = vY1 + vHeight;
775
776 ::GpiMove(m_hPS, &vPoint[0]);
777 ::GpiBox( m_hPS // handle to a presentation space
778 ,DRO_OUTLINE // draw the box outline ? or ?
779 ,&vPoint[1] // address of the corner
780 ,0L // horizontal corner radius
781 ,0L // vertical corner radius
782 );
783 if(vWidth > 4 && vHeight > 4)
784 {
785 int nTmp;
786
787 vPoint[0].x += 2; vPoint[0].y += 2;
788 vPoint[1].x -= 2; vPoint[1].y -= 2;
789 ::GpiMove(m_hPS, &vPoint[0]);
790 ::GpiLine(m_hPS, &vPoint[1]);
791 nTmp = vPoint[0].x;
792 vPoint[0].x = vPoint[1].x;
793 vPoint[1].x = nTmp;
794 ::GpiMove(m_hPS, &vPoint[0]);
795 ::GpiLine(m_hPS, &vPoint[1]);
796 }
5fd2b2c6
DW
797 CalcBoundingBox( vX1
798 ,vY1
799 );
800
801 wxCoord vX2 = vX1 + vWidth;
802 wxCoord vY2 = vY1 + vHeight;
803
804 CalcBoundingBox( vX2
805 ,vY2
806 );
f07bb01b 807} // end of wxDC::DoDrawCheckMark
f6bcfd97 808
51c1d535
DW
809void wxDC::DoDrawPoint(
810 wxCoord vX
811, wxCoord vY
812)
1408104d 813{
51c1d535 814 POINTL vPoint;
5fd2b2c6 815 COLORREF vColor = 0x00ffffff;
51c1d535 816
5fd2b2c6
DW
817 if (m_pen.Ok())
818 {
819 vColor = m_pen.GetColour().GetPixel();
820 }
821 ::GpiSetColor(m_hPS, vColor);
51c1d535 822 vPoint.x = vX;
5fd2b2c6 823 vPoint.y = OS2Y(vY,0);
51c1d535 824 ::GpiSetPel(m_hPS, &vPoint);
5fd2b2c6
DW
825 CalcBoundingBox( vX
826 ,vY
827 );
f07bb01b 828} // end of wxDC::DoDrawPoint
1408104d 829
51c1d535
DW
830void wxDC::DoDrawPolygon(
831 int n
832, wxPoint vPoints[]
833, wxCoord vXoffset
834, wxCoord vYoffset
835, int nFillStyle
836)
1408104d 837{
51c1d535
DW
838 ULONG ulCount = 1; // Number of polygons.
839 POLYGON vPlgn; // polygon.
840 ULONG flOptions = 0L; // Drawing options.
841
842//////////////////////////////////////////////////////////////////////////////
843// This contains fields of option bits... to draw boundary lines as well as
844// the area interior.
845//
846// Drawing boundary lines:
847// POLYGON_NOBOUNDARY Does not draw boundary lines.
848// POLYGON_BOUNDARY Draws boundary lines (the default).
849//
850// Construction of the area interior:
851// POLYGON_ALTERNATE Constructs interior in alternate mode
852// (the default).
853// POLYGON_WINDING Constructs interior in winding mode.
854//////////////////////////////////////////////////////////////////////////////
855
856 ULONG flModel = 0L; // Drawing model.
857
858//////////////////////////////////////////////////////////////////////////////
859// Drawing model.
860// POLYGON_INCL Fill is inclusive of bottom right (the default).
861// POLYGON_EXCL Fill is exclusive of bottom right.
862// This is provided to aid migration from other graphics models.
863//////////////////////////////////////////////////////////////////////////////
864
865 LONG lHits = 0L; // Correlation/error indicator.
866 POINTL vPoint;
867 int i;
868 int nIsTRANSPARENT = 0;
869 LONG lBorderColor = 0L;
870 LONG lColor = 0L;
871
872 lBorderColor = m_pen.GetColour().GetPixel();
873 lColor = m_brush.GetColour().GetPixel();
874 if(m_brush.GetStyle() == wxTRANSPARENT)
875 nIsTRANSPARENT = 1;
876
877 vPlgn.ulPoints = n;
878 vPlgn.aPointl = (POINTL*) calloc( n + 1
879 ,sizeof(POINTL)
880 ); // well, new will call malloc
881
882 for(i = 0; i < n; i++)
883 {
5fd2b2c6
DW
884 vPlgn.aPointl[i].x = vPoints[i].x; // +xoffset;
885 vPlgn.aPointl[i].y = OS2Y(vPoints[i].y,0); // +yoffset;
51c1d535
DW
886 }
887 flModel = POLYGON_BOUNDARY;
888 if(nFillStyle == wxWINDING_RULE)
889 flModel |= POLYGON_WINDING;
890 else
891 flModel |= POLYGON_ALTERNATE;
892
893 vPoint.x = vXoffset;
5fd2b2c6 894 vPoint.y = OS2Y(vYoffset,0);
51c1d535
DW
895
896 ::GpiSetColor(m_hPS, lBorderColor);
897 ::GpiMove(m_hPS, &vPoint);
898 lHits = ::GpiPolygons(m_hPS, ulCount, &vPlgn, flOptions, flModel);
899 free(vPlgn.aPointl);
f07bb01b 900} // end of wxDC::DoDrawPolygon
1408104d 901
51c1d535
DW
902void wxDC::DoDrawLines(
903 int n
904, wxPoint vPoints[]
905, wxCoord vXoffset
906, wxCoord vYoffset
907)
1408104d 908{
51c1d535
DW
909 POINTL vPoint;
910
5fd2b2c6
DW
911 if (vXoffset != 0L || vXoffset != 0L)
912 {
913 int i;
914
915 vPoint.x = vPoints[0].x + vXoffset;
916 vPoint.y = OS2Y(vPoints[0].y + vYoffset,0);
917 ::GpiMove(m_hPS, &vPoint);
51c1d535 918
5fd2b2c6 919 LONG lBorderColor = m_pen.GetColour().GetPixel();
51c1d535 920
5fd2b2c6
DW
921 ::GpiSetColor(m_hPS, lBorderColor);
922 for(i = 1; i < n; i++)
923 {
924 vPoint.x = vPoints[i].x + vXoffset;
925 vPoint.y = OS2Y(vPoints[i].y + vYoffset,0);
926 ::GpiLine(m_hPS, &vPoint);
927 }
928 }
929 else
51c1d535 930 {
5fd2b2c6
DW
931 int i;
932
933 CalcBoundingBox( vPoints[i].x
934 ,vPoints[i].y
935 );
936 vPoint.x = vPoints[0].x;
937 vPoint.y = OS2Y(vPoints[0].y,0);
938 ::GpiMove(m_hPS, &vPoint);
939
940 for (i = 0; i < n; i++)
941 {
942 CalcBoundingBox( vPoints[i].x
943 ,vPoints[i].y
944 );
945 vPoint.x = vPoints[i].x;
946 vPoint.y = OS2Y(vPoints[i].y,0);
947 ::GpiLine(m_hPS, &vPoint);
948 }
51c1d535 949 }
f07bb01b 950} // end of wxDC::DoDrawLines
1408104d 951
7e99520b 952void wxDC::DoDrawRectangle(
f44fdfb0 953 wxCoord vX
7e99520b
DW
954, wxCoord vY
955, wxCoord vWidth
956, wxCoord vHeight
957)
1408104d 958{
7e99520b 959 POINTL vPoint[2];
51c1d535
DW
960 LONG lControl;
961 LONG lColor;
962 LONG lBorderColor;
963 int nIsTRANSPARENT = 0;
7e99520b 964
5fd2b2c6
DW
965 vY = OS2Y(vY,vHeight);
966
967 wxCoord vX2 = vX + vWidth;
968 wxCoord vY2 = vY + vHeight;
969
7e99520b 970 vPoint[0].x = vX;
5fd2b2c6 971 vPoint[0].y = vY;
f44fdfb0 972 vPoint[1].x = vX + vWidth;
5fd2b2c6 973 vPoint[1].y = vY + vHeight;
7e99520b 974 ::GpiMove(m_hPS, &vPoint[0]);
51c1d535
DW
975 lColor = m_brush.GetColour().GetPixel();
976 lBorderColor = m_pen.GetColour().GetPixel();
977 if (m_brush.GetStyle() == wxTRANSPARENT)
978 nIsTRANSPARENT = 1;
979 if(lColor == lBorderColor || nIsTRANSPARENT)
980 {
981 lControl = DRO_OUTLINEFILL; //DRO_FILL;
982 if(m_brush.GetStyle() == wxTRANSPARENT)
983 lControl = DRO_OUTLINE;
984
8d854fa9 985 ::GpiSetColor(m_hPS, lColor);
51c1d535
DW
986 ::GpiBox( m_hPS // handle to a presentation space
987 ,lControl // draw the box outline ? or ?
988 ,&vPoint[1] // address of the corner
989 ,0L // horizontal corner radius
990 ,0L // vertical corner radius
991 );
992 }
993 else
994 {
995 lControl = DRO_OUTLINE;
996 ::GpiSetColor( m_hPS
997 ,lBorderColor
998 );
999 ::GpiBox( m_hPS
1000 ,lControl
1001 ,&vPoint[1]
1002 ,0L
1003 ,0L
1004 );
1005 lControl = DRO_FILL;
1006 ::GpiSetColor( m_hPS
1007 ,lColor
1008 );
8d854fa9 1009 vPoint[0].x = vX + 1;
5fd2b2c6 1010 vPoint[0].y = vY + 1;
ad7f3189
DW
1011 vPoint[1].x = vX + vWidth - 1;
1012 vPoint[1].y = vY + vHeight - 1;
8d854fa9 1013 ::GpiMove(m_hPS, &vPoint[0]);
51c1d535
DW
1014 ::GpiBox( m_hPS
1015 ,lControl
1016 ,&vPoint[1]
1017 ,0L
1018 ,0L
1019 );
1020 }
5fd2b2c6
DW
1021 CalcBoundingBox(vX, vY);
1022 CalcBoundingBox(vX2, vY2);
f07bb01b 1023} // end of wxDC::DoDrawRectangle
1408104d 1024
7e99520b
DW
1025void wxDC::DoDrawRoundedRectangle(
1026 wxCoord vX
1027, wxCoord vY
1028, wxCoord vWidth
1029, wxCoord vHeight
1030, double dRadius
1031)
1408104d 1032{
7e99520b 1033 POINTL vPoint[2];
51c1d535 1034 LONG lControl;
7e99520b 1035
5fd2b2c6
DW
1036 vY = OS2Y(vY,vHeight);
1037
1038 wxCoord vX2 = (vX + vWidth);
1039 wxCoord vY2 = (vY + vHeight);
1040
7e99520b 1041 vPoint[0].x = vX;
8d854fa9 1042 vPoint[0].y = YLOG2DEV(vY) - vHeight;
7e99520b 1043 vPoint[1].x = vX + vWidth;
8d854fa9 1044 vPoint[1].y = vY;
7e99520b 1045 ::GpiMove(m_hPS, &vPoint[0]);
51c1d535
DW
1046
1047 lControl = DRO_OUTLINEFILL; //DRO_FILL;
1048 if (m_brush.GetStyle() == wxTRANSPARENT)
1049 lControl = DRO_OUTLINE;
f44fdfb0
DW
1050 ::GpiBox( m_hPS // handle to a presentation space
1051 ,DRO_OUTLINE // draw the box outline ? or ?
1052 ,&vPoint[1] // address of the corner
1053 ,(LONG)dRadius // horizontal corner radius
1054 ,(LONG)dRadius // vertical corner radius
7e99520b 1055 );
5fd2b2c6
DW
1056 CalcBoundingBox(vX, vY);
1057 CalcBoundingBox(vX2, vY2);
f07bb01b 1058} // end of wxDC::DoDrawRoundedRectangle
1408104d 1059
51c1d535
DW
1060// Draw Ellipse within box (x,y) - (x+width, y+height)
1061void wxDC::DoDrawEllipse(
1062 wxCoord vX
1063, wxCoord vY
1064, wxCoord vWidth
1065, wxCoord vHeight
1066)
1408104d 1067{
51c1d535
DW
1068 POINTL vPtlPos; // Structure for current position
1069 FIXED vFxMult; // Multiplier for ellipse
1070 ARCPARAMS vArcp; // Structure for arc parameters
1071
5fd2b2c6
DW
1072 vY = OS2Y(vY,vHeight);
1073
51c1d535
DW
1074 vArcp.lR = 0;
1075 vArcp.lQ = vHeight/2;
1076 vArcp.lP = vWidth/2;
1077 vArcp.lS = 0;
1078 ::GpiSetArcParams( m_hPS
1079 ,&vArcp
1080 ); // Sets parameters to default
1081 vPtlPos.x = vX + vWidth/2; // Loads x-coordinate
1082 vPtlPos.y = vY + vHeight/2; // Loads y-coordinate
1083 ::GpiMove( m_hPS
1084 ,&vPtlPos
1085 ); // Sets current position
1086 vFxMult = MAKEFIXED(1, 0); /* Sets multiplier */
1087
1088 //
1089 // DRO_FILL, DRO_OTLINEFILL - where to get
1090 //
1091 ::GpiFullArc( m_hPS
1092 ,DRO_OUTLINE
1093 ,vFxMult
1094 ); // Draws full arc with center at current position
5fd2b2c6
DW
1095
1096 wxCoord vX2 = (vX + vWidth);
1097 wxCoord vY2 = (vY + vHeight);
1098
1099 CalcBoundingBox(vX, vY);
1100 CalcBoundingBox(vX2, vY2);
f07bb01b 1101} // end of wxDC::DoDrawEllipse
1408104d 1102
51c1d535
DW
1103void wxDC::DoDrawEllipticArc(
1104 wxCoord vX
1105, wxCoord vY
1106, wxCoord vWidth
1107, wxCoord vHeight
1108, double dSa
1109, double dEa
1110)
1408104d 1111{
51c1d535
DW
1112 POINTL vPtlPos; // Structure for current position
1113 FIXED vFxMult; // Multiplier for ellipse
1114 ARCPARAMS vArcp; // Structure for arc parameters
1115 FIXED vFSa;
1116 FIXED vFSweepa; // Start angle, sweep angle
1117 double dIntPart;
1118 double dFractPart;
1119 double dRadius;
1120
5fd2b2c6
DW
1121 vY = OS2Y(vY,vHeight);
1122
51c1d535
DW
1123 dFractPart = modf(dSa,&dIntPart);
1124 vFSa = MAKEFIXED((int)dIntPart, (int)(dFractPart * 0xffff) );
1125 dFractPart = modf(dEa - dSa, &dIntPart);
1126 vFSweepa = MAKEFIXED((int)dIntPart, (int)(dFractPart * 0xffff) );
1127
1128 //
1129 // Ellipse main axis (r,q), (p,s) with center at (0,0)
1130 //
1131 vArcp.lR = 0;
1132 vArcp.lQ = vHeight/2;
1133 vArcp.lP = vWidth/2;
1134 vArcp.lS = 0;
1135 ::GpiSetArcParams(m_hPS, &vArcp); // Sets parameters to default
1136 vPtlPos.x = vX + vWidth/2 * (1. + cos(DegToRad(dSa))); // Loads x-coordinate
1137 vPtlPos.y = vY + vHeight/2 * (1. + sin(DegToRad(dSa))); // Loads y-coordinate
1138 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
1139
1140 //
1141 // May be not to the center ?
1142 //
1143 vPtlPos.x = vX + vWidth/2 ; // Loads x-coordinate
1144 vPtlPos.y = vY + vHeight/2; // Loads y-coordinate
1145 vFxMult = MAKEFIXED(1, 0); // Sets multiplier
1146
1147 //
1148 // DRO_FILL, DRO_OTLINEFILL - where to get
1149 //
1150 ::GpiPartialArc( m_hPS
1151 ,&vPtlPos
1152 ,vFxMult
1153 ,vFSa
1154 ,vFSweepa
1155 );
5fd2b2c6
DW
1156 wxCoord vX2 = (vX + vWidth);
1157 wxCoord vY2 = (vY + vHeight);
1158
1159 CalcBoundingBox(vX, vY);
1160 CalcBoundingBox(vX2, vY2);
f07bb01b 1161} // end of wxDC::DoDrawEllipticArc
1408104d 1162
f07bb01b
DW
1163void wxDC::DoDrawIcon(
1164 const wxIcon& rIcon
1165, wxCoord vX
1166, wxCoord vY
1167)
1408104d 1168{
5fd2b2c6 1169 vY = OS2Y(vY,rIcon.GetHeight());
f07bb01b
DW
1170 wxCHECK_RET( rIcon.Ok(), wxT("invalid icon in DrawIcon") );
1171
1172 ::WinDrawPointer( GetHPS()
1173 ,vX
1174 ,vY
1175 ,(HPOINTER)GetHiconOf(rIcon)
1176 ,DP_NORMAL
1177 );
5fd2b2c6
DW
1178 CalcBoundingBox(vX, vY);
1179 CalcBoundingBox(vX + rIcon.GetWidth(), vY + rIcon.GetHeight());
f07bb01b
DW
1180} // end of wxDC::DoDrawIcon
1181
1182void wxDC::DoDrawBitmap(
1183 const wxBitmap& rBmp
1184, wxCoord vX
1185, wxCoord vY
1186, bool bUseMask
1187)
1408104d 1188{
f07bb01b
DW
1189 POINTL vPoint = {vX, vY};
1190
1191 ::WinDrawBitmap( GetHPS()
1192 ,(HBITMAP)GetHbitmapOf(rBmp)
1193 ,NULL
1194 ,&vPoint
1195 ,0L
1196 ,0L
1197 ,DBM_NORMAL
1198 );
1199} // end of wxDC::DoDrawBitmap
1408104d 1200
7e99520b
DW
1201void wxDC::DoDrawText(
1202 const wxString& rsText
1203, wxCoord vX
1204, wxCoord vY
1205)
1408104d 1206{
5fd2b2c6
DW
1207 wxCoord vWidth;
1208 wxCoord vHeight;
1209
7e99520b
DW
1210 DrawAnyText( rsText
1211 ,vX
1212 ,vY
1213 );
5fd2b2c6
DW
1214
1215 CalcBoundingBox(vX, vY);
1216 GetTextExtent(rsText, &vWidth, &vHeight);
1217 CalcBoundingBox((vX + vWidth), (vY + vHeight));
1218} // end of wxDC::DoDrawText
1408104d 1219
7e99520b
DW
1220void wxDC::DrawAnyText(
1221 const wxString& rsText
1222, wxCoord vX
1223, wxCoord vY
1224)
f6bcfd97 1225{
7e99520b
DW
1226 int nOldBackground = 0;
1227 POINTL vPtlStart;
1228 LONG lHits;
5fd2b2c6
DW
1229 wxCoord vTextX = 0;
1230 wxCoord vTextY = 0;
f6bcfd97 1231
7e99520b
DW
1232 //
1233 // prepare for drawing the text
1234 //
1235
1236 //
1237 // Set text color attributes
1238 //
1239 if (m_textForegroundColour.Ok())
1240 {
1241 SetTextColor( m_hPS
1242 ,(int)m_textForegroundColour.GetPixel()
1243 );
1244 }
1245
1246 if (m_textBackgroundColour.Ok())
1247 {
1248 nOldBackground = SetTextBkColor( m_hPS
1249 ,(int)m_textBackgroundColour.GetPixel()
1250 );
1251 }
1252 SetBkMode( m_hPS
1253 ,m_backgroundMode
1254 );
5fd2b2c6
DW
1255 GetTextExtent( rsText
1256 ,&vTextX
1257 ,&vTextY
1258 );
7e99520b 1259 vPtlStart.x = vX;
5fd2b2c6 1260 vPtlStart.y = OS2Y(vY,vTextY);
7e99520b
DW
1261
1262 lHits = ::GpiCharStringAt( m_hPS
1263 ,&vPtlStart
1264 ,rsText.length()
1265 ,(PCH)rsText.c_str()
1266 );
1267 if (lHits != GPI_OK)
1268 {
1269 wxLogLastError(wxT("TextOut"));
1270 }
1271
1272 //
1273 // Restore the old parameters (text foreground colour may be left because
1274 // it never is set to anything else, but background should remain
1275 // transparent even if we just drew an opaque string)
1276 //
1277 if (m_textBackgroundColour.Ok())
1278 SetTextBkColor( m_hPS
1279 ,nOldBackground
1280 );
1281 SetBkMode( m_hPS
1282 ,wxTRANSPARENT
1283 );
1284}
1285
1286void wxDC::DoDrawRotatedText(
1287 const wxString& rsText
1288, wxCoord vX
1289, wxCoord vY
1290, double dAngle
1291)
c8ce6bcc 1292{
7e99520b
DW
1293 if (dAngle == 0.0)
1294 {
1295 DoDrawText( rsText
1296 ,vX
1297 ,vY
1298 );
1299 }
1300
c8ce6bcc
DW
1301 // TODO:
1302 /*
1303 if ( angle == 0.0 )
1304 {
1305 DoDrawText(text, x, y);
1306 }
1307 else
1308 {
1309 LOGFONT lf;
1310 wxFillLogFont(&lf, &m_font);
1311
1312 // GDI wants the angle in tenth of degree
1313 long angle10 = (long)(angle * 10);
1314 lf.lfEscapement = angle10;
1315 lf. lfOrientation = angle10;
1316
1317 HFONT hfont = ::CreateFontIndirect(&lf);
1318 if ( !hfont )
1319 {
1320 wxLogLastError("CreateFont");
1321 }
1322 else
1323 {
1324 HFONT hfontOld = ::SelectObject(GetHdc(), hfont);
1325
1326 DrawAnyText(text, x, y);
1327
1328 (void)::SelectObject(GetHdc(), hfontOld);
1329 }
1330
1331 // call the bounding box by adding all four vertices of the rectangle
1332 // containing the text to it (simpler and probably not slower than
1333 // determining which of them is really topmost/leftmost/...)
1334 wxCoord w, h;
1335 GetTextExtent(text, &w, &h);
1336
1337 double rad = DegToRad(angle);
1338
1339 // "upper left" and "upper right"
1340 CalcBoundingBox(x, y);
1341 CalcBoundingBox(x + w*cos(rad), y - h*sin(rad));
1342 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
1343
1344 // "bottom left" and "bottom right"
1345 x += (wxCoord)(h*sin(rad));
1346 y += (wxCoord)(h*cos(rad));
1347 CalcBoundingBox(x, y);
1348 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
1349 }
1350*/
1351}
1352
fb46a9a6
DW
1353// ---------------------------------------------------------------------------
1354// set GDI objects
1355// ---------------------------------------------------------------------------
1408104d 1356
5fd2b2c6
DW
1357void wxDC::SetPalette(
1358 const wxPalette& rPalette
1359)
1408104d 1360{
5fd2b2c6
DW
1361 if (m_hOldFont)
1362 {
1363 m_hOldFont = 0;
1364 }
1365 m_palette = rPalette;
1366 if (!rPalette.Ok())
1367 {
1368 if (m_hOldFont)
1369 {
1370 m_hOldFont = 0;
1371 }
1372 }
1373 HPALETTE hOldPal = ::GpiSelectPalette((HDC) m_hPS, (HPALETTE) m_palette.GetHPALETTE());
1374 if (!m_hOldPalette)
1375 m_hOldPalette = (WXHPALETTE)hOldPal;
1376} // end of wxDC::SetPalette
1408104d 1377
f6bcfd97
BP
1378void wxDC::SetFont(
1379 const wxFont& rFont
1380)
1408104d 1381{
f6bcfd97
BP
1382 //
1383 // Set the old object temporarily, in case the assignment deletes an object
1384 // that's not yet selected out.
1385 //
1386 if (m_hOldFont)
1387 {
f6bcfd97
BP
1388 m_hOldFont = 0;
1389 }
f6bcfd97 1390 m_font = rFont;
f6bcfd97
BP
1391 if (!rFont.Ok())
1392 {
f6bcfd97
BP
1393 m_hOldFont = 0;
1394 }
1395
e99762c0
DW
1396 m_font.SetPS(m_hPS); // this will realize the font
1397
1398 if (m_font.Ok())
f6bcfd97 1399 {
e99762c0 1400 HFONT hFont = m_font.GetResourceHandle();
f6bcfd97
BP
1401 if (hFont == (HFONT) NULL)
1402 {
1403 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
1404 }
1405 if (!m_hOldFont)
1406 m_hOldFont = (WXHFONT) hFont;
1407 }
e99762c0 1408} // end of wxDC::SetFont
1408104d 1409
7e99520b
DW
1410void wxDC::SetPen(
1411 const wxPen& rPen
1412)
1408104d 1413{
7e99520b
DW
1414 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1415
1416 if (m_pen == rPen)
1417 return;
1418 m_pen = rPen;
1419 if (!m_pen.Ok())
1420 return;
1421
26ac77db
DW
1422 if (m_hOldPen)
1423 m_hOldPen = 0L;
1424 m_pen = rPen;
7e99520b 1425
26ac77db 1426 if (!m_pen.Ok())
7e99520b 1427 {
26ac77db
DW
1428 if (m_hOldPen)
1429 {
1430 m_pen.SetPS((HPS)m_hOldPen);
1431 }
1432 m_hOldPen = 0L;
7e99520b 1433 }
51c1d535 1434
26ac77db 1435 if (m_pen.Ok())
51c1d535 1436 {
26ac77db
DW
1437 if (m_pen.GetResourceHandle())
1438 {
1439 m_pen.SetPS(m_hPS);
1440 if (!m_hOldPen)
1441 m_hOldPen = m_pen.GetPS();
1442 }
51c1d535 1443 }
1408104d 1444}
7e99520b 1445
51c1d535
DW
1446void wxDC::SetBrush(
1447 const wxBrush& rBrush
1448)
1408104d 1449{
15f03b25
DW
1450 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1451
15f03b25
DW
1452 if (m_hOldBrush)
1453 m_hOldBrush = 0L;
1454 m_brush = rBrush;
5fd2b2c6
DW
1455 if (!m_brush.Ok())
1456 if (m_brush == rBrush)
1457 return;
1458 if (!m_brush.Ok())
1459 if (m_hOldBrush)
1460 m_hOldBrush = 0L;
15f03b25
DW
1461
1462 if (!m_brush.Ok())
1463 {
1464 if (m_hOldBrush)
1465 {
1466 m_brush.SetPS((HPS)m_hOldBrush);
1467 }
1468 m_hOldBrush = 0L;
1469 }
1470
1471 if (m_brush.Ok())
1472 {
1473 if (m_brush.GetResourceHandle())
1474 {
1475 m_brush.SetPS(m_hPS);
1476 if (!m_hOldBrush)
5fd2b2c6 1477 m_hOldBrush = (WXHWND)m_brush.GetPS();
15f03b25
DW
1478 }
1479 }
1480} // end of wxDC::SetBrush
1408104d 1481
5fd2b2c6
DW
1482void wxDC::SetBackground(
1483 const wxBrush& rBrush
1484)
1408104d 1485{
5fd2b2c6
DW
1486 m_backgroundBrush = rBrush;
1487 if (!m_backgroundBrush.Ok())
1488 return;
1489 if (m_pCanvas)
1490 {
1491 bool bCustomColours = TRUE;
1492
1493 //
1494 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1495 // change background colours from the control-panel specified colours.
1496 //
1497 if (m_pCanvas->IsKindOf(CLASSINFO(wxWindow)) &&
1498 ((m_pCanvas->GetWindowStyleFlag() & wxUSER_COLOURS) != wxUSER_COLOURS))
1499 bCustomColours = FALSE;
1500 if (bCustomColours)
1501 {
1502 if (m_backgroundBrush.GetStyle()==wxTRANSPARENT)
1503 {
1504 m_pCanvas->SetTransparent(TRUE);
1505 }
1506 else
1507 {
1508 //
1509 // Setting the background brush of a DC
1510 // doesn't affect the window background colour. However,
1511 // I'm leaving in the transparency setting because it's needed by
1512 // various controls (e.g. wxStaticText) to determine whether to draw
1513 // transparently or not. TODO: maybe this should be a new function
1514 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1515 // parent?
1516 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1517 //
1518 m_pCanvas->SetTransparent(FALSE);
1519 }
1520 }
1521 }
1522 COLORREF vNewColor = m_backgroundBrush.GetColour().GetPixel();
1523 (void)::GpiSetBackColor((HPS)m_hPS, (LONG)vNewColor);
1524} // end of wxDC::SetBackground
1408104d 1525
7e99520b
DW
1526void wxDC::SetBackgroundMode(
1527 int nMode
1528)
1408104d 1529{
7e99520b 1530 m_backgroundMode = nMode;
5fd2b2c6 1531} // end of wxDC::SetBackgroundMode
1408104d 1532
5fd2b2c6
DW
1533void wxDC::SetLogicalFunction(
1534 int nFunction
1535)
1408104d 1536{
5fd2b2c6
DW
1537 m_logicalFunction = nFunction;
1538 SetRop((WXHDC)m_hDC);
1539} // wxDC::SetLogicalFunction
1408104d 1540
5fd2b2c6
DW
1541void wxDC::SetRop(
1542 WXHDC hDC
1543)
ce44c50e 1544{
5fd2b2c6 1545 if (!hDC || m_logicalFunction < 0)
ce44c50e
DW
1546 return;
1547
5fd2b2c6 1548 LONG lCRop;
ce44c50e
DW
1549 switch (m_logicalFunction)
1550 {
5fd2b2c6
DW
1551 case wxXOR:
1552 lCRop = FM_XOR;
1553 break;
1554
1555 case wxINVERT:
1556 lCRop = FM_INVERT;
1557 break;
1558
1559 case wxOR_REVERSE:
1560 lCRop = FM_MERGESRCNOT;
1561 break;
1562
1563 case wxAND_REVERSE:
1564 lCRop = FM_NOTMASKSRC;
1565 break;
1566
1567 case wxCLEAR:
1568 lCRop = FM_ONE;
1569 break;
1570
1571 case wxSET:
1572 lCRop = FM_ZERO;
1573 break;
1574
1575 case wxSRC_INVERT:
1576 lCRop = FM_MERGENOTSRC;
1577 break;
1578
1579 case wxOR_INVERT:
1580 lCRop = FM_MERGESRCNOT;
1581 break;
1582
1583 case wxAND:
1584 lCRop = FM_AND;
1585 break;
1586
1587 case wxOR:
1588 lCRop = FM_OR;
1589 break;
1590
1591 case wxAND_INVERT:
1592 lCRop = FM_SUBTRACT;
1593 break;
1594
1595 case wxEQUIV:
1596 case wxNAND:
1597 case wxCOPY:
1598 default:
1599 lCRop = FM_OVERPAINT;
1600 break;
ce44c50e 1601 }
5fd2b2c6
DW
1602 ::GpiSetMix((HPS)hDC, lCRop);
1603} // end of wxDC::SetRop
ce44c50e 1604
5fd2b2c6
DW
1605bool wxDC::StartDoc(
1606 const wxString& rsMessage
1607)
ce44c50e 1608{
fb46a9a6
DW
1609 // We might be previewing, so return TRUE to let it continue.
1610 return TRUE;
5fd2b2c6 1611} // end of wxDC::StartDoc
fb46a9a6
DW
1612
1613void wxDC::EndDoc()
1614{
5fd2b2c6 1615} // end of wxDC::EndDoc
fb46a9a6
DW
1616
1617void wxDC::StartPage()
1618{
5fd2b2c6 1619} // end of wxDC::StartPage
fb46a9a6
DW
1620
1621void wxDC::EndPage()
1622{
5fd2b2c6 1623} // end of wxDC::EndPage
fb46a9a6
DW
1624
1625// ---------------------------------------------------------------------------
1626// text metrics
1627// ---------------------------------------------------------------------------
1628
7cdc2f1e 1629wxCoord wxDC::GetCharHeight() const
fb46a9a6 1630{
05a8bfed
DW
1631 FONTMETRICS vFM; // metrics structure
1632
1633 ::GpiQueryFontMetrics( m_hPS
1634 ,sizeof(FONTMETRICS)
1635 ,&vFM
1636 );
1637 return YDEV2LOGREL(vFM.lXHeight);
fb46a9a6
DW
1638}
1639
7cdc2f1e 1640wxCoord wxDC::GetCharWidth() const
fb46a9a6 1641{
05a8bfed
DW
1642 FONTMETRICS vFM; // metrics structure
1643
1644 ::GpiQueryFontMetrics( m_hPS
1645 ,sizeof(FONTMETRICS)
1646 ,&vFM
1647 );
1648 return XDEV2LOGREL(vFM.lAveCharWidth);
7e99520b
DW
1649}
1650
1651void wxDC::DoGetTextExtent(
1652 const wxString& rsString
1653, wxCoord* pvX
1654, wxCoord* pvY
f44fdfb0 1655, wxCoord* pvDescent
7e99520b
DW
1656, wxCoord* pvExternalLeading
1657, wxFont* pTheFont
1658) const
1659{
1660 POINTL avPoint[TXTBOX_COUNT];
1661 POINTL vPtMin;
1662 POINTL vPtMax;
1663 int i;
1664 int l;
1665 FONTMETRICS vFM; // metrics structure
1666 BOOL bRc;
1667 char* pStr;
1668 ERRORID vErrorCode; // last error id code
1669 wxFont* pFontToUse = (wxFont*)pTheFont;
1670
2c4a8d17
DW
1671 char zMsg[128]; // DEBUG
1672 wxString sError;
1673
7e99520b
DW
1674 if (!pFontToUse)
1675 pFontToUse = (wxFont*)&m_font;
1676 l = rsString.length();
1677 pStr = (PCH) rsString.c_str();
fb46a9a6 1678
7e99520b
DW
1679 //
1680 // In world coordinates.
1681 //
1682 bRc = ::GpiQueryTextBox( m_hPS
1683 ,l
1684 ,pStr
1685 ,TXTBOX_COUNT // return maximum information
1686 ,avPoint // array of coordinates points
f44fdfb0 1687 );
7e99520b
DW
1688 if(!bRc)
1689 {
1690 vErrorCode = ::WinGetLastError(wxGetInstance());
2c4a8d17
DW
1691 sError = wxPMErrorToStr(vErrorCode);
1692 // DEBUG
1693 sprintf(zMsg, "GpiQueryTextBox for %s: failed with Error: %x - %s", pStr, vErrorCode, sError.c_str());
1694 (void)wxMessageBox( "wxWindows Menu sample"
1695 ,zMsg
1696 ,wxICON_INFORMATION
1697 );
7e99520b
DW
1698 }
1699
1700 vPtMin.x = avPoint[0].x;
1701 vPtMax.x = avPoint[0].x;
1702 vPtMin.y = avPoint[0].y;
1703 vPtMax.y = avPoint[0].y;
1704 for (i = 1; i < 4; i++)
1705 {
1706 if(vPtMin.x > avPoint[i].x) vPtMin.x = avPoint[i].x;
1707 if(vPtMin.y > avPoint[i].y) vPtMin.y = avPoint[i].y;
1708 if(vPtMax.x < avPoint[i].x) vPtMax.x = avPoint[i].x;
1709 if(vPtMax.y < avPoint[i].y) vPtMax.y = avPoint[i].y;
1710 }
1711 ::GpiQueryFontMetrics( m_hPS
1712 ,sizeof(FONTMETRICS)
1713 ,&vFM
1714 );
1715
1716 if (pvX)
1717 *pvX = (wxCoord)(vPtMax.x - vPtMin.x + 1);
1718 if (pvY)
1719 *pvY = (wxCoord)(vPtMax.y - vPtMin.y + 1);
1720 if (pvDescent)
1721 *pvDescent = vFM.lMaxDescender;
f44fdfb0 1722 if (pvExternalLeading)
7e99520b 1723 *pvExternalLeading = vFM.lExternalLeading;
fb46a9a6
DW
1724}
1725
5fd2b2c6
DW
1726void wxDC::SetMapMode(
1727 int nMode
1728)
fb46a9a6 1729{
5fd2b2c6
DW
1730 int nPixelWidth = 0;
1731 int nPixelHeight = 0;
1732 int nMmWidth = 1;
1733 int nMmHeight = 1;
1734 LONG lArray[CAPS_VERTICAL_RESOLUTION];
fb46a9a6 1735
5fd2b2c6
DW
1736 m_mappingMode = nMode;
1737
1738 if(::DevQueryCaps( m_hDC
1739 ,CAPS_FAMILY
1740 ,CAPS_VERTICAL_RESOLUTION
1741 ,lArray
1742 ))
1743 {
1744 LONG lHorzRes;
1745 LONG lVertRes;
1746
1747 nPixelWidth = lArray[CAPS_WIDTH];
1748 nPixelHeight = lArray[CAPS_HEIGHT];
1749 lHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
1750 lVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
1751 nMmWidth = (lHorzRes/1000) * nPixelWidth;
1752 nMmWidth = (lVertRes/1000) * nPixelHeight;
1753 }
1754 if ((nPixelWidth == 0) || (nPixelHeight == 0) || (nMmWidth == 0) || (nMmHeight == 0))
1755 {
1756 return;
1757 }
1758
1759 double dMm2pixelsX = nPixelWidth/nMmWidth;
1760 double dMm2pixelsY = nPixelHeight/nMmHeight;
1761
1762 switch (nMode)
1763 {
1764 case wxMM_TWIPS:
1765 m_logicalScaleX = (twips2mm * dMm2pixelsX);
1766 m_logicalScaleY = (twips2mm * dMm2pixelsY);
1767 break;
1768
1769 case wxMM_POINTS:
1770 m_logicalScaleX = (pt2mm * dMm2pixelsX);
1771 m_logicalScaleY = (pt2mm * dMm2pixelsY);
1772 break;
1773
1774 case wxMM_METRIC:
1775 m_logicalScaleX = dMm2pixelsX;
1776 m_logicalScaleY = dMm2pixelsY;
1777 break;
1778
1779 case wxMM_LOMETRIC:
1780 m_logicalScaleX = (dMm2pixelsX/10.0);
1781 m_logicalScaleY = (dMm2pixelsY/10.0);
1782 break;
1783
1784 case wxMM_TEXT:
1785 default:
1786 m_logicalScaleX = 1.0;
1787 m_logicalScaleY = 1.0;
1788 break;
1789 }
1790 SIZEL vSize;
1791 ULONG ulOptions;
1792
1793 ulOptions = ::GpiQueryPS(m_hPS, &vSize);
1794 if (!ulOptions & PU_ARBITRARY)
1795 {
1796 ulOptions = PU_ARBITRARY | GPIF_DEFAULT;
1797 ::GpiSetPS(m_hPS, &vSize, ulOptions);
1798 }
1799 m_nWindowExtX = (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT);
1800 m_nWindowExtY = (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT);
1801 // ????
1802}; // end of wxDC::SetMapMode
1803
1804void wxDC::SetUserScale(
1805 double dX
1806, double dY
1807)
fb46a9a6 1808{
5fd2b2c6
DW
1809 m_userScaleX = dX;
1810 m_userScaleY = dY;
fb46a9a6
DW
1811
1812 SetMapMode(m_mappingMode);
5fd2b2c6 1813} // end of wxDC::SetUserScale
fb46a9a6 1814
5fd2b2c6
DW
1815void wxDC::SetAxisOrientation(
1816 bool bXLeftRight
1817, bool bYBottomUp
1818)
fb46a9a6 1819{
5fd2b2c6
DW
1820 m_signX = bXLeftRight ? 1 : -1;
1821 m_signY = bYBottomUp ? -1 : 1;
fb46a9a6
DW
1822
1823 SetMapMode(m_mappingMode);
5fd2b2c6 1824} // end of wxDC::SetAxisOrientation
fb46a9a6 1825
5fd2b2c6
DW
1826void wxDC::SetSystemScale(
1827 double dX
1828, double dY
1829)
fb46a9a6 1830{
5fd2b2c6
DW
1831 m_scaleX = dX;
1832 m_scaleY = dY;
fb46a9a6
DW
1833
1834 SetMapMode(m_mappingMode);
5fd2b2c6 1835} // end of wxDC::SetSystemScale
fb46a9a6 1836
5fd2b2c6
DW
1837void wxDC::SetLogicalOrigin(
1838 wxCoord vX
1839, wxCoord vY
1840)
fb46a9a6 1841{
5fd2b2c6
DW
1842 RECTL vRect;
1843
1844 ::GpiQueryPageViewport( m_hPS
1845 ,&vRect
1846 );
1847 vRect.xRight -= vX;
1848 vRect.yTop += vY;
1849 vRect.xLeft = vX;
1850 vRect.yBottom = vY;
1851 ::GpiSetPageViewport( m_hPS
1852 ,&vRect
1853 );
1854}; // end of wxDC::SetLogicalOrigin
fb46a9a6 1855
8d854fa9 1856void wxDC::SetDeviceOrigin(
5fd2b2c6
DW
1857 wxCoord vX
1858, wxCoord vY
8d854fa9 1859)
fb46a9a6 1860{
26ac77db
DW
1861 RECTL vRect;
1862
5fd2b2c6
DW
1863 m_deviceOriginX = vX;
1864 m_deviceOriginY = vY;
26ac77db
DW
1865 ::GpiQueryPageViewport( m_hPS
1866 ,&vRect
1867 );
5fd2b2c6
DW
1868 vRect.xLeft += vX;
1869 vRect.xRight += vX;
1870 vRect.yBottom -= vY;
1871 vRect.yTop -= vY;
26ac77db
DW
1872 ::GpiSetPageViewport( m_hPS
1873 ,&vRect
1874 );
5fd2b2c6 1875}; // end of wxDC::SetDeviceOrigin
fb46a9a6
DW
1876
1877// ---------------------------------------------------------------------------
1878// coordinates transformations
1879// ---------------------------------------------------------------------------
1880
7cdc2f1e 1881wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
fb46a9a6 1882{
f6bcfd97
BP
1883 return (wxCoord) (((x) - m_deviceOriginX)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX) - m_logicalOriginX);
1884}
fb46a9a6 1885
7cdc2f1e 1886wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
fb46a9a6 1887{
f6bcfd97
BP
1888 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX));
1889}
fb46a9a6 1890
7cdc2f1e 1891wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
fb46a9a6 1892{
f6bcfd97
BP
1893 return (wxCoord) (((y) - m_deviceOriginY)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY) - m_logicalOriginY);
1894}
fb46a9a6 1895
7cdc2f1e 1896wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
fb46a9a6 1897{
f6bcfd97
BP
1898 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY));
1899}
fb46a9a6 1900
7cdc2f1e 1901wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
fb46a9a6 1902{
f6bcfd97
BP
1903 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
1904}
fb46a9a6 1905
7cdc2f1e 1906wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
fb46a9a6 1907{
f6bcfd97
BP
1908 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX);
1909}
fb46a9a6 1910
7cdc2f1e 1911wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
fb46a9a6 1912{
f6bcfd97
BP
1913 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
1914}
fb46a9a6 1915
7cdc2f1e 1916wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
fb46a9a6 1917{
f6bcfd97
BP
1918 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY);
1919}
fb46a9a6
DW
1920
1921// ---------------------------------------------------------------------------
1922// bit blit
1923// ---------------------------------------------------------------------------
1924
5afb9458
DW
1925bool wxDC::DoBlit(
1926 wxCoord vXdest
1927, wxCoord vYdest
1928, wxCoord vWidth
1929, wxCoord vHeight
1930, wxDC* pSource
1931, wxCoord vXsrc
1932, wxCoord vYsrc
1933, int nRop
1934, bool bUseMask
893758d5
DW
1935, wxCoord vXsrcMask
1936, wxCoord vYsrcMask
5afb9458 1937)
fb46a9a6 1938{
5afb9458
DW
1939 wxMask* pMask = NULL;
1940 CHARBUNDLE vCbnd;
1941 COLORREF vOldTextColor;
1942 COLORREF vOldBackground = ::GpiQueryBackColor(m_hPS);
5afb9458
DW
1943
1944 if (bUseMask)
1945 {
1946 const wxBitmap& rBmp = pSource->m_vSelectedBitmap;
1947
1948 pMask = rBmp.GetMask();
1949 if (!(rBmp.Ok() && pMask && pMask->GetMaskBitmap()))
1950 {
1951 bUseMask = FALSE;
1952 }
1953 }
1954
1955 ::GpiQueryAttrs( m_hPS
1956 ,PRIM_CHAR
1957 ,CBB_COLOR
1958 ,&vCbnd
1959 );
1960 vOldTextColor = (COLORREF)vCbnd.lColor;
1961
1962 if (m_textForegroundColour.Ok())
1963 {
1964 vCbnd.lColor = (LONG)m_textForegroundColour.GetPixel();
1965 ::GpiSetAttrs( m_hPS // presentation-space handle
1966 ,PRIM_CHAR // Char primitive.
1967 ,CBB_COLOR // sets color.
1968 ,0
1969 ,&vCbnd // buffer for attributes.
1970 );
1971 }
1972 if (m_textBackgroundColour.Ok())
1973 {
1974 ::GpiSetBackColor(m_hPS, (LONG)m_textBackgroundColour.GetPixel());
1975 }
1976
1977 LONG lRop = ROP_SRCCOPY;
1978
1979 switch (nRop)
1980 {
1981 case wxXOR: lRop = ROP_SRCINVERT; break;
1982 case wxINVERT: lRop = ROP_DSTINVERT; break;
1983 case wxOR_REVERSE: lRop = 0x00DD0228; break;
1984 case wxAND_REVERSE: lRop = ROP_SRCERASE; break;
1985 case wxCLEAR: lRop = ROP_ZERO; break;
1986 case wxSET: lRop = ROP_ONE; break;
1987 case wxOR_INVERT: lRop = ROP_MERGEPAINT; break;
1988 case wxAND: lRop = ROP_SRCAND; break;
1989 case wxOR: lRop = ROP_SRCPAINT; break;
1990 case wxEQUIV: lRop = 0x00990066; break;
1991 case wxNAND: lRop = 0x007700E6; break;
1992 case wxAND_INVERT: lRop = 0x00220326; break;
1993 case wxCOPY: lRop = ROP_SRCCOPY; break;
1994 case wxNO_OP: lRop = ROP_NOTSRCERASE; break;
1995 case wxSRC_INVERT: lRop = ROP_SRCINVERT; break;
1996 case wxNOR: lRop = ROP_NOTSRCCOPY; break;
1997 default:
1998 wxFAIL_MSG( wxT("unsupported logical function") );
1999 return FALSE;
2000 }
2001
2002 bool bSuccess;
b02121c3 2003
5afb9458
DW
2004 if (bUseMask)
2005 {
2006 //
2007 // Blit bitmap with mask
2008 //
2009
2010 //
b02121c3 2011 // Create a temp buffer bitmap and DCs/PSs to access it and the mask
5afb9458 2012 //
b02121c3
DW
2013 HDC hDCMask;
2014 HDC hDCBuffer;
2015 HPS hPSMask;
2016 HPS hPSBuffer;
2017 DEVOPENSTRUC vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
2018 BITMAPINFOHEADER2 vBmpHdr;
893758d5 2019 HBITMAP hBufBitmap;
b02121c3
DW
2020 SIZEL vSize = {0, 0};
2021 LONG rc;
2022
b02121c3
DW
2023 memset(&vBmpHdr, 0, sizeof(BITMAPINFOHEADER2));
2024 vBmpHdr.cbFix = sizeof(BITMAPINFOHEADER2);
2025 vBmpHdr.cx = vWidth;
2026 vBmpHdr.cy = vHeight;
2027 vBmpHdr.cPlanes = 1;
2028 vBmpHdr.cBitCount = 24;
2029
893758d5
DW
2030#if wxUSE_DC_CACHEING
2031 if (TRUE)
2032 {
2033 //
2034 // create a temp buffer bitmap and DCs to access it and the mask
2035 //
2036 wxDCCacheEntry* pDCCacheEntry1 = FindDCInCache( NULL
2037 ,pSource->GetHPS()
2038 );
2039 wxDCCacheEntry* pDCCacheEntry2 = FindDCInCache( pDCCacheEntry1
2040 ,GetHPS()
2041 );
2042 wxDCCacheEntry* pBitmapCacheEntry = FindBitmapInCache( GetHPS()
2043 ,vWidth
2044 ,vHeight
2045 );
2046
2047 hPSMask = pDCCacheEntry1->m_hPS;
2048 hDCBuffer = (HDC)pDCCacheEntry2->m_hPS;
2049 hBufBitmap = (HBITMAP)pBitmapCacheEntry->m_hBitmap;
2050 }
2051 else
2052#endif
2053 {
2054 hDCMask = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2055 hDCBuffer = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2056 hPSMask = ::GpiCreatePS(vHabmain, hDCMask, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2057 hPSBuffer = ::GpiCreatePS(vHabmain, hDCBuffer, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2058 hBufBitmap = ::GpiCreateBitmap(GetHPS(), &vBmpHdr, 0L, NULL, NULL);
2059 }
2060
b02121c3
DW
2061 POINTL aPoint1[4] = { 0, 0
2062 ,vWidth, vHeight
2063 ,vXdest, vYdest
2064 ,vXdest + vWidth, vYdest + vHeight
2065 };
2066 POINTL aPoint2[4] = { 0, 0
2067 ,vWidth, vHeight
2068 ,vXsrc, vYsrc
2069 ,vXsrc + vWidth, vYsrc + vHeight
2070 };
2071 POINTL aPoint3[4] = { vXdest, vYdest
23e356de
DW
2072 ,vXdest + vWidth, vYdest + vHeight
2073 ,vXsrc, vYsrc
2074 ,vXsrc + vWidth, vYsrc + vHeight
2075 };
2076 POINTL aPoint4[4] = { vXdest, vYdest
b02121c3
DW
2077 ,vXdest + vWidth, vYdest + vHeight
2078 ,0, 0
2079 ,vWidth, vHeight
2080 };
2081 ::GpiSetBitmap(hPSMask, (HBITMAP) pMask->GetMaskBitmap());
2082 ::GpiSetBitmap(hPSBuffer, (HBITMAP) hBufBitmap);
5afb9458 2083
b02121c3
DW
2084 //
2085 // Copy dest to buffer
2086 //
2087 rc = ::GpiBitBlt( hPSBuffer
2088 ,GetHPS()
2089 ,4L
2090 ,aPoint1
2091 ,ROP_SRCCOPY
2092 ,BBO_IGNORE
2093 );
2094 if (rc == GPI_ERROR)
2095 {
2096 wxLogLastError(wxT("BitBlt"));
2097 }
5afb9458 2098
b02121c3
DW
2099 //
2100 // Copy src to buffer using selected raster op
2101 //
2102 rc = ::GpiBitBlt( hPSBuffer
2103 ,GetHPS()
2104 ,4L
2105 ,aPoint2
2106 ,lRop
2107 ,BBO_IGNORE
2108 );
2109 if (rc == GPI_ERROR)
2110 {
2111 wxLogLastError(wxT("BitBlt"));
2112 }
5afb9458 2113
b02121c3
DW
2114 //
2115 // Set masked area in buffer to BLACK (pixel value 0)
2116 //
2117 COLORREF vPrevBkCol = ::GpiQueryBackColor(GetHPS());
2118 COLORREF vPrevCol = ::GpiQueryColor(GetHPS());
2119
2120 ::GpiSetBackColor(GetHPS(), OS2RGB(255, 255, 255));
2121 ::GpiSetColor(GetHPS(), OS2RGB(0, 0, 0));
2122
2123 rc = ::GpiBitBlt( hPSBuffer
2124 ,hPSMask
2125 ,4L
2126 ,aPoint2
2127 ,ROP_SRCAND
2128 ,BBO_IGNORE
2129 );
2130 if (rc == GPI_ERROR)
2131 {
2132 wxLogLastError(wxT("BitBlt"));
2133 }
5afb9458 2134
b02121c3
DW
2135 //
2136 // Set unmasked area in dest to BLACK
2137 //
2138 ::GpiSetBackColor(GetHPS(), OS2RGB(0, 0, 0));
2139 ::GpiSetColor(GetHPS(), OS2RGB(255, 255, 255));
2140 rc = ::GpiBitBlt( GetHPS()
2141 ,hPSMask
2142 ,4L
23e356de 2143 ,aPoint3
b02121c3
DW
2144 ,ROP_SRCAND
2145 ,BBO_IGNORE
2146 );
2147 if (rc == GPI_ERROR)
2148 {
2149 wxLogLastError(wxT("BitBlt"));
5afb9458 2150 }
b02121c3
DW
2151
2152 //
2153 // Restore colours to original values
2154 //
2155 ::GpiSetBackColor(GetHPS(), vPrevBkCol);
2156 ::GpiSetColor(GetHPS(), vPrevCol);
2157
2158 //
2159 // OR buffer to dest
2160 //
2161 rc = ::GpiBitBlt( GetHPS()
2162 ,hPSMask
2163 ,4L
23e356de 2164 ,aPoint4
b02121c3
DW
2165 ,ROP_SRCPAINT
2166 ,BBO_IGNORE
2167 );
2168 if (rc == GPI_ERROR)
2169 {
2170 bSuccess = FALSE;
2171 wxLogLastError(wxT("BitBlt"));
2172 }
2173
2174 //
2175 // Tidy up temporary DCs and bitmap
2176 //
2177 ::GpiSetBitmap(hPSMask, NULLHANDLE);
2178 ::GpiSetBitmap(hPSBuffer, NULLHANDLE);
893758d5 2179#if !wxUSE_DC_CACHEING
b02121c3
DW
2180 ::GpiDestroyPS(hPSMask);
2181 ::GpiDestroyPS(hPSBuffer);
2182 ::DevCloseDC(hDCMask);
2183 ::DevCloseDC(hDCBuffer);
2184 ::GpiDeleteBitmap(hBufBitmap);
893758d5 2185#endif
b02121c3 2186 bSuccess = TRUE;
5afb9458 2187 }
b02121c3
DW
2188 else // no mask, just BitBlt() it
2189 {
2190 POINTL aPoint[4] = { vXdest, vYdest
2191 ,vXdest + vWidth, vYdest + vHeight
2192 ,vXsrc, vYsrc
2193 ,vXsrc + vWidth, vYsrc + vHeight
2194 };
2195
5afb9458
DW
2196 bSuccess = (::GpiBitBlt( m_hPS
2197 ,pSource->GetHPS()
2198 ,4L
2199 ,aPoint
2200 ,lRop
2201 ,BBO_IGNORE
2202 ) != GPI_ERROR);
2203 if (!bSuccess )
2204 {
2205 wxLogLastError(wxT("BitBlt"));
2206 }
b02121c3 2207 }
5afb9458
DW
2208 vCbnd.lColor = (LONG)vOldTextColor;
2209 ::GpiSetAttrs( m_hPS // presentation-space handle
2210 ,PRIM_CHAR // Char primitive.
2211 ,CBB_COLOR // sets color.
2212 ,0
2213 ,&vCbnd // buffer for attributes.
2214 );
2215 ::GpiSetBackColor(m_hPS, (LONG)vOldBackground);
2216 return bSuccess;
fb46a9a6
DW
2217}
2218
5fd2b2c6
DW
2219void wxDC::DoGetSize(
2220 int* pnWidth
2221, int* pnHeight
2222) const
fb46a9a6 2223{
5fd2b2c6
DW
2224 LONG lArray[CAPS_HEIGHT];
2225
2226 if(::DevQueryCaps( m_hDC
2227 ,CAPS_FAMILY
2228 ,CAPS_HEIGHT
2229 ,lArray
2230 ))
2231 {
2232 *pnWidth = lArray[CAPS_WIDTH];
2233 *pnHeight = lArray[CAPS_HEIGHT];
2234 }
2235}; // end of wxDC::DoGetSize(
fb46a9a6 2236
5fd2b2c6
DW
2237void wxDC::DoGetSizeMM(
2238 int* pnWidth
2239, int* pnHeight
2240) const
fb46a9a6 2241{
5fd2b2c6
DW
2242 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2243
2244 if(::DevQueryCaps( m_hDC
2245 ,CAPS_FAMILY
2246 ,CAPS_VERTICAL_RESOLUTION
2247 ,lArray
2248 ))
2249 {
2250 int nWidth;
2251 int nHeight;
2252 int nHorzRes;
2253 int nVertRes;
2254
2255 nWidth = lArray[CAPS_WIDTH];
2256 nHeight = lArray[CAPS_HEIGHT];
2257 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2258 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2259 nWidth = (nHorzRes/1000) * nWidth;
2260 nHeight = (nVertRes/1000) * nHeight;
2261 }
2262}; // end of wxDC::DoGetSizeMM
fb46a9a6
DW
2263
2264wxSize wxDC::GetPPI() const
2265{
5fd2b2c6
DW
2266 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2267 int nWidth;
2268 int nHeight;
fb46a9a6 2269
5fd2b2c6
DW
2270 if(::DevQueryCaps( m_hDC
2271 ,CAPS_FAMILY
2272 ,CAPS_VERTICAL_RESOLUTION
2273 ,lArray
2274 ))
2275 {
2276 int nPelWidth;
2277 int nPelHeight;
2278 int nHorzRes;
2279 int nVertRes;
2280
2281 nPelWidth = lArray[CAPS_WIDTH];
2282 nPelHeight = lArray[CAPS_HEIGHT];
2283 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2284 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2285 nWidth = (nHorzRes/39.3) * nPelWidth;
2286 nHeight = (nVertRes/39.3) * nPelHeight;
2287 }
2288 return (wxSize(nWidth,nHeight));
2289} // end of wxDC::GetPPI
2290
2291void wxDC::SetLogicalScale(
2292 double dX
2293, double dY
2294)
fb46a9a6 2295{
5fd2b2c6
DW
2296 m_logicalScaleX = dX;
2297 m_logicalScaleY = dY;
2298}; // end of wxDC::SetLogicalScale
fb46a9a6
DW
2299
2300#if WXWIN_COMPATIBILITY
2301void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
2302 float *descent, float *externalLeading,
2303 wxFont *theFont, bool use16bit) const
2304{
7cdc2f1e 2305 wxCoord x1, y1, descent1, externalLeading1;
fb46a9a6
DW
2306 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
2307 *x = x1; *y = y1;
2308 if (descent)
2309 *descent = descent1;
2310 if (externalLeading)
2311 *externalLeading = externalLeading1;
2312}
2313#endif
2314