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