GetClipboardTextA(), GetClipboardTextW() を teraterm/common へ移動
- ttlib_static_cpp.cpp へ
@@ -146,6 +146,9 @@ | ||
146 | 146 | COM_FLOWCTRL, |
147 | 147 | }; |
148 | 148 | |
149 | +/* | |
150 | + * ttlib_static | |
151 | + */ | |
149 | 152 | typedef struct { |
150 | 153 | const char *section; // セクション名 |
151 | 154 | const char *title_key; // タイトル(NULLのとき、title_default を常に使用) |
@@ -156,6 +159,8 @@ | ||
156 | 159 | |
157 | 160 | int TTMessageBoxW(HWND hWnd, const TTMessageBoxInfoW *info, UINT uType, const char *UILanguageFile, ...); |
158 | 161 | wchar_t *TTGetLangStrW(const char *section, const char *key, const wchar_t *def, const char *UILanguageFile); |
162 | +wchar_t *GetClipboardTextW(HWND hWnd, BOOL empty); | |
163 | +char *GetClipboardTextA(HWND hWnd, BOOL empty); | |
159 | 164 | |
160 | 165 | #ifdef __cplusplus |
161 | 166 | } |
@@ -1,3 +1,31 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 1994-1998 T. Teranishi | |
3 | + * (C) 2006-2020 TeraTerm Project | |
4 | + * All rights reserved. | |
5 | + * | |
6 | + * Redistribution and use in source and binary forms, with or without | |
7 | + * modification, are permitted provided that the following conditions | |
8 | + * are met: | |
9 | + * | |
10 | + * 1. Redistributions of source code must retain the above copyright | |
11 | + * notice, this list of conditions and the following disclaimer. | |
12 | + * 2. Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * 3. The name of the author may not be used to endorse or promote products | |
16 | + * derived from this software without specific prior written permission. | |
17 | + * | |
18 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR | |
19 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
20 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
21 | + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
23 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
27 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | + */ | |
1 | 29 | |
2 | 30 | #include <windows.h> |
3 | 31 | #include <string.h> |
@@ -74,3 +102,111 @@ | ||
74 | 102 | |
75 | 103 | return r; |
76 | 104 | } |
105 | + | |
106 | +/** | |
107 | + * クリップボードからwchar_t文字列を取得する | |
108 | + * 文字列長が必要なときはwcslen()すること | |
109 | + * @param hWnd | |
110 | + * @param emtpy TRUEのときクリップボードを空にする | |
111 | + * @retval 文字列へのポインタ 使用後free()すること | |
112 | + * 文字がない(またはエラー時)はNULL | |
113 | + */ | |
114 | +wchar_t *GetClipboardTextW(HWND hWnd, BOOL empty) | |
115 | +{ | |
116 | + UINT Cf; | |
117 | + wchar_t *str_w = NULL; | |
118 | + size_t str_w_len; | |
119 | + HGLOBAL TmpHandle; | |
120 | + | |
121 | + if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { | |
122 | + Cf = CF_UNICODETEXT; | |
123 | + } | |
124 | + else if (IsClipboardFormatAvailable(CF_TEXT)) { | |
125 | + Cf = CF_TEXT; | |
126 | + } | |
127 | + else if (IsClipboardFormatAvailable(CF_OEMTEXT)) { | |
128 | + Cf = CF_OEMTEXT; | |
129 | + } | |
130 | + else { | |
131 | + return NULL; | |
132 | + } | |
133 | + | |
134 | + if (!OpenClipboard(hWnd)) { | |
135 | + return NULL; | |
136 | + } | |
137 | + TmpHandle = GetClipboardData(Cf); | |
138 | + if (TmpHandle == NULL) { | |
139 | + return NULL; | |
140 | + } | |
141 | + if (Cf == CF_UNICODETEXT) { | |
142 | + const wchar_t *str_cb = (wchar_t *)GlobalLock(TmpHandle); | |
143 | + if (str_cb != NULL) { | |
144 | + size_t str_cb_len = GlobalSize(TmpHandle); // bytes | |
145 | + str_w_len = str_cb_len / sizeof(wchar_t); | |
146 | + str_w = (wchar_t *)malloc((str_w_len + 1) * sizeof(wchar_t)); // +1 for terminator | |
147 | + if (str_w != NULL) { | |
148 | + memcpy(str_w, str_cb, str_cb_len); | |
149 | + str_w[str_w_len] = L'\0'; | |
150 | + } | |
151 | + } | |
152 | + } | |
153 | + else { | |
154 | + const char *str_cb = (char *)GlobalLock(TmpHandle); | |
155 | + if (str_cb != NULL) { | |
156 | + size_t str_cb_len = GlobalSize(TmpHandle); | |
157 | + str_w_len = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str_cb, (int)str_cb_len, NULL, 0); | |
158 | + str_w = (wchar_t *)malloc(sizeof(wchar_t) * (str_w_len + 1)); // +1 for terminator | |
159 | + if (str_w != NULL) { | |
160 | + str_w_len = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str_cb, (int)str_cb_len, str_w, (int)str_w_len); | |
161 | + str_w[str_w_len] = L'\0'; | |
162 | + } | |
163 | + } | |
164 | + } | |
165 | + GlobalUnlock(TmpHandle); | |
166 | + if (empty) { | |
167 | + EmptyClipboard(); | |
168 | + } | |
169 | + CloseClipboard(); | |
170 | + return str_w; | |
171 | +} | |
172 | + | |
173 | +/** | |
174 | + * クリップボードからANSI文字列を取得する | |
175 | + * 文字列長が必要なときはstrlen()すること | |
176 | + * @param hWnd | |
177 | + * @param emtpy TRUEのときクリップボードを空にする | |
178 | + * @retval 文字列へのポインタ 使用後free()すること | |
179 | + * 文字がない(またはエラー時)はNULL | |
180 | + */ | |
181 | +char *GetClipboardTextA(HWND hWnd, BOOL empty) | |
182 | +{ | |
183 | + HGLOBAL hGlobal; | |
184 | + const char *lpStr; | |
185 | + size_t length; | |
186 | + char *pool; | |
187 | + | |
188 | + OpenClipboard(hWnd); | |
189 | + hGlobal = (HGLOBAL)GetClipboardData(CF_TEXT); | |
190 | + if (hGlobal == NULL) { | |
191 | + CloseClipboard(); | |
192 | + return NULL; | |
193 | + } | |
194 | + lpStr = (const char *)GlobalLock(hGlobal); | |
195 | + length = GlobalSize(hGlobal); | |
196 | + if (length == 0) { | |
197 | + pool = NULL; | |
198 | + } else { | |
199 | + pool = (char *)malloc(length + 1); // +1 for terminator | |
200 | + memcpy(pool, lpStr, length); | |
201 | + pool[length] = '\0'; | |
202 | + } | |
203 | + GlobalUnlock(hGlobal); | |
204 | + if (empty) { | |
205 | + EmptyClipboard(); | |
206 | + } | |
207 | + CloseClipboard(); | |
208 | + | |
209 | + return pool; | |
210 | +} | |
211 | + | |
212 | + |
@@ -724,75 +724,6 @@ | ||
724 | 724 | } |
725 | 725 | #endif |
726 | 726 | |
727 | -/** | |
728 | - * クリップボードからwchar_t文字列を取得する | |
729 | - * 文字列長が必要なときはwcslen()すること | |
730 | - * @param hWnd | |
731 | - * @param emtpy TRUEのときクリップボードを空にする | |
732 | - * @retval 文字列へのポインタ 使用後free()すること | |
733 | - * 文字がない(またはエラー時)はNULL | |
734 | - * | |
735 | - * TODO ttssh2/ttxssh/auth.c の GetClipboardTextA() の置き換え | |
736 | - */ | |
737 | -static wchar_t *GetClipboardTextW(HWND hWnd, BOOL empty) | |
738 | -{ | |
739 | - UINT Cf; | |
740 | - wchar_t *str_w = NULL; | |
741 | - size_t str_w_len; | |
742 | - HGLOBAL TmpHandle; | |
743 | - | |
744 | - if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { | |
745 | - Cf = CF_UNICODETEXT; | |
746 | - } | |
747 | - else if (IsClipboardFormatAvailable(CF_TEXT)) { | |
748 | - Cf = CF_TEXT; | |
749 | - } | |
750 | - else if (IsClipboardFormatAvailable(CF_OEMTEXT)) { | |
751 | - Cf = CF_OEMTEXT; | |
752 | - } | |
753 | - else { | |
754 | - return NULL; | |
755 | - } | |
756 | - | |
757 | - if (!OpenClipboard(hWnd)) { | |
758 | - return NULL; | |
759 | - } | |
760 | - TmpHandle = GetClipboardData(Cf); | |
761 | - if (TmpHandle == NULL) { | |
762 | - return NULL; | |
763 | - } | |
764 | - if (Cf == CF_UNICODETEXT) { | |
765 | - const wchar_t *str_cb = (wchar_t *)GlobalLock(TmpHandle); | |
766 | - if (str_cb != NULL) { | |
767 | - size_t str_cb_len = GlobalSize(TmpHandle); // bytes | |
768 | - str_w_len = str_cb_len / sizeof(wchar_t); | |
769 | - str_w = malloc((str_w_len + 1) * sizeof(wchar_t)); // +1 for terminator | |
770 | - if (str_w != NULL) { | |
771 | - memcpy(str_w, str_cb, str_cb_len); | |
772 | - str_w[str_w_len] = L'\0'; | |
773 | - } | |
774 | - } | |
775 | - } | |
776 | - else { | |
777 | - const char *str_cb = (char *)GlobalLock(TmpHandle); | |
778 | - if (str_cb != NULL) { | |
779 | - size_t str_cb_len = GlobalSize(TmpHandle); | |
780 | - str_w_len = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str_cb, (int)str_cb_len, NULL, 0); | |
781 | - str_w = malloc(sizeof(wchar_t) * (str_w_len + 1)); // +1 for terminator | |
782 | - if (str_w != NULL) { | |
783 | - str_w_len = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str_cb, (int)str_cb_len, str_w, (int)str_w_len); | |
784 | - str_w[str_w_len] = L'\0'; | |
785 | - } | |
786 | - } | |
787 | - } | |
788 | - GlobalUnlock(TmpHandle); | |
789 | - if (empty) { | |
790 | - EmptyClipboard(); | |
791 | - } | |
792 | - CloseClipboard(); | |
793 | - return str_w; | |
794 | -} | |
795 | - | |
796 | 727 | #if UNICODE_INTERNAL_BUFF |
797 | 728 | /** |
798 | 729 | * クリップボード用テキスト送信する |
@@ -739,46 +739,6 @@ | ||
739 | 739 | return TRUE; |
740 | 740 | } |
741 | 741 | |
742 | -/** | |
743 | - * クリップボードからANSI文字列を取得する | |
744 | - * 文字列長が必要なときはstrlen()すること | |
745 | - * @param hWnd | |
746 | - * @param emtpy TRUEのときクリップボードを空にする | |
747 | - * @retval 文字列へのポインタ 使用後free()すること | |
748 | - * 文字がない(またはエラー時)はNULL | |
749 | - */ | |
750 | -char *GetClipboardTextA(HWND hWnd, BOOL empty) | |
751 | -{ | |
752 | - HGLOBAL hGlobal; | |
753 | - const char *lpStr; | |
754 | - size_t length; | |
755 | - char *pool; | |
756 | - | |
757 | - OpenClipboard(hWnd); | |
758 | - hGlobal = (HGLOBAL)GetClipboardData(CF_TEXT); | |
759 | - if (hGlobal == NULL) { | |
760 | - CloseClipboard(); | |
761 | - return NULL; | |
762 | - } | |
763 | - lpStr = (const char *)GlobalLock(hGlobal); | |
764 | - length = GlobalSize(hGlobal); | |
765 | - if (length == 0) { | |
766 | - pool = NULL; | |
767 | - } else { | |
768 | - pool = (char *)malloc(length + 1); // +1 for terminator | |
769 | - memcpy(pool, lpStr, length); | |
770 | - pool[length] = '\0'; | |
771 | - } | |
772 | - GlobalUnlock(hGlobal); | |
773 | - if (empty) { | |
774 | - EmptyClipboard(); | |
775 | - } | |
776 | - CloseClipboard(); | |
777 | - | |
778 | - return pool; | |
779 | -} | |
780 | - | |
781 | - | |
782 | 742 | static INT_PTR CALLBACK auth_dlg_proc(HWND dlg, UINT msg, WPARAM wParam, |
783 | 743 | LPARAM lParam) |
784 | 744 | { |