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();
338 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
345 void wxGDIImage::InitStandardHandlers()
347 AddHandler(new wxBMPResourceHandler
);
348 AddHandler(new wxBMPFileHandler
);
350 // Not added by default: include xpmhand.h in your app
351 // and call these in your wxApp::OnInit.
352 // AddHandler(new wxXPMFileHandler);
353 // AddHandler(new wxXPMDataHandler);
355 AddHandler(new wxICOResourceHandler
);
356 AddHandler(new wxICOFileHandler
);
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 bool wxBMPResourceHandler::LoadFile(
365 , const wxString
& rName
372 // TODO: load a bitmap from a file
374 rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName));
376 wxBitmapRefData* pData = bitmap->GetBitmapData();
382 if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) )
384 wxLogLastError("GetObject(HBITMAP)");
387 data->m_width = bm.bmWidth;
388 data->m_height = bm.bmHeight;
389 data->m_depth = bm.bmBitsPixel;
393 // it's probably not found
394 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
403 bool wxBMPFileHandler::LoadFile(
405 , const wxString
& rName
407 , long WXUNUSED(lFlags
)
408 , int WXUNUSED(nDesiredWidth
)
409 , int WXUNUSED(nDesiredHeight
)
412 #if wxUSE_IMAGE_LOADING_IN_OS2
413 wxPalette
* pPalette
= NULL
;
415 bool bSuccess
= FALSE
; /* wxLoadIntoBitmap( WXSTRINGCAST rName
419 if (bSuccess
&& pPalette
)
421 pBitmap
->SetPalette(*pPalette
);
424 // it was copied by the bitmap if it was loaded successfully
433 bool wxBMPFileHandler::SaveFile(
435 , const wxString
& rName
436 , int WXUNUSED(nType
)
437 , const wxPalette
* pPal
440 #if wxUSE_IMAGE_LOADING_IN_OS2
441 wxPalette
* pActualPalette
= (wxPalette
*)pPal
;
444 pActualPalette
= pBitmap
->GetPalette();
445 /* return(wxSaveBitmap( WXSTRINGCAST rName
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 bool wxICOFileHandler::LoadIcon(
461 , const wxString
& rName
468 #if wxUSE_RESOURCE_LOADING_IN_OS2
474 // TODO: load icon directly from a file
477 HICON hicon = ::ExtractIcon(wxGetInstance(), name, first);
480 wxLogSysError(_T("Failed to load icon from the file '%s'"),
486 size = GetHiconSize(hicon);
488 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
491 #endif // Win32/Win16
493 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
494 (desiredHeight != -1 && desiredHeight != size.y) )
496 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
497 "of the size mismatch: actual (%d, %d), "
498 "requested (%d, %d)"),
500 desiredWidth, desiredHeight);
502 ::DestroyIcon(hicon);
507 icon->SetHICON((WXHICON)hicon);
508 icon->SetSize(size.x, size.y);
518 bool wxICOResourceHandler::LoadIcon(
520 , const wxString
& rName
523 , int WXUNUSED(nDesiredWidth
)
524 , int WXUNUSED(nDesiredHeight
)
529 hIcon
= ::WinLoadFileIcon( (PSZ
)rName
.c_str()
530 ,TRUE
// load for private use
533 pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32
536 pIcon
->SetHICON((WXHICON
)hIcon
);
539 } // end of wxICOResourceHandler::LoadIcon
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
545 static wxSize
GetHiconSize(
549 wxSize
vSize(32, 32); // default
551 // all OS/2 icons are 32x32