]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/xpmhand.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmap: XPM handler and constructors
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "xpmhand.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/palette.h"
28 #include "wx/dcmemory.h"
29 #include "wx/bitmap.h"
33 #include "wx/msw/private.h"
38 #include "../xpm/xpm34.h"
41 #include "wx/xpmhand.h"
42 #include "wx/msw/dib.h"
46 static void XpmToBitmap(wxBitmap
*bitmap
,
48 const XpmAttributes
& xpmAttr
)
50 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
51 refData
->m_hBitmap
= (WXHBITMAP
)ximage
->bitmap
;
53 // first set the bitmap width, height, depth...
55 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(bm
), (LPSTR
) & bm
) )
57 wxLogLastError("GetObject(bitmap)");
60 refData
->m_width
= bm
.bmWidth
;
61 refData
->m_height
= bm
.bmHeight
;
62 refData
->m_depth
= bm
.bmPlanes
* bm
.bmBitsPixel
;
63 refData
->m_numColors
= xpmAttr
.npixels
;
65 // next get the mask, if any
66 if ( xpmAttr
.mask_pixel
!= XpmUndefPixel
)
69 const char *clrString
= xpmAttr
.colorTable
[xpmAttr
.mask_pixel
].c_color
;
70 if ( strcmp(clrString
, "None") == 0 )
72 // TODO what to do here??
78 sscanf(clrString
, "#%02x%02x%02x", &red
, &green
, &blue
);
81 wxMask
*mask
= new wxMask(*bitmap
, wxColour(red
, green
, blue
));
82 bitmap
->SetMask(mask
);
86 #endif // wxUSE_XPM_IN_MSW
88 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
90 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
,
93 int desiredWidth
, int desiredHeight
)
97 XpmAttributes xpmAttr
;
100 dc
= CreateCompatibleDC(NULL
);
103 xpmAttr
.valuemask
= XpmReturnPixels
| XpmColorTable
;
104 int errorStatus
= XpmReadFileToImage(&dc
,
105 wxMBSTRINGCAST name
.fn_str(),
110 if (errorStatus
== XpmSuccess
)
112 XpmToBitmap(bitmap
, ximage
, xpmAttr
);
114 XpmFree(xpmAttr
.pixels
);
115 XpmFreeAttributes(&xpmAttr
);
119 #if WXWIN_COMPATIBILITY_2
120 bitmap
->SetOk(errorStatus
== XpmSuccess
);
121 #endif // WXWIN_COMPATIBILITY_2
125 #endif // wxUSE_XPM_IN_MSW
130 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
,
131 const wxString
& name
,
133 const wxPalette
*palette
)
138 HDC dc
= CreateCompatibleDC(NULL
);
141 if ( SelectObject(dc
, GetHbitmapOf(*bitmap
)) )
143 /* for following SetPixel */
144 /* fill the XImage struct 'by hand' */
145 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
146 ximage
.width
= refData
->m_width
;
147 ximage
.height
= refData
->m_height
;
148 ximage
.depth
= refData
->m_depth
;
149 ximage
.bitmap
= (HBITMAP
)refData
->m_hBitmap
;
150 int errorStatus
= XpmWriteFileFromImage(&dc
, wxMBSTRINGCAST name
.fn_str(),
151 &ximage
, (XImage
*) NULL
,
152 (XpmAttributes
*) NULL
);
157 if (errorStatus
== XpmSuccess
)
158 return TRUE
; /* no error */
161 #endif // !wxUSE_XPM_IN_MSW
166 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
168 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
,
177 XpmAttributes xpmAttr
;
179 HDC dc
= CreateCompatibleDC(NULL
); /* memory DC */
183 xpmAttr
.valuemask
= XpmReturnInfos
| XpmColorTable
;
184 int errorStatus
= XpmCreateImageFromData(&dc
, (char **)data
,
190 if ( errorStatus
== XpmSuccess
)
192 XpmToBitmap(bitmap
, ximage
, xpmAttr
);
194 XpmFree(xpmAttr
.pixels
);
195 XpmFreeAttributes(&xpmAttr
);
196 XImageFree(ximage
); // releases the malloc, but does not destroy bitmap
199 #if WXWIN_COMPATIBILITY_2
200 bitmap
->SetOk(errorStatus
== XpmSuccess
);
201 #endif // WXWIN_COMPATIBILITY_2
205 #endif // wxUSE_XPM_IN_MSW