]> git.saurik.com Git - wxWidgets.git/blame - src/msw/xpmhand.cpp
fix for red to blue mapping in toolbar buttons
[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
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
10a0bdb1
VZ
65#endif // wxUSE_XPM_IN_MSW
66
2fd284a4
JS
67IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
68
10a0bdb1
VZ
69bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
70 const wxString& name,
71 long flags,
72 int desiredWidth, int desiredHeight)
2fd284a4
JS
73{
74#if wxUSE_XPM_IN_MSW
75 XImage *ximage;
76 XpmAttributes xpmAttr;
77 HDC dc;
78
2fd284a4
JS
79 dc = CreateCompatibleDC(NULL);
80 if (dc)
81 {
82 xpmAttr.valuemask = XpmReturnPixels;
73974df1 83 int errorStatus = XpmReadFileToImage(&dc, wxMBSTRINGCAST name.fn_str(), &ximage, (XImage **) NULL, &xpmAttr);
2fd284a4
JS
84 DeleteDC(dc);
85 if (errorStatus == XpmSuccess)
86 {
0d0512bd 87 XpmToBitmap(bitmap, ximage, xpmAttr);
2fd284a4 88
2fd284a4
JS
89 XpmFreeAttributes(&xpmAttr);
90 XImageFree(ximage);
2fd284a4 91 }
0d0512bd
VZ
92
93#if WXWIN_COMPATIBILITY_2
94 bitmap->SetOk(errorStatus == XpmSuccess);
95#endif // WXWIN_COMPATIBILITY_2
96
97 return bitmap->Ok();
2fd284a4
JS
98 }
99#endif
100
101 return FALSE;
102}
103
10a0bdb1
VZ
104bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
105 const wxString& name,
106 int type,
107 const wxPalette *palette)
2fd284a4
JS
108{
109#if wxUSE_XPM_IN_MSW
110 HDC dc = NULL;
0d0512bd 111
2fd284a4 112 XImage ximage;
0d0512bd 113
2fd284a4
JS
114 dc = CreateCompatibleDC(NULL);
115 if (dc)
116 {
0d0512bd 117 if ( SelectObject(dc, GetHbitmapOf(*bitmap)) )
2fd284a4
JS
118 {
119 /* for following SetPixel */
120 /* fill the XImage struct 'by hand' */
0d0512bd
VZ
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;
73974df1 126 int errorStatus = XpmWriteFileFromImage(&dc, wxMBSTRINGCAST name.fn_str(),
0d0512bd
VZ
127 &ximage, (XImage *) NULL,
128 (XpmAttributes *) NULL);
129
2fd284a4
JS
130 if (dc)
131 DeleteDC(dc);
0d0512bd 132
2fd284a4
JS
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
144IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
145
10a0bdb1
VZ
146bool wxXPMDataHandler::Create(wxBitmap *bitmap,
147 void *data,
148 long flags,
149 int width,
150 int height,
151 int depth)
2fd284a4
JS
152{
153#if wxUSE_XPM_IN_MSW
154 XImage *ximage;
155 int ErrorStatus;
156 XpmAttributes xpmAttr;
157 HDC dc;
158
2fd284a4
JS
159 dc = CreateCompatibleDC(NULL); /* memory DC */
160
161 if (dc)
162 {
163 xpmAttr.valuemask = XpmReturnInfos; /* get infos back */
164 ErrorStatus = XpmCreateImageFromData(&dc, (char **)data,
0d0512bd 165 &ximage, (XImage **) NULL, &xpmAttr);
2fd284a4
JS
166
167 if (ErrorStatus == XpmSuccess)
168 {
0d0512bd 169 XpmToBitmap(bitmap, ximage, xpmAttr);
2fd284a4 170
2fd284a4
JS
171 XpmFreeAttributes(&xpmAttr);
172
173 XImageFree(ximage); // releases the malloc, but does not detroy
174 // the bitmap
0d0512bd 175 }
2fd284a4
JS
176 else
177 {
0d0512bd 178 // XpmDebugError(ErrorStatus, NULL);
2fd284a4 179 }
0d0512bd
VZ
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();
2fd284a4
JS
188 }
189#endif
190
191 return FALSE;
192}
193