]> git.saurik.com Git - wxWidgets.git/blob - src/msw/gdiimage.cpp
Fix HTML help on Mac.
[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, wxGDIImageHandler)
181 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxGDIImageHandler)
182
183 // ----------------------------------------------------------------------------
184 // private functions
185 // ----------------------------------------------------------------------------
186
187 static wxSize GetHiconSize(HICON hicon);
188 #endif
189 // __MICROWIN__
190
191 // ============================================================================
192 // implementation
193 // ============================================================================
194
195 wxList wxGDIImage::ms_handlers;
196
197 // ----------------------------------------------------------------------------
198 // wxGDIImage functions forwarded to wxGDIImageRefData
199 // ----------------------------------------------------------------------------
200
201 bool wxGDIImage::FreeResource(bool WXUNUSED(force))
202 {
203 if ( !IsNull() )
204 {
205 GetGDIImageData()->Free();
206 GetGDIImageData()->m_handle = 0;
207 }
208
209 return TRUE;
210 }
211
212 WXHANDLE wxGDIImage::GetResourceHandle()
213 {
214 return GetHandle();
215 }
216
217 // ----------------------------------------------------------------------------
218 // wxGDIImage handler stuff
219 // ----------------------------------------------------------------------------
220
221 void wxGDIImage::AddHandler(wxGDIImageHandler *handler)
222 {
223 ms_handlers.Append(handler);
224 }
225
226 void wxGDIImage::InsertHandler(wxGDIImageHandler *handler)
227 {
228 ms_handlers.Insert(handler);
229 }
230
231 bool wxGDIImage::RemoveHandler(const wxString& name)
232 {
233 wxGDIImageHandler *handler = FindHandler(name);
234 if ( handler )
235 {
236 ms_handlers.DeleteObject(handler);
237 return TRUE;
238 }
239 else
240 return FALSE;
241 }
242
243 wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& name)
244 {
245 wxNode *node = ms_handlers.First();
246 while ( node )
247 {
248 wxGDIImageHandler *handler = (wxGDIImageHandler *)node->Data();
249 if ( handler->GetName() == name )
250 return handler;
251 node = node->Next();
252 }
253
254 return NULL;
255 }
256
257 wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& extension,
258 long type)
259 {
260 wxNode *node = ms_handlers.First();
261 while ( node )
262 {
263 wxGDIImageHandler *handler = (wxGDIImageHandler *)node->Data();
264 if ( (handler->GetExtension() = extension) &&
265 (type == -1 || handler->GetType() == type) )
266 {
267 return handler;
268 }
269
270 node = node->Next();
271 }
272 return NULL;
273 }
274
275 wxGDIImageHandler *wxGDIImage::FindHandler(long type)
276 {
277 wxNode *node = ms_handlers.First();
278 while ( node )
279 {
280 wxGDIImageHandler *handler = (wxGDIImageHandler *)node->Data();
281 if ( handler->GetType() == type )
282 return handler;
283
284 node = node->Next();
285 }
286
287 return NULL;
288 }
289
290 void wxGDIImage::CleanUpHandlers()
291 {
292 wxNode *node = ms_handlers.First();
293 while ( node )
294 {
295 wxGDIImageHandler *handler = (wxGDIImageHandler *)node->Data();
296 wxNode *next = node->Next();
297 delete handler;
298 delete node;
299 node = next;
300 }
301 }
302
303 void wxGDIImage::InitStandardHandlers()
304 {
305 #ifndef __WXMICROWIN__
306 AddHandler(new wxBMPResourceHandler);
307 AddHandler(new wxBMPFileHandler);
308 AddHandler(new wxICOResourceHandler);
309 AddHandler(new wxICOFileHandler);
310 #endif
311 }
312
313 #ifndef __WXMICROWIN__
314
315 // ----------------------------------------------------------------------------
316 // wxBitmap handlers
317 // ----------------------------------------------------------------------------
318
319 bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap,
320 const wxString& name, long WXUNUSED(flags),
321 int WXUNUSED(desiredWidth),
322 int WXUNUSED(desiredHeight))
323 {
324 // TODO: load colourmap.
325 bitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), name));
326
327 wxBitmapRefData *data = bitmap->GetBitmapData();
328 if ( bitmap->Ok() )
329 {
330 BITMAP bm;
331 if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(BITMAP), (LPSTR) &bm) )
332 {
333 wxLogLastError(wxT("GetObject(HBITMAP)"));
334 }
335
336 data->m_width = bm.bmWidth;
337 data->m_height = bm.bmHeight;
338 data->m_depth = bm.bmBitsPixel;
339 }
340 else
341 {
342 // it's probably not found
343 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
344 name.c_str());
345 }
346
347 return bitmap->Ok();
348 }
349
350 bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap,
351 const wxString& name, long WXUNUSED(flags),
352 int WXUNUSED(desiredWidth),
353 int WXUNUSED(desiredHeight))
354 {
355 #if wxUSE_IMAGE_LOADING_IN_MSW
356 wxPalette *palette = NULL;
357 bool success = wxLoadIntoBitmap(WXSTRINGCAST name, bitmap, &palette) != 0;
358 if ( success && palette )
359 {
360 bitmap->SetPalette(*palette);
361 }
362
363 // it was copied by the bitmap if it was loaded successfully
364 delete palette;
365
366 return success;
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 *pal)
376 {
377 #if wxUSE_IMAGE_LOADING_IN_MSW
378 wxPalette *actualPalette = (wxPalette *)pal;
379 if ( !actualPalette )
380 actualPalette = bitmap->GetPalette();
381 return wxSaveBitmap(WXSTRINGCAST name, bitmap, actualPalette) != 0;
382 #else
383 return FALSE;
384 #endif
385 }
386
387 // ----------------------------------------------------------------------------
388 // wxIcon handlers
389 // ----------------------------------------------------------------------------
390
391 bool wxICOFileHandler::LoadIcon(wxIcon *icon,
392 const wxString& name,
393 long WXUNUSED(flags),
394 int desiredWidth, int desiredHeight)
395 {
396 #if wxUSE_RESOURCE_LOADING_IN_MSW
397 icon->UnRef();
398
399 // actual size
400 wxSize size;
401
402 #ifdef __WIN32__
403 HICON hicon = NULL;
404
405 // were we asked for a large icon?
406 if ( desiredWidth == ::GetSystemMetrics(SM_CXICON) &&
407 desiredHeight == ::GetSystemMetrics(SM_CYICON) )
408 {
409 // get the first large icon from file
410 if ( !::ExtractIconEx(name, 0, &hicon, NULL, 1) )
411 {
412 // it is not an error, but it might still be useful to be informed
413 // about it optionally
414 wxLogTrace(_T("iconload"),
415 _T("No large icons found in the file '%s'."),
416 name.c_str());
417 }
418 }
419 else if ( desiredWidth == ::GetSystemMetrics(SM_CXSMICON) &&
420 desiredHeight == ::GetSystemMetrics(SM_CYSMICON) )
421 {
422 // get the first small icon from file
423 if ( !::ExtractIconEx(name, 0, NULL, &hicon, 1) )
424 {
425 wxLogTrace(_T("iconload"),
426 _T("No small icons found in the file '%s'."),
427 name.c_str());
428 }
429 }
430 //else: not standard size, load below
431
432 if ( !hicon )
433 {
434 // take any (the first one) icon from the file by default
435 hicon = ::ExtractIcon(wxGetInstance(), name, 0 /* first */);
436 }
437
438 if ( !hicon )
439 {
440 wxLogSysError(_T("Failed to load icon from the file '%s'"),
441 name.c_str());
442
443 return FALSE;
444 }
445
446 size = GetHiconSize(hicon);
447 #else // Win16
448 HICON hicon = ReadIconFile((wxChar *)name.c_str(),
449 wxGetInstance(),
450 &size.x, &size.y);
451 #endif // Win32/Win16
452
453 if ( (desiredWidth != -1 && desiredWidth != size.x) ||
454 (desiredHeight != -1 && desiredHeight != size.y) )
455 {
456 wxLogTrace(_T("iconload"),
457 _T("Returning FALSE from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
458 size.x, size.y,
459 desiredWidth, desiredHeight);
460
461 ::DestroyIcon(hicon);
462
463 return FALSE;
464 }
465
466 icon->SetHICON((WXHICON)hicon);
467 icon->SetSize(size.x, size.y);
468
469 return icon->Ok();
470 #else
471 return FALSE;
472 #endif
473 }
474
475 bool wxICOResourceHandler::LoadIcon(wxIcon *icon,
476 const wxString& name,
477 long WXUNUSED(flags),
478 int desiredWidth, int desiredHeight)
479 {
480 HICON hicon;
481
482 #if defined(__WIN32__) && !defined(__SC__)
483 if ( desiredWidth != -1 && desiredHeight != -1 )
484 {
485 hicon = (HICON)::LoadImage(wxGetInstance(), name, IMAGE_ICON,
486 desiredWidth, desiredHeight,
487 LR_DEFAULTCOLOR);
488 }
489 else
490 #endif // Win32
491 {
492 hicon = ::LoadIcon(wxGetInstance(), name);
493 }
494
495 wxSize size = GetHiconSize(hicon);
496 icon->SetSize(size.x, size.y);
497
498 // Override the found values with desired values
499 if ( desiredWidth > -1 && desiredHeight > -1 )
500 {
501 icon->SetSize(desiredWidth, desiredHeight);
502 }
503
504 icon->SetHICON((WXHICON)hicon);
505
506 return icon->Ok();
507 }
508
509 // ----------------------------------------------------------------------------
510 // private functions
511 // ----------------------------------------------------------------------------
512
513 static wxSize GetHiconSize(HICON hicon)
514 {
515 wxSize size(32, 32); // default
516
517 #ifdef __WIN32__
518 // Win32s doesn't have GetIconInfo function...
519 if ( hicon && wxGetOsVersion() != wxWIN32S )
520 {
521 ICONINFO info;
522 if ( !::GetIconInfo(hicon, &info) )
523 {
524 wxLogLastError(wxT("GetIconInfo"));
525 }
526 else
527 {
528 HBITMAP hbmp = info.hbmMask;
529 if ( hbmp )
530 {
531 BITMAP bm;
532 if ( ::GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm) )
533 {
534 size = wxSize(bm.bmWidth, bm.bmHeight);
535 }
536
537 ::DeleteObject(info.hbmMask);
538 }
539 if ( info.hbmColor )
540 ::DeleteObject(info.hbmColor);
541 }
542 }
543 #endif
544
545 return size;
546 }
547 #endif
548 // __WXMICROWIN__