]> git.saurik.com Git - wxWidgets.git/blame - src/msw/gdiimage.cpp
Small warning fixes.
[wxWidgets.git] / src / msw / gdiimage.cpp
CommitLineData
0d0512bd
VZ
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>
65571936 9// Licence: wxWindows licence
0d0512bd
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
0d0512bd
VZ
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"
f94dfb38 33 #include "wx/log.h"
0d0512bd
VZ
34#endif // WX_PRECOMP
35
e42a6c18
VZ
36#include "wx/msw/private.h"
37
0d0512bd
VZ
38#include "wx/app.h"
39
f66f0fd7 40#include "wx/bitmap.h"
474df218 41#include "wx/msw/gdiimage.h"
4676948b
JS
42
43#if wxUSE_WXDIB
474df218 44#include "wx/msw/dib.h"
4676948b
JS
45#endif
46
47#ifdef __WXWINCE__
48#include <winreg.h>
49#include <shellapi.h>
50#endif
f66f0fd7 51
419430a0
JS
52#include "wx/file.h"
53
d162a7ee
VZ
54#include "wx/listimpl.cpp"
55WX_DEFINE_LIST(wxGDIImageHandlerList);
56
0d0512bd
VZ
57// ----------------------------------------------------------------------------
58// private classes
59// ----------------------------------------------------------------------------
60
04ef50df
JS
61#ifndef __WXMICROWIN__
62
0d0512bd
VZ
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
66class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler
67{
68public:
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
81private:
82 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
83};
84
85class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
86{
87public:
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
98private:
99 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
100};
101
102class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler
103{
104public:
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 {
4f8090e0 118 return false;
0d0512bd
VZ
119 }
120
121 virtual bool Save(wxGDIImage *WXUNUSED(image),
122 const wxString& WXUNUSED(name),
123 int WXUNUSED(type))
124 {
4f8090e0 125 return false;
0d0512bd
VZ
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);
4f8090e0 134 wxCHECK_MSG( icon, false, _T("wxIconHandler only works with icons") );
0d0512bd
VZ
135
136 return LoadIcon(icon, name, flags, desiredWidth, desiredHeight);
137 }
138
139protected:
140 virtual bool LoadIcon(wxIcon *icon,
141 const wxString& name, long flags,
142 int desiredWidth = -1, int desiredHeight = -1) = 0;
143};
144
145class WXDLLEXPORT wxICOFileHandler : public wxIconHandler
146{
147public:
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
158private:
159 DECLARE_DYNAMIC_CLASS(wxICOFileHandler)
160};
161
162class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler
163{
164public:
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
175private:
176 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler)
177};
178
179// ----------------------------------------------------------------------------
180// wxWin macros
181// ----------------------------------------------------------------------------
182
621b3e21
VZ
183IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
184IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
185IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject)
186IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject)
0d0512bd
VZ
187
188// ----------------------------------------------------------------------------
189// private functions
190// ----------------------------------------------------------------------------
191
04ef50df
JS
192#endif
193 // __MICROWIN__
0d0512bd
VZ
194
195// ============================================================================
196// implementation
197// ============================================================================
198
d162a7ee 199wxGDIImageHandlerList wxGDIImage::ms_handlers;
0d0512bd
VZ
200
201// ----------------------------------------------------------------------------
202// wxGDIImage functions forwarded to wxGDIImageRefData
203// ----------------------------------------------------------------------------
204
205bool wxGDIImage::FreeResource(bool WXUNUSED(force))
206{
207 if ( !IsNull() )
208 {
209 GetGDIImageData()->Free();
210 GetGDIImageData()->m_handle = 0;
211 }
212
4f8090e0 213 return true;
0d0512bd
VZ
214}
215
2b5f62a0 216WXHANDLE wxGDIImage::GetResourceHandle() const
0d0512bd
VZ
217{
218 return GetHandle();
219}
220
221// ----------------------------------------------------------------------------
222// wxGDIImage handler stuff
223// ----------------------------------------------------------------------------
224
225void wxGDIImage::AddHandler(wxGDIImageHandler *handler)
226{
227 ms_handlers.Append(handler);
228}
229
230void wxGDIImage::InsertHandler(wxGDIImageHandler *handler)
231{
232 ms_handlers.Insert(handler);
233}
234
235bool wxGDIImage::RemoveHandler(const wxString& name)
236{
237 wxGDIImageHandler *handler = FindHandler(name);
238 if ( handler )
239 {
240 ms_handlers.DeleteObject(handler);
4f8090e0 241 return true;
0d0512bd
VZ
242 }
243 else
4f8090e0 244 return false;
0d0512bd
VZ
245}
246
247wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& name)
248{
222ed1d6 249 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
250 while ( node )
251 {
d162a7ee 252 wxGDIImageHandler *handler = node->GetData();
0d0512bd
VZ
253 if ( handler->GetName() == name )
254 return handler;
4f8090e0 255 node = node->GetNext();
0d0512bd
VZ
256 }
257
258 return NULL;
259}
260
261wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& extension,
262 long type)
263{
222ed1d6 264 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
265 while ( node )
266 {
d162a7ee 267 wxGDIImageHandler *handler = node->GetData();
0d0512bd
VZ
268 if ( (handler->GetExtension() = extension) &&
269 (type == -1 || handler->GetType() == type) )
270 {
271 return handler;
272 }
273
4f8090e0 274 node = node->GetNext();
0d0512bd
VZ
275 }
276 return NULL;
277}
278
279wxGDIImageHandler *wxGDIImage::FindHandler(long type)
280{
222ed1d6 281 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
282 while ( node )
283 {
d162a7ee 284 wxGDIImageHandler *handler = node->GetData();
0d0512bd
VZ
285 if ( handler->GetType() == type )
286 return handler;
287
4f8090e0 288 node = node->GetNext();
0d0512bd
VZ
289 }
290
291 return NULL;
292}
293
294void wxGDIImage::CleanUpHandlers()
295{
222ed1d6 296 wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst();
0d0512bd
VZ
297 while ( node )
298 {
d162a7ee 299 wxGDIImageHandler *handler = node->GetData();
222ed1d6 300 wxGDIImageHandlerList::compatibility_iterator next = node->GetNext();
0d0512bd 301 delete handler;
222ed1d6 302 ms_handlers.Erase( node );
0d0512bd
VZ
303 node = next;
304 }
305}
306
307void wxGDIImage::InitStandardHandlers()
308{
04ef50df 309#ifndef __WXMICROWIN__
0d0512bd
VZ
310 AddHandler(new wxBMPResourceHandler);
311 AddHandler(new wxBMPFileHandler);
0d0512bd
VZ
312 AddHandler(new wxICOResourceHandler);
313 AddHandler(new wxICOFileHandler);
04ef50df 314#endif
0d0512bd
VZ
315}
316
04ef50df
JS
317#ifndef __WXMICROWIN__
318
0d0512bd
VZ
319// ----------------------------------------------------------------------------
320// wxBitmap handlers
321// ----------------------------------------------------------------------------
322
323bool 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
8bbbae21 331 if ( !bitmap->Ok() )
0d0512bd
VZ
332 {
333 // it's probably not found
334 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
335 name.c_str());
8bbbae21 336
4f8090e0 337 return false;
8bbbae21
VZ
338 }
339
340 BITMAP bm;
341 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(BITMAP), (LPSTR) &bm) )
342 {
343 wxLogLastError(wxT("GetObject(HBITMAP)"));
0d0512bd
VZ
344 }
345
8bbbae21
VZ
346 bitmap->SetWidth(bm.bmWidth);
347 bitmap->SetHeight(bm.bmHeight);
348 bitmap->SetDepth(bm.bmBitsPixel);
349
9542f0a1
VZ
350 // use 0xc0c0c0 as transparent colour by default
351 bitmap->SetMask(new wxMask(*bitmap, *wxLIGHT_GREY));
352
4f8090e0 353 return true;
0d0512bd
VZ
354}
355
356bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap,
357 const wxString& name, long WXUNUSED(flags),
358 int WXUNUSED(desiredWidth),
359 int WXUNUSED(desiredHeight))
360{
4676948b 361#if wxUSE_WXDIB
474df218 362 wxCHECK_MSG( bitmap, false, _T("NULL bitmap in LoadFile") );
d275c7eb 363
474df218 364 wxDIB dib(name);
0d0512bd 365
a91bcd3b 366 return dib.IsOk() && bitmap->CopyFromDIB(dib);
4676948b 367#else
213ceb3f 368 return false;
4676948b 369#endif
0d0512bd
VZ
370}
371
372bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap,
373 const wxString& name,
374 int WXUNUSED(type),
2b254edf 375 const wxPalette * WXUNUSED(pal))
0d0512bd 376{
4676948b 377#if wxUSE_WXDIB
2b254edf
VZ
378 wxCHECK_MSG( bitmap, false, _T("NULL bitmap in SaveFile") );
379
380 wxDIB dib(*bitmap);
381
382 return dib.Save(name);
4676948b 383#else
213ceb3f 384 return false;
4676948b 385#endif
0d0512bd
VZ
386}
387
388// ----------------------------------------------------------------------------
389// wxIcon handlers
390// ----------------------------------------------------------------------------
391
392bool wxICOFileHandler::LoadIcon(wxIcon *icon,
393 const wxString& name,
33ac7e6f 394 long WXUNUSED(flags),
0d0512bd
VZ
395 int desiredWidth, int desiredHeight)
396{
0d0512bd
VZ
397 icon->UnRef();
398
399 // actual size
400 wxSize size;
401
a4352768
VZ
402 HICON hicon = NULL;
403
2dace059
VZ
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.
ff9aab3c 409 int iconIndex = 0;
2dace059 410 wxString nameReal(name);
ff9aab3c 411 wxString strIconIndex = name.AfterLast(wxT(';'));
b642d457 412 if (strIconIndex != name)
ff9aab3c
JS
413 {
414 iconIndex = wxAtoi(strIconIndex);
2dace059 415 nameReal = name.BeforeLast(wxT(';'));
ff9aab3c
JS
416 }
417
2b5f62a0
VZ
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
4676948b 439 // were we asked for a large icon?
a4352768
VZ
440 if ( desiredWidth == ::GetSystemMetrics(SM_CXICON) &&
441 desiredHeight == ::GetSystemMetrics(SM_CYICON) )
442 {
ff9aab3c 443 // get the specified large icon from file
2dace059 444 if ( !::ExtractIconEx(nameReal, iconIndex, &hicon, NULL, 1) )
a4352768
VZ
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 {
ff9aab3c 456 // get the specified small icon from file
2dace059 457 if ( !::ExtractIconEx(nameReal, iconIndex, NULL, &hicon, 1) )
a4352768
VZ
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
4676948b 466#ifndef __WXWINCE__
a4352768
VZ
467 if ( !hicon )
468 {
d8f3f983
RD
469 // take any size icon from the file by index
470 hicon = ::ExtractIcon(wxGetInstance(), nameReal, iconIndex);
a4352768 471 }
4676948b 472#endif
a4352768 473
0d0512bd
VZ
474 if ( !hicon )
475 {
476 wxLogSysError(_T("Failed to load icon from the file '%s'"),
477 name.c_str());
478
4f8090e0 479 return false;
0d0512bd
VZ
480 }
481
6bad4c32 482 size = wxGetHiconSize(hicon);
0d0512bd
VZ
483
484 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
485 (desiredHeight != -1 && desiredHeight != size.y) )
486 {
a4352768 487 wxLogTrace(_T("iconload"),
4f8090e0 488 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
a4352768
VZ
489 size.x, size.y,
490 desiredWidth, desiredHeight);
0d0512bd
VZ
491
492 ::DestroyIcon(hicon);
493
4f8090e0 494 return false;
0d0512bd
VZ
495 }
496
497 icon->SetHICON((WXHICON)hicon);
498 icon->SetSize(size.x, size.y);
499
500 return icon->Ok();
0d0512bd
VZ
501}
502
503bool wxICOResourceHandler::LoadIcon(wxIcon *icon,
504 const wxString& name,
33ac7e6f 505 long WXUNUSED(flags),
0d0512bd
VZ
506 int desiredWidth, int desiredHeight)
507{
508 HICON hicon;
509
9cb9fdb3
VZ
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
2dace059
VZ
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)
9990cb50
VZ
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
9990cb50 522 if ( hasSize )
0d0512bd 523 {
9990cb50
VZ
524 hicon = (HICON)::LoadImage(wxGetInstance(), name, IMAGE_ICON,
525 desiredWidth, desiredHeight,
526 LR_DEFAULTCOLOR);
0d0512bd 527 }
9990cb50 528 else
9990cb50
VZ
529 {
530 hicon = ::LoadIcon(wxGetInstance(), name);
531 }
fea2b62e 532
2dace059 533 // next check if it's not a standard icon
4676948b 534#ifndef __WXWINCE__
9cb9fdb3 535 if ( !hicon && !hasSize )
2dace059
VZ
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 },
c2edb18a 546 { wxT("wxICON_INFORMATION"), IDI_ASTERISK },
2dace059
VZ
547 };
548
549 for ( size_t nIcon = 0; !hicon && nIcon < WXSIZEOF(stdIcons); nIcon++ )
550 {
551 if ( name == stdIcons[nIcon].name )
552 {
9cb9fdb3 553 hicon = ::LoadIcon((HINSTANCE)NULL, stdIcons[nIcon].id);
2dace059
VZ
554 }
555 }
556 }
4676948b 557#endif
2dace059 558
6bad4c32 559 wxSize size = wxGetHiconSize(hicon);
0d0512bd
VZ
560 icon->SetSize(size.x, size.y);
561
0d0512bd
VZ
562 icon->SetHICON((WXHICON)hicon);
563
564 return icon->Ok();
565}
566
567// ----------------------------------------------------------------------------
568// private functions
569// ----------------------------------------------------------------------------
570
6bad4c32 571wxSize wxGetHiconSize(HICON hicon)
0d0512bd 572{
7010702f
WS
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
4676948b 577#ifndef __WXWINCE__
0d0512bd
VZ
578 if ( hicon && wxGetOsVersion() != wxWIN32S )
579 {
580 ICONINFO info;
581 if ( !::GetIconInfo(hicon, &info) )
582 {
f6bcfd97 583 wxLogLastError(wxT("GetIconInfo"));
0d0512bd
VZ
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 }
7010702f
WS
602#else
603 wxUnusedVar(hicon);
4676948b 604#endif
0d0512bd
VZ
605 return size;
606}
474df218
VZ
607
608#endif // __WXMICROWIN__
609