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