1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/imaglist.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #if wxUSE_IMAGLIST && !defined(wxHAS_NATIVE_IMAGELIST)
19 #include "wx/imaglist.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList
, wxObject
)
32 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxGenericImageList
)
34 wxGenericImageList::wxGenericImageList( int width
, int height
, bool mask
, int initialCount
)
36 (void)Create(width
, height
, mask
, initialCount
);
39 wxGenericImageList::~wxGenericImageList()
44 int wxGenericImageList::GetImageCount() const
46 return m_images
.GetCount();
49 bool wxGenericImageList::Create( int width
, int height
, bool WXUNUSED(mask
), int WXUNUSED(initialCount
) )
57 bool wxGenericImageList::Create()
62 int wxGenericImageList::Add( const wxBitmap
&bitmap
)
64 wxASSERT_MSG( (bitmap
.GetWidth() >= m_width
&& bitmap
.GetHeight() == m_height
)
65 || (m_width
== 0 && m_height
== 0),
66 wxT("invalid bitmap size in wxImageList: this might work ")
67 wxT("on this platform but definitely won't under Windows.") );
69 const int index
= int(m_images
.GetCount());
71 if (bitmap
.IsKindOf(CLASSINFO(wxIcon
)))
73 m_images
.Append( new wxIcon( (const wxIcon
&) bitmap
) );
77 // Mimic behaviour of Windows ImageList_Add that automatically breaks up the added
78 // bitmap into sub-images of the correct size
79 if (m_width
> 0 && bitmap
.GetWidth() > m_width
&& bitmap
.GetHeight() >= m_height
)
81 int numImages
= bitmap
.GetWidth() / m_width
;
82 for (int subIndex
= 0; subIndex
< numImages
; subIndex
++)
84 wxRect
rect(m_width
* subIndex
, 0, m_width
, m_height
);
85 wxBitmap tmpBmp
= bitmap
.GetSubBitmap(rect
);
86 m_images
.Append( new wxBitmap(tmpBmp
) );
91 m_images
.Append( new wxBitmap(bitmap
) );
95 if (m_width
== 0 && m_height
== 0)
97 m_width
= bitmap
.GetWidth();
98 m_height
= bitmap
.GetHeight();
104 int wxGenericImageList::Add( const wxBitmap
& bitmap
, const wxBitmap
& mask
)
106 wxBitmap
bmp(bitmap
);
108 bmp
.SetMask(new wxMask(mask
));
112 int wxGenericImageList::Add( const wxBitmap
& bitmap
, const wxColour
& maskColour
)
114 wxImage img
= bitmap
.ConvertToImage();
115 img
.SetMaskColour(maskColour
.Red(), maskColour
.Green(), maskColour
.Blue());
116 return Add(wxBitmap(img
));
119 const wxBitmap
*wxGenericImageList::GetBitmapPtr( int index
) const
121 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
123 wxCHECK_MSG( node
, NULL
, wxT("wrong index in image list") );
125 return (wxBitmap
*)node
->GetData();
129 wxBitmap
wxGenericImageList::GetBitmap(int index
) const
131 const wxBitmap
* bmp
= GetBitmapPtr(index
);
139 wxIcon
wxGenericImageList::GetIcon(int index
) const
141 const wxBitmap
* bmp
= GetBitmapPtr(index
);
145 icon
.CopyFromBitmap(*bmp
);
152 bool wxGenericImageList::Replace( int index
, const wxBitmap
&bitmap
)
154 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
156 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
158 wxBitmap
* newBitmap
= (bitmap
.IsKindOf(CLASSINFO(wxIcon
))) ?
159 #if defined(__VISAGECPP__)
160 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
161 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
164 new wxBitmap( (const wxIcon
&) bitmap
)
166 : new wxBitmap(bitmap
) ;
168 if (index
== (int) m_images
.GetCount() - 1)
170 delete node
->GetData();
171 m_images
.Erase( node
);
172 m_images
.Append( newBitmap
);
176 wxObjectList::compatibility_iterator next
= node
->GetNext();
177 delete node
->GetData();
178 m_images
.Erase( node
);
179 m_images
.Insert( next
, newBitmap
);
185 bool wxGenericImageList::Replace( int index
, const wxBitmap
&bitmap
, const wxBitmap
&mask
)
187 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
189 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
191 wxBitmap
* newBitmap
= (bitmap
.IsKindOf(CLASSINFO(wxIcon
))) ?
192 #if defined(__VISAGECPP__)
193 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
194 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
197 new wxBitmap( (const wxIcon
&) bitmap
)
199 : new wxBitmap(bitmap
) ;
201 if (index
== (int) m_images
.GetCount() - 1)
203 delete node
->GetData();
204 m_images
.Erase( node
);
205 m_images
.Append( newBitmap
);
209 wxObjectList::compatibility_iterator next
= node
->GetNext();
210 delete node
->GetData();
211 m_images
.Erase( node
);
212 m_images
.Insert( next
, newBitmap
);
216 newBitmap
->SetMask(new wxMask(mask
));
221 bool wxGenericImageList::Remove( int index
)
223 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
225 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
227 delete node
->GetData();
228 m_images
.Erase( node
);
233 bool wxGenericImageList::RemoveAll()
235 WX_CLEAR_LIST(wxObjectList
, m_images
);
241 bool wxGenericImageList::GetSize( int index
, int &width
, int &height
) const
246 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
248 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
250 wxBitmap
*bm
= (wxBitmap
*)node
->GetData();
251 width
= bm
->GetWidth();
252 height
= bm
->GetHeight();
257 bool wxGenericImageList::Draw( int index
, wxDC
&dc
, int x
, int y
,
258 int flags
, bool WXUNUSED(solidBackground
) )
260 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
262 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
264 wxBitmap
*bm
= (wxBitmap
*)node
->GetData();
266 if (bm
->IsKindOf(CLASSINFO(wxIcon
)))
267 dc
.DrawIcon( * ((wxIcon
*) bm
), x
, y
);
269 dc
.DrawBitmap( *bm
, x
, y
, (flags
& wxIMAGELIST_DRAW_TRANSPARENT
) > 0 );
274 #endif // wxUSE_IMAGLIST