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