imaglist.cpp 7.6 KB

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