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