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.

207 lines
7.2 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/system/console_colour_scope.hpp
  3. *
  4. * Purpose: Scopes the console colour (and intensity).
  5. *
  6. * Created: 20th July 2006
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2006-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/system/console_colour_scope.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::console_colour_scope class
  42. * (\ref group__library__windows_window "Windows Window" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_SYSTEM_HPP_CONSOLE_COLOUR_SCOPE
  45. #define WINSTL_INCL_WINSTL_SYSTEM_HPP_CONSOLE_COLOUR_SCOPE
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_SYSTEM_HPP_CONSOLE_COLOUR_SCOPE_MAJOR 1
  48. # define WINSTL_VER_WINSTL_SYSTEM_HPP_CONSOLE_COLOUR_SCOPE_MINOR 0
  49. # define WINSTL_VER_WINSTL_SYSTEM_HPP_CONSOLE_COLOUR_SCOPE_REVISION 5
  50. # define WINSTL_VER_WINSTL_SYSTEM_HPP_CONSOLE_COLOUR_SCOPE_EDIT 9
  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. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  59. # ifndef WINSTL_INCL_WINSTL_ERROR_HPP_EXCEPTIONS
  60. # include <winstl/error/exceptions.hpp>
  61. # endif /* !WINSTL_INCL_WINSTL_ERROR_HPP_EXCEPTIONS */
  62. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  63. /* /////////////////////////////////////////////////////////////////////////
  64. * Namespace
  65. */
  66. #ifndef _WINSTL_NO_NAMESPACE
  67. # if defined(_STLSOFT_NO_NAMESPACE) || \
  68. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  69. /* There is no stlsoft namespace, so must define ::winstl */
  70. namespace winstl
  71. {
  72. # else
  73. /* Define stlsoft::winstl_project */
  74. namespace stlsoft
  75. {
  76. namespace winstl_project
  77. {
  78. # endif /* _STLSOFT_NO_NAMESPACE */
  79. #endif /* !_WINSTL_NO_NAMESPACE */
  80. /* /////////////////////////////////////////////////////////////////////////
  81. * Classes
  82. */
  83. /** \brief Provides scoping of the colour (and intensity) of the console
  84. *
  85. * \ingroup group__library__windows_window
  86. *
  87. * This class provides scoping of the colour (and intensity) of the console
  88. * via the API functions <code>GetConsoleScreenBufferInfo()</code> and
  89. * <code>SetConsoleTextAttribute()</code>.
  90. */
  91. class console_colour_scope
  92. {
  93. public:
  94. /// This type
  95. typedef console_colour_scope class_type;
  96. // Construction
  97. public:
  98. /// \brief Sets the console text attribute(s), remembering the current
  99. /// state so it can be reset in the destructor.
  100. ///
  101. /// The constructor applies the given text attributes to the given
  102. /// console screen buffer, after first recording the current state so
  103. /// that they can be reset when the instance is destroyed.
  104. ///
  105. /// \exception winstl::windows_exception If \ref page__exception_agnostic "exception handling is enabled",
  106. /// an instance of \link winstl::windows_exception windows_exception\endlink
  107. /// will be thrown if the console text attributes cannot be elicited
  108. /// or changed. If \ref page__exception_agnostic "exception handling is not enabled",
  109. /// the console attributes are left as they are, and the destructor
  110. /// makes no attempt at modification.
  111. ///
  112. /// \param hBuffer Handle the console screen buffer.
  113. /// \param textAttributes The text attributes to be applied to the console
  114. ss_explicit_k console_colour_scope(HANDLE hBuffer, WORD textAttributes)
  115. : m_hBuffer(hBuffer)
  116. , m_attributes(init_(hBuffer, textAttributes))
  117. {}
  118. /// Resets the console text attribute(s) to the original form.
  119. ~console_colour_scope() stlsoft_throw_0()
  120. {
  121. #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
  122. if(0xffffffff != m_attributes)
  123. #endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
  124. {
  125. ::SetConsoleTextAttribute(m_hBuffer, static_cast<WORD>(m_attributes));
  126. }
  127. }
  128. /// \name Implementation
  129. /// @{
  130. private:
  131. static ws_uint32_t init_(HANDLE hBuffer, WORD textAttributes)
  132. {
  133. ws_uint32_t attr = 0xffffffff;
  134. CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
  135. if(!::GetConsoleScreenBufferInfo(hBuffer, &bufferInfo))
  136. {
  137. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  138. STLSOFT_THROW_X(windows_exception("Could not retrieve console buffer information", ::GetLastError()));
  139. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  140. }
  141. else
  142. {
  143. if(!::SetConsoleTextAttribute(hBuffer, textAttributes))
  144. {
  145. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  146. STLSOFT_THROW_X(windows_exception("Could not set console text attributes", ::GetLastError()));
  147. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  148. }
  149. else
  150. {
  151. attr = bufferInfo.wAttributes;
  152. }
  153. }
  154. return attr;
  155. }
  156. /// @}
  157. /// \name Members
  158. /// @{
  159. private:
  160. HANDLE m_hBuffer;
  161. ws_uint32_t m_attributes;
  162. /// @}
  163. /// \name Not to be implemented
  164. /// @{
  165. private:
  166. console_colour_scope(class_type const& rhs);
  167. class_type const& operator =(class_type const& rhs);
  168. /// @}
  169. };
  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_SYSTEM_HPP_CONSOLE_COLOUR_SCOPE */
  182. /* ///////////////////////////// end of file //////////////////////////// */