]> git.saurik.com Git - wxWidgets.git/blob - src/msw/icon.cpp
1. wxStaticBitmap now uses mask even for bitmaps (and not only icons)
[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 HICON hicon = ::CreateIconIndirect(&iconInfo);
111 if ( !hicon )
112 {
113 wxLogLastError("CreateIconIndirect");
114 }
115 else
116 {
117 SetHICON((WXHICON)hicon);
118 SetSize(bmp.GetWidth(), bmp.GetHeight());
119 }
120
121 if ( !bmp.GetMask() )
122 {
123 // we created the mask, now delete it
124 delete mask;
125 }
126 #else // Win16
127 // there are some functions in curico.cpp which probably could be used
128 // here...
129 wxFAIL_MSG("not implemented");
130 #endif // Win32/16
131 }
132
133 void wxIcon::CreateIconFromXpm(const char **data)
134 {
135 wxBitmap bmp(data);
136 CopyFromBitmap(bmp);
137 }
138
139 bool wxIcon::LoadFile(const wxString& filename,
140 long type,
141 int desiredWidth, int desiredHeight)
142 {
143 UnRef();
144
145 wxGDIImageHandler *handler = FindHandler(type);
146
147 if ( !handler )
148 {
149 // say something?
150 return FALSE;
151 }
152
153 return handler->Load(this, filename, type, desiredWidth, desiredHeight);
154 }
155