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