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