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 2565 - (hide annotations) (download) (as text)
Sat Jul 26 00:19:31 2008 UTC (15 years, 8 months ago) by maya
Original Path: teraterm/trunk/teraterm/clipboar.c
File MIME type: text/x-csrc
File size: 12879 byte(s)
ConfirmChangePaste のダイアログサイズを保存できるようにした。

1 maya 2476 /* 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 maya 2488 #include "vtdisp.h"
9 maya 2476 #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     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a))
166     CBMemPtr2++;
167    
168     EndFlag = (CBMemPtr[CBMemPtr2]==0);
169     if (! EndFlag)
170     {
171     CBByte = CBMemPtr[CBMemPtr2];
172     CBMemPtr2++;
173     // Decoding characters which are encoded by MACRO
174     // to support NUL character sending
175     //
176     // [encoded character] --> [decoded character]
177     // 01 01 --> 00
178     // 01 02 --> 01
179     if (CBByte==0x01) /* 0x01 from MACRO */
180     {
181     CBByte = CBMemPtr[CBMemPtr2];
182     CBMemPtr2++;
183     CBByte = CBByte - 1; // character just after 0x01
184     }
185     }
186     else if (CBAddCR)
187     {
188     EndFlag = FALSE;
189     CBAddCR = FALSE;
190     CBByte = 0x0d;
191     }
192     else {
193     CBEndPaste();
194     return;
195     }
196    
197     if (! EndFlag)
198     {
199     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
200     CBSendCR = (CBByte==0x0D);
201     CBRetrySend = (c==0);
202     if ((! CBRetrySend) &&
203     (ts.LocalEcho>0))
204     {
205     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
206     CBRetryEcho = (c==0);
207     }
208     }
209     else
210     c=0;
211     }
212     while (c>0);
213    
214     if (CBMemPtr!=NULL)
215     {
216     GlobalUnlock(CBMemHandle);
217     CBMemPtr=NULL;
218     }
219     }
220    
221     void CBEndPaste()
222     {
223     TalkStatus = IdTalkKeyb;
224    
225     if (CBMemHandle!=NULL)
226     {
227     if (CBMemPtr!=NULL)
228     GlobalUnlock(CBMemHandle);
229     if (CBDDE)
230     GlobalFree(CBMemHandle);
231     }
232     if (!CBDDE) CloseClipboard();
233    
234     CBDDE = FALSE;
235     CBMemHandle = NULL;
236     CBMemPtr = NULL;
237     CBMemPtr2 = 0;
238     CBAddCR = FALSE;
239     }
240    
241    
242     static char *ClipboardPtr = NULL;
243     static int PasteCanceled = 0;
244    
245     static LRESULT CALLBACK OnClipboardDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp)
246     {
247     LOGFONT logfont;
248     HFONT font;
249     char uimsg[MAX_UIMSG];
250 maya 2489 //char *p;
251     POINT p;
252     RECT rc_dsk, rc_dlg;
253     int dlg_height, dlg_width;
254     OSVERSIONINFO osvi;
255 maya 2563 static int ok2right, info2bottom, edit2ok, edit2info;
256     RECT rc_edit, rc_ok, rc_cancel, rc_info;
257 maya 2476
258     switch (msg) {
259     case WM_INITDIALOG:
260     #if 0
261     for (p = ClipboardPtr; *p ; p++) {
262     char buf[20];
263     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%02x ", *p);
264     OutputDebugString(buf);
265     }
266     #endif
267    
268     font = (HFONT)SendMessage(hDlgWnd, WM_GETFONT, 0, 0);
269     GetObject(font, sizeof(LOGFONT), &logfont);
270     if (get_lang_font("DLG_TAHOMA_FONT", hDlgWnd, &logfont, &DlgClipboardFont, ts.UILanguageFile)) {
271     SendDlgItemMessage(hDlgWnd, IDC_EDIT, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
272     SendDlgItemMessage(hDlgWnd, IDC_CLIPBOARD_INFO, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
273     SendDlgItemMessage(hDlgWnd, IDOK, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
274     SendDlgItemMessage(hDlgWnd, IDCANCEL, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
275     }
276     else {
277     DlgClipboardFont = NULL;
278     }
279    
280     GetWindowText(hDlgWnd, uimsg, sizeof(uimsg));
281     get_lang_msg("DLG_CLIPBOARD_TITLE", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
282     SetWindowText(hDlgWnd, ts.UIMsg);
283     GetDlgItemText(hDlgWnd, IDC_CLIPBOARD_INFO, uimsg, sizeof(uimsg));
284     get_lang_msg("DLG_CLIPBOARD_INFO", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
285     SetDlgItemText(hDlgWnd, IDC_CLIPBOARD_INFO, ts.UIMsg);
286     GetDlgItemText(hDlgWnd, IDCANCEL, uimsg, sizeof(uimsg));
287     get_lang_msg("BTN_CANCEL", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
288     SetDlgItemText(hDlgWnd, IDCANCEL, ts.UIMsg);
289     GetDlgItemText(hDlgWnd, IDOK, uimsg, sizeof(uimsg));
290     get_lang_msg("BTN_OK", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
291     SetDlgItemText(hDlgWnd, IDOK, ts.UIMsg);
292    
293     SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_SETTEXT, 0, (LPARAM)ClipboardPtr);
294 maya 2480
295 maya 2488 DispConvScreenToWin(CursorX, CursorY, &p.x, &p.y);
296     ClientToScreen(HVTWin, &p);
297 maya 2489
298     // �L�����b�g���������������o���������������\���t����������
299     // �m�F�E�C���h�E�����������������\���������������������B
300 maya 2565 // �E�C���h�E���������o������������������ (2008.4.24 maya)
301 maya 2489 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
302     GetVersionEx(&osvi);
303     if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion == 4) {
304     // NT4.0 ���}���`���j�^API��������
305     SystemParametersInfo(SPI_GETWORKAREA, 0, &rc_dsk, 0);
306     }
307     else {
308     HMONITOR hm;
309     POINT pt;
310     MONITORINFO mi;
311    
312     pt.x = p.x;
313     pt.y = p.y;
314     hm = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
315    
316     mi.cbSize = sizeof(MONITORINFO);
317     GetMonitorInfo(hm, &mi);
318     rc_dsk = mi.rcWork;
319     }
320     GetWindowRect(hDlgWnd, &rc_dlg);
321     dlg_height = rc_dlg.bottom-rc_dlg.top;
322     dlg_width = rc_dlg.right-rc_dlg.left;
323     if (p.y < rc_dsk.top) {
324     p.y = rc_dsk.top;
325     }
326     else if (p.y + dlg_height > rc_dsk.bottom) {
327     p.y = rc_dsk.bottom - dlg_height;
328     }
329     if (p.x < rc_dsk.left) {
330     p.x = rc_dsk.left;
331     }
332     else if (p.x + dlg_width > rc_dsk.right) {
333     p.x = rc_dsk.right - dlg_width;
334     }
335    
336 maya 2488 SetWindowPos(hDlgWnd, NULL, p.x, p.y,
337     0, 0, SWP_NOSIZE | SWP_NOZORDER);
338 maya 2480
339 maya 2563 // �����T�C�Y�����K�v���l���v�Z
340     GetClientRect(hDlgWnd, &rc_dlg);
341     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
342     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
343     GetWindowRect(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), &rc_info);
344 yutakapon 2560
345 maya 2563 p.x = rc_dlg.right;
346     p.y = rc_dlg.bottom;
347     ClientToScreen(hDlgWnd, &p);
348     ok2right = p.x - rc_ok.left;
349     info2bottom = p.y - rc_info.top;
350     edit2ok = rc_ok.left - rc_edit.right;
351     edit2info = rc_info.top - rc_edit.bottom;
352    
353 maya 2565 // �T�C�Y������
354     SetWindowPos(hDlgWnd, NULL, 0, 0,
355     ts.PasteDialogSize.cx, ts.PasteDialogSize.cy,
356     SWP_NOZORDER | SWP_NOMOVE);
357    
358 maya 2476 return TRUE;
359    
360     case WM_COMMAND:
361     switch (LOWORD(wp)) {
362     case IDOK:
363     {
364     int len = SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXTLENGTH, 0, 0);
365     HGLOBAL hMem;
366     char *buf;
367    
368     hMem = GlobalAlloc(GMEM_MOVEABLE, len + 1);
369     buf = GlobalLock(hMem);
370     SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXT, len + 1, (LPARAM)buf);
371     GlobalUnlock(hMem);
372    
373     EmptyClipboard();
374     SetClipboardData(CF_TEXT, hMem);
375    
376     // hMem���N���b�v�{�[�h�������������������A�j�����������������B
377    
378     if (DlgClipboardFont != NULL) {
379     DeleteObject(DlgClipboardFont);
380     }
381    
382     EndDialog(hDlgWnd, IDOK);
383     }
384     break;
385    
386     case IDCANCEL:
387     PasteCanceled = 1;
388    
389     if (DlgClipboardFont != NULL) {
390     DeleteObject(DlgClipboardFont);
391     }
392    
393     EndDialog(hDlgWnd, IDCANCEL);
394     break;
395    
396     default:
397     return FALSE;
398     }
399    
400     #if 0
401     case WM_CLOSE:
402     PasteCanceled = 1;
403     EndDialog(hDlgWnd, 0);
404     return TRUE;
405     #endif
406    
407 yutakapon 2560 case WM_SIZE:
408     {
409 maya 2563 // ���z�u
410     POINT p;
411     int dlg_w, dlg_h;
412 yutakapon 2560
413 maya 2563 GetClientRect(hDlgWnd, &rc_dlg);
414     dlg_w = rc_dlg.right;
415     dlg_h = rc_dlg.bottom;
416 yutakapon 2560
417 maya 2563 GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
418     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
419     GetWindowRect(GetDlgItem(hDlgWnd, IDCANCEL), &rc_cancel);
420     GetWindowRect(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), &rc_info);
421 yutakapon 2560
422 maya 2563 // OK
423     p.x = rc_ok.left;
424     p.y = rc_ok.top;
425     ScreenToClient(hDlgWnd, &p);
426     SetWindowPos(GetDlgItem(hDlgWnd, IDOK), 0,
427     dlg_w - ok2right, p.y, 0, 0,
428     SWP_NOSIZE | SWP_NOZORDER);
429    
430     // CANCEL
431     p.x = rc_cancel.left;
432     p.y = rc_cancel.top;
433     ScreenToClient(hDlgWnd, &p);
434     SetWindowPos(GetDlgItem(hDlgWnd, IDCANCEL), 0,
435     dlg_w - ok2right, p.y, 0, 0,
436     SWP_NOSIZE | SWP_NOZORDER);
437    
438     // INFO
439     p.x = rc_info.left;
440     p.y = rc_info.top;
441     ScreenToClient(hDlgWnd, &p);
442     SetWindowPos(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), 0,
443     p.x, dlg_h - info2bottom, 0, 0,
444     SWP_NOSIZE | SWP_NOZORDER);
445    
446     // EDIT
447     p.x = rc_edit.left;
448     p.y = rc_edit.top;
449     ScreenToClient(hDlgWnd, &p);
450     SetWindowPos(GetDlgItem(hDlgWnd, IDC_EDIT), 0,
451     0, 0, dlg_w - p.x - edit2ok - ok2right, dlg_h - p.y - edit2info - info2bottom,
452     SWP_NOMOVE | SWP_NOZORDER);
453 maya 2565
454     // �T�C�Y������
455     GetWindowRect(hDlgWnd, &rc_dlg);
456     ts.PasteDialogSize.cx = rc_dlg.right - rc_dlg.left;
457     ts.PasteDialogSize.cy = rc_dlg.bottom - rc_dlg.top;
458 yutakapon 2560 }
459     return TRUE;
460    
461 maya 2476 default:
462     return FALSE;
463     }
464     return TRUE;
465     }
466    
467     //
468     // �N���b�v�{�[�h�����s�R�[�h�����������������A�m�F�_�C�A���O���\�������B
469     // �N���b�v�{�[�h�����X�����\�B
470     //
471     // return 0: Cancel
472     // 1: Paste OK
473     //
474     // (2008.2.3 yutaka)
475     //
476     int CBStartPasteConfirmChange(HWND HWin)
477     {
478     UINT Cf;
479     HANDLE hText;
480     char *pText;
481     int pos;
482     int ret = 0;
483    
484     if (ts.ConfirmChangePaste == 0)
485     return 1;
486    
487     if (! cv.Ready)
488     goto error;
489     if (TalkStatus!=IdTalkKeyb)
490     goto error;
491    
492     if (IsClipboardFormatAvailable(CF_TEXT))
493     Cf = CF_TEXT;
494     else if (IsClipboardFormatAvailable(CF_OEMTEXT))
495     Cf = CF_OEMTEXT;
496     else
497     goto error;
498    
499     if (!OpenClipboard(HWin))
500     goto error;
501    
502     hText = GetClipboardData(Cf);
503    
504     if (hText != NULL) {
505     pText = (char *)GlobalLock(hText);
506     pos = strcspn(pText, "\r\n"); // ���s����������������
507     if (pText[pos] != '\0') {
508     ClipboardPtr = pText;
509     PasteCanceled = 0;
510     ret = DialogBox(hInst, MAKEINTRESOURCE(IDD_CLIPBOARD_DIALOG),
511     HVTWin, (DLGPROC)OnClipboardDlgProc);
512     if (ret == 0 || ret == -1) {
513     ret = GetLastError();
514     }
515    
516     if (PasteCanceled) {
517     ret = 0;
518     GlobalUnlock(hText);
519     CloseClipboard();
520     goto error;
521     }
522    
523     }
524    
525     ret = 1;
526    
527     GlobalUnlock(hText);
528     }
529    
530     CloseClipboard();
531    
532     error:
533     return (ret);
534     }

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