Mono bitmap processing updates
[wxWidgets.git] / src / os2 / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: icon.cpp
3 // Purpose: wxIcon class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/09/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/defs.h"
21 #include "wx/list.h"
22 #include "wx/utils.h"
23 #include "wx/app.h"
24 #include "wx/icon.h"
25 #endif
26
27 #include "wx/os2/private.h"
28 #include "assert.h"
29
30 #include "wx/icon.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 // ----------------------------------------------------------------------------
39 // wxIconRefData
40 // ----------------------------------------------------------------------------
41
42 void wxIconRefData::Free()
43 {
44 if (m_hIcon)
45 ::WinFreeFileIcon((HPOINTER)m_hIcon);
46 }
47
48 // ----------------------------------------------------------------------------
49 // wxIcon
50 // ----------------------------------------------------------------------------
51
52 wxIcon::wxIcon()
53 : m_bIsXpm(FALSE)
54 {
55 }
56
57 wxIcon::wxIcon(
58 const char WXUNUSED(bits)[]
59 , int WXUNUSED(nWidth)
60 , int WXUNUSED(nHeight)
61 )
62 : m_bIsXpm(FALSE)
63 {
64 }
65
66 wxIcon::wxIcon(
67 const wxString& rIconFile
68 , long lFlags
69 , int nDesiredWidth
70 , int nDesiredHeight
71 )
72 : m_bIsXpm(FALSE)
73 {
74 //
75 // A very poor hack, but we have to have separate icon files from windows
76 // So we have a modified name where replace the last three characters
77 // with os2. Also need the extension.
78 //
79 wxString sOs2Name = rIconFile.Mid(0, rIconFile.Length() - 3);
80
81 sOs2Name += "Os2.ico";
82 LoadFile( sOs2Name
83 ,lFlags
84 ,nDesiredWidth
85 ,nDesiredHeight
86 );
87 }
88
89 wxIcon::~wxIcon()
90 {
91 }
92
93 void wxIcon::CreateIconFromXpm(
94 const char** ppData
95 )
96 {
97 wxBitmap vBmp(ppData);
98
99 CopyFromBitmap(vBmp);
100 if (GetHICON())
101 {
102 m_bIsXpm = TRUE;
103 m_vXpmSrc = vBmp;
104 }
105 } // end of wxIcon::CreateIconFromXpm
106
107 void wxIcon::CopyFromBitmap(
108 const wxBitmap& rBmp
109 )
110 {
111 wxMask* pMask = rBmp.GetMask();
112 HBITMAP hBmp = NULLHANDLE;
113 HBITMAP hBmpMask = NULLHANDLE;
114 HBITMAP hOldBitmap = NULLHANDLE;
115 ERRORID vError;
116 wxString sError;
117 LONG lHits;
118
119 if (!pMask)
120 {
121 //
122 // We must have a mask for an icon, so even if it's probably incorrect,
123 // do create it (grey is the "standard" transparent colour)
124 //
125 pMask = new wxMask( rBmp
126 ,*wxLIGHT_GREY
127 );
128 }
129
130 BITMAPINFOHEADER2 vHeader;
131 SIZEL vSize = {0, 0};
132 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
133 HDC hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
134 HDC hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
135 HPS hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC);
136 HPS hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC);
137 POINTL vPoint[4] = { 0, 0, rBmp.GetWidth(), rBmp.GetHeight(),
138 0, 0, rBmp.GetWidth(), rBmp.GetHeight()
139 };
140 POINTL vPointMask[4] = { 0, 0, rBmp.GetWidth(), rBmp.GetHeight() * 2,
141 0, 0, rBmp.GetWidth(), rBmp.GetHeight()
142 };
143
144 POINTERINFO vIconInfo;
145
146 memset(&vIconInfo, '\0', sizeof(POINTERINFO));
147 vIconInfo.fPointer = FALSE; // we want an icon, not a pointer
148
149 memset(&vHeader, '\0', 16);
150 vHeader.cbFix = 16;
151 vHeader.cx = (ULONG)rBmp.GetWidth();
152 vHeader.cy = (ULONG)rBmp.GetHeight();
153 vHeader.cPlanes = 1L;
154 vHeader.cBitCount = 24;
155
156 hBmp = ::GpiCreateBitmap( hPSDst
157 ,&vHeader
158 ,0L
159 ,NULL
160 ,NULL
161 );
162
163 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, hBmp)) == HBM_ERROR)
164 {
165 vError = ::WinGetLastError(vHabmain);
166 sError = wxPMErrorToStr(vError);
167 }
168 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)rBmp.GetHBITMAP())) == HBM_ERROR)
169 {
170 vError = ::WinGetLastError(vHabmain);
171 sError = wxPMErrorToStr(vError);
172 }
173 if ((lHits = ::GpiBitBlt( hPSDst
174 ,hPSSrc
175 ,4L
176 ,vPoint
177 ,ROP_SRCCOPY
178 ,BBO_IGNORE
179 )) == GPI_ERROR)
180 {
181 vError = ::WinGetLastError(vHabmain);
182 sError = wxPMErrorToStr(vError);
183 }
184 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, NULLHANDLE)) == HBM_ERROR)
185 {
186 vError = ::WinGetLastError(vHabmain);
187 sError = wxPMErrorToStr(vError);
188 }
189 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, NULLHANDLE)) == HBM_ERROR)
190 {
191 vError = ::WinGetLastError(vHabmain);
192 sError = wxPMErrorToStr(vError);
193 }
194 vIconInfo.hbmColor = hBmp;
195
196 vHeader.cy = (ULONG)rBmp.GetHeight() * 2;
197 hBmpMask = ::GpiCreateBitmap( hPSDst
198 ,&vHeader
199 ,0L
200 ,NULL
201 ,NULL
202 );
203
204 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, hBmpMask)) == HBM_ERROR)
205 {
206 vError = ::WinGetLastError(vHabmain);
207 sError = wxPMErrorToStr(vError);
208 }
209 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)pMask->GetMaskBitmap())) == HBM_ERROR)
210 {
211 vError = ::WinGetLastError(vHabmain);
212 sError = wxPMErrorToStr(vError);
213 }
214 if ((lHits = ::GpiBitBlt( hPSDst
215 ,hPSSrc
216 ,4L
217 ,vPointMask
218 ,ROP_SRCCOPY
219 ,BBO_IGNORE
220 )) == GPI_ERROR)
221 {
222 vError = ::WinGetLastError(vHabmain);
223 sError = wxPMErrorToStr(vError);
224 }
225 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, NULLHANDLE)) == HBM_ERROR)
226 {
227 vError = ::WinGetLastError(vHabmain);
228 sError = wxPMErrorToStr(vError);
229 }
230 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, NULLHANDLE)) == HBM_ERROR)
231 {
232 vError = ::WinGetLastError(vHabmain);
233 sError = wxPMErrorToStr(vError);
234 }
235
236 vIconInfo.hbmPointer = hBmpMask;
237
238 HICON hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP
239 ,&vIconInfo
240 );
241
242 if (!hIcon)
243 {
244 wxLogLastError(wxT("WinCreatePointerIndirect"));
245 vError = ::WinGetLastError(vHabmain);
246 sError = wxPMErrorToStr(vError);
247 }
248 else
249 {
250 SetHICON((WXHICON)hIcon);
251 SetSize( rBmp.GetWidth()
252 ,rBmp.GetHeight()
253 );
254 }
255
256 if (!rBmp.GetMask())
257 {
258 //
259 // We created the mask, now delete it
260 //
261 delete pMask;
262 }
263 ::GpiSetBitmap(hPSSrc, NULL);
264 ::GpiSetBitmap(hPSDst, NULL);
265 ::GpiDestroyPS(hPSSrc);
266 ::GpiDestroyPS(hPSDst);
267 ::DevCloseDC(hDCSrc);
268 ::DevCloseDC(hDCDst);
269 } // end of wxIcon::CopyFromBitmap
270
271 bool wxIcon::LoadFile(
272 const wxString& rFilename
273 , long lType
274 , int nDesiredWidth
275 , int nDesiredHeight
276 )
277 {
278 HPS hPs = NULLHANDLE;
279
280 UnRef();
281
282 wxGDIImageHandler* pHandler = FindHandler(lType);
283
284 if (pHandler)
285 return(pHandler->Load( this
286 ,rFilename
287 ,hPs
288 ,lType
289 ,nDesiredWidth
290 ,nDesiredHeight
291 ));
292 else
293 return(FALSE);
294 }
295