Fix assert when adding items with bitmaps wxBitmapComboBox.
[wxWidgets.git] / src / msw / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/list.h"
29 #include "wx/utils.h"
30 #include "wx/app.h"
31 #include "wx/icon.h"
32 #include "wx/bitmap.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/msw/private.h"
37
38 // ----------------------------------------------------------------------------
39 // wxWin macros
40 // ----------------------------------------------------------------------------
41
42 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
43
44 // ============================================================================
45 // implementation
46 // ============================================================================
47
48 // ----------------------------------------------------------------------------
49 // wxIconRefData
50 // ----------------------------------------------------------------------------
51
52 void wxIconRefData::Free()
53 {
54 if ( m_hIcon )
55 {
56 #ifndef __WXMICROWIN__
57 ::DestroyIcon((HICON) m_hIcon);
58 #endif
59
60 m_hIcon = 0;
61 }
62 }
63
64 // ----------------------------------------------------------------------------
65 // wxIcon
66 // ----------------------------------------------------------------------------
67
68 wxIcon::wxIcon(const char bits[], int width, int height)
69 {
70 wxBitmap bmp(bits, width, height);
71 CopyFromBitmap(bmp);
72 }
73
74 wxIcon::wxIcon(const wxString& iconfile,
75 wxBitmapType type,
76 int desiredWidth,
77 int desiredHeight)
78
79 {
80 LoadFile(iconfile, type, desiredWidth, desiredHeight);
81 }
82
83 wxIcon::wxIcon(const wxIconLocation& loc)
84 {
85 // wxICOFileHandler accepts names in the format "filename;index"
86 wxString fullname = loc.GetFileName();
87 if ( loc.GetIndex() )
88 {
89 fullname << wxT(';') << loc.GetIndex();
90 }
91 //else: 0 is default
92
93 LoadFile(fullname, wxBITMAP_TYPE_ICO);
94 }
95
96 wxIcon::~wxIcon()
97 {
98 }
99
100 wxObjectRefData *wxIcon::CloneRefData(const wxObjectRefData *dataOrig) const
101 {
102 const wxIconRefData *
103 data = static_cast<const wxIconRefData *>(dataOrig);
104 if ( !data )
105 return NULL;
106
107 // we don't have to copy m_hIcon because we're only called from SetHICON()
108 // which overwrites m_hIcon anyhow currently
109 //
110 // and if we're called from SetWidth/Height/Depth(), it doesn't make sense
111 // to copy it neither as the handle would be inconsistent with the new size
112 return new wxIconRefData(*data);
113 }
114
115 void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
116 {
117 #ifndef __WXMICROWIN__
118 HICON hicon = wxBitmapToHICON(bmp);
119 if ( !hicon )
120 {
121 wxLogLastError(wxT("CreateIconIndirect"));
122 }
123 else
124 {
125 SetHICON((WXHICON)hicon);
126 SetSize(bmp.GetWidth(), bmp.GetHeight());
127 }
128 #endif // __WXMICROWIN__
129 }
130
131 void wxIcon::CreateIconFromXpm(const char* const* data)
132 {
133 wxBitmap bmp(data);
134 CopyFromBitmap(bmp);
135 }
136
137 bool wxIcon::LoadFile(const wxString& filename,
138 wxBitmapType type,
139 int desiredWidth, int desiredHeight)
140 {
141 UnRef();
142
143 wxGDIImageHandler *handler = FindHandler(type);
144
145 if ( !handler )
146 {
147 // load via wxBitmap which, in turn, uses wxImage allowing us to
148 // support more formats
149 wxBitmap bmp;
150 if ( !bmp.LoadFile(filename, type) )
151 return false;
152
153 CopyFromBitmap(bmp);
154 return true;
155 }
156
157 return handler->Load(this, filename, type, desiredWidth, desiredHeight);
158 }
159
160 bool wxIcon::CreateFromHICON(WXHICON icon)
161 {
162 SetHICON(icon);
163 if ( !IsOk() )
164 return false;
165
166 SetSize(wxGetHiconSize(icon));
167
168 return true;
169 }