]> git.saurik.com Git - wxWidgets.git/blob - src/msw/xpmhand.cpp
applied correction from Marc Newsam in calculations of linesize
[wxWidgets.git] / src / msw / xpmhand.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xpmhand.cpp
3 // Purpose: wxBitmap: XPM handler and constructors
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "xpmhand.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/list.h"
25 #include "wx/utils.h"
26 #include "wx/app.h"
27 #include "wx/palette.h"
28 #include "wx/dcmemory.h"
29 #include "wx/bitmap.h"
30 #include "wx/icon.h"
31 #endif
32
33 #include "wx/msw/private.h"
34 #include "wx/log.h"
35
36 #if wxUSE_XPM_IN_MSW
37 #define FOR_MSW 1
38
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"
45 #else
46 #include <xpm.h>
47 #endif
48 #endif
49
50 #include "wx/xpmhand.h"
51 #include "wx/msw/dib.h"
52
53 #if wxUSE_XPM_IN_MSW
54
55 static void XpmToBitmap(wxBitmap *bitmap,
56 const XImage *ximage,
57 const XImage *xmask,
58 const XpmAttributes& xpmAttr)
59 {
60 wxBitmapRefData *refData = bitmap->GetBitmapData();
61 refData->m_hBitmap = (WXHBITMAP)ximage->bitmap;
62
63 // first set the bitmap width, height, depth...
64 BITMAP bm;
65 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) )
66 {
67 wxLogLastError(wxT("GetObject(bitmap)"));
68 }
69
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;
74
75 // GRG Jan/2000, mask support
76 if (xmask)
77 {
78 wxMask *mask = new wxMask();
79 mask->SetMaskBitmap((WXHBITMAP) wxInvertMask(xmask->bitmap,
80 bm.bmWidth, bm.bmHeight));
81 bitmap->SetMask(mask);
82 }
83 }
84
85 #endif // wxUSE_XPM_IN_MSW
86
87 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
88
89 bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
90 const wxString& name,
91 long WXUNUSED(flags),
92 int WXUNUSED(desiredWidth),
93 int WXUNUSED(desiredHeight))
94 {
95 #if wxUSE_XPM_IN_MSW
96 XImage *ximage = NULL;
97 XImage *xmask = NULL;
98 XpmAttributes xpmAttr;
99 HDC dc;
100
101 dc = CreateCompatibleDC(NULL);
102 if (dc)
103 {
104 xpmAttr.valuemask = XpmReturnPixels | XpmColorTable;
105 int errorStatus = XpmReadFileToImage(&dc,
106 wxMBSTRINGCAST name.fn_str(),
107 &ximage,
108 &xmask,
109 &xpmAttr);
110 DeleteDC(dc);
111 if (errorStatus == XpmSuccess)
112 {
113 XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
114
115 XpmFree(xpmAttr.pixels);
116 XpmFreeAttributes(&xpmAttr);
117 XImageFree(ximage);
118 if (xmask)
119 XDestroyImage(xmask);
120 }
121
122 #if WXWIN_COMPATIBILITY_2
123 bitmap->SetOk(errorStatus == XpmSuccess);
124 #endif // WXWIN_COMPATIBILITY_2
125
126 return bitmap->Ok();
127 }
128 #endif // wxUSE_XPM_IN_MSW
129
130 return FALSE;
131 }
132
133 bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
134 const wxString& name,
135 int WXUNUSED(type),
136 const wxPalette *WXUNUSED(palette))
137 {
138 #if wxUSE_XPM_IN_MSW
139 XImage ximage;
140 XImage xmask;
141 bool hasmask = FALSE;
142
143 HDC dc = CreateCompatibleDC(NULL);
144 if (dc)
145 {
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;
152
153 // GRG Jan/2000, mask support
154 hasmask = (refData->m_bitmapMask != NULL);
155 if (hasmask)
156 {
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...
161 */
162 xmask.bitmap = (HBITMAP) refData->m_bitmapMask->GetMaskBitmap();
163 xmask.width = refData->m_width;
164 xmask.height = refData->m_height;
165 xmask.depth = 1;
166 }
167
168 int errorStatus = XpmWriteFileFromImage(
169 &dc,
170 wxMBSTRINGCAST name.fn_str(),
171 &ximage,
172 (hasmask? &xmask : (XImage *)NULL),
173 (XpmAttributes *) NULL);
174
175 DeleteDC(dc);
176
177 return (errorStatus == XpmSuccess);
178 }
179 #endif // !wxUSE_XPM_IN_MSW
180
181 return FALSE;
182 }
183
184 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
185
186 bool wxXPMDataHandler::Create(wxBitmap *bitmap,
187 void *data,
188 long WXUNUSED(flags),
189 int WXUNUSED(width),
190 int WXUNUSED(height),
191 int WXUNUSED(depth))
192 {
193 #if wxUSE_XPM_IN_MSW
194 XImage *ximage = NULL;
195 XImage *xmask = NULL;
196 XpmAttributes xpmAttr;
197
198 HDC dc = CreateCompatibleDC(NULL); /* memory DC */
199
200 if (dc)
201 {
202 xpmAttr.valuemask = XpmReturnInfos | XpmColorTable;
203 int errorStatus = XpmCreateImageFromData(&dc, (char **)data,
204 &ximage,
205 &xmask,
206 &xpmAttr);
207 DeleteDC(dc);
208
209 if ( errorStatus == XpmSuccess )
210 {
211 XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
212
213 XpmFree(xpmAttr.pixels);
214 XpmFreeAttributes(&xpmAttr);
215 XImageFree(ximage); // releases the malloc, but does not destroy bitmap
216 if (xmask)
217 XDestroyImage(xmask);
218 }
219
220 #if WXWIN_COMPATIBILITY_2
221 bitmap->SetOk(errorStatus == XpmSuccess);
222 #endif // WXWIN_COMPATIBILITY_2
223
224 return bitmap->Ok();
225 }
226 #endif // wxUSE_XPM_IN_MSW
227
228 return FALSE;
229 }
230