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 3635 - (hide annotations) (download) (as text)
Wed Oct 7 15:46:43 2009 UTC (14 years, 6 months ago) by yutakapon
File MIME type: text/x-csrc
File size: 14790 byte(s)
ConfirmChangePasteダイアログにステータスバーを付け、ダイアログウィンドウの右下にリサイズアイコンが表示されるようにした。

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

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