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

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