[Ttssh2-commit] [8245] common/i18n.c でUnicode版APIを使うようにした

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2019年 10月 3日 (木) 00:06:57 JST


Revision: 8245
          https://osdn.net/projects/ttssh2/scm/svn/commits/8245
Author:   zmatsuo
Date:     2019-10-03 00:06:57 +0900 (Thu, 03 Oct 2019)
Log Message:
-----------
common/i18n.c でUnicode版APIを使うようにした

- 使えない場合ANSI版APIを使用する(9xの場合)
  - NT系ではUnicode版lngファイルを使用可能になった
  - いつでも言語変更できるようになった(SetI18DlgStrs()を使用している場合)
- メニューやプログラムで言語変更している部分は未対応

Modified Paths:
--------------
    branches/unicode_buf/teraterm/common/compat_win.cpp
    branches/unicode_buf/teraterm/common/compat_win.h
    branches/unicode_buf/teraterm/common/i18n.c
    branches/unicode_buf/teraterm/common/ttlib.c
    branches/unicode_buf/teraterm/common/ttlib.h
    branches/unicode_buf/teraterm/ttpcmn/CMakeLists.txt
    branches/unicode_buf/teraterm/ttpcmn/ttcmn.c
    branches/unicode_buf/teraterm/ttptek/CMakeLists.txt
    branches/unicode_buf/ttpmenu/CMakeLists.txt

-------------- next part --------------
Modified: branches/unicode_buf/teraterm/common/compat_win.cpp
===================================================================
--- branches/unicode_buf/teraterm/common/compat_win.cpp	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/common/compat_win.cpp	2019-10-02 15:06:57 UTC (rev 8245)
@@ -34,6 +34,8 @@
 
 #include "dllutil.h"
 
+BOOL (WINAPI *pSetWindowTextW)(HWND hWnd, LPCWSTR lpString);
+DWORD (WINAPI *pGetPrivateProfileStringW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault, LPWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName);
 UINT (WINAPI *pDragQueryFileW)(HDROP hDrop, UINT iFile, LPWSTR lpszFile, UINT cch);
 DWORD (WINAPI *pGetFileAttributesW)(LPCWSTR lpFileName);
 BOOL (WINAPI *pSetDlgItemTextW)(HWND hDlg, int nIDDlgItem, LPCWSTR lpString);
@@ -59,6 +61,7 @@
 	{ "MonitorFromRect", (void **)&pMonitorFromRect },
 	{ "AdjustWindowRectExForDpi", (void **)&pAdjustWindowRectExForDpi },
 	{ "SetDlgItemTextW", (void **)&pSetDlgItemTextW },
+	{ "SetWindowTextW", (void **)&pSetWindowTextW },
 	{ NULL, NULL },
 };
 
@@ -82,6 +85,7 @@
 
 static const APIInfo Lists_kernel32[] = {
 	{ "GetFileAttributesW", (void **)&pGetFileAttributesW },
+	{ "GetPrivateProfileStringW", (void **)&pGetPrivateProfileStringW },
 	{ NULL, NULL },
 };
 

Modified: branches/unicode_buf/teraterm/common/compat_win.h
===================================================================
--- branches/unicode_buf/teraterm/common/compat_win.h	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/common/compat_win.h	2019-10-02 15:06:57 UTC (rev 8245)
@@ -67,7 +67,10 @@
 #define OPENFILENAME_SIZE_VERSION_400A	76
 #endif
 
-extern UINT (WINAPI *pDragQueryFileW)(HDROP hDrop, UINT iFile, LPWSTR lpszFile, UINT cch);
+extern BOOL(WINAPI *pSetWindowTextW)(HWND hWnd, LPCWSTR lpString);
+extern DWORD(WINAPI *pGetPrivateProfileStringW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault,
+												LPWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName);
+extern UINT(WINAPI *pDragQueryFileW)(HDROP hDrop, UINT iFile, LPWSTR lpszFile, UINT cch);
 extern DWORD (WINAPI *pGetFileAttributesW)(LPCWSTR lpFileName);
 extern BOOL (WINAPI *pSetDlgItemTextW)(HWND hDlg, int nIDDlgItem, LPCWSTR lpString);
 extern BOOL (WINAPI *pAlphaBlend)(HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);

Modified: branches/unicode_buf/teraterm/common/i18n.c
===================================================================
--- branches/unicode_buf/teraterm/common/i18n.c	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/common/i18n.c	2019-10-02 15:06:57 UTC (rev 8245)
@@ -29,23 +29,32 @@
 #include "i18n.h"
 #include "ttlib.h"
 #include "codeconv.h"
+#include "compat_win.h"
 
 #include <assert.h>
 #include <tchar.h>
 
-#if defined(UNICODE)
-DllExport void GetI18nStrW(const char *section, const char *key, wchar_t *buf, int buf_len, const wchar_t *def, const char *iniFile)
+DllExport void WINAPI GetI18nStrW(const char *section, const char *key, wchar_t *buf, int buf_len, const wchar_t *def,
+								  const char *iniFile)
 {
-	wchar_t sectionW[64];
-	wchar_t keyW[128];
-	wchar_t iniFileW[MAX_PATH];
-	MultiByteToWideChar(CP_ACP, 0, section, -1, sectionW, _countof(sectionW));
-	MultiByteToWideChar(CP_ACP, 0, key, -1, keyW, _countof(keyW));
-	MultiByteToWideChar(CP_ACP, 0, iniFile, -1, iniFileW, _countof(iniFileW));
-	GetPrivateProfileStringW(sectionW, keyW, def, buf, buf_len, iniFileW);
+	if (pGetPrivateProfileStringW != NULL) {
+		wchar_t sectionW[64];
+		wchar_t keyW[128];
+		wchar_t iniFileW[MAX_PATH];
+		MultiByteToWideChar(CP_ACP, 0, section, -1, sectionW, _countof(sectionW));
+		MultiByteToWideChar(CP_ACP, 0, key, -1, keyW, _countof(keyW));
+		MultiByteToWideChar(CP_ACP, 0, iniFile, -1, iniFileW, _countof(iniFileW));
+		pGetPrivateProfileStringW(sectionW, keyW, def, buf, buf_len, iniFileW);
+	}
+	else {
+		char tmp[MAX_UIMSG];
+		char defA[MAX_UIMSG];
+		WideCharToMultiByte(CP_ACP, 0, def, -1, defA, _countof(defA), NULL, NULL);
+		GetPrivateProfileStringA(section, key, defA, tmp, _countof(tmp), iniFile);
+		MultiByteToWideChar(CP_ACP, 0, tmp, -1, buf, buf_len);
+	}
 	RestoreNewLineW(buf);
 }
-#endif
 
 DllExport void WINAPI GetI18nStr(const char *section, const char *key, PCHAR buf, int buf_len, const char *def, const char *iniFile)
 {
@@ -57,26 +66,28 @@
 void GetI18nStrU8(const char *section, const char *key, char *buf, int buf_len, const char *def, const char *iniFile)
 {
 	size_t r;
-#if defined(UNICODE)
-	wchar_t tmp[MAX_UIMSG];
-	wchar_t defW[MAX_UIMSG];
-	r = UTF8ToWideChar(def, -1, defW, _countof(defW));
-	assert(r != 0);
-	GetI18nStrW(section, key, tmp, _countof(tmp), defW, iniFile);
-	r = buf_len;
-	WideCharToUTF8(tmp, NULL, buf, &r);
-	assert(r != 0);
-#else
-	// ANSI -> Wide -> utf8
-	char strA[MAX_UIMSG];
-	wchar_t strW[MAX_UIMSG];
-	GetI18nStr(section, key, strA, _countof(strA), def, iniFile);
-	r = MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, _countof(strW));
-	assert(r != 0);
-	r = buf_len;
-	WideCharToUTF8(strW, NULL, buf, &r);
-	assert(r != 0);
-#endif
+	if (pGetPrivateProfileStringW != NULL) {
+		// unicode base
+		wchar_t tmp[MAX_UIMSG];
+		wchar_t defW[MAX_UIMSG];
+		r = UTF8ToWideChar(def, -1, defW, _countof(defW));
+		assert(r != 0);
+		GetI18nStrW(section, key, tmp, _countof(tmp), defW, iniFile);
+		r = buf_len;
+		WideCharToUTF8(tmp, NULL, buf, &r);
+		assert(r != 0);
+	}
+	else {
+		// ANSI -> Wide -> utf8
+		char strA[MAX_UIMSG];
+		wchar_t strW[MAX_UIMSG];
+		GetI18nStr(section, key, strA, _countof(strA), def, iniFile);
+		r = MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, _countof(strW));
+		assert(r != 0);
+		r = buf_len;
+		WideCharToUTF8(strW, NULL, buf, &r);
+		assert(r != 0);
+	}
 }
 
 int WINAPI GetI18nLogfont(const char *section, const char *key, PLOGFONTA logfont, int ppi, const char *iniFile)
@@ -116,22 +127,44 @@
 	size_t i;
 	assert(hDlgWnd != NULL);
 	assert(infoCount > 0);
-	for (i = 0 ; i < infoCount; i++) {
-		const char *key = infos[i].key;
-		TCHAR uimsg[MAX_UIMSG];
-		GetI18nStrT(section, key, uimsg, sizeof(uimsg), _T(""), UILanguageFile);
-		if (uimsg[0] != _T('\0')) {
-			const int nIDDlgItem = infos[i].nIDDlgItem;
-			BOOL r;
-			if (nIDDlgItem == 0) {
-				r = SetWindowText(hDlgWnd, uimsg);
-				assert(r != 0);
-			} else {
-				r = SetDlgItemText(hDlgWnd, nIDDlgItem, uimsg);
-				assert(r != 0);
+	if (pGetPrivateProfileStringW == NULL) {
+		// ANSI
+		for (i = 0 ; i < infoCount; i++) {
+			const char *key = infos[i].key;
+			char uimsg[MAX_UIMSG];
+			GetI18nStr(section, key, uimsg, sizeof(uimsg), _T(""), UILanguageFile);
+			if (uimsg[0] != '\0') {
+				const int nIDDlgItem = infos[i].nIDDlgItem;
+				BOOL r;
+				if (nIDDlgItem == 0) {
+					r = SetWindowTextA(hDlgWnd, uimsg);
+					assert(r != 0);
+				} else {
+					r = SetDlgItemTextA(hDlgWnd, nIDDlgItem, uimsg);
+					assert(r != 0);
+				}
+				(void)r;
 			}
-			(void)r;
 		}
+	} else {
+		// UNICODE
+		for (i = 0 ; i < infoCount; i++) {
+			const char *key = infos[i].key;
+			wchar_t uimsg[MAX_UIMSG];
+			GetI18nStrW(section, key, uimsg, sizeof(uimsg), L"", UILanguageFile);
+			if (uimsg[0] != L'\0') {
+				const int nIDDlgItem = infos[i].nIDDlgItem;
+				BOOL r;
+				if (nIDDlgItem == 0) {
+					r = pSetWindowTextW(hDlgWnd, uimsg);
+					assert(r != 0);
+				} else {
+					r = pSetDlgItemTextW(hDlgWnd, nIDDlgItem, uimsg);
+					assert(r != 0);
+				}
+				(void)r;
+			}
+		}
 	}
 }
 

Modified: branches/unicode_buf/teraterm/common/ttlib.c
===================================================================
--- branches/unicode_buf/teraterm/common/ttlib.c	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/common/ttlib.c	2019-10-02 15:06:57 UTC (rev 8245)
@@ -807,6 +807,46 @@
 	memcpy(Text, buf, size);
 }
 
+void RestoreNewLineW(wchar_t *Text)
+{
+	int i, j=0;
+	int size= wcslen(Text);
+	wchar_t *buf = (wchar_t *)_alloca((size+1) * sizeof(wchar_t));
+
+	memset(buf, 0, (size+1) * sizeof(wchar_t));
+	for (i=0; i<size; i++) {
+		if (Text[i] == L'\\' && i<size ) {
+			switch (Text[i+1]) {
+				case L'\\':
+					buf[j] = L'\\';
+					i++;
+					break;
+				case L'n':
+					buf[j] = L'\n';
+					i++;
+					break;
+				case L't':
+					buf[j] = L'\t';
+					i++;
+					break;
+				case L'0':
+					buf[j] = L'\0';
+					i++;
+					break;
+				default:
+					buf[j] = L'\\';
+			}
+			j++;
+		}
+		else {
+			buf[j] = Text[i];
+			j++;
+		}
+	}
+	/* use memcpy to copy with '\0' */
+	memcpy(Text, buf, size * sizeof(wchar_t));
+}
+
 BOOL GetNthString(PCHAR Source, int Nth, int Size, PCHAR Dest)
 {
 	int i, j, k;

Modified: branches/unicode_buf/teraterm/common/ttlib.h
===================================================================
--- branches/unicode_buf/teraterm/common/ttlib.h	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/common/ttlib.h	2019-10-02 15:06:57 UTC (rev 8245)
@@ -67,6 +67,7 @@
 void ParseStrftimeFileName(PCHAR FName, int destlen);
 void ConvFName(PCHAR HomeDir, PCHAR Temp, int templen, PCHAR DefExt, PCHAR FName, int destlen);
 void RestoreNewLine(PCHAR Text);
+void RestoreNewLineW(wchar_t *Text);
 BOOL GetNthString(PCHAR Source, int Nth, int Size, PCHAR Dest);
 void GetNthNum(PCHAR Source, int Nth, int far *Num);
 int GetNthNum2(PCHAR Source, int Nth, int defval);

Modified: branches/unicode_buf/teraterm/ttpcmn/CMakeLists.txt
===================================================================
--- branches/unicode_buf/teraterm/ttpcmn/CMakeLists.txt	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/ttpcmn/CMakeLists.txt	2019-10-02 15:06:57 UTC (rev 8245)
@@ -21,6 +21,10 @@
   ../common/tt_res.h
   ../common/codeconv.h
   ../common/codeconv.cpp
+  ../common/compat_win.h
+  ../common/compat_win.cpp
+  ../common/dllutil.h
+  ../common/dllutil.cpp
   )
 
 source_group(

Modified: branches/unicode_buf/teraterm/ttpcmn/ttcmn.c
===================================================================
--- branches/unicode_buf/teraterm/ttpcmn/ttcmn.c	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/ttpcmn/ttcmn.c	2019-10-02 15:06:57 UTC (rev 8245)
@@ -54,6 +54,7 @@
 #include "compat_w95.h"
 #include "tt_res.h"
 #include "codeconv.h"
+#include "compat_win.h"
 
 #define DllExport __declspec(dllexport)
 #include "ttcommon.h"
@@ -3023,6 +3024,7 @@
 				// dll\x83\x8D\x81[\x83h\x8E\xB8\x94s\x81Ateraterm\x82\xAA\x8BN\x93\xAE\x82\xB5\x82Ȃ\xA2
 				return FALSE;
 			}
+			WinCompatInit();
 			break;
 		case DLL_PROCESS_DETACH:
 			/* do process cleanup */

Modified: branches/unicode_buf/teraterm/ttptek/CMakeLists.txt
===================================================================
--- branches/unicode_buf/teraterm/ttptek/CMakeLists.txt	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/teraterm/ttptek/CMakeLists.txt	2019-10-02 15:06:57 UTC (rev 8245)
@@ -7,7 +7,6 @@
 
 set(COMMON_SRC
   ../common/tttypes.h
-  ../common/i18n.c
   ../common/i18n.h
   ../common/ttlib.c
   ../common/ttlib.h

Modified: branches/unicode_buf/ttpmenu/CMakeLists.txt
===================================================================
--- branches/unicode_buf/ttpmenu/CMakeLists.txt	2019-10-02 15:06:44 UTC (rev 8244)
+++ branches/unicode_buf/ttpmenu/CMakeLists.txt	2019-10-02 15:06:57 UTC (rev 8245)
@@ -9,6 +9,10 @@
   ../teraterm/common/i18n.c
   ../teraterm/common/codeconv.h
   ../teraterm/common/codeconv.cpp
+  ../teraterm/common/compat_win.h
+  ../teraterm/common/compat_win.cpp
+  ../teraterm/common/dllutil.h
+  ../teraterm/common/dllutil.cpp
   )
 
 source_group(


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