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