]>
Commit | Line | Data |
---|---|---|
2fd284a4 JS |
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__ | |
10a0bdb1 | 13 | #pragma implementation "xpmhand.h" |
2fd284a4 JS |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
10a0bdb1 | 20 | #pragma hdrstop |
2fd284a4 JS |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
10a0bdb1 VZ |
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" | |
2fd284a4 JS |
31 | #endif |
32 | ||
33 | #include "wx/msw/private.h" | |
34 | #include "wx/log.h" | |
35 | ||
2fd284a4 | 36 | #if wxUSE_XPM_IN_MSW |
10a0bdb1 | 37 | #define FOR_MSW 1 |
9b5ae11a VZ |
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 | |
2fd284a4 JS |
48 | #endif |
49 | ||
50 | #include "wx/xpmhand.h" | |
51 | #include "wx/msw/dib.h" | |
52 | ||
10a0bdb1 VZ |
53 | #if wxUSE_XPM_IN_MSW |
54 | ||
0d0512bd VZ |
55 | static void XpmToBitmap(wxBitmap *bitmap, |
56 | const XImage *ximage, | |
9853e3b7 | 57 | const XImage *xmask, |
0d0512bd VZ |
58 | const XpmAttributes& xpmAttr) |
59 | { | |
60 | wxBitmapRefData *refData = bitmap->GetBitmapData(); | |
61 | refData->m_hBitmap = (WXHBITMAP)ximage->bitmap; | |
62 | ||
4b7f2165 | 63 | // first set the bitmap width, height, depth... |
0d0512bd VZ |
64 | BITMAP bm; |
65 | if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) ) | |
66 | { | |
f6bcfd97 | 67 | wxLogLastError(wxT("GetObject(bitmap)")); |
0d0512bd VZ |
68 | } |
69 | ||
9853e3b7 GRG |
70 | refData->m_width = bm.bmWidth; |
71 | refData->m_height = bm.bmHeight; | |
72 | refData->m_depth = bm.bmPlanes * bm.bmBitsPixel; | |
0d0512bd | 73 | refData->m_numColors = xpmAttr.npixels; |
4b7f2165 | 74 | |
9853e3b7 GRG |
75 | // GRG Jan/2000, mask support |
76 | if (xmask) | |
4b7f2165 | 77 | { |
9853e3b7 | 78 | wxMask *mask = new wxMask(); |
3ca6a5f0 BP |
79 | mask->SetMaskBitmap((WXHBITMAP) wxInvertMask(xmask->bitmap, |
80 | bm.bmWidth, bm.bmHeight)); | |
4b7f2165 VZ |
81 | bitmap->SetMask(mask); |
82 | } | |
0d0512bd VZ |
83 | } |
84 | ||
10a0bdb1 VZ |
85 | #endif // wxUSE_XPM_IN_MSW |
86 | ||
2fd284a4 JS |
87 | IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) |
88 | ||
10a0bdb1 VZ |
89 | bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, |
90 | const wxString& name, | |
33ac7e6f KB |
91 | long WXUNUSED(flags), |
92 | int WXUNUSED(desiredWidth), | |
93 | int WXUNUSED(desiredHeight)) | |
2fd284a4 JS |
94 | { |
95 | #if wxUSE_XPM_IN_MSW | |
9853e3b7 GRG |
96 | XImage *ximage = NULL; |
97 | XImage *xmask = NULL; | |
2fd284a4 JS |
98 | XpmAttributes xpmAttr; |
99 | HDC dc; | |
100 | ||
2fd284a4 JS |
101 | dc = CreateCompatibleDC(NULL); |
102 | if (dc) | |
103 | { | |
4b7f2165 VZ |
104 | xpmAttr.valuemask = XpmReturnPixels | XpmColorTable; |
105 | int errorStatus = XpmReadFileToImage(&dc, | |
106 | wxMBSTRINGCAST name.fn_str(), | |
107 | &ximage, | |
9853e3b7 | 108 | &xmask, |
4b7f2165 VZ |
109 | &xpmAttr); |
110 | DeleteDC(dc); | |
111 | if (errorStatus == XpmSuccess) | |
112 | { | |
9853e3b7 | 113 | XpmToBitmap(bitmap, ximage, xmask, xpmAttr); |
2fd284a4 | 114 | |
4b7f2165 VZ |
115 | XpmFree(xpmAttr.pixels); |
116 | XpmFreeAttributes(&xpmAttr); | |
117 | XImageFree(ximage); | |
9853e3b7 GRG |
118 | if (xmask) |
119 | XDestroyImage(xmask); | |
4b7f2165 | 120 | } |
0d0512bd VZ |
121 | |
122 | #if WXWIN_COMPATIBILITY_2 | |
4b7f2165 | 123 | bitmap->SetOk(errorStatus == XpmSuccess); |
0d0512bd VZ |
124 | #endif // WXWIN_COMPATIBILITY_2 |
125 | ||
4b7f2165 | 126 | return bitmap->Ok(); |
2fd284a4 | 127 | } |
4b7f2165 | 128 | #endif // wxUSE_XPM_IN_MSW |
2fd284a4 JS |
129 | |
130 | return FALSE; | |
131 | } | |
132 | ||
10a0bdb1 VZ |
133 | bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, |
134 | const wxString& name, | |
33ac7e6f KB |
135 | int WXUNUSED(type), |
136 | const wxPalette *WXUNUSED(palette)) | |
2fd284a4 JS |
137 | { |
138 | #if wxUSE_XPM_IN_MSW | |
4b7f2165 | 139 | XImage ximage; |
9853e3b7 GRG |
140 | XImage xmask; |
141 | bool hasmask = FALSE; | |
0d0512bd | 142 | |
4b7f2165 | 143 | HDC dc = CreateCompatibleDC(NULL); |
2fd284a4 JS |
144 | if (dc) |
145 | { | |
9853e3b7 GRG |
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) | |
2fd284a4 | 156 | { |
9853e3b7 GRG |
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; | |
4b7f2165 | 166 | } |
9853e3b7 GRG |
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); | |
4b7f2165 VZ |
178 | } |
179 | #endif // !wxUSE_XPM_IN_MSW | |
180 | ||
2fd284a4 | 181 | return FALSE; |
2fd284a4 JS |
182 | } |
183 | ||
184 | IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) | |
185 | ||
10a0bdb1 VZ |
186 | bool wxXPMDataHandler::Create(wxBitmap *bitmap, |
187 | void *data, | |
33ac7e6f KB |
188 | long WXUNUSED(flags), |
189 | int WXUNUSED(width), | |
190 | int WXUNUSED(height), | |
191 | int WXUNUSED(depth)) | |
2fd284a4 JS |
192 | { |
193 | #if wxUSE_XPM_IN_MSW | |
9853e3b7 GRG |
194 | XImage *ximage = NULL; |
195 | XImage *xmask = NULL; | |
2fd284a4 | 196 | XpmAttributes xpmAttr; |
2fd284a4 | 197 | |
4b7f2165 | 198 | HDC dc = CreateCompatibleDC(NULL); /* memory DC */ |
2fd284a4 JS |
199 | |
200 | if (dc) | |
201 | { | |
4b7f2165 VZ |
202 | xpmAttr.valuemask = XpmReturnInfos | XpmColorTable; |
203 | int errorStatus = XpmCreateImageFromData(&dc, (char **)data, | |
204 | &ximage, | |
9853e3b7 | 205 | &xmask, |
4b7f2165 VZ |
206 | &xpmAttr); |
207 | DeleteDC(dc); | |
2fd284a4 | 208 | |
4b7f2165 VZ |
209 | if ( errorStatus == XpmSuccess ) |
210 | { | |
9853e3b7 | 211 | XpmToBitmap(bitmap, ximage, xmask, xpmAttr); |
0d0512bd | 212 | |
4b7f2165 VZ |
213 | XpmFree(xpmAttr.pixels); |
214 | XpmFreeAttributes(&xpmAttr); | |
215 | XImageFree(ximage); // releases the malloc, but does not destroy bitmap | |
9853e3b7 GRG |
216 | if (xmask) |
217 | XDestroyImage(xmask); | |
4b7f2165 | 218 | } |
0d0512bd VZ |
219 | |
220 | #if WXWIN_COMPATIBILITY_2 | |
221 | bitmap->SetOk(errorStatus == XpmSuccess); | |
222 | #endif // WXWIN_COMPATIBILITY_2 | |
223 | ||
4b7f2165 | 224 | return bitmap->Ok(); |
2fd284a4 | 225 | } |
4b7f2165 | 226 | #endif // wxUSE_XPM_IN_MSW |
2fd284a4 JS |
227 | |
228 | return FALSE; | |
229 | } | |
230 |