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/xpmhand.h"
35 #include "wx/os2/gdiimage.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // all image handlers are declared/defined in this file because the outside
42 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
44 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
47 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
52 virtual bool LoadFile( wxBitmap
* pBitmap
53 ,const wxString
& rName
59 virtual bool SaveFile( wxBitmap
* pBitmap
60 ,const wxString
& rName
62 ,const wxPalette
* pPalette
= NULL
66 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
69 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
72 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
74 wxBITMAP_TYPE_BMP_RESOURCE
)
78 virtual bool LoadFile( wxBitmap
* pBitmap
79 ,const wxString
& rName
87 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
90 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
93 wxIconHandler( const wxString
& rName
96 ) : wxGDIImageHandler( rName
103 // creating and saving icons is not supported
104 virtual bool Create( wxGDIImage
* WXUNUSED(pImage
)
105 ,void* WXUNUSED(pData
)
106 ,long WXUNUSED(lFlags
)
107 ,int WXUNUSED(nWidth
)
108 ,int WXUNUSED(nHeight
)
109 ,int WXUNUSED(nDepth
) = 1
115 virtual bool Save( wxGDIImage
* WXUNUSED(pImage
)
116 ,const wxString
& WXUNUSED(rName
)
122 virtual bool Load( wxGDIImage
* pImage
123 ,const wxString
& rName
130 wxIcon
* pIcon
= wxDynamicCast(pImage
, wxIcon
);
131 wxCHECK_MSG(pIcon
, FALSE
, _T("wxIconHandler only works with icons"));
133 return LoadIcon( pIcon
143 virtual bool LoadIcon( wxIcon
* pIcon
144 ,const wxString
& rName
147 ,int nDesiredWidth
= -1
148 ,int nDesiredHeight
= -1
152 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
155 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
161 virtual bool LoadIcon( wxIcon
* pIcon
162 ,const wxString
& rName
165 ,int nDesiredWidth
= -1
166 ,int nDesiredHeight
= -1
170 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
173 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
176 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
178 wxBITMAP_TYPE_ICO_RESOURCE
)
182 virtual bool LoadIcon( wxIcon
* pIcon
183 ,const wxString
& rName
186 ,int nDesiredWidth
= -1
187 ,int nDesiredHeight
= -1
191 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 #if !USE_SHARED_LIBRARIES
199 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
200 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
201 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
202 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 static wxSize
GetHiconSize(WXHICON hicon
);
211 // ============================================================================
213 // ============================================================================
215 wxList
wxGDIImage::ms_handlers
;
217 // ----------------------------------------------------------------------------
218 // wxGDIImage functions forwarded to wxGDIImageRefData
219 // ----------------------------------------------------------------------------
221 bool wxGDIImage::FreeResource(
222 bool WXUNUSED(bForce
)
227 GetGDIImageData()->Free();
228 GetGDIImageData()->m_hHandle
= 0;
234 WXHANDLE
wxGDIImage::GetResourceHandle()
239 // ----------------------------------------------------------------------------
240 // wxGDIImage handler stuff
241 // ----------------------------------------------------------------------------
243 void wxGDIImage::AddHandler(
244 wxGDIImageHandler
* pHandler
247 ms_handlers
.Append(pHandler
);
250 void wxGDIImage::InsertHandler(
251 wxGDIImageHandler
* pHandler
254 ms_handlers
.Insert(pHandler
);
257 bool wxGDIImage::RemoveHandler(
258 const wxString
& rName
261 wxGDIImageHandler
* pHandler
= FindHandler(rName
);
265 ms_handlers
.DeleteObject(pHandler
);
272 wxGDIImageHandler
* wxGDIImage::FindHandler(
273 const wxString
& rName
276 wxNode
* pNode
= ms_handlers
.First();
280 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
282 if (pHandler
->GetName() == rName
)
284 pNode
= pNode
->Next();
289 wxGDIImageHandler
* wxGDIImage::FindHandler(
290 const wxString
& rExtension
294 wxNode
* pNode
= ms_handlers
.First();
298 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
300 if ((pHandler
->GetExtension() = rExtension
) &&
301 (lType
== -1 || pHandler
->GetType() == lType
))
305 pNode
= pNode
->Next();
310 wxGDIImageHandler
* wxGDIImage::FindHandler(
314 wxNode
* pNode
= ms_handlers
.First();
318 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
320 if (pHandler
->GetType() == lType
)
322 pNode
= pNode
->Next();
327 void wxGDIImage::CleanUpHandlers()
329 wxNode
* pNode
= ms_handlers
.First();
333 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
334 wxNode
* pNext
= pNode
->Next();
337 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
344 void wxGDIImage::InitStandardHandlers()
346 AddHandler(new wxBMPResourceHandler
);
347 AddHandler(new wxBMPFileHandler
);
350 AddHandler(new wxXPMFileHandler
);
351 AddHandler(new wxXPMDataHandler
);
354 AddHandler(new wxICOResourceHandler
);
355 AddHandler(new wxICOFileHandler
);
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 bool wxBMPResourceHandler::LoadFile(
364 , const wxString
& rName
371 // TODO: load a bitmap from a file
373 rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName));
375 wxBitmapRefData* pData = bitmap->GetBitmapData();
381 if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) )
383 wxLogLastError("GetObject(HBITMAP)");
386 data->m_width = bm.bmWidth;
387 data->m_height = bm.bmHeight;
388 data->m_depth = bm.bmBitsPixel;
392 // it's probably not found
393 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
402 bool wxBMPFileHandler::LoadFile(
404 , const wxString
& rName
406 , long WXUNUSED(lFlags
)
407 , int WXUNUSED(nDesiredWidth
)
408 , int WXUNUSED(nDesiredHeight
)
411 #if wxUSE_IMAGE_LOADING_IN_OS2
412 wxPalette
* pPalette
= NULL
;
414 bool bSuccess
= FALSE
; /* wxLoadIntoBitmap( WXSTRINGCAST rName
418 if (bSuccess
&& pPalette
)
420 pBitmap
->SetPalette(*pPalette
);
423 // it was copied by the bitmap if it was loaded successfully
432 bool wxBMPFileHandler::SaveFile(
434 , const wxString
& rName
435 , int WXUNUSED(nType
)
436 , const wxPalette
* pPal
439 #if wxUSE_IMAGE_LOADING_IN_OS2
440 wxPalette
* pActualPalette
= (wxPalette
*)pPal
;
443 pActualPalette
= pBitmap
->GetPalette();
444 /* return(wxSaveBitmap( WXSTRINGCAST rName
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 bool wxICOFileHandler::LoadIcon(
460 , const wxString
& rName
467 #if wxUSE_RESOURCE_LOADING_IN_OS2
473 // TODO: load icon directly from a file
476 HICON hicon = ::ExtractIcon(wxGetInstance(), name, first);
479 wxLogSysError(_T("Failed to load icon from the file '%s'"),
485 size = GetHiconSize(hicon);
487 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
490 #endif // Win32/Win16
492 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
493 (desiredHeight != -1 && desiredHeight != size.y) )
495 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
496 "of the size mismatch: actual (%d, %d), "
497 "requested (%d, %d)"),
499 desiredWidth, desiredHeight);
501 ::DestroyIcon(hicon);
506 icon->SetHICON((WXHICON)hicon);
507 icon->SetSize(size.x, size.y);
517 bool wxICOResourceHandler::LoadIcon(
519 , const wxString
& rName
522 , int WXUNUSED(nDesiredWidth
)
523 , int WXUNUSED(nDesiredHeight
)
528 hIcon
= ::WinLoadFileIcon( (PSZ
)rName
.c_str()
529 ,TRUE
// load for private use
532 pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32
535 pIcon
->SetHICON((WXHICON
)hIcon
);
538 } // end of wxICOResourceHandler::LoadIcon
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 static wxSize
GetHiconSize(
548 wxSize
vSize(32, 32); // default
550 // all OS/2 icons are 32x32