]> git.saurik.com Git - wxWidgets.git/blame - src/generic/imaglist.cpp
Handle Shift-TAB correctly in wxOSX/Carbon wxComboBox.
[wxWidgets.git] / src / generic / imaglist.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/generic/imaglist.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
0e1381f2 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
1e6d9499
JS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
93763ad5 14 #pragma hdrstop
1e6d9499
JS
15#endif
16
9cd7a3f7 17#if wxUSE_IMAGLIST && !defined(wxHAS_NATIVE_IMAGELIST)
9de05dfd 18
8a3e173a 19#include "wx/imaglist.h"
9de05dfd 20
da80ae71
WS
21#ifndef WX_PRECOMP
22 #include "wx/dc.h"
923d28da 23 #include "wx/icon.h"
155ecd4c 24 #include "wx/image.h"
da80ae71
WS
25#endif
26
c801d85f
KB
27//-----------------------------------------------------------------------------
28// wxImageList
29//-----------------------------------------------------------------------------
30
3cd94a0d 31IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject)
b31989e2 32IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList)
b31989e2 33
3cd94a0d 34wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount )
c801d85f 35{
f6bcfd97 36 (void)Create(width, height, mask, initialCount);
e1e955e1 37}
c801d85f 38
3cd94a0d 39wxGenericImageList::~wxGenericImageList()
c801d85f 40{
3fc93ebd 41 (void)RemoveAll();
e1e955e1 42}
c801d85f 43
3cd94a0d 44int wxGenericImageList::GetImageCount() const
c801d85f 45{
b1d4dd7a 46 return m_images.GetCount();
e1e955e1 47}
c801d85f 48
3cd94a0d 49bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
f6bcfd97
BP
50{
51 m_width = width;
52 m_height = height;
53
54 return Create();
55}
56
3cd94a0d 57bool wxGenericImageList::Create()
c801d85f 58{
ca65c044 59 return true;
e1e955e1 60}
c801d85f 61
3cd94a0d 62int wxGenericImageList::Add( const wxBitmap &bitmap )
c801d85f 63{
5b9003ae 64 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
b931414d 65 || (m_width == 0 && m_height == 0),
9a83f860
VZ
66 wxT("invalid bitmap size in wxImageList: this might work ")
67 wxT("on this platform but definitely won't under Windows.") );
9bd41907 68
0e1381f2
PC
69 const int index = int(m_images.GetCount());
70
80a46597 71 if (bitmap.IsKindOf(wxCLASSINFO(wxIcon)))
5b9003ae 72 {
f60d0f94 73 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
5b9003ae 74 }
f60d0f94 75 else
5b9003ae 76 {
4c51a665 77 // Mimic behaviour of Windows ImageList_Add that automatically breaks up the added
5b9003ae
RR
78 // bitmap into sub-images of the correct size
79 if (m_width > 0 && bitmap.GetWidth() > m_width && bitmap.GetHeight() >= m_height)
80 {
81 int numImages = bitmap.GetWidth() / m_width;
82 for (int subIndex = 0; subIndex < numImages; subIndex++)
83 {
84 wxRect rect(m_width * subIndex, 0, m_width, m_height);
85 wxBitmap tmpBmp = bitmap.GetSubBitmap(rect);
86 m_images.Append( new wxBitmap(tmpBmp) );
87 }
88 }
89 else
90 {
91 m_images.Append( new wxBitmap(bitmap) );
92 }
93 }
b931414d
RD
94
95 if (m_width == 0 && m_height == 0)
96 {
97 m_width = bitmap.GetWidth();
98 m_height = bitmap.GetHeight();
99 }
93763ad5 100
0e1381f2 101 return index;
e1e955e1 102}
c801d85f 103
3cd94a0d 104int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
4d449473
VS
105{
106 wxBitmap bmp(bitmap);
a1b806b9 107 if (mask.IsOk())
a7f1fbf6 108 bmp.SetMask(new wxMask(mask));
4d449473
VS
109 return Add(bmp);
110}
111
3cd94a0d 112int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
4d449473 113{
368d59f0 114 wxImage img = bitmap.ConvertToImage();
4d449473 115 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
368d59f0 116 return Add(wxBitmap(img));
4d449473
VS
117}
118
49bf4e3e 119const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
b46e8696 120{
a1abd1a9 121 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 122
d3b9f782 123 wxCHECK_MSG( node, NULL, wxT("wrong index in image list") );
43543d98 124
b1d4dd7a 125 return (wxBitmap*)node->GetData();
40413a5b 126}
43543d98 127
49bf4e3e 128// Get the bitmap
f45fac95 129wxBitmap wxGenericImageList::GetBitmap(int index) const
49bf4e3e
JS
130{
131 const wxBitmap* bmp = GetBitmapPtr(index);
132 if (bmp)
133 return *bmp;
134 else
135 return wxNullBitmap;
136}
137
138// Get the icon
f45fac95 139wxIcon wxGenericImageList::GetIcon(int index) const
49bf4e3e
JS
140{
141 const wxBitmap* bmp = GetBitmapPtr(index);
142 if (bmp)
143 {
144 wxIcon icon;
145 icon.CopyFromBitmap(*bmp);
146 return icon;
147 }
148 else
149 return wxNullIcon;
150}
151
3cd94a0d 152bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
c801d85f 153{
a1abd1a9 154 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 155
ca65c044 156 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
f60d0f94 157
80a46597 158 wxBitmap* newBitmap = (bitmap.IsKindOf(wxCLASSINFO(wxIcon))) ?
999836aa
VZ
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)
162 new wxBitmap(bitmap)
163 #else
164 new wxBitmap( (const wxIcon&) bitmap )
165 #endif
166 : new wxBitmap(bitmap) ;
f60d0f94 167
b1d4dd7a 168 if (index == (int) m_images.GetCount() - 1)
b46e8696 169 {
222ed1d6
MB
170 delete node->GetData();
171 m_images.Erase( node );
f60d0f94 172 m_images.Append( newBitmap );
b46e8696
RR
173 }
174 else
175 {
a1abd1a9 176 wxObjectList::compatibility_iterator next = node->GetNext();
222ed1d6
MB
177 delete node->GetData();
178 m_images.Erase( node );
f60d0f94 179 m_images.Insert( next, newBitmap );
b46e8696 180 }
43543d98 181
ca65c044 182 return true;
e1e955e1 183}
c801d85f 184
f644bc11
RR
185bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap, const wxBitmap &mask )
186{
a1abd1a9 187 wxObjectList::compatibility_iterator node = m_images.Item( index );
f644bc11
RR
188
189 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
190
80a46597 191 wxBitmap* newBitmap = (bitmap.IsKindOf(wxCLASSINFO(wxIcon))) ?
f644bc11
RR
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)
195 new wxBitmap(bitmap)
196 #else
197 new wxBitmap( (const wxIcon&) bitmap )
198 #endif
199 : new wxBitmap(bitmap) ;
200
201 if (index == (int) m_images.GetCount() - 1)
202 {
203 delete node->GetData();
204 m_images.Erase( node );
205 m_images.Append( newBitmap );
206 }
207 else
208 {
a1abd1a9 209 wxObjectList::compatibility_iterator next = node->GetNext();
f644bc11
RR
210 delete node->GetData();
211 m_images.Erase( node );
212 m_images.Insert( next, newBitmap );
213 }
da80ae71 214
a1b806b9 215 if (mask.IsOk())
f644bc11
RR
216 newBitmap->SetMask(new wxMask(mask));
217
218 return true;
219}
220
3cd94a0d 221bool wxGenericImageList::Remove( int index )
c801d85f 222{
a1abd1a9 223 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 224
ca65c044 225 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 226
222ed1d6
MB
227 delete node->GetData();
228 m_images.Erase( node );
43543d98 229
ca65c044 230 return true;
e1e955e1 231}
c801d85f 232
3cd94a0d 233bool wxGenericImageList::RemoveAll()
c801d85f 234{
a1abd1a9 235 WX_CLEAR_LIST(wxObjectList, m_images);
b46e8696 236 m_images.Clear();
43543d98 237
ca65c044 238 return true;
e1e955e1 239}
c801d85f 240
3cd94a0d 241bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
c801d85f 242{
b46e8696
RR
243 width = 0;
244 height = 0;
43543d98 245
a1abd1a9 246 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 247
ca65c044 248 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 249
b1d4dd7a 250 wxBitmap *bm = (wxBitmap*)node->GetData();
c801d85f
KB
251 width = bm->GetWidth();
252 height = bm->GetHeight();
43543d98 253
ca65c044 254 return true;
e1e955e1 255}
c801d85f 256
3cd94a0d 257bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
219f895a 258 int flags, bool WXUNUSED(solidBackground) )
c801d85f 259{
a1abd1a9 260 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 261
ca65c044 262 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 263
b1d4dd7a 264 wxBitmap *bm = (wxBitmap*)node->GetData();
f60d0f94 265
80a46597 266 if (bm->IsKindOf(wxCLASSINFO(wxIcon)))
f60d0f94
JS
267 dc.DrawIcon( * ((wxIcon*) bm), x, y);
268 else
269 dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
219f895a 270
ca65c044 271 return true;
e1e955e1 272}
c801d85f 273
da80ae71 274#endif // wxUSE_IMAGLIST