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