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.

252 lines
8.4 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/window/window_text_scope.hpp
  3. *
  4. * Purpose: Window text scoping class.
  5. *
  6. * Created: 21st August 2003
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-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_text_scope.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::window_text_scope class
  42. * (\ref group__library__windows_window "Windows Window" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_TEXT_SCOPE
  45. #define WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_TEXT_SCOPE
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_TEXT_SCOPE_MAJOR 4
  48. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_TEXT_SCOPE_MINOR 1
  49. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_TEXT_SCOPE_REVISION 1
  50. # define WINSTL_VER_WINSTL_WINDOW_HPP_WINDOW_TEXT_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. #ifndef WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_TRAITS
  62. # include <winstl/window/window_traits.hpp>
  63. #endif /* !WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_TRAITS */
  64. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
  65. # include <stlsoft/memory/auto_buffer.hpp>
  66. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
  67. #ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
  68. # include <winstl/memory/processheap_allocator.hpp>
  69. #endif /* !WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR */
  70. /* /////////////////////////////////////////////////////////////////////////
  71. * Namespace
  72. */
  73. #ifndef _WINSTL_NO_NAMESPACE
  74. # if defined(_STLSOFT_NO_NAMESPACE) || \
  75. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  76. /* There is no stlsoft namespace, so must define ::winstl */
  77. namespace winstl
  78. {
  79. # else
  80. /* Define stlsoft::winstl_project */
  81. namespace stlsoft
  82. {
  83. namespace winstl_project
  84. {
  85. # endif /* _STLSOFT_NO_NAMESPACE */
  86. #endif /* !_WINSTL_NO_NAMESPACE */
  87. /* /////////////////////////////////////////////////////////////////////////
  88. * Classes
  89. */
  90. // window_text_scope
  91. /** \brief Provides scoping of the text of a window.
  92. *
  93. * \ingroup group__library__windows_window
  94. *
  95. * This class provides scoping of the text of a window via the API
  96. * function SetWindowText().
  97. */
  98. template <ss_typename_param_k S>
  99. class window_text_scope
  100. {
  101. /// \name Member Types
  102. /// @{
  103. public:
  104. /// The string type
  105. typedef S string_type;
  106. /// The type of the current instantiation
  107. typedef window_text_scope<S> class_type;
  108. /// The character type
  109. typedef ss_typename_type_k string_type::value_type char_type;
  110. /// The traits type
  111. typedef window_traits<char_type> traits_type;
  112. private:
  113. typedef stlsoft_ns_qual(auto_buffer_old)< char_type
  114. , processheap_allocator<char_type>
  115. > buffer_type_;
  116. /// @}
  117. /// \name Construction
  118. /// @{
  119. public:
  120. /// \brief Toggles the window text
  121. ///
  122. /// Takes a HWND and changes sets its text, which is set back to
  123. /// the original in the destructor.
  124. ///
  125. /// \param wnd The window whose text is to be modified
  126. /// \param s The new, temporary, window text
  127. ss_explicit_k window_text_scope(HWND wnd, char_type const* s)
  128. : m_hwnd(wnd)
  129. {
  130. buffer_type_ buffer(1 + traits_type::get_window_text_length(m_hwnd));
  131. if(0 < buffer.size()) // Some allocators return NULL, rather than throw
  132. {
  133. buffer[buffer.size() - 1] = char_type(0);
  134. traits_type::get_window_text(m_hwnd, &buffer[0], buffer.size());
  135. m_oldText = buffer.data();
  136. traits_type::set_window_text(m_hwnd, s);
  137. }
  138. }
  139. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  140. /// \brief Toggles the window text
  141. ///
  142. /// Takes a HWND and changes sets its text, which is set back to
  143. /// the original in the destructor.
  144. ///
  145. /// \param wnd The window whose text is to be modified
  146. /// \param s The new, temporary, window text
  147. #if 0
  148. template <ss_typename_param_k S>
  149. ss_explicit_k window_text_scope(HWND wnd, S const& s)
  150. : m_hwnd(wnd)
  151. {
  152. buffer_type_ buffer(1 + traits_type::get_window_text_length(m_hwnd));
  153. if(0 < buffer.size()) // Some allocators return NULL, rather than throw
  154. {
  155. buffer[buffer.size() - 1] = char_type(0);
  156. traits_type::get_window_text(m_hwnd, &buffer[0], buffer.size());
  157. m_oldText = buffer.data();
  158. traits_type::set_window_text(m_hwnd, s);
  159. }
  160. }
  161. #endif /* 0 */
  162. /// \brief Toggles the window text
  163. ///
  164. /// Takes a HWND and changes sets its text, which is set back to
  165. /// the original in the destructor.
  166. ///
  167. /// \param wnd The window whose text is to be modified
  168. /// \param s The new, temporary, window text
  169. template< ss_typename_param_k W
  170. , ss_typename_param_k S1
  171. >
  172. ss_explicit_k window_text_scope(W &wnd, S1 const& s)
  173. : m_hwnd(get_HWND(wnd))
  174. {
  175. buffer_type_ buffer(1 + traits_type::get_window_text_length(m_hwnd));
  176. if(0 < buffer.size()) // Some allocators return NULL, rather than throw
  177. {
  178. buffer[buffer.size() - 1] = char_type(0);
  179. traits_type::get_window_text(m_hwnd, &buffer[0], buffer.size());
  180. m_oldText = buffer.data();
  181. traits_type::set_window_text(m_hwnd, s);
  182. }
  183. }
  184. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  185. /// Resets the window text
  186. ~window_text_scope() stlsoft_throw_0()
  187. {
  188. traits_type::set_window_text(m_hwnd, m_oldText);
  189. }
  190. /// @}
  191. /// \name Members
  192. /// @{
  193. private:
  194. HWND m_hwnd;
  195. string_type m_oldText;
  196. /// @}
  197. /// \name Not to be implemented
  198. /// @{
  199. private:
  200. window_text_scope(class_type const& rhs);
  201. class_type const& operator =(class_type const& rhs);
  202. /// @}
  203. };
  204. /* ////////////////////////////////////////////////////////////////////// */
  205. #ifndef _WINSTL_NO_NAMESPACE
  206. # if defined(_STLSOFT_NO_NAMESPACE) || \
  207. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  208. } // namespace winstl
  209. # else
  210. } // namespace winstl_project
  211. } // namespace stlsoft
  212. # endif /* _STLSOFT_NO_NAMESPACE */
  213. #endif /* !_WINSTL_NO_NAMESPACE */
  214. /* ////////////////////////////////////////////////////////////////////// */
  215. #endif /* WINSTL_INCL_WINSTL_WINDOW_HPP_WINDOW_TEXT_SCOPE */
  216. /* ///////////////////////////// end of file //////////////////////////// */