]> git.saurik.com Git - wxWidgets.git/blob - src/msw/xpmhand.cpp
help search is much faster now (7 times! that's what I call optimization ;-)
[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 #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 BITMAP bm;
54 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) )
55 {
56 wxLogLastError("GetObject(bitmap)");
57 }
58
59 refData->m_width = bm.bmWidth;
60 refData->m_height = bm.bmHeight;
61 refData->m_depth = bm.bmPlanes * bm.bmBitsPixel;
62 refData->m_numColors = xpmAttr.npixels;
63 }
64
65 #endif // wxUSE_XPM_IN_MSW
66
67 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
68
69 bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
70 const wxString& name,
71 long flags,
72 int desiredWidth, int desiredHeight)
73 {
74 #if wxUSE_XPM_IN_MSW
75 XImage *ximage;
76 XpmAttributes xpmAttr;
77 HDC dc;
78
79 dc = CreateCompatibleDC(NULL);
80 if (dc)
81 {
82 xpmAttr.valuemask = XpmReturnPixels;
83 int errorStatus = XpmReadFileToImage(&dc, wxMBSTRINGCAST name.fn_str(), &ximage, (XImage **) NULL, &xpmAttr);
84 DeleteDC(dc);
85 if (errorStatus == XpmSuccess)
86 {
87 XpmToBitmap(bitmap, ximage, xpmAttr);
88
89 XpmFreeAttributes(&xpmAttr);
90 XImageFree(ximage);
91 }
92
93 #if WXWIN_COMPATIBILITY_2
94 bitmap->SetOk(errorStatus == XpmSuccess);
95 #endif // WXWIN_COMPATIBILITY_2
96
97 return bitmap->Ok();
98 }
99 #endif
100
101 return FALSE;
102 }
103
104 bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
105 const wxString& name,
106 int type,
107 const wxPalette *palette)
108 {
109 #if wxUSE_XPM_IN_MSW
110 HDC dc = NULL;
111
112 XImage ximage;
113
114 dc = CreateCompatibleDC(NULL);
115 if (dc)
116 {
117 if ( SelectObject(dc, GetHbitmapOf(*bitmap)) )
118 {
119 /* for following SetPixel */
120 /* fill the XImage struct 'by hand' */
121 wxBitmapRefData *refData = bitmap->GetBitmapData();
122 ximage.width = refData->m_width;
123 ximage.height = refData->m_height;
124 ximage.depth = refData->m_depth;
125 ximage.bitmap = (HBITMAP)refData->m_hBitmap;
126 int errorStatus = XpmWriteFileFromImage(&dc, wxMBSTRINGCAST name.fn_str(),
127 &ximage, (XImage *) NULL,
128 (XpmAttributes *) NULL);
129
130 if (dc)
131 DeleteDC(dc);
132
133 if (errorStatus == XpmSuccess)
134 return TRUE; /* no error */
135 else
136 return FALSE;
137 } else return FALSE;
138 } else return FALSE;
139 #else
140 return FALSE;
141 #endif
142 }
143
144 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
145
146 bool wxXPMDataHandler::Create(wxBitmap *bitmap,
147 void *data,
148 long flags,
149 int width,
150 int height,
151 int depth)
152 {
153 #if wxUSE_XPM_IN_MSW
154 XImage *ximage;
155 int ErrorStatus;
156 XpmAttributes xpmAttr;
157 HDC dc;
158
159 dc = CreateCompatibleDC(NULL); /* memory DC */
160
161 if (dc)
162 {
163 xpmAttr.valuemask = XpmReturnInfos; /* get infos back */
164 ErrorStatus = XpmCreateImageFromData(&dc, (char **)data,
165 &ximage, (XImage **) NULL, &xpmAttr);
166
167 if (ErrorStatus == XpmSuccess)
168 {
169 XpmToBitmap(bitmap, ximage, xpmAttr);
170
171 XpmFreeAttributes(&xpmAttr);
172
173 XImageFree(ximage); // releases the malloc, but does not detroy
174 // the bitmap
175 }
176 else
177 {
178 // XpmDebugError(ErrorStatus, NULL);
179 }
180
181 DeleteDC(dc);
182
183 #if WXWIN_COMPATIBILITY_2
184 bitmap->SetOk(errorStatus == XpmSuccess);
185 #endif // WXWIN_COMPATIBILITY_2
186
187 return bitmap->Ok();
188 }
189 #endif
190
191 return FALSE;
192 }
193