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 3306 - (hide annotations) (download) (as text)
Sun Apr 12 12:45:43 2009 UTC (15 years ago) by maya
File MIME type: text/x-csrc
File size: 13509 byte(s)
複数行のデータをペーストするとき、改行ごとに遅延させる時間を設定できるようにした。
  http://sourceforge.jp/ticket/browse.php?group_id=1412&tid=15575
  500バイトごとに 10ms 待つ workaround (http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?view=rev&root=ttssh2&revision=2578) は削除した。
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    
13     #include "ttwinman.h"
14     #include "ttcommon.h"
15    
16     #include "clipboar.h"
17     #include "tt_res.h"
18    
19     // for clipboard copy
20     static HGLOBAL CBCopyHandle = NULL;
21     static PCHAR CBCopyPtr = NULL;
22    
23     // for clipboard paste
24     static HGLOBAL CBMemHandle = NULL;
25     static PCHAR CBMemPtr = NULL;
26     static LONG CBMemPtr2 = 0;
27     static BOOL CBAddCR = FALSE;
28     static BYTE CBByte;
29     static BOOL CBRetrySend;
30     static BOOL CBRetryEcho;
31     static BOOL CBSendCR;
32     static BOOL CBDDE;
33    
34     static HFONT DlgClipboardFont;
35    
36     PCHAR CBOpen(LONG MemSize)
37     {
38     if (MemSize==0) return (NULL);
39     if (CBCopyHandle!=NULL) return (NULL);
40     CBCopyPtr = NULL;
41     CBCopyHandle = GlobalAlloc(GMEM_MOVEABLE, MemSize);
42     if (CBCopyHandle == NULL)
43     MessageBeep(0);
44     else {
45     CBCopyPtr = GlobalLock(CBCopyHandle);
46     if (CBCopyPtr == NULL)
47     {
48     GlobalFree(CBCopyHandle);
49     CBCopyHandle = NULL;
50     MessageBeep(0);
51     }
52     }
53     return (CBCopyPtr);
54     }
55    
56     void CBClose()
57     {
58     BOOL Empty;
59     if (CBCopyHandle==NULL) return;
60    
61     Empty = FALSE;
62     if (CBCopyPtr!=NULL)
63     Empty = (CBCopyPtr[0]==0);
64    
65     GlobalUnlock(CBCopyHandle);
66     CBCopyPtr = NULL;
67    
68     if (OpenClipboard(HVTWin))
69     {
70     EmptyClipboard();
71     if (! Empty)
72     SetClipboardData(CF_TEXT, CBCopyHandle);
73     CloseClipboard();
74     }
75     CBCopyHandle = NULL;
76     }
77    
78     void CBStartPaste(HWND HWin, BOOL AddCR,
79     int BuffSize, PCHAR DataPtr, int DataSize)
80     //
81     // DataPtr and DataSize are used only for DDE
82     // For clipboard, BuffSize should be 0
83     // DataSize should be <= BuffSize
84     {
85     UINT Cf;
86    
87     if (! cv.Ready) return;
88     if (TalkStatus!=IdTalkKeyb) return;
89    
90     CBAddCR = AddCR;
91    
92     if (BuffSize==0) // for clipboard
93     {
94     if (IsClipboardFormatAvailable(CF_TEXT))
95     Cf = CF_TEXT;
96     else if (IsClipboardFormatAvailable(CF_OEMTEXT))
97     Cf = CF_OEMTEXT;
98     else return;
99     }
100    
101     CBMemHandle = NULL;
102     CBMemPtr = NULL;
103     CBMemPtr2 = 0;
104     CBDDE = FALSE;
105     if (BuffSize==0) //clipboard
106     {
107     if (OpenClipboard(HWin))
108     CBMemHandle = GetClipboardData(Cf);
109     if (CBMemHandle!=NULL) TalkStatus=IdTalkCB;
110     }
111     else { // dde
112     CBMemHandle = GlobalAlloc(GHND,BuffSize);
113     if (CBMemHandle != NULL)
114     {
115     CBDDE = TRUE;
116     CBMemPtr = GlobalLock(CBMemHandle);
117     if (CBMemPtr != NULL)
118     {
119     memcpy(CBMemPtr,DataPtr,DataSize);
120     GlobalUnlock(CBMemHandle);
121     CBMemPtr=NULL;
122     TalkStatus=IdTalkCB;
123     }
124     }
125     }
126     CBRetrySend = FALSE;
127     CBRetryEcho = FALSE;
128     CBSendCR = FALSE;
129     if (TalkStatus != IdTalkCB) CBEndPaste();
130     }
131    
132     // �����������N���b�v�{�[�h������DDE�f�[�^���[�������������B
133     //
134     // CBMemHandle�n���h�����O���[�o�������������A�����������I�������������A
135     // �����N���b�v�{�[�h������DDE�f�[�^�������������������������i�j�����������\�������j�B
136     // �����A�f�[�^���� null-terminate �����������������O�����������������A�������f�[�^����
137     // �����������B
138     // (2006.11.6 yutaka)
139     void CBSend()
140     {
141     int c;
142     BOOL EndFlag;
143    
144     if (CBMemHandle==NULL) return;
145    
146     if (CBRetrySend)
147     {
148     CBRetryEcho = (ts.LocalEcho>0);
149     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
150     CBRetrySend = (c==0);
151     if (CBRetrySend) return;
152     }
153    
154     if (CBRetryEcho)
155     {
156     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
157     CBRetryEcho = (c==0);
158     if (CBRetryEcho) return;
159     }
160    
161     CBMemPtr = GlobalLock(CBMemHandle);
162     if (CBMemPtr==NULL) return;
163    
164     do {
165 maya 3306 if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
166 maya 3227 CBMemPtr2++;
167 maya 3306 // added PasteDelayPerLine (2009.4.12 maya)
168     Sleep(ts.PasteDelayPerLine);
169     }
170 maya 3227
171     EndFlag = (CBMemPtr[CBMemPtr2]==0);
172     if (! EndFlag)
173     {
174     CBByte = CBMemPtr[CBMemPtr2];
175     CBMemPtr2++;
176     // Decoding characters which are encoded by MACRO
177     // to support NUL character sending
178     //
179     // [encoded character] --> [decoded character]
180     // 01 01 --> 00
181     // 01 02 --> 01
182     if (CBByte==0x01) /* 0x01 from MACRO */
183     {
184     CBByte = CBMemPtr[CBMemPtr2];
185     CBMemPtr2++;
186     CBByte = CBByte - 1; // character just after 0x01
187     }
188     }
189     else if (CBAddCR)
190     {
191     EndFlag = FALSE;
192     CBAddCR = FALSE;
193     CBByte = 0x0d;
194     }
195     else {
196     CBEndPaste();
197     return;
198     }
199    
200     if (! EndFlag)
201     {
202     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
203     CBSendCR = (CBByte==0x0D);
204     CBRetrySend = (c==0);
205     if ((! CBRetrySend) &&
206     (ts.LocalEcho>0))
207     {
208     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
209     CBRetryEcho = (c==0);
210     }
211     }
212     else
213     c=0;
214     }
215     while (c>0);
216    
217     if (CBMemPtr!=NULL)
218     {
219     GlobalUnlock(CBMemHandle);
220     CBMemPtr=NULL;
221     }
222     }
223    
224     void CBEndPaste()
225     {
226     TalkStatus = IdTalkKeyb;
227    
228     if (CBMemHandle!=NULL)
229     {
230     if (CBMemPtr!=NULL)
231     GlobalUnlock(CBMemHandle);
232     if (CBDDE)
233     GlobalFree(CBMemHandle);
234     }
235     if (!CBDDE) CloseClipboard();
236    
237     CBDDE = FALSE;
238     CBMemHandle = NULL;
239     CBMemPtr = NULL;
240     CBMemPtr2 = 0;
241     CBAddCR = FALSE;
242     }
243    
244    
245     static char *ClipboardPtr = NULL;
246     static int PasteCanceled = 0;
247    
248     static LRESULT CALLBACK OnClipboardDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp)
249     {
250     LOGFONT logfont;
251     HFONT font;
252     char uimsg[MAX_UIMSG];
253     //char *p;
254     POINT p;
255     RECT rc_dsk, rc_dlg;
256     int dlg_height, dlg_width;
257     OSVERSIONINFO osvi;
258     static int ok2right, info2bottom, edit2ok, edit2info;
259     RECT rc_edit, rc_ok, rc_cancel, rc_info;
260    
261     switch (msg) {
262     case WM_INITDIALOG:
263     #if 0
264     for (p = ClipboardPtr; *p ; p++) {
265     char buf[20];
266     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%02x ", *p);
267     OutputDebugString(buf);
268     }
269     #endif
270    
271     font = (HFONT)SendMessage(hDlgWnd, WM_GETFONT, 0, 0);
272     GetObject(font, sizeof(LOGFONT), &logfont);
273     if (get_lang_font("DLG_TAHOMA_FONT", hDlgWnd, &logfont, &DlgClipboardFont, ts.UILanguageFile)) {
274     SendDlgItemMessage(hDlgWnd, IDC_EDIT, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
275     SendDlgItemMessage(hDlgWnd, IDC_CLIPBOARD_INFO, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
276     SendDlgItemMessage(hDlgWnd, IDOK, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
277     SendDlgItemMessage(hDlgWnd, IDCANCEL, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
278     }
279     else {
280     DlgClipboardFont = NULL;
281     }
282    
283     GetWindowText(hDlgWnd, uimsg, sizeof(uimsg));
284     get_lang_msg("DLG_CLIPBOARD_TITLE", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
285     SetWindowText(hDlgWnd, ts.UIMsg);
286     GetDlgItemText(hDlgWnd, IDC_CLIPBOARD_INFO, uimsg, sizeof(uimsg));
287     get_lang_msg("DLG_CLIPBOARD_INFO", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
288     SetDlgItemText(hDlgWnd, IDC_CLIPBOARD_INFO, ts.UIMsg);
289     GetDlgItemText(hDlgWnd, IDCANCEL, uimsg, sizeof(uimsg));
290     get_lang_msg("BTN_CANCEL", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
291     SetDlgItemText(hDlgWnd, IDCANCEL, ts.UIMsg);
292     GetDlgItemText(hDlgWnd, IDOK, uimsg, sizeof(uimsg));
293     get_lang_msg("BTN_OK", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
294     SetDlgItemText(hDlgWnd, IDOK, ts.UIMsg);
295    
296     SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_SETTEXT, 0, (LPARAM)ClipboardPtr);
297    
298     DispConvScreenToWin(CursorX, CursorY, &p.x, &p.y);
299     ClientToScreen(HVTWin, &p);
300    
301     // �L�����b�g���������������o���������������\���t����������
302     // �m�F�E�C���h�E�����������������\���������������������B
303     // �E�C���h�E���������o������������������ (2008.4.24 maya)
304     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
305     GetVersionEx(&osvi);
306     if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion == 4) {
307     // NT4.0 ���}���`���j�^API��������
308     SystemParametersInfo(SPI_GETWORKAREA, 0, &rc_dsk, 0);
309     }
310     else {
311     HMONITOR hm;
312     POINT pt;
313     MONITORINFO mi;
314    
315     pt.x = p.x;
316     pt.y = p.y;
317     hm = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
318    
319     mi.cbSize = sizeof(MONITORINFO);
320     GetMonitorInfo(hm, &mi);
321     rc_dsk = mi.rcWork;
322     }
323     GetWindowRect(hDlgWnd, &rc_dlg);
324     dlg_height = rc_dlg.bottom-rc_dlg.top;
325     dlg_width = rc_dlg.right-rc_dlg.left;
326     if (p.y < rc_dsk.top) {
327     p.y = rc_dsk.top;
328     }
329     else if (p.y + dlg_height > rc_dsk.bottom) {
330     p.y = rc_dsk.bottom - dlg_height;
331     }
332     if (p.x < rc_dsk.left) {
333     p.x = rc_dsk.left;
334     }
335     else if (p.x + dlg_width > rc_dsk.right) {
336     p.x = rc_dsk.right - dlg_width;
337     }
338    
339     SetWindowPos(hDlgWnd, NULL, p.x, p.y,
340     0, 0, SWP_NOSIZE | SWP_NOZORDER);
341    
342     // �����T�C�Y�����K�v���l���v�Z
343     GetClientRect(hDlgWnd, &rc_dlg);
344     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
345     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
346     GetWindowRect(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), &rc_info);
347    
348     p.x = rc_dlg.right;
349     p.y = rc_dlg.bottom;
350     ClientToScreen(hDlgWnd, &p);
351     ok2right = p.x - rc_ok.left;
352     info2bottom = p.y - rc_info.top;
353     edit2ok = rc_ok.left - rc_edit.right;
354     edit2info = rc_info.top - rc_edit.bottom;
355    
356     // �T�C�Y������
357     SetWindowPos(hDlgWnd, NULL, 0, 0,
358     ts.PasteDialogSize.cx, ts.PasteDialogSize.cy,
359     SWP_NOZORDER | SWP_NOMOVE);
360    
361     return TRUE;
362    
363     case WM_COMMAND:
364     switch (LOWORD(wp)) {
365     case IDOK:
366     {
367     int len = SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXTLENGTH, 0, 0);
368     HGLOBAL hMem;
369     char *buf;
370    
371     hMem = GlobalAlloc(GMEM_MOVEABLE, len + 1);
372     buf = GlobalLock(hMem);
373     SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXT, len + 1, (LPARAM)buf);
374     GlobalUnlock(hMem);
375    
376     EmptyClipboard();
377     SetClipboardData(CF_TEXT, hMem);
378    
379     // hMem���N���b�v�{�[�h�������������������A�j�����������������B
380    
381     if (DlgClipboardFont != NULL) {
382     DeleteObject(DlgClipboardFont);
383     }
384    
385     EndDialog(hDlgWnd, IDOK);
386     }
387     break;
388    
389     case IDCANCEL:
390     PasteCanceled = 1;
391    
392     if (DlgClipboardFont != NULL) {
393     DeleteObject(DlgClipboardFont);
394     }
395    
396     EndDialog(hDlgWnd, IDCANCEL);
397     break;
398    
399     default:
400     return FALSE;
401     }
402    
403     #if 0
404     case WM_CLOSE:
405     PasteCanceled = 1;
406     EndDialog(hDlgWnd, 0);
407     return TRUE;
408     #endif
409    
410     case WM_SIZE:
411     {
412     // ���z�u
413     POINT p;
414     int dlg_w, dlg_h;
415    
416     GetClientRect(hDlgWnd, &rc_dlg);
417     dlg_w = rc_dlg.right;
418     dlg_h = rc_dlg.bottom;
419    
420     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
421     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
422     GetWindowRect(GetDlgItem(hDlgWnd, IDCANCEL), &rc_cancel);
423     GetWindowRect(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), &rc_info);
424    
425     // OK
426     p.x = rc_ok.left;
427     p.y = rc_ok.top;
428     ScreenToClient(hDlgWnd, &p);
429     SetWindowPos(GetDlgItem(hDlgWnd, IDOK), 0,
430     dlg_w - ok2right, p.y, 0, 0,
431     SWP_NOSIZE | SWP_NOZORDER);
432    
433     // CANCEL
434     p.x = rc_cancel.left;
435     p.y = rc_cancel.top;
436     ScreenToClient(hDlgWnd, &p);
437     SetWindowPos(GetDlgItem(hDlgWnd, IDCANCEL), 0,
438     dlg_w - ok2right, p.y, 0, 0,
439     SWP_NOSIZE | SWP_NOZORDER);
440    
441     // INFO
442     p.x = rc_info.left;
443     p.y = rc_info.top;
444     ScreenToClient(hDlgWnd, &p);
445     SetWindowPos(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), 0,
446     p.x, dlg_h - info2bottom, 0, 0,
447     SWP_NOSIZE | SWP_NOZORDER);
448    
449     // EDIT
450     p.x = rc_edit.left;
451     p.y = rc_edit.top;
452     ScreenToClient(hDlgWnd, &p);
453     SetWindowPos(GetDlgItem(hDlgWnd, IDC_EDIT), 0,
454     0, 0, dlg_w - p.x - edit2ok - ok2right, dlg_h - p.y - edit2info - info2bottom,
455     SWP_NOMOVE | SWP_NOZORDER);
456    
457     // �T�C�Y������
458     GetWindowRect(hDlgWnd, &rc_dlg);
459     ts.PasteDialogSize.cx = rc_dlg.right - rc_dlg.left;
460     ts.PasteDialogSize.cy = rc_dlg.bottom - rc_dlg.top;
461     }
462     return TRUE;
463    
464     default:
465     return FALSE;
466     }
467     return TRUE;
468     }
469    
470     //
471     // �N���b�v�{�[�h�����s�R�[�h�����������������A�m�F�_�C�A���O���\�������B
472     // �N���b�v�{�[�h�����X�����\�B
473     //
474     // return 0: Cancel
475     // 1: Paste OK
476     //
477     // (2008.2.3 yutaka)
478     //
479     int CBStartPasteConfirmChange(HWND HWin)
480     {
481     UINT Cf;
482     HANDLE hText;
483     char *pText;
484     int pos;
485     int ret = 0;
486    
487     if (ts.ConfirmChangePaste == 0)
488     return 1;
489    
490     if (! cv.Ready)
491     goto error;
492     if (TalkStatus!=IdTalkKeyb)
493     goto error;
494    
495     if (IsClipboardFormatAvailable(CF_TEXT))
496     Cf = CF_TEXT;
497     else if (IsClipboardFormatAvailable(CF_OEMTEXT))
498     Cf = CF_OEMTEXT;
499     else
500     goto error;
501    
502     if (!OpenClipboard(HWin))
503     goto error;
504    
505     hText = GetClipboardData(Cf);
506    
507     if (hText != NULL) {
508     pText = (char *)GlobalLock(hText);
509     pos = strcspn(pText, "\r\n"); // ���s����������������
510     if (pText[pos] != '\0') {
511     ClipboardPtr = pText;
512     PasteCanceled = 0;
513     ret = DialogBox(hInst, MAKEINTRESOURCE(IDD_CLIPBOARD_DIALOG),
514     HVTWin, (DLGPROC)OnClipboardDlgProc);
515     if (ret == 0 || ret == -1) {
516     ret = GetLastError();
517     }
518    
519     if (PasteCanceled) {
520     ret = 0;
521     GlobalUnlock(hText);
522     CloseClipboard();
523     goto error;
524     }
525    
526     }
527    
528     ret = 1;
529    
530     GlobalUnlock(hText);
531     }
532    
533     CloseClipboard();
534    
535     error:
536     return (ret);
537     }

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