]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/dc.cpp
Moving y pos transofrmation code to wxOS2::wxDC elimiantes need for some #ifdef __WXP...
[wxWidgets.git] / src / os2 / dc.cpp
index a2926a2788f30b0ff89fac4f96e1120a71cb63f6..c43a2d31933e6fcf5193874c5416f26810669daf 100644 (file)
@@ -22,6 +22,7 @@
     #include "wx/dcmemory.h"
     #include "wx/log.h"
     #include "wx/icon.h"
+    #include "wx/msgdlg.h"
 #endif
 
 #include "wx/dcprint.h"
@@ -143,52 +144,67 @@ wxDC::wxDC(void)
 
     m_bOwnsDC      = FALSE;
     m_hDC          = 0;
-    m_nDCCount     = 0;
     m_hOldPS       = NULL;
     m_hPS          = NULL;
     m_bIsPaintTime = FALSE; // True at Paint Time
     m_brush.GetColour().Set("WHITE");
-}
+} // end of wxDC::wxDC
 
 wxDC::~wxDC(void)
 {
-}
+    if ( m_hDC != 0 )
+    {
+        SelectOldObjects(m_hDC);
+
+        // if we own the HDC, we delete it, otherwise we just release it
+
+        if (m_bOwnsDC)
+        {
+            if(m_hPS)
+            {
+                ::GpiAssociate(m_hPS, NULLHANDLE);
+                ::GpiDestroyPS(m_hPS);
+            }
+            m_hPS = NULLHANDLE;
+            ::DevCloseDC((HDC)m_hDC);
+        }
+        else
+        {
+            //
+            // Just Dissacociate, not destroy if we don't own the DC
+            //
+            if(m_hPS)
+            {
+                ::GpiAssociate(m_hPS, NULLHANDLE);
+            }
+        }
+    }
+} // end of wxDC::~wxDC
 
 // This will select current objects out of the DC,
 // which is what you have to do before deleting the
 // DC.
-void wxDC::SelectOldObjects(WXHDC dc)
+void wxDC::SelectOldObjects(
+  WXHDC                             hPS
+)
 {
-    if (dc)
+    if (hPS)
     {
         if (m_hOldBitmap)
         {
-//            ::SelectObject((HDC) dc, (HBITMAP) m_oldBitmap);
+            ::GpiSetBitmap(hPS, (HBITMAP) m_hOldBitmap);
             if (m_vSelectedBitmap.Ok())
             {
                 m_vSelectedBitmap.SetSelectedInto(NULL);
             }
         }
         m_hOldBitmap = 0;
-        if (m_hOldPen)
-        {
-//            ::SelectObject((HDC) dc, (HPEN) m_oldPen);
-        }
+        //
+        // OS/2 has no other native GDI objects to set in a PS/DC like windows
+        //
         m_hOldPen = 0;
-        if (m_hOldBrush)
-        {
-//            ::SelectObject((HDC) dc, (HBRUSH) m_oldBrush);
-        }
         m_hOldBrush = 0;
-        if (m_hOldFont)
-        {
-//            ::SelectObject((HDC) dc, (HFONT) m_oldFont);
-        }
         m_hOldFont = 0;
-        if (m_hOldPalette)
-        {
-//            ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, TRUE);
-        }
         m_hOldPalette = 0;
     }
 
@@ -198,7 +214,7 @@ void wxDC::SelectOldObjects(WXHDC dc)
     m_font            = wxNullFont;
     m_backgroundBrush = wxNullBrush;
     m_vSelectedBitmap = wxNullBitmap;
-}
+} // end of wxDC::SelectOldObjects
 
 // ---------------------------------------------------------------------------
 // clipping
@@ -283,18 +299,27 @@ bool wxDC::CanDrawBitmap() const
 
 bool wxDC::CanGetTextExtent() const
 {
-    // What sort of display is it?
-    int technology = 0; // TODO:  ::GetDeviceCaps(GetHdc(), TECHNOLOGY);
+    LONG                            lTechnology = 0L;
 
-    // TODO: return (technology == DT_RASDISPLAY) || (technology == DT_RASPRINTER);
-    return FALSE;
-}
+    ::DevQueryCaps(GetHDC(), CAPS_TECHNOLOGY, 1L, &lTechnology);
+    return (lTechnology == CAPS_TECH_RASTER_DISPLAY) || (lTechnology == CAPS_TECH_RASTER_PRINTER);
+} // end of wxDC::CanGetTextExtent
 
 int wxDC::GetDepth() const
 {
-   // TODO:
-   return (1);
-}
+    LONG                            lArray[CAPS_COLOR_BITCOUNT];
+    int                             nBitsPerPixel;
+
+    if(::DevQueryCaps( GetHDC()
+                      ,CAPS_FAMILY
+                      ,CAPS_COLOR_BITCOUNT
+                      ,lArray
+                     ))
+    {
+        nBitsPerPixel = (int)lArray[CAPS_COLOR_BITCOUNT];
+    }
+    return nBitsPerPixel;
+} // end of wxDC::GetDepth
 
 // ---------------------------------------------------------------------------
 // drawing
@@ -346,10 +371,35 @@ bool wxDC::DoGetPixel(
         return(FALSE);
 }
 
-void wxDC::DoCrossHair(wxCoord x, wxCoord y)
+void wxDC::DoCrossHair(
+  wxCoord                           vX
+, wxCoord                           vY
+)
 {
-   // TODO
-}
+    wxCoord                         vX1 = vX - VIEWPORT_EXTENT;
+    wxCoord                         vY1 = vY - VIEWPORT_EXTENT;
+    wxCoord                         vX2 = vX + VIEWPORT_EXTENT;
+    wxCoord                         vY2 = vY + VIEWPORT_EXTENT;
+    POINTL                          vPoint[4];
+
+    vPoint[0].x = vX1;
+    vPoint[0].y = m_vRclPaint.yTop - vY;
+
+    vPoint[1].x = vX2;
+    vPoint[1].y = m_vRclPaint.yTop - vY;
+
+    ::GpiMove(m_hPS, &vPoint[0]);
+    ::GpiLine(m_hPS, &vPoint[1]);
+
+    vPoint[2].x = vX;
+    vPoint[2].y = m_vRclPaint.yTop - vY1;
+
+    vPoint[3].x = vX;
+    vPoint[3].y = m_vRclPaint.yTop - vY2;
+
+    ::GpiMove(m_hPS, &vPoint[2]);
+    ::GpiLine(m_hPS, &vPoint[3]);
+} // end of wxDC::DoCrossHair
 
 void wxDC::DoDrawLine(
   wxCoord                           vX1
@@ -366,7 +416,7 @@ void wxDC::DoDrawLine(
     vPoint[1].y = m_vRclPaint.yTop - vY2;
     ::GpiMove(m_hPS, &vPoint[0]);
     ::GpiLine(m_hPS, &vPoint[1]);
-}
+} // end of wxDC::DoDrawLine
 
 //////////////////////////////////////////////////////////////////////////////
 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
@@ -460,7 +510,7 @@ void wxDC::DoDrawArc(
     vPtlArc[1].x = vX2;
     vPtlArc[1].y = vY2;
     ::GpiPointArc(m_hPS, vPtlArc); // Draws the arc
-}
+} // end of wxDC::DoDrawArc
 
 void wxDC::DoDrawCheckMark(
   wxCoord                           vX1
@@ -497,7 +547,7 @@ void wxDC::DoDrawCheckMark(
         ::GpiMove(m_hPS, &vPoint[0]);
         ::GpiLine(m_hPS, &vPoint[1]);
     }
-}
+} // end of wxDC::DoDrawCheckMark
 
 void wxDC::DoDrawPoint(
   wxCoord                           vX
@@ -509,7 +559,7 @@ void wxDC::DoDrawPoint(
     vPoint.x = vX;
     vPoint.y = m_vRclPaint.yTop - vY;
     ::GpiSetPel(m_hPS, &vPoint);
-}
+} // end of wxDC::DoDrawPoint
 
 void wxDC::DoDrawPolygon(
   int                               n
@@ -581,7 +631,7 @@ void wxDC::DoDrawPolygon(
     ::GpiMove(m_hPS, &vPoint);
     lHits = ::GpiPolygons(m_hPS, ulCount, &vPlgn, flOptions, flModel);
     free(vPlgn.aPointl);
-}
+} // end of wxDC::DoDrawPolygon
 
 void wxDC::DoDrawLines(
   int                               n
@@ -606,7 +656,7 @@ void wxDC::DoDrawLines(
         vPoint.y = vPoints[0].y + vYoffset;
         ::GpiLine(m_hPS, &vPoint);
     }
-}
+} // end of wxDC::DoDrawLines
 
 void wxDC::DoDrawRectangle(
   wxCoord                           vX
@@ -672,7 +722,7 @@ void wxDC::DoDrawRectangle(
                  ,0L
                 );
     }
-}
+} // end of wxDC::DoDrawRectangle
 
 void wxDC::DoDrawRoundedRectangle(
   wxCoord                           vX
@@ -700,7 +750,7 @@ void wxDC::DoDrawRoundedRectangle(
              ,(LONG)dRadius // horizontal corner radius
              ,(LONG)dRadius // vertical corner radius
             );
-}
+} // end of wxDC::DoDrawRoundedRectangle
 
 // Draw Ellipse within box (x,y) - (x+width, y+height)
 void wxDC::DoDrawEllipse(
@@ -735,7 +785,7 @@ void wxDC::DoDrawEllipse(
                  ,DRO_OUTLINE
                  ,vFxMult
                 ); // Draws full arc with center at current position
-}
+} // end of wxDC::DoDrawEllipse
 
 void wxDC::DoDrawEllipticArc(
   wxCoord                           vX
@@ -788,20 +838,42 @@ void wxDC::DoDrawEllipticArc(
                     ,vFSa
                     ,vFSweepa
                    );
-}
+} // end of wxDC::DoDrawEllipticArc
 
-void wxDC::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y)
+void wxDC::DoDrawIcon(
+  const wxIcon&                     rIcon
+, wxCoord                           vX
+, wxCoord                           vY
+)
 {
-    // TODO:
-}
+    wxCHECK_RET( rIcon.Ok(), wxT("invalid icon in DrawIcon") );
+
+    ::WinDrawPointer( GetHPS()
+                     ,vX
+                     ,vY
+                     ,(HPOINTER)GetHiconOf(rIcon)
+                     ,DP_NORMAL
+                    );
+} // end of wxDC::DoDrawIcon
 
-void wxDC::DoDrawBitmap( const wxBitmap &bmp
-                        ,wxCoord x, wxCoord y
-                        ,bool useMask
-                       )
+void wxDC::DoDrawBitmap(
+  const wxBitmap&                   rBmp
+, wxCoord                           vX
+, wxCoord                           vY
+, bool                              bUseMask
+)
 {
-   // TODO
-}
+    POINTL                          vPoint = {vX, vY};
+
+    ::WinDrawBitmap( GetHPS()
+                    ,(HBITMAP)GetHbitmapOf(rBmp)
+                    ,NULL
+                    ,&vPoint
+                    ,0L
+                    ,0L
+                    ,DBM_NORMAL
+                   );
+} // end of wxDC::DoDrawBitmap
 
 void wxDC::DoDrawText(
   const wxString&                   rsText
@@ -1342,11 +1414,6 @@ bool wxDC::DoBlit(
     CHARBUNDLE                      vCbnd;
     COLORREF                        vOldTextColor;
     COLORREF                        vOldBackground = ::GpiQueryBackColor(m_hPS);
-    POINTL                          aPoint[4] = { vXdest, vYdest
-                                                 ,vXdest + vWidth, vYdest + vHeight
-                                                 ,vXsrc, vYsrc
-                                                 ,vXsrc + vWidth, vYsrc + vHeight
-                                                };
 
     if (bUseMask)
     {
@@ -1407,7 +1474,7 @@ bool wxDC::DoBlit(
     }
 
     bool                            bSuccess;
-#if 0
+
     if (bUseMask)
     {
         //
@@ -1415,68 +1482,163 @@ bool wxDC::DoBlit(
         //
 
         //
-        // Create a temp buffer bitmap and DCs to access it and the mask
+        // Create a temp buffer bitmap and DCs/PSs to access it and the mask
         //
-            HDC dc_mask = ::CreateCompatibleDC(GetHdcOf(*source));
-            HDC dc_buffer = ::CreateCompatibleDC(GetHdc());
-            HBITMAP buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), width, height);
-            ::SelectObject(dc_mask, (HBITMAP) mask->GetMaskBitmap());
-            ::SelectObject(dc_buffer, buffer_bmap);
-
-            // copy dest to buffer
-            if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
-                           GetHdc(), xdest, ydest, SRCCOPY) )
-            {
-                wxLogLastError(wxT("BitBlt"));
-            }
+        HDC                             hDCMask;
+        HDC                             hDCBuffer;
+        HPS                             hPSMask;
+        HPS                             hPSBuffer;
+        DEVOPENSTRUC                    vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
+        BITMAPINFOHEADER2               vBmpHdr;
+        SIZEL                           vSize = {0, 0};
+        LONG                            rc;
+
+        hDCMask = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
+        hDCBuffer = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
+        hPSMask = ::GpiCreatePS(vHabmain, hDCMask, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
+        hPSBuffer = ::GpiCreatePS(vHabmain, hDCBuffer, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
+
+        memset(&vBmpHdr, 0, sizeof(BITMAPINFOHEADER2));
+        vBmpHdr.cbFix     = sizeof(BITMAPINFOHEADER2);
+        vBmpHdr.cx        = vWidth;
+        vBmpHdr.cy        = vHeight;
+        vBmpHdr.cPlanes   = 1;
+        vBmpHdr.cBitCount = 24;
+
+        HBITMAP                         hBufBitmap = ::GpiCreateBitmap(GetHPS(), &vBmpHdr, 0L, NULL, NULL);
+        POINTL                          aPoint1[4] = { 0, 0
+                                                      ,vWidth, vHeight
+                                                      ,vXdest, vYdest
+                                                      ,vXdest + vWidth, vYdest + vHeight
+                                                     };
+        POINTL                          aPoint2[4] = { 0, 0
+                                                      ,vWidth, vHeight
+                                                      ,vXsrc, vYsrc
+                                                      ,vXsrc + vWidth, vYsrc + vHeight
+                                                     };
+        POINTL                          aPoint3[4] = { vXdest, vYdest
+                                                      ,vXdest + vWidth, vYdest + vHeight
+                                                      ,vXsrc, vYsrc
+                                                      ,vXsrc + vWidth, vYsrc + vHeight
+                                                     };
+        POINTL                          aPoint4[4] = { vXdest, vYdest
+                                                      ,vXdest + vWidth, vYdest + vHeight
+                                                      ,0, 0
+                                                      ,vWidth, vHeight
+                                                     };
+        ::GpiSetBitmap(hPSMask, (HBITMAP) pMask->GetMaskBitmap());
+        ::GpiSetBitmap(hPSBuffer, (HBITMAP) hBufBitmap);
 
-            // copy src to buffer using selected raster op
-            if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
-                           GetHdcOf(*source), xsrc, ysrc, dwRop) )
-            {
-                wxLogLastError(wxT("BitBlt"));
-            }
+        //
+        // Copy dest to buffer
+        //
+        rc = ::GpiBitBlt( hPSBuffer
+                         ,GetHPS()
+                         ,4L
+                         ,aPoint1
+                         ,ROP_SRCCOPY
+                         ,BBO_IGNORE
+                        );
+        if (rc == GPI_ERROR)
+        {
+            wxLogLastError(wxT("BitBlt"));
+        }
 
-            // set masked area in buffer to BLACK (pixel value 0)
-            COLORREF prevBkCol = ::SetBkColor(GetHdc(), RGB(255, 255, 255));
-            COLORREF prevCol = ::SetTextColor(GetHdc(), RGB(0, 0, 0));
-            if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
-                           dc_mask, xsrc, ysrc, SRCAND) )
-            {
-                wxLogLastError(wxT("BitBlt"));
-            }
+        //
+        // Copy src to buffer using selected raster op
+        //
+        rc = ::GpiBitBlt( hPSBuffer
+                         ,GetHPS()
+                         ,4L
+                         ,aPoint2
+                         ,lRop
+                         ,BBO_IGNORE
+                        );
+        if (rc == GPI_ERROR)
+        {
+            wxLogLastError(wxT("BitBlt"));
+        }
 
-            // set unmasked area in dest to BLACK
-            ::SetBkColor(GetHdc(), RGB(0, 0, 0));
-            ::SetTextColor(GetHdc(), RGB(255, 255, 255));
-            if ( !::BitBlt(GetHdc(), xdest, ydest, (int)width, (int)height,
-                           dc_mask, xsrc, ysrc, SRCAND) )
-            {
-                wxLogLastError(wxT("BitBlt"));
-            }
-            ::SetBkColor(GetHdc(), prevBkCol);   // restore colours to original values
-            ::SetTextColor(GetHdc(), prevCol);
-
-            // OR buffer to dest
-            success = ::BitBlt(GetHdc(), xdest, ydest,
-                               (int)width, (int)height,
-                               dc_buffer, 0, 0, SRCPAINT) != 0;
-            if ( !success )
-            {
-                wxLogLastError(wxT("BitBlt"));
-            }
+        //
+        // Set masked area in buffer to BLACK (pixel value 0)
+        //
+        COLORREF                        vPrevBkCol = ::GpiQueryBackColor(GetHPS());
+        COLORREF                        vPrevCol = ::GpiQueryColor(GetHPS());
+
+        ::GpiSetBackColor(GetHPS(), OS2RGB(255, 255, 255));
+        ::GpiSetColor(GetHPS(), OS2RGB(0, 0, 0));
+
+        rc = ::GpiBitBlt( hPSBuffer
+                         ,hPSMask
+                         ,4L
+                         ,aPoint2
+                         ,ROP_SRCAND
+                         ,BBO_IGNORE
+                        );
+        if (rc == GPI_ERROR)
+        {
+            wxLogLastError(wxT("BitBlt"));
+        }
 
-            // tidy up temporary DCs and bitmap
-            ::SelectObject(dc_mask, 0);
-            ::DeleteDC(dc_mask);
-            ::SelectObject(dc_buffer, 0);
-            ::DeleteDC(dc_buffer);
-            ::DeleteObject(buffer_bmap);
+        //
+        // Set unmasked area in dest to BLACK
+        //
+        ::GpiSetBackColor(GetHPS(), OS2RGB(0, 0, 0));
+        ::GpiSetColor(GetHPS(), OS2RGB(255, 255, 255));
+        rc = ::GpiBitBlt( GetHPS()
+                         ,hPSMask
+                         ,4L
+                         ,aPoint3
+                         ,ROP_SRCAND
+                         ,BBO_IGNORE
+                        );
+        if (rc == GPI_ERROR)
+        {
+            wxLogLastError(wxT("BitBlt"));
         }
+
+        //
+        // Restore colours to original values
+        //
+        ::GpiSetBackColor(GetHPS(), vPrevBkCol);
+        ::GpiSetColor(GetHPS(), vPrevCol);
+
+        //
+        // OR buffer to dest
+        //
+        rc = ::GpiBitBlt( GetHPS()
+                         ,hPSMask
+                         ,4L
+                         ,aPoint4
+                         ,ROP_SRCPAINT
+                         ,BBO_IGNORE
+                        );
+        if (rc == GPI_ERROR)
+        {
+            bSuccess = FALSE;
+            wxLogLastError(wxT("BitBlt"));
+        }
+
+        //
+        // Tidy up temporary DCs and bitmap
+        //
+        ::GpiSetBitmap(hPSMask, NULLHANDLE);
+        ::GpiSetBitmap(hPSBuffer, NULLHANDLE);
+        ::GpiDestroyPS(hPSMask);
+        ::GpiDestroyPS(hPSBuffer);
+        ::DevCloseDC(hDCMask);
+        ::DevCloseDC(hDCBuffer);
+        ::GpiDeleteBitmap(hBufBitmap);
+        bSuccess = TRUE;
     }
-#endif
-//    else // no mask, just BitBlt() it
-//    {
+    else // no mask, just BitBlt() it
+    {
+        POINTL                          aPoint[4] = { vXdest, vYdest
+                                                     ,vXdest + vWidth, vYdest + vHeight
+                                                     ,vXsrc, vYsrc
+                                                     ,vXsrc + vWidth, vYsrc + vHeight
+                                                    };
+
         bSuccess = (::GpiBitBlt( m_hPS
                                 ,pSource->GetHPS()
                                 ,4L
@@ -1488,7 +1650,7 @@ bool wxDC::DoBlit(
         {
             wxLogLastError(wxT("BitBlt"));
         }
-//    }
+    }
     vCbnd.lColor = (LONG)vOldTextColor;
     ::GpiSetAttrs( m_hPS           // presentation-space handle
                   ,PRIM_CHAR       // Char primitive.