1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/imaglist.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
16 #if wxUSE_IMAGLIST && !defined(wxHAS_NATIVE_IMAGELIST)
18 #include "wx/imaglist.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList
, wxObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxGenericImageList
)
33 wxGenericImageList::wxGenericImageList( int width
, int height
, bool mask
, int initialCount
)
35 (void)Create(width
, height
, mask
, initialCount
);
38 wxGenericImageList::~wxGenericImageList()
43 int wxGenericImageList::GetImageCount() const
45 return m_images
.GetCount();
48 bool wxGenericImageList::Create( int width
, int height
, bool WXUNUSED(mask
), int WXUNUSED(initialCount
) )
56 bool wxGenericImageList::Create()
61 int wxGenericImageList::Add( const wxBitmap
&bitmap
)
63 wxASSERT_MSG( (bitmap
.GetWidth() >= m_width
&& bitmap
.GetHeight() == m_height
)
64 || (m_width
== 0 && m_height
== 0),
65 wxT("invalid bitmap size in wxImageList: this might work ")
66 wxT("on this platform but definitely won't under Windows.") );
68 const int index
= int(m_images
.GetCount());
70 if (bitmap
.IsKindOf(wxCLASSINFO(wxIcon
)))
72 m_images
.Append( new wxIcon( (const wxIcon
&) bitmap
) );
76 // Mimic behaviour of Windows ImageList_Add that automatically breaks up the added
77 // bitmap into sub-images of the correct size
78 if (m_width
> 0 && bitmap
.GetWidth() > m_width
&& bitmap
.GetHeight() >= m_height
)
80 int numImages
= bitmap
.GetWidth() / m_width
;
81 for (int subIndex
= 0; subIndex
< numImages
; subIndex
++)
83 wxRect
rect(m_width
* subIndex
, 0, m_width
, m_height
);
84 wxBitmap tmpBmp
= bitmap
.GetSubBitmap(rect
);
85 m_images
.Append( new wxBitmap(tmpBmp
) );
90 m_images
.Append( new wxBitmap(bitmap
) );
94 if (m_width
== 0 && m_height
== 0)
96 m_width
= bitmap
.GetWidth();
97 m_height
= bitmap
.GetHeight();
103 int wxGenericImageList::Add( const wxBitmap
& bitmap
, const wxBitmap
& mask
)
105 wxBitmap
bmp(bitmap
);
107 bmp
.SetMask(new wxMask(mask
));
111 int wxGenericImageList::Add( const wxBitmap
& bitmap
, const wxColour
& maskColour
)
113 wxImage img
= bitmap
.ConvertToImage();
114 img
.SetMaskColour(maskColour
.Red(), maskColour
.Green(), maskColour
.Blue());
115 return Add(wxBitmap(img
));
118 const wxBitmap
*wxGenericImageList::GetBitmapPtr( int index
) const
120 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
122 wxCHECK_MSG( node
, NULL
, wxT("wrong index in image list") );
124 return (wxBitmap
*)node
->GetData();
128 wxBitmap
wxGenericImageList::GetBitmap(int index
) const
130 const wxBitmap
* bmp
= GetBitmapPtr(index
);
138 wxIcon
wxGenericImageList::GetIcon(int index
) const
140 const wxBitmap
* bmp
= GetBitmapPtr(index
);
144 icon
.CopyFromBitmap(*bmp
);
151 bool wxGenericImageList::Replace( int index
, const wxBitmap
&bitmap
)
153 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
155 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
157 wxBitmap
* newBitmap
= (bitmap
.IsKindOf(wxCLASSINFO(wxIcon
))) ?
158 #if defined(__VISAGECPP__)
159 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
160 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
163 new wxBitmap( (const wxIcon
&) bitmap
)
165 : new wxBitmap(bitmap
) ;
167 if (index
== (int) m_images
.GetCount() - 1)
169 delete node
->GetData();
170 m_images
.Erase( node
);
171 m_images
.Append( newBitmap
);
175 wxObjectList::compatibility_iterator next
= node
->GetNext();
176 delete node
->GetData();
177 m_images
.Erase( node
);
178 m_images
.Insert( next
, newBitmap
);
184 bool wxGenericImageList::Replace( int index
, const wxBitmap
&bitmap
, const wxBitmap
&mask
)
186 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
188 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
190 wxBitmap
* newBitmap
= (bitmap
.IsKindOf(wxCLASSINFO(wxIcon
))) ?
191 #if defined(__VISAGECPP__)
192 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
193 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
196 new wxBitmap( (const wxIcon
&) bitmap
)
198 : new wxBitmap(bitmap
) ;
200 if (index
== (int) m_images
.GetCount() - 1)
202 delete node
->GetData();
203 m_images
.Erase( node
);
204 m_images
.Append( newBitmap
);
208 wxObjectList::compatibility_iterator next
= node
->GetNext();
209 delete node
->GetData();
210 m_images
.Erase( node
);
211 m_images
.Insert( next
, newBitmap
);
215 newBitmap
->SetMask(new wxMask(mask
));
220 bool wxGenericImageList::Remove( int index
)
222 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
224 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
226 delete node
->GetData();
227 m_images
.Erase( node
);
232 bool wxGenericImageList::RemoveAll()
234 WX_CLEAR_LIST(wxObjectList
, m_images
);
240 bool wxGenericImageList::GetSize( int index
, int &width
, int &height
) const
245 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
247 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
249 wxBitmap
*bm
= (wxBitmap
*)node
->GetData();
250 width
= bm
->GetWidth();
251 height
= bm
->GetHeight();
256 bool wxGenericImageList::Draw( int index
, wxDC
&dc
, int x
, int y
,
257 int flags
, bool WXUNUSED(solidBackground
) )
259 wxObjectList::compatibility_iterator node
= m_images
.Item( index
);
261 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
263 wxBitmap
*bm
= (wxBitmap
*)node
->GetData();
265 if (bm
->IsKindOf(wxCLASSINFO(wxIcon
)))
266 dc
.DrawIcon( * ((wxIcon
*) bm
), x
, y
);
268 dc
.DrawBitmap( *bm
, x
, y
, (flags
& wxIMAGELIST_DRAW_TRANSPARENT
) > 0 );
273 #endif // wxUSE_IMAGLIST