]> git.saurik.com Git - wxWidgets.git/blame - src/msw/xpmhand.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / msw / xpmhand.cpp
CommitLineData
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
46static 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 {
f6bcfd97 58 wxLogLastError(wxT("GetObject(bitmap)"));
0d0512bd
VZ
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 69 wxMask *mask = new wxMask();
3ca6a5f0
BP
70 mask->SetMaskBitmap((WXHBITMAP) wxInvertMask(xmask->bitmap,
71 bm.bmWidth, bm.bmHeight));
4b7f2165
VZ
72 bitmap->SetMask(mask);
73 }
0d0512bd
VZ
74}
75
10a0bdb1
VZ
76#endif // wxUSE_XPM_IN_MSW
77
2fd284a4
JS
78IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
79
10a0bdb1
VZ
80bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
81 const wxString& name,
82 long flags,
83 int desiredWidth, int desiredHeight)
2fd284a4
JS
84{
85#if wxUSE_XPM_IN_MSW
9853e3b7
GRG
86 XImage *ximage = NULL;
87 XImage *xmask = NULL;
2fd284a4
JS
88 XpmAttributes xpmAttr;
89 HDC dc;
90
2fd284a4
JS
91 dc = CreateCompatibleDC(NULL);
92 if (dc)
93 {
4b7f2165
VZ
94 xpmAttr.valuemask = XpmReturnPixels | XpmColorTable;
95 int errorStatus = XpmReadFileToImage(&dc,
96 wxMBSTRINGCAST name.fn_str(),
97 &ximage,
9853e3b7 98 &xmask,
4b7f2165
VZ
99 &xpmAttr);
100 DeleteDC(dc);
101 if (errorStatus == XpmSuccess)
102 {
9853e3b7 103 XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
2fd284a4 104
4b7f2165
VZ
105 XpmFree(xpmAttr.pixels);
106 XpmFreeAttributes(&xpmAttr);
107 XImageFree(ximage);
9853e3b7
GRG
108 if (xmask)
109 XDestroyImage(xmask);
4b7f2165 110 }
0d0512bd
VZ
111
112#if WXWIN_COMPATIBILITY_2
4b7f2165 113 bitmap->SetOk(errorStatus == XpmSuccess);
0d0512bd
VZ
114#endif // WXWIN_COMPATIBILITY_2
115
4b7f2165 116 return bitmap->Ok();
2fd284a4 117 }
4b7f2165 118#endif // wxUSE_XPM_IN_MSW
2fd284a4
JS
119
120 return FALSE;
121}
122
10a0bdb1
VZ
123bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
124 const wxString& name,
125 int type,
126 const wxPalette *palette)
2fd284a4
JS
127{
128#if wxUSE_XPM_IN_MSW
4b7f2165 129 XImage ximage;
9853e3b7
GRG
130 XImage xmask;
131 bool hasmask = FALSE;
0d0512bd 132
4b7f2165 133 HDC dc = CreateCompatibleDC(NULL);
2fd284a4
JS
134 if (dc)
135 {
9853e3b7
GRG
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)
2fd284a4 146 {
9853e3b7
GRG
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;
4b7f2165 156 }
9853e3b7
GRG
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);
4b7f2165
VZ
168 }
169#endif // !wxUSE_XPM_IN_MSW
170
2fd284a4 171 return FALSE;
2fd284a4
JS
172}
173
174IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
175
10a0bdb1
VZ
176bool wxXPMDataHandler::Create(wxBitmap *bitmap,
177 void *data,
178 long flags,
179 int width,
180 int height,
181 int depth)
2fd284a4
JS
182{
183#if wxUSE_XPM_IN_MSW
9853e3b7
GRG
184 XImage *ximage = NULL;
185 XImage *xmask = NULL;
2fd284a4 186 XpmAttributes xpmAttr;
2fd284a4 187
4b7f2165 188 HDC dc = CreateCompatibleDC(NULL); /* memory DC */
2fd284a4
JS
189
190 if (dc)
191 {
4b7f2165
VZ
192 xpmAttr.valuemask = XpmReturnInfos | XpmColorTable;
193 int errorStatus = XpmCreateImageFromData(&dc, (char **)data,
194 &ximage,
9853e3b7 195 &xmask,
4b7f2165
VZ
196 &xpmAttr);
197 DeleteDC(dc);
2fd284a4 198
4b7f2165
VZ
199 if ( errorStatus == XpmSuccess )
200 {
9853e3b7 201 XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
0d0512bd 202
4b7f2165
VZ
203 XpmFree(xpmAttr.pixels);
204 XpmFreeAttributes(&xpmAttr);
205 XImageFree(ximage); // releases the malloc, but does not destroy bitmap
9853e3b7
GRG
206 if (xmask)
207 XDestroyImage(xmask);
4b7f2165 208 }
0d0512bd
VZ
209
210#if WXWIN_COMPATIBILITY_2
211 bitmap->SetOk(errorStatus == XpmSuccess);
212#endif // WXWIN_COMPATIBILITY_2
213
4b7f2165 214 return bitmap->Ok();
2fd284a4 215 }
4b7f2165 216#endif // wxUSE_XPM_IN_MSW
2fd284a4
JS
217
218 return FALSE;
219}
220