00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: winstl/system/console_functions.h 00003 * 00004 * Purpose: Console functions. 00005 * 00006 * Created: 3rd December 2005 00007 * Updated: 10th August 2009 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2005-2009, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * ////////////////////////////////////////////////////////////////////// */ 00039 00040 00048 #ifndef WINSTL_INCL_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS 00049 #define WINSTL_INCL_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS 00050 00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00052 # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_MAJOR 2 00053 # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_MINOR 1 00054 # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_REVISION 2 00055 # define WINSTL_VER_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS_EDIT 17 00056 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00057 00058 /* ///////////////////////////////////////////////////////////////////////// 00059 * Compatibility 00060 */ 00061 00062 /* 00063 [Incompatibilies-start] 00064 [Incompatibilies-end] 00065 */ 00066 00067 /* ///////////////////////////////////////////////////////////////////////// 00068 * Includes 00069 */ 00070 00071 #ifndef WINSTL_INCL_WINSTL_H_WINSTL 00072 # include <winstl/winstl.h> 00073 #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */ 00074 00075 #if defined(STLSOFT_UNITTEST) && \ 00076 ( !defined(STLSOFT_COMPILER_IS_COMO) && \ 00077 !defined(STLSOFT_COMPILER_IS_WATCOM)) 00078 # include <winstl/dl/dl_call.hpp> 00079 #endif /* STLSOFT_UNITTEST */ 00080 00081 /* ///////////////////////////////////////////////////////////////////////// 00082 * Namespace 00083 */ 00084 00085 #if !defined(_WINSTL_NO_NAMESPACE) && \ 00086 !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00087 # if defined(_STLSOFT_NO_NAMESPACE) 00088 /* There is no stlsoft namespace, so must define ::winstl */ 00089 namespace winstl 00090 { 00091 # else 00092 /* Define stlsoft::winstl_project */ 00093 00094 namespace stlsoft 00095 { 00096 00097 namespace winstl_project 00098 { 00099 00100 # endif /* _STLSOFT_NO_NAMESPACE */ 00101 #endif /* !_WINSTL_NO_NAMESPACE */ 00102 00103 /* ///////////////////////////////////////////////////////////////////////// 00104 * Functions 00105 */ 00106 00111 STLSOFT_INLINE ws_size_t winstl__get_console_width(void) 00112 { 00113 CONSOLE_SCREEN_BUFFER_INFO csbi; 00114 00115 /* csbi.dwSize = sizeof(csbi); */ 00116 00117 if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) 00118 { 00119 return csbi.dwMaximumWindowSize.X; 00120 } 00121 00122 return ~(ws_size_t)(0); 00123 } 00124 00125 #if !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) && \ 00126 ( !defined(_WIN32_WINNT) || \ 00127 _WIN32_WINNT < 0x0500 || \ 00128 ( defined(STLSOFT_COMPILER_IS_BORLAND) && \ 00129 !defined(CONSOLE_NO_SELECTION))) 00130 00131 STLSOFT_INLINE HWND GetConsoleWindow() 00132 { 00133 typedef HWND (WINAPI *GCW_t)(); 00134 00135 HMODULE Kernel32 = STLSOFT_NS_GLOBAL(LoadLibraryA)("KERNEL32"); 00136 GCW_t pfn = stlsoft_reinterpret_cast(GCW_t, STLSOFT_NS_GLOBAL(GetProcAddress)(Kernel32, "GetConsoleWindow")); 00137 00138 if(NULL == pfn) 00139 { 00140 return NULL; 00141 } 00142 else 00143 { 00144 HWND hwnd = (*pfn)(); 00145 00146 STLSOFT_NS_GLOBAL(FreeLibrary)(Kernel32); 00147 00148 return hwnd; 00149 } 00150 } 00151 00152 #else /* ? _WIN32_WINNT */ 00153 00154 #endif /* _WIN32_WINNT */ 00155 00164 STLSOFT_INLINE HWND winstl__get_console_window(void) 00165 { 00166 return GetConsoleWindow(); 00167 } 00168 00169 /* ///////////////////////////////////////////////////////////////////////// 00170 * Namespace 00171 */ 00172 00173 #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION 00174 namespace winstl 00175 { 00176 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00177 00178 /* ///////////////////////////////////////////////////////////////////////// 00179 * C++ functions 00180 */ 00181 00182 #if defined(__cplusplus) 00183 00188 inline ws_size_t get_console_width() 00189 { 00190 return winstl__get_console_width(); 00191 } 00192 00201 inline HWND get_console_window(void) 00202 { 00203 return winstl__get_console_window(); 00204 } 00205 00206 #endif /* __cplusplus */ 00207 00208 /* ///////////////////////////////////////////////////////////////////////// 00209 * Unit-testing 00210 */ 00211 00212 #ifdef STLSOFT_UNITTEST 00213 # include "./unittest/console_functions_unittest_.h" 00214 #endif /* STLSOFT_UNITTEST */ 00215 00216 /* ////////////////////////////////////////////////////////////////////// */ 00217 00218 #ifndef _WINSTL_NO_NAMESPACE 00219 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00220 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00221 } /* namespace winstl */ 00222 # else 00223 } /* namespace winstl_project */ 00224 } /* namespace stlsoft */ 00225 # endif /* _STLSOFT_NO_NAMESPACE */ 00226 #endif /* !_WINSTL_NO_NAMESPACE */ 00227 00228 /* ////////////////////////////////////////////////////////////////////// */ 00229 00230 #endif /* WINSTL_INCL_WINSTL_SYSTEM_H_CONSOLE_FUNCTIONS */ 00231 00232 /* ///////////////////////////// end of file //////////////////////////// */