]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/xpmhand.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmap: XPM handler and constructors
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "xpmhand.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/palette.h"
30 #include "wx/dcmemory.h"
31 #include "wx/bitmap.h"
35 #include "wx/msw/private.h"
42 #include "../src/xpm/xpm34.h"
45 #include "wx/xpmhand.h"
46 #include "wx/msw/dib.h"
48 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
50 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
51 int desiredWidth
, int desiredHeight
)
55 XpmAttributes xpmAttr
;
58 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
59 dc
= CreateCompatibleDC(NULL
);
62 xpmAttr
.valuemask
= XpmReturnPixels
;
63 int errorStatus
= XpmReadFileToImage(&dc
, WXSTRINGCAST name
, &ximage
, (XImage
**) NULL
, &xpmAttr
);
65 if (errorStatus
== XpmSuccess
)
67 M_BITMAPHANDLERDATA
->m_hBitmap
= (WXHBITMAP
) ximage
->bitmap
;
70 GetObject((HBITMAP
)M_BITMAPHANDLERDATA
->m_hBitmap
, sizeof(bm
), (LPSTR
) & bm
);
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
);
79 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
84 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
93 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
100 dc
= CreateCompatibleDC(NULL
);
103 if (SelectObject(dc
, (HBITMAP
) M_BITMAPHANDLERDATA
->m_hBitmap
))
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
, WXSTRINGCAST name
,
112 &ximage
, (XImage
*) NULL
, (XpmAttributes
*) NULL
);
117 if (errorStatus
== XpmSuccess
)
118 return TRUE
; /* no error */
128 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
130 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
135 XpmAttributes xpmAttr
;
138 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
139 M_BITMAPHANDLERDATA
->m_numColors
= 0;
141 dc
= CreateCompatibleDC(NULL
); /* memory DC */
145 xpmAttr
.valuemask
= XpmReturnInfos
; /* get infos back */
146 ErrorStatus
= XpmCreateImageFromData(&dc
, (char **)data
,
147 &ximage
, (XImage
**) NULL
, &xpmAttr
);
149 if (ErrorStatus
== XpmSuccess
)
151 /* ximage is malloced and contains bitmap and attributes */
152 M_BITMAPHANDLERDATA
->m_hBitmap
= (WXHBITMAP
) ximage
->bitmap
;
155 GetObject((HBITMAP
) M_BITMAPHANDLERDATA
->m_hBitmap
, sizeof(bm
), (LPSTR
) & bm
);
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
);
163 XImageFree(ximage
); // releases the malloc, but does not detroy
165 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
172 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
173 // XpmDebugError(ErrorStatus, NULL);