]> git.saurik.com Git - wxWidgets.git/blob - src/msw/xpmhand.cpp
1. wxIcon/wxCursor change, wxGDIImage class added
[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 <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/list.h"
27 #include "wx/utils.h"
28 #include "wx/app.h"
29 #include "wx/palette.h"
30 #include "wx/dcmemory.h"
31 #include "wx/bitmap.h"
32 #include "wx/icon.h"
33 #endif
34
35 #include "wx/msw/private.h"
36 #include "wx/log.h"
37
38 #include "assert.h"
39
40 #if wxUSE_XPM_IN_MSW
41 #define FOR_MSW 1
42 #include "../xpm/xpm34.h"
43 #endif
44
45 #include "wx/xpmhand.h"
46 #include "wx/msw/dib.h"
47
48 static void XpmToBitmap(wxBitmap *bitmap,
49 const XImage *ximage,
50 const XpmAttributes& xpmAttr)
51 {
52 wxBitmapRefData *refData = bitmap->GetBitmapData();
53 refData->m_hBitmap = (WXHBITMAP)ximage->bitmap;
54
55 BITMAP bm;
56 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) )
57 {
58 wxLogLastError("GetObject(bitmap)");
59 }
60
61 refData->m_width = bm.bmWidth;
62 refData->m_height = bm.bmHeight;
63 refData->m_depth = bm.bmPlanes * bm.bmBitsPixel;
64 refData->m_numColors = xpmAttr.npixels;
65 }
66
67 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
68
69 bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
70 int desiredWidth, int desiredHeight)
71 {
72 #if wxUSE_XPM_IN_MSW
73 XImage *ximage;
74 XpmAttributes xpmAttr;
75 HDC dc;
76
77 dc = CreateCompatibleDC(NULL);
78 if (dc)
79 {
80 xpmAttr.valuemask = XpmReturnPixels;
81 int errorStatus = XpmReadFileToImage(&dc, wxMBSTRINGCAST name.fn_str(), &ximage, (XImage **) NULL, &xpmAttr);
82 DeleteDC(dc);
83 if (errorStatus == XpmSuccess)
84 {
85 XpmToBitmap(bitmap, ximage, xpmAttr);
86
87 XpmFreeAttributes(&xpmAttr);
88 XImageFree(ximage);
89 }
90
91 #if WXWIN_COMPATIBILITY_2
92 bitmap->SetOk(errorStatus == XpmSuccess);
93 #endif // WXWIN_COMPATIBILITY_2
94
95 return bitmap->Ok();
96 }
97 #endif
98
99 return FALSE;
100 }
101
102 bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
103 {
104 #if wxUSE_XPM_IN_MSW
105 HDC dc = NULL;
106
107 XImage ximage;
108
109 dc = CreateCompatibleDC(NULL);
110 if (dc)
111 {
112 if ( SelectObject(dc, GetHbitmapOf(*bitmap)) )
113 {
114 /* for following SetPixel */
115 /* fill the XImage struct 'by hand' */
116 wxBitmapRefData *refData = bitmap->GetBitmapData();
117 ximage.width = refData->m_width;
118 ximage.height = refData->m_height;
119 ximage.depth = refData->m_depth;
120 ximage.bitmap = (HBITMAP)refData->m_hBitmap;
121 int errorStatus = XpmWriteFileFromImage(&dc, wxMBSTRINGCAST name.fn_str(),
122 &ximage, (XImage *) NULL,
123 (XpmAttributes *) NULL);
124
125 if (dc)
126 DeleteDC(dc);
127
128 if (errorStatus == XpmSuccess)
129 return TRUE; /* no error */
130 else
131 return FALSE;
132 } else return FALSE;
133 } else return FALSE;
134 #else
135 return FALSE;
136 #endif
137 }
138
139 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
140
141 bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth)
142 {
143 #if wxUSE_XPM_IN_MSW
144 XImage *ximage;
145 int ErrorStatus;
146 XpmAttributes xpmAttr;
147 HDC dc;
148
149 dc = CreateCompatibleDC(NULL); /* memory DC */
150
151 if (dc)
152 {
153 xpmAttr.valuemask = XpmReturnInfos; /* get infos back */
154 ErrorStatus = XpmCreateImageFromData(&dc, (char **)data,
155 &ximage, (XImage **) NULL, &xpmAttr);
156
157 if (ErrorStatus == XpmSuccess)
158 {
159 XpmToBitmap(bitmap, ximage, xpmAttr);
160
161 XpmFreeAttributes(&xpmAttr);
162
163 XImageFree(ximage); // releases the malloc, but does not detroy
164 // the bitmap
165 }
166 else
167 {
168 // XpmDebugError(ErrorStatus, NULL);
169 }
170
171 DeleteDC(dc);
172
173 #if WXWIN_COMPATIBILITY_2
174 bitmap->SetOk(errorStatus == XpmSuccess);
175 #endif // WXWIN_COMPATIBILITY_2
176
177 return bitmap->Ok();
178 }
179 #endif
180
181 return FALSE;
182 }
183