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 6452 - (hide annotations) (download) (as text)
Mon Aug 1 09:01:20 2016 UTC (7 years, 8 months ago) by doda
File MIME type: text/x-csrc
File size: 22980 byte(s)
メモリリークが起きないように、念のため NULL かチェックを行う。
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 doda 4769 #include "ttlib.h"
17 maya 3227
18     #include "clipboar.h"
19     #include "tt_res.h"
20    
21     // for clipboard copy
22     static HGLOBAL CBCopyHandle = NULL;
23     static PCHAR CBCopyPtr = NULL;
24 maya 5071 static HGLOBAL CBCopyWideHandle = NULL;
25     static LPWSTR CBCopyWidePtr = NULL;
26 maya 3227
27     // for clipboard paste
28     static HGLOBAL CBMemHandle = NULL;
29     static PCHAR CBMemPtr = NULL;
30     static LONG CBMemPtr2 = 0;
31     static BYTE CBByte;
32     static BOOL CBRetrySend;
33     static BOOL CBRetryEcho;
34     static BOOL CBSendCR;
35 doda 3974 static BOOL CBEchoOnly;
36 doda 5259 static BOOL CBInsertDelay = FALSE;
37 maya 3227
38     static HFONT DlgClipboardFont;
39    
40     PCHAR CBOpen(LONG MemSize)
41     {
42 maya 3393 if (MemSize==0) {
43     return (NULL);
44     }
45     if (CBCopyHandle!=NULL) {
46     return (NULL);
47     }
48     CBCopyPtr = NULL;
49     CBCopyHandle = GlobalAlloc(GMEM_MOVEABLE, MemSize);
50     if (CBCopyHandle == NULL) {
51     MessageBeep(0);
52     }
53     else {
54     CBCopyPtr = GlobalLock(CBCopyHandle);
55     if (CBCopyPtr == NULL) {
56     GlobalFree(CBCopyHandle);
57     CBCopyHandle = NULL;
58     MessageBeep(0);
59     }
60     }
61     return (CBCopyPtr);
62 maya 3227 }
63    
64     void CBClose()
65     {
66 maya 3393 BOOL Empty;
67 maya 5071 int WideCharLength;
68    
69 maya 3393 if (CBCopyHandle==NULL) {
70     return;
71     }
72 maya 3227
73 maya 5071 WideCharLength = MultiByteToWideChar(CP_ACP, 0, CBCopyPtr, -1, NULL, 0);
74     CBCopyWideHandle = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * WideCharLength);
75     if (CBCopyWideHandle) {
76     CBCopyWidePtr = (LPWSTR)GlobalLock(CBCopyWideHandle);
77     MultiByteToWideChar(CP_ACP, 0, CBCopyPtr, -1, CBCopyWidePtr, WideCharLength);
78     GlobalUnlock(CBCopyWideHandle);
79     }
80    
81 maya 3393 Empty = FALSE;
82     if (CBCopyPtr!=NULL) {
83     Empty = (CBCopyPtr[0]==0);
84     }
85 maya 3227
86 maya 3393 GlobalUnlock(CBCopyHandle);
87     CBCopyPtr = NULL;
88 maya 3227
89 maya 3393 if (OpenClipboard(HVTWin)) {
90     EmptyClipboard();
91     if (! Empty) {
92     SetClipboardData(CF_TEXT, CBCopyHandle);
93 maya 5071 if (CBCopyWidePtr) {
94     SetClipboardData(CF_UNICODETEXT, CBCopyWideHandle);
95     CBCopyWidePtr = NULL;
96     }
97 maya 3393 }
98     CloseClipboard();
99     }
100     CBCopyHandle = NULL;
101 maya 5071 CBCopyWideHandle = NULL;
102 maya 3227 }
103    
104 doda 6440 void CBStartSend(PCHAR DataPtr, int DataSize, BOOL EchoOnly)
105 maya 3227 {
106 doda 6440 if (! cv.Ready) {
107     return;
108     }
109     if (TalkStatus!=IdTalkKeyb) {
110     return;
111     }
112    
113     CBEchoOnly = EchoOnly;
114    
115 doda 6452 if (CBMemHandle) {
116     GlobalFree(CBMemHandle);
117     }
118 doda 6440 CBMemHandle = NULL;
119     CBMemPtr = NULL;
120     CBMemPtr2 = 0;
121    
122     CBInsertDelay = FALSE;
123    
124     CBRetrySend = FALSE;
125     CBRetryEcho = FALSE;
126     CBSendCR = FALSE;
127    
128     if ((CBMemHandle = GlobalAlloc(GHND, DataSize)) != NULL) {
129     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
130     memcpy(CBMemPtr, DataPtr, DataSize);
131     GlobalUnlock(CBMemHandle);
132     CBMemPtr=NULL;
133     TalkStatus=IdTalkCB;
134     }
135     }
136    
137     if (TalkStatus != IdTalkCB) {
138     CBEndPaste();
139     }
140     }
141    
142     void CBStartPaste(HWND HWin, BOOL AddCR, BOOL Bracketed)
143     {
144 doda 6449 static char BracketStart[] = "\033[200~";
145     static char BracketEnd[] = "\033[201~";
146 maya 3393 UINT Cf;
147 doda 6449 PCHAR TmpPtr;
148     LPWSTR TmpPtrW;
149     HGLOBAL TmpHandle;
150     int BuffLen, BracketLen;
151 maya 3227
152 maya 3393 if (! cv.Ready) {
153     return;
154     }
155     if (TalkStatus!=IdTalkKeyb) {
156     return;
157     }
158 maya 3227
159 doda 6440 if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
160     Cf = CF_UNICODETEXT;
161 maya 3393 }
162 doda 6440 else if (IsClipboardFormatAvailable(CF_TEXT)) {
163     Cf = CF_TEXT;
164     }
165     else if (IsClipboardFormatAvailable(CF_OEMTEXT)) {
166     Cf = CF_OEMTEXT;
167     }
168     else {
169     return;
170     }
171 maya 3227
172 doda 3974 CBEchoOnly = FALSE;
173    
174 doda 6452 if (CBMemHandle) {
175     GlobalFree(CBMemHandle);
176     }
177 maya 3393 CBMemHandle = NULL;
178     CBMemPtr = NULL;
179     CBMemPtr2 = 0;
180 doda 5259
181 doda 6449 if (ts.PasteDelayPerLine > 0) {
182     CBInsertDelay = TRUE;
183     }
184     else {
185     CBInsertDelay = FALSE;
186     }
187    
188 doda 6440 CBRetrySend = FALSE;
189     CBRetryEcho = FALSE;
190     CBSendCR = FALSE;
191 maya 5071
192 doda 6440 if (OpenClipboard(HWin)) {
193 doda 6449 if ((TmpHandle = GetClipboardData(Cf)) != NULL) {
194     if (Cf == CF_UNICODETEXT) {
195     TmpPtrW = (LPWSTR)GlobalLock(TmpHandle);
196     BuffLen = WideCharToMultiByte(CP_ACP, 0, TmpPtrW, -1, 0, 0, NULL, NULL);
197     }
198     else {
199     TmpPtr = (PCHAR)GlobalLock(TmpHandle);
200     BuffLen = strlen(TmpPtr) + 1;
201     }
202 maya 5071
203 doda 6449 if (Bracketed) {
204     BuffLen += sizeof(BracketStart) + sizeof(BracketEnd);
205     }
206 maya 5071
207 doda 6449 if (AddCR) {
208     BuffLen++;
209     }
210    
211     if ((CBMemHandle = GlobalAlloc(GHND, BuffLen)) != NULL) {
212     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
213     if (Bracketed) {
214     strncpy_s(CBMemPtr, BuffLen, BracketStart, _TRUNCATE);
215     BracketLen = strlen(CBMemPtr);
216 maya 5071 }
217 doda 6449 else {
218     BracketLen = 0;
219     }
220 doda 6440
221 doda 6449 if (Cf == CF_UNICODETEXT) {
222     WideCharToMultiByte(CP_ACP, 0, TmpPtrW, -1, CBMemPtr+BracketLen, BuffLen-BracketLen, NULL, NULL);
223     }
224     else {
225     strncat_s(CBMemPtr, BuffLen, TmpPtr, _TRUNCATE);
226     }
227    
228     if (Bracketed) {
229     strncat_s(CBMemPtr, BuffLen, BracketEnd, _TRUNCATE);
230     }
231    
232     if (AddCR) {
233     strncat_s(CBMemPtr, BuffLen, "\r", _TRUNCATE);
234     }
235    
236     CBMemPtr = NULL;
237    
238     TalkStatus = IdTalkCB;
239 maya 5071 }
240 doda 6449 GlobalUnlock(CBMemHandle);
241     CBMemPtr = NULL;
242 maya 5071 }
243 doda 6449 GlobalUnlock(TmpHandle);
244 maya 3393 }
245 doda 6449 CloseClipboard();
246 maya 3393 }
247 doda 6440
248 maya 3393 if (TalkStatus != IdTalkCB) {
249     CBEndPaste();
250     }
251 maya 3227 }
252    
253 doda 4769 void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer)
254     {
255     HANDLE tmpHandle = NULL;
256     char *tmpPtr = NULL;
257 doda 6448 int len, header_len, footer_len, b64_len;
258 maya 5071 UINT Cf;
259     LPWSTR tmpPtrWide = NULL;
260 doda 4769
261     if (! cv.Ready) {
262     return;
263     }
264     if (TalkStatus!=IdTalkKeyb) {
265     return;
266     }
267    
268     CBEchoOnly = FALSE;
269    
270 doda 6452 if (CBMemHandle) {
271     GlobalFree(CBMemHandle);
272     }
273 doda 4769 CBMemHandle = NULL;
274     CBMemPtr = NULL;
275     CBMemPtr2 = 0;
276    
277 doda 5259 if (ts.PasteDelayPerLine > 0) {
278     CBInsertDelay = TRUE;
279     }
280     else {
281     CBInsertDelay = FALSE;
282     }
283    
284 doda 6449 CBRetrySend = FALSE;
285     CBRetryEcho = FALSE;
286     CBSendCR = FALSE;
287    
288 maya 5071 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(HWin)) {
289     Cf = CF_UNICODETEXT;
290     if ((tmpHandle = GetClipboardData(CF_UNICODETEXT)) == NULL) {
291     CloseClipboard();
292     }
293     }
294     else if (IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(HWin)) {
295     Cf = CF_TEXT;
296 doda 4769 if ((tmpHandle = GetClipboardData(CF_TEXT)) == NULL) {
297     CloseClipboard();
298     }
299     }
300     else if (IsClipboardFormatAvailable(CF_OEMTEXT) && OpenClipboard(HWin)) {
301 maya 5071 Cf = CF_OEMTEXT;
302 doda 4769 if ((tmpHandle = GetClipboardData(CF_OEMTEXT)) == NULL) {
303     CloseClipboard();
304     }
305     }
306    
307 doda 6448 if (tmpHandle) {
308     if (Cf == CF_UNICODETEXT) {
309 maya 5071 if ((tmpPtrWide = GlobalLock(tmpHandle)) != NULL) {
310 doda 6448 len = WideCharToMultiByte(CP_ACP, 0, tmpPtrWide, -1, 0, 0, NULL, NULL);
311     if ((tmpPtr = (char *)calloc(sizeof(char), len)) != NULL) {
312     WideCharToMultiByte(CP_ACP, 0, tmpPtrWide, -1, tmpPtr, len, NULL, NULL);
313 maya 5071 }
314 doda 6448 // WideCharToMultiByte �������������������� \0 ����������
315     // \0 ���G���R�[�h������������������ 1 ������
316     len--;
317 maya 5071 GlobalUnlock(tmpPtrWide);
318     }
319     }
320 doda 6448 else {
321 maya 5071 if ((tmpPtr = GlobalLock(tmpHandle)) != NULL) {
322     len = strlen(tmpPtr);
323 doda 6448 }
324     }
325    
326     if (tmpPtr) {
327     header_len = strlen(header);
328     footer_len = strlen(footer);
329    
330     b64_len = (len + 2) / 3 * 4 + header_len + footer_len + 1;
331    
332     if ((CBMemHandle = GlobalAlloc(GHND, b64_len)) != NULL) {
333     if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
334     if (header_len > 0) {
335     strncpy_s(CBMemPtr, b64_len, header, _TRUNCATE);
336 doda 4769 }
337 doda 6448 b64encode(CBMemPtr + header_len, b64_len - header_len, tmpPtr, len);
338     if (footer_len > 0) {
339     strncat_s(CBMemPtr, b64_len, footer, _TRUNCATE);
340     }
341     TalkStatus=IdTalkCB;
342     GlobalUnlock(CBMemPtr);
343     CBMemPtr = NULL;
344 doda 4769 }
345 doda 6448 }
346    
347     if (Cf == CF_UNICODETEXT) {
348     free(tmpPtr);
349     }
350     else {
351 maya 5071 GlobalUnlock(tmpPtr);
352 doda 4769 }
353     }
354 doda 6448 CloseClipboard();
355 doda 4769 }
356    
357     if (TalkStatus != IdTalkCB) {
358     CBEndPaste();
359     }
360     }
361    
362 maya 3227 // �����������N���b�v�{�[�h������DDE�f�[�^���[�������������B
363     //
364     // CBMemHandle�n���h�����O���[�o�������������A�����������I�������������A
365     // �����N���b�v�{�[�h������DDE�f�[�^�������������������������i�j�����������\�������j�B
366     // �����A�f�[�^���� null-terminate �����������������O�����������������A�������f�[�^����
367     // �����������B
368     // (2006.11.6 yutaka)
369     void CBSend()
370     {
371 maya 3393 int c;
372     BOOL EndFlag;
373 doda 3688 static DWORD lastcr;
374     DWORD now;
375 maya 3227
376 maya 3393 if (CBMemHandle==NULL) {
377     return;
378     }
379 maya 3227
380 doda 3974 if (CBEchoOnly) {
381     CBEcho();
382     return;
383     }
384    
385 doda 5259 if (CBInsertDelay) {
386     now = GetTickCount();
387     if (now - lastcr < (DWORD)ts.PasteDelayPerLine) {
388     return;
389     }
390 doda 3688 }
391    
392 maya 3393 if (CBRetrySend) {
393     CBRetryEcho = (ts.LocalEcho>0);
394     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
395     CBRetrySend = (c==0);
396     if (CBRetrySend) {
397     return;
398     }
399     }
400 maya 3227
401 maya 3393 if (CBRetryEcho) {
402     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
403     CBRetryEcho = (c==0);
404     if (CBRetryEcho) {
405     return;
406     }
407     }
408 maya 3227
409 maya 4009 CBMemPtr = GlobalLock(CBMemHandle);
410     if (CBMemPtr==NULL) {
411 maya 3393 return;
412     }
413 maya 3227
414 maya 3393 do {
415     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
416     CBMemPtr2++;
417     // added PasteDelayPerLine (2009.4.12 maya)
418 doda 5259 if (CBInsertDelay) {
419 doda 3690 lastcr = now;
420     CBSendCR = FALSE;
421     SetTimer(HVTWin, IdPasteDelayTimer, ts.PasteDelayPerLine, NULL);
422     break;
423     }
424 maya 3393 }
425    
426     EndFlag = (CBMemPtr[CBMemPtr2]==0);
427 doda 6449 if (! EndFlag) {
428 maya 3393 CBByte = CBMemPtr[CBMemPtr2];
429     CBMemPtr2++;
430 maya 3227 // Decoding characters which are encoded by MACRO
431     // to support NUL character sending
432     //
433     // [encoded character] --> [decoded character]
434     // 01 01 --> 00
435     // 01 02 --> 01
436 maya 3393 if (CBByte==0x01) { /* 0x01 from MACRO */
437     CBByte = CBMemPtr[CBMemPtr2];
438     CBMemPtr2++;
439     CBByte = CBByte - 1; // character just after 0x01
440     }
441     }
442     else {
443     CBEndPaste();
444     return;
445     }
446 maya 3227
447 maya 3393 if (! EndFlag) {
448     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
449     CBSendCR = (CBByte==0x0D);
450     CBRetrySend = (c==0);
451     if ((! CBRetrySend) &&
452     (ts.LocalEcho>0)) {
453     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
454     CBRetryEcho = (c==0);
455     }
456     }
457     else {
458     c=0;
459     }
460     }
461     while (c>0);
462 maya 3227
463 maya 4009 if (CBMemPtr!=NULL) {
464 maya 3393 GlobalUnlock(CBMemHandle);
465     CBMemPtr=NULL;
466     }
467 maya 3227 }
468    
469 doda 3974 void CBEcho()
470     {
471     if (CBMemHandle==NULL) {
472     return;
473     }
474    
475     if (CBRetryEcho && CommTextEcho(&cv,(PCHAR)&CBByte,1) == 0) {
476     return;
477     }
478    
479     if ((CBMemPtr = GlobalLock(CBMemHandle)) == NULL) {
480     return;
481     }
482    
483     do {
484     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
485     CBMemPtr2++;
486     }
487    
488     if (CBMemPtr[CBMemPtr2] == 0) {
489     CBRetryEcho = FALSE;
490     CBEndPaste();
491     return;
492     }
493    
494     CBByte = CBMemPtr[CBMemPtr2];
495     CBMemPtr2++;
496    
497     // Decoding characters which are encoded by MACRO
498     // to support NUL character sending
499     //
500     // [encoded character] --> [decoded character]
501     // 01 01 --> 00
502     // 01 02 --> 01
503     if (CBByte==0x01) { /* 0x01 from MACRO */
504     CBByte = CBMemPtr[CBMemPtr2];
505     CBMemPtr2++;
506     CBByte = CBByte - 1; // character just after 0x01
507     }
508    
509     CBSendCR = (CBByte==0x0D);
510    
511     } while (CommTextEcho(&cv,(PCHAR)&CBByte,1) > 0);
512    
513     CBRetryEcho = TRUE;
514    
515     if (CBMemHandle != NULL) {
516     GlobalUnlock(CBMemHandle);
517     CBMemPtr=NULL;
518     }
519     }
520    
521 maya 3227 void CBEndPaste()
522     {
523 maya 3393 TalkStatus = IdTalkKeyb;
524 maya 3227
525 maya 3393 if (CBMemHandle!=NULL) {
526     if (CBMemPtr!=NULL) {
527     GlobalUnlock(CBMemHandle);
528     }
529 doda 6449 GlobalFree(CBMemHandle);
530 maya 3393 }
531 maya 3227
532 maya 3393 CBMemHandle = NULL;
533     CBMemPtr = NULL;
534     CBMemPtr2 = 0;
535 doda 3974 CBEchoOnly = FALSE;
536 doda 5259 CBInsertDelay = FALSE;
537 maya 3227 }
538    
539 doda 6418 BOOL CBSetClipboard(HWND owner, HGLOBAL hMem)
540     {
541     char *buf;
542     int wide_len;
543     HGLOBAL wide_hMem;
544     LPWSTR wide_buf;
545 maya 3227
546 doda 6418 if (OpenClipboard(owner) == 0)
547     return FALSE;
548    
549     buf = GlobalLock(hMem);
550    
551     wide_len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
552     wide_hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * wide_len);
553     if (wide_hMem) {
554     wide_buf = (LPWSTR)GlobalLock(wide_hMem);
555     MultiByteToWideChar(CP_ACP, 0, buf, -1, wide_buf, wide_len);
556     GlobalUnlock(wide_hMem);
557     }
558    
559     GlobalUnlock(hMem);
560    
561     EmptyClipboard();
562     SetClipboardData(CF_TEXT, hMem);
563     if (wide_buf) {
564     SetClipboardData(CF_UNICODETEXT, wide_hMem);
565     }
566     CloseClipboard();
567    
568     return TRUE;
569     }
570    
571     HGLOBAL CBAllocClipboardMem(char *text)
572     {
573     HGLOBAL hMem;
574     char *buf;
575     int len;
576    
577     len = strlen(text);
578    
579     hMem = GlobalAlloc(GMEM_MOVEABLE, len+1);
580     if (hMem) {
581     buf = GlobalLock(hMem);
582     strncpy_s(buf, len+1, text, _TRUNCATE);
583     GlobalUnlock(hMem);
584     }
585    
586     return hMem;
587     }
588    
589 maya 3227 static char *ClipboardPtr = NULL;
590     static int PasteCanceled = 0;
591    
592     static LRESULT CALLBACK OnClipboardDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp)
593     {
594     LOGFONT logfont;
595     HFONT font;
596     char uimsg[MAX_UIMSG];
597     //char *p;
598     POINT p;
599     RECT rc_dsk, rc_dlg;
600     int dlg_height, dlg_width;
601 maya 3902 static int ok2right, edit2ok, edit2bottom;
602     RECT rc_edit, rc_ok, rc_cancel;
603 yutakapon 3635 // for status bar
604     static HWND hStatus = NULL;
605 maya 3641 static init_width, init_height;
606 maya 3227
607     switch (msg) {
608     case WM_INITDIALOG:
609     #if 0
610     for (p = ClipboardPtr; *p ; p++) {
611     char buf[20];
612     _snprintf_s(buf, sizeof(buf), _TRUNCATE, "%02x ", *p);
613     OutputDebugString(buf);
614     }
615     #endif
616    
617     font = (HFONT)SendMessage(hDlgWnd, WM_GETFONT, 0, 0);
618     GetObject(font, sizeof(LOGFONT), &logfont);
619     if (get_lang_font("DLG_TAHOMA_FONT", hDlgWnd, &logfont, &DlgClipboardFont, ts.UILanguageFile)) {
620     SendDlgItemMessage(hDlgWnd, IDC_EDIT, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
621     SendDlgItemMessage(hDlgWnd, IDOK, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
622     SendDlgItemMessage(hDlgWnd, IDCANCEL, WM_SETFONT, (WPARAM)DlgClipboardFont, MAKELPARAM(TRUE,0));
623     }
624     else {
625     DlgClipboardFont = NULL;
626     }
627    
628     GetWindowText(hDlgWnd, uimsg, sizeof(uimsg));
629     get_lang_msg("DLG_CLIPBOARD_TITLE", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
630     SetWindowText(hDlgWnd, ts.UIMsg);
631     GetDlgItemText(hDlgWnd, IDCANCEL, uimsg, sizeof(uimsg));
632     get_lang_msg("BTN_CANCEL", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
633     SetDlgItemText(hDlgWnd, IDCANCEL, ts.UIMsg);
634     GetDlgItemText(hDlgWnd, IDOK, uimsg, sizeof(uimsg));
635     get_lang_msg("BTN_OK", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
636     SetDlgItemText(hDlgWnd, IDOK, ts.UIMsg);
637    
638     SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_SETTEXT, 0, (LPARAM)ClipboardPtr);
639    
640     DispConvScreenToWin(CursorX, CursorY, &p.x, &p.y);
641     ClientToScreen(HVTWin, &p);
642    
643     // �L�����b�g���������������o���������������\���t����������
644     // �m�F�E�C���h�E�����������������\���������������������B
645     // �E�C���h�E���������o������������������ (2008.4.24 maya)
646 yutakapon 6286 if (!HasMultiMonitorSupport()) {
647 maya 3509 // NT4.0, 95 ���}���`���j�^API��������
648 maya 3227 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc_dsk, 0);
649     }
650     else {
651     HMONITOR hm;
652     POINT pt;
653     MONITORINFO mi;
654    
655     pt.x = p.x;
656     pt.y = p.y;
657     hm = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
658    
659     mi.cbSize = sizeof(MONITORINFO);
660     GetMonitorInfo(hm, &mi);
661     rc_dsk = mi.rcWork;
662     }
663     GetWindowRect(hDlgWnd, &rc_dlg);
664     dlg_height = rc_dlg.bottom-rc_dlg.top;
665     dlg_width = rc_dlg.right-rc_dlg.left;
666     if (p.y < rc_dsk.top) {
667     p.y = rc_dsk.top;
668     }
669     else if (p.y + dlg_height > rc_dsk.bottom) {
670     p.y = rc_dsk.bottom - dlg_height;
671     }
672     if (p.x < rc_dsk.left) {
673     p.x = rc_dsk.left;
674     }
675     else if (p.x + dlg_width > rc_dsk.right) {
676     p.x = rc_dsk.right - dlg_width;
677     }
678    
679     SetWindowPos(hDlgWnd, NULL, p.x, p.y,
680     0, 0, SWP_NOSIZE | SWP_NOZORDER);
681    
682 maya 3641 // �_�C�A���O�������T�C�Y������
683     GetWindowRect(hDlgWnd, &rc_dlg);
684     init_width = rc_dlg.right - rc_dlg.left;
685     init_height = rc_dlg.bottom - rc_dlg.top;
686    
687 maya 3227 // �����T�C�Y�����K�v���l���v�Z
688     GetClientRect(hDlgWnd, &rc_dlg);
689     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
690     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
691    
692     p.x = rc_dlg.right;
693     p.y = rc_dlg.bottom;
694     ClientToScreen(hDlgWnd, &p);
695     ok2right = p.x - rc_ok.left;
696 maya 3902 edit2bottom = p.y - rc_edit.bottom;
697 maya 3227 edit2ok = rc_ok.left - rc_edit.right;
698    
699     // �T�C�Y������
700     SetWindowPos(hDlgWnd, NULL, 0, 0,
701     ts.PasteDialogSize.cx, ts.PasteDialogSize.cy,
702     SWP_NOZORDER | SWP_NOMOVE);
703    
704 yutakapon 3635 // ���T�C�Y�A�C�R�����E�����\���������������A�X�e�[�^�X�o�[���t�����B
705     InitCommonControls();
706     hStatus = CreateStatusWindow(
707     WS_CHILD | WS_VISIBLE |
708     CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hDlgWnd, 1);
709    
710 maya 3227 return TRUE;
711    
712     case WM_COMMAND:
713     switch (LOWORD(wp)) {
714     case IDOK:
715     {
716     int len = SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXTLENGTH, 0, 0);
717     HGLOBAL hMem;
718     char *buf;
719    
720 doda 6418 hMem = GlobalAlloc(GMEM_MOVEABLE, len + 1);
721     if (hMem) {
722 maya 3902 buf = GlobalLock(hMem);
723     SendMessage(GetDlgItem(hDlgWnd, IDC_EDIT), WM_GETTEXT, len + 1, (LPARAM)buf);
724 doda 6417 GlobalUnlock(hMem);
725    
726 doda 6418 if (! CBSetClipboard(hDlgWnd, hMem)) {
727     // �N���b�v�{�[�h�����Z�b�g�����s�������� hMem ���j������
728     // ���������������N���b�v�{�[�h�������������������A�j����������������
729     GlobalFree(hMem);
730 maya 5071 }
731 maya 3902 }
732 maya 3227
733     if (DlgClipboardFont != NULL) {
734     DeleteObject(DlgClipboardFont);
735     }
736    
737 yutakapon 3635 DestroyWindow(hStatus);
738 maya 3227 EndDialog(hDlgWnd, IDOK);
739     }
740     break;
741    
742     case IDCANCEL:
743     PasteCanceled = 1;
744    
745     if (DlgClipboardFont != NULL) {
746     DeleteObject(DlgClipboardFont);
747     }
748    
749 yutakapon 3635 DestroyWindow(hStatus);
750 maya 3227 EndDialog(hDlgWnd, IDCANCEL);
751     break;
752    
753     default:
754     return FALSE;
755     }
756    
757     #if 0
758     case WM_CLOSE:
759     PasteCanceled = 1;
760     EndDialog(hDlgWnd, 0);
761     return TRUE;
762     #endif
763    
764     case WM_SIZE:
765     {
766     // ���z�u
767     POINT p;
768     int dlg_w, dlg_h;
769    
770     GetClientRect(hDlgWnd, &rc_dlg);
771     dlg_w = rc_dlg.right;
772     dlg_h = rc_dlg.bottom;
773    
774     GetWindowRect(GetDlgItem(hDlgWnd, IDC_EDIT), &rc_edit);
775     GetWindowRect(GetDlgItem(hDlgWnd, IDOK), &rc_ok);
776     GetWindowRect(GetDlgItem(hDlgWnd, IDCANCEL), &rc_cancel);
777    
778     // OK
779     p.x = rc_ok.left;
780     p.y = rc_ok.top;
781     ScreenToClient(hDlgWnd, &p);
782     SetWindowPos(GetDlgItem(hDlgWnd, IDOK), 0,
783     dlg_w - ok2right, p.y, 0, 0,
784     SWP_NOSIZE | SWP_NOZORDER);
785    
786     // CANCEL
787     p.x = rc_cancel.left;
788     p.y = rc_cancel.top;
789     ScreenToClient(hDlgWnd, &p);
790     SetWindowPos(GetDlgItem(hDlgWnd, IDCANCEL), 0,
791     dlg_w - ok2right, p.y, 0, 0,
792     SWP_NOSIZE | SWP_NOZORDER);
793    
794     // EDIT
795     p.x = rc_edit.left;
796     p.y = rc_edit.top;
797     ScreenToClient(hDlgWnd, &p);
798     SetWindowPos(GetDlgItem(hDlgWnd, IDC_EDIT), 0,
799 maya 3902 0, 0, dlg_w - p.x - edit2ok - ok2right, dlg_h - p.y - edit2bottom,
800 maya 3227 SWP_NOMOVE | SWP_NOZORDER);
801    
802     // �T�C�Y������
803     GetWindowRect(hDlgWnd, &rc_dlg);
804     ts.PasteDialogSize.cx = rc_dlg.right - rc_dlg.left;
805     ts.PasteDialogSize.cy = rc_dlg.bottom - rc_dlg.top;
806 yutakapon 3635
807     // status bar
808     SendMessage(hStatus , msg , wp , lp);
809 maya 3227 }
810     return TRUE;
811    
812 maya 3641 case WM_GETMINMAXINFO:
813     {
814     // �_�C�A���O�������T�C�Y����������������������������
815     LPMINMAXINFO lpmmi;
816     lpmmi = (LPMINMAXINFO)lp;
817     lpmmi->ptMinTrackSize.x = init_width;
818     lpmmi->ptMinTrackSize.y = init_height;
819     }
820     return FALSE;
821    
822 maya 3227 default:
823     return FALSE;
824     }
825     return TRUE;
826     }
827    
828 yutakapon 3535 // �t�@�C�������`���������������Atext���������������������B
829     static int search_clipboard(char *filename, char *text)
830     {
831     int ret = 0; // no hit
832     FILE *fp = NULL;
833     char buf[256];
834     int len;
835 doda 6435
836 yutakapon 3535 if (filename == NULL || filename[0] == '\0')
837     goto error;
838    
839     fp = fopen(filename, "r");
840     if (fp == NULL)
841     goto error;
842    
843     // TODO: ���s��256byte�������������������l��
844     while (fgets(buf, sizeof(buf), fp) != NULL) {
845     len = strlen(buf);
846 doda 6435 if (buf[len - 1] == '\n')
847 yutakapon 3535 buf[len - 1] = '\0';
848     if (buf[0] == '\0')
849     continue;
850     if (strstr(text, buf)) { // hit
851     ret = 1;
852     break;
853     }
854     }
855    
856     error:
857     if (fp)
858     fclose(fp);
859    
860     return (ret);
861     }
862    
863    
864 maya 3227 //
865     // �N���b�v�{�[�h�����s�R�[�h�����������������A�m�F�_�C�A���O���\�������B
866     // �N���b�v�{�[�h�����X�����\�B
867     //
868     // return 0: Cancel
869     // 1: Paste OK
870     //
871     // (2008.2.3 yutaka)
872     //
873 doda 4260 int CBStartPasteConfirmChange(HWND HWin, BOOL AddCR)
874 maya 3227 {
875     UINT Cf;
876     HANDLE hText;
877 doda 6418 char *pText, *tail;
878 maya 3227 int pos;
879     int ret = 0;
880 doda 6418 BOOL confirm = FALSE, need_writeback = FALSE;
881 maya 5071 HANDLE wide_hText;
882     LPWSTR wide_buf;
883     int mb_len;
884 maya 3227
885 doda 6418 if (!ts.ConfirmChangePaste && !ts.TrimTrailingNLonPaste)
886 maya 3227 return 1;
887    
888 doda 6435 if (! cv.Ready)
889 maya 3227 goto error;
890     if (TalkStatus!=IdTalkKeyb)
891     goto error;
892    
893 maya 5071 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
894     Cf = CF_UNICODETEXT;
895     else if (IsClipboardFormatAvailable(CF_TEXT))
896 maya 3227 Cf = CF_TEXT;
897     else if (IsClipboardFormatAvailable(CF_OEMTEXT))
898     Cf = CF_OEMTEXT;
899 doda 6435 else
900 maya 3227 goto error;
901    
902 doda 6435 if (!OpenClipboard(HWin))
903 maya 3227 goto error;
904    
905 maya 5071 if (Cf == CF_UNICODETEXT) {
906     wide_hText = GetClipboardData(CF_UNICODETEXT);
907     if (wide_hText != NULL) {
908     wide_buf = GlobalLock(wide_hText);
909     mb_len = WideCharToMultiByte(CP_ACP, 0, wide_buf, -1, NULL, 0, NULL, NULL);
910     ClipboardPtr = (char *)calloc(sizeof(char), mb_len);
911     WideCharToMultiByte(CP_ACP, 0, wide_buf, -1, ClipboardPtr, mb_len, NULL, NULL);
912     GlobalUnlock(wide_hText);
913 doda 4273 }
914     else {
915 maya 5071 CloseClipboard();
916     goto error;
917 doda 4273 }
918 maya 5071 }
919     else {
920     hText = GetClipboardData(Cf);
921     if (hText != NULL) {
922     pText = (char *)GlobalLock(hText);
923 maya 3902 ClipboardPtr = (char *)calloc(sizeof(char), strlen(pText)+1);
924     memcpy(ClipboardPtr, pText, strlen(pText));
925     GlobalUnlock(hText);
926 maya 3227 }
927 maya 3902 else {
928     CloseClipboard();
929 maya 5071 goto error;
930 maya 3902 }
931 maya 5071 }
932     CloseClipboard();
933 maya 3227
934 doda 6418 if (ts.TrimTrailingNLonPaste) {
935     for (tail=ClipboardPtr+strlen(ClipboardPtr)-1; tail >= ClipboardPtr; tail--) {
936     if (*tail == '\r' || *tail == '\n') {
937     *tail = '\0';
938     need_writeback = TRUE;
939     }
940     else {
941     break;
942     }
943     }
944     }
945    
946 maya 5071 if (AddCR) {
947     if (ts.ConfirmChangePasteCR) {
948     confirm = TRUE;
949     }
950     }
951     else {
952     pos = strcspn(ClipboardPtr, "\r\n"); // ���s����������������
953     if (ClipboardPtr[pos] != '\0' || AddCR) {
954     confirm = TRUE;
955     }
956     }
957 maya 3227
958 maya 5071 // �������T�[�`����
959     if (!confirm && search_clipboard(ts.ConfirmChangePasteStringFile, ClipboardPtr)) {
960     confirm = TRUE;
961 maya 3227 }
962    
963 maya 5071 if (confirm) {
964     PasteCanceled = 0;
965     ret = DialogBox(hInst, MAKEINTRESOURCE(IDD_CLIPBOARD_DIALOG),
966     HVTWin, (DLGPROC)OnClipboardDlgProc);
967     if (ret == 0 || ret == -1) {
968     ret = GetLastError();
969     }
970    
971     if (PasteCanceled) {
972     ret = 0;
973     goto error;
974     }
975     }
976 doda 6418 else if (need_writeback) {
977     HGLOBAL hMem;
978     hMem = CBAllocClipboardMem(ClipboardPtr);
979     if (hMem) {
980     if (! CBSetClipboard(NULL, hMem)) {
981     // �N���b�v�{�[�h�����Z�b�g�����s�������� hMem ���j������
982     // ���������������N���b�v�{�[�h�������������������A�j����������������
983     GlobalFree(hMem);
984     }
985     }
986     }
987 maya 5071
988     ret = 1;
989    
990 maya 3227 error:
991 maya 5071 if (ClipboardPtr != NULL) {
992     free(ClipboardPtr);
993     ClipboardPtr = NULL;
994     }
995 maya 3227 return (ret);
996     }

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