]> git.saurik.com Git - wxWidgets.git/blame - src/msw/icon.cpp
fixed memory allocation code of wxStreamBuffer to not realloc() new[]ed memory any...
[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"
2bda0e17
KB
38#endif
39
40#include "wx/msw/private.h"
2bda0e17 41
47d67540 42#if wxUSE_RESOURCE_LOADING_IN_MSW
0d0512bd
VZ
43 #include "wx/msw/curico.h"
44 #include "wx/msw/curicop.h"
2bda0e17
KB
45#endif
46
0d0512bd
VZ
47// ----------------------------------------------------------------------------
48// wxWin macros
49// ----------------------------------------------------------------------------
50
4b7f2165 51IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxIconBase)
2bda0e17 52
0d0512bd
VZ
53// ============================================================================
54// implementation
55// ============================================================================
2bda0e17 56
0d0512bd
VZ
57// ----------------------------------------------------------------------------
58// wxIconRefData
59// ----------------------------------------------------------------------------
2bda0e17 60
0d0512bd 61void wxIconRefData::Free()
2bda0e17 62{
0d0512bd 63 if ( m_hIcon )
032af30f 64 {
0d0512bd 65 ::DestroyIcon((HICON) m_hIcon);
032af30f
VZ
66
67 m_hIcon = 0;
68 }
2bda0e17
KB
69}
70
0d0512bd
VZ
71// ----------------------------------------------------------------------------
72// wxIcon
73// ----------------------------------------------------------------------------
2bda0e17 74
4b7f2165 75wxIcon::wxIcon(const char bits[], int width, int height)
2bda0e17 76{
4b7f2165
VZ
77 wxBitmap bmp(bits, width, height);
78 CopyFromBitmap(bmp);
2bda0e17
KB
79}
80
0d0512bd
VZ
81wxIcon::wxIcon(const wxString& iconfile,
82 long flags,
83 int desiredWidth,
84 int desiredHeight)
2bda0e17
KB
85
86{
0d0512bd 87 LoadFile(iconfile, flags, desiredWidth, desiredHeight);
2bda0e17
KB
88}
89
0d0512bd 90wxIcon::~wxIcon()
2bda0e17
KB
91{
92}
93
4b7f2165
VZ
94void 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
2edc119b
GRG
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
4b7f2165
VZ
126 HICON hicon = ::CreateIconIndirect(&iconInfo);
127 if ( !hicon )
128 {
f6bcfd97 129 wxLogLastError(wxT("CreateIconIndirect"));
4b7f2165
VZ
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...
669f7a11
JS
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");
4b7f2165
VZ
155#endif // Win32/16
156}
157
158void wxIcon::CreateIconFromXpm(const char **data)
159{
160 wxBitmap bmp(data);
161 CopyFromBitmap(bmp);
162}
163
0d0512bd
VZ
164bool wxIcon::LoadFile(const wxString& filename,
165 long type,
166 int desiredWidth, int desiredHeight)
2bda0e17 167{
0d0512bd 168 UnRef();
2bda0e17 169
0d0512bd 170 wxGDIImageHandler *handler = FindHandler(type);
2bda0e17 171
0d0512bd 172 if ( !handler )
2bda0e17 173 {
0d0512bd
VZ
174 // say something?
175 return FALSE;
2bda0e17
KB
176 }
177
0d0512bd 178 return handler->Load(this, filename, type, desiredWidth, desiredHeight);
2bda0e17
KB
179}
180