
Embedding a Scheme Interpreter in the Tk Toolkit Erick Gallesio Universitede Nice SophiaAntipolis Lab oratoire IS CNRS URA Bat avenue Alb ert Einstein Sophia Antipolis Valbonne FRANCE Abstract an exciting challenge To keep intact the TkTcl pair spirit a little applicative language was nec STk is a graphical package which rely on Tk and essary Scheme was a go o d candidate to re the Scheme programming language Concretely place Tcl b ecause it is small clean and well de it can be seen as the Tk package where the Tcl ned since it is an IEEE standard language as been replaced by a Scheme inter Programming with STk can b e done at two preter Programming with STk can be done at distinct levels First level is quite identical than two distinct levels First level is quite identical programming Tk with Tcl excepting several mi than programming Tk with Tcl Second level of nor syntactic dierences Second level of pro programming uses a ful l object oriented system gramming uses a full ob ject oriented system Those two programming levels and current imple with multiinheritance generic functions and a mentation are described here true meta ob ject proto col Those two levels of programming are briey describ ed in the two rst sections Section is devoted to implementation Introduction and section exp oses some encountered problems when mixing Tk and Scheme Todays available graphical to olkits for applica tive languages are not satisfactory Most of the time they ask to the user to b e an X exp ert which STk First level must cop e with complicated arcane details such as server connections or queue events This is a The rst level of STk uses the standard Scheme true problem since p eople which use this kind constructs To work at this level a user must of languages are generally not inclined in system know a little set of rewriting rules from the orig programming and little of them get over the gap inal TkTcl library With these rules the Tk b etween the language and the to olkit abstraction manual pages and a little knowledge of Scheme levels heshe can easily build a STk program Creation of a new widget button lab el can Tk is a p owerful X graphical to olkit dened vas is done with sp ecial STk primitives pro at the University of Berkeley by JOusterhout cedures For instance creating a new button can This to olkit gives to the user high level wid gets such as buttons or menu and is easily pro b e done with grammable In particular a little knowledge of button b X fundamentals are needed to build an applica tion with it Tk package rely on an interpretative Note that the name of the widget must b e language named Tcl However dep endencies stringied due to the Scheme evaluation mech b etween those two packages are not to o intricate anism The call of a widget creation primitive and replacing Tcl by an applicative language was denes a new Scheme ob ject This ob ject which To illustrate STk rst level of programming is considered as a new basic type by the Scheme interpreter is automatically stored in a variable Figure shows the simple le browser describ ed whose name is equal to the string passed to the in written in STk creation function So the preceding button cre ation would dene an ob ject stored in the b vari able This ob ject is a sp ecial kind of pro cedure STk Second level which is generally used as in pure Tk to cus tomize its asso ciated widget For instance the Programming with material shown b efore is a lit expression tle bit tedious and more complicated than co d b configure ing with Tcl since one have to add parenthesis text Hello world pairs and quote options values Its only interest border is to bring the p ower and exibility of Tk to the Scheme world p ermits to set the text and background options of the b button As we can see on this exam The second level of STk is far more interest ple parameters must b e well quoted in regard ing since it uses a full ob ject oriented extension of the Scheme evaluation rules Since this nota of the Scheme language Dening an ob ject ori tion is barely crude the Common Lisp keyword ented layer on Scheme is a current activity in mechanism has b een introduced in the Scheme the Scheme community and several packages are 1 interpreter Consequently the preceding ex available The ob ject layer of STk is derived pression could have b een written as from a package called Tiny Clos This exten b configure sion provides ob jects ala CLOS Common Lisp text Hello world Ob ject System In fact the prop osed extension border is much closer from the ob jects one can nd in Dylan since this language is already a tentative which in turn is b oth close from Common Lisp to merge CLOS notions in a Scheme like language and pure Tk Of course as in Tk parameters can b e passed at widget creation time and our button STk ob ject extension gives to the user a full creation and initialization could have b een done in a single expression ob ject oriented system with multiinheritance and generic functions Furthermore all the im button b plementation rely on a true meta ob ject proto col text Hello world in the spirit of This mo del has b een used to border embo dy all the predened Tk widgets in a hier The Tk binding mechanism which serves to archy of Stk classes create widget event handlers follow the same kind of rules The b o dy of a Tk handler must b e writ ten of course in Scheme Following example Class hierarchy shows such a script in this example the lab el With the STk ob ject system every Tk graphical indicates how many times mouse button has b een depressed Button press counter increment ob ject used in a program such as a menu a la is achieved with the simple script given in the b el or a button is represented as an ob ject in the bind call Scheme core All the dened STk classes build a hierarchy which is briey describ ed here Firstly define count all the classes shared a unique ancestor the pack append Tkobject class Instances of this class con label l tain informations which are necessary to estab textvariable count lish a communication b etween the Scheme and fill bind l Tk worlds Ob jects of this class have two main set count count slots named Id and parent The Id slot con 1 tains a string normally generated by the system A keyword is a symbol b eginning with a colon It can b een seen as a symbolic constant ie its value is itself which corresp ond to a unique variable name in usrlocalbinstk f scrollbar scroll command list yview listbox list yscroll scroll set relief raised geometry x pack append scroll right filly list left expand fill define browse dir file if not string dir set file stringappend dir file if directory file system format f browsestk A file if file file let ed getenv EDITOR if ed system stringappend ed file system stringappend xedit file error Bad directory or file file define dir if argc car argv system format f ls a A tmpbrowse dir withinputfromfile tmpbrowse lambda do f readline readline eofobject f list insert end f bind list Controlc destroy bind list DoubleButton browse dir selection get Figure A simple le browser STk hierarchy is shown in Figure the Tk world The parent slot contains a ref erence to the ob ject which graphically includes the current ob ject Normally end users will not Basic notions have to use direct instances of the Tkobject 2 This section describ es basic concepts of our ob class ject extension on a small example Denition of The next level in our class hierarchy denes a new ob ject class is done with the defclass form a fork with two branches the Tkwidget For instance class and Tkcanvasitem class Instances of defclass person the former class are classical widgets such as name initarg name buttons or menus since instances of the later accessor name are ob jects contained in a canvas such as lines age initarg age or rectangles Tk widgets are also divided in denes a p erson characteristics Two slots are two categories Tksimplewidgets and Tk declared name and age Characteristics of a slot compositewidgets Simple widgets are directly are expressed with its denition Here for in implemented as Tk ob jects and comp osite ones stance it is said that the slot name can b e inited are built up on simple widgets eg le browser with the keyword name up on instance creation alert messages and so on A partial view of the and that an accessor function should b e gener 2 ated for it Creation of a new instance is done All classes whose name b egins with the Tk prex with the make constructor are not intended for the nal user Tk-object Tk-widget Tk-canvas-item Tk-simple-widget Frame Label Message Rectangle Line Button Tk-complex-button Check-button Radio-button Figure A partial view of STk hierarchy defclass Button Label define p make person command accessor command name Smith initarg command age allocation pseudo This call p ermits to build a new p erson and to type any initialize the slots which comp ose himher metaclass Tk Reading the value of a slot can b e done with the function slotvalue For instance This denition says that a Button is a in herits from Label with an extra slot called slotvalue p age command This slots allo cation scheme is said to 3 p ermits to get the value of slot age of the p ob b e pseudo Pseudoslots are sp ecial purp ose ject Setting this slot can b e done by using the slots they can b e used as normal slots but they generalized set dened in STk are not allo cated in the Scheme world ie their value is stored in one of the structures manipu set slotvalue p age lated by the Tk library instead of in a Scheme ob
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-