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