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 6461 - (hide annotations) (download) (as text)
Tue Aug 2 17:33:48 2016 UTC (7 years, 8 months ago) by doda
File MIME type: text/x-csrc
File size: 23785 byte(s)
GlobalLock() で返されたポインタに対して GlobalUnlock() を行っていたのを修正。
1 maya 3227 /* Tera Term
2     Copyright(C) 1994-1998 T. Teranishi
3     All rights reserved. */
4    
5     /* TERATERM.EXE, Clipboard routines */
6     #include "teraterm.h"
7     #include "tttypes.h"
8     #include "vtdisp.h"
9     #include <string.h>
10     #include <stdlib.h>
11     #include <stdio.h>
12 yutakapon 3635 #include <commctrl.h>
13 maya 3227
14     #include "ttwinman.h"
15     #include "ttcommon.h"
16 doda 4769 #include "ttlib.h"
17 maya 3227
18     #include "clipboar.h"
19     #include "tt_res.h"
20    
21     // for clipboard copy
22     static HGLOBAL CBCopyHandle = NULL;
23     static PCHAR CBCopyPtr = NULL;
24 maya 5071 static HGLOBAL CBCopyWideHandle = NULL;
25     static LPWSTR CBCopyWidePtr = NULL;
26 maya 3227
27     // for clipboard paste
28     static HGLOBAL CBMemHandle = NULL;
29     static PCHAR CBMemPtr = NULL;
30     static LONG CBMemPtr2 = 0;
31     static BYTE CBByte;
32     static BOOL CBRetrySend;
33     static BOOL CBRetryEcho;
34     static BOOL CBSendCR;
35 doda 3974 static BOOL CBEchoOnly;
36 doda 5259 static BOOL CBInsertDelay = FALSE;
37 maya 3227
38     static HFONT DlgClipboardFont;
39    
40 doda 6456 static LRESULT CALLBACK OnClipboardDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp);
41    
42 maya 3227 PCHAR CBOpen(LONG MemSize)
43     {
44 maya 3393 if (MemSize==0) {
45     return (NULL);
46     }
47     if (CBCopyHandle!=NULL) {
48     return (NULL);
49     }
50     CBCopyPtr = NULL;
51     CBCopyHandle = GlobalAlloc(GMEM_MOVEABLE, MemSize);
52     if (CBCopyHandle == NULL) {
53     MessageBeep(0);
54     }
55     else {
56     CBCopyPtr = GlobalLock(CBCopyHandle);
57     if (CBCopyPtr == NULL) {
58     GlobalFree(CBCopyHandle);
59     CBCopyHandle = NULL;
60     MessageBeep(0);
61     }
62     }
63     return (CBCopyPtr);
64 maya 3227 }
65    
66     void CBClose()
67     {
68 maya 3393 BOOL Empty;
69 maya 5071 int WideCharLength;
70    
71 maya 3393 if (CBCopyHandle==NULL) {
72     return;
73     }
74 maya 3227
75 maya 5071 WideCharLength = MultiByteToWideChar(CP_ACP, 0, CBCopyPtr, -1, NULL, 0);
76     CBCopyWideHandle = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * WideCharLength);
77     if (CBCopyWideHandle) {
78     CBCopyWidePtr = (LPWSTR)GlobalLock(CBCopyWideHandle);
79     MultiByteToWideChar(CP_ACP, 0, CBCopyPtr, -1, CBCopyWidePtr, WideCharLength);
80     GlobalUnlock(CBCopyWideHandle);
81     }
82    
83 maya 3393 Empty = FALSE;
84     if (CBCopyPtr!=NULL) {
85     Empty = (CBCopyPtr[0]==0);
86     }
87 maya 3227
88 maya 3393 GlobalUnlock(CBCopyHandle);
89     CBCopyPtr = NULL;
90 maya 3227
91 maya 3393 if (OpenClipboard(HVTWin)) {
92     EmptyClipboard();
93     if (! Empty) {
94     SetClipboardData(CF_TEXT, CBCopyHandle);
95 maya 5071 if (CBCopyWidePtr) {
96     SetClipboardData(CF_UNICODETEXT, CBCopyWideHandle);
97     CBCopyWidePtr = NULL;
98     }
99 maya 3393 }
100     CloseClipboard();
101     }
102     CBCopyHandle = NULL;
103 maya 5071 CBCopyWideHandle = NULL;
104 maya 3227 }
105    
106 doda 6440 void CBStartSend(PCHAR DataPtr, int DataSize, BOOL EchoOnly)
107 maya 3227 {
108 doda 6440 if (! cv.Ready) {
109     return;
110     }
111     if (TalkStatus!=IdTalkKeyb) {
112     return;
113     }
114    
115     CBEchoOnly = EchoOnly;
116    
117 doda 6452 if (CBMemHandle) {
118     GlobalFree(CBMemHandle);
119     }
120 doda 6440 CBMemHandle = NULL;
121     CBMemPtr = NULL;
122     CBMemPtr2 = 0;
123    
124     CBInsertDelay = FALSE;
125    
126     CBRetrySend = FALSE;
127     CBRetryEcho = FALSE;
128     CBSendCR = FALSE;
129    
130     if ((CBMemHandle = GlobalAlloc(GHND, DataSize)) != NULL) {
131     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
132     memcpy(CBMemPtr, DataPtr, DataSize);
133     GlobalUnlock(CBMemHandle);
134     CBMemPtr=NULL;
135     TalkStatus=IdTalkCB;
136     }
137     }
138    
139     if (TalkStatus != IdTalkCB) {
140     CBEndPaste();
141     }
142     }
143    
144 doda 6455 // �N���b�v�{�[�h�o�b�t�@������������ CR / LF ����������������
145     BOOL TrimTrailingNL(BOOL AddCR, BOOL Bracketed) {
146     PCHAR tail;
147     if (ts.TrimTrailingNLonPaste) {
148     for (tail = CBMemPtr+strlen(CBMemPtr)-1; tail >= CBMemPtr; tail--) {
149     if (*tail != '\r' && *tail != '\n') {
150     break;
151     }
152     *tail = '\0';
153     }
154     }
155    
156     return TRUE;
157     }
158    
159 doda 6456 // �t�@�C�������`���������������Atext���������������������B
160     BOOL search_dict(char *filename, char *text)
161     {
162     BOOL ret = FALSE;
163     FILE *fp = NULL;
164     char buf[256];
165     int len;
166    
167     if (filename == NULL || filename[0] == '\0')
168     return FALSE;
169    
170     if ((fp = fopen(filename, "r")) == NULL)
171     return FALSE;
172    
173     // TODO: ���s��256byte�������������������l��
174     while (fgets(buf, sizeof(buf), fp) != NULL) {
175     len = strlen(buf);
176     if (len <= 1) {
177     continue;
178     }
179     if (buf[len - 1] == '\n') {
180     buf[len - 1] = '\0';
181     }
182     if (strstr(text, buf)) { // hit
183     ret = 1;
184     break;
185     }
186     }
187    
188     fclose(fp);
189    
190     return (ret);
191     }
192    
193     /*
194     * �N���b�v�{�[�h�����e���m�F���A�\���t�����s�����m�F�_�C�A���O���o���B
195     *
196     * �����l:
197     * TRUE -> ���������A�\���t�������{
198     * FALSE -> �\���t�����~
199     */
200 doda 6459 BOOL CheckClipboardContent(HWND HWin, BOOL AddCR, BOOL Bracketed)
201 doda 6456 {
202     int pos;
203     int ret = IDOK;
204     BOOL confirm = FALSE;
205    
206     if (!ts.ConfirmChangePaste) {
207     return TRUE;
208     }
209    
210     /*
211     * ConfirmChangePasteCR ����������
212     * �����������������������B
213     *
214     * ChangePasteCR !ChangePasteCR
215     * AddCR !AddCR AddCR !AddCR
216     * ���s���� o o x(2) o
217     * ���s���� o(1) x x x
218     *
219     * ChangePasteCR �� AddCR �������m�F���s����(1���m�F����)���������������A
220     * !ChangePasteCR �������l�������AAddCR ���������� CR ����������������
221     * �m�F���s������������ 2 �����������m�F���s�p���������v�\�������������B
222     * 2 ����������������������?
223     */
224    
225     if (AddCR) {
226     if (ts.ConfirmChangePasteCR) {
227     confirm = TRUE;
228     }
229     }
230     else {
231     pos = strcspn(CBMemPtr, "\r\n"); // ���s����������������
232     if (CBMemPtr[pos] != '\0') {
233     confirm = TRUE;
234     }
235     }
236    
237     // �������T�[�`����
238     if (!confirm && search_dict(ts.ConfirmChangePasteStringFile, CBMemPtr)) {
239     confirm = TRUE;
240     }
241    
242     if (confirm) {
243     ret = DialogBox(hInst, MAKEINTRESOURCE(IDD_CLIPBOARD_DIALOG),
244 doda 6459 HWin, (DLGPROC)OnClipboardDlgProc);
245 doda 6456 /*
246     * ���O���_�C�A���O�����e���N���b�v�{�[�h�����������������������A�K�v?
247     */
248     }
249    
250     if (ret == IDOK) {
251     return TRUE;
252     }
253     else {
254     return FALSE;
255     }
256     }
257    
258 doda 6453 #define BracketStartLen (sizeof(BracketStart)-1)
259     #define BracketEndLen (sizeof(BracketEnd)-1)
260 doda 6440 void CBStartPaste(HWND HWin, BOOL AddCR, BOOL Bracketed)
261     {
262 doda 6449 static char BracketStart[] = "\033[200~";
263     static char BracketEnd[] = "\033[201~";
264 maya 3393 UINT Cf;
265 doda 6449 PCHAR TmpPtr;
266     LPWSTR TmpPtrW;
267     HGLOBAL TmpHandle;
268 doda 6453 unsigned int StrLen = 0, BuffLen = 0;
269 maya 3227
270 maya 3393 if (! cv.Ready) {
271     return;
272     }
273     if (TalkStatus!=IdTalkKeyb) {
274     return;
275     }
276 maya 3227
277 doda 6440 if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
278     Cf = CF_UNICODETEXT;
279 maya 3393 }
280 doda 6440 else if (IsClipboardFormatAvailable(CF_TEXT)) {
281     Cf = CF_TEXT;
282     }
283     else if (IsClipboardFormatAvailable(CF_OEMTEXT)) {
284     Cf = CF_OEMTEXT;
285     }
286     else {
287     return;
288     }
289 maya 3227
290 doda 3974 CBEchoOnly = FALSE;
291    
292 doda 6452 if (CBMemHandle) {
293     GlobalFree(CBMemHandle);
294     }
295 maya 3393 CBMemHandle = NULL;
296     CBMemPtr = NULL;
297     CBMemPtr2 = 0;
298 doda 5259
299 doda 6449 if (ts.PasteDelayPerLine > 0) {
300     CBInsertDelay = TRUE;
301     }
302     else {
303     CBInsertDelay = FALSE;
304     }
305    
306 doda 6440 CBRetrySend = FALSE;
307     CBRetryEcho = FALSE;
308     CBSendCR = FALSE;
309 maya 5071
310 doda 6440 if (OpenClipboard(HWin)) {
311 doda 6449 if ((TmpHandle = GetClipboardData(Cf)) != NULL) {
312     if (Cf == CF_UNICODETEXT) {
313     TmpPtrW = (LPWSTR)GlobalLock(TmpHandle);
314     BuffLen = WideCharToMultiByte(CP_ACP, 0, TmpPtrW, -1, 0, 0, NULL, NULL);
315     }
316     else {
317     TmpPtr = (PCHAR)GlobalLock(TmpHandle);
318     BuffLen = strlen(TmpPtr) + 1;
319     }
320 maya 5071
321 doda 6449 if (Bracketed) {
322 doda 6453 BuffLen += BracketStartLen + BracketEndLen;
323 doda 6449 }
324 maya 5071
325 doda 6449 if (AddCR) {
326     BuffLen++;
327     }
328    
329     if ((CBMemHandle = GlobalAlloc(GHND, BuffLen)) != NULL) {
330     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
331     if (Cf == CF_UNICODETEXT) {
332 doda 6453 WideCharToMultiByte(CP_ACP, 0, TmpPtrW, -1, CBMemPtr, BuffLen, NULL, NULL);
333 doda 6449 }
334     else {
335 doda 6453 strncpy_s(CBMemPtr, BuffLen, TmpPtr, _TRUNCATE);
336 doda 6449 }
337    
338     TalkStatus = IdTalkCB;
339 maya 5071 }
340     }
341 doda 6449 GlobalUnlock(TmpHandle);
342 maya 3393 }
343 doda 6449 CloseClipboard();
344 maya 3393 }
345 doda 6440
346 doda 6453 // �\���t�����������������o���������� IdTalkCB ������
347    
348 maya 3393 if (TalkStatus != IdTalkCB) {
349 doda 6453 // �������s�����������������\���t�������f����
350 maya 3393 CBEndPaste();
351 doda 6453 return;
352 maya 3393 }
353 doda 6453
354     // �\���t���O���N���b�v�{�[�h�����e���m�F/���H�������������������s��
355    
356 doda 6455 if (!TrimTrailingNL(AddCR, Bracketed)) {
357     CBEndPaste();
358     return;
359     }
360    
361 doda 6459 if (!CheckClipboardContent(HWin, AddCR, Bracketed)) {
362 doda 6456 CBEndPaste();
363     return;
364     }
365    
366 doda 6453 // AddCR / Bracket �p�����������������m�F�A�������������m��
367     StrLen = strlen(CBMemPtr);
368     BuffLen = StrLen + 1; // strlen + NUL
369     if (AddCR) {
370     BuffLen++;
371     }
372     if (Bracketed) {
373     BuffLen += BracketStartLen + BracketEndLen;
374     }
375    
376     if (GlobalSize(CBMemHandle) < BuffLen) {
377     GlobalUnlock(CBMemHandle);
378     CBMemPtr = NULL;
379     if ((TmpHandle = GlobalReAlloc(CBMemHandle, BuffLen, 0)) == NULL) {
380     /*
381     * �s�������m�����s�������� CR/Bracket �������\���t�����s���������A
382     * ���������\���t�����������~����(CBEndPaste()������)�������B
383     */
384     // CBEndPaste();
385     return;
386     }
387     CBMemHandle = TmpHandle;
388     CBMemPtr = GlobalLock(CBMemHandle);
389     }
390    
391     if (AddCR) {
392     CBMemPtr[StrLen++] = '\r';
393     }
394    
395     if (Bracketed) {
396     BuffLen = GlobalSize(CBMemHandle);
397     memmove_s(CBMemPtr+BracketStartLen, BuffLen-BracketStartLen, CBMemPtr, StrLen);
398     memcpy_s(CBMemPtr, BuffLen, BracketStart, BracketStartLen);
399     strncat_s(CBMemPtr, BuffLen, BracketEnd, _TRUNCATE);
400     }
401    
402     GlobalUnlock(CBMemHandle);
403     CBMemPtr = NULL;
404 maya 3227 }
405    
406 doda 4769 void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer)
407     {
408     HANDLE tmpHandle = NULL;
409     char *tmpPtr = NULL;
410 doda 6448 int len, header_len, footer_len, b64_len;
411 maya 5071 UINT Cf;
412     LPWSTR tmpPtrWide = NULL;
413 doda 4769
414     if (! cv.Ready) {
415     return;
416     }
417     if (TalkStatus!=IdTalkKeyb) {
418     return;
419     }
420    
421     CBEchoOnly = FALSE;
422    
423 doda 6452 if (CBMemHandle) {
424     GlobalFree(CBMemHandle);
425     }
426 doda 4769 CBMemHandle = NULL;
427     CBMemPtr = NULL;
428     CBMemPtr2 = 0;
429    
430 doda 5259 if (ts.PasteDelayPerLine > 0) {
431     CBInsertDelay = TRUE;
432     }
433     else {
434     CBInsertDelay = FALSE;
435     }
436    
437 doda 6449 CBRetrySend = FALSE;
438     CBRetryEcho = FALSE;
439     CBSendCR = FALSE;
440    
441 maya 5071 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(HWin)) {
442     Cf = CF_UNICODETEXT;
443     if ((tmpHandle = GetClipboardData(CF_UNICODETEXT)) == NULL) {
444     CloseClipboard();
445     }
446     }
447     else if (IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(HWin)) {
448     Cf = CF_TEXT;
449 doda 4769 if ((tmpHandle = GetClipboardData(CF_TEXT)) == NULL) {
450     CloseClipboard();
451     }
452     }
453     else if (IsClipboardFormatAvailable(CF_OEMTEXT) && OpenClipboard(HWin)) {
454 maya 5071 Cf = CF_OEMTEXT;
455 doda 4769 if ((tmpHandle = GetClipboardData(CF_OEMTEXT)) == NULL) {
456     CloseClipboard();
457     }
458     }
459    
460 doda 6448 if (tmpHandle) {
461     if (Cf == CF_UNICODETEXT) {
462 maya 5071 if ((tmpPtrWide = GlobalLock(tmpHandle)) != NULL) {
463 doda 6448 len = WideCharToMultiByte(CP_ACP, 0, tmpPtrWide, -1, 0, 0, NULL, NULL);
464     if ((tmpPtr = (char *)calloc(sizeof(char), len)) != NULL) {
465     WideCharToMultiByte(CP_ACP, 0, tmpPtrWide, -1, tmpPtr, len, NULL, NULL);
466 maya 5071 }
467 doda 6448 // WideCharToMultiByte �������������������� \0 ����������
468     // \0 ���G���R�[�h������������������ 1 ������
469     len--;
470 doda 6461 GlobalUnlock(tmpHandle);
471 maya 5071 }
472     }
473 doda 6448 else {
474 maya 5071 if ((tmpPtr = GlobalLock(tmpHandle)) != NULL) {
475     len = strlen(tmpPtr);
476 doda 6448 }
477     }
478    
479     if (tmpPtr) {
480     header_len = strlen(header);
481     footer_len = strlen(footer);
482    
483     b64_len = (len + 2) / 3 * 4 + header_len + footer_len + 1;
484    
485     if ((CBMemHandle = GlobalAlloc(GHND, b64_len)) != NULL) {
486     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
487     if (header_len > 0) {
488     strncpy_s(CBMemPtr, b64_len, header, _TRUNCATE);
489 doda 4769 }
490 doda 6448 b64encode(CBMemPtr + header_len, b64_len - header_len, tmpPtr, len);
491     if (footer_len > 0) {
492     strncat_s(CBMemPtr, b64_len, footer, _TRUNCATE);
493     }
494     TalkStatus=IdTalkCB;
495     GlobalUnlock(CBMemPtr);
496     CBMemPtr = NULL;
497 doda 4769 }
498 doda 6448 }
499    
500     if (Cf == CF_UNICODETEXT) {
501     free(tmpPtr);
502     }
503     else {
504 doda 6461 GlobalUnlock(tmpHandle);
505 doda 4769 }
506     }
507 doda 6448 CloseClipboard();
508 doda 4769 }
509    
510     if (TalkStatus != IdTalkCB) {
511     CBEndPaste();
512     }
513     }
514    
515 maya 3227 // �����������N���b�v�{�[�h������DDE�f�[�^���[�������������B
516     //
517     // CBMemHandle�n���h�����O���[�o�������������A�����������I�������������A
518     // �����N���b�v�{�[�h������DDE�f�[�^�������������������������i�j�����������\�������j�B
519     // �����A�f�[�^���� null-terminate �����������������O�����������������A�������f�[�^����
520     // �����������B
521     // (2006.11.6 yutaka)
522     void CBSend()
523     {
524 maya 3393 int c;
525     BOOL EndFlag;
526 doda 3688 static DWORD lastcr;
527     DWORD now;
528 maya 3227
529 maya 3393 if (CBMemHandle==NULL) {
530     return;
531     }
532 maya 3227
533 doda 3974 if (CBEchoOnly) {
534     CBEcho();
535     return;
536     }
537    
538 doda 5259 if (CBInsertDelay) {
539     now = GetTickCount();
540     if (now - lastcr < (DWORD)ts.PasteDelayPerLine) {
541     return;
542     }
543 doda 3688 }
544    
545 maya 3393 if (CBRetrySend) {
546     CBRetryEcho = (ts.LocalEcho>0);
547     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
548     CBRetrySend = (c==0);
549     if (CBRetrySend) {
550     return;
551     }
552     }
553 maya 3227
554 maya 3393 if (CBRetryEcho) {
555     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
556     CBRetryEcho = (c==0);
557     if (CBRetryEcho) {
558     return;
559     }
560     }
561 maya 3227
562 maya 4009 CBMemPtr = GlobalLock(CBMemHandle);
563     if (CBMemPtr==NULL) {
564 maya 3393 return;
565     }
566 maya 3227
567 maya 3393 do {
568     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
569     CBMemPtr2++;
570     // added PasteDelayPerLine (2009.4.12 maya)
571 doda 5259 if (CBInsertDelay) {
572 doda 3690 lastcr = now;
573     CBSendCR = FALSE;
574     SetTimer(HVTWin, IdPasteDelayTimer, ts.PasteDelayPerLine, NULL);
575     break;
576     }
577 maya 3393 }
578    
579     EndFlag = (CBMemPtr[CBMemPtr2]==0);
580 doda 6449 if (! EndFlag) {
581 maya 3393 CBByte = CBMemPtr[CBMemPtr2];
582     CBMemPtr2++;
583 maya 3227 // Decoding characters which are encoded by MACRO
584     // to support NUL character sending
585     //
586     // [encoded character] --> [decoded character]
587     // 01 01 --> 00
588     // 01 02 --> 01
589 maya 3393 if (CBByte==0x01) { /* 0x01 from MACRO */
590     CBByte = CBMemPtr[CBMemPtr2];
591     CBMemPtr2++;
592     CBByte = CBByte - 1; // character just after 0x01
593     }
594     }
595     else {
596     CBEndPaste();
597     return;
598     }
599 maya 3227
600 maya 3393 if (! EndFlag) {
601     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
602     CBSendCR = (CBByte==0x0D);
603     CBRetrySend = (c==0);
604     if ((! CBRetrySend) &&
605     (ts.LocalEcho>0)) {
606     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
607     CBRetryEcho = (c==0);
608     }
609     }
610     else {
611     c=0;
612     }
613     }
614     while (c>0);
615 maya 3227
616 maya 4009 if (CBMemPtr!=NULL) {
617 maya 3393 GlobalUnlock(CBMemHandle);
618     CBMemPtr=NULL;
619     }
620 maya 3227 }
621    
622 doda 3974 void CBEcho()
623     {
624     if (CBMemHandle==NULL) {
625     return;
626     }
627    
628     if (CBRetryEcho && CommTextEcho(&cv,(PCHAR)&CBByte,1) == 0) {
629     return;
630     }
631    
632     if ((CBMemPtr = GlobalLock(CBMemHandle)) == NULL) {
633     return;
634     }
635    
636     do {
637     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
638     CBMemPtr2++;
639     }
640    
641     if (CBMemPtr[CBMemPtr2] == 0) {
642     CBRetryEcho = FALSE;
643     CBEndPaste();
644     return;
645     }
646    
647     CBByte = CBMemPtr[CBMemPtr2];
648     CBMemPtr2++;
649    
650     // Decoding characters which are encoded by MACRO
651     // to support NUL character sending
652     //
653     // [encoded character] --> [decoded character]
654     // 01 01 --> 00
655     // 01 02 --> 01
656     if (CBByte==0x01) { /* 0x01 from MACRO */
657     CBByte = CBMemPtr[CBMemPtr2];
658     CBMemPtr2++;
659     CBByte = CBByte - 1; // character just after 0x01
660     }
661    
662     CBSendCR = (CBByte==0x0D);
663    
664     } while (CommTextEcho(&cv,(PCHAR)&CBByte,1) > 0);
665    
666     CBRetryEcho = TRUE;
667    
668     if (CBMemHandle != NULL) {
669     GlobalUnlock(CBMemHandle);
670     CBMemPtr=NULL;
671     }
672     }
673    
674 maya 3227 void CBEndPaste()
675     {
676 maya 3393 TalkStatus = IdTalkKeyb;
677 maya 3227
678 maya 3393 if (CBMemHandle!=NULL) {
679     if (CBMemPtr!=NULL) {
680     GlobalUnlock(CBMemHandle);
681     }
682 doda 6449 GlobalFree(CBMemHandle);
683 maya 3393 }
684 maya 3227
685 maya 3393 CBMemHandle = NULL;
686     CBMemPtr = NULL;
687     CBMemPtr2 = 0;
688 doda 3974 CBEchoOnly = FALSE;
689 doda 5259 CBInsertDelay = FALSE;
690 maya 3227 }
691    
692 doda 6418 BOOL CBSetClipboard(HWND owner, HGLOBAL hMem)
693     {
694     char *buf;
695     int wide_len;
696     HGLOBAL wide_hMem;
697     LPWSTR wide_buf;
698 maya 3227
699 doda 6418 if (OpenClipboard(owner) == 0)
700     return FALSE;
701    
702     buf = GlobalLock(hMem);
703    
704     wide_len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
705     wide_hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * wide_len);
706     if (wide_hMem) {
707     wide_buf = (LPWSTR)GlobalLock(wide_hMem);
708     MultiByteToWideChar(CP_ACP, 0, buf, -1, wide_buf, wide_len);
709     GlobalUnlock(wide_hMem);
710     }
711    
712     GlobalUnlock(hMem);
713    
714     EmptyClipboard();
715     SetClipboardData(CF_TEXT, hMem);
716     if (wide_buf) {
717     SetClipboardData(CF_UNICODETEXT, wide_hMem);
718     }
719     CloseClipboard();
720    
721     return TRUE;
722     }
723    
724     HGLOBAL CBAllocClipboardMem(char *text)
725     {
726     HGLOBAL hMem;
727     char *buf;
728     int len;
729    
730     len = strlen(text);
731    
732     hMem = GlobalAlloc(GMEM_MOVEABLE, len+1);
733     if (hMem) {
734     buf = GlobalLock(hMem);
735     strncpy_s(buf, len+1, text, _TRUNCATE);
736     GlobalUnlock(hMem);
737     }
738    
739     return hMem;
740     }
741    
742 maya 3227 static LRESULT CALLBACK OnClipboardDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp)
743     {
744     LOGFONT logfont;
745     HFONT font;
746     char uimsg[MAX_UIMSG];
747     POINT p;
748     RECT rc_dsk, rc_dlg;
749     int dlg_height, dlg_width;
750 maya 3902 static int ok2right, edit2ok, edit2bottom;
751     RECT rc_edit, rc_ok, rc_cancel;
752 yutakapon 3635 // for status bar
753     static HWND hStatus = NULL;
754 maya 3641 static init_width, init_height;
755 maya 3227
756     switch (msg) {
757     case WM_INITDIALOG:
758     font = (HFONT)SendMessage(hDlgWnd, WM_GETFONT, 0, 0);
759     GetObject(font, sizeof(LOGFONT), &logfont);
760     if (get_lang_font("DLG_TAHOMA_FONT", hDlgWnd, &logfont, &DlgClipboardFont, ts.UILanguageFile)) {
761     SendDlgItemMessage(hDlgWnd, IDC_EDIT, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
762     SendDlgItemMessage(hDlgWnd, IDOK, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
763     SendDlgItemMessage(hDlgWnd, IDCANCEL, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
764     }
765     else {
766     DlgClipboardFont = NULL;
767     }
768    
769     GetWindowText(hDlgWnd, uimsg, sizeof(uimsg));
770     get_lang_msg("DLG_CLIPBOARD_TITLE", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
771     SetWindowText(hDlgWnd, ts.UIMsg);
772     GetDlgItemText(hDlgWnd, IDCANCEL, uimsg, sizeof(uimsg));
773     get_lang_msg("BTN_CANCEL", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
774     SetDlgItemText(hDlgWnd, IDCANCEL, ts.UIMsg);
775     GetDlgItemText(hDlgWnd, IDOK, uimsg, sizeof(uimsg));
776     get_lang_msg("BTN_OK", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
777     SetDlgItemText(hDlgWnd, IDOK, ts.UIMsg);
778    
779 doda 6456 SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_SETTEXT, 0, (LPARAM)CBMemPtr);
780 maya 3227
781 doda 6459 if (ActiveWin == IdVT) { // VT Window
782     /*
783     * Caret off ���� GetCaretPos() �����m���������������������A
784     * vtdisp.c �������������������l�����v�Z����
785     */
786     DispConvScreenToWin(CursorX, CursorY, &p.x, &p.y);
787     }
788     else if (!GetCaretPos(&p)) { // Tek Window
789     /*
790     * Tek Window �������������l�������������|������ GetCaretPos() ���g��
791     * GetCaretPos() ���G���[���������������O������ 0, 0 ������������
792     */
793     p.x = 0;
794     p.y = 0;
795     }
796 maya 3227
797 doda 6460 // x, y �������� 0 �������e�E�B���h�E���������������������������A
798     // �������h������ x �� 1 ������
799     if (p.x == 0 && p.y == 0) {
800     p.x = 1;
801     }
802    
803 doda 6459 ClientToScreen(GetParent(hDlgWnd), &p);
804    
805 maya 3227 // �L�����b�g���������������o���������������\���t����������
806     // �m�F�E�C���h�E�����������������\���������������������B
807     // �E�C���h�E���������o������������������ (2008.4.24 maya)
808 yutakapon 6286 if (!HasMultiMonitorSupport()) {
809 maya 3509 // NT4.0, 95 ���}���`���j�^API��������
810 maya 3227 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc_dsk, 0);
811     }
812     else {
813     HMONITOR hm;
814     POINT pt;
815     MONITORINFO mi;
816    
817     pt.x = p.x;
818     pt.y = p.y;
819     hm = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
820    
821     mi.cbSize = sizeof(MONITORINFO);
822     GetMonitorInfo(hm, &mi);
823     rc_dsk = mi.rcWork;
824     }
825     GetWindowRect(hDlgWnd, &rc_dlg);
826     dlg_height = rc_dlg.bottom-rc_dlg.top;
827     dlg_width = rc_dlg.right-rc_dlg.left;
828     if (p.y < rc_dsk.top) {
829     p.y = rc_dsk.top;
830     }
831     else if (p.y + dlg_height > rc_dsk.bottom) {
832     p.y = rc_dsk.bottom - dlg_height;
833     }
834     if (p.x < rc_dsk.left) {
835     p.x = rc_dsk.left;
836     }
837     else if (p.x + dlg_width > rc_dsk.right) {
838     p.x = rc_dsk.right - dlg_width;
839     }
840    
841     SetWindowPos(hDlgWnd, NULL, p.x, p.y,
842     0, 0, SWP_NOSIZE | SWP_NOZORDER);
843    
844 maya 3641 // �_�C�A���O�������T�C�Y������
845     GetWindowRect(hDlgWnd, &rc_dlg);
846     init_width = rc_dlg.right - rc_dlg.left;
847     init_height = rc_dlg.bottom - rc_dlg.top;
848    
849 maya 3227 // �����T�C�Y�����K�v���l���v�Z
850     GetClientRect(hDlgWnd, &rc_dlg);
851     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
852     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
853    
854     p.x = rc_dlg.right;
855     p.y = rc_dlg.bottom;
856     ClientToScreen(hDlgWnd, &p);
857     ok2right = p.x - rc_ok.left;
858 maya 3902 edit2bottom = p.y - rc_edit.bottom;
859 maya 3227 edit2ok = rc_ok.left - rc_edit.right;
860    
861     // �T�C�Y������
862     SetWindowPos(hDlgWnd, NULL, 0, 0,
863     ts.PasteDialogSize.cx, ts.PasteDialogSize.cy,
864     SWP_NOZORDER | SWP_NOMOVE);
865    
866 yutakapon 3635 // ���T�C�Y�A�C�R�����E�����\���������������A�X�e�[�^�X�o�[���t�����B
867     InitCommonControls();
868     hStatus = CreateStatusWindow(
869     WS_CHILD | WS_VISIBLE |
870     CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hDlgWnd, 1);
871    
872 maya 3227 return TRUE;
873    
874     case WM_COMMAND:
875     switch (LOWORD(wp)) {
876     case IDOK:
877     {
878 doda 6456 unsigned len = SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXTLENGTH, 0, 0);
879 maya 3227 HGLOBAL hMem;
880 doda 6456 INT_PTR result = IDCANCEL;
881 maya 3227
882 doda 6456 if (CBMemHandle == NULL) {
883     CBMemHandle = GlobalAlloc(GHND, len+1);
884     }
885     else if (GlobalSize(CBMemHandle) <= len) {
886     if (CBMemPtr) {
887     GlobalUnlock(CBMemHandle);
888     CBMemPtr = NULL;
889     }
890     hMem = GlobalReAlloc(CBMemHandle, len+1, 0);
891     if (hMem) {
892     CBMemHandle = hMem;
893     CBMemPtr = GlobalLock(CBMemHandle);
894     }
895     else {
896     /*
897     * ���������m�����������������������������������B
898     *
899     * �_�C�A���O�������������s�������������l������
900     * �L�����Z�������������������������A����������������
901     * �s���������v�����������A���������������������e��
902     * �\���t���������e���B
903     *
904     * ���������������S�����|���A���������J�������\���t����
905     * �s�������������������B
906     */
907     GlobalFree(CBMemHandle);
908     CBMemHandle = NULL;
909     }
910     }
911 doda 6417
912 doda 6456 if (CBMemHandle) {
913     if (CBMemPtr == NULL) {
914     CBMemPtr = GlobalLock(CBMemHandle);
915 maya 5071 }
916 doda 6456 SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXT, GlobalSize(CBMemHandle), (LPARAM)CBMemPtr);
917     result = IDOK;
918 maya 3902 }
919 maya 3227
920     if (DlgClipboardFont != NULL) {
921     DeleteObject(DlgClipboardFont);
922     }
923    
924 yutakapon 3635 DestroyWindow(hStatus);
925 doda 6456 EndDialog(hDlgWnd, result);
926 maya 3227 }
927     break;
928    
929     case IDCANCEL:
930     if (DlgClipboardFont != NULL) {
931     DeleteObject(DlgClipboardFont);
932     }
933    
934 yutakapon 3635 DestroyWindow(hStatus);
935 maya 3227 EndDialog(hDlgWnd, IDCANCEL);
936     break;
937    
938     default:
939     return FALSE;
940     }
941    
942     case WM_SIZE:
943     {
944     // ���z�u
945     POINT p;
946     int dlg_w, dlg_h;
947    
948     GetClientRect(hDlgWnd, &rc_dlg);
949     dlg_w = rc_dlg.right;
950     dlg_h = rc_dlg.bottom;
951    
952     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
953     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
954     GetWindowRect(GetDlgItem(hDlgWnd, IDCANCEL), &rc_cancel);
955    
956     // OK
957     p.x = rc_ok.left;
958     p.y = rc_ok.top;
959     ScreenToClient(hDlgWnd, &p);
960     SetWindowPos(GetDlgItem(hDlgWnd, IDOK), 0,
961     dlg_w - ok2right, p.y, 0, 0,
962     SWP_NOSIZE | SWP_NOZORDER);
963    
964     // CANCEL
965     p.x = rc_cancel.left;
966     p.y = rc_cancel.top;
967     ScreenToClient(hDlgWnd, &p);
968     SetWindowPos(GetDlgItem(hDlgWnd, IDCANCEL), 0,
969     dlg_w - ok2right, p.y, 0, 0,
970     SWP_NOSIZE | SWP_NOZORDER);
971    
972     // EDIT
973     p.x = rc_edit.left;
974     p.y = rc_edit.top;
975     ScreenToClient(hDlgWnd, &p);
976     SetWindowPos(GetDlgItem(hDlgWnd, IDC_EDIT), 0,
977 maya 3902 0, 0, dlg_w - p.x - edit2ok - ok2right, dlg_h - p.y - edit2bottom,
978 maya 3227 SWP_NOMOVE | SWP_NOZORDER);
979    
980     // �T�C�Y������
981     GetWindowRect(hDlgWnd, &rc_dlg);
982     ts.PasteDialogSize.cx = rc_dlg.right - rc_dlg.left;
983     ts.PasteDialogSize.cy = rc_dlg.bottom - rc_dlg.top;
984 yutakapon 3635
985     // status bar
986     SendMessage(hStatus , msg , wp , lp);
987 maya 3227 }
988     return TRUE;
989    
990 maya 3641 case WM_GETMINMAXINFO:
991     {
992     // �_�C�A���O�������T�C�Y����������������������������
993     LPMINMAXINFO lpmmi;
994     lpmmi = (LPMINMAXINFO)lp;
995     lpmmi->ptMinTrackSize.x = init_width;
996     lpmmi->ptMinTrackSize.y = init_height;
997     }
998     return FALSE;
999    
1000 maya 3227 default:
1001     return FALSE;
1002     }
1003     return TRUE;
1004     }

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