added support for gcc precompiled headers
[wxWidgets.git] / src / msw / 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "gdiimage.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #endif // WX_PRECOMP
35
36 #include "wx/msw/private.h"
37
38 #include "wx/app.h"
39
40 #include "wx/bitmap.h"
41 #include "wx/msw/gdiimage.h"
42
43 #if wxUSE_WXDIB
44 #include "wx/msw/dib.h"
45 #endif
46
47 #ifdef __WXWINCE__
48 #include <winreg.h>
49 #include <shellapi.h>
50 #endif
51
52 #include "wx/listimpl.cpp"
53 WX_DEFINE_LIST(wxGDIImageHandlerList);
54
55 // ----------------------------------------------------------------------------
56 // private classes
57 // ----------------------------------------------------------------------------
58
59 #ifndef __WXMICROWIN__
60
61 // all image handlers are declared/defined in this file because the outside
62 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
63
64 class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler
65 {
66 public:
67 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
68 wxBITMAP_TYPE_BMP)
69 {
70 }
71
72 virtual bool LoadFile(wxBitmap *bitmap,
73 const wxString& name, long flags,
74 int desiredWidth, int desiredHeight);
75 virtual bool SaveFile(wxBitmap *bitmap,
76 const wxString& name, int type,
77 const wxPalette *palette = NULL);
78
79 private:
80 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
81 };
82
83 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
84 {
85 public:
86 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
87 wxEmptyString,
88 wxBITMAP_TYPE_BMP_RESOURCE)
89 {
90 }
91
92 virtual bool LoadFile(wxBitmap *bitmap,
93 const wxString& name, long flags,
94 int desiredWidth, int desiredHeight);
95
96 private:
97 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
98 };
99
100 class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler
101 {
102 public:
103 wxIconHandler(const wxString& name, const wxString& ext, long type)
104 : wxGDIImageHandler(name, ext, type)
105 {
106 }
107
108 // creating and saving icons is not supported
109 virtual bool Create(wxGDIImage *WXUNUSED(image),
110 void *WXUNUSED(data),
111 long WXUNUSED(flags),
112 int WXUNUSED(width),
113 int WXUNUSED(height),
114 int WXUNUSED(depth) = 1)
115 {
116 return false;
117 }
118
119 virtual bool Save(wxGDIImage *WXUNUSED(image),
120 const wxString& WXUNUSED(name),
121 int WXUNUSED(type))
122 {
123 return false;
124 }
125
126 virtual bool Load(wxGDIImage *image,
127 const wxString& name,
128 long flags,
129 int desiredWidth, int desiredHeight)
130 {
131 wxIcon *icon = wxDynamicCast(image, wxIcon);
132 wxCHECK_MSG( icon, false, _T("wxIconHandler only works with icons") );
133
134 return LoadIcon(icon, name, flags, desiredWidth, desiredHeight);
135 }
136
137 protected:
138 virtual bool LoadIcon(wxIcon *icon,
139 const wxString& name, long flags,
140 int desiredWidth = -1, int desiredHeight = -1) = 0;
141 };
142
143 class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
144 {
145 public:
146 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
147 _T("ico"),
148 wxBITMAP_TYPE_ICO)
149 {
150 }
151
152 virtual bool LoadIcon(wxIcon *icon,
153 const wxString& name, long flags,
154 int desiredWidth = -1, int desiredHeight = -1);
155
156 private:
157 DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
158 };
159
160 class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
161 {
162 public:
163 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
164 _T("ico"),
165 wxBITMAP_TYPE_ICO_RESOURCE)
166 {
167 }
168
169 virtual bool LoadIcon(wxIcon *icon,
170 const wxString& name, long flags,
171 int desiredWidth = -1, int desiredHeight = -1);
172
173 private:
174 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler)
175 };
176
177 // ----------------------------------------------------------------------------
178 // wxWin macros
179 // ----------------------------------------------------------------------------
180
181 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
182 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
183 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
184 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
185
186 // ----------------------------------------------------------------------------
187 // private functions
188 // ----------------------------------------------------------------------------
189
190 #endif
191 // __MICROWIN__
192
193 // ============================================================================
194 // implementation
195 // ============================================================================
196
197 wxGDIImageHandlerList wxGDIImage::ms_handlers;
198
199 // ----------------------------------------------------------------------------
200 // wxGDIImage functions forwarded to wxGDIImageRefData
201 // ----------------------------------------------------------------------------
202
203 bool wxGDIImage::FreeResource(bool WXUNUSED(force))
204 {
205 if ( !IsNull() )
206 {
207 GetGDIImageData()->Free();
208 GetGDIImageData()->m_handle = 0;
209 }
210
211 return true;
212 }
213
214 WXHANDLE wxGDIImage::GetResourceHandle() const
215 {
216 return GetHandle();
217 }
218
219 // ----------------------------------------------------------------------------
220 // wxGDIImage handler stuff
221 // ----------------------------------------------------------------------------
222
223 void wxGDIImage::AddHandler(wxGDIImageHandler *handler)
224 {
225 ms_handlers.Append(handler);
226 }
227
228 void wxGDIImage::InsertHandler(wxGDIImageHandler *handler)
229 {
230 ms_handlers.Insert(handler);
231 }
232
233 bool wxGDIImage::RemoveHandler(const wxString& name)
234 {
235 wxGDIImageHandler *handler = FindHandler(name);
236 if ( handler )
237 {
238 ms_handlers.DeleteObject(handler);
239 return true;
240 }
241 else
242 return false;
243 }
244
245 wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& name)
246 {
247 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
248 while ( node )
249 {
250 wxGDIImageHandler *handler = node->GetData();
251 if ( handler->GetName() == name )
252 return handler;
253 node = node->GetNext();
254 }
255
256 return NULL;
257 }
258
259 wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& extension,
260 long type)
261 {
262 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
263 while ( node )
264 {
265 wxGDIImageHandler *handler = node->GetData();
266 if ( (handler->GetExtension() = extension) &&
267 (type == -1 || handler->GetType() == type) )
268 {
269 return handler;
270 }
271
272 node = node->GetNext();
273 }
274 return NULL;
275 }
276
277 wxGDIImageHandler *wxGDIImage::FindHandler(long type)
278 {
279 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
280 while ( node )
281 {
282 wxGDIImageHandler *handler = node->GetData();
283 if ( handler->GetType() == type )
284 return handler;
285
286 node = node->GetNext();
287 }
288
289 return NULL;
290 }
291
292 void wxGDIImage::CleanUpHandlers()
293 {
294 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
295 while ( node )
296 {
297 wxGDIImageHandler *handler = node->GetData();
298 wxGDIImageHandlerList::compatibility_iterator next = node->GetNext();
299 delete handler;
300 ms_handlers.Erase( node );
301 node = next;
302 }
303 }
304
305 void wxGDIImage::InitStandardHandlers()
306 {
307 #ifndef __WXMICROWIN__
308 AddHandler(new wxBMPResourceHandler);
309 AddHandler(new wxBMPFileHandler);
310 AddHandler(new wxICOResourceHandler);
311 AddHandler(new wxICOFileHandler);
312 #endif
313 }
314
315 #ifndef __WXMICROWIN__
316
317 // ----------------------------------------------------------------------------
318 // wxBitmap handlers
319 // ----------------------------------------------------------------------------
320
321 bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap,
322 const wxString& name, long WXUNUSED(flags),
323 int WXUNUSED(desiredWidth),
324 int WXUNUSED(desiredHeight))
325 {
326 // TODO: load colourmap.
327 bitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), name));
328
329 if ( !bitmap->Ok() )
330 {
331 // it's probably not found
332 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
333 name.c_str());
334
335 return false;
336 }
337
338 BITMAP bm;
339 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(BITMAP), (LPSTR) &bm) )
340 {
341 wxLogLastError(wxT("GetObject(HBITMAP)"));
342 }
343
344 bitmap->SetWidth(bm.bmWidth);
345 bitmap->SetHeight(bm.bmHeight);
346 bitmap->SetDepth(bm.bmBitsPixel);
347
348 return true;
349 }
350
351 bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap,
352 const wxString& name, long WXUNUSED(flags),
353 int WXUNUSED(desiredWidth),
354 int WXUNUSED(desiredHeight))
355 {
356 #if wxUSE_WXDIB
357 wxCHECK_MSG( bitmap, false, _T("NULL bitmap in LoadFile") );
358
359 wxDIB dib(name);
360
361 return dib.IsOk() && bitmap->CopyFromDIB(dib);
362 #else
363 return FALSE;
364 #endif
365 }
366
367 bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap,
368 const wxString& name,
369 int WXUNUSED(type),
370 const wxPalette * WXUNUSED(pal))
371 {
372 #if wxUSE_WXDIB
373 wxCHECK_MSG( bitmap, false, _T("NULL bitmap in SaveFile") );
374
375 wxDIB dib(*bitmap);
376
377 return dib.Save(name);
378 #else
379 return FALSE;
380 #endif
381 }
382
383 // ----------------------------------------------------------------------------
384 // wxIcon handlers
385 // ----------------------------------------------------------------------------
386
387 bool wxICOFileHandler::LoadIcon(wxIcon *icon,
388 const wxString& name,
389 long WXUNUSED(flags),
390 int desiredWidth, int desiredHeight)
391 {
392 icon->UnRef();
393
394 // actual size
395 wxSize size;
396
397 HICON hicon = NULL;
398
399 // Parse the filename: it may be of the form "filename;n" in order to
400 // specify the nth icon in the file.
401 //
402 // For the moment, ignore the issue of possible semicolons in the
403 // filename.
404 int iconIndex = 0;
405 wxString nameReal(name);
406 wxString strIconIndex = name.AfterLast(wxT(';'));
407 if (strIconIndex != name)
408 {
409 iconIndex = wxAtoi(strIconIndex);
410 nameReal = name.BeforeLast(wxT(';'));
411 }
412
413 #if 0
414 // If we don't know what size icon we're looking for,
415 // try to find out what's there.
416 // Unfortunately this doesn't work, because ExtractIconEx
417 // will scale the icon to the 'desired' size, even if that
418 // size of icon isn't explicitly stored. So we would have
419 // to parse the icon file outselves.
420 if ( desiredWidth == -1 &&
421 desiredHeight == -1)
422 {
423 // Try loading a large icon first
424 if ( ::ExtractIconEx(nameReal, iconIndex, &hicon, NULL, 1) == 1)
425 {
426 }
427 // Then try loading a small icon
428 else if ( ::ExtractIconEx(nameReal, iconIndex, NULL, &hicon, 1) == 1)
429 {
430 }
431 }
432 else
433 #endif
434 // were we asked for a large icon?
435 if ( desiredWidth == ::GetSystemMetrics(SM_CXICON) &&
436 desiredHeight == ::GetSystemMetrics(SM_CYICON) )
437 {
438 // get the specified large icon from file
439 if ( !::ExtractIconEx(nameReal, iconIndex, &hicon, NULL, 1) )
440 {
441 // it is not an error, but it might still be useful to be informed
442 // about it optionally
443 wxLogTrace(_T("iconload"),
444 _T("No large icons found in the file '%s'."),
445 name.c_str());
446 }
447 }
448 else if ( desiredWidth == ::GetSystemMetrics(SM_CXSMICON) &&
449 desiredHeight == ::GetSystemMetrics(SM_CYSMICON) )
450 {
451 // get the specified small icon from file
452 if ( !::ExtractIconEx(nameReal, iconIndex, NULL, &hicon, 1) )
453 {
454 wxLogTrace(_T("iconload"),
455 _T("No small icons found in the file '%s'."),
456 name.c_str());
457 }
458 }
459 //else: not standard size, load below
460
461 #ifndef __WXWINCE__
462 if ( !hicon )
463 {
464 // take any size icon from the file by index
465 hicon = ::ExtractIcon(wxGetInstance(), nameReal, iconIndex);
466 }
467 #endif
468
469 if ( !hicon )
470 {
471 wxLogSysError(_T("Failed to load icon from the file '%s'"),
472 name.c_str());
473
474 return false;
475 }
476
477 size = wxGetHiconSize(hicon);
478
479 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
480 (desiredHeight != -1 && desiredHeight != size.y) )
481 {
482 wxLogTrace(_T("iconload"),
483 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
484 size.x, size.y,
485 desiredWidth, desiredHeight);
486
487 ::DestroyIcon(hicon);
488
489 return false;
490 }
491
492 icon->SetHICON((WXHICON)hicon);
493 icon->SetSize(size.x, size.y);
494
495 return icon->Ok();
496 }
497
498 bool wxICOResourceHandler::LoadIcon(wxIcon *icon,
499 const wxString& name,
500 long WXUNUSED(flags),
501 int desiredWidth, int desiredHeight)
502 {
503 HICON hicon;
504
505 // do we need the icon of the specific size or would any icon do?
506 bool hasSize = desiredWidth != -1 || desiredHeight != -1;
507
508 wxASSERT_MSG( !hasSize || (desiredWidth != -1 && desiredHeight != -1),
509 _T("width and height should be either both -1 or not") );
510
511 // try to load the icon from this program first to allow overriding the
512 // standard icons (although why one would want to do it considering that
513 // we already have wxApp::GetStdIcon() is unclear)
514
515 // note that we can't just always call LoadImage() because it seems to do
516 // some icon rescaling internally which results in very ugly 16x16 icons
517 if ( hasSize )
518 {
519 hicon = (HICON)::LoadImage(wxGetInstance(), name, IMAGE_ICON,
520 desiredWidth, desiredHeight,
521 LR_DEFAULTCOLOR);
522 }
523 else
524 {
525 hicon = ::LoadIcon(wxGetInstance(), name);
526 }
527
528 // next check if it's not a standard icon
529 #ifndef __WXWINCE__
530 if ( !hicon && !hasSize )
531 {
532 static const struct
533 {
534 const wxChar *name;
535 LPTSTR id;
536 } stdIcons[] =
537 {
538 { wxT("wxICON_QUESTION"), IDI_QUESTION },
539 { wxT("wxICON_WARNING"), IDI_EXCLAMATION },
540 { wxT("wxICON_ERROR"), IDI_HAND },
541 { wxT("wxICON_INFORMATION"), IDI_ASTERISK },
542 };
543
544 for ( size_t nIcon = 0; !hicon && nIcon < WXSIZEOF(stdIcons); nIcon++ )
545 {
546 if ( name == stdIcons[nIcon].name )
547 {
548 hicon = ::LoadIcon((HINSTANCE)NULL, stdIcons[nIcon].id);
549 }
550 }
551 }
552 #endif
553
554 wxSize size = wxGetHiconSize(hicon);
555 icon->SetSize(size.x, size.y);
556
557 icon->SetHICON((WXHICON)hicon);
558
559 return icon->Ok();
560 }
561
562 // ----------------------------------------------------------------------------
563 // private functions
564 // ----------------------------------------------------------------------------
565
566 wxSize wxGetHiconSize(HICON hicon)
567 {
568 wxSize size(32, 32); // default
569 #ifndef __WXWINCE__
570 if ( hicon && wxGetOsVersion() != wxWIN32S )
571 {
572 ICONINFO info;
573 if ( !::GetIconInfo(hicon, &info) )
574 {
575 wxLogLastError(wxT("GetIconInfo"));
576 }
577 else
578 {
579 HBITMAP hbmp = info.hbmMask;
580 if ( hbmp )
581 {
582 BITMAP bm;
583 if ( ::GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm) )
584 {
585 size = wxSize(bm.bmWidth, bm.bmHeight);
586 }
587
588 ::DeleteObject(info.hbmMask);
589 }
590 if ( info.hbmColor )
591 ::DeleteObject(info.hbmColor);
592 }
593 }
594 #endif
595 return size;
596 }
597
598 #endif // __WXMICROWIN__
599