Added GetBitmap, GetIcon to wxImageList
[wxWidgets.git] / src / generic / 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "imaglist.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/defs.h"
22
23 #ifndef __WXPALMOS__
24
25 #include "wx/generic/imaglist.h"
26
27 #include "wx/icon.h"
28 #include "wx/image.h"
29 #include "wx/dc.h"
30
31 //-----------------------------------------------------------------------------
32 // wxImageList
33 //-----------------------------------------------------------------------------
34
35 IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject)
36
37 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
38 /*
39 * wxImageList has to be a real class or we have problems with
40 * the run-time information.
41 */
42
43 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList)
44 #endif
45
46 wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount )
47 {
48 (void)Create(width, height, mask, initialCount);
49 }
50
51 wxGenericImageList::~wxGenericImageList()
52 {
53 (void)RemoveAll();
54 }
55
56 int wxGenericImageList::GetImageCount() const
57 {
58 return m_images.GetCount();
59 }
60
61 bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
62 {
63 m_width = width;
64 m_height = height;
65
66 return Create();
67 }
68
69 bool wxGenericImageList::Create()
70 {
71 return true;
72 }
73
74 int wxGenericImageList::Add( const wxBitmap &bitmap )
75 {
76 wxASSERT_MSG( (bitmap.GetWidth() == m_width && bitmap.GetHeight() == m_height)
77 || (m_width == 0 && m_height == 0),
78 _T("invalid bitmap size in wxImageList: this might work ")
79 _T("on this platform but definitely won't under Windows.") );
80
81 if (bitmap.IsKindOf(CLASSINFO(wxIcon)))
82 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
83 else
84 m_images.Append( new wxBitmap(bitmap) );
85
86 if (m_width == 0 && m_height == 0)
87 {
88 m_width = bitmap.GetWidth();
89 m_height = bitmap.GetHeight();
90 }
91
92 return m_images.GetCount()-1;
93 }
94
95 int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
96 {
97 wxBitmap bmp(bitmap);
98 if (mask.Ok())
99 bmp.SetMask(new wxMask(mask));
100 return Add(bmp);
101 }
102
103 int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
104 {
105 wxImage img = bitmap.ConvertToImage();
106 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
107 return Add(wxBitmap(img));
108 }
109
110 const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
111 {
112 wxList::compatibility_iterator node = m_images.Item( index );
113
114 wxCHECK_MSG( node, (wxBitmap *) NULL, wxT("wrong index in image list") );
115
116 return (wxBitmap*)node->GetData();
117 }
118
119 // Get the bitmap
120 wxBitmap wxImageList::GetBitmap(int index) const
121 {
122 const wxBitmap* bmp = GetBitmapPtr(index);
123 if (bmp)
124 return *bmp;
125 else
126 return wxNullBitmap;
127 }
128
129 // Get the icon
130 wxIcon wxImageList::GetIcon(int index) const
131 {
132 const wxBitmap* bmp = GetBitmapPtr(index);
133 if (bmp)
134 {
135 wxIcon icon;
136 icon.CopyFromBitmap(*bmp);
137 return icon;
138 }
139 else
140 return wxNullIcon;
141 }
142
143 bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
144 {
145 wxList::compatibility_iterator node = m_images.Item( index );
146
147 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
148
149 wxBitmap* newBitmap = (bitmap.IsKindOf(CLASSINFO(wxIcon))) ?
150 #if defined(__VISAGECPP__)
151 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
152 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
153 new wxBitmap(bitmap)
154 #else
155 new wxBitmap( (const wxIcon&) bitmap )
156 #endif
157 : new wxBitmap(bitmap) ;
158
159 if (index == (int) m_images.GetCount() - 1)
160 {
161 delete node->GetData();
162 m_images.Erase( node );
163 m_images.Append( newBitmap );
164 }
165 else
166 {
167 wxList::compatibility_iterator next = node->GetNext();
168 delete node->GetData();
169 m_images.Erase( node );
170 m_images.Insert( next, newBitmap );
171 }
172
173 return true;
174 }
175
176 bool wxGenericImageList::Remove( int index )
177 {
178 wxList::compatibility_iterator node = m_images.Item( index );
179
180 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
181
182 delete node->GetData();
183 m_images.Erase( node );
184
185 return true;
186 }
187
188 bool wxGenericImageList::RemoveAll()
189 {
190 WX_CLEAR_LIST(wxList, m_images);
191 m_images.Clear();
192
193 return true;
194 }
195
196 bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
197 {
198 width = 0;
199 height = 0;
200
201 wxList::compatibility_iterator node = m_images.Item( index );
202
203 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
204
205 wxBitmap *bm = (wxBitmap*)node->GetData();
206 width = bm->GetWidth();
207 height = bm->GetHeight();
208
209 return true;
210 }
211
212 bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
213 int flags, bool WXUNUSED(solidBackground) )
214 {
215 wxList::compatibility_iterator node = m_images.Item( index );
216
217 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
218
219 wxBitmap *bm = (wxBitmap*)node->GetData();
220
221 if (bm->IsKindOf(CLASSINFO(wxIcon)))
222 dc.DrawIcon( * ((wxIcon*) bm), x, y);
223 else
224 dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
225
226 return true;
227 }
228
229 #endif // __WXPALMOS__