]> git.saurik.com Git - wxWidgets.git/blame - src/msw/xpmhand.cpp
Changed order of #ifdefs to get native version on OS/2, even if
[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
VZ
37 #define FOR_MSW 1
38 #include "../xpm/xpm34.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,
48 const XpmAttributes& xpmAttr)
49{
50 wxBitmapRefData *refData = bitmap->GetBitmapData();
51 refData->m_hBitmap = (WXHBITMAP)ximage->bitmap;
52
4b7f2165 53 // first set the bitmap width, height, depth...
0d0512bd
VZ
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;
4b7f2165
VZ
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 }
0d0512bd
VZ
84}
85
10a0bdb1
VZ
86#endif // wxUSE_XPM_IN_MSW
87
2fd284a4
JS
88IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
89
10a0bdb1
VZ
90bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
91 const wxString& name,
92 long flags,
93 int desiredWidth, int desiredHeight)
2fd284a4
JS
94{
95#if wxUSE_XPM_IN_MSW
96 XImage *ximage;
97 XpmAttributes xpmAttr;
98 HDC dc;
99
2fd284a4
JS
100 dc = CreateCompatibleDC(NULL);
101 if (dc)
102 {
4b7f2165
VZ
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);
2fd284a4 113
4b7f2165
VZ
114 XpmFree(xpmAttr.pixels);
115 XpmFreeAttributes(&xpmAttr);
116 XImageFree(ximage);
117 }
0d0512bd
VZ
118
119#if WXWIN_COMPATIBILITY_2
4b7f2165 120 bitmap->SetOk(errorStatus == XpmSuccess);
0d0512bd
VZ
121#endif // WXWIN_COMPATIBILITY_2
122
4b7f2165 123 return bitmap->Ok();
2fd284a4 124 }
4b7f2165 125#endif // wxUSE_XPM_IN_MSW
2fd284a4
JS
126
127 return FALSE;
128}
129
10a0bdb1
VZ
130bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
131 const wxString& name,
132 int type,
133 const wxPalette *palette)
2fd284a4
JS
134{
135#if wxUSE_XPM_IN_MSW
4b7f2165 136 XImage ximage;
0d0512bd 137
4b7f2165 138 HDC dc = CreateCompatibleDC(NULL);
2fd284a4
JS
139 if (dc)
140 {
0d0512bd 141 if ( SelectObject(dc, GetHbitmapOf(*bitmap)) )
2fd284a4
JS
142 {
143 /* for following SetPixel */
144 /* fill the XImage struct 'by hand' */
0d0512bd
VZ
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;
73974df1 150 int errorStatus = XpmWriteFileFromImage(&dc, wxMBSTRINGCAST name.fn_str(),
0d0512bd
VZ
151 &ximage, (XImage *) NULL,
152 (XpmAttributes *) NULL);
153
2fd284a4
JS
154 if (dc)
155 DeleteDC(dc);
0d0512bd 156
2fd284a4
JS
157 if (errorStatus == XpmSuccess)
158 return TRUE; /* no error */
4b7f2165
VZ
159 }
160 }
161#endif // !wxUSE_XPM_IN_MSW
162
2fd284a4 163 return FALSE;
2fd284a4
JS
164}
165
166IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
167
10a0bdb1
VZ
168bool wxXPMDataHandler::Create(wxBitmap *bitmap,
169 void *data,
170 long flags,
171 int width,
172 int height,
173 int depth)
2fd284a4
JS
174{
175#if wxUSE_XPM_IN_MSW
176 XImage *ximage;
2fd284a4 177 XpmAttributes xpmAttr;
2fd284a4 178
4b7f2165 179 HDC dc = CreateCompatibleDC(NULL); /* memory DC */
2fd284a4
JS
180
181 if (dc)
182 {
4b7f2165
VZ
183 xpmAttr.valuemask = XpmReturnInfos | XpmColorTable;
184 int errorStatus = XpmCreateImageFromData(&dc, (char **)data,
185 &ximage,
186 (XImage **) NULL,
187 &xpmAttr);
188 DeleteDC(dc);
2fd284a4 189
4b7f2165
VZ
190 if ( errorStatus == XpmSuccess )
191 {
192 XpmToBitmap(bitmap, ximage, xpmAttr);
0d0512bd 193
4b7f2165
VZ
194 XpmFree(xpmAttr.pixels);
195 XpmFreeAttributes(&xpmAttr);
196 XImageFree(ximage); // releases the malloc, but does not destroy bitmap
197 }
0d0512bd
VZ
198
199#if WXWIN_COMPATIBILITY_2
200 bitmap->SetOk(errorStatus == XpmSuccess);
201#endif // WXWIN_COMPATIBILITY_2
202
4b7f2165 203 return bitmap->Ok();
2fd284a4 204 }
4b7f2165 205#endif // wxUSE_XPM_IN_MSW
2fd284a4
JS
206
207 return FALSE;
208}
209