]>
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/xpm.h"
41 #include "wx/xpmhand.h"
42 #include "wx/msw/dib.h"
46 static void XpmToBitmap(wxBitmap
*bitmap
,
49 const XpmAttributes
& xpmAttr
)
51 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
52 refData
->m_hBitmap
= (WXHBITMAP
)ximage
->bitmap
;
54 // first set the bitmap width, height, depth...
56 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(bm
), (LPSTR
) & bm
) )
58 wxLogLastError(wxT("GetObject(bitmap)"));
61 refData
->m_width
= bm
.bmWidth
;
62 refData
->m_height
= bm
.bmHeight
;
63 refData
->m_depth
= bm
.bmPlanes
* bm
.bmBitsPixel
;
64 refData
->m_numColors
= xpmAttr
.npixels
;
66 // GRG Jan/2000, mask support
69 wxMask
*mask
= new wxMask();
70 mask
->SetMaskBitmap((WXHBITMAP
) wxInvertMask(xmask
->bitmap
));
71 bitmap
->SetMask(mask
);
75 #endif // wxUSE_XPM_IN_MSW
77 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
79 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
,
82 int desiredWidth
, int desiredHeight
)
85 XImage
*ximage
= NULL
;
87 XpmAttributes xpmAttr
;
90 dc
= CreateCompatibleDC(NULL
);
93 xpmAttr
.valuemask
= XpmReturnPixels
| XpmColorTable
;
94 int errorStatus
= XpmReadFileToImage(&dc
,
95 wxMBSTRINGCAST name
.fn_str(),
100 if (errorStatus
== XpmSuccess
)
102 XpmToBitmap(bitmap
, ximage
, xmask
, xpmAttr
);
104 XpmFree(xpmAttr
.pixels
);
105 XpmFreeAttributes(&xpmAttr
);
108 XDestroyImage(xmask
);
111 #if WXWIN_COMPATIBILITY_2
112 bitmap
->SetOk(errorStatus
== XpmSuccess
);
113 #endif // WXWIN_COMPATIBILITY_2
117 #endif // wxUSE_XPM_IN_MSW
122 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
,
123 const wxString
& name
,
125 const wxPalette
*palette
)
130 bool hasmask
= FALSE
;
132 HDC dc
= CreateCompatibleDC(NULL
);
135 /* fill the XImage struct 'by hand' */
136 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
137 ximage
.width
= refData
->m_width
;
138 ximage
.height
= refData
->m_height
;
139 ximage
.depth
= refData
->m_depth
;
140 ximage
.bitmap
= (HBITMAP
)refData
->m_hBitmap
;
142 // GRG Jan/2000, mask support
143 hasmask
= (refData
->m_bitmapMask
!= NULL
);
146 /* Strangely enough, the MSW version of xpmlib is not
147 * coherent with itself regarding masks; we have to invert
148 * the mask we get when loading, but we still must pass it
149 * 'as is' when saving...
151 xmask
.bitmap
= (HBITMAP
) refData
->m_bitmapMask
->GetMaskBitmap();
152 xmask
.width
= refData
->m_width
;
153 xmask
.height
= refData
->m_height
;
157 int errorStatus
= XpmWriteFileFromImage(
159 wxMBSTRINGCAST name
.fn_str(),
161 (hasmask
? &xmask
: (XImage
*)NULL
),
162 (XpmAttributes
*) NULL
);
166 return (errorStatus
== XpmSuccess
);
168 #endif // !wxUSE_XPM_IN_MSW
173 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
175 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
,
183 XImage
*ximage
= NULL
;
184 XImage
*xmask
= NULL
;
185 XpmAttributes xpmAttr
;
187 HDC dc
= CreateCompatibleDC(NULL
); /* memory DC */
191 xpmAttr
.valuemask
= XpmReturnInfos
| XpmColorTable
;
192 int errorStatus
= XpmCreateImageFromData(&dc
, (char **)data
,
198 if ( errorStatus
== XpmSuccess
)
200 XpmToBitmap(bitmap
, ximage
, xmask
, xpmAttr
);
202 XpmFree(xpmAttr
.pixels
);
203 XpmFreeAttributes(&xpmAttr
);
204 XImageFree(ximage
); // releases the malloc, but does not destroy bitmap
206 XDestroyImage(xmask
);
209 #if WXWIN_COMPATIBILITY_2
210 bitmap
->SetOk(errorStatus
== XpmSuccess
);
211 #endif // WXWIN_COMPATIBILITY_2
215 #endif // wxUSE_XPM_IN_MSW