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