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