You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
7.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/window/window_enable_scope.hpp (originally MWWndScp.h; ::SynesisWin)
  3. *
  4. * Purpose: Window enable-state scoping class.
  5. *
  6. * Created: 5th January 1996
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1996-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/window/window_enable_scope.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::window_enable_scope class
  42. * (\ref group__library__windows_window "Windows Window" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_ENABLE_SCOPE
  45. #define WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_ENABLE_SCOPE
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_ENABLE_SCOPE_MAJOR 4
  48. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_ENABLE_SCOPE_MINOR 1
  49. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_ENABLE_SCOPE_REVISION 1
  50. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_ENABLE_SCOPE_EDIT 90
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  56. # include <winstl/winstl.h>
  57. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  58. #ifndef WINSTL_INCL_SHIMS_ATTRIBUTE_HPP_GET_HWND
  59. # include <winstl/shims/attribute/get_HWND.hpp>
  60. #endif /* !WINSTL_INCL_SHIMS_ATTRIBUTE_HPP_GET_HWND */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _WINSTL_NO_NAMESPACE
  65. # if defined(_STLSOFT_NO_NAMESPACE) || \
  66. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  67. /* There is no stlsoft namespace, so must define ::winstl */
  68. namespace winstl
  69. {
  70. # else
  71. /* Define stlsoft::winstl_project */
  72. namespace stlsoft
  73. {
  74. namespace winstl_project
  75. {
  76. # endif /* _STLSOFT_NO_NAMESPACE */
  77. #endif /* !_WINSTL_NO_NAMESPACE */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Classes
  80. */
  81. // window_enable_scope
  82. /** \brief Provides scoping of the enable status of a window.
  83. *
  84. * \ingroup group__library__windows_window
  85. *
  86. * This class provides scoping of the enable status of a window via the API
  87. * function EnableWindow().
  88. */
  89. class window_enable_scope
  90. {
  91. public:
  92. /// This type
  93. typedef window_enable_scope class_type;
  94. // Construction
  95. public:
  96. /// \brief Toggles the window enable state
  97. ///
  98. /// Takes a HWND and changes it's current enable-status, which is set back to
  99. /// the original state in the destructor.
  100. ///
  101. /// \param wnd The window whose enable state is to be controlled
  102. ss_explicit_k window_enable_scope(HWND wnd)
  103. : m_hwnd(wnd)
  104. , m_bEnableOnDtor(::IsWindowEnabled(m_hwnd) != false)
  105. {
  106. ::EnableWindow(m_hwnd, !m_bEnableOnDtor);
  107. }
  108. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  109. /// \brief Toggles the window enable state
  110. ///
  111. /// Takes a HWND and changes it's current enable-status, which is set back to
  112. /// the original state in the destructor.
  113. ///
  114. /// \param wnd The window whose enable state is to be controlled
  115. template <ss_typename_param_k W>
  116. ss_explicit_k window_enable_scope(W &wnd)
  117. : m_hwnd(get_HWND(wnd))
  118. , m_bEnableOnDtor(::IsWindowEnabled(m_hwnd) != false)
  119. {
  120. ::EnableWindow(m_hwnd, !m_bEnableOnDtor);
  121. }
  122. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  123. /// Modifies the window enable state
  124. ///
  125. /// \param wnd The window whose enable state is to be controlled
  126. /// \param bEnableOnCtor The state to set in the constructor
  127. /// \param bEnableOnDtor The state it is reset to in the destructor
  128. window_enable_scope(HWND wnd, ws_bool_t bEnableOnCtor, ws_bool_t bEnableOnDtor)
  129. : m_hwnd(wnd)
  130. , m_bEnableOnDtor(bEnableOnDtor)
  131. {
  132. ::EnableWindow(m_hwnd, bEnableOnCtor);
  133. }
  134. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  135. /// Modifies the window enable state
  136. ///
  137. /// \param wnd The window whose enable state is to be controlled
  138. /// \param bEnableOnCtor The state to set in the constructor
  139. /// \param bEnableOnDtor The state it is reset to in the destructor
  140. template <ss_typename_param_k W>
  141. window_enable_scope(W &wnd, ws_bool_t bEnableOnCtor, ws_bool_t bEnableOnDtor)
  142. : m_hwnd(get_HWND(wnd))
  143. , m_bEnableOnDtor(bEnableOnDtor)
  144. {
  145. ::EnableWindow(m_hwnd, bEnableOnCtor);
  146. }
  147. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  148. /// Resets the enable status
  149. ~window_enable_scope() stlsoft_throw_0()
  150. {
  151. #ifdef STLSOFT_CF_USE_RAW_OFFSETOF_IN_STATIC_ASSERT
  152. WINSTL_STATIC_ASSERT(STLSOFT_RAW_OFFSETOF(class_type, m_hwnd) < STLSOFT_RAW_OFFSETOF(class_type, m_bEnableOnDtor));
  153. #endif /* STLSOFT_CF_USE_RAW_OFFSETOF_IN_STATIC_ASSERT */
  154. ::EnableWindow(m_hwnd, m_bEnableOnDtor);
  155. }
  156. // Members
  157. private:
  158. HWND m_hwnd;
  159. ws_bool_t m_bEnableOnDtor;
  160. // Not to be implemented
  161. private:
  162. window_enable_scope(class_type const& rhs);
  163. class_type const& operator =(class_type const& rhs);
  164. };
  165. /* ////////////////////////////////////////////////////////////////////// */
  166. #ifndef _WINSTL_NO_NAMESPACE
  167. # if defined(_STLSOFT_NO_NAMESPACE) || \
  168. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  169. } // namespace winstl
  170. # else
  171. } // namespace winstl_project
  172. } // namespace stlsoft
  173. # endif /* _STLSOFT_NO_NAMESPACE */
  174. #endif /* !_WINSTL_NO_NAMESPACE */
  175. /* ////////////////////////////////////////////////////////////////////// */
  176. #endif /* WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_ENABLE_SCOPE */
  177. /* ///////////////////////////// end of file //////////////////////////// */