]> git.saurik.com Git - wxWidgets.git/blame - src/os2/gdiimage.cpp
[1231183] 'cleanup: mismatched indentation' and other cleanings.
[wxWidgets.git] / src / os2 / gdiimage.cpp
CommitLineData
b4eb4c6b
DW
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>
65571936 9// Licence: wxWindows licence
b4eb4c6b
DW
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
aa213887
SN
20#ifdef __GNUG__
21 #pragma implementation "gdiimage.h"
22#endif
23
b4eb4c6b
DW
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"
b4eb4c6b 33#include "wx/app.h"
b4eb4c6b
DW
34#include "wx/os2/gdiimage.h"
35
258f5b3e
DW
36#include "wx/listimpl.cpp"
37WX_DEFINE_LIST(wxGDIImageHandlerList);
38
b4eb4c6b
DW
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
46class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler
47{
48public:
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
67private:
3029781e
DW
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 }
b4eb4c6b
DW
82 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
83};
84
85class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
86{
87public:
88 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
89 wxEmptyString,
90 wxBITMAP_TYPE_BMP_RESOURCE)
91 {
92 }
93
94 virtual bool LoadFile( wxBitmap* pBitmap
3029781e 95 ,int nId
b4eb4c6b
DW
96 ,long lFlags
97 ,int nDesiredWidth
98 ,int nDesiredHeight
99 );
100
101private:
102 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
103};
104
105class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler
106{
107public:
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
157protected:
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;
3029781e 165private:
6670f564
WS
166 inline virtual bool Load( wxGDIImage* WXUNUSED(pImage),
167 int WXUNUSED(nId),
168 long WXUNUSED(lFlags),
169 int WXUNUSED(nDesiredWidth),
170 int WXUNUSED(nDesiredHeight) )
3029781e 171 {
6670f564 172 return false;
3029781e 173 }
b4eb4c6b
DW
174};
175
176class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
177{
178public:
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
193private:
194 DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
195};
196
197class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
198{
199public:
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
214private:
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)
4a46a5df
DW
225 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
226 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
b4eb4c6b
DW
227#endif
228
b4eb4c6b
DW
229// ============================================================================
230// implementation
231// ============================================================================
232
258f5b3e 233wxGDIImageHandlerList wxGDIImage::ms_handlers;
b4eb4c6b
DW
234
235// ----------------------------------------------------------------------------
236// wxGDIImage functions forwarded to wxGDIImageRefData
237// ----------------------------------------------------------------------------
238
6670f564 239bool wxGDIImage::FreeResource( bool WXUNUSED(bForce) )
b4eb4c6b
DW
240{
241 if ( !IsNull() )
242 {
243 GetGDIImageData()->Free();
244 GetGDIImageData()->m_hHandle = 0;
245 }
246
6670f564 247 return true;
b4eb4c6b
DW
248}
249
250WXHANDLE wxGDIImage::GetResourceHandle()
251{
252 return GetHandle();
253}
254
255// ----------------------------------------------------------------------------
256// wxGDIImage handler stuff
257// ----------------------------------------------------------------------------
258
6670f564 259void wxGDIImage::AddHandler( wxGDIImageHandler* pHandler )
b4eb4c6b
DW
260{
261 ms_handlers.Append(pHandler);
262}
263
6670f564 264void wxGDIImage::InsertHandler( wxGDIImageHandler* pHandler )
b4eb4c6b
DW
265{
266 ms_handlers.Insert(pHandler);
267}
268
6670f564 269bool wxGDIImage::RemoveHandler( const wxString& rName )
b4eb4c6b
DW
270{
271 wxGDIImageHandler* pHandler = FindHandler(rName);
272
273 if (pHandler)
274 {
275 ms_handlers.DeleteObject(pHandler);
6670f564 276 return true;
b4eb4c6b
DW
277 }
278 else
6670f564 279 return false;
b4eb4c6b
DW
280}
281
282wxGDIImageHandler* wxGDIImage::FindHandler(
283 const wxString& rName
284)
285{
53ac3021
DW
286 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
287
288 while ( pNode )
b4eb4c6b 289 {
53ac3021
DW
290 wxGDIImageHandler* pHandler = pNode->GetData();
291
292 if ( pHandler->GetName() == rName )
293 return pHandler;
294 pNode = pNode->GetNext();
b4eb4c6b 295 }
eb65cb34 296 return((wxGDIImageHandler*)NULL);
b4eb4c6b
DW
297}
298
299wxGDIImageHandler* wxGDIImage::FindHandler(
300 const wxString& rExtension
301, long lType
302)
303{
53ac3021
DW
304 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
305 while ( pNode )
b4eb4c6b 306 {
53ac3021
DW
307 wxGDIImageHandler* pHandler = pNode->GetData();
308
309 if ( (pHandler->GetExtension() = rExtension) &&
310 (lType == -1 || pHandler->GetType() == lType) )
b4eb4c6b 311 {
53ac3021 312 return pHandler;
b4eb4c6b 313 }
53ac3021 314 pNode = pNode->GetNext();
b4eb4c6b 315 }
eb65cb34 316 return((wxGDIImageHandler*)NULL);
b4eb4c6b
DW
317}
318
319wxGDIImageHandler* wxGDIImage::FindHandler(
320 long lType
321)
322{
53ac3021
DW
323 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
324
325 while ( pNode )
b4eb4c6b 326 {
53ac3021 327 wxGDIImageHandler* pHandler = pNode->GetData();
b4eb4c6b 328
53ac3021
DW
329 if ( pHandler->GetType() == lType )
330 return pHandler;
331 pNode = pNode->GetNext();
b4eb4c6b 332 }
258f5b3e 333 return((wxGDIImageHandler*)NULL);
b4eb4c6b
DW
334}
335
336void wxGDIImage::CleanUpHandlers()
337{
53ac3021
DW
338 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
339
340 while ( pNode )
b4eb4c6b 341 {
53ac3021
DW
342 wxGDIImageHandler* pHandler = pNode->GetData();
343 wxGDIImageHandlerList::compatibility_iterator pNext = pNode->GetNext();
344
345 delete pHandler;
346 ms_handlers.Erase( pNode );
347 pNode = pNext;
b4eb4c6b
DW
348 }
349}
350
351void wxGDIImage::InitStandardHandlers()
352{
353 AddHandler(new wxBMPResourceHandler);
354 AddHandler(new wxBMPFileHandler);
b4eb4c6b
DW
355 AddHandler(new wxICOResourceHandler);
356 AddHandler(new wxICOFileHandler);
357}
358
359// ----------------------------------------------------------------------------
360// wxBitmap handlers
361// ----------------------------------------------------------------------------
362
6670f564
WS
363bool wxBMPResourceHandler::LoadFile( wxBitmap* pBitmap,
364 int nId,
365 long WXUNUSED(lFlags),
366 int WXUNUSED(nDesiredWidth),
367 int WXUNUSED(nDesiredHeight) )
b4eb4c6b 368{
3029781e
DW
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() )
b4eb4c6b 386 {
3029781e 387 BITMAPINFOHEADER vBmph;
b4eb4c6b 388
3029781e
DW
389 ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap), &vBmph);
390 pData->m_nWidth = vBmph.cx;
391 pData->m_nHeight = vBmph.cy;
392 pData->m_nDepth = vBmph.cBitCount;
b4eb4c6b 393 }
3029781e
DW
394 return(pBitmap->Ok());
395} // end of wxBMPResourceHandler::LoadFile
b4eb4c6b 396
6670f564
WS
397bool 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) )
b4eb4c6b
DW
403{
404#if wxUSE_IMAGE_LOADING_IN_OS2
6670f564 405 wxPalette* pPalette = NULL;
b4eb4c6b 406
6670f564
WS
407 bool bSuccess = false; /* wxLoadIntoBitmap( WXSTRINGCAST rName
408 ,pBitmap
409 ,&pPalette
410 ) != 0; */
b4eb4c6b
DW
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
6670f564
WS
421 wxUnusedVar(pBitmap);
422 return false;
b4eb4c6b
DW
423#endif
424}
425
6670f564
WS
426bool wxBMPFileHandler::SaveFile( wxBitmap* pBitmap,
427 const wxString& WXUNUSED(rName),
428 int WXUNUSED(nType),
429 const wxPalette* pPal )
b4eb4c6b
DW
430{
431#if wxUSE_IMAGE_LOADING_IN_OS2
6670f564 432 wxPalette* pActualPalette = (wxPalette *)pPal;
b4eb4c6b
DW
433
434 if (!pActualPalette)
435 pActualPalette = pBitmap->GetPalette();
436 /* return(wxSaveBitmap( WXSTRINGCAST rName
437 ,pBitmap
438 ,pActualPalette
439 ) != 0); */
6670f564 440 return false;
b4eb4c6b 441#else
6670f564
WS
442 wxUnusedVar(pBitmap);
443 wxUnusedVar(pPal);
444 return false;
b4eb4c6b
DW
445#endif
446}
447
448// ----------------------------------------------------------------------------
449// wxIcon handlers
450// ----------------------------------------------------------------------------
451
6670f564
WS
452bool 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) )
b4eb4c6b
DW
458{
459#if wxUSE_RESOURCE_LOADING_IN_OS2
460 pIcon->UnRef();
461
6670f564 462 return false;
b4eb4c6b 463#else
6670f564
WS
464 wxUnusedVar(pIcon);
465 return false;
b4eb4c6b
DW
466#endif
467}
468
6670f564
WS
469bool wxICOResourceHandler::LoadIcon( wxIcon* pIcon,
470 const wxString& rName,
471 HPS WXUNUSED(hPs),
472 long WXUNUSED(lFlags),
473 int WXUNUSED(nDesiredWidth),
474 int WXUNUSED(nDesiredHeight) )
b4eb4c6b 475{
22e90769 476 HPOINTER hIcon;
b4eb4c6b 477
22e90769
DW
478 hIcon = ::WinLoadFileIcon( (PSZ)rName.c_str()
479 ,TRUE // load for private use
480 );
b4eb4c6b 481
22e90769 482 pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32
b4eb4c6b 483
22e90769 484 pIcon->SetHICON((WXHICON)hIcon);
b4eb4c6b 485
22e90769
DW
486 return pIcon->Ok();
487} // end of wxICOResourceHandler::LoadIcon