Rationnalization of the tcl/tk init process and use function instead of a silly globa...
[scilab.git] / scilab / modules / tclsci / src / c / InitTclTk.c
1 /*--------------------------------------------------------------------------*/
2 /* INRIA 2005 */
3 /* Allan CORNET */
4 /*--------------------------------------------------------------------------*/
5 #include "InitTclTk.h"
6 #include "TclEvents.h"
7 #ifndef _MSC_VER
8  #include <dirent.h>
9  #include <ctype.h>
10 #endif
11 #include "setgetSCIpath.h"
12 #include "sciprint.h"
13 #include "Scierror.h"
14 #include "localization.h"
15 #include "scilabmode.h"
16 #include "ScilabEval.h"
17 /*--------------------------------------------------------------------------*/
18 BOOL TK_Started=FALSE;
19 /*--------------------------------------------------------------------------*/
20 static char *GetSciPath(void);
21 /*--------------------------------------------------------------------------*/
22 int OpenTCLsci(void)
23 /* Checks if tcl/tk has already been initialised and if not */
24 /* initialise it. It must find the tcl script */
25 {
26   char *SciPath=NULL;
27   char TkScriptpath[PATH_MAX];
28   char MyCommand[2048];
29
30 #ifndef _MSC_VER
31   DIR *tmpdir=NULL;
32   //  Display *XTKdisplay;
33 #endif
34
35   FILE *tmpfile2=NULL;
36
37 #ifdef TCL_MAJOR_VERSION
38   #ifdef TCL_MINOR_VERSION
39     #if TCL_MAJOR_VERSION >= 8
40       #if TCL_MINOR_VERSION > 0
41          Tcl_FindExecutable(" ");
42       #endif
43     #endif
44   #endif
45 #endif
46   SciPath=GetSciPath();
47
48   /* test SCI validity */
49   if (SciPath==NULL)
50   {
51         sciprint(_("    The SCI environment variable is not set.\n      TCL initialisation failed !\n"));
52     return(1);
53   }
54
55 #ifdef _MSC_VER
56   strcpy(TkScriptpath, SciPath);
57   strcat(TkScriptpath, "/modules/tclsci/tcl/TK_Scilab.tcl");
58
59   tmpfile2 = fopen(TkScriptpath,"r");
60   if (tmpfile2==NULL)
61   {
62         sciprint(_("    Unable to find TCL initialisation scripts.\n    Check your SCI environment variable.\n  TCL initialisation failed !"));
63     return(1);
64   }
65   else fclose(tmpfile2);
66 #else
67   tmpdir=opendir(SciPath);
68   if (tmpdir==NULL)
69     {
70       sciprint(_("      The SCI environment variable is not set.\n      TCL initialisation failed !\n"));
71       return(1);
72     }
73   else closedir(tmpdir);
74   strcpy(TkScriptpath,SciPath);
75   strcat(TkScriptpath,"/modules/tclsci/tcl/TK_Scilab.tcl");
76   tmpfile2 = fopen(TkScriptpath,"r");
77   if (tmpfile2==NULL)
78     {
79       sciprint(_("      Unable to find TCL initialisation scripts.\n    Check your SCI environment variable.\n  TCL initialisation failed !"));
80       return(1);
81     }
82   else fclose(tmpfile2);
83 #endif /* _MSC_VER */
84
85   if (TCLinterp == NULL)
86     {
87       TCLinterp = Tcl_CreateInterp();
88           if ( TCLinterp == NULL )
89           {
90                 Scierror(999,_("Tcl Error : Tcl_CreateInterp.\n"));
91                 return (1);
92           }
93
94       if ( Tcl_Init(TCLinterp) == TCL_ERROR)
95           {
96                 Scierror(999,_("Tcl Error : Tcl_Init %s\n"),TCLinterp->result);
97                 return (1);
98           }
99
100       if ( Tk_Init(TCLinterp) == TCL_ERROR)
101           {
102                 Scierror(999,_("Tcl Error : Tk_Init %s\n"),TCLinterp->result);
103                 return (1);
104           }
105
106       sprintf(MyCommand, "set SciPath \"%s\";",SciPath);
107
108           if ( Tcl_Eval(TCLinterp,MyCommand) == TCL_ERROR  )
109           {
110                 Scierror(999,_("Tcl Error : %s\n"),TCLinterp->result);
111                 return (1);
112           }
113
114           Tcl_CreateCommand(TCLinterp,"ScilabEval",TCL_EvalScilabCmd,(ClientData)1,NULL);
115     }
116
117   if (TKmainWindow == NULL)
118     {
119       TKmainWindow = Tk_MainWindow(TCLinterp);
120       Tk_GeometryRequest(TKmainWindow,2,2);
121
122           if ( Tcl_EvalFile(TCLinterp,TkScriptpath) == TCL_ERROR  )
123           {
124                 Scierror(999,_("Tcl Error : %s\n"),TCLinterp->result);
125                 return (1);
126           }
127
128       flushTKEvents();
129     }
130
131   if (SciPath) {FREE(SciPath);SciPath=NULL;}
132   return(0);
133
134 }
135 /*--------------------------------------------------------------------------*/
136 BOOL CloseTCLsci(void)
137 {
138         if ( getScilabMode() != SCILAB_NWNI )
139         {
140                 if (isTkStarted())
141                 {
142                         Tcl_DeleteInterp(TCLinterp);
143                         TCLinterp=NULL;
144                         TKmainWindow=NULL;
145                         setTkStarted(FALSE);
146                         return TRUE;
147                 }
148         }
149         return FALSE;
150 }
151 /*--------------------------------------------------------------------------*/
152 static char *GetSciPath(void)
153 /* force SciPath to Unix format for compatibility (Windows) */
154 {
155         char *PathUnix=NULL;
156         char *SciPathTmp=NULL;
157         int i=0;
158
159         SciPathTmp=getSCIpath();
160
161         if (SciPathTmp)
162         {
163                 PathUnix=(char*)MALLOC( ((int)strlen(SciPathTmp)+1)*sizeof(char) );
164
165                 strcpy(PathUnix,SciPathTmp);
166                 for (i=0;i<(int)strlen(PathUnix);i++)
167                 {
168                         if (PathUnix[i]=='\\') PathUnix[i]='/';
169                 }
170         }
171         if (SciPathTmp) {FREE(SciPathTmp);SciPathTmp=NULL;}
172         return PathUnix;
173 }
174 /*--------------------------------------------------------------------------*/
175 BOOL isTkStarted(void){
176         return TK_Started;
177 }
178 /*--------------------------------------------------------------------------*/ 
179 void setTkStarted(BOOL isTkSet){
180         TK_Started=isTkSet;
181 }
182 /*--------------------------------------------------------------------------*/