Support for automatic setup.h in OS/2 with OW builds. __WXOS2__ final removal. Source...
[wxWidgets.git] / src / os2 / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/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 POINTERINFO vIconInfo;
139
140 memset(&vIconInfo, '\0', sizeof(POINTERINFO));
141 vIconInfo.fPointer = FALSE; // we want an icon, not a pointer
142
143 memset(&vHeader, '\0', 16);
144 vHeader.cbFix = 16;
145 vHeader.cx = (ULONG)rBmp.GetWidth();
146 vHeader.cy = (ULONG)rBmp.GetHeight();
147 vHeader.cPlanes = 1L;
148 vHeader.cBitCount = 24;
149
150 hBmp = ::GpiCreateBitmap( hPSDst
151 ,&vHeader
152 ,0L
153 ,NULL
154 ,NULL
155 );
156
157 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, hBmp)) == HBM_ERROR)
158 {
159 vError = ::WinGetLastError(vHabmain);
160 sError = wxPMErrorToStr(vError);
161 }
162 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)rBmp.GetHBITMAP())) == HBM_ERROR)
163 {
164 vError = ::WinGetLastError(vHabmain);
165 sError = wxPMErrorToStr(vError);
166 }
167 if ((lHits = ::GpiBitBlt( hPSDst
168 ,hPSSrc
169 ,4L
170 ,vPoint
171 ,ROP_SRCCOPY
172 ,BBO_IGNORE
173 )) == GPI_ERROR)
174 {
175 vError = ::WinGetLastError(vHabmain);
176 sError = wxPMErrorToStr(vError);
177 }
178 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, NULLHANDLE)) == HBM_ERROR)
179 {
180 vError = ::WinGetLastError(vHabmain);
181 sError = wxPMErrorToStr(vError);
182 }
183 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, NULLHANDLE)) == HBM_ERROR)
184 {
185 vError = ::WinGetLastError(vHabmain);
186 sError = wxPMErrorToStr(vError);
187 }
188 vIconInfo.hbmColor = hBmp;
189
190 vHeader.cy = (ULONG)rBmp.GetHeight() * 2;
191 hBmpMask = ::GpiCreateBitmap( hPSDst
192 ,&vHeader
193 ,0L
194 ,NULL
195 ,NULL
196 );
197
198 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, hBmpMask)) == HBM_ERROR)
199 {
200 vError = ::WinGetLastError(vHabmain);
201 sError = wxPMErrorToStr(vError);
202 }
203 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)pMask->GetMaskBitmap())) == HBM_ERROR)
204 {
205 vError = ::WinGetLastError(vHabmain);
206 sError = wxPMErrorToStr(vError);
207 }
208 if ((lHits = ::GpiBitBlt( hPSDst
209 ,hPSSrc
210 ,4L
211 ,vPointMask
212 ,ROP_SRCCOPY
213 ,BBO_IGNORE
214 )) == GPI_ERROR)
215 {
216 vError = ::WinGetLastError(vHabmain);
217 sError = wxPMErrorToStr(vError);
218 }
219 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, NULLHANDLE)) == HBM_ERROR)
220 {
221 vError = ::WinGetLastError(vHabmain);
222 sError = wxPMErrorToStr(vError);
223 }
224 if ((hOldBitmap = ::GpiSetBitmap(hPSDst, NULLHANDLE)) == HBM_ERROR)
225 {
226 vError = ::WinGetLastError(vHabmain);
227 sError = wxPMErrorToStr(vError);
228 }
229
230 vIconInfo.hbmPointer = hBmpMask;
231
232 HICON hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP, &vIconInfo);
233
234 if (!hIcon)
235 {
236 wxLogLastError(wxT("WinCreatePointerIndirect"));
237 vError = ::WinGetLastError(vHabmain);
238 sError = wxPMErrorToStr(vError);
239 }
240 else
241 {
242 SetHICON((WXHICON)hIcon);
243 SetSize( rBmp.GetWidth()
244 ,rBmp.GetHeight()
245 );
246 }
247
248 if (!rBmp.GetMask())
249 {
250 //
251 // We created the mask, now delete it
252 //
253 delete pMask;
254 }
255 ::GpiSetBitmap(hPSSrc, NULL);
256 ::GpiSetBitmap(hPSDst, NULL);
257 ::GpiDestroyPS(hPSSrc);
258 ::GpiDestroyPS(hPSDst);
259 ::DevCloseDC(hDCSrc);
260 ::DevCloseDC(hDCDst);
261 } // end of wxIcon::CopyFromBitmap
262
263 bool wxIcon::LoadFile( const wxString& rFilename,
264 long lType,
265 int nDesiredWidth,
266 int nDesiredHeight )
267 {
268 HPS hPs = NULLHANDLE;
269
270 UnRef();
271
272 wxGDIImageHandler* pHandler = FindHandler(lType);
273
274 if (pHandler)
275 return(pHandler->Load( this
276 ,rFilename
277 ,hPs
278 ,lType
279 ,nDesiredWidth
280 ,nDesiredHeight
281 ));
282 else
283 return false;
284 }