]> git.saurik.com Git - wxWidgets.git/blob - src/msw/icon.cpp
reformatted to fit in the page width
[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, wxGDIObject)
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxIconRefData
60 // ----------------------------------------------------------------------------
61
62 void wxIconRefData::Free()
63 {
64 if ( m_hIcon )
65 {
66 #ifndef __WXMICROWIN__
67 ::DestroyIcon((HICON) m_hIcon);
68 #endif
69
70 m_hIcon = 0;
71 }
72 }
73
74 // ----------------------------------------------------------------------------
75 // wxIcon
76 // ----------------------------------------------------------------------------
77
78 wxIcon::wxIcon(const char bits[], int width, int height)
79 {
80 wxBitmap bmp(bits, width, height);
81 CopyFromBitmap(bmp);
82 }
83
84 wxIcon::wxIcon(const wxString& iconfile,
85 long flags,
86 int desiredWidth,
87 int desiredHeight)
88
89 {
90 LoadFile(iconfile, flags, desiredWidth, desiredHeight);
91 }
92
93 wxIcon::~wxIcon()
94 {
95 }
96
97 void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
98 {
99 #ifndef __WXMICROWIN__
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
114 // black out the transparent area to preserve background colour, because
115 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
116 // the mask to the dest rect.
117 {
118 MemoryHDC dcSrc, dcDst;
119 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
120 selectBitmap(dcDst, iconInfo.hbmColor);
121
122 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
123 dcSrc, 0, 0, SRCAND) )
124 {
125 wxLogLastError(_T("BitBlt"));
126 }
127 }
128
129 HICON hicon = ::CreateIconIndirect(&iconInfo);
130 if ( !hicon )
131 {
132 wxLogLastError(wxT("CreateIconIndirect"));
133 }
134 else
135 {
136 SetHICON((WXHICON)hicon);
137 SetSize(bmp.GetWidth(), bmp.GetHeight());
138 }
139
140 if ( !bmp.GetMask() )
141 {
142 // we created the mask, now delete it
143 delete mask;
144 }
145
146 // delete the inverted mask bitmap we created as well
147 ::DeleteObject(iconInfo.hbmMask);
148 #else // Win16
149 // there are some functions in curico.cpp which probably could be used
150 // here...
151 // This probably doesn't work.
152 HBITMAP hBitmap = (HBITMAP) bmp.GetHBITMAP();
153 HICON hIcon = MakeIconFromBitmap((HINSTANCE) wxGetInstance(), hBitmap);
154 if (hIcon)
155 {
156 SetHICON((WXHICON)hIcon);
157 SetSize(bmp.GetWidth(), bmp.GetHeight());
158 }
159
160 // wxFAIL_MSG("Bitmap to icon conversion (including use of XPMs for icons) not implemented");
161 #endif // Win32/16
162 #endif
163 }
164
165 void wxIcon::CreateIconFromXpm(const char **data)
166 {
167 wxBitmap bmp(data);
168 CopyFromBitmap(bmp);
169 }
170
171 bool wxIcon::LoadFile(const wxString& filename,
172 long type,
173 int desiredWidth, int desiredHeight)
174 {
175 UnRef();
176
177 wxGDIImageHandler *handler = FindHandler(type);
178
179 if ( !handler )
180 {
181 // say something?
182 return FALSE;
183 }
184
185 return handler->Load(this, filename, type, desiredWidth, desiredHeight);
186 }
187