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.

209 lines
7.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/window/window_visible_scope.hpp
  3. *
  4. * Purpose: Window visible-state scoping class.
  5. *
  6. * Created: 26th May 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-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_visible_scope.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::window_visible_scope class
  42. * (\ref group__library__windows_window "Windows Window" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_VISIBLE_SCOPE
  45. #define WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_VISIBLE_SCOPE
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_VISIBLE_SCOPE_MAJOR 4
  48. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_VISIBLE_SCOPE_MINOR 1
  49. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_VISIBLE_SCOPE_REVISION 1
  50. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_VISIBLE_SCOPE_EDIT 39
  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_visible_scope
  82. /** \brief Provides scoping of the visible status of a window.
  83. *
  84. * \ingroup group__library__windows_window
  85. *
  86. * This class provides scoping of the visible status of a window via the API
  87. * function ShowWindow().
  88. */
  89. class window_visible_scope
  90. {
  91. public:
  92. /// This type
  93. typedef window_visible_scope class_type;
  94. // Construction
  95. public:
  96. /// \brief Toggles the window visible state
  97. ///
  98. /// Takes a HWND and changes it's current visible-status, which is set back to
  99. /// the original state in the destructor.
  100. ///
  101. /// \param wnd The window whose visible state is to be controlled
  102. ss_explicit_k window_visible_scope(HWND wnd)
  103. : m_hwnd(wnd)
  104. , m_stateOnDtor(::IsWindowVisible(m_hwnd) ? SW_SHOW : SW_HIDE)
  105. {
  106. ::ShowWindow(m_hwnd, (SW_SHOW == m_stateOnDtor) ? SW_HIDE : SW_SHOW);
  107. }
  108. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  109. /// \brief Toggles the window visible state
  110. ///
  111. /// Takes a HWND and changes it's current visible-status, which is set back to
  112. /// the original state in the destructor.
  113. ///
  114. /// \param wnd The window whose visible state is to be controlled
  115. template <ss_typename_param_k W>
  116. ss_explicit_k window_visible_scope(W &wnd)
  117. : m_hwnd(get_HWND(wnd))
  118. , m_stateOnDtor(::IsWindowVisible(m_hwnd) ? SW_SHOW : SW_HIDE)
  119. {
  120. ::ShowWindow(m_hwnd, (SW_SHOW == m_stateOnDtor) ? SW_HIDE : SW_SHOW);
  121. }
  122. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  123. /// Modifies the window visible state
  124. ///
  125. /// \param wnd The window whose visible state is to be controlled
  126. /// \param stateOnCtor The state to set in the constructor
  127. /// \param stateOnDtor The state it is reset to in the destructor
  128. window_visible_scope(HWND wnd, ws_int_t stateOnCtor, ws_int_t stateOnDtor)
  129. : m_hwnd(wnd)
  130. , m_stateOnDtor(stateOnDtor)
  131. {
  132. ::ShowWindow(m_hwnd, stateOnCtor);
  133. }
  134. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  135. /// Modifies the window visible state
  136. ///
  137. /// \param wnd The window whose visible state is to be controlled
  138. /// \param stateOnCtor The state to set in the constructor
  139. /// \param stateOnDtor The state it is reset to in the destructor
  140. template <ss_typename_param_k W>
  141. window_visible_scope(W &wnd, ws_int_t stateOnCtor, ws_int_t stateOnDtor)
  142. : m_hwnd(get_HWND(wnd))
  143. , m_stateOnDtor(stateOnDtor)
  144. {
  145. ::ShowWindow(m_hwnd, stateOnCtor);
  146. }
  147. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  148. /// Resets the visible status
  149. ~window_visible_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_stateOnDtor));
  153. #endif /* STLSOFT_CF_USE_RAW_OFFSETOF_IN_STATIC_ASSERT */
  154. ::ShowWindow(m_hwnd, m_stateOnDtor);
  155. }
  156. // Members
  157. private:
  158. HWND const m_hwnd;
  159. ws_int_t const m_stateOnDtor;
  160. // Not to be implemented
  161. private:
  162. window_visible_scope(class_type const& rhs);
  163. class_type const& operator =(class_type const& rhs);
  164. };
  165. ////////////////////////////////////////////////////////////////////////////
  166. // Unit-testing
  167. #ifdef STLSOFT_UNITTEST
  168. # include "./unittest/window_visible_scope_unittest_.h"
  169. #endif /* STLSOFT_UNITTEST */
  170. /* ////////////////////////////////////////////////////////////////////// */
  171. #ifndef _WINSTL_NO_NAMESPACE
  172. # if defined(_STLSOFT_NO_NAMESPACE) || \
  173. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  174. } // namespace winstl
  175. # else
  176. } // namespace winstl_project
  177. } // namespace stlsoft
  178. # endif /* _STLSOFT_NO_NAMESPACE */
  179. #endif /* !_WINSTL_NO_NAMESPACE */
  180. /* ////////////////////////////////////////////////////////////////////// */
  181. #endif /* WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_VISIBLE_SCOPE */
  182. /* ///////////////////////////// end of file //////////////////////////// */