]> git.saurik.com Git - wxWidgets.git/blame - src/msw/icon.cpp
another segfault fix
[wxWidgets.git] / src / msw / icon.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
0d0512bd 2// Name: msw/icon.cpp
2bda0e17
KB
3// Purpose: wxIcon class
4// Author: Julian Smart
0d0512bd 5// Modified by: 20.11.99 (VZ): don't derive from wxBitmap any more
2bda0e17
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
8ed57d93 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
0d0512bd
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
0d0512bd 21 #pragma implementation "icon.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
0d0512bd 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
0d0512bd
VZ
32 #include "wx/defs.h"
33 #include "wx/list.h"
34 #include "wx/utils.h"
35 #include "wx/app.h"
36 #include "wx/icon.h"
f02b73c4 37 #include "wx/bitmap.h"
f94dfb38 38 #include "wx/log.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
2bda0e17 42
47d67540 43#if wxUSE_RESOURCE_LOADING_IN_MSW
0d0512bd
VZ
44 #include "wx/msw/curico.h"
45 #include "wx/msw/curicop.h"
2bda0e17
KB
46#endif
47
0d0512bd
VZ
48// ----------------------------------------------------------------------------
49// wxWin macros
50// ----------------------------------------------------------------------------
51
4b7f2165 52IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxIconBase)
2bda0e17 53
0d0512bd
VZ
54// ============================================================================
55// implementation
56// ============================================================================
2bda0e17 57
0d0512bd
VZ
58// ----------------------------------------------------------------------------
59// wxIconRefData
60// ----------------------------------------------------------------------------
2bda0e17 61
0d0512bd 62void wxIconRefData::Free()
2bda0e17 63{
0d0512bd 64 if ( m_hIcon )
032af30f 65 {
04ef50df 66#ifndef __WXMICROWIN__
0d0512bd 67 ::DestroyIcon((HICON) m_hIcon);
04ef50df 68#endif
032af30f
VZ
69
70 m_hIcon = 0;
71 }
2bda0e17
KB
72}
73
0d0512bd
VZ
74// ----------------------------------------------------------------------------
75// wxIcon
76// ----------------------------------------------------------------------------
2bda0e17 77
4b7f2165 78wxIcon::wxIcon(const char bits[], int width, int height)
2bda0e17 79{
4b7f2165
VZ
80 wxBitmap bmp(bits, width, height);
81 CopyFromBitmap(bmp);
2bda0e17
KB
82}
83
0d0512bd
VZ
84wxIcon::wxIcon(const wxString& iconfile,
85 long flags,
86 int desiredWidth,
87 int desiredHeight)
2bda0e17
KB
88
89{
0d0512bd 90 LoadFile(iconfile, flags, desiredWidth, desiredHeight);
2bda0e17
KB
91}
92
0d0512bd 93wxIcon::~wxIcon()
2bda0e17
KB
94{
95}
96
4b7f2165
VZ
97void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
98{
04ef50df 99#ifndef __WXMICROWIN__
4b7f2165
VZ
100#ifdef __WIN32__
101 wxMask *mask = bmp.GetMask();
102 if ( !mask )
103 {
104 // we must have a mask for an icon, so even if it's probably incorrect,
105 // do create it (grey is the "standard" transparent colour)
106 mask = new wxMask(bmp, *wxLIGHT_GREY);
107 }
108
109 ICONINFO iconInfo;
110 iconInfo.fIcon = TRUE; // we want an icon, not a cursor
111 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
112 iconInfo.hbmColor = GetHbitmapOf(bmp);
113
2edc119b
GRG
114 /* GRG: black out the transparent area to preserve background
115 * colour, because Windows blits the original bitmap using
116 * SRCINVERT (XOR) after applying the mask to the dest rect.
117 */
118 HDC dcSrc = ::CreateCompatibleDC(NULL);
119 HDC dcDst = ::CreateCompatibleDC(NULL);
120 SelectObject(dcSrc, (HBITMAP)mask->GetMaskBitmap());
121 SelectObject(dcDst, iconInfo.hbmColor);
122
123 BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(), dcSrc, 0, 0, SRCAND);
124
125 SelectObject(dcDst, NULL);
126 SelectObject(dcSrc, NULL);
127 DeleteDC(dcDst);
128 DeleteDC(dcSrc);
129
4b7f2165
VZ
130 HICON hicon = ::CreateIconIndirect(&iconInfo);
131 if ( !hicon )
132 {
f6bcfd97 133 wxLogLastError(wxT("CreateIconIndirect"));
4b7f2165
VZ
134 }
135 else
136 {
137 SetHICON((WXHICON)hicon);
138 SetSize(bmp.GetWidth(), bmp.GetHeight());
139 }
140
141 if ( !bmp.GetMask() )
142 {
143 // we created the mask, now delete it
144 delete mask;
145 }
146#else // Win16
147 // there are some functions in curico.cpp which probably could be used
148 // here...
669f7a11
JS
149 // This probably doesn't work.
150 HBITMAP hBitmap = (HBITMAP) bmp.GetHBITMAP();
151 HICON hIcon = MakeIconFromBitmap((HINSTANCE) wxGetInstance(), hBitmap);
152 if (hIcon)
153 {
154 SetHICON((WXHICON)hIcon);
155 SetSize(bmp.GetWidth(), bmp.GetHeight());
156 }
157
158// wxFAIL_MSG("Bitmap to icon conversion (including use of XPMs for icons) not implemented");
4b7f2165 159#endif // Win32/16
04ef50df 160#endif
4b7f2165
VZ
161}
162
163void wxIcon::CreateIconFromXpm(const char **data)
164{
165 wxBitmap bmp(data);
166 CopyFromBitmap(bmp);
167}
168
0d0512bd
VZ
169bool wxIcon::LoadFile(const wxString& filename,
170 long type,
171 int desiredWidth, int desiredHeight)
2bda0e17 172{
0d0512bd 173 UnRef();
2bda0e17 174
0d0512bd 175 wxGDIImageHandler *handler = FindHandler(type);
2bda0e17 176
0d0512bd 177 if ( !handler )
2bda0e17 178 {
0d0512bd
VZ
179 // say something?
180 return FALSE;
2bda0e17
KB
181 }
182
0d0512bd 183 return handler->Load(this, filename, type, desiredWidth, desiredHeight);
2bda0e17
KB
184}
185