924 * @param tb @Textbuf type to be changed |
924 * @param tb @Textbuf type to be changed |
925 * @return Return true on successfull change of Textbuf, or false otherwise |
925 * @return Return true on successfull change of Textbuf, or false otherwise |
926 */ |
926 */ |
927 bool InsertTextBufferClipboard(Textbuf *tb) |
927 bool InsertTextBufferClipboard(Textbuf *tb) |
928 { |
928 { |
929 if (IsClipboardFormatAvailable(CF_TEXT)) { |
929 HGLOBAL cbuf; |
930 HGLOBAL cbuf; |
930 char utf8_buf[512]; |
931 const byte *data, *dataptr; |
931 const char *ptr; |
932 uint16 width = 0; |
932 |
933 uint16 length = 0; |
933 WChar c; |
934 |
934 uint16 width, length; |
|
935 |
|
936 if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { |
|
937 int bytec; |
|
938 |
|
939 OpenClipboard(NULL); |
|
940 cbuf = GetClipboardData(CF_UNICODETEXT); |
|
941 |
|
942 ptr = GlobalLock(cbuf); |
|
943 bytec = WideCharToMultiByte(CP_UTF8, 0, (wchar_t*)ptr, -1, utf8_buf, lengthof(utf8_buf), NULL, NULL); |
|
944 GlobalUnlock(cbuf); |
|
945 CloseClipboard(); |
|
946 |
|
947 if (bytec == 0) { |
|
948 DEBUG(misc, 0) ("[utf8] Error converting '%s'. Errno %d", ptr, GetLastError()); |
|
949 return false; |
|
950 } |
|
951 } else if (IsClipboardFormatAvailable(CF_TEXT)) { |
935 OpenClipboard(NULL); |
952 OpenClipboard(NULL); |
936 cbuf = GetClipboardData(CF_TEXT); |
953 cbuf = GetClipboardData(CF_TEXT); |
937 data = GlobalLock(cbuf); // clipboard data |
954 |
938 dataptr = data; |
955 ptr = GlobalLock(cbuf); |
939 |
956 ttd_strlcpy(utf8_buf, ptr, lengthof(utf8_buf)); |
940 for (; IsValidAsciiChar(*dataptr, CS_ALPHANUMERAL) && (tb->length + length) < (tb->maxlength - 1) && |
|
941 (tb->maxwidth == 0 || width + tb->width + GetCharacterWidth(FS_NORMAL, (byte)*dataptr) <= tb->maxwidth); dataptr++) { |
|
942 width += GetCharacterWidth(FS_NORMAL, (byte)*dataptr); |
|
943 length++; |
|
944 } |
|
945 |
|
946 if (length == 0) return false; |
|
947 |
|
948 memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos); |
|
949 memcpy(tb->buf + tb->caretpos, data, length); |
|
950 tb->width += width; |
|
951 tb->caretxoffs += width; |
|
952 |
|
953 tb->length += length; |
|
954 tb->caretpos += length; |
|
955 tb->buf[tb->length] = '\0'; // terminating zero |
|
956 |
|
957 GlobalUnlock(cbuf); |
957 GlobalUnlock(cbuf); |
958 CloseClipboard(); |
958 CloseClipboard(); |
959 return true; |
959 } else { |
960 } |
960 return false; |
961 return false; |
961 } |
|
962 |
|
963 width = length = 0; |
|
964 |
|
965 for (ptr = utf8_buf; (c = Utf8Consume(&ptr)) != '\0';) { |
|
966 byte charwidth; |
|
967 |
|
968 if (!IsPrintable(c)) break; |
|
969 if (tb->length + length >= tb->maxlength - 1) break; |
|
970 charwidth = GetCharacterWidth(FS_NORMAL, c); |
|
971 |
|
972 if (tb->maxwidth != 0 && width + tb->width + charwidth > tb->maxwidth) break; |
|
973 |
|
974 width += charwidth; |
|
975 length += Utf8CharLen(c); |
|
976 } |
|
977 |
|
978 if (length == 0) return false; |
|
979 |
|
980 memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos); |
|
981 memcpy(tb->buf + tb->caretpos, utf8_buf, length); |
|
982 tb->width += width; |
|
983 tb->caretxoffs += width; |
|
984 |
|
985 tb->length += length; |
|
986 tb->caretpos += length; |
|
987 tb->buf[tb->length] = '\0'; // terminating zero |
|
988 |
|
989 return true; |
962 } |
990 } |
963 |
991 |
964 |
992 |
965 void CSleep(int milliseconds) |
993 void CSleep(int milliseconds) |
966 { |
994 { |