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