]> git.saurik.com Git - wxWidgets.git/blob - src/os2/gdiimage.cpp
fixed double deletion (patch #945491)
[wxWidgets.git] / src / os2 / gdiimage.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/gdiimage.cpp
3 // Purpose: wxGDIImage implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.11.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "gdiimage.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27
28 #ifndef WX_PRECOMP
29 #include "wx/string.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/os2/private.h"
33 #include "wx/app.h"
34 #include "wx/os2/gdiimage.h"
35
36 #include "wx/listimpl.cpp"
37 WX_DEFINE_LIST(wxGDIImageHandlerList);
38
39 // ----------------------------------------------------------------------------
40 // private classes
41 // ----------------------------------------------------------------------------
42
43 // all image handlers are declared/defined in this file because the outside
44 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
45
46 class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler
47 {
48 public:
49 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
50 wxBITMAP_TYPE_BMP)
51 {
52 }
53
54 virtual bool LoadFile( wxBitmap* pBitmap
55 ,const wxString& rName
56 ,HPS hPs
57 ,long lFlags
58 ,int nDesiredWidth
59 ,int nDesiredHeight
60 );
61 virtual bool SaveFile( wxBitmap* pBitmap
62 ,const wxString& rName
63 ,int lType
64 ,const wxPalette* pPalette = NULL
65 );
66
67 private:
68 inline virtual bool LoadFile( wxBitmap* pBitmap
69 ,int nId
70 ,long lFlags
71 ,int nDesiredWidth
72 ,int nDesiredHeight
73 )
74 {
75 return wxBitmapHandler::LoadFile( pBitmap
76 ,nId
77 ,lFlags
78 ,nDesiredWidth
79 ,nDesiredHeight
80 );
81 }
82 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
83 };
84
85 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
86 {
87 public:
88 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
89 wxEmptyString,
90 wxBITMAP_TYPE_BMP_RESOURCE)
91 {
92 }
93
94 virtual bool LoadFile( wxBitmap* pBitmap
95 ,int nId
96 ,long lFlags
97 ,int nDesiredWidth
98 ,int nDesiredHeight
99 );
100
101 private:
102 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
103 };
104
105 class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler
106 {
107 public:
108 wxIconHandler( const wxString& rName
109 ,const wxString& rExt
110 ,long lType
111 ) : wxGDIImageHandler( rName
112 ,rExt
113 ,lType
114 )
115 {
116 }
117
118 // creating and saving icons is not supported
119 virtual bool Create( wxGDIImage* WXUNUSED(pImage)
120 ,void* WXUNUSED(pData)
121 ,long WXUNUSED(lFlags)
122 ,int WXUNUSED(nWidth)
123 ,int WXUNUSED(nHeight)
124 ,int WXUNUSED(nDepth) = 1
125 )
126 {
127 return(FALSE);
128 }
129
130 virtual bool Save( wxGDIImage* WXUNUSED(pImage)
131 ,const wxString& WXUNUSED(rName)
132 ,int WXUNUSED(nType)
133 )
134 {
135 return(FALSE);
136 }
137 virtual bool Load( wxGDIImage* pImage
138 ,const wxString& rName
139 ,HPS hPs
140 ,long lFlags
141 ,int nDesiredWidth
142 ,int nDesiredHeight
143 )
144 {
145 wxIcon* pIcon = wxDynamicCast(pImage, wxIcon);
146 wxCHECK_MSG(pIcon, FALSE, _T("wxIconHandler only works with icons"));
147
148 return LoadIcon( pIcon
149 ,rName
150 ,hPs
151 ,lFlags
152 ,nDesiredWidth
153 ,nDesiredHeight
154 );
155 }
156
157 protected:
158 virtual bool LoadIcon( wxIcon* pIcon
159 ,const wxString& rName
160 ,HPS hPs
161 ,long lFlags
162 ,int nDesiredWidth = -1
163 ,int nDesiredHeight = -1
164 ) = 0;
165 private:
166 inline virtual bool Load( wxGDIImage* pImage
167 ,int nId
168 ,long lFlags
169 ,int nDesiredWidth
170 ,int nDesiredHeight
171 )
172 {
173 return FALSE;
174 }
175 };
176
177 class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
178 {
179 public:
180 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
181 _T("ico"),
182 wxBITMAP_TYPE_ICO)
183 {
184 }
185
186 virtual bool LoadIcon( wxIcon * pIcon
187 ,const wxString& rName
188 ,HPS hPs
189 ,long lFlags
190 ,int nDesiredWidth = -1
191 ,int nDesiredHeight = -1
192 );
193
194 private:
195 DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
196 };
197
198 class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
199 {
200 public:
201 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
202 _T("ico"),
203 wxBITMAP_TYPE_ICO_RESOURCE)
204 {
205 }
206
207 virtual bool LoadIcon( wxIcon* pIcon
208 ,const wxString& rName
209 ,HPS hPs
210 ,long lFlags
211 ,int nDesiredWidth = -1
212 ,int nDesiredHeight = -1
213 );
214
215 private:
216 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler)
217 };
218
219 // ----------------------------------------------------------------------------
220 // wxWin macros
221 // ----------------------------------------------------------------------------
222
223 #if !USE_SHARED_LIBRARIES
224 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
225 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
226 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
227 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
228 #endif
229
230 // ============================================================================
231 // implementation
232 // ============================================================================
233
234 wxGDIImageHandlerList wxGDIImage::ms_handlers;
235
236 // ----------------------------------------------------------------------------
237 // wxGDIImage functions forwarded to wxGDIImageRefData
238 // ----------------------------------------------------------------------------
239
240 bool wxGDIImage::FreeResource(
241 bool WXUNUSED(bForce)
242 )
243 {
244 if ( !IsNull() )
245 {
246 GetGDIImageData()->Free();
247 GetGDIImageData()->m_hHandle = 0;
248 }
249
250 return(TRUE);
251 }
252
253 WXHANDLE wxGDIImage::GetResourceHandle()
254 {
255 return GetHandle();
256 }
257
258 // ----------------------------------------------------------------------------
259 // wxGDIImage handler stuff
260 // ----------------------------------------------------------------------------
261
262 void wxGDIImage::AddHandler(
263 wxGDIImageHandler* pHandler
264 )
265 {
266 ms_handlers.Append(pHandler);
267 }
268
269 void wxGDIImage::InsertHandler(
270 wxGDIImageHandler* pHandler
271 )
272 {
273 ms_handlers.Insert(pHandler);
274 }
275
276 bool wxGDIImage::RemoveHandler(
277 const wxString& rName
278 )
279 {
280 wxGDIImageHandler* pHandler = FindHandler(rName);
281
282 if (pHandler)
283 {
284 ms_handlers.DeleteObject(pHandler);
285 return(TRUE);
286 }
287 else
288 return(FALSE);
289 }
290
291 wxGDIImageHandler* wxGDIImage::FindHandler(
292 const wxString& rName
293 )
294 {
295 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
296
297 while ( pNode )
298 {
299 wxGDIImageHandler* pHandler = pNode->GetData();
300
301 if ( pHandler->GetName() == rName )
302 return pHandler;
303 pNode = pNode->GetNext();
304 }
305 return((wxGDIImageHandler*)NULL);
306 }
307
308 wxGDIImageHandler* wxGDIImage::FindHandler(
309 const wxString& rExtension
310 , long lType
311 )
312 {
313 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
314 while ( pNode )
315 {
316 wxGDIImageHandler* pHandler = pNode->GetData();
317
318 if ( (pHandler->GetExtension() = rExtension) &&
319 (lType == -1 || pHandler->GetType() == lType) )
320 {
321 return pHandler;
322 }
323 pNode = pNode->GetNext();
324 }
325 return((wxGDIImageHandler*)NULL);
326 }
327
328 wxGDIImageHandler* wxGDIImage::FindHandler(
329 long lType
330 )
331 {
332 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
333
334 while ( pNode )
335 {
336 wxGDIImageHandler* pHandler = pNode->GetData();
337
338 if ( pHandler->GetType() == lType )
339 return pHandler;
340 pNode = pNode->GetNext();
341 }
342 return((wxGDIImageHandler*)NULL);
343 }
344
345 void wxGDIImage::CleanUpHandlers()
346 {
347 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
348
349 while ( pNode )
350 {
351 wxGDIImageHandler* pHandler = pNode->GetData();
352 wxGDIImageHandlerList::compatibility_iterator pNext = pNode->GetNext();
353
354 delete pHandler;
355 ms_handlers.Erase( pNode );
356 pNode = pNext;
357 }
358 }
359
360 void wxGDIImage::InitStandardHandlers()
361 {
362 AddHandler(new wxBMPResourceHandler);
363 AddHandler(new wxBMPFileHandler);
364 AddHandler(new wxICOResourceHandler);
365 AddHandler(new wxICOFileHandler);
366 }
367
368 // ----------------------------------------------------------------------------
369 // wxBitmap handlers
370 // ----------------------------------------------------------------------------
371
372 bool wxBMPResourceHandler::LoadFile(
373 wxBitmap* pBitmap
374 , int nId
375 , long lFlags
376 , int nDesiredWidth
377 , int nDesiredHeight
378 )
379 {
380 SIZEL vSize = {0, 0};
381 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
382 HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
383 HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
384
385 pBitmap->SetHBITMAP((WXHBITMAP)::GpiLoadBitmap( hPS
386 ,NULLHANDLE
387 ,nId
388 ,0
389 ,0
390 ));
391 ::GpiDestroyPS(hPS);
392 ::DevCloseDC(hDC);
393
394 wxBitmapRefData* pData = pBitmap->GetBitmapData();
395
396 if ( pBitmap->Ok() )
397 {
398 BITMAPINFOHEADER vBmph;
399
400 ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap), &vBmph);
401 pData->m_nWidth = vBmph.cx;
402 pData->m_nHeight = vBmph.cy;
403 pData->m_nDepth = vBmph.cBitCount;
404 }
405 return(pBitmap->Ok());
406 } // end of wxBMPResourceHandler::LoadFile
407
408 bool wxBMPFileHandler::LoadFile(
409 wxBitmap* pBitmap
410 , const wxString& rName
411 , HPS hPs
412 , long WXUNUSED(lFlags)
413 , int WXUNUSED(nDesiredWidth)
414 , int WXUNUSED(nDesiredHeight)
415 )
416 {
417 #if wxUSE_IMAGE_LOADING_IN_OS2
418 wxPalette* pPalette = NULL;
419
420 bool bSuccess = FALSE; /* wxLoadIntoBitmap( WXSTRINGCAST rName
421 ,pBitmap
422 ,&pPalette
423 ) != 0; */
424 if (bSuccess && pPalette)
425 {
426 pBitmap->SetPalette(*pPalette);
427 }
428
429 // it was copied by the bitmap if it was loaded successfully
430 delete pPalette;
431
432 return(bSuccess);
433 #else
434 return(FALSE);
435 #endif
436 }
437
438 bool wxBMPFileHandler::SaveFile(
439 wxBitmap* pBitmap
440 , const wxString& rName
441 , int WXUNUSED(nType)
442 , const wxPalette* pPal
443 )
444 {
445 #if wxUSE_IMAGE_LOADING_IN_OS2
446 wxPalette* pActualPalette = (wxPalette *)pPal;
447
448 if (!pActualPalette)
449 pActualPalette = pBitmap->GetPalette();
450 /* return(wxSaveBitmap( WXSTRINGCAST rName
451 ,pBitmap
452 ,pActualPalette
453 ) != 0); */
454 return(FALSE);
455 #else
456 return(FALSE);
457 #endif
458 }
459
460 // ----------------------------------------------------------------------------
461 // wxIcon handlers
462 // ----------------------------------------------------------------------------
463
464 bool wxICOFileHandler::LoadIcon(
465 wxIcon* pIcon
466 , const wxString& rName
467 , HPS hPs
468 , long lFlags
469 , int nDesiredWidth
470 , int nDesiredHeight
471 )
472 {
473 #if wxUSE_RESOURCE_LOADING_IN_OS2
474 pIcon->UnRef();
475
476 // actual size
477 wxSize vSize;
478
479 return(FALSE);
480 #else
481 return(FALSE);
482 #endif
483 }
484
485 bool wxICOResourceHandler::LoadIcon(
486 wxIcon* pIcon
487 , const wxString& rName
488 , HPS hPs
489 , long lFlags
490 , int WXUNUSED(nDesiredWidth)
491 , int WXUNUSED(nDesiredHeight)
492 )
493 {
494 HPOINTER hIcon;
495
496 hIcon = ::WinLoadFileIcon( (PSZ)rName.c_str()
497 ,TRUE // load for private use
498 );
499
500 pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32
501
502
503 pIcon->SetHICON((WXHICON)hIcon);
504
505 return pIcon->Ok();
506 } // end of wxICOResourceHandler::LoadIcon
507