Tcl/Tk has proven to be an excellent language for building small
programs that require a Graphical User Interface (GUI). However, it is
often inadequate for use in large commercial applications for a number
of reasons:
* Execution speed is usually too slow for serious computation.
* Complex data structures are difficult to construct.
* The lack of structure and typing in the Tcl language complicates
the development of large codes.
* Tcl/Tk source code is easily read by the end user, making it hard
for developers to protect proprietary algorithms.
* Large Tcl/Tk programs typically consist of many separate files
that must be correctly positioned within the target computer's
file system. This can make the programs difficult to install,
maintain and administer.
To circumvent these problems, we have constructed a system that makes
it easy for a C or C++ program to invoke and interact with Tcl/Tk.
This allows data structures and compute-intensive algorithms to be
coded in C or C++ while the GUI is coded in Tcl/Tk. It also allows the
entire application to be compiled into a single stand-alone
executable, which is not easily readable by the end user and which can
be run on computers that do not have Tcl/Tk installed. We call our
system "ET" for "Embedded Tk".
1. A Simple Example: ``Hello, World!''
The following is an ET implementation of the classic "Hello, World!"
program:
void main(int argc, char **argv){
Et_Init(&argc,argv);
ET( button .b -text {Hello, World!} -command exit; pack .b );
Et_MainLoop();
}
This example is short, but is serves to illustrate the basic structure
of any ET application. The first line of the main() procedure is a
call to the function Et_Init(). This function initializes the ET
system by creating a Tcl/Tk interpreter, connecting to the X11 server,
and creating a main window. The last line of main() implements the
event loop. Everything in between constitutes setup code. In this
example, the setup code is a short Tcl/Tk script contained within the
special macro ET(). The et2c macro preprocessor will replace this ET()
macro with C code that causes the enclosed Tcl/Tk script to be
executed.
Of course, there is nothing in this example that could not also be
done by calling Tcl/Tk library routines directly, without the
intervening ET abstraction. The advantage of using ET is that it
makes the interface between C and Tcl/Tk considerably less cumbersome
and error-prone, allowing the programmer to focus more mental energy
on the algorithm and less on the syntax of the programming language.
1.1 Compiling ``Hello, World!''
To compile the hello world example, we must first process the source
file using the et2c macro preprocessor, then link the results with the
et.o library. Suppose the example code is contained in the file
hello.c. Then to compile the example (on most systems) requires the
following steps:
et2c hello.c >hello_.c
cc -o hello hello_.c et.o -ltk -ltcl -lX11 -lm
Assuming it is statically linked, the resulting executable file hello
contains everything needed to run the program: the Tcl/Tk interpreter,
the startup scripts and the application code. The program can be
moved to other binary-compatible computers and executed there even if
the other computers do not have Tcl/Tk installed.
Additional information is provided below.
1.2 How to obtain sources and documentation
Complete sources to the et2c macro preprocessor and et.o library
comprise less than 2000 lines of code, including comments. These
sources, together with source code to all example programs discussed
below, are available for anonymous FTP from ftp.vnet.net in the
directory /pub/users/drh.
A copy of this documentation is also available from the same FTP site.
The documentation is available in either PostScript, HTML, or an ASCII
text file.
2. A Summary Of Services Provided By ET
The overall goal of ET is to simplify the interface between C and an
embedded Tcl/Tk-based GUI. To this end, the ET system provides a
number of services that aid in initializing the Tcl/Tk interpreter and
in transferring data and control between Tcl/Tk and C. The services
provided by ET are summarized here and described in more detail in
subsequent sections.
2.1 Routines to initialization the Tcl/Tk interpreter
The et.o library includes routines Et_Init() and Et_MainLoop() that
initialize the ET package and implement the X11 event loop. A third
routine Et_ReadStdin() allows standard input to be read and
interpreted by the Tcl/Tk interpreter at run-time.
2.2 Macros to invoking Tcl/Tk from within C
The ET() macro looks and works just like a function in C, except that
its argument is a Tcl/Tk script instead of C code. ET() returns
either ET_OK or ET_ERROR depending upon the success or failure of the
script. Similar routines ET_STR(), ET_INT() and ET_DBL() also take a
Tcl/Tk script as their argument, but return a string, an integer, or a
double-precision floating point number instead of the status code.
2.3 A method to pass variable contents from C to Tcl/Tk
Wherever the string %d(x) occurs inside an ET() macro, the integer C
expression x is converted to ASCII and substituted in place of the
%d(x). Similarly, %s(x) can be used to substitute a character string,
and %f(x) will substitute a floating point value. The string %q(x)
works like %s(x) except that a backslash is inserted before each
character that has special meaning to Tcl/Tk.
2.4 Macros for creating new Tcl/Tk commands in C
The macro "ET_PROC( newcmd ){ ... }" defines a C function that is
invoked whenever the newcmd command is executed by the Tcl/Tk
interpreter. Parameters argc and argv describe the arguments to the
command. If a file named xyzzy.c contains one or more ET_PROC macros,
then the commands associated with those macros are registered with the
Tcl/Tk interpreter by invoking "ET_INSTALL_COMMANDS( xyzzy.c )" after
the Et_Init() in the main procedure.
2.5 Macros for linking external Tcl/Tk scripts into a C program
The macro "ET_INCLUDE( script.tcl )" causes the Tcl/Tk script in the
file script.tcl to be made a part of the C program and executed at the
point in the C program where the ET_INCLUDE macro is found. The
external Tcl/Tk script is normally read into the C program at
compile-time and thus becomes part of the executable. However, if the
-dynamic option is given to the et2c macro preprocessor, loading of
the external Tcl/Tk script is deferred to run-time.
2.6 Tcl/Tk return status macros
The macros ET_OK and ET_ERROR are set equal to TCL_OK and TCL_ERROR.
This often eliminates the need to put "#include " at the beginning of
files that use ET.
2.7 Convenience variables
ET defines three global C variables as a convenience to the
programmer. Et_Interp is a pointer to the Tcl/Tk interpreter used by
ET. Et_MainWindow is the main window of the ET application.
Et_Display is the Display pointer required as the first argument to
many XLib routines. ET also provides two global Tcl variables,
cmd_name and cmd_dir. These contain the name of the executable and
the directory where the executable is found.
3. Example 2: A Decimal Clock
The preceding "Hello, World!" example program demonstrated the basic
structure of an ET application including the use of the Et_Init()
function to initialize the Tcl/Tk interpreter and the Et_MainLoop()
function for implementing the X11 event loop. The following program
will demonstrate additional aspects of the the ET system.
3.1 Source code for the decimal clock example
/* This file implements a clock that shows the hour as
** fixed-point number X, such that
**
** 0.000 <= X < 24.000
**
** X represents a fractional hour, not hours and minutes.
** Thus the time "8.500" means half past 8 o'clock, not
** ten minutes till 9.
*/
#include <time.h>
void main(int argc, char **argv){
Et_Init(&argc,argv);
ET_INSTALL_COMMANDS;
ET(
没有合适的资源?快使用搜索试试~ 我知道了~
sourcenav-6.0.tar.gz
5星 · 超过95%的资源 需积分: 10 242 下载量 158 浏览量
2010-01-07
01:44:16
上传
评论 2
收藏 18.03MB GZ 举报
温馨提示
共6113个文件
c:1124个
tcl:872个
h:424个
sourcenav-6.0.tar.gz,一个Linux下的源代码查看器的源代码
资源推荐
资源详情
资源评论
收起资源包目录
sourcenav-6.0.tar.gz (6113个子文件)
5.6.0 10KB
5.6.0 10KB
5.004 3KB
5.004 3KB
5.004_01 7KB
5.004_01 7KB
5.004_02 7KB
5.004_02 7KB
5.004_03 7KB
5.004_03 7KB
5.004_04 7KB
5.004_04 7KB
5.004_05 7KB
5.004_05 7KB
5.005 7KB
5.005 7KB
5.005_01 7KB
5.005_01 7KB
5.005_02 9KB
5.005_02 9KB
5.005_03 8KB
5.005_03 8KB
wish.1 8KB
tixwish.1 7KB
tclsh.1 5KB
itkwish.1 2KB
itclsh.1 913B
s60.mmp.1 152B
ChangeLog.1999 89KB
s60.mmp.2 152B
ChangeLog.2000 92KB
ChangeLog.2001 136KB
FileSystem.3 55KB
CrtChannel.3 30KB
ConfigWidg.3 28KB
OpenFileChnl.3 27KB
Notifier.3 27KB
SetOptions.3 25KB
Encoding.3 25KB
Notifier.3 24KB
CrtItemType.3 23KB
ParseCmd.3 19KB
TraceVar.3 15KB
RegExp.3 15KB
ParseArgv.3 14KB
TextLayout.3 14KB
Object.3 14KB
3DBorder.3 13KB
CrtObjCmd.3 11KB
CrtImgType.3 11KB
CrtPhImgFmt.3 10KB
GetBitmap.3 10KB
GetCursor.3 10KB
SetVar.3 10KB
ListObj.3 10KB
Resolvers.3 10KB
SetResult.3 10KB
StringObj.3 10KB
CrtSlave.3 10KB
Utf.3 9KB
Hash.3 8KB
AddErrInfo.3 8KB
Eval.3 8KB
Thread.3 8KB
ObjectType.3 8KB
FindPhoto.3 8KB
OpenTcp.3 7KB
GetColor.3 7KB
TraceCmd.3 7KB
SplitList.3 7KB
ObjSetVar.3 7KB
BindTable.3 7KB
CanvTkwin.3 6KB
Async.3 6KB
CrtCommand.3 6KB
CrtErrHdlr.3 6KB
ConfigWind.3 6KB
CrtWindow.3 6KB
CrtInterp.3 6KB
DString.3 6KB
MeasureChar.3 6KB
Interp.3 6KB
Exit.3 5KB
WindowId.3 5KB
editline.3 5KB
CrtSelHdlr.3 5KB
GetImage.3 5KB
CanvPsY.3 5KB
StdChannels.3 5KB
CrtTrace.3 4KB
GetFont.3 4KB
LinkVar.3 4KB
CanvTxtInfo.3 4KB
Preserve.3 4KB
Preserve.3 4KB
ExprLong.3 4KB
DoOneEvent.3 4KB
DoOneEvent.3 4KB
ByteArrObj.3 4KB
MaintGeom.3 4KB
共 6113 条
- 1
- 2
- 3
- 4
- 5
- 6
- 62
morre
- 粉丝: 187
- 资源: 2336
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页