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 2563 - (hide annotations) (download) (as text)
Wed Jul 16 16:07:32 2008 UTC (15 years, 8 months ago) by maya
Original Path: teraterm/trunk/teraterm/clipboar.c
File MIME type: text/x-csrc
File size: 12545 byte(s)
クリップボード用ダイアログのリサイズに対応。

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     // �E�C���h�E���������o������������������ (2008.4.24 mya)
301     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 2476 return TRUE;
354    
355     case WM_COMMAND:
356     switch (LOWORD(wp)) {
357     case IDOK:
358     {
359     int len = SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXTLENGTH, 0, 0);
360     HGLOBAL hMem;
361     char *buf;
362    
363     hMem = GlobalAlloc(GMEM_MOVEABLE, len + 1);
364     buf = GlobalLock(hMem);
365     SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXT, len + 1, (LPARAM)buf);
366     GlobalUnlock(hMem);
367    
368     EmptyClipboard();
369     SetClipboardData(CF_TEXT, hMem);
370    
371     // hMem���N���b�v�{�[�h�������������������A�j�����������������B
372    
373     if (DlgClipboardFont != NULL) {
374     DeleteObject(DlgClipboardFont);
375     }
376    
377     EndDialog(hDlgWnd, IDOK);
378     }
379     break;
380    
381     case IDCANCEL:
382     PasteCanceled = 1;
383    
384     if (DlgClipboardFont != NULL) {
385     DeleteObject(DlgClipboardFont);
386     }
387    
388     EndDialog(hDlgWnd, IDCANCEL);
389     break;
390    
391     default:
392     return FALSE;
393     }
394    
395     #if 0
396     case WM_CLOSE:
397     PasteCanceled = 1;
398     EndDialog(hDlgWnd, 0);
399     return TRUE;
400     #endif
401    
402 yutakapon 2560 case WM_SIZE:
403     {
404 maya 2563 // ���z�u
405     POINT p;
406     int dlg_w, dlg_h;
407 yutakapon 2560
408 maya 2563 GetClientRect(hDlgWnd, &rc_dlg);
409     dlg_w = rc_dlg.right;
410     dlg_h = rc_dlg.bottom;
411 yutakapon 2560
412 maya 2563 GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
413     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
414     GetWindowRect(GetDlgItem(hDlgWnd, IDCANCEL), &rc_cancel);
415     GetWindowRect(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), &rc_info);
416 yutakapon 2560
417 maya 2563 // OK
418     p.x = rc_ok.left;
419     p.y = rc_ok.top;
420     ScreenToClient(hDlgWnd, &p);
421     SetWindowPos(GetDlgItem(hDlgWnd, IDOK), 0,
422     dlg_w - ok2right, p.y, 0, 0,
423     SWP_NOSIZE | SWP_NOZORDER);
424    
425     // CANCEL
426     p.x = rc_cancel.left;
427     p.y = rc_cancel.top;
428     ScreenToClient(hDlgWnd, &p);
429     SetWindowPos(GetDlgItem(hDlgWnd, IDCANCEL), 0,
430     dlg_w - ok2right, p.y, 0, 0,
431     SWP_NOSIZE | SWP_NOZORDER);
432    
433     // INFO
434     p.x = rc_info.left;
435     p.y = rc_info.top;
436     ScreenToClient(hDlgWnd, &p);
437     SetWindowPos(GetDlgItem(hDlgWnd, IDC_CLIPBOARD_INFO), 0,
438     p.x, dlg_h - info2bottom, 0, 0,
439     SWP_NOSIZE | SWP_NOZORDER);
440    
441     // EDIT
442     p.x = rc_edit.left;
443     p.y = rc_edit.top;
444     ScreenToClient(hDlgWnd, &p);
445     SetWindowPos(GetDlgItem(hDlgWnd, IDC_EDIT), 0,
446     0, 0, dlg_w - p.x - edit2ok - ok2right, dlg_h - p.y - edit2info - info2bottom,
447     SWP_NOMOVE | SWP_NOZORDER);
448 yutakapon 2560 }
449     return TRUE;
450    
451 maya 2476 default:
452     return FALSE;
453     }
454     return TRUE;
455     }
456    
457     //
458     // �N���b�v�{�[�h�����s�R�[�h�����������������A�m�F�_�C�A���O���\�������B
459     // �N���b�v�{�[�h�����X�����\�B
460     //
461     // return 0: Cancel
462     // 1: Paste OK
463     //
464     // (2008.2.3 yutaka)
465     //
466     int CBStartPasteConfirmChange(HWND HWin)
467     {
468     UINT Cf;
469     HANDLE hText;
470     char *pText;
471     int pos;
472     int ret = 0;
473    
474     if (ts.ConfirmChangePaste == 0)
475     return 1;
476    
477     if (! cv.Ready)
478     goto error;
479     if (TalkStatus!=IdTalkKeyb)
480     goto error;
481    
482     if (IsClipboardFormatAvailable(CF_TEXT))
483     Cf = CF_TEXT;
484     else if (IsClipboardFormatAvailable(CF_OEMTEXT))
485     Cf = CF_OEMTEXT;
486     else
487     goto error;
488    
489     if (!OpenClipboard(HWin))
490     goto error;
491    
492     hText = GetClipboardData(Cf);
493    
494     if (hText != NULL) {
495     pText = (char *)GlobalLock(hText);
496     pos = strcspn(pText, "\r\n"); // ���s����������������
497     if (pText[pos] != '\0') {
498     ClipboardPtr = pText;
499     PasteCanceled = 0;
500     ret = DialogBox(hInst, MAKEINTRESOURCE(IDD_CLIPBOARD_DIALOG),
501     HVTWin, (DLGPROC)OnClipboardDlgProc);
502     if (ret == 0 || ret == -1) {
503     ret = GetLastError();
504     }
505    
506     if (PasteCanceled) {
507     ret = 0;
508     GlobalUnlock(hText);
509     CloseClipboard();
510     goto error;
511     }
512    
513     }
514    
515     ret = 1;
516    
517     GlobalUnlock(hText);
518     }
519    
520     CloseClipboard();
521    
522     error:
523     return (ret);
524     }

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