]> git.saurik.com Git - wxWidgets.git/blame - src/os2/icon.cpp
SF bug 1327872
[wxWidgets.git] / src / os2 / icon.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: icon.cpp
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__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
3b9e3455
DW
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"
6eb280e9 25 #include "wx/log.h"
0e320a79
DW
26#endif
27
fb9010ed
DW
28#include "wx/os2/private.h"
29#include "assert.h"
30
0e320a79
DW
31#include "wx/icon.h"
32
4a46a5df 33 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
0e320a79 34
3b9e3455
DW
35// ============================================================================
36// implementation
37// ============================================================================
0e320a79 38
3b9e3455
DW
39// ----------------------------------------------------------------------------
40// wxIconRefData
41// ----------------------------------------------------------------------------
0e320a79 42
3b9e3455 43void wxIconRefData::Free()
0e320a79 44{
43543d98
DW
45 if (m_hIcon)
46 ::WinFreeFileIcon((HPOINTER)m_hIcon);
0e320a79
DW
47}
48
3b9e3455
DW
49// ----------------------------------------------------------------------------
50// wxIcon
51// ----------------------------------------------------------------------------
52
0e320a79 53wxIcon::wxIcon()
77f4f0a7 54 :m_bIsXpm(false)
0e320a79
DW
55{
56}
57
77f4f0a7
WS
58wxIcon::wxIcon( const char WXUNUSED(bits)[],
59 int WXUNUSED(nWidth),
60 int WXUNUSED(nHeight) )
61 :m_bIsXpm(false)
0e320a79
DW
62{
63}
64
77f4f0a7
WS
65wxIcon::wxIcon( const wxString& rIconFile,
66 long lFlags,
67 int nDesiredWidth,
68 int nDesiredHeight )
69 :m_bIsXpm(false)
0e320a79 70{
914589c2
DW
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 //
77f4f0a7 76 wxString sOs2Name = rIconFile.Mid(0, rIconFile.Length() - 3);
914589c2 77
0fba44b4 78 sOs2Name += wxT("Os2.ico");
914589c2 79 LoadFile( sOs2Name
3b9e3455
DW
80 ,lFlags
81 ,nDesiredWidth
82 ,nDesiredHeight
83 );
0e320a79
DW
84}
85
86wxIcon::~wxIcon()
87{
88}
89
6f38c86f
DW
90void wxIcon::CreateIconFromXpm(
91 const char** ppData
92)
93{
94 wxBitmap vBmp(ppData);
95
96 CopyFromBitmap(vBmp);
29172908
DW
97 if (GetHICON())
98 {
6670f564 99 m_bIsXpm = true;
29172908
DW
100 m_vXpmSrc = vBmp;
101 }
6f38c86f
DW
102} // end of wxIcon::CreateIconFromXpm
103
6670f564 104void wxIcon::CopyFromBitmap( const wxBitmap& rBmp )
6f38c86f
DW
105{
106 wxMask* pMask = rBmp.GetMask();
16ff355b
DW
107 HBITMAP hBmp = NULLHANDLE;
108 HBITMAP hBmpMask = NULLHANDLE;
52315bc3
DW
109 HBITMAP hOldBitmap = NULLHANDLE;
110 ERRORID vError;
111 wxString sError;
112 LONG lHits;
6f38c86f
DW
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
16ff355b 125 BITMAPINFOHEADER2 vHeader;
6f38c86f
DW
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);
b93aba84
SN
132 POINTL vPoint[4] = { {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight()},
133 {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight()}
6f38c86f 134 };
b93aba84
SN
135 POINTL vPointMask[4] = { {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight() * 2},
136 {0, 0}, {rBmp.GetWidth(), rBmp.GetHeight()}
16ff355b
DW
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)
52315bc3
DW
159 {
160 vError = ::WinGetLastError(vHabmain);
161 sError = wxPMErrorToStr(vError);
162 }
16ff355b 163 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)rBmp.GetHBITMAP())) == HBM_ERROR)
52315bc3
DW
164 {
165 vError = ::WinGetLastError(vHabmain);
166 sError = wxPMErrorToStr(vError);
167 }
52315bc3
DW
168 if ((lHits = ::GpiBitBlt( hPSDst
169 ,hPSSrc
170 ,4L
171 ,vPoint
16ff355b
DW
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
29172908 191 vHeader.cy = (ULONG)rBmp.GetHeight() * 2;
16ff355b
DW
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
52315bc3
DW
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 }
6f38c86f 230
16ff355b 231 vIconInfo.hbmPointer = hBmpMask;
6f38c86f 232
77f4f0a7
WS
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
6670f564
WS
236
237 HICON hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP, &vIconInfo);
6f38c86f
DW
238
239 if (!hIcon)
240 {
241 wxLogLastError(wxT("WinCreatePointerIndirect"));
52315bc3
DW
242 vError = ::WinGetLastError(vHabmain);
243 sError = wxPMErrorToStr(vError);
6f38c86f
DW
244 }
245 else
246 {
247 SetHICON((WXHICON)hIcon);
248 SetSize( rBmp.GetWidth()
249 ,rBmp.GetHeight()
250 );
251 }
6670f564 252#endif
6f38c86f
DW
253
254 if (!rBmp.GetMask())
255 {
256 //
257 // We created the mask, now delete it
258 //
259 delete pMask;
260 }
16ff355b
DW
261 ::GpiSetBitmap(hPSSrc, NULL);
262 ::GpiSetBitmap(hPSDst, NULL);
263 ::GpiDestroyPS(hPSSrc);
264 ::GpiDestroyPS(hPSDst);
265 ::DevCloseDC(hDCSrc);
266 ::DevCloseDC(hDCDst);
6f38c86f
DW
267} // end of wxIcon::CopyFromBitmap
268
77f4f0a7
WS
269bool wxIcon::LoadFile( const wxString& rFilename,
270 long lType,
271 int nDesiredWidth,
272 int nDesiredHeight )
fb9010ed 273{
8ea3f821 274 HPS hPs = NULLHANDLE;
3b9e3455
DW
275
276 UnRef();
b41cdbf4
DW
277
278 wxGDIImageHandler* pHandler = FindHandler(lType);
3b9e3455
DW
279
280 if (pHandler)
281 return(pHandler->Load( this
282 ,rFilename
8ea3f821 283 ,hPs
3b9e3455
DW
284 ,lType
285 ,nDesiredWidth
286 ,nDesiredHeight
287 ));
fb9010ed 288 else
77f4f0a7 289 return false;
0e320a79 290}