Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/clipboar.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8457 - (hide annotations) (download) (as text)
Fri Jan 10 13:12:00 2020 UTC (4 years, 3 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 36247 byte(s)
クリップボードを閉じない場合があったので修正

- r8371
- [Ttssh2-devel 4356]
1 doda 6806 /*
2     * Copyright (C) 1994-1998 T. Teranishi
3 doda 8445 * (C) 2006-2019 TeraTerm Project
4 doda 6806 * All rights reserved.
5     *
6 doda 6841 * Redistribution and use in source and binary forms, with or without
7     * modification, are permitted provided that the following conditions
8     * are met:
9 doda 6806 *
10 doda 6841 * 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 doda 6806 *
18 doda 6841 * 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 doda 6806 */
29 maya 3227
30     /* TERATERM.EXE, Clipboard routines */
31     #include "teraterm.h"
32     #include "tttypes.h"
33     #include "vtdisp.h"
34 doda 7140 #include "vtterm.h"
35 maya 3227 #include <string.h>
36     #include <stdlib.h>
37     #include <stdio.h>
38 yutakapon 3635 #include <commctrl.h>
39 doda 8445 #define _CRTDBG_MAP_ALLOC
40     #include <crtdbg.h>
41     #include <wchar.h>
42 maya 3227
43     #include "ttwinman.h"
44     #include "ttcommon.h"
45 doda 4769 #include "ttlib.h"
46 zmatsuo 7509 #include "dlglib.h"
47 doda 8445 #include "codeconv.h"
48 maya 3227
49     #include "clipboar.h"
50     #include "tt_res.h"
51 doda 8445 #include "../ttpmacro/fileread.h"
52     #include "unicode_test.h"
53     #include "sendmem.h"
54     #include "clipboarddlg.h"
55 maya 3227
56     // for clipboard copy
57     static HGLOBAL CBCopyHandle = NULL;
58     static PCHAR CBCopyPtr = NULL;
59 maya 5071 static HGLOBAL CBCopyWideHandle = NULL;
60     static LPWSTR CBCopyWidePtr = NULL;
61 maya 3227
62     // for clipboard paste
63     static HGLOBAL CBMemHandle = NULL;
64     static PCHAR CBMemPtr = NULL;
65     static LONG CBMemPtr2 = 0;
66     static BYTE CBByte;
67     static BOOL CBRetrySend;
68     static BOOL CBRetryEcho;
69     static BOOL CBSendCR;
70 doda 3974 static BOOL CBEchoOnly;
71 doda 5259 static BOOL CBInsertDelay = FALSE;
72 maya 3227
73 doda 8445 static const wchar_t BracketStartW[] = L"\033[200~";
74     static const wchar_t BracketEndW[] = L"\033[201~";
75 doda 6456
76 doda 8445 static void CBEcho(void);
77     #if !UNICODE_INTERNAL_BUFF
78     static INT_PTR CALLBACK OnClipboardDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp);
79     #endif
80    
81     #if !UNICODE_INTERNAL_BUFF
82 maya 3227 PCHAR CBOpen(LONG MemSize)
83     {
84 maya 3393 if (MemSize==0) {
85     return (NULL);
86     }
87     if (CBCopyHandle!=NULL) {
88     return (NULL);
89     }
90     CBCopyPtr = NULL;
91     CBCopyHandle = GlobalAlloc(GMEM_MOVEABLE, MemSize);
92     if (CBCopyHandle == NULL) {
93     MessageBeep(0);
94     }
95     else {
96     CBCopyPtr = GlobalLock(CBCopyHandle);
97     if (CBCopyPtr == NULL) {
98     GlobalFree(CBCopyHandle);
99     CBCopyHandle = NULL;
100     MessageBeep(0);
101     }
102     }
103     return (CBCopyPtr);
104 maya 3227 }
105    
106     void CBClose()
107     {
108 maya 3393 BOOL Empty;
109 maya 5071 int WideCharLength;
110    
111 maya 3393 if (CBCopyHandle==NULL) {
112     return;
113     }
114 maya 3227
115 maya 5071 WideCharLength = MultiByteToWideChar(CP_ACP, 0, CBCopyPtr, -1, NULL, 0);
116     CBCopyWideHandle = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * WideCharLength);
117     if (CBCopyWideHandle) {
118     CBCopyWidePtr = (LPWSTR)GlobalLock(CBCopyWideHandle);
119     MultiByteToWideChar(CP_ACP, 0, CBCopyPtr, -1, CBCopyWidePtr, WideCharLength);
120     GlobalUnlock(CBCopyWideHandle);
121     }
122    
123 maya 3393 Empty = FALSE;
124     if (CBCopyPtr!=NULL) {
125     Empty = (CBCopyPtr[0]==0);
126     }
127 maya 3227
128 maya 3393 GlobalUnlock(CBCopyHandle);
129     CBCopyPtr = NULL;
130 maya 3227
131 maya 3393 if (OpenClipboard(HVTWin)) {
132     EmptyClipboard();
133     if (! Empty) {
134     SetClipboardData(CF_TEXT, CBCopyHandle);
135 maya 5071 if (CBCopyWidePtr) {
136     SetClipboardData(CF_UNICODETEXT, CBCopyWideHandle);
137     CBCopyWidePtr = NULL;
138     }
139 maya 3393 }
140     CloseClipboard();
141     }
142     CBCopyHandle = NULL;
143 maya 5071 CBCopyWideHandle = NULL;
144 maya 3227 }
145 doda 8445 #endif
146 maya 3227
147 doda 8445 /**
148     * ���������M
149     * ���������������g�p����������
150     * - DDE(TTL)
151     * - �u���[�h�L���X�g
152     */
153 doda 6440 void CBStartSend(PCHAR DataPtr, int DataSize, BOOL EchoOnly)
154 maya 3227 {
155 doda 6440 if (! cv.Ready) {
156     return;
157     }
158     if (TalkStatus!=IdTalkKeyb) {
159     return;
160     }
161    
162     CBEchoOnly = EchoOnly;
163    
164 doda 6452 if (CBMemHandle) {
165     GlobalFree(CBMemHandle);
166     }
167 doda 6440 CBMemHandle = NULL;
168     CBMemPtr = NULL;
169     CBMemPtr2 = 0;
170    
171     CBInsertDelay = FALSE;
172    
173     CBRetrySend = FALSE;
174     CBRetryEcho = FALSE;
175     CBSendCR = FALSE;
176    
177 doda 6535 if ((CBMemHandle = GlobalAlloc(GHND, DataSize+1)) != NULL) {
178 doda 6440 if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
179     memcpy(CBMemPtr, DataPtr, DataSize);
180 doda 6535 // WM_COPYDATA ���������������f�[�^�� NUL Terminate ���������������� NUL ���t������
181     CBMemPtr[DataSize] = 0;
182 doda 6440 GlobalUnlock(CBMemHandle);
183     CBMemPtr=NULL;
184     TalkStatus=IdTalkCB;
185     }
186     }
187     if (TalkStatus != IdTalkCB) {
188     CBEndPaste();
189     }
190     }
191    
192 doda 8445 #if !UNICODE_INTERNAL_BUFF
193 doda 6455 // �N���b�v�{�[�h�o�b�t�@������������ CR / LF ����������������
194 doda 8445 static BOOL TrimTrailingNL(BOOL AddCR, BOOL Bracketed)
195     {
196 doda 6455 PCHAR tail;
197 doda 6594 if (ts.PasteFlag & CPF_TRIM_TRAILING_NL) {
198 doda 6455 for (tail = CBMemPtr+strlen(CBMemPtr)-1; tail >= CBMemPtr; tail--) {
199     if (*tail != '\r' && *tail != '\n') {
200     break;
201     }
202     *tail = '\0';
203     }
204     }
205    
206     return TRUE;
207     }
208 doda 8445 #endif
209 doda 6455
210 doda 8445 static void TrimTrailingNLW(wchar_t *src)
211     {
212     wchar_t *tail = src + wcslen(src) - 1;
213     while(tail >= src) {
214     if (*tail != L'\r' && *tail != L'\n') {
215     break;
216     }
217     *tail = L'\0';
218     tail--;
219     }
220     }
221    
222 doda 6595 // ���s�� CR+LF �����K������
223 doda 8445 static BOOL NormalizeLineBreak(BOOL AddCR, BOOL Bracketed) {
224 doda 6595 char *p, *p2;
225     unsigned int len, need_len, alloc_len;
226     HGLOBAL TmpHandle;
227    
228     if (!(ts.PasteFlag & CPF_NORMALIZE_LINEBREAK)) {
229     return TRUE;
230     }
231    
232     p = CBMemPtr;
233    
234     // �\���t���f�[�^������(len)�A���������K�������f�[�^������(need_len)���J�E���g
235     for (len=0, need_len=0, p=CBMemPtr; *p != '\0'; p++, len++, need_len++) {
236     if (*p == CR) {
237     need_len++;
238     if (*(p+1) == LF) {
239     len++;
240     p++;
241     }
242     }
243     else if (*p == LF) {
244     need_len++;
245     }
246     }
247    
248     // ���K�������f�[�^�������������� => ���K�����K�v����
249     if (need_len == len) {
250     return TRUE;
251     }
252    
253     // AddCR / Bracketed ���������������o�b�t�@���v�Z��������
254     // ������������������������������������
255     alloc_len = need_len + 1;
256     if (AddCR) {
257     alloc_len++;
258     }
259     if (Bracketed) {
260     // ������
261     alloc_len += 12;
262     }
263    
264     // �o�b�t�@�T�C�Y�����K�������K�v�������l�����������������o�b�t�@���m��������
265     if (GlobalSize(CBMemHandle) < alloc_len) {
266     GlobalUnlock(CBMemHandle);
267     CBMemPtr = NULL;
268     if ((TmpHandle = GlobalReAlloc(CBMemHandle, alloc_len, 0)) == NULL) {
269     // �������������������s
270     CBMemPtr = GlobalLock(CBMemHandle);
271    
272     // �������������K���������\���t������������
273     return TRUE;
274     }
275     CBMemHandle = TmpHandle;
276     CBMemPtr = GlobalLock(CBMemHandle);
277     }
278    
279     p = CBMemPtr + len - 1;
280     p2 = CBMemPtr + need_len;
281     *p2-- = '\0';
282    
283     while (len > 0 && p < p2) {
284     if (*p == LF) {
285     *p2-- = *p--;
286     if (--len == 0) {
287     *p2 = CR;
288     break;
289     }
290     if (*p != CR) {
291     *p2-- = CR;
292     if (p2 <= p) {
293     break;
294     }
295     else {
296     continue;
297     }
298     }
299     }
300     else if (*p == CR) {
301     *p2-- = LF;
302     if (p == p2)
303     break;
304     }
305     *p2-- = *p--;
306     len--;
307     }
308    
309     return TRUE;
310     }
311    
312 doda 8445 static wchar_t *NormalizeLineBreakW(const wchar_t *src_)
313     {
314     const wchar_t *src = src_;
315     wchar_t *dest_top;
316     wchar_t *dest;
317     size_t len, need_len, alloc_len;
318    
319     // �\���t���f�[�^������(len)�A���������K�������f�[�^������(need_len)���J�E���g
320     for (len=0, need_len=0, src=src_; *src != '\0'; src++, len++, need_len++) {
321     if (*src == CR) {
322     need_len++;
323     if (*(src+1) == LF) {
324     len++;
325     src++;
326     }
327     }
328     else if (*src == LF) {
329     need_len++;
330     }
331     }
332    
333     // ���K�������f�[�^�������������� => ���K�����K�v����
334     if (need_len == len) {
335     dest = _wcsdup(src_);
336     return dest;
337     }
338     alloc_len = need_len + 1;
339    
340     dest_top = (wchar_t *)malloc(sizeof(wchar_t) * alloc_len);
341    
342     src = src_ + len - 1;
343     dest = dest_top + need_len;
344     *dest-- = '\0';
345     len = need_len;
346    
347     while (len > 0 && dest_top <= dest) {
348     if (*src == LF) {
349     *dest-- = *src--;
350     if (--len == 0) {
351     *dest = CR;
352     break;
353     }
354     if (*src != CR) {
355     *dest-- = CR;
356     if (dest <= dest_top) {
357     break;
358     }
359     else {
360     continue;
361     }
362     }
363     }
364     else if (*src == CR) {
365     *dest-- = LF;
366     if (src == dest)
367     break;
368     }
369     *dest-- = *src--;
370     len--;
371     }
372    
373     return dest_top;
374     }
375    
376 doda 6456 // �t�@�C�������`���������������Atext���������������������B
377 doda 8445 static BOOL search_dict(char *filename, char *text)
378 doda 6456 {
379     BOOL ret = FALSE;
380     FILE *fp = NULL;
381     char buf[256];
382 doda 8445 size_t len;
383 doda 6456
384     if (filename == NULL || filename[0] == '\0')
385     return FALSE;
386    
387     if ((fp = fopen(filename, "r")) == NULL)
388     return FALSE;
389    
390     // TODO: ���s��256byte�������������������l��
391     while (fgets(buf, sizeof(buf), fp) != NULL) {
392     len = strlen(buf);
393     if (len <= 1) {
394     continue;
395     }
396     if (buf[len - 1] == '\n') {
397     buf[len - 1] = '\0';
398     }
399     if (strstr(text, buf)) { // hit
400     ret = 1;
401     break;
402     }
403     }
404    
405     fclose(fp);
406    
407     return (ret);
408     }
409    
410 doda 8445 /**
411     * �t�@�C�������`���������������Atext���������������������B
412     * ���������� TRUE������
413     */
414     static BOOL search_dictW(char *filename, const wchar_t *text)
415     {
416     BOOL result = FALSE;
417     const wchar_t *buf_top = LoadFileWA(filename, NULL);
418     const wchar_t *buf = buf_top;
419     if (buf == NULL) {
420     return FALSE;
421     }
422    
423     for(;;) {
424     const wchar_t *line_end;
425     size_t len;
426     wchar_t *search_str;
427     if (*buf == 0) {
428     break;
429     }
430     if (*buf == '\r' || *buf == '\n') {
431     buf++;
432     continue;
433     }
434     line_end = wcspbrk(buf, L"\r\n");
435     if (line_end == NULL) {
436     // ���s������
437     len = wcslen(buf);
438     if (len == 0) {
439     // �I��
440     break;
441     }
442     } else {
443     len = line_end - buf;
444     }
445     search_str = (wchar_t *)malloc(sizeof(wchar_t) * (len+1));
446     if (search_str == NULL)
447     continue;
448     memcpy(search_str, buf, sizeof(wchar_t) * len);
449     search_str[len] = 0;
450     buf += len;
451     result = (wcsstr(text, search_str) != NULL);
452     free(search_str);
453     if (result) {
454     result = TRUE;
455     break;
456     }
457     }
458     free((void *)buf_top);
459     return result;
460     }
461    
462 doda 6456 /*
463     * �N���b�v�{�[�h�����e���m�F���A�\���t�����s�����m�F�_�C�A���O���o���B
464     *
465     * �����l:
466     * TRUE -> ���������A�\���t�������{
467     * FALSE -> �\���t�����~
468     */
469 doda 8445 #if !UNICODE_INTERNAL_BUFF
470     static BOOL CheckClipboardContent(HWND HWin, BOOL AddCR, BOOL Bracketed)
471 doda 6456 {
472 doda 8445 INT_PTR ret = IDOK;
473 doda 6456 BOOL confirm = FALSE;
474    
475 doda 6594 if ((ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE) == 0) {
476 doda 6456 return TRUE;
477     }
478    
479     /*
480     * ConfirmChangePasteCR ����������
481     * �����������������������B
482     *
483     * ChangePasteCR !ChangePasteCR
484     * AddCR !AddCR AddCR !AddCR
485     * ���s���� o o x(2) o
486     * ���s���� o(1) x x x
487     *
488     * ChangePasteCR �� AddCR �������m�F���s����(1���m�F����)���������������A
489     * !ChangePasteCR �������l�������AAddCR ���������� CR ����������������
490     * �m�F���s������������ 2 �����������m�F���s�p���������v�\�������������B
491     * 2 ����������������������?
492     */
493    
494     if (AddCR) {
495 doda 6594 if (ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE_CR) {
496 doda 6456 confirm = TRUE;
497     }
498     }
499     else {
500 doda 8445 size_t pos = strcspn(CBMemPtr, "\r\n"); // ���s����������������
501 doda 6456 if (CBMemPtr[pos] != '\0') {
502     confirm = TRUE;
503     }
504     }
505    
506     // �������T�[�`����
507     if (!confirm && search_dict(ts.ConfirmChangePasteStringFile, CBMemPtr)) {
508     confirm = TRUE;
509     }
510    
511     if (confirm) {
512 zmatsuo 7509 ret = TTDialogBox(hInst, MAKEINTRESOURCE(IDD_CLIPBOARD_DIALOG),
513 doda 8445 HWin, OnClipboardDlgProc);
514 doda 6456 /*
515     * ���O���_�C�A���O�����e���N���b�v�{�[�h�����������������������A�K�v?
516     */
517     }
518    
519     if (ret == IDOK) {
520     return TRUE;
521     }
522     else {
523     return FALSE;
524     }
525     }
526 doda 8445 #endif
527 doda 6456
528 doda 8445 /*
529     * �N���b�v�{�[�h�����e���m�F���A�\���t�����s�����m�F�_�C�A���O���o���B
530     *
531     * �����l:
532     * TRUE -> ���������A�\���t�������{
533     * FALSE -> �\���t�����~
534     */
535     static BOOL CheckClipboardContentW(HWND HWin, const wchar_t *str_w, BOOL AddCR, wchar_t **out_str_w)
536     {
537     INT_PTR ret;
538     BOOL confirm = FALSE;
539    
540     *out_str_w = NULL;
541     if ((ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE) == 0) {
542     return TRUE;
543     }
544    
545     if (AddCR) {
546     if (ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE_CR) {
547     confirm = TRUE;
548     }
549     }
550     else {
551     size_t pos = wcscspn(str_w, L"\r\n"); // ���s����������������
552     if (str_w[pos] != '\0') {
553     confirm = TRUE;
554     }
555     }
556    
557     // �������T�[�`����
558     if (!confirm && search_dictW(ts.ConfirmChangePasteStringFile, str_w)) {
559     confirm = TRUE;
560     }
561    
562     ret = IDOK;
563     if (confirm) {
564     clipboarddlgdata dlg_data;
565     dlg_data.strW_ptr = str_w;
566     dlg_data.UILanguageFile = ts.UILanguageFile;
567     ret = clipboarddlg(hInst, HWin, &dlg_data);
568     *out_str_w = dlg_data.strW_edited_ptr;
569     }
570    
571     if (ret == IDOK) {
572     return TRUE;
573     }
574     else {
575     return FALSE;
576     }
577     }
578    
579     #if !UNICODE_INTERNAL_BUFF
580 doda 6440 void CBStartPaste(HWND HWin, BOOL AddCR, BOOL Bracketed)
581     {
582 maya 3393 UINT Cf;
583 doda 6449 PCHAR TmpPtr;
584     LPWSTR TmpPtrW;
585     HGLOBAL TmpHandle;
586 doda 8445 size_t StrLen = 0, BuffLen = 0;
587 maya 3227
588 maya 3393 if (! cv.Ready) {
589     return;
590     }
591     if (TalkStatus!=IdTalkKeyb) {
592     return;
593     }
594 maya 3227
595 doda 6440 if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
596     Cf = CF_UNICODETEXT;
597 maya 3393 }
598 doda 6440 else if (IsClipboardFormatAvailable(CF_TEXT)) {
599     Cf = CF_TEXT;
600     }
601     else if (IsClipboardFormatAvailable(CF_OEMTEXT)) {
602     Cf = CF_OEMTEXT;
603     }
604     else {
605     return;
606     }
607 maya 3227
608 doda 3974 CBEchoOnly = FALSE;
609    
610 doda 6452 if (CBMemHandle) {
611     GlobalFree(CBMemHandle);
612     }
613 maya 3393 CBMemHandle = NULL;
614     CBMemPtr = NULL;
615     CBMemPtr2 = 0;
616 doda 5259
617 doda 6449 if (ts.PasteDelayPerLine > 0) {
618     CBInsertDelay = TRUE;
619     }
620     else {
621     CBInsertDelay = FALSE;
622     }
623    
624 doda 6440 CBRetrySend = FALSE;
625     CBRetryEcho = FALSE;
626     CBSendCR = FALSE;
627 maya 5071
628 doda 6440 if (OpenClipboard(HWin)) {
629 doda 6449 if ((TmpHandle = GetClipboardData(Cf)) != NULL) {
630     if (Cf == CF_UNICODETEXT) {
631     TmpPtrW = (LPWSTR)GlobalLock(TmpHandle);
632     BuffLen = WideCharToMultiByte(CP_ACP, 0, TmpPtrW, -1, 0, 0, NULL, NULL);
633     }
634     else {
635     TmpPtr = (PCHAR)GlobalLock(TmpHandle);
636     BuffLen = strlen(TmpPtr) + 1;
637     }
638 maya 5071
639 doda 6449 if (Bracketed) {
640 doda 6453 BuffLen += BracketStartLen + BracketEndLen;
641 doda 6449 }
642 maya 5071
643 doda 6449 if (AddCR) {
644     BuffLen++;
645     }
646    
647     if ((CBMemHandle = GlobalAlloc(GHND, BuffLen)) != NULL) {
648     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
649     if (Cf == CF_UNICODETEXT) {
650 doda 8445 WideCharToMultiByte(CP_ACP, 0, TmpPtrW, -1, CBMemPtr, (int)BuffLen, NULL, NULL);
651 doda 6449 }
652     else {
653 doda 6453 strncpy_s(CBMemPtr, BuffLen, TmpPtr, _TRUNCATE);
654 doda 6449 }
655    
656     TalkStatus = IdTalkCB;
657 maya 5071 }
658     }
659 doda 6449 GlobalUnlock(TmpHandle);
660 maya 3393 }
661 doda 6449 CloseClipboard();
662 maya 3393 }
663 doda 6440
664 doda 6453 // �\���t�����������������o���������� IdTalkCB ������
665    
666 maya 3393 if (TalkStatus != IdTalkCB) {
667 doda 6453 // �������s�����������������\���t�������f����
668 maya 3393 CBEndPaste();
669 doda 6453 return;
670 maya 3393 }
671 doda 6453
672     // �\���t���O���N���b�v�{�[�h�����e���m�F/���H�������������������s��
673    
674 doda 6455 if (!TrimTrailingNL(AddCR, Bracketed)) {
675     CBEndPaste();
676     return;
677     }
678    
679 doda 6595 if (!NormalizeLineBreak(AddCR, Bracketed)) {
680     CBEndPaste();
681     return;
682     }
683    
684 doda 6459 if (!CheckClipboardContent(HWin, AddCR, Bracketed)) {
685 doda 6456 CBEndPaste();
686     return;
687     }
688    
689 doda 6453 // AddCR / Bracket �p�����������������m�F�A�������������m��
690     StrLen = strlen(CBMemPtr);
691     BuffLen = StrLen + 1; // strlen + NUL
692     if (AddCR) {
693     BuffLen++;
694     }
695     if (Bracketed) {
696     BuffLen += BracketStartLen + BracketEndLen;
697     }
698    
699     if (GlobalSize(CBMemHandle) < BuffLen) {
700     GlobalUnlock(CBMemHandle);
701     CBMemPtr = NULL;
702     if ((TmpHandle = GlobalReAlloc(CBMemHandle, BuffLen, 0)) == NULL) {
703     /*
704     * �s�������m�����s�������� CR/Bracket �������\���t�����s���������A
705     * ���������\���t�����������~����(CBEndPaste()������)�������B
706     */
707     // CBEndPaste();
708     return;
709     }
710     CBMemHandle = TmpHandle;
711     CBMemPtr = GlobalLock(CBMemHandle);
712     }
713    
714     if (AddCR) {
715     CBMemPtr[StrLen++] = '\r';
716 doda 6593 CBMemPtr[StrLen++] = 0;
717 doda 6453 }
718    
719     if (Bracketed) {
720     BuffLen = GlobalSize(CBMemHandle);
721     memmove_s(CBMemPtr+BracketStartLen, BuffLen-BracketStartLen, CBMemPtr, StrLen);
722     memcpy_s(CBMemPtr, BuffLen, BracketStart, BracketStartLen);
723     strncat_s(CBMemPtr, BuffLen, BracketEnd, _TRUNCATE);
724     }
725    
726     GlobalUnlock(CBMemHandle);
727     CBMemPtr = NULL;
728 maya 3227 }
729 doda 8445 #endif
730 maya 3227
731 doda 8445 /**
732     * �N���b�v�{�[�h����wchar_t����������������
733     * �����������K�v��������wcslen()��������
734     * @param hWnd
735     * @param emtpy TRUE�������N���b�v�{�[�h����������
736     * @retval �����������|�C���^ �g�p��free()��������
737     * ����������(�������G���[��)��NULL
738     *
739     * TODO ttssh2/ttxssh/auth.c �� GetClipboardTextA() ���u������
740     */
741     static wchar_t *GetClipboardTextW(HWND hWnd, BOOL empty)
742     {
743     UINT Cf;
744     wchar_t *str_w = NULL;
745     size_t str_w_len;
746     HGLOBAL TmpHandle;
747    
748     if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
749     Cf = CF_UNICODETEXT;
750     }
751     else if (IsClipboardFormatAvailable(CF_TEXT)) {
752     Cf = CF_TEXT;
753     }
754     else if (IsClipboardFormatAvailable(CF_OEMTEXT)) {
755     Cf = CF_OEMTEXT;
756     }
757     else {
758     return NULL;
759     }
760    
761     if (!OpenClipboard(hWnd)) {
762     return NULL;
763     }
764     TmpHandle = GetClipboardData(Cf);
765     if (TmpHandle == NULL) {
766     return NULL;
767     }
768     if (Cf == CF_UNICODETEXT) {
769     const wchar_t *str_cb = (wchar_t *)GlobalLock(TmpHandle);
770     if (str_cb != NULL) {
771     size_t str_cb_len = GlobalSize(TmpHandle); // bytes
772     str_w_len = str_cb_len / sizeof(wchar_t);
773     str_w = malloc((str_w_len + 1) * sizeof(wchar_t)); // +1 for terminator
774     if (str_w != NULL) {
775     memcpy(str_w, str_cb, str_cb_len);
776     str_w[str_w_len] = L'\0';
777     }
778     }
779     }
780     else {
781     const char *str_cb = (char *)GlobalLock(TmpHandle);
782     if (str_cb != NULL) {
783     size_t str_cb_len = GlobalSize(TmpHandle);
784     str_w_len = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str_cb, (int)str_cb_len, NULL, 0);
785     str_w = malloc(sizeof(wchar_t) * (str_w_len + 1)); // +1 for terminator
786     if (str_w != NULL) {
787     str_w_len = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str_cb, (int)str_cb_len, str_w, (int)str_w_len);
788     str_w[str_w_len] = L'\0';
789     }
790     }
791     }
792     GlobalUnlock(TmpHandle);
793     if (empty) {
794     EmptyClipboard();
795     }
796     CloseClipboard();
797     return str_w;
798     }
799    
800     #if UNICODE_INTERNAL_BUFF
801     /**
802     * �N���b�v�{�[�h�p�e�L�X�g���M����
803     *
804     * @param str_w �����������|�C���^
805     * malloc()�������o�b�t�@�A���M��������������free()������
806     * @param str_len ������(wchar_t�P��)
807 zmatsuo 8456 * 0 �������� L'\0' ����
808 doda 8445 */
809     static void CBSendStart(wchar_t *str_w, size_t str_len)
810     {
811     SendMem *sm;
812     if (str_len == 0) {
813     str_len = wcslen(str_w);
814     }
815 zmatsuo 8456 sm = SendMemTextW(str_w, str_len);
816 doda 8445 if (sm == NULL)
817     return;
818     if (ts.PasteDelayPerLine != 0) {
819     SendMemInitDelay(sm, ts.PasteDelayPerLine, 0);
820     }
821     #if 0
822     SendMemInitDialog(sm, hInst, HVTWin, ts.UILanguageFile);
823     SendMemInitDialogCaption(sm, L"from clipboard");
824     SendMemInitDialogFilename(sm, L"Clipboard");
825     #endif
826     SendMemStart(sm);
827     }
828     #endif
829    
830     #if UNICODE_INTERNAL_BUFF
831     void CBStartPaste(HWND HWin, BOOL AddCR, BOOL Bracketed)
832     {
833     // unsigned int StrLen = 0;
834     wchar_t *str_w;
835     wchar_t *str_w_edited;
836    
837     if (! cv.Ready) {
838     return;
839     }
840     if (TalkStatus!=IdTalkKeyb) {
841     return;
842     }
843    
844     CBEchoOnly = FALSE;
845    
846     str_w = GetClipboardTextW(HWin, FALSE);
847     if (str_w == NULL) {
848     // �N���b�v�{�[�h����������������������������
849     CBEndPaste();
850     return;
851     }
852    
853     if (ts.PasteFlag & CPF_TRIM_TRAILING_NL) {
854     // �o�b�t�@���������s������
855     TrimTrailingNLW(str_w);
856     }
857    
858     if (!(ts.PasteFlag & CPF_NORMALIZE_LINEBREAK)) {
859     // ���s�����K��
860     wchar_t *dest = NormalizeLineBreakW(str_w);
861     free(str_w);
862     str_w = dest;
863     }
864    
865     if (!CheckClipboardContentW(HWin, str_w, AddCR, &str_w_edited)) {
866     CBEndPaste();
867     return;
868     }
869     if (str_w_edited != NULL) {
870     // �_�C�A���O�����W������
871     free(str_w);
872     str_w = str_w_edited;
873     }
874    
875     if (AddCR) {
876     size_t str_len = wcslen(str_w) + 2;
877     str_w = realloc(str_w, sizeof(wchar_t) * str_len);
878     str_w[str_len-2] = L'\r';
879     str_w[str_len-1] = 0;
880     }
881    
882     if (Bracketed) {
883     const size_t BracketStartLenW = _countof(BracketStartW) - 1;
884     const size_t BracketEndLenW = _countof(BracketEndW) - 1;
885     size_t str_len = wcslen(str_w);
886     size_t dest_len = str_len + BracketStartLenW + BracketEndLenW;
887     wchar_t *dest = malloc(sizeof(wchar_t) * (dest_len+1));
888     size_t i = 0;
889     wmemcpy(&dest[i], BracketStartW, BracketStartLenW);
890     i += BracketStartLenW;
891     wmemcpy(&dest[i], str_w, str_len);
892     i += str_len;
893     wmemcpy(&dest[i], BracketEndW, BracketEndLenW);
894     i += BracketEndLenW;
895     dest[i] = 0;
896     free(str_w);
897     str_w = dest;
898     }
899    
900     CBSendStart(str_w, 0);
901     }
902     #endif
903    
904     #if !UNICODE_INTERNAL_BUFF
905 doda 4769 void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer)
906     {
907     HANDLE tmpHandle = NULL;
908     char *tmpPtr = NULL;
909 doda 8445 size_t len, header_len, footer_len, b64_len;
910 maya 5071 UINT Cf;
911     LPWSTR tmpPtrWide = NULL;
912 doda 4769
913     if (! cv.Ready) {
914     return;
915     }
916     if (TalkStatus!=IdTalkKeyb) {
917     return;
918     }
919    
920     CBEchoOnly = FALSE;
921    
922 doda 6452 if (CBMemHandle) {
923     GlobalFree(CBMemHandle);
924     }
925 doda 4769 CBMemHandle = NULL;
926     CBMemPtr = NULL;
927     CBMemPtr2 = 0;
928    
929 doda 5259 if (ts.PasteDelayPerLine > 0) {
930     CBInsertDelay = TRUE;
931     }
932     else {
933     CBInsertDelay = FALSE;
934     }
935    
936 doda 6449 CBRetrySend = FALSE;
937     CBRetryEcho = FALSE;
938     CBSendCR = FALSE;
939    
940 maya 5071 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(HWin)) {
941     Cf = CF_UNICODETEXT;
942     if ((tmpHandle = GetClipboardData(CF_UNICODETEXT)) == NULL) {
943     CloseClipboard();
944     }
945     }
946     else if (IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(HWin)) {
947     Cf = CF_TEXT;
948 doda 4769 if ((tmpHandle = GetClipboardData(CF_TEXT)) == NULL) {
949     CloseClipboard();
950     }
951     }
952     else if (IsClipboardFormatAvailable(CF_OEMTEXT) && OpenClipboard(HWin)) {
953 maya 5071 Cf = CF_OEMTEXT;
954 doda 4769 if ((tmpHandle = GetClipboardData(CF_OEMTEXT)) == NULL) {
955     CloseClipboard();
956     }
957     }
958    
959 doda 6448 if (tmpHandle) {
960     if (Cf == CF_UNICODETEXT) {
961 maya 5071 if ((tmpPtrWide = GlobalLock(tmpHandle)) != NULL) {
962 doda 6448 len = WideCharToMultiByte(CP_ACP, 0, tmpPtrWide, -1, 0, 0, NULL, NULL);
963     if ((tmpPtr = (char *)calloc(sizeof(char), len)) != NULL) {
964     WideCharToMultiByte(CP_ACP, 0, tmpPtrWide, -1, tmpPtr, len, NULL, NULL);
965 maya 5071 }
966 doda 6448 // WideCharToMultiByte �������������������� \0 ����������
967     // \0 ���G���R�[�h������������������ 1 ������
968     len--;
969 doda 6461 GlobalUnlock(tmpHandle);
970 maya 5071 }
971     }
972 doda 6448 else {
973 maya 5071 if ((tmpPtr = GlobalLock(tmpHandle)) != NULL) {
974     len = strlen(tmpPtr);
975 doda 6448 }
976     }
977    
978     if (tmpPtr) {
979     header_len = strlen(header);
980     footer_len = strlen(footer);
981    
982     b64_len = (len + 2) / 3 * 4 + header_len + footer_len + 1;
983    
984     if ((CBMemHandle = GlobalAlloc(GHND, b64_len)) != NULL) {
985     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
986     if (header_len > 0) {
987     strncpy_s(CBMemPtr, b64_len, header, _TRUNCATE);
988 doda 4769 }
989 doda 6448 b64encode(CBMemPtr + header_len, b64_len - header_len, tmpPtr, len);
990     if (footer_len > 0) {
991     strncat_s(CBMemPtr, b64_len, footer, _TRUNCATE);
992     }
993     TalkStatus=IdTalkCB;
994     GlobalUnlock(CBMemPtr);
995     CBMemPtr = NULL;
996 doda 4769 }
997 doda 6448 }
998    
999     if (Cf == CF_UNICODETEXT) {
1000     free(tmpPtr);
1001     }
1002     else {
1003 doda 6461 GlobalUnlock(tmpHandle);
1004 doda 4769 }
1005     }
1006 doda 6448 CloseClipboard();
1007 doda 4769 }
1008    
1009     if (TalkStatus != IdTalkCB) {
1010     CBEndPaste();
1011     }
1012     }
1013 doda 8445 #endif
1014 doda 4769
1015 doda 8445 #if UNICODE_INTERNAL_BUFF
1016     void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer)
1017     {
1018     size_t mb_len, b64_len, header_len = 0, footer_len = 0;
1019     wchar_t *str_w = NULL;
1020     char *str_mb = NULL;
1021     char *str_b64 = NULL;
1022    
1023     if (! cv.Ready) {
1024     return;
1025     }
1026     if (TalkStatus!=IdTalkKeyb) {
1027     return;
1028     }
1029    
1030     CBEchoOnly = FALSE;
1031    
1032     str_w = GetClipboardTextW(HWin, FALSE);
1033     if (str_w == NULL) {
1034     // �N���b�v�{�[�h����������������������������
1035     goto error;
1036     }
1037    
1038     if (ts.Language == IdUtf8 || ts.KanjiCodeSend == IdUTF8) {
1039     str_mb = ToU8W(str_w);
1040     }
1041     else {
1042     str_mb = ToCharW(str_w);
1043     }
1044    
1045     if (str_mb == NULL) {
1046     goto error;
1047     }
1048    
1049     if (header != NULL) {
1050     header_len = strlen(header);
1051     }
1052     if (footer != NULL) {
1053     footer_len = strlen(footer);
1054     }
1055    
1056     mb_len = strlen(str_mb);
1057     b64_len = (mb_len + 2) / 3 * 4 + header_len + footer_len + 1;
1058    
1059     if ((str_b64 = malloc(b64_len)) == NULL) {;
1060     goto error;
1061     }
1062    
1063     if (header_len > 0) {
1064     strncpy_s(str_b64, b64_len, header, _TRUNCATE);
1065     }
1066    
1067     b64encode(str_b64 + header_len, b64_len - header_len, str_mb, mb_len);
1068    
1069     if (footer_len > 0) {
1070     strncat_s(str_b64, b64_len, footer, _TRUNCATE);
1071     }
1072    
1073     free(str_w);
1074     if ((str_w = ToWcharA(str_b64)) == NULL) {
1075     goto error;
1076     }
1077    
1078     free(str_mb);
1079     free(str_b64);
1080    
1081     // �\���t�����������������o����
1082     CBSendStart(str_w, 0);
1083    
1084     return;
1085    
1086     error:
1087     free(str_w);
1088     free(str_mb);
1089     free(str_b64);
1090     CBEndPaste();
1091     return;
1092     }
1093     #endif
1094    
1095 maya 3227 // �����������N���b�v�{�[�h������DDE�f�[�^���[�������������B
1096     //
1097     // CBMemHandle�n���h�����O���[�o�������������A�����������I�������������A
1098     // �����N���b�v�{�[�h������DDE�f�[�^�������������������������i�j�����������\�������j�B
1099     // �����A�f�[�^���� null-terminate �����������������O�����������������A�������f�[�^����
1100     // �����������B
1101     // (2006.11.6 yutaka)
1102     void CBSend()
1103     {
1104 maya 3393 int c;
1105     BOOL EndFlag;
1106 doda 3688 static DWORD lastcr;
1107     DWORD now;
1108 maya 3227
1109 maya 3393 if (CBMemHandle==NULL) {
1110     return;
1111     }
1112 maya 3227
1113 doda 3974 if (CBEchoOnly) {
1114     CBEcho();
1115     return;
1116     }
1117    
1118 doda 5259 if (CBInsertDelay) {
1119     now = GetTickCount();
1120     if (now - lastcr < (DWORD)ts.PasteDelayPerLine) {
1121     return;
1122     }
1123 doda 3688 }
1124    
1125 maya 3393 if (CBRetrySend) {
1126     CBRetryEcho = (ts.LocalEcho>0);
1127     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
1128     CBRetrySend = (c==0);
1129     if (CBRetrySend) {
1130     return;
1131     }
1132     }
1133 maya 3227
1134 maya 3393 if (CBRetryEcho) {
1135     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
1136     CBRetryEcho = (c==0);
1137     if (CBRetryEcho) {
1138     return;
1139     }
1140     }
1141 maya 3227
1142 maya 4009 CBMemPtr = GlobalLock(CBMemHandle);
1143     if (CBMemPtr==NULL) {
1144 maya 3393 return;
1145     }
1146 maya 3227
1147 maya 3393 do {
1148     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
1149     CBMemPtr2++;
1150     // added PasteDelayPerLine (2009.4.12 maya)
1151 doda 5259 if (CBInsertDelay) {
1152 doda 3690 lastcr = now;
1153     CBSendCR = FALSE;
1154     SetTimer(HVTWin, IdPasteDelayTimer, ts.PasteDelayPerLine, NULL);
1155     break;
1156     }
1157 maya 3393 }
1158    
1159     EndFlag = (CBMemPtr[CBMemPtr2]==0);
1160 doda 6449 if (! EndFlag) {
1161 maya 3393 CBByte = CBMemPtr[CBMemPtr2];
1162     CBMemPtr2++;
1163 maya 3227 // Decoding characters which are encoded by MACRO
1164     // to support NUL character sending
1165     //
1166     // [encoded character] --> [decoded character]
1167     // 01 01 --> 00
1168     // 01 02 --> 01
1169 maya 3393 if (CBByte==0x01) { /* 0x01 from MACRO */
1170     CBByte = CBMemPtr[CBMemPtr2];
1171     CBMemPtr2++;
1172     CBByte = CBByte - 1; // character just after 0x01
1173     }
1174     }
1175     else {
1176     CBEndPaste();
1177     return;
1178     }
1179 maya 3227
1180 maya 3393 if (! EndFlag) {
1181     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
1182     CBSendCR = (CBByte==0x0D);
1183     CBRetrySend = (c==0);
1184     if ((! CBRetrySend) &&
1185     (ts.LocalEcho>0)) {
1186     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
1187     CBRetryEcho = (c==0);
1188     }
1189     }
1190     else {
1191     c=0;
1192     }
1193     }
1194     while (c>0);
1195 maya 3227
1196 maya 4009 if (CBMemPtr!=NULL) {
1197 maya 3393 GlobalUnlock(CBMemHandle);
1198     CBMemPtr=NULL;
1199     }
1200 maya 3227 }
1201    
1202 doda 8445 static void CBEcho()
1203 doda 3974 {
1204     if (CBMemHandle==NULL) {
1205     return;
1206     }
1207    
1208     if (CBRetryEcho && CommTextEcho(&cv,(PCHAR)&CBByte,1) == 0) {
1209     return;
1210     }
1211    
1212     if ((CBMemPtr = GlobalLock(CBMemHandle)) == NULL) {
1213     return;
1214     }
1215    
1216     do {
1217     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
1218     CBMemPtr2++;
1219     }
1220    
1221     if (CBMemPtr[CBMemPtr2] == 0) {
1222     CBRetryEcho = FALSE;
1223     CBEndPaste();
1224     return;
1225     }
1226    
1227     CBByte = CBMemPtr[CBMemPtr2];
1228     CBMemPtr2++;
1229    
1230     // Decoding characters which are encoded by MACRO
1231     // to support NUL character sending
1232     //
1233     // [encoded character] --> [decoded character]
1234     // 01 01 --> 00
1235     // 01 02 --> 01
1236     if (CBByte==0x01) { /* 0x01 from MACRO */
1237     CBByte = CBMemPtr[CBMemPtr2];
1238     CBMemPtr2++;
1239     CBByte = CBByte - 1; // character just after 0x01
1240     }
1241    
1242     CBSendCR = (CBByte==0x0D);
1243    
1244     } while (CommTextEcho(&cv,(PCHAR)&CBByte,1) > 0);
1245    
1246     CBRetryEcho = TRUE;
1247    
1248     if (CBMemHandle != NULL) {
1249     GlobalUnlock(CBMemHandle);
1250     CBMemPtr=NULL;
1251     }
1252     }
1253    
1254 maya 3227 void CBEndPaste()
1255     {
1256 maya 3393 TalkStatus = IdTalkKeyb;
1257 maya 3227
1258 maya 3393 if (CBMemHandle!=NULL) {
1259     if (CBMemPtr!=NULL) {
1260     GlobalUnlock(CBMemHandle);
1261     }
1262 doda 6449 GlobalFree(CBMemHandle);
1263 maya 3393 }
1264 maya 3227
1265 maya 3393 CBMemHandle = NULL;
1266     CBMemPtr = NULL;
1267     CBMemPtr2 = 0;
1268 doda 3974 CBEchoOnly = FALSE;
1269 doda 5259 CBInsertDelay = FALSE;
1270 doda 8445 _CrtCheckMemory();
1271 maya 3227 }
1272    
1273 doda 8445 /**
1274     * �N���b�v�{�[�h���e�L�X�g���Z�b�g����
1275     * str_w �N���b�v�{�[�h���Z�b�g���������������|�C���^
1276     * NULL�������N���b�v�{�[�h����������(str_len���Q����������)
1277     * str_len ��������
1278     * 0�����������������������Z�o������
1279     */
1280     BOOL CBSetTextW(HWND hWnd, const wchar_t *str_w, size_t str_len)
1281     {
1282     HGLOBAL CBCopyWideHandle;
1283    
1284     if (str_w == NULL) {
1285     str_len = 0;
1286     } else {
1287     if (str_len == 0)
1288     str_len = wcslen(str_w);
1289     }
1290    
1291     if (!OpenClipboard(hWnd)) {
1292     return FALSE;
1293     }
1294    
1295     EmptyClipboard();
1296     if (str_len == 0) {
1297     CloseClipboard();
1298     return TRUE;
1299     }
1300    
1301     {
1302     // ���������R�s�[�A������L'\0'��������
1303     wchar_t *CBCopyWidePtr;
1304     const size_t alloc_bytes = (str_len + 1) * sizeof(wchar_t);
1305     CBCopyWideHandle = GlobalAlloc(GMEM_MOVEABLE, alloc_bytes);
1306     if (CBCopyWideHandle == NULL) {
1307 zmatsuo 8457 CloseClipboard();
1308 doda 8445 return FALSE;
1309     }
1310     CBCopyWidePtr = (wchar_t *)GlobalLock(CBCopyWideHandle);
1311     if (CBCopyWidePtr == NULL) {
1312 zmatsuo 8457 CloseClipboard();
1313 doda 8445 return FALSE;
1314     }
1315     memcpy(CBCopyWidePtr, str_w, alloc_bytes - sizeof(wchar_t));
1316     CBCopyWidePtr[str_len] = L'\0';
1317     GlobalUnlock(CBCopyWideHandle);
1318     }
1319    
1320     SetClipboardData(CF_UNICODETEXT, CBCopyWideHandle);
1321    
1322     // TODO 9x�n����������CF_TEXT���Z�b�g��������������?
1323     CloseClipboard();
1324    
1325     return TRUE;
1326     }
1327    
1328     #if 0
1329 doda 6418 BOOL CBSetClipboard(HWND owner, HGLOBAL hMem)
1330     {
1331     char *buf;
1332     int wide_len;
1333     HGLOBAL wide_hMem;
1334 zmatsuo 7509 LPWSTR wide_buf = 0;
1335 maya 3227
1336 doda 6418 if (OpenClipboard(owner) == 0)
1337     return FALSE;
1338    
1339     buf = GlobalLock(hMem);
1340    
1341     wide_len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
1342     wide_hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * wide_len);
1343     if (wide_hMem) {
1344     wide_buf = (LPWSTR)GlobalLock(wide_hMem);
1345     MultiByteToWideChar(CP_ACP, 0, buf, -1, wide_buf, wide_len);
1346     GlobalUnlock(wide_hMem);
1347     }
1348    
1349     GlobalUnlock(hMem);
1350    
1351     EmptyClipboard();
1352     SetClipboardData(CF_TEXT, hMem);
1353     if (wide_buf) {
1354     SetClipboardData(CF_UNICODETEXT, wide_hMem);
1355     }
1356     CloseClipboard();
1357    
1358     return TRUE;
1359     }
1360 doda 8445 #endif
1361 doda 6418
1362 doda 8445 #if 0
1363 doda 6418 HGLOBAL CBAllocClipboardMem(char *text)
1364     {
1365     HGLOBAL hMem;
1366     char *buf;
1367     int len;
1368    
1369     len = strlen(text);
1370    
1371     hMem = GlobalAlloc(GMEM_MOVEABLE, len+1);
1372     if (hMem) {
1373     buf = GlobalLock(hMem);
1374     strncpy_s(buf, len+1, text, _TRUNCATE);
1375     GlobalUnlock(hMem);
1376     }
1377    
1378     return hMem;
1379     }
1380 doda 8445 #endif
1381 doda 6418
1382 doda 8445 #if !UNICODE_INTERNAL_BUFF
1383     static INT_PTR CALLBACK OnClipboardDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp)
1384 maya 3227 {
1385 zmatsuo 7509 static const DlgTextInfo TextInfos[] = {
1386     { 0, "DLG_CLIPBOARD_TITLE" },
1387     { IDCANCEL, "BTN_CANCEL" },
1388     { IDOK, "BTN_OK" },
1389     };
1390 maya 3227 POINT p;
1391 zmatsuo 8408 RECT rc_dlg;
1392 maya 3902 static int ok2right, edit2ok, edit2bottom;
1393     RECT rc_edit, rc_ok, rc_cancel;
1394 yutakapon 3635 // for status bar
1395     static HWND hStatus = NULL;
1396 zmatsuo 7509 static int init_width, init_height;
1397 maya 3227
1398     switch (msg) {
1399     case WM_INITDIALOG:
1400 zmatsuo 7509 SetDlgTexts(hDlgWnd, TextInfos, _countof(TextInfos), ts.UILanguageFile);
1401 maya 3227
1402 zmatsuo 7509 SetDlgItemTextA(hDlgWnd, IDC_EDIT, CBMemPtr);
1403 maya 3227
1404 doda 6459 if (ActiveWin == IdVT) { // VT Window
1405     /*
1406     * Caret off ���� GetCaretPos() �����m���������������������A
1407     * vtdisp.c �������������������l�����v�Z����
1408     */
1409     DispConvScreenToWin(CursorX, CursorY, &p.x, &p.y);
1410     }
1411     else if (!GetCaretPos(&p)) { // Tek Window
1412     /*
1413     * Tek Window �������������l�������������|������ GetCaretPos() ���g��
1414     * GetCaretPos() ���G���[���������������O������ 0, 0 ������������
1415     */
1416     p.x = 0;
1417     p.y = 0;
1418     }
1419 maya 3227
1420 doda 6460 // x, y �������� 0 �������e�E�B���h�E���������������������������A
1421     // �������h������ x �� 1 ������
1422     if (p.x == 0 && p.y == 0) {
1423     p.x = 1;
1424     }
1425    
1426 doda 6459 ClientToScreen(GetParent(hDlgWnd), &p);
1427 maya 3227 SetWindowPos(hDlgWnd, NULL, p.x, p.y,
1428     0, 0, SWP_NOSIZE | SWP_NOZORDER);
1429 zmatsuo 8408 MoveWindowToDisplay(hDlgWnd);
1430 maya 3227
1431 maya 3641 // �_�C�A���O�������T�C�Y������
1432     GetWindowRect(hDlgWnd, &rc_dlg);
1433     init_width = rc_dlg.right - rc_dlg.left;
1434     init_height = rc_dlg.bottom - rc_dlg.top;
1435    
1436 maya 3227 // �����T�C�Y�����K�v���l���v�Z
1437     GetClientRect(hDlgWnd, &rc_dlg);
1438     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
1439     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
1440    
1441     p.x = rc_dlg.right;
1442     p.y = rc_dlg.bottom;
1443     ClientToScreen(hDlgWnd, &p);
1444     ok2right = p.x - rc_ok.left;
1445 maya 3902 edit2bottom = p.y - rc_edit.bottom;
1446 maya 3227 edit2ok = rc_ok.left - rc_edit.right;
1447    
1448     // �T�C�Y������
1449     SetWindowPos(hDlgWnd, NULL, 0, 0,
1450     ts.PasteDialogSize.cx, ts.PasteDialogSize.cy,
1451     SWP_NOZORDER | SWP_NOMOVE);
1452    
1453 yutakapon 3635 // ���T�C�Y�A�C�R�����E�����\���������������A�X�e�[�^�X�o�[���t�����B
1454     InitCommonControls();
1455     hStatus = CreateStatusWindow(
1456     WS_CHILD | WS_VISIBLE |
1457     CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hDlgWnd, 1);
1458    
1459 maya 3227 return TRUE;
1460    
1461     case WM_COMMAND:
1462     switch (LOWORD(wp)) {
1463     case IDOK:
1464     {
1465 zmatsuo 7509 unsigned len = SendMessageA(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXTLENGTH, 0, 0);
1466 maya 3227 HGLOBAL hMem;
1467 doda 6456 INT_PTR result = IDCANCEL;
1468 maya 3227
1469 doda 6456 if (CBMemHandle == NULL) {
1470     CBMemHandle = GlobalAlloc(GHND, len+1);
1471     }
1472     else if (GlobalSize(CBMemHandle) <= len) {
1473     if (CBMemPtr) {
1474     GlobalUnlock(CBMemHandle);
1475     CBMemPtr = NULL;
1476     }
1477     hMem = GlobalReAlloc(CBMemHandle, len+1, 0);
1478     if (hMem) {
1479     CBMemHandle = hMem;
1480     CBMemPtr = GlobalLock(CBMemHandle);
1481     }
1482     else {
1483     /*
1484     * ���������m�����������������������������������B
1485     *
1486     * �_�C�A���O�������������s�������������l������
1487     * �L�����Z�������������������������A����������������
1488     * �s���������v�����������A���������������������e��
1489     * �\���t���������e���B
1490     *
1491     * ���������������S�����|���A���������J�������\���t����
1492     * �s�������������������B
1493     */
1494     GlobalFree(CBMemHandle);
1495     CBMemHandle = NULL;
1496     }
1497     }
1498 doda 6417
1499 doda 6456 if (CBMemHandle) {
1500     if (CBMemPtr == NULL) {
1501     CBMemPtr = GlobalLock(CBMemHandle);
1502 maya 5071 }
1503 zmatsuo 7509 GetDlgItemTextA(hDlgWnd, IDC_EDIT, CBMemPtr, GlobalSize(CBMemHandle));
1504 doda 6456 result = IDOK;
1505 maya 3902 }
1506 maya 3227
1507 yutakapon 3635 DestroyWindow(hStatus);
1508 zmatsuo 7509 TTEndDialog(hDlgWnd, result);
1509 maya 3227 }
1510     break;
1511    
1512     case IDCANCEL:
1513 yutakapon 3635 DestroyWindow(hStatus);
1514 zmatsuo 7509 TTEndDialog(hDlgWnd, IDCANCEL);
1515 maya 3227 break;
1516    
1517     default:
1518     return FALSE;
1519     }
1520    
1521     case WM_SIZE:
1522     {
1523     // ���z�u
1524     POINT p;
1525     int dlg_w, dlg_h;
1526    
1527     GetClientRect(hDlgWnd, &rc_dlg);
1528     dlg_w = rc_dlg.right;
1529     dlg_h = rc_dlg.bottom;
1530    
1531     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
1532     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
1533     GetWindowRect(GetDlgItem(hDlgWnd, IDCANCEL), &rc_cancel);
1534    
1535     // OK
1536     p.x = rc_ok.left;
1537     p.y = rc_ok.top;
1538     ScreenToClient(hDlgWnd, &p);
1539     SetWindowPos(GetDlgItem(hDlgWnd, IDOK), 0,
1540     dlg_w - ok2right, p.y, 0, 0,
1541     SWP_NOSIZE | SWP_NOZORDER);
1542    
1543     // CANCEL
1544     p.x = rc_cancel.left;
1545     p.y = rc_cancel.top;
1546     ScreenToClient(hDlgWnd, &p);
1547     SetWindowPos(GetDlgItem(hDlgWnd, IDCANCEL), 0,
1548     dlg_w - ok2right, p.y, 0, 0,
1549     SWP_NOSIZE | SWP_NOZORDER);
1550    
1551     // EDIT
1552     p.x = rc_edit.left;
1553     p.y = rc_edit.top;
1554     ScreenToClient(hDlgWnd, &p);
1555     SetWindowPos(GetDlgItem(hDlgWnd, IDC_EDIT), 0,
1556 maya 3902 0, 0, dlg_w - p.x - edit2ok - ok2right, dlg_h - p.y - edit2bottom,
1557 maya 3227 SWP_NOMOVE | SWP_NOZORDER);
1558    
1559     // �T�C�Y������
1560     GetWindowRect(hDlgWnd, &rc_dlg);
1561     ts.PasteDialogSize.cx = rc_dlg.right - rc_dlg.left;
1562     ts.PasteDialogSize.cy = rc_dlg.bottom - rc_dlg.top;
1563 yutakapon 3635
1564     // status bar
1565     SendMessage(hStatus , msg , wp , lp);
1566 maya 3227 }
1567     return TRUE;
1568    
1569 maya 3641 case WM_GETMINMAXINFO:
1570     {
1571     // �_�C�A���O�������T�C�Y����������������������������
1572     LPMINMAXINFO lpmmi;
1573     lpmmi = (LPMINMAXINFO)lp;
1574     lpmmi->ptMinTrackSize.x = init_width;
1575     lpmmi->ptMinTrackSize.y = init_height;
1576     }
1577     return FALSE;
1578    
1579 maya 3227 default:
1580     return FALSE;
1581     }
1582     return TRUE;
1583     }
1584 doda 8445 #endif

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26