Mono bitmap processing 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 = cBlueFore;
1547 pucData++;
1548 *pucData = cGreenFore;
1549 pucData++;
1550 *pucData = cRedFore;
1551 pucData++;
1552 }
1553 else
1554 {
1555 *pucData = cBlueBack;
1556 pucData++;
1557 *pucData = cGreenBack;
1558 pucData++;
1559 *pucData = cRedBack;
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 ::GpiSetColor(m_hPS, m_pen.GetColour().GetPixel());
1883 }
1884 }
1885
1886 void wxDC::SetBrush(
1887 const wxBrush& rBrush
1888 )
1889 {
1890 wxCHECK_RET( Ok(), wxT("invalid window dc") );
1891
1892 if (m_hOldBrush)
1893 m_hOldBrush = 0L;
1894 m_brush = rBrush;
1895 if (!m_brush.Ok())
1896 if (m_brush == rBrush)
1897 return;
1898 if (!m_brush.Ok())
1899 if (m_hOldBrush)
1900 m_hOldBrush = 0L;
1901
1902 if (!m_brush.Ok())
1903 {
1904 if (m_hOldBrush)
1905 {
1906 m_brush.SetPS((HPS)m_hOldBrush);
1907 }
1908 m_hOldBrush = 0L;
1909 }
1910
1911 if (m_brush.Ok())
1912 {
1913 if (m_brush.GetResourceHandle())
1914 {
1915 m_brush.SetPS(m_hPS);
1916 if (!m_hOldBrush)
1917 m_hOldBrush = (WXHWND)m_brush.GetPS();
1918 }
1919 }
1920 } // end of wxDC::SetBrush
1921
1922 void wxDC::SetBackground(
1923 const wxBrush& rBrush
1924 )
1925 {
1926 m_backgroundBrush = rBrush;
1927 if (!m_backgroundBrush.Ok())
1928 return;
1929 if (m_pCanvas)
1930 {
1931 bool bCustomColours = TRUE;
1932
1933 //
1934 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1935 // change background colours from the control-panel specified colours.
1936 //
1937 if (m_pCanvas->IsKindOf(CLASSINFO(wxWindow)) &&
1938 ((m_pCanvas->GetWindowStyleFlag() & wxUSER_COLOURS) != wxUSER_COLOURS))
1939 bCustomColours = FALSE;
1940 if (bCustomColours)
1941 {
1942 if (m_backgroundBrush.GetStyle()==wxTRANSPARENT)
1943 {
1944 m_pCanvas->SetTransparent(TRUE);
1945 }
1946 else
1947 {
1948 //
1949 // Setting the background brush of a DC
1950 // doesn't affect the window background colour. However,
1951 // I'm leaving in the transparency setting because it's needed by
1952 // various controls (e.g. wxStaticText) to determine whether to draw
1953 // transparently or not. TODO: maybe this should be a new function
1954 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1955 // parent?
1956 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1957 //
1958 m_pCanvas->SetTransparent(FALSE);
1959 }
1960 }
1961 }
1962 COLORREF vNewColor = m_backgroundBrush.GetColour().GetPixel();
1963 (void)::GpiSetBackColor((HPS)m_hPS, (LONG)vNewColor);
1964 } // end of wxDC::SetBackground
1965
1966 void wxDC::SetBackgroundMode(
1967 int nMode
1968 )
1969 {
1970 m_backgroundMode = nMode;
1971 } // end of wxDC::SetBackgroundMode
1972
1973 void wxDC::SetLogicalFunction(
1974 int nFunction
1975 )
1976 {
1977 m_logicalFunction = nFunction;
1978 SetRop((WXHDC)m_hDC);
1979 } // wxDC::SetLogicalFunction
1980
1981 void wxDC::SetRop(
1982 WXHDC hDC
1983 )
1984 {
1985 if (!hDC || m_logicalFunction < 0)
1986 return;
1987
1988 LONG lCRop;
1989 switch (m_logicalFunction)
1990 {
1991 case wxXOR:
1992 lCRop = FM_XOR;
1993 break;
1994
1995 case wxINVERT:
1996 lCRop = FM_INVERT;
1997 break;
1998
1999 case wxOR_REVERSE:
2000 lCRop = FM_MERGESRCNOT;
2001 break;
2002
2003 case wxAND_REVERSE:
2004 lCRop = FM_NOTMASKSRC;
2005 break;
2006
2007 case wxCLEAR:
2008 lCRop = FM_ONE;
2009 break;
2010
2011 case wxSET:
2012 lCRop = FM_ZERO;
2013 break;
2014
2015 case wxSRC_INVERT:
2016 lCRop = FM_MERGENOTSRC;
2017 break;
2018
2019 case wxOR_INVERT:
2020 lCRop = FM_MERGESRCNOT;
2021 break;
2022
2023 case wxAND:
2024 lCRop = FM_AND;
2025 break;
2026
2027 case wxOR:
2028 lCRop = FM_OR;
2029 break;
2030
2031 case wxAND_INVERT:
2032 lCRop = FM_SUBTRACT;
2033 break;
2034
2035 case wxEQUIV:
2036 case wxNAND:
2037 case wxCOPY:
2038 default:
2039 lCRop = FM_OVERPAINT;
2040 break;
2041 }
2042 ::GpiSetMix((HPS)hDC, lCRop);
2043 } // end of wxDC::SetRop
2044
2045 bool wxDC::StartDoc(
2046 const wxString& rsMessage
2047 )
2048 {
2049 // We might be previewing, so return TRUE to let it continue.
2050 return TRUE;
2051 } // end of wxDC::StartDoc
2052
2053 void wxDC::EndDoc()
2054 {
2055 } // end of wxDC::EndDoc
2056
2057 void wxDC::StartPage()
2058 {
2059 } // end of wxDC::StartPage
2060
2061 void wxDC::EndPage()
2062 {
2063 } // end of wxDC::EndPage
2064
2065 // ---------------------------------------------------------------------------
2066 // text metrics
2067 // ---------------------------------------------------------------------------
2068
2069 wxCoord wxDC::GetCharHeight() const
2070 {
2071 FONTMETRICS vFM; // metrics structure
2072
2073 ::GpiQueryFontMetrics( m_hPS
2074 ,sizeof(FONTMETRICS)
2075 ,&vFM
2076 );
2077 return YDEV2LOGREL(vFM.lXHeight);
2078 }
2079
2080 wxCoord wxDC::GetCharWidth() const
2081 {
2082 FONTMETRICS vFM; // metrics structure
2083
2084 ::GpiQueryFontMetrics( m_hPS
2085 ,sizeof(FONTMETRICS)
2086 ,&vFM
2087 );
2088 return XDEV2LOGREL(vFM.lAveCharWidth);
2089 }
2090
2091 void wxDC::DoGetTextExtent(
2092 const wxString& rsString
2093 , wxCoord* pvX
2094 , wxCoord* pvY
2095 , wxCoord* pvDescent
2096 , wxCoord* pvExternalLeading
2097 , wxFont* pTheFont
2098 ) const
2099 {
2100 POINTL avPoint[TXTBOX_COUNT];
2101 POINTL vPtMin;
2102 POINTL vPtMax;
2103 int i;
2104 int l;
2105 FONTMETRICS vFM; // metrics structure
2106 BOOL bRc;
2107 char* pStr;
2108 ERRORID vErrorCode; // last error id code
2109 wxFont* pFontToUse = (wxFont*)pTheFont;
2110
2111 char zMsg[128]; // DEBUG
2112 wxString sError;
2113
2114 if (!pFontToUse)
2115 pFontToUse = (wxFont*)&m_font;
2116 l = rsString.length();
2117 pStr = (PCH) rsString.c_str();
2118
2119 //
2120 // In world coordinates.
2121 //
2122 bRc = ::GpiQueryTextBox( m_hPS
2123 ,l
2124 ,pStr
2125 ,TXTBOX_COUNT // return maximum information
2126 ,avPoint // array of coordinates points
2127 );
2128 if(!bRc)
2129 {
2130 vErrorCode = ::WinGetLastError(wxGetInstance());
2131 sError = wxPMErrorToStr(vErrorCode);
2132 // DEBUG
2133 sprintf(zMsg, "GpiQueryTextBox for %s: failed with Error: %x - %s", pStr, vErrorCode, sError.c_str());
2134 (void)wxMessageBox( "wxWindows Menu sample"
2135 ,zMsg
2136 ,wxICON_INFORMATION
2137 );
2138 }
2139
2140 vPtMin.x = avPoint[0].x;
2141 vPtMax.x = avPoint[0].x;
2142 vPtMin.y = avPoint[0].y;
2143 vPtMax.y = avPoint[0].y;
2144 for (i = 1; i < 4; i++)
2145 {
2146 if(vPtMin.x > avPoint[i].x) vPtMin.x = avPoint[i].x;
2147 if(vPtMin.y > avPoint[i].y) vPtMin.y = avPoint[i].y;
2148 if(vPtMax.x < avPoint[i].x) vPtMax.x = avPoint[i].x;
2149 if(vPtMax.y < avPoint[i].y) vPtMax.y = avPoint[i].y;
2150 }
2151 ::GpiQueryFontMetrics( m_hPS
2152 ,sizeof(FONTMETRICS)
2153 ,&vFM
2154 );
2155
2156 if (pvX)
2157 *pvX = (wxCoord)(vPtMax.x - vPtMin.x + 1);
2158 if (pvY)
2159 *pvY = (wxCoord)(vPtMax.y - vPtMin.y + 1);
2160 if (pvDescent)
2161 *pvDescent = vFM.lMaxDescender;
2162 if (pvExternalLeading)
2163 *pvExternalLeading = vFM.lExternalLeading;
2164 }
2165
2166 void wxDC::SetMapMode(
2167 int nMode
2168 )
2169 {
2170 int nPixelWidth = 0;
2171 int nPixelHeight = 0;
2172 int nMmWidth = 1;
2173 int nMmHeight = 1;
2174 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2175
2176 m_mappingMode = nMode;
2177
2178 if(::DevQueryCaps( m_hDC
2179 ,CAPS_FAMILY
2180 ,CAPS_VERTICAL_RESOLUTION
2181 ,lArray
2182 ))
2183 {
2184 LONG lHorzRes;
2185 LONG lVertRes;
2186
2187 nPixelWidth = lArray[CAPS_WIDTH];
2188 nPixelHeight = lArray[CAPS_HEIGHT];
2189 lHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2190 lVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2191 nMmWidth = (lHorzRes/1000) * nPixelWidth;
2192 nMmWidth = (lVertRes/1000) * nPixelHeight;
2193 }
2194 if ((nPixelWidth == 0) || (nPixelHeight == 0) || (nMmWidth == 0) || (nMmHeight == 0))
2195 {
2196 return;
2197 }
2198
2199 double dMm2pixelsX = nPixelWidth/nMmWidth;
2200 double dMm2pixelsY = nPixelHeight/nMmHeight;
2201
2202 switch (nMode)
2203 {
2204 case wxMM_TWIPS:
2205 m_logicalScaleX = (twips2mm * dMm2pixelsX);
2206 m_logicalScaleY = (twips2mm * dMm2pixelsY);
2207 break;
2208
2209 case wxMM_POINTS:
2210 m_logicalScaleX = (pt2mm * dMm2pixelsX);
2211 m_logicalScaleY = (pt2mm * dMm2pixelsY);
2212 break;
2213
2214 case wxMM_METRIC:
2215 m_logicalScaleX = dMm2pixelsX;
2216 m_logicalScaleY = dMm2pixelsY;
2217 break;
2218
2219 case wxMM_LOMETRIC:
2220 m_logicalScaleX = (dMm2pixelsX/10.0);
2221 m_logicalScaleY = (dMm2pixelsY/10.0);
2222 break;
2223
2224 case wxMM_TEXT:
2225 default:
2226 m_logicalScaleX = 1.0;
2227 m_logicalScaleY = 1.0;
2228 break;
2229 }
2230 SIZEL vSize;
2231 ULONG ulOptions;
2232
2233 ulOptions = ::GpiQueryPS(m_hPS, &vSize);
2234 if (!ulOptions & PU_ARBITRARY)
2235 {
2236 ulOptions = PU_ARBITRARY | GPIF_DEFAULT;
2237 ::GpiSetPS(m_hPS, &vSize, ulOptions);
2238 }
2239 m_nWindowExtX = (int)MS_XDEV2LOG(VIEWPORT_EXTENT);
2240 m_nWindowExtY = (int)MS_YDEV2LOG(VIEWPORT_EXTENT);
2241 // ????
2242 }; // end of wxDC::SetMapMode
2243
2244 void wxDC::SetUserScale(
2245 double dX
2246 , double dY
2247 )
2248 {
2249 m_userScaleX = dX;
2250 m_userScaleY = dY;
2251
2252 SetMapMode(m_mappingMode);
2253 } // end of wxDC::SetUserScale
2254
2255 void wxDC::SetAxisOrientation(
2256 bool bXLeftRight
2257 , bool bYBottomUp
2258 )
2259 {
2260 m_signX = bXLeftRight ? 1 : -1;
2261 m_signY = bYBottomUp ? -1 : 1;
2262
2263 SetMapMode(m_mappingMode);
2264 } // end of wxDC::SetAxisOrientation
2265
2266 void wxDC::SetSystemScale(
2267 double dX
2268 , double dY
2269 )
2270 {
2271 m_scaleX = dX;
2272 m_scaleY = dY;
2273
2274 SetMapMode(m_mappingMode);
2275 } // end of wxDC::SetSystemScale
2276
2277 void wxDC::SetLogicalOrigin(
2278 wxCoord vX
2279 , wxCoord vY
2280 )
2281 {
2282 RECTL vRect;
2283
2284 ::GpiQueryPageViewport( m_hPS
2285 ,&vRect
2286 );
2287 vRect.xRight -= vX;
2288 vRect.yTop += vY;
2289 vRect.xLeft = vX;
2290 vRect.yBottom = vY;
2291 ::GpiSetPageViewport( m_hPS
2292 ,&vRect
2293 );
2294 }; // end of wxDC::SetLogicalOrigin
2295
2296 void wxDC::SetDeviceOrigin(
2297 wxCoord vX
2298 , wxCoord vY
2299 )
2300 {
2301 RECTL vRect;
2302
2303 m_deviceOriginX = vX;
2304 m_deviceOriginY = vY;
2305 ::GpiQueryPageViewport( m_hPS
2306 ,&vRect
2307 );
2308 vRect.xLeft += vX;
2309 vRect.xRight += vX;
2310 vRect.yBottom -= vY;
2311 vRect.yTop -= vY;
2312 ::GpiSetPageViewport( m_hPS
2313 ,&vRect
2314 );
2315 }; // end of wxDC::SetDeviceOrigin
2316
2317 // ---------------------------------------------------------------------------
2318 // coordinates transformations
2319 // ---------------------------------------------------------------------------
2320
2321 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2322 {
2323 return (wxCoord) (((x) - m_deviceOriginX)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX) - m_logicalOriginX);
2324 }
2325
2326 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2327 {
2328 // axis orientation is not taken into account for conversion of a distance
2329 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_scaleX));
2330 }
2331
2332 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2333 {
2334 return (wxCoord) (((y) - m_deviceOriginY)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY) - m_logicalOriginY);
2335 }
2336
2337 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2338 {
2339 // axis orientation is not taken into account for conversion of a distance
2340 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_scaleY));
2341 }
2342
2343 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2344 {
2345 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
2346 }
2347
2348 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2349 {
2350 // axis orientation is not taken into account for conversion of a distance
2351 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_scaleX);
2352 }
2353
2354 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2355 {
2356 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
2357 }
2358
2359 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2360 {
2361 // axis orientation is not taken into account for conversion of a distance
2362 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_scaleY);
2363 }
2364
2365 // ---------------------------------------------------------------------------
2366 // bit blit
2367 // ---------------------------------------------------------------------------
2368
2369 bool wxDC::DoBlit(
2370 wxCoord vXdest
2371 , wxCoord vYdest
2372 , wxCoord vWidth
2373 , wxCoord vHeight
2374 , wxDC* pSource
2375 , wxCoord vXsrc
2376 , wxCoord vYsrc
2377 , int nRop
2378 , bool bUseMask
2379 , wxCoord vXsrcMask
2380 , wxCoord vYsrcMask
2381 )
2382 {
2383 wxMask* pMask = NULL;
2384 CHARBUNDLE vCbnd;
2385 COLORREF vOldTextColor;
2386 COLORREF vOldBackground = ::GpiQueryBackColor(m_hPS);
2387
2388 if (bUseMask)
2389 {
2390 const wxBitmap& rBmp = pSource->m_vSelectedBitmap;
2391
2392 pMask = rBmp.GetMask();
2393 if (!(rBmp.Ok() && pMask && pMask->GetMaskBitmap()))
2394 {
2395 bUseMask = FALSE;
2396 }
2397 }
2398
2399 ::GpiQueryAttrs( m_hPS
2400 ,PRIM_CHAR
2401 ,CBB_COLOR
2402 ,&vCbnd
2403 );
2404 vOldTextColor = (COLORREF)vCbnd.lColor;
2405
2406 if (m_textForegroundColour.Ok())
2407 {
2408 vCbnd.lColor = (LONG)m_textForegroundColour.GetPixel();
2409 ::GpiSetAttrs( m_hPS // presentation-space handle
2410 ,PRIM_CHAR // Char primitive.
2411 ,CBB_COLOR // sets color.
2412 ,0
2413 ,&vCbnd // buffer for attributes.
2414 );
2415 }
2416 if (m_textBackgroundColour.Ok())
2417 {
2418 ::GpiSetBackColor(m_hPS, (LONG)m_textBackgroundColour.GetPixel());
2419 }
2420
2421 LONG lRop = ROP_SRCCOPY;
2422
2423 switch (nRop)
2424 {
2425 case wxXOR: lRop = ROP_SRCINVERT; break;
2426 case wxINVERT: lRop = ROP_DSTINVERT; break;
2427 case wxOR_REVERSE: lRop = 0x00DD0228; break;
2428 case wxAND_REVERSE: lRop = ROP_SRCERASE; break;
2429 case wxCLEAR: lRop = ROP_ZERO; break;
2430 case wxSET: lRop = ROP_ONE; break;
2431 case wxOR_INVERT: lRop = ROP_MERGEPAINT; break;
2432 case wxAND: lRop = ROP_SRCAND; break;
2433 case wxOR: lRop = ROP_SRCPAINT; break;
2434 case wxEQUIV: lRop = 0x00990066; break;
2435 case wxNAND: lRop = 0x007700E6; break;
2436 case wxAND_INVERT: lRop = 0x00220326; break;
2437 case wxCOPY: lRop = ROP_SRCCOPY; break;
2438 case wxNO_OP: lRop = ROP_NOTSRCERASE; break;
2439 case wxSRC_INVERT: lRop = ROP_SRCINVERT; break;
2440 case wxNOR: lRop = ROP_NOTSRCCOPY; break;
2441 default:
2442 wxFAIL_MSG( wxT("unsupported logical function") );
2443 return FALSE;
2444 }
2445
2446 bool bSuccess;
2447
2448 if (bUseMask)
2449 {
2450 //
2451 // Blit bitmap with mask
2452 //
2453
2454 //
2455 // Create a temp buffer bitmap and DCs/PSs to access it and the mask
2456 //
2457 HDC hDCMask;
2458 HDC hDCBuffer;
2459 HPS hPSMask;
2460 HPS hPSBuffer;
2461 DEVOPENSTRUC vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
2462 BITMAPINFOHEADER2 vBmpHdr;
2463 HBITMAP hBufBitmap;
2464 SIZEL vSize = {0, 0};
2465 LONG rc;
2466
2467 memset(&vBmpHdr, 0, sizeof(BITMAPINFOHEADER2));
2468 vBmpHdr.cbFix = sizeof(BITMAPINFOHEADER2);
2469 vBmpHdr.cx = vWidth;
2470 vBmpHdr.cy = vHeight;
2471 vBmpHdr.cPlanes = 1;
2472 vBmpHdr.cBitCount = 24;
2473
2474 #if wxUSE_DC_CACHEING
2475 if (TRUE)
2476 {
2477 //
2478 // create a temp buffer bitmap and DCs to access it and the mask
2479 //
2480 wxDCCacheEntry* pDCCacheEntry1 = FindDCInCache( NULL
2481 ,pSource->GetHPS()
2482 );
2483 wxDCCacheEntry* pDCCacheEntry2 = FindDCInCache( pDCCacheEntry1
2484 ,GetHPS()
2485 );
2486 wxDCCacheEntry* pBitmapCacheEntry = FindBitmapInCache( GetHPS()
2487 ,vWidth
2488 ,vHeight
2489 );
2490
2491 hPSMask = pDCCacheEntry1->m_hPS;
2492 hDCBuffer = (HDC)pDCCacheEntry2->m_hPS;
2493 hBufBitmap = (HBITMAP)pBitmapCacheEntry->m_hBitmap;
2494 }
2495 else
2496 #endif
2497 {
2498 hDCMask = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2499 hDCBuffer = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
2500 hPSMask = ::GpiCreatePS(vHabmain, hDCMask, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2501 hPSBuffer = ::GpiCreatePS(vHabmain, hDCBuffer, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2502 hBufBitmap = ::GpiCreateBitmap(GetHPS(), &vBmpHdr, 0L, NULL, NULL);
2503 }
2504
2505 POINTL aPoint1[4] = { 0, 0
2506 ,vWidth, vHeight
2507 ,vXdest, vYdest
2508 ,vXdest + vWidth, vYdest + vHeight
2509 };
2510 POINTL aPoint2[4] = { 0, 0
2511 ,vWidth, vHeight
2512 ,vXsrc, vYsrc
2513 ,vXsrc + vWidth, vYsrc + vHeight
2514 };
2515 POINTL aPoint3[4] = { vXdest, vYdest
2516 ,vXdest + vWidth, vYdest + vHeight
2517 ,vXsrc, vYsrc
2518 ,vXsrc + vWidth, vYsrc + vHeight
2519 };
2520 POINTL aPoint4[4] = { vXdest, vYdest
2521 ,vXdest + vWidth, vYdest + vHeight
2522 ,0, 0
2523 ,vWidth, vHeight
2524 };
2525 ::GpiSetBitmap(hPSMask, (HBITMAP) pMask->GetMaskBitmap());
2526 ::GpiSetBitmap(hPSBuffer, (HBITMAP) hBufBitmap);
2527
2528 //
2529 // Copy dest to buffer
2530 //
2531 rc = ::GpiBitBlt( hPSBuffer
2532 ,GetHPS()
2533 ,4L
2534 ,aPoint1
2535 ,ROP_SRCCOPY
2536 ,BBO_IGNORE
2537 );
2538 if (rc == GPI_ERROR)
2539 {
2540 wxLogLastError(wxT("BitBlt"));
2541 }
2542
2543 //
2544 // Copy src to buffer using selected raster op
2545 //
2546 rc = ::GpiBitBlt( hPSBuffer
2547 ,GetHPS()
2548 ,4L
2549 ,aPoint2
2550 ,lRop
2551 ,BBO_IGNORE
2552 );
2553 if (rc == GPI_ERROR)
2554 {
2555 wxLogLastError(wxT("BitBlt"));
2556 }
2557
2558 //
2559 // Set masked area in buffer to BLACK (pixel value 0)
2560 //
2561 COLORREF vPrevBkCol = ::GpiQueryBackColor(GetHPS());
2562 COLORREF vPrevCol = ::GpiQueryColor(GetHPS());
2563
2564 ::GpiSetBackColor(GetHPS(), OS2RGB(255, 255, 255));
2565 ::GpiSetColor(GetHPS(), OS2RGB(0, 0, 0));
2566
2567 rc = ::GpiBitBlt( hPSBuffer
2568 ,hPSMask
2569 ,4L
2570 ,aPoint2
2571 ,ROP_SRCAND
2572 ,BBO_IGNORE
2573 );
2574 if (rc == GPI_ERROR)
2575 {
2576 wxLogLastError(wxT("BitBlt"));
2577 }
2578
2579 //
2580 // Set unmasked area in dest to BLACK
2581 //
2582 ::GpiSetBackColor(GetHPS(), OS2RGB(0, 0, 0));
2583 ::GpiSetColor(GetHPS(), OS2RGB(255, 255, 255));
2584 rc = ::GpiBitBlt( GetHPS()
2585 ,hPSMask
2586 ,4L
2587 ,aPoint3
2588 ,ROP_SRCAND
2589 ,BBO_IGNORE
2590 );
2591 if (rc == GPI_ERROR)
2592 {
2593 wxLogLastError(wxT("BitBlt"));
2594 }
2595
2596 //
2597 // Restore colours to original values
2598 //
2599 ::GpiSetBackColor(GetHPS(), vPrevBkCol);
2600 ::GpiSetColor(GetHPS(), vPrevCol);
2601
2602 //
2603 // OR buffer to dest
2604 //
2605 rc = ::GpiBitBlt( GetHPS()
2606 ,hPSMask
2607 ,4L
2608 ,aPoint4
2609 ,ROP_SRCPAINT
2610 ,BBO_IGNORE
2611 );
2612 if (rc == GPI_ERROR)
2613 {
2614 bSuccess = FALSE;
2615 wxLogLastError(wxT("BitBlt"));
2616 }
2617
2618 //
2619 // Tidy up temporary DCs and bitmap
2620 //
2621 ::GpiSetBitmap(hPSMask, NULLHANDLE);
2622 ::GpiSetBitmap(hPSBuffer, NULLHANDLE);
2623 #if !wxUSE_DC_CACHEING
2624 ::GpiDestroyPS(hPSMask);
2625 ::GpiDestroyPS(hPSBuffer);
2626 ::DevCloseDC(hDCMask);
2627 ::DevCloseDC(hDCBuffer);
2628 ::GpiDeleteBitmap(hBufBitmap);
2629 #endif
2630 bSuccess = TRUE;
2631 }
2632 else // no mask, just BitBlt() it
2633 {
2634 POINTL aPoint[4] = { vXdest, vYdest
2635 ,vXdest + vWidth, vYdest + vHeight
2636 ,vXsrc, vYsrc
2637 ,vXsrc + vWidth, vYsrc + vHeight
2638 };
2639
2640 bSuccess = (::GpiBitBlt( m_hPS
2641 ,pSource->GetHPS()
2642 ,4L
2643 ,aPoint
2644 ,lRop
2645 ,BBO_IGNORE
2646 ) != GPI_ERROR);
2647 if (!bSuccess )
2648 {
2649 wxLogLastError(wxT("BitBlt"));
2650 }
2651 }
2652 vCbnd.lColor = (LONG)vOldTextColor;
2653 ::GpiSetAttrs( m_hPS // presentation-space handle
2654 ,PRIM_CHAR // Char primitive.
2655 ,CBB_COLOR // sets color.
2656 ,0
2657 ,&vCbnd // buffer for attributes.
2658 );
2659 ::GpiSetBackColor(m_hPS, (LONG)vOldBackground);
2660 return bSuccess;
2661 }
2662
2663 void wxDC::DoGetSize(
2664 int* pnWidth
2665 , int* pnHeight
2666 ) const
2667 {
2668 LONG lArray[CAPS_HEIGHT];
2669
2670 if(::DevQueryCaps( m_hDC
2671 ,CAPS_FAMILY
2672 ,CAPS_HEIGHT
2673 ,lArray
2674 ))
2675 {
2676 *pnWidth = lArray[CAPS_WIDTH];
2677 *pnHeight = lArray[CAPS_HEIGHT];
2678 }
2679 }; // end of wxDC::DoGetSize(
2680
2681 void wxDC::DoGetSizeMM(
2682 int* pnWidth
2683 , int* pnHeight
2684 ) const
2685 {
2686 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2687
2688 if(::DevQueryCaps( m_hDC
2689 ,CAPS_FAMILY
2690 ,CAPS_VERTICAL_RESOLUTION
2691 ,lArray
2692 ))
2693 {
2694 int nWidth;
2695 int nHeight;
2696 int nHorzRes;
2697 int nVertRes;
2698
2699 nWidth = lArray[CAPS_WIDTH];
2700 nHeight = lArray[CAPS_HEIGHT];
2701 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2702 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2703 nWidth = (nHorzRes/1000) * nWidth;
2704 nHeight = (nVertRes/1000) * nHeight;
2705 }
2706 }; // end of wxDC::DoGetSizeMM
2707
2708 wxSize wxDC::GetPPI() const
2709 {
2710 LONG lArray[CAPS_VERTICAL_RESOLUTION];
2711 int nWidth;
2712 int nHeight;
2713
2714 if(::DevQueryCaps( m_hDC
2715 ,CAPS_FAMILY
2716 ,CAPS_VERTICAL_RESOLUTION
2717 ,lArray
2718 ))
2719 {
2720 int nPelWidth;
2721 int nPelHeight;
2722 int nHorzRes;
2723 int nVertRes;
2724
2725 nPelWidth = lArray[CAPS_WIDTH];
2726 nPelHeight = lArray[CAPS_HEIGHT];
2727 nHorzRes = lArray[CAPS_HORIZONTAL_RESOLUTION]; // returns pel/meter
2728 nVertRes = lArray[CAPS_VERTICAL_RESOLUTION]; // returns pel/meter
2729 nWidth = (nHorzRes/39.3) * nPelWidth;
2730 nHeight = (nVertRes/39.3) * nPelHeight;
2731 }
2732 return (wxSize(nWidth,nHeight));
2733 } // end of wxDC::GetPPI
2734
2735 void wxDC::SetLogicalScale(
2736 double dX
2737 , double dY
2738 )
2739 {
2740 m_logicalScaleX = dX;
2741 m_logicalScaleY = dY;
2742 }; // end of wxDC::SetLogicalScale
2743
2744 #if WXWIN_COMPATIBILITY
2745 void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
2746 float *descent, float *externalLeading,
2747 wxFont *theFont, bool use16bit) const
2748 {
2749 wxCoord x1, y1, descent1, externalLeading1;
2750 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
2751 *x = x1; *y = y1;
2752 if (descent)
2753 *descent = descent1;
2754 if (externalLeading)
2755 *externalLeading = externalLeading1;
2756 }
2757 #endif
2758