]>
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 bm
.bmWidth
, bm
.bmHeight
));
72 bitmap
->SetMask(mask
);
76 #endif // wxUSE_XPM_IN_MSW
78 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
80 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
,
83 int desiredWidth
, int desiredHeight
)
86 XImage
*ximage
= NULL
;
88 XpmAttributes xpmAttr
;
91 dc
= CreateCompatibleDC(NULL
);
94 xpmAttr
.valuemask
= XpmReturnPixels
| XpmColorTable
;
95 int errorStatus
= XpmReadFileToImage(&dc
,
96 wxMBSTRINGCAST name
.fn_str(),
101 if (errorStatus
== XpmSuccess
)
103 XpmToBitmap(bitmap
, ximage
, xmask
, xpmAttr
);
105 XpmFree(xpmAttr
.pixels
);
106 XpmFreeAttributes(&xpmAttr
);
109 XDestroyImage(xmask
);
112 #if WXWIN_COMPATIBILITY_2
113 bitmap
->SetOk(errorStatus
== XpmSuccess
);
114 #endif // WXWIN_COMPATIBILITY_2
118 #endif // wxUSE_XPM_IN_MSW
123 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
,
124 const wxString
& name
,
126 const wxPalette
*palette
)
131 bool hasmask
= FALSE
;
133 HDC dc
= CreateCompatibleDC(NULL
);
136 /* fill the XImage struct 'by hand' */
137 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
138 ximage
.width
= refData
->m_width
;
139 ximage
.height
= refData
->m_height
;
140 ximage
.depth
= refData
->m_depth
;
141 ximage
.bitmap
= (HBITMAP
)refData
->m_hBitmap
;
143 // GRG Jan/2000, mask support
144 hasmask
= (refData
->m_bitmapMask
!= NULL
);
147 /* Strangely enough, the MSW version of xpmlib is not
148 * coherent with itself regarding masks; we have to invert
149 * the mask we get when loading, but we still must pass it
150 * 'as is' when saving...
152 xmask
.bitmap
= (HBITMAP
) refData
->m_bitmapMask
->GetMaskBitmap();
153 xmask
.width
= refData
->m_width
;
154 xmask
.height
= refData
->m_height
;
158 int errorStatus
= XpmWriteFileFromImage(
160 wxMBSTRINGCAST name
.fn_str(),
162 (hasmask
? &xmask
: (XImage
*)NULL
),
163 (XpmAttributes
*) NULL
);
167 return (errorStatus
== XpmSuccess
);
169 #endif // !wxUSE_XPM_IN_MSW
174 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
176 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
,
184 XImage
*ximage
= NULL
;
185 XImage
*xmask
= NULL
;
186 XpmAttributes xpmAttr
;
188 HDC dc
= CreateCompatibleDC(NULL
); /* memory DC */
192 xpmAttr
.valuemask
= XpmReturnInfos
| XpmColorTable
;
193 int errorStatus
= XpmCreateImageFromData(&dc
, (char **)data
,
199 if ( errorStatus
== XpmSuccess
)
201 XpmToBitmap(bitmap
, ximage
, xmask
, xpmAttr
);
203 XpmFree(xpmAttr
.pixels
);
204 XpmFreeAttributes(&xpmAttr
);
205 XImageFree(ximage
); // releases the malloc, but does not destroy bitmap
207 XDestroyImage(xmask
);
210 #if WXWIN_COMPATIBILITY_2
211 bitmap
->SetOk(errorStatus
== XpmSuccess
);
212 #endif // WXWIN_COMPATIBILITY_2
216 #endif // wxUSE_XPM_IN_MSW