windowid.h 4.4 KB

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