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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/string.h"
28 #include "wx/os2/private.h"
32 #include "wx/os2/gdiimage.h"
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(_T("Windows bitmap file"), _T("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 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
66 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
69 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
71 wxBITMAP_TYPE_BMP_RESOURCE
)
75 virtual bool LoadFile( wxBitmap
* pBitmap
76 ,const wxString
& rName
84 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
87 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
90 wxIconHandler( const wxString
& rName
93 ) : wxGDIImageHandler( rName
100 // creating and saving icons is not supported
101 virtual bool Create( wxGDIImage
* WXUNUSED(pImage
)
102 ,void* WXUNUSED(pData
)
103 ,long WXUNUSED(lFlags
)
104 ,int WXUNUSED(nWidth
)
105 ,int WXUNUSED(nHeight
)
106 ,int WXUNUSED(nDepth
) = 1
112 virtual bool Save( wxGDIImage
* WXUNUSED(pImage
)
113 ,const wxString
& WXUNUSED(rName
)
119 virtual bool Load( wxGDIImage
* pImage
120 ,const wxString
& rName
127 wxIcon
* pIcon
= wxDynamicCast(pImage
, wxIcon
);
128 wxCHECK_MSG(pIcon
, FALSE
, _T("wxIconHandler only works with icons"));
130 return LoadIcon( pIcon
140 virtual bool LoadIcon( wxIcon
* pIcon
141 ,const wxString
& rName
144 ,int nDesiredWidth
= -1
145 ,int nDesiredHeight
= -1
149 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
152 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
158 virtual bool LoadIcon( wxIcon
* pIcon
159 ,const wxString
& rName
162 ,int nDesiredWidth
= -1
163 ,int nDesiredHeight
= -1
167 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
170 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
173 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
175 wxBITMAP_TYPE_ICO_RESOURCE
)
179 virtual bool LoadIcon( wxIcon
* pIcon
180 ,const wxString
& rName
183 ,int nDesiredWidth
= -1
184 ,int nDesiredHeight
= -1
188 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 #if !USE_SHARED_LIBRARIES
196 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
197 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
198 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
199 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 static wxSize
GetHiconSize(WXHICON hicon
);
208 // ============================================================================
210 // ============================================================================
212 wxList
wxGDIImage::ms_handlers
;
214 // ----------------------------------------------------------------------------
215 // wxGDIImage functions forwarded to wxGDIImageRefData
216 // ----------------------------------------------------------------------------
218 bool wxGDIImage::FreeResource(
219 bool WXUNUSED(bForce
)
224 GetGDIImageData()->Free();
225 GetGDIImageData()->m_hHandle
= 0;
231 WXHANDLE
wxGDIImage::GetResourceHandle()
236 // ----------------------------------------------------------------------------
237 // wxGDIImage handler stuff
238 // ----------------------------------------------------------------------------
240 void wxGDIImage::AddHandler(
241 wxGDIImageHandler
* pHandler
244 ms_handlers
.Append(pHandler
);
247 void wxGDIImage::InsertHandler(
248 wxGDIImageHandler
* pHandler
251 ms_handlers
.Insert(pHandler
);
254 bool wxGDIImage::RemoveHandler(
255 const wxString
& rName
258 wxGDIImageHandler
* pHandler
= FindHandler(rName
);
262 ms_handlers
.DeleteObject(pHandler
);
269 wxGDIImageHandler
* wxGDIImage::FindHandler(
270 const wxString
& rName
273 wxNode
* pNode
= ms_handlers
.First();
277 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
279 if (pHandler
->GetName() == rName
)
281 pNode
= pNode
->Next();
286 wxGDIImageHandler
* wxGDIImage::FindHandler(
287 const wxString
& rExtension
291 wxNode
* pNode
= ms_handlers
.First();
295 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
297 if ((pHandler
->GetExtension() = rExtension
) &&
298 (lType
== -1 || pHandler
->GetType() == lType
))
302 pNode
= pNode
->Next();
307 wxGDIImageHandler
* wxGDIImage::FindHandler(
311 wxNode
* pNode
= ms_handlers
.First();
315 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
317 if (pHandler
->GetType() == lType
)
319 pNode
= pNode
->Next();
324 void wxGDIImage::CleanUpHandlers()
326 wxNode
* pNode
= ms_handlers
.First();
330 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
331 wxNode
* pNext
= pNode
->Next();
339 void wxGDIImage::InitStandardHandlers()
341 AddHandler(new wxBMPResourceHandler
);
342 AddHandler(new wxBMPFileHandler
);
344 // Not added by default: include xpmhand.h in your app
345 // and call these in your wxApp::OnInit.
346 // AddHandler(new wxXPMFileHandler);
347 // AddHandler(new wxXPMDataHandler);
349 AddHandler(new wxICOResourceHandler
);
350 AddHandler(new wxICOFileHandler
);
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 bool wxBMPResourceHandler::LoadFile(
359 , const wxString
& rName
366 // TODO: load a bitmap from a file
368 rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName));
370 wxBitmapRefData* pData = bitmap->GetBitmapData();
376 if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) )
378 wxLogLastError("GetObject(HBITMAP)");
381 data->m_width = bm.bmWidth;
382 data->m_height = bm.bmHeight;
383 data->m_depth = bm.bmBitsPixel;
387 // it's probably not found
388 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
397 bool wxBMPFileHandler::LoadFile(
399 , const wxString
& rName
401 , long WXUNUSED(lFlags
)
402 , int WXUNUSED(nDesiredWidth
)
403 , int WXUNUSED(nDesiredHeight
)
406 #if wxUSE_IMAGE_LOADING_IN_OS2
407 wxPalette
* pPalette
= NULL
;
409 bool bSuccess
= FALSE
; /* wxLoadIntoBitmap( WXSTRINGCAST rName
413 if (bSuccess
&& pPalette
)
415 pBitmap
->SetPalette(*pPalette
);
418 // it was copied by the bitmap if it was loaded successfully
427 bool wxBMPFileHandler::SaveFile(
429 , const wxString
& rName
430 , int WXUNUSED(nType
)
431 , const wxPalette
* pPal
434 #if wxUSE_IMAGE_LOADING_IN_OS2
435 wxPalette
* pActualPalette
= (wxPalette
*)pPal
;
438 pActualPalette
= pBitmap
->GetPalette();
439 /* return(wxSaveBitmap( WXSTRINGCAST rName
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
453 bool wxICOFileHandler::LoadIcon(
455 , const wxString
& rName
462 #if wxUSE_RESOURCE_LOADING_IN_OS2
468 // TODO: load icon directly from a file
471 HICON hicon = ::ExtractIcon(wxGetInstance(), name, first);
474 wxLogSysError(_T("Failed to load icon from the file '%s'"),
480 size = GetHiconSize(hicon);
482 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
485 #endif // Win32/Win16
487 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
488 (desiredHeight != -1 && desiredHeight != size.y) )
490 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
491 "of the size mismatch: actual (%d, %d), "
492 "requested (%d, %d)"),
494 desiredWidth, desiredHeight);
496 ::DestroyIcon(hicon);
501 icon->SetHICON((WXHICON)hicon);
502 icon->SetSize(size.x, size.y);
512 bool wxICOResourceHandler::LoadIcon(
514 , const wxString
& rName
521 // TODO: load icon from a file
525 #if defined(__WIN32__) && !defined(__SC__)
526 if ( desiredWidth != -1 && desiredHeight != -1 )
528 hicon = (HICON)::LoadImage(wxGetInstance(), name, IMAGE_ICON,
529 desiredWidth, desiredHeight,
535 hicon = ::LoadIcon(wxGetInstance(), name);
538 wxSize size = GetHiconSize(hicon);
539 icon->SetSize(size.x, size.y);
541 // Override the found values with desired values
542 if ( desiredWidth > -1 && desiredHeight > -1 )
544 icon->SetSize(desiredWidth, desiredHeight);
547 icon->SetHICON((WXHICON)hicon);
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 static wxSize
GetHiconSize(
562 wxSize
vSize(32, 32); // default
564 // all OS/2 icons are 32x32