[Ttssh2-commit] [8532] layer_for_unicode に API 追加

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2020年 2月 9日 (日) 14:55:07 JST


Revision: 8532
          https://osdn.net/projects/ttssh2/scm/svn/commits/8532
Author:   zmatsuo
Date:     2020-02-09 14:55:07 +0900 (Sun, 09 Feb 2020)
Log Message:
-----------
layer_for_unicode に API 追加

- _SendMessageW() 追加
  - _SendDlgItemMessageW() と処理を共通化
- 処理できるメッセージを追加
  - WM_GETTEXT, LB_GETTEXT, LB_GETTEXTLEN, LB_INSERTSTRING
- _GetWindowTextW(), _GetWindowTextLengthW() 追加

Modified Paths:
--------------
    trunk/teraterm/common/compat_win.cpp
    trunk/teraterm/common/compat_win.h
    trunk/teraterm/common/layer_for_unicode.cpp
    trunk/teraterm/common/layer_for_unicode.h

-------------- next part --------------
Modified: trunk/teraterm/common/compat_win.cpp
===================================================================
--- trunk/teraterm/common/compat_win.cpp	2020-02-06 16:08:10 UTC (rev 8531)
+++ trunk/teraterm/common/compat_win.cpp	2020-02-09 05:55:07 UTC (rev 8532)
@@ -35,6 +35,9 @@
 #include "dllutil.h"
 #include "ttlib.h"
 
+// for debug
+//#define UNICODE_API_DISABLE	1
+
 ATOM (WINAPI *pRegisterClassW)(const WNDCLASSW *lpWndClass);
 HWND (WINAPI *pCreateWindowExW)(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight,
  HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
@@ -41,6 +44,7 @@
 LRESULT (WINAPI *pDefWindowProcW)(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
 HPROPSHEETPAGE (WINAPI *pCreatePropertySheetPageW)(LPCPROPSHEETPAGEW constPropSheetPagePointer);
 INT_PTR (WINAPI *pPropertySheetW)(LPCPROPSHEETHEADERW constPropSheetHeaderPointer);
+LRESULT (WINAPI *pSendMessageW)(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
 LRESULT (WINAPI *pSendDlgItemMessageW)(HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam);
 BOOL (WINAPI *pModifyMenuW)(HMENU hMnu, UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem);
 int(WINAPI *pGetMenuStringW)(HMENU hMenu, UINT uIDItem, LPWSTR lpString, int cchMax, UINT flags);
@@ -51,7 +55,6 @@
 BOOL (WINAPI *pSetDlgItemTextW)(HWND hDlg, int nIDDlgItem, LPCWSTR lpString);
 BOOL (WINAPI *pGetDlgItemTextW)(HWND hDlg, int nIDDlgItem, LPWSTR lpString, int cchMax);
 BOOL (WINAPI *pAlphaBlend)(HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);
-BOOL (WINAPI *pEnumDisplayMonitors)(HDC,LPCRECT,MONITORENUMPROC,LPARAM);
 DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext);
 BOOL (WINAPI *pIsValidDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext);
 UINT (WINAPI *pGetDpiForWindow)(HWND hwnd);
@@ -74,11 +77,16 @@
 BOOL (WINAPI *pInsertMenuW)(HMENU hMenu, UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem);
 BOOL (WINAPI *pAppendMenuW)(HMENU hMenu, UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem);
 
+// multi monitor Windows98+/Windows2000+
+BOOL (WINAPI *pEnumDisplayMonitors)(HDC,LPCRECT,MONITORENUMPROC,LPARAM);
 HMONITOR (WINAPI *pMonitorFromWindow)(HWND hwnd, DWORD dwFlags);
 HMONITOR (WINAPI *pMonitorFromPoint)(POINT pt, DWORD dwFlags);
 HMONITOR (WINAPI *pMonitorFromRect)(LPCRECT lprc, DWORD dwFlags);
 BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR hMonitor, LPMONITORINFO lpmi);
 
+int (WINAPI *pGetWindowTextW)(HWND hWnd, LPWSTR lpString, int nMaxCount);
+int (WINAPI *pGetWindowTextLengthW)(HWND hWnd);
+
 /**
  *	GetConsoleWindow() \x82Ɠ\xAF\x82\xB6\x93\xAE\x8D\xEC\x82\xF0\x82\xB7\x82\xE9
  *	 https://support.microsoft.com/ja-jp/help/124103/how-to-obtain-a-console-window-handle-hwnd
@@ -119,9 +127,11 @@
 }
 
 static const APIInfo Lists_user32[] = {
+#ifndef UNICODE_API_DISABLE
 	{ "RegisterClassW", (void **)&pRegisterClassW },
 	{ "CreateWindowExW", (void **)&pCreateWindowExW },
 	{ "DefWindowProcW", (void **)&pDefWindowProcW },
+#endif
 	{ "SetLayeredWindowAttributes", (void **)&pSetLayeredWindowAttributes },
 	{ "SetThreadDpiAwarenessContext", (void **)&pSetThreadDpiAwarenessContext },
 	{ "IsValidDpiAwarenessContext", (void **)&pIsValidDpiAwarenessContext },
@@ -129,6 +139,7 @@
 	{ "MonitorFromRect", (void **)&pMonitorFromRect },
 	{ "AdjustWindowRectEx", (void **)&pAdjustWindowRectEx },
 	{ "AdjustWindowRectExForDpi", (void **)&pAdjustWindowRectExForDpi },
+#ifndef UNICODE_API_DISABLE
 	{ "SetDlgItemTextW", (void **)&pSetDlgItemTextW },
 	{ "GetDlgItemTextW", (void **)&pGetDlgItemTextW },
 	{ "SetWindowTextW", (void **)&pSetWindowTextW },
@@ -139,6 +150,11 @@
 	{ "DialogBoxIndirectParamW", (void **)&pDialogBoxIndirectParamW },
 	{ "InsertMenuW", (void **)&pInsertMenuW },
 	{ "AppendMenuW", (void **)&pAppendMenuW },
+	{ "SendMessageW", (void **)&pSendMessageW },
+	{ "GetWindowTextW", (void **)&pGetWindowTextW },
+	{ "GetWindowTextLengthW", (void **)&pGetWindowTextLengthW },
+#endif
+
 	{ "MonitorFromWindow", (void **)&pMonitorFromWindow },
 	{ "MonitorFromPoint", (void **)&pMonitorFromPoint },
 	{ "GetMonitorInfoA", (void **)&pGetMonitorInfoA },
@@ -151,8 +167,10 @@
 };
 
 static const APIInfo Lists_gdi32[] = {
+#ifndef UNICODE_API_DISABLE
 	{ "AddFontResourceExW", (void **)&pAddFontResourceExW },
 	{ "RemoveFontResourceExW", (void **)&pRemoveFontResourceExW },
+#endif
 	{},
 };
 
@@ -162,25 +180,33 @@
 };
 
 static const APIInfo Lists_kernel32[] = {
+#ifndef UNICODE_API_DISABLE
 	{ "GetFileAttributesW", (void **)&pGetFileAttributesW },
 	{ "GetPrivateProfileStringW", (void **)&pGetPrivateProfileStringW },
+#endif
 	{ "GetConsoleWindow", (void **)&pGetConsoleWindow },
 	{},
 };
 
 static const APIInfo Lists_shell32[] = {
+#ifndef UNICODE_API_DISABLE
 	{ "DragQueryFileW", (void **)&pDragQueryFileW },
+#endif
 	{},
 };
 
 static const APIInfo Lists_comctl32[] = {
+#ifndef UNICODE_API_DISABLE
 	{ "CreatePropertySheetPageW", (void **)&pCreatePropertySheetPageW },
 	{ "PropertySheetW", (void **)&pPropertySheetW },
+#endif
 	{},
 };
 
 static const APIInfo Lists_hhctrl[] = {
+#ifndef UNICODE_API_DISABLE
 	{ "HtmlHelpW", (void **)&pHtmlHelpW },
+#endif
 	{ "HtmlHelpA", (void **)&pHtmlHelpA },
 	{},
 };

Modified: trunk/teraterm/common/compat_win.h
===================================================================
--- trunk/teraterm/common/compat_win.h	2020-02-06 16:08:10 UTC (rev 8531)
+++ trunk/teraterm/common/compat_win.h	2020-02-09 05:55:07 UTC (rev 8532)
@@ -107,6 +107,9 @@
 extern HMONITOR (WINAPI *pMonitorFromPoint)(POINT pt, DWORD dwFlags);
 extern HMONITOR (WINAPI *pMonitorFromRect)(LPCRECT lprc, DWORD dwFlags);
 extern BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR hMonitor, LPMONITORINFO lpmi);
+extern LRESULT (WINAPI *pSendMessageW)(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
+extern int (WINAPI *pGetWindowTextW)(HWND hWnd, LPWSTR lpString, int nMaxCount);
+extern int (WINAPI *pGetWindowTextLengthW)(HWND hWnd);
 
 
 void WinCompatInit();

Modified: trunk/teraterm/common/layer_for_unicode.cpp
===================================================================
--- trunk/teraterm/common/layer_for_unicode.cpp	2020-02-06 16:08:10 UTC (rev 8531)
+++ trunk/teraterm/common/layer_for_unicode.cpp	2020-02-09 05:55:07 UTC (rev 8532)
@@ -117,45 +117,157 @@
 	return attr;
 }
 
-LRESULT _SendDlgItemMessageW(HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam)
+/**
+ * hWnd \x82ɐݒ肳\x82\xEA\x82Ă\xA2\x82镶\x8E\x9A\x97\xF1\x82\xF0\x8E擾
+ *
+ * @param[in]		hWnd
+ * @param[in,out]	lenW	\x95\xB6\x8E\x9A\x90\x94(L'\0'\x82\xF0\x8A܂܂Ȃ\xA2)
+ * @return			\x95\xB6\x8E\x9A\x97\xF1
+ */
+static wchar_t *SendMessageAFromW_WM_GETTEXT(HWND hWnd, size_t *lenW)
 {
-	if (pSendDlgItemMessageW != NULL) {
-		return pSendDlgItemMessageW(hDlg, nIDDlgItem, Msg, wParam, lParam);
+	// lenA = excluding the terminating null character.
+	size_t lenA = SendMessageA(hWnd, WM_GETTEXTLENGTH, 0, 0);
+	char *strA = (char *)malloc(lenA + 1);
+	if (strA == NULL) {
+		*lenW = 0;
+		return NULL;
 	}
+	lenA = GetWindowTextA(hWnd, strA, (int)(lenA + 1));
+	strA[lenA] = '\0';
+	wchar_t *strW = ToWcharA(strA);
+	free(strA);
+	if (strW == NULL) {
+		*lenW = 0;
+		return NULL;
+	}
+	*lenW = wcslen(strW);
+	return strW;
+}
 
+/**
+ * hWnd(ListBox) \x82ɐݒ肳\x82\xEA\x82Ă\xA2\x82镶\x8E\x9A\x97\xF1\x82\xF0\x8E擾
+ *
+ * @param[in]		hWnd
+ * @param[in]		wParam	\x8D\x80\x96ڔԍ\x86(0\x81`)
+ * @param[in,out]	lenW	\x95\xB6\x8E\x9A\x90\x94(L'\0'\x82\xF0\x8A܂܂Ȃ\xA2)
+ * @return			\x95\xB6\x8E\x9A\x97\xF1
+ */
+static wchar_t *SendMessageAFromW_LB_GETTEXT(HWND hWnd, WPARAM wParam, size_t *lenW)
+{
+	// lenA = excluding the terminating null character.
+	size_t lenA = SendMessageA(hWnd, LB_GETTEXTLEN, wParam, 0);
+	char *strA = (char *)malloc(lenA + 1);
+	if (strA == NULL) {
+		*lenW = 0;
+		return NULL;
+	}
+	lenA = SendMessageA(hWnd, LB_GETTEXT, wParam, (LPARAM)strA);
+	wchar_t *strW = ToWcharA(strA);
+	free(strA);
+	if (strW == NULL) {
+		*lenW = 0;
+		return NULL;
+	}
+	*lenW = wcslen(strW);
+	return strW;
+}
+
+int _GetWindowTextW(HWND hWnd, LPWSTR lpString, int nMaxCount)
+{
+	if (pGetWindowTextW != NULL) {
+		GetWindowTextW(hWnd, lpString, nMaxCount);
+	}
+
+	size_t lenW;
+	wchar_t *strW = SendMessageAFromW_WM_GETTEXT(hWnd, &lenW);
+	wchar_t *dest_ptr = (wchar_t *)lpString;
+	size_t dest_len = (size_t)nMaxCount;
+	wcsncpy_s(dest_ptr, dest_len, strW, _TRUNCATE);
+	free(strW);
+	return (int)(dest_len - 1);
+}
+
+int _GetWindowTextLengthW(HWND hWnd)
+{
+	if (pGetWindowTextLengthW != NULL) {
+		return pGetWindowTextLengthW(hWnd);
+	}
+
+	size_t lenW;
+	wchar_t *strW = SendMessageAFromW_WM_GETTEXT(hWnd, &lenW);
+	free(strW);
+	return (int)(lenW - 1);
+}
+
+static LRESULT SendMessageAFromW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
+{
 	LRESULT retval;
 	switch(Msg) {
 	case CB_ADDSTRING:
-	case LB_ADDSTRING: {
+	case LB_ADDSTRING:
+	case LB_INSERTSTRING: {
 		char *strA = ToCharW((wchar_t *)lParam);
-		retval = SendDlgItemMessageA(hDlg, nIDDlgItem, Msg, wParam, (LPARAM)strA);
+		retval = SendMessageA(hWnd, Msg, wParam, (LPARAM)strA);
 		free(strA);
-		break;
+		return retval;
 	}
-	case WM_GETTEXTLENGTH: {
-		retval = 0;
-		LRESULT len = SendDlgItemMessageA(hDlg, nIDDlgItem, WM_GETTEXTLENGTH, 0, 0);
-		len++;  // for '\0'
-		char *strA = (char *)malloc(sizeof(char) * len);
-		if (strA != NULL) {
-			GetDlgItemTextA(hDlg, nIDDlgItem, strA, (int)len);
-			strA[len-1] = '\0';
-			wchar_t *strW = ToWcharA(strA);
-			if (strW != NULL) {
-				retval = (LRESULT)wcslen(strW);// '\0'\x82\xF0\x8A܂܂Ȃ\xA2\x92\xB7\x82\xB3\x82\xF0\x95Ԃ\xB7
-				free(strW);
-			}
-			free(strA);
+	case WM_GETTEXTLENGTH:
+	case LB_GETTEXTLEN: {
+		size_t lenW;
+		wchar_t *strW;
+		if (Msg == WM_GETTEXTLENGTH) {
+			strW = SendMessageAFromW_WM_GETTEXT(hWnd, &lenW);
 		}
-		break;
+		else {
+			strW = SendMessageAFromW_LB_GETTEXT(hWnd, wParam, &lenW);
+		}
+		free(strW);
+		return lenW;
 	}
+	case WM_GETTEXT:
+	case LB_GETTEXT: {
+		size_t lenW;
+		wchar_t *strW;
+		size_t dest_len;
+		if (Msg == WM_GETTEXT) {
+			strW = SendMessageAFromW_WM_GETTEXT(hWnd, &lenW);
+			dest_len = (size_t)wParam;
+		}
+		else {
+			strW = SendMessageAFromW_LB_GETTEXT(hWnd, wParam, &lenW);
+			dest_len = lenW + 1;
+		}
+		wchar_t *dest_ptr = (wchar_t *)lParam;
+		wcsncpy_s(dest_ptr, dest_len, strW, _TRUNCATE);
+		free(strW);
+		return dest_len - 1 < lenW ? dest_len - 1 : lenW;
+	}
 	default:
-		retval = SendDlgItemMessageA(hDlg, nIDDlgItem, Msg, wParam, lParam);
+		retval = SendMessageA(hWnd, Msg, wParam, lParam);
 		break;
 	}
 	return retval;
 }
 
+LRESULT _SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
+{
+	if (pSendMessageW != NULL) {
+		return pSendMessageW(hWnd, Msg, wParam, lParam);
+	}
+	return SendMessageAFromW(hWnd, Msg, wParam, lParam);
+}
+
+LRESULT _SendDlgItemMessageW(HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam)
+{
+	if (pSendDlgItemMessageW != NULL) {
+		return pSendDlgItemMessageW(hDlg, nIDDlgItem, Msg, wParam, lParam);
+	}
+
+	HWND hWnd = GetDlgItem(hDlg, nIDDlgItem);
+	return SendMessageAFromW(hWnd, Msg, wParam, lParam);
+}
+
 HWND _CreateWindowExW(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y,
 							 int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
 {
@@ -224,25 +336,18 @@
 		return pGetDlgItemTextW(hDlg, nIDDlgItem, lpString, cchMax);
 	}
 
-	// \x82\xB1\x82̕\xB6\x8E\x9A\x97񒷂\xCD ANSI
-	size_t len = SendDlgItemMessageA(hDlg, nIDDlgItem, WM_GETTEXTLENGTH, 0, 0);
-	len++;  // for '\0'
-	char *strA = (char *)malloc(sizeof(char) * len);
-	if (strA != NULL) {
-		GetDlgItemTextA(hDlg, nIDDlgItem, strA, (int)len);
-		strA[len - 1] = '\0';
-		wchar_t *strW = ToWcharA(strA);
-		if (strW != NULL) {
-			wcscpy_s(lpString, cchMax, strW);
-			UINT len = (UINT)wcslen(strW); // '\0' \x82\xF0\x8A܂܂Ȃ\xA2\x92\xB7\x82\xB3\x82\xF0\x95Ԃ\xB7
-			free(strW);
-			return len;
-		}
+	if (cchMax <= 1) {
+		return 0;
 	}
 
-	if (cchMax > 0)
-		lpString[0] = 0;
-	return 0;
+	HWND hWnd = GetDlgItem(hDlg, nIDDlgItem);
+	size_t lenW;
+	wchar_t *strW = SendMessageAFromW_WM_GETTEXT(hWnd, &lenW);
+	wchar_t *dest_ptr = lpString;
+	size_t dest_len = (size_t)cchMax;
+	wcsncpy_s(dest_ptr, dest_len, strW, _TRUNCATE);
+	free(strW);
+	return (UINT)(dest_len - 1 < lenW ? dest_len - 1 : lenW);
 }
 
 /**

Modified: trunk/teraterm/common/layer_for_unicode.h
===================================================================
--- trunk/teraterm/common/layer_for_unicode.h	2020-02-06 16:08:10 UTC (rev 8531)
+++ trunk/teraterm/common/layer_for_unicode.h	2020-02-09 05:55:07 UTC (rev 8532)
@@ -46,6 +46,7 @@
 DWORD _GetFileAttributesW(LPCWSTR lpFileName);
 UINT _DragQueryFileW(HDROP hDrop, UINT iFile, LPWSTR lpszFile, UINT cch);
 LRESULT _SendDlgItemMessageW(HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam);
+LRESULT _SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
 HWND _CreateWindowExW(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y,
 							 int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance,
 							 LPVOID lpParam);
@@ -55,6 +56,8 @@
 BOOL _InsertMenuW(HMENU hMenu, UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem);
 BOOL _AppendMenuW(HMENU hMenu, UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem);
 HWND _HtmlHelpW(HWND hwndCaller, LPCWSTR pszFile, UINT uCommand, DWORD_PTR dwData);
+int _GetWindowTextW(HWND hWnd, LPWSTR lpString, int nMaxCount);
+int _GetWindowTextLengthW(HWND hWnd);
 
 // gdi32.lib
 int _AddFontResourceW(LPCWSTR lpFileName);


Ttssh2-commit メーリングリストの案内
Back to archive index