InitTclTk.c

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------------*/
00002 /* INRIA 2005 */
00003 /* Allan CORNET */
00004 /*-----------------------------------------------------------------------------------*/ 
00005 #include "InitTclTk.h"
00006 #include "TclEvents.h"
00007 #ifndef _MSC_VER
00008  #include <dirent.h>
00009  #include <X11/Xlib.h>
00010  #include <X11/Xatom.h>
00011  #include <X11/Xutil.h>
00012  #include <X11/cursorfont.h>
00013  #include <X11/Intrinsic.h>
00014  #include <X11/StringDefs.h>
00015  #include <X11/Shell.h>
00016  #include <X11/Xos.h>
00017  #include <ctype.h>
00018 #endif
00019 #include "setgetSCIpath.h"
00020 #include "sciprint.h"
00021 /*-----------------------------------------------------------------------------------*/ 
00022 extern int TCL_EvalScilabCmd(ClientData clientData,Tcl_Interp * theinterp,int objc,CONST char ** argv);
00023 extern int GetWITH_GUI(void);
00024 /*-----------------------------------------------------------------------------------*/ 
00025 int TK_Started=0;
00026 #ifndef _MSC_VER
00027   int XTKsocket=0;
00028 #endif
00029 /*-----------------------------------------------------------------------------------*/ 
00030 char *GetSciPath(void);
00031 /*-----------------------------------------------------------------------------------*/
00032 void initTCLTK(void)
00033 {
00034         if ( GetWITH_GUI() )
00035         {
00036                 if ( OpenTCLsci()==0 ) 
00037                 {
00038                         TK_Started=1;
00039                 }
00040                 else
00041                 {
00042                         TK_Started=0;
00043                 }
00044         }
00045         else
00046         {
00047                 TK_Started=0;
00048         }
00049 }
00050 /*-----------------------------------------------------------------------------------*/
00051 int OpenTCLsci(void)
00052 /* Checks if tcl/tk has already been initialised and if not */
00053 /* initialise it. It must find the tcl script */
00054 {
00055   char *SciPath=NULL;
00056   char TkScriptpath[2048];
00057   char MyCommand[2048];
00058 
00059 #ifndef _MSC_VER
00060   DIR *tmpdir=NULL;
00061   Display *XTKdisplay;
00062 #endif
00063 
00064   FILE *tmpfile2=NULL;
00065 
00066 #ifdef TCL_MAJOR_VERSION
00067   #ifdef TCL_MINOR_VERSION
00068     #if TCL_MAJOR_VERSION >= 8
00069       #if TCL_MINOR_VERSION > 0
00070          Tcl_FindExecutable(" ");
00071       #endif
00072     #endif
00073   #endif
00074 #endif
00075   SciPath=GetSciPath();
00076   
00077   /* test SCI validity */
00078   if (SciPath==NULL)
00079     {
00080       sciprint(TCL_WARNING1);
00081       return(1);
00082     }
00083 
00084 #ifdef _MSC_VER
00085   strcpy(TkScriptpath, SciPath);
00086   strcat(TkScriptpath, "/modules/tclsci/tcl/TK_Scilab.tcl");
00087 
00088   tmpfile2 = fopen(TkScriptpath,"r");
00089   if (tmpfile2==NULL) 
00090     {
00091       sciprint(TCL_WARNING2);
00092       return(1);
00093     }
00094   else fclose(tmpfile2);
00095 #else
00096   tmpdir=opendir(SciPath);
00097   if (tmpdir==NULL) 
00098     {
00099       sciprint(TCL_WARNING1);
00100       return(1);
00101     }
00102   else closedir(tmpdir);
00103   strcpy(TkScriptpath,SciPath);
00104   strcat(TkScriptpath, "/modules/tclsci/tcl/TK_Scilab.tcl");
00105   tmpfile2 = fopen(TkScriptpath,"r");
00106   if (tmpfile2==NULL) 
00107     {
00108       sciprint(TCL_WARNING2);
00109       return(1);
00110     }
00111   else fclose(tmpfile2);
00112 #endif /* _MSC_VER */ 
00113   
00114   if (TCLinterp == NULL) 
00115     {
00116       TCLinterp = Tcl_CreateInterp();
00117           if ( TCLinterp == NULL )
00118           {
00119             Scierror(999,TCL_ERROR1);
00120                 return (1);
00121           }
00122 
00123       if ( Tcl_Init(TCLinterp) == TCL_ERROR)
00124           {
00125                 Scierror(999,TCL_ERROR2);
00126                 return (1);
00127           }
00128 
00129       if ( Tk_Init(TCLinterp) == TCL_ERROR)
00130           {
00131                 Scierror(999,TCL_ERROR3);
00132                 return (1);
00133           }
00134 
00135       sprintf(MyCommand, "set SciPath \"%s\";",SciPath); 
00136       
00137           if ( Tcl_Eval(TCLinterp,MyCommand) == TCL_ERROR  )
00138           {
00139                 Scierror(999,TCL_ERROR4,TCLinterp->result);
00140                 return (1);
00141           }
00142       
00143           Tcl_CreateCommand(TCLinterp,"ScilabEval",TCL_EvalScilabCmd,(ClientData)1,NULL);
00144     }
00145    
00146   if (TKmainWindow == NULL)
00147     {
00148       TKmainWindow = Tk_MainWindow(TCLinterp);
00149           #ifndef _MSC_VER
00150         XTKdisplay=Tk_Display(TKmainWindow);
00151         XTKsocket = ConnectionNumber(XTKdisplay);
00152           #endif
00153       
00154       Tk_GeometryRequest(TKmainWindow,2,2);
00155 
00156           if ( Tcl_EvalFile(TCLinterp,TkScriptpath) == TCL_ERROR  )
00157           {
00158                 Scierror(999,TCL_ERROR4,TCLinterp->result);
00159                 return (1);
00160           }
00161 
00162       flushTKEvents();
00163     }
00164 
00165   if (SciPath) {FREE(SciPath);SciPath=NULL;}
00166   return(0);
00167 
00168 }
00169 /*-----------------------------------------------------------------------------------*/
00170 int CloseTCLsci(void)
00171 {
00172         int bOK=0;
00173         if ( GetWITH_GUI() )
00174         {
00175                 if (TK_Started)
00176                 {
00177                         Tcl_DeleteInterp(TCLinterp);
00178                         TCLinterp=NULL;
00179                         TKmainWindow=NULL;
00180                         bOK=1;
00181                         TK_Started=0;
00182                 }
00183         }
00184         return bOK;
00185 }
00186 /*-----------------------------------------------------------------------------------*/
00187 char *GetSciPath(void)
00188 /* force SciPath to Unix format for compatibility (Windows) */
00189 {
00190         char *PathUnix=NULL;
00191         char *SciPathTmp=NULL;
00192         int i=0;
00193 
00194         SciPathTmp=getSCIpath();
00195 
00196         if (SciPathTmp)
00197         {
00198                 PathUnix=(char*)MALLOC( ((int)strlen(SciPathTmp)+1)*sizeof(char) );
00199 
00200                 strcpy(PathUnix,SciPathTmp);
00201                 for (i=0;i<(int)strlen(PathUnix);i++)
00202                 {
00203                         if (PathUnix[i]=='\\') PathUnix[i]='/';
00204                 }
00205         }
00206         if (SciPathTmp) {FREE(SciPathTmp);SciPathTmp=NULL;}
00207         return PathUnix;
00208 }
00209 /*-----------------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:04:01 2007 for Scilab [trunk] by  doxygen 1.5.1