]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/imaglist.cpp
cleanup - reformatting (lots), minor code tweaks
[wxWidgets.git] / src / mac / carbon / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/imaglist.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_IMAGLIST
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/defs.h"
20
21 #include "wx/imaglist.h"
22
23 #include "wx/icon.h"
24 #include "wx/image.h"
25 #include "wx/dc.h"
26
27 //-----------------------------------------------------------------------------
28 // wxImageList
29 //-----------------------------------------------------------------------------
30
31 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
32
33 wxImageList::wxImageList( int width, int height, bool mask, int initialCount )
34 {
35 (void)Create(width, height, mask, initialCount);
36 }
37
38 wxImageList::~wxImageList()
39 {
40 (void)RemoveAll();
41 }
42
43 int wxImageList::GetImageCount() const
44 {
45 return m_images.GetCount();
46 }
47
48 bool wxImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
49 {
50 m_width = width;
51 m_height = height;
52
53 return Create();
54 }
55
56 bool wxImageList::Create()
57 {
58 return true;
59 }
60
61 int wxImageList::Add( const wxIcon &bitmap )
62 {
63 wxASSERT_MSG( (bitmap.GetWidth() == m_width && bitmap.GetHeight() == m_height)
64 || (m_width == 0 && m_height == 0),
65 _T("invalid bitmap size in wxImageList: this might work ")
66 _T("on this platform but definitely won't under Windows.") );
67
68 m_images.Append( new wxIcon( bitmap ) );
69
70 if (m_width == 0 && m_height == 0)
71 {
72 m_width = bitmap.GetWidth();
73 m_height = bitmap.GetHeight();
74 }
75
76 return m_images.GetCount()-1;
77 }
78
79 int wxImageList::Add( const wxBitmap &bitmap )
80 {
81 wxASSERT_MSG( (bitmap.GetWidth() == m_width && bitmap.GetHeight() == m_height)
82 || (m_width == 0 && m_height == 0),
83 _T("invalid bitmap size in wxImageList: this might work ")
84 _T("on this platform but definitely won't under Windows.") );
85
86 m_images.Append( new wxBitmap(bitmap) );
87
88 if (m_width == 0 && m_height == 0)
89 {
90 m_width = bitmap.GetWidth();
91 m_height = bitmap.GetHeight();
92 }
93
94 return m_images.GetCount()-1;
95 }
96
97 int wxImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
98 {
99 wxBitmap bmp(bitmap);
100 if (mask.Ok())
101 bmp.SetMask(new wxMask(mask));
102 return Add(bmp);
103 }
104
105 int wxImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
106 {
107 wxImage img = bitmap.ConvertToImage();
108 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
109 return Add(wxBitmap(img));
110 }
111
112 // Get the bitmap
113 wxBitmap wxImageList::GetBitmap(int index) const
114 {
115 wxList::compatibility_iterator node = m_images.Item( index );
116
117 wxCHECK_MSG( node, wxNullBitmap , wxT("wrong index in image list") );
118
119 wxObject* obj = (wxObject*) node->GetData();
120 if ( obj == NULL )
121 return wxNullBitmap ;
122 else if ( obj->IsKindOf(CLASSINFO(wxIcon)) )
123 return wxBitmap( *(wx_static_cast(wxIcon*,obj)) ) ;
124 else
125 return *(wx_static_cast(wxBitmap*,obj)) ;
126 }
127
128 // Get the icon
129 wxIcon wxImageList::GetIcon(int index) const
130 {
131 wxList::compatibility_iterator node = m_images.Item( index );
132
133 wxCHECK_MSG( node, wxNullIcon , wxT("wrong index in image list") );
134
135 wxObject* obj = (wxObject*) node->GetData();
136 if ( obj == NULL )
137 return wxNullIcon ;
138 else if( obj->IsKindOf(CLASSINFO(wxBitmap)) )
139 {
140 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
141 return wxNullIcon ;
142 }
143 else
144 return *(wx_static_cast(wxIcon*,obj)) ;
145 }
146
147 bool wxImageList::Replace( int index, const wxBitmap &bitmap )
148 {
149 wxList::compatibility_iterator node = m_images.Item( index );
150
151 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
152
153 wxBitmap* newBitmap = new wxBitmap(bitmap) ;
154
155 if (index == (int) m_images.GetCount() - 1)
156 {
157 delete node->GetData();
158 m_images.Erase( node );
159 m_images.Append( newBitmap );
160 }
161 else
162 {
163 wxList::compatibility_iterator next = node->GetNext();
164 delete node->GetData();
165 m_images.Erase( node );
166 m_images.Insert( next, newBitmap );
167 }
168
169 return true;
170 }
171
172 bool wxImageList::Replace( int index, const wxIcon &bitmap )
173 {
174 wxList::compatibility_iterator node = m_images.Item( index );
175
176 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
177
178 wxIcon* newBitmap = new wxIcon(bitmap) ;
179
180 if (index == (int) m_images.GetCount() - 1)
181 {
182 delete node->GetData();
183 m_images.Erase( node );
184 m_images.Append( newBitmap );
185 }
186 else
187 {
188 wxList::compatibility_iterator next = node->GetNext();
189 delete node->GetData();
190 m_images.Erase( node );
191 m_images.Insert( next, newBitmap );
192 }
193
194 return true;
195 }
196
197 bool wxImageList::Remove( int index )
198 {
199 wxList::compatibility_iterator node = m_images.Item( index );
200
201 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
202
203 delete node->GetData();
204 m_images.Erase( node );
205
206 return true;
207 }
208
209 bool wxImageList::RemoveAll()
210 {
211 WX_CLEAR_LIST(wxList, m_images);
212 m_images.Clear();
213
214 return true;
215 }
216
217 bool wxImageList::GetSize( int index, int &width, int &height ) const
218 {
219 width = 0;
220 height = 0;
221
222 wxList::compatibility_iterator node = m_images.Item( index );
223
224 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
225
226 wxObject *obj = (wxObject*)node->GetData();
227 if( obj->IsKindOf(CLASSINFO(wxIcon)))
228 {
229 wxIcon *bm = wx_static_cast( wxIcon* , obj ) ;
230 width = bm->GetWidth();
231 height = bm->GetHeight();
232 }
233 else
234 {
235 wxBitmap *bm = wx_static_cast( wxBitmap* , obj ) ;
236 width = bm->GetWidth();
237 height = bm->GetHeight();
238 }
239 return true;
240 }
241
242 bool wxImageList::Draw( int index, wxDC &dc, int x, int y,
243 int flags, bool WXUNUSED(solidBackground) )
244 {
245 wxList::compatibility_iterator node = m_images.Item( index );
246
247 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
248
249 wxObject *obj = (wxObject*)node->GetData();
250 if( obj->IsKindOf(CLASSINFO(wxIcon)))
251 {
252 wxIcon *bm = wx_static_cast( wxIcon* , obj ) ;
253 dc.DrawIcon( *bm , x, y);
254 }
255 else
256 {
257 wxBitmap *bm = wx_static_cast( wxBitmap* , obj ) ;
258 dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
259 }
260
261 return true;
262 }
263
264 #endif // wxUSE_IMAGLIST
265