]>
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 "../xpm/xpm34.h"
45 #include "wx/xpmhand.h"
46 #include "wx/msw/dib.h"
48 static void XpmToBitmap(wxBitmap
*bitmap
,
50 const XpmAttributes
& xpmAttr
)
52 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
53 refData
->m_hBitmap
= (WXHBITMAP
)ximage
->bitmap
;
56 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(bm
), (LPSTR
) & bm
) )
58 wxLogLastError("GetObject(bitmap)");
61 refData
->m_width
= bm
.bmWidth
;
62 refData
->m_height
= bm
.bmHeight
;
63 refData
->m_depth
= bm
.bmPlanes
* bm
.bmBitsPixel
;
64 refData
->m_numColors
= xpmAttr
.npixels
;
67 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
69 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
70 int desiredWidth
, int desiredHeight
)
74 XpmAttributes xpmAttr
;
77 dc
= CreateCompatibleDC(NULL
);
80 xpmAttr
.valuemask
= XpmReturnPixels
;
81 int errorStatus
= XpmReadFileToImage(&dc
, wxMBSTRINGCAST name
.fn_str(), &ximage
, (XImage
**) NULL
, &xpmAttr
);
83 if (errorStatus
== XpmSuccess
)
85 XpmToBitmap(bitmap
, ximage
, xpmAttr
);
87 XpmFreeAttributes(&xpmAttr
);
91 #if WXWIN_COMPATIBILITY_2
92 bitmap
->SetOk(errorStatus
== XpmSuccess
);
93 #endif // WXWIN_COMPATIBILITY_2
102 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
109 dc
= CreateCompatibleDC(NULL
);
112 if ( SelectObject(dc
, GetHbitmapOf(*bitmap
)) )
114 /* for following SetPixel */
115 /* fill the XImage struct 'by hand' */
116 wxBitmapRefData
*refData
= bitmap
->GetBitmapData();
117 ximage
.width
= refData
->m_width
;
118 ximage
.height
= refData
->m_height
;
119 ximage
.depth
= refData
->m_depth
;
120 ximage
.bitmap
= (HBITMAP
)refData
->m_hBitmap
;
121 int errorStatus
= XpmWriteFileFromImage(&dc
, wxMBSTRINGCAST name
.fn_str(),
122 &ximage
, (XImage
*) NULL
,
123 (XpmAttributes
*) NULL
);
128 if (errorStatus
== XpmSuccess
)
129 return TRUE
; /* no error */
139 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
141 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
146 XpmAttributes xpmAttr
;
149 dc
= CreateCompatibleDC(NULL
); /* memory DC */
153 xpmAttr
.valuemask
= XpmReturnInfos
; /* get infos back */
154 ErrorStatus
= XpmCreateImageFromData(&dc
, (char **)data
,
155 &ximage
, (XImage
**) NULL
, &xpmAttr
);
157 if (ErrorStatus
== XpmSuccess
)
159 XpmToBitmap(bitmap
, ximage
, xpmAttr
);
161 XpmFreeAttributes(&xpmAttr
);
163 XImageFree(ximage
); // releases the malloc, but does not detroy
168 // XpmDebugError(ErrorStatus, NULL);
173 #if WXWIN_COMPATIBILITY_2
174 bitmap
->SetOk(errorStatus
== XpmSuccess
);
175 #endif // WXWIN_COMPATIBILITY_2