File: InitTclTk.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (263 lines) | stat: -rw-r--r-- 6,160 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*-----------------------------------------------------------------------------------*/
/* INRIA 2005 */
/* Allan CORNET */
/*-----------------------------------------------------------------------------------*/ 
#include "InitTclTk.h"
#include "TclEvents.h"
#ifndef WIN32
 #include <dirent.h>
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 #include <X11/Xutil.h>
 #include <X11/cursorfont.h>
 #include <X11/Intrinsic.h>
 #include <X11/StringDefs.h>
 #include <X11/Shell.h>
 #include <X11/Xos.h>
 #include <ctype.h>
#endif
/*-----------------------------------------------------------------------------------*/ 
extern int TCL_EvalScilabCmd(ClientData clientData,Tcl_Interp * theinterp,int objc,CONST char ** argv);
extern int IsFromC(void);
extern void sci_tk_activate(void);
/*-----------------------------------------------------------------------------------*/ 
int TK_Started=0;
#ifndef WIN32
  int XTKsocket=0;
#endif
/*-----------------------------------------------------------------------------------*/ 
char *GetSciPath(void);
#if defined(__CYGWIN32__) 
static char *GetSciPathCyg(void);
#endif
/*-----------------------------------------------------------------------------------*/
static int first =0;
/*-----------------------------------------------------------------------------------*/ 
void initTCLTK(void)
{
	if ( OpenTCLsci()==0 ) TK_Started=1;
}
/*-----------------------------------------------------------------------------------*/
int OpenTCLsci(void)
/* Checks if tcl/tk has already been initialised and if not */
/* initialise it. It must find the tcl script */
{
  char *SciPath=NULL;
  char TkScriptpath[2048];
  char MyCommand[2048];

#ifndef WIN32
  DIR *tmpdir=NULL;
  Display *XTKdisplay;
#endif

  FILE *tmpfile=NULL;

#ifdef TCL_MAJOR_VERSION
  #ifdef TCL_MINOR_VERSION
    #if TCL_MAJOR_VERSION >= 8
      #if TCL_MINOR_VERSION > 0
         Tcl_FindExecutable(" ");
      #endif
    #endif
  #endif
#endif
  SciPath=GetSciPath();
  
  /* test SCI validity */
  if (SciPath==NULL)
    {
      sciprint(TCL_WARNING1);
      return(1);
    }

#ifdef WIN32
  strcpy(TkScriptpath, SciPath);
  strcat(TkScriptpath, "/tcl/TK_Scilab.tcl");


  tmpfile = fopen(TkScriptpath,"r");
  if (tmpfile==NULL) 
    {
      sciprint(TCL_WARNING2);
      return(1);
    }
  else fclose(tmpfile);
#else
  tmpdir=opendir(SciPath);
  if (tmpdir==NULL) 
    {
      sciprint(TCL_WARNING1);
      return(1);
    }
  else closedir(tmpdir);
  strcpy(TkScriptpath,SciPath);
  strcat(TkScriptpath, "/tcl/TK_Scilab.tcl");
  tmpfile = fopen(TkScriptpath,"r");
  if (tmpfile==NULL) 
    {
      sciprint(TCL_WARNING2);
      return(1);
    }
  else fclose(tmpfile);
#endif /* WIN32 */ 
  
#if defined(__CYGWIN32__) 
  /* we must pass X: pathnames to tcl */
  if ( SciPath ) FREE(SciPath);
  SciPath=GetSciPathCyg();  
  strcpy(TkScriptpath,SciPath);
  strcat(TkScriptpath, "/tcl/TK_Scilab.tcl");
#endif

  if (TCLinterp == NULL) 
    {
      TCLinterp = Tcl_CreateInterp();
	  if ( TCLinterp == NULL )
	  {
	    Scierror(999,TCL_ERROR1);
		return (1);
	  }

      if ( Tcl_Init(TCLinterp) == TCL_ERROR)
	  {
		Scierror(999,TCL_ERROR2);
		return (1);
	  }

      if ( Tk_Init(TCLinterp) == TCL_ERROR)
	  {
		Scierror(999,TCL_ERROR3);
		return (1);
	  }

      sprintf(MyCommand, "set SciPath \"%s\";",SciPath); 
      
	  if ( Tcl_Eval(TCLinterp,MyCommand) == TCL_ERROR  )
	  {
		Scierror(999,TCL_ERROR4,TCLinterp->result);
		return (1);
	  }
      
	  Tcl_CreateCommand(TCLinterp,"ScilabEval",TCL_EvalScilabCmd,(ClientData)1,NULL);
    }
   
  if (TKmainWindow == NULL)
    {
      TKmainWindow = Tk_MainWindow(TCLinterp);
	  #ifndef WIN32
        XTKdisplay=Tk_Display(TKmainWindow);
        XTKsocket = ConnectionNumber(XTKdisplay);
	  #endif
      
      Tk_GeometryRequest(TKmainWindow,2,2);

  	  if ( Tcl_EvalFile(TCLinterp,TkScriptpath) == TCL_ERROR  )
	  {
		Scierror(999,TCL_ERROR4,TCLinterp->result);
		return (1);
	  }

      flushTKEvents();
    }

  if (SciPath) {FREE(SciPath);SciPath=NULL;}
  return(0);

}
/*-----------------------------------------------------------------------------------*/
int CloseTCLsci(void)
{
	int bOK=0;
	if (TK_Started)
	{
		Tcl_DeleteInterp(TCLinterp);
		TCLinterp=NULL;
		TKmainWindow=NULL;
		bOK=1;
		TK_Started=0;
		first=0;
	}
	return bOK;
}
/*-----------------------------------------------------------------------------------*/
char *GetSciPath(void)
/* force SciPath to Unix format for compatibility (Windows) */
{
	char *PathUnix=NULL;
	char *SciPathTmp=NULL;
	int i=0;

	SciPathTmp=getenv("SCI");

	if (SciPathTmp)
	{
		PathUnix=(char*)MALLOC( ((int)strlen(SciPathTmp)+1)*sizeof(char) );

		strcpy(PathUnix,SciPathTmp);
		for (i=0;i<(int)strlen(PathUnix);i++)
		{
			if (PathUnix[i]=='\\') PathUnix[i]='/';
		}
	}
	
	return PathUnix;
}
/*-----------------------------------------------------------------------------------*/
#if defined(__CYGWIN32__) 
/* from cygwin /cygdrive/f/ to f: 
 * PathUnix must be a valid pathname returned by GetSciPath
 */
static char *GetSciPathCyg(void)
/* force SciPath to Unix format for compatibility (Windows) */
{
  const char *cygwin = "/cygdrive/";
  char *PathUnix=NULL;
  char *SciPathTmp=NULL;
  int i=0;

  SciPathTmp=getenv("SCI");

  if (SciPathTmp)
    {
      PathUnix=(char*)MALLOC( ((int)strlen(SciPathTmp)+1)*sizeof(char) );
      
      strcpy(PathUnix,SciPathTmp);
      for (i=0;i<(int)strlen(PathUnix);i++)
	{
	  if (PathUnix[i]=='\\') PathUnix[i]='/';
	}
    }
  if (strncmp(PathUnix,cygwin,strlen(cygwin))==0)
    {
      strcpy(PathUnix,SciPathTmp +strlen(cygwin)-1);
      *PathUnix = *(PathUnix+1);
      *(PathUnix+1)= ':';
    }
  return PathUnix;
}
#endif
/*-----------------------------------------------------------------------------------*/
int ReInitTCL(void)
{
	if (TK_Started != 1 )
	{
		if ( first == 0) 
		{
			sci_tk_activate();
			first++;
			if ( TK_Started != 1 ) 
			{
				initTCLTK();
				/* Derniere chance ;) d'initialisation */
				if ( TK_Started != 1 ) 
				{
					Scierror(999,TCL_ERROR20);
					return 0;
				}
			}
		}
	}
	return 0;
}
/*-----------------------------------------------------------------------------------*/