]> git.saurik.com Git - wxWidgets.git/blob - include/wx/withimages.h
Don't define __STRICT_ANSI__, we should build both with and without it.
[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 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_WITHIMAGES_H_
11 #define _WX_WITHIMAGES_H_
12
13 #include "wx/defs.h"
14 #include "wx/icon.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 enum
25 {
26 NO_IMAGE = -1
27 };
28
29 wxWithImages()
30 {
31 m_imageList = NULL;
32 m_ownsImageList = false;
33 }
34
35 virtual ~wxWithImages()
36 {
37 FreeIfNeeded();
38 }
39
40 // Sets the image list to use, it is *not* deleted by the control.
41 virtual void SetImageList(wxImageList* imageList)
42 {
43 FreeIfNeeded();
44 m_imageList = imageList;
45 }
46
47 // As SetImageList() but we will delete the image list ourselves.
48 void AssignImageList(wxImageList* imageList)
49 {
50 SetImageList(imageList);
51 m_ownsImageList = true;
52 }
53
54 // Get pointer (may be NULL) to the associated image list.
55 wxImageList* GetImageList() const { return m_imageList; }
56
57 protected:
58 // Return true if we have a valid image list.
59 bool HasImageList() const { return m_imageList != NULL; }
60
61 // Return the image with the given index from the image list.
62 //
63 // If there is no image list or if index == NO_IMAGE, silently returns
64 // wxNullIcon.
65 wxIcon GetImage(int iconIndex) const
66 {
67 return m_imageList && iconIndex != NO_IMAGE
68 ? m_imageList->GetIcon(iconIndex)
69 : wxNullIcon;
70 }
71
72 private:
73 // Free the image list if necessary, i.e. if we own it.
74 void FreeIfNeeded()
75 {
76 if ( m_ownsImageList )
77 {
78 delete m_imageList;
79 m_imageList = NULL;
80
81 // We don't own it any more.
82 m_ownsImageList = false;
83 }
84 }
85
86
87 // The associated image list or NULL.
88 wxImageList* m_imageList;
89
90 // False by default, if true then we delete m_imageList.
91 bool m_ownsImageList;
92
93 wxDECLARE_NO_COPY_CLASS(wxWithImages);
94 };
95
96 #endif // _WX_WITHIMAGES_H_