]> git.saurik.com Git - wxWidgets.git/blame - src/os2/icon.cpp
Include wx/dynarray.h according to precompiled headers of wx/wx.h (with other minor...
[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
19#ifndef WX_PRECOMP
3b9e3455
DW
20 #include "wx/list.h"
21 #include "wx/utils.h"
22 #include "wx/app.h"
23 #include "wx/icon.h"
6eb280e9 24 #include "wx/log.h"
0e320a79
DW
25#endif
26
fb9010ed
DW
27#include "wx/os2/private.h"
28#include "assert.h"
29
0e320a79
DW
30#include "wx/icon.h"
31
4a46a5df 32 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
0e320a79 33
3b9e3455
DW
34// ============================================================================
35// implementation
36// ============================================================================
0e320a79 37
3b9e3455
DW
38// ----------------------------------------------------------------------------
39// wxIconRefData
40// ----------------------------------------------------------------------------
0e320a79 41
3b9e3455 42void wxIconRefData::Free()
0e320a79 43{
43543d98
DW
44 if (m_hIcon)
45 ::WinFreeFileIcon((HPOINTER)m_hIcon);
0e320a79
DW
46}
47
3b9e3455
DW
48// ----------------------------------------------------------------------------
49// wxIcon
50// ----------------------------------------------------------------------------
51
0e320a79 52wxIcon::wxIcon()
77f4f0a7 53 :m_bIsXpm(false)
0e320a79
DW
54{
55}
56
77f4f0a7
WS
57wxIcon::wxIcon( const char WXUNUSED(bits)[],
58 int WXUNUSED(nWidth),
59 int WXUNUSED(nHeight) )
60 :m_bIsXpm(false)
0e320a79
DW
61{
62}
63
77f4f0a7
WS
64wxIcon::wxIcon( const wxString& rIconFile,
65 long lFlags,
66 int nDesiredWidth,
67 int nDesiredHeight )
68 :m_bIsXpm(false)
0e320a79 69{
914589c2
DW
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 //
cb7d7375 75 wxString sOs2Name = rIconFile.Mid(0, rIconFile.length() - 3);
914589c2 76
0fba44b4 77 sOs2Name += wxT("Os2.ico");
914589c2 78 LoadFile( sOs2Name
3b9e3455
DW
79 ,lFlags
80 ,nDesiredWidth
81 ,nDesiredHeight
82 );
0e320a79
DW
83}
84
85wxIcon::~wxIcon()
86{
87}
88
6f38c86f
DW
89void wxIcon::CreateIconFromXpm(
90 const char** ppData
91)
92{
93 wxBitmap vBmp(ppData);
94
95 CopyFromBitmap(vBmp);
29172908
DW
96 if (GetHICON())
97 {
6670f564 98 m_bIsXpm = true;
29172908
DW
99 m_vXpmSrc = vBmp;
100 }
6f38c86f
DW
101} // end of wxIcon::CreateIconFromXpm
102
6670f564 103void wxIcon::CopyFromBitmap( const wxBitmap& rBmp )
6f38c86f
DW
104{
105 wxMask* pMask = rBmp.GetMask();
16ff355b
DW
106 HBITMAP hBmp = NULLHANDLE;
107 HBITMAP hBmpMask = NULLHANDLE;
52315bc3
DW
108 HBITMAP hOldBitmap = NULLHANDLE;
109 ERRORID vError;
110 wxString sError;
111 LONG lHits;
6f38c86f
DW
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
cb7d7375
WS
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;
16ff355b
DW
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)
52315bc3
DW
157 {
158 vError = ::WinGetLastError(vHabmain);
159 sError = wxPMErrorToStr(vError);
160 }
16ff355b 161 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)rBmp.GetHBITMAP())) == HBM_ERROR)
52315bc3
DW
162 {
163 vError = ::WinGetLastError(vHabmain);
164 sError = wxPMErrorToStr(vError);
165 }
52315bc3
DW
166 if ((lHits = ::GpiBitBlt( hPSDst
167 ,hPSSrc
168 ,4L
169 ,vPoint
16ff355b
DW
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
29172908 189 vHeader.cy = (ULONG)rBmp.GetHeight() * 2;
16ff355b
DW
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
52315bc3
DW
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 }
6f38c86f 228
16ff355b 229 vIconInfo.hbmPointer = hBmpMask;
6f38c86f 230
6670f564 231 HICON hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP, &vIconInfo);
6f38c86f
DW
232
233 if (!hIcon)
234 {
235 wxLogLastError(wxT("WinCreatePointerIndirect"));
52315bc3
DW
236 vError = ::WinGetLastError(vHabmain);
237 sError = wxPMErrorToStr(vError);
6f38c86f
DW
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 }
16ff355b
DW
254 ::GpiSetBitmap(hPSSrc, NULL);
255 ::GpiSetBitmap(hPSDst, NULL);
256 ::GpiDestroyPS(hPSSrc);
257 ::GpiDestroyPS(hPSDst);
258 ::DevCloseDC(hDCSrc);
259 ::DevCloseDC(hDCDst);
6f38c86f
DW
260} // end of wxIcon::CopyFromBitmap
261
77f4f0a7
WS
262bool wxIcon::LoadFile( const wxString& rFilename,
263 long lType,
264 int nDesiredWidth,
265 int nDesiredHeight )
fb9010ed 266{
8ea3f821 267 HPS hPs = NULLHANDLE;
3b9e3455
DW
268
269 UnRef();
b41cdbf4
DW
270
271 wxGDIImageHandler* pHandler = FindHandler(lType);
3b9e3455
DW
272
273 if (pHandler)
274 return(pHandler->Load( this
275 ,rFilename
8ea3f821 276 ,hPs
3b9e3455
DW
277 ,lType
278 ,nDesiredWidth
279 ,nDesiredHeight
280 ));
fb9010ed 281 else
77f4f0a7 282 return false;
0e320a79 283}