00001
00002
00003 #include "clipboard.h"
00004 #include "Messages.h"
00005 #include "Warnings.h"
00006 #include "Errors.h"
00007 #include "wgnuplib.h"
00008 #include "WindowList.h"
00009 #include "win_mem_alloc.h"
00010
00011
00012 static BOOL ThreadPasteRunning=FALSE;
00013 static HANDLE hThreadPaste;
00014 static char *PasteForThread=NULL;
00015
00016 static BOOL ReadyForAnewLign=FALSE;
00017
00018
00019
00020
00021
00022 static BOOL SpecialPaste=FALSE;
00023
00024 extern HDC TryToGetDC(HWND hWnd);
00025 extern LPTW GetTextWinScilab(void);
00026 extern int IsAScalar(int RhsNumber);
00027
00028 void CreateThreadPaste(char *Text)
00029 {
00030 DWORD IdThreadPaste;
00031 PasteForThread=(char*)MALLOC( (strlen(Text)+1)*sizeof(char));
00032 strcpy(PasteForThread,Text);
00033 PasteForThread[strlen(PasteForThread)]='\0';
00034
00035 hThreadPaste=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)SendInputText,NULL,0,(LPDWORD)&IdThreadPaste);
00036
00037 }
00038
00039 BOOL IsReadyForAnewLign(void)
00040 {
00041 return ReadyForAnewLign;
00042 }
00043
00044 void SetReadyOrNotForAnewLign(BOOL Ready)
00045 {
00046 ReadyForAnewLign=Ready;
00047 }
00048
00049 DWORD WINAPI SendInputText(LPVOID lpParam )
00050
00051 {
00052 #define TEMPOTOUCHE 1
00053 int lg=0;
00054 int i=0;
00055
00056 char *TextToSend=NULL;
00057
00058 LPTW lptw=GetTextWinScilab();
00059
00060 ThreadPasteRunning=TRUE;
00061
00062 lg=strlen((char*)PasteForThread)+1;
00063 TextToSend=(char*)MALLOC(lg*sizeof(char));
00064 strcpy(TextToSend,(char*)PasteForThread);
00065 TextToSend[strlen(TextToSend)]='\0';
00066
00067 FREE(PasteForThread);
00068 PasteForThread=NULL;
00069
00070
00071
00072 while ( C2F (ismenu) () == 1 ) {Sleep(TEMPOTOUCHE);}
00073
00074 while ( lptw->bGetCh == FALSE ) {Sleep(TEMPOTOUCHE);}
00075
00076
00077
00078 lg=strlen(TextToSend);
00079 i=0;
00080 while(i<lg)
00081 {
00082 long count;
00083
00084
00085 count = lptw->KeyBufIn - lptw->KeyBufOut;
00086
00087 if (count < 0) count = count+lptw->KeyBufSize;
00088 if (count < (long) (lptw->KeyBufSize-1))
00089 {
00090 if (TextToSend[i] == '\t') *lptw->KeyBufIn++ = ' ';
00091 else *lptw->KeyBufIn++ = TextToSend[i];
00092 if (lptw->KeyBufIn - lptw->KeyBuf >= (signed)lptw->KeyBufSize)
00093 lptw->KeyBufIn = lptw->KeyBuf;
00094 }
00095
00096 if (TextToSend[i]==10)
00097 {
00098
00099
00100
00101 *lptw->KeyBufIn = 13;
00102 SetReadyOrNotForAnewLign(FALSE);
00103 while ( IsReadyForAnewLign() == FALSE ) {Sleep(TEMPOTOUCHE);}
00104 while ( C2F (ismenu) () == 1 ) {Sleep(TEMPOTOUCHE);}
00105 while ( lptw->bGetCh == FALSE ) {Sleep(TEMPOTOUCHE);}
00106 }
00107 if (TextToSend[i]==13)
00108 {
00109 SetReadyOrNotForAnewLign(FALSE);
00110 while ( IsReadyForAnewLign() == FALSE ) {Sleep(TEMPOTOUCHE);}
00111 while ( C2F (ismenu) () == 1 ) {Sleep(TEMPOTOUCHE);}
00112 while ( lptw->bGetCh == FALSE ) {Sleep(TEMPOTOUCHE);}
00113 if (i<lg-1)
00114 {
00115
00116 if (TextToSend[i+1]==10) i++;
00117 }
00118 }
00119 i++;
00120
00121 }
00122
00123
00124 FREE(TextToSend);
00125 TextToSend=NULL;
00126 ThreadPasteRunning=FALSE;
00127 CloseHandle( hThreadPaste );
00128
00129 return 0;
00130 }
00131
00132 void PasteFunction(LPTW lptw,BOOL special)
00133
00134
00135
00136
00137
00138 {
00139 HDC hdc;
00140 HGLOBAL hGMem;
00141 TEXTMETRIC tm;
00142 UINT type;
00143 LPSTR lpMem;
00144
00145 SpecialPaste=special;
00146
00147 hdc = (HDC)TryToGetDC (lptw->hWndText);
00148 SelectObject(hdc, lptw->hfont);
00149 GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
00150 if (tm.tmCharSet == OEM_CHARSET) type = CF_OEMTEXT;
00151 else type = CF_TEXT;
00152 ReleaseDC (lptw->hWndText, hdc);
00153
00154
00155 OpenClipboard (lptw->hWndText);
00156 hGMem = GetClipboardData (type);
00157
00158 if (hGMem)
00159 {
00160 lpMem = GlobalLock( hGMem );
00161 if (!ThreadPasteRunning) SendMessage(lptw->hWndText, WM_SETTEXT, 0,(LPARAM) lpMem);
00162 GlobalUnlock (hGMem);
00163 }
00164 CloseClipboard ();
00165 }
00166
00167 BOOL IsEmptyClipboard(LPTW lptw)
00168
00169 {
00170 BOOL Retour=TRUE;
00171 HDC hdc;
00172 HGLOBAL hGMem;
00173 TEXTMETRIC tm;
00174 UINT type;
00175 LPSTR lpMem=NULL;
00176
00177
00178
00179 hdc = (HDC)TryToGetDC (lptw->hWndText);
00180 SelectObject(hdc, lptw->hfont);
00181 GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
00182 if (tm.tmCharSet == OEM_CHARSET) type = CF_OEMTEXT;
00183 else type = CF_TEXT;
00184 ReleaseDC (lptw->hWndText, hdc);
00185
00186
00187 OpenClipboard (lptw->hWndText);
00188 hGMem = GetClipboardData (type);
00189
00190 if (hGMem)
00191 {
00192 lpMem = GlobalLock( hGMem );
00193 if (lpMem==NULL) Retour=TRUE;
00194 else Retour=FALSE;
00195 GlobalUnlock (hGMem);
00196 }
00197 CloseClipboard ();
00198
00199 return Retour;
00200 }
00201
00202
00203
00204
00205
00206 void TextCopyClip(LPTW lptw)
00207 {
00208 int size, count;
00209 HGLOBAL hGMem;
00210 LPSTR cbuf, cp;
00211 POINT pt, end;
00212 TEXTMETRIC tm;
00213 UINT type;
00214 HDC hdc;
00215
00216 if ((lptw->MarkBegin.x == lptw->MarkEnd.x) &&
00217 (lptw->MarkBegin.y == lptw->MarkEnd.y) ) {
00218
00219 return;
00220 }
00221
00222 size = (lptw->MarkEnd.y - lptw->MarkBegin.y + 1)
00223 * (lptw->ScreenSize.x + 2) + 1;
00224 hGMem = GlobalAlloc(GMEM_MOVEABLE, (DWORD)size);
00225 cbuf = cp = (LPSTR)GlobalLock(hGMem);
00226 if (cp == (LPSTR)NULL)
00227 return;
00228
00229 pt.x = lptw->MarkBegin.x;
00230 pt.y = lptw->MarkBegin.y;
00231 end.x = lptw->MarkEnd.x;
00232 end.y = lptw->MarkEnd.y;
00233
00234 while (pt.y < end.y) {
00235
00236 count = lptw->ScreenSize.x - pt.x;
00237 _fmemcpy(cp, lptw->ScreenBuffer + lptw->ScreenSize.x*pt.y+pt.x, count);
00238
00239 for (count=count-1; count>=0; count--) {
00240 if (cp[count]!=' ')
00241 break;
00242 cp[count] = '\0';
00243 }
00244 cp[++count] = '\r';
00245 cp[++count] = '\n';
00246 cp[++count] = '\0';
00247 cp += count;
00248 pt.y++;
00249 pt.x=0;
00250 }
00251
00252 count = end.x - pt.x;
00253 if (end.y != lptw->ScreenSize.y) {
00254 _fmemcpy(cp, lptw->ScreenBuffer + lptw->ScreenSize.x*pt.y+pt.x, count);
00255 cp[count] = '\0';
00256 }
00257 size = _fstrlen(cbuf) + 1;
00258 GlobalUnlock(hGMem);
00259 hGMem = GlobalReAlloc(hGMem, (DWORD)size, GMEM_MOVEABLE);
00260
00261 hdc = (HDC)TryToGetDC(lptw->hWndText);
00262 SelectObject(hdc, lptw->hfont);
00263 GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
00264 if (tm.tmCharSet == OEM_CHARSET)
00265 type = CF_OEMTEXT;
00266 else
00267 type = CF_TEXT;
00268 ReleaseDC(lptw->hWndText, hdc);
00269
00270 OpenClipboard(lptw->hWndParent);
00271 EmptyClipboard();
00272 SetClipboardData(type, hGMem);
00273 CloseClipboard();
00274 }
00275
00276 void CleanClipboard(LPTW lptw)
00277 {
00278 if (ThreadPasteRunning)
00279 {
00280 SuspendThread(hThreadPaste);
00281 TerminateThread(hThreadPaste,1);
00282 ThreadPasteRunning=FALSE;
00283 CloseHandle( hThreadPaste );
00284 }
00285
00286 OpenClipboard(lptw->hWndParent);
00287 EmptyClipboard();
00288 CloseClipboard();
00289 }
00290
00291 HANDLE GetHandleThreadPaste(void)
00292 {
00293 return hThreadPaste;
00294 }
00295
00296 BOOL GetThreadPasteRunning(void)
00297 {
00298 return ThreadPasteRunning;
00299 }
00300
00301 void SetThreadPasteRunning(BOOL Running)
00302 {
00303 ThreadPasteRunning=Running;
00304 }
00305
00306 int InterfaceWindowsClipboard(char *fname,unsigned long l)
00307 {
00308 static int l1,n1,m1;
00309 char *param1=NULL,*param2=NULL,*param3=NULL;
00310
00311 Rhs=Max(0,Rhs);
00312 CheckRhs(0,2);
00313 CheckLhs(0,1);
00314
00315 if ( IsWindowInterface() )
00316 {
00317 if ( (Rhs == 1) && (GetType(1)==sci_strings) )
00318 {
00319 GetRhsVar(1,"c",&m1,&n1,&l1);
00320 param1=cstk(l1);
00321
00322 if ( ( strcmp(param1,"paste") == 0 ) || ( strcmp(param1,"pastespecial") == 0 ) )
00323 {
00324 char *output=NULL ;
00325 LPTW lptw=GetTextWinScilab();
00326
00327 output=GetTextFromClipboard(lptw);
00328
00329 if (output)
00330 {
00331 CreateVarFromPtr( 1, "c",(m1=strlen(output), &m1),&n1,&output);
00332 FREE(output);
00333 LhsVar(1)=1;
00334 }
00335 else
00336 {
00337 m1=0;
00338 n1=0;
00339 l1=0;
00340 CreateVar(1,"d", &m1, &n1, &l1);
00341 LhsVar(1)=1;
00342 }
00343
00344 C2F(putlhsvar)();
00345 return 0;
00346 }
00347 else
00348 {
00349 Scierror(999,MSG_ERROR7);
00350 return 0;
00351 }
00352 }
00353
00354 if (Rhs == 2)
00355 {
00356 if ( (GetType(1)==sci_strings) && (GetType(2)==sci_strings) )
00357 {
00358 GetRhsVar(1,"c",&m1,&n1,&l1);
00359 param1=cstk(l1);
00360
00361 if ( strcmp(param1,"do") == 0 )
00362 {
00363 GetRhsVar(2,"c",&m1,&n1,&l1);
00364 param2=cstk(l1);
00365
00366 if ( strcmp(param2,"paste") == 0 )
00367 {
00368 Callback_PASTE();
00369 }
00370 else if ( strcmp(param2,"copy") == 0 )
00371 {
00372 Callback_MCOPY();
00373 }
00374 else if ( strcmp(param2,"empty") == 0 )
00375 {
00376 Callback_EMPTYCLIPBOARD();
00377 }
00378 else
00379 {
00380 Scierror(999,"Incorrect second parameter.");
00381 return 0;
00382 }
00383
00384 m1=0;
00385 n1=0;
00386 l1=0;
00387 CreateVar(1,"d", &m1, &n1, &l1);
00388 LhsVar(1)=1;
00389 C2F(putlhsvar)();
00390 return 0;
00391 }
00392
00393 if ( strcmp(param1,"copy") == 0 )
00394 {
00395 char *TextToPutInClipboard=NULL;
00396 char **Str=NULL;
00397 LPTW lptw=GetTextWinScilab();
00398
00399 GetRhsVar(2,"S",&m1,&n1,&Str);
00400
00401
00402 if ( (m1==1) && (n1==1) )
00403 {
00404 TextToPutInClipboard=Str[0];
00405 PutTextInClipboard(lptw,TextToPutInClipboard);
00406 }
00407 else
00408 {
00409 int i=0;
00410 int j=0;
00411 int l=0;
00412 char *TextToSendInClipboard=NULL;
00413 int SizeofTextToSendInClipboard=0;
00414 char **buffer = (char**)MALLOC( (m1*n1)*sizeof(char *) );
00415
00416 for (i=0; i<m1; i++) for (j=0; j<n1; j++)
00417 {
00418 SizeofTextToSendInClipboard=SizeofTextToSendInClipboard+strlen(Str[j*m1+i])+strlen(" \n");
00419 buffer[i*n1+j]=(char*)MALLOC(strlen(Str[j*m1+i])+1);
00420 sprintf(buffer[i*n1+j],"%s",Str[j*m1+i]);
00421 }
00422
00423 TextToSendInClipboard=(char*)MALLOC( (SizeofTextToSendInClipboard)*sizeof(char) );
00424 strcpy(TextToSendInClipboard,"");
00425
00426 for (i=0; i<m1; i++)
00427 {
00428 for (j=0; j<n1; j++)
00429 {
00430 strcat(TextToSendInClipboard,buffer[l++]);
00431 strcat(TextToSendInClipboard," ");
00432 }
00433 if ( i != (m1-1) ) strcat(TextToSendInClipboard,"\n");
00434 }
00435
00436 PutTextInClipboard(lptw,TextToSendInClipboard);
00437
00438 if (buffer) {FREE(buffer);buffer=NULL;}
00439
00440 if(TextToSendInClipboard) {FREE(TextToSendInClipboard);TextToSendInClipboard=NULL;}
00441 }
00442
00443 m1=0;
00444 n1=0;
00445 l1=0;
00446 CreateVar(1,"d", &m1, &n1, &l1);
00447 LhsVar(1)=1;
00448 C2F(putlhsvar)();
00449 return 0;
00450 }
00451 else
00452 {
00453 Scierror(999,"Incorrect parameters. See : help clipboard");
00454 return 0;
00455 }
00456 }
00457
00458 if ( (IsAScalar(1)) && (GetType(2)==sci_strings) )
00459 {
00460 GetRhsVar(2,"c",&m1,&n1,&l1);
00461 param2=cstk(l1);
00462
00463 if ( ( strcmp(param2,"EMF") == 0 ) || ( strcmp(param2,"DIB") == 0 ) )
00464 {
00465 int num_win=-2;
00466 GetRhsVar(1,"i",&m1,&n1,&l1);
00467 num_win=*istk(l1);
00468 if (num_win>=0)
00469 {
00470 struct BCG *ScilabGC=NULL;
00471 ScilabGC = getWindowXgcNumber (num_win);
00472
00473 if (ScilabGC)
00474 {
00475 if ( strcmp(param2,"EMF") == 0 )
00476 {
00477 CopyToClipboardEMF (ScilabGC);
00478 }
00479 else
00480 {
00481 CopyToClipboardBitmap (ScilabGC);
00482 }
00483
00484 m1=0;
00485 n1=0;
00486 l1=0;
00487 CreateVar(1,"d", &m1, &n1, &l1);
00488 LhsVar(1)=1;
00489 C2F(putlhsvar)();
00490 return 0;
00491 }
00492 else
00493 {
00494 Scierror(999,"Invalid Windows number.");
00495 return 0;
00496 }
00497 }
00498 else
00499 {
00500 Scierror(999,"parameter must be >= 0.");
00501 return 0;
00502 }
00503 }
00504 else
00505 {
00506 Scierror(999,"Second parameter must be 'EMF' or 'DIB'.");
00507 return 0;
00508 }
00509 }
00510 }
00511 else
00512 {
00513 Scierror(999,"Incorrect parameters. See : help clipboard");
00514 return 0;
00515 }
00516 }
00517 else
00518 {
00519 if ( (Rhs == 2) && (IsAScalar(1)) && (GetType(2)==sci_strings) )
00520 {
00521 GetRhsVar(2,"c",&m1,&n1,&l1);
00522 param2=cstk(l1);
00523 if ( ( strcmp(param2,"EMF") == 0 ) || ( strcmp(param2,"DIB") == 0 ) )
00524 {
00525 int num_win=-2;
00526 GetRhsVar(1,"i",&m1,&n1,&l1);
00527 num_win=*istk(l1);
00528 if (num_win>=0)
00529 {
00530 struct BCG *ScilabGC=NULL;
00531 ScilabGC = getWindowXgcNumber (num_win);
00532
00533 if (ScilabGC)
00534 {
00535 if ( strcmp(param2,"EMF") == 0 )
00536 {
00537 CopyToClipboardEMF (ScilabGC);
00538 }
00539 else
00540 {
00541 CopyToClipboardBitmap (ScilabGC);
00542 }
00543
00544 m1=0;
00545 n1=0;
00546 l1=0;
00547 CreateVar(1,"d", &m1, &n1, &l1);
00548 LhsVar(1)=1;
00549 C2F(putlhsvar)();
00550 return 0;
00551 }
00552 else
00553 {
00554 Scierror(999,"Invalid Windows number.");
00555 return 0;
00556 }
00557 }
00558 else
00559 {
00560 Scierror(999,"parameter must be >= 0.");
00561 return 0;
00562 }
00563 }
00564 else
00565 {
00566 Scierror(999,"Second parameter must be 'EMF' or 'DIB'.");
00567 return 0;
00568 }
00569 }
00570 else
00571 {
00572 Scierror(999,MSG_ERROR10);
00573 return 0;
00574 }
00575 Scierror(999,MSG_ERROR10);
00576 return 0;
00577 }
00578 return 0;
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845 }
00846
00847 char * GetTextFromClipboard(LPTW lptw)
00848 {
00849 char *Text=NULL;
00850
00851 HDC hdc;
00852 HGLOBAL hGMem;
00853 TEXTMETRIC tm;
00854 UINT type;
00855 LPSTR lpMem;
00856
00857
00858
00859 hdc = (HDC)TryToGetDC (lptw->hWndText);
00860 SelectObject(hdc, lptw->hfont);
00861 GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
00862 if (tm.tmCharSet == OEM_CHARSET) type = CF_OEMTEXT;
00863 else type = CF_TEXT;
00864 ReleaseDC (lptw->hWndText, hdc);
00865
00866
00867 OpenClipboard (lptw->hWndText);
00868 hGMem = GetClipboardData (type);
00869
00870 if (hGMem)
00871 {
00872 lpMem = GlobalLock( hGMem );
00873 Text= (char*) MALLOC (sizeof(char)*(strlen(lpMem)+1));
00874 strcpy(Text,lpMem);
00875 GlobalUnlock (hGMem);
00876 }
00877 CloseClipboard ();
00878
00879 return Text;
00880 }
00881
00882 void PutTextInClipboard(LPTW lptw,char *Text)
00883 {
00884 int size;
00885 HGLOBAL hGMem;
00886 LPSTR cbuf;
00887
00888 TEXTMETRIC tm;
00889 UINT type;
00890 HDC hdc;
00891
00892 size=strlen(Text)+1;
00893
00894 hGMem = GlobalAlloc(GMEM_MOVEABLE, (DWORD)size);
00895 cbuf = (LPSTR)GlobalLock(hGMem);
00896 if (cbuf == (LPSTR)NULL) return;
00897 strcpy(cbuf,Text);
00898
00899
00900 hdc = (HDC)TryToGetDC(lptw->hWndText);
00901 SelectObject(hdc, lptw->hfont);
00902 GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
00903 if (tm.tmCharSet == OEM_CHARSET)
00904 type = CF_OEMTEXT;
00905 else
00906 type = CF_TEXT;
00907 ReleaseDC(lptw->hWndText, hdc);
00908
00909
00910 OpenClipboard(lptw->hWndParent);
00911 EmptyClipboard();
00912 SetClipboardData(type, hGMem);
00913 CloseClipboard();
00914 }
00915