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