]> git.saurik.com Git - wxWidgets.git/blob - src/os2/gdiimage.cpp
wxOS2 with Open Watcom: correct PCH usage, missing headers, warning fixes, source...
[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* WXUNUSED(pImage),
167 int WXUNUSED(nId),
168 long WXUNUSED(lFlags),
169 int WXUNUSED(nDesiredWidth),
170 int WXUNUSED(nDesiredHeight) )
171 {
172 return false;
173 }
174 };
175
176 class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
177 {
178 public:
179 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
180 _T("ico"),
181 wxBITMAP_TYPE_ICO)
182 {
183 }
184
185 virtual bool LoadIcon( wxIcon * pIcon
186 ,const wxString& rName
187 ,HPS hPs
188 ,long lFlags
189 ,int nDesiredWidth = -1
190 ,int nDesiredHeight = -1
191 );
192
193 private:
194 DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
195 };
196
197 class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
198 {
199 public:
200 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
201 _T("ico"),
202 wxBITMAP_TYPE_ICO_RESOURCE)
203 {
204 }
205
206 virtual bool LoadIcon( wxIcon* pIcon
207 ,const wxString& rName
208 ,HPS hPs
209 ,long lFlags
210 ,int nDesiredWidth = -1
211 ,int nDesiredHeight = -1
212 );
213
214 private:
215 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler)
216 };
217
218 // ----------------------------------------------------------------------------
219 // wxWin macros
220 // ----------------------------------------------------------------------------
221
222 #if !USE_SHARED_LIBRARIES
223 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
224 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
225 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
226 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
227 #endif
228
229 // ============================================================================
230 // implementation
231 // ============================================================================
232
233 wxGDIImageHandlerList wxGDIImage::ms_handlers;
234
235 // ----------------------------------------------------------------------------
236 // wxGDIImage functions forwarded to wxGDIImageRefData
237 // ----------------------------------------------------------------------------
238
239 bool wxGDIImage::FreeResource( bool WXUNUSED(bForce) )
240 {
241 if ( !IsNull() )
242 {
243 GetGDIImageData()->Free();
244 GetGDIImageData()->m_hHandle = 0;
245 }
246
247 return true;
248 }
249
250 WXHANDLE wxGDIImage::GetResourceHandle()
251 {
252 return GetHandle();
253 }
254
255 // ----------------------------------------------------------------------------
256 // wxGDIImage handler stuff
257 // ----------------------------------------------------------------------------
258
259 void wxGDIImage::AddHandler( wxGDIImageHandler* pHandler )
260 {
261 ms_handlers.Append(pHandler);
262 }
263
264 void wxGDIImage::InsertHandler( wxGDIImageHandler* pHandler )
265 {
266 ms_handlers.Insert(pHandler);
267 }
268
269 bool wxGDIImage::RemoveHandler( const wxString& rName )
270 {
271 wxGDIImageHandler* pHandler = FindHandler(rName);
272
273 if (pHandler)
274 {
275 ms_handlers.DeleteObject(pHandler);
276 return true;
277 }
278 else
279 return false;
280 }
281
282 wxGDIImageHandler* wxGDIImage::FindHandler(
283 const wxString& rName
284 )
285 {
286 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
287
288 while ( pNode )
289 {
290 wxGDIImageHandler* pHandler = pNode->GetData();
291
292 if ( pHandler->GetName() == rName )
293 return pHandler;
294 pNode = pNode->GetNext();
295 }
296 return((wxGDIImageHandler*)NULL);
297 }
298
299 wxGDIImageHandler* wxGDIImage::FindHandler(
300 const wxString& rExtension
301 , long lType
302 )
303 {
304 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
305 while ( pNode )
306 {
307 wxGDIImageHandler* pHandler = pNode->GetData();
308
309 if ( (pHandler->GetExtension() = rExtension) &&
310 (lType == -1 || pHandler->GetType() == lType) )
311 {
312 return pHandler;
313 }
314 pNode = pNode->GetNext();
315 }
316 return((wxGDIImageHandler*)NULL);
317 }
318
319 wxGDIImageHandler* wxGDIImage::FindHandler(
320 long lType
321 )
322 {
323 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
324
325 while ( pNode )
326 {
327 wxGDIImageHandler* pHandler = pNode->GetData();
328
329 if ( pHandler->GetType() == lType )
330 return pHandler;
331 pNode = pNode->GetNext();
332 }
333 return((wxGDIImageHandler*)NULL);
334 }
335
336 void wxGDIImage::CleanUpHandlers()
337 {
338 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
339
340 while ( pNode )
341 {
342 wxGDIImageHandler* pHandler = pNode->GetData();
343 wxGDIImageHandlerList::compatibility_iterator pNext = pNode->GetNext();
344
345 delete pHandler;
346 ms_handlers.Erase( pNode );
347 pNode = pNext;
348 }
349 }
350
351 void wxGDIImage::InitStandardHandlers()
352 {
353 AddHandler(new wxBMPResourceHandler);
354 AddHandler(new wxBMPFileHandler);
355 AddHandler(new wxICOResourceHandler);
356 AddHandler(new wxICOFileHandler);
357 }
358
359 // ----------------------------------------------------------------------------
360 // wxBitmap handlers
361 // ----------------------------------------------------------------------------
362
363 bool wxBMPResourceHandler::LoadFile( wxBitmap* pBitmap,
364 int nId,
365 long WXUNUSED(lFlags),
366 int WXUNUSED(nDesiredWidth),
367 int WXUNUSED(nDesiredHeight) )
368 {
369 SIZEL vSize = {0, 0};
370 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
371 HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
372 HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
373
374 pBitmap->SetHBITMAP((WXHBITMAP)::GpiLoadBitmap( hPS
375 ,NULLHANDLE
376 ,nId
377 ,0
378 ,0
379 ));
380 ::GpiDestroyPS(hPS);
381 ::DevCloseDC(hDC);
382
383 wxBitmapRefData* pData = pBitmap->GetBitmapData();
384
385 if ( pBitmap->Ok() )
386 {
387 BITMAPINFOHEADER vBmph;
388
389 ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap), &vBmph);
390 pData->m_nWidth = vBmph.cx;
391 pData->m_nHeight = vBmph.cy;
392 pData->m_nDepth = vBmph.cBitCount;
393 }
394 return(pBitmap->Ok());
395 } // end of wxBMPResourceHandler::LoadFile
396
397 bool wxBMPFileHandler::LoadFile( wxBitmap* pBitmap,
398 const wxString& WXUNUSED(rName),
399 HPS WXUNUSED(hPs),
400 long WXUNUSED(lFlags),
401 int WXUNUSED(nDesiredWidth),
402 int WXUNUSED(nDesiredHeight) )
403 {
404 #if wxUSE_IMAGE_LOADING_IN_OS2
405 wxPalette* pPalette = NULL;
406
407 bool bSuccess = false; /* wxLoadIntoBitmap( WXSTRINGCAST rName
408 ,pBitmap
409 ,&pPalette
410 ) != 0; */
411 if (bSuccess && pPalette)
412 {
413 pBitmap->SetPalette(*pPalette);
414 }
415
416 // it was copied by the bitmap if it was loaded successfully
417 delete pPalette;
418
419 return(bSuccess);
420 #else
421 wxUnusedVar(pBitmap);
422 return false;
423 #endif
424 }
425
426 bool wxBMPFileHandler::SaveFile( wxBitmap* pBitmap,
427 const wxString& WXUNUSED(rName),
428 int WXUNUSED(nType),
429 const wxPalette* pPal )
430 {
431 #if wxUSE_IMAGE_LOADING_IN_OS2
432 wxPalette* pActualPalette = (wxPalette *)pPal;
433
434 if (!pActualPalette)
435 pActualPalette = pBitmap->GetPalette();
436 /* return(wxSaveBitmap( WXSTRINGCAST rName
437 ,pBitmap
438 ,pActualPalette
439 ) != 0); */
440 return false;
441 #else
442 wxUnusedVar(pBitmap);
443 wxUnusedVar(pPal);
444 return false;
445 #endif
446 }
447
448 // ----------------------------------------------------------------------------
449 // wxIcon handlers
450 // ----------------------------------------------------------------------------
451
452 bool wxICOFileHandler::LoadIcon( wxIcon* pIcon,
453 const wxString& WXUNUSED(rName),
454 HPS WXUNUSED(hPs),
455 long WXUNUSED(lFlags),
456 int WXUNUSED(nDesiredWidth),
457 int WXUNUSED(nDesiredHeight) )
458 {
459 #if wxUSE_RESOURCE_LOADING_IN_OS2
460 pIcon->UnRef();
461
462 return false;
463 #else
464 wxUnusedVar(pIcon);
465 return false;
466 #endif
467 }
468
469 bool wxICOResourceHandler::LoadIcon( wxIcon* pIcon,
470 const wxString& rName,
471 HPS WXUNUSED(hPs),
472 long WXUNUSED(lFlags),
473 int WXUNUSED(nDesiredWidth),
474 int WXUNUSED(nDesiredHeight) )
475 {
476 HPOINTER hIcon;
477
478 hIcon = ::WinLoadFileIcon( (PSZ)rName.c_str()
479 ,TRUE // load for private use
480 );
481
482 pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32
483
484 pIcon->SetHICON((WXHICON)hIcon);
485
486 return pIcon->Ok();
487 } // end of wxICOResourceHandler::LoadIcon