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