| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/windowid.h |
| 3 | // Purpose: wxWindowID class - a class for managing window ids |
| 4 | // Author: Brian Vanderburg II |
| 5 | // Created: 2007-09-21 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2007 Brian Vanderburg II |
| 8 | // Licence: wxWindows licence |
| 9 | /////////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef _WX_WINDOWID_H_ |
| 12 | #define _WX_WINDOWID_H_ |
| 13 | |
| 14 | // NB: do not include defs.h as we are included from it |
| 15 | |
| 16 | typedef int wxWindowID; |
| 17 | |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | // wxWindowIDRef: reference counted id value |
| 20 | // ---------------------------------------------------------------------------- |
| 21 | |
| 22 | // A wxWindowIDRef object wraps an id value and marks it as (un)used as |
| 23 | // necessary. All ids returned from wxWindow::NewControlId() should be assigned |
| 24 | // to an instance of this class to ensure that the id is marked as being in |
| 25 | // use. |
| 26 | // |
| 27 | // This class is always defined but it is trivial if wxUSE_AUTOID_MANAGEMENT is |
| 28 | // off. |
| 29 | class WXDLLIMPEXP_CORE wxWindowIDRef |
| 30 | { |
| 31 | public: |
| 32 | // default ctor |
| 33 | wxWindowIDRef() |
| 34 | { |
| 35 | m_id = wxID_NONE; |
| 36 | } |
| 37 | |
| 38 | // ctor taking id values |
| 39 | wxWindowIDRef(int id) |
| 40 | { |
| 41 | Init(id); |
| 42 | } |
| 43 | |
| 44 | wxWindowIDRef(long id) |
| 45 | { |
| 46 | Init(id); |
| 47 | } |
| 48 | |
| 49 | wxWindowIDRef(const wxWindowIDRef& id) |
| 50 | { |
| 51 | Init(id.m_id); |
| 52 | } |
| 53 | |
| 54 | // dtor |
| 55 | ~wxWindowIDRef() |
| 56 | { |
| 57 | Assign(wxID_NONE); |
| 58 | } |
| 59 | |
| 60 | // assignment |
| 61 | wxWindowIDRef& operator=(int id) |
| 62 | { |
| 63 | Assign(id); |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | wxWindowIDRef& operator=(long id) |
| 68 | { |
| 69 | Assign(id); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | wxWindowIDRef& operator=(const wxWindowIDRef& id) |
| 74 | { |
| 75 | if (&id != this) |
| 76 | Assign(id.m_id); |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | // access to the stored id value |
| 81 | wxWindowID GetValue() const |
| 82 | { |
| 83 | return m_id; |
| 84 | } |
| 85 | |
| 86 | operator wxWindowID() const |
| 87 | { |
| 88 | return m_id; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | #if wxUSE_AUTOID_MANAGEMENT |
| 93 | // common part of all ctors: call Assign() for our new id |
| 94 | void Init(wxWindowID id) |
| 95 | { |
| 96 | // m_id must be initialized before calling Assign() |
| 97 | m_id = wxID_NONE; |
| 98 | Assign(id); |
| 99 | } |
| 100 | |
| 101 | // increase reference count of id, decrease the one of m_id |
| 102 | void Assign(wxWindowID id); |
| 103 | #else // !wxUSE_AUTOID_MANAGEMENT |
| 104 | // trivial stubs for the functions above |
| 105 | void Init(wxWindowID id) |
| 106 | { |
| 107 | m_id = id; |
| 108 | } |
| 109 | |
| 110 | void Assign(wxWindowID id) |
| 111 | { |
| 112 | m_id = id; |
| 113 | } |
| 114 | #endif // wxUSE_AUTOID_MANAGEMENT/!wxUSE_AUTOID_MANAGEMENT |
| 115 | |
| 116 | |
| 117 | wxWindowID m_id; |
| 118 | }; |
| 119 | |
| 120 | // comparison operators |
| 121 | inline bool operator==(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs) |
| 122 | { |
| 123 | return lhs.GetValue() == rhs.GetValue(); |
| 124 | } |
| 125 | |
| 126 | inline bool operator==(const wxWindowIDRef& lhs, int rhs) |
| 127 | { |
| 128 | return lhs.GetValue() == rhs; |
| 129 | } |
| 130 | |
| 131 | inline bool operator==(const wxWindowIDRef& lhs, long rhs) |
| 132 | { |
| 133 | return lhs.GetValue() == rhs; |
| 134 | } |
| 135 | |
| 136 | inline bool operator==(int lhs, const wxWindowIDRef& rhs) |
| 137 | { |
| 138 | return rhs == lhs; |
| 139 | } |
| 140 | |
| 141 | inline bool operator==(long lhs, const wxWindowIDRef& rhs) |
| 142 | { |
| 143 | return rhs == lhs; |
| 144 | } |
| 145 | |
| 146 | inline bool operator!=(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs) |
| 147 | { |
| 148 | return !(lhs == rhs); |
| 149 | } |
| 150 | |
| 151 | inline bool operator!=(const wxWindowIDRef& lhs, int rhs) |
| 152 | { |
| 153 | return !(lhs == rhs); |
| 154 | } |
| 155 | |
| 156 | inline bool operator!=(const wxWindowIDRef& lhs, long rhs) |
| 157 | { |
| 158 | return !(lhs == rhs); |
| 159 | } |
| 160 | |
| 161 | inline bool operator!=(int lhs, const wxWindowIDRef& rhs) |
| 162 | { |
| 163 | return !(lhs == rhs); |
| 164 | } |
| 165 | |
| 166 | inline bool operator!=(long lhs, const wxWindowIDRef& rhs) |
| 167 | { |
| 168 | return !(lhs == rhs); |
| 169 | } |
| 170 | |
| 171 | // ---------------------------------------------------------------------------- |
| 172 | // wxIdManager |
| 173 | // ---------------------------------------------------------------------------- |
| 174 | |
| 175 | class WXDLLIMPEXP_CORE wxIdManager |
| 176 | { |
| 177 | public: |
| 178 | // This returns an id value and not an wxWindowIDRef. The returned value |
| 179 | // should be assigned a.s.a.p to a wxWindowIDRef. The IDs are marked as |
| 180 | // reserved so that another call to ReserveId before assigning the id to a |
| 181 | // wxWindowIDRef will not use the same ID |
| 182 | static wxWindowID ReserveId(int count = 1); |
| 183 | |
| 184 | // This will release an unused reserved ID. This should only be called |
| 185 | // if the ID returned by ReserveId was NOT assigned to a wxWindowIDRef |
| 186 | // for some purpose, maybe an early return from a function |
| 187 | static void UnreserveId(wxWindowID id, int count = 1); |
| 188 | }; |
| 189 | |
| 190 | #endif // _WX_WINDOWID_H_ |