1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/gdiimage.cpp
3 // Purpose: wxGDIImage implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "gdiimage.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
29 #include "wx/string.h"
32 #include "wx/os2/private.h"
34 #include "wx/os2/gdiimage.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // all image handlers are declared/defined in this file because the outside
41 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
43 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
46 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
51 virtual bool LoadFile( wxBitmap
* pBitmap
52 ,const wxString
& rName
58 virtual bool SaveFile( wxBitmap
* pBitmap
59 ,const wxString
& rName
61 ,const wxPalette
* pPalette
= NULL
65 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
68 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
71 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
73 wxBITMAP_TYPE_BMP_RESOURCE
)
77 virtual bool LoadFile( wxBitmap
* pBitmap
78 ,const wxString
& rName
86 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
89 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
92 wxIconHandler( const wxString
& rName
95 ) : wxGDIImageHandler( rName
102 // creating and saving icons is not supported
103 virtual bool Create( wxGDIImage
* WXUNUSED(pImage
)
104 ,void* WXUNUSED(pData
)
105 ,long WXUNUSED(lFlags
)
106 ,int WXUNUSED(nWidth
)
107 ,int WXUNUSED(nHeight
)
108 ,int WXUNUSED(nDepth
) = 1
114 virtual bool Save( wxGDIImage
* WXUNUSED(pImage
)
115 ,const wxString
& WXUNUSED(rName
)
121 virtual bool Load( wxGDIImage
* pImage
122 ,const wxString
& rName
129 wxIcon
* pIcon
= wxDynamicCast(pImage
, wxIcon
);
130 wxCHECK_MSG(pIcon
, FALSE
, _T("wxIconHandler only works with icons"));
132 return LoadIcon( pIcon
142 virtual bool LoadIcon( wxIcon
* pIcon
143 ,const wxString
& rName
146 ,int nDesiredWidth
= -1
147 ,int nDesiredHeight
= -1
151 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
154 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
160 virtual bool LoadIcon( wxIcon
* pIcon
161 ,const wxString
& rName
164 ,int nDesiredWidth
= -1
165 ,int nDesiredHeight
= -1
169 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
172 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
175 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
177 wxBITMAP_TYPE_ICO_RESOURCE
)
181 virtual bool LoadIcon( wxIcon
* pIcon
182 ,const wxString
& rName
185 ,int nDesiredWidth
= -1
186 ,int nDesiredHeight
= -1
190 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 #if !USE_SHARED_LIBRARIES
198 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
199 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
200 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
201 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 static wxSize
GetHiconSize(WXHICON hicon
);
210 // ============================================================================
212 // ============================================================================
214 wxList
wxGDIImage::ms_handlers
;
216 // ----------------------------------------------------------------------------
217 // wxGDIImage functions forwarded to wxGDIImageRefData
218 // ----------------------------------------------------------------------------
220 bool wxGDIImage::FreeResource(
221 bool WXUNUSED(bForce
)
226 GetGDIImageData()->Free();
227 GetGDIImageData()->m_hHandle
= 0;
233 WXHANDLE
wxGDIImage::GetResourceHandle()
238 // ----------------------------------------------------------------------------
239 // wxGDIImage handler stuff
240 // ----------------------------------------------------------------------------
242 void wxGDIImage::AddHandler(
243 wxGDIImageHandler
* pHandler
246 ms_handlers
.Append(pHandler
);
249 void wxGDIImage::InsertHandler(
250 wxGDIImageHandler
* pHandler
253 ms_handlers
.Insert(pHandler
);
256 bool wxGDIImage::RemoveHandler(
257 const wxString
& rName
260 wxGDIImageHandler
* pHandler
= FindHandler(rName
);
264 ms_handlers
.DeleteObject(pHandler
);
271 wxGDIImageHandler
* wxGDIImage::FindHandler(
272 const wxString
& rName
275 wxNode
* pNode
= ms_handlers
.First();
279 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
281 if (pHandler
->GetName() == rName
)
283 pNode
= pNode
->Next();
288 wxGDIImageHandler
* wxGDIImage::FindHandler(
289 const wxString
& rExtension
293 wxNode
* pNode
= ms_handlers
.First();
297 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
299 if ((pHandler
->GetExtension() = rExtension
) &&
300 (lType
== -1 || pHandler
->GetType() == lType
))
304 pNode
= pNode
->Next();
309 wxGDIImageHandler
* wxGDIImage::FindHandler(
313 wxNode
* pNode
= ms_handlers
.First();
317 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
319 if (pHandler
->GetType() == lType
)
321 pNode
= pNode
->Next();
326 void wxGDIImage::CleanUpHandlers()
328 wxNode
* pNode
= ms_handlers
.First();
332 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
333 wxNode
* pNext
= pNode
->Next();
336 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
343 void wxGDIImage::InitStandardHandlers()
345 AddHandler(new wxBMPResourceHandler
);
346 AddHandler(new wxBMPFileHandler
);
347 AddHandler(new wxICOResourceHandler
);
348 AddHandler(new wxICOFileHandler
);
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
355 bool wxBMPResourceHandler::LoadFile(
357 , const wxString
& rName
364 // TODO: load a bitmap from a file
366 rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName));
368 wxBitmapRefData* pData = bitmap->GetBitmapData();
374 if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) )
376 wxLogLastError("GetObject(HBITMAP)");
379 data->m_width = bm.bmWidth;
380 data->m_height = bm.bmHeight;
381 data->m_depth = bm.bmBitsPixel;
385 // it's probably not found
386 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
395 bool wxBMPFileHandler::LoadFile(
397 , const wxString
& rName
399 , long WXUNUSED(lFlags
)
400 , int WXUNUSED(nDesiredWidth
)
401 , int WXUNUSED(nDesiredHeight
)
404 #if wxUSE_IMAGE_LOADING_IN_OS2
405 wxPalette
* pPalette
= NULL
;
407 bool bSuccess
= FALSE
; /* wxLoadIntoBitmap( WXSTRINGCAST rName
411 if (bSuccess
&& pPalette
)
413 pBitmap
->SetPalette(*pPalette
);
416 // it was copied by the bitmap if it was loaded successfully
425 bool wxBMPFileHandler::SaveFile(
427 , const wxString
& rName
428 , int WXUNUSED(nType
)
429 , const wxPalette
* pPal
432 #if wxUSE_IMAGE_LOADING_IN_OS2
433 wxPalette
* pActualPalette
= (wxPalette
*)pPal
;
436 pActualPalette
= pBitmap
->GetPalette();
437 /* return(wxSaveBitmap( WXSTRINGCAST rName
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 bool wxICOFileHandler::LoadIcon(
453 , const wxString
& rName
460 #if wxUSE_RESOURCE_LOADING_IN_OS2
466 // TODO: load icon directly from a file
469 HICON hicon = ::ExtractIcon(wxGetInstance(), name, first);
472 wxLogSysError(_T("Failed to load icon from the file '%s'"),
478 size = GetHiconSize(hicon);
480 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
483 #endif // Win32/Win16
485 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
486 (desiredHeight != -1 && desiredHeight != size.y) )
488 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
489 "of the size mismatch: actual (%d, %d), "
490 "requested (%d, %d)"),
492 desiredWidth, desiredHeight);
494 ::DestroyIcon(hicon);
499 icon->SetHICON((WXHICON)hicon);
500 icon->SetSize(size.x, size.y);
510 bool wxICOResourceHandler::LoadIcon(
512 , const wxString
& rName
515 , int WXUNUSED(nDesiredWidth
)
516 , int WXUNUSED(nDesiredHeight
)
521 hIcon
= ::WinLoadFileIcon( (PSZ
)rName
.c_str()
522 ,TRUE
// load for private use
525 pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32
528 pIcon
->SetHICON((WXHICON
)hIcon
);
531 } // end of wxICOResourceHandler::LoadIcon
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 static wxSize
GetHiconSize(
541 wxSize
vSize(32, 32); // default
543 // all OS/2 icons are 32x32