]> git.saurik.com Git - wxWidgets.git/blame - include/wx/withimages.h
using Run of base class
[wxWidgets.git] / include / wx / withimages.h
CommitLineData
abfdefed
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/withimages.h
3// Purpose: Declaration of a simple wxWithImages class.
4// Author: Vadim Zeitlin
5// Created: 2011-08-17
abfdefed
VZ
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"
665ec08c 14#include "wx/icon.h"
abfdefed
VZ
15#include "wx/imaglist.h"
16
17// ----------------------------------------------------------------------------
18// wxWithImages: mix-in class providing access to wxImageList.
19// ----------------------------------------------------------------------------
20
21class WXDLLIMPEXP_CORE wxWithImages
22{
23public:
1871b9fa
VZ
24 enum
25 {
26 NO_IMAGE = -1
27 };
28
abfdefed
VZ
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
57protected:
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 //
1871b9fa 63 // If there is no image list or if index == NO_IMAGE, silently returns
abfdefed
VZ
64 // wxNullIcon.
65 wxIcon GetImage(int iconIndex) const
66 {
1871b9fa
VZ
67 return m_imageList && iconIndex != NO_IMAGE
68 ? m_imageList->GetIcon(iconIndex)
69 : wxNullIcon;
abfdefed
VZ
70 }
71
72private:
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_