MSW compilation (and other) fixes
[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 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
49
50 bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
51 int desiredWidth, int desiredHeight)
52 {
53 #if wxUSE_XPM_IN_MSW
54 XImage *ximage;
55 XpmAttributes xpmAttr;
56 HDC dc;
57
58 M_BITMAPHANDLERDATA->m_ok = FALSE;
59 dc = CreateCompatibleDC(NULL);
60 if (dc)
61 {
62 xpmAttr.valuemask = XpmReturnPixels;
63 int errorStatus = XpmReadFileToImage(&dc, wxMBSTRINGCAST name.fn_str(), &ximage, (XImage **) NULL, &xpmAttr);
64 DeleteDC(dc);
65 if (errorStatus == XpmSuccess)
66 {
67 M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ximage->bitmap;
68
69 BITMAP bm;
70 GetObject((HBITMAP)M_BITMAPHANDLERDATA->m_hBitmap, sizeof(bm), (LPSTR) & bm);
71
72 M_BITMAPHANDLERDATA->m_width = (bm.bmWidth);
73 M_BITMAPHANDLERDATA->m_height = (bm.bmHeight);
74 M_BITMAPHANDLERDATA->m_depth = (bm.bmPlanes * bm.bmBitsPixel);
75 M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels;
76 XpmFreeAttributes(&xpmAttr);
77 XImageFree(ximage);
78
79 M_BITMAPHANDLERDATA->m_ok = TRUE;
80 return TRUE;
81 }
82 else
83 {
84 M_BITMAPHANDLERDATA->m_ok = FALSE;
85 return FALSE;
86 }
87 }
88 #endif
89
90 return FALSE;
91 }
92
93 bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
94 {
95 #if wxUSE_XPM_IN_MSW
96 HDC dc = NULL;
97
98 XImage ximage;
99
100 dc = CreateCompatibleDC(NULL);
101 if (dc)
102 {
103 if (SelectObject(dc, (HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap))
104 {
105 /* for following SetPixel */
106 /* fill the XImage struct 'by hand' */
107 ximage.width = M_BITMAPHANDLERDATA->m_width;
108 ximage.height = M_BITMAPHANDLERDATA->m_height;
109 ximage.depth = M_BITMAPHANDLERDATA->m_depth;
110 ximage.bitmap = (HBITMAP)M_BITMAPHANDLERDATA->m_hBitmap;
111 int errorStatus = XpmWriteFileFromImage(&dc, wxMBSTRINGCAST name.fn_str(),
112 &ximage, (XImage *) NULL, (XpmAttributes *) NULL);
113
114 if (dc)
115 DeleteDC(dc);
116
117 if (errorStatus == XpmSuccess)
118 return TRUE; /* no error */
119 else
120 return FALSE;
121 } else return FALSE;
122 } else return FALSE;
123 #else
124 return FALSE;
125 #endif
126 }
127
128 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
129
130 bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth)
131 {
132 #if wxUSE_XPM_IN_MSW
133 XImage *ximage;
134 int ErrorStatus;
135 XpmAttributes xpmAttr;
136 HDC dc;
137
138 M_BITMAPHANDLERDATA->m_ok = FALSE;
139 M_BITMAPHANDLERDATA->m_numColors = 0;
140
141 dc = CreateCompatibleDC(NULL); /* memory DC */
142
143 if (dc)
144 {
145 xpmAttr.valuemask = XpmReturnInfos; /* get infos back */
146 ErrorStatus = XpmCreateImageFromData(&dc, (char **)data,
147 &ximage, (XImage **) NULL, &xpmAttr);
148
149 if (ErrorStatus == XpmSuccess)
150 {
151 /* ximage is malloced and contains bitmap and attributes */
152 M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ximage->bitmap;
153
154 BITMAP bm;
155 GetObject((HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap, sizeof(bm), (LPSTR) & bm);
156
157 M_BITMAPHANDLERDATA->m_width = (bm.bmWidth);
158 M_BITMAPHANDLERDATA->m_height = (bm.bmHeight);
159 M_BITMAPHANDLERDATA->m_depth = (bm.bmPlanes * bm.bmBitsPixel);
160 M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels;
161 XpmFreeAttributes(&xpmAttr);
162
163 XImageFree(ximage); // releases the malloc, but does not detroy
164 // the bitmap
165 M_BITMAPHANDLERDATA->m_ok = TRUE;
166 DeleteDC(dc);
167
168 return TRUE;
169 }
170 else
171 {
172 M_BITMAPHANDLERDATA->m_ok = FALSE;
173 // XpmDebugError(ErrorStatus, NULL);
174 DeleteDC(dc);
175 return FALSE;
176 }
177 }
178 #endif
179
180 return FALSE;
181 }
182