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"
36 #include "wx/os2/gdiimage.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // all image handlers are declared/defined in this file because the outside
43 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
45 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
48 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
53 virtual bool LoadFile( wxBitmap
* pBitmap
54 ,const wxString
& rName
60 virtual bool SaveFile( wxBitmap
* pBitmap
61 ,const wxString
& rName
63 ,const wxPalette
* pPalette
= NULL
67 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
70 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
73 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
75 wxBITMAP_TYPE_BMP_RESOURCE
)
79 virtual bool LoadFile( wxBitmap
* pBitmap
80 ,const wxString
& rName
88 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
91 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
94 wxIconHandler( const wxString
& rName
97 ) : wxGDIImageHandler( rName
104 // creating and saving icons is not supported
105 virtual bool Create( wxGDIImage
* WXUNUSED(pImage
)
106 ,void* WXUNUSED(pData
)
107 ,long WXUNUSED(lFlags
)
108 ,int WXUNUSED(nWidth
)
109 ,int WXUNUSED(nHeight
)
110 ,int WXUNUSED(nDepth
) = 1
116 virtual bool Save( wxGDIImage
* WXUNUSED(pImage
)
117 ,const wxString
& WXUNUSED(rName
)
123 virtual bool Load( wxGDIImage
* pImage
124 ,const wxString
& rName
131 wxIcon
* pIcon
= wxDynamicCast(pImage
, wxIcon
);
132 wxCHECK_MSG(pIcon
, FALSE
, _T("wxIconHandler only works with icons"));
134 return LoadIcon( pIcon
144 virtual bool LoadIcon( wxIcon
* pIcon
145 ,const wxString
& rName
148 ,int nDesiredWidth
= -1
149 ,int nDesiredHeight
= -1
153 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
156 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
162 virtual bool LoadIcon( wxIcon
* pIcon
163 ,const wxString
& rName
166 ,int nDesiredWidth
= -1
167 ,int nDesiredHeight
= -1
171 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
174 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
177 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
179 wxBITMAP_TYPE_ICO_RESOURCE
)
183 virtual bool LoadIcon( wxIcon
* pIcon
184 ,const wxString
& rName
187 ,int nDesiredWidth
= -1
188 ,int nDesiredHeight
= -1
192 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 #if !USE_SHARED_LIBRARIES
200 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
201 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
202 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
203 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 static wxSize
GetHiconSize(WXHICON hicon
);
212 // ============================================================================
214 // ============================================================================
216 wxList
wxGDIImage::ms_handlers
;
218 // ----------------------------------------------------------------------------
219 // wxGDIImage functions forwarded to wxGDIImageRefData
220 // ----------------------------------------------------------------------------
222 bool wxGDIImage::FreeResource(
223 bool WXUNUSED(bForce
)
228 GetGDIImageData()->Free();
229 GetGDIImageData()->m_hHandle
= 0;
235 WXHANDLE
wxGDIImage::GetResourceHandle()
240 // ----------------------------------------------------------------------------
241 // wxGDIImage handler stuff
242 // ----------------------------------------------------------------------------
244 void wxGDIImage::AddHandler(
245 wxGDIImageHandler
* pHandler
248 ms_handlers
.Append(pHandler
);
251 void wxGDIImage::InsertHandler(
252 wxGDIImageHandler
* pHandler
255 ms_handlers
.Insert(pHandler
);
258 bool wxGDIImage::RemoveHandler(
259 const wxString
& rName
262 wxGDIImageHandler
* pHandler
= FindHandler(rName
);
266 ms_handlers
.DeleteObject(pHandler
);
273 wxGDIImageHandler
* wxGDIImage::FindHandler(
274 const wxString
& rName
277 wxNode
* pNode
= ms_handlers
.First();
281 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
283 if (pHandler
->GetName() == rName
)
285 pNode
= pNode
->Next();
290 wxGDIImageHandler
* wxGDIImage::FindHandler(
291 const wxString
& rExtension
295 wxNode
* pNode
= ms_handlers
.First();
299 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
301 if ((pHandler
->GetExtension() = rExtension
) &&
302 (lType
== -1 || pHandler
->GetType() == lType
))
306 pNode
= pNode
->Next();
311 wxGDIImageHandler
* wxGDIImage::FindHandler(
315 wxNode
* pNode
= ms_handlers
.First();
319 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
321 if (pHandler
->GetType() == lType
)
323 pNode
= pNode
->Next();
328 void wxGDIImage::CleanUpHandlers()
330 wxNode
* pNode
= ms_handlers
.First();
334 wxGDIImageHandler
* pHandler
= (wxGDIImageHandler
*)pNode
->Data();
335 wxNode
* pNext
= pNode
->Next();
343 void wxGDIImage::InitStandardHandlers()
345 AddHandler(new wxBMPResourceHandler
);
346 AddHandler(new wxBMPFileHandler
);
348 // Not added by default: include xpmhand.h in your app
349 // and call these in your wxApp::OnInit.
350 // AddHandler(new wxXPMFileHandler);
351 // AddHandler(new wxXPMDataHandler);
353 AddHandler(new wxICOResourceHandler
);
354 AddHandler(new wxICOFileHandler
);
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
361 bool wxBMPResourceHandler::LoadFile(
363 , const wxString
& rName
370 // TODO: load a bitmap from a file
372 rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName));
374 wxBitmapRefData* pData = bitmap->GetBitmapData();
380 if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) )
382 wxLogLastError("GetObject(HBITMAP)");
385 data->m_width = bm.bmWidth;
386 data->m_height = bm.bmHeight;
387 data->m_depth = bm.bmBitsPixel;
391 // it's probably not found
392 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
401 bool wxBMPFileHandler::LoadFile(
403 , const wxString
& rName
405 , long WXUNUSED(lFlags
)
406 , int WXUNUSED(nDesiredWidth
)
407 , int WXUNUSED(nDesiredHeight
)
410 #if wxUSE_IMAGE_LOADING_IN_OS2
411 wxPalette
* pPalette
= NULL
;
413 bool bSuccess
= FALSE
; /* wxLoadIntoBitmap( WXSTRINGCAST rName
417 if (bSuccess
&& pPalette
)
419 pBitmap
->SetPalette(*pPalette
);
422 // it was copied by the bitmap if it was loaded successfully
431 bool wxBMPFileHandler::SaveFile(
433 , const wxString
& rName
434 , int WXUNUSED(nType
)
435 , const wxPalette
* pPal
438 #if wxUSE_IMAGE_LOADING_IN_OS2
439 wxPalette
* pActualPalette
= (wxPalette
*)pPal
;
442 pActualPalette
= pBitmap
->GetPalette();
443 /* return(wxSaveBitmap( WXSTRINGCAST rName
453 // ----------------------------------------------------------------------------
455 // ----------------------------------------------------------------------------
457 bool wxICOFileHandler::LoadIcon(
459 , const wxString
& rName
466 #if wxUSE_RESOURCE_LOADING_IN_OS2
472 // TODO: load icon directly from a file
475 HICON hicon = ::ExtractIcon(wxGetInstance(), name, first);
478 wxLogSysError(_T("Failed to load icon from the file '%s'"),
484 size = GetHiconSize(hicon);
486 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
489 #endif // Win32/Win16
491 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
492 (desiredHeight != -1 && desiredHeight != size.y) )
494 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
495 "of the size mismatch: actual (%d, %d), "
496 "requested (%d, %d)"),
498 desiredWidth, desiredHeight);
500 ::DestroyIcon(hicon);
505 icon->SetHICON((WXHICON)hicon);
506 icon->SetSize(size.x, size.y);
516 bool wxICOResourceHandler::LoadIcon(
518 , const wxString
& rName
521 , int WXUNUSED(nDesiredWidth
)
522 , int WXUNUSED(nDesiredHeight
)
527 hIcon
= ::WinLoadFileIcon( (PSZ
)rName
.c_str()
528 ,TRUE
// load for private use
531 pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32
534 pIcon
->SetHICON((WXHICON
)hIcon
);
537 } // end of wxICOResourceHandler::LoadIcon
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 static wxSize
GetHiconSize(
547 wxSize
vSize(32, 32); // default
549 // all OS/2 icons are 32x32