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