]>
Commit | Line | Data |
---|---|---|
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 | #include "../xpm/xpm34.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/xpmhand.h" | |
42 | #include "wx/msw/dib.h" | |
43 | ||
44 | #if wxUSE_XPM_IN_MSW | |
45 | ||
46 | static void XpmToBitmap(wxBitmap *bitmap, | |
47 | const XImage *ximage, | |
48 | const XpmAttributes& xpmAttr) | |
49 | { | |
50 | wxBitmapRefData *refData = bitmap->GetBitmapData(); | |
51 | refData->m_hBitmap = (WXHBITMAP)ximage->bitmap; | |
52 | ||
53 | // first set the bitmap width, height, depth... | |
54 | BITMAP bm; | |
55 | if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) ) | |
56 | { | |
57 | wxLogLastError("GetObject(bitmap)"); | |
58 | } | |
59 | ||
60 | refData->m_width = bm.bmWidth; | |
61 | refData->m_height = bm.bmHeight; | |
62 | refData->m_depth = bm.bmPlanes * bm.bmBitsPixel; | |
63 | refData->m_numColors = xpmAttr.npixels; | |
64 | ||
65 | // next get the mask, if any | |
66 | if ( xpmAttr.mask_pixel != XpmUndefPixel ) | |
67 | { | |
68 | int red, green, blue; | |
69 | const char *clrString = xpmAttr.colorTable[xpmAttr.mask_pixel].c_color; | |
70 | if ( strcmp(clrString, "None") == 0 ) | |
71 | { | |
72 | // TODO what to do here?? | |
73 | red = green = 0; | |
74 | blue = 1; | |
75 | } | |
76 | else | |
77 | { | |
78 | sscanf(clrString, "#%02x%02x%02x", &red, &green, &blue); | |
79 | } | |
80 | ||
81 | wxMask *mask = new wxMask(*bitmap, wxColour(red, green, blue)); | |
82 | bitmap->SetMask(mask); | |
83 | } | |
84 | } | |
85 | ||
86 | #endif // wxUSE_XPM_IN_MSW | |
87 | ||
88 | IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) | |
89 | ||
90 | bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, | |
91 | const wxString& name, | |
92 | long flags, | |
93 | int desiredWidth, int desiredHeight) | |
94 | { | |
95 | #if wxUSE_XPM_IN_MSW | |
96 | XImage *ximage; | |
97 | XpmAttributes xpmAttr; | |
98 | HDC dc; | |
99 | ||
100 | dc = CreateCompatibleDC(NULL); | |
101 | if (dc) | |
102 | { | |
103 | xpmAttr.valuemask = XpmReturnPixels | XpmColorTable; | |
104 | int errorStatus = XpmReadFileToImage(&dc, | |
105 | wxMBSTRINGCAST name.fn_str(), | |
106 | &ximage, | |
107 | (XImage **)NULL, | |
108 | &xpmAttr); | |
109 | DeleteDC(dc); | |
110 | if (errorStatus == XpmSuccess) | |
111 | { | |
112 | XpmToBitmap(bitmap, ximage, xpmAttr); | |
113 | ||
114 | XpmFree(xpmAttr.pixels); | |
115 | XpmFreeAttributes(&xpmAttr); | |
116 | XImageFree(ximage); | |
117 | } | |
118 | ||
119 | #if WXWIN_COMPATIBILITY_2 | |
120 | bitmap->SetOk(errorStatus == XpmSuccess); | |
121 | #endif // WXWIN_COMPATIBILITY_2 | |
122 | ||
123 | return bitmap->Ok(); | |
124 | } | |
125 | #endif // wxUSE_XPM_IN_MSW | |
126 | ||
127 | return FALSE; | |
128 | } | |
129 | ||
130 | bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, | |
131 | const wxString& name, | |
132 | int type, | |
133 | const wxPalette *palette) | |
134 | { | |
135 | #if wxUSE_XPM_IN_MSW | |
136 | XImage ximage; | |
137 | ||
138 | HDC dc = CreateCompatibleDC(NULL); | |
139 | if (dc) | |
140 | { | |
141 | if ( SelectObject(dc, GetHbitmapOf(*bitmap)) ) | |
142 | { | |
143 | /* for following SetPixel */ | |
144 | /* fill the XImage struct 'by hand' */ | |
145 | wxBitmapRefData *refData = bitmap->GetBitmapData(); | |
146 | ximage.width = refData->m_width; | |
147 | ximage.height = refData->m_height; | |
148 | ximage.depth = refData->m_depth; | |
149 | ximage.bitmap = (HBITMAP)refData->m_hBitmap; | |
150 | int errorStatus = XpmWriteFileFromImage(&dc, wxMBSTRINGCAST name.fn_str(), | |
151 | &ximage, (XImage *) NULL, | |
152 | (XpmAttributes *) NULL); | |
153 | ||
154 | if (dc) | |
155 | DeleteDC(dc); | |
156 | ||
157 | if (errorStatus == XpmSuccess) | |
158 | return TRUE; /* no error */ | |
159 | } | |
160 | } | |
161 | #endif // !wxUSE_XPM_IN_MSW | |
162 | ||
163 | return FALSE; | |
164 | } | |
165 | ||
166 | IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) | |
167 | ||
168 | bool wxXPMDataHandler::Create(wxBitmap *bitmap, | |
169 | void *data, | |
170 | long flags, | |
171 | int width, | |
172 | int height, | |
173 | int depth) | |
174 | { | |
175 | #if wxUSE_XPM_IN_MSW | |
176 | XImage *ximage; | |
177 | XpmAttributes xpmAttr; | |
178 | ||
179 | HDC dc = CreateCompatibleDC(NULL); /* memory DC */ | |
180 | ||
181 | if (dc) | |
182 | { | |
183 | xpmAttr.valuemask = XpmReturnInfos | XpmColorTable; | |
184 | int errorStatus = XpmCreateImageFromData(&dc, (char **)data, | |
185 | &ximage, | |
186 | (XImage **) NULL, | |
187 | &xpmAttr); | |
188 | DeleteDC(dc); | |
189 | ||
190 | if ( errorStatus == XpmSuccess ) | |
191 | { | |
192 | XpmToBitmap(bitmap, ximage, xpmAttr); | |
193 | ||
194 | XpmFree(xpmAttr.pixels); | |
195 | XpmFreeAttributes(&xpmAttr); | |
196 | XImageFree(ximage); // releases the malloc, but does not destroy bitmap | |
197 | } | |
198 | ||
199 | #if WXWIN_COMPATIBILITY_2 | |
200 | bitmap->SetOk(errorStatus == XpmSuccess); | |
201 | #endif // WXWIN_COMPATIBILITY_2 | |
202 | ||
203 | return bitmap->Ok(); | |
204 | } | |
205 | #endif // wxUSE_XPM_IN_MSW | |
206 | ||
207 | return FALSE; | |
208 | } | |
209 |