]> git.saurik.com Git - wxWidgets.git/blob - src/os2/gdiimage.cpp
no message
[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 license
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
34 #include "wx/app.h"
35
36 #include "wx/os2/gdiimage.h"
37
38 // ----------------------------------------------------------------------------
39 // private classes
40 // ----------------------------------------------------------------------------
41
42 // all image handlers are declared/defined in this file because the outside
43 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
44
45 class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler
46 {
47 public:
48 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
49 wxBITMAP_TYPE_BMP)
50 {
51 }
52
53 virtual bool LoadFile( wxBitmap* pBitmap
54 ,const wxString& rName
55 ,HPS hPs
56 ,long lFlags
57 ,int nDesiredWidth
58 ,int nDesiredHeight
59 );
60 virtual bool SaveFile( wxBitmap* pBitmap
61 ,const wxString& rName
62 ,int lType
63 ,const wxPalette* pPalette = NULL
64 );
65
66 private:
67 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
68 };
69
70 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
71 {
72 public:
73 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
74 wxEmptyString,
75 wxBITMAP_TYPE_BMP_RESOURCE)
76 {
77 }
78
79 virtual bool LoadFile( wxBitmap* pBitmap
80 ,const wxString& rName
81 ,HPS hPs
82 ,long lFlags
83 ,int nDesiredWidth
84 ,int nDesiredHeight
85 );
86
87 private:
88 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
89 };
90
91 class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler
92 {
93 public:
94 wxIconHandler( const wxString& rName
95 ,const wxString& rExt
96 ,long lType
97 ) : wxGDIImageHandler( rName
98 ,rExt
99 ,lType
100 )
101 {
102 }
103
104 // creating and saving icons is not supported
105 virtual bool Create( wxGDIImage* WXUNUSED(pImage)
106 ,void* WXUNUSED(pData)
107 ,long WXUNUSED(lFlags)
108 ,int WXUNUSED(nWidth)
109 ,int WXUNUSED(nHeight)
110 ,int WXUNUSED(nDepth) = 1
111 )
112 {
113 return(FALSE);
114 }
115
116 virtual bool Save( wxGDIImage* WXUNUSED(pImage)
117 ,const wxString& WXUNUSED(rName)
118 ,int WXUNUSED(nType)
119 )
120 {
121 return(FALSE);
122 }
123 virtual bool Load( wxGDIImage* pImage
124 ,const wxString& rName
125 ,HPS hPs
126 ,long lFlags
127 ,int nDesiredWidth
128 ,int nDesiredHeight
129 )
130 {
131 wxIcon* pIcon = wxDynamicCast(pImage, wxIcon);
132 wxCHECK_MSG(pIcon, FALSE, _T("wxIconHandler only works with icons"));
133
134 return LoadIcon( pIcon
135 ,rName
136 ,hPs
137 ,lFlags
138 ,nDesiredWidth
139 ,nDesiredHeight
140 );
141 }
142
143 protected:
144 virtual bool LoadIcon( wxIcon* pIcon
145 ,const wxString& rName
146 ,HPS hPs
147 ,long lFlags
148 ,int nDesiredWidth = -1
149 ,int nDesiredHeight = -1
150 ) = 0;
151 };
152
153 class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
154 {
155 public:
156 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
157 _T("ico"),
158 wxBITMAP_TYPE_ICO)
159 {
160 }
161
162 virtual bool LoadIcon( wxIcon * pIcon
163 ,const wxString& rName
164 ,HPS hPs
165 ,long lFlags
166 ,int nDesiredWidth = -1
167 ,int nDesiredHeight = -1
168 );
169
170 private:
171 DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
172 };
173
174 class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
175 {
176 public:
177 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
178 _T("ico"),
179 wxBITMAP_TYPE_ICO_RESOURCE)
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(wxICOResourceHandler)
193 };
194
195 // ----------------------------------------------------------------------------
196 // wxWin macros
197 // ----------------------------------------------------------------------------
198
199 #if !USE_SHARED_LIBRARIES
200 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
201 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
202 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxGDIImageHandler)
203 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxGDIImageHandler)
204 #endif
205
206 // ----------------------------------------------------------------------------
207 // private functions
208 // ----------------------------------------------------------------------------
209
210 static wxSize GetHiconSize(WXHICON hicon);
211
212 // ============================================================================
213 // implementation
214 // ============================================================================
215
216 wxList wxGDIImage::ms_handlers;
217
218 // ----------------------------------------------------------------------------
219 // wxGDIImage functions forwarded to wxGDIImageRefData
220 // ----------------------------------------------------------------------------
221
222 bool wxGDIImage::FreeResource(
223 bool WXUNUSED(bForce)
224 )
225 {
226 if ( !IsNull() )
227 {
228 GetGDIImageData()->Free();
229 GetGDIImageData()->m_hHandle = 0;
230 }
231
232 return(TRUE);
233 }
234
235 WXHANDLE wxGDIImage::GetResourceHandle()
236 {
237 return GetHandle();
238 }
239
240 // ----------------------------------------------------------------------------
241 // wxGDIImage handler stuff
242 // ----------------------------------------------------------------------------
243
244 void wxGDIImage::AddHandler(
245 wxGDIImageHandler* pHandler
246 )
247 {
248 ms_handlers.Append(pHandler);
249 }
250
251 void wxGDIImage::InsertHandler(
252 wxGDIImageHandler* pHandler
253 )
254 {
255 ms_handlers.Insert(pHandler);
256 }
257
258 bool wxGDIImage::RemoveHandler(
259 const wxString& rName
260 )
261 {
262 wxGDIImageHandler* pHandler = FindHandler(rName);
263
264 if (pHandler)
265 {
266 ms_handlers.DeleteObject(pHandler);
267 return(TRUE);
268 }
269 else
270 return(FALSE);
271 }
272
273 wxGDIImageHandler* wxGDIImage::FindHandler(
274 const wxString& rName
275 )
276 {
277 wxNode* pNode = ms_handlers.First();
278
279 while (pNode)
280 {
281 wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data();
282
283 if (pHandler->GetName() == rName)
284 return(pHandler);
285 pNode = pNode->Next();
286 }
287 return(NULL);
288 }
289
290 wxGDIImageHandler* wxGDIImage::FindHandler(
291 const wxString& rExtension
292 , long lType
293 )
294 {
295 wxNode* pNode = ms_handlers.First();
296
297 while (pNode)
298 {
299 wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data();
300
301 if ((pHandler->GetExtension() = rExtension) &&
302 (lType == -1 || pHandler->GetType() == lType))
303 {
304 return(pHandler);
305 }
306 pNode = pNode->Next();
307 }
308 return(NULL);
309 }
310
311 wxGDIImageHandler* wxGDIImage::FindHandler(
312 long lType
313 )
314 {
315 wxNode* pNode = ms_handlers.First();
316
317 while (pNode)
318 {
319 wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data();
320
321 if (pHandler->GetType() == lType)
322 return pHandler;
323 pNode = pNode->Next();
324 }
325 return(NULL);
326 }
327
328 void wxGDIImage::CleanUpHandlers()
329 {
330 wxNode* pNode = ms_handlers.First();
331
332 while (pNode)
333 {
334 wxGDIImageHandler* pHandler = (wxGDIImageHandler *)pNode->Data();
335 wxNode* pNext = pNode->Next();
336
337 delete pHandler;
338 delete pNode;
339 pNode = pNext;
340 }
341 }
342
343 void wxGDIImage::InitStandardHandlers()
344 {
345 AddHandler(new wxBMPResourceHandler);
346 AddHandler(new wxBMPFileHandler);
347
348 // Not added by default: include xpmhand.h in your app
349 // and call these in your wxApp::OnInit.
350 // AddHandler(new wxXPMFileHandler);
351 // AddHandler(new wxXPMDataHandler);
352
353 AddHandler(new wxICOResourceHandler);
354 AddHandler(new wxICOFileHandler);
355 }
356
357 // ----------------------------------------------------------------------------
358 // wxBitmap handlers
359 // ----------------------------------------------------------------------------
360
361 bool wxBMPResourceHandler::LoadFile(
362 wxBitmap* pBitmap
363 , const wxString& rName
364 , HPS hPs
365 , long lFlags
366 , int nDesiredWidth
367 , int nDesiredHeight
368 )
369 {
370 // TODO: load a bitmap from a file
371 /*
372 rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName));
373
374 wxBitmapRefData* pData = bitmap->GetBitmapData();
375
376 if (pBitmap->Ok())
377 {
378 BITMAP bm;
379
380 if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) )
381 {
382 wxLogLastError("GetObject(HBITMAP)");
383 }
384
385 data->m_width = bm.bmWidth;
386 data->m_height = bm.bmHeight;
387 data->m_depth = bm.bmBitsPixel;
388 }
389 else
390 {
391 // it's probably not found
392 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
393 name.c_str());
394 }
395
396 return bitmap->Ok();
397 */
398 return(FALSE);
399 }
400
401 bool wxBMPFileHandler::LoadFile(
402 wxBitmap* pBitmap
403 , const wxString& rName
404 , HPS hPs
405 , long WXUNUSED(lFlags)
406 , int WXUNUSED(nDesiredWidth)
407 , int WXUNUSED(nDesiredHeight)
408 )
409 {
410 #if wxUSE_IMAGE_LOADING_IN_OS2
411 wxPalette* pPalette = NULL;
412
413 bool bSuccess = FALSE; /* wxLoadIntoBitmap( WXSTRINGCAST rName
414 ,pBitmap
415 ,&pPalette
416 ) != 0; */
417 if (bSuccess && pPalette)
418 {
419 pBitmap->SetPalette(*pPalette);
420 }
421
422 // it was copied by the bitmap if it was loaded successfully
423 delete pPalette;
424
425 return(bSuccess);
426 #else
427 return(FALSE);
428 #endif
429 }
430
431 bool wxBMPFileHandler::SaveFile(
432 wxBitmap* pBitmap
433 , const wxString& rName
434 , int WXUNUSED(nType)
435 , const wxPalette* pPal
436 )
437 {
438 #if wxUSE_IMAGE_LOADING_IN_OS2
439 wxPalette* pActualPalette = (wxPalette *)pPal;
440
441 if (!pActualPalette)
442 pActualPalette = pBitmap->GetPalette();
443 /* return(wxSaveBitmap( WXSTRINGCAST rName
444 ,pBitmap
445 ,pActualPalette
446 ) != 0); */
447 return(FALSE);
448 #else
449 return(FALSE);
450 #endif
451 }
452
453 // ----------------------------------------------------------------------------
454 // wxIcon handlers
455 // ----------------------------------------------------------------------------
456
457 bool wxICOFileHandler::LoadIcon(
458 wxIcon* pIcon
459 , const wxString& rName
460 , HPS hPs
461 , long lFlags
462 , int nDesiredWidth
463 , int nDesiredHeight
464 )
465 {
466 #if wxUSE_RESOURCE_LOADING_IN_OS2
467 pIcon->UnRef();
468
469 // actual size
470 wxSize vSize;
471
472 // TODO: load icon directly from a file
473 /*
474 #ifdef __WIN32__
475 HICON hicon = ::ExtractIcon(wxGetInstance(), name, first);
476 if ( !hicon )
477 {
478 wxLogSysError(_T("Failed to load icon from the file '%s'"),
479 name.c_str());
480
481 return FALSE;
482 }
483
484 size = GetHiconSize(hicon);
485 #else // Win16
486 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
487 wxGetInstance(),
488 &size.x, &size.y);
489 #endif // Win32/Win16
490
491 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
492 (desiredHeight != -1 && desiredHeight != size.y) )
493 {
494 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
495 "of the size mismatch: actual (%d, %d), "
496 "requested (%d, %d)"),
497 size.x, size.y,
498 desiredWidth, desiredHeight);
499
500 ::DestroyIcon(hicon);
501
502 return FALSE;
503 }
504
505 icon->SetHICON((WXHICON)hicon);
506 icon->SetSize(size.x, size.y);
507
508 return icon->Ok();
509 */
510 return(FALSE);
511 #else
512 return(FALSE);
513 #endif
514 }
515
516 bool wxICOResourceHandler::LoadIcon(
517 wxIcon* pIcon
518 , const wxString& rName
519 , HPS hPs
520 , long lFlags
521 , int nDesiredWidth
522 , int nDesiredHeight
523 )
524 {
525 // TODO: load icon from a file
526 /*
527 HICON hicon;
528
529 #if defined(__WIN32__) && !defined(__SC__)
530 if ( desiredWidth != -1 && desiredHeight != -1 )
531 {
532 hicon = (HICON)::LoadImage(wxGetInstance(), name, IMAGE_ICON,
533 desiredWidth, desiredHeight,
534 LR_DEFAULTCOLOR);
535 }
536 else
537 #endif // Win32
538 {
539 hicon = ::LoadIcon(wxGetInstance(), name);
540 }
541
542 wxSize size = GetHiconSize(hicon);
543 icon->SetSize(size.x, size.y);
544
545 // Override the found values with desired values
546 if ( desiredWidth > -1 && desiredHeight > -1 )
547 {
548 icon->SetSize(desiredWidth, desiredHeight);
549 }
550
551 icon->SetHICON((WXHICON)hicon);
552
553 return icon->Ok();
554 */
555 return(FALSE);
556 }
557
558 // ----------------------------------------------------------------------------
559 // private functions
560 // ----------------------------------------------------------------------------
561
562 static wxSize GetHiconSize(
563 WXHICON hicon
564 )
565 {
566 wxSize vSize(32, 32); // default
567
568 // all OS/2 icons are 32x32
569 return(vSize);
570 }
571