subwin.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/subwin.h
  3. // Purpose: helper for implementing the controls with subwindows
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 2004-12-11
  7. // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MSW_SUBWIN_H_
  11. #define _WX_MSW_SUBWIN_H_
  12. #include "wx/msw/private.h"
  13. // ----------------------------------------------------------------------------
  14. // wxSubwindows contains all HWNDs making part of a single wx control
  15. // ----------------------------------------------------------------------------
  16. class WXDLLIMPEXP_CORE wxSubwindows
  17. {
  18. public:
  19. // the number of subwindows can be specified either as parameter to ctor or
  20. // later in Create()
  21. wxSubwindows(size_t n = 0) { Init(); if ( n ) Create(n); }
  22. // allocate enough space for the given number of windows
  23. void Create(size_t n)
  24. {
  25. wxASSERT_MSG( !m_hwnds, wxT("Create() called twice?") );
  26. m_count = n;
  27. m_hwnds = (HWND *)calloc(n, sizeof(HWND));
  28. m_ids = new wxWindowIDRef[n];
  29. }
  30. // non-virtual dtor, this class is not supposed to be used polymorphically
  31. ~wxSubwindows()
  32. {
  33. for ( size_t n = 0; n < m_count; n++ )
  34. {
  35. if ( m_hwnds[n] )
  36. ::DestroyWindow(m_hwnds[n]);
  37. }
  38. free(m_hwnds);
  39. delete [] m_ids;
  40. }
  41. // get the number of subwindows
  42. size_t GetCount() const { return m_count; }
  43. // access a given window
  44. HWND& Get(size_t n)
  45. {
  46. wxASSERT_MSG( n < m_count, wxT("subwindow index out of range") );
  47. return m_hwnds[n];
  48. }
  49. HWND operator[](size_t n) const
  50. {
  51. return const_cast<wxSubwindows *>(this)->Get(n);
  52. }
  53. // initialize the given window: id will be stored in wxWindowIDRef ensuring
  54. // that it is not reused while this object exists
  55. void Set(size_t n, HWND hwnd, wxWindowID id)
  56. {
  57. wxASSERT_MSG( n < m_count, wxT("subwindow index out of range") );
  58. m_hwnds[n] = hwnd;
  59. m_ids[n] = id;
  60. }
  61. // check if we have this window
  62. bool HasWindow(HWND hwnd)
  63. {
  64. for ( size_t n = 0; n < m_count; n++ )
  65. {
  66. if ( m_hwnds[n] == hwnd )
  67. return true;
  68. }
  69. return false;
  70. }
  71. // methods which are forwarded to all subwindows
  72. // ---------------------------------------------
  73. // show/hide everything
  74. void Show(bool show)
  75. {
  76. int sw = show ? SW_SHOW : SW_HIDE;
  77. for ( size_t n = 0; n < m_count; n++ )
  78. {
  79. if ( m_hwnds[n] )
  80. ::ShowWindow(m_hwnds[n], sw);
  81. }
  82. }
  83. // enable/disable everything
  84. void Enable(bool enable)
  85. {
  86. for ( size_t n = 0; n < m_count; n++ )
  87. {
  88. if ( m_hwnds[n] )
  89. ::EnableWindow(m_hwnds[n], enable);
  90. }
  91. }
  92. // set font for all windows
  93. void SetFont(const wxFont& font)
  94. {
  95. HFONT hfont = GetHfontOf(font);
  96. wxCHECK_RET( hfont, wxT("invalid font") );
  97. for ( size_t n = 0; n < m_count; n++ )
  98. {
  99. if ( m_hwnds[n] )
  100. {
  101. ::SendMessage(m_hwnds[n], WM_SETFONT, (WPARAM)hfont, 0);
  102. // otherwise the window might not be redrawn correctly
  103. ::InvalidateRect(m_hwnds[n], NULL, FALSE /* don't erase bg */);
  104. }
  105. }
  106. }
  107. // find the bounding box for all windows
  108. wxRect GetBoundingBox() const
  109. {
  110. wxRect r;
  111. for ( size_t n = 0; n < m_count; n++ )
  112. {
  113. if ( m_hwnds[n] )
  114. {
  115. RECT rc;
  116. ::GetWindowRect(m_hwnds[n], &rc);
  117. r.Union(wxRectFromRECT(rc));
  118. }
  119. }
  120. return r;
  121. }
  122. private:
  123. void Init()
  124. {
  125. m_count = 0;
  126. m_hwnds = NULL;
  127. }
  128. // number of elements in m_hwnds array
  129. size_t m_count;
  130. // the HWNDs we contain
  131. HWND *m_hwnds;
  132. // the IDs of the windows
  133. wxWindowIDRef *m_ids;
  134. wxDECLARE_NO_COPY_CLASS(wxSubwindows);
  135. };
  136. // convenient macro to forward a few methods which are usually propagated to
  137. // subwindows to a wxSubwindows object
  138. //
  139. // parameters should be:
  140. // - cname the name of the class implementing these methods
  141. // - base the name of its base class
  142. // - subwins the name of the member variable of type wxSubwindows *
  143. #define WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(cname, base, subwins) \
  144. bool cname::ContainsHWND(WXHWND hWnd) const \
  145. { \
  146. return subwins && subwins->HasWindow((HWND)hWnd); \
  147. } \
  148. \
  149. bool cname::Show(bool show) \
  150. { \
  151. if ( !base::Show(show) ) \
  152. return false; \
  153. \
  154. if ( subwins ) \
  155. subwins->Show(show); \
  156. \
  157. return true; \
  158. } \
  159. \
  160. bool cname::Enable(bool enable) \
  161. { \
  162. if ( !base::Enable(enable) ) \
  163. return false; \
  164. \
  165. if ( subwins ) \
  166. subwins->Enable(enable); \
  167. \
  168. return true; \
  169. } \
  170. \
  171. bool cname::SetFont(const wxFont& font) \
  172. { \
  173. if ( !base::SetFont(font) ) \
  174. return false; \
  175. \
  176. if ( subwins ) \
  177. subwins->SetFont(font); \
  178. \
  179. return true; \
  180. }
  181. #endif // _WX_MSW_SUBWIN_H_