]>
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 DW |
52 | wxIcon::wxIcon() |
53 | { | |
54 | } | |
55 | ||
3b9e3455 DW |
56 | wxIcon::wxIcon( |
57 | const char WXUNUSED(bits)[] | |
58 | , int WXUNUSED(nWidth) | |
59 | , int WXUNUSED(nHeight) | |
60 | ) | |
0e320a79 DW |
61 | { |
62 | } | |
63 | ||
3b9e3455 DW |
64 | wxIcon::wxIcon( |
65 | const wxString& rIconFile | |
66 | , long lFlags | |
67 | , int nDesiredWidth | |
68 | , int nDesiredHeight | |
69 | ) | |
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 | // | |
76 | wxString sOs2Name = rIconFile.Mid(0, rIconFile.Length() - 3); | |
77 | ||
78 | sOs2Name += "Os2.ico"; | |
79 | LoadFile( sOs2Name | |
3b9e3455 DW |
80 | ,lFlags |
81 | ,nDesiredWidth | |
82 | ,nDesiredHeight | |
83 | ); | |
0e320a79 DW |
84 | } |
85 | ||
86 | wxIcon::~wxIcon() | |
87 | { | |
88 | } | |
89 | ||
6f38c86f DW |
90 | void wxIcon::CreateIconFromXpm( |
91 | const char** ppData | |
92 | ) | |
93 | { | |
94 | wxBitmap vBmp(ppData); | |
95 | ||
96 | CopyFromBitmap(vBmp); | |
97 | } // end of wxIcon::CreateIconFromXpm | |
98 | ||
99 | void wxIcon::CopyFromBitmap( | |
100 | const wxBitmap& rBmp | |
101 | ) | |
102 | { | |
103 | wxMask* pMask = rBmp.GetMask(); | |
16ff355b DW |
104 | HBITMAP hBmp = NULLHANDLE; |
105 | HBITMAP hBmpMask = NULLHANDLE; | |
52315bc3 DW |
106 | HBITMAP hOldBitmap = NULLHANDLE; |
107 | ERRORID vError; | |
108 | wxString sError; | |
109 | LONG lHits; | |
6f38c86f DW |
110 | |
111 | if (!pMask) | |
112 | { | |
113 | // | |
114 | // We must have a mask for an icon, so even if it's probably incorrect, | |
115 | // do create it (grey is the "standard" transparent colour) | |
116 | // | |
117 | pMask = new wxMask( rBmp | |
118 | ,*wxLIGHT_GREY | |
119 | ); | |
120 | } | |
121 | ||
16ff355b | 122 | BITMAPINFOHEADER2 vHeader; |
6f38c86f DW |
123 | SIZEL vSize = {0, 0}; |
124 | DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L}; | |
125 | HDC hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE); | |
126 | HDC hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE); | |
127 | HPS hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC); | |
128 | HPS hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC); | |
129 | POINTL vPoint[4] = { 0, 0, rBmp.GetWidth(), rBmp.GetHeight(), | |
130 | 0, 0, rBmp.GetWidth(), rBmp.GetHeight() | |
131 | }; | |
16ff355b DW |
132 | POINTL vPointMask[4] = { 0, 0, rBmp.GetWidth(), rBmp.GetHeight(), |
133 | 0, 0, rBmp.GetWidth(), rBmp.GetHeight() | |
134 | }; | |
135 | ||
136 | POINTERINFO vIconInfo; | |
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 | ||
188 | vHeader.cy = (ULONG)rBmp.GetHeight(); | |
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 DW |
229 | |
230 | HICON hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP | |
231 | ,&vIconInfo | |
232 | ); | |
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 | ||
3b9e3455 DW |
263 | bool wxIcon::LoadFile( |
264 | const wxString& rFilename | |
265 | , long lType | |
266 | , int nDesiredWidth | |
267 | , int nDesiredHeight | |
268 | ) | |
fb9010ed | 269 | { |
8ea3f821 | 270 | HPS hPs = NULLHANDLE; |
3b9e3455 DW |
271 | |
272 | UnRef(); | |
b41cdbf4 DW |
273 | |
274 | wxGDIImageHandler* pHandler = FindHandler(lType); | |
3b9e3455 DW |
275 | |
276 | if (pHandler) | |
277 | return(pHandler->Load( this | |
278 | ,rFilename | |
8ea3f821 | 279 | ,hPs |
3b9e3455 DW |
280 | ,lType |
281 | ,nDesiredWidth | |
282 | ,nDesiredHeight | |
283 | )); | |
fb9010ed | 284 | else |
3b9e3455 | 285 | return(FALSE); |
0e320a79 DW |
286 | } |
287 |