]>
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 |
9853e3b7 | 38 | #include "../xpm/xpm.h" |
2fd284a4 JS |
39 | #endif |
40 | ||
41 | #include "wx/xpmhand.h" | |
42 | #include "wx/msw/dib.h" | |
43 | ||
10a0bdb1 VZ |
44 | #if wxUSE_XPM_IN_MSW |
45 | ||
0d0512bd VZ |
46 | static void XpmToBitmap(wxBitmap *bitmap, |
47 | const XImage *ximage, | |
9853e3b7 | 48 | const XImage *xmask, |
0d0512bd VZ |
49 | const XpmAttributes& xpmAttr) |
50 | { | |
51 | wxBitmapRefData *refData = bitmap->GetBitmapData(); | |
52 | refData->m_hBitmap = (WXHBITMAP)ximage->bitmap; | |
53 | ||
4b7f2165 | 54 | // first set the bitmap width, height, depth... |
0d0512bd VZ |
55 | BITMAP bm; |
56 | if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) ) | |
57 | { | |
58 | wxLogLastError("GetObject(bitmap)"); | |
59 | } | |
60 | ||
9853e3b7 GRG |
61 | refData->m_width = bm.bmWidth; |
62 | refData->m_height = bm.bmHeight; | |
63 | refData->m_depth = bm.bmPlanes * bm.bmBitsPixel; | |
0d0512bd | 64 | refData->m_numColors = xpmAttr.npixels; |
4b7f2165 | 65 | |
9853e3b7 GRG |
66 | // GRG Jan/2000, mask support |
67 | if (xmask) | |
4b7f2165 | 68 | { |
9853e3b7 GRG |
69 | wxMask *mask = new wxMask(); |
70 | mask->SetMaskBitmap((WXHBITMAP) wxInvertMask(xmask->bitmap)); | |
4b7f2165 VZ |
71 | bitmap->SetMask(mask); |
72 | } | |
0d0512bd VZ |
73 | } |
74 | ||
10a0bdb1 VZ |
75 | #endif // wxUSE_XPM_IN_MSW |
76 | ||
2fd284a4 JS |
77 | IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) |
78 | ||
10a0bdb1 VZ |
79 | bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, |
80 | const wxString& name, | |
81 | long flags, | |
82 | int desiredWidth, int desiredHeight) | |
2fd284a4 JS |
83 | { |
84 | #if wxUSE_XPM_IN_MSW | |
9853e3b7 GRG |
85 | XImage *ximage = NULL; |
86 | XImage *xmask = NULL; | |
2fd284a4 JS |
87 | XpmAttributes xpmAttr; |
88 | HDC dc; | |
89 | ||
2fd284a4 JS |
90 | dc = CreateCompatibleDC(NULL); |
91 | if (dc) | |
92 | { | |
4b7f2165 VZ |
93 | xpmAttr.valuemask = XpmReturnPixels | XpmColorTable; |
94 | int errorStatus = XpmReadFileToImage(&dc, | |
95 | wxMBSTRINGCAST name.fn_str(), | |
96 | &ximage, | |
9853e3b7 | 97 | &xmask, |
4b7f2165 VZ |
98 | &xpmAttr); |
99 | DeleteDC(dc); | |
100 | if (errorStatus == XpmSuccess) | |
101 | { | |
9853e3b7 | 102 | XpmToBitmap(bitmap, ximage, xmask, xpmAttr); |
2fd284a4 | 103 | |
4b7f2165 VZ |
104 | XpmFree(xpmAttr.pixels); |
105 | XpmFreeAttributes(&xpmAttr); | |
106 | XImageFree(ximage); | |
9853e3b7 GRG |
107 | if (xmask) |
108 | XDestroyImage(xmask); | |
4b7f2165 | 109 | } |
0d0512bd VZ |
110 | |
111 | #if WXWIN_COMPATIBILITY_2 | |
4b7f2165 | 112 | bitmap->SetOk(errorStatus == XpmSuccess); |
0d0512bd VZ |
113 | #endif // WXWIN_COMPATIBILITY_2 |
114 | ||
4b7f2165 | 115 | return bitmap->Ok(); |
2fd284a4 | 116 | } |
4b7f2165 | 117 | #endif // wxUSE_XPM_IN_MSW |
2fd284a4 JS |
118 | |
119 | return FALSE; | |
120 | } | |
121 | ||
10a0bdb1 VZ |
122 | bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, |
123 | const wxString& name, | |
124 | int type, | |
125 | const wxPalette *palette) | |
2fd284a4 JS |
126 | { |
127 | #if wxUSE_XPM_IN_MSW | |
4b7f2165 | 128 | XImage ximage; |
9853e3b7 GRG |
129 | XImage xmask; |
130 | bool hasmask = FALSE; | |
0d0512bd | 131 | |
4b7f2165 | 132 | HDC dc = CreateCompatibleDC(NULL); |
2fd284a4 JS |
133 | if (dc) |
134 | { | |
9853e3b7 GRG |
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; | |
141 | ||
142 | // GRG Jan/2000, mask support | |
143 | hasmask = (refData->m_bitmapMask != NULL); | |
144 | if (hasmask) | |
2fd284a4 | 145 | { |
9853e3b7 GRG |
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... | |
150 | */ | |
151 | xmask.bitmap = (HBITMAP) refData->m_bitmapMask->GetMaskBitmap(); | |
152 | xmask.width = refData->m_width; | |
153 | xmask.height = refData->m_height; | |
154 | xmask.depth = 1; | |
4b7f2165 | 155 | } |
9853e3b7 GRG |
156 | |
157 | int errorStatus = XpmWriteFileFromImage( | |
158 | &dc, | |
159 | wxMBSTRINGCAST name.fn_str(), | |
160 | &ximage, | |
161 | (hasmask? &xmask : (XImage *)NULL), | |
162 | (XpmAttributes *) NULL); | |
163 | ||
164 | DeleteDC(dc); | |
165 | ||
166 | return (errorStatus == XpmSuccess); | |
4b7f2165 VZ |
167 | } |
168 | #endif // !wxUSE_XPM_IN_MSW | |
169 | ||
2fd284a4 | 170 | return FALSE; |
2fd284a4 JS |
171 | } |
172 | ||
173 | IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) | |
174 | ||
10a0bdb1 VZ |
175 | bool wxXPMDataHandler::Create(wxBitmap *bitmap, |
176 | void *data, | |
177 | long flags, | |
178 | int width, | |
179 | int height, | |
180 | int depth) | |
2fd284a4 JS |
181 | { |
182 | #if wxUSE_XPM_IN_MSW | |
9853e3b7 GRG |
183 | XImage *ximage = NULL; |
184 | XImage *xmask = NULL; | |
2fd284a4 | 185 | XpmAttributes xpmAttr; |
2fd284a4 | 186 | |
4b7f2165 | 187 | HDC dc = CreateCompatibleDC(NULL); /* memory DC */ |
2fd284a4 JS |
188 | |
189 | if (dc) | |
190 | { | |
4b7f2165 VZ |
191 | xpmAttr.valuemask = XpmReturnInfos | XpmColorTable; |
192 | int errorStatus = XpmCreateImageFromData(&dc, (char **)data, | |
193 | &ximage, | |
9853e3b7 | 194 | &xmask, |
4b7f2165 VZ |
195 | &xpmAttr); |
196 | DeleteDC(dc); | |
2fd284a4 | 197 | |
4b7f2165 VZ |
198 | if ( errorStatus == XpmSuccess ) |
199 | { | |
9853e3b7 | 200 | XpmToBitmap(bitmap, ximage, xmask, xpmAttr); |
0d0512bd | 201 | |
4b7f2165 VZ |
202 | XpmFree(xpmAttr.pixels); |
203 | XpmFreeAttributes(&xpmAttr); | |
204 | XImageFree(ximage); // releases the malloc, but does not destroy bitmap | |
9853e3b7 GRG |
205 | if (xmask) |
206 | XDestroyImage(xmask); | |
4b7f2165 | 207 | } |
0d0512bd VZ |
208 | |
209 | #if WXWIN_COMPATIBILITY_2 | |
210 | bitmap->SetOk(errorStatus == XpmSuccess); | |
211 | #endif // WXWIN_COMPATIBILITY_2 | |
212 | ||
4b7f2165 | 213 | return bitmap->Ok(); |
2fd284a4 | 214 | } |
4b7f2165 | 215 | #endif // wxUSE_XPM_IN_MSW |
2fd284a4 JS |
216 | |
217 | return FALSE; | |
218 | } | |
219 |