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