]>
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 WXUNUSED(desiredWidth
),
93 int WXUNUSED(desiredHeight
))
96 XImage
*ximage
= NULL
;
98 XpmAttributes xpmAttr
;
101 dc
= CreateCompatibleDC(NULL
);
104 xpmAttr
.valuemask
= XpmReturnPixels
| XpmColorTable
;
105 int errorStatus
= XpmReadFileToImage(&dc
,
106 wxMBSTRINGCAST name
.fn_str(),
111 if (errorStatus
== XpmSuccess
)
113 XpmToBitmap(bitmap
, ximage
, xmask
, xpmAttr
);
115 XpmFree(xpmAttr
.pixels
);
116 XpmFreeAttributes(&xpmAttr
);
119 XDestroyImage(xmask
);
122 #if WXWIN_COMPATIBILITY_2
123 bitmap
->SetOk(errorStatus
== XpmSuccess
);
124 #endif // WXWIN_COMPATIBILITY_2
128 #endif // wxUSE_XPM_IN_MSW
133 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
,
134 const wxString
& name
,
136 const wxPalette
*WXUNUSED(palette
))
141 bool hasmask
= FALSE
;
143 HDC dc
= CreateCompatibleDC(NULL
);
146 /* fill the XImage struct 'by hand' */
147 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
148 ximage
.width
= refData
->m_width
;
149 ximage
.height
= refData
->m_height
;
150 ximage
.depth
= refData
->m_depth
;
151 ximage
.bitmap
= (HBITMAP
)refData
->m_hBitmap
;
153 // GRG Jan/2000, mask support
154 hasmask
= (refData
->m_bitmapMask
!= NULL
);
157 /* Strangely enough, the MSW version of xpmlib is not
158 * coherent with itself regarding masks; we have to invert
159 * the mask we get when loading, but we still must pass it
160 * 'as is' when saving...
162 xmask
.bitmap
= (HBITMAP
) refData
->m_bitmapMask
->GetMaskBitmap();
163 xmask
.width
= refData
->m_width
;
164 xmask
.height
= refData
->m_height
;
168 int errorStatus
= XpmWriteFileFromImage(
170 wxMBSTRINGCAST name
.fn_str(),
172 (hasmask
? &xmask
: (XImage
*)NULL
),
173 (XpmAttributes
*) NULL
);
177 return (errorStatus
== XpmSuccess
);
179 #endif // !wxUSE_XPM_IN_MSW
184 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
186 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
,
188 long WXUNUSED(flags
),
190 int WXUNUSED(height
),
194 XImage
*ximage
= NULL
;
195 XImage
*xmask
= NULL
;
196 XpmAttributes xpmAttr
;
198 HDC dc
= CreateCompatibleDC(NULL
); /* memory DC */
202 xpmAttr
.valuemask
= XpmReturnInfos
| XpmColorTable
;
203 int errorStatus
= XpmCreateImageFromData(&dc
, (char **)data
,
209 if ( errorStatus
== XpmSuccess
)
211 XpmToBitmap(bitmap
, ximage
, xmask
, xpmAttr
);
213 XpmFree(xpmAttr
.pixels
);
214 XpmFreeAttributes(&xpmAttr
);
215 XImageFree(ximage
); // releases the malloc, but does not destroy bitmap
217 XDestroyImage(xmask
);
220 #if WXWIN_COMPATIBILITY_2
221 bitmap
->SetOk(errorStatus
== XpmSuccess
);
222 #endif // WXWIN_COMPATIBILITY_2
226 #endif // wxUSE_XPM_IN_MSW