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
, wxGDIImageHandler
) 
 202     IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
) 
 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
); 
 349     AddHandler(new wxXPMFileHandler
); 
 350     AddHandler(new wxXPMDataHandler
); 
 352     AddHandler(new wxICOResourceHandler
); 
 353     AddHandler(new wxICOFileHandler
); 
 356 // ---------------------------------------------------------------------------- 
 358 // ---------------------------------------------------------------------------- 
 360 bool wxBMPResourceHandler::LoadFile( 
 362 , const wxString
&                   rName
 
 369     // TODO: load a bitmap from a file 
 371     rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName)); 
 373     wxBitmapRefData*                pData = bitmap->GetBitmapData(); 
 379         if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) ) 
 381             wxLogLastError("GetObject(HBITMAP)"); 
 384         data->m_width = bm.bmWidth; 
 385         data->m_height = bm.bmHeight; 
 386         data->m_depth = bm.bmBitsPixel; 
 390         // it's probably not found 
 391         wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."), 
 400 bool wxBMPFileHandler::LoadFile( 
 402 , const wxString
&                   rName
 
 404 , long                              WXUNUSED(lFlags
) 
 405 , int                               WXUNUSED(nDesiredWidth
) 
 406 , int                               WXUNUSED(nDesiredHeight
) 
 409 #if wxUSE_IMAGE_LOADING_IN_OS2 
 410     wxPalette
*                      pPalette 
= NULL
; 
 412     bool                            bSuccess 
= FALSE
; /* wxLoadIntoBitmap( WXSTRINGCAST rName 
 416     if (bSuccess 
&& pPalette
) 
 418         pBitmap
->SetPalette(*pPalette
); 
 421     // it was copied by the bitmap if it was loaded successfully 
 430 bool wxBMPFileHandler::SaveFile( 
 432 , const wxString
&                   rName
 
 433 , int                               WXUNUSED(nType
) 
 434 , const wxPalette
*                  pPal
 
 437 #if wxUSE_IMAGE_LOADING_IN_OS2 
 438     wxPalette
*                      pActualPalette 
= (wxPalette 
*)pPal
; 
 441         pActualPalette 
= pBitmap
->GetPalette(); 
 442     /* return(wxSaveBitmap( WXSTRINGCAST rName 
 452 // ---------------------------------------------------------------------------- 
 454 // ---------------------------------------------------------------------------- 
 456 bool wxICOFileHandler::LoadIcon( 
 458 , const wxString
&                   rName
 
 465 #if wxUSE_RESOURCE_LOADING_IN_OS2 
 471     // TODO: load icon directly from a file 
 474     HICON hicon = ::ExtractIcon(wxGetInstance(), name, first); 
 477         wxLogSysError(_T("Failed to load icon from the file '%s'"), 
 483     size = GetHiconSize(hicon); 
 485     HICON hicon = ReadIconFile((wxChar *)name.c_str(), 
 488 #endif // Win32/Win16 
 490     if ( (desiredWidth != -1 && desiredWidth != size.x) || 
 491          (desiredHeight != -1 && desiredHeight != size.y) ) 
 493         wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because " 
 494                       "of the size mismatch: actual (%d, %d), " 
 495                       "requested (%d, %d)"), 
 497                       desiredWidth, desiredHeight); 
 499         ::DestroyIcon(hicon); 
 504     icon->SetHICON((WXHICON)hicon); 
 505     icon->SetSize(size.x, size.y); 
 515 bool wxICOResourceHandler::LoadIcon( 
 517 , const wxString
&                   rName
 
 520 , int                               WXUNUSED(nDesiredWidth
) 
 521 , int                               WXUNUSED(nDesiredHeight
) 
 526     hIcon 
= ::WinLoadFileIcon( (PSZ
)rName
.c_str() 
 527                               ,TRUE 
// load for private use 
 530     pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32 
 533     pIcon
->SetHICON((WXHICON
)hIcon
); 
 536 } // end of wxICOResourceHandler::LoadIcon 
 538 // ---------------------------------------------------------------------------- 
 540 // ---------------------------------------------------------------------------- 
 542 static wxSize 
GetHiconSize( 
 546     wxSize                          
vSize(32, 32);    // default 
 548     // all OS/2 icons are 32x32