]>
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"
39 // allow the user to use the system-wide xpm.h by defining
40 // wxUSE_XPM_H_IN_PATH (and always use xpm.h in path if __WX_SETUP_H__ is
41 // defined which means that we use configure as it always add -I../xpm to
42 // the include path if needed)
43 #if !defined(__WX_SETUP_H__) && !defined(wxUSE_XPM_H_IN_PATH)
44 #include "../xpm/xpm.h"
50 #include "wx/xpmhand.h"
51 #include "wx/msw/dib.h"
55 static void XpmToBitmap(wxBitmap
*bitmap
,
58 const XpmAttributes
& xpmAttr
)
60 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
61 refData
->m_hBitmap
= (WXHBITMAP
)ximage
->bitmap
;
63 // first set the bitmap width, height, depth...
65 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(bm
), (LPSTR
) & bm
) )
67 wxLogLastError(wxT("GetObject(bitmap)"));
70 refData
->m_width
= bm
.bmWidth
;
71 refData
->m_height
= bm
.bmHeight
;
72 refData
->m_depth
= bm
.bmPlanes
* bm
.bmBitsPixel
;
73 refData
->m_numColors
= xpmAttr
.npixels
;
75 // GRG Jan/2000, mask support
78 wxMask
*mask
= new wxMask();
79 mask
->SetMaskBitmap((WXHBITMAP
) wxInvertMask(xmask
->bitmap
,
80 bm
.bmWidth
, bm
.bmHeight
));
81 bitmap
->SetMask(mask
);
85 #endif // wxUSE_XPM_IN_MSW
87 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
89 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
,
92 int desiredWidth
, int desiredHeight
)
95 XImage
*ximage
= NULL
;
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
, xmask
, xpmAttr
);
114 XpmFree(xpmAttr
.pixels
);
115 XpmFreeAttributes(&xpmAttr
);
118 XDestroyImage(xmask
);
121 #if WXWIN_COMPATIBILITY_2
122 bitmap
->SetOk(errorStatus
== XpmSuccess
);
123 #endif // WXWIN_COMPATIBILITY_2
127 #endif // wxUSE_XPM_IN_MSW
132 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
,
133 const wxString
& name
,
135 const wxPalette
*palette
)
140 bool hasmask
= FALSE
;
142 HDC dc
= CreateCompatibleDC(NULL
);
145 /* fill the XImage struct 'by hand' */
146 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
147 ximage
.width
= refData
->m_width
;
148 ximage
.height
= refData
->m_height
;
149 ximage
.depth
= refData
->m_depth
;
150 ximage
.bitmap
= (HBITMAP
)refData
->m_hBitmap
;
152 // GRG Jan/2000, mask support
153 hasmask
= (refData
->m_bitmapMask
!= NULL
);
156 /* Strangely enough, the MSW version of xpmlib is not
157 * coherent with itself regarding masks; we have to invert
158 * the mask we get when loading, but we still must pass it
159 * 'as is' when saving...
161 xmask
.bitmap
= (HBITMAP
) refData
->m_bitmapMask
->GetMaskBitmap();
162 xmask
.width
= refData
->m_width
;
163 xmask
.height
= refData
->m_height
;
167 int errorStatus
= XpmWriteFileFromImage(
169 wxMBSTRINGCAST name
.fn_str(),
171 (hasmask
? &xmask
: (XImage
*)NULL
),
172 (XpmAttributes
*) NULL
);
176 return (errorStatus
== XpmSuccess
);
178 #endif // !wxUSE_XPM_IN_MSW
183 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
185 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
,
193 XImage
*ximage
= NULL
;
194 XImage
*xmask
= NULL
;
195 XpmAttributes xpmAttr
;
197 HDC dc
= CreateCompatibleDC(NULL
); /* memory DC */
201 xpmAttr
.valuemask
= XpmReturnInfos
| XpmColorTable
;
202 int errorStatus
= XpmCreateImageFromData(&dc
, (char **)data
,
208 if ( errorStatus
== XpmSuccess
)
210 XpmToBitmap(bitmap
, ximage
, xmask
, xpmAttr
);
212 XpmFree(xpmAttr
.pixels
);
213 XpmFreeAttributes(&xpmAttr
);
214 XImageFree(ximage
); // releases the malloc, but does not destroy bitmap
216 XDestroyImage(xmask
);
219 #if WXWIN_COMPATIBILITY_2
220 bitmap
->SetOk(errorStatus
== XpmSuccess
);
221 #endif // WXWIN_COMPATIBILITY_2
225 #endif // wxUSE_XPM_IN_MSW