| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: os2/xpmhand.cpp |
| 3 | // Purpose: wxBitmap: XPM handler and constructors |
| 4 | // Author: Julian Smart |
| 5 | // Modified for OS/2 by: Evgeny Kotsuba |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "xpmhand.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | //#include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include "wx/list.h" |
| 25 | #include "wx/utils.h" |
| 26 | #include "wx/app.h" |
| 27 | #include "wx/palette.h" |
| 28 | #include "wx/dcmemory.h" |
| 29 | #include "wx/bitmap.h" |
| 30 | #include "wx/icon.h" |
| 31 | #endif |
| 32 | |
| 33 | #include "wx/os2/private.h" |
| 34 | #include "wx/log.h" |
| 35 | |
| 36 | |
| 37 | #if wxUSE_XPM_IN_OS2 |
| 38 | #define FOR_MSW 1 |
| 39 | #include "../xpm/xpm.h" |
| 40 | #endif |
| 41 | |
| 42 | #include "wx/xpmhand.h" |
| 43 | |
| 44 | #if wxUSE_XPM_IN_OS2 |
| 45 | |
| 46 | static void XpmToBitmap( |
| 47 | wxBitmap* pBitmap |
| 48 | , const XImage* pXimage |
| 49 | , const XImage* pXmask |
| 50 | , const XpmAttributes& rXpmAttr |
| 51 | ) |
| 52 | { |
| 53 | wxBitmapRefData* pRefData = pBitmap->GetBitmapData(); |
| 54 | pRefData->m_hBitmap = (WXHBITMAP)pXimage->bitmap; |
| 55 | |
| 56 | // |
| 57 | // First set the bitmap width, height, depth... |
| 58 | // |
| 59 | BITMAPINFOHEADER vBm; |
| 60 | |
| 61 | if (!::GpiQueryBitmapParameters( GetHbitmapOf(*pBitmap) |
| 62 | ,&vBm |
| 63 | )) |
| 64 | { |
| 65 | wxLogLastError(wxT("GetObject(pBitmap)")); |
| 66 | } |
| 67 | |
| 68 | pRefData->m_nWidth = vBm.cx; |
| 69 | pRefData->m_nHeight = vBm.cy; |
| 70 | pRefData->m_nDepth = vBm.cPlanes * vBm.cBitCount; |
| 71 | pRefData->m_nNumColors = rXpmAttr.npixels; |
| 72 | |
| 73 | if (pXmask) |
| 74 | { |
| 75 | wxMask* pMask = new wxMask(); |
| 76 | |
| 77 | pMask->SetMaskBitmap((WXHBITMAP) wxInvertMask( pXmask->bitmap |
| 78 | ,vBm.cx |
| 79 | ,vBm.cy |
| 80 | )); |
| 81 | pBitmap->SetMask(pMask); |
| 82 | } |
| 83 | } // end of XpmToBitmap |
| 84 | |
| 85 | IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) |
| 86 | |
| 87 | bool wxXPMFileHandler::LoadFile( |
| 88 | wxBitmap* pBitmap |
| 89 | , const wxString& rName |
| 90 | , long lFlags |
| 91 | , int lDesiredWidth |
| 92 | , int lDesiredHeight |
| 93 | ) |
| 94 | { |
| 95 | XImage* pXimage = NULL; |
| 96 | XImage* pXmask = NULL; |
| 97 | XpmAttributes vXpmAttr; |
| 98 | HDC hDC; |
| 99 | HPS hPS; |
| 100 | DEVOPENSTRUC dOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L}; |
| 101 | SIZEL vSizl; |
| 102 | |
| 103 | hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&dOP, NULLHANDLE); |
| 104 | hPS = ::GpiCreatePS(vHabmain, hDC, &vSizl, PU_PELS | GPIT_MICRO | GPIA_ASSOC); |
| 105 | if (hPS) |
| 106 | { |
| 107 | vXpmAttr.valuemask = XpmReturnPixels | XpmColorTable; |
| 108 | |
| 109 | int nErrorStatus = XpmReadFileToImage( &hPS |
| 110 | ,wxMBSTRINGCAST rName.fn_str() |
| 111 | ,&pXimage |
| 112 | ,&pXmask |
| 113 | ,&vXpmAttr |
| 114 | ); |
| 115 | ::GpiDestroyPS(hPS); |
| 116 | ::DevCloseDC(hDC); |
| 117 | if (nErrorStatus == XpmSuccess) |
| 118 | { |
| 119 | XpmToBitmap( pBitmap |
| 120 | ,pXimage |
| 121 | ,pXmask |
| 122 | ,vXpmAttr |
| 123 | ); |
| 124 | XpmFree(vXpmAttr.pixels); |
| 125 | XpmFreeAttributes(&vXpmAttr); |
| 126 | XImageFree(pXimage); |
| 127 | if (pXmask) |
| 128 | XDestroyImage(pXmask); |
| 129 | } |
| 130 | return pBitmap->Ok(); |
| 131 | } |
| 132 | return FALSE; |
| 133 | } // end of wxXPMFileHandler::LoadFile |
| 134 | |
| 135 | bool wxXPMFileHandler::SaveFile( |
| 136 | wxBitmap* pBitmap |
| 137 | , const wxString& rName |
| 138 | , int lType |
| 139 | , const wxPalette* pPalette |
| 140 | ) |
| 141 | { |
| 142 | XImage vXimage; |
| 143 | XImage vXmask; |
| 144 | bool bHasmask = FALSE; |
| 145 | HDC hDC; |
| 146 | HPS hPS; |
| 147 | DEVOPENSTRUC dOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L}; |
| 148 | SIZEL vSizl; |
| 149 | |
| 150 | hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&dOP, NULLHANDLE); |
| 151 | hPS = ::GpiCreatePS(vHabmain, hDC, &vSizl, PU_PELS | GPIT_MICRO | GPIA_ASSOC); |
| 152 | if (hPS) |
| 153 | { |
| 154 | // fill the XImage struct 'by hand' |
| 155 | wxBitmapRefData* pRefData = pBitmap->GetBitmapData(); |
| 156 | |
| 157 | vXimage.width = pRefData->m_nWidth; |
| 158 | vXimage.height = pRefData->m_nHeight; |
| 159 | vXimage.depth = pRefData->m_nDepth; |
| 160 | vXimage.bitmap = (HBITMAP)pRefData->m_hBitmap; |
| 161 | |
| 162 | bHasmask = (pRefData->m_pBitmapMask != NULL); |
| 163 | if (bHasmask) |
| 164 | { |
| 165 | // |
| 166 | // Strangely enough, the MSW version of xpmlib is not |
| 167 | // coherent with itself regarding masks; we have to invert |
| 168 | // the mask we get when loading, but we still must pass it |
| 169 | // 'as is' when saving... |
| 170 | // |
| 171 | vXmask.bitmap = (HBITMAP)pRefData->m_pBitmapMask->GetMaskBitmap(); |
| 172 | vXmask.width = pRefData->m_nWidth; |
| 173 | vXmask.height = pRefData->m_nHeight; |
| 174 | vXmask.depth = 1; |
| 175 | } |
| 176 | |
| 177 | int nErrorStatus = XpmWriteFileFromImage( &hPS |
| 178 | ,wxMBSTRINGCAST rName.fn_str() |
| 179 | ,&vXimage |
| 180 | ,(bHasmask? &vXmask : (XImage *)NULL) |
| 181 | ,(XpmAttributes *) NULL |
| 182 | ); |
| 183 | ::GpiDestroyPS(hPS); |
| 184 | ::DevCloseDC(hDC); |
| 185 | return (nErrorStatus == XpmSuccess); |
| 186 | } |
| 187 | return FALSE; |
| 188 | } // end of wxXPMFileHandler::SaveFile |
| 189 | |
| 190 | IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) |
| 191 | |
| 192 | bool wxXPMDataHandler::Create( |
| 193 | wxBitmap* pBitmap |
| 194 | , void* pData |
| 195 | , long lFlags |
| 196 | , int nWidth |
| 197 | , int nHeight |
| 198 | , int nDepth |
| 199 | ) |
| 200 | { |
| 201 | XImage* pXimage = NULL; |
| 202 | XImage* pXmask = NULL; |
| 203 | XpmAttributes vXpmAttr; |
| 204 | HDC hDC; |
| 205 | HPS hPS; |
| 206 | DEVOPENSTRUC dOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L}; |
| 207 | SIZEL vSizl; |
| 208 | |
| 209 | hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&dOP, NULLHANDLE); |
| 210 | hPS = ::GpiCreatePS(vHabmain, hDC, &vSizl, PU_PELS | GPIT_MICRO | GPIA_ASSOC); |
| 211 | if (hPS) |
| 212 | { |
| 213 | vXpmAttr.valuemask = XpmReturnInfos | XpmColorTable; |
| 214 | |
| 215 | int nErrorStatus = XpmCreateImageFromData( &hPS |
| 216 | ,(char **)pData |
| 217 | ,&pXimage |
| 218 | ,&pXmask |
| 219 | ,&vXpmAttr |
| 220 | ); |
| 221 | ::GpiDestroyPS(hPS); |
| 222 | ::DevCloseDC(hDC); |
| 223 | if (nErrorStatus == XpmSuccess) |
| 224 | { |
| 225 | XpmToBitmap( pBitmap |
| 226 | ,pXimage |
| 227 | ,pXmask |
| 228 | ,vXpmAttr |
| 229 | ); |
| 230 | XpmFree(vXpmAttr.pixels); |
| 231 | XpmFreeAttributes(&vXpmAttr); |
| 232 | XImageFree(pXimage); // releases the malloc, but does not destroy bitmap |
| 233 | if (pXmask) |
| 234 | XDestroyImage(pXmask); |
| 235 | } |
| 236 | return pBitmap->Ok(); |
| 237 | } |
| 238 | return FALSE; |
| 239 | } // end of wxXPMDataHandler::Create |
| 240 | |
| 241 | #endif // wxUSE_XPM_IN_OS2 |