]> git.saurik.com Git - wxWidgets.git/blame - src/os2/icon.cpp
centralized the handling of border styles; added borders support for wxListBox and...
[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__
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 //
cb7d7375 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
cb7d7375
WS
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;
16ff355b
DW
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)
52315bc3
DW
158 {
159 vError = ::WinGetLastError(vHabmain);
160 sError = wxPMErrorToStr(vError);
161 }
16ff355b 162 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)rBmp.GetHBITMAP())) == HBM_ERROR)
52315bc3
DW
163 {
164 vError = ::WinGetLastError(vHabmain);
165 sError = wxPMErrorToStr(vError);
166 }
52315bc3
DW
167 if ((lHits = ::GpiBitBlt( hPSDst
168 ,hPSSrc
169 ,4L
170 ,vPoint
16ff355b
DW
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
29172908 190 vHeader.cy = (ULONG)rBmp.GetHeight() * 2;
16ff355b
DW
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
52315bc3
DW
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 }
6f38c86f 229
16ff355b 230 vIconInfo.hbmPointer = hBmpMask;
6f38c86f 231
6670f564 232 HICON hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP, &vIconInfo);
6f38c86f
DW
233
234 if (!hIcon)
235 {
236 wxLogLastError(wxT("WinCreatePointerIndirect"));
52315bc3
DW
237 vError = ::WinGetLastError(vHabmain);
238 sError = wxPMErrorToStr(vError);
6f38c86f
DW
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 }
16ff355b
DW
255 ::GpiSetBitmap(hPSSrc, NULL);
256 ::GpiSetBitmap(hPSDst, NULL);
257 ::GpiDestroyPS(hPSSrc);
258 ::GpiDestroyPS(hPSDst);
259 ::DevCloseDC(hDCSrc);
260 ::DevCloseDC(hDCDst);
6f38c86f
DW
261} // end of wxIcon::CopyFromBitmap
262
77f4f0a7
WS
263bool wxIcon::LoadFile( const wxString& rFilename,
264 long lType,
265 int nDesiredWidth,
266 int nDesiredHeight )
fb9010ed 267{
8ea3f821 268 HPS hPs = NULLHANDLE;
3b9e3455
DW
269
270 UnRef();
b41cdbf4
DW
271
272 wxGDIImageHandler* pHandler = FindHandler(lType);
3b9e3455
DW
273
274 if (pHandler)
275 return(pHandler->Load( this
276 ,rFilename
8ea3f821 277 ,hPs
3b9e3455
DW
278 ,lType
279 ,nDesiredWidth
280 ,nDesiredHeight
281 ));
fb9010ed 282 else
77f4f0a7 283 return false;
0e320a79 284}