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 licence 
  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/os2/gdiimage.h" 
  36 #include "wx/listimpl.cpp" 
  37 WX_DEFINE_LIST(wxGDIImageHandlerList
); 
  39 // ---------------------------------------------------------------------------- 
  41 // ---------------------------------------------------------------------------- 
  43 // all image handlers are declared/defined in this file because the outside 
  44 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids) 
  46 class WXDLLEXPORT wxBMPFileHandler 
: public wxBitmapHandler
 
  49     wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"), 
  54     virtual bool LoadFile( wxBitmap
*       pBitmap
 
  55                           ,const wxString
& rName
 
  61     virtual bool SaveFile( wxBitmap
*        pBitmap
 
  62                           ,const wxString
&  rName
 
  64                           ,const wxPalette
* pPalette 
= NULL
 
  68     inline virtual bool LoadFile( wxBitmap
*       pBitmap
 
  75         return wxBitmapHandler::LoadFile( pBitmap
 
  82     DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
) 
  85 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
 
  88     wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"), 
  90                                              wxBITMAP_TYPE_BMP_RESOURCE
) 
  94     virtual bool LoadFile( wxBitmap
*       pBitmap
 
 102     DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
) 
 105 class WXDLLEXPORT wxIconHandler 
: public wxGDIImageHandler
 
 108     wxIconHandler( const wxString
& rName
 
 109                   ,const wxString
& rExt
 
 111                  ) : wxGDIImageHandler( rName
 
 118     // creating and saving icons is not supported 
 119     virtual bool Create( wxGDIImage
* WXUNUSED(pImage
) 
 120                         ,void*       WXUNUSED(pData
) 
 121                         ,long        WXUNUSED(lFlags
) 
 122                         ,int         WXUNUSED(nWidth
) 
 123                         ,int         WXUNUSED(nHeight
) 
 124                         ,int         WXUNUSED(nDepth
) = 1 
 130     virtual bool Save( wxGDIImage
*     WXUNUSED(pImage
) 
 131                       ,const wxString
& WXUNUSED(rName
) 
 137     virtual bool Load( wxGDIImage
*     pImage
 
 138                       ,const wxString
& rName
 
 145         wxIcon
*                     pIcon 
= wxDynamicCast(pImage
, wxIcon
); 
 146         wxCHECK_MSG(pIcon
, FALSE
, _T("wxIconHandler only works with icons")); 
 148         return LoadIcon( pIcon
 
 158     virtual bool LoadIcon( wxIcon
*         pIcon
 
 159                           ,const wxString
& rName
 
 162                           ,int             nDesiredWidth 
= -1 
 163                           ,int             nDesiredHeight 
= -1 
 166     inline virtual bool Load( wxGDIImage
*     pImage
 
 177 class WXDLLEXPORT wxICOFileHandler 
: public wxIconHandler
 
 180     wxICOFileHandler() : wxIconHandler(_T("ICO icon file"), 
 186     virtual bool LoadIcon( wxIcon 
*        pIcon
 
 187                           ,const wxString
& rName
 
 190                           ,int             nDesiredWidth 
= -1 
 191                           ,int             nDesiredHeight 
= -1 
 195     DECLARE_DYNAMIC_CLASS(wxICOFileHandler
) 
 198 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
 
 201     wxICOResourceHandler() : wxIconHandler(_T("ICO resource"), 
 203                                            wxBITMAP_TYPE_ICO_RESOURCE
) 
 207     virtual bool LoadIcon( wxIcon
*         pIcon
 
 208                           ,const wxString
& rName
 
 211                           ,int             nDesiredWidth 
= -1 
 212                           ,int             nDesiredHeight 
= -1 
 216     DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
) 
 219 // ---------------------------------------------------------------------------- 
 221 // ---------------------------------------------------------------------------- 
 223 #if !USE_SHARED_LIBRARIES 
 224     IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
) 
 225     IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
) 
 226     IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
) 
 227     IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
) 
 230 // ============================================================================ 
 232 // ============================================================================ 
 234 wxGDIImageHandlerList 
wxGDIImage::ms_handlers
; 
 236 // ---------------------------------------------------------------------------- 
 237 // wxGDIImage functions forwarded to wxGDIImageRefData 
 238 // ---------------------------------------------------------------------------- 
 240 bool wxGDIImage::FreeResource( 
 241   bool                              WXUNUSED(bForce
) 
 246         GetGDIImageData()->Free(); 
 247         GetGDIImageData()->m_hHandle 
= 0; 
 253 WXHANDLE 
wxGDIImage::GetResourceHandle() 
 258 // ---------------------------------------------------------------------------- 
 259 // wxGDIImage handler stuff 
 260 // ---------------------------------------------------------------------------- 
 262 void wxGDIImage::AddHandler( 
 263   wxGDIImageHandler
*                pHandler
 
 266     ms_handlers
.Append(pHandler
); 
 269 void wxGDIImage::InsertHandler( 
 270   wxGDIImageHandler
*                pHandler
 
 273     ms_handlers
.Insert(pHandler
); 
 276 bool wxGDIImage::RemoveHandler( 
 277   const wxString
&                   rName
 
 280     wxGDIImageHandler
*              pHandler 
= FindHandler(rName
); 
 284         ms_handlers
.DeleteObject(pHandler
); 
 291 wxGDIImageHandler
* wxGDIImage::FindHandler( 
 292   const wxString
&                   rName
 
 295     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 299         wxGDIImageHandler
*          pHandler 
= pNode
->GetData(); 
 301         if ( pHandler
->GetName() == rName 
) 
 303         pNode 
= pNode
->GetNext(); 
 305     return((wxGDIImageHandler
*)NULL
); 
 308 wxGDIImageHandler
* wxGDIImage::FindHandler( 
 309   const wxString
&                   rExtension
 
 313     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 316         wxGDIImageHandler
*          pHandler 
= pNode
->GetData(); 
 318         if ( (pHandler
->GetExtension() = rExtension
) && 
 319              (lType 
== -1 || pHandler
->GetType() == lType
) ) 
 323         pNode 
= pNode
->GetNext(); 
 325     return((wxGDIImageHandler
*)NULL
); 
 328 wxGDIImageHandler
* wxGDIImage::FindHandler( 
 332     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 336         wxGDIImageHandler
*          pHandler 
= pNode
->GetData(); 
 338         if ( pHandler
->GetType() == lType 
) 
 340         pNode 
= pNode
->GetNext(); 
 342     return((wxGDIImageHandler
*)NULL
); 
 345 void wxGDIImage::CleanUpHandlers() 
 347     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 351         wxGDIImageHandler
*                              pHandler 
= pNode
->GetData(); 
 352         wxGDIImageHandlerList::compatibility_iterator   pNext 
= pNode
->GetNext(); 
 355         ms_handlers
.Erase( pNode 
); 
 360 void wxGDIImage::InitStandardHandlers() 
 362     AddHandler(new wxBMPResourceHandler
); 
 363     AddHandler(new wxBMPFileHandler
); 
 364     AddHandler(new wxICOResourceHandler
); 
 365     AddHandler(new wxICOFileHandler
); 
 368 // ---------------------------------------------------------------------------- 
 370 // ---------------------------------------------------------------------------- 
 372 bool wxBMPResourceHandler::LoadFile( 
 380     SIZEL                               vSize 
= {0, 0}; 
 381     DEVOPENSTRUC                        vDop  
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L}; 
 382     HDC                                 hDC   
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
); 
 383     HPS                                 hPS   
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS 
| GPIA_ASSOC
); 
 385     pBitmap
->SetHBITMAP((WXHBITMAP
)::GpiLoadBitmap( hPS
 
 394     wxBitmapRefData
*                pData 
= pBitmap
->GetBitmapData(); 
 398         BITMAPINFOHEADER            vBmph
; 
 400         ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap
), &vBmph
); 
 401         pData
->m_nWidth  
= vBmph
.cx
; 
 402         pData
->m_nHeight 
= vBmph
.cy
; 
 403         pData
->m_nDepth  
= vBmph
.cBitCount
; 
 405     return(pBitmap
->Ok()); 
 406 } // end of wxBMPResourceHandler::LoadFile 
 408 bool wxBMPFileHandler::LoadFile( 
 410 , const wxString
&                   rName
 
 412 , long                              WXUNUSED(lFlags
) 
 413 , int                               WXUNUSED(nDesiredWidth
) 
 414 , int                               WXUNUSED(nDesiredHeight
) 
 417 #if wxUSE_IMAGE_LOADING_IN_OS2 
 418     wxPalette
*                      pPalette 
= NULL
; 
 420     bool                            bSuccess 
= FALSE
; /* wxLoadIntoBitmap( WXSTRINGCAST rName 
 424     if (bSuccess 
&& pPalette
) 
 426         pBitmap
->SetPalette(*pPalette
); 
 429     // it was copied by the bitmap if it was loaded successfully 
 438 bool wxBMPFileHandler::SaveFile( 
 440 , const wxString
&                   rName
 
 441 , int                               WXUNUSED(nType
) 
 442 , const wxPalette
*                  pPal
 
 445 #if wxUSE_IMAGE_LOADING_IN_OS2 
 446     wxPalette
*                      pActualPalette 
= (wxPalette 
*)pPal
; 
 449         pActualPalette 
= pBitmap
->GetPalette(); 
 450     /* return(wxSaveBitmap( WXSTRINGCAST rName 
 460 // ---------------------------------------------------------------------------- 
 462 // ---------------------------------------------------------------------------- 
 464 bool wxICOFileHandler::LoadIcon( 
 466 , const wxString
&                   rName
 
 473 #if wxUSE_RESOURCE_LOADING_IN_OS2 
 485 bool wxICOResourceHandler::LoadIcon( 
 487 , const wxString
&                   rName
 
 490 , int                               WXUNUSED(nDesiredWidth
) 
 491 , int                               WXUNUSED(nDesiredHeight
) 
 496     hIcon 
= ::WinLoadFileIcon( (PSZ
)rName
.c_str() 
 497                               ,TRUE 
// load for private use 
 500     pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32 
 503     pIcon
->SetHICON((WXHICON
)hIcon
); 
 506 } // end of wxICOResourceHandler::LoadIcon