1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/gdiimage.cpp
3 // Purpose: wxGDIImage implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
24 #include "wx/string.h"
28 #include "wx/os2/private.h"
29 #include "wx/os2/gdiimage.h"
31 #include "wx/listimpl.cpp"
32 WX_DEFINE_LIST(wxGDIImageHandlerList
)
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // all image handlers are declared/defined in this file because the outside
39 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
41 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
44 wxBMPFileHandler() : wxBitmapHandler(wxT("Windows bitmap file"), wxT("bmp"),
49 virtual bool LoadFile( wxBitmap
* pBitmap
50 ,const wxString
& rName
56 virtual bool SaveFile( wxBitmap
* pBitmap
57 ,const wxString
& rName
59 ,const wxPalette
* pPalette
= NULL
63 inline virtual bool LoadFile( wxBitmap
* pBitmap
70 return wxBitmapHandler::LoadFile( pBitmap
77 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
80 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
83 wxBMPResourceHandler() : wxBitmapHandler(wxT("Windows bitmap resource"),
85 wxBITMAP_TYPE_BMP_RESOURCE
)
89 virtual bool LoadFile( wxBitmap
* pBitmap
97 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
100 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
103 wxIconHandler( const wxString
& rName
104 ,const wxString
& rExt
106 ) : wxGDIImageHandler( rName
113 // creating and saving icons is not supported
114 virtual bool Create( wxGDIImage
* WXUNUSED(pImage
)
115 ,const void* WXUNUSED(pData
)
116 ,wxBitmapType
WXUNUSED(lFlags
)
117 ,int WXUNUSED(nWidth
)
118 ,int WXUNUSED(nHeight
)
119 ,int WXUNUSED(nDepth
) = 1
125 virtual bool Save( const wxGDIImage
* WXUNUSED(pImage
)
126 ,const wxString
& WXUNUSED(rName
)
127 ,wxBitmapType
WXUNUSED(nType
)
132 virtual bool Load( wxGDIImage
* pImage
133 ,const wxString
& rName
140 wxIcon
* pIcon
= wxDynamicCast(pImage
, wxIcon
);
141 wxCHECK_MSG(pIcon
, false, wxT("wxIconHandler only works with icons"));
143 return LoadIcon( pIcon
153 virtual bool LoadIcon( wxIcon
* pIcon
154 ,const wxString
& rName
157 ,int nDesiredWidth
= -1
158 ,int nDesiredHeight
= -1
161 inline virtual bool Load( wxGDIImage
* WXUNUSED(pImage
),
163 wxBitmapType
WXUNUSED(lFlags
),
164 int WXUNUSED(nDesiredWidth
),
165 int WXUNUSED(nDesiredHeight
) )
171 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
174 wxICOFileHandler() : wxIconHandler(wxT("ICO icon file"),
180 virtual bool LoadIcon( wxIcon
* pIcon
181 ,const wxString
& rName
184 ,int nDesiredWidth
= -1
185 ,int nDesiredHeight
= -1
189 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
192 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
195 wxICOResourceHandler() : wxIconHandler(wxT("ICO resource"),
197 wxBITMAP_TYPE_ICO_RESOURCE
)
201 virtual bool LoadIcon( wxIcon
* pIcon
202 ,const wxString
& rName
205 ,int nDesiredWidth
= -1
206 ,int nDesiredHeight
= -1
210 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
218 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
219 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
220 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
222 // ============================================================================
224 // ============================================================================
226 wxGDIImageHandlerList
wxGDIImage::ms_handlers
;
228 // ----------------------------------------------------------------------------
229 // wxGDIImage functions forwarded to wxGDIImageRefData
230 // ----------------------------------------------------------------------------
232 bool wxGDIImage::FreeResource( bool WXUNUSED(bForce
) )
236 GetGDIImageData()->Free();
237 GetGDIImageData()->m_hHandle
= 0;
243 WXHANDLE
wxGDIImage::GetResourceHandle() const
248 // ----------------------------------------------------------------------------
249 // wxGDIImage handler stuff
250 // ----------------------------------------------------------------------------
252 void wxGDIImage::AddHandler( wxGDIImageHandler
* pHandler
)
254 ms_handlers
.Append(pHandler
);
257 void wxGDIImage::InsertHandler( wxGDIImageHandler
* pHandler
)
259 ms_handlers
.Insert(pHandler
);
262 bool wxGDIImage::RemoveHandler( const wxString
& rName
)
264 wxGDIImageHandler
* pHandler
= FindHandler(rName
);
268 ms_handlers
.DeleteObject(pHandler
);
275 wxGDIImageHandler
* wxGDIImage::FindHandler(
276 const wxString
& rName
279 wxGDIImageHandlerList::compatibility_iterator pNode
= ms_handlers
.GetFirst();
283 wxGDIImageHandler
* pHandler
= pNode
->GetData();
285 if ( pHandler
->GetName() == rName
)
287 pNode
= pNode
->GetNext();
292 wxGDIImageHandler
* wxGDIImage::FindHandler(
293 const wxString
& rExtension
297 wxGDIImageHandlerList::compatibility_iterator pNode
= ms_handlers
.GetFirst();
300 wxGDIImageHandler
* pHandler
= pNode
->GetData();
302 if ( (pHandler
->GetExtension() == rExtension
) &&
303 (lType
== -1 || pHandler
->GetType() == lType
) )
307 pNode
= pNode
->GetNext();
312 wxGDIImageHandler
* wxGDIImage::FindHandler(
316 wxGDIImageHandlerList::compatibility_iterator pNode
= ms_handlers
.GetFirst();
320 wxGDIImageHandler
* pHandler
= pNode
->GetData();
322 if ( pHandler
->GetType() == lType
)
324 pNode
= pNode
->GetNext();
329 void wxGDIImage::CleanUpHandlers()
331 wxGDIImageHandlerList::compatibility_iterator pNode
= ms_handlers
.GetFirst();
335 wxGDIImageHandler
* pHandler
= pNode
->GetData();
336 wxGDIImageHandlerList::compatibility_iterator pNext
= pNode
->GetNext();
339 ms_handlers
.Erase( pNode
);
344 void wxGDIImage::InitStandardHandlers()
346 AddHandler(new wxBMPResourceHandler
);
347 AddHandler(new wxBMPFileHandler
);
348 AddHandler(new wxICOResourceHandler
);
349 AddHandler(new wxICOFileHandler
);
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
356 bool wxBMPResourceHandler::LoadFile( wxBitmap
* pBitmap
,
358 wxBitmapType
WXUNUSED(lFlags
),
359 int WXUNUSED(nDesiredWidth
),
360 int WXUNUSED(nDesiredHeight
) )
362 SIZEL vSize
= {0, 0};
363 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
364 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
365 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
367 pBitmap
->SetHBITMAP((WXHBITMAP
)::GpiLoadBitmap( hPS
376 wxBitmapRefData
* pData
= pBitmap
->GetBitmapData();
378 if ( pBitmap
->IsOk() )
380 BITMAPINFOHEADER vBmph
;
382 ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap
), &vBmph
);
383 pData
->m_nWidth
= vBmph
.cx
;
384 pData
->m_nHeight
= vBmph
.cy
;
385 pData
->m_nDepth
= vBmph
.cBitCount
;
387 return(pBitmap
->IsOk());
388 } // end of wxBMPResourceHandler::LoadFile
390 bool wxBMPFileHandler::LoadFile( wxBitmap
* pBitmap
,
391 const wxString
& WXUNUSED(rName
),
393 wxBitmapType
WXUNUSED(lFlags
),
394 int WXUNUSED(nDesiredWidth
),
395 int WXUNUSED(nDesiredHeight
) )
397 #if defined(wxUSE_IMAGE_LOADING_IN_OS2) && wxUSE_IMAGE_LOADING_IN_OS2
398 wxPalette
* pPalette
= NULL
;
400 bool bSuccess
= false; /* wxLoadIntoBitmap( WXSTRINGCAST rName
404 if (bSuccess
&& pPalette
)
406 pBitmap
->SetPalette(*pPalette
);
409 // it was copied by the bitmap if it was loaded successfully
414 wxUnusedVar(pBitmap
);
419 bool wxBMPFileHandler::SaveFile( wxBitmap
* pBitmap
,
420 const wxString
& WXUNUSED(rName
),
421 wxBitmapType
WXUNUSED(nType
),
422 const wxPalette
* pPal
)
424 #if defined(wxUSE_IMAGE_LOADING_IN_OS2) && wxUSE_IMAGE_LOADING_IN_OS2
425 wxPalette
* pActualPalette
= (wxPalette
*)pPal
;
428 pActualPalette
= pBitmap
->GetPalette();
429 /* return(wxSaveBitmap( WXSTRINGCAST rName
435 wxUnusedVar(pBitmap
);
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 bool wxICOFileHandler::LoadIcon( wxIcon
* pIcon
,
446 const wxString
& WXUNUSED(rName
),
448 wxBitmapType
WXUNUSED(lFlags
),
449 int WXUNUSED(nDesiredWidth
),
450 int WXUNUSED(nDesiredHeight
) )
452 #if defined(wxUSE_RESOURCE_LOADING_IN_OS2) && wxUSE_RESOURCE_LOADING_IN_OS2
462 bool wxICOResourceHandler::LoadIcon( wxIcon
* pIcon
,
463 const wxString
& rName
,
465 wxBitmapType
WXUNUSED(lFlags
),
466 int WXUNUSED(nDesiredWidth
),
467 int WXUNUSED(nDesiredHeight
) )
471 hIcon
= ::WinLoadFileIcon( rName
.c_str()
472 ,TRUE
// load for private use
475 pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32
477 pIcon
->SetHICON((WXHICON
)hIcon
);
479 return pIcon
->IsOk();
480 } // end of wxICOResourceHandler::LoadIcon