OS/2 Image processing updates along with some statusbar updates
[wxWidgets.git] / src / os2 / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose: wxDC class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/14/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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"
25 #include "wx/msgdlg.h"
26 #include "wx/module.h"
27 #endif
28
29 #include "wx/dcprint.h"
30
31 #include <string.h>
32 #include <math.h>
33
34 #include "wx/os2/private.h"
35
36 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
37
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
59 // ---------------------------------------------------------------------------
60 // constants
61 // ---------------------------------------------------------------------------
62
63 static const int VIEWPORT_EXTENT = 1000;
64
65 static const int MM_POINTS = 9;
66 static const int MM_METRIC = 10;
67
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
78 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
79
80 int 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
97 int QueryTextBkColor(
98 HPS hPS
99 )
100 {
101 CHARBUNDLE vCbnd;
102
103 ::GpiQueryAttrs( hPS // presentation-space handle
104 ,PRIM_CHAR // Char primitive.
105 ,CBB_BACK_COLOR // Background color.
106 ,&vCbnd // buffer for attributes.
107 );
108 return vCbnd.lBackColor;
109 }
110
111
112 int 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
132 int 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
149 // ===========================================================================
150 // implementation
151 // ===========================================================================
152
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
165 wxList wxDC::m_svBitmapCache;
166 wxList wxDC::m_svDCCache;
167
168 wxDCCacheEntry::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
182 wxDCCacheEntry::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
194 wxDCCacheEntry::~wxDCCacheEntry()
195 {
196 if (m_hBitmap)
197 ::GpiDeleteBitmap(m_hBitmap);
198 if (m_hPS)
199 ::GpiDestroyPS(m_hPS);
200 } // end of wxDCCacheEntry::~wxDCCacheEntry
201
202 wxDCCacheEntry* wxDC::FindBitmapInCache(
203 HPS hPS
204 , int nWidth
205 , int nHeight
206 )
207 {
208 int nDepth = 24; // we'll fix this later ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
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
269 wxDCCacheEntry* 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
300 void wxDC::AddToBitmapCache(
301 wxDCCacheEntry* pEntry
302 )
303 {
304 m_svBitmapCache.Append(pEntry);
305 } // end of wxDC::AddToBitmapCache
306
307 void wxDC::AddToDCCache(
308 wxDCCacheEntry* pEntry
309 )
310 {
311 m_svDCCache.Append(pEntry);
312 } // end of wxDC::AddToDCCache
313
314 void 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
325 class wxDCModule : public wxModule
326 {
327 public:
328 virtual bool OnInit() { return TRUE; }
329 virtual void OnExit() { wxDC::ClearCache(); }
330
331 private:
332 DECLARE_DYNAMIC_CLASS(wxDCModule)
333 }; // end of CLASS wxDCModule
334
335 IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
336
337 #endif // ndef for wxUSE_DC_CACHEING
338
339 // ---------------------------------------------------------------------------
340 // wxDC
341 // ---------------------------------------------------------------------------
342
343 wxDC::wxDC(void)
344 {
345 wxColour vColor;
346
347 m_pCanvas = NULL;
348
349 m_hOldBitmap = 0;
350 m_hOldPen = 0;
351 m_hOldBrush = 0;
352 m_hOldFont = 0;
353 m_hOldPalette = 0;
354
355 m_bOwnsDC = FALSE;
356 m_hDC = 0;
357 m_hOldPS = NULL;
358 m_hPS = NULL;
359 m_bIsPaintTime = FALSE; // True at Paint Time
360
361 vColor.InitFromName("BLACK");
362 m_pen.SetColour(vColor);
363 vColor.Set("WHITE");
364 m_brush.SetColour(vColor);
365 } // end of wxDC::wxDC
366
367 wxDC::~wxDC(void)
368 {
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
397
398 // This will select current objects out of the DC,
399 // which is what you have to do before deleting the
400 // DC.
401 void wxDC::SelectOldObjects(
402 WXHDC hPS
403 )
404 {
405 if (hPS)
406 {
407 if (m_hOldBitmap)
408 {
409 ::GpiSetBitmap(hPS, (HBITMAP) m_hOldBitmap);
410 if (m_vSelectedBitmap.Ok())
411 {
412 m_vSelectedBitmap.SetSelectedInto(NULL);
413 }
414 }
415 m_hOldBitmap = 0;
416 //
417 // OS/2 has no other native GDI objects to set in a PS/DC like windows
418 //
419 m_hOldPen = 0;
420 m_hOldBrush = 0;
421 m_hOldFont = 0;
422 m_hOldPalette = 0;
423 }
424
425 m_brush = wxNullBrush;
426 m_pen = wxNullPen;
427 m_palette = wxNullPalette;
428 m_font = wxNullFont;
429 m_backgroundBrush = wxNullBrush;
430 m_vSelectedBitmap = wxNullBitmap;
431 } // end of wxDC::SelectOldObjects
432
433 // ---------------------------------------------------------------------------
434 // clipping
435 // ---------------------------------------------------------------------------
436
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); \
447 }
448
449 void wxDC::DoSetClippingRegion(
450 wxCoord vX
451 , wxCoord vY
452 , wxCoord vWidth
453 , wxCoord vHeight
454 )
455 {
456 RECTL vRect;
457
458 vY = OS2Y(vY,vHeight);
459 m_clipping = TRUE;
460 vRect.xLeft = vX;
461 vRect.yTop = vY + vHeight;
462 vRect.xRight = vX + vWidth;
463 vRect.yBottom = vY;
464 ::GpiIntersectClipRectangle(m_hPS, &vRect);
465 DO_SET_CLIPPING_BOX()
466 } // end of wxDC::DoSetClippingRegion
467
468 void wxDC::DoSetClippingRegionAsRegion(
469 const wxRegion& rRegion
470 )
471 {
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
482
483 void wxDC::DestroyClippingRegion(void)
484 {
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
505
506 // ---------------------------------------------------------------------------
507 // query capabilities
508 // ---------------------------------------------------------------------------
509
510 bool wxDC::CanDrawBitmap() const
511 {
512 return TRUE;
513 }
514
515 bool wxDC::CanGetTextExtent() const
516 {
517 LONG lTechnology = 0L;
518
519 ::DevQueryCaps(GetHDC(), CAPS_TECHNOLOGY, 1L, &lTechnology);
520 return (lTechnology == CAPS_TECH_RASTER_DISPLAY) || (lTechnology == CAPS_TECH_RASTER_PRINTER);
521 } // end of wxDC::CanGetTextExtent
522
523 int wxDC::GetDepth() const
524 {
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
538
539 // ---------------------------------------------------------------------------
540 // drawing
541 // ---------------------------------------------------------------------------
542
543 void wxDC::Clear()
544 {
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
557 ::GpiErase(m_hPS);
558 } // end of wxDC::Clear
559
560 bool wxDC::DoFloodFill(
561 wxCoord vX
562 , wxCoord vY
563 , const wxColour& rCol
564 , int nStyle
565 )
566 {
567 POINTL vPtlPos;
568 LONG lColor;
569 LONG lOptions;
570 LONG lHits;
571 bool bSuccess = FALSE;
572
573 vPtlPos.x = vX; // Loads x-coordinate
574 vPtlPos.y = OS2Y(vY,0); // Loads y-coordinate
575 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
576 lColor = rCol.GetPixel();
577 lOptions = FF_BOUNDARY;
578 if(wxFLOOD_SURFACE == nStyle)
579 lOptions = FF_SURFACE;
580
581 if ((lHits = ::GpiFloodFill(m_hPS, lOptions, lColor)) != GPI_ERROR)
582 bSuccess = TRUE;
583 return TRUE;
584 } // end of wxDC::DoFloodFill
585
586 bool wxDC::DoGetPixel(
587 wxCoord vX
588 , wxCoord vY
589 , wxColour* pCol
590 ) const
591 {
592 POINTL vPoint;
593 LONG lColor;
594
595 vPoint.x = vX;
596 vPoint.y = OS2Y(vY,0);
597 lColor = ::GpiSetPel(m_hPS, &vPoint);
598
599 //
600 // Get the color of the pen
601 //
602 LONG lPencolor = 0x00ffffff;
603
604 if (m_pen.Ok())
605 {
606 lPencolor = m_pen.GetColour().GetPixel();
607 }
608
609 //
610 // return the color of the pixel
611 //
612 if(pCol)
613 pCol->Set( GetRValue(lColor)
614 ,GetGValue(lColor)
615 ,GetBValue(lColor)
616 );
617 return(lColor == lPencolor);
618 } // end of wxDC::DoGetPixel
619
620 void wxDC::DoCrossHair(
621 wxCoord vX
622 , wxCoord vY
623 )
624 {
625 vY = OS2Y(vY,0);
626
627 wxCoord vX1 = vX - VIEWPORT_EXTENT;
628 wxCoord vY1 = vY - VIEWPORT_EXTENT;
629 wxCoord vX2 = vX + VIEWPORT_EXTENT;
630 wxCoord vY2 = vY + VIEWPORT_EXTENT;
631 POINTL vPoint[4];
632
633 vPoint[0].x = vX1;
634 vPoint[0].y = vY;
635
636 vPoint[1].x = vX2;
637 vPoint[1].y = vY;
638
639 ::GpiMove(m_hPS, &vPoint[0]);
640 ::GpiLine(m_hPS, &vPoint[1]);
641
642 vPoint[2].x = vX;
643 vPoint[2].y = vY1;
644
645 vPoint[3].x = vX;
646 vPoint[3].y = vY2;
647
648 ::GpiMove(m_hPS, &vPoint[2]);
649 ::GpiLine(m_hPS, &vPoint[3]);
650 CalcBoundingBox(vX1, vY1);
651 CalcBoundingBox(vX2, vY2);
652 } // end of wxDC::DoCrossHair
653
654 void wxDC::DoDrawLine(
655 wxCoord vX1
656 , wxCoord vY1
657 , wxCoord vX2
658 , wxCoord vY2
659 )
660 {
661 POINTL vPoint[2];
662
663 vY1 = OS2Y(vY1,0);
664 vY2 = OS2Y(vY2,0);
665
666 vPoint[0].x = vX1;
667 vPoint[0].y = vY1;
668 vPoint[1].x = vX2;
669 vPoint[1].y = vY2;
670 ::GpiMove(m_hPS, &vPoint[0]);
671 ::GpiLine(m_hPS, &vPoint[1]);
672 CalcBoundingBox(vX1, vY1);
673 CalcBoundingBox(vX2, vY2);
674 } // end of wxDC::DoDrawLine
675
676 //////////////////////////////////////////////////////////////////////////////
677 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
678 // and ending at (x2, y2). The current pen is used for the outline and the
679 // current brush for filling the shape. The arc is drawn in an anticlockwise
680 // direction from the start point to the end point.
681 //////////////////////////////////////////////////////////////////////////////
682 void wxDC::DoDrawArc(
683 wxCoord vX1
684 , wxCoord vY1
685 , wxCoord vX2
686 , wxCoord vY2
687 , wxCoord vXc
688 , wxCoord vYc
689 )
690 {
691 POINTL vPtlPos;
692 POINTL vPtlArc[2]; // Structure for current position
693 int nDx;
694 int nDy;
695 double dRadius;
696 double dAngl1;
697 double dAngl2;
698 double dAnglmid;
699 wxCoord vXm;
700 wxCoord vYm;
701 ARCPARAMS vArcp; // Structure for arc parameters
702
703 if((vX1 == vXc && vY1 == vXc) || (vX2 == vXc && vY2 == vXc))
704 return; // Draw point ??
705 dRadius = 0.5 * ( hypot( (double)(vY1 - vYc)
706 ,(double)(vX1 - vXc)
707 ) +
708 hypot( (double)(vY2 - vYc)
709 ,(double)(vX2 - vXc)
710 )
711 );
712
713 dAngl1 = atan2( (double)(vY1 - vYc)
714 ,(double)(vX1 - vXc)
715 );
716 dAngl2 = atan2( (double)(vY2 - vYc)
717 ,(double)(vX2 - vXc)
718 );
719 if(dAngl2 < dAngl1)
720 dAngl2 += M_PI * 2;
721
722 //
723 // GpiPointArc can't draw full arc
724 //
725 if(dAngl2 == dAngl1 || (vX1 == vX2 && vY1 == vY2) )
726 {
727 //
728 // Medium point
729 //
730 dAnglmid = (dAngl1 + dAngl2)/2. + M_PI;
731 vXm = vXc + dRadius * cos(dAnglmid);
732 vYm = vYc + dRadius * sin(dAnglmid);
733 DoDrawArc( vX1, vY1
734 ,vXm, vYm
735 ,vXc, vYc
736 );
737 DoDrawArc( vXm, vYm
738 ,vX2, vY2
739 ,vXc, vYc
740 );
741 return;
742 }
743
744 //
745 // Medium point
746 //
747 dAnglmid = (dAngl1 + dAngl2)/2.;
748 vXm = vXc + dRadius * cos(dAnglmid);
749 vYm = vYc + dRadius * sin(dAnglmid);
750
751 //
752 // Ellipse main axis (r,q), (p,s) with center at (0,0) */
753 //
754 vArcp.lR = 0;
755 vArcp.lQ = 1;
756 vArcp.lP = 1;
757 vArcp.lS = 0;
758 ::GpiSetArcParams(m_hPS, &vArcp); // Sets parameters to default
759
760 vPtlPos.x = vX1; // Loads x-coordinate
761 vPtlPos.y = vY1; // Loads y-coordinate
762 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
763 vPtlArc[0].x = vXm;
764 vPtlArc[0].y = vYm;
765 vPtlArc[1].x = vX2;
766 vPtlArc[1].y = vY2;
767 ::GpiPointArc(m_hPS, vPtlArc); // Draws the arc
768 CalcBoundingBox( (vXc - dRadius)
769 ,(vYc - dRadius)
770 );
771 CalcBoundingBox( (vXc + dRadius)
772 ,(vYc + dRadius)
773 );
774 } // end of wxDC::DoDrawArc
775
776 void wxDC::DoDrawCheckMark(
777 wxCoord vX1
778 , wxCoord vY1
779 , wxCoord vWidth
780 , wxCoord vHeight
781 )
782 {
783 POINTL vPoint[2];
784
785 vY1 = OS2Y(vY1,vHeight);
786
787 vPoint[0].x = vX1;
788 vPoint[0].y = vY1;
789 vPoint[1].x = vX1 + vWidth;
790 vPoint[1].y = vY1 + vHeight;
791
792 ::GpiMove(m_hPS, &vPoint[0]);
793 ::GpiBox( m_hPS // handle to a presentation space
794 ,DRO_OUTLINE // draw the box outline ? or ?
795 ,&vPoint[1] // address of the corner
796 ,0L // horizontal corner radius
797 ,0L // vertical corner radius
798 );
799 if(vWidth > 4 && vHeight > 4)
800 {
801 int nTmp;
802
803 vPoint[0].x += 2; vPoint[0].y += 2;
804 vPoint[1].x -= 2; vPoint[1].y -= 2;
805 ::GpiMove(m_hPS, &vPoint[0]);
806 ::GpiLine(m_hPS, &vPoint[1]);
807 nTmp = vPoint[0].x;
808 vPoint[0].x = vPoint[1].x;
809 vPoint[1].x = nTmp;
810 ::GpiMove(m_hPS, &vPoint[0]);
811 ::GpiLine(m_hPS, &vPoint[1]);
812 }
813 CalcBoundingBox( vX1
814 ,vY1
815 );
816
817 wxCoord vX2 = vX1 + vWidth;
818 wxCoord vY2 = vY1 + vHeight;
819
820 CalcBoundingBox( vX2
821 ,vY2
822 );
823 } // end of wxDC::DoDrawCheckMark
824
825 void wxDC::DoDrawPoint(
826 wxCoord vX
827 , wxCoord vY
828 )
829 {
830 POINTL vPoint;
831 COLORREF vColor = 0x00ffffff;
832
833 if (m_pen.Ok())
834 {
835 vColor = m_pen.GetColour().GetPixel();
836 }
837 ::GpiSetColor(m_hPS, vColor);
838 vPoint.x = vX;
839 vPoint.y = OS2Y(vY,0);
840 ::GpiSetPel(m_hPS, &vPoint);
841 CalcBoundingBox( vX
842 ,vY
843 );
844 } // end of wxDC::DoDrawPoint
845
846 void wxDC::DoDrawPolygon(
847 int n
848 , wxPoint vPoints[]
849 , wxCoord vXoffset
850 , wxCoord vYoffset
851 , int nFillStyle
852 )
853 {
854 ULONG ulCount = 1; // Number of polygons.
855 POLYGON vPlgn; // polygon.
856 ULONG flOptions = 0L; // Drawing options.
857
858 //////////////////////////////////////////////////////////////////////////////
859 // This contains fields of option bits... to draw boundary lines as well as
860 // the area interior.
861 //
862 // Drawing boundary lines:
863 // POLYGON_NOBOUNDARY Does not draw boundary lines.
864 // POLYGON_BOUNDARY Draws boundary lines (the default).
865 //
866 // Construction of the area interior:
867 // POLYGON_ALTERNATE Constructs interior in alternate mode
868 // (the default).
869 // POLYGON_WINDING Constructs interior in winding mode.
870 //////////////////////////////////////////////////////////////////////////////
871
872 ULONG flModel = 0L; // Drawing model.
873
874 //////////////////////////////////////////////////////////////////////////////
875 // Drawing model.
876 // POLYGON_INCL Fill is inclusive of bottom right (the default).
877 // POLYGON_EXCL Fill is exclusive of bottom right.
878 // This is provided to aid migration from other graphics models.
879 //////////////////////////////////////////////////////////////////////////////
880
881 LONG lHits = 0L; // Correlation/error indicator.
882 POINTL vPoint;
883 int i;
884 int nIsTRANSPARENT = 0;
885 LONG lBorderColor = 0L;
886 LONG lColor = 0L;
887
888 lBorderColor = m_pen.GetColour().GetPixel();
889 lColor = m_brush.GetColour().GetPixel();
890 if(m_brush.GetStyle() == wxTRANSPARENT)
891 nIsTRANSPARENT = 1;
892
893 vPlgn.ulPoints = n;
894 vPlgn.aPointl = (POINTL*) calloc( n + 1
895 ,sizeof(POINTL)
896 ); // well, new will call malloc
897
898 for(i = 0; i < n; i++)
899 {
900 vPlgn.aPointl[i].x = vPoints[i].x; // +xoffset;
901 vPlgn.aPointl[i].y = OS2Y(vPoints[i].y,0); // +yoffset;
902 }
903 flModel = POLYGON_BOUNDARY;
904 if(nFillStyle == wxWINDING_RULE)
905 flModel |= POLYGON_WINDING;
906 else
907 flModel |= POLYGON_ALTERNATE;
908
909 vPoint.x = vXoffset;
910 vPoint.y = OS2Y(vYoffset,0);
911
912 ::GpiSetColor(m_hPS, lBorderColor);
913 ::GpiMove(m_hPS, &vPoint);
914 lHits = ::GpiPolygons(m_hPS, ulCount, &vPlgn, flOptions, flModel);
915 free(vPlgn.aPointl);
916 } // end of wxDC::DoDrawPolygon
917
918 void wxDC::DoDrawLines(
919 int n
920 , wxPoint vPoints[]
921 , wxCoord vXoffset
922 , wxCoord vYoffset
923 )
924 {
925 POINTL vPoint;
926
927 if (vXoffset != 0L || vXoffset != 0L)
928 {
929 int i;
930
931 vPoint.x = vPoints[0].x + vXoffset;
932 vPoint.y = OS2Y(vPoints[0].y + vYoffset,0);
933 ::GpiMove(m_hPS, &vPoint);
934
935 LONG lBorderColor = m_pen.GetColour().GetPixel();
936
937 ::GpiSetColor(m_hPS, lBorderColor);
938 for(i = 1; i < n; i++)
939 {
940 vPoint.x = vPoints[i].x + vXoffset;
941 vPoint.y = OS2Y(vPoints[i].y + vYoffset,0);
942 ::GpiLine(m_hPS, &vPoint);
943 }
944 }
945 else
946 {
947 int i;
948
949 CalcBoundingBox( vPoints[i].x
950 ,vPoints[i].y
951 );
952 vPoint.x = vPoints[0].x;
953 vPoint.y = OS2Y(vPoints[0].y,0);
954 ::GpiMove(m_hPS, &vPoint);
955
956 for (i = 0; i < n; i++)
957 {
958 CalcBoundingBox( vPoints[i].x
959 ,vPoints[i].y
960 );
961 vPoint.x = vPoints[i].x;
962 vPoint.y = OS2Y(vPoints[i].y,0);
963 ::GpiLine(m_hPS, &vPoint);
964 }
965 }
966 } // end of wxDC::DoDrawLines
967
968 void wxDC::DoDrawRectangle(
969 wxCoord vX
970 , wxCoord vY
971 , wxCoord vWidth
972 , wxCoord vHeight
973 )
974 {
975 POINTL vPoint[2];
976 LONG lControl;
977 LONG lColor;
978 LONG lBorderColor;
979 int nIsTRANSPARENT = 0;
980
981 //
982 // Might be a memory DC with no Paint rect
983 //
984 if (!(m_vRclPaint.yTop == 0 &&
985 m_vRclPaint.yBottom == 0 &&
986 m_vRclPaint.xRight == 0 &&
987 m_vRclPaint.xLeft == 0))
988 vY = OS2Y(vY,vHeight);
989
990 wxCoord vX2 = vX + vWidth;
991 wxCoord vY2 = vY + vHeight;
992
993 vPoint[0].x = vX;
994 vPoint[0].y = vY;
995 vPoint[1].x = vX + vWidth - 1;
996 vPoint[1].y = vY + vHeight - 1;
997 ::GpiMove(m_hPS, &vPoint[0]);
998 lColor = m_brush.GetColour().GetPixel();
999 lBorderColor = m_pen.GetColour().GetPixel();
1000 if (m_brush.GetStyle() == wxTRANSPARENT)
1001 nIsTRANSPARENT = 1;
1002 if(lColor == lBorderColor || nIsTRANSPARENT)
1003 {
1004 lControl = DRO_OUTLINEFILL; //DRO_FILL;
1005 if(m_brush.GetStyle() == wxTRANSPARENT)
1006 lControl = DRO_OUTLINE;
1007
1008 ::GpiSetColor(m_hPS, lColor);
1009 ::GpiBox( m_hPS // handle to a presentation space
1010 ,lControl // draw the box outline ? or ?
1011 ,&vPoint[1] // address of the corner
1012 ,0L // horizontal corner radius
1013 ,0L // vertical corner radius
1014 );
1015 }
1016 else
1017 {
1018 lControl = DRO_OUTLINE;
1019 ::GpiSetColor( m_hPS
1020 ,lBorderColor
1021 );
1022 ::GpiBox( m_hPS
1023 ,lControl
1024 ,&vPoint[1]
1025 ,0L
1026 ,0L
1027 );
1028 lControl = DRO_FILL;
1029 ::GpiSetColor( m_hPS
1030 ,lColor
1031 );
1032 vPoint[0].x = vX + 1;
1033 vPoint[0].y = vY + 1;
1034 vPoint[1].x = vX + vWidth - 2;
1035 vPoint[1].y = vY + vHeight - 2;
1036 ::GpiMove(m_hPS, &vPoint[0]);
1037 ::GpiBox( m_hPS
1038 ,lControl
1039 ,&vPoint[1]
1040 ,0L
1041 ,0L
1042 );
1043 }
1044 CalcBoundingBox(vX, vY);
1045 CalcBoundingBox(vX2, vY2);
1046 } // end of wxDC::DoDrawRectangle
1047
1048 void wxDC::DoDrawRoundedRectangle(
1049 wxCoord vX
1050 , wxCoord vY
1051 , wxCoord vWidth
1052 , wxCoord vHeight
1053 , double dRadius
1054 )
1055 {
1056 POINTL vPoint[2];
1057 LONG lControl;
1058
1059 vY = OS2Y(vY,vHeight);
1060
1061 wxCoord vX2 = (vX + vWidth);
1062 wxCoord vY2 = (vY + vHeight);
1063
1064 vPoint[0].x = vX;
1065 vPoint[0].y = YLOG2DEV(vY) - vHeight;
1066 vPoint[1].x = vX + vWidth;
1067 vPoint[1].y = vY;
1068 ::GpiMove(m_hPS, &vPoint[0]);
1069
1070 lControl = DRO_OUTLINEFILL; //DRO_FILL;
1071 if (m_brush.GetStyle() == wxTRANSPARENT)
1072 lControl = DRO_OUTLINE;
1073 ::GpiBox( m_hPS // handle to a presentation space
1074 ,DRO_OUTLINE // draw the box outline ? or ?
1075 ,&vPoint[1] // address of the corner
1076 ,(LONG)dRadius // horizontal corner radius
1077 ,(LONG)dRadius // vertical corner radius
1078 );
1079 CalcBoundingBox(vX, vY);
1080 CalcBoundingBox(vX2, vY2);
1081 } // end of wxDC::DoDrawRoundedRectangle
1082
1083 // Draw Ellipse within box (x,y) - (x+width, y+height)
1084 void wxDC::DoDrawEllipse(
1085 wxCoord vX
1086 , wxCoord vY
1087 , wxCoord vWidth
1088 , wxCoord vHeight
1089 )
1090 {
1091 POINTL vPtlPos; // Structure for current position
1092 FIXED vFxMult; // Multiplier for ellipse
1093 ARCPARAMS vArcp; // Structure for arc parameters
1094
1095 vY = OS2Y(vY,vHeight);
1096
1097 vArcp.lR = 0;
1098 vArcp.lQ = vHeight/2;
1099 vArcp.lP = vWidth/2;
1100 vArcp.lS = 0;
1101 ::GpiSetArcParams( m_hPS
1102 ,&vArcp
1103 ); // Sets parameters to default
1104 vPtlPos.x = vX + vWidth/2; // Loads x-coordinate
1105 vPtlPos.y = vY + vHeight/2; // Loads y-coordinate
1106 ::GpiMove( m_hPS
1107 ,&vPtlPos
1108 ); // Sets current position
1109 vFxMult = MAKEFIXED(1, 0); /* Sets multiplier */
1110
1111 //
1112 // DRO_FILL, DRO_OTLINEFILL - where to get
1113 //
1114 ::GpiFullArc( m_hPS
1115 ,DRO_OUTLINE
1116 ,vFxMult
1117 ); // Draws full arc with center at current position
1118
1119 wxCoord vX2 = (vX + vWidth);
1120 wxCoord vY2 = (vY + vHeight);
1121
1122 CalcBoundingBox(vX, vY);
1123 CalcBoundingBox(vX2, vY2);
1124 } // end of wxDC::DoDrawEllipse
1125
1126 void wxDC::DoDrawEllipticArc(
1127 wxCoord vX
1128 , wxCoord vY
1129 , wxCoord vWidth
1130 , wxCoord vHeight
1131 , double dSa
1132 , double dEa
1133 )
1134 {
1135 POINTL vPtlPos; // Structure for current position
1136 FIXED vFxMult; // Multiplier for ellipse
1137 ARCPARAMS vArcp; // Structure for arc parameters
1138 FIXED vFSa;
1139 FIXED vFSweepa; // Start angle, sweep angle
1140 double dIntPart;
1141 double dFractPart;
1142 double dRadius;
1143
1144 vY = OS2Y(vY,vHeight);
1145
1146 dFractPart = modf(dSa,&dIntPart);
1147 vFSa = MAKEFIXED((int)dIntPart, (int)(dFractPart * 0xffff) );
1148 dFractPart = modf(dEa - dSa, &dIntPart);
1149 vFSweepa = MAKEFIXED((int)dIntPart, (int)(dFractPart * 0xffff) );
1150
1151 //
1152 // Ellipse main axis (r,q), (p,s) with center at (0,0)
1153 //
1154 vArcp.lR = 0;
1155 vArcp.lQ = vHeight/2;
1156 vArcp.lP = vWidth/2;
1157 vArcp.lS = 0;
1158 ::GpiSetArcParams(m_hPS, &vArcp); // Sets parameters to default
1159 vPtlPos.x = vX + vWidth/2 * (1. + cos(DegToRad(dSa))); // Loads x-coordinate
1160 vPtlPos.y = vY + vHeight/2 * (1. + sin(DegToRad(dSa))); // Loads y-coordinate
1161 ::GpiMove(m_hPS, &vPtlPos); // Sets current position
1162
1163 //
1164 // May be not to the center ?
1165 //
1166 vPtlPos.x = vX + vWidth/2 ; // Loads x-coordinate
1167 vPtlPos.y = vY + vHeight/2; // Loads y-coordinate
1168 vFxMult = MAKEFIXED(1, 0); // Sets multiplier
1169
1170 //
1171 // DRO_FILL, DRO_OTLINEFILL - where to get
1172 //
1173 ::GpiPartialArc( m_hPS
1174 ,&vPtlPos
1175 ,vFxMult
1176 ,vFSa
1177 ,vFSweepa
1178 );
1179 wxCoord vX2 = (vX + vWidth);
1180 wxCoord vY2 = (vY + vHeight);
1181
1182 CalcBoundingBox(vX, vY);
1183 CalcBoundingBox(vX2, vY2);
1184 } // end of wxDC::DoDrawEllipticArc
1185
1186 void wxDC::DoDrawIcon(
1187 const wxIcon& rIcon
1188 , wxCoord vX
1189 , wxCoord vY
1190 )
1191 {
1192 //
1193 // Need to copy back into a bitmap. ::WinDrawPointer uses device coords
1194 // and I don't feel like figuring those out for scrollable windows so
1195 // just convert to a bitmap then let the DoDrawBitmap routing display it
1196 //
1197 if (rIcon.IsXpm())
1198 {
1199 DoDrawBitmap(rIcon.GetXpmSrc(), vX, vY, TRUE);
1200 }
1201 else
1202 {
1203 wxBitmap vBitmap(rIcon);
1204
1205 DoDrawBitmap(vBitmap, vX, vY, FALSE);
1206 }
1207 CalcBoundingBox(vX, vY);
1208 CalcBoundingBox(vX + rIcon.GetWidth(), vY + rIcon.GetHeight());
1209 } // end of wxDC::DoDrawIcon
1210
1211 void wxDC::DoDrawBitmap(
1212 const wxBitmap& rBmp
1213 , wxCoord vX
1214 , wxCoord vY
1215 , bool bUseMask
1216 )
1217 {
1218 if (!IsKindOf(CLASSINFO(wxPrinterDC)))
1219 {
1220 HBITMAP hBitmap = (HBITMAP)rBmp.GetHBITMAP();
1221 HBITMAP hBitmapOld;
1222
1223 vY = OS2Y(vY,rBmp.GetHeight());
1224
1225 //
1226 // Flip the picture as OS/2 is upside-down
1227 //
1228 POINTL vPoint[4] = { vX, vY + rBmp.GetHeight()
1229 ,vX + rBmp.GetWidth(), vY
1230 ,0, 0
1231 ,rBmp.GetWidth(), rBmp.GetHeight()
1232 };
1233 if (bUseMask)
1234 {
1235 wxMask* pMask = rBmp.GetMask();
1236
1237 if (pMask)
1238 {
1239 //
1240 // Need to imitate ::MaskBlt in windows.
1241 // 1) Extract the bits from from the bitmap.
1242 // 2) Extract the bits from the mask
1243 // 3) Using the mask bits do the following:
1244 // A) If the mask byte is 00 leave the bitmap byte alone
1245 // B) If the mask byte is FF copy the screen color into
1246 // bitmap byte
1247 // 4) Create a new bitmap and set its bits to the above result
1248 // 5) Blit this to the screen PS
1249 //
1250 HBITMAP hMask = (HBITMAP)pMask->GetMaskBitmap();
1251 HBITMAP hOldBitmap = NULLHANDLE;
1252 HBITMAP hNewBitmap = NULLHANDLE;
1253 unsigned char* pucBits; // buffer that will contain the bitmap data
1254 unsigned char* pucBitsMask; // buffer that will contain the mask data
1255 unsigned char* pucData; // pointer to use to traverse bitmap data
1256 unsigned char* pucDataMask; // pointer to use to traverse mask data
1257 LONG lHits;
1258 ERRORID vError;
1259 wxString sError;
1260
1261 //
1262 // The usual Memory context creation stuff
1263 //
1264 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
1265 SIZEL vSize = {0, 0};
1266 HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1267 HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
1268
1269 //
1270 // The usual bitmap header stuff
1271 //
1272 BITMAPINFOHEADER2 vHeader;
1273 BITMAPINFO2 vInfo;
1274
1275 memset(&vHeader, '\0', 16);
1276 vHeader.cbFix = 16;
1277 vHeader.cx = (ULONG)rBmp.GetWidth();
1278 vHeader.cy = (ULONG)rBmp.GetHeight();
1279 vHeader.cPlanes = 1L;
1280 vHeader.cBitCount = 24;
1281
1282 memset(&vInfo, '\0', 16);
1283 vInfo.cbFix = 16;
1284 vInfo.cx = (ULONG)rBmp.GetWidth();
1285 vInfo.cy = (ULONG)rBmp.GetHeight();
1286 vInfo.cPlanes = 1;
1287 vInfo.cBitCount = 24; // Set to desired count going in
1288
1289 //
1290 // Create the buffers for data....all wxBitmaps are 24 bit internally
1291 //
1292 int nBytesPerLine = rBmp.GetWidth() * 3;
1293 int nSizeDWORD = sizeof(DWORD);
1294 int nLineBoundary = nBytesPerLine % nSizeDWORD;
1295 int nPadding = 0;
1296 int i;
1297 int j;
1298 LONG lScans = 0L;
1299 LONG lColor = 0L;
1300
1301 //
1302 // Need to get a background color for mask blitting
1303 //
1304 if (IsKindOf(CLASSINFO(wxPaintDC)))
1305 {
1306 wxPaintDC* pPaintDC = wxDynamicCast(this, wxPaintDC);
1307
1308 lColor = pPaintDC->m_pCanvas->GetBackgroundColour().GetPixel();
1309 }
1310 else if (GetBrush() != wxNullBrush)
1311 lColor = GetBrush().GetColour().GetPixel();
1312 else
1313 lColor = m_textBackgroundColour.GetPixel();
1314
1315 //
1316 // Bitmap must be ina double-word alligned address so we may
1317 // have some padding to worry about
1318 //
1319 if (nLineBoundary > 0)
1320 {
1321 nPadding = nSizeDWORD - nLineBoundary;
1322 nBytesPerLine += nPadding;
1323 }
1324 pucBits = (unsigned char *)malloc(nBytesPerLine * rBmp.GetHeight());
1325 pucBitsMask = (unsigned char *)malloc(nBytesPerLine * rBmp.GetHeight());
1326 memset(pucBits, '\0', (nBytesPerLine * rBmp.GetHeight()));
1327 memset(pucBitsMask, '\0', (nBytesPerLine * rBmp.GetHeight()));
1328
1329 //
1330 // Extract the bitmap and mask data
1331 //
1332 if ((hOldBitmap = ::GpiSetBitmap(hPS, hBitmap)) == HBM_ERROR)
1333 {
1334 vError = ::WinGetLastError(vHabmain);
1335 sError = wxPMErrorToStr(vError);
1336 }
1337 if ((lScans = ::GpiQueryBitmapBits( hPS
1338 ,0L
1339 ,(LONG)rBmp.GetHeight()
1340 ,(PBYTE)pucBits
1341 ,&vInfo
1342 )) == GPI_ALTERROR)
1343 {
1344 vError = ::WinGetLastError(vHabmain);
1345 sError = wxPMErrorToStr(vError);
1346 }
1347 if ((hOldBitmap = ::GpiSetBitmap(hPS, hMask)) == HBM_ERROR)
1348 {
1349 vError = ::WinGetLastError(vHabmain);
1350 sError = wxPMErrorToStr(vError);
1351 }
1352 if ((lScans = ::GpiQueryBitmapBits( hPS
1353 ,0L
1354 ,(LONG)rBmp.GetHeight()
1355 ,(PBYTE)pucBitsMask
1356 ,&vInfo
1357 )) == GPI_ALTERROR)
1358 {
1359 vError = ::WinGetLastError(vHabmain);
1360 sError = wxPMErrorToStr(vError);
1361 }
1362
1363 //
1364 // Now set the bytes(bits) according to the mask values
1365 // 3 bytes per pel...must handle one at a time
1366 //
1367 pucData = pucBits;
1368 pucDataMask = pucBitsMask;
1369
1370 for (i = 0; i < rBmp.GetHeight(); i++)
1371 {
1372 for (j = 0; j < rBmp.GetWidth(); j++)
1373 {
1374 // Byte 1
1375 if (*pucDataMask == 0xFF) // leave bitmap byte alone
1376 pucData++;
1377 else
1378 {
1379 *pucData = ((unsigned char)(lColor >> 16));
1380 pucData++;
1381 }
1382 // Byte 2
1383 if (*(pucDataMask + 1) == 0xFF) // leave bitmap byte alone
1384 pucData++;
1385 else
1386 {
1387 *pucData = ((unsigned char)(lColor >> 8));
1388 pucData++;
1389 }
1390
1391 // Byte 3
1392 if (*(pucDataMask + 2) == 0xFF) // leave bitmap byte alone
1393 pucData++;
1394 else
1395 {
1396 *pucData = ((unsigned char)lColor);
1397 pucData++;
1398 }
1399 pucDataMask += 3;
1400 }
1401 for (j = 0; j < nPadding; j++)
1402 {
1403 pucData++;
1404 pucDataMask++;
1405 }
1406 }
1407 //
1408 // Create a new bitmap
1409 //
1410 if ((hNewBitmap = ::GpiCreateBitmap( hPS
1411 ,&vHeader
1412 ,CBM_INIT
1413 ,(PBYTE)pucBits
1414 ,&vInfo
1415 )) == GPI_ERROR)
1416 {
1417 vError = ::WinGetLastError(vHabmain);
1418 sError = wxPMErrorToStr(vError);
1419 }
1420
1421 //
1422 // Now blit it to the screen PS
1423 //
1424 if ((lHits = ::GpiWCBitBlt( (HPS)GetHPS()
1425 ,hNewBitmap
1426 ,4
1427 ,vPoint
1428 ,ROP_SRCCOPY
1429 ,BBO_IGNORE
1430 )) == GPI_ERROR)
1431 {
1432 vError = ::WinGetLastError(vHabmain);
1433 sError = wxPMErrorToStr(vError);
1434 }
1435
1436 //
1437 // Clean up
1438 //
1439 free(pucBits);
1440 free(pucBitsMask);
1441 ::GpiSetBitmap(hPS, NULLHANDLE);
1442 ::GpiDestroyPS(hPS);
1443 ::DevCloseDC(hDC);
1444 }
1445 }
1446 else
1447 {
1448 LONG lOldForeGround = ::GpiQueryColor((HPS)GetHPS());
1449 LONG lOldBackGround = ::GpiQueryBackColor((HPS)GetHPS());
1450
1451 if (m_textForegroundColour.Ok())
1452 {
1453 ::GpiSetColor( (HPS)GetHPS()
1454 ,m_textForegroundColour.GetPixel()
1455 );
1456 }
1457 if (m_textBackgroundColour.Ok())
1458 {
1459 ::GpiSetBackColor( (HPS)GetHPS()
1460 ,m_textBackgroundColour.GetPixel()
1461 );
1462 }
1463 //
1464 // Need to alter bits in a mono bitmap to match the new
1465 // background-foreground if it is different.
1466 //
1467 if (rBmp.IsMono() &&
1468 ((m_textForegroundColour.GetPixel() != lOldForeGround) ||
1469 (m_textBackgroundColour.GetPixel() != lOldBackGround)))
1470 {
1471 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
1472 SIZEL vSize = {0, 0};
1473 HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1474 HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
1475
1476 int nBytesPerLine = rBmp.GetWidth() * 3;
1477 int i, j;
1478 LONG lForeGround = m_textForegroundColour.GetPixel();
1479 LONG lBackGround = m_textBackgroundColour.GetPixel();
1480 LONG lScans;
1481 HBITMAP hOldBitmap = NULLHANDLE;
1482 BITMAPINFO2 vInfo;
1483 ERRORID vError;
1484 wxString sError;
1485
1486
1487 memset(&vInfo, '\0', 16);
1488 vInfo.cbFix = 16;
1489 vInfo.cx = (ULONG)rBmp.GetWidth();
1490 vInfo.cy = (ULONG)rBmp.GetHeight();
1491 vInfo.cPlanes = 1;
1492 vInfo.cBitCount = 24;
1493
1494 unsigned char* pucBits; // buffer that will contain the bitmap data
1495 unsigned char* pucData; // pointer to use to traverse bitmap data
1496
1497 pucBits = new unsigned char[nBytesPerLine * rBmp.GetHeight()];
1498 memset(pucBits, '\0', (nBytesPerLine * rBmp.GetHeight()));
1499
1500 if ((hOldBitmap = ::GpiSetBitmap(hPS, hBitmap)) == HBM_ERROR)
1501 {
1502 vError = ::WinGetLastError(vHabmain);
1503 sError = wxPMErrorToStr(vError);
1504 return;
1505 }
1506 if ((lScans = ::GpiQueryBitmapBits( hPS
1507 ,0L
1508 ,(LONG)rBmp.GetHeight()
1509 ,(PBYTE)pucBits
1510 ,&vInfo
1511 )) == GPI_ALTERROR)
1512 {
1513 vError = ::WinGetLastError(vHabmain);
1514 sError = wxPMErrorToStr(vError);
1515 return;
1516 }
1517 unsigned char cOldRedFore = (unsigned char)(lOldForeGround >> 16);
1518 unsigned char cOldGreenFore = (unsigned char)(lOldForeGround >> 8);
1519 unsigned char cOldBlueFore = (unsigned char)lOldForeGround;
1520
1521 unsigned char cOldRedBack = (unsigned char)(lOldBackGround >> 16);
1522 unsigned char cOldGreenBack = (unsigned char)(lOldBackGround >> 8);
1523 unsigned char cOldBlueBack = (unsigned char)lOldBackGround;
1524
1525 unsigned char cRedFore = (unsigned char)(lForeGround >> 16);
1526 unsigned char cGreenFore = (unsigned char)(lForeGround >> 8);
1527 unsigned char cBlueFore = (unsigned char)lForeGround;
1528
1529 unsigned char cRedBack = (unsigned char)(lBackGround >> 16);
1530 unsigned char cGreenBack = (unsigned char)(lBackGround >> 8);
1531 unsigned char cBlueBack = (unsigned char)lBackGround;
1532
1533 pucData = pucBits;
1534 for (i = 0; i < rBmp.GetHeight(); i++)
1535 {
1536 for (j = 0; j < rBmp.GetWidth(); j++)
1537 {
1538 unsigned char cBmpRed = *pucData;
1539 unsigned char cBmpGreen = *(pucData + 1);
1540 unsigned char cBmpBlue = *(pucData + 2);
1541
1542 if ((cBmpRed == cOldRedFore) &&
1543 (cBmpGreen == cOldGreenFore) &&
1544 (cBmpBlue == cOldBlueFore))
1545 {
1546 *pucData = cRedFore;
1547 pucData++;
1548 *pucData = cGreenFore;
1549 pucData++;
1550 *pucData = cBlueFore;
1551 pucData++;
1552 }
1553 else
1554 {
1555 *pucData = cRedBack;
1556 pucData++;
1557 *pucData = cGreenBack;
1558 pucData++;
1559 *pucData = cBlueBack;
1560 pucData++;
1561 }
1562 }
1563 }
1564 if ((lScans = ::GpiSetBitmapBits( hPS
1565 ,0L
1566 ,(LONG)rBmp.GetHeight()
1567 ,(PBYTE)pucBits
1568 ,&vInfo
1569 )) == GPI_ALTERROR)
1570 {
1571 vError = ::WinGetLastError(vHabmain);
1572 sError = wxPMErrorToStr(vError);
1573 return;
1574 }
1575 delete [] pucBits;
1576 ::GpiSetBitmap(hPS, NULLHANDLE);
1577 ::GpiDestroyPS(hPS);
1578 ::DevCloseDC(hDC);
1579 }
1580 ::GpiWCBitBlt( (HPS)GetHPS()
1581 ,hBitmap
1582 ,4
1583 ,vPoint
1584 ,ROP_SRCCOPY
1585 ,BBO_IGNORE
1586 );
1587 ::GpiSetBitmap((HPS)GetHPS(), hBitmapOld);
1588 ::GpiSetColor((HPS)GetHPS(), lOldForeGround);
1589 ::GpiSetBackColor((HPS)GetHPS(), lOldBackGround);
1590 }
1591 }
1592 } // end of wxDC::DoDrawBitmap
1593
1594 void wxDC::DoDrawText(
1595 const wxString& rsText
1596 , wxCoord vX
1597 , wxCoord vY
1598 )
1599 {
1600 wxCoord vWidth;
1601 wxCoord vHeight;
1602
1603 DrawAnyText( rsText
1604 ,vX
1605 ,vY
1606 );
1607
1608 CalcBoundingBox(vX, vY);
1609 GetTextExtent(rsText, &vWidth, &vHeight);
1610 CalcBoundingBox((vX + vWidth), (vY + vHeight));
1611 } // end of wxDC::DoDrawText
1612
1613 void wxDC::DrawAnyText(
1614 const wxString& rsText
1615 , wxCoord vX
1616 , wxCoord vY
1617 )
1618 {
1619 int nOldBackground = 0;
1620 POINTL vPtlStart;
1621 LONG lHits;
1622 wxCoord vTextX = 0;
1623 wxCoord vTextY = 0;
1624
1625 //
1626 // prepare for drawing the text
1627 //
1628
1629 //
1630 // Set text color attributes
1631 //
1632 if (m_textForegroundColour.Ok())
1633 {
1634 SetTextColor( m_hPS
1635 ,(int)m_textForegroundColour.GetPixel()
1636 );
1637 }
1638
1639 if (m_textBackgroundColour.Ok())
1640 {
1641 nOldBackground = SetTextBkColor( m_hPS
1642 ,(int)m_textBackgroundColour.GetPixel()
1643 );
1644 }
1645 SetBkMode( m_hPS
1646 ,m_backgroundMode
1647 );
1648 GetTextExtent( rsText
1649 ,&vTextX
1650 ,&vTextY
1651 );
1652 vPtlStart.x = vX;
1653 vPtlStart.y = OS2Y(vY,vTextY);
1654
1655 lHits = ::GpiCharStringAt( m_hPS
1656 ,&vPtlStart
1657 ,rsText.length()
1658 ,(PCH)rsText.c_str()
1659 );
1660 if (lHits != GPI_OK)
1661 {
1662 wxLogLastError(wxT("TextOut"));
1663 }
1664
1665 //
1666 // Restore the old parameters (text foreground colour may be left because
1667 // it never is set to anything else, but background should remain
1668 // transparent even if we just drew an opaque string)
1669 //
1670 if (m_textBackgroundColour.Ok())
1671 SetTextBkColor( m_hPS
1672 ,nOldBackground
1673 );
1674 SetBkMode( m_hPS
1675 ,wxTRANSPARENT
1676 );
1677 }
1678
1679 void wxDC::DoDrawRotatedText(
1680 const wxString& rsText
1681 , wxCoord vX
1682 , wxCoord vY
1683 , double dAngle
1684 )
1685 {
1686 if (dAngle == 0.0)
1687 {
1688 DoDrawText( rsText
1689 ,vX
1690 ,vY
1691 );
1692 }
1693
1694 // TODO:
1695 /*
1696 if ( angle == 0.0 )
1697 {
1698 DoDrawText(text, x, y);
1699 }
1700 else
1701 {
1702 LOGFONT lf;
1703 wxFillLogFont(&lf, &m_font);
1704
1705 // GDI wants the angle in tenth of degree
1706 long angle10 = (long)(angle * 10);
1707 lf.lfEscapement = angle10;
1708 lf. lfOrientation = angle10;
1709
1710 HFONT hfont = ::CreateFontIndirect(&lf);
1711 if ( !hfont )
1712 {
1713 wxLogLastError("CreateFont");
1714 }
1715 else
1716 {
1717 HFONT hfontOld = ::SelectObject(GetHdc(), hfont);
1718
1719 DrawAnyText(text, x, y);
1720
1721 (void)::SelectObject(GetHdc(), hfontOld);
1722 }
1723
1724 // call the bounding box by adding all four vertices of the rectangle
1725 // containing the text to it (simpler and probably not slower than
1726 // determining which of them is really topmost/leftmost/...)
1727 wxCoord w, h;
1728 GetTextExtent(text, &w, &h);
1729
1730 double rad = DegToRad(angle);
1731
1732 // "upper left" and "upper right"
1733 CalcBoundingBox(x, y);
1734 CalcBoundingBox(x + w*cos(rad), y - h*sin(rad));
1735 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
1736
1737 // "bottom left" and "bottom right"
1738 x += (wxCoord)(h*sin(rad));
1739 y += (wxCoord)(h*cos(rad));
1740 CalcBoundingBox(x, y);
1741 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
1742 }
1743 */
1744 }
1745
1746 // ---------------------------------------------------------------------------
1747 // set GDI objects
1748 // ---------------------------------------------------------------------------
1749
1750 void wxDC::DoSelectPalette(
1751 bool bRealize
1752 )
1753 {
1754 //
1755 // Set the old object temporarily, in case the assignment deletes an object
1756 // that's not yet selected out.
1757 //
1758 if (m_hOldPalette)
1759 {
1760 m_hOldPalette = 0;
1761 }
1762
1763 if (m_palette.Ok())
1764 {
1765 HPALETTE hOldPal;
1766
1767 hOldPal = ::GpiSelectPalette((HDC) m_hPS, (HPALETTE) m_palette.GetHPALETTE());
1768 if (!m_hOldPalette)
1769 m_hOldPalette = (WXHPALETTE)hOldPal;
1770 }
1771 } // end of wxDC::DoSelectPalette
1772
1773 void wxDC::InitializePalette()
1774 {
1775 if (wxDisplayDepth() <= 8 )
1776 {
1777 //
1778 // Look for any window or parent that has a custom palette. If any has
1779 // one then we need to use it in drawing operations
1780 //
1781 wxWindow* pWin = m_pCanvas->GetAncestorWithCustomPalette();
1782
1783 m_hasCustomPalette = pWin && pWin->HasCustomPalette();
1784 if (m_hasCustomPalette)
1785 {
1786 m_palette = pWin->GetPalette();
1787
1788 //
1789 // turn on PM translation for this palette
1790 //
1791 DoSelectPalette();
1792 }
1793 }
1794 } // end of wxDC::InitializePalette
1795
1796 void wxDC::SetPalette(
1797 const wxPalette& rPalette
1798 )
1799 {
1800 if (m_hOldFont)
1801 {
1802 m_hOldFont = 0;
1803 }
1804 m_palette = rPalette;
1805 if (!rPalette.Ok())
1806 {
1807 if (m_hOldFont)
1808 {
1809 m_hOldFont = 0;
1810 }
1811 }
1812 HPALETTE hOldPal = ::GpiSelectPalette((HDC) m_hPS, (HPALETTE) m_palette.GetHPALETTE());
1813 if (!m_hOldPalette)
1814 m_hOldPalette = (WXHPALETTE)hOldPal;
1815 } // end of wxDC::SetPalette
1816
1817 void wxDC::SetFont(
1818 const wxFont& rFont
1819 )
1820 {
1821 //
1822 // Set the old object temporarily, in case the assignment deletes an object
1823 // that's not yet selected out.
1824 //
1825 if (m_hOldFont)
1826 {
1827 m_hOldFont = 0;
1828 }
1829 m_font = rFont;
1830 if (!rFont.Ok())
1831 {
1832 m_hOldFont = 0;
1833 }
1834
1835 m_font.SetPS(m_hPS); // this will realize the font
1836
1837 if (m_font.Ok())
1838 {
1839 HFONT hFont = m_font.GetResourceHandle();
1840 if (hFont == (HFONT) NULL)
1841 {
1842 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
1843 }
1844 if (!m_hOldFont)
1845 m_hOldFont = (WXHFONT) hFont;
1846 }
1847 } // end of wxDC::SetFont
1848
1849 void wxDC::SetPen(
1850 const wxPen& rPen
1851 )
1852 {
1853 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1854
1855 if (m_pen == rPen)
1856 return;
1857 m_pen = rPen;
1858 if (!m_pen.Ok())
1859 return;
1860
1861 if (m_hOldPen)
1862 m_hOldPen = 0L;
1863 m_pen = rPen;
1864
1865 if (!m_pen.Ok())
1866 {
1867 if (m_hOldPen)
1868 {
1869 m_pen.SetPS((HPS)m_hOldPen);
1870 }
1871 m_hOldPen = 0L;
1872 }
1873
1874 if (m_pen.Ok())
1875 {
1876 if (m_pen.GetResourceHandle())
1877 {
1878 m_pen.SetPS(m_hPS);
1879 if (!m_hOldPen)
1880 m_hOldPen = m_pen.GetPS();
1881 }
1882 }
1883 }
1884
1885 void wxDC::SetBrush(
1886 const wxBrush& rBrush
1887 )
1888 {
1889 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1890
1891 if (m_hOldBrush)
1892 m_hOldBrush = 0L;
1893 m_brush = rBrush;
1894 if (!m_brush.Ok())
1895 if (m_brush == rBrush)
1896 return;
1897 if (!m_brush.Ok())
1898 if (m_hOldBrush)
1899 m_hOldBrush = 0L;
1900
1901 if (!m_brush.Ok())
1902 {
1903 if (m_hOldBrush)
1904 {
1905 m_brush.SetPS((HPS)m_hOldBrush);
1906 }
1907 m_hOldBrush = 0L;
1908 }
1909
1910 if (m_brush.Ok())
1911 {
1912 if (m_brush.GetResourceHandle())
1913 {
1914 m_brush.SetPS(m_hPS);
1915 if (!m_hOldBrush)
1916 m_hOldBrush = (WXHWND)m_brush.GetPS();
1917 }
1918 }
1919 } // end of wxDC::SetBrush
1920
1921 void wxDC::SetBackground(
1922 const wxBrush& rBrush
1923 )
1924 {
1925 m_backgroundBrush = rBrush;
1926 if (!m_backgroundBrush.Ok())
1927 return;
1928 if (m_pCanvas)
1929 {
1930 bool bCustomColours = TRUE;
1931
1932 //
1933 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1934 // change background colours from the control-panel specified colours.
1935 //
1936 if (m_pCanvas->IsKindOf(CLASSINFO(wxWindow)) &&
1937 ((m_pCanvas->GetWindowStyleFlag() & wxUSER_COLOURS) != wxUSER_COLOURS))
1938 bCustomColours = FALSE;
1939 if (bCustomColours)
1940 {
1941 if (m_backgroundBrush.GetStyle()==wxTRANSPARENT)
1942 {
1943 m_pCanvas->SetTransparent(TRUE);
1944 }
1945 else
1946 {
1947 //
1948 // Setting the background brush of a DC
1949 // doesn't affect the window background colour. However,
1950 // I'm leaving in the transparency setting because it's needed by
1951 // various controls (e.g. wxStaticText) to determine whether to draw
1952 // transparently or not. TODO: maybe this should be a new function
1953 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1954 // parent?
1955 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1956 //
1957 m_pCanvas->SetTransparent(FALSE);
1958 }
1959 }
1960 }
1961 COLORREF vNewColor = m_backgroundBrush.GetColour().GetPixel();
1962 (void)::GpiSetBackColor((HPS)m_hPS, (LONG)vNewColor);
1963 } // end of wxDC::SetBackground
1964
1965 void wxDC::SetBackgroundMode(
1966 int nMode
1967 )
1968 {
1969 m_backgroundMode = nMode;
1970 } // end of wxDC::SetBackgroundMode
1971
1972 void wxDC::SetLogicalFunction(
1973 int nFunction
1974 )
1975 {
1976 m_logicalFunction = nFunction;
1977 SetRop((WXHDC)m_hDC);
1978 } // wxDC::SetLogicalFunction
1979
1980 void wxDC::SetRop(
1981 WXHDC hDC
1982 )
1983 {
1984 if (!hDC || m_logicalFunction < 0)
1985 return;
1986
1987 LONG lCRop;
1988 switch (m_logicalFunction)
1989 {
1990 case wxXOR:
1991 lCRop = FM_XOR;
1992 break;
1993
1994 case wxINVERT:
1995 lCRop = FM_INVERT;
1996 break;
1997
1998 case wxOR_REVERSE:
1999 lCRop = FM_MERGESRCNOT;
2000 break;
2001
2002 case wxAND_REVERSE:
2003 lCRop = FM_NOTMASKSRC;
2004 break;
2005
2006 case wxCLEAR:
2007 lCRop = FM_ONE;
2008 break;
2009
2010 case wxSET:
2011 lCRop = FM_ZERO;
2012 break;
2013
2014 case wxSRC_INVERT:
2015 lCRop = FM_MERGENOTSRC;
2016 break;
2017
2018 case wxOR_INVERT:
2019 lCRop = FM_MERGESRCNOT;
2020 break;
2021
2022 case wxAND:
2023 lCRop = FM_AND;
2024 break;
2025
2026 case wxOR:
2027 lCRop = FM_OR;
2028 break;
2029
2030 case wxAND_INVERT:
2031 lCRop = FM_SUBTRACT;
2032 break;
2033
2034 case wxEQUIV:
2035 case wxNAND:
2036 case wxCOPY:
2037 default:
2038 lCRop = FM_OVERPAINT;
2039 break;
2040 }
2041 ::GpiSetMix((HPS)hDC, lCRop);
2042 } // end of wxDC::SetRop
2043
2044 bool wxDC::StartDoc(
2045 const wxString& rsMessage
2046 )
2047 {
2048 // We might be previewing, so return TRUE to let it continue.
2049 return TRUE;
2050 } // end of wxDC::StartDoc
2051
2052 void wxDC::EndDoc()
2053 {
2054 } // end of wxDC::EndDoc
2055
2056 void wxDC::StartPage()
2057 {
2058 } // end of wxDC::StartPage
2059
2060 void wxDC::EndPage()
2061 {
2062 } // end of wxDC::EndPage
2063
2064 // ---------------------------------------------------------------------------
2065 // text metrics
2066 // ---------------------------------------------------------------------------
2067
2068 wxCoord wxDC::GetCharHeight() const
2069 {
2070 FONTMETRICS vFM; // metrics structure
2071
2072 ::GpiQueryFontMetrics( m_hPS
2073 ,sizeof(FONTMETRICS)
2074 ,&vFM
2075 );
2076 return YDEV2LOGREL(vFM.lXHeight);
2077 }
2078
2079 wxCoord wxDC::GetCharWidth() const
2080 {
2081 FONTMETRICS vFM; // metrics structure
2082
2083 ::GpiQueryFontMetrics( m_hPS
2084 ,sizeof(FONTMETRICS)
2085 ,&vFM
2086 );
2087 return XDEV2LOGREL(vFM.lAveCharWidth);
2088 }
2089
2090 void wxDC::DoGetTextExtent(
2091 const wxString& rsString
2092 , wxCoord* pvX
2093 , wxCoord* pvY
2094 , wxCoord* pvDescent
2095 , wxCoord* pvExternalLeading
2096 , wxFont* pTheFont
2097 ) const
2098 {
2099 POINTL avPoint[TXTBOX_COUNT];
2100 POINTL vPtMin;
2101 POINTL vPtMax;
2102 int i;
2103 int l;
2104 FONTMETRICS vFM; // metrics structure
2105 BOOL bRc;
2106 char* pStr;
2107 ERRORID vErrorCode; // last error id code
2108 wxFont* pFontToUse = (wxFont*)pTheFont;
2109
2110 char zMsg[128]; // DEBUG
2111 wxString sError;
2112
2113 if (!pFontToUse)
2114 pFontToUse = (wxFont*)&m_font;
2115 l = rsString.length();
2116 pStr = (PCH) rsString.c_str();
2117
2118 //
2119 // In world coordinates.
2120 //
2121 bRc = ::GpiQueryTextBox( m_hPS
2122 ,l
2123 ,pStr
2124 ,TXTBOX_COUNT // return maximum information
2125 ,avPoint // array of coordinates points
2126 );
2127 if(!bRc)
2128 {
2129 vErrorCode = ::WinGetLastError(wxGetInstance());
2130 sError = wxPMErrorToStr(vErrorCode);
2131 // DEBUG
2132 sprintf(zMsg, "GpiQueryTextBox for %s: failed with Error: %x - %s", pStr, vErrorCode, sError.c_str());
2133 (void)wxMessageBox( "wxWindows Menu sample"
2134 ,zMsg
2135 ,wxICON_INFORMATION
2136 );
2137 }
2138
2139 vPtMin.x = avPoint[0].x;
2140 vPtMax.x = avPoint[0].x;
2141 vPtMin.y = avPoint[0].y;
2142 vPtMax.y = avPoint[0].y;
2143 for (i = 1; i < 4; i++)
2144 {
2145 if(vPtMin.x > avPoint[i].x) vPtMin.x = avPoint[i].x;
2146 if(vPtMin.y > avPoint[i].y) vPtMin.y = avPoint[i].y;
2147 if(vPtMax.x < avPoint[i].x) vPtMax.x = avPoint[i].x;
2148 if(vPtMax.y < avPoint[i].y) vPtMax.y = avPoint[i].y;
2149 }
2150 ::GpiQueryFontMetrics( m_hPS
2151 ,sizeof(FONTMETRICS)
2152 ,&vFM
2153 );
2154
2155 if (pvX)
2156 *pvX = (wxCoord)(vPtMax.x - vPtMin.x + 1);
2157 if (pvY)
2158 *pvY = (wxCoord)(vPtMax.y - vPtMin.y + 1);
2159 if (pvDescent)
2160 *pvDescent = vFM.lMaxDescender;
2161 if (pvExternalLeading)
2162 *pvExternalLeading = vFM.lExternalLeading;
2163 }
2164
2165 void wxDC::SetMapMode(
2166 int nMode
2167 )
2168 {
2169 int nPixelWidth = 0;
2170 int nPixelHeight = 0;
2171 int nMmWidth = 1;
2172 int nMmHeight = 1;
2173 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2174
2175 m_mappingMode = nMode;
2176
2177 if(::DevQueryCaps( m_hDC
2178 ,CAPS_FAMILY
2179 ,CAPS_VERTICAL_RESOLUTION
2180 ,lArray
2181 ))
2182 {
2183 LONG lHorzRes;
2184 LONG lVertRes;
2185
2186 nPixelWidth = lArray[CAPS_WIDTH];
2187 nPixelHeight = lArray[CAPS_HEIGHT];
2188 lHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2189 lVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2190 nMmWidth = (lHorzRes/1000) * nPixelWidth;
2191 nMmWidth = (lVertRes/1000) * nPixelHeight;
2192 }
2193 if ((nPixelWidth == 0) || (nPixelHeight == 0) || (nMmWidth == 0) || (nMmHeight == 0))
2194 {
2195 return;
2196 }
2197
2198 double dMm2pixelsX = nPixelWidth/nMmWidth;
2199 double dMm2pixelsY = nPixelHeight/nMmHeight;
2200
2201 switch (nMode)
2202 {
2203 case wxMM_TWIPS:
2204 m_logicalScaleX = (twips2mm * dMm2pixelsX);
2205 m_logicalScaleY = (twips2mm * dMm2pixelsY);
2206 break;
2207
2208 case wxMM_POINTS:
2209 m_logicalScaleX = (pt2mm * dMm2pixelsX);
2210 m_logicalScaleY = (pt2mm * dMm2pixelsY);
2211 break;
2212
2213 case wxMM_METRIC:
2214 m_logicalScaleX = dMm2pixelsX;
2215 m_logicalScaleY = dMm2pixelsY;
2216 break;
2217
2218 case wxMM_LOMETRIC:
2219 m_logicalScaleX = (dMm2pixelsX/10.0);
2220 m_logicalScaleY = (dMm2pixelsY/10.0);
2221 break;
2222
2223 case wxMM_TEXT:
2224 default:
2225 m_logicalScaleX = 1.0;
2226 m_logicalScaleY = 1.0;
2227 break;
2228 }
2229 SIZEL vSize;
2230 ULONG ulOptions;
2231
2232 ulOptions = ::GpiQueryPS(m_hPS, &vSize);
2233 if (!ulOptions & PU_ARBITRARY)
2234 {
2235 ulOptions = PU_ARBITRARY | GPIF_DEFAULT;
2236 ::GpiSetPS(m_hPS, &vSize, ulOptions);
2237 }
2238 m_nWindowExtX = (int)MS_XDEV2LOG(VIEWPORT_EXTENT);
2239 m_nWindowExtY = (int)MS_YDEV2LOG(VIEWPORT_EXTENT);
2240 // ????
2241 }; // end of wxDC::SetMapMode
2242
2243 void wxDC::SetUserScale(
2244 double dX
2245 , double dY
2246 )
2247 {
2248 m_userScaleX = dX;
2249 m_userScaleY = dY;
2250
2251 SetMapMode(m_mappingMode);
2252 } // end of wxDC::SetUserScale
2253
2254 void wxDC::SetAxisOrientation(
2255 bool bXLeftRight
2256 , bool bYBottomUp
2257 )
2258 {
2259 m_signX = bXLeftRight ? 1 : -1;
2260 m_signY = bYBottomUp ? -1 : 1;
2261
2262 SetMapMode(m_mappingMode);
2263 } // end of wxDC::SetAxisOrientation
2264
2265 void wxDC::SetSystemScale(
2266 double dX
2267 , double dY
2268 )
2269 {
2270 m_scaleX = dX;
2271 m_scaleY = dY;
2272
2273 SetMapMode(m_mappingMode);
2274 } // end of wxDC::SetSystemScale
2275
2276 void wxDC::SetLogicalOrigin(
2277 wxCoord vX
2278 , wxCoord vY
2279 )
2280 {
2281 RECTL vRect;
2282
2283 ::GpiQueryPageViewport( m_hPS
2284 ,&vRect
2285 );
2286 vRect.xRight -= vX;
2287 vRect.yTop += vY;
2288 vRect.xLeft = vX;
2289 vRect.yBottom = vY;
2290 ::GpiSetPageViewport( m_hPS
2291 ,&vRect
2292 );
2293 }; // end of wxDC::SetLogicalOrigin
2294
2295 void wxDC::SetDeviceOrigin(
2296 wxCoord vX
2297 , wxCoord vY
2298 )
2299 {
2300 RECTL vRect;
2301
2302 m_deviceOriginX = vX;
2303 m_deviceOriginY = vY;
2304 ::GpiQueryPageViewport( m_hPS
2305 ,&vRect
2306 );
2307 vRect.xLeft += vX;
2308 vRect.xRight += vX;
2309 vRect.yBottom -= vY;
2310 vRect.yTop -= vY;
2311 ::GpiSetPageViewport( m_hPS
2312 ,&vRect
2313 );
2314 }; // end of wxDC::SetDeviceOrigin
2315
2316 // ---------------------------------------------------------------------------
2317 // coordinates transformations
2318 // ---------------------------------------------------------------------------
2319
2320 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2321 {
2322 return (wxCoord) (((x) - m_deviceOriginX)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX) - m_logicalOriginX);
2323 }
2324
2325 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2326 {
2327 // axis orientation is not taken into account for conversion of a distance
2328 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_scaleX));
2329 }
2330
2331 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2332 {
2333 return (wxCoord) (((y) - m_deviceOriginY)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY) - m_logicalOriginY);
2334 }
2335
2336 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2337 {
2338 // axis orientation is not taken into account for conversion of a distance
2339 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_scaleY));
2340 }
2341
2342 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2343 {
2344 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
2345 }
2346
2347 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2348 {
2349 // axis orientation is not taken into account for conversion of a distance
2350 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_scaleX);
2351 }
2352
2353 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2354 {
2355 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
2356 }
2357
2358 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2359 {
2360 // axis orientation is not taken into account for conversion of a distance
2361 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_scaleY);
2362 }
2363
2364 // ---------------------------------------------------------------------------
2365 // bit blit
2366 // ---------------------------------------------------------------------------
2367
2368 bool wxDC::DoBlit(
2369 wxCoord vXdest
2370 , wxCoord vYdest
2371 , wxCoord vWidth
2372 , wxCoord vHeight
2373 , wxDC* pSource
2374 , wxCoord vXsrc
2375 , wxCoord vYsrc
2376 , int nRop
2377 , bool bUseMask
2378 , wxCoord vXsrcMask
2379 , wxCoord vYsrcMask
2380 )
2381 {
2382 wxMask* pMask = NULL;
2383 CHARBUNDLE vCbnd;
2384 COLORREF vOldTextColor;
2385 COLORREF vOldBackground = ::GpiQueryBackColor(m_hPS);
2386
2387 if (bUseMask)
2388 {
2389 const wxBitmap& rBmp = pSource->m_vSelectedBitmap;
2390
2391 pMask = rBmp.GetMask();
2392 if (!(rBmp.Ok() && pMask && pMask->GetMaskBitmap()))
2393 {
2394 bUseMask = FALSE;
2395 }
2396 }
2397
2398 ::GpiQueryAttrs( m_hPS
2399 ,PRIM_CHAR
2400 ,CBB_COLOR
2401 ,&vCbnd
2402 );
2403 vOldTextColor = (COLORREF)vCbnd.lColor;
2404
2405 if (m_textForegroundColour.Ok())
2406 {
2407 vCbnd.lColor = (LONG)m_textForegroundColour.GetPixel();
2408 ::GpiSetAttrs( m_hPS // presentation-space handle
2409 ,PRIM_CHAR // Char primitive.
2410 ,CBB_COLOR // sets color.
2411 ,0
2412 ,&vCbnd // buffer for attributes.
2413 );
2414 }
2415 if (m_textBackgroundColour.Ok())
2416 {
2417 ::GpiSetBackColor(m_hPS, (LONG)m_textBackgroundColour.GetPixel());
2418 }
2419
2420 LONG lRop = ROP_SRCCOPY;
2421
2422 switch (nRop)
2423 {
2424 case wxXOR: lRop = ROP_SRCINVERT; break;
2425 case wxINVERT: lRop = ROP_DSTINVERT; break;
2426 case wxOR_REVERSE: lRop = 0x00DD0228; break;
2427 case wxAND_REVERSE: lRop = ROP_SRCERASE; break;
2428 case wxCLEAR: lRop = ROP_ZERO; break;
2429 case wxSET: lRop = ROP_ONE; break;
2430 case wxOR_INVERT: lRop = ROP_MERGEPAINT; break;
2431 case wxAND: lRop = ROP_SRCAND; break;
2432 case wxOR: lRop = ROP_SRCPAINT; break;
2433 case wxEQUIV: lRop = 0x00990066; break;
2434 case wxNAND: lRop = 0x007700E6; break;
2435 case wxAND_INVERT: lRop = 0x00220326; break;
2436 case wxCOPY: lRop = ROP_SRCCOPY; break;
2437 case wxNO_OP: lRop = ROP_NOTSRCERASE; break;
2438 case wxSRC_INVERT: lRop = ROP_SRCINVERT; break;
2439 case wxNOR: lRop = ROP_NOTSRCCOPY; break;
2440 default:
2441 wxFAIL_MSG( wxT("unsupported logical function") );
2442 return FALSE;
2443 }
2444
2445 bool bSuccess;
2446
2447 if (bUseMask)
2448 {
2449 //
2450 // Blit bitmap with mask
2451 //
2452
2453 //
2454 // Create a temp buffer bitmap and DCs/PSs to access it and the mask
2455 //
2456 HDC hDCMask;
2457 HDC hDCBuffer;
2458 HPS hPSMask;
2459 HPS hPSBuffer;
2460 DEVOPENSTRUC vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
2461 BITMAPINFOHEADER2 vBmpHdr;
2462 HBITMAP hBufBitmap;
2463 SIZEL vSize = {0, 0};
2464 LONG rc;
2465
2466 memset(&vBmpHdr, 0, sizeof(BITMAPINFOHEADER2));
2467 vBmpHdr.cbFix = sizeof(BITMAPINFOHEADER2);
2468 vBmpHdr.cx = vWidth;
2469 vBmpHdr.cy = vHeight;
2470 vBmpHdr.cPlanes = 1;
2471 vBmpHdr.cBitCount = 24;
2472
2473 #if wxUSE_DC_CACHEING
2474 if (TRUE)
2475 {
2476 //
2477 // create a temp buffer bitmap and DCs to access it and the mask
2478 //
2479 wxDCCacheEntry* pDCCacheEntry1 = FindDCInCache( NULL
2480 ,pSource->GetHPS()
2481 );
2482 wxDCCacheEntry* pDCCacheEntry2 = FindDCInCache( pDCCacheEntry1
2483 ,GetHPS()
2484 );
2485 wxDCCacheEntry* pBitmapCacheEntry = FindBitmapInCache( GetHPS()
2486 ,vWidth
2487 ,vHeight
2488 );
2489
2490 hPSMask = pDCCacheEntry1->m_hPS;
2491 hDCBuffer = (HDC)pDCCacheEntry2->m_hPS;
2492 hBufBitmap = (HBITMAP)pBitmapCacheEntry->m_hBitmap;
2493 }
2494 else
2495 #endif
2496 {
2497 hDCMask = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2498 hDCBuffer = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2499 hPSMask = ::GpiCreatePS(vHabmain, hDCMask, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2500 hPSBuffer = ::GpiCreatePS(vHabmain, hDCBuffer, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2501 hBufBitmap = ::GpiCreateBitmap(GetHPS(), &vBmpHdr, 0L, NULL, NULL);
2502 }
2503
2504 POINTL aPoint1[4] = { 0, 0
2505 ,vWidth, vHeight
2506 ,vXdest, vYdest
2507 ,vXdest + vWidth, vYdest + vHeight
2508 };
2509 POINTL aPoint2[4] = { 0, 0
2510 ,vWidth, vHeight
2511 ,vXsrc, vYsrc
2512 ,vXsrc + vWidth, vYsrc + vHeight
2513 };
2514 POINTL aPoint3[4] = { vXdest, vYdest
2515 ,vXdest + vWidth, vYdest + vHeight
2516 ,vXsrc, vYsrc
2517 ,vXsrc + vWidth, vYsrc + vHeight
2518 };
2519 POINTL aPoint4[4] = { vXdest, vYdest
2520 ,vXdest + vWidth, vYdest + vHeight
2521 ,0, 0
2522 ,vWidth, vHeight
2523 };
2524 ::GpiSetBitmap(hPSMask, (HBITMAP) pMask->GetMaskBitmap());
2525 ::GpiSetBitmap(hPSBuffer, (HBITMAP) hBufBitmap);
2526
2527 //
2528 // Copy dest to buffer
2529 //
2530 rc = ::GpiBitBlt( hPSBuffer
2531 ,GetHPS()
2532 ,4L
2533 ,aPoint1
2534 ,ROP_SRCCOPY
2535 ,BBO_IGNORE
2536 );
2537 if (rc == GPI_ERROR)
2538 {
2539 wxLogLastError(wxT("BitBlt"));
2540 }
2541
2542 //
2543 // Copy src to buffer using selected raster op
2544 //
2545 rc = ::GpiBitBlt( hPSBuffer
2546 ,GetHPS()
2547 ,4L
2548 ,aPoint2
2549 ,lRop
2550 ,BBO_IGNORE
2551 );
2552 if (rc == GPI_ERROR)
2553 {
2554 wxLogLastError(wxT("BitBlt"));
2555 }
2556
2557 //
2558 // Set masked area in buffer to BLACK (pixel value 0)
2559 //
2560 COLORREF vPrevBkCol = ::GpiQueryBackColor(GetHPS());
2561 COLORREF vPrevCol = ::GpiQueryColor(GetHPS());
2562
2563 ::GpiSetBackColor(GetHPS(), OS2RGB(255, 255, 255));
2564 ::GpiSetColor(GetHPS(), OS2RGB(0, 0, 0));
2565
2566 rc = ::GpiBitBlt( hPSBuffer
2567 ,hPSMask
2568 ,4L
2569 ,aPoint2
2570 ,ROP_SRCAND
2571 ,BBO_IGNORE
2572 );
2573 if (rc == GPI_ERROR)
2574 {
2575 wxLogLastError(wxT("BitBlt"));
2576 }
2577
2578 //
2579 // Set unmasked area in dest to BLACK
2580 //
2581 ::GpiSetBackColor(GetHPS(), OS2RGB(0, 0, 0));
2582 ::GpiSetColor(GetHPS(), OS2RGB(255, 255, 255));
2583 rc = ::GpiBitBlt( GetHPS()
2584 ,hPSMask
2585 ,4L
2586 ,aPoint3
2587 ,ROP_SRCAND
2588 ,BBO_IGNORE
2589 );
2590 if (rc == GPI_ERROR)
2591 {
2592 wxLogLastError(wxT("BitBlt"));
2593 }
2594
2595 //
2596 // Restore colours to original values
2597 //
2598 ::GpiSetBackColor(GetHPS(), vPrevBkCol);
2599 ::GpiSetColor(GetHPS(), vPrevCol);
2600
2601 //
2602 // OR buffer to dest
2603 //
2604 rc = ::GpiBitBlt( GetHPS()
2605 ,hPSMask
2606 ,4L
2607 ,aPoint4
2608 ,ROP_SRCPAINT
2609 ,BBO_IGNORE
2610 );
2611 if (rc == GPI_ERROR)
2612 {
2613 bSuccess = FALSE;
2614 wxLogLastError(wxT("BitBlt"));
2615 }
2616
2617 //
2618 // Tidy up temporary DCs and bitmap
2619 //
2620 ::GpiSetBitmap(hPSMask, NULLHANDLE);
2621 ::GpiSetBitmap(hPSBuffer, NULLHANDLE);
2622 #if !wxUSE_DC_CACHEING
2623 ::GpiDestroyPS(hPSMask);
2624 ::GpiDestroyPS(hPSBuffer);
2625 ::DevCloseDC(hDCMask);
2626 ::DevCloseDC(hDCBuffer);
2627 ::GpiDeleteBitmap(hBufBitmap);
2628 #endif
2629 bSuccess = TRUE;
2630 }
2631 else // no mask, just BitBlt() it
2632 {
2633 POINTL aPoint[4] = { vXdest, vYdest
2634 ,vXdest + vWidth, vYdest + vHeight
2635 ,vXsrc, vYsrc
2636 ,vXsrc + vWidth, vYsrc + vHeight
2637 };
2638
2639 bSuccess = (::GpiBitBlt( m_hPS
2640 ,pSource->GetHPS()
2641 ,4L
2642 ,aPoint
2643 ,lRop
2644 ,BBO_IGNORE
2645 ) != GPI_ERROR);
2646 if (!bSuccess )
2647 {
2648 wxLogLastError(wxT("BitBlt"));
2649 }
2650 }
2651 vCbnd.lColor = (LONG)vOldTextColor;
2652 ::GpiSetAttrs( m_hPS // presentation-space handle
2653 ,PRIM_CHAR // Char primitive.
2654 ,CBB_COLOR // sets color.
2655 ,0
2656 ,&vCbnd // buffer for attributes.
2657 );
2658 ::GpiSetBackColor(m_hPS, (LONG)vOldBackground);
2659 return bSuccess;
2660 }
2661
2662 void wxDC::DoGetSize(
2663 int* pnWidth
2664 , int* pnHeight
2665 ) const
2666 {
2667 LONG lArray[CAPS_HEIGHT];
2668
2669 if(::DevQueryCaps( m_hDC
2670 ,CAPS_FAMILY
2671 ,CAPS_HEIGHT
2672 ,lArray
2673 ))
2674 {
2675 *pnWidth = lArray[CAPS_WIDTH];
2676 *pnHeight = lArray[CAPS_HEIGHT];
2677 }
2678 }; // end of wxDC::DoGetSize(
2679
2680 void wxDC::DoGetSizeMM(
2681 int* pnWidth
2682 , int* pnHeight
2683 ) const
2684 {
2685 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2686
2687 if(::DevQueryCaps( m_hDC
2688 ,CAPS_FAMILY
2689 ,CAPS_VERTICAL_RESOLUTION
2690 ,lArray
2691 ))
2692 {
2693 int nWidth;
2694 int nHeight;
2695 int nHorzRes;
2696 int nVertRes;
2697
2698 nWidth = lArray[CAPS_WIDTH];
2699 nHeight = lArray[CAPS_HEIGHT];
2700 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2701 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2702 nWidth = (nHorzRes/1000) * nWidth;
2703 nHeight = (nVertRes/1000) * nHeight;
2704 }
2705 }; // end of wxDC::DoGetSizeMM
2706
2707 wxSize wxDC::GetPPI() const
2708 {
2709 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2710 int nWidth;
2711 int nHeight;
2712
2713 if(::DevQueryCaps( m_hDC
2714 ,CAPS_FAMILY
2715 ,CAPS_VERTICAL_RESOLUTION
2716 ,lArray
2717 ))
2718 {
2719 int nPelWidth;
2720 int nPelHeight;
2721 int nHorzRes;
2722 int nVertRes;
2723
2724 nPelWidth = lArray[CAPS_WIDTH];
2725 nPelHeight = lArray[CAPS_HEIGHT];
2726 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2727 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2728 nWidth = (nHorzRes/39.3) * nPelWidth;
2729 nHeight = (nVertRes/39.3) * nPelHeight;
2730 }
2731 return (wxSize(nWidth,nHeight));
2732 } // end of wxDC::GetPPI
2733
2734 void wxDC::SetLogicalScale(
2735 double dX
2736 , double dY
2737 )
2738 {
2739 m_logicalScaleX = dX;
2740 m_logicalScaleY = dY;
2741 }; // end of wxDC::SetLogicalScale
2742
2743 #if WXWIN_COMPATIBILITY
2744 void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
2745 float *descent, float *externalLeading,
2746 wxFont *theFont, bool use16bit) const
2747 {
2748 wxCoord x1, y1, descent1, externalLeading1;
2749 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
2750 *x = x1; *y = y1;
2751 if (descent)
2752 *descent = descent1;
2753 if (externalLeading)
2754 *externalLeading = externalLeading1;
2755 }
2756 #endif
2757