]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dc.cpp
Typo fix
[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{
81039e0d
DW
545 //
546 // If this is a canvas DC then just fill with the background color
547 // Otherwise purge the whole thing
548 //
549 if (m_pCanvas)
550 {
551 RECTL vRect;
552
553 ::GpiQueryClipBox(m_hPS, &vRect);
554 ::WinFillRect(m_hPS, &vRect, ::GpiQueryBackColor(m_hPS));
555 }
556 else
445c7bca 557 ::GpiErase(m_hPS);
5fd2b2c6 558} // end of wxDC::Clear
0e320a79 559
387ebd3e 560bool wxDC::DoFloodFill(
51c1d535
DW
561 wxCoord vX
562, wxCoord vY
563, const wxColour& rCol
564, int nStyle
565)
0e320a79 566{
51c1d535
DW
567 POINTL vPtlPos;
568 LONG lColor;
569 LONG lOptions;
570
571 vPtlPos.x = vX; // Loads x-coordinate
5fd2b2c6 572 vPtlPos.y = OS2Y(vY,0); // Loads y-coordinate
51c1d535
DW
573 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
574 lColor = rCol.GetPixel();
575 lOptions = FF_BOUNDARY;
576 if(wxFLOOD_SURFACE == nStyle)
577 lOptions = FF_SURFACE;
578
579 ::GpiFloodFill(m_hPS, lOptions, lColor);
387ebd3e
JS
580
581 return TRUE;
5fd2b2c6 582} // end of wxDC::DoFloodFill
0e320a79 583
51c1d535
DW
584bool wxDC::DoGetPixel(
585 wxCoord vX
586, wxCoord vY
587, wxColour* pCol
588) const
1408104d 589{
51c1d535
DW
590 POINTL vPoint;
591 LONG lColor;
592
593 vPoint.x = vX;
5fd2b2c6 594 vPoint.y = OS2Y(vY,0);
51c1d535 595 lColor = ::GpiSetPel(m_hPS, &vPoint);
5fd2b2c6
DW
596
597 //
598 // Get the color of the pen
599 //
600 LONG lPencolor = 0x00ffffff;
601
602 if (m_pen.Ok())
603 {
604 lPencolor = m_pen.GetColour().GetPixel();
605 }
606
607 //
608 // return the color of the pixel
609 //
610 if(pCol)
611 pCol->Set( GetRValue(lColor)
612 ,GetGValue(lColor)
613 ,GetBValue(lColor)
614 );
615 return(lColor == lPencolor);
616} // end of wxDC::DoGetPixel
0e320a79 617
f07bb01b
DW
618void wxDC::DoCrossHair(
619 wxCoord vX
620, wxCoord vY
621)
0e320a79 622{
5fd2b2c6
DW
623 vY = OS2Y(vY,0);
624
f07bb01b
DW
625 wxCoord vX1 = vX - VIEWPORT_EXTENT;
626 wxCoord vY1 = vY - VIEWPORT_EXTENT;
627 wxCoord vX2 = vX + VIEWPORT_EXTENT;
628 wxCoord vY2 = vY + VIEWPORT_EXTENT;
629 POINTL vPoint[4];
630
631 vPoint[0].x = vX1;
5fd2b2c6 632 vPoint[0].y = vY;
f07bb01b
DW
633
634 vPoint[1].x = vX2;
5fd2b2c6 635 vPoint[1].y = vY;
f07bb01b
DW
636
637 ::GpiMove(m_hPS, &vPoint[0]);
638 ::GpiLine(m_hPS, &vPoint[1]);
639
640 vPoint[2].x = vX;
5fd2b2c6 641 vPoint[2].y = vY1;
f07bb01b
DW
642
643 vPoint[3].x = vX;
5fd2b2c6 644 vPoint[3].y = vY2;
f07bb01b
DW
645
646 ::GpiMove(m_hPS, &vPoint[2]);
647 ::GpiLine(m_hPS, &vPoint[3]);
5fd2b2c6
DW
648 CalcBoundingBox(vX1, vY1);
649 CalcBoundingBox(vX2, vY2);
f07bb01b 650} // end of wxDC::DoCrossHair
1408104d 651
7e99520b
DW
652void wxDC::DoDrawLine(
653 wxCoord vX1
654, wxCoord vY1
655, wxCoord vX2
656, wxCoord vY2
657)
0e320a79 658{
7e99520b
DW
659 POINTL vPoint[2];
660
5fd2b2c6
DW
661 vY1 = OS2Y(vY1,0);
662 vY2 = OS2Y(vY2,0);
663
7e99520b 664 vPoint[0].x = vX1;
5fd2b2c6 665 vPoint[0].y = vY1;
7e99520b 666 vPoint[1].x = vX2;
5fd2b2c6 667 vPoint[1].y = vY2;
7e99520b
DW
668 ::GpiMove(m_hPS, &vPoint[0]);
669 ::GpiLine(m_hPS, &vPoint[1]);
5fd2b2c6
DW
670 CalcBoundingBox(vX1, vY1);
671 CalcBoundingBox(vX2, vY2);
f07bb01b 672} // end of wxDC::DoDrawLine
1408104d 673
51c1d535
DW
674//////////////////////////////////////////////////////////////////////////////
675// Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
676// and ending at (x2, y2). The current pen is used for the outline and the
677// current brush for filling the shape. The arc is drawn in an anticlockwise
678// direction from the start point to the end point.
679//////////////////////////////////////////////////////////////////////////////
680void wxDC::DoDrawArc(
681 wxCoord vX1
682, wxCoord vY1
683, wxCoord vX2
684, wxCoord vY2
685, wxCoord vXc
686, wxCoord vYc
687)
1408104d 688{
51c1d535
DW
689 POINTL vPtlPos;
690 POINTL vPtlArc[2]; // Structure for current position
691 int nDx;
692 int nDy;
693 double dRadius;
694 double dAngl1;
695 double dAngl2;
696 double dAnglmid;
697 wxCoord vXm;
698 wxCoord vYm;
699 ARCPARAMS vArcp; // Structure for arc parameters
700
701 if((vX1 == vXc && vY1 == vXc) || (vX2 == vXc && vY2 == vXc))
702 return; // Draw point ??
703 dRadius = 0.5 * ( hypot( (double)(vY1 - vYc)
704 ,(double)(vX1 - vXc)
705 ) +
706 hypot( (double)(vY2 - vYc)
707 ,(double)(vX2 - vXc)
708 )
709 );
710
711 dAngl1 = atan2( (double)(vY1 - vYc)
712 ,(double)(vX1 - vXc)
713 );
714 dAngl2 = atan2( (double)(vY2 - vYc)
715 ,(double)(vX2 - vXc)
716 );
717 if(dAngl2 < dAngl1)
718 dAngl2 += M_PI * 2;
719
720 //
721 // GpiPointArc can't draw full arc
722 //
723 if(dAngl2 == dAngl1 || (vX1 == vX2 && vY1 == vY2) )
724 {
725 //
726 // Medium point
727 //
728 dAnglmid = (dAngl1 + dAngl2)/2. + M_PI;
729 vXm = vXc + dRadius * cos(dAnglmid);
730 vYm = vYc + dRadius * sin(dAnglmid);
e99762c0
DW
731 DoDrawArc( vX1, vY1
732 ,vXm, vYm
733 ,vXc, vYc
51c1d535 734 );
e99762c0
DW
735 DoDrawArc( vXm, vYm
736 ,vX2, vY2
737 ,vXc, vYc
51c1d535
DW
738 );
739 return;
740 }
741
742 //
743 // Medium point
744 //
745 dAnglmid = (dAngl1 + dAngl2)/2.;
746 vXm = vXc + dRadius * cos(dAnglmid);
747 vYm = vYc + dRadius * sin(dAnglmid);
748
749 //
750 // Ellipse main axis (r,q), (p,s) with center at (0,0) */
751 //
752 vArcp.lR = 0;
753 vArcp.lQ = 1;
754 vArcp.lP = 1;
755 vArcp.lS = 0;
756 ::GpiSetArcParams(m_hPS, &vArcp); // Sets parameters to default
757
758 vPtlPos.x = vX1; // Loads x-coordinate
759 vPtlPos.y = vY1; // Loads y-coordinate
760 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
e99762c0
DW
761 vPtlArc[0].x = vXm;
762 vPtlArc[0].y = vYm;
51c1d535
DW
763 vPtlArc[1].x = vX2;
764 vPtlArc[1].y = vY2;
765 ::GpiPointArc(m_hPS, vPtlArc); // Draws the arc
5fd2b2c6
DW
766 CalcBoundingBox( (vXc - dRadius)
767 ,(vYc - dRadius)
768 );
769 CalcBoundingBox( (vXc + dRadius)
770 ,(vYc + dRadius)
771 );
f07bb01b 772} // end of wxDC::DoDrawArc
1408104d 773
51c1d535
DW
774void wxDC::DoDrawCheckMark(
775 wxCoord vX1
776, wxCoord vY1
777, wxCoord vWidth
778, wxCoord vHeight
779)
f6bcfd97 780{
51c1d535
DW
781 POINTL vPoint[2];
782
5fd2b2c6
DW
783 vY1 = OS2Y(vY1,vHeight);
784
51c1d535
DW
785 vPoint[0].x = vX1;
786 vPoint[0].y = vY1;
787 vPoint[1].x = vX1 + vWidth;
788 vPoint[1].y = vY1 + vHeight;
789
790 ::GpiMove(m_hPS, &vPoint[0]);
791 ::GpiBox( m_hPS // handle to a presentation space
792 ,DRO_OUTLINE // draw the box outline ? or ?
793 ,&vPoint[1] // address of the corner
794 ,0L // horizontal corner radius
795 ,0L // vertical corner radius
796 );
797 if(vWidth > 4 && vHeight > 4)
798 {
799 int nTmp;
800
801 vPoint[0].x += 2; vPoint[0].y += 2;
802 vPoint[1].x -= 2; vPoint[1].y -= 2;
803 ::GpiMove(m_hPS, &vPoint[0]);
804 ::GpiLine(m_hPS, &vPoint[1]);
805 nTmp = vPoint[0].x;
806 vPoint[0].x = vPoint[1].x;
807 vPoint[1].x = nTmp;
808 ::GpiMove(m_hPS, &vPoint[0]);
809 ::GpiLine(m_hPS, &vPoint[1]);
810 }
5fd2b2c6
DW
811 CalcBoundingBox( vX1
812 ,vY1
813 );
814
815 wxCoord vX2 = vX1 + vWidth;
816 wxCoord vY2 = vY1 + vHeight;
817
818 CalcBoundingBox( vX2
819 ,vY2
820 );
f07bb01b 821} // end of wxDC::DoDrawCheckMark
f6bcfd97 822
51c1d535
DW
823void wxDC::DoDrawPoint(
824 wxCoord vX
825, wxCoord vY
826)
1408104d 827{
51c1d535 828 POINTL vPoint;
5fd2b2c6 829 COLORREF vColor = 0x00ffffff;
51c1d535 830
5fd2b2c6
DW
831 if (m_pen.Ok())
832 {
833 vColor = m_pen.GetColour().GetPixel();
834 }
835 ::GpiSetColor(m_hPS, vColor);
51c1d535 836 vPoint.x = vX;
5fd2b2c6 837 vPoint.y = OS2Y(vY,0);
51c1d535 838 ::GpiSetPel(m_hPS, &vPoint);
5fd2b2c6
DW
839 CalcBoundingBox( vX
840 ,vY
841 );
f07bb01b 842} // end of wxDC::DoDrawPoint
1408104d 843
51c1d535
DW
844void wxDC::DoDrawPolygon(
845 int n
846, wxPoint vPoints[]
847, wxCoord vXoffset
848, wxCoord vYoffset
849, int nFillStyle
850)
1408104d 851{
51c1d535
DW
852 ULONG ulCount = 1; // Number of polygons.
853 POLYGON vPlgn; // polygon.
854 ULONG flOptions = 0L; // Drawing options.
855
856//////////////////////////////////////////////////////////////////////////////
857// This contains fields of option bits... to draw boundary lines as well as
858// the area interior.
859//
860// Drawing boundary lines:
861// POLYGON_NOBOUNDARY Does not draw boundary lines.
862// POLYGON_BOUNDARY Draws boundary lines (the default).
863//
864// Construction of the area interior:
865// POLYGON_ALTERNATE Constructs interior in alternate mode
866// (the default).
867// POLYGON_WINDING Constructs interior in winding mode.
868//////////////////////////////////////////////////////////////////////////////
869
870 ULONG flModel = 0L; // Drawing model.
871
872//////////////////////////////////////////////////////////////////////////////
873// Drawing model.
874// POLYGON_INCL Fill is inclusive of bottom right (the default).
875// POLYGON_EXCL Fill is exclusive of bottom right.
876// This is provided to aid migration from other graphics models.
877//////////////////////////////////////////////////////////////////////////////
878
879 LONG lHits = 0L; // Correlation/error indicator.
880 POINTL vPoint;
881 int i;
882 int nIsTRANSPARENT = 0;
883 LONG lBorderColor = 0L;
884 LONG lColor = 0L;
885
886 lBorderColor = m_pen.GetColour().GetPixel();
887 lColor = m_brush.GetColour().GetPixel();
888 if(m_brush.GetStyle() == wxTRANSPARENT)
889 nIsTRANSPARENT = 1;
890
891 vPlgn.ulPoints = n;
892 vPlgn.aPointl = (POINTL*) calloc( n + 1
893 ,sizeof(POINTL)
894 ); // well, new will call malloc
895
896 for(i = 0; i < n; i++)
897 {
5fd2b2c6
DW
898 vPlgn.aPointl[i].x = vPoints[i].x; // +xoffset;
899 vPlgn.aPointl[i].y = OS2Y(vPoints[i].y,0); // +yoffset;
51c1d535
DW
900 }
901 flModel = POLYGON_BOUNDARY;
902 if(nFillStyle == wxWINDING_RULE)
903 flModel |= POLYGON_WINDING;
904 else
905 flModel |= POLYGON_ALTERNATE;
906
907 vPoint.x = vXoffset;
5fd2b2c6 908 vPoint.y = OS2Y(vYoffset,0);
51c1d535
DW
909
910 ::GpiSetColor(m_hPS, lBorderColor);
911 ::GpiMove(m_hPS, &vPoint);
912 lHits = ::GpiPolygons(m_hPS, ulCount, &vPlgn, flOptions, flModel);
913 free(vPlgn.aPointl);
f07bb01b 914} // end of wxDC::DoDrawPolygon
1408104d 915
51c1d535
DW
916void wxDC::DoDrawLines(
917 int n
918, wxPoint vPoints[]
919, wxCoord vXoffset
920, wxCoord vYoffset
921)
1408104d 922{
51c1d535
DW
923 POINTL vPoint;
924
5fd2b2c6
DW
925 if (vXoffset != 0L || vXoffset != 0L)
926 {
927 int i;
928
929 vPoint.x = vPoints[0].x + vXoffset;
930 vPoint.y = OS2Y(vPoints[0].y + vYoffset,0);
931 ::GpiMove(m_hPS, &vPoint);
51c1d535 932
5fd2b2c6 933 LONG lBorderColor = m_pen.GetColour().GetPixel();
51c1d535 934
5fd2b2c6
DW
935 ::GpiSetColor(m_hPS, lBorderColor);
936 for(i = 1; i < n; i++)
937 {
938 vPoint.x = vPoints[i].x + vXoffset;
939 vPoint.y = OS2Y(vPoints[i].y + vYoffset,0);
940 ::GpiLine(m_hPS, &vPoint);
941 }
942 }
943 else
51c1d535 944 {
5fd2b2c6
DW
945 int i;
946
947 CalcBoundingBox( vPoints[i].x
948 ,vPoints[i].y
949 );
950 vPoint.x = vPoints[0].x;
951 vPoint.y = OS2Y(vPoints[0].y,0);
952 ::GpiMove(m_hPS, &vPoint);
953
954 for (i = 0; i < n; i++)
955 {
956 CalcBoundingBox( vPoints[i].x
957 ,vPoints[i].y
958 );
959 vPoint.x = vPoints[i].x;
960 vPoint.y = OS2Y(vPoints[i].y,0);
961 ::GpiLine(m_hPS, &vPoint);
962 }
51c1d535 963 }
f07bb01b 964} // end of wxDC::DoDrawLines
1408104d 965
7e99520b 966void wxDC::DoDrawRectangle(
f44fdfb0 967 wxCoord vX
7e99520b
DW
968, wxCoord vY
969, wxCoord vWidth
970, wxCoord vHeight
971)
1408104d 972{
7e99520b 973 POINTL vPoint[2];
51c1d535
DW
974 LONG lControl;
975 LONG lColor;
976 LONG lBorderColor;
977 int nIsTRANSPARENT = 0;
7e99520b 978
5fd2b2c6
DW
979 vY = OS2Y(vY,vHeight);
980
981 wxCoord vX2 = vX + vWidth;
982 wxCoord vY2 = vY + vHeight;
983
7e99520b 984 vPoint[0].x = vX;
5fd2b2c6 985 vPoint[0].y = vY;
f44fdfb0 986 vPoint[1].x = vX + vWidth;
5fd2b2c6 987 vPoint[1].y = vY + vHeight;
7e99520b 988 ::GpiMove(m_hPS, &vPoint[0]);
51c1d535
DW
989 lColor = m_brush.GetColour().GetPixel();
990 lBorderColor = m_pen.GetColour().GetPixel();
991 if (m_brush.GetStyle() == wxTRANSPARENT)
992 nIsTRANSPARENT = 1;
993 if(lColor == lBorderColor || nIsTRANSPARENT)
994 {
995 lControl = DRO_OUTLINEFILL; //DRO_FILL;
996 if(m_brush.GetStyle() == wxTRANSPARENT)
997 lControl = DRO_OUTLINE;
998
8d854fa9 999 ::GpiSetColor(m_hPS, lColor);
51c1d535
DW
1000 ::GpiBox( m_hPS // handle to a presentation space
1001 ,lControl // draw the box outline ? or ?
1002 ,&vPoint[1] // address of the corner
1003 ,0L // horizontal corner radius
1004 ,0L // vertical corner radius
1005 );
1006 }
1007 else
1008 {
1009 lControl = DRO_OUTLINE;
1010 ::GpiSetColor( m_hPS
1011 ,lBorderColor
1012 );
1013 ::GpiBox( m_hPS
1014 ,lControl
1015 ,&vPoint[1]
1016 ,0L
1017 ,0L
1018 );
1019 lControl = DRO_FILL;
1020 ::GpiSetColor( m_hPS
1021 ,lColor
1022 );
8d854fa9 1023 vPoint[0].x = vX + 1;
5fd2b2c6 1024 vPoint[0].y = vY + 1;
ad7f3189
DW
1025 vPoint[1].x = vX + vWidth - 1;
1026 vPoint[1].y = vY + vHeight - 1;
8d854fa9 1027 ::GpiMove(m_hPS, &vPoint[0]);
51c1d535
DW
1028 ::GpiBox( m_hPS
1029 ,lControl
1030 ,&vPoint[1]
1031 ,0L
1032 ,0L
1033 );
1034 }
5fd2b2c6
DW
1035 CalcBoundingBox(vX, vY);
1036 CalcBoundingBox(vX2, vY2);
f07bb01b 1037} // end of wxDC::DoDrawRectangle
1408104d 1038
7e99520b
DW
1039void wxDC::DoDrawRoundedRectangle(
1040 wxCoord vX
1041, wxCoord vY
1042, wxCoord vWidth
1043, wxCoord vHeight
1044, double dRadius
1045)
1408104d 1046{
7e99520b 1047 POINTL vPoint[2];
51c1d535 1048 LONG lControl;
7e99520b 1049
5fd2b2c6
DW
1050 vY = OS2Y(vY,vHeight);
1051
1052 wxCoord vX2 = (vX + vWidth);
1053 wxCoord vY2 = (vY + vHeight);
1054
7e99520b 1055 vPoint[0].x = vX;
8d854fa9 1056 vPoint[0].y = YLOG2DEV(vY) - vHeight;
7e99520b 1057 vPoint[1].x = vX + vWidth;
8d854fa9 1058 vPoint[1].y = vY;
7e99520b 1059 ::GpiMove(m_hPS, &vPoint[0]);
51c1d535
DW
1060
1061 lControl = DRO_OUTLINEFILL; //DRO_FILL;
1062 if (m_brush.GetStyle() == wxTRANSPARENT)
1063 lControl = DRO_OUTLINE;
f44fdfb0
DW
1064 ::GpiBox( m_hPS // handle to a presentation space
1065 ,DRO_OUTLINE // draw the box outline ? or ?
1066 ,&vPoint[1] // address of the corner
1067 ,(LONG)dRadius // horizontal corner radius
1068 ,(LONG)dRadius // vertical corner radius
7e99520b 1069 );
5fd2b2c6
DW
1070 CalcBoundingBox(vX, vY);
1071 CalcBoundingBox(vX2, vY2);
f07bb01b 1072} // end of wxDC::DoDrawRoundedRectangle
1408104d 1073
51c1d535
DW
1074// Draw Ellipse within box (x,y) - (x+width, y+height)
1075void wxDC::DoDrawEllipse(
1076 wxCoord vX
1077, wxCoord vY
1078, wxCoord vWidth
1079, wxCoord vHeight
1080)
1408104d 1081{
51c1d535
DW
1082 POINTL vPtlPos; // Structure for current position
1083 FIXED vFxMult; // Multiplier for ellipse
1084 ARCPARAMS vArcp; // Structure for arc parameters
1085
5fd2b2c6
DW
1086 vY = OS2Y(vY,vHeight);
1087
51c1d535
DW
1088 vArcp.lR = 0;
1089 vArcp.lQ = vHeight/2;
1090 vArcp.lP = vWidth/2;
1091 vArcp.lS = 0;
1092 ::GpiSetArcParams( m_hPS
1093 ,&vArcp
1094 ); // Sets parameters to default
1095 vPtlPos.x = vX + vWidth/2; // Loads x-coordinate
1096 vPtlPos.y = vY + vHeight/2; // Loads y-coordinate
1097 ::GpiMove( m_hPS
1098 ,&vPtlPos
1099 ); // Sets current position
1100 vFxMult = MAKEFIXED(1, 0); /* Sets multiplier */
1101
1102 //
1103 // DRO_FILL, DRO_OTLINEFILL - where to get
1104 //
1105 ::GpiFullArc( m_hPS
1106 ,DRO_OUTLINE
1107 ,vFxMult
1108 ); // Draws full arc with center at current position
5fd2b2c6
DW
1109
1110 wxCoord vX2 = (vX + vWidth);
1111 wxCoord vY2 = (vY + vHeight);
1112
1113 CalcBoundingBox(vX, vY);
1114 CalcBoundingBox(vX2, vY2);
f07bb01b 1115} // end of wxDC::DoDrawEllipse
1408104d 1116
51c1d535
DW
1117void wxDC::DoDrawEllipticArc(
1118 wxCoord vX
1119, wxCoord vY
1120, wxCoord vWidth
1121, wxCoord vHeight
1122, double dSa
1123, double dEa
1124)
1408104d 1125{
51c1d535
DW
1126 POINTL vPtlPos; // Structure for current position
1127 FIXED vFxMult; // Multiplier for ellipse
1128 ARCPARAMS vArcp; // Structure for arc parameters
1129 FIXED vFSa;
1130 FIXED vFSweepa; // Start angle, sweep angle
1131 double dIntPart;
1132 double dFractPart;
1133 double dRadius;
1134
5fd2b2c6
DW
1135 vY = OS2Y(vY,vHeight);
1136
51c1d535
DW
1137 dFractPart = modf(dSa,&dIntPart);
1138 vFSa = MAKEFIXED((int)dIntPart, (int)(dFractPart * 0xffff) );
1139 dFractPart = modf(dEa - dSa, &dIntPart);
1140 vFSweepa = MAKEFIXED((int)dIntPart, (int)(dFractPart * 0xffff) );
1141
1142 //
1143 // Ellipse main axis (r,q), (p,s) with center at (0,0)
1144 //
1145 vArcp.lR = 0;
1146 vArcp.lQ = vHeight/2;
1147 vArcp.lP = vWidth/2;
1148 vArcp.lS = 0;
1149 ::GpiSetArcParams(m_hPS, &vArcp); // Sets parameters to default
1150 vPtlPos.x = vX + vWidth/2 * (1. + cos(DegToRad(dSa))); // Loads x-coordinate
1151 vPtlPos.y = vY + vHeight/2 * (1. + sin(DegToRad(dSa))); // Loads y-coordinate
1152 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
1153
1154 //
1155 // May be not to the center ?
1156 //
1157 vPtlPos.x = vX + vWidth/2 ; // Loads x-coordinate
1158 vPtlPos.y = vY + vHeight/2; // Loads y-coordinate
1159 vFxMult = MAKEFIXED(1, 0); // Sets multiplier
1160
1161 //
1162 // DRO_FILL, DRO_OTLINEFILL - where to get
1163 //
1164 ::GpiPartialArc( m_hPS
1165 ,&vPtlPos
1166 ,vFxMult
1167 ,vFSa
1168 ,vFSweepa
1169 );
5fd2b2c6
DW
1170 wxCoord vX2 = (vX + vWidth);
1171 wxCoord vY2 = (vY + vHeight);
1172
1173 CalcBoundingBox(vX, vY);
1174 CalcBoundingBox(vX2, vY2);
f07bb01b 1175} // end of wxDC::DoDrawEllipticArc
1408104d 1176
f07bb01b
DW
1177void wxDC::DoDrawIcon(
1178 const wxIcon& rIcon
1179, wxCoord vX
1180, wxCoord vY
1181)
1408104d 1182{
5fd2b2c6 1183 vY = OS2Y(vY,rIcon.GetHeight());
f07bb01b
DW
1184 wxCHECK_RET( rIcon.Ok(), wxT("invalid icon in DrawIcon") );
1185
1186 ::WinDrawPointer( GetHPS()
1187 ,vX
1188 ,vY
1189 ,(HPOINTER)GetHiconOf(rIcon)
1190 ,DP_NORMAL
1191 );
5fd2b2c6
DW
1192 CalcBoundingBox(vX, vY);
1193 CalcBoundingBox(vX + rIcon.GetWidth(), vY + rIcon.GetHeight());
f07bb01b
DW
1194} // end of wxDC::DoDrawIcon
1195
1196void wxDC::DoDrawBitmap(
1197 const wxBitmap& rBmp
1198, wxCoord vX
1199, wxCoord vY
1200, bool bUseMask
1201)
1408104d 1202{
c354beea
DW
1203 if (!bUseMask && !IsKindOf(CLASSINFO(wxPrinterDC)))
1204 {
1205 HBITMAP hBitmap = (HBITMAP)rBmp.GetHBITMAP();
1206 wxBitmap vNewBitmap( rBmp.GetWidth()
1207 ,rBmp.GetHeight()
1208 ,rBmp.GetDepth()
1209 );
1210 HBITMAP hBitmapOld = ::GpiSetBitmap((HPS)GetHPS(), vNewBitmap.GetHBITMAP());
1211 LONG lOldTextground = ::GpiQueryColor((HPS)GetHPS());
1212 LONG lOldBackground = ::GpiQueryBackColor((HPS)GetHPS());
1213
1214 if (m_textForegroundColour.Ok())
1215 {
1216 ::GpiSetColor( (HPS)GetHPS()
1217 ,m_textForegroundColour.GetPixel()
1218 );
1219 }
1220 if (m_textBackgroundColour.Ok())
1221 {
1222 ::GpiSetBackColor( (HPS)GetHPS()
1223 ,m_textBackgroundColour.GetPixel()
1224 );
1225 }
1226
1227 vY = OS2Y(vY,rBmp.GetHeight());
1228
1229 POINTL vPoint[4] = { vX, vY + rBmp.GetHeight()
1230 ,vX + rBmp.GetWidth(), vY
1231 ,0, 0
1232 ,rBmp.GetWidth(), rBmp.GetHeight()
1233 };
1234 ::GpiWCBitBlt( (HPS)GetHPS()
1235 ,hBitmap
1236 ,4
1237 ,vPoint
1238 ,ROP_SRCCOPY
1239 ,BBO_IGNORE
1240 );
1241 ::GpiSetBitmap((HPS)GetHPS(), hBitmapOld);
1242 ::GpiSetColor((HPS)GetHPS(), lOldTextground);
1243 ::GpiSetBackColor((HPS)GetHPS(), lOldBackground);
1244 }
f07bb01b 1245} // end of wxDC::DoDrawBitmap
1408104d 1246
7e99520b
DW
1247void wxDC::DoDrawText(
1248 const wxString& rsText
1249, wxCoord vX
1250, wxCoord vY
1251)
1408104d 1252{
5fd2b2c6
DW
1253 wxCoord vWidth;
1254 wxCoord vHeight;
1255
7e99520b
DW
1256 DrawAnyText( rsText
1257 ,vX
1258 ,vY
1259 );
5fd2b2c6
DW
1260
1261 CalcBoundingBox(vX, vY);
1262 GetTextExtent(rsText, &vWidth, &vHeight);
1263 CalcBoundingBox((vX + vWidth), (vY + vHeight));
1264} // end of wxDC::DoDrawText
1408104d 1265
7e99520b
DW
1266void wxDC::DrawAnyText(
1267 const wxString& rsText
1268, wxCoord vX
1269, wxCoord vY
1270)
f6bcfd97 1271{
7e99520b
DW
1272 int nOldBackground = 0;
1273 POINTL vPtlStart;
1274 LONG lHits;
5fd2b2c6
DW
1275 wxCoord vTextX = 0;
1276 wxCoord vTextY = 0;
f6bcfd97 1277
7e99520b
DW
1278 //
1279 // prepare for drawing the text
1280 //
1281
1282 //
1283 // Set text color attributes
1284 //
1285 if (m_textForegroundColour.Ok())
1286 {
1287 SetTextColor( m_hPS
1288 ,(int)m_textForegroundColour.GetPixel()
1289 );
1290 }
1291
1292 if (m_textBackgroundColour.Ok())
1293 {
1294 nOldBackground = SetTextBkColor( m_hPS
1295 ,(int)m_textBackgroundColour.GetPixel()
1296 );
1297 }
1298 SetBkMode( m_hPS
1299 ,m_backgroundMode
1300 );
5fd2b2c6
DW
1301 GetTextExtent( rsText
1302 ,&vTextX
1303 ,&vTextY
1304 );
7e99520b 1305 vPtlStart.x = vX;
5fd2b2c6 1306 vPtlStart.y = OS2Y(vY,vTextY);
7e99520b
DW
1307
1308 lHits = ::GpiCharStringAt( m_hPS
1309 ,&vPtlStart
1310 ,rsText.length()
1311 ,(PCH)rsText.c_str()
1312 );
1313 if (lHits != GPI_OK)
1314 {
1315 wxLogLastError(wxT("TextOut"));
1316 }
1317
1318 //
1319 // Restore the old parameters (text foreground colour may be left because
1320 // it never is set to anything else, but background should remain
1321 // transparent even if we just drew an opaque string)
1322 //
1323 if (m_textBackgroundColour.Ok())
1324 SetTextBkColor( m_hPS
1325 ,nOldBackground
1326 );
1327 SetBkMode( m_hPS
1328 ,wxTRANSPARENT
1329 );
1330}
1331
1332void wxDC::DoDrawRotatedText(
1333 const wxString& rsText
1334, wxCoord vX
1335, wxCoord vY
1336, double dAngle
1337)
c8ce6bcc 1338{
7e99520b
DW
1339 if (dAngle == 0.0)
1340 {
1341 DoDrawText( rsText
1342 ,vX
1343 ,vY
1344 );
1345 }
1346
c8ce6bcc
DW
1347 // TODO:
1348 /*
1349 if ( angle == 0.0 )
1350 {
1351 DoDrawText(text, x, y);
1352 }
1353 else
1354 {
1355 LOGFONT lf;
1356 wxFillLogFont(&lf, &m_font);
1357
1358 // GDI wants the angle in tenth of degree
1359 long angle10 = (long)(angle * 10);
1360 lf.lfEscapement = angle10;
1361 lf. lfOrientation = angle10;
1362
1363 HFONT hfont = ::CreateFontIndirect(&lf);
1364 if ( !hfont )
1365 {
1366 wxLogLastError("CreateFont");
1367 }
1368 else
1369 {
1370 HFONT hfontOld = ::SelectObject(GetHdc(), hfont);
1371
1372 DrawAnyText(text, x, y);
1373
1374 (void)::SelectObject(GetHdc(), hfontOld);
1375 }
1376
1377 // call the bounding box by adding all four vertices of the rectangle
1378 // containing the text to it (simpler and probably not slower than
1379 // determining which of them is really topmost/leftmost/...)
1380 wxCoord w, h;
1381 GetTextExtent(text, &w, &h);
1382
1383 double rad = DegToRad(angle);
1384
1385 // "upper left" and "upper right"
1386 CalcBoundingBox(x, y);
1387 CalcBoundingBox(x + w*cos(rad), y - h*sin(rad));
1388 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
1389
1390 // "bottom left" and "bottom right"
1391 x += (wxCoord)(h*sin(rad));
1392 y += (wxCoord)(h*cos(rad));
1393 CalcBoundingBox(x, y);
1394 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
1395 }
1396*/
1397}
1398
fb46a9a6
DW
1399// ---------------------------------------------------------------------------
1400// set GDI objects
1401// ---------------------------------------------------------------------------
1408104d 1402
938aa9c4
DW
1403void wxDC::DoSelectPalette(
1404 bool bRealize
1405)
1406{
1407 //
1408 // Set the old object temporarily, in case the assignment deletes an object
1409 // that's not yet selected out.
1410 //
1411 if (m_hOldPalette)
1412 {
1413 m_hOldPalette = 0;
1414 }
1415
1416 if (m_palette.Ok())
1417 {
1418 HPALETTE hOldPal;
1419
1420 hOldPal = ::GpiSelectPalette((HDC) m_hPS, (HPALETTE) m_palette.GetHPALETTE());
1421 if (!m_hOldPalette)
1422 m_hOldPalette = (WXHPALETTE)hOldPal;
1423 }
1424} // end of wxDC::DoSelectPalette
1425
1426void wxDC::InitializePalette()
1427{
1428 if (wxDisplayDepth() <= 8 )
1429 {
1430 //
1431 // Look for any window or parent that has a custom palette. If any has
1432 // one then we need to use it in drawing operations
1433 //
1434 wxWindow* pWin = m_pCanvas->GetAncestorWithCustomPalette();
1435
1436 m_hasCustomPalette = pWin && pWin->HasCustomPalette();
1437 if (m_hasCustomPalette)
1438 {
1439 m_palette = pWin->GetPalette();
1440
1441 //
1442 // turn on PM translation for this palette
1443 //
1444 DoSelectPalette();
1445 }
1446 }
1447} // end of wxDC::InitializePalette
1448
5fd2b2c6
DW
1449void wxDC::SetPalette(
1450 const wxPalette& rPalette
1451)
1408104d 1452{
5fd2b2c6
DW
1453 if (m_hOldFont)
1454 {
1455 m_hOldFont = 0;
1456 }
1457 m_palette = rPalette;
1458 if (!rPalette.Ok())
1459 {
1460 if (m_hOldFont)
1461 {
1462 m_hOldFont = 0;
1463 }
1464 }
1465 HPALETTE hOldPal = ::GpiSelectPalette((HDC) m_hPS, (HPALETTE) m_palette.GetHPALETTE());
1466 if (!m_hOldPalette)
1467 m_hOldPalette = (WXHPALETTE)hOldPal;
1468} // end of wxDC::SetPalette
1408104d 1469
f6bcfd97
BP
1470void wxDC::SetFont(
1471 const wxFont& rFont
1472)
1408104d 1473{
f6bcfd97
BP
1474 //
1475 // Set the old object temporarily, in case the assignment deletes an object
1476 // that's not yet selected out.
1477 //
1478 if (m_hOldFont)
1479 {
f6bcfd97
BP
1480 m_hOldFont = 0;
1481 }
f6bcfd97 1482 m_font = rFont;
f6bcfd97
BP
1483 if (!rFont.Ok())
1484 {
f6bcfd97
BP
1485 m_hOldFont = 0;
1486 }
1487
e99762c0
DW
1488 m_font.SetPS(m_hPS); // this will realize the font
1489
1490 if (m_font.Ok())
f6bcfd97 1491 {
e99762c0 1492 HFONT hFont = m_font.GetResourceHandle();
f6bcfd97
BP
1493 if (hFont == (HFONT) NULL)
1494 {
1495 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
1496 }
1497 if (!m_hOldFont)
1498 m_hOldFont = (WXHFONT) hFont;
1499 }
e99762c0 1500} // end of wxDC::SetFont
1408104d 1501
7e99520b
DW
1502void wxDC::SetPen(
1503 const wxPen& rPen
1504)
1408104d 1505{
7e99520b
DW
1506 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1507
1508 if (m_pen == rPen)
1509 return;
1510 m_pen = rPen;
1511 if (!m_pen.Ok())
1512 return;
1513
26ac77db
DW
1514 if (m_hOldPen)
1515 m_hOldPen = 0L;
1516 m_pen = rPen;
7e99520b 1517
26ac77db 1518 if (!m_pen.Ok())
7e99520b 1519 {
26ac77db
DW
1520 if (m_hOldPen)
1521 {
1522 m_pen.SetPS((HPS)m_hOldPen);
1523 }
1524 m_hOldPen = 0L;
7e99520b 1525 }
51c1d535 1526
26ac77db 1527 if (m_pen.Ok())
51c1d535 1528 {
26ac77db
DW
1529 if (m_pen.GetResourceHandle())
1530 {
1531 m_pen.SetPS(m_hPS);
1532 if (!m_hOldPen)
1533 m_hOldPen = m_pen.GetPS();
1534 }
51c1d535 1535 }
1408104d 1536}
7e99520b 1537
51c1d535
DW
1538void wxDC::SetBrush(
1539 const wxBrush& rBrush
1540)
1408104d 1541{
15f03b25
DW
1542 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1543
15f03b25
DW
1544 if (m_hOldBrush)
1545 m_hOldBrush = 0L;
1546 m_brush = rBrush;
5fd2b2c6
DW
1547 if (!m_brush.Ok())
1548 if (m_brush == rBrush)
1549 return;
1550 if (!m_brush.Ok())
1551 if (m_hOldBrush)
1552 m_hOldBrush = 0L;
15f03b25
DW
1553
1554 if (!m_brush.Ok())
1555 {
1556 if (m_hOldBrush)
1557 {
1558 m_brush.SetPS((HPS)m_hOldBrush);
1559 }
1560 m_hOldBrush = 0L;
1561 }
1562
1563 if (m_brush.Ok())
1564 {
1565 if (m_brush.GetResourceHandle())
1566 {
1567 m_brush.SetPS(m_hPS);
1568 if (!m_hOldBrush)
5fd2b2c6 1569 m_hOldBrush = (WXHWND)m_brush.GetPS();
15f03b25
DW
1570 }
1571 }
1572} // end of wxDC::SetBrush
1408104d 1573
5fd2b2c6
DW
1574void wxDC::SetBackground(
1575 const wxBrush& rBrush
1576)
1408104d 1577{
5fd2b2c6
DW
1578 m_backgroundBrush = rBrush;
1579 if (!m_backgroundBrush.Ok())
1580 return;
1581 if (m_pCanvas)
1582 {
1583 bool bCustomColours = TRUE;
1584
1585 //
1586 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1587 // change background colours from the control-panel specified colours.
1588 //
1589 if (m_pCanvas->IsKindOf(CLASSINFO(wxWindow)) &&
1590 ((m_pCanvas->GetWindowStyleFlag() & wxUSER_COLOURS) != wxUSER_COLOURS))
1591 bCustomColours = FALSE;
1592 if (bCustomColours)
1593 {
1594 if (m_backgroundBrush.GetStyle()==wxTRANSPARENT)
1595 {
1596 m_pCanvas->SetTransparent(TRUE);
1597 }
1598 else
1599 {
1600 //
1601 // Setting the background brush of a DC
1602 // doesn't affect the window background colour. However,
1603 // I'm leaving in the transparency setting because it's needed by
1604 // various controls (e.g. wxStaticText) to determine whether to draw
1605 // transparently or not. TODO: maybe this should be a new function
1606 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1607 // parent?
1608 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1609 //
1610 m_pCanvas->SetTransparent(FALSE);
1611 }
1612 }
1613 }
1614 COLORREF vNewColor = m_backgroundBrush.GetColour().GetPixel();
1615 (void)::GpiSetBackColor((HPS)m_hPS, (LONG)vNewColor);
1616} // end of wxDC::SetBackground
1408104d 1617
7e99520b
DW
1618void wxDC::SetBackgroundMode(
1619 int nMode
1620)
1408104d 1621{
7e99520b 1622 m_backgroundMode = nMode;
5fd2b2c6 1623} // end of wxDC::SetBackgroundMode
1408104d 1624
5fd2b2c6
DW
1625void wxDC::SetLogicalFunction(
1626 int nFunction
1627)
1408104d 1628{
5fd2b2c6
DW
1629 m_logicalFunction = nFunction;
1630 SetRop((WXHDC)m_hDC);
1631} // wxDC::SetLogicalFunction
1408104d 1632
5fd2b2c6
DW
1633void wxDC::SetRop(
1634 WXHDC hDC
1635)
ce44c50e 1636{
5fd2b2c6 1637 if (!hDC || m_logicalFunction < 0)
ce44c50e
DW
1638 return;
1639
5fd2b2c6 1640 LONG lCRop;
ce44c50e
DW
1641 switch (m_logicalFunction)
1642 {
5fd2b2c6
DW
1643 case wxXOR:
1644 lCRop = FM_XOR;
1645 break;
1646
1647 case wxINVERT:
1648 lCRop = FM_INVERT;
1649 break;
1650
1651 case wxOR_REVERSE:
1652 lCRop = FM_MERGESRCNOT;
1653 break;
1654
1655 case wxAND_REVERSE:
1656 lCRop = FM_NOTMASKSRC;
1657 break;
1658
1659 case wxCLEAR:
1660 lCRop = FM_ONE;
1661 break;
1662
1663 case wxSET:
1664 lCRop = FM_ZERO;
1665 break;
1666
1667 case wxSRC_INVERT:
1668 lCRop = FM_MERGENOTSRC;
1669 break;
1670
1671 case wxOR_INVERT:
1672 lCRop = FM_MERGESRCNOT;
1673 break;
1674
1675 case wxAND:
1676 lCRop = FM_AND;
1677 break;
1678
1679 case wxOR:
1680 lCRop = FM_OR;
1681 break;
1682
1683 case wxAND_INVERT:
1684 lCRop = FM_SUBTRACT;
1685 break;
1686
1687 case wxEQUIV:
1688 case wxNAND:
1689 case wxCOPY:
1690 default:
1691 lCRop = FM_OVERPAINT;
1692 break;
ce44c50e 1693 }
5fd2b2c6
DW
1694 ::GpiSetMix((HPS)hDC, lCRop);
1695} // end of wxDC::SetRop
ce44c50e 1696
5fd2b2c6
DW
1697bool wxDC::StartDoc(
1698 const wxString& rsMessage
1699)
ce44c50e 1700{
fb46a9a6
DW
1701 // We might be previewing, so return TRUE to let it continue.
1702 return TRUE;
5fd2b2c6 1703} // end of wxDC::StartDoc
fb46a9a6
DW
1704
1705void wxDC::EndDoc()
1706{
5fd2b2c6 1707} // end of wxDC::EndDoc
fb46a9a6
DW
1708
1709void wxDC::StartPage()
1710{
5fd2b2c6 1711} // end of wxDC::StartPage
fb46a9a6
DW
1712
1713void wxDC::EndPage()
1714{
5fd2b2c6 1715} // end of wxDC::EndPage
fb46a9a6
DW
1716
1717// ---------------------------------------------------------------------------
1718// text metrics
1719// ---------------------------------------------------------------------------
1720
7cdc2f1e 1721wxCoord wxDC::GetCharHeight() const
fb46a9a6 1722{
05a8bfed
DW
1723 FONTMETRICS vFM; // metrics structure
1724
1725 ::GpiQueryFontMetrics( m_hPS
1726 ,sizeof(FONTMETRICS)
1727 ,&vFM
1728 );
1729 return YDEV2LOGREL(vFM.lXHeight);
fb46a9a6
DW
1730}
1731
7cdc2f1e 1732wxCoord wxDC::GetCharWidth() const
fb46a9a6 1733{
05a8bfed
DW
1734 FONTMETRICS vFM; // metrics structure
1735
1736 ::GpiQueryFontMetrics( m_hPS
1737 ,sizeof(FONTMETRICS)
1738 ,&vFM
1739 );
1740 return XDEV2LOGREL(vFM.lAveCharWidth);
7e99520b
DW
1741}
1742
1743void wxDC::DoGetTextExtent(
1744 const wxString& rsString
1745, wxCoord* pvX
1746, wxCoord* pvY
f44fdfb0 1747, wxCoord* pvDescent
7e99520b
DW
1748, wxCoord* pvExternalLeading
1749, wxFont* pTheFont
1750) const
1751{
1752 POINTL avPoint[TXTBOX_COUNT];
1753 POINTL vPtMin;
1754 POINTL vPtMax;
1755 int i;
1756 int l;
1757 FONTMETRICS vFM; // metrics structure
1758 BOOL bRc;
1759 char* pStr;
1760 ERRORID vErrorCode; // last error id code
1761 wxFont* pFontToUse = (wxFont*)pTheFont;
1762
2c4a8d17
DW
1763 char zMsg[128]; // DEBUG
1764 wxString sError;
1765
7e99520b
DW
1766 if (!pFontToUse)
1767 pFontToUse = (wxFont*)&m_font;
1768 l = rsString.length();
1769 pStr = (PCH) rsString.c_str();
fb46a9a6 1770
7e99520b
DW
1771 //
1772 // In world coordinates.
1773 //
1774 bRc = ::GpiQueryTextBox( m_hPS
1775 ,l
1776 ,pStr
1777 ,TXTBOX_COUNT // return maximum information
1778 ,avPoint // array of coordinates points
f44fdfb0 1779 );
7e99520b
DW
1780 if(!bRc)
1781 {
1782 vErrorCode = ::WinGetLastError(wxGetInstance());
2c4a8d17
DW
1783 sError = wxPMErrorToStr(vErrorCode);
1784 // DEBUG
1785 sprintf(zMsg, "GpiQueryTextBox for %s: failed with Error: %x - %s", pStr, vErrorCode, sError.c_str());
1786 (void)wxMessageBox( "wxWindows Menu sample"
1787 ,zMsg
1788 ,wxICON_INFORMATION
1789 );
7e99520b
DW
1790 }
1791
1792 vPtMin.x = avPoint[0].x;
1793 vPtMax.x = avPoint[0].x;
1794 vPtMin.y = avPoint[0].y;
1795 vPtMax.y = avPoint[0].y;
1796 for (i = 1; i < 4; i++)
1797 {
1798 if(vPtMin.x > avPoint[i].x) vPtMin.x = avPoint[i].x;
1799 if(vPtMin.y > avPoint[i].y) vPtMin.y = avPoint[i].y;
1800 if(vPtMax.x < avPoint[i].x) vPtMax.x = avPoint[i].x;
1801 if(vPtMax.y < avPoint[i].y) vPtMax.y = avPoint[i].y;
1802 }
1803 ::GpiQueryFontMetrics( m_hPS
1804 ,sizeof(FONTMETRICS)
1805 ,&vFM
1806 );
1807
1808 if (pvX)
1809 *pvX = (wxCoord)(vPtMax.x - vPtMin.x + 1);
1810 if (pvY)
1811 *pvY = (wxCoord)(vPtMax.y - vPtMin.y + 1);
1812 if (pvDescent)
1813 *pvDescent = vFM.lMaxDescender;
f44fdfb0 1814 if (pvExternalLeading)
7e99520b 1815 *pvExternalLeading = vFM.lExternalLeading;
fb46a9a6
DW
1816}
1817
5fd2b2c6
DW
1818void wxDC::SetMapMode(
1819 int nMode
1820)
fb46a9a6 1821{
5fd2b2c6
DW
1822 int nPixelWidth = 0;
1823 int nPixelHeight = 0;
1824 int nMmWidth = 1;
1825 int nMmHeight = 1;
1826 LONG lArray[CAPS_VERTICAL_RESOLUTION];
fb46a9a6 1827
5fd2b2c6
DW
1828 m_mappingMode = nMode;
1829
1830 if(::DevQueryCaps( m_hDC
1831 ,CAPS_FAMILY
1832 ,CAPS_VERTICAL_RESOLUTION
1833 ,lArray
1834 ))
1835 {
1836 LONG lHorzRes;
1837 LONG lVertRes;
1838
1839 nPixelWidth = lArray[CAPS_WIDTH];
1840 nPixelHeight = lArray[CAPS_HEIGHT];
1841 lHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
1842 lVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
1843 nMmWidth = (lHorzRes/1000) * nPixelWidth;
1844 nMmWidth = (lVertRes/1000) * nPixelHeight;
1845 }
1846 if ((nPixelWidth == 0) || (nPixelHeight == 0) || (nMmWidth == 0) || (nMmHeight == 0))
1847 {
1848 return;
1849 }
1850
1851 double dMm2pixelsX = nPixelWidth/nMmWidth;
1852 double dMm2pixelsY = nPixelHeight/nMmHeight;
1853
1854 switch (nMode)
1855 {
1856 case wxMM_TWIPS:
1857 m_logicalScaleX = (twips2mm * dMm2pixelsX);
1858 m_logicalScaleY = (twips2mm * dMm2pixelsY);
1859 break;
1860
1861 case wxMM_POINTS:
1862 m_logicalScaleX = (pt2mm * dMm2pixelsX);
1863 m_logicalScaleY = (pt2mm * dMm2pixelsY);
1864 break;
1865
1866 case wxMM_METRIC:
1867 m_logicalScaleX = dMm2pixelsX;
1868 m_logicalScaleY = dMm2pixelsY;
1869 break;
1870
1871 case wxMM_LOMETRIC:
1872 m_logicalScaleX = (dMm2pixelsX/10.0);
1873 m_logicalScaleY = (dMm2pixelsY/10.0);
1874 break;
1875
1876 case wxMM_TEXT:
1877 default:
1878 m_logicalScaleX = 1.0;
1879 m_logicalScaleY = 1.0;
1880 break;
1881 }
1882 SIZEL vSize;
1883 ULONG ulOptions;
1884
1885 ulOptions = ::GpiQueryPS(m_hPS, &vSize);
1886 if (!ulOptions & PU_ARBITRARY)
1887 {
1888 ulOptions = PU_ARBITRARY | GPIF_DEFAULT;
1889 ::GpiSetPS(m_hPS, &vSize, ulOptions);
1890 }
74de9ed6
GD
1891 m_nWindowExtX = (int)MS_XDEV2LOG(VIEWPORT_EXTENT);
1892 m_nWindowExtY = (int)MS_YDEV2LOG(VIEWPORT_EXTENT);
5fd2b2c6
DW
1893 // ????
1894}; // end of wxDC::SetMapMode
1895
1896void wxDC::SetUserScale(
1897 double dX
1898, double dY
1899)
fb46a9a6 1900{
5fd2b2c6
DW
1901 m_userScaleX = dX;
1902 m_userScaleY = dY;
fb46a9a6
DW
1903
1904 SetMapMode(m_mappingMode);
5fd2b2c6 1905} // end of wxDC::SetUserScale
fb46a9a6 1906
5fd2b2c6
DW
1907void wxDC::SetAxisOrientation(
1908 bool bXLeftRight
1909, bool bYBottomUp
1910)
fb46a9a6 1911{
5fd2b2c6
DW
1912 m_signX = bXLeftRight ? 1 : -1;
1913 m_signY = bYBottomUp ? -1 : 1;
fb46a9a6
DW
1914
1915 SetMapMode(m_mappingMode);
5fd2b2c6 1916} // end of wxDC::SetAxisOrientation
fb46a9a6 1917
5fd2b2c6
DW
1918void wxDC::SetSystemScale(
1919 double dX
1920, double dY
1921)
fb46a9a6 1922{
5fd2b2c6
DW
1923 m_scaleX = dX;
1924 m_scaleY = dY;
fb46a9a6
DW
1925
1926 SetMapMode(m_mappingMode);
5fd2b2c6 1927} // end of wxDC::SetSystemScale
fb46a9a6 1928
5fd2b2c6
DW
1929void wxDC::SetLogicalOrigin(
1930 wxCoord vX
1931, wxCoord vY
1932)
fb46a9a6 1933{
5fd2b2c6
DW
1934 RECTL vRect;
1935
1936 ::GpiQueryPageViewport( m_hPS
1937 ,&vRect
1938 );
1939 vRect.xRight -= vX;
1940 vRect.yTop += vY;
1941 vRect.xLeft = vX;
1942 vRect.yBottom = vY;
1943 ::GpiSetPageViewport( m_hPS
1944 ,&vRect
1945 );
1946}; // end of wxDC::SetLogicalOrigin
fb46a9a6 1947
8d854fa9 1948void wxDC::SetDeviceOrigin(
5fd2b2c6
DW
1949 wxCoord vX
1950, wxCoord vY
8d854fa9 1951)
fb46a9a6 1952{
26ac77db
DW
1953 RECTL vRect;
1954
5fd2b2c6
DW
1955 m_deviceOriginX = vX;
1956 m_deviceOriginY = vY;
26ac77db
DW
1957 ::GpiQueryPageViewport( m_hPS
1958 ,&vRect
1959 );
5fd2b2c6
DW
1960 vRect.xLeft += vX;
1961 vRect.xRight += vX;
1962 vRect.yBottom -= vY;
1963 vRect.yTop -= vY;
26ac77db
DW
1964 ::GpiSetPageViewport( m_hPS
1965 ,&vRect
1966 );
5fd2b2c6 1967}; // end of wxDC::SetDeviceOrigin
fb46a9a6
DW
1968
1969// ---------------------------------------------------------------------------
1970// coordinates transformations
1971// ---------------------------------------------------------------------------
1972
7cdc2f1e 1973wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
fb46a9a6 1974{
f6bcfd97
BP
1975 return (wxCoord) (((x) - m_deviceOriginX)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX) - m_logicalOriginX);
1976}
fb46a9a6 1977
7cdc2f1e 1978wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
fb46a9a6 1979{
74de9ed6
GD
1980 // axis orientation is not taken into account for conversion of a distance
1981 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_scaleX));
f6bcfd97 1982}
fb46a9a6 1983
7cdc2f1e 1984wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
fb46a9a6 1985{
f6bcfd97
BP
1986 return (wxCoord) (((y) - m_deviceOriginY)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY) - m_logicalOriginY);
1987}
fb46a9a6 1988
7cdc2f1e 1989wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
fb46a9a6 1990{
74de9ed6
GD
1991 // axis orientation is not taken into account for conversion of a distance
1992 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_scaleY));
f6bcfd97 1993}
fb46a9a6 1994
7cdc2f1e 1995wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
fb46a9a6 1996{
f6bcfd97
BP
1997 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
1998}
fb46a9a6 1999
7cdc2f1e 2000wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
fb46a9a6 2001{
74de9ed6
GD
2002 // axis orientation is not taken into account for conversion of a distance
2003 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_scaleX);
f6bcfd97 2004}
fb46a9a6 2005
7cdc2f1e 2006wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
fb46a9a6 2007{
f6bcfd97
BP
2008 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
2009}
fb46a9a6 2010
7cdc2f1e 2011wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
fb46a9a6 2012{
74de9ed6
GD
2013 // axis orientation is not taken into account for conversion of a distance
2014 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_scaleY);
f6bcfd97 2015}
fb46a9a6
DW
2016
2017// ---------------------------------------------------------------------------
2018// bit blit
2019// ---------------------------------------------------------------------------
2020
5afb9458
DW
2021bool wxDC::DoBlit(
2022 wxCoord vXdest
2023, wxCoord vYdest
2024, wxCoord vWidth
2025, wxCoord vHeight
2026, wxDC* pSource
2027, wxCoord vXsrc
2028, wxCoord vYsrc
2029, int nRop
2030, bool bUseMask
893758d5
DW
2031, wxCoord vXsrcMask
2032, wxCoord vYsrcMask
5afb9458 2033)
fb46a9a6 2034{
5afb9458
DW
2035 wxMask* pMask = NULL;
2036 CHARBUNDLE vCbnd;
2037 COLORREF vOldTextColor;
2038 COLORREF vOldBackground = ::GpiQueryBackColor(m_hPS);
5afb9458
DW
2039
2040 if (bUseMask)
2041 {
2042 const wxBitmap& rBmp = pSource->m_vSelectedBitmap;
2043
2044 pMask = rBmp.GetMask();
2045 if (!(rBmp.Ok() && pMask && pMask->GetMaskBitmap()))
2046 {
2047 bUseMask = FALSE;
2048 }
2049 }
2050
2051 ::GpiQueryAttrs( m_hPS
2052 ,PRIM_CHAR
2053 ,CBB_COLOR
2054 ,&vCbnd
2055 );
2056 vOldTextColor = (COLORREF)vCbnd.lColor;
2057
2058 if (m_textForegroundColour.Ok())
2059 {
2060 vCbnd.lColor = (LONG)m_textForegroundColour.GetPixel();
2061 ::GpiSetAttrs( m_hPS // presentation-space handle
2062 ,PRIM_CHAR // Char primitive.
2063 ,CBB_COLOR // sets color.
2064 ,0
2065 ,&vCbnd // buffer for attributes.
2066 );
2067 }
2068 if (m_textBackgroundColour.Ok())
2069 {
2070 ::GpiSetBackColor(m_hPS, (LONG)m_textBackgroundColour.GetPixel());
2071 }
2072
2073 LONG lRop = ROP_SRCCOPY;
2074
2075 switch (nRop)
2076 {
2077 case wxXOR: lRop = ROP_SRCINVERT; break;
2078 case wxINVERT: lRop = ROP_DSTINVERT; break;
2079 case wxOR_REVERSE: lRop = 0x00DD0228; break;
2080 case wxAND_REVERSE: lRop = ROP_SRCERASE; break;
2081 case wxCLEAR: lRop = ROP_ZERO; break;
2082 case wxSET: lRop = ROP_ONE; break;
2083 case wxOR_INVERT: lRop = ROP_MERGEPAINT; break;
2084 case wxAND: lRop = ROP_SRCAND; break;
2085 case wxOR: lRop = ROP_SRCPAINT; break;
2086 case wxEQUIV: lRop = 0x00990066; break;
2087 case wxNAND: lRop = 0x007700E6; break;
2088 case wxAND_INVERT: lRop = 0x00220326; break;
2089 case wxCOPY: lRop = ROP_SRCCOPY; break;
2090 case wxNO_OP: lRop = ROP_NOTSRCERASE; break;
2091 case wxSRC_INVERT: lRop = ROP_SRCINVERT; break;
2092 case wxNOR: lRop = ROP_NOTSRCCOPY; break;
2093 default:
2094 wxFAIL_MSG( wxT("unsupported logical function") );
2095 return FALSE;
2096 }
2097
2098 bool bSuccess;
b02121c3 2099
5afb9458
DW
2100 if (bUseMask)
2101 {
2102 //
2103 // Blit bitmap with mask
2104 //
2105
2106 //
b02121c3 2107 // Create a temp buffer bitmap and DCs/PSs to access it and the mask
5afb9458 2108 //
b02121c3
DW
2109 HDC hDCMask;
2110 HDC hDCBuffer;
2111 HPS hPSMask;
2112 HPS hPSBuffer;
2113 DEVOPENSTRUC vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
2114 BITMAPINFOHEADER2 vBmpHdr;
893758d5 2115 HBITMAP hBufBitmap;
b02121c3
DW
2116 SIZEL vSize = {0, 0};
2117 LONG rc;
2118
b02121c3
DW
2119 memset(&vBmpHdr, 0, sizeof(BITMAPINFOHEADER2));
2120 vBmpHdr.cbFix = sizeof(BITMAPINFOHEADER2);
2121 vBmpHdr.cx = vWidth;
2122 vBmpHdr.cy = vHeight;
2123 vBmpHdr.cPlanes = 1;
2124 vBmpHdr.cBitCount = 24;
2125
893758d5
DW
2126#if wxUSE_DC_CACHEING
2127 if (TRUE)
2128 {
2129 //
2130 // create a temp buffer bitmap and DCs to access it and the mask
2131 //
2132 wxDCCacheEntry* pDCCacheEntry1 = FindDCInCache( NULL
2133 ,pSource->GetHPS()
2134 );
2135 wxDCCacheEntry* pDCCacheEntry2 = FindDCInCache( pDCCacheEntry1
2136 ,GetHPS()
2137 );
2138 wxDCCacheEntry* pBitmapCacheEntry = FindBitmapInCache( GetHPS()
2139 ,vWidth
2140 ,vHeight
2141 );
2142
2143 hPSMask = pDCCacheEntry1->m_hPS;
2144 hDCBuffer = (HDC)pDCCacheEntry2->m_hPS;
2145 hBufBitmap = (HBITMAP)pBitmapCacheEntry->m_hBitmap;
2146 }
2147 else
2148#endif
2149 {
2150 hDCMask = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2151 hDCBuffer = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2152 hPSMask = ::GpiCreatePS(vHabmain, hDCMask, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2153 hPSBuffer = ::GpiCreatePS(vHabmain, hDCBuffer, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2154 hBufBitmap = ::GpiCreateBitmap(GetHPS(), &vBmpHdr, 0L, NULL, NULL);
2155 }
2156
b02121c3
DW
2157 POINTL aPoint1[4] = { 0, 0
2158 ,vWidth, vHeight
2159 ,vXdest, vYdest
2160 ,vXdest + vWidth, vYdest + vHeight
2161 };
2162 POINTL aPoint2[4] = { 0, 0
2163 ,vWidth, vHeight
2164 ,vXsrc, vYsrc
2165 ,vXsrc + vWidth, vYsrc + vHeight
2166 };
2167 POINTL aPoint3[4] = { vXdest, vYdest
23e356de
DW
2168 ,vXdest + vWidth, vYdest + vHeight
2169 ,vXsrc, vYsrc
2170 ,vXsrc + vWidth, vYsrc + vHeight
2171 };
2172 POINTL aPoint4[4] = { vXdest, vYdest
b02121c3
DW
2173 ,vXdest + vWidth, vYdest + vHeight
2174 ,0, 0
2175 ,vWidth, vHeight
2176 };
2177 ::GpiSetBitmap(hPSMask, (HBITMAP) pMask->GetMaskBitmap());
2178 ::GpiSetBitmap(hPSBuffer, (HBITMAP) hBufBitmap);
5afb9458 2179
b02121c3
DW
2180 //
2181 // Copy dest to buffer
2182 //
2183 rc = ::GpiBitBlt( hPSBuffer
2184 ,GetHPS()
2185 ,4L
2186 ,aPoint1
2187 ,ROP_SRCCOPY
2188 ,BBO_IGNORE
2189 );
2190 if (rc == GPI_ERROR)
2191 {
2192 wxLogLastError(wxT("BitBlt"));
2193 }
5afb9458 2194
b02121c3
DW
2195 //
2196 // Copy src to buffer using selected raster op
2197 //
2198 rc = ::GpiBitBlt( hPSBuffer
2199 ,GetHPS()
2200 ,4L
2201 ,aPoint2
2202 ,lRop
2203 ,BBO_IGNORE
2204 );
2205 if (rc == GPI_ERROR)
2206 {
2207 wxLogLastError(wxT("BitBlt"));
2208 }
5afb9458 2209
b02121c3
DW
2210 //
2211 // Set masked area in buffer to BLACK (pixel value 0)
2212 //
2213 COLORREF vPrevBkCol = ::GpiQueryBackColor(GetHPS());
2214 COLORREF vPrevCol = ::GpiQueryColor(GetHPS());
2215
2216 ::GpiSetBackColor(GetHPS(), OS2RGB(255, 255, 255));
2217 ::GpiSetColor(GetHPS(), OS2RGB(0, 0, 0));
2218
2219 rc = ::GpiBitBlt( hPSBuffer
2220 ,hPSMask
2221 ,4L
2222 ,aPoint2
2223 ,ROP_SRCAND
2224 ,BBO_IGNORE
2225 );
2226 if (rc == GPI_ERROR)
2227 {
2228 wxLogLastError(wxT("BitBlt"));
2229 }
5afb9458 2230
b02121c3
DW
2231 //
2232 // Set unmasked area in dest to BLACK
2233 //
2234 ::GpiSetBackColor(GetHPS(), OS2RGB(0, 0, 0));
2235 ::GpiSetColor(GetHPS(), OS2RGB(255, 255, 255));
2236 rc = ::GpiBitBlt( GetHPS()
2237 ,hPSMask
2238 ,4L
23e356de 2239 ,aPoint3
b02121c3
DW
2240 ,ROP_SRCAND
2241 ,BBO_IGNORE
2242 );
2243 if (rc == GPI_ERROR)
2244 {
2245 wxLogLastError(wxT("BitBlt"));
5afb9458 2246 }
b02121c3
DW
2247
2248 //
2249 // Restore colours to original values
2250 //
2251 ::GpiSetBackColor(GetHPS(), vPrevBkCol);
2252 ::GpiSetColor(GetHPS(), vPrevCol);
2253
2254 //
2255 // OR buffer to dest
2256 //
2257 rc = ::GpiBitBlt( GetHPS()
2258 ,hPSMask
2259 ,4L
23e356de 2260 ,aPoint4
b02121c3
DW
2261 ,ROP_SRCPAINT
2262 ,BBO_IGNORE
2263 );
2264 if (rc == GPI_ERROR)
2265 {
2266 bSuccess = FALSE;
2267 wxLogLastError(wxT("BitBlt"));
2268 }
2269
2270 //
2271 // Tidy up temporary DCs and bitmap
2272 //
2273 ::GpiSetBitmap(hPSMask, NULLHANDLE);
2274 ::GpiSetBitmap(hPSBuffer, NULLHANDLE);
893758d5 2275#if !wxUSE_DC_CACHEING
b02121c3
DW
2276 ::GpiDestroyPS(hPSMask);
2277 ::GpiDestroyPS(hPSBuffer);
2278 ::DevCloseDC(hDCMask);
2279 ::DevCloseDC(hDCBuffer);
2280 ::GpiDeleteBitmap(hBufBitmap);
893758d5 2281#endif
b02121c3 2282 bSuccess = TRUE;
5afb9458 2283 }
b02121c3
DW
2284 else // no mask, just BitBlt() it
2285 {
2286 POINTL aPoint[4] = { vXdest, vYdest
2287 ,vXdest + vWidth, vYdest + vHeight
2288 ,vXsrc, vYsrc
2289 ,vXsrc + vWidth, vYsrc + vHeight
2290 };
2291
5afb9458
DW
2292 bSuccess = (::GpiBitBlt( m_hPS
2293 ,pSource->GetHPS()
2294 ,4L
2295 ,aPoint
2296 ,lRop
2297 ,BBO_IGNORE
2298 ) != GPI_ERROR);
2299 if (!bSuccess )
2300 {
2301 wxLogLastError(wxT("BitBlt"));
2302 }
b02121c3 2303 }
5afb9458
DW
2304 vCbnd.lColor = (LONG)vOldTextColor;
2305 ::GpiSetAttrs( m_hPS // presentation-space handle
2306 ,PRIM_CHAR // Char primitive.
2307 ,CBB_COLOR // sets color.
2308 ,0
2309 ,&vCbnd // buffer for attributes.
2310 );
2311 ::GpiSetBackColor(m_hPS, (LONG)vOldBackground);
2312 return bSuccess;
fb46a9a6
DW
2313}
2314
5fd2b2c6
DW
2315void wxDC::DoGetSize(
2316 int* pnWidth
2317, int* pnHeight
2318) const
fb46a9a6 2319{
5fd2b2c6
DW
2320 LONG lArray[CAPS_HEIGHT];
2321
2322 if(::DevQueryCaps( m_hDC
2323 ,CAPS_FAMILY
2324 ,CAPS_HEIGHT
2325 ,lArray
2326 ))
2327 {
2328 *pnWidth = lArray[CAPS_WIDTH];
2329 *pnHeight = lArray[CAPS_HEIGHT];
2330 }
2331}; // end of wxDC::DoGetSize(
fb46a9a6 2332
5fd2b2c6
DW
2333void wxDC::DoGetSizeMM(
2334 int* pnWidth
2335, int* pnHeight
2336) const
fb46a9a6 2337{
5fd2b2c6
DW
2338 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2339
2340 if(::DevQueryCaps( m_hDC
2341 ,CAPS_FAMILY
2342 ,CAPS_VERTICAL_RESOLUTION
2343 ,lArray
2344 ))
2345 {
2346 int nWidth;
2347 int nHeight;
2348 int nHorzRes;
2349 int nVertRes;
2350
2351 nWidth = lArray[CAPS_WIDTH];
2352 nHeight = lArray[CAPS_HEIGHT];
2353 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2354 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2355 nWidth = (nHorzRes/1000) * nWidth;
2356 nHeight = (nVertRes/1000) * nHeight;
2357 }
2358}; // end of wxDC::DoGetSizeMM
fb46a9a6
DW
2359
2360wxSize wxDC::GetPPI() const
2361{
5fd2b2c6
DW
2362 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2363 int nWidth;
2364 int nHeight;
fb46a9a6 2365
5fd2b2c6
DW
2366 if(::DevQueryCaps( m_hDC
2367 ,CAPS_FAMILY
2368 ,CAPS_VERTICAL_RESOLUTION
2369 ,lArray
2370 ))
2371 {
2372 int nPelWidth;
2373 int nPelHeight;
2374 int nHorzRes;
2375 int nVertRes;
2376
2377 nPelWidth = lArray[CAPS_WIDTH];
2378 nPelHeight = lArray[CAPS_HEIGHT];
2379 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2380 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2381 nWidth = (nHorzRes/39.3) * nPelWidth;
2382 nHeight = (nVertRes/39.3) * nPelHeight;
2383 }
2384 return (wxSize(nWidth,nHeight));
2385} // end of wxDC::GetPPI
2386
2387void wxDC::SetLogicalScale(
2388 double dX
2389, double dY
2390)
fb46a9a6 2391{
5fd2b2c6
DW
2392 m_logicalScaleX = dX;
2393 m_logicalScaleY = dY;
2394}; // end of wxDC::SetLogicalScale
fb46a9a6
DW
2395
2396#if WXWIN_COMPATIBILITY
2397void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
2398 float *descent, float *externalLeading,
2399 wxFont *theFont, bool use16bit) const
2400{
7cdc2f1e 2401 wxCoord x1, y1, descent1, externalLeading1;
fb46a9a6
DW
2402 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
2403 *x = x1; *y = y1;
2404 if (descent)
2405 *descent = descent1;
2406 if (externalLeading)
2407 *externalLeading = externalLeading1;
2408}
2409#endif
2410