]> git.saurik.com Git - wxWidgets.git/blob - src/msw/icon.cpp
cw makefile for minimal - to be used as template
[wxWidgets.git] / src / msw / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: icon.cpp
3 // Purpose: wxIcon class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "icon.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/list.h"
27 #include "wx/utils.h"
28 #include "wx/app.h"
29 #include "wx/icon.h"
30 #endif
31
32 #include "wx/msw/private.h"
33 #include "assert.h"
34
35 #if wxUSE_XPM_IN_MSW
36 #define FOR_MSW 1
37 #include "../src/xpm/xpm34.h"
38 #endif
39
40 #if wxUSE_RESOURCE_LOADING_IN_MSW
41 #include "wx/msw/curico.h"
42 #include "wx/msw/curicop.h"
43 #endif
44
45 #if !USE_SHARED_LIBRARIES
46 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
47 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxBitmapHandler)
48 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxBitmapHandler)
49 #endif
50
51 /*
52 * Icons
53 */
54
55
56 wxIconRefData::wxIconRefData(void)
57 {
58 m_hIcon = (WXHICON) NULL ;
59 }
60
61 wxIconRefData::~wxIconRefData(void)
62 {
63 if ( m_hIcon )
64 ::DestroyIcon((HICON) m_hIcon);
65 }
66
67 wxIcon::wxIcon(void)
68 {
69 }
70
71 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
72 {
73 }
74
75 wxIcon::wxIcon(const wxString& icon_file, long flags,
76 int desiredWidth, int desiredHeight)
77
78 {
79 LoadFile(icon_file, flags, desiredWidth, desiredHeight);
80 }
81
82 wxIcon::~wxIcon(void)
83 {
84 }
85
86 bool wxIcon::FreeResource(bool force)
87 {
88 if (M_ICONDATA && M_ICONDATA->m_hIcon)
89 {
90 DestroyIcon((HICON) M_ICONDATA->m_hIcon);
91 M_ICONDATA->m_hIcon = (WXHICON) NULL;
92 }
93 return TRUE;
94 }
95
96 bool wxIcon::LoadFile(const wxString& filename, long type,
97 int desiredWidth, int desiredHeight)
98 {
99 UnRef();
100
101 m_refData = new wxIconRefData;
102
103 wxBitmapHandler *handler = FindHandler(type);
104
105 if ( handler )
106 return handler->LoadFile(this, filename, type, desiredWidth, desiredHeight);
107 else
108 return FALSE;
109 }
110
111 void wxIcon::SetHICON(WXHICON ico)
112 {
113 if ( !M_ICONDATA )
114 m_refData = new wxIconRefData;
115
116 M_ICONDATA->m_hIcon = ico;
117 }
118
119 bool wxICOFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
120 int desiredWidth, int desiredHeight)
121 {
122 #if wxUSE_RESOURCE_LOADING_IN_MSW
123 if ( bitmap->IsKindOf(CLASSINFO(wxIcon)) )
124 {
125 wxIcon *icon = (wxIcon *)bitmap;
126 wxIconRefData *data = (wxIconRefData *)icon->GetRefData();
127 data->m_hIcon = (WXHICON)ReadIconFile((char *)name.c_str(), wxGetInstance(),
128 &data->m_width, &data->m_height);
129
130 data->m_ok = data->m_hIcon != 0;
131 return data->m_ok;
132 }
133 else
134 return FALSE;
135 #else
136 return FALSE;
137 #endif
138 }
139
140 bool wxICOResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
141 int desiredWidth, int desiredHeight)
142 {
143 if ( bitmap->IsKindOf(CLASSINFO(wxIcon)) )
144 {
145 #if defined(__WIN32__)
146 if (desiredWidth > -1 && desiredHeight > -1)
147 {
148 M_ICONHANDLERDATA->m_hIcon = (WXHICON) ::LoadImage(wxGetInstance(), name, IMAGE_ICON, desiredWidth, desiredHeight, LR_DEFAULTCOLOR);
149 }
150 else
151 #endif
152 {
153 M_ICONHANDLERDATA->m_hIcon = (WXHICON) ::LoadIcon(wxGetInstance(), name);
154 }
155
156 #ifdef __WIN32__
157 // Win32s doesn't have GetIconInfo function...
158 if (M_ICONHANDLERDATA->m_hIcon && wxGetOsVersion()!=wxWIN32S)
159 {
160 ICONINFO info ;
161 if (::GetIconInfo((HICON) M_ICONHANDLERDATA->m_hIcon, &info))
162 {
163 HBITMAP ms_bitmap = info.hbmMask ;
164 if (ms_bitmap)
165 {
166 BITMAP bm;
167 ::GetObject(ms_bitmap, sizeof(BITMAP), (LPSTR) &bm);
168 M_ICONHANDLERDATA->m_width = bm.bmWidth;
169 M_ICONHANDLERDATA->m_height = bm.bmHeight;
170 }
171 if (info.hbmMask)
172 ::DeleteObject(info.hbmMask) ;
173 if (info.hbmColor)
174 ::DeleteObject(info.hbmColor) ;
175 }
176 }
177 #else
178 M_ICONHANDLERDATA->m_width = 32;
179 M_ICONHANDLERDATA->m_height = 32;
180 #endif
181 // Override the found values with desired values
182 if (desiredWidth > -1 && desiredHeight > -1)
183 {
184 M_ICONHANDLERDATA->m_width = desiredWidth;
185 M_ICONHANDLERDATA->m_height = desiredHeight;
186 }
187
188 M_ICONHANDLERDATA->m_ok = (M_ICONHANDLERDATA->m_hIcon != 0);
189 return M_ICONHANDLERDATA->m_ok;
190 }
191 else
192 return FALSE;
193 }
194