]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/xpmhand.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / msw / xpmhand.cpp
... / ...
CommitLineData
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/xpm.h"
39#endif
40
41#include "wx/xpmhand.h"
42#include "wx/msw/dib.h"
43
44#if wxUSE_XPM_IN_MSW
45
46static void XpmToBitmap(wxBitmap *bitmap,
47 const XImage *ximage,
48 const XImage *xmask,
49 const XpmAttributes& xpmAttr)
50{
51 wxBitmapRefData *refData = bitmap->GetBitmapData();
52 refData->m_hBitmap = (WXHBITMAP)ximage->bitmap;
53
54 // first set the bitmap width, height, depth...
55 BITMAP bm;
56 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) )
57 {
58 wxLogLastError(wxT("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 // GRG Jan/2000, mask support
67 if (xmask)
68 {
69 wxMask *mask = new wxMask();
70 mask->SetMaskBitmap((WXHBITMAP) wxInvertMask(xmask->bitmap,
71 bm.bmWidth, bm.bmHeight));
72 bitmap->SetMask(mask);
73 }
74}
75
76#endif // wxUSE_XPM_IN_MSW
77
78IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
79
80bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
81 const wxString& name,
82 long flags,
83 int desiredWidth, int desiredHeight)
84{
85#if wxUSE_XPM_IN_MSW
86 XImage *ximage = NULL;
87 XImage *xmask = NULL;
88 XpmAttributes xpmAttr;
89 HDC dc;
90
91 dc = CreateCompatibleDC(NULL);
92 if (dc)
93 {
94 xpmAttr.valuemask = XpmReturnPixels | XpmColorTable;
95 int errorStatus = XpmReadFileToImage(&dc,
96 wxMBSTRINGCAST name.fn_str(),
97 &ximage,
98 &xmask,
99 &xpmAttr);
100 DeleteDC(dc);
101 if (errorStatus == XpmSuccess)
102 {
103 XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
104
105 XpmFree(xpmAttr.pixels);
106 XpmFreeAttributes(&xpmAttr);
107 XImageFree(ximage);
108 if (xmask)
109 XDestroyImage(xmask);
110 }
111
112#if WXWIN_COMPATIBILITY_2
113 bitmap->SetOk(errorStatus == XpmSuccess);
114#endif // WXWIN_COMPATIBILITY_2
115
116 return bitmap->Ok();
117 }
118#endif // wxUSE_XPM_IN_MSW
119
120 return FALSE;
121}
122
123bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
124 const wxString& name,
125 int type,
126 const wxPalette *palette)
127{
128#if wxUSE_XPM_IN_MSW
129 XImage ximage;
130 XImage xmask;
131 bool hasmask = FALSE;
132
133 HDC dc = CreateCompatibleDC(NULL);
134 if (dc)
135 {
136 /* fill the XImage struct 'by hand' */
137 wxBitmapRefData *refData = bitmap->GetBitmapData();
138 ximage.width = refData->m_width;
139 ximage.height = refData->m_height;
140 ximage.depth = refData->m_depth;
141 ximage.bitmap = (HBITMAP)refData->m_hBitmap;
142
143 // GRG Jan/2000, mask support
144 hasmask = (refData->m_bitmapMask != NULL);
145 if (hasmask)
146 {
147 /* Strangely enough, the MSW version of xpmlib is not
148 * coherent with itself regarding masks; we have to invert
149 * the mask we get when loading, but we still must pass it
150 * 'as is' when saving...
151 */
152 xmask.bitmap = (HBITMAP) refData->m_bitmapMask->GetMaskBitmap();
153 xmask.width = refData->m_width;
154 xmask.height = refData->m_height;
155 xmask.depth = 1;
156 }
157
158 int errorStatus = XpmWriteFileFromImage(
159 &dc,
160 wxMBSTRINGCAST name.fn_str(),
161 &ximage,
162 (hasmask? &xmask : (XImage *)NULL),
163 (XpmAttributes *) NULL);
164
165 DeleteDC(dc);
166
167 return (errorStatus == XpmSuccess);
168 }
169#endif // !wxUSE_XPM_IN_MSW
170
171 return FALSE;
172}
173
174IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
175
176bool wxXPMDataHandler::Create(wxBitmap *bitmap,
177 void *data,
178 long flags,
179 int width,
180 int height,
181 int depth)
182{
183#if wxUSE_XPM_IN_MSW
184 XImage *ximage = NULL;
185 XImage *xmask = NULL;
186 XpmAttributes xpmAttr;
187
188 HDC dc = CreateCompatibleDC(NULL); /* memory DC */
189
190 if (dc)
191 {
192 xpmAttr.valuemask = XpmReturnInfos | XpmColorTable;
193 int errorStatus = XpmCreateImageFromData(&dc, (char **)data,
194 &ximage,
195 &xmask,
196 &xpmAttr);
197 DeleteDC(dc);
198
199 if ( errorStatus == XpmSuccess )
200 {
201 XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
202
203 XpmFree(xpmAttr.pixels);
204 XpmFreeAttributes(&xpmAttr);
205 XImageFree(ximage); // releases the malloc, but does not destroy bitmap
206 if (xmask)
207 XDestroyImage(xmask);
208 }
209
210#if WXWIN_COMPATIBILITY_2
211 bitmap->SetOk(errorStatus == XpmSuccess);
212#endif // WXWIN_COMPATIBILITY_2
213
214 return bitmap->Ok();
215 }
216#endif // wxUSE_XPM_IN_MSW
217
218 return FALSE;
219}
220