]> git.saurik.com Git - wxWidgets.git/blob - src/os2/gdiimage.cpp
Added chapter on collection and container classes to contents
[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 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
339 delete pNode;
340 #endif
341 pNode = pNext;
342 }
343 }
344
345 void wxGDIImage::InitStandardHandlers()
346 {
347 AddHandler(new wxBMPResourceHandler);
348 AddHandler(new wxBMPFileHandler);
349
350 // Not added by default: include xpmhand.h in your app
351 // and call these in your wxApp::OnInit.
352 // AddHandler(new wxXPMFileHandler);
353 // AddHandler(new wxXPMDataHandler);
354
355 AddHandler(new wxICOResourceHandler);
356 AddHandler(new wxICOFileHandler);
357 }
358
359 // ----------------------------------------------------------------------------
360 // wxBitmap handlers
361 // ----------------------------------------------------------------------------
362
363 bool wxBMPResourceHandler::LoadFile(
364 wxBitmap* pBitmap
365 , const wxString& rName
366 , HPS hPs
367 , long lFlags
368 , int nDesiredWidth
369 , int nDesiredHeight
370 )
371 {
372 // TODO: load a bitmap from a file
373 /*
374 rBitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), rName));
375
376 wxBitmapRefData* pData = bitmap->GetBitmapData();
377
378 if (pBitmap->Ok())
379 {
380 BITMAP bm;
381
382 if ( !::GetObject(GetHbitmapOf(*pBitmap), sizeof(BITMAP), (LPSTR) &bm) )
383 {
384 wxLogLastError("GetObject(HBITMAP)");
385 }
386
387 data->m_width = bm.bmWidth;
388 data->m_height = bm.bmHeight;
389 data->m_depth = bm.bmBitsPixel;
390 }
391 else
392 {
393 // it's probably not found
394 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
395 name.c_str());
396 }
397
398 return bitmap->Ok();
399 */
400 return(FALSE);
401 }
402
403 bool wxBMPFileHandler::LoadFile(
404 wxBitmap* pBitmap
405 , const wxString& rName
406 , HPS hPs
407 , long WXUNUSED(lFlags)
408 , int WXUNUSED(nDesiredWidth)
409 , int WXUNUSED(nDesiredHeight)
410 )
411 {
412 #if wxUSE_IMAGE_LOADING_IN_OS2
413 wxPalette* pPalette = NULL;
414
415 bool bSuccess = FALSE; /* wxLoadIntoBitmap( WXSTRINGCAST rName
416 ,pBitmap
417 ,&pPalette
418 ) != 0; */
419 if (bSuccess && pPalette)
420 {
421 pBitmap->SetPalette(*pPalette);
422 }
423
424 // it was copied by the bitmap if it was loaded successfully
425 delete pPalette;
426
427 return(bSuccess);
428 #else
429 return(FALSE);
430 #endif
431 }
432
433 bool wxBMPFileHandler::SaveFile(
434 wxBitmap* pBitmap
435 , const wxString& rName
436 , int WXUNUSED(nType)
437 , const wxPalette* pPal
438 )
439 {
440 #if wxUSE_IMAGE_LOADING_IN_OS2
441 wxPalette* pActualPalette = (wxPalette *)pPal;
442
443 if (!pActualPalette)
444 pActualPalette = pBitmap->GetPalette();
445 /* return(wxSaveBitmap( WXSTRINGCAST rName
446 ,pBitmap
447 ,pActualPalette
448 ) != 0); */
449 return(FALSE);
450 #else
451 return(FALSE);
452 #endif
453 }
454
455 // ----------------------------------------------------------------------------
456 // wxIcon handlers
457 // ----------------------------------------------------------------------------
458
459 bool wxICOFileHandler::LoadIcon(
460 wxIcon* pIcon
461 , const wxString& rName
462 , HPS hPs
463 , long lFlags
464 , int nDesiredWidth
465 , int nDesiredHeight
466 )
467 {
468 #if wxUSE_RESOURCE_LOADING_IN_OS2
469 pIcon->UnRef();
470
471 // actual size
472 wxSize vSize;
473
474 // TODO: load icon directly from a file
475 /*
476 #ifdef __WIN32__
477 HICON hicon = ::ExtractIcon(wxGetInstance(), name, first);
478 if ( !hicon )
479 {
480 wxLogSysError(_T("Failed to load icon from the file '%s'"),
481 name.c_str());
482
483 return FALSE;
484 }
485
486 size = GetHiconSize(hicon);
487 #else // Win16
488 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
489 wxGetInstance(),
490 &size.x, &size.y);
491 #endif // Win32/Win16
492
493 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
494 (desiredHeight != -1 && desiredHeight != size.y) )
495 {
496 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
497 "of the size mismatch: actual (%d, %d), "
498 "requested (%d, %d)"),
499 size.x, size.y,
500 desiredWidth, desiredHeight);
501
502 ::DestroyIcon(hicon);
503
504 return FALSE;
505 }
506
507 icon->SetHICON((WXHICON)hicon);
508 icon->SetSize(size.x, size.y);
509
510 return icon->Ok();
511 */
512 return(FALSE);
513 #else
514 return(FALSE);
515 #endif
516 }
517
518 bool wxICOResourceHandler::LoadIcon(
519 wxIcon* pIcon
520 , const wxString& rName
521 , HPS hPs
522 , long lFlags
523 , int WXUNUSED(nDesiredWidth)
524 , int WXUNUSED(nDesiredHeight)
525 )
526 {
527 HPOINTER hIcon;
528
529 hIcon = ::WinLoadFileIcon( (PSZ)rName.c_str()
530 ,TRUE // load for private use
531 );
532
533 pIcon->SetSize(32, 32); // all OS/2 icons are 32 x 32
534
535
536 pIcon->SetHICON((WXHICON)hIcon);
537
538 return pIcon->Ok();
539 } // end of wxICOResourceHandler::LoadIcon
540
541 // ----------------------------------------------------------------------------
542 // private functions
543 // ----------------------------------------------------------------------------
544
545 static wxSize GetHiconSize(
546 WXHICON hicon
547 )
548 {
549 wxSize vSize(32, 32); // default
550
551 // all OS/2 icons are 32x32
552 return(vSize);
553 }
554