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.

266 lines
7.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/system/console_functions.h
  3. *
  4. * Purpose: Console functions.
  5. *
  6. * Created: 3rd December 2005
  7. * Updated: 9th September 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2010, 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_functions.h
  40. *
  41. * \brief [C++ only] Definition of the winstl::basic_environment_block class
  42. * template
  43. * (\ref group__library__system "System" Library).
  44. */
  45. #ifndef WINSTL_INCL_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS
  46. #define WINSTL_INCL_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_MAJOR 2
  49. # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_MINOR 2
  50. # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_REVISION 2
  51. # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_EDIT 19
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. [Incompatibilies-end]
  59. */
  60. /* /////////////////////////////////////////////////////////////////////////
  61. * Includes
  62. */
  63. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  64. # include <winstl/winstl.h>
  65. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  66. #if defined(STLSOFT_UNITTEST) && \
  67. ( !defined(STLSOFT_COMPILER_IS_COMO) && \
  68. !defined(STLSOFT_COMPILER_IS_WATCOM))
  69. # include <winstl/dl/dl_call.hpp>
  70. #endif /* STLSOFT_UNITTEST */
  71. /* /////////////////////////////////////////////////////////////////////////
  72. * Namespace
  73. */
  74. #if !defined(_WINSTL_NO_NAMESPACE) && \
  75. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  76. # if defined(_STLSOFT_NO_NAMESPACE)
  77. /* There is no stlsoft namespace, so must define ::winstl */
  78. namespace winstl
  79. {
  80. # else
  81. /* Define stlsoft::winstl_project */
  82. namespace stlsoft
  83. {
  84. namespace winstl_project
  85. {
  86. # endif /* _STLSOFT_NO_NAMESPACE */
  87. #endif /* !_WINSTL_NO_NAMESPACE */
  88. /* /////////////////////////////////////////////////////////////////////////
  89. * Functions
  90. */
  91. /** \brief Evalutes the current width of the console.
  92. *
  93. * \ingroup group__library__system
  94. */
  95. STLSOFT_INLINE ws_size_t winstl_C_get_console_width(void)
  96. {
  97. HANDLE hStdOut = STLSOFT_NS_GLOBAL(GetStdHandle)(STD_OUTPUT_HANDLE);
  98. if(INVALID_HANDLE_VALUE != hStdOut)
  99. {
  100. CONSOLE_SCREEN_BUFFER_INFO csbi;
  101. if(STLSOFT_NS_GLOBAL(GetConsoleScreenBufferInfo)(hStdOut, &csbi))
  102. {
  103. return csbi.dwMaximumWindowSize.X;
  104. }
  105. }
  106. #ifdef _DEBUG
  107. STLSOFT_NS_GLOBAL(GetLastError)();
  108. #endif /* _DEBUG */
  109. return ~stlsoft_static_cast(ws_size_t, 0);
  110. }
  111. #if !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) && \
  112. ( !defined(_WIN32_WINNT) || \
  113. _WIN32_WINNT < 0x0500 || \
  114. ( defined(STLSOFT_COMPILER_IS_BORLAND) && \
  115. !defined(CONSOLE_NO_SELECTION)))
  116. STLSOFT_INLINE HWND GetConsoleWindow()
  117. {
  118. typedef HWND (WINAPI *GCW_t)();
  119. HMODULE Kernel32 = STLSOFT_NS_GLOBAL(LoadLibraryA)("KERNEL32");
  120. GCW_t pfn = stlsoft_reinterpret_cast(GCW_t, STLSOFT_NS_GLOBAL(GetProcAddress)(Kernel32, "GetConsoleWindow"));
  121. if(NULL == pfn)
  122. {
  123. return NULL;
  124. }
  125. else
  126. {
  127. HWND hwnd = (*pfn)();
  128. STLSOFT_NS_GLOBAL(FreeLibrary)(Kernel32);
  129. return hwnd;
  130. }
  131. }
  132. #else /* ? _WIN32_WINNT */
  133. #endif /* _WIN32_WINNT */
  134. /** Returns the window handle of the current console, or NULL if it cannot
  135. * be found
  136. *
  137. * \ingroup group__library__system
  138. *
  139. * \warning This only works on Windows 2000, or later, operating systems. It
  140. * will return NULL on other operating systems.
  141. */
  142. STLSOFT_INLINE HWND winstl_C_get_console_window(void)
  143. {
  144. return GetConsoleWindow();
  145. }
  146. /* /////////////////////////////////////////////////////////////////////////
  147. * Obsolete symbols
  148. *
  149. * NOTE: these are only defined if:
  150. *
  151. * - we're generating documentation, or
  152. * - STLSOFT_OBSOLETE is specified, or
  153. * - it's STLSoft 1.9 (or earlier)
  154. */
  155. #if defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) || \
  156. defined(STLSOFT_OBSOLETE) || \
  157. _STLSOFT_VER < 0x010a0000
  158. /** \def winstl__get_console_window
  159. *
  160. * \deprecated Use winstl_C_get_console_window
  161. */
  162. # define winstl__get_console_window winstl_C_get_console_window
  163. /** \def winstl__get_console_width
  164. *
  165. * \deprecated Use winstl_C_get_console_width
  166. */
  167. # define winstl__get_console_width winstl_C_get_console_width
  168. #endif /* obsolete || 1.9 */
  169. /* /////////////////////////////////////////////////////////////////////////
  170. * Namespace
  171. */
  172. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  173. namespace winstl
  174. {
  175. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  176. /* /////////////////////////////////////////////////////////////////////////
  177. * C++ functions
  178. */
  179. #if defined(__cplusplus)
  180. /** \brief Evalutes the current width of the console.
  181. *
  182. * \ingroup group__library__system
  183. */
  184. inline ws_size_t get_console_width()
  185. {
  186. return winstl_C_get_console_width();
  187. }
  188. /** Returns the window handle of the current console, or NULL if it cannot
  189. * be found
  190. *
  191. * \ingroup group__library__system
  192. *
  193. * \warning This only works on Windows 2000, or later, operating systems. It
  194. * will return NULL on other operating systems.
  195. */
  196. inline HWND get_console_window(void)
  197. {
  198. return winstl_C_get_console_window();
  199. }
  200. #endif /* __cplusplus */
  201. /* /////////////////////////////////////////////////////////////////////////
  202. * Unit-testing
  203. */
  204. #ifdef STLSOFT_UNITTEST
  205. # include "./unittest/console_functions_unittest_.h"
  206. #endif /* STLSOFT_UNITTEST */
  207. /* ////////////////////////////////////////////////////////////////////// */
  208. #ifndef _WINSTL_NO_NAMESPACE
  209. # if defined(_STLSOFT_NO_NAMESPACE) || \
  210. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  211. } /* namespace winstl */
  212. # else
  213. } /* namespace winstl_project */
  214. } /* namespace stlsoft */
  215. # endif /* _STLSOFT_NO_NAMESPACE */
  216. #endif /* !_WINSTL_NO_NAMESPACE */
  217. /* ////////////////////////////////////////////////////////////////////// */
  218. #endif /* WINSTL_INCL_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS */
  219. /* ///////////////////////////// end of file //////////////////////////// */