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
* WXUNUSED(pImage
), 
 168                               long        WXUNUSED(lFlags
), 
 169                               int         WXUNUSED(nDesiredWidth
), 
 170                               int         WXUNUSED(nDesiredHeight
) ) 
 176 class WXDLLEXPORT wxICOFileHandler 
: public wxIconHandler
 
 179     wxICOFileHandler() : wxIconHandler(_T("ICO icon file"), 
 185     virtual bool LoadIcon( wxIcon 
*        pIcon
 
 186                           ,const wxString
& rName
 
 189                           ,int             nDesiredWidth 
= -1 
 190                           ,int             nDesiredHeight 
= -1 
 194     DECLARE_DYNAMIC_CLASS(wxICOFileHandler
) 
 197 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
 
 200     wxICOResourceHandler() : wxIconHandler(_T("ICO resource"), 
 202                                            wxBITMAP_TYPE_ICO_RESOURCE
) 
 206     virtual bool LoadIcon( wxIcon
*         pIcon
 
 207                           ,const wxString
& rName
 
 210                           ,int             nDesiredWidth 
= -1 
 211                           ,int             nDesiredHeight 
= -1 
 215     DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
) 
 218 // ---------------------------------------------------------------------------- 
 220 // ---------------------------------------------------------------------------- 
 222 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
) 
 223 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
) 
 224 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
) 
 225 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
) 
 227 // ============================================================================ 
 229 // ============================================================================ 
 231 wxGDIImageHandlerList 
wxGDIImage::ms_handlers
; 
 233 // ---------------------------------------------------------------------------- 
 234 // wxGDIImage functions forwarded to wxGDIImageRefData 
 235 // ---------------------------------------------------------------------------- 
 237 bool wxGDIImage::FreeResource( bool WXUNUSED(bForce
) ) 
 241         GetGDIImageData()->Free(); 
 242         GetGDIImageData()->m_hHandle 
= 0; 
 248 WXHANDLE 
wxGDIImage::GetResourceHandle() 
 253 // ---------------------------------------------------------------------------- 
 254 // wxGDIImage handler stuff 
 255 // ---------------------------------------------------------------------------- 
 257 void wxGDIImage::AddHandler( wxGDIImageHandler
* pHandler 
) 
 259     ms_handlers
.Append(pHandler
); 
 262 void wxGDIImage::InsertHandler( wxGDIImageHandler
* pHandler 
) 
 264     ms_handlers
.Insert(pHandler
); 
 267 bool wxGDIImage::RemoveHandler( const wxString
& rName 
) 
 269     wxGDIImageHandler
*              pHandler 
= FindHandler(rName
); 
 273         ms_handlers
.DeleteObject(pHandler
); 
 280 wxGDIImageHandler
* wxGDIImage::FindHandler( 
 281   const wxString
&                   rName
 
 284     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 288         wxGDIImageHandler
*          pHandler 
= pNode
->GetData(); 
 290         if ( pHandler
->GetName() == rName 
) 
 292         pNode 
= pNode
->GetNext(); 
 294     return((wxGDIImageHandler
*)NULL
); 
 297 wxGDIImageHandler
* wxGDIImage::FindHandler( 
 298   const wxString
&                   rExtension
 
 302     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 305         wxGDIImageHandler
*          pHandler 
= pNode
->GetData(); 
 307         if ( (pHandler
->GetExtension() = rExtension
) && 
 308              (lType 
== -1 || pHandler
->GetType() == lType
) ) 
 312         pNode 
= pNode
->GetNext(); 
 314     return((wxGDIImageHandler
*)NULL
); 
 317 wxGDIImageHandler
* wxGDIImage::FindHandler( 
 321     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 325         wxGDIImageHandler
*          pHandler 
= pNode
->GetData(); 
 327         if ( pHandler
->GetType() == lType 
) 
 329         pNode 
= pNode
->GetNext(); 
 331     return((wxGDIImageHandler
*)NULL
); 
 334 void wxGDIImage::CleanUpHandlers() 
 336     wxGDIImageHandlerList::compatibility_iterator   pNode 
= ms_handlers
.GetFirst(); 
 340         wxGDIImageHandler
*                              pHandler 
= pNode
->GetData(); 
 341         wxGDIImageHandlerList::compatibility_iterator   pNext 
= pNode
->GetNext(); 
 344         ms_handlers
.Erase( pNode 
); 
 349 void wxGDIImage::InitStandardHandlers() 
 351     AddHandler(new wxBMPResourceHandler
); 
 352     AddHandler(new wxBMPFileHandler
); 
 353     AddHandler(new wxICOResourceHandler
); 
 354     AddHandler(new wxICOFileHandler
); 
 357 // ---------------------------------------------------------------------------- 
 359 // ---------------------------------------------------------------------------- 
 361 bool wxBMPResourceHandler::LoadFile( wxBitmap
* pBitmap
, 
 363                                      long      WXUNUSED(lFlags
), 
 364                                      int       WXUNUSED(nDesiredWidth
), 
 365                                      int       WXUNUSED(nDesiredHeight
) ) 
 367     SIZEL                               vSize 
= {0, 0}; 
 368     DEVOPENSTRUC                        vDop  
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L}; 
 369     HDC                                 hDC   
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
); 
 370     HPS                                 hPS   
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS 
| GPIA_ASSOC
); 
 372     pBitmap
->SetHBITMAP((WXHBITMAP
)::GpiLoadBitmap( hPS
 
 381     wxBitmapRefData
*                pData 
= pBitmap
->GetBitmapData(); 
 385         BITMAPINFOHEADER            vBmph
; 
 387         ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap
), &vBmph
); 
 388         pData
->m_nWidth  
= vBmph
.cx
; 
 389         pData
->m_nHeight 
= vBmph
.cy
; 
 390         pData
->m_nDepth  
= vBmph
.cBitCount
; 
 392     return(pBitmap
->Ok()); 
 393 } // end of wxBMPResourceHandler::LoadFile 
 395 bool wxBMPFileHandler::LoadFile( wxBitmap
*       pBitmap
, 
 396                                  const wxString
& WXUNUSED(rName
), 
 398                                  long            WXUNUSED(lFlags
), 
 399                                  int             WXUNUSED(nDesiredWidth
), 
 400                                  int             WXUNUSED(nDesiredHeight
) ) 
 402 #if wxUSE_IMAGE_LOADING_IN_OS2 
 403     wxPalette
* pPalette 
= NULL
; 
 405     bool bSuccess 
= false; /* wxLoadIntoBitmap( WXSTRINGCAST rName 
 409     if (bSuccess 
&& pPalette
) 
 411         pBitmap
->SetPalette(*pPalette
); 
 414     // it was copied by the bitmap if it was loaded successfully 
 419     wxUnusedVar(pBitmap
); 
 424 bool wxBMPFileHandler::SaveFile( wxBitmap
*        pBitmap
, 
 425                                  const wxString
&  WXUNUSED(rName
), 
 427                                  const wxPalette
* pPal 
) 
 429 #if wxUSE_IMAGE_LOADING_IN_OS2 
 430     wxPalette
* pActualPalette 
= (wxPalette 
*)pPal
; 
 433         pActualPalette 
= pBitmap
->GetPalette(); 
 434     /* return(wxSaveBitmap( WXSTRINGCAST rName 
 440     wxUnusedVar(pBitmap
); 
 446 // ---------------------------------------------------------------------------- 
 448 // ---------------------------------------------------------------------------- 
 450 bool wxICOFileHandler::LoadIcon( wxIcon
*         pIcon
, 
 451                                  const wxString
& WXUNUSED(rName
), 
 453                                  long            WXUNUSED(lFlags
), 
 454                                  int             WXUNUSED(nDesiredWidth
), 
 455                                  int             WXUNUSED(nDesiredHeight
) ) 
 457 #if wxUSE_RESOURCE_LOADING_IN_OS2 
 467 bool wxICOResourceHandler::LoadIcon( wxIcon
*         pIcon
, 
 468                                      const wxString
& rName
, 
 470                                      long            WXUNUSED(lFlags
), 
 471                                      int             WXUNUSED(nDesiredWidth
), 
 472                                      int             WXUNUSED(nDesiredHeight
) ) 
 476     hIcon 
= ::WinLoadFileIcon( (PSZ
)rName
.c_str() 
 477                               ,TRUE 
// load for private use 
 480     pIcon
->SetSize(32, 32); // all OS/2 icons are 32 x 32 
 482     pIcon
->SetHICON((WXHICON
)hIcon
); 
 485 } // end of wxICOResourceHandler::LoadIcon