OS/2 Image processing updates along with some statusbar updates
[wxWidgets.git] / src / os2 / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: icon.cpp
3 // Purpose: wxIcon class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/09/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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
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"
25 #endif
26
27 #include "wx/os2/private.h"
28 #include "assert.h"
29
30 #include "wx/icon.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 // ----------------------------------------------------------------------------
39 // wxIconRefData
40 // ----------------------------------------------------------------------------
41
42 void wxIconRefData::Free()
43 {
44 if (m_hIcon)
45 ::WinFreeFileIcon((HPOINTER)m_hIcon);
46 }
47
48 // ----------------------------------------------------------------------------
49 // wxIcon
50 // ----------------------------------------------------------------------------
51
52 wxIcon::wxIcon()
53 {
54 }
55
56 wxIcon::wxIcon(
57 const char WXUNUSED(bits)[]
58 , int WXUNUSED(nWidth)
59 , int WXUNUSED(nHeight)
60 )
61 {
62 }
63
64 wxIcon::wxIcon(
65 const wxString& rIconFile
66 , long lFlags
67 , int nDesiredWidth
68 , int nDesiredHeight
69 )
70 {
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
80 ,lFlags
81 ,nDesiredWidth
82 ,nDesiredHeight
83 );
84 }
85
86 wxIcon::~wxIcon()
87 {
88 }
89
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();
104 HBITMAP hBmp = NULLHANDLE;
105 HBITMAP hBmpMask = NULLHANDLE;
106 HBITMAP hOldBitmap = NULLHANDLE;
107 ERRORID vError;
108 wxString sError;
109 LONG lHits;
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
122 BITMAPINFOHEADER2 vHeader;
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 };
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)
156 {
157 vError = ::WinGetLastError(vHabmain);
158 sError = wxPMErrorToStr(vError);
159 }
160 if ((hOldBitmap = ::GpiSetBitmap(hPSSrc, (HBITMAP)rBmp.GetHBITMAP())) == HBM_ERROR)
161 {
162 vError = ::WinGetLastError(vHabmain);
163 sError = wxPMErrorToStr(vError);
164 }
165 if ((lHits = ::GpiBitBlt( hPSDst
166 ,hPSSrc
167 ,4L
168 ,vPoint
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
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 }
227
228 vIconInfo.hbmPointer = hBmpMask;
229
230 HICON hIcon = ::WinCreatePointerIndirect( HWND_DESKTOP
231 ,&vIconInfo
232 );
233
234 if (!hIcon)
235 {
236 wxLogLastError(wxT("WinCreatePointerIndirect"));
237 vError = ::WinGetLastError(vHabmain);
238 sError = wxPMErrorToStr(vError);
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 }
255 ::GpiSetBitmap(hPSSrc, NULL);
256 ::GpiSetBitmap(hPSDst, NULL);
257 ::GpiDestroyPS(hPSSrc);
258 ::GpiDestroyPS(hPSDst);
259 ::DevCloseDC(hDCSrc);
260 ::DevCloseDC(hDCDst);
261 } // end of wxIcon::CopyFromBitmap
262
263 bool wxIcon::LoadFile(
264 const wxString& rFilename
265 , long lType
266 , int nDesiredWidth
267 , int nDesiredHeight
268 )
269 {
270 HPS hPs = NULLHANDLE;
271
272 UnRef();
273
274 wxGDIImageHandler* pHandler = FindHandler(lType);
275
276 if (pHandler)
277 return(pHandler->Load( this
278 ,rFilename
279 ,hPs
280 ,lType
281 ,nDesiredWidth
282 ,nDesiredHeight
283 ));
284 else
285 return(FALSE);
286 }
287