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