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