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