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 wxNode* pNode = ms_handlers.First();
302
303 while (pNode)
304 {
305 wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data();
306
307 if (pHandler->GetName() == rName)
308 return(pHandler);
309 pNode = pNode->Next();
310 }
311 return(NULL);
312 }
313
314 wxGDIImageHandler* wxGDIImage::FindHandler(
315 const wxString& rExtension
316 , long lType
317 )
318 {
319 wxNode* pNode = ms_handlers.First();
320
321 while (pNode)
322 {
323 wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data();
324
325 if ((pHandler->GetExtension() = rExtension) &&
326 (lType == -1 || pHandler->GetType() == lType))
327 {
328 return(pHandler);
329 }
330 pNode = pNode->Next();
331 }
332 return(NULL);
333 }
334
335 wxGDIImageHandler* wxGDIImage::FindHandler(
336 long lType
337 )
338 {
339 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
340 while ( node )
341 {
342 wxGDIImageHandler *handler = node->GetData();
343 if ( handler->GetType() == type )
344 return handler;
345
346 node = node->GetNext();
347 }
348
349 return((wxGDIImageHandler*)NULL);
350 }
351
352 void wxGDIImage::CleanUpHandlers()
353 {
354 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
355 while ( node )
356 {
357 wxGDIImageHandler *handler = node->GetData();
358 wxGDIImageHandlerList::compatibility_iterator next = node->GetNext();
359 delete handler;
360 ms_handlers.Erase( node );
361 node = next;
362 }
363 }
364
365 void wxGDIImage::InitStandardHandlers()
366 {
367 AddHandler(new wxBMPResourceHandler);
368 AddHandler(new wxBMPFileHandler);
369 AddHandler(new wxICOResourceHandler);
370 AddHandler(new wxICOFileHandler);
371 }
372
373 // ----------------------------------------------------------------------------
374 // wxBitmap handlers
375 // ----------------------------------------------------------------------------
376
377 bool wxBMPResourceHandler::LoadFile(
378 wxBitmap* pBitmap
379 , int nId
380 , long lFlags
381 , int nDesiredWidth
382 , int nDesiredHeight
383 )
384 {
385 SIZEL vSize = {0, 0};
386 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
387 HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
388 HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
389
390 pBitmap->SetHBITMAP((WXHBITMAP)::GpiLoadBitmap( hPS
391 ,NULLHANDLE
392 ,nId
393 ,0
394 ,0
395 ));
396 ::GpiDestroyPS(hPS);
397 ::DevCloseDC(hDC);
398
399 wxBitmapRefData* pData = pBitmap->GetBitmapData();
400
401 if ( pBitmap->Ok() )
402 {
403 BITMAPINFOHEADER vBmph;
404
405 ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap), &vBmph);
406 pData->m_nWidth = vBmph.cx;
407 pData->m_nHeight = vBmph.cy;
408 pData->m_nDepth = vBmph.cBitCount;
409 }
410 return(pBitmap->Ok());
411 } // end of wxBMPResourceHandler::LoadFile
412
413 bool wxBMPFileHandler::LoadFile(
414 wxBitmap* pBitmap
415 , const wxString& rName
416 , HPS hPs
417 , long WXUNUSED(lFlags)
418 , int WXUNUSED(nDesiredWidth)
419 , int WXUNUSED(nDesiredHeight)
420 )
421 {
422 #if wxUSE_IMAGE_LOADING_IN_OS2
423 wxPalette* pPalette = NULL;
424
425 bool bSuccess = FALSE; /* wxLoadIntoBitmap( WXSTRINGCAST rName
426 ,pBitmap
427 ,&pPalette
428 ) != 0; */
429 if (bSuccess && pPalette)
430 {
431 pBitmap->SetPalette(*pPalette);
432 }
433
434 // it was copied by the bitmap if it was loaded successfully
435 delete pPalette;
436
437 return(bSuccess);
438 #else
439 return(FALSE);
440 #endif
441 }
442
443 bool wxBMPFileHandler::SaveFile(
444 wxBitmap* pBitmap
445 , const wxString& rName
446 , int WXUNUSED(nType)
447 , const wxPalette* pPal
448 )
449 {
450 #if wxUSE_IMAGE_LOADING_IN_OS2
451 wxPalette* pActualPalette = (wxPalette *)pPal;
452
453 if (!pActualPalette)
454 pActualPalette = pBitmap->GetPalette();
455 /* return(wxSaveBitmap( WXSTRINGCAST rName
456 ,pBitmap
457 ,pActualPalette
458 ) != 0); */
459 return(FALSE);
460 #else
461 return(FALSE);
462 #endif
463 }
464
465 // ----------------------------------------------------------------------------
466 // wxIcon handlers
467 // ----------------------------------------------------------------------------
468
469 bool wxICOFileHandler::LoadIcon(
470 wxIcon* pIcon
471 , const wxString& rName
472 , HPS hPs
473 , long lFlags
474 , int nDesiredWidth
475 , int nDesiredHeight
476 )
477 {
478 #if wxUSE_RESOURCE_LOADING_IN_OS2
479 pIcon->UnRef();
480
481 // actual size
482 wxSize vSize;
483
484 return(FALSE);
485 #else
486 return(FALSE);
487 #endif
488 }
489
490 bool wxICOResourceHandler::LoadIcon(
491 wxIcon* pIcon
492 , const wxString& rName
493 , HPS hPs
494 , long lFlags
495 , int WXUNUSED(nDesiredWidth)
496 , int WXUNUSED(nDesiredHeight)
497 )
498 {
499 HPOINTER hIcon;
500
501 hIcon = ::WinLoadFileIcon( (PSZ)rName.c_str()
502 ,TRUE // load for private use
503 );
504
505 pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32
506
507
508 pIcon->SetHICON((WXHICON)hIcon);
509
510 return pIcon->Ok();
511 } // end of wxICOResourceHandler::LoadIcon
512
513 // ----------------------------------------------------------------------------
514 // private functions
515 // ----------------------------------------------------------------------------
516
517 static wxSize GetHiconSize(
518 WXHICON hicon
519 )
520 {
521 wxSize vSize(32, 32); // default
522
523 // all OS/2 icons are 32x32
524 return(vSize);
525 }
526