]> git.saurik.com Git - wxWidgets.git/blob - src/os2/gdiimage.cpp
fix showing keyboard cues under Win2k/XP
[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 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
223 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
224 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
225 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
226
227 // ============================================================================
228 // implementation
229 // ============================================================================
230
231 wxGDIImageHandlerList wxGDIImage::ms_handlers;
232
233 // ----------------------------------------------------------------------------
234 // wxGDIImage functions forwarded to wxGDIImageRefData
235 // ----------------------------------------------------------------------------
236
237 bool wxGDIImage::FreeResource( bool WXUNUSED(bForce) )
238 {
239 if ( !IsNull() )
240 {
241 GetGDIImageData()->Free();
242 GetGDIImageData()->m_hHandle = 0;
243 }
244
245 return true;
246 }
247
248 WXHANDLE wxGDIImage::GetResourceHandle()
249 {
250 return GetHandle();
251 }
252
253 // ----------------------------------------------------------------------------
254 // wxGDIImage handler stuff
255 // ----------------------------------------------------------------------------
256
257 void wxGDIImage::AddHandler( wxGDIImageHandler* pHandler )
258 {
259 ms_handlers.Append(pHandler);
260 }
261
262 void wxGDIImage::InsertHandler( wxGDIImageHandler* pHandler )
263 {
264 ms_handlers.Insert(pHandler);
265 }
266
267 bool wxGDIImage::RemoveHandler( const wxString& rName )
268 {
269 wxGDIImageHandler* pHandler = FindHandler(rName);
270
271 if (pHandler)
272 {
273 ms_handlers.DeleteObject(pHandler);
274 return true;
275 }
276 else
277 return false;
278 }
279
280 wxGDIImageHandler* wxGDIImage::FindHandler(
281 const wxString& rName
282 )
283 {
284 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
285
286 while ( pNode )
287 {
288 wxGDIImageHandler* pHandler = pNode->GetData();
289
290 if ( pHandler->GetName() == rName )
291 return pHandler;
292 pNode = pNode->GetNext();
293 }
294 return((wxGDIImageHandler*)NULL);
295 }
296
297 wxGDIImageHandler* wxGDIImage::FindHandler(
298 const wxString& rExtension
299 , long lType
300 )
301 {
302 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
303 while ( pNode )
304 {
305 wxGDIImageHandler* pHandler = pNode->GetData();
306
307 if ( (pHandler->GetExtension() = rExtension) &&
308 (lType == -1 || pHandler->GetType() == lType) )
309 {
310 return pHandler;
311 }
312 pNode = pNode->GetNext();
313 }
314 return((wxGDIImageHandler*)NULL);
315 }
316
317 wxGDIImageHandler* wxGDIImage::FindHandler(
318 long lType
319 )
320 {
321 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
322
323 while ( pNode )
324 {
325 wxGDIImageHandler* pHandler = pNode->GetData();
326
327 if ( pHandler->GetType() == lType )
328 return pHandler;
329 pNode = pNode->GetNext();
330 }
331 return((wxGDIImageHandler*)NULL);
332 }
333
334 void wxGDIImage::CleanUpHandlers()
335 {
336 wxGDIImageHandlerList::compatibility_iterator pNode = ms_handlers.GetFirst();
337
338 while ( pNode )
339 {
340 wxGDIImageHandler* pHandler = pNode->GetData();
341 wxGDIImageHandlerList::compatibility_iterator pNext = pNode->GetNext();
342
343 delete pHandler;
344 ms_handlers.Erase( pNode );
345 pNode = pNext;
346 }
347 }
348
349 void wxGDIImage::InitStandardHandlers()
350 {
351 AddHandler(new wxBMPResourceHandler);
352 AddHandler(new wxBMPFileHandler);
353 AddHandler(new wxICOResourceHandler);
354 AddHandler(new wxICOFileHandler);
355 }
356
357 // ----------------------------------------------------------------------------
358 // wxBitmap handlers
359 // ----------------------------------------------------------------------------
360
361 bool wxBMPResourceHandler::LoadFile( wxBitmap* pBitmap,
362 int nId,
363 long WXUNUSED(lFlags),
364 int WXUNUSED(nDesiredWidth),
365 int WXUNUSED(nDesiredHeight) )
366 {
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() )
384 {
385 BITMAPINFOHEADER vBmph;
386
387 ::GpiQueryBitmapParameters(GetHbitmapOf(*pBitmap), &vBmph);
388 pData->m_nWidth = vBmph.cx;
389 pData->m_nHeight = vBmph.cy;
390 pData->m_nDepth = vBmph.cBitCount;
391 }
392 return(pBitmap->Ok());
393 } // end of wxBMPResourceHandler::LoadFile
394
395 bool 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) )
401 {
402 #if wxUSE_IMAGE_LOADING_IN_OS2
403 wxPalette* pPalette = NULL;
404
405 bool bSuccess = false; /* wxLoadIntoBitmap( WXSTRINGCAST rName
406 ,pBitmap
407 ,&pPalette
408 ) != 0; */
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
419 wxUnusedVar(pBitmap);
420 return false;
421 #endif
422 }
423
424 bool wxBMPFileHandler::SaveFile( wxBitmap* pBitmap,
425 const wxString& WXUNUSED(rName),
426 int WXUNUSED(nType),
427 const wxPalette* pPal )
428 {
429 #if wxUSE_IMAGE_LOADING_IN_OS2
430 wxPalette* pActualPalette = (wxPalette *)pPal;
431
432 if (!pActualPalette)
433 pActualPalette = pBitmap->GetPalette();
434 /* return(wxSaveBitmap( WXSTRINGCAST rName
435 ,pBitmap
436 ,pActualPalette
437 ) != 0); */
438 return false;
439 #else
440 wxUnusedVar(pBitmap);
441 wxUnusedVar(pPal);
442 return false;
443 #endif
444 }
445
446 // ----------------------------------------------------------------------------
447 // wxIcon handlers
448 // ----------------------------------------------------------------------------
449
450 bool 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) )
456 {
457 #if wxUSE_RESOURCE_LOADING_IN_OS2
458 pIcon->UnRef();
459
460 return false;
461 #else
462 wxUnusedVar(pIcon);
463 return false;
464 #endif
465 }
466
467 bool wxICOResourceHandler::LoadIcon( wxIcon* pIcon,
468 const wxString& rName,
469 HPS WXUNUSED(hPs),
470 long WXUNUSED(lFlags),
471 int WXUNUSED(nDesiredWidth),
472 int WXUNUSED(nDesiredHeight) )
473 {
474 HPOINTER hIcon;
475
476 hIcon = ::WinLoadFileIcon( (PSZ)rName.c_str()
477 ,TRUE // load for private use
478 );
479
480 pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32
481
482 pIcon->SetHICON((WXHICON)hIcon);
483
484 return pIcon->Ok();
485 } // end of wxICOResourceHandler::LoadIcon