1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/os2/gdiimage.cpp
3 // Purpose:     wxGDIImage implementation
4 // Author:      Vadim Zeitlin
5 // Modified by:
6 // Created:     20.11.99
7 // Copyright:   (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence:     wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 // ============================================================================
12 // declarations
13 // ============================================================================
14 
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18 
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21 
22 
23 #ifndef WX_PRECOMP
24     #include "wx/string.h"
25     #include "wx/app.h"
26 #endif // WX_PRECOMP
27 
28 #include "wx/os2/private.h"
29 #include "wx/os2/gdiimage.h"
30 
31 #include "wx/listimpl.cpp"
32 WX_DEFINE_LIST(wxGDIImageHandlerList)
33 
34 // ----------------------------------------------------------------------------
35 // private classes
36 // ----------------------------------------------------------------------------
37 
38 // all image handlers are declared/defined in this file because the outside
39 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
40 
41 class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler
42 {
43 public:
wxBMPFileHandler()44     wxBMPFileHandler() : wxBitmapHandler(wxT("Windows bitmap file"), wxT("bmp"),
45                                          wxBITMAP_TYPE_BMP)
46     {
47     }
48 
49     virtual bool LoadFile( wxBitmap*       pBitmap
50                           ,const wxString& rName
51                           ,HPS             hPs
52                           ,wxBitmapType    lFlags
53                           ,int             nDesiredWidth
54                           ,int             nDesiredHeight
55                          );
56     virtual bool SaveFile( wxBitmap*        pBitmap
57                           ,const wxString&  rName
58                           ,wxBitmapType     lType
59                           ,const wxPalette* pPalette = NULL
60                          );
61 
62 private:
LoadFile(wxBitmap * pBitmap,int nId,wxBitmapType lFlags,int nDesiredWidth,int nDesiredHeight)63     inline virtual bool LoadFile( wxBitmap*       pBitmap
64                                  ,int             nId
65                                  ,wxBitmapType    lFlags
66                                  ,int             nDesiredWidth
67                                  ,int             nDesiredHeight
68                                 )
69     {
70         return wxBitmapHandler::LoadFile( pBitmap
71                                          ,nId
72                                          ,lFlags
73                                          ,nDesiredWidth
74                                          ,nDesiredHeight
75                                         );
76     }
77     DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
78 };
79 
80 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
81 {
82 public:
wxBMPResourceHandler()83     wxBMPResourceHandler() : wxBitmapHandler(wxT("Windows bitmap resource"),
84                                              wxEmptyString,
85                                              wxBITMAP_TYPE_BMP_RESOURCE)
86     {
87     }
88 
89     virtual bool LoadFile( wxBitmap*       pBitmap
90                           ,int             nId
91                           ,wxBitmapType    lFlags
92                           ,int             nDesiredWidth
93                           ,int             nDesiredHeight
94                          );
95 
96 private:
97     DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
98 };
99 
100 class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler
101 {
102 public:
wxIconHandler(const wxString & rName,const wxString & rExt,wxBitmapType lType)103     wxIconHandler( const wxString& rName
104                   ,const wxString& rExt
105                   ,wxBitmapType    lType
106                  ) : wxGDIImageHandler( rName
107                                        ,rExt
108                                        ,lType
109                                       )
110     {
111     }
112 
113     // creating and saving icons is not supported
Create(wxGDIImage * WXUNUSED (pImage),const void * WXUNUSED (pData),wxBitmapType WXUNUSED (lFlags),int WXUNUSED (nWidth),int WXUNUSED (nHeight),int WXUNUSED (nDepth)=1)114     virtual bool Create( wxGDIImage* WXUNUSED(pImage)
115                         ,const void* WXUNUSED(pData)
116                         ,wxBitmapType WXUNUSED(lFlags)
117                         ,int         WXUNUSED(nWidth)
118                         ,int         WXUNUSED(nHeight)
119                         ,int         WXUNUSED(nDepth) = 1
120                        )
121     {
122         return false;
123     }
124 
Save(const wxGDIImage * WXUNUSED (pImage),const wxString & WXUNUSED (rName),wxBitmapType WXUNUSED (nType)) const125     virtual bool Save( const wxGDIImage* WXUNUSED(pImage)
126                       ,const wxString&   WXUNUSED(rName)
127                       ,wxBitmapType      WXUNUSED(nType)
128                      ) const
129     {
130         return false;
131     }
Load(wxGDIImage * pImage,const wxString & rName,HPS hPs,wxBitmapType lFlags,int nDesiredWidth,int nDesiredHeight)132     virtual bool Load( wxGDIImage*     pImage
133                       ,const wxString& rName
134                       ,HPS             hPs
135                       ,wxBitmapType    lFlags
136                       ,int             nDesiredWidth
137                       ,int             nDesiredHeight
138                      )
139     {
140         wxIcon*                     pIcon = wxDynamicCast(pImage, wxIcon);
141         wxCHECK_MSG(pIcon, false, wxT("wxIconHandler only works with icons"));
142 
143         return LoadIcon( pIcon
144                         ,rName
145                         ,hPs
146                         ,lFlags
147                         ,nDesiredWidth
148                         ,nDesiredHeight
149                        );
150     }
151 
152 protected:
153     virtual bool LoadIcon( wxIcon*         pIcon
154                           ,const wxString& rName
155                           ,HPS             hPs
156                           ,wxBitmapType    lFlags
157                           ,int             nDesiredWidth = -1
158                           ,int             nDesiredHeight = -1
159                          ) = 0;
160 private:
Load(wxGDIImage * WXUNUSED (pImage),int WXUNUSED (nId),wxBitmapType WXUNUSED (lFlags),int WXUNUSED (nDesiredWidth),int WXUNUSED (nDesiredHeight))161     inline virtual bool Load( wxGDIImage* WXUNUSED(pImage),
162                               int         WXUNUSED(nId),
163                               wxBitmapType WXUNUSED(lFlags),
164                               int         WXUNUSED(nDesiredWidth),
165                               int         WXUNUSED(nDesiredHeight) )
166     {
167         return false;
168     }
169 };
170 
171 class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
172 {
173 public:
wxICOFileHandler()174     wxICOFileHandler() : wxIconHandler(wxT("ICO icon file"),
175                                        wxT("ico"),
176                                        wxBITMAP_TYPE_ICO)
177     {
178     }
179 
180     virtual bool LoadIcon( wxIcon *        pIcon
181                           ,const wxString& rName
182                           ,HPS             hPs
183                           ,wxBitmapType    lFlags
184                           ,int             nDesiredWidth = -1
185                           ,int             nDesiredHeight = -1
186                          );
187 
188 private:
189     DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
190 };
191 
192 class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
193 {
194 public:
wxICOResourceHandler()195     wxICOResourceHandler() : wxIconHandler(wxT("ICO resource"),
196                                            wxT("ico"),
197                                            wxBITMAP_TYPE_ICO_RESOURCE)
198     {
199     }
200 
201     virtual bool LoadIcon( wxIcon*         pIcon
202                           ,const wxString& rName
203                           ,HPS             hPs
204                           ,wxBitmapType    lFlags
205                           ,int             nDesiredWidth = -1
206                           ,int             nDesiredHeight = -1
207                          );
208 
209 private:
210     DECLARE_DYNAMIC_CLASS(wxICOResourceHandler)
211 };
212 
213 // ----------------------------------------------------------------------------
214 // wxWin macros
215 // ----------------------------------------------------------------------------
216 
217 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
218 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
219 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
220 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
221 
222 // ============================================================================
223 // implementation
224 // ============================================================================
225 
226 wxGDIImageHandlerList wxGDIImage::ms_handlers;
227 
228 // ----------------------------------------------------------------------------
229 // wxGDIImage functions forwarded to wxGDIImageRefData
230 // ----------------------------------------------------------------------------
231 
FreeResource(bool WXUNUSED (bForce))232 bool wxGDIImage::FreeResource( bool WXUNUSED(bForce) )
233 {
234     if ( !IsNull() )
235     {
236         GetGDIImageData()->Free();
237         GetGDIImageData()->m_hHandle = 0;
238     }
239 
240     return true;
241 }
242 
GetResourceHandle() const243 WXHANDLE wxGDIImage::GetResourceHandle() const
244 {
245     return GetHandle();
246 }
247 
248 // ----------------------------------------------------------------------------
249 // wxGDIImage handler stuff
250 // ----------------------------------------------------------------------------
251 
AddHandler(wxGDIImageHandler * pHandler)252 void wxGDIImage::AddHandler( wxGDIImageHandler* pHandler )
253 {
254     ms_handlers.Append(pHandler);
255 }
256 
InsertHandler(wxGDIImageHandler * pHandler)257 void wxGDIImage::InsertHandler( wxGDIImageHandler* pHandler )
258 {
259     ms_handlers.Insert(pHandler);
260 }
261 
RemoveHandler(const wxString & rName)262 bool wxGDIImage::RemoveHandler( const wxString& rName )
263 {
264     wxGDIImageHandler*              pHandler = FindHandler(rName);
265 
266     if (pHandler)
267     {
268         ms_handlers.DeleteObject(pHandler);
269         return true;
270     }
271     else
272         return false;
273 }
274 
FindHandler(const wxString & rName)275 wxGDIImageHandler* wxGDIImage::FindHandler(
276   const wxString&                   rName
277 )
278 {
279     wxGDIImageHandlerList::compatibility_iterator   pNode = ms_handlers.GetFirst();
280 
281     while ( pNode )
282     {
283         wxGDIImageHandler*          pHandler = pNode->GetData();
284 
285         if ( pHandler->GetName() == rName )
286             return pHandler;
287         pNode = pNode->GetNext();
288     }
289     return(NULL);
290 }
291 
FindHandler(const wxString & rExtension,wxBitmapType lType)292 wxGDIImageHandler* wxGDIImage::FindHandler(
293   const wxString&                   rExtension
294 , wxBitmapType                      lType
295 )
296 {
297     wxGDIImageHandlerList::compatibility_iterator   pNode = ms_handlers.GetFirst();
298     while ( pNode )
299     {
300         wxGDIImageHandler*          pHandler = pNode->GetData();
301 
302         if ( (pHandler->GetExtension() == rExtension) &&
303              (lType == -1 || pHandler->GetType() == lType) )
304         {
305             return pHandler;
306         }
307         pNode = pNode->GetNext();
308     }
309     return(NULL);
310 }
311 
FindHandler(wxBitmapType lType)312 wxGDIImageHandler* wxGDIImage::FindHandler(
313   wxBitmapType                           lType
314 )
315 {
316     wxGDIImageHandlerList::compatibility_iterator   pNode = ms_handlers.GetFirst();
317 
318     while ( pNode )
319     {
320         wxGDIImageHandler*          pHandler = pNode->GetData();
321 
322         if ( pHandler->GetType() == lType )
323             return pHandler;
324         pNode = pNode->GetNext();
325     }
326     return(NULL);
327 }
328 
CleanUpHandlers()329 void wxGDIImage::CleanUpHandlers()
330 {
331     wxGDIImageHandlerList::compatibility_iterator   pNode = ms_handlers.GetFirst();
332 
333     while ( pNode )
334     {
335         wxGDIImageHandler*                              pHandler = pNode->GetData();
336         wxGDIImageHandlerList::compatibility_iterator   pNext = pNode->GetNext();
337 
338         delete pHandler;
339         ms_handlers.Erase( pNode );
340         pNode = pNext;
341     }
342 }
343 
InitStandardHandlers()344 void wxGDIImage::InitStandardHandlers()
345 {
346     AddHandler(new wxBMPResourceHandler);
347     AddHandler(new wxBMPFileHandler);
348     AddHandler(new wxICOResourceHandler);
349     AddHandler(new wxICOFileHandler);
350 }
351 
352 // ----------------------------------------------------------------------------
353 // wxBitmap handlers
354 // ----------------------------------------------------------------------------
355 
LoadFile(wxBitmap * pBitmap,int nId,wxBitmapType WXUNUSED (lFlags),int WXUNUSED (nDesiredWidth),int WXUNUSED (nDesiredHeight))356 bool wxBMPResourceHandler::LoadFile( wxBitmap* pBitmap,
357                                      int       nId,
358                                      wxBitmapType WXUNUSED(lFlags),
359                                      int       WXUNUSED(nDesiredWidth),
360                                      int       WXUNUSED(nDesiredHeight) )
361 {
362     SIZEL                               vSize = {0, 0};
363     DEVOPENSTRUC                        vDop  = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
364     HDC                                 hDC   = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
365     HPS                                 hPS   = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
366 
367     pBitmap->SetHBITMAP((WXHBITMAP)::GpiLoadBitmap( hPS
368                                                    ,NULLHANDLE
369                                                    ,nId
370                                                    ,0
371                                                    ,0
372                                                   ));
373     ::GpiDestroyPS(hPS);
374     ::DevCloseDC(hDC);
375 
376     wxBitmapRefData*                pData = pBitmap->GetBitmapData();
377 
378     if ( pBitmap->IsOk() )
379     {
380         BITMAPINFOHEADER            vBmph;
381 
382         ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap), &vBmph);
383         pData->m_nWidth  = vBmph.cx;
384         pData->m_nHeight = vBmph.cy;
385         pData->m_nDepth  = vBmph.cBitCount;
386     }
387     return(pBitmap->IsOk());
388 } // end of wxBMPResourceHandler::LoadFile
389 
LoadFile(wxBitmap * pBitmap,const wxString & WXUNUSED (rName),HPS WXUNUSED (hPs),wxBitmapType WXUNUSED (lFlags),int WXUNUSED (nDesiredWidth),int WXUNUSED (nDesiredHeight))390 bool wxBMPFileHandler::LoadFile( wxBitmap*       pBitmap,
391                                  const wxString& WXUNUSED(rName),
392                                  HPS             WXUNUSED(hPs),
393                                  wxBitmapType    WXUNUSED(lFlags),
394                                  int             WXUNUSED(nDesiredWidth),
395                                  int             WXUNUSED(nDesiredHeight) )
396 {
397 #if defined(wxUSE_IMAGE_LOADING_IN_OS2) && wxUSE_IMAGE_LOADING_IN_OS2
398     wxPalette* pPalette = NULL;
399 
400     bool bSuccess = false; /* wxLoadIntoBitmap( WXSTRINGCAST rName
401                                                ,pBitmap
402                                                ,&pPalette
403                                               ) != 0; */
404     if (bSuccess && pPalette)
405     {
406         pBitmap->SetPalette(*pPalette);
407     }
408 
409     // it was copied by the bitmap if it was loaded successfully
410     delete pPalette;
411 
412     return(bSuccess);
413 #else
414     wxUnusedVar(pBitmap);
415     return false;
416 #endif
417 }
418 
SaveFile(wxBitmap * pBitmap,const wxString & WXUNUSED (rName),wxBitmapType WXUNUSED (nType),const wxPalette * pPal)419 bool wxBMPFileHandler::SaveFile( wxBitmap*        pBitmap,
420                                  const wxString&  WXUNUSED(rName),
421                                  wxBitmapType     WXUNUSED(nType),
422                                  const wxPalette* pPal )
423 {
424 #if defined(wxUSE_IMAGE_LOADING_IN_OS2) && wxUSE_IMAGE_LOADING_IN_OS2
425     wxPalette* pActualPalette = (wxPalette *)pPal;
426 
427     if (!pActualPalette)
428         pActualPalette = pBitmap->GetPalette();
429     /* return(wxSaveBitmap( WXSTRINGCAST rName
430                         ,pBitmap
431                         ,pActualPalette
432                        ) != 0); */
433     return false;
434 #else
435     wxUnusedVar(pBitmap);
436     wxUnusedVar(pPal);
437     return false;
438 #endif
439 }
440 
441 // ----------------------------------------------------------------------------
442 // wxIcon handlers
443 // ----------------------------------------------------------------------------
444 
LoadIcon(wxIcon * pIcon,const wxString & WXUNUSED (rName),HPS WXUNUSED (hPs),wxBitmapType WXUNUSED (lFlags),int WXUNUSED (nDesiredWidth),int WXUNUSED (nDesiredHeight))445 bool wxICOFileHandler::LoadIcon( wxIcon*         pIcon,
446                                  const wxString& WXUNUSED(rName),
447                                  HPS             WXUNUSED(hPs),
448                                  wxBitmapType    WXUNUSED(lFlags),
449                                  int             WXUNUSED(nDesiredWidth),
450                                  int             WXUNUSED(nDesiredHeight) )
451 {
452 #if defined(wxUSE_RESOURCE_LOADING_IN_OS2) && wxUSE_RESOURCE_LOADING_IN_OS2
453     pIcon->UnRef();
454 
455     return false;
456 #else
457     wxUnusedVar(pIcon);
458     return false;
459 #endif
460 }
461 
LoadIcon(wxIcon * pIcon,const wxString & rName,HPS WXUNUSED (hPs),wxBitmapType WXUNUSED (lFlags),int WXUNUSED (nDesiredWidth),int WXUNUSED (nDesiredHeight))462 bool wxICOResourceHandler::LoadIcon( wxIcon*         pIcon,
463                                      const wxString& rName,
464                                      HPS             WXUNUSED(hPs),
465                                      wxBitmapType    WXUNUSED(lFlags),
466                                      int             WXUNUSED(nDesiredWidth),
467                                      int             WXUNUSED(nDesiredHeight) )
468 {
469     HPOINTER                        hIcon;
470 
471     hIcon = ::WinLoadFileIcon( rName.c_str()
472                               ,TRUE // load for private use
473                              );
474 
475     pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32
476 
477     pIcon->SetHICON((WXHICON)hIcon);
478 
479     return pIcon->IsOk();
480 } // end of wxICOResourceHandler::LoadIcon
481