[Ttssh2-commit] [8301] TipWinのAPIをUnicodeを考慮して修正

scmno****@osdn***** scmno****@osdn*****
2019年 10月 15日 (火) 01:12:58 JST


Revision: 8301
          https://osdn.net/projects/ttssh2/scm/svn/commits/8301
Author:   zmatsuo
Date:     2019-10-15 01:12:57 +0900 (Tue, 15 Oct 2019)
Log Message:
-----------
TipWinのAPIをUnicodeを考慮して修正

- TipWinCreate() を TipWinCreateA() に改名
- 位置とテキストの指定が不要な TipWinCreate() を追加
- TipWinSetText() を TipWinSetTextA() に改名
- TipWinSetText() は TipWinSetTextA/W() に置き換えるようにした
- WM_NCDESTORY の処理は何も行っていなかったので削除
- WM_SETTEXT の処理を CTipWin::SetText() に移動
  - Unicode版を考慮
- IsClassRegistered(), UnregisterClass() は削除
  - exe,dllごとにウィンドウクラスの登録を行うようになったので不要

Modified Paths:
--------------
    trunk/teraterm/common/tipwin.cpp
    trunk/teraterm/common/tipwin.h
    trunk/teraterm/teraterm/sizetip.c
    trunk/teraterm/ttpdlg/ttdlg.c
    trunk/ttssh2/ttxssh/auth.c

-------------- next part --------------
Modified: trunk/teraterm/common/tipwin.cpp
===================================================================
--- trunk/teraterm/common/tipwin.cpp	2019-10-14 14:36:15 UTC (rev 8300)
+++ trunk/teraterm/common/tipwin.cpp	2019-10-14 16:12:57 UTC (rev 8301)
@@ -68,6 +68,8 @@
 
 #include "tipwin.h"
 
+#define TipWinClassName _T("TeraTermTipWinClass")
+
 typedef struct tagTipWinData {
 	HFONT tip_font;
 	COLORREF tip_bg;
@@ -79,7 +81,6 @@
 	RECT rect;
 	int px;
 	int py;
-	TCHAR class_name[32];
 } TipWin;
 
 VOID CTipWin::CalcStrRect(VOID)
@@ -150,33 +151,6 @@
 
 		case WM_NCHITTEST:
 			return HTTRANSPARENT;
-		case WM_SETTEXT:
-			if(self) {
-				LPCTSTR str = (LPCTSTR) lParam;
-				const int str_width = self->tWin->str_rect.right - self->tWin->str_rect.left;
-				const int str_height = self->tWin->str_rect.bottom - self->tWin->str_rect.top;
-
-				free((void *)(self->tWin->str));
-				self->tWin->str_len = _tcslen(str);
-				self->tWin->str = _tcsdup(str);
-				self->CalcStrRect();
-
-				SetWindowPos(hWnd, NULL,
-							 0, 0,
-							 str_width + TIP_WIN_FRAME_WIDTH * 2, str_height + TIP_WIN_FRAME_WIDTH * 2,
-				             SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
-				InvalidateRect(hWnd, NULL, FALSE);
-			}
-			break;
-		case WM_NCDESTROY:
-				/*
-				 * use-after-free\x82ɂ\xE6\x82\xE8Tera Term\x82̓\xAE\x8D삪\x95s\x88\xC0\x92\xE8\x82ƂȂ\xE9\x96\xE2\x91\xE8\x82\xF0\x8FC\x90\xB3\x82\xB5\x82\xBD\x81B
-				 *
-				 * WinMain\x82\xC5 CVTWindow \x83N\x83\x89\x83X\x82̃R\x83\x93\x83X\x83g\x83\x89\x83N\x83^\x82ŁA\x83A\x83\x8D\x83P\x81[\x83g\x82\xB5\x82\xBD
-				 * TipWin\x83\x81\x83\x93\x83o\x81[\x82\xF0\x81A\x82\xB1\x82\xB1\x82̃^\x83C\x83~\x83\x93\x83O\x82ʼn\xF0\x95\xFA\x82\xB5\x82Ă\xA2\x82\xBD\x82\xBD\x82߁B
-				 * \x90\xB3\x82\xB5\x82\xAD\x82\xCD CVTWindow \x83N\x83\x89\x83X\x82̃f\x83X\x83g\x83\x89\x83N\x83^\x82ʼn\xF0\x95\xFA\x82\xB7\x82\xE9\x81B
-				 */
-			break;
 		case WM_TIMER:
 			if(self) {
 				if(self->timerid > 0)
@@ -213,15 +187,6 @@
 	}
 }
 
-BOOL CTipWin::IsClassRegistered()
-{
-	if (*class_name == NULL) {
-		_snprintf_s(class_name, sizeof(class_name), _TRUNCATE, _T("%s_%p"), TipWinClassName, hInstance);
-	}
-	WNDCLASS twc = { 0 };
-	return (GetClassInfo(hInstance, class_name, &twc) > 0);
-}
-
 ATOM CTipWin::RegisterClass()
 {
 	WNDCLASS wc;
@@ -238,11 +203,6 @@
 	return ::RegisterClass(&wc);
 }
 
-BOOL CTipWin::UnregisterClass()
-{
-	return ::UnregisterClass(class_name, hInstance);
-}
-
 VOID CTipWin::Create(HWND pHwnd)
 {
 	LOGFONTA logfont;
@@ -251,9 +211,10 @@
 	if(hInstance == NULL) {
 		hInstance = (HINSTANCE)GetWindowLongPtr(pHwnd, GWLP_HINSTANCE);
 	}
-	if (! IsClassRegistered()) {
-		RegisterClass();
+	if (class_name[0] == 0) {
+		_snprintf_s(class_name, _countof(class_name), _TRUNCATE, _T("%s_%p"), TipWinClassName, hInstance);
 	}
+	RegisterClass();
 	if (tWin == NULL) {
 		return;
 	}
@@ -268,14 +229,6 @@
 	logfont.lfWidth = MulDiv(logfont.lfWidth, uDpi, 96);
 	logfont.lfHeight = MulDiv(logfont.lfHeight, uDpi, 96);
 	tWin->tip_font = CreateFontIndirect(&logfont);
-
-	/*
-	 * RegisterClass()\x82\xAA\x8E\xB8\x94s\x82\xB5\x82\xBD\x8Fꍇ\x82́ACreateWindowEx()\x82\xAA 87 (ERROR_INVALID_PARAMETER)\x82\xC5
-	 * \x83G\x83\x89\x81[\x82ƂȂ邽\x82߁A\x8CĂяo\x82\xB3\x82Ȃ\xA2\x82悤\x82ɂ\xB7\x82\xE9\x81B
-	 *
-	 * WindowsMe(9x)\x82ł́ASSH\x94F\x8F؃_\x83C\x83A\x83\x8D\x83O\x82̃c\x81[\x83\x8B\x83`\x83b\x83v\x95\\x8E\xA6\x82\xC5 RegisterClass() \x82\xAA
-	 * \x8E\xB8\x94s\x82\xB7\x82\xE9\x81B\x8C\xB4\x88\xF6\x95s\x96\xBE\x81B
-	 */
 	tWin->tip_wnd =
 		CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
 					   class_name,
@@ -316,13 +269,25 @@
 	}
 }
 
-VOID CTipWin::SetText(TCHAR *str)
+VOID CTipWin::SetText(const TCHAR *str)
 {
-	if(IsExists()) {
-		SetWindowText(tWin->tip_wnd, str);
-		// \x83c\x81[\x83\x8B\x83`\x83b\x83v\x82̃e\x83L\x83X\x83g\x82ƃE\x83B\x83\x93\x83h\x83E\x82̕`\x89揇\x82̊֌W\x82Ńe\x83L\x83X\x83g\x82\xF02\x93x\x95`\x89悵\x82ăc\x81[\x83\x8B\x83`\x83b\x83v\x83E\x83B\x83\x93\x83h\x83E\x82\xF0\x83\x8A\x83T\x83C\x83Y\x82\xB7\x82\xE9
-		SetWindowText(tWin->tip_wnd, str);
+	if(!IsExists()) {
+		return;
 	}
+
+	TipWin* self = tWin;
+	self->str_len = _tcslen(str);
+	self->str = _tcsdup(str);
+	CalcStrRect();
+
+	const int str_width = self->str_rect.right - self->str_rect.left;
+	const int str_height = self->str_rect.bottom - self->str_rect.top;
+	SetWindowText(tWin->tip_wnd, str);
+	SetWindowPos(tWin->tip_wnd, NULL,
+				 0, 0,
+				 str_width + TIP_WIN_FRAME_WIDTH * 2, str_height + TIP_WIN_FRAME_WIDTH * 2,
+				 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
+	InvalidateRect(tWin->tip_wnd, NULL, FALSE);
 }
 
 VOID CTipWin::SetHideTimer(int ms)
@@ -362,11 +327,18 @@
 	return FALSE;
 }
 
-TipWin *TipWinCreate(HINSTANCE hInstance, HWND src, int cx, int cy, const TCHAR *str)
+TipWin *TipWinCreate(HINSTANCE hInstance, HWND src)
 {
 	CTipWin* tipwin = new CTipWin(hInstance);
 	tipwin->Create(src);
-	tipwin->SetText((TCHAR*)str);
+	return (TipWin*)tipwin;
+}
+
+TipWin *TipWinCreateA(HINSTANCE hInstance, HWND src, int cx, int cy, const char *str)
+{
+	CTipWin* tipwin = new CTipWin(hInstance);
+	tipwin->Create(src);
+	tipwin->SetText(str);
 	tipwin->SetPos(cx, cy);
 	tipwin->SetVisible(TRUE);
 	return (TipWin*)tipwin;
@@ -393,7 +365,7 @@
 
 	/* \x95\xB6\x8E\x9A\x97\xF1\x82̒\xB7\x82\xB3\x82\xF0\x8Cv\x8EZ\x82\xB7\x82\xE9 */
 	str_len = _tcslen(str);
-	
+
 	/* DPI\x82\xF0\x8E擾\x82\xB7\x82\xE9 */
 	uDpi = GetMonitorDpiFromWindow(src);
 
@@ -431,7 +403,7 @@
 	tipwin->SetPos(x, y);
 }
 
-void TipWinSetText(TipWin* tWin, TCHAR *str)
+void TipWinSetTextA(TipWin* tWin, const char *str)
 {
 	CTipWin* tipwin = (CTipWin*) tWin;
 	tipwin->SetText(str);

Modified: trunk/teraterm/common/tipwin.h
===================================================================
--- trunk/teraterm/common/tipwin.h	2019-10-14 14:36:15 UTC (rev 8300)
+++ trunk/teraterm/common/tipwin.h	2019-10-14 16:12:57 UTC (rev 8301)
@@ -37,12 +37,13 @@
 
 #define	TIP_WIN_FRAME_WIDTH	6
 
-#define TipWinClassName _T("TeraTermTipWinClass")
-
 typedef struct tagTipWinData TipWin;
 
-TipWin *TipWinCreate(HINSTANCE hInstance, HWND src, int cx, int cy, const TCHAR *str);
-void TipWinSetText(TipWin *tWin, TCHAR *text);
+TipWin *TipWinCreate(HINSTANCE hInstance, HWND src);
+TipWin *TipWinCreateA(HINSTANCE hInstance, HWND src, int cx, int cy, const char *str);
+TipWin *TipWinCreateW(HINSTANCE hInstance, HWND src, int cx, int cy, const wchar_t *str);
+void TipWinSetTextA(TipWin *tWin, const char *text);
+void TipWinSetTextW(TipWin *tWin, const wchar_t *text);
 void TipWinDestroy(TipWin *tWin);
 void TipWinGetTextWidthHeight(HWND src, const TCHAR *str, int *width, int *height);
 void TipWinGetPos(TipWin *tWin, int *x, int *y);
@@ -52,6 +53,14 @@
 int TipWinIsExists(TipWin *tWin);
 int TipWinIsVisible(TipWin *tWin);
 
+#if !defined(_UNICODE)
+#define	TipWinCreateT(p1, p2, p3, p4, p5)	TipWinCreateA(p1, p2, p3, p4, p5)
+#define TipWinSetText(p1, p2)				TipWinSetTextA(p1, p2)
+#else
+#define	TipWinCreateT(p1, p2, p3, p4, p5)	TipWinCreateW(p1, p2, p3, p4, p5)
+#define TipWinSetText(p1, p2)				TipWinSetTextW(p1, p2)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
@@ -64,7 +73,8 @@
 	~CTipWin();
 	VOID Create(HWND pHwnd);
 	VOID Destroy();
-	VOID SetText(TCHAR *str);
+	VOID SetText(const char *str);
+	VOID SetText(const wchar_t *str);
 	POINT GetPos();
 	VOID SetPos(int x, int y);
 	VOID SetHideTimer(int ms);

Modified: trunk/teraterm/teraterm/sizetip.c
===================================================================
--- trunk/teraterm/teraterm/sizetip.c	2019-10-14 14:36:15 UTC (rev 8300)
+++ trunk/teraterm/teraterm/sizetip.c	2019-10-14 16:12:57 UTC (rev 8301)
@@ -144,7 +144,7 @@
 		cx = point.x;
 		cy = point.y;
 
-		SizeTip = TipWinCreate(NULL, src, cx, cy, str);
+		SizeTip = TipWinCreateT(NULL, src, cx, cy, str);
 
 		//OutputDebugPrintf("Created: (%d,%d)\n", cx, cy);
 

Modified: trunk/teraterm/ttpdlg/ttdlg.c
===================================================================
--- trunk/teraterm/ttpdlg/ttdlg.c	2019-10-14 14:36:15 UTC (rev 8300)
+++ trunk/teraterm/ttpdlg/ttdlg.c	2019-10-14 16:12:57 UTC (rev 8301)
@@ -1300,18 +1300,16 @@
 
 			// \x83c\x81[\x83\x8B\x83`\x83b\x83v\x82\xF0\x95\\x8E\xA6\x82\xB7\x82\xE9
 			if (g_SerialDlgSpeedTip == NULL) {
-				g_SerialDlgSpeedTip = TipWinCreate(hInst, hWnd, cx, cy, str);
+				g_SerialDlgSpeedTip = TipWinCreate(hInst, hWnd);
 				TipWinSetHideTimer(g_SerialDlgSpeedTip, tooltip_timeout);
+			}
+			if (!TipWinIsVisible(g_SerialDlgSpeedTip))
+				TipWinSetVisible(g_SerialDlgSpeedTip, TRUE);
 
-			} else {
-				if (!TipWinIsVisible(g_SerialDlgSpeedTip))
-					TipWinSetVisible(g_SerialDlgSpeedTip, TRUE);
+			TipWinSetText(g_SerialDlgSpeedTip, str);
+			TipWinSetPos(g_SerialDlgSpeedTip, cx, cy);
+			TipWinSetHideTimer(g_SerialDlgSpeedTip, tooltip_timeout);
 
-				TipWinSetText(g_SerialDlgSpeedTip, str);
-				TipWinSetPos(g_SerialDlgSpeedTip, cx, cy);
-				TipWinSetHideTimer(g_SerialDlgSpeedTip, tooltip_timeout);
-			}
-
 			break;
 	}
     return CallWindowProc(g_defSerialDlgSpeedComboboxWndProc, hWnd, msg, wp, lp);

Modified: trunk/ttssh2/ttxssh/auth.c
===================================================================
--- trunk/ttssh2/ttxssh/auth.c	2019-10-14 14:36:15 UTC (rev 8300)
+++ trunk/ttssh2/ttxssh/auth.c	2019-10-14 16:12:57 UTC (rev 8301)
@@ -122,7 +122,7 @@
 					_tcscat_s(uimsg, _countof(uimsg), pvar->ts->UIMsg);
 				}
 				GetWindowRect(control, &rect);
-				data->tipwin = TipWinCreate(hInst, control, rect.left, rect.bottom, uimsg);
+				data->tipwin = TipWinCreateT(hInst, control, rect.left, rect.bottom, uimsg);
 			}
 
 			return 0;


Ttssh2-commit メーリングリストの案内