withimages.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/withimages.h
  3. // Purpose: Declaration of a simple wxWithImages class.
  4. // Author: Vadim Zeitlin
  5. // Created: 2011-08-17
  6. // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_WITHIMAGES_H_
  10. #define _WX_WITHIMAGES_H_
  11. #include "wx/defs.h"
  12. #include "wx/icon.h"
  13. #include "wx/imaglist.h"
  14. // ----------------------------------------------------------------------------
  15. // wxWithImages: mix-in class providing access to wxImageList.
  16. // ----------------------------------------------------------------------------
  17. class WXDLLIMPEXP_CORE wxWithImages
  18. {
  19. public:
  20. enum
  21. {
  22. NO_IMAGE = -1
  23. };
  24. wxWithImages()
  25. {
  26. m_imageList = NULL;
  27. m_ownsImageList = false;
  28. }
  29. virtual ~wxWithImages()
  30. {
  31. FreeIfNeeded();
  32. }
  33. // Sets the image list to use, it is *not* deleted by the control.
  34. virtual void SetImageList(wxImageList* imageList)
  35. {
  36. FreeIfNeeded();
  37. m_imageList = imageList;
  38. }
  39. // As SetImageList() but we will delete the image list ourselves.
  40. void AssignImageList(wxImageList* imageList)
  41. {
  42. SetImageList(imageList);
  43. m_ownsImageList = true;
  44. }
  45. // Get pointer (may be NULL) to the associated image list.
  46. wxImageList* GetImageList() const { return m_imageList; }
  47. protected:
  48. // Return true if we have a valid image list.
  49. bool HasImageList() const { return m_imageList != NULL; }
  50. // Return the image with the given index from the image list.
  51. //
  52. // If there is no image list or if index == NO_IMAGE, silently returns
  53. // wxNullIcon.
  54. wxIcon GetImage(int iconIndex) const
  55. {
  56. return m_imageList && iconIndex != NO_IMAGE
  57. ? m_imageList->GetIcon(iconIndex)
  58. : wxNullIcon;
  59. }
  60. private:
  61. // Free the image list if necessary, i.e. if we own it.
  62. void FreeIfNeeded()
  63. {
  64. if ( m_ownsImageList )
  65. {
  66. delete m_imageList;
  67. m_imageList = NULL;
  68. // We don't own it any more.
  69. m_ownsImageList = false;
  70. }
  71. }
  72. // The associated image list or NULL.
  73. wxImageList* m_imageList;
  74. // False by default, if true then we delete m_imageList.
  75. bool m_ownsImageList;
  76. wxDECLARE_NO_COPY_CLASS(wxWithImages);
  77. };
  78. #endif // _WX_WITHIMAGES_H_