Practical Programming in TCL and TK
Practical Programming in TCL and TK
By BrentB.Welch
Why Tcl?
On-line Examples
Ftp Archives
Newsgroups
Typographic Conventions
Hot Tips
Book Organization
Tcl Commands
Hello, World!
Variables
Command Substitution
Math Expressions
Backslash Substitution
Procedures
A Factorial Example
Comments
Fine Points
Reference
Command-Line Arguments
Predefined Variables
Next Steps
Related Chapters
Tcl Lists
Constructing Lists
Related Chapters
If Then Else
Switch
While
Foreach
For
Catch
Error
Return
Scope
Array Syntax
File Attributes
Environment Variables
Syntax Summary
Using Packages
Interactive Conveniences
Coding Style
Cross-Platform Support
Debugging
Scriptics' TclPro
Other Tools
Performance Tuning
Chapter14. Namespaces
Using Namespaces
Namespace Variables
Command Lookup
Nested Namespaces
Introspection
Notes
Chapter15. Internationalization
Message Catalogs
Client Sockets
Server Sockets
Basic Authentication
Domain Handlers
Document Types
Form Handlers
Programming Reference
Server Configuration
Creating Interpreters
Safe Interpreters
Command Aliases
Hidden Commands
Substitutions
Security Policies
Tk in Child Interpreters
Chapter21. Tk Fundamentals
Hello, World! in Tk
Naming Tk Widgets
Configuring Tk Widgets
Chapter22. Tk by Example
ExecLog
A Tcl Shell
Anchoring
Packing Order
Unpacking a Widget
Packer Summary
A Basic Grid
place Basics
Event Syntax
Modifiers
Event Sequences
Virtual Events
Event Keywords
PartIV. Tk Widgets
Button Attributes
Button Operations
Keyboard Traversal
Menu Attributes
An Introduction to Resources
User-Defined Buttons
User-Defined Menus
Chapter30. Scrollbars
Using Scrollbars
Using Listboxes
Listbox Bindings
Listbox Attributes
Text Indices
Text Marks
Text Tags
The Selection
Tag Bindings
Searching Text
Embedded Widgets
Embedded Images
Text Bindings
Text Operations
Text Attributes
Canvas Coordinates
Hello, World!
Canvas Objects
Canvas Operations
Generating Postscript
Canvas Attributes
Hints
PartV. Tk Details
Selection Handlers
Standard Dialogs
Custom Dialogs
Configuring Attributes
Size
Colors
Naming a Font
X Font Names
Font Metrics
Text Attributes
Chapter40. Send
Communicating Processes
The wm Command
The tk Command
App-Defaults Files
Defining Preferences
Basic Concepts
A C Command Procedure
Using autoconf
Final Cleanup
wish
Obsolete Features
Bindings
Scrollbar Interface
pack info
Focus
Radiobutton Value
Entry Widget
Menus
Listboxes
No geometry Attribute
Text Widget
Color Attributes
Canvas scrollincrement
The Selection
Cross-Platform Scripts
Network Sockets
Virtual Events
Standard Dialogs
Namespaces
Safe-Tcl
New lsort
tcl_precision Variable
Http Package
Platform-Independent Fonts
Application Embedding
grid rowconfigure
Thread Safety
Miscellaneous
Proposed Tk Changes
Technical Support
Index
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Copyright
Library of Congress Cataloging-in-Publication Data
Welch, Brent. B.
Practical programming in Tcl and Tk / Brent B. Welch.-- 3rd ed.
p. cm.
ISBN 0-13-022028-0
1. Tcl (Computer program language) 2. Tk toolkit. I. Title.
QA76.73.T44 W45 1999
005.13'3--dc21 99-047206
Credits
Editorial/Production Supervision: Joan L. McNamara
Acquisitions Editor: Mark Taub
Marketing Manager: Kate Hargett
Editorial Assistant: Michael Fredette
Cover Design Director: Jerry Votta
Cover Design: Design Source
Manufacturing Manager: Alexis R. Heydt
2000, 1997 by Prentice Hall PTR
Prentice-Hall, Inc.
Upper Saddle River, New Jersey 07458
Prentice Hall books are widely used by corporations and government agencies for training, marketing,
and resale. The publisher offers discounts on this book when ordered in bulk quantities. For more
information, contact:
Corporate Sales Department, Prentice Hall PTR, One Lake Street, Upper Saddle River, NJ 07458
Phone: 800-382-3419; Fax: 201-236-7141; email: corpsales@prenhall.com
All rights reserved. No part of this book may be reproduced, in any form or by any means, without
permission in writing from the publisher.
All product names mentioned herein are the trademarks of their respective owners.
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
Prentice-Hall International (UK) Limited, London
Prentice-Hall of Australia Pty. Limited, Sydney
Prentice-Hall Canada Inc., Toronto
Prentice-Hall Hispanoamericana, S.A., Mexico
Prentice-Hall of India Private Limited, New Delhi
Prentice-Hall of Japan, Inc., Tokyo
Prentice-Hall (Singapore) Pte. Ltd., Singapore
Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro
Dedication
to Jody, Christopher, Daniel, and Michael
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
List of Examples
1.1 The "Hello, World!" example
1.2 Tcl variables
1.3 Command substitution
1.4 Simple arithmetic
1.5 Nested commands
1.6 Built-in math functions
1.7 Grouping expressions with braces
1.8 Quoting special characters with backslash
1.9 Continuing long lines with backslashes
1.10 Grouping with double quotes vs. braces
1.11 Embedded command and variable substitution
1.12 Defining a procedure
1.13 A while loop to compute factorial
1.14 A recursive definition of factorial
1.15 Using set to return a variable value
1.16 Embedded variable references
1.17 Using info to determine if a variable exists
1.18 Controlling precision with tcl_precision
2.1 A standalone Tcl script on UNIX
and the aspects of C programming to create Tcl extensions is given a lighter treatment. I have been
lucky to remain involved in the core Tcl development, and I hope I can pass along the insights I have
gained by working with Tcl.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Why Tcl?
As a scripting language, Tcl is similar to other UNIX shell languages such as the Bourne Shell (sh),
the C Shell (csh), the Korn Shell (ksh), and Perl. Shell programs let you execute other programs. They
provide enough programmability (variables, control flow, and procedures) to let you build complex
scripts that assemble existing programs into a new tool tailored for your needs. Shells are wonderful
for automating routine chores.
It is the ability to easily add a Tcl interpreter to your application that sets it apart from other shells. Tcl
fills the role of an extension language that is used to configure and customize applications. There is no
need to invent a command language for your new application, or struggle to provide some sort of user-
programmability for your tool. Instead, by adding a Tcl interpreter, you structure your application as a
set of primitive operations that can be composed by a script to best suit the needs of your users. It also
allows other programs to have programmatic control over your application, leading to suites of
applications that work well together.
The Tcl C library has clean interfaces and is simple to use. The library implements the basic interpreter
and a set of core scripting commands that implement variables, flow control, and procedures (see page
22). There is also a set of commands that access operating system services to run other programs,
access the file system, and use network sockets. Tk adds commands to create graphical user interfaces.
Tcl and Tk provide a "virtual machine" that is portable across UNIX, Windows, and Macintosh
environments.
The Tcl virtual machine is extensible because your application can define new Tcl commands. These
commands are associated with a C or C++ procedure that your application provides. The result is
applications that are split into a set of primitives written in a compiled language and exported as Tcl
commands. A Tcl script is used to compose the primitives into the overall application. The script layer
has access to shell-like capability to run other programs, has access to the file system, and can call
directly into the compiled part of the application through the Tcl commands you define. In addition,
from the C programming level, you can call Tcl scripts, set and query Tcl variables, and even trace the
execution of the Tcl interpreter.
There are many Tcl extensions freely available on the Internet. Most extensions include a C library
that provides some new functionality, and a Tcl interface to the library. Examples include database
access, telephone control, MIDI controller access, and expect, which adds Tcl commands to control
interactive programs.
The most notable extension is Tk, a toolkit for graphical user interfaces. Tk defines Tcl commands
that let you create and manipulate user interface widgets. The script-based approach to user interface
programming has three benefits:
Development is fast because of the rapid turnaround; there is no waiting for long compilations.
The Tcl commands provide a higher-level interface than most standard C library user-interface
toolkits. Simple user interfaces require just a handful of commands to define them. At the same
time, it is possible to refine the user interface in order to get every detail just so. The fast
turnaround aids the refinement process.
The user interface can be factored out from the rest of your application. The developer can
concentrate on the implementation of the application core and then fairly painlessly work up a
user interface. The core set of Tk widgets is often sufficient for all your user interface needs.
However, it is also possible to write custom Tk widgets in C, and again there are many
contributed Tk widgets available on the network.
There are other choices for extension languages that include Visual Basic, Scheme, Elisp, Perl,
Python, and Javascript. Your choice between them is partly a matter of taste. Tcl has simple constructs
and looks somewhat like C. It is easy to add new Tcl primitives by writing C procedures. Tcl is very
easy to learn, and I have heard many great stories of users completing impressive projects in a short
amount of time (e.g., a few weeks), even though they never used Tcl before.
Java has exploded onto the computer scene since this book was first published. Java is a great systems
programming language that in the long run could displace C and C++. This is fine for Tcl, which is
designed to glue together building blocks written in any system programming language. Tcl was
designed to work with C, but has been adapted to work with the Java Virtual Machine. Where I say "C
or C++", you can now say "C, C++, or Java," but the details are a bit different with Java. This book
does not describe the Tcl/Java interface, but you can find TclBlend on the CD-ROM. TclBlend loads
the Java Virtual Machine into your Tcl application and lets you invoke Java methods. It also lets you
implement Tcl commands in Java instead of C or C++.
Javascript is a language from Netscape that is designed to script interactions with Web pages.
Javascript is important because Netscape is widely deployed. However, Tcl provides a more general
purpose scripting solution that can be used in a wide variety of applications. The Tcl/Tk Web browser
plugin provides a way to run Tcl in your browser. It turns out to be more of a Java alternative than a
JavaScript alternative. The plugin lets you run Tcl applications inside your browser, while JavaScript
gives you fine grain control over the browser and HTML display. The plugin is described in Chapter
20.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Tcl and Tk Versions
Tcl and Tk continue to evolve. See http://www.beedub.com/book/ for updates and news about the
latest Tcl releases. Tcl and Tk have had separate version numbers for historical reasons, but they are
released in pairs that work together. The original edition of this book was based on Tcl 7.4 and Tk 4.0,
and there were a few references to features in Tk 3.6. This third edition has been updated to reflect
new features added through Tcl/Tk 8.2:
Tcl 7.5 and Tk 4.1 had their final release in May 1996. These releases feature the port of Tk to
the Windows and Macintosh environments. The Safe-Tcl security mechanism was introduced to
support safe execution of network applets. There is also network socket support and a new
Input/Output (I/O) subsystem to support high-performance event-driven I/O.
Tcl 7.6 and Tk 4.2 had their final release in October 1996. These releases include improvements
in Safe-Tcl, and improvements to the grid geometry manager introduced in Tk 4.1. Cross-
platform support includes virtual events (e.g., <<Copy>> as opposed to <Control-c>), standard
dialogs, and more file manipulation commands.
Tcl 7.7 and Tk 4.3 were internal releases used for the development of the Tcl/Tk plug-in for the
Netscape Navigator and Microsoft Internet Explorer Web browsers. Their development actually
proceeded in parallel to Tcl 7.6 and Tk 4.2. The plug-in has been released for a wide variety of
platforms, including Solaris/SPARC, Solaris/INTEL, SunOS, Linux, Digital UNIX, IRIX,
HP/UX, Windows 95, Windows NT, and the Macintosh. The browser plug-in supports Tcl
applets in Web pages and uses the sophisticated security mechanism of Safe-Tcl to provide
safety.
Tcl 8.0 features an on-the-fly compiler for Tcl that provides many-times faster Tcl scripts. Tcl
8.0 supports strings with embedded null characters. The compiler is transparent to Tcl scripts,
but extension writers need to learn some new C APIs to take advantage of its potential. The
release history of 8.0 spread out over a couple of years as John Ousterhout moved from Sun
Microsystems to Scriptics Corporation. The widely used 8.0p2 release was made in the fall of
1997, but the final patch release, 8.0.5, was made in the spring of 1999.
Tk changed its version to match Tcl at 8.0. Tk 8.0 includes a new platform-independent font
mechanism, native menus and menu bars, and more native widgets for better native look and feel
on Windows and Macintosh.
Tcl/Tk 8.1 features full Unicode support, a new regular expression engine that provides all the
features found in Perl 5, and thread safety so that you can embed Tcl into multithreaded
applications. Tk does a heroic job of finding the correct font to display your Unicode characters,
and it adds a message catalog facility so that you can write internationalized applications. The
release history of Tcl/Tk 8.1 also straddled the Sun to Scriptics transition. The first alpha release
was made in the fall of 1997, and the final patch release, 8.1.1, was made in May 1999.
Tcl/Tk 8.2 is primarily a bug fix and stabilization release. There are a few minor additions to the
Tcl C library APIs to support more extensions without requiring core patches. Tcl/Tk 8.2 went
rapidly into final release in the summer of 1999.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Who Should Read This Book
This book is meant to be useful to the beginner in Tcl as well as the expert. For the beginner and
expert alike, I recommend careful study of Chapter 1, Tcl Fundamentals. The programming model of
T0cl is designed to be simple, but it is different from many programming languages. The model is
based on string substitutions, and it is important that you understand it properly to avoid trouble in
complex cases. The remainder of the book consists of examples that demonstrate how to use Tcl and
Tk productively. For your reference, each chapter has tables that summarize the Tcl commands and Tk
widgets they describe.
This book assumes that you have some programming experience, although you should be able to get
by even if you are a complete novice. Knowledge of UNIX shell programming will help, but it is not
required. Where aspects of window systems are relevant, I provide some background information.
Chapter 2 describes the details of using Tcl and Tk on UNIX, Windows, and Macintosh.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
How to Read This Book
This book is best used in a hands-on manner, trying the examples at the computer. The book tries to
fill the gap between the terse Tcl and Tk manual pages, which are complete but lack context and
examples, and existing Tcl programs that may or may not be documented or well written.
I recommend the on-line manual pages for the Tcl and Tk commands. They provide a detailed
reference guide to each command. This book summarizes much of the information from the manual
pages, but it does not provide the complete details, which can vary from release to release. HTML
versions of the on-line manual pages can be found on the CD-ROM that comes with this book.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Other Tcl Books
This book was the second Tcl book after the original book by John Ousterhout, the creator of Tcl.
Since then, the number of Tcl books has increased remarkably. The following are just some of the
books currently available.
Tcl and the Tk Toolkit (Addison-Wesley, 1994) by John Ousterhout provides a broad overview of all
aspects of Tcl and Tk, even though it covers only Tcl 7.3 and Tk 3.6. The book provides a more
detailed treatment of C programming for Tcl extensions.
Exploring Expect (O'Reilly & Associates, Inc., 1995) by Don Libes is a great book about an extremely
useful Tcl extension. Expect lets you automate the use of interactive programs like ftp and telnet that
expect to interact with a user. By combining expect and Tk, you can create graphical user interfaces
for old applications that you cannot modify directly.
Graphical Applications with Tcl & Tk (M&T Press, 1996) by Eric Johnson is oriented toward
Windows users. The second edition is up-to-date with Tcl/Tk 8.0.
Tcl/Tk Tools (O'Reilly & Associates, Inc., 1997) by Mark Harrison describes many useful Tcl
extensions. These include Oracle and Sybase interfaces, object-oriented language enhancements,
additional Tk widgets, and much more. The chapters were contributed by the authors of the
extensions, so they provide authoritative information on some excellent additions to the Tcl toolbox.
CGI Developers Resource, Web Programming with Tcl and Perl (Prentice Hall, 1997) by John Ivler
presents Tcl-based solutions to programming Web sites.
Effective Tcl/Tk Programming (Addison Wesley, 1997) by Michael McLennan and Mark Harrison
illustrate Tcl and Tk with examples and application design guidelines.
Interactive Web Applications with Tcl/Tk (AP Professional, 1998) by Michael Doyle and Hattie
Schroeder describes Tcl programming in the context of the Web browser plugin.
Tcl/Tk for Programmers (IEEE Computer Society, 1998) by Adrian Zimmer describes Unix and
Windows programming with Tcl/Tk. This book also includes solved exercises at the end of each
chapter.
Tcl/Tk for Real Programmers (Academic Press, 1999) by Clif Flynt is another example-oriented book.
Tcl/Tk in a Nutshell (O'Reilly, 1999) by Paul Raines and Jeff Tranter is a handy reference guide. It
covers several popular extensions including Expect, [incr Tcl], Tix, TclX, BLT, SybTcl, OraTcl, and
TclODBC. There is a tiny pocket-reference guide for Tcl/Tk that may eliminate the need to thumb
through my large book to find the syntax of a particular Tcl or Tk command.
Web Tcl Complete (McGraw Hill, 1999) by Steve Ball describes programming with the Tcl Web
Server. It also covers Tcl/Java integration using TclBlend.
[incr Tcl] From The Ground Up (Osborn-McGraw Hill, 1999) by Chad Smith describes the [incr Tcl]
object-oriented extension to Tcl.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
On-line Examples
The book comes with a CD-ROM that has source code for all of the examples, plus a selection of Tcl
freeware found on the Internet. The CD-ROM is created with the Linux mkhybrid program, so it is
readable on UNIX, Windows, and Macintosh. There, you will find the versions of Tcl and Tk that
were available as the book went to press. You can also retrieve the sources shown in the book from my
personal Web site:
http://www.beedub.com/book/
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Ftp Archives
The primary site for the Tcl and Tk distributions is given below as a Universal Resource Location
(URL):
ftp://ftp.scriptics.com/pub/tcl
You can use FTP and log in to the host (e.g., ftp.scriptics.com) under the anonymous user name. Give
your e-mail address as the password. The directory is in the URL after the host name (e.g., /pub/tcl).
There are many sites that mirror this distribution. The mirror sites provide an archive site for
contributed Tcl commands, Tk widgets, and applications. There is also a set of Frequently Asked
Questions files. These are some of the sites that maintain Tcl archives
ftp://ftp.neosoft.com/pub/tcl
ftp://ftp.syd.dit.csiro.au/pub/tk
ftp://ftp.ibp.fr/pub/tcl
ftp://src.doc.ic.ac.uk/packages/tcl/
ftp://ftp.luth.se/pub/unix/tcl/
ftp://sunsite.cnlab-switch.ch/mirror/tcl
ftp://ftp.sterling.com/programming/languages/tcl
ftp://ftp.sunet.se/pub/lang/tcl
ftp://ftp.cs.columbia.edu/archives/tcl
ftp://ftp.uni-paderborn.de/pub/unix/tcl
ftp://sunsite.unc.edu/pub/languages/tcl
ftp://ftp.funet.fi/pub/languages/tcl
You can use a World Wide Web browser like Mosaic, Netscape, Internet Explorer, or Lynx to access
these sites. Enter the URL as specified above, and you are presented with a directory listing of that
location. From there you can change directories and fetch files.
If you do not have direct FTP access, you can use an e-mail server for FTP. Send e-mail to
ftpmail@decwrl.dec.com with the message Help to get directions. If you are on BITNET, send e-mail
to bitftp@pucc.princeton.edu.
You can search for FTP sites that have Tcl by using the Archie service that indexes the contents of
anonymous FTP servers. Information about using Archie can be obtained by sending mail to
archie@archie.sura.net that contains the message Help.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
World Wide Web
Start with these World Wide Web pages about Tcl:
http://www.scriptics.com/
http://www.sco.com/Technology/tcl/Tcl.html
http://www.purl.org/NET/Tcl-FAQ/
The home page for this book contains errata for all editions. This is the only URL I control personally,
and I plan to keep it up-to-date indefinitely:
http://www.beedub.com/book/
The Prentice Hall Web site has information about the book, but you must use its search facility to find
the exact location. Start at:
http://www.prenhall.com/
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Newsgroups
The comp.lang.tcl newsgroup is very active. It provides a forum for questions and answers about
Tcl. Announcements about Tcl extensions and applications are posted to the
comp.lang.tcl.announce newsgroup.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Typographic Conventions
The more important examples are set apart with a title and horizontal rules, while others appear in-
line. The examples use courier for Tcl and C code. When interesting results are returned by a Tcl
command, those are presented below in oblique courier. The => is not part of the return value in the
following example.
expr 5 + 8
=> 13
The courier font is also used when naming Tcl commands and C procedures within sentences.
The usage of a Tcl command is presented in the following example. The command name and constant
keywords appear in courier. Variable values appear in courier oblique. Optional arguments are
surrounded with question marks.
set varname ?value?
The name of a program is in italics:
xterm
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Hot Tips
The icon in the margin marks a "hot tip" as judged by the reviewers of the book.
The visual markers help you locate the more useful sections in the book. These
are also listed in the index under Hot Tip.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Book Organization
The chapters of the book are divided into seven parts. The first part describes basic Tcl features. The
first chapter describes the fundamental mechanisms that characterize the Tcl language. This is an
important chapter that provides the basic grounding you will need to use Tcl effectively. Even if you
have programmed in Tcl already, you should review Chapter 1. Chapter 2 goes over the details of
using Tcl and Tk on UNIX, Windows, and Macintosh. Chapter 3 presents a sample application, a CGI
script, that illustrates typical Tcl programming. The rest of Part I covers the basic Tcl commands in
more detail, including string handling, data types, control flow, procedures, and scoping issues. Part I
finishes with a description of the facilities for file I/O and running other programs.
Part II describes advanced Tcl programming. It starts with eval, which lets you generate Tcl programs
on the fly. Regular expressions provide powerful string processing. If your data-processing application
runs slowly, you can probably boost its performance significantly with the regular expression facilities.
Namespaces partition the global scope of procedures and variables. Unicode and message catalogs
support internationalized applications. Libraries and packages provide a way to organize your code for
sharing among projects. The introspection facilities of Tcl tell you about the internal state of Tcl.
Event driven I/O helps server applications manage several clients simultaneously. Network sockets are
used to implement the HTTP protocol used to fetch pages on the World Wide Web. Safe-Tcl is used to
provide a secure environment to execute applets downloaded over the network. TclHttpd is an
extensible web server built in Tcl. You can build applications on top of this server, or embed it into
your existing applications to give them a web interface.
Part III introduces Tk. It gives an overview of the toolkit facilities. A few complete examples are
examined in detail to illustrate the features of Tk. Event bindings associate Tcl commands with events
like keystrokes and button clicks. Part III ends with three chapters on the Tk geometry managers that
provide powerful facilities for organizing your user interface.
Part IV describes the Tk widgets. These include buttons, menus, scrollbars, labels, text entries,
multiline and multifont text areas, drawing canvases, listboxes, and scales. The Tk widgets are highly
configurable and very programmable, but their default behaviors make them easy to use as well. The
resource database that can configure widgets provides an easy way to control the overall look of your
application.
Part V describes the rest of the Tk facilities. These include selections, keyboard focus, and standard
dialogs. Fonts, colors, images, and other attributes that are common to the Tk widgets are described in
detail. This part ends with a few larger Tk examples.
Part VI is an introduction to C programming and Tcl. The goal of this part is to get you started in the
right direction when you need to extend Tcl with new commands written in C or integrate Tcl into
custom applications.
Part VII provides a chapter for each of the Tcl/Tk releases covered by the book. These chapters
provide details about what features were changed and added. They also provide a quick reference if
you need to update a program or start to use a new version.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
What's New in the Third Edition
The third edition is up-to-date with Tcl/Tk 8.2. The main new Tcl/Tk features are Internationalization,
which is covered in Chapter 15, a new regular expression engine, which is described in Chapter 11,
and thread-safety. There is a new chapter about compiling C extensions, and there is a more complete
C extension example. The chapters on Eval and the Web browser plugin received a thorough update. I
made a light sweep through the remainder of the book correcting errors and improving examples.
Perhaps the best addition for the reader is an all-new index.
My favorite addition to the book is Chapter 18 that describes TclHttpd, a Web server built in Tcl.
TclHttpd provides a number of nice ways to integrate a Web server with a Tcl application, replacing
the standard CGI interface with something that is much more flexible and efficient. I have been using
this server for the last year to build www.scriptics.com. This freely available server has been used to
build several other products, plus it provides an easy way for you to bring up your own Web server.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
First Edition Thanks
I would like to thank my managers and colleagues at Xerox PARC for their patience with me as I
worked on this book. The tips and tricks in this book came partly from my own work as I helped lab
members use Tcl, and partly from them as they taught me. Dave Nichols' probing questions forced me
to understand the basic mechanisms of the Tcl interpreter. Dan Swinehart and Lawrence Butcher kept
me sharp with their own critiques. Ron Frederick and Berry Kerchival adopted Tk for their graphical
interfaces and amazed me with their rapid results. Becky Burwell, Rich Gold, Carl Hauser, John
Maxwell, Ken Pier, Marvin Theimer, and Mohan Vishwanath made use of my early drafts, and their
questions pointed out large holes in the text. Karin Petersen, Bill Schilit, and Terri Watson kept life
interesting by using Tcl in very nonstandard ways. I especially thank my managers, Mark Weiser and
Doug Terry, for their understanding and support.
I thank John Ousterhout for Tcl and Tk, which are wonderful systems built with excellent
craftsmanship. John was kind enough to provide me with an advance version of Tk 4.0 so that I could
learn about its new features well before its first beta release.
Thanks to the Tcl programmers out on the Net, from whom I learned many tricks. John LoVerso and
Stephen Uhler are the hottest Tcl programmers I know.
Many thanks to the patient reviewers of early drafts: Pierre David, Clif Flynt, Simon Kenyon, Eugene
Lee, Don Libes, Lee Moore, Joe Moss, Hador Shemtov, Frank Stajano, Charles Thayer, and Jim
Thornton.
Many folks contributed suggestions by e-mail: Miguel Angel, Stephen Bensen, Jeff Blaine, Tom
Charnock, Brian Cooper, Patrick D'Cruze, Benoit Desrosiers, Ted Dunning, Mark Eichin, Paul
Friberg, Carl Gauthier, David Gerdes, Klaus Hackenberg, Torkle Hasle, Marti Hearst, Jean-Pierre
Herbert, Jamie Honan, Norman Klein, Joe Konstan, Susan Larson, Hkan Liljegren, Lionel Mallet,
Dejan Milojicic, Greg Minshall, Bernd Mohr, Will Morse, Heiko Nardmann, Gerd Neugebauer, TV
Raman, Cary Renzema, Rob Riepel, Dan Schenk, Jean-Guy Schneider, Elizabeth Scholl, Karl
Schwamb, Rony Shapiro, Peter Simanyi, Vince Skahan, Bill Stumbo, Glen Vanderburg, Larry Virden,
Reed Wade, and Jim Wight. Unfortunately, I could not respond to every suggestion, even some that
were excellent.
Thanks to the editors and staff at Prentice Hall. Mark Taub has been very helpful as I progressed
through my first book. Lynn Schneider and Kerry Reardon were excellent copy and production editors,
respectively.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Second Edition Thanks
I get to thank John Ousterhout again, this time for supporting me as I worked in the Tcl/Tk group at
Sun Microsystems. The rest of the group deserve a lot of credit for turning Tcl and Tk into a dynamite
cross-platform solution. Scott Stanton led the Tk port to the PC. Ray Johnson led the Tk port to the
Macintosh. Jacob Levy implemented the event-driven I/O system, Safe-Tcl, and the browser plug-in.
Brian Lewis built the Tcl compiler. Ken Corey worked on Java integration and helped with the
SpecTcl user interface builder. Syd Polk generalized the menu system to work with native widgets on
the Macintosh and Windows. Colin Stevens generalized the font mechanism and worked on
internationalization for Tk.
Stephen Uhler deserves special thanks for inspiring many of the cool examples I use in this book. He
was the lead on the SpecTcl user interface builder. He built the core HTML display library on which I
based an editor. We worked closely together on the first versions of TclHttpd. He taught me how to
write compact, efficient Tcl code and to use regular expression substitutions in amazing ways. I hope
he has learned at least a little from me.
Thanks again to Mark Taub, Eileen Clark, and Martha Williams at Prentice Hall. George Williams
helped me assemble the files for the CD-ROM.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Third Edition Thanks
John Ousterhout continues his wonderful role as Tcl benefactor, now as founder of Scriptics
Corporation. I'd like to thank every one of the great folks that I work with at Scriptics, especially the
pioneering crew of Sarah Daniels, Scott Stanton, Ray Johnson, Bryan Surles, Melissa Hirschl, Lee
Bernhard, Suresh Sastry, Emil Scaffon, Pat P., Scott Redman, and Berry Kercheval. The rest of the
gang deserves a big thanks for making Scriptics such an enjoyable place to work. Jerry Peek, who is a
notable author himself, provided valuable advice and wonderfully detailed comments! Ken Jones told
me about a great indexing tool.
I'd like to thank all the readers that drop me the encouraging note or probing question via e-mail. I am
always interested in new and interesting uses of Tcl!
Thanks to the editors at Prentice Hall: Mark Taub, Joan McNamara, and Joan Eurell. Mark continues
to encourage me to come out with new editions, and the Joans helped me complete this third edition
on time.
Finally, I thank my wonderful wife Jody for her love, kindness, patience, wit, and understanding as I
worked long hours. Happily, many of those hours were spent working from home. I now have three
sons, Christopher, Daniel, and Michael, who get the credit for keeping me from degenerating into a
complete nerd.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Preface
Contact the Author
I am always open to comments about this book. My e-mail address is welch@acm.org. It helps me sort
through my mail if you put the word "book" or the title of the book into the e-mail subject line. Visit
my Web site at:
http://www.beedub.com/
for current news about the book and my other interests.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Part I: Tcl Basics
Part I introduces the basics of Tcl. Everyone should read Chapter 1, which describes the
fundamental properties of the language. Tcl is really quite simple, so beginners can pick it up
quickly. The experienced programmer should review Chapter 1 to eliminate any misconceptions
that come from using other languages.
Chapter 2 is a short introduction to running Tcl and Tk on UNIX, Windows, and Macintosh
systems. You may want to look at this chapter first so you can try out the examples as you read
Chapter 1.
Chapter 3 presents a sample application, a CGI script, that implements a guestbook for a Web
site. The example uses several facilities that are described in detail in later chapters. The goal is
to provide a working example that illustrates the power of Tcl.
The rest of Part I covers basic programming with Tcl. Simple string processing is covered in
Chapter 4. Tcl lists, which share the syntax rules of Tcl commands, are explained in Chapter 5.
Control structure like loops and if statements are described in Chapter 6. Chapter 7 describes Tcl
procedures, which are new commands that you write in Tcl. Chapter 8 discusses Tcl arrays.
Arrays are the most flexible and useful data structure in Tcl. Chapter 9 describes file I/O and
running other programs. These facilities let you build Tcl scripts that glue together other
programs and process data in files.
After reading Part I you will know enough Tcl to read and understand other Tcl programs, and to
write simple programs yourself.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartI. Tcl Basics
Chapter 1. Tcl Fundamentals
This chapter describes the basic syntax rules for the Tcl scripting language. It describes the basic
mechanisms used by the Tcl interpreter: substitution and grouping. It touches lightly on the following
Tcl commands: puts, format, set, expr, string, while, incr, and proc.
Tcl is a string-based command language. The language has only a few fundamental constructs and
relatively little syntax, which makes it easy to learn. The Tcl syntax is meant to be simple. Tcl is
designed to be a glue that assembles software building blocks into applications. A simpler glue makes
the job easier. In addition, Tcl is interpreted when the application runs. The interpreter makes it easy to
build and refine your application in an interactive manner. A great way to learn Tcl is to try out
commands interactively. If you are not sure how to run Tcl on your system, see Chapter 2 for
instructions for starting Tcl on UNIX, Windows, and Macintosh systems.
This chapter takes you through the basics of the Tcl language syntax. Even if you are an expert
programmer, it is worth taking the time to read these few pages to make sure you understand the
fundamentals of Tcl. The basic mechanisms are all related to strings and string substitutions, so it is
fairly easy to visualize what is going on in the interpreter. The model is a little different from some
other programming languages with which you may already be familiar, so it is worth making sure you
understand the basic concepts.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Tcl Commands
Tcl stands for Tool Command Language. A command does something for you, like output a string,
compute a math expression, or display a widget on the screen. Tcl casts everything into the mold of a
command, even programming constructs like variable assignment and procedure definition. Tcl adds a
tiny amount of syntax needed to properly invoke commands, and then it leaves all the hard work up to
the command implementation.
The basic syntax for a Tcl command is:
command arg1 arg2 arg3 ...
The command is either the name of a built-in command or a Tcl procedure. White space (i.e., spaces or
tabs) is used to separate the command name and its arguments, and a newline (i.e., the end of line
character) or semicolon is used to terminate a command. Tcl does not interpret the arguments to the
commands except to perform grouping, which allows multiple words in one argument, and
substitution, which is used with programming variables and nested command calls. The behavior of
the Tcl command processor can be summarized in three basic steps:
Argument grouping.
Value substitution of nested commands, variables, and backslash escapes.
Command invocation. It is up to the command to interpret its arguments.
This model is described in detail in this Chapter.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Hello, World!
Example 1-1 The "Hello, World!" example.
puts stdout {Hello, World!}
=> Hello, World!
In this example, the command is puts, which takes two arguments: an I/O stream identifier and a
string. puts writes the string to the I/O stream along with a trailing newline character. There are two
points to emphasize:
Arguments are interpreted by the command. In the example, stdout is used to identify the
standard output stream. The use of stdout as a name is a convention employed by puts and the
other I/O commands. Also, stderr is used to identify the standard error output, and stdin is
used to identify the standard input. Chapter 9 describes how to open other files for I/O.
Curly braces are used to group words together into a single argument. The puts command
receives Hello, World! as its second argument.
The braces are not part of the value.
The braces are syntax for the interpreter, and they get stripped off before the value is passed to the
command. Braces group all characters, including newlines and nested braces, until a matching brace is
found. Tcl also uses double quotes for grouping. Grouping arguments will be described in more detail
later.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Variables
The set command is used to assign a value to a variable. It takes two arguments: The first is the name
of the variable, and the second is the value. Variable names can be any length, and case is significant.
In fact, you can use any character in a variable name.
It is not necessary to declare Tcl variables before you use them.
The interpreter will create the variable when it is first assigned a value. The value of a variable is
obtained later with the dollar-sign syntax, illustrated in Example 1-2:
Example 1-2 Tcl variables.
set var 5
=> 5
set b $var
=> 5
The second set command assigns to variable b the value of variable var. The use of the dollar sign is
our first example of substitution. You can imagine that the second set command gets rewritten by
substituting the value of var for $var to obtain a new command.
set b 5
The actual implementation of substitution is more efficient, which is important when the value is
large.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Command Substitution
The second form of substitution is command substitution. A nested command is delimited by square
brackets, [ ]. The Tcl interpreter takes everything between the brackets and evaluates it as a
command. It rewrites the outer command by replacing the square brackets and everything between
them with the result of the nested command. This is similar to the use of backquotes in other shells,
except that it has the additional advantage of supporting arbitrary nesting of commands.
Example 1-3 Command substitution.
set len [string length foobar]
=> 6
In Example 1-3, the nested command is:
string length foobar
This command returns the length of the string foobar. The string command is described in detail
starting on page 45. The nested command runs first.
Then, command substitution causes the outer command to be rewritten as if it were:
set len 6
If there are several cases of command substitution within a single command, the interpreter processes
them from left to right. As each right bracket is encountered, the command it delimits is evaluated.
This results in a sensible ordering in which nested commands are evaluated first so that their result can
be used in arguments to the outer command.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Math Expressions
The Tcl interpreter itself does not evaluate math expressions. Tcl just does grouping, substitutions and
command invocations. The expr command is used to parse and evaluate math expressions.
Example 1-4 Simple arithmetic.
expr 7.2 / 4
=> 1.8
The math syntax supported by expr is the same as the C expression syntax. The expr command deals
with integer, floating point, and boolean values. Logical operations return either 0 (false) or 1 (true).
Integer values are promoted to floating point values as needed. Octal values are indicated by a leading
zero (e.g., 033 is 27 decimal). Hexadecimal values are indicated by a leading 0x. Scientific notation
for floating point numbers is supported. A summary of the operator precedence is given on page 20.
You can include variable references and nested commands in math expressions. The following
example uses expr to add the value of x to the length of the string foobar. As a result of the innermost
command substitution, the expr command sees 6 + 7, and len gets the value 13:
Example 1-5 Nested commands.
set x 7
set len [expr [string length foobar] + $x]
=> 13
The expression evaluator supports a number of built-in math functions. (For a complete listing, see
page 21.) Example 1-6 computes the value of pi:
Example 1-6 Built-in math functions.
remaining arguments to format are to be formatted. Note that the trailing \n usually found in a C
printf call is not needed because puts provides one for us. For more information about the format
command, see page 52.
Square Brackets Do Not Group
The square bracket syntax used for command substitution does not provide grouping. Instead, a nested
command is considered part of the current group. In the command below, the double quotes group the
last argument, and the nested command is just part of that group.
puts stdout "The length of $s is [string length $s]."
If an argument is made up only of a nested command, you do not need to group it with double-quotes
because the Tcl parser treats the whole nested command as part of the group.
puts stdout [string length $s]
The following is a redundant use of double quotes:
puts stdout "[expr $x + $y]"
Grouping before Substitution
The Tcl parser makes a single pass through a command as it makes grouping decisions and performs
string substitutions. Grouping decisions are made before substitutions are performed, which is an
important property of Tcl. This means that the values being substituted do not affect grouping because
the grouping decisions have already been made.
The following example demonstrates how nested command substitution affects grouping. A nested
command is treated as an unbroken sequence of characters, regardless of its internal structure. It is
included with the surrounding group of characters when collecting arguments for the main command.
Example 1-11 Embedded command and variable substitution.
set x 7; set y 9
puts stdout $x+$y=[expr $x + $y]
=> 7+9=16
In Example 1-11, the second argument to puts is:
$x+$y=[expr $x + $y]
The white space inside the nested command is ignored for the purposes of grouping the argument. By
the time Tcl encounters the left bracket, it has already done some variable substitutions to obtain:
7+9=
When the left bracket is encountered, the interpreter calls itself recursively to evaluate the nested
command. Again, the $x and $y are substituted before calling expr. Finally, the result of expr is
substituted for everything from the left bracket to the right bracket. The puts command gets the
following as its second argument:
7+9=16
Grouping before substitution.
The point of this example is that the grouping decision about puts's second argument is made before
the command substitution is done. Even if the result of the nested command contained spaces or other
special characters, they would be ignored for the purposes of grouping the arguments to the outer
command. Grouping and variable substitution interact the same as grouping and command
substitution. Spaces or special characters in variable values do not affect grouping decisions because
these decisions are made before the variable values are substituted.
If you want the output to look nicer in the example, with spaces around the + and =, then you must use
double quotes to explicitly group the argument to puts:
puts stdout "$x + $y = [expr $x + $y]"
The double quotes are used for grouping in this case to allow the variable and command substitution
on the argument to puts.
Grouping Math Expressions with Braces
It turns out that expr does its own substitutions inside curly braces. This is explained in more detail on
page 15. This means you can write commands like the one below and the substitutions on the variables
in the expression still occur:
puts stdout "$x + $y = [expr {$x + $y}]"
More Substitution Examples
If you have several substitutions with no white space between them, you can avoid grouping with
quotes. The following command sets concat to the value of variables a, b, and c all concatenated
together:
set concat $a$b$c
Again, if you want to add spaces, you'll need to use quotes:
set concat "$a $b $c"
In general, you can place a bracketed command or variable reference anywhere. The following
computes a command name:
[findCommand $x] arg arg
When you use Tk, you often use widget names as command names:
$text insert end "Hello, World!"
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Procedures
Tcl uses the proc command to define procedures. Once defined, a Tcl procedure is used just like any
of the other built-in Tcl commands. The basic syntax to define a procedure is:
proc name arglist body
The first argument is the name of the procedure being defined. The second argument is a list of
parameters to the procedure. The third argument is a command body that is one or more Tcl
commands.
The procedure name is case sensitive, and in fact it can contain any characters. Procedure names and
variable names do not conflict with each other. As a convention, this book begins procedure names
with uppercase letters and it begins variable names with lowercase letters. Good programming style is
important as your Tcl scripts get larger. Tcl coding style is discussed in Chapter 12.
Example 1-12 Defining a procedure.
proc Diag {a b} {
set c [expr sqrt($a * $a + $b * $b)]
return $c
}
puts "The diagonal of a 3, 4 right triangle is [Diag 3 4]"
=> The diagonal of a 3, 4 right triangle is 5.0
The Diag procedure defined in the example computes the length of the diagonal side of a right triangle
given the lengths of the other two sides. The sqrt function is one of many math functions supported
by the expr command. The variable c is local to the procedure; it is defined only during execution of
Diag. Variable scope is discussed further in Chapter 7. It is not really necessary to use the variable c in
this example. The procedure can also be written as:
proc Diag {a b} {
return [expr sqrt($a * $a + $b * $b)]
}
The return command is used to return the result of the procedure. The return command is optional in
this example because the Tcl interpreter returns the value of the last command in the body as the value
of the procedure. So, the procedure could be reduced to:
proc Diag {a b} {
expr sqrt($a * $a + $b * $b)
}
Note the stylized use of curly braces in the example. The curly brace at the end of the first line starts
the third argument to proc, which is the command body. In this case, the Tcl interpreter sees the
opening left brace, causing it to ignore newline characters and scan the text until a matching right
brace is found. Double quotes have the same property. They group characters, including newlines,
until another double quote is found. The result of the grouping is that the third argument to proc is a
sequence of commands. When they are evaluated later, the embedded newlines will terminate each
command.
The other crucial effect of the curly braces around the procedure body is to delay any substitutions in
the body until the time the procedure is called. For example, the variables a, b, and c are not defined
until the procedure is called, so we do not want to do variable substitution at the time Diag is defined.
The proc command supports additional features such as having variable numbers of arguments and
default values for arguments. These are described in detail in Chapter 7.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
A Factorial Example
To reinforce what we have learned so far, below is a longer example that uses a while loop to
compute the factorial function:
Example 1-13 A while loop to compute factorial.
proc Factorial {x} {
set i 1; set product 1
while {$i <= $x} {
set product [expr $product * $i]
incr i
}
return $product
}
Factorial 10
=> 3628800
The semicolon is used on the first line to remind you that it is a command terminator just like the
newline character. The while loop is used to multiply all the numbers from one up to the value of x.
The first argument to while is a boolean expression, and its second argument is a command body to
execute. The while/ command and other control structures are described in Chapter 6.
The same math expression evaluator used by the expr command is used by while to evaluate the
boolean expression. There is no need to explicitly use the expr command in the first argument to
while, even if you have a much more complex expression.
The loop body and the procedure body are grouped with curly braces in the same way. The opening
curly brace must be on the same line as proc and while. If you like to put opening curly braces on the
line after a while or if statement,
you must escape the newline with a backslash:
The Tcl interpreter makes some assumptions about variable names that make it easy to embed variable
references into other strings. By default, it assumes that variable names contain only letters, digits, and
the underscore. The construct $foo.o represents a concatenation of the value of foo and the literal
".o".
If the variable reference is not delimited by punctuation or white space, then you can use curly braces
to explicitly delimit the variable name (e.g., ${x}). You can also use this to reference variables with
funny characters in their name, although you probably do not want variables named like that. If you
find yourself using funny variable names, or computing the names of variables, then you may want to
use the upvar command.
Example 1-16 Embedded variable references.
set foo filename
set object $foo.o
=> filename.o
set a AAA
set b abc${a}def
=> abcAAAdef
set .o yuk!
set x ${.o}y
=> yuk!y
The unset Command
You can delete a variable with the unset command:
unset varName varName2 ...
Any number of variable names can be passed to the unset command. However, unset will raise an
error if a variable is not already defined.
Using info to Find Out about Variables
The existence of a variable can be tested with the info exists command. For example, because incr
requires that a variable exist, you might have to test for the existence of the variable first.
Example 1-17 Using info to determine if a variable exists.
if {![info exists foobar]} {
set foobar 0
} else {
incr foobar
}
Example 7-6 on page 86 implements a new version of incr which handles this case.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
More about Math Expressions
This section describes a few fine points about math in Tcl scripts. In Tcl 7.6 and earlier versions math
is not that efficient because of conversions between strings and numbers. The expr command must
convert its arguments from strings to numbers. It then does all its computations with double precision
floating point values. The result is formatted into a string that has, by default, 12 significant digits.
This number can be changed by setting the tcl_precision variable to the number of significant digits
desired. Seventeen digits of precision are enough to ensure that no information is lost when converting
back and forth between a string and an IEEE double precision number:
Example 1-18 Controlling precision with tcl_precision.
expr 1 / 3
=> 0
expr 1 / 3.0
=> 0.333333333333
set tcl_precision 17
=> 17
expr 1 / 3.0
# The trailing 1 is the IEEE rounding digit
=> 0.33333333333333331
In Tcl 8.0 and later versions, the overhead of conversions is eliminated in most cases by the built-in
compiler. Even so, Tcl was not designed to support math-intensive applications. You may want to
implement math-intensive code in a compiled language and register the function as a Tcl command as
described in Chapter 44.
There is support for string comparisons by expr, so you can test string values in if statements. You
must use quotes so that expr knows to do string comparisons:
if {$answer == "yes"} {... }
However, the string compare and string equal commands described in Chapter 4 are more
reliable because expr may do conversions on strings that look like numbers. The issues with string
operations and expr are discussed on page 48.
Expressions can include variable and command substitutions and still be grouped with curly braces.
This is because an argument to expr is subject to two rounds of substitution: one by the Tcl interpreter,
and a second by expr itself. Ordinarily this is not a problem because math values do not contain the
characters that are special to the Tcl interpreter. The second round of substitutions is needed to support
commands like while and if that use the expression evaluator internally.
Grouping expressions can make them run more efficiently.
You should always group expressions in curly braces and let expr do command and variable
substitutions. Otherwise, your values may suffer extra conversions from numbers to strings and back
to numbers. Not only is this process slow, but the conversions can loose precision in certain
circumstances. For example, suppose x is computed from a math function:
set x [expr {sqrt(2.0)}]
At this point the value of x is a double-precision floating point value, just as you would expect. If you
do this:
set two [expr $x * $x]
then you may or may not get 2.0 as the result! This is because Tcl will substitute $x and expr will
concatenate all its arguments into one string, and then parse the expression again. In contrast, if you do
this:
set two [expr {$x * $x}]
then expr will do the substitutions, and it will be careful to preserve the floating point value of x. The
expression will be more accurate and run more efficiently because no string conversions will be done.
The story behind Tcl values is described in more detail in Chapter 44 on C programming and Tcl.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Comments
Tcl uses the pound character, #, for comments. Unlike in many other languages, the # must occur at
the beginning of a command. A # that occurs elsewhere is not treated specially. An easy trick to
append a comment to the end of a command is to precede the # with a semicolon to terminate the
previous command:
# Here are some parameters
set rate 7.0 ;# The interest rate
set months 60 ;# The loan term
One subtle effect to watch for is that a backslash effectively continues a comment line onto the next
line of the script. In addition, a semicolon inside a comment is not significant. Only a newline
terminates comments:
# Here is the start of a Tcl comment \
and some more of it; still in the comment
The behavior of a backslash in comments is pretty obscure, but it can be exploited as shown in
Example 2-3 on page 27.
A surprising property of Tcl comments is that curly braces inside comments are still counted for the
purposes of finding matching brackets. I think the motivation for this mis-feature was to keep the
original Tcl parser simpler. However, it means that the following will not work as expected to
comment out an alternate version of an if expression:
# if {boolean expression1} {
if {boolean expression2} {
some commands
}
The previous sequence results in an extra left curly brace, and probably a complaint about a missing
close brace at the end of your script! A technique I use to comment out large chunks of code is to put
the code inside an if block that will never execute:
if {0} {
unused code here
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Substitution and Grouping Summary
The following rules summarize the fundamental mechanisms of grouping and substitution that are
performed by the Tcl interpreter before it invokes a command:
Command arguments are separated by white space, unless arguments are grouped with curly
braces or double quotes as described below.
Grouping with curly braces, { }, prevents substitutions. Braces nest. The interpreter includes all
characters between the matching left and right brace in the group, including newlines,
semicolons, and nested braces. The enclosing (i.e., outermost) braces are not included in the
group's value.
Grouping with double quotes, " ", allows substitutions. The interpreter groups everything until
another double quote is found, including newlines and semicolons. The enclosing quotes are not
included in the group of characters. A double-quote character can be included in the group by
quoting it with a backslash, (e.g., \").
Grouping decisions are made before substitutions are performed, which means that the values of
variables or command results do not affect grouping.
A dollar sign, $, causes variable substitution. Variable names can be any length, and case is
significant. If variable references are embedded into other strings, or if they include characters
other than letters, digits, and the underscore, they can be distinguished with the ${varname}
syntax.
Square brackets, [ ], cause command substitution. Everything between the brackets is treated as
a command, and everything including the brackets is replaced with the result of the command.
Nesting is allowed.
The backslash character, \, is used to quote special characters. You can think of this as another
form of substitution in which the backslash and the next character or group of characters are
replaced with a new character.
Substitutions can occur anywhere unless prevented by curly brace grouping. Part of a group can
be a constant string, and other parts of it can be the result of substitutions. Even the command
name can be affected by substitutions.
the current group. The following sets x to the concatenation of two command results because
there is no space between ] and [.
set x [cmd1][cmd2]
Newlines and semicolons are ignored when grouping with braces or double quotes. They get
included in the group of characters just like all the others. The following sets x to a string that
contains newlines:
set x "This is line one.
This is line two.
This is line three."
During command substitution, newlines and semicolons are significant as command terminators.
If you have a long command that is nested in square brackets, put a backslash before the newline
if you want to continue the command on another line. This was illustrated in Example 1-9 on
page 8.
A dollar sign followed by something other than a letter, digit, underscore, or left parenthesis is
treated as a literal dollar sign. The following sets x to the single character $.
set x $
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter1. Tcl Fundamentals
Reference
Backslash Sequences
Table 1-1. Backslash sequences.
\a
Bell. (0x7)
\b
Backspace. (0x8)
\f
Form feed. (0xc)
\n
Newline. (0xa)
\r
Carriage return. (0xd)
\t
Tab. (0x9)
\v
Vertical tab. (0xb)
\<newline>
Replace the newline and the leading white space on the next line with a space.
\\
Backslash. ('\')
\ooo Octal specification of character code. 1, 2, or 3 digits.
\xhh Hexadecimal specification of character code. 1 or 2 digits.
\uhhhh Hexadecimal specification of a 16-bit Unicode character value. 4 hex digits.
\c Replaced with literal c if c is not one of the cases listed above. In particular, \$, \", \{,
\}, \], and \[ are used to obtain these characters.
Arithmetic Operators
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter2. Getting Started
The source Command
You can enter Tcl commands interactively at the % prompt. It is a good idea to try out the examples in
this book as you read along. The highlighted examples from the book are on the CD-ROM in the
exsource folder. You can edit these scripts in your favorite editor. Save your examples to a file and
then execute them with the Tcl source command:
source filename
The source command reads Tcl commands from a file and evaluates them just as if you had typed
them interactively.
Chapter 3 develops a sample application. To get started, just open an editor on a file named cgi1.tcl.
Each time you update this file you can save it, reload it into Tcl with the source command, and test it
again. Development goes quickly because you do not wait for things to compile!
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter2. Getting Started
UNIX Tcl Scripts
On UNIX you can create a stand alone Tcl or Tcl/Tk script much like an sh or csh script. The trick is
in the first line of the file that contains your script. If the first line of a file begins with #!pathname,
then UNIX uses pathname as the interpreter for the rest of the script. The "Hello, World!" program
from Chapter 1 is repeated in Example 2-1 with the special starting line:
Example 2-1 A standalone Tcl script on UNIX.
#!/usr/local/bin/tclsh
puts stdout {Hello, World!}
Similarly, the Tk hello world program from Chapter 21 is shown in Example 2-2:
Example 2-2 A standalone Tk script on UNIX.
#!/usr/local/bin/wish
button .hello -text Hello -command {puts "Hello, World!"}
pack .hello -padx 10 -pady 10
The actual pathnames for tclsh and wish may be different on your system. If you type the pathname for
the interpreter wrong, you receive a confusing "command not found" error. You can find out the
complete pathname of the Tcl interpreter with the info nameofexecutable command. This is what
appears on my system:
info nameofexecutable
=> /home/welch/install/solaris/bin/tclsh8.2
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter2. Getting Started
The Macintosh and ResEdit
If you want to create a self-contained Tcl/Tk application on Macintosh, you must copy the Wish
program and add a Macintosh resource named tclshrc that has the start-up Tcl code. The Tcl code
can be a single source command that reads your script file. Here are step-by-step instructions to create
the resource using ResEdit:
First, make a copy of Wish and open the copy in ResEdit.
Pull down the Resource menu and select Create New Resource operation to make a new TEXT
resource.
ResEdit opens a window and you can type in text. Type in a source command that names your
script:
source "Hard Disk:Tcl/Tk 8.1:Applications:MyScript.tcl"
Set the name of the resource to be tclshrc. You do this through the Get Resource Info dialog
under the Resources menu in ResEdit.
This sequence of commands is captured in an application called "Drag n Drop Tclets", which comes
with the Macintosh Tcl distribution. If you drag a Tcl script onto this icon, it will create a copy of Wish
and create the tclshrc text resource that has a source command that will load that script.
If you have a Macintosh development environment, you can build a version of Wish that has additional
resources built right in. You add the resources to the applicationInit.r file. If a resource contains
Tcl code, you use it like this:
source -rcrc resource
If you don't want to edit resources, you can just use the Wish Source menu to select a script to run.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter2. Getting Started
The console Command
The Windows and Macintosh platforms have a built-in console that is used to enter Tcl commands
interactively. You can control this console with the console command. The console is visible by
default. Hide the console like this:
console hide
Display the console like this:
console show
The console is implemented by a second Tcl interpreter. You can evaluate Tcl commands in that
interpreter with:
console eval command
There is an alternate version of this console called TkCon. It is included on the CD-ROM, and you can
find current versions on the Internet. TkCon was created by Jeff Hobbs and has lots of nice features.
You can use TkCon on Unix systems, too.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter2. Getting Started
Command-Line Arguments
If you run a script from the command line, for example from a UNIX shell, you can pass the script
command-line arguments. You can also specify these arguments in the shortcut command in
Windows. For example, under UNIX you can type this at a shell:
% myscript.tcl arg1 arg2 arg3
In Windows, you can have a shortcut that runs wish on your script and also passes additional
arguments:
"c:\Program Files\TCL82\wish.exe" c:\your\script.tcl arg1
The Tcl shells pass the command-line arguments to the script as the value of the argv variable. The
number of command-line arguments is given by the argc variable. The name of the program, or script,
is not part of argv nor is it counted by argc. Instead, it is put into the argv0 variable. Table 2-2 lists all
the predefined variables in the Tcl shells. argv is a list, so you can use the lindex command, which is
described on page 59, to extract items from it:
set arg1 [lindex $argv 0]
The following script prints its arguments (foreach is described on page 73):
Example 2-4 The EchoArgs script.
# Tcl script to echo command line arguments
puts "Program: $argv0"
puts "Number of arguments: $argc"
set i 0
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartI. Tcl Basics
Chapter 3. The Guestbook CGI Application
This chapter presents a simple Tcl program that computes a Web page. The chapter provides a brief
background to HTML and the CGI interface to Web servers.
This chapter presents a complete, but simple guestbook program that computes an HTML document,
or Web page, based on the contents of a simple database. The basic idea is that a user with a Web
browser visits a page that is computed by the program. The details of how the page gets from your
program to the user with the Web browser vary from system to system. The Tcl Web Server described
in Chapter 18 comes with this guestbook example already set up. You can also use these scripts on
your own Web server, but you will need help from your Webmaster to set things up.
The chapter provides a very brief introduction to HTML and CGI programming. HTML is a way to
specify text formatting, including hypertext links to other pages on the World Wide Web. CGI is a
standard for communication between a Web server that delivers documents and a program that
computes documents for the server. There are many books on these subjects alone. CGI Developers
Resource, Web Programming with Tcl and Perl by John Ivler (Prentice Hall, 1997) is a good reference
for details that are left unexplained here.
A guestbook is a place for visitors to sign their name and perhaps provide other information. We will
build a guestbook that takes advantage of the World Wide Web. Our guests can leave their address as
a Universal Resource Location (URL). The guestbook will be presented as a page that has hypertext
links to all these URLs so that other guests can visit them. The program works by keeping a simple
database of the guests, and it generates the guestbook page from the database.
The Tcl scripts described in this chapter use commands and techniques that are described in more
detail in later chapters. The goal of the examples is to demonstrate the power of Tcl without
explaining every detail. If the examples in this chapter raise questions, you can follow the references to
examples in other chapters that do go into more depth.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter3. The Guestbook CGI Application
A Quick Introduction to HTML
Web pages are written in a text markup language called HTML (HyperText Markup Language). The
idea of HTML is that you annotate, or mark up, regular text with special tags that indicate structure
and formatting. For example, the title of a Web page is defined like this:
<TITLE>My Home Page</TITLE>
The tags provide general formatting guidelines, but the browsers that display HTML pages have
freedom in how they display things. This keeps the markup simple. The general syntax for HTML tags
is:
<tag parameters>normal text</tag>
As shown here, the tags usually come in pairs. The open tag may have some parameters, and the close
tag name begins with a slash. The case of a tag is not considered, so <title>, <Title>, and <TITLE>
are all valid and mean the same thing. The corresponding close tag could be </title>, </Title>,
</TITLE>, or even </TiTlE>.
The <A> tag defines hypertext links that reference other pages on the Web. The hypertext links connect
pages into a Web so that you can move from page to page to page and find related information. It is
the flexibility of the links that make the Web so interesting. The <A> tag takes an HREF parameter that
defines the destination of the link. If you wanted to link to my home page, you would put this in your
page:
<A HREF="http://www.beedub.com/">Brent Welch</A>
When this construct appears in a Web page, your browser typically displays "Brent Welch" in blue
underlined text. When you click on that text, your browser switches to the page at the address
"http://www.beedub.com/". There is a lot more to HTML, of course, but this should give you a basic
idea of what is going on in the examples. The following list summarizes the HTML tags that will be
The clock command is used twice: once to get the current time in seconds, and a second time to
format the time into a nice looking string. The clock command is described in detail on page 173.
Fortunately, there is no conflict between the markup syntax used by HTML and the Tcl syntax for
embedded commands, so we can mix the two in the argument to the puts command. Double quotes
are used to group the argument to puts so that the clock commands will be executed. When run, the
output of the program will look like this:
Example 3-2 Output of Example 3-1.
Content-Type: text/html
<TITLE>The Current Time</TITLE>
The time is <B>Wed Oct 16 11:23:43 1996</B>
This example is a bit sloppy in its use of HTML, but it should display properly in most Web browsers.
Example 3-3 includes all the required tags for a proper HTML document.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter3. The Guestbook CGI Application
The guestbook.cgi Script
The guestbook.cgi script computes a page that lists all the registered guests. The example is shown
first, and then each part of it is discussed in more detail later. One thing to note right away is that the
HTML tags are generated by procedures that hide the details of the HTML syntax. The first lines of
the script use the UNIX trick to have tclsh interpret the script. This trick is described on page 26:
Example 3-3 The guestbook.cgi script.
#!/bin/sh
# guestbook.cgi
# Implement a simple guestbook page.
# The set of visitors is kept in a simple database.
# The newguest.cgi script will update the database.
# \
exec tclsh "$0" ${1+"$@"}
# The cgilib.tcl file has helper procedures
# The guestbook.data file has the database
# Both file are in the same directory as the script
set dir [file dirname [info script]]
source [file join $dir cgilib.tcl]
set datafile [file join $dir guestbook.data]
Cgi_Header "Brent's Guestbook" {BGCOLOR=white TEXT=black}
P
if {![file exists $datafile]} {
puts "No registered guests, yet."
P
puts "Be the first [Link {registered guest!}newguest.html]"
} else {
puts "The following folks have registered in my GuestBook."
P
puts [Link Register newguest.html]
H2 Guests
catch {source $datafile}
foreach name [lsort [array names Guestbook]] {
set item $Guestbook($name)
set homepage [lindex $item 0]
set markup [lindex $item 1]
H3 [Link $name $homepage]
puts $markup
}
}
Cgi_End
Using a Script Library File
The script uses a number of Tcl procedures that make working with HTML and the CGI interface
easier. These procedures are kept in the cgilib.tcl file, which is kept in the same directory as the
main script. The script starts by sourcing the cgilib.tcl file so that these procedures are available.
The following command determines the location of the cgilib.tcl file based on the location of the
main script. The info script command returns the file name of the script. The file dirname and
file join commands manipulate file names in a platform-independent way. They are described on
page 102. I use this trick to avoid putting absolute file names into my scripts, which would have to be
changed if the program moves later:
set dir [file dirname [info script]]
source [file join $dir cgilib.tcl]
Beginning the HTML Page
The following command generates the standard information that comes at the beginning of an HTML
page:
Cgi_Header {Brent's GuestBook} {bgcolor=white text=black}
The Cgi_Header is shown in Example 3-4:
Example 3-4 The Cgi_Header procedure.
proc Cgi_Header {title {bodyparams {}}} {
puts stdout \
"Content-Type: text/html
<HTML>
<HEAD>
<TITLE>$title</TITLE>
</HEAD>
<BODY $bodyparams>
<H1>$title</H1>"
}
The Cgi_Header procedure takes as arguments the title for the page and some optional parameters for
the HTML <Body> tag. The guestbook.cgi script specifies black text on a white background to avoid
the standard gray background of most browsers. The procedure definition uses the syntax for an
optional parameter, so you do not have to pass bodyparams to Cgi_Header. Default values for
procedure parameters are described on page 81.
The Cgi_Header procedure just contains a single puts command that generates the standard
boilerplate that appears at the beginning of the output. Note that several lines are grouped together
with double quotes. Double quotes are used so that the variable references mixed into the HTML are
substituted properly.
The output begins with the CGI content-type information, a blank line, and then the HTML. The
HTML is divided into a head and a body part. The <TITLE> tag goes in the head section of an HTML
document. Finally, browsers display the title in a different place than the rest of the page, so I always
want to repeat the title as a level-one heading (i.e., H1) in the body of the page.
Simple Tags and Hypertext Links
The next thing the program does is to see whether there are any registered guests or not. The file
command, which is described in detail on page 102, is used to see whether there is any data:
if {![file exists $datafile]} {
If the database file does not exist, a different page is displayed to encourage a registration. The page
includes a hypertext link to a registration page. The newguest.html page will be described in more
detail later:
puts "No registered guests, yet."
P
puts "Be the first [Link {registered guest!}newguest.html]"
The P command generates the HTML for a paragraph break. This trivial procedure saves us a few
keystrokes:
proc P {} {
puts <P>
}
The Link command formats and returns the HTML for a hypertext link. Instead of printing the HTML
directly, it is returned, so you can include it in-line with other text you are printing:
Example 3-5 The Link command formats a hypertext link.
proc Link {text url} {
return "<A HREF=\"$url\">$text</A>"
}
The output of the program would be as below if there were no data:
Example 3-6 Initial output of guestbook.cgi.
Content-Type: text/html
<HTML>
<HEAD>
<TITLE>Brent's Guestbook</TITLE>
</HEAD>
<BODY BGCOLOR=white TEXT=black>
<H1>Brent's Guestbook</H1>
<P>
No registered guests.
<P>
Be the first <A HREF="newguest.html">registered guest!</A>
</BODY>
</HTML>
If the database file exists, then the real work begins. We first generate a link to the registration page,
and a level-two header to separate that from the guest list:
puts [Link Register newguest.html]
H2 Guests
The H2 procedure handles the detail of including the matching close tag:
proc H2 {string} {
puts "<H2>$string</H2>"
}
Using a Tcl Array for the Database
The datafile contains Tcl commands that define an array that holds the guestbook data. If this file is
kept in the same directory as the guestbook.cgi script, then you can compute its name:
set dir [file dirname [info script]]
set datafile [file join $dir guestbook.data]
By using Tcl commands to represent the data, we can load the data with the source command. The
catch command is used to protect the script from a bad data file, which will show up as an error from
the source command. Catching errors is described in detail on page 79:
catch {source $datafile}
The Guestbook variable is the array defined in guestbook.data. Array variables are the topic of
Chapter 8. Each element of the array is defined with a Tcl command that looks like this:
set Guestbook(key) {url markup}
The person's name is the array index, or key. The value of the array element is a Tcl list with two
elements: their URL and some additional HTML markup that they can include in the guestbook. Tcl
lists are the topic of Chapter 5. The following example shows what the command looks like with real
data:
set {Guestbook(Brent Welch)} {
http://www.beedub.com/
{<img src=http://www.beedub.com/welch.gif>}
}
The spaces in the name result in additional braces to group the whole variable name and each list
element. This syntax is explained on page 90. Do not worry about it now. We will see on page 42 that
all the braces in the previous statement are generated automatically. The main point is that the person's
name is the key, and the value is a list with two elements.
The array names command returns all the indices, or keys, in the array, and the lsort command sorts
these alphabetically. The foreach command loops over the sorted list, setting the loop variable x to
each key in turn:
foreach name [lsort [array names Guestbook]] {
Given the key, we get the value like this:
set item $Guestbook($name)
The two list elements are extracted with lindex, which is described on page 63.
set homepage [lindex $item 0]
set markup [lindex $item 1]
We generate the HTML for the guestbook entry as a level-three header that contains a hypertext link to
the guest's home page. We follow the link with any HTML markup text that the guest has supplied to
embellish his or her entry. The H3 procedure is similar to the H2 procedure already shown, except it
generates <H3> tags:
H3 [Link $name $homepage]
puts $markup
Sample Output
The last thing the script does is call Cgi_End to output the proper closing tags. Example 3-7 shows the
output of the guestbook.cgi script:
Example 3-7 Output of guestbook.cgi.
Content-Type: text/html
<HTML>
<HEAD>
<TITLE>Brent's Guestbook</TITLE>
</HEAD>
<BODY BGCOLOR=white TEXT=black>
<H1>Brent's Guestbook</H1>
<P>
The following folks have registered in my guestbook.
<P>
<A HREF="newguest.html">Register</A>
<H2>Guests</H2>
<H3><A HREF="http://www.beedub.com/">Brent Welch</A></H3>
<IMG SRC="http://www.beedub.com/welch.gif">
</BODY>
</HTML>
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter3. The Guestbook CGI Application
Defining Forms and Processing Form Data
The guestbook.cgi script only generates output. The other half of CGI deals with input from the user. Input
is more complex for two reasons. First, we have to define another HTML page that has a form for the user to
fill out. Second, the data from the form is organized and encoded in a standard form that must be decoded by
the script. Example 3-8 on page 40 defines a very simple form, and the procedure that decodes the form data
is shown in Example 11-6 on page 155.
The guestbook page contains a link to newguest.html . This page contains a form that lets a user register his
or her name, home page URL, and some additional HTML markup. The form has a submit button. When a
user clicks that button in their browser, the information from the form is passed to the newguest.cgi script.
This script updates the database and computes another page for the user that acknowledges the user's
contribution.
The newguest.html Form
An HTML form contains tags that define data entry fields, buttons, checkboxes, and other elements that let
the user specify values. For example, a one-line entry field that is used to enter the home page URL is defined
like this:
<INPUT TYPE=text NAME=url>
The INPUT tag is used to define several kinds of input elements, and its type parameter indicates what kind.
In this case, TYPE=text creates a one-line text entry field. The submit button is defined with an INPUT tag that
has TYPE=submit , and the VALUE parameter becomes the text that appears on the button:
<INPUT TYPE=submit NAME=submit VALUE=Register>
A general type-in window is defined with the TEXTAREA tag. This creates a multiline, scrolling text field that
is useful for specifying lots of information, such as a free-form comment. In our case we will let guests type
in HTML that will appear with their guestbook entry. The text between the open and close TEXTAREA tags is
inserted into the type-in window when the page is first displayed.
<TEXTAREA NAME=markup ROWS=10 COLS=50>Hello.</TEXTAREA>
A common parameter to the form tags is NAME= something . This name identifies the data that will come back
from the form. The tags also have parameters that affect their display, such as the label on the submit button
and the size of the text area. Those details are not important for our example. The complete form is shown in
Example 3-8 :
Example 3-8 The newguest.html form.
<!Doctype HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>Register in my Guestbook</TITLE>
<!-- Author: bwelch -->
<META HTTP-Equiv=Editor Content="SunLabs WebTk 1.0beta 10/11/96">
</HEAD>
<BODY>
<FORM ACTION="newguest.cgi" METHOD="POST">
<H1>Register in my Guestbook</H1>
<UL>
<LI>Name <INPUT TYPE="text" NAME="name" SIZE="40">
<LI>URL <INPUT TYPE="text" NAME="url" SIZE="40">
<P>
If you don't have a home page, you can use an email URL like "mailto:welch@acm.org"
<LI>Additional HTML to include after your link:
<BR>
<TEXTAREA NAME="html" COLS="60" ROWS="15">
</TEXTAREA>
<LI><INPUT TYPE="submit" NAME="new" VALUE="Add me to your guestbook">
<LI><INPUT TYPE="submit" NAME="update" VALUE="Update my guestbook entry">
</UL>
</FORM>
</BODY>
</HTML>
The newguest.cgi Script
When the user clicks the Submit button in their browser, the data from the form is passed to the program
identified by the Action parameter of the form tag. That program takes the data, does something useful with
it, and then returns a new page for the browser to display. In our case the FORM tag names newguest.cgi as
the program to handle the data:
<FORM ACTION=newguest.cgi METHOD=POST>
The CGI specification defines how the data from the form is passed to the program. The data is encoded and
organized so that the program can figure out the values the user specified for each form element. The
encoding is handled rather nicely with some regular expression tricks that are done in Cgi_Parse . Cgi_Parse
saves the form data, and Cgi_Value gets a form value in the script. These procedures are described in
Example 11-6 on page 155. Example 3-9 starts out by calling Cgi_Parse :
Example 3-9 The newguest.cgi script.
#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
# source cgilib.tcl from the same directory as newguest.cgi
set dir [file dirname [info script]]
source [file join $dir cgilib.tcl]
set datafile [file join $dir guestbook.data]
Cgi_Parse
# Open the datafile in append mode
if [catch {open $datafile a}out] {
Cgi_Header "Guestbook Registration Error" \
{BGCOLOR=black TEXT=red}
P
puts "Cannot open the data file"
P
puts $out;# the error message
exit 0
}
# Append a Tcl set command that defines the guest's entry
puts $out ""
puts $out [list set Guestbook([Cgi_Value name]) \
[list [Cgi_Value url] [Cgi_Value html]]]
close $out
# Return a page to the browser
Cgi_Header "Guestbook Registration Confirmed" \
{BGCOLOR=white TEXT=black}
puts "
<DL>
<DT>Name
<DD>[Cgi_Value name]
<DT>URL
<DD>[Link [Cgi_Value url] [Cgi_Value url]]
</DL>
[Cgi_Value html]
"
Cgi_End
The main idea of the newguest.cgi script is that it saves the data to a file as a Tcl command that defines an
element of the Guestbook array. This lets the guestbook.cgi script simply load the data by using the Tcl
source command. This trick of storing data as a Tcl script saves us from the chore of defining a new file
format and writing code to parse it. Instead, we can rely on the well-tuned Tcl implementation to do the hard
work for us efficiently.
The script opens the datafile in append mode so that it can add a new record to the end. Opening files is
described in detail on page 110. The script uses a catch command to guard against errors. If an error occurs,
a page explaining the error is returned to the user. Working with files is one of the most common sources of
errors (permission denied, disk full, file-not-found, and so on), so I always open the file inside a catch
statement:
if [catch {open $datafile a} out] {
# an error occurred
} else {
# open was ok
}
In this command, the variable out gets the result of the open command, which is either a file descriptor or an
error message. This style of using catch is described in detail in Example 6-14 on page 77.
The script writes the data as a Tcl set command. The list command is used to format the data properly:
puts $out [list set Guestbook([Cgi_Value name]) \
[list [Cgi_Value url] [Cgi_Value html]]]
There are two lists. First the url and html values are formatted into one list. This list will be the value of the
array element. Then, the whole Tcl command is formed as a list. In simplified form, the command is
generated from this:
list set variable value
Using the list command ensures that the result will always be a valid Tcl command that sets the variable to
the given value. The list command is described in more detail on page 61.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter3. The Guestbook CGI Application
The cgi.tcl Package
The cgilib.tcl file included with this book just barely scratches the surface of things you might like
to do in a CGI script. Don Libes has created a comprehensive package for CGI scripts known as
cgi.tcl. You can find it on the web at
http://expect.nist.gov/cgi.tcl/
One of Don's goals in cgi.tcl was to eliminate the need to directly write any HTML markup at all.
Instead, he has defined a whole suite of Tcl commands similar to the P and H2 procedures shown in
this chapter that automatically emit the matching close tags. He also has support procedures to deal
with browser cookies, page redirects, and other CGI features.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter3. The Guestbook CGI Application
Next Steps
There are a number of details that can be added to this example. A user may want to update their entry,
for example. They could do that now, but they would have to retype everything. They might also like a
chance to check the results of their registration and make changes before committing them. This
requires another page that displays their guest entry as it would appear on a page, and also has the
fields that let them update the data.
The details of how a CGI script is hooked up with a Web server vary from server to server. You
should ask your local Webmaster for help if you want to try this out on your local web site. The Tcl
Web Server comes with this guestbook example already set up, plus it has a number of other very
interesting ways to generate pages. My own taste in web page generation has shifted from CGI to a
template-based approach supported by the Tcl Web Server. This is the topic of Chapter 18.
The next few chapters describe basic Tcl commands and data structures. We return to this example in
Chapter 11 on regular expressions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartI. Tcl Basics
Chapter 4. String Processing in Tcl
This chapter describes string manipulation and simple pattern matching. Tcl commands described are:
string, append, format, scan, and binary. The string command is a collection of several useful
string manipulation operations.
Strings are the basic data item in Tcl, so it should not be surprising that there are a large number of
commands to manipulate strings. A closely related topic is pattern matching, in which string
comparisons are made more powerful by matching a string against a pattern. This chapter describes a
simple pattern matching mechanism that is similar to that used in many other shell languages. Chapter
11 describes a more complex and powerful regular expression pattern matching mechanism.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter4. String Processing in Tcl
The string Command
The string command is really a collection of operations you can perform on strings. The following
example calculates the length of the value of a variable.
set name "Brent Welch"
string length $name
=> 11
The first argument to string determines the operation. You can ask string for valid operations by
giving it a bad one:
string junk
=> bad option "junk": should be bytelength, compare,
equal, first, index, is, last, length, map, match, range,
repeat, replace, tolower, totitle, toupper, trim, trimleft,
trimright, wordend, or wordstart
This trick of feeding a Tcl command bad arguments to find out its usage is common across many
commands. Table 4-1 summarizes the string command
Table 4-1. The string command.
string bytelength
str
Returns the number of bytes used to store a string, which may be different
from the character length returned by string length because of UTF-8
encoding. See page 210 of Chapter 15 about Unicode and UTF-8.
string compare ?-
nocase? ?-length
len? str1 str2
Compares strings lexicographically. Use -nocase for case insensitve
comparison. Use -length to limit the comparison to the first len characters.
Returns 0 if equal, -1 if str1 sorts before str2, else 1.
string equal ?-
nocase? str1 str2
Compares strings and returns 1 if they are the same. Use -nocase for case
insensitve comparison.
string first str1
str2
Returns the index in str2 of the first occurrence of str1, or -1 if str1 is not
found.
string index string
index
Returns the character at the specified index. An index counts from zero. Use
end for the last character.
string is class ?-
strict? ?-
failindex varname?
string
Returns 1 if string belongs to class. If -strict, then empty strings never
match, otherwise they always match. If -failindex is specified, then
varname is assigned the index of the character in string that prevented it
from being a member of class. See Table 4-3 on page 50 for character class
names.
string last str1
str2
Returns the index in str2 of the last occurrence of str1, or -1 if str1 is not
found.
string length
string
Returns the number of characters in string.
string map ?-
nocase? charMap
string
Returns a new string created by mapping characters in string according to
the input, output list in charMap. See page 51.
string match
pattern str
Returns 1 if str matches the pattern, else 0. Glob-style matching is used.
See page 48.
string range str i
j
Returns the range of characters in str from i to j.
string repeat str
count
Returns str repeated count times.
string replace str
first last?newstr?
Returns a new string created by replacing characters first through last
with newstr, or nothing.
string tolower
string?first? ?
last?
Returns string in lower case. first and last determine the range of
string on which to operate.
string totitle
string?first? ?
last?
Capitalizes string by replacing its first character with the Unicode title
case, or upper case, and the rest with lower case. first and last determine
the range of string on which to operate.
string toupper
string?first? ?
last?
Returns string in upper case. first and last determine the range of
string on which to operate.
string trim
string?chars?
Trims the characters in chars from both ends of string. chars defaults to
whitespace.
string trimleft
string?chars?
Trims the characters in chars from the beginning of string. chars defaults
to whitespace.
string trimright
string?chars?
Trims the characters in chars from the end of string. chars defaults to
whitespace.
string wordend str
ix
Returns the index in str of the character after the word containing the
character at index ix.
string wordstart
str ix
Returns the index in str of the first character in the word containing the
character at index ix.
These are the string operations I use most:
The equal operation, which is shown in Example 4-2 on page 48.
String match. This pattern matching operation is described on page 48.
The tolower, totitle, and toupper operations convert case.
The trim, trimright, and trimleft operations are handy for cleaning up strings.
These new operations were added in Tcl 8.1 (actually, they first appeared in the 8.1.1 patch release):
The equal operation, which is simpler than using string compare.
The is operation that test for kinds of strings. String classes are listed in Table 4-3 on page 50.
The map operation that translates characters (e.g., like the Unix tr command.)
The repeat and replace operations.
The totitle operation, which is handy for capitalizing words.
String Indices
Several of the string operations involve string indices that are positions within a string. Tcl counts
characters in strings starting with zero. The special index end is used to specify the last character in a
string:
string range abcd 2 end
=> cd
Tcl 8.1 added syntax for specifying an index relative to the end. Specify end-N to get the Nth caracter
before the end. For example, the following command returns a new string that drops the first and last
characters from the original:
string range $string 1 end-1
There are several operations that pick apart strings: first, last, wordstart, wordend, index, and
range. If you find yourself using combinations of these operations to pick apart data, it will be faster if
you can do it with the regular expression pattern matcher described in Chapter 11.
Strings and Expressions
Strings can be compared with expr, if, and while using the comparison operators ==, !=, < and >.
However, there are a number of subtle issues that can cause problems. First, you must quote the string
value so that the expression parser can identify it as a string type. Then, you must group the expression
with curly braces to prevent the double quotes from being stripped off by the main interpreter:
if {$x == "foo"}command
expr is unreliable for string comparison.
Ironically, despite the quotes, the expression evaluator first converts items to numbers if possible, and
then converts them back if it detects a case of string comparison. The conversion back is always done
as a decimal number. This can lead to unexpected conversions between strings that look like
hexadecimal or octal numbers. The following boolean expression is true!
if {"0xa" == "10"} {puts stdout ack! }
=> ack!
A safe way to compare strings is to use the string compare and equal operations. These operations
work faster because the unnecessary conversions are eliminated. Like the C library strcmp function,
string compare returns 0 if the strings are equal, minus 1 if the first string is lexicographically less
than the second, or 1 if the first string is greater than the second:
Example 4-1 Comparing strings with string compare.
if {[string compare $s1 $s2] == 0} {
# strings are equal
}
The string equal command added in Tcl 8.1 makes this simpler:
Example 4-2 Comparing strings with string equal.
if {[string equal $s1 $s2]} {
# strings are equal
}
String Matching
The string match command implements glob-style pattern matching that is modeled after the file
name pattern matching done by various UNIX shells.
The heritage of the word "glob" is rooted in UNIX, and Tcl preserves this historical oddity in the glob
command that does pattern matching on file names. The glob command is described on page 115.
Table 4-2 shows the three constructs used in string match patterns:
Table 4-2. Matching characters used with string match.
*
Match any number of any characters.
?
Match exactly one character.
[chars] Match any character in chars.
Any other characters in a pattern are taken as literals that must match the input exactly. The following
example matches all strings that begin with a:
string match a* alpha
=> 1
To match all two-letter strings:
string match ?? XY
=> 1
To match all strings that begin with either a or b:
string match {[ab]*}cello
=> 0
Be careful! Square brackets are also special to the Tcl interpreter, so you will need to wrap the pattern
up in curly braces to prevent it from being interpreted as a nested command. Another approach is to
put the pattern into a variable:
set pat {[ab]*x}
string match $pat box
=> 1
You can specify a range of characters with the syntax [x-y]. For example, [a-z] represents the set of
all lower-case letters, and [0-9] represents all the digits. You can include more than one range in a set.
Any letter, digit, or the underscore is matched with:
string match {[a-zA-Z0-9_]}$char
The set matches only a single character. To match more complicated patterns, like one or more
characters from a set, then you need to use regular expression matching, which is described on page
148.
If you need to include a literal *, ?, or bracket in your pattern, preface it with a backslash:
string match {*\?}what?
=> 1
In this case the pattern is quoted with curly braces because the Tcl interpreter is also doing backslash
substitutions. Without the braces, you would have to use two backslashes. They are replaced with a
single backslash by Tcl before string match is called.
string match *\\? what?
Character Classes
The string is command tests a string to see whether it belongs to a particular class. This is useful
for input validation. For example, to make sure something is a number, you do:
if {![string is integer $input]} {
error "Invalid input. Please enter a number."
}
Classes are defined in terms of the Unicode character set, which means they are more general than
specifying character sets with ranges over the ASCII encoding. For example, alpha includes many
characters outside the range of [A-Za-z] because of different characters in other alphabets. The
classes are listed in Table 4-3.
Table 4-3. Character class names.
alnum
Any alphabet or digit character.
alpha
Any alphabet character.
ascii
Any character with a 7-bit character code (i.e., less than 128.)
boolean
0, 1, true, false (in any case).
control
Character code less than 32, and not NULL.
digit
Any digit character.
double
A valid floating point number.
false
0 or false (in any case).
graph
Any printing characters, not including space characters.
integer
A valid integer.
lower
A string in all lower case.
print
A synonym for alnum.
punct
Any punctuation character.
space
Space, tab, newline, carriage return, vertical tab, backspace.
true
1 or true (in any case).
upper
A string all in upper case.
wordchar
Alphabet, digit, and the underscore.
xdigit
Valid hexadecimal digits.
Mapping Strings
The string map command translates a string based on a character map. The map is in the form of a
input, output list. Whereever a string contains an input sequence, that is replaced with the
corresponding output. For example:
string map "food" {f p d l}
=> pool
The inputs and outputs can be more than one character and do not have to be the same length:
string map "food" {f p d ll oo u}
=> pull
Example 4-3 is more practical. It uses string map to replace fancy quotes and hyphens produced by
Microsoft Word into ASCII equivalents. It uses the open, read, and close file operations that are
described in Chapter 9, and the fconfigure command described on page 223 to ensure that the file
format is UNIX friendly.
Example 4-3 Mapping Microsoft World special characters to ASCII.
proc Dos2Unix {filename} {
set input [open $filename]
set output [open $filename.new]
fconfigure $output -translation lf
puts $output [string map {
\223 "
\224 "
\222 '
\226 -
}[read $input]]
close $input
close $output
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter4. String Processing in Tcl
The append Command
The append command takes a variable name as its first argument and concatenates its remaining
arguments onto the current value of the named variable. The variable is created if it does not already
exist:
set foo z
append foo a b c
set foo
=> zabc
The append command is efficient with large strings.
The append command provides an efficient way to add items to the end of a string. It modifies a
variable directly, so it can exploit the memory allocation scheme used internally by Tcl. Using the
append command like this:
append x " some new stuff"
is always faster than this:
set x "$x some new stuff"
The lappend command described on page 61 has similar performance benefits when working with Tcl
lists.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter4. String Processing in Tcl
The format Command
The format command is similar to the C printf function. It formats a string according to a format
specification:
format spec value1 value2 ...
The spec argument includes literals and keywords. The literals are placed in the result as is, while
each keyword indicates how to format the corresponding argument. The keywords are introduced with
a percent sign, %, followed by zero or more modifiers, and terminate with a conversion specifier.
Example keywords include %f for floating point, %d for integer, and %s for string format. Use %% to
obtain a single percent character. The most general keyword specification for each argument contains
up to six parts:
position specifier
flags
field width
precision
word length
conversion character
These components are explained by a series of examples. The examples use double quotes around the
format specification. This is because often the format contains white space, so grouping is required, as
well as backslash substitutions like \t or \n, and the quotes allow substitution of these special
characters. Table 4-4 lists the conversion characters:
Table 4-4. Format conversions.
d
Signed integer.
u
Unsigned integer.
i
Signed integer. The argument may be in hex (0x) or octal (0) format.
o
Unsigned octal.
x or X
Unsigned hexadecimal. 'x' gives lowercase results.
c
Map from an integer to the ASCII character it represents.
s
A string.
f
Floating point number in the format a.b.
e or E
Floating point number in scientific notation, a.bE+-c.
g or G
Floating point number in either %f or %e format, whichever is shorter.
A position specifier is i$, which means take the value from argument i as opposed to the normally
corresponding argument. The position counts from 1. If a position is specified for one format keyword,
the position must be used for all of them. If you group the format specification with double quotes, you
need to quote the $ with a backslash:
set lang 2
format "%${lang}\$s" one un uno
=> un
The position specifier is useful for picking a string from a set, such as this simple language-specific
example. The message catalog facility described in Chapter 15 is a much more sophisticated way to
solve this problem. The position is also useful if the same value is repeated in the formatted string.
The flags in a format are used to specify padding and justification. In the following examples, the #
causes a leading 0x to be printed in the hexadecimal value. The zero in 08 causes the field to be
padded with zeros. Table 4-5 summarizes the format flag characters.
format "%#x" 20
=> 0x14
format "%#08x" 10
=> 0x0000000a
Table 4-5. Format flags.
-
Left justify the field.
+
Always include a sign, either + or -.
space Precede a number with a space, unless the number has a leading sign. Useful for packing
numbers close together.
0
Pad with zeros.
#
Leading 0 for octal. Leading 0x for hex. Always include a decimal point in floating point. Do
not remove trailing zeros (%g).
After the flags you can specify a minimum field width value. The value is padded to this width with
spaces, or with zeros if the 0 flag is used:
format "%-20s %3d" Label 2
=> Label 2
You can compute a field width and pass it to format as one of the arguments by using * as the field
width specifier. In this case the next argument is used as the field width instead of the value, and the
argument after that is the value that gets formatted.
set maxl 8
format "%-*s = %s" $maxl Key Value
=> Key = Value
The precision comes next, and it is specified with a period and a number. For %f and %e it indicates
how many digits come after the decimal point. For %g it indicates the total number of significant digits
used. For %d and %x it indicates how many digits will be printed, padding with zeros if necessary.
format "%6.2f %6.2d" 1 1
=> 1.00 01
The storage length part comes last but it is rarely useful because Tcl maintains all floating point values
in double-precision, and all integers as long words.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter4. String Processing in Tcl
The scan Command
The scan command parses a string according to a format specification and assigns values to variables.
It returns the number of successful conversions it made. The general form of the command is:
scan string format var ?var? ?var? ...
The format for scan is nearly the same as in the format command. There is no %u scan format. The %c
scan format converts one character to its decimal value.
The scan format includes a set notation. Use square brackets to delimit a set of characters. The set
matches one or more characters that are copied into the variable. A dash is used to specify a range. The
following scans a field of all lowercase letters.
scan abcABC {%[a-z]}result
=> 1
set result
=> abc
If the first character in the set is a right square bracket, then it is considered part of the set. If the first
character in the set is ^, then characters not in the set match. Again, put a right square bracket
immediately after the ^ to include it in the set. Nothing special is required to include a left square
bracket in the set. As in the previous example, you will want to protect the format with braces, or use
backslashes, because square brackets are special to the Tcl parser.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter4. String Processing in Tcl
The binary Command
Tcl 8.0 added support for binary strings. Previous versions of Tcl used null-terminated strings
internally, which foils the manipulation of some types of data. Tcl now uses counted strings, so it can
tolerate a null byte in a string value without truncating it.
This section describes the binary command that provides conversions between strings and packed
binary data representations. The binary format command takes values and packs them according to a
template. For example, this can be used to format a floating point vector in memory suitable for
passing to Fortran. The resulting binary value is returned:
binary format template value ?value ...?
The binary scan command extracts values from a binary string according to a similar template. For
example, this is useful for extracting data stored in binary format. It assigns values to a set of Tcl
variables:
binary scan value template variable ?variable ...?
Format Templates
The template consists of type keys and counts. The types are summarized in Table 4-6. In the table,
count is the optional count following the type letter.
Table 4-6. Binary conversion types.
a
A character string of length count. Padded with nulls in binary format.
A
A character string of length count. Padded with spaces in binary format. Trailing nulls and
blanks are discarded in binary scan.
b
A binary string of length count. Low-to-high order.
B
A binary string of length count. High-to-low order.
h
A hexadecimal string of length count. Low-to-high order.
H
A hexadecimal string of length count. High-to-low order. (More commonly used than h.)
c
An 8-bit character code. The count is for repetition.
s
A 16-bit integer in little-endian byte order. The count is for repetition.
S
A 16-bit integer in big-endian byte order. The count is for repetition.
i
A 32-bit integer in little-endian byte order. The count is for repetition.
I
A 32-bit integer in big-endian byte order. The count is for repetition.
f
Single-precision floating point value in native format. count is for repetition.
d
Double-precision floating point value in native format. count is for repetition.
x
Pack count null bytes with binary format.
Skip count bytes with binary scan.
X
Backup count bytes.
@
Skip to absolute position specified by count. If count is *, skip to the end.
The count is interpreted differently depending on the type. For types like integer (i) and double (d),
the count is a repetition count (e.g., i3 means three integers). For strings, the count is a length (e.g., a3
means a three-character string). If no count is specified, it defaults to 1. If count is *, then binary
scan uses all the remaining bytes in the value.
Several type keys can be specified in a template. Each key-count combination moves an imaginary
cursor through the binary data. There are special type keys to move the cursor. The x key generates
null bytes in binary format, and it skips over bytes in binary scan. The @ key uses its count as an
absolute byte offset to which to set the cursor. As a special case, @* skips to the end of the data. The X
key backs up count bytes.
Numeric types have a particular byte order that determines how their value is laid out in memory. The
type keys are lowercase for little-endian byte order (e.g., Intel) and uppercase for big-endian byte order
(e.g., SPARC and Motorola). Different integer sizes are 16-bit (s or S), 32-bit (i or I), and possibly
64-bit (l or L) on those machines that support 64-bit integers. Note that the official byte order for data
transmitted over a network is big-endian. Floating point values are always machine-specific, so it only
makes sense to format and scan these values on the same machine.
There are three string types: character (a or A), binary (b or B), and hexadecimal (h or H). With these
types the count is the length of the string. The a type pads its value to the specified length with null
bytes in binary format and the A type pads its value with spaces. If the value is too long, it is
truncated. In binary scan, the A type strips trailing blanks and nulls.
A binary string consists of zeros and ones. The b type specifies bits from low-to-high order, and the B
type specifies bits from high-to-low order. A hexadecimal string specifies 4 bits (i.e., nybbles) with
each character. The h type specifies nybbles from low-to-high order, and the H type specifies nybbles
from high-to-low order. The B and H formats match the way you normally write out numbers.
Examples
When you experiment with binary format and binary scan, remember that Tcl treats things as
strings by default. A "6", for example, is the character 6 with character code 54 or 0x36. The c type
returns these character codes:
set input 6
binary scan $input "c" 6val
set 6val
=> 54
You can scan several character codes at a time:
binary scan abc "c3" list
=> 1
set list
=> 97 98 99
The previous example uses a single type key, so binary scan sets one corresponding Tcl variable. If
you want each character code in a separate variable, use separate type keys:
binary scan abc "ccc" x y z
=> 3
set z
=> 99
Use the H format to get hexadecimal values:
binary scan 6 "H2" 6val
set 6val
=> 36
Use the a and A formats to extract fixed width fields. Here the * count is used to get all the rest of the
string. Note that A trims trailing spaces:
binary scan "hello world " a3x2A* first second
puts "\"$first\" \"$second\""
=> "hel" " world"
Use the @ key to seek to a particular offset in a value. The following command gets the second double-
precision number from a vector. Assume the vector is read from a binary data file:
binary scan $vector "@8d" double
With binary format, the a and A types create fixed width fields. A pads its field with spaces, if
necessary. The value is truncated if the string is too long:
binary format "A9A3" hello world
=> hello wor
An array of floating point values can be created with this command:
binary format "f*" 1.2 3.45 7.43 -45.67 1.03e4
Remember that floating point values are always in native format, so you have to read them on the
same type of machine that they were created. With integer data you specify either big-endian or little-
endian formats. The tcl_platform variable described on page 182 can tell you the byte order of the
current platform.
Binary Data and File I/O
When working with binary data in files, you need to turn off the newline translations and character set
encoding that Tcl performs automatically. These are described in more detail on pages 114 and 209.
For example, if you are generating binary data, the following command puts your standard output in
binary mode:
fconfigure stdout -translation binary -encoding binary
puts [binary format "B8" 11001010]
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter4. String Processing in Tcl
Related Chapters
To learn more about manipulating data in Tcl, read about lists in Chapter 5 and arrays in Chapter
8.
For more about pattern matching, read about regular expressions in Chapter 11.
For more about file I/O, see Chapter 9.
For information on Unicode and other Internationalization issues, see Chapter 15.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartI. Tcl Basics
Chapter 5. Tcl Lists
This chapter describes Tcl lists. Tcl commands described are: list, lindex, llength, lrange,
lappend, linsert, lreplace, lsearch, lsort, concat, join, and split.
Lists in Tcl have the same structure as Tcl commands. All the rules you learned about grouping
arguments in Chapter 1 apply to creating valid Tcl lists. However, when you work with Tcl lists, it is
best to think of lists in terms of operations instead of syntax. Tcl commands provide operations to put
values into a list, get elements from lists, count the elements of lists, replace elements of lists, and so
on. The syntax can sometimes be confusing, especially when you have to group arguments to the list
commands themselves.
Lists are used with commands such as foreach that take lists as arguments. In addition, lists are
important when you are building up a command to be evaluated later. Delayed command evaluation
with eval is described in Chapter 10, and similar issues with Tk callback commands are described in
Chapter 27.
However, Tcl lists are not often the right way to build complicated data structures in scripts. You may
find Tcl arrays more useful, and they are the topic of Chapter 8. List operations are also not right for
handling unstructured data such as user input. Use regular expressions instead, which are described in
Chapter 11.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
Tcl Lists
A Tcl list is a sequence of values. When you write out a list, it has the same syntax as a Tcl command.
A list has its elements separated by white space.Braces or quotes can be used to group words with
white space into a single list element. Because of the relationship between lists and commands, the
list-related commands described in this chapter are used often when constructing Tcl commands.
Big lists were often slow before Tcl 8.0.
Unlike list data structures in other languages, Tcl lists are just strings with a special interpretation. The
string representation must be parsed on each list access, so be careful when you use large lists. A list
with a few elements will not slow down your code much. A list with hundreds or thousands of
elements can be very slow. If you find yourself maintaining large lists that must be frequently
accessed, consider changing your code to use arrays instead.
The performance of lists was improved by the Tcl compiler added in Tcl 8.0. The compiler stores lists
in an internal format that requires constant time to access. Accessing the first element costs the same
as accessing any other element in the list. Before Tcl 8.0, the cost of accessing an element was
proportional to the number of elements before it in the list. The internal format also records the
number of list elements, so getting the length of a list is cheap. Before Tcl 8.0, computing the length
required reading the whole list.
Table 5-1 briefly describes the Tcl commands related to lists.
Table 5-1. List-related commands.
list arg1 arg2 ... Creates a list out of all its arguments.
lindex list i Returns the ith element from list.
llength list Returns the number of elements in list.
lrange list i j Returns the ith through jth elements from list.
lappend listVar
arg arg ...
Appends elements to the value of listVar.
linsert list index
arg arg ...
Inserts elements into list before the element at position index. Returns a
new list.
lreplace list i j
arg arg ...
Replaces elements i through j of list with the args. Returns a new list.
lsearch ?mode?
list value
Returns the index of the element in list that matches the value according to
the mode, which is -exact, -glob, or -regexp. -glob is the default. Returns
-1 if not found.
lsort ?switches?
list
Sorts elements of the list according to the switches: -ascii, -integer, -
real, -dictionary, -increasing, -decreasing, -index ix, -command
command.
Returns a new list.
concat list list
...
Joins multiple lists together into one list.
join list
joinString
Merges the elements of a list together by separating them with joinString.
split string
splitChars
Splits a string up into list elements, using the characters in splitChars as
boundaries between list elements.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
Constructing Lists
Constructing a list can be tricky because you must maintain proper list syntax. In simple cases, you can
do this by hand. In more complex cases, however, you should use Tcl commands that take care of
quoting so that the syntax comes out right.
The list command
The list command constructs a list out of its arguments so that there is one list element for each
argument. If any of the arguments contain special characters, the list command adds quoting to
ensure that they are parsed as a single element of the resulting list. The automatic quoting is very
useful, and the examples in this book use the list command frequently. The next example uses list
to create a list with three values, two of which contain special characters.
Example 5-1 Constructing a list with the list command.
set x {1 2}
=> 1 2
set y foo
=> foo
set l1 [list $x "a b" $y]
=> {1 2} {a b}foo
set l2 "\{$x\\a b}$y"
=> {1 2} {a b}foo
The list command does automatic quoting.
Compare the use of list with doing the quoting by hand in Example 5-1. The assignment of l2
requires carefully constructing the first list element by using quoted braces. The braces must be turned
off so that $x can be substituted, but we need to group the result so that it remains a single list element.
We also have to know in advance that $x contains a space, so quoting is required. We are taking a risk
by not quoting $y because we know it doesn't contain spaces. If its value changes in the future, the
structure of the list can change and even become invalid. In contrast, the list command takes care of
all these details automatically.
When I first experimented with Tcl lists, I became confused by the treatment of curly braces. In the
assignment to x, for example, the curly braces disappear. However, they come back again when $x is
put into a bigger list. Also, the double quotes around a b get changed into curly braces. What's going
on? Remember that there are two steps. In the first step, the Tcl parser groups arguments. In the
grouping process, the braces and quotes are syntax that define groups. These syntax characters get
stripped off. The braces and quotes are not part of the value. In the second step, the list command
creates a valid Tcl list. This may require quoting to get the list elements into the right groups. The
list command uses curly braces to group values back into list elements.
The lappend Command
The lappend command is used to append elements to the end of a list. The first argument to lappend
is the name of a Tcl variable, and the rest of the arguments are added to the variable's value as new list
elements. Like list, lappend preserves the structure of its arguments. It may add braces to group the
values of its arguments so that they retain their identity as list elements when they are appended onto
the string representation of the list.
Example 5-2 Using lappend to add elements to a list.
lappend new 1 2
=> 1 2
lappend new 3 "4 5"
=> 1 2 3 {4 5}
set new
=> 1 2 3 {4 5}
The lappend command is unique among the list-related commands because its first argument is the
name of a list-valued variable, while all the other commands take list values as arguments. You can
call lappend with the name of an undefined variable and the variable will be created.
The lappend command is implemented efficiently to take advantage of the way that Tcl stores lists
internally. It is always more efficient to use lappend than to try and append elements by hand.
The concat Command
The concat command is useful for splicing lists together. It works by concatenating its arguments,
separating them with spaces. This joins multiple lists into one list where the top-level list elements in
each input list become top-level list elements in the resulting list:
Example 5-3 Using concat to splice lists together.
set x {4 5 6}
set y {2 3}
set z 1
concat $z $y $x
=> 1 2 3 4 5 6
Double quotes behave much like the concat command. In simple cases, double quotes behave exactly
like concat. However, the concat command trims extra white space from the end of its arguments
before joining them together with a single separating space character. Example 5-4 compares the use
of list, concat, and double quotes:
Example 5-4 Double quotes compared to the concat and list commands.
set x {1 2}
=> 1 2
set y "$x 3"
=> 1 2 3
set y [concat $x 3]
=> 1 2 3
set s { 2 }
=> 2
set y "1 $s 3"
=> 1 2 3
set y [concat 1 $s 3]
=> 1 2 3
set z [list $x $s 3]
=> {1 2} { 2 } 3
The distinction between list and concat becomes important when Tcl commands are built
dynamically. The basic rule is that list and lappend preserve list structure, while concat (or double
quotes) eliminates one level of list structure. The distinction can be subtle because there are examples
where list and concat return the same results. Unfortunately, this can lead to data-dependent bugs.
Throughout the examples of this book, you will see the list command used to safely construct lists.
This issue is discussed more in Chapter 10.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
Getting List Elements: llength, lindex, and lrange
The llength command returns the number of elements in a list.
llength {a b {c d}"e f g" h}
=> 5
llength {}
=> 0
The lindex command returns a particular element of a list. It takes an index; list indices count from
zero.
set x {1 2 3}
lindex $x 1
=> 2
You can use the keyword end to specify the last element of a list, or the syntax end-N to count back
from the end of the list. The following commands are equivalent ways to get the element just before
the last element in a list.
lindex $list [expr {[llength $list] - 2}]
lindex $list end-1
The lrange command returns a range of list elements. It takes a list and two indices as arguments.
Again, end or end-N can be used as an index:
lrange {1 2 3 {4 5}}2 end
=> 3 {4 5}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
Modifying Lists: linsert and lreplace
The linsert command inserts elements into a list value at a specified index. If the index is zero or
less, then the elements are added to the front. If the index is equal to or greater than the length of the
list, then the elements are appended to the end. Otherwise, the elements are inserted before the element
that is currently at the specified index.
lreplace replaces a range of list elements with new elements. If you don't specify any new elements,
you effectively delete elements from a list.
Note: linsert and lreplace do not modify an existing list. Instead, they return a new list value. In the
following example, the lreplace command does not change the value of x:
Example 5-5 Modifying lists with linsert and lreplace.
linsert {1 2}0 new stuff
=> new stuff 1 2
set x [list a {b c}e d]
=> a {b c}e d
lreplace $x 1 2 B C
=> a B C d
lreplace $x 0 0
=> {b c}e d
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
Searching Lists: lsearch
lsearch returns the index of a value in the list, or -1 if it is not present. lsearch supports pattern
matching in its search. Glob-style pattern matching is the default, and this can be disabled with the -
exact flag. The semantics of glob pattern matching is described in Chapter 4. The -regexp option lets
you specify the list value with a regular expression. Regular expressions are described in Chapter 11.
In the following example, the glob pattern l* matches the value list.
lsearch {here is a list}l*
=> 3
Example 5-6 uses lreplace and lsearch to delete a list element by value. The value is found with
lsearch. The value is removed with an lreplace that does not specify any replacement list elements:
Example 5-6 Deleting a list element by value.
proc ldelete {list value } {
set ix [lsearch -exact $list $value]
if {$ix >= 0} {
return [lreplace $list $ix $ix]
} else {
return $list
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
Sorting Lists: lsort
You can sort a list in a variety of ways with lsort. The list is not sorted in place. Instead, a new list
value is returned. The basic types of sorts are specified with the -ascii, -dictionary, -integer, or -
real options. The -increasing or -decreasing option indicate the sorting order. The default option
set is -ascii -increasing. An ASCII sort uses character codes, and a dictionary sort folds together
case and treats digits like numbers. For example:
lsort -ascii {a Z n2 n100}
=> Z a n100 n2
lsort -dictionary {a Z n2 n100}
=> a n2 n100 Z
You can provide your own sorting function for special-purpose sorting. For example, suppose you
have a list of names, where each element is itself a list containing the person's first name, middle name
(if any), and last name. The default sorts by everyone's first name. If you want to sort by their last
name, you need to supply a sorting command.
Example 5-7 Sorting a list using a comparison function.
proc NameCompare {a b} {
set alast [lindex $a end]
set blast [lindex $b end]
set res [string compare $alast $blast]
if {$res != 0} {
return $res
} else {
return [string compare $a $b]
}
}
set list {{Brent B. Welch} {John Ousterhout} {Miles Davis}}
=> {Brent B. Welch} {John Ousterhout} {Miles Davis}
lsort -command NameCompare $list
=> "not
The default separator character for split is white space, which contains spaces, tabs, and newlines. If
there are multiple separator characters in a row, these result in empty list elements; the separators are
not collapsed. The following command splits on commas, periods, spaces, and tabs. The
backslashspace sequence is used to include a space in the set of characters. You could also group the
argument to split with double quotes:
set line "\tHello, world."
split $line \,.\t
=> {}Hello {}world {}
A trick that splits each character into a list element is to specify an empty string as the split character.
This lets you get at individual characters with list operations:
split abc {}
=> a b c
However, if you write scripts that process data one character at a time, they may run slowly. Read
Chapter 11 about regular expressions for hints on really efficient string processing.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
The join Command
The join command is the inverse of split. It takes a list value and reformats it with specified
characters separating the list elements. In doing so, it removes any curly braces from the string
representation of the list that are used to group the top-level elements. For example:
join {1 {2 3} {4 5 6}}:
=> 1:2 3:4 5 6
If the treatment of braces is puzzling, remember that the first value is parsed into a list. The braces
around element values disappear in the process. Example 5-9 shows a way to implement join in a Tcl
procedure, which may help to understand the process:
Example 5-9 Implementing join in Tcl.
proc join {list sep} {
set s {} ;# s is the current separator
set result {}
foreach x $list {
append result $s $x
set s $sep
}
return $result
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter5. Tcl Lists
Related Chapters
Arrays are the other main data structure in Tcl. They are described in Chapter 8.
List operations are used when generating Tcl code dynamically. Chapter 10 describes these
techniques when using the eval command.
The foreach command loops over the values in a list. It is described on page 73 in Chapter 6.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartI. Tcl Basics
Chapter 6. Control Structure Commands
This chapter describes the Tcl commands that implement control structures: if, switch, foreach,
while, for, break, continue, catch, error, and return.
Control structure in Tcl is achieved with commands, just like everything else. There are looping
commands: while, foreach, and for. There are conditional commands: if and switch. There is an
error handling command: catch. Finally, there are some commands to fine-tune control structures:
break, continue, return, and error.
A control structure command often has a command body that is executed later, either conditionally or
in a loop. In this case, it is important to group the command body with curly braces to avoid
substitutions at the time the control structure command is invoked. Group with braces, and let the
control structure command trigger evaluation at the proper time. A control structure command returns
the value of the last command it chose to execute.
Another pleasant property of curly braces is that they group things together while including newlines.
The examples use braces in a way that is both readable and convenient for extending the control
structure commands across multiple lines.
Commands like if, for, and while involve boolean expressions. They use the expr command
internally, so there is no need for you to invoke expr explicitly to evaluate their boolean test
expressions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter6. Control Structure Commands
If Then Else
The if command is the basic conditional command. If an expression is true, then execute one
command body; otherwise, execute another command body. The second command body (the else
clause) is optional. The syntax of the command is:
if expression ?then? body1 ?else? ?body2?
The then and else keywords are optional. In practice, I omit then but use else as illustrated in the
next example. I always use braces around the command bodies, even in the simplest cases:
Example 6-1 A conditional if then else command.
if {$x == 0} {
puts stderr "Divide by zero!"
} else {
set slope [expr $y/$x]
}
Curly brace positioning is important.
The style of this example takes advantage of the way the Tcl interpreter parses commands. Recall that
newlines are command terminators, except when the interpreter is in the middle of a group defined by
braces or double quotes. The stylized placement of the opening curly brace at the end of the first and
third lines exploits this property to extend the if command over multiple lines.
The first argument to if is a boolean expression. As a matter of style this expression is grouped with
curly braces. The expression evaluator performs variable and command substitution on the expression.
Using curly braces ensures that these substitutions are performed at the proper time. It is possible to be
lax in this regard, with constructs such as:
if $x break continue
This is a sloppy, albeit legitimate, if command that will either break out of a loop or continue with the
next iteration depending on the value of variable x. This style is fragile and error prone. Instead,
always use braces around the command bodies to avoid trouble later when you modify the command.
The following is much better (use then if it suits your taste):
if {$x} {
break
} else {
continue
}
When you are testing the result of a command, you can get away without using curly braces around the
command, like this:
if [command] body1
However, it turns out that you can execute the if statement more efficiently if you always group the
expression with braces, like this:
if {[command]}body1
You can create chained conditionals by using the elseif keyword. Again, note the careful placement
of curly braces that create a single if command:
Example 6-2 Chained conditional with elseif.
if {$key < 0} {
incr range 1
} elseif {$key == 0} {
return $range
} else {
incr range -1
}
Any number of conditionals can be chained in this manner. However, the switch command provides a
more powerful way to test multiple conditions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter6. Control Structure Commands
Switch
The switch command is used to branch to one of many command bodies depending on the value of an
expression. The choice can be made on the basis of pattern matching as well as simple comparisons.
Pattern matching is discussed in more detail in Chapter 4 and Chapter 11. The general form of the
command is:
switch flags value pat1 body1 pat2 body2 ...
Any number of pattern-body pairs can be specified. If multiple patterns match, only the body of the
first matching pattern is evaluated. You can also group all the pattern-body pairs into one argument:
switch flags value {pat1 body1 pat2 body2 ... }
The first form allows substitutions on the patterns but will require backslashes to continue the
command onto multiple lines. This is shown in Example 6-4 on page 72. The second form groups all
the patterns and bodies into one argument. This makes it easy to group the whole command without
worrying about newlines, but it suppresses any substitutions on the patterns. This is shown in Example
6-3. In either case, you should always group the command bodies with curly braces so that substitution
occurs only on the body with the pattern that matches the value.
There are four possible flags that determine how value is matched.
-exact
Matches the value exactly to one of the patterns. This is the default.
-glob
Uses glob-style pattern matching. See page 48.
-regexp
Uses regular expression pattern matching. See page 134.
--
No flag (or end of flags). Necessary when value can begin with -.
The switch command raises an error if any other flag is specified or if the value begins with -. In
practice I always use the -- flag before value so that I don't have to worry about that problem.
If the pattern associated with the last body is default, then this command body is executed if no other
patterns match. The default keyword works only on the last pattern-body pair. If you use the default
pattern on an earlier body, it will be treated as a pattern to match the literal string default:
Example 6-3 Using switch for an exact match.
switch -exact -- $value {
foo { doFoo; incr count(foo) }
bar { doBar; return $count(foo)}
default { incr count(other) }
}
If you have variable references or backslash sequences in the patterns, then you cannot use braces
around all the pattern-body pairs. You must use backslashes to escape the newlines in the command:
Example 6-4 Using switch with substitutions in the patterns.
switch -regexp -- $value \
^$key { body1 }\
\t### { body2 }\
{[0-9]*} { body3 }
In this example, the first and second patterns have substitutions performed to replace $key with its
value and \t with a tab character. The third pattern is quoted with curly braces to prevent command
substitution; square brackets are part of the regular expression syntax, too. (See page Chapter 11.)
If the body associated with a pattern is just a dash, -, then the switch command "falls through" to the
body associated with the next pattern. You can tie together any number of patterns in this manner.
Example 6-5 A switch with "fall through" cases.
switch -glob -- $value {
X* -
Y* { takeXorYaction $value }
}
Comments in switch Commands
A comment can occur only where the Tcl parser expects a command to begin.
This restricts the location of comments in a switch command. You must put them
inside the command body associated with a pattern, as shown in Example 6-6. If
you put a comment at the same level as the patterns, the switch command will try
to interpret the comment as one or more pattern-body pairs.
Example 6-6 Comments in switch commands.
switch -- $value {
# this comment confuses switch
pattern { # this comment is ok }
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter6. Control Structure Commands
While
The while command takes two arguments, a test and a command body:
while booleanExpr body
The while command repeatedly tests the boolean expression and then executes the body if the
expression is true (nonzero). Because the test expression is evaluated again before each iteration of the
loop, it is crucial to protect the expression from any substitutions before the while command is
invoked. The following is an infinite loop (see also Example 1-13 on page 12):
set i 0 ; while $i<10 {incr i}
The following behaves as expected:
set i 0 ; while {$i<10} {incr i}
It is also possible to put nested commands in the boolean expression. The following example uses
gets to read standard input. The gets command returns the number of characters read, returning -1
upon end of file. Each time through the loop, the variable line contains the next line in the file:
Example 6-7 A while loop to read standard input.
set numLines 0 ; set numChars 0
while {[gets stdin line] >= 0} {
incr numLines
incr numChars [string length $line]
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter6. Control Structure Commands
Foreach
The foreach command loops over a command body assigning one or more loop variables to each of
the values in one or more lists. Multiple loop variables were introduced in Tcl 7.5. The syntax for the
simple case of a single variable and a single list is:
foreach loopVar valueList commandBody
The first argument is the name of a variable, and the command body is executed once for each element
in the list with the loop variable taking on successive values in the list. The list can be entered
explicitly, as in the next example:
Example 6-8 Looping with foreach.
set i 1
foreach value {1 3 5 7 11 13 17 19 23} {
set i [expr $i*$value]
}
set i
=> 111546435
It is also common to use a list-valued variable or command result instead of a static list value. The
next example loops through command-line arguments. The variable argv is set by the Tcl interpreter
to be a list of the command-line arguments given when the interpreter was started:
Example 6-9 Parsing command-line arguments.
# argv is set by the Tcl shells
# possible flags are:
# -max integer
# -force
# -verbose
set state flag
set force 0
set verbose 0
set max 10
foreach arg $argv {
switch -- $state {
flag {
switch -glob -- $arg {
-f* {set force 1}
-v* {set verbose 1}
-max {set state max}
default {error "unknown flag $arg"}
}
}
max {
set max $arg
set state flag
}
}
}
The loop uses the state variable to keep track of what is expected next, which in this example is
either a flag or the integer value for -max. The -- flag to switch is required in this example because
the switch command complains about a bad flag if the pattern begins with a - character. The -glob
option lets the user abbreviate the -force and -verbose options.
If the list of values is to contain variable values or command results, then the list
command should be used to form the list. Avoid double quotes because if any
values or command results contain spaces or braces, the list structure will be
reparsed, which can lead to errors or unexpected results.
Example 6-10 Using list with foreach.
foreach x [list $a $b [foo]] {
puts stdout "x = $x"
}
The loop variable x will take on the value of a, the value of b, and the result of the foo command,
regardless of any special characters or whitespace in those values.
Multiple Loop Variables
You can have more than one loop variable with foreach. Suppose you have two loop variables x and
y. In the first iteration of the loop, x gets the first value from the value list and y gets the second value.
In the second iteration, x gets the third value and y gets the fourth value. This continues until there are
no more values. If there are not enough values to assign to all the loop variables, the extra variables get
the empty string as their value.
Example 6-11 Multiple loop variables with foreach.
foreach {key value} {orange 55 blue 72 red 24 green} {
puts "$key: $value"
}
orange: 55
blue: 72
red: 24
green:
If you have a command that returns a short list of values, then you can abuse the foreach command to
assign the results of the commands to several variables all at once. For example, suppose the command
MinMax returns two values as a list: the minimum and maximum values. Here is one way to get the
values:
set result [MinMax $list]
set min [lindex $result 0]
set max [lindex $result 1]
The foreach command lets us do this much more compactly:
foreach {min max}[MinMax $list] {break}
The break in the body of the foreach loop guards against the case where the command returns more
values than we expected. This trick is encapsulated into the lassign procedure in Example 10-4 on
page 131.
Multiple Value Lists
The foreach command has the ability to loop over multiple value lists in parallel. In this case, each
value list can also have one or more variables. The foreach command keeps iterating until all values
are used from all value lists. If a value list runs out of values before the last iteration of the loop, its
corresponding loop variables just get the empty string for their value.
Example 6-12 Multiple value lists with foreach.
foreach {k1 k2} {orange blue red green black}value {55 72 24} {
puts "$k1 $k2: $value"
}
orange blue: 55
red green: 72
black : 24
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter6. Control Structure Commands
For
The for command is similar to the C for statement. It takes four arguments:
for initial test final body
The first argument is a command to initialize the loop. The second argument is a boolean expression
that determines whether the loop body will execute. The third argument is a command to execute after
the loop body:
Example 6-13 A for loop.
for {set i 0} {$i < 10} {incr i 3} {
lappend aList $i
}
set aList
=> 0 3 6 9
You could use for to iterate over a list, but you should really use foreach instead. Code like the
following is slow and cluttered:
for {set i 0} {$i < [llength $list]} {incr i} {
set value [lindex $list $i]
}
This is the same as:
foreach value $list {
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter6. Control Structure Commands
Break and Continue
You can control loop execution with the break and continue commands. The break command causes
immediate exit from a loop, while the continue command causes the loop to continue with the next
iteration. There is no goto command in Tcl.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter6. Control Structure Commands
Catch
Until now we have ignored the possibility of errors. In practice, however, a command will raise an
error if it is called with the wrong number of arguments, or if it detects some error condition particular
to its implementation. An uncaught error aborts execution of a script.
[*]
The catch command is used
to trap such errors. It takes two arguments:
[*]
More precisely, the Tcl script unwinds and the current Tcl_Eval procedure in the C runtime library returns TCL_ERROR. There are three
cases. In interactive use, the Tcl shell prints the error message. In Tk, errors that arise during event handling trigger a call to bgerror, a Tcl
procedure you can implement in your application. In your own C code, you should check the result of Tcl_Eval and take appropriate action
in the case of an error.
catch command ?resultVar?
The first argument to catch is a command body. The second argument is the name of a variable that
will contain the result of the command, or an error message if the command raises an error. catch
returns zero if there was no error caught, or a nonzero error code if it did catch an error.
You should use curly braces to group the command instead of double quotes because catch invokes
the full Tcl interpreter on the command. If double quotes are used, an extra round of substitutions
occurs before catch is even called. The simplest use of catch looks like the following:
catch {command }
A more careful catch phrase saves the result and prints an error message:
Example 6-14 A standard catch phrase.
if {[catch { command arg1 arg2 ... }result]} {
puts stderr $result
} else {
The return, break, and continue code options take effect in the caller of the procedure doing the
exceptional return. If -code return is specified, then the calling procedure returns. If -code break is
specified, then the calling procedure breaks out of a loop, and if -code continue is specified, then the
calling procedure continues to the next iteration of the loop. These -code options to return enable the
construction of new control structures entirely in Tcl. The following example implements the break
command with a Tcl procedure:
proc break {} {
return -code break
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartI. Tcl Basics
Chapter 7. Procedures and Scope
Procedures encapsulate a set of commands, and they introduce a local scope for variables. Commands
described are: proc, global, and upvar.
Procedures parameterize a commonly used sequence of commands. In addition, each procedure has a
new local scope for variables. The scope of a variable is the range of commands over which it is
defined. Originally, Tcl had one global scope for shared variables, local scopes within procedures, and
one global scope for procedures. Tcl 8.0 added namespaces that provide new scopes for procedures
and global variables. For simple applications you can ignore namespaces and just use the global scope.
Namespaces are described in Chapter 14.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter7. Procedures and Scope
The proc Command
A Tcl procedure is defined with the proc command. It takes three arguments:
proc name params body
The first argument is the procedure name, which is added to the set of commands understood by the
Tcl interpreter. The name is case sensitive and can contain any characters. Procedure names do not
conflict with variable names. The second argument is a list of parameter names. The last argument is
the body of the procedure.
Once defined, a Tcl procedure is used just like any other Tcl command. When it is called, each
argument is assigned to the corresponding parameter and the body is evaluated. The result of the
procedure is the result returned by the last command in the body. The return command can be used to
return a specific value.
Procedures can have default parameters so that the caller can leave out some of the command
arguments. A default parameter is specified with its name and default value, as shown in the next
example:
Example 7-1 Default parameter values.
proc P2 {a {b 7} {c -2}} {
expr $a / $b + $c
}
P2 6 3
=> 0
Here the procedure P2 can be called with one, two, or three arguments. If it is called with only one
argument, then the parameters b and c take on the values specified in the proc command. If two
arguments are provided, then only c gets the default value, and the arguments are assigned to a and b.
At least one argument and no more than three arguments can be passed to P2.
A procedure can take a variable number of arguments by specifying the args keyword as the last
parameter. When the procedure is called, the args parameter is a list that contains all the remaining
values:
Example 7-2 Variable number of arguments.
proc ArgTest {a {b foo}args} {
foreach param {a b args} {
puts stdout "\t$param = [set $param]"
}
}
set x one
set y {two things}
set z \[special\$
ArgTest $x
=> a = one
b = foo
args =
ArgTest $y $z
=> a = two things
b = [special$
args =
ArgTest $x $y $z
=> a = one
b = two things
args = {[special$}
ArgTest $z $y $z $x
=> a = [special$
b = two things
args = {[special$}one
The effect of the list structure in args is illustrated by the treatment of variable z in Example 7-2. The
value of z has special characters in it. When $z is passed as the value of parameter b, its value comes
through to the procedure unchanged. When $z is part of the optional parameters, quoting is
automatically added to create a valid Tcl list as the value of args. Example 10-3 on page 127
illustrates a technique that uses eval to undo the effect of the added list structure.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter7. Procedures and Scope
Changing Command Names with rename
The rename command changes the name of a command. There are two main uses for rename. The first
is to augment an existing procedure. Before you redefine it with proc, rename the existing command:
rename foo foo.orig
From within the new implementation of foo you can invoke the original command as foo.orig.
Existing users of foo will transparently use the new version.
The other thing you can do with rename is completely hide a command by renaming it to the empty
string. For example, you might not want users to execute UNIX programs, so you could disable exec
with the following command:
rename exec {}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter7. Procedures and Scope
Scope
By default there is a single, global scope for procedure names. This means that you can use a
procedure anywhere in your script. Variables defined outside any procedure are global variables.
However, as described below, global variables are not automatically visible inside procedures. There is
a different namespace for variables and procedures, so you could have a procedure and a global
variable with the same name without conflict. You can use the namespace facility described in Chapter
7 to manage procedures and global variables.
Each procedure has a local scope for variables. That is, variables introduced in the procedure live only
for the duration of the procedure call. After the procedure returns, those variables are undefined.
Variables defined outside the procedure are not visible to a procedure unless the upvar or global
scope commands are used. You can also use qualified names to name variables in a namespace scope.
The global and upvar commands are described later in this chapter. Qualified names are described on
page 198. If the same variable name exists in an outer scope, it is unaffected by the use of that variable
name inside a procedure.
In Example 7-3, the variable a in the global scope is different from the parameter a to P1. Similarly,
the global variable b is different from the variable b inside P1:
Example 7-3 Variable scope and Tcl procedures.
set a 5
set b -8
proc P1 {a} {
set b 42
if {$a < 0} {
return $b
} else {
return $a
}
}
P1 $b
=> 42
P1 [expr $a*2]
=> 10
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter7. Procedures and Scope
The global Command
Global scope is the toplevel scope. This scope is outside of any procedure. Variables defined at the
global scope must be made accessible to the commands inside a procedure by using the global
command. The syntax for global is:
global varName1 varName2 ...
The global command goes inside a procedure.
The global command adds a global variable to the current scope. A common mistake is to have a
single global command and expect that to apply to all procedures. However, a global command in
the global scope has no effect. Instead, you must put a global command in all procedures that access
the global variable. The variable can be undefined at the time the global command is used. When the
variable is defined, it becomes visible in the global scope.
Example 7-4 shows a random number generator. Before we look at the example, let me point out that
the best way to get random numbers in Tcl is to use the rand() math function:
expr rand()
=> .137287362934
The point of the example is to show a state variable, the seed, that has to persist between calls to
random, so it is kept in a global variable. The choice of randomSeed as the name of the global variable
associates it with the random number generator. It is important to pick names of global variables
carefully to avoid conflict with other parts of your program. For comparison, Example 14-1 on page
196 uses namespaces to hide the state variable:
Your code can pass name around as a handle on an object, then use upvar to get access to the data
associated with the object. Your code is just written to use the state variable, which is an alias to the
state variable for the current object. This technique is illustrated in Example 17-7 on page 232.
Namespaces and upvar
You can use upvar to create aliases for namespace variables, too. Namespaces are described in
Chapter 14. For example, as an alternative to reserving all global variables beginning with state, you
can use a namespace to hide these variables:
upvar #0 state::$name state
Now state is an alias to the namespace variable. This upvar trick works from inside any namespace.
Commands That Take Variable Names
Several Tcl commands involve variable names. For example, the Tk widgets can be associated with a
global Tcl variable. The vwait and tkwait commands also take variable names as arguments.
Upvar aliases do not work with text variables.
The aliases created with upvar do not work with these commands, nor do they work if you use trace,
which is described on page 183. Instead, you must use the actual name of the global variable. To
continue the above example where state is an alias, you cannot:
vwait state(foo)
button .b -textvariable state(foo)
Instead, you must
vwait state$name\(foo)
button .b -textvariable state$name\(foo)
The backslash turns off the array reference so Tcl does not try to access name as an array. You do not
need to worry about special characters in $name, except parentheses. Once the name has been passed
into the Tk widget it will be used directly as a variable name.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartI. Tcl Basics
Chapter 8. Tcl Arrays
This chapter describes Tcl arrays, which provide a flexible mechanism to build many other data
structures in Tcl. Tcl command described is: array.
An array is a Tcl variable with a string-valued index. You can think of the index as a key, and the array
as a collection of related data items identified by different keys. The index, or key, can be any string
value. Internally, an array is implemented with a hash table, so the cost of accessing each array
element is about the same. Before Tcl 8.0, arrays had a performance advantage over lists that took time
to access proportional to the size of the list.
The flexibility of arrays makes them an important tool for the Tcl programmer. A common use of
arrays is to manage a collection of variables, much as you use a C struct or Pascal record. This chapter
shows how to create several simple data structures using Tcl arrays.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter8. Tcl Arrays
Array Syntax
The index of an array is delimited by parentheses. The index can have any string value, and it can be
the result of variable or command substitution. Array elements are defined with set:
set arr(index) value
The value of an array element is obtained with $ substitution:
set foo $arr(index)
Example 8-1 uses the loop variable value $i as an array index. It sets arr(x) to the product of 1 * 2
* ... * x:
Example 8-1 Using arrays.
set arr(0) 1
for {set i 1} {$i <= 10} {incr i} {
set arr($i) [expr {$i * $arr([expr $i-1])}]
}
Complex Indices
An array index can be any string, like orange, 5, 3.1415, or foo,bar. The examples in this chapter,
and in this book, often use indices that are pretty complex strings to create flexible data structures. As
a rule of thumb, you can use any string for an index, but avoid using a string that contains spaces.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
Running Programs with exec
The exec command runs programs from your Tcl script.
[*]
For example:
[*]
Unlike other UNIX shell exec commands, the Tcl exec does not replace the current process with the new one. Instead, the Tcl library
forks first and executes the program as a child process.
set d [exec date]
The standard output of the program is returned as the value of the exec command. However, if the
program writes to its standard error channel or exits with a nonzero status code, then exec raises an
error. If you do not care about the exit status, or you use a program that insists on writing to standard
error, then you can use catch to mask the errors:
catch {exec program arg arg} result
The exec command supports a full set of I/O redirection and pipeline syntax. Each process normally
has three I/O channels associated with it: standard input, standard output, and standard error. With I/O
redirection, you can divert these I/O channels to files or to I/O channels you have opened with the Tcl
open command. A pipeline is a chain of processes that have the standard output of one command
hooked up to the standard input of the next command in the pipeline. Any number of programs can be
linked together into a pipeline.
Example 9-1 Using exec on a process pipeline.
set n [exec sort < /etc/passwd | uniq | wc -l 2> /dev/null]
Example 9-1 uses exec to run three programs in a pipeline. The first program is sort, which takes its
input from the file /etc/passwd. The output of sort is piped into uniq, which suppresses duplicate
lines. The output of uniq is piped into wc, which counts the lines. The error output of the command is
diverted to the null device to suppress any error messages. Table 9-1 provides a summary of the syntax
understood by the exec command.
Table 9-1. Summary of the exec syntax for I/O redirection.
-keepnewline
(First argument.) Do not discard trailing newline from the result.
|
Pipes standard output from one process into another.
|&
Pipes both standard output and standard error output.
< fileName Takes input from the named file.
<@ fileId Takes input from the I/O channel identified by fileId.
<< value Takes input from the given value.
> fileName Overwrites fileName with standard output.
2> fileName Overwrites fileName with standard error output.
>& fileName Overwrites fileName with both standard error and standard out.
>> fileName Appends standard output to the named file.
2>> fileName Appends standard error to the named file.
>>& fileName Appends both standard error and standard output to the named file.
>@ fileId Directs standard output to the I/O channel identified by fileId.
2>@ fileId Directs standard error to the I/O channel identified by fileId.
>&@ fileId Directs both standard error and standard output to the I/O channel.
&
As the last argument, indicates pipeline should run in background.
A trailing & causes the program to run in the background. In this case, the process identifier is returned
by the exec command. Otherwise, the exec command blocks during execution of the program, and the
standard output of the program is the return value of exec. The trailing newline in the output is
trimmed off, unless you specify -keepnewline as the first argument to exec.
If you look closely at the I/O redirection syntax, you'll see that it is built up from a few basic building
blocks. The basic idea is that | stands for pipeline, > for output, and < for input. The standard error is
joined to the standard output by &. Standard error is diverted separately by using 2>. You can use your
own I/O channels by using @.
The auto_noexec Variable
The Tcl shell programs are set up during interactive use to attempt to execute unknown Tcl commands
as programs. For example, you can get a directory listing by typing:
ls
instead of:
exec ls
This is handy if you are using the Tcl interpreter as a general shell. It can also cause unexpected
behavior when you are just playing around. To turn this off, define the auto_noexec variable:
set auto_noexec anything
Limitations of exec on Windows
Windows 3.1 has an unfortunate combination of special cases that stem from console-mode programs,
16-bit programs, and 32-bit programs. In addition, pipes are really just simulated by writing output
from one process to a temporary file and then having the next process read from that file. If exec or a
process pipeline fails, it is because of a fundamental limitation of Windows. The good news is that
Windows 95 and Windows NT cleaned up most of the problems with exec. Windows NT 4.0 is the
most robust.
Tcl 8.0p2 was the last release to officially support Windows 3.1. That release includes Tcl1680.dll,
which is necessary to work with the win32s subsystem. If you copy that file into the same directory as
the other Tcl DLLs, you may be able to use later releases of Tcl on Windows 3.1. However, there is no
guarantee this trick will continue to work.
AppleScript on Macintosh
The exec command is not provided on the Macintosh. Tcl ships with an AppleScript extension that
lets you control other Macintosh applications. You can find documentation in the AppleScript.html
that goes with the distribution. You must use package require to load the AppleScript command:
package require Tclapplescript
AppleScript junk
=> bad option "junk": must be compile, decompile, delete,
execute, info, load, run, or store.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
The file Command
The file command provides several ways to check the status of files in the file system. For example,
you can find out if a file exists, what type of file it is, and other file attributes. There are facilities for
manipulating files in a platform-independent manner. Table 9-2 provides a summary of the various
forms of the file command. They are described in more detail later. Note that the split, join, and
pathtype operations were added in Tcl 7.5. The copy, delete, mkdir, and rename operations were
added in Tcl 7.6. The attributes operation was added in Tcl 8.0
Table 9-2. The file command options.
file atime name Returns access time as a decimal string.
file attributes name ?
option? ?value? ...
Queries or sets file attributes. (Tcl 8.0)
file copy ?-force? source
destination
Copies file source to file destination. The source and
destination can be directories. (Tcl 7.6)
file delete ?-force? name Deletes the named file. (Tcl 7.6)
file dirname name Returns parent directory of file name.
file executable name Returns 1 if name has execute permission, else 0.
file exists name Returns 1 if name exists, else 0.
file extension name Returns the part of name from the last dot (i.e., .) to the end. The
dot is included in the return value.
file isdirectory name Returns 1 if name is a directory, else 0.
file isfile name Returns 1 if name is not a directory, symbolic link, or device, else 0.
file join path path... Joins pathname components into a new pathname. (Tcl 7.5)
file lstat name var Places attributes of the link name into var.
syntax for the current platform. You can construct these names with file join described later. You
can also convert a UNIX-like name to a native name with file nativename.
Several of the file operations operate on pathnames as opposed to returning information about the
file itself. You can use the dirname, extension, join, pathtype, rootname, split, and tail
operations on any string; there is no requirement that the pathnames refer to an existing file.
Building up Pathnames: file join
You can get into trouble if you try to construct file names by simply joining components with a slash.
If part of the name is in native format, joining things with slashes will result in incorrect pathnames on
Macintosh and Windows. The same problem arises when you accept user input. The user is likely to
provide file names in native format. For example, this construct will not create a valid pathname on
the Macintosh because $tcl_library is in native format:
set file $tcl_library/init.tcl
Use file join to construct file names.
The platform-independent way to construct file names is with file join. The following command
returns the name of the init.tcl file in native format:
set file [file join $tcl_library init.tcl]
The file join operation can join any number of pathname components. In addition, it has the feature
that an absolute pathname overrides any previous components. For example (on UNIX), /b/c is an
absolute pathname, so it overrides any paths that come before it in the arguments to file join:
file join a b/c d
=> a/b/c/d
file join a /b/c d
=> /b/c/d
On Macintosh, a relative pathname starts with a colon, and an absolute pathname does not. To specify
an absolute path, you put a trailing colon on the first component so that it is interpreted as a volume
specifier. These relative components are joined into a relative pathname:
file join a :b:c d
=> :a:b:c:d
In the next case, b:c is an absolute pathname with b: as the volume specifier. The absolute name
overrides the previous relative name:
file join a b:c d
=> b:c:d
The file join operation converts UNIX-style pathnames to native format. For example, on Macintosh
you get this:
file join /usr/local/lib
=> usr:local:lib
Chopping Pathnames: split, dirname, tail
The file split command divides a pathname into components. It is the inverse of file join. The
split operation detects automatically if the input is in native or UNIX format. The results of file
split may contain some syntax to help resolve ambiguous cases when the results are passed back to
file join. For example, on Macintosh a UNIX-style pathname is split on slash separators. The
Macintosh syntax for a volume specifier (Disk:) is returned on the leading component:
file split "/Disk/System Folder/Extensions"
=> Disk: {System Folder} Extensions
A common reason to split up pathnames is to divide a pathname into the directory part and the file
part. This task is handled directly by the dirname and tail operations. The dirname operation returns
the parent directory of a pathname, while tail returns the trailing component of the pathname:
file dirname /a/b/c
=> /a/b
file tail /a/b/c
=> c
For a pathname with a single component, the dirname option returns ".", on UNIX and Windows, or
":" on Macintosh. This is the name of the current directory.
The extension and root options are also complementary. The extension option returns everything
from the last period in the name to the end (i.e., the file suffix including the period.) The root option
returns everything up to, but not including, the last period in the pathname:
file root /a/b.c
=> /a/b
file extension /a/b.c
=> .c
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
Manipulating Files and Directories
Tcl 7.6 added file operations to copy files, delete files, rename files, and create directories. In earlier
versions it was necessary to exec other programs to do these things, except on Macintosh, where cp,
rm, mv, mkdir, and rmdir were built in. These commands are no longer supported on the Macintosh.
Your scripts should use the file command operations described below to manipulate files in a
platform-independent way.
File name patterns are not directly supported by the file operations. Instead, you can use the glob
command described on page 115 to get a list of file names that match a pattern.
Copying Files
The file copy operation copies files and directories. The following example copies file1 to file2.
If file2 already exists, the operation raises an error unless the -force option is specified:
file copy ?-force? file1 file2
Several files can be copied into a destination directory. The names of the source files are preserved.
The -force option indicates that files under directory can be replaced:
file copy ?-force? file1 file2 ... directory
Directories can be recursively copied. The -force option indicates that files under dir2 can be
replaced:
file copy ?-force? dir1 dir2
Creating Directories
atime
The last access time, in seconds.
ctime
The last change time (not the create time), in seconds.
dev
The device identifier, an integer.
gid
The group owner, an integer.
ino
The file number (i.e., inode number), an integer.
mode
The permission bits.
mtime
The last modify time, in seconds.
nlink
The number of links, or directory references, to the file.
size
The number of bytes in the file.
type
file, directory, characterSpecial, blockSpecial, fifo, link, or socket.
uid
The owner's user ID, an integer.
Example 9-3 uses the device (dev) and inode (ino) attributes of a file to determine whether two
pathnames reference the same file. The attributes are UNIX specific; they are not well defined on
Windows and Macintosh.
Example 9-3 Determining whether pathnames reference the same file.
proc fileeq { path1 path2 } {
file stat $path1 stat1
file stat $path2 stat2
expr $stat1(ino) == $stat2(ino) && \
$stat1(dev) == $stat2(dev)
}
The file attributes operation was added in Tcl 8.0 to provide access to platform-specific
attributes. The attributes operation lets you set and query attributes. The interface uses option-value
pairs. With no options, all the current values are returned.
file attributes book.doc
=> -creator FRAM -hidden 0 -readonly 0 -type MAKR
These Macintosh attributes are explained in Table 9-4. The four-character type codes used on
Macintosh are illustrated on page 516. With a single option, only that value is returned:
file attributes book.doc -readonly
=> 0
The attributes are modified by specifying one or more optionvalue pairs. Setting attributes can raise
an error if you do not have the right permissions:
file attributes book.doc -readonly 1 -hidden 0
Table 9-4. Platform-specific file attributes.
-permissions
mode
File permission bits. mode is a number with bits defined by the chmod system
call. (UNIX)
-group ID The group owner of the file. (UNIX)
-owner ID The owner of the file. (UNIX)
-archive bool The archive bit, which is set by backup programs. (Windows)
-hidden bool If set, then the file does not appear in listings. (Windows, Macintosh)
-readonly bool If set, then you cannot write the file. (Windows, Macintosh)
-system bool If set, then you cannot remove the file. (Windows)
-creator type type is 4-character code of creating application. (Macintosh)
-type type type is 4-character type code. (Macintosh)
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
Input/Output Command Summary
The following sections describe how to open, read, and write files. The basic model is that you open a
file, read or write it, then close the file. Network sockets also use the commands described here.
Socket programming is discussed in Chapter 17, and more advanced event-driven I/O is described in
Chapter 16. Table 9-5 lists the basic commands associated with file I/O:
Table 9-5. Tcl commands used for file access.
open what ?access? ?permissions? Returns channel ID for a file or pipeline.
puts ?-nonewline? ?channel?
string
Writes a string.
gets channel ?varname? Reads a line.
read channel ?numBytes? Reads numBytes bytes, or all data.
read -nonewline channel Reads all bytes and discard the last \n.
tell channel Returns the seek offset.
seek channel offset ?origin? Sets the seek offset. origin is one of start, current, or
end.
eof channel Queries end-of-file status.
flush channel Writes buffers of a channel.
close channel Closes an I/O channel.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
Opening Files for I/O
The open command sets up an I/O channel to either a file or a pipeline of processes. The return value
of open is an identifier for the I/O channel. Store the result of open in a variable and use the variable as
you used the stdout, stdin, and stderr identifiers in the examples so far. The basic syntax is:
open what ?access? ?permissions?
The what argument is either a file name or a pipeline specification similar to that used by the exec
command. The access argument can take two forms, either a short character sequence that is
compatible with the fopen library routine, or a list of POSIX access flags. Table 9-6 summarizes the
first form, while Table 9-7 summarizes the POSIX flags. If access is not specified, it defaults to read.
Example 9-4 Opening a file for writing.
set fileId [open /tmp/foo w 0600]
puts $fileId "Hello, foo!"
close $fileId
Table 9-6. Summary of the open access arguments.
r
Opens for reading. The file must exist.
r+
Opens for reading and writing. The file must exist.
w
Opens for writing. Truncate if it exists. Create if it does not exist.
w+
Opens for reading and writing. Truncate or create.
a
Opens for writing. Data is appended to the file.
a+
Opens for reading and writing. Data is appended.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
Matching File Names with glob
The glob command expands a pattern into the set of matching file names. The general form of the
glob command is:
glob ?flags? pattern ?pattern? ...
The pattern syntax is similar to the string match patterns:
* matches zero or more characters.
? matches a single character.
[abc] matches a set of characters.
{a,b,c} matches any of a, b, or c.
All other characters must match themselves.
The -nocomplain flag causes glob to return an empty list if no files match the pattern. Otherwise,
glob raises an error if no files match.
The -- flag must be used if the pattern begins with a -.
Unlike the glob matching in csh, the Tcl glob command matches only the names of existing files. In
csh, the {a,b} construct can match nonexistent names. In addition, the results of glob are not sorted.
Use the lsort command to sort its result if you find it important.
Example 9-11 shows the FindFile procedure, which traverses the file system hierarchy using
recursion. At each iteration it saves its current directory and then attempts to change to the next
subdirectory. A catch guards against bogus names. The glob command matches file names:
Example 9-11 Finding a file by name.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
Environment Variables
Environment variables are a collection of string-valued variables associated with each process. The
process's environment variables are available through the global array env. The name of the
environment variable is the index, (e.g., env(PATH)), and the array element contains the current value
of the environment variable. If assignments are made to env, they result in changes to the
corresponding environment variable. Environment variables are inherited by child processes, so
programs run with the exec command inherit the environment of the Tcl script. The following
example prints the values of environment variables.
Example 9-12 Printing environment variable values.
proc printenv { args } {
global env
set maxl 0
if {[llength $args] == 0} {
set args [lsort [array names env]]
}
foreach x $args {
if {[string length $x] > $maxl} {
set maxl [string length $x]
}
}
incr maxl 2
foreach x $args {
puts stdout [format "%*s = %s" $maxl $x $env($x)]
}
}
printenv USER SHELL TERM
=>
USER = welch
SHELL = /bin/csh
TERM = tx
Note: Environment variables can be initialized for Macintosh applications by editing a resource of type
STR# whose name is Tcl Environment Variables. This resource is part of the tclsh and wish
applications. Follow the directions on page 28 for using ResEdit. The format of the resource values is
NAME=VALUE.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter9. Working with Files and Programs
The registry Command
Windows uses the registry to store various system configuration information. The Windows tool to
browse and edit the registry is called regedit. Tcl provides a registry command. It is a loadable
package that you must load by using:
package require registry
The registry structure has keys, value names, and typed data. The value names are stored under a key,
and each value name has data associated with it. The keys are organized into a hierarchical naming
system, so another way to think of the value names is as an extra level in the hierarchy. The main point
is that you need to specify both a key name and a value name in order to get something out of the
registry. The key names have one of the following formats:
\\hostname\rootname\keypath
rootname\keypath
rootname
The rootname is one of HKEY_LOCAL_MACHINE, HKEY_PERFORMANCE_DATA, HKEY_USERS,
HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_CURRENT_CONFIG, or HKEY_DYN_DATA. Tables 9-8
and 9-9 summarize the registry command and data types:
Table 9-8. The registry command.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartII. Advanced Tcl
Chapter 10. Quoting Issues and Eval
This chapter describes explicit calls to the interpreter with the eval command. An extra round of
substitutions is performed that results in some useful effects. The chapter describes the quoting
problems with eval and the ways to avoid them. The uplevel command evaluates commands in a
different scope. The subst command does substitutions but no command invocation.
Dynamic evaluation makes Tcl flexible and powerful, but it can be tricky to use properly. The basic
idea is that you create a string and then use the eval command to interpret that string as a command or
a series of commands. Creating program code on the fly is easy with an interpreted language like Tcl,
and very hard, if not impossible, with a statically compiled language like C++ or Java. There are
several ways that dynamic code evaluation is used in Tcl:
In some cases, a simple procedure isn't quite good enough, and you need to glue together a
command from a few different pieces and then execute the result using eval. This often occurs
with wrappers, which provide a thin layer of functionality over existing commands.
Callbacks are script fragments that are saved and evaluated later in response to some event.
Examples include the commands associated with Tk buttons, fileevent I/O handlers, and after
timer handlers. Callbacks are a flexible way to link different parts of an application together.
You can add new control structures to Tcl using the uplevel command. For example, you can
write a function that applies a command to each line in a file or each node in a tree.
You can have a mixture of code and data, and just process the code part with the subst
command. For example, this is useful in HTML templates described in Chapter 18. There are
also some powerful combinations of subst and regsub described in Chapter 11.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter10. Quoting Issues and Eval
Constructing Code with the list Command
It can be tricky to assemble a command so that it is evaluated properly by eval. The same difficulties
apply to commands like after, uplevel, and the Tk send command, all of which have similar
properties to eval, except that the command evaluation occurs later or in a different context.
Constructing commands dynamically is a source of many problems. The worst part is that you can
write code that works sometimes but not others, which can be very confusing.
Use list when constructing commands.
The root of the quoting problems is the internal use of concat by eval and similar commands to
concatenate their arguments into one command string. The concat can lose some important list
structure so that arguments are not passed through as you expect. The general strategy to avoid these
problems is to use list and lappend to explicitly form the command callback as a single, well-
structured list.
The eval Command
The eval command results in another call to the Tcl interpreter. If you construct a command
dynamically, you must use eval to interpret it. For example, suppose we want to construct the
following command now but execute it later:
puts stdout "Hello, World!"
In this case, it is sufficient to do the following:
Even though there are only two Tcl commands to create a user interface button, we will write a
procedure that replaces the two commands with one. Our first version might be:
proc PackedButton {name txt cmd} {
button $name -text $txt -command $cmd
pack $name -side left
}
This is not a very flexible procedure. The main problem is that it hides the full power of the Tk button
command, which can really take about 20 widget configuration options, such as -background, -
cursor, -relief, and more. They are listed on page 391. For example, you can easily make a red
button like this:
button .foo -text Foo -command foo -background red
A better version of PackedButton uses args to pass through extra configuration options to the button
command. The args parameter is a list of all the extra arguments passed to the Tcl procedure. My first
attempt to use $args looked like this, but it was not correct:
proc PackedButton {name txt cmd args} {
button $name -text $txt -command $cmd $args
pack $name -side left
}
PackedButton .foo "Hello, World!" {exit} -background red
=> unknown option "-background red"
The problem is that $args is a list value, and button gets the whole list as a single argument. Instead,
button needs to get the elements of $args as individual arguments.
Use eval with $args
In this case, you can use eval because it concatenates its arguments to form a single list before
evaluation. The single list is, by definition, the same as a single Tcl command, so the button
command parses correctly. Here we give eval two lists, which it joins into one command:
eval {button $name -text $txt -command $cmd} $args
The use of the braces in this command is discussed in more detail below. We also generalize our
procedure to take some options to the pack command. This argument, pack, must be a list of packing
options. The final version of PackedButton is shown in Example 10-3:
Example 10-3 Using eval with $args.
# PackedButton creates and packs a button.
proc PackedButton {path txt cmd {pack {-side right}} args} {
eval {button $path -text $txt -command $cmd} $args
eval {pack $path} $pack
}
In PackedButton, both pack and args are list-valued parameters that are used as parts of a command.
The internal concat done by eval is perfect for this situation. The simplest call to PackedButton is:
PackedButton .new "New" { New }
The quotes and curly braces are redundant in this case but are retained to convey some type
information. The quotes imply a string label, and the braces imply a command. The pack argument
takes on its default value, and the args variable is an empty list. The two commands executed by
PackedButton are:
button .new -text New -command New
pack .new -side right
PackedButton creates a horizontal stack of buttons by default. The packing can be controlled with a
packing specification:
PackedButton .save "Save" { Save $file } {-side left}
The two commands executed by PackedButton are:
button .new -text Save -command { Save $file }
pack .new -side left
The remaining arguments, if any, are passed through to the button command. This lets the caller fine-
tune some of the button attributes:
PackedButton .quit Quit { Exit } {-side left -padx 5} \
-background red}
The two commands executed by PackedButton are:
button .quit -text Quit -command { Exit }-background red
pack .quit -side left -padx 5
You can see a difference between the pack and args argument in the call to PackedButton. You need
to group the packing options explicitly into a single argument. The args parameter is automatically
made into a list of all remaining arguments. In fact, if you group the extra button parameters, it will be
a mistake:
PackedButton .quit Quit { Exit } {-side left -padx 5} \
{-background red}
=> unknown option "-background red"
Correct Quoting with eval
What about the peculiar placement of braces in PackedButton?
eval {button $path -text $txt -command $cmd} $args
By using braces, we control the number of times different parts of the command are seen by the Tcl
evaluator. Without any braces, everything goes through two rounds of substitution. The braces prevent
one of those rounds. In the above command, only $args is substituted twice. Before eval is called, the
$args is replaced with its list value. Then, eval is invoked, and it concatenates its two list arguments
into one list, which is now a properly formed command. The second round of substitutions done by
eval replaces the txt and cmd values.
Do not use double quotes with eval.
You may be tempted to use double quotes instead of curly braces in your uses of eval. Don't give in!
Using double quotes is, mostly likely, wrong. Suppose the first eval command is written like this:
eval "button $path -text $txt -command $cmd $args"
Incidentally, the previous is equivalent to:
eval button $path -text $txt -command $cmd $args
These versions happen to work with the following call because txt and cmd have one-word values
with no special characters in them:
PackedButton .quit Quit { Exit }
The button command that is ultimately evaluated is:
button .quit -text Quit -command { Exit }
In the next call, an error is raised:
PackedButton .save "Save As" [list Save $file]
=> unknown option "As"
This is because the button command is this:
button .save -text Save As -command Save /a/b/c
But it should look like this instead:
button .save -text {Save As}-command {Save /a/b/c}
The problem is that the structure of the button command is now wrong. The value of txt and cmd are
substituted first, before eval is even called, and then the whole command is parsed again. The worst
part is that sometimes using double quotes works, and sometimes it fails. The success of using double
quotes depends on the value of the parameters. When those values contain spaces or special characters,
the command gets parsed incorrectly.
Braces: the one true way to group arguments to eval.
To repeat, the safe construct is:
eval {button $path -text $txt -command $cmd} $args
The following variations are also correct. The first uses list to do quoting automatically, and the
others use backslashes or braces to prevent the extra round of substitutions:
eval [list button $path -text $txt -command $cmd] $args
eval button \$path -text \$txt -command \$cmd $args
eval button {$path} -text {$txt} -command {$cmd} $args
Finally, here is one more incorrect approach that tries to quote by hand:
eval "button {$path}-text {$txt}-command {$cmd} $args"
The problem above is that quoting is not always done with curly braces. If a value contains an
unmatched curly brace, Tcl would have used backslashes to quote it, and the above command would
raise an error:
set blob "foo\{bar space"
=> foo{bar space
eval "puts {$blob}"
=> missing close brace
eval puts {$blob}
=> foo{bar space
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter10. Quoting Issues and Eval
The uplevel Command
The uplevel command is similar to eval, except that it evaluates a command in a different scope than
the current procedure. It is useful for defining new control structures entirely in Tcl. The syntax for
uplevel is:
uplevel ?level? command ?list1 list2 ...?
As with upvar, the level parameter is optional and defaults to 1, which means to execute the
command in the scope of the calling procedure. The other common use of level is #0, which means to
evaluate the command in the global scope. You can count up farther than one (e.g., 2 or 3), or count
down from the global level (e.g., #1 or #2), but these cases rarely make sense.
When you specify the command argument, you must be aware of any substitutions that might be
performed by the Tcl interpreter before uplevel is called. If you are entering the command directly,
protect it with curly braces so that substitutions occur in the other scope. The following affects the
variable x in the caller's scope:
uplevel {set x [expr $x + 1]}
However, the following will use the value of x in the current scope to define the value of x in the
calling scope, which is probably not what was intended:
uplevel "set x [expr $x + 1]"
If you are constructing the command dynamically, again use list. This fragment is used later in
Example 10-4:
uplevel [list foreach $args $valueList {break}]
It is common to have the command in a variable. This is the case when the command has been passed
into your new control flow procedure as an argument. In this case, you should evaluate the command
one level up. Put the level in explicitly to avoid cases where $cmd looks like a number!
uplevel 1 $cmd
Another common scenario is reading commands from users as part of an application. In this case, you
should evaluate the command at the global scope. Example 16-2 on page 220 illustrates this use of
uplevel:
uplevel #0 $cmd
If you are assembling a command from a few different lists, such as the args parameter, then you can
use concat to form the command:
uplevel [concat $cmd $args]
The lists in $cmd and $args are concatenated into a single list, which is a valid Tcl command. Like
eval, uplevel uses concat internally if it is given extra arguments, so you can leave out the explicit
use of concat. The following commands are equivalent:
uplevel [concat $cmd $args]
uplevel "$cmd $args"
uplevel $cmd $args
Example 10-4 shows list assignment using the foreach trick described on Page 75. List assignment is
useful if a command returns several values in a list. The lassign procedure assigns the list elements to
several variables. The lassign procedure hides the foreach trick, but it must use the uplevel
command so that the loop variables get assigned in the correct scope. The list command is used to
construct the foreach command that is executed in the caller's scope. This is necessary so that
$variables and $values get substituted before the command is evaluated in the other scope.
Example 10-4 lassign: list assignment with foreach.
# Assign a set of variables from a list of values.
# If there are more values than variables, they are returned.
# If there are fewer values than variables,
# the variables get the empty string.
proc lassign {valueList args} {
if {[llength $args] == 0} {
error "wrong # args: lassign list varname ?varname..?"
}
if {[llength $valueList] == 0} {
# Ensure one trip through the foreach loop
set valueList [list {}]
}
uplevel 1 [list foreach $args $valueList {break}]
return [lrange $valueList [llength $args] end]
}
Example 10-5 illustrates a new control structure with the File_Process procedure that applies a
callback to each line in a file. The call to uplevel allows the callback to be concatenated with the
line to form the command. The list command is used to quote any special characters in line, so it
appears as a single argument to the command.
Example 10-5 The File_Process procedure applies a command to each line of a file.
proc File_Process {file callback} {
set in [open $file]
while {[gets $file line] >= 0} {
uplevel 1 $callback [list $line]
}
close $in
}
What is the difference between these two commands?
uplevel 1 [list $callback $line]
uplevel 1 $callback [list $line]
The first form limits callback to be the name of the command, while the second form allows
callback to be a command prefix. Once again, what is the bug with this version?
uplevel 1 $callback $line
The arbitrary value of $line is concatenated to the callback command, and it is likely to be a
malformed command when executed.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter10. Quoting Issues and Eval
The subst Command
The subst command is useful when you have a mixture of Tcl commands, Tcl variable references,
and plain old data. The subst command looks through the data for square brackets, dollar signs, and
backslashes, and it does substitutions on those. It leaves the rest of the data alone:
set a "foo bar"
subst {a=$a date=[exec date]}
=> a=foo bar date=Thu Dec 15 10:13:48 PST 1994
The subst command does not honor the quoting effect of curly braces. It does substitutions regardless
of braces:
subst {a=$a date={[exec date]}}
=> a=foo bar date={Thu Dec 15 10:15:31 PST 1994}
You can use backslashes to prevent variable and command substitution.
subst {a=\$a date=\[exec date]}
=> a=$a date=[exec date]
You can use other backslash substitutions like \uXXXX to get Unicode characters, \n to get newlines, or
\-newline to hide newlines.
The subst command takes flags that limit the substitutions it will perform. The flags are -
nobackslashes, -nocommands, or -novariables. You can specify one or more of these flags before
the string that needs to be substituted:
subst -novariables {a=$a date=[exec date]}
=> a=$a date=Thu Dec 15 10:15:31 PST 1994
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter11. Regular Expressions
When to Use Regular Expressions
Regular expressions can seem overly complex at first. They introduce their own syntax and their own
rules, and you may be tempted to use simpler commands like string first, string range, or
string match to process your strings. However, often a single regular expression command can
replace a sequence of several string commands. Any time you can replace several Tcl commands
with one, you get a performance improvement. Furthermore, the regular expression matcher is
implemented in optimized C code, so pattern matching is fast.
The regular expression matcher does more than test for a match. It also tells you what part of your
input string matches the pattern. This is useful for picking data out of a large input string. In fact, you
can capture several pieces of data in just one match by using subexpressions. The regexp Tcl
command makes this easy by assigning the matching data to Tcl variables. If you find yourself using
string first and string range to pick out data, remember that regexp can do it in one step
instead.
The regular expression matcher is structured so that patterns are first compiled into an form that is
efficient to match. If you use the same pattern frequently, then the expensive compilation phase is
done only once, and all your matching uses the efficient form. These details are completely hidden by
the Tcl interface. If you use a pattern twice, Tcl will nearly always be able to retrieve the compiled
form of the pattern. As you can see, the regular expression matcher is optimized for lots of heavy-duty
string processing.
Avoiding a Common Problem
Group your patterns with curly braces.
One of the stumbling blocks with regular expressions is that they use some of the same special
characters as Tcl. Any pattern that contains brackets, dollar signs, or spaces must be quoted when used
in a Tcl command. In many cases you can group the regular expression with curly braces, so Tcl pays
no attention to it. However, when using Tcl 8.0 (or earlier) you may need Tcl to do backslash
substitutions on part of the pattern, and then you need to worry about quoting the special characters in
the regular expression.
Advanced regular expressions eliminate this problem because backslash substitution is now done by
the regular expression engine. Previously, to get \n to mean the newline character (or \t for tab) you
had to let Tcl do the substitution. With Tcl 8.1, \n and \t inside a regular expression mean newline
and tab. In fact, there are now about 20 backslash escapes you can use in patterns. Now more than
ever, remember to group your patterns with curly braces to avoid conflicts between Tcl and the regular
expression engine.
The patterns in the first sections of this Chapter ignore this problem. The sample expressions in Table
11-7 on page 151 are quoted for use within Tcl scripts. Most are quoted simply by putting the whole
pattern in braces, but some are shown without braces for comparison.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter11. Regular Expressions
Regular Expression Syntax
This section describes the basics of regular expression patterns, which are found in all versions of Tcl.
There are occasional references to features added by advanced regular expressions, but they are
covered in more detail starting on page 138. There is enough syntax in regular expressions that there
are five tables that summarize all the options. These tables appear together starting at page 145.
A regular expression is a sequence of the following items:
A literal character.
A matching character, character set, or character class.
A repetition quantifier.
An alternation clause.
A subpattern grouped with parentheses.
Matching Characters
Most characters simply match themselves. The following pattern matches an a followed by a b:
ab
The general wild-card character is the period, ".". It matches any single character. The following
pattern matches an a followed by any character:
a.
Remember that matches can occur anywhere within a string; a pattern does not have to match the
whole string. You can change that by using anchors, which are described on page 137.
Character Sets
The matching character can be restricted to a set of characters with the [xyz] syntax. Any of the
characters between the two brackets is allowed to match. For example, the following matches either
Hello or hello:
[Hh]ello
The matching set can be specified as a range over the character set with the [x-y] syntax. The
following matches any digit:
[0-9]
There is also the ability to specify the complement of a set. That is, the matching character can be
anything except what is in the set. This is achieved with the [^xyz] syntax. Ranges and complements
can be combined. The following matches anything except the uppercase and lowercase letters:
[^a-zA-Z]
Using special characters in character sets.
If you want a ] in your character set, put it immediately after the initial opening bracket. You do not
need to do anything special to include [ in your character set. The following matches any square
brackets or curley braces:
[][{}]
Most regular expression syntax characters are no longer special inside character sets. This means you
do not need to backslash anything inside a bracketed character set except for backslash itself. The
following pattern matches several of the syntax characters used in regular expressions:
[][+*?()|\\]
Advanced regular expressions add names and backslash escapes as shorthand for common sets of
characters like white space, alpha, alphanumeric, and more. These are described on page 139 and
listed in Table 11-3 on page 146.
Quantifiers
Repetition is specified with *, for zero or more, +, for one or more, and ?, for zero or one. These
quantifiers apply to the previous item, which is either a matching character, a character set, or a
subpattern grouped with parentheses. The following matches a string that contains b followed by zero
or more a's:
ba*
You can group part of the pattern with parentheses and then apply a quantifier to that part of the
pattern. The following matches a string that has one or more sequences of ab:
(ab)+
The pattern that matches anything, even the empty string, is:
.*
These quantifiers have a greedy matching behavior: They match as many characters as possible.
Advanced regular expressions add nongreedy matching, which is described on page 140. For example,
a pattern to match a single line might look like this:
.*\n
However, as a greedy match, this will match all the lines in the input, ending with the last newline in
the input string. The following pattern matches up through the first newline.
[^\n]*\n
We will shorten this pattern even further on page 140 by using nongreedy quantifiers. There are also
special newline sensitive modes you can turn on with some options described on page 143.
Alternation
Alternation lets you test more than one pattern at the same time. The matching engine is designed to be
able to test multiple patterns in parallel, so alternation is efficient. Alternation is specified with |, the
pipe symbol. Another way to match either Hello or hello is:
hello|Hello
You can also write this pattern as:
(h|H)ello
or as:
[hH]ello
Anchoring a Match
By default a pattern does not have to match the whole string. There can be unmatched characters
before and after the match. You can anchor the match to the beginning of the string by starting the
pattern with ^, or to the end of the string by ending the pattern with $. You can force the pattern to
match the whole string by using both. All strings that begin with spaces or tabs are matched with:
^[ \t]+
If you have many text lines in your input, you may be tempted to think of ^ as meaning "beginning of
line" instead of "beginning of string." By default, the ^ and $ anchors are relative to the whole input,
and embedded newlines are ignored. Advanced regular expressions support options that make the ^
and $ anchors line-oriented. They also add the \A and \Z anchors that always match the beginning and
end of the string, respectively.
Backslash Quoting
Use the backslash character to turn off these special characters :
. * ? + [ ] ( ) ^ $ | \
For example, to match the plus character, you will need:
\+
Remember that this quoting is not necessary inside a bracketed expression (i.e., a character set
definition.) For example, to match either plus or question mark, either of these patterns will work:
(\+|\?)
[+?]
To match a single backslash, you need two. You must do this everywhere, even inside a bracketed
expression. Or you can use \B, which was added as part of advanced regular expressions. Both of these
match a single backslash:
\\
\B
Unknown backslash sequences are an error.
Versions of Tcl before 8.1 ignored unknown backslash sequences in regular expressions. For example,
\= was just =, and \w was just w. Even \n was just n, which was probably frustrating to many
beginners trying to get a newline into their pattern. Advanced regular expressions add backslash
sequences for tab, newline, character classes, and more. This is a convenient improvement, but in rare
cases it may change the semantics of a pattern. Usually these cases are where an unneeded backslash
suddenly takes on meaning, or causes an error because it is unknown.
Matching Precedence
If a pattern can match several parts of a string, the matcher takes the match that occurs earliest in the
input string. Then, if there is more than one match from that same point because of alternation in the
pattern, the matcher takes the longest possible match. The rule of thumb is: first, then longest. This
rule gets changed by nongreedy quantifiers that prefer a shorter match.
Watch out for *, which means zero or more, because zero of anything is pretty easy to match. Suppose
your pattern is:
[a-z]*
This pattern will match against 123abc, but not how you expect. Instead of matching on the letters in
the string, the pattern will match on the zero-length substring at the very beginning of the input string!
This behavior can be seen by using the -indices option of the regexp command described on page
148. This option tells you the location of the matching string instead of the value of the matching
string.
Capturing Subpatterns
Use parentheses to capture a subpattern. The string that matches the pattern within parentheses is
remembered in a matching variable, which is a Tcl variable that gets assigned the string that matches
the pattern. Using parentheses to capture subpatterns is very useful. Suppose we want to get everything
between the <td> and </td> tags in some HTML. You can use this pattern:
<td>([^<]*)</td>
The matching variable gets assigned the part of the input string that matches the pattern inside the
parentheses. You can capture many subpatterns in one match, which makes it a very efficient way to
pick apart your data. Matching variables are explained in more detail on page 148 in the context of the
regexp command.
Sometimes you need to introduce parentheses but you do not care about the match that occurs inside
them. The pattern is slightly more efficient if the matcher does not need to remember the match.
Advanced regular expressions add noncapturing parentheses with this syntax:
(?:pattern)
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter11. Regular Expressions
Advanced Regular Expressions
The syntax added by advanced regular expressions is mostly just short hand notation for constructs
you can make with the basic syntax already described. There are also some new features that add
additional power: nongreedy quantifiers, back references, look-ahead patterns, and named character
classes. If you are just starting out with regular expressions, you can ignore most of this section, except
for the one about backslash sequences. Once you master the basics, of if you are already familar with
regular expressions in Tcl (or the UNIX vi editor or grep utility), then you may be interested in the
new features of advanced regular expressions.
Compatibility with Patterns in Tcl 8.0
Advanced regular expressions add syntax in an upward compatible way. Old patterns continue to work
with the new matcher, but advanced regular expressions will raise errors if given to old versions of
Tcl. For example, the question mark is used in many of the new constructs, and it is artfully placed in
locations that would not be legal in older versions of regular expressions. The added syntax is
summarized in Table 11-2 on page 145.
If you have unbraced patterns from older code, they are very likely to be correct in Tcl 8.1 and later
versions. For example, the following pattern picks out everything up to the next newline. The pattern
is unbraced, so Tcl substitutes the newline character for each occurrence of \n. The square brackets are
quoted so that Tcl does not think they delimit a nested commmand:
regexp "(\[^\n\]+)\n" $input
The above command behaves identically when using advanced regular expressions, although you can
now also write it like this:
regexp {([^\n]+)\n} $input
The curley braces hide the brackets from the Tcl parser, so they do not need to be escaped with
arguments are optional. If present, match is set to be the part of the string that matched the pattern. The
remaining variables are set to be the substrings of string that matched the corresponding subpatterns
in pattern. The correspondence is based on the order of left parentheses in the pattern to avoid
ambiguities that can arise from nested subpatterns.
Example 11-2 uses regexp to pick the hostname out of the DISPLAY environment variable, which has
the form:
hostname:display.screen
Example 11-2 Using regular expressions to parse a string.
set env(DISPLAY) sage:0.1
regexp {([^:]*):}$env(DISPLAY) match host
=> 1
set match
=> sage:
set host
=> sage
The pattern involves a complementary set, [^:], to match anything except a colon. It uses repetition,
*, to repeat that zero or more times. It groups that part into a subexpression with parentheses. The
literal colon ensures that the DISPLAY value matches the format we expect. The part of the string that
matches the complete pattern is stored into the match variable. The part that matches the subpattern is
stored into host. The whole pattern has been grouped with braces to quote the square brackets.
Without braces it would be:
regexp (\[^:\]*): $env(DISPLAY) match host
With advanced regular expressions the nongreedy quantifier *? can replace the complementary set:
regexp (.*?): $env(DISPLAY) match host
This is quite a powerful statement, and it is efficient. If we had only had the string command to work
with, we would have needed to resort to the following, which takes roughly twice as long to interpret:
set i [string first : $env(DISPLAY)]
if {$i >= 0} {
set host [string range $env(DISPLAY) 0 [expr $i-1]]
}
A Pattern to Match URLs
Example 11-3 demonstrates a pattern with several subpatterns that extract the different parts of a URL.
There are lots of subpatterns, and you can determine which match variable is associated with which
subpattern by counting the left parenthesis. The pattern will be discussed in more detail after the
example:
Example 11-3 A pattern to match URLs.
set url http://www.beedub.com:80/index.html
regexp {([^:]+)://([^:/]+)(:([0-9]+))?(/.*)}$url \
match protocol x serverport path
=> 1
set match
=> http://www.beedub.com:80/index.html
set protocol
=> http
set server
=> www.beedub.com
set x
=> :80
set port
=> 80
set path
=> /index.html
Let's look at the pattern one piece at a time. The first part looks for the protocol, which is separated by
a colon from the rest of the URL. The first part of the pattern is one or more characters that are not a
colon, followed by a colon. This matches the http: part of the URL:
[^:]+:
Using nongreedy +? quantifier, you could also write that as:
.+?:
The next part of the pattern looks for the server name, which comes after two slashes. The server name
is followed either by a colon and a port number, or by a slash. The pattern uses a complementary set
that specifies one or more characters that are not a colon or a slash. This matches the
//www.beedub.com part of the URL:
//[^:/]+
The port number is optional, so a subpattern is delimited with parentheses and followed by a question
mark. An additional set of parentheses are added to capture the port number without the leading colon.
This matches the :80 part of the URL:
(:([0-9]+))?
The last part of the pattern is everything else, starting with a slash. This matches the /index.html part
of the URL:
/.*
Use subpatterns to parse strings.
To make this pattern really useful, we delimit several subpatterns with parentheses:
([^:]+)://([^:/]+)(:([0-9]+))?(/.*)
These parentheses do not change the way the pattern matches. Only the optional port number really
needs the parentheses in this example. However, the regexp command gives us access to the strings
that match these subpatterns. In one step regexp can test for a valid URL and divide it into the
protocol part, the server, the port, and the trailing path.
The parentheses around the port number include the : before the digits. We've used a dummy variable
that gets the : and the port number, and another match variable that just gets the port number. By using
noncapturing parentheses in advanced regular expressions, we can eliminate the unused match
variable. We can also replace both complementary character sets with a nongreedy .+? match.
Example 11-4 shows this variation:
Example 11-4 An advanced regular expression to match URLs.
set url http://www.beedub.com:80/book/
regexp {(.+?)://(.+?)(?::([0-9]+))?(/.*)}$url \
match protocol server port path
=> 1
set match
=> http://www.beedub.com:80/book/
set protocol
=> http
set server
=> www.beedub.com
set port
=> 80
set path
=> /book/
Sample Regular Expressions
The table in this section lists regular expressions as you would use them in Tcl commands. Most are
quoted with curly braces to turn off the special meaning of square brackets and dollar signs. Other
patterns are grouped with double quotes and use backslash quoting because the patterns include
backslash sequences like \n and \t. In Tcl 8.0 and earlier, these must be substituted by Tcl before the
regexp command is called. In these cases, the equivalent advanced regular expression is also shown.
Table 11-7. Sample regular expressions.
{^[yY]}
Begins with y or Y, as in a Yes answer.
{^(yes|YES|Yes)$}
Exactly "yes", "Yes", or "YES".
"^\[^ \t:\]+:"
Begins with colon-delimited field that has no spaces or tabs.
{^\S+:}
Same as above, using \S for "not space".
"^\[ \t]*$"
A string of all spaces or tabs.
{(?n)^\s*$}
A blank line using newline sensitive mode.
"(\n|^)\[^\n\]*(\n|$)"
A blank line, the hard way.
{^[A-Za-z]+$}
Only letters.
{^[[:alpha:]]+$}
Only letters, the Unicode way.
{[A-Za-z0-9_]+}
Letters, digits, and the underscore.
{\w+}
Letters, digits, and the underscore using \w.
{[][${}\\]}
The set of Tcl special characters: ] [ $ { } \
"\[^\n\]*\n"
Everything up to a newline.
{.*?\n}
Everything up to a newline using nongreedy *?
{\.}
A period.
{[][$^?+*()|\\]}
The set of regular expression special characters: ] [ $ ^ ? + * ( ) | \
<H1>(.*?)</H1>
An H1 HTML tag. The subpattern matches the string between the tags.
<!--.*?-->
HTML comments.
{[0-9a-hA-H][0-9a-hA-H]}
2 hex digits.
{[[:xdigit:]]{2}}
2 hex digits, using advanced regular expressions.
{\d{1,3}}
1 to 3 digits, using advanced regular expressions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter11. Regular Expressions
The regsub Command
The regsub command does string substitution based on pattern matching. It is very useful for
processing your data. It can perform simple tasks like replacing sequences of spaces and tabs with a
single space. It can perform complex data transforms, too, as described in the next section. Its syntax
is:
regsub ?switches? pattern string subspec varname
The regsub command returns the number of matches and replacements, or 0 if there was no match.
regsub copies string to varname, replacing occurrences of pattern with the substitution specified by
subspec. If the pattern does not match, then string is copied to varname without modification. The
optional switches include:
-all, which means to replace all occurrences of the pattern. Otherwise only the first occurrence
is replaced.
The -nocase, -expanded, -line, -linestop, and -lineanchor switches are the same as in the
regexp command. They are described on page 148.
The -- switch separates the pattern from the switches, which is necessary if your pattern begins
with a -.
The replacement pattern, subspec, can contain literal characters as well as the following special
sequences:
& is replaced with the string that matched the pattern.
\x , where x is a number, is replaced with the string that matched the corresponding subpattern in
pattern. The correspondence is based on the order of left parentheses in the pattern
specification.
The following replaces a user's home directory with a ~:
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartII. Advanced Tcl
Chapter 12. Script Libraries and Packages
Collections of Tcl commands are kept in libraries and organized into packages. Tcl automatically
loads libraries as an application uses their commands. Tcl commands discussed are: package,
pkg_mkIndex, auto_mkindex, unknown, and tcl_findLibrary.
Libraries group useful sets of Tcl procedures so that they can be used by multiple applications. For
example, you could use any of the code examples that come with this book by creating a script library
and then directing your application to check in that library for missing procedures. One way to
structure a large application is to have a short main script and a library of support scripts. The
advantage of this approach is that not all the Tcl code needs to be loaded to start the application.
Applications start up quickly, and as new features are accessed, the code that implements them is
loaded automatically.
The Tcl package facility supports version numbers and has a provide/require model of use. Typically,
each file in a library provides one package with a particular version number. Packages also work with
shared object libraries that implement Tcl commands in compiled code, which are described in
Chapter 44. A package can be provided by a combination of script files and object files. Applications
specify which packages they require and the libraries are loaded automatically. The package facility is
an alternative to the auto loading scheme used in earlier versions of Tcl. You can use either
mechanism, and this chapter describes them both.
If you create a package you may wish to use the namespace facility to avoid conflicts between
procedures and global variables used in different packages. Namespaces are the topic of Chapter 14.
Before Tcl 8.0 you had to use your own conventions to avoid conflicts. This chapter explains a simple
coding convention for large Tcl programs. I use this convention in exmh, a mail user interface that has
grown from about 2,000 to over 35,000 lines of Tcl code. A majority of the code has been contributed
by the exmh user community. Such growth might not have been possible without coding conventions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
Locating Packages: The auto_path Variable
The package facility assumes that Tcl libraries are kept in well-known directories. The list of well-
known directories is kept in the auto_path Tcl variable. This is initialized by tclsh and wish to include
the Tcl script library directory, the Tk script library directory (for wish), and the parent directory of the
Tcl script library directory. For example, on my Macintosh auto_path is a list of these three
directories:
Disk:System Folder:Extensions:Tool Command Language:tcl8.2
Disk:System Folder:Extensions:Tool Command Language
Disk:System Folder:Extensions:Tool Command Language:tk8.2
On my Windows 95 machine the auto_path lists these directories:
c:\Program Files\Tcl\lib\Tcl8.2
c:\Program Files\Tcl\lib
c:\Program Files\Tcl\lib\Tk8.2
On my UNIX workstation the auto_path lists these directories:
/usr/local/tcl/lib/tcl8.2
/usr/local/tcl/lib
/usr/local/tcl/lib/tk8.2
The package facility searches these directories and their subdirectories for packages. The easiest way
to manage your own packages is to create a directory at the same level as the Tcl library:
/usr/local/tcl/lib/welchbook
Packages in this location, for example, will be found automatically because the auto_path list
includes /usr/local/tcl/lib. You can also add directories to the auto_path explicitly:
lappend auto_path directory
One trick I often use is to put the directory containing the main script into the auto_path. The
following command sets this up:
lappend auto_path [file dirname [info script]]
If your code is split into bin and lib directories, then scripts in the bin directory can add the adjacent
lib directory to their auto_path with this command:
lappend auto_path \
[file join [file dirname [info script]] ../lib]
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
Using Packages
Each script file in a library declares what package it implements with the package provide
command:
package provide name version
The name identifies the package, and the version has a major.minor format. The convention is that
the minor version number can change and the package implementation will still be compatible. If the
package changes in an incompatible way, then the major version number should change. For example,
Chapter 17 defines several procedures that use the HTTP network protocol. These include Http_Open,
Http_Get, and Http_Validate. The file that contains the procedures starts with this command:
package provide Http 1.0
Case is significant in package names. In particular, the package that comes with Tcl is named http ?all
lowercase.
More than one file can contribute to the same package simply by specifying the same name and
version. In addition, different versions of the same package can be kept in the same directory but in
different files.
An application specifies the packages it needs with the package require command:
package require name ?version? ?-exact?
If the version is left off, then the highest available version is loaded. Otherwise the highest version
with the same major number is loaded. For example, if the client requires version 1.1, version 1.2
could be loaded if it exists, but versions 1.0 and 2.0 would not be loaded. You can restrict the package
to a specific version with the -exact flag. If no matching version can be found, then the package
require command raises an error.
Put the appropriate package require and package provide commands in your code.
Ensure that your library directories, or their parent directories, are listed in the auto_path
variable.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
The package Command
The package command has several operations that are used primarily by the pkg_mkIndex procedure
and the automatic loading facility. These operations are summarized in Table 12-2.
Table 12-2. The package command.
package forget package Deletes registration information for package.
package ifneeded
package ?command?
Queries or sets the command used to set up automatic loading of a
package.
package names
Returns the set of registered packages.
package provide package
version
Declares that a script file defines commands for package with the
given version.
package require package
?version? ?-exact?
Declares that a script uses package. The -exact flag specifies that the
exact version must be loaded. Otherwise, the highest matching
version is loaded.
package unknown ?
command?
Queries or sets the command used to locate packages.
package vcompare v1 v2 Compares version v1 and v2. Returns 0 if they are equal, minus 1 if v1
is less than v2, or 1 if v1 is greater than v2.
package versions
package
Returns which versions of the package are registered.
package vsatisfies v1
v2
Returns 1 if v1 is greater or equal to v2 and still has the same major
version number. Otherwise returns 0.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
Libraries Based on the tclIndex File
You can create libraries without using the package command. The basic idea is that a directory has a
library of script files, and an index of the Tcl commands defined in the library is kept in a tclIndex
file. The drawback is that versions are not supported and you may need to adjust the auto_path to list
your library directory. The main advantage of this approach is that this mechanism has been part of Tcl
since the earliest versions. If you currently maintain a library using tclIndex files, it will still work.
You must generate the index that records what procedures are defined in the library. The
auto_mkindex procedure creates the index, which is stored in a file named tclIndex that is kept in the
script library directory. (Watch out for the difference in capitalization between auto_mkindex and
pkg_mkIndex!) Suppose all the examples from this book are in the directory
/usr/local/tcl/welchbook. You can make the examples into a script library by creating the
tclIndex file:
auto_mkindex /usr/local/tcl/welchbook *.tcl
You will need to update the tclIndex file if you add procedures or change any of their names. A
conservative approach to this is shown in the next example. It is conservative because it re-creates the
index if anything in the library has changed since the tclIndex file was last generated, whether or not
the change added or removed a Tcl procedure.
Example 12-1 Maintaining a tclIndex file.
proc Library_UpdateIndex { libdir } {
set index [file join $libdir tclIndex]
if {![file exists $index]} {
set doit 1
} else {
set age [file mtime $index]
set doit 0
# Changes to directory may mean files were deleted
if {[file mtime $libdir] > $age} {
set doit 1
} else {
# Check each file for modification
foreach file [glob [file join $libdir *.tcl]] {
if {[file mtime $file] > $age} {
set doit 1
break
}
}
}
}
if { $doit } {
auto_mkindex $libdir *.tcl
}
}
Tcl uses the auto_path variable to record a list of directories to search for unknown commands. To
continue our example, you can make the procedures in the book examples available by putting this
command at the beginning of your scripts:
lappend auto_path /usr/local/tcl/welchbook
This has no effect if you have not created the tclIndex file. If you want to be extra careful, you can
call Library_UpdateIndex. This will update the index if you add new things to the library.
lappend auto_path /usr/local/tcl/welchbook
Library_UpdateIndex /usr/local/tcl/welchbook
This will not work if there is no tclIndex file at all because Tcl won't be able to find the
implementation of Library_UpdateIndex. Once the tclIndex has been created for the first time, then
this will ensure that any new procedures added to the library will be installed into tclIndex. In
practice, if you want this sort of automatic update, it is wise to include something like the
Library_UpdateIndex procedure directly into your application as opposed to loading it from the
library it is supposed to be maintaining.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
The unknown Command
Automatic loading of Tcl commands is implemented by the unknown command. Whenever the Tcl
interpreter encounters a command that it does not know about, it calls the unknown command with the
name of the missing command. The unknown command is implemented in Tcl, so you are free to
provide your own mechanism to handle unknown commands. This chapter describes the behavior of
the default implementation of unknown, which can be found in the init.tcl file in the Tcl library. The
location of the library is returned by the info library command.
How Auto Loading Works
The unknown command uses an array named auto_index. One element of the array is defined for each
procedure that can be automatically loaded. The auto_index array is initialized by the package
mechanism or by tclIndex files. The value of an auto_index element is a command that defines the
procedure. Typical commands are:
source [file join $dir bind_ui.tcl]
load [file join $dir mime.so] Mime
The $dir gets substituted with the name of the directory that contains the library file, so the result is a
source or load command that defines the missing Tcl command. The substitution is done with eval,
so you could initialize auto_index with any commands at all. Example 12-2 is a simplified version of
the code that reads the tclIndex file.
Example 12-2 Loading a tclIndex file.
# This is a simplified part of the auto_load_index procedure.
# Go through auto_path from back to front.
set i [expr [llength $auto_path]-1]
for {} {$i >= 0} {incr i -1} {
set dir [lindex $auto_path $i]
if [catch {open [file join $dir tclIndex]} f] {
# No index
continue
}
# eval the file as a script. Because eval is
# used instead of source, an extra round of
# substitutions is performed and $dir gets expanded
# The real code checks for errors here.
eval [read $f]
close $f
}
Disabling the Library Facility: auto_noload
If you do not want the unknown procedure to try and load procedures, you can set the auto_noload
variable to disable the mechanism:
set auto_noload anything
Auto loading is quite fast. I use it regularly on applications both large and small. A large application
will start faster if you only need to load the code necessary to start it up. As you access more features
of your application, the code will load automatically. Even a small application benefits from auto
loading because it encourages you to keep commonly used code in procedure libraries.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
Interactive Conveniences
The unknown command provides a few other conveniences. These are used only when you are typing
commands directly. They are disabled once execution enters a procedure or if the Tcl shell is not being
used interactively. The convenience features are automatic execution of programs, command history,
and command abbreviation. These options are tried, in order, if a command implementation cannot be
loaded from a script library.
Auto Execute
The unknown procedure implements a second feature: automatic execution of external programs. This
makes a Tcl shell behave more like other UNIX shells that are used to execute programs. The search
for external programs is done using the standard PATH environment variable that is used by other shells
to find programs. If you want to disable the feature all together, set the auto_noexec variable:
set auto_noexec anything
History
The history facility described in Chapter 13 is implemented by the unknown procedure.
Abbreviations
If you type a unique prefix of a command, unknown recognizes it and executes the matching command
for you. This is done after automatic program execution is attempted and history substitutions are
performed.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
Tcl Shell Library Environment
Tcl searches for its script library directory when it starts up. In early versions of Tcl you had to
compile in the correct location, set a Windows registry value, or set the TCL_LIBRARY environment
variable to the correct location. Recent versions of Tcl use a standard searching scheme to locate the
script library. The search understands the standard installation and build environments for Tcl, and it
should eliminate the need to use the TCL_LIBRARY environment variable. On Windows the search
for the library used to depend on registry values, but this has also been discontinued in favor of a
standard search. In summary, "it should just work." However, this section explains how Tcl finds its
script library so that you can troubleshoot problems.
Locating the Tcl Script Library
The default library location is defined when you configure the source distribution, which is explained
on page 644. At this time an initial value for the auto_path variable is defined. (This default value
appears in tcl_pkgPath, but changing this variable has no effect once Tcl has started. I just pretend
tcl_pkgPath does not exist.) These values are just hints; Tcl may use other directories depending on
what it finds in the file system.
When Tcl starts up, it searches for a directory that contains its init.tcl startup script. You can short-
circuit the search by defining the TCL_LIBRARY environment variable. If this is defined, Tcl uses it only
for its script library directory. However, you should not need to define this with normal installations of
Tcl 8.0.5 or later. In my environment I'm often using several different versions of Tcl for various
applications and testing purposes, so setting TCL_LIBRARY is never correct for all possibilities. If I find
myself setting this environment variable, I know something is wrong with my Tcl installations!
The standard search starts with the default value that is compiled into Tcl (e.g.,
/usr/local/lib/tcl8.1.) After that, the following directories are examined for an init.tcl file.
These example values assume Tcl version 8.1 and patch level 8.1.1:
../lib/tcl8.1
../../lib/tcl8.1
../library
../../tcl8.1.1/library
../../../tcl8.1.1/library
The first two directories correspond to the standard installation directories, while the last three
correspond to the standard build environment for Tcl or Tk. The first directory in the list that contains
a valid init.tcl file becomes the Tcl script library. This directory location is saved in the
tcl_library global variable, and it is also returned by the info library command.
The primary thing defined by init.tcl is the implementation of the unknown procedure. It also
initializes auto_path to contain $tcl_library and the parent directory of $tcl_library. There may
be additional directories added to auto_path depending on the compiled in value of tcl_pkgPath.
tcl_findLibrary
A generalization of this search is implemented by tcl_findLibrary. This procedure is designed for
use by extensions like Tk and [incr Tcl]. Of course, Tcl cannot use tcl_findLibrary itself because it
is defined in init.tcl!
The tcl_findLibrary procedure searches relative to the location of the main program (e.g., tclsh or
wish) and assumes a standard installation or a standard build environment. It also supports an override
by an environment variable, and it takes care of sourcing an initialization script. The usage of
tcl_findLibrary is:
tcl_findLibrary base version patch script enVar varName
The base is the prefix of the script library directory name. The version is the main version number
(e.g., "8.0"). The patch is the full patch level (e.g., "8.0.3"). The script is the initialization script to
source from the directory. The enVar names an environment variable that can be used to override the
default search path. The varName is the name of a variable to set to name of the directory found by
tcl_findLibrary. A side effect of tcl_findLibrary is to source the script from the directory. An
example call is:
tcl_findLibrary tk 8.0 8.0.3 tk.tcl TK_LIBRARY tk_library
This call first checks to see whether TK_LIBRARY is defined in the environment. If so, it uses its value.
Otherwise, it searches the following directories for a file named tk.tcl. It sources the script and sets
the tk_library variable to the directory containing that file. The search is relative to the value
returned by info nameofexecutable:
../lib/tk8.0
../../lib/tk8.0
../library
../../tk8.0.3/library
../../../tk8.0.3/library
Tk also adds $tk_library to the end of auto_path, so the other script files in that directory are
available to the application:
lappend auto_path $tk_library
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter12. Script Libraries and Packages
Coding Style
If you supply a package, you need to follow some simple coding conventions to make your library
easier to use by other programmers. You can use the namespace facility introduced in Tcl 8.0. You can
also use conventions to avoid name conflicts with other library packages and the main application.
This section describes the conventions I developed before namespaces were added to Tcl.
A Module Prefix for Procedure Names
The first convention is to choose an identifying prefix for the procedures in your package. For
example, the preferences package in Chapter 42 uses Pref as its prefix. All the procedures provided
by the library begin with Pref. This convention is extended to distinguish between private and
exported procedures. An exported procedure has an underscore after its prefix, and it is acceptable to
call this procedure from the main application or other library packages. Examples include Pref_Add,
Pref_Init, and Pref_Dialog. A private procedure is meant for use only by the other procedures in
the same package. Its name does not have the underscore. Examples include PrefDialogItem and
PrefXres.
This naming convention precludes casual names like doit, setup, layout, and so on. Without using
namespaces, there is no way to hide procedure names, so you must maintain the naming convention
for all procedures in a package.
A Global Array for State Variables
You should use the same prefix on the global variables used by your package. You can alter the
capitalization; just keep the same prefix. I capitalize procedure names and use lowercase letters for
variables. By sticking with the same prefix you identify what variables belong to the package and you
avoid conflict with other packages.
Collect state in a global array.
In general, I try to use a single global array for a package. The array provides a convenient place to
collect a set of related variables, much as a struct is used in C. For example, the preferences package
uses the pref array to hold all its state information. It is also a good idea to keep the use of the array
private. It is better coding practice to provide exported procedures than to let other modules access
your data structures directly. This makes it easier to change the implementation of your package
without affecting its clients.
If you do need to export a few key variables from your module, use the underscore convention to
distinguish exported variables. If you need more than one global variable, just stick with the prefix
convention to avoid conflicts.
The Official Tcl Style Guide
John Ousterhout has published two programming style guides, one for C programming known as "The
Engineering Manual" and one for Tcl scripts known as "The Style Guide". These describe details
about file structure as well as naming conventions for modules, procedures, and variables. The Tcl
Style Guide conventions use Tcl namespaces to separate packages. Namespaces automatically provide
a way to avoid conflict between procedure names. Namespaces also support collections of variables
without having to use arrays for grouping.
You can find these style guides on the CD-ROM and also in ftp://ftp.scriptics.com/pub/tcl/doc. The
Engineering Manual is distributed as a compressed tar file, engManual.tar.Z, that contains sample
files as well as the main document. The Style Guide is distributed as styleGuide.ps (or .pdf).
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartII. Advanced Tcl
Chapter 13. Reflection and Debugging
This chapter describes commands that give you a view into the interpreter. The history command and
a simple debugger are useful during development and debugging. The info command provides a
variety of information about the internal state of the Tcl interpreter. The time command measures the
time it takes to execute a command. Tcl commands discussed are: clock, info, history, and time.
Reflection provides feedback to a script about the internal state of the interpreter. This is useful in a
variety of cases, from testing to see whether a variable exists to dumping the state of the interpreter.
The info command provides lots of different information about the interpreter.
The clock command is useful for formatting dates as well as parsing date and time values. It also
provides high-resolution timer information for precise measurements.
Interactive command history is the third topic of the chapter. The history facility can save you some
typing if you spend a lot of time entering commands interactively.
Debugging is the last topic. The old-fashioned approach of adding puts commands to your code is
often quite useful. For tough problems, however, a real debugger is invaluable. The TclPro tools from
Scriptics include a high quality debugger and static code checker. The tkinspect program is an
inspector that lets you look into the state of a Tk application. It can hook up to any Tk application
dynamically, so it proves quite useful.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter13. Reflection and Debugging
The clock Command
The clock command has facilities for getting the current time, formatting time values, and scanning
printed time strings to get an integer time value. The clock command was added in Tcl 7.5. Table 13-
1 summarizes the clock command:
Table 13-1. The clock command.
clock clicks
A system-dependent high resolution counter.
clock format value ?-format str? Formats a clock value according to str.
clock scan string ?-base clock? ?
-gmt boolean?
Parses date string and return seconds value. The clock
value determines the date.
clock seconds
Returns the current time in seconds.
The following command prints the current time:
clock format [clock seconds]
=> Sun Nov 24 14:57:04 1996
The clock seconds command returns the current time, in seconds since a starting epoch. The clock
format command formats an integer value into a date string. It takes an optional argument that
controls the format. The format strings contains % keywords that are replaced with the year, month,
day, date, hours, minutes, and seconds, in various formats. The default string is:
%a %b %d %H:%M:%S %Z %Y
Tables 13-2 and 13-3 summarize the clock formatting strings:
info nameofexecutable
The file name of the program (e.g., of tclsh or wish).
info patchlevel
The release patch level for Tcl.
info procs ?pattern? A list of all Tcl procedures, or those that match pattern.
info script
The name of the file being processed, or the empty string.
info sharedlibextension
The file name suffix of shared libraries.
info tclversion
The version number of Tcl.
info vars ?pattern? A list of all visible variables, or those matching pattern.
Variables
There are three categories of variables: local, global, and visible. Information about these categories is
returned by the locals, globals, and vars operations, respectively. The local variables include
procedure arguments as well as locally defined variables. The global variables include all variables
defined at the global scope. The visible variables include locals, plus any variables made visible via
global or upvar commands. A pattern can be specified to limit the returned list of variables to those
that match the pattern. The pattern is interpreted according to the rules of string match, which is
described on page 48:
info globals auto*
=> auto_index auto_noexec auto_path
Namespaces, which are the topic of the next chapter, partition global variables into different scopes.
You query the variables visible in a namespace with:
info vars namespace::*
Remember that a variable may not be defined yet even though a global or upvar command has
declared it visible in the current scope. Use the info exists command to test whether a variable or an
array element is defined or not. An example is shown on page 90.
Procedures
You can find out everything about a Tcl procedure with the args, body, and default operations. This
is illustrated in the following Proc_Show example. The puts commands use the -nonewline flag
because the newlines in the procedure body, if any, are retained:
Example 13-2 Printing a procedure definition.
proc Proc_Show {{namepat *}{file stdout}}{
foreach proc [info procs $namepat] {
set space ""
puts -nonewline $file "proc $proc {"
foreach arg [info args $proc] {
if [info default $proc $arg value] {
puts -nonewline $file "$space{$arg $value}"
} else {
puts -nonewline $file $space$arg
}
set space " "
}
# No newline needed because info body may return a
# value that starts with a newline
puts -nonewline $file "}{"
puts -nonewline $file [info body $proc]
puts $file "}"
}
}
Example 13-3 is a more elaborate example of procedure introspection that comes from the
direct.tcl file, which is part of the Tcl Web Server described in Chapter 18. This code is used to
map URL requests and the associated query data directly into Tcl procedure calls. This is discussed in
more detail on page 247. The Web server collects Web form data into an array called form. Example
13-3 matches up elements of the form array with procedure arguments, and it collects extra elements
into an args parameter. If a form value is missing, then the default argument value or the empty string
is used:
Example 13-3 Mapping form data onto procedure arguments.
# cmd is the name of the procedure to invoke
# form is an array containing form values
set cmdOrig $cmd
set params [info args $cmdOrig]
# Match elements of the form array to parameters
foreach arg $params {
if {![info exists form($arg)]} {
if {[info default $cmdOrig $arg value]} {
lappend cmd $value
} elseif {[string compare $arg "args"] == 0} {
set needargs yes
} else {
lappend cmd {}
}
} else {
lappend cmd $form($arg)
}
}
# If args is a parameter, then append the form data
# that does not match other parameters as extra parameters
if {[info exists needargs]} {
foreach {name value} $valuelist {
if {[lsearch $params $name] < 0} {
lappend cmd $name $value
}
}
}
# Eval the command
set code [catch $cmd result]
The info commands operation returns a list of all commands, which includes both built-in commands
defined in C and Tcl procedures. There is no operation that just returns the list of built-in commands.
Example 13-4 finds the built-in commands by removing all the procedures from the list of commands.
Example 13-4 Finding built-in commands.
proc Command_Info {{pattern *}}{
# Create a table of procedures for quick lookup
foreach p [info procs $pattern] {
set isproc($p) 1
}
# Look for command not in the procedure table
set result {}
foreach c [info commands $pattern] {
if {![info exists isproc($c)]}{
lappend result $c
}
}
return [lsort $result]
}
The Call Stack
The info level operation returns information about the Tcl evaluation stack, or call stack. The global
level is numbered zero. A procedure called from the global level is at level one in the call stack. A
procedure it calls is at level two, and so on. The info level command returns the current level
number of the stack if no level number is specified.
If a positive level number is specified (e.g., info level 3), then the command returns the procedure
name and argument values at that level in the call stack. If a negative level is specified, then it is
relative to the current call stack. Relative level -1 is the level of the current procedure's caller, and
relative level 0 is the current procedure. The following example prints the call stack. The Call_trace
procedure avoids printing information about itself by starting at one less than the current call stack
level:
Example 13-5 Getting a trace of the Tcl call stack.
proc Call_Trace {{file stdout}}{
puts $file "Tcl Call Trace"
for {set x [expr [info level]-1]}{$x > 0}{incr x -1}{
puts $file "$x: [info level $x]"
}
}
Command Evaluation
If you want to know how many Tcl commands are executed, use the info cmdcount command. This
counts all commands, not just top-level commands. The counter is never reset, so you need to sample
it before and after a test run if you want to know how many commands are executed during a test.
The info complete operation figures out whether a string is a complete Tcl command. This is useful
for command interpreters that need to wait until the user has typed in a complete Tcl command before
passing it to eval. Example 13-6 defines Command_Process that gets a line of input and builds up a
command. When the command is complete, the command is executed at the global scope.
Command_Process takes two callbacks as arguments. The inCmd is evaluated to get the line of input,
and the outCmd is evaluated to display the results. Chapter 10 describes callbacks why the curly braces
are used with eval as they are in this example:
Example 13-6 A procedure to read and evaluate commands.
proc Command_Process {inCmd outCmd}{
global command
append command(line) [eval $inCmd]
if [info complete $command(line)] {
set code [catch {uplevel #0 $command(line)}result]
eval $outCmd {$result $code}
set command(line) {}
}
}
proc Command_Read {{in stdin}}{
if [eof $in] {
if {$in != "stdin"}{
close $in
}
return {}
}
return [gets $in]
}
proc Command_Display {file result code}{
puts stdout $result
}
while {![eof stdin]}{
Command_Process {Command_Read stdin}\
{Command_Display stdout}
}
Scripts and the Library
The name of the current script file is returned with the info script command. For example, if you
use the source command to read commands from a file, then info script returns the name of that
file if it is called during execution of the commands in that script. This is true even if the info script
command is called from a procedure that is not defined in the script.
Use info script to find related files.
I often use info script to source or process files stored in the same directory as the script that is
running. A few examples are shown in Example 13-7.
Example 13-7 Using info script to find related files.
# Get the directory containing the current script.
set dir [file dirname [info script]]
# Source a file in the same directory
source [file join $dir helper.tcl]
# Add an adjacent script library directory to auto_path
# The use of ../lib with file join is cross-platform safe.
lappend auto_path [file join $dir ../lib]
The pathname of the Tcl library is stored in the tcl_library variable, and it is also returned by the
info library command. While you could put scripts into this directory, it might be better to have a
separate directory and use the script library facility described in Chapter 12. This makes it easier to
deal with new releases of Tcl and to package up your code if you want other sites to use it.
Version Numbers
Each Tcl release has a version number such as 7.4 or 8.0. This number is returned by the info
tclversion command. If you want your script to run on a variety of Tcl releases, you may need to test
the version number and take different actions in the case of incompatibilities between releases.
The Tcl release cycle starts with one or two alpha and beta releases before the final release, and there
may even be a patch release after that. The info patchlevel command returns a qualified version
number, like 8.0b1 for the first beta release of 8.0. We switched from using "p" (e.g., 8.0p2) to a three-
level scheme (e.g., 8.0.3) for patch releases. The patch level is zero for the final release (e.g., 8.2.0). In
general, you should be prepared for feature changes during the beta cycle, but there should only be bug
fixes in the patch releases. Another rule of thumb is that the Tcl script interface remains quite
compatible between releases; feature additions are upward compatible.
Execution Environment
The file name of the program being executed is returned with info nameofexecutable. This is more
precise than the name in the argv0 variable, which could be a relative name or a name found in a
command directory on your command search path. It is still possible for info nameofexecutable to
return a relative pathname if the user runs your program as ./foo, for example. The following
construct always returns the absolute pathname of the current program. If info nameofexecutable
returns an absolute pathname, then the value of the current directory is ignored. The pwd command is
described on page 115:
file join [pwd] [info nameofexecutable]
A few operations support dynamic loading of shared libraries, which are described in Chapter 44. The
info sharedlibextension returns the file name suffix of dynamic link libraries. The info loaded
command returns a list of libraries that have been loaded into an interpreter. Multiple interpreters are
described in Chapter 19.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter13. Reflection and Debugging
Cross-Platform Support
Tcl is designed so that you can write scripts that run unchanged on UNIX, Macintosh, and Windows
platforms. In practice, you may need a small amount of code that is specific to a particular platform.
You can find out information about the platform via the tcl_platform variable. This is an array with
these elements defined:
tcl_platform(platform) is one of unix, macintosh, or windows.
tcl_platform(os) identifies the operating system. Examples include MacOS, Solaris, Linux,
Win32s (Windows 3.1 with the Win32 subsystem), Windows 95, Windows NT, and SunOS.
tcl_platform(osVersion) gives the version number of the operating system.
tcl_platform(machine) identifies the hardware. Examples include ppc (Power PC), 68k
(68000 family), sparc, intel, mips, and alpha.
tcl_platform(isWrapped) indicates that the application has been wrapped up into a single
executable with TclPro Wrapper. This is not defined in normal circumstances.
tcl_platform(user) gives the login name of the current user.
tcl_platform(debug) indicates that Tcl was compiled with debugging symbols.
tcl_platform(thread) indicates that Tcl was compiled with thread support enabled.
On some platforms a hostname is defined. If available, it is returned with the info hostname
command. This command may return an empty string.
One of the most significant areas affected by cross-platform portability is the file system and the way
files are named. This topic is discussed on page 103.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter13. Reflection and Debugging
Tracing Variable Values
The trace command registers a command to be called whenever a variable is accessed, modified, or
unset. This form of the command is:
trace variable name ops command
The name is a Tcl variable name, which can be a simple variable, an array, or an array element. If a
whole array is traced, the trace is invoked when any element is used according to ops. The ops
argument is one or more of the letters r, for read traces, w, for write traces, and u, for unset traces. The
command is executed when one of these events occurs. It is invoked as:
command name1 name2 op
The name1 argument is the variable or array name. The name2 argument is the name of the array index,
or null if the trace is on a simple variable. If there is an unset trace on an entire array and the array is
unset, name2 is also null. The value of the variable is not passed to the procedure. The traced variable
is one level up the Tcl call stack. The upvar, uplevel, or global commands need to be used to make
the variable visible in the scope of command. These commands are described in more detail in Chapter
7.
A read trace is invoked before the value of the variable is returned, so if it changes the variable itself,
the new value is returned. A write trace is called after the variable is modified. The unset trace is
called after the variable is unset.
Read-Only Variables
Example 13-8 uses traces to implement a read-only variable. A variable is modified before the trace
procedure is called, so the ReadOnly variable is needed to preserve the original value. When a variable
is unset, the traces are automatically removed, so the unset trace action reestablishes the trace
explicitly. Note that the upvar alias (e.g., var) cannot be used to set up the trace:
history
Short for history info with no count.
history add command ?
exec?
Adds the command to the history list. If exec is specified, then execute
the command.
history change new ?
event?
Changes the command specified by event to new in the command
history.
history event ?event? Returns the command specified by event.
history info ?count? Returns a formatted history list of the last count commands, or of all
commands.
history keep count Limits the history to the last count commands.
history nextid
Returns the number of the next event.
history redo ?event? Repeats the specified command.
History Syntax
Some extra syntax is supported when running interactively to make the history facility more
convenient to use. Table 13-6 shows the special history syntax supported by tclsh and wish.
Table 13-6. Special history syntax.
!!
Repeats the previous command.
!n Repeats command number n.If n is negative it counts backward from the current
command. The previous command is event -1.
!prefix Repeats the last command that begins with prefix.
!pattern Repeats the last command that matches pattern.
^old^new Globally replaces old with new in the last command.
The next example shows how some of the history operations work:
Example 13-10 Interactive history usage.
% set a 5
5
% set a [expr $a+7]
12
% history
1 set a 5
2 set a [expr $a+7]
3 history
% !2
19
% !!
26
% ^7^13
39
% !h
1 set a 5
2 set a [expr $a+7]
3 history
4 set a [expr $a+7]
5 set a [expr $a+7]
6 set a [expr $a+13]
7 history
A Comparison to C Shell History Syntax
The history syntax shown in the previous example is simpler than the history syntax provided by the C
shell. Not all of the history operations are supported with special syntax. The substitutions (using
^old^new) are performed globally on the previous command. This is different from the quick-history
of the C shell. Instead, it is like the !:gs/old/new/ history command. So, for example, if the example
had included ^a^b in an attempt to set b to 39, an error would have occurred because the command
would have used b before it was defined:
set b [expr $b+7]
If you want to improve the history syntax, you will need to modify the unknown command, which is
where it is implemented. This command is discussed in more detail in Chapter 12. Here is the code
from the unknown command that implements the extra history syntax. The main limitation in
comparison with the C shell history syntax is that the ! substitutions are performed only when ! is at
the beginning of the command:
Example 13-11 Implementing special history syntax.
# Excerpts from the standard unknown command
# uplevel is used to run the command in the right context
if {$name == "!!"}{
set newcmd [history event]
} elseif {[regexp {^!(.+)$}$name dummy event]}{
set newcmd [history event $event]
} elseif {[regexp {^\^([^^]*)\^([^^]*)\^?$}$name x old new]}{
set newcmd [history event -1]
catch {regsub -all -- $old $newcmd $new newcmd}
}
if {[info exists newcmd]}{
history change $newcmd 0
return [uplevel $newcmd]
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter13. Reflection and Debugging
Debugging
The rapid turnaround with Tcl coding means that it is often sufficient to add a few puts statements to
your script to gain some insight about its behavior. This solution doesn't scale too well, however. A
slight improvement is to add a Debug procedure that can have its output controlled better. You can log
the information to a file, or turn it off completely. In a Tk application, it is simple to create a text
widget to hold the contents of the log so that you can view it from the application. Here is a simple
Debug procedure. To enable it you need to set the debug(enable) variable. To have its output go to
your terminal, set debug(file) to stderr.
Example 13-12 A Debug procedure.
proc Debug { args }{
global debug
if {![info exists debug(enabled)]}{
# Default is to do nothing
return
}
puts $debug(file) [join $args " "]
}
proc DebugOn {{file {}}}{
global debug
set debug(enabled) 1
if {[string length $file] == 0}{
set debug(file) stderr
} else {
if [catch {open $file w}fileID] {
puts stderr "Cannot open $file: $fileID"
set debug(file) stderr
} else {
puts stderr "Debug info to $file"
set debug(file) $fileID
}
}
}
TclPro Compiler
TclPro Compiler is really just a reader and writer for the byte codes that the Tcl byte-code compiler
generates internally. It lets you precompile scripts and save the results, and then load the byte-code
later instead of raw source. This provides a great way to hide your source code, if that is important to
you. It turns out to save less time than you might think, however. By the time it reads the file from
disk, decodes it, and builds the necessary Tcl data structures, it is not much faster than reading a
source file and compiling it on the fly.
TclPro Wrapper
TclPro Wrapper assembles a collection of Tcl scripts, data files, and a Tcl/Tk interpreter into a single
executable file. This makes distribution of your Tcl application as easy as giving out one file. The Tcl
C library has been augmented with hooks in its file system access routines so that a wrapped
application can look inside itself for files. The rule is that if you use a relative pathname (i.e.,
lib/myfile.dat), then the wrapped application will look first inside itself for the file. If the file is not
found, or if the pathname is absolute (e.g., /usr/local/lib/myfile.dat), then Tcl looks on your
hard disk for the file. The nice thing about TclPro Wrapper is that it handles all kinds of files, not just
Tcl source files. It works by concatenating a ZIP file onto the end of a specially prepared Tcl
interpreter. TclPro comes with pre-built interpreters that include Expect, [incr Tcl], and TclX, or you
can build your own interpreter that contains custom C extensions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter13. Reflection and Debugging
Other Tools
The Tcl community has built many interesting and useful tools to help your Tcl development. Only
two of them are mentioned below, but you can find many more at the Scriptics Tcl Resource Center:
http://www.scriptics.com/resource/
The tkinspect Program
The tkinspect program is a Tk application that lets you look at the state of other
Tk applications. It displays procedures, variables, and the Tk widget hierarchy.
With tkinspect you can issue commands to another application in order to change
variables or test out commands. This turns out to be a very useful way to debug
Tk applications. It was written by Sam Shen and is available on the CD-ROM.
The current FTP address for this is:
ftp.neosoft.com:/pub/tcl/sorted/devel/tkinspect-5.1.6.tar.gz
The Tuba Debugger
Tuba is a debugger written purely in Tcl. It sets breakpoints by rewriting Tcl procedures to contain
extra calls to the debugger. A small amount of support code is loaded into your application
automatically, and the debugger application can set breakpoints, watch variables, and trace execution.
It was written by John Stump and is available on the CD-ROM. The current URL for this package is:
http://www.geocities.com/SiliconValley/Ridge/2549/tuba/
The bgerror Command
When a Tcl script encounters an error during background processing, such as handling file events or
during the command associated with a button, it signals the error by calling the bgerror procedure. A
default implementation displays a dialog and gives you an opportunity to view the Tcl call stack at the
point of the error. You can supply your own version of bgerror. For example, when my exmh mail
application gets an error it offers to send mail to me with a few words of explanation from the user and
a copy of the stack trace. I get interesting bug reports from all over the world!
The bgerror command is called with one argument that is the error message. The global variable
errorInfo contains the stack trace information. There is an example tkerror implementation in the
on-line sources associated with this book.
The tkerror Command
The bgerror command used to be called tkerror. When event processing shifted from Tk into Tcl
with Tcl 7.5 and Tk 4.1, the name tkerror was changed to bgerror. Backwards compatibility is
provided so that if tkerror is defined, then tkerror is called instead of bgerror. I have run into
problems with the compatibility setup and have found it more reliable to update my applications to use
bgerror instead of tkerror. If you have an application that runs under either Tk 4.0 or Tk 4.1, you
can simply define both:
proc bgerror [info args tkerror] [info body tkerror]
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter13. Reflection and Debugging
Performance Tuning
The time command measures the execution time of a Tcl command. It takes an optional parameter
that is a repetition count:
time {set a "Hello, World!"}1000
=> 28 microseconds per iteration
If you need the result of the command being timed, use set to capture the result:
puts $log "command: [time {set result [command]}]"
Time stamps in a Log
Another way to gain insight into the performance of your script is to generate log records that contain
time stamps. The clock seconds value is too coarse, but you can couple it with the clock clicks
value to get higher resolution measurements. Use the code shown in Example 13-1 on page 175 to
calibrate the clicks per second on your system. Example 13-13 writes log records that contain the
current time and the number of clicks since the last record. There will be occasional glitches in the
clicks value when the system counter wraps around or is reset by the system clock, but it will normally
give pretty accurate results. The Log procedure adds overhead, too, so you should take several
measurements in a tight loop to see how long each Log call takes:
Example 13-13 Time Stamps in log records.
proc Log {args}{
global log
if [info exists log(file)] {
set now [clock clicks]
puts $log(file) [format "%s (%d)\t%s" \
[clock format [clock seconds]] \
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Using Namespaces
Namespaces add new syntax to procedure and variable names. A double colon, ::, separates the
namespace name from the variable or procedure name. You use this syntax to reference procedures
and variables in a different namespace. The namespace import command lets you name things in
other namespaces without the extra syntax. Namespaces can be nested, so you can create a hierarchy of
scopes. These concepts are explained in more detail in the rest of this chapter.
One feature not provided by namespaces is any sort of protection, or a way to enforce access controls
between different namespaces. This sort of thing is awkward, if not impossible, to provide in a
dynamic language like Tcl. For example, you are always free to use namespace eval to reach into any
other namespace. Instead of providing strict controls, namespaces are meant to provide structure that
enables large scale programming.
The package facility described in Chapter 12 was designed before namespaces. This chapter illustrates
a style that ties the two facilities together, but they are not strictly related. It is possible to create a
package named A that implements a namespace B, or to use a package without namespaces, or a
namespace without a package. However, it makes sense to use the facilities together.
Example 14-1 repeats the random number generator from Example 7-4 on page 85 using namespaces.
The standard naming style conventions for namespaces use lowercase:
Example 14-1 Random number generator using namespaces.
package provide random 1.0
namespace eval random {
# Create a variable inside the namespace
variable seed [clock seconds]
# Make the procedures visible to namespace import
namespace export init random range
# Create procedures inside the namespace
proc init { value } {
variable seed
set seed $value
}
proc random {} {
variable seed
set seed [expr ($seed*9301 + 49297) % 233280]
return [expr $seed/double(233280)]
}
proc range { range } {
expr int([random]*$range)
}
}
Example 14-1 defines three procedures and a variable inside the namespace random. From inside the
namespace, you can use these procedures and variables directly. From outside the namespace, you use
the :: syntax for namespace qualifiers. For example, the state variable is just seed within the
namespace, but you use random::seed to refer to the variable from outside the namespace. Using the
procedures looks like this:
random::random
=> 0.3993355624142661
random::range 10
=> 4
If you use a package a lot you can import its procedures. A namespace declares what procedures can
be imported with the namespace export command. Once you import a procedure, you can use it
without a qualified name:
namespace import random::random
random
=> 0.54342849794238679
Importing and exporting are described in more detail later.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Namespace Variables
The variable command defines a variable inside a namespace. It is like the set command because it
can define a value for the variable. You can declare several namespace variables with one variable
command. The general form is:
variable name ?value? ?name value? ...
If you have an array, do not assign a value in the variable command. Instead, use regular Tcl
commands after you declare the variable. You can put any commands inside a namespace block:
namespace eval foo {
variable arr
array set arr {name value name2 value2}
}
A namespace variable is similar to a global variable because it is outside the scope of any procedures.
Procedures use the variable command or qualified names to reference namespace variables. For
example, the random procedure has a variable command that brings the namespace variable into the
current scope:
variable seed
If a procedure has a variable command that names a new variable, it is created in the namespace
when it is first set.
Watch out for conflicts with global variables.
You need to be careful when you use variables inside a namespace block. If you declare them with a
variable command, they are clearly namespace variables. However, if you forget to declare them,
then they will either become namespace variables, or latch onto an existing global variable by the
same name. Consider the following code:
namespace eval foo {
variable table
for {set i 1} {$i <= 256} {incr i} {
set table($i) [format %c $i]
}
}
If there is already a global variable i, then the for loop will use that variable. Otherwise, it will create
the foo::i variable. I found this behavior surprising, but it does make it easier to access global
variables like env without first declaring them with global inside the namespace block.
Qualified Names
A fully qualified name begins with ::, which is the name for the global namespace. A fully qualified
name unambiguously names a procedure or a variable. The fully qualified name works anywhere. If
you use a fully qualified variable name, it is not necessary to use a global command. For example,
suppose namespace foo has a namespace variable x, and there is also a global variable x. The global
variable x can be named with this:
::x
The :: syntax does not affect variable substitutions. You can get the value of the global variable x
with $::x. Name the namespace variable x with this:
::foo::x
A partially qualified name does not have a leading ::. In this case the name is resolved from the
current namespace. For example, the following also names the namespace variable x:
foo::x
You can use qualified names with global. Once you do this, you can access the variable with its short
name:
global ::foo::x
set x 5
Declaring variables is more efficient than using qualified names.
The Tcl byte-code compiler generates faster code when you declare namespace and global variables.
Each procedure context has its own table of variables. The table can be accessed by a direct slot index,
or by a hash table lookup of the variable name. The hash table lookup is slower than the direct slot
access. When you use the variable or global command, then the compiler can use a direct slot
access. If you use qualified names, the compiler uses the more general hash table lookup.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Command Lookup
A command is looked up first in the current name space. If it is not found there, then it is looked up in
the global namespace. This means that you can use all the built-in Tcl commands inside a namespace
with no special effort.
You can play games by redefining commands within a namespace. For example, a namespace could
define a procedure named set. To get the built-in set you could use ::set, while set referred to the
set defined inside namespace. Obviously you need to be quite careful when you do this.
You can use qualified names when defining procedures. This eliminates the need to put the proc
commands inside a namespace block. However, you still need to use namespace eval to create the
namespace before you can create procedures inside it. Example 14-2 repeats the random number
generator using qualified names. random::init does not need a variable command because it uses a
qualified name for seed:
Example 14-2 Random number generator using qualified names.
namespace eval random {
# Create a variable inside the namespace
variable seed [clock seconds]
}
# Create procedures inside the namespace
proc random::init { seed } {
set ::random::seed $seed
}
proc random::random {} {
variable seed
set seed [expr ($seed*9301 + 49297) % 233280]
return [expr $seed/double(233280)]
}
proc random::range { range } {
expr int([random]*$range)
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Nested Namespaces
Namespaces can be nested inside other namespaces. Example 14-3 shows three namespaces that have
their own specific variable x. The fully qualified names for these variables are ::foo::x, ::bar::x,
and ::bar::foo::x.
Example 14-3 Nested namespaces.
namespace eval foo {
variable x 1 ;# ::foo::x
}
namespace eval bar {
variable x 2 ;# ::bar::x
namespace foo {
variable x 3 ;# ::bar::foo::x
}
puts $foo::x ;# prints 3
}
puts $foo::x ;# prints 1
Partially qualified names can refer to two different objects.
In Example 14-3 the partially qualified name foo::x can reference one of two variables depending on
the current namespace. From the global scope the name foo::x refers to the namespace variable x
inside ::foo. From the ::bar namespace, foo::x refers to the variable x inside ::bar::foo.
If you want to unambiguously name a variable in the current namespace, you have two choices. The
simplest is to bring the variable into scope with the variable command:
variable x
set x something
If you need to give out the name of the variable, then you have two choices. The most general solution
is to use the namespace current command to create a fully qualified name:
trace variable [namespace current]::x r \
[namespace current]::traceproc
However, it is simpler to just explicitly write out the namespace as in:
trace variable ::myname::x r ::myname::traceproc
The drawback of this approach is that it litters your code with references to ::myname::, which might
be subject to change during program development.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Importing and Exporting Procedures
Commands can be imported from namespaces to make it easier to name them. An imported command
can be used without its namespace qualifier. Each namespace specifies exported procedures that can
be the target of an import. Variables cannot be imported. Note that importing is only a convenience;
you can always use qualified names to access any procedure. As a matter of style, I avoid importing
names, so I know what package a command belongs to when I'm reading code.
The namespace export command goes inside the namespace block, and it specifies what procedures a
namespace exports. The specification is a list of string match patterns that are compared against the
set of commands defined in a namespace. The export list can be defined before the procedures being
exported. You can do more than one namespace export to add more procedures, or patterns, to the
export list for a namespace. Use the -clear flag if you need to reset the export list.
namespace export ?-clear? ?pat? ?pat? ...
Only exported names appear in package indexes.
When you create the pkgIndex.tcl package index file with pkg_mkIndex, which is described Chapter
12, you should be aware that only exported names appear in the index. Because of this, I often resort to
exporting everything. I never plan to import the names, but I do rely on automatic code loading based
on the index files. This exports everything:
namespace export *
The namespace import command makes commands in another namespace visible in the current
namespace. An import can cause conflicts with commands in the current namespace. The namespace
import command raises an error if there is a conflict. You can override this with the -force option.
The general form of the command is:
namespace import ?-force? namespace::pat ?namespace::pat?...
The pat is a string match type pattern that is matched against exported commands defined in
namespace. You cannot use patterns to match namespace. The namespace can be a fully or partially
qualified name of a namespace.
If you are lazy, you can import all procedures from a namespace:
namespace import random::*
The drawback of this approach is that random exports an init procedure, which might conflict with
another module you import in the same way. It is safer to import just the procedures you plan on
using:
namespace import random::random random::range
A namespace import takes a snapshot.
If the set of procedures in a namespace changes, or if its export list changes, then this has no effect on
any imports that have already occurred from that namespace.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Callbacks and Namespaces
Commands like after, bind, and button take arguments that are Tcl scripts that are evaluated later.
These callback commands execute later in the global scope by default. If you want a callback to be
evaluated in a particular namespace, you can construct the callback with namespace code. This
command does not execute the callback. Instead, it generates a Tcl command that will execute in the
current namespace scope when it is evaluated later. For example, suppose ::current is the current
namespace. The namespace code command determines the current scope and adds that to the
namespace inscope command it generates:
set callback [namespace code {set x 1}]
=> namespace inscope ::current {set x 1}
# sometime later ...
eval $callback
When you evaluate $callback later, it executes in the ::current namespace because of the
namespace inscope command. In particular, if there is a namespace variable ::current::x, then that
variable is modified. An alternative to using namespace code is to name the variable with a qualified
name:
set callback {set ::current::x 1}
The drawback of this approach is that it makes it tedious to move the code to a different namespace.
If you need substitutions to occur on the command when you define it, use list to construct it. Using
list is discussed in more detail on pages 123 and 389. Example 14-4 wraps up the list and the
namespace inscope into the code procedure, which is handy because you almost always want to use
list when constructing callbacks. The uplevel in code ensures that the correct namespace is
captured; you can use code anywhere:
Example 14-4 The code procedure to wrap callbacks.
proc code {args} {
set namespace [uplevel {namespace current}]
return [list namespace inscope $namespace $args]
}
namespace eval foo {
variable y "y value" x {}
set callback [code set x $y]
=> namespace inscope ::foo {set x {y value}}
}
The example defines a callback that will set ::foo::x to y value. If you want to set x to the value
that y has at the time of the callback, then you do not want to do any substitutions. In that case, the
original namespace code is what you want:
set callback [namespace code {set x $y}]
=> namespace inscope ::foo {set x $y}
If the callback has additional arguments added by the caller, namespace inscope correctly adds them.
For example, the scrollbar protocol described on page 431 adds parameters to the callback that
controls a scrollbar.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Introspection
The info commands operation returns all the commands that are currently visible. It is described in
more detail on page 179. You can limit the information returned with a string match pattern. You
can also include a namespace specifier in the pattern to see what is visible in a namespace. Remember
that global commands and imported commands are visible, so info commands returns more than just
what is defined by the namespace. Example 14-5 uses namespace origin, which returns the original
name of imported commands, to sort out the commands that are really defined in a namespace:
Example 14-5 Listing commands defined by a namespace.
proc Namespace_List {{namespace {}}} {
if {[string length $namespace] == 0} {
# Determine the namespace of our caller
set namespace [uplevel {namespace current}]
}
set result {}
foreach cmd [info commands ${namespace}::*] {
if {[namespace origin $cmd] == $cmd} {
lappend result $cmd
}
}
return [lsort $result]
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
The namespace Command
Table 14-1 summarizes the namespace operations:
Table 14-1. The namespace command.
namespace current
Returns the current namespace.
namespace children ?
name? ?pat?
Returns names of nested namespaces. name defaults to current
namespace. pat is a string match pattern that limits what is
returned.
namespace code script Generates a namespace inscope command that will eval script in
the current namespace.
namespace delete name ?
name? ...
Deletes the variables and commands from the specified namespaces.
namespace eval name cmd ?
args? ...
Concatenates args, if present, onto cmd and evaluates it in name
namespace.
namespace export ?-
clear? ?pat? ?pat? ...
Adds patterns to the export list for current namespace. Returns export
list if no patterns.
namespace forget pat ?
pat? ...
Undoes the import of names matching patterns.
namespace import ?-
force? pat ?pat? ...
Adds the names matching the patterns to the current namespace.
namespace inscope name
cmd ?args? ...
Appends args, if present, onto cmd as list elements and evaluates it
in name namespace.
namespace origin cmd Returns the original name of cmd.
namespace parent ?name? Returns the parent namespace of name, or of the current namespace.
namespace qualifiers
name
Returns the part of name up to the last :: in it.
namespace which ?flag?
name
Returns the fully qualified version of name. The flag is one of -
command, -variable, or -namespace.
namespace tail name Returns the last component of name.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Converting Existing Packages to use Namespaces
Suppose you have an existing set of Tcl procedures that you want to wrap in a namespace. Obviously,
you start by surrounding your existing code in a namespace eval block. However, you need to
consider three things: global variables, exported procedures, and callbacks.
Global variables remain global until you change your code to use variable instead of global.
Some variables may make sense to leave at the global scope. Remember that the variables that
Tcl defines are global, including env, tcl_platform, and the others listed in Table 2-2 on page
30. If you use the upvar #0 trick described on page 86, you can adapt this to namespaces by
doing this instead:
upvar #0 [namespace current]::$instance state
Exporting procedures makes it more convenient for users of your package. It is not strictly
necessary because they can always use qualified names to reference your procedures. An export
list is a good hint about which procedures are expected to be used by other packages. Remember
that the export list determines what procedures are visible in the index created by pkg_mkIndex.
Callbacks execute at the global scope. If you use variable traces and variables associated with Tk
widgets, these are also treated as global variables. If you want a callback to invoke a namespace
procedure, or if you give out the name of a namespace variable, then you must construct fully
qualified variable and procedure names. You can hardwire the current namespace:
button .foo -command ::myname::callback \
-textvariable ::myname::textvar
or you can use namespace current:
button .foo -command [namespace current]::callback \
-textvariable [namespace current]::textvar
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
[incr Tcl] Object System
The Tcl namespace facility does not provide classes and inheritance. It just provides new scopes and a
way to hide procedures and variables inside a scope. There are Tcl C APIs that support hooks in
variable name and command lookup for object systems so that they can implement classes and
inheritance. By exploiting these interfaces, various object systems can be added to Tcl as shared
libraries.
The Tcl namespace facility was proposed by Michael McLennan based on his experiences with [incr
Tcl], which is the most widely used object-oriented extension for Tcl. [incr Tcl] provides classes,
inheritance, and protected variables and commands. If you are familiar with C++, [incr Tcl] should
feel similar. A complete treatment of [incr Tcl] is not made in this book. Tcl/Tk Tools (Mark Harrison,
O'Reilly & Associates, Inc., 1997) is an excellent source of information. You can find a version of
[incr Tcl] on the CD-ROM. The [incr Tcl] home page is:
http://www.tcltk.com/itcl/
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter14. Namespaces
Notes
The final section of this chapter touches on a variety of features of the namespace facility.
Names for Widgets, Images, and Interpreters
There are a number of Tcl extensions that are not affected by the namespaces described in this chapter,
which apply only to commands and variable names. For example, when you create a Tk widget, a Tcl
command is also created that corresponds to the Tk widget. This command is always created in the
global command namespace even when you create the Tk widget from inside a namespace eval
block. Other examples include Tcl interpreters, which are described in Chapter 19, and Tk images,
which are described in Chapter 38.
The variable command at the global scope
It turns out that you can use variable like the global command if your procedures are not inside a
namespace. This is consistent because it means "this variable belongs to the current namespace,"
which might be the global namespace.
Auto Loading and auto_import
The following sequence of commands can be used to import commands from the foo package:
package require foo
namespace import foo::*
However, because of the default behavior of packages, there may not be anything that matches foo::*
after the package require. Instead, there are entries in the auto_index array that will be used to load
those procedures when you first use them. The auto loading mechanism is described in Chapter 12. To
account for this, Tcl calls out to a hook procedure called auto_import. This default implementation of
this procedure searches auto_index and forcibly loads any pending procedures that match the import
pattern. Packages like [incr Tcl] exploit this hook to implement more elaborate schemes. The
auto_import hook was first introduced in Tcl 8.0.3.
Namespaces and uplevel
Namespaces affect the Tcl call frames just like procedures do. If you walk the call stack with info
level, the namespace frames are visible. This means that you can get access to all variables with
uplevel and upvar. Level #0 is still the absolute global scope, outside any namespace or procedure.
Try out Call_Trace from Example 13-5 on page 180 on your code that uses namespaces to see the
effect.
Naming Quirks
When you name a namespace, you are allowed to have extra colons at the end. You can also have two
or more colons as the separator between namespace name components. These rules make it easier to
assemble names by adding to the value returned from namespace current. These all name the same
namespace:
::foo::bar
::foo::bar::
::foo:::::::bar
The name of the global namespace can be either :: or the empty string. This follows from the
treatment of :: in namespace names.
When you name a variable or command, a trailing :: is significant. In the following command a
variable inside the ::foo::bar namespace is modified. The variable has an empty string for its name!
set ::foo::bar:: 3
namespace eval ::foo::bar { set {} }
=> 3
If you want to embed a reference to a variable just before two colons, use a backslash to turn off the
variable name parsing before the colons:
set x xval
set y $x\::foo
=> xval::foo
Miscellaneous
You can remove names you have imported:
namespace forget random::init
You can rename imported procedures to modify their names:
rename range Range
You can even move a procedure into another namespace with rename:
rename random::init myspace::init
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartII. Advanced Tcl
Chapter 15. Internationalization
This chapter describes features that support text processing for different character sets such as ASCII
and Japanese. Tcl can read and write data in various character set encodings, but it processes data in a
standard character set called Unicode. Tcl has a message catalog that lets you generate different
versions of an application for different languages. Tcl commands described are: encoding and msgcat.
Different languages use different alphabets, or character sets. An encoding is a standard way to
represent a character set. Tcl hides most of the issues associated with encodings and character sets, but
you need to be aware of them when you write applications that are used in different countries. You can
also write an application using a message catalog so that the strings you display to users can be in the
language of their choice. Using a message catalog is more work, but Tcl makes it as easy as possible.
Most of the hard work in dealing with character set encodings is done "under the covers" by the Tcl C
library. The Tcl C library underwent substantial changes to support international character sets. Instead
of using 8-bit bytes to store characters, Tcl uses a 16-bit character set called Unicode, which is large
enough to encode the alphabets of all languages. There is also plenty of room left over to represent
special characters like and .
In spite of all the changes to support Unicode, there are few changes visible to the Tcl script writer.
Scripts written for Tcl 8.0 and earlier continue to work fine with Tcl 8.1 and later versions. You only
need to modify scripts if you want to take advantage of the features added to support
internationalization.
This chapter begins with a discussion of what a character set is and why different codings are used to
represent them. It concludes with a discussion of message catalogs.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter15. Internationalization
Character Sets and Encodings
If you are from the United States, you've probably never thought twice about character sets. Most
computers use the ASCII encoding, which has 127 characters. That is enough for the 26 letters in the
English alphabet, upper case and lower case, plus numbers, various punctuation characters, and control
characters like tab and newline. ASCII fits easily in 8-bit characters, which can represent 256 different
values.
European alphabets include accented characters like , , and . The ISO Latin-1 encoding is a
superset of ASCII that encodes 256 characters. It shares the ASCII encoding in values 0 through 127
and uses the "high half" of the encoding space to represent accented characters as well as special
characters like . There are several ISO Latin encodings to handle different alphabets, and these share
the trick of encoding ASCII in the lower half and other characters in the high half. You might see these
encodings referred to as iso8859-1, iso8859-2, and so on.
Asian character sets are simply too large to fit into 8-bit encodings. There are a number of 16-bit
encodings for these languages. If you work with these, you are probably familiar with the "Big 5" or
ShiftJIS encodings.
Unicode is an international standard character set encoding. There are both 16-bit Unicode and 32-bit
Unicode standards, but Tcl and just about everyone else just use the 16-bit standard. Unicode has the
important property that it can encode all the important character sets without conflicts and overlap. By
converting all characters to the Unicode encoding, Tcl can work with different character sets
simultaneously.
The System Encoding
Computer systems are set up with a standard system encoding for their files. If you always work with
this encoding, then you can ignore character set issues. Tcl will read files and automatically convert
them from the system encoding to Unicode. When Tcl writes files, it automatically converts from
Unicode to the system encoding. If you are curious, you can find out the system encoding with:
encoding system
=> cp1252
The "cp" is short for "code page," the term that Windows uses to refer to different encodings. On my
Unix system, the system encoding is iso8859-1.
Do not change the system encoding.
You could also change the system encoding with:
encoding system encoding
But this is not a good idea. It immediately changes how Tcl passes strings to your operating system,
and it is likely to leave Tcl in an unusable state. Tcl automatically determines the system encoding for
you. Don't bother trying to set it yourself.
The encoding names command lists all the encodings that Tcl knows about. The encodings are kept
in files stored in the encoding directory under the Tcl script library. They are loaded automatically the
first time you use an encoding.
lsort [encoding names]
=> ascii big5 cp1250 cp1251 cp1252 cp1253 cp1254 cp1255
cp1256 cp1257 cp1258 cp437 cp737 cp775 cp850 cp852 cp855
cp857 cp860 cp861 cp862 cp863 cp864 cp865 cp866 cp869
cp874 cp932 cp936 cp949 cp950 dingbats euc-cn euc-jp euc-kr
gb12345 gb1988 gb2312 identity iso2022 iso2022-jp
iso2022-kr iso8859-1 iso8859-2 iso8859-3 iso8859-4
iso8859-5 iso8859-6 iso8859-7 iso8859-8 iso8859-9
jis0201 jis0208 jis0212 ksc5601 macCentEuro macCroatian
macCyrillic macDingbats macGreek macIceland macJapan
macRoman macRomania macThai macTurkish macUkraine
shiftjis symbol unicode utf-8
The encoding names reflect their origin. The "cp" refers to the "code pages" that Windows uses to
manage encodings. The "mac" encodings come from the Macintosh. The "iso," "euc," "gb," and "jis"
encodings come from various standards bodies.
File Encodings and fconfigure
The conversion to Unicode happens automatically in the Tcl C library. When Tcl reads and writes
files, it translates from the current system encoding into Unicode. If you have files in different
encodings, you can use the fconfigure command to set the encoding. For example, to read a file in
the standard Russian encoding (iso8859-7):
set in [open README.russian]
fconfigure $in -encoding iso8859-7
Example 15-1 shows a simple utility I use in exmh,
[*]
a MIME-aware mail reader. MIME has its own
convention for specifying the character set encoding of a mail message that differs slightly from Tcl's
naming convention. The procedure launders the name and then sets the encoding. Exmh was already
aware of MIME character sets, so it could choose fonts for message display. Adding this procedure
and adding two calls to it was all I had to do adapt exmh to Unicode.
[*]
The exmh home page is http://www.beedub.com/exmh/. It is a wonderful tool that helps me manage tons of e-mail. It is written in Tcl/Tk,
of course, and relies on the MH mail system, which limits it to UNIX.
Example 15-1 MIME character sets.and file encodings.
proc Mime_SetEncoding {file charset} {
regsub -all {(iso|jis|us)-} $charset {\1}charset
set charset [string tolower charset]
regsub usascii $charset ascii charset
fconfigure $file -encoding $charset
}
Scripts in Different Encodings
If you have scripts that are not in the system encoding, then you cannot use source to load them.
However, it is easy to read the files yourself under the proper encoding and use eval to process them.
Example 15-2 adds a -encoding flag to the source command. This is likely to become a built-in
feature in future versions of Tcl so that commands like info script will work properly:
Example 15-2 Using scripts in nonstandard encodings.
proc Source {args} {
set file [llength $args end]
if {[llength $args] == 3 &&
[string equal -encoding [lindex $args 0]]} {
set encoding [lindex $args 1]
set in [open $file]
fconfigure $in -encoding $encoding
set script [read $in]
close $in
return [uplevel 1 $script]
} elseif {[llength $args] == 1} {
return [uplevel 1 [list source $file]]
} else {
return -code error \
"Usage: Source ?-encoding encoding? file?"
}
}
Unicode and UTF-8
UTF-8 is an encoding for Unicode. While Unicode represents all characters with 16 bits, the UTF-8
encoding uses either 8, 16, or 24 bits to represent one Unicode character. This variable-width encoding
is useful because it uses 8 bits to represent ASCII characters. This means that a pure ASCII string, one
with character codes all fewer than 128, is also a UTF-8 string. Tcl uses UTF-8 internally to make the
transition to Unicode easier. It allows interoperability with Tcl extensions that have not been made
Unicode-aware. They can continue to pass ASCII strings to Tcl, and Tcl will interpret them correctly.
As a Tcl script writer, you can mostly ignore UTF-8 and just think of Tcl as being built on Unicode
(i.e., full 16-bit character set support). If you write Tcl extensions in C or C++, however, the impact of
UTF-8 and Unicode is quite visible. This is explained in more detail in Chapter 44.
Tcl lets you read and write files in UTF-8 encoding or directly in Unicode. This is useful if you need to
use the same file on systems that have different system encodings. These files might be scripts,
message catalogs, or documentation. Instead of using a particular native format, you can use Unicode
or UTF-8 and read the files the same way on any of your systems. Of course, you will have to set the
encoding properly by using fconfigure as shown earlier.
The Binary Encoding
If you want to read a data file and suppress all character set transformations, use the binary encoding:
fconfigure $in -encoding binary
Under the binary encoding, Tcl reads in each 8-bit byte and stores it into the lower half of a 16-bit
Unicode character with the high half set to zero. During binary output, Tcl writes out the lower byte of
each Unicode character. You can see that reading in binary and then writing it out doesn't change any
bits. Watch out if you read something in one encoding and then write it out in binary. Any information
in the high byte of the Unicode character gets lost!
Tcl actually handles the binary encoding more efficiently than just described, but logically the
previous description is still accurate. As described in Chapter 44, Tcl can manage data in several
forms, not just strings. When you read a file in binary format, Tcl stores the data as a ByteArray that
is simply 8 bits of data in each byte. However, if you ask for this data as a string (e.g., with the puts
command), Tcl automatically converts from 8-bit bytes to 16-bit Unicode characters by setting the
high byte to all zeros.
The binary command also manipulates data in ByteArray format. If you read a file with the binary
encoding and then use the binary command to process the data, Tcl will keep the data in an efficient
form.
The string command also understands the ByteArray format, so you can do operations like string
length, string range, and string index on binary data without suffering the conversion cost from
a ByteArray to a UTF-8 string.
Conversions Between Encodings
The encoding command lets you convert strings between encodings. The encoding convertfrom
command converts data in some other encoding into a Unicode string. The encoding convertto
command converts a Unicode string into some other encoding. For example, the following two
sequences of commands are equivalent. They both read data from a file that is in Big5 encoding and
convert it to Unicode:
fconfigure $input -encoding gb12345
set unicode [read $input]
or
fconfigure $input -encoding binary
set unicode [encoding convertfrom gb12345 [read $input]]
In general, you can lose information when you go from Unicode to any other encoding, so you ought to
be aware of the limitations of the encodings you are using. In particular, the binary encoding may not
preserve your data if it starts out from an arbitrary Unicode string. Similarly, an encoding like iso-
8859-2 may simply not have a representation of a given Unicode character.
The encoding Command
Table 15-1 summarizes the encoding command:
Table 15-1. The encoding command.
encoding convertfrom ?
encoding? data
Converts binary data from the specified encoding, which defaults
to the system encoding, into Unicode.
encoding convertto ?
encoding? string
Converts string from Unicode into data in the encoding format,
which defaults to the system encoding.
encoding names
Returns the names of known encodings.
encoding system ?encoding? Queries or change the system encoding.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter15. Internationalization
Message Catalogs
A message catalog is a list of messages that your application will display. The main idea is that you
can maintain several catalogs, one for each language you support. Unfortunately, you have to be
explicit about using message catalogs. Everywhere you generate output or display strings in Tk
widgets, you need to change your code to go through a message catalog. Fortunately, Tcl uses a nice
trick to make this fairly easy and to keep your code readable. Instead of using keys like "message42" to
get messages out of the catalog, Tcl just uses the strings you would use by default. For example,
instead of this code:
puts "Hello, World!"
A version that uses message catalogs looks like this:
puts [msgcat::mc "Hello, World!"]
If you have not already loaded your message catalog, or if your catalog doesn't contain a mapping for
"Hello, World!", then msgcat::mc just returns its argument. Actually, you can define just what
happens in the case of unknown inputs by defining your own msgcat::mcunknown procedure, but the
default behavior is quite good.
The message catalog is implemented in Tcl in the msgcat package. You need to use package require
to make it available to your scripts:
package require msgcat
In addition, all the procedures in the package begin with "mc," so you can use namespace import to
shorten their names further. I am not a big fan of namespace import, but if you use message catalogs,
you will be calling the msgcat::mc function a lot, so it may be worthwhile to import it:
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter16. Event-Driven Programming
The Tcl Event Loop
An event loop is built into Tcl. Tcl checks for events and calls out to handlers that have been
registered for different types of events. Some of the events are processed internally to Tcl. You can
register Tcl commands to be called in response to events. There are also C APIs to event loop, which
are described on page 691. Event processing is active all the time in Tk applications. If you do not use
Tk, you can start the event loop with the vwait command as shown in Example 16-2 on page 220. The
four event classes are handled in the following order:
Window events. These include keystrokes and button clicks. Handlers are set up for these
automatically by the Tk widgets, and you can register window event handlers with the bind
command described in Chapter 26.
File events. The fileevent command registers handlers for these events.
Timer events. The after command registers commands to occur at specific times.
Idle events. These events are processed when there is nothing else to do. The Tk widgets use idle
events to display themselves. The after idle command registers a command to run at the next
idle time.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter16. Event-Driven Programming
The after Command
The after command sets up commands to happen in the future. In its simplest form, it pauses the
application for a specified time, in milliseconds. The example below waits for half a second:
after 500
During this time, the application does not process events. You can use the vwait command as shown
on page 220 to keep the Tcl event loop active during the waiting period. The after command can
register a Tcl command to occur after a period of time, in milliseconds:
after milliseconds cmd arg arg...
The after command treats its arguments like eval; if you give it extra arguments, it concatenates
them to form a single command. If your argument structure is important, use list to build the
command. The following example always works, no matter what the value of myvariable is:
after 500 [list puts $myvariable]
The return value of after is an identifier for the registered command. You can cancel this command
with the after cancel operation. You specify either the identifier returned from after, or the
command string. In the latter case, the event that matches the command string exactly is canceled.
Table 16-1 summarizes the after command:
Table 16-1. The after command.
line of input is read and processed. The vwait command is described on the next page. Example 22-1
on page 318 also uses fileevent to read from a pipeline.
Example 16-1 A read event file handler.
proc Reader { pipe } {
global done
if {[eof $pipe]} {
catch {close $pipe}
set done 1
return
}
gets $pipe line
# Process the line here...
}
set pipe [open "|some command"]
fileevent $pipe readable [list Reader $pipe]
vwait done
There can be at most one read handler and one write handler for an I/O channel. If you register a
handler and one is already registered, then the old registration is removed. If you call fileevent
without a command argument, it returns the currently registered command, or it returns the empty
string if there is none. If you register the empty string, it deletes the current file handler. Table 16-2
summarizes the fileevent command.
Table 16-2. The fileevent command.
fileevent fileId readable ?
command?
Queries or registers command to be called when fileId is
readable.
fileevent fileId writable ?
command?
Queries or registers command to be called when fileId is
writable.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter16. Event-Driven Programming
The vwait Command
The vwait command waits until a variable is modified. For example, you can set variable x at a future
time, and then wait for that variable to be set with vwait.
set x 0
after 500 {set x 1}
vwait x
Waiting with vwait causes Tcl to enter the event loop. Tcl will process events until the variable x is
modified. The vwait command completes when some Tcl code runs in response to an event and
modifies the variable. In this case the event is a timer event, and the Tcl code is simply:
set x 1
In some cases vwait is used only to start the event loop. Example 16-2 sets up a file event handler for
stdin that will read and execute commands. Once this is set up, vwait is used to enter the event loop
and process commands until the input channel is closed. The process exits at that point, so the vwait
variable Stdin(wait) is not used:
Example 16-2 Using vwait to activate the event loop.
proc Stdin_Start {prompt} {
global Stdin
set Stdin(line) ""
puts -nonewline $prompt
flush stdout
fileevent stdin readable [list StdinRead $prompt]
vwait Stdin(wait)
}
proc StdinRead {prompt} {
global Stdin
if {[eof stdin]} {
exit
}
append Stdin(line) [gets stdin]
if {[info complete $Stdin(line)]} {
catch {uplevel #0 $Stdin(line)}result
puts $result
puts -nonewline $prompt
flush stdout
set Stdin(line) {}
} else {
append Stdin(line) \n
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter16. Event-Driven Programming
The fconfigure Command
The fconfigure command sets and queries several properties of I/O channels. The default settings for
channels are suitable for most cases. If you do event-driven I/O you may want to set your channel into
nonblocking mode. If you handle binary data, you should turn off end of line and character set
translations. You can query the channel parameters like this:
fconfigure stdin
-blocking 1 -buffering none -buffersize 4096 -encoding
iso8859-1 -eofchar {}-translation lf
Table 16-3 summarizes the properties controlled by fconfigure. They are discussed in more detail
below.
Table 16-3. I/O channel properties controlled by fconfigure.
-blocking
Blocks until I/O channel is ready: 0 or 1.
-buffering
Buffer mode: none, line, or full.
-buffersize
Number of characters in the buffer.
-eofchar
Special end of file character. Control-z (\x1a) for DOS. Null otherwise.
-encoding
The character set encoding.
-error
Returns the last POSIX error message associated with a channel.
-translation
End of line translation: auto, lf, cr, crlf, binary.
-mode
Serial devices only. Format: baud,parity,data,stop
-peername
Sockets only. IP address of remote host.
-peerport
Sockets only. Port number of remote host.
Nonblocking I/O
By default, I/O channels are blocking. A gets or read will wait until data is available before returning.
A puts may also wait if the I/O channel is not ready to accept data. This behavior is all right if you are
using disk files, which are essentially always ready. If you use pipelines or network sockets, however,
the blocking behavior can hang up your application.
The fconfigure command can set a channel into nonblocking mode. A gets or read command may
return immediately with no data. This occurs when there is no data available on a socket or pipeline. A
puts to a nonblocking channel will accept all the data and buffer it internally. When the underlying
device (i.e., a pipeline or socket) is ready, then Tcl automatically writes out the buffered data.
Nonblocking channels are useful because your application can do something else while waiting for the
I/O channel. You can also manage several nonblocking I/O channels at once. Nonblocking channels
should be used with the fileevent command described earlier. The following command puts a channel
into nonblocking mode:
fconfigure fileID -blocking 0
It is not strictly necessary to put a channel into nonblocking mode if you use fileevent. However, if
the channel is in blocking mode, then it is still possible for the gets or read done by your fileevent
procedure to block. For example, an I/O channel might have some data ready, but not a complete line.
In this case, a gets would block, unless the channel is nonblocking. Perhaps the best motivation for a
nonblocking channel is the buffering behavior of a nonblocking puts. You can even close a channel
that has buffered data, and Tcl will automatically write out the buffers as the channel becomes ready.
For these reasons, it is common to use a nonblocking channel with fileevent. Example 16-3 shows a
fileevent handler for a nonblocking channel. As described above, the gets may not find a complete
line, in which case it doesn't read anything and returns -1.
Example 16-3 A read event file handler for a nonblocking channel.
set pipe [open "|some command"]
fileevent $pipe readable [list Reader $pipe]
fconfigure $pipe -blocking 0
proc Reader { pipe } {
global done
if {[eof $pipe]} {
catch {close $pipe}
set done 1
return
}
if {[gets $pipe line] < 0} {
# We blocked anyway because only part of a line
# was available for input
} else {
# Process one line
}
}
vwait done
The fblocked Command
The fblocked command returns 1 if a channel does not have data ready. Normally the fileevent
command takes care of waiting for data, so I have seen fblocked useful only in testing channel
implementations.
Buffering
By default, Tcl buffers data, so I/O is more efficient. The underlying device is accessed less frequently,
so there is less overhead. In some cases you may want data to be visible immediately and buffering
gets in the way. The following turns off all buffering:
fconfigure fileID -buffering none
Full buffering means that output data is accumulated until a buffer fills; then a write is performed. For
reading, Tcl attempts to read a whole buffer each time more data is needed. The read-ahead for
buffering will not block. The -buffersize parameter controls the buffer size:
fconfigure fileID -buffering full -buffersize 8192
Line buffering is used by default on stdin and stdout. Each newline in an output channel causes a
write operation. Read buffering is the same as full buffering. The following command turns on line
buffering:
fconfigure fileID -buffering line
End of Line Translations
On UNIX, text lines end with a newline character (\n). On Macintosh they end with a carriage return
(\r). On Windows they end with a carriage return, newline sequence (\r\n). Network sockets also use
the carriage return, newline sequence. By default, Tcl accepts any of these, and the line terminator can
even change within a channel. All of these different conventions are converted to the UNIX style so
that once read, text lines always end with a newline character (\n). Both the read and gets commands
do this conversion. By default, text lines are generated in the platform-native format during output.
The default behavior is almost always what you want, but you can control the translation with
fconfigure. Table 16-4 shows settings for -translation:
Table 16-4. End of line translation modes.
binary
No translation at all.
lf
UNIX-style, which also means no translations.
cr
Macintosh style. On input, carriage returns are converted to newlines. On output, newlines
are converted to carriage returns.
crlf
Windows and Network style. On input, carriage return, newline is converted to a newline.
On output, a newline is converted to a carriage return, newline.
auto
The default behavior. On input, all end of line conventions are converted to a newline.
Output is in native format.
End of File Character
In DOS file systems, there may be a Control-z character (\x1a) at the end of a text file. By default, this
character is ignored on the Windows platform if it occurs at the end of the file, and this character is
output when you close the file. You can turn this off by specifying an empty string for the end of file
character:
fconfigure fileID -eofchar {}
Serial Devices
The -mode attribute specifies the baud rate, parity mode, the number of data bits, and the number of
stop bits:
set tty [open /dev/ttya]
fconfigure $tty -mode
=> 9600,0,8,2
If you need to set additional attributes of the serial channel, you will have to write a command in C
that makes the system calls you need. If you are familiar with serial devices, you know there are quite
a few possibilities!
Windows has some special device names that always connect you to the serial line devices when you
use open. They are com1 through com8. The system console is named con. The null device is nul.
UNIX has names for serial devices in /dev. The serial devices are /dev/ttya, /dev/ttyb, and so on.
The system console is /dev/console. The current terminal is /dev/tty. The null device is
/dev/null.
Macintosh needs a special command to open serial devices. This is provided by a third-party extension
that you can find at the Tcl Resource Center under:
http://www.scriptics.com/resource/software/extensions/macintosh
Character Set Encodings
Tcl automatically converts various character set encodings into Unicode internally. It cannot
automatically detect the encoding for a file or network socket, however, so you need to use
fconfigure -encoding if you are reading data that is not in the system's default encoding. Character
set issues are explained in more detail in Chapter 15.
Configuring Read-Write Channels
If you have a channel that is used for both input and output, you can set the channel parameters
independently for input and output. In this case, you can specify a two-element list for the parameter
value. The first element is for the input side of the channel, and the second element is for the output
side of the channel. If you specify only a single element, it applies to both input and output. For
example, the following command forces output end of line translations to be crlf mode, leaves the
input channel on automatic, and sets the buffer size for both input and output:
fconfigure pipe -translation {auto crlf}-buffersize 4096
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartII. Advanced Tcl
Chapter 17. Socket Programming
This chapter shows how to use sockets for programming network clients and servers. Advanced I/O
techniques for sockets are described, including nonblocking I/O and control over I/O buffering. Tcl
commands discussed are: socket, fconfigure, and http::geturl.
Sockets are network communication channels. The sockets described in this chapter use the TCP
network protocol, although you can find Tcl extensions that create sockets using other protocols. TCP
provides a reliable byte stream between two hosts connected to a network. TCP handles all the issues
about routing information across the network, and it automatically recovers if data is lost or corrupted
along the way. TCP is the basis for other protocols like Telnet, FTP, and HTTP.
A Tcl script can use a network socket just like an open file or pipeline. Instead of using the Tcl open
command, you use the socket command to open a socket. Then you use gets, puts, and read to
transfer data. The close command closes a network socket.
Network programming distinguishes between clients and servers. A server is a process or program that
runs for long periods of time and controls access to some resource. For example, an FTP server
governs access to files, and an HTTP server provides access to hypertext pages on the World Wide
Web. A client typically connects to the server for a limited time in order to gain access to the resource.
For example, when a Web browser fetches a hypertext page, it is acting as a client. The extended
examples in this chapter show how to program the client side of the HTTP protocol.
The Scotty extension supports many network protocols.
The Scotty Tcl extension provides access to other network protocols like UDP, DNS, and RPC. It also
supports the SNMP network management protocol and the MIB database associated with SNMP.
Scotty is a great extension package that is widely used for network management applications. Its home
page is:
http://wwwsnmp.cs.utwente.nl/~schoenw/scotty/
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter17. Socket Programming
Client Sockets
A client opens a socket by specifying the host address and port number for the server of the socket.
The host address gives the network location (i.e., which computer), and the port selects a particular
server from all the possible servers that may be running on that host. For example, HTTP servers
typically use port 80, while FTP servers use port 20. The following example shows how to open a
client socket to a Web server:
set s [socket www.scriptics.com 80]
There are two forms for host names. The previous example uses a domain name: www.scriptics.com.
You can also specify raw IP addresses, which are specified with four dot-separated integers (e.g.,
128.15.115.32). A domain name is mapped into a raw IP address by the system software, and it is
almost always a better idea to use a domain name in case the IP address assignment for the host
changes. This can happen when hosts are upgraded or they move to a different part of the network. As
of Tcl 8.2, there is no direct access from Tcl to the DNS service that maps host names to IP addresses.
You'll need to use Scotty to get DNS access.
Some systems also provide symbolic names for well-known port numbers. For example, instead of
using 20 for the FTP service, you can use ftp. On UNIX systems, the well-known port numbers are
listed in the file named /etc/services.
Client Socket Options
The socket command accepts some optional arguments when opening the client-side socket. The
general form of the command is:
socket ?-async? ?-myaddr address? ?-myport myport? host port
Ordinarily the address and port on the client side are chosen automatically. If your computer has
multiple network interfaces, you can select one with the -myaddr option. The address value can be a
domain name or an IP address. If your application needs a specific client port, it can choose one with
the -myport option. If the port is in use, the socket command will raise an error.
The -async option causes connection to happen in the background, and the socket command returns
immediately. The socket becomes writable when the connection completes, or fails. You can use
fileevent to get a callback when this occurs. This is shown in Example 17-1. If you use the socket
before the connection completes, and the socket is in blocking mode, then Tcl automatically blocks
and waits for the connection to complete. If the socket is in nonblocking mode, attempts to use the
socket return immediately. The gets and read commands would return -1, and fblocked would return
1 in this situation.
In some cases, it can take a long time to open the connection to the server. Usually this occurs when
the server host is down, and it may take longer than you want for the connection to time out. The
following example sets up a timer with after so that you can choose your own timeout limit on the
connection:
Example 17-1 Opening a client socket with a timeout.
proc Socket_Client {host port timeout} {
global connected
after $timeout {set connected timeout}
set sock [socket -async $host $port]
fileevent $sock w {set connected ok}
vwait connected
if {$connected == "timeout"} {
return -code error timeout
} else {
return $sock
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter17. Socket Programming
Server Sockets
A TCP server socket allows multiple clients. The way this works is that the socket command creates a
listening socket, and then new sockets are created when clients make connections to the server. Tcl
takes care of all the details and makes this easy to use. You simply specify a port number and give the
socket command a callback to execute when a client connects to your server socket. The callback is
just a Tcl command. A simple example is shown below:
Example 17-2 Opening a server socket.
set listenSocket [socket -server Accept 2540]
proc Accept {newSock addr port} {
puts "Accepted $newSock from $addr port $port"
}
vwait forever
The Accept command is the callback. With server sockets, Tcl adds additional arguments to the
callback before it calls it. The arguments are the new socket connection, and the host and port number
of the remote client. In this simple example, Accept just prints out its arguments.
The vwait command puts Tcl into its event loop so that it can do the background processing necessary
to accept connections. The vwait command will wait until the forever variable is modified, which
won't happen in this simple example. The key point is that Tcl processes other events (e.g., network
connections and other file I/O) while it waits. If you have a Tk application (e.g., wish), then it already
has an event loop to handle window system events, so you do not need to use vwait. The Tcl event
loop is discussed on page 217.
Server Socket Options
By default, Tcl lets the operating system choose the network interface used for the server socket, and
you simply supply the port number. If your computer has multiple interfaces, you may want to specify
a particular one. Use the -myaddr option for this. The general form of the command to open server
sockets is:
the buffering behavior of the network socket. You can use Example 17-3 as a template for more
interesting services.
The Echo_Server procedure opens the socket and saves the result in echo(main). When this socket is
closed later, the server stops accepting new connections but existing connections won't be affected. If
you want to experiment with this server, start it and wait for connections like this:
Echo_Server 2540
vwait forever
The EchoAccept procedure uses the fconfigure command to set up line buffering. This means that
each puts by the server results in a network transmission to the client. The importance of this will be
described in more detail later. A complete description of the fconfigure command is given in
Chapter 16. The EchoAccept procedure uses the fileevent command to register a procedure that
handles I/O on the socket. In this example, the Echo procedure will be called whenever the socket is
readable. Note that it is not necessary to put the socket into nonblocking mode when using the
fileevent callback. The effects of nonblocking mode are discussed on page 221.
EchoAccept saves information about each client in the echo array. This is used only to print out a
message when a client closes its connection. In a more sophisticated server, however, you may need to
keep more interesting state about each client. The name of the socket provides a convenient handle on
the client. In this case, it is used as part of the array index.
The Echo procedure first checks to see whether the socket has been closed by the client or there is an
error when reading the socket. The if expression only performs the gets if the eof does not return
true:
if {[eof $sock] || [catch {gets $sock line}]} {
Closing the socket automatically clears the fileevent registration. If you forget to close the socket
upon the end of file condition, the Tcl event loop will invoke your callback repeatedly. It is important
to close it when you detect end of file.
Example 17-4 A client of the echo service.
proc Echo_Client {host port} {
set s [socket $host $port]
fconfigure $s -buffering line
return $s
}
set s [Echo_Client localhost 2540]
puts $s "Hello!"
gets $s
=> Hello!
In the normal case, the server simply reads a line with gets and then writes it back to the client with
puts. If the line is "quit," then the server closes its main socket. This prevents any more connections
by new clients, but it doesn't affect any clients that are already connected.
Example 17-4 shows a sample client of the Echo service. The main point is to ensure that the socket is
line buffered so that each puts by the client results in a network transmission. (Or, more precisely,
each newline character results in a network transmission.) If you forget to set line buffering with
fconfigure, the client's gets command will probably hang because the server will not get any data; it
will be stuck in buffers on the client.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter17. Socket Programming
Fetching a URL with HTTP
The HyperText Transport Protocol (HTTP) is the protocol used on the World Wide Web. This section
presents a procedure to fetch pages or images from a server on the Web. Items in the Web are
identified with a Universal Resource Location (URL) that specifies a host, port, and location on the
host. The basic outline of HTTP is that a client sends a URL to a server, and the server responds with
some header information and some content data. The header information describes the content, which
can be hypertext, images, postscript, and more.
Example 17-5 Opening a connection to an HTTP server.
proc Http_Open {url} {
global http
if {![regexp -nocase {^(http://)?([^:/]+)(:([0-9])+)?(/.*)}\
$url x protocol server y port path]} {
error "bogus URL: $url"
}
if {[string length $port] == 0} {
set port 80
}
set sock [socket $server $port]
puts $sock "GET $path HTTP/1.0"
puts $sock "Host: $server"
puts $sock "User-Agent: Tcl/Tk Http_Open"
puts $sock ""
flush $sock
return $sock
}
The Http_Open procedure uses regexp to pick out the server and port from the URL. This regular
expression is described in detail on page 149. The leading http:// is optional, and so is the port
number. If the port is left off, then the standard port 80 is used. If the regular expression matches, then
a socket command opens the network connection.
The protocol begins with the client sending a line that identifies the command (GET), the path, and the
protocol version. The path is the part of the URL after the server and port specification. The rest of the
request is lines in the following format:
key: value
The Host identifies the server, which supports servers that implement more than one server name. The
User-Agent identifies the client program, which is often a browser like Netscape Navigator or
Internet Explorer. The key-value lines are terminated with a blank line. This data is flushed out of the
Tcl buffering system with the flush command. The server will respond by sending the URL contents
back over the socket. This is described shortly, but first we consider proxies.
Proxy Servers
A proxy is used to get through firewalls that many organizations set up to isolate their network from
the Internet. The proxy accepts HTTP requests from clients inside the firewall and then forwards the
requests outside the firewall. It also relays the server's response back to the client. The protocol is
nearly the same when using the proxy. The difference is that the complete URL is passed to the GET
command so that the proxy can locate the server. Example 17-6 uses a proxy if one is defined:
Example 17-6 Opening a connection to an HTTP server.
# Http_Proxy sets or queries the proxy
proc Http_Proxy {{new {}}} {
global http
if ![info exists http(proxy)] {
return {}
}
if {[string length $new] == 0} {
return $http(proxy):$http(proxyPort)
} else {
regexp {^([^:]+):([0-9]+)$}$new x \
http(proxy) http(proxyPort)
}
}
proc Http_Open {url {cmd GET} {query {}}} {
global http
if {![regexp -nocase {^(http://)?([^:/]+)(:([0-9])+)?(/.*)}\
$url x protocol server y port path]} {
error "bogus URL: $url"
}
if {[string length $port] == 0} {
set port 80
}
if {[info exists http(proxy)] &&
[string length $http(proxy)]} {
set sock [socket $http(proxy) $http(proxyPort)]
puts $sock "$cmd http://$server:$port$path HTTP/1.0"
} else {
set sock [socket $server $port]
puts $sock "$cmd $path HTTP/1.0"
}
puts $sock "User-Agent: Tcl/Tk Http_Open"
puts $sock "Host: $server"
if {[string length $query] > 0} {
puts $sock "Content-Length: [string length $query]"
puts $sock ""
puts $sock $query
}
puts $sock ""
flush $sock
fconfigure $sock -blocking 0
return $sock
}
The HEAD Request
In Example 17-6, the Http_Open procedure takes a cmd parameter so that the user of Http_Open can
perform different operations. The GET operation fetches the contents of a URL. The HEAD operation
just fetches the description of a URL, which is useful to validate a URL. The POST operation transmits
query data to the server (e.g., values from a form) and also fetches the contents of the URL. All of
these operations follow a similar protocol. The reply from the server is a status line followed by lines
that have key-value pairs. This format is similar to the client's request. The reply header is followed by
content data with GET and POST operations. Example 17-7 implements the HEAD command, which does
not involve any reply data:
Example 17-7 Http_Head validates a URL.
proc Http_Head {url} {
upvar #0 $url state
catch {unset state}
set state(sock) [Http_Open $url HEAD]
fileevent $state(sock) readable [list HttpHeader $url]
# Specify the real name, not the upvar alias, to vwait
vwait $url\(status)
catch {close $state(sock)}
return $state(status)
}
proc HttpHeader {url} {
upvar #0 $url state
if {[eof $state(sock)]} {
set state(status) eof
close $state(sock)
return
}
if {[catch {gets $state(sock) line}nbytes]} {
set state(status) error
lappend state(headers) [list error $nbytes]
close $state(sock)
return
}
if {$nbytes < 0} {
# Read would block
return
} elseif {$nbytes == 0} {
# Header complete
set state(status) head
} elseif {![info exists state(headers)]} {
# Initial status reply from the server
set state(headers) [list http $line]
} else {
# Process key-value pairs
regexp {^([^:]+): *(.*)$}$line x key value
lappend state(headers) [string tolower $key] $value
}
}
The Http_Head procedure uses Http_Open to contact the server. The HttpHeader procedure is
registered as a fileevent handler to read the server's reply. A global array keeps state about each
operation. The URL is used in the array name, and upvar is used to create an alias to the name (upvar
is described on page 86):
upvar #0 $url state
You cannot use the upvar alias as the variable specified to vwait. Instead, you must use the actual
name. The backslash turns off the array reference in order to pass the name of the array element to
vwait, otherwise Tcl tries to reference url as an array:
vwait $url\(status)
The HttpHeader procedure checks for special cases: end of file, an error on the gets, or a short read
on a nonblocking socket. The very first reply line contains a status code from the server that is in a
different format than the rest of the header lines:
code message
The code is a three-digit numeric code. 200 is OK. Codes in the 400's and 500's indicate an error. The
codes are explained fully in RFC 1945 that specifies HTTP 1.0. The first line is saved with the key
http:
set state(headers) [list http $line]
The rest of the header lines are parsed into key-value pairs and appended onto state(headers). This
format can be used to initialize an array:
array set header $state(headers)
When HttpHeader gets an empty line, the header is complete and it sets the state(status) variable,
which signals Http_Head. Finally, Http_Head returns the status to its caller. The complete information
about the request is still in the global array named by the URL. Example 17-8 illustrates the use of
Http_Head:
Example 17-8 Using Http_Head.
set url http://www.sun.com/
set status [Http_Head $url]
=> eof
upvar #0 $url state
array set info $state(headers)
parray info
info(http) HTTP/1.0 200 OK
info(server) Apache/1.1.1
info(last-modified) Nov ...
info(content-type) text/html
The GET and POST Requests
Example 17-9 shows Http_Get, which implements the GET and POST requests. The difference between
these is that POST sends query data to the server after the request header. Both operations get a reply
from the server that is divided into a descriptive header and the content data. The Http_Open
procedure sends the request and the query, if present, and reads the reply header. Http_Get reads the
content.
The descriptive header returned by the server is in the same format as the client's request. One of the
key-value pairs returned by the server specifies the Content-Type of the URL. The types come from
the MIME standard, which is described in RFC 1521. Typical content types are:
text/html ? HyperText Markup Language (HTML), which is introduced in Chapter 3.
text/plain ? plain text with no markup.
image/gif ? image data in GIF format.
image/jpeg ? image data in JPEG format.
application/postscript ? a postscript document.
application/x-tcl ? a Tcl program! This type is discussed in Chapter 20.
Example 17-9 Http_Get fetches the contents of a URL.
proc Http_Get {url {query {}}} {
upvar #0 $url state ;# Alias to global array
catch {unset state} ;# Aliases still valid.
if {[string length $query] > 0} {
set state(sock) [Http_Open $url POST $query]
} else {
set state(sock) [Http_Open $url GET]
}
set sock $state(sock)
fileevent $sock readable [list HttpHeader $url]
# Specify the real name, not the upvar alias, to vwait
vwait $url\(status)
set header(content-type) {}
set header(http) "500 unknown error"
array set header $state(headers)
# Check return status.
# 200 is OK, other codes indicate a problem.
regsub "HTTP/1.. " $header(http) {}header(http)
if {![string match 2* $header(http)]} {
catch {close $sock}
if {[info exists header(location)] &&
[string match 3* $header(http)]} {
# 3xx is a redirection to another URL
set state(link) $header(location)
return [Http_Get $header(location) $query]
}
return -code error $header(http)
}
# Set up to read the content data
switch -glob -- $header(content-type) {
text/* {
# Read HTML into memory
fileevent $sock readable [list HttpGetText $url]
}
default {
# Copy content data to a file
fconfigure $sock -translation binary
set state(filename) [File_TempName http]
if [catch {open $state(filename) w}out] {
set state(status) error
set state(error) $out
close $sock
return $header(content-type)
}
set state(fd) $out
fcopy $sock $out -command [list HttpCopyDone $url]
}
}
vwait $url\(status)
return $header(content-type)
}
Http_Get uses Http_Open to initiate the request, and then it looks for errors. It handles redirection
errors that occur if a URL has changed. These have error codes that begin with 3. A common case of
this error is when a user omits the trailing slash on a URL (e.g., http://www.scriptics.com). Most
servers respond with:
302 Document has moved
Location: http://www.scriptics.com/
If the content-type is text, then Http_Get sets up a fileevent handler to read this data into memory.
The socket is in nonblocking mode, so the read handler can read as much data as possible each time it
is called. This is more efficient than using gets to read a line at a time. The text will be stored in the
state(body) variable for use by the caller of Http_Get. Example 17-10 shows the HttpGetText
fileevent handler:
Example 17-10 HttpGetText reads text URLs.
proc HttpGetText {url} {
upvar #0 $url state
if {[eof $state(sock)]} {
# Content complete
set state(status) done
close $state(sock)
} elseif {[catch {read $state(sock)}block]} {
set state(status) error
lappend state(headers) [list error $block]
close $state(sock)
} else {
append state(body) $block
}
}
The content may be in binary format. This poses a problem for Tcl 7.6 and earlier. A null character
will terminate the value, so values with embedded nulls cannot be processed safely by Tcl scripts. Tcl
8.0 supports strings and variable values with arbitrary binary data. Example 17-9 uses fcopy to copy
data from the socket to a file without storing it in Tcl variables. This command was introduced in Tcl
7.5 as unsupported0, and became fcopy in Tcl 8.0. It takes a callback argument that is invoked when
the copy is complete. The callback gets additional arguments that are the bytes transferred and an
optional error string. In this case, these arguments are added to the url argument specified in the
fcopy command. Example 17-11 shows the HttpCopyDone callback:
Example 17-11 HttpCopyDone is used with fcopy.
proc HttpCopyDone {url bytes {error {}}} {
upvar #0 $url state
if {[string length $error]} {
set state(status) error
lappend state(headers) [list error $error]
} else {
set state(status) ok
}
close $state(sock)
close $state(fd)
}
The user of Http_Get uses the information in the state array to determine the status of the fetch and
where to find the content. There are four cases to deal with:
There was an error, which is indicated by the state(error) element.
There was a redirection, in which case, the new URL is in state(link). The client of Http_Get
should change the URL and look at its state instead. You can use upvar to redefine the alias for
the state array:
upvar #0 $state(link) state
There was text content. The content is in state(body).
There was another content type that was copied to state(filename).
The fcopy Command
The fcopy command can do a complete copy in the background. It automatically sets up fileevent
handlers, so you do not have to use fileevent yourself. It also manages its buffers efficiently. The
general form of the command is:
fcopy input output ?-size size? ?-command callback?
The -command argument makes fcopy work in the background. When the copy is complete or an error
occurs, the callback is invoked with one or two additional arguments: the number of bytes copied,
and, in the case of an error, it is also passed an error string:
fcopy $in $out -command [list CopyDone $in $out]
proc CopyDone {in out bytes {error {}} {
close $in ; close $out
}
With a background copy, the fcopy command transfers data from input until end of file or size bytes
have been transferred. If no -size argument is given, then the copy goes until end of file. It is not safe
to do other I/O operations with input or output during a background fcopy. If either input or output
gets closed while the copy is in progress, the current copy is stopped. If the input is closed, then all
data already queued for output is written out.
Without a -command argument, the fcopy command reads as much as possible depending on the
blocking mode of input and the optional size parameter. Everything it reads is queued for output
before fcopy returns. If output is blocking, then fcopy returns after the data is written out. If input is
blocking, then fcopy can block attempting to read size bytes or until end of file.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter17. Socket Programming
The http Package
The standard Tcl library includes an http package that is based on the code I wrote for this chapter.
This section documents the package, which has a slightly different interface. The library version uses
namespaces and combines the Http_Get, Http_Head, and Http_Post procedures into a single
http::geturl procedure. The examples in this chapter are still interesting, but you should look at
http.tcl in the Tcl library, which I also wrote. Definitely use the standard http package for your
production code.
http::config
The http::config command is used to set the proxy information, time-outs, and the User-Agent and
Accept headers that are generated in the HTTP request. You can specify the proxy host and port, or
you can specify a Tcl command that is run to determine the proxy. With no arguments, http::config
returns the current settings:
http::config
=> -accept */* -proxyfilter httpProxyRequired -proxyhost
{}-proxyport {}-timeout unlimited
-useragent {Tcl http client package 2.0}
If you specify just one option, its value is returned:
http::config -proxyfilter
=> httpProxyRequired
You can set one or more options:
http::config -proxyhost webcache.eng -proxyport 8080
The default proxy filter just returns the -proxyhost and -proxyport values if they are set. You can
supply a smarter filter that picks a proxy based on the host in the URL. The proxy filter is called with
the hostname and should return a list of two elements, the proxy host and port. If no proxy is required,
return an empty list.
The -timeout value limits the time the transaction can take. Its value is unlimited for no timeout, or
a milliseconds value. You can specify 500, for example, to have a half-second timeout.
http::geturl
The http::geturl procedure does a GET, POST, or HEAD transaction depending on its arguments. By
default, http::geturl blocks until the request completes and it returns a token that represents the
transaction. As described below, you use the token to get the results of the transaction. If you supply a
-command callback option, then http::geturl returns immediately and invokes callback when the
transaction completes. The callback is passed the token that represents the transaction. Table 17-1 lists
the options to http::geturl:
Table 17-1. Options to the http::geturl command.
-blocksize num Block size when copying to a channel.
-channel fileID The fileID is an open file or socket. The URL data is copied to this channel
instead of saving it in memory.
-command
callback
Calls callback when the transaction completes. The token from http::geturl
is passed to callback.
-handler
command
Called from the event handler to read data from the URL.
-headers list The list specifies a set of headers that are included in the HTTP request. The
list alternates between header keys and values.
-progress
command
Calls command after each block is copied to a channel. It gets called with three
parameters:
command token totalsize currentsize
-query
codedstring
Issues a POST request with the codedstring form data.
-timeout msec Aborts the request after msec milliseconds have elapsed.
-validate bool If bool is true, a HEAD request is made.
For simple applications you can simply block on the transaction:
set token [http::geturl www.beedub.com/index.html]
=> http::1
The leading http:// in the URL is optional. The return value is a token that is also the name of a
global array that contains state about the transaction. Names like http::1 are used instead of using the
URL as the array name. You can use upvar to convert the return value from http::geturl to an array
variable:
upvar #0 $token state
By default, the URL data is saved in state(body). The elements of the state array are described in
Table 17-2:
Table 17-2. Elements of the http::geturl state array.
body
The contents of the URL.
currentsize
The current number of bytes transferred.
error
An explanation of why the transaction was aborted.
http
The HTTP reply status.
meta
A list of the keys and values in the reply header.
status
The current status: pending, ok, eof, or reset.
totalsize
The expected size of the returned data.
type
The content type of the returned data.
url
The URL of the request.
A handful of access functions are provided so that you can avoid using the state array directly. These
are listed in Table 17-3:
Table 17-3. The http support procedures.
http::data $token
Returns state(body).
http::status $token
Returns state(status).
http::error $token
Returns state(error).
http::code $token
Returns state(http).
http::wait $token
Blocks until the transaction completes.
http::cleanup $token
Unsets the state array named by $token.
You can take advantage of the asynchronous interface by specifying a command that is called when
the transaction completes. The callback is passed the token returned from http::geturl so that it can
access the transaction state:
http::geturl $url -command [list Url_Display $text $url]
proc Url_Display {text url token} {
upvar #0 $token state
# Display the url in text
}
You can have http::geturl copy the URL to a file or socket with the -channel option. This is useful
for downloading large files or images. In this case, you can get a progress callback so that you can
provide user feedback during the transaction. Example 17-12 shows a simple downloading script:
Example 17-12 Downloading files with http::geturl.
#!/usr/local/tclsh8.0
if {$argc < 2} {
puts stderr "Usage: $argv0 url file"
exit 1
}
set url [lindex $argv 0]
set file [lindex $argv 1]
set out [open $file w]
proc progress {token total current} {
puts -nonewline "."
}
http::config -proxyhost webcache.eng -proxyport 8080
set token [http::geturl $url -progress progress \
-headers {Pragma no-cache}-channel $out]
close $out
# Print out the return header information
puts ""
upvar #0 $token state
puts $state(http)
foreach {key value}$state(meta) {
puts "$key: $value"
}
exit 0
http::formatQuery
If you specify form data with the -query option, then http::geturl does a POST transaction. You
need to encode the form data for safe transmission. The http::formatQuery procedure takes a list of
keys and values and encodes them in x-www-url-encoded format. Pass this result as the query data:
http::formatQuery name "Brent Welch" title "Tcl Programmer"
=> name=Brent+Welch&title=Tcl+Programmer
http::reset
You can cancel an outstanding transaction with http::reset:
http::reset $token
This is done automatically when you setup a -timeout with http::config.
http::cleanup
When you are done with the data returned from http::geturl, use the http::cleanup procedure to
unset the state variable used to store the data.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter17. Socket Programming
Basic Authentication
Web pages are often password protected. The most common form of this uses a protocol called Basic
Authentication, which is not very strong, but easy to implement. With this scheme, the server responds
to an HTTP request with a 401 error status and a Www-Authenticate header, which specifies the
authentication protocol the server wants to use. For example, the server response can contain the
following information:
HTTP/1.0 401 Authorization Required
Www-Authenticate: Basic realm="My Pages"
The realm is meant to be an authentication domain. In practice, it is used in the string that gets
displayed to the user as part of the password prompt. For example, a Web browser will display this
prompt:
Enter the password for My Pages at www.beedub.com
After getting the user name and password from the user, the Web browser tries its HTTP request
again. This time it includes an Authorization header that contains the user name and password
encoded with base64 encoding. There is no encryption at all ?anyone can decode the string, which is
why this is not a strong form of protection. The Tcl Web Server includes a base64.tcl file that has
Base64_Encode and Base64_Decode procedures. Example 17-13 illustrates the Basic Authentication
protocol. http::geturl takes a -headers option that lets you pass additional headers in the request.
Example 17-13 Basic Authentication using http::geturl.
proc BasicAuthentication {url promptProc} {
set token [http::geturl $url]
http::wait $token
if {[string match 401* [http::status $token]]} {
upvar #0 $token data
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter18. TclHttpd Web Server
Integrating TclHttpd with your Application
The bulk of this chapter describes the various ways you can extend the server and integrate it into your
application. TclHttpd is interesting because, as a Tcl script, it is easy to add to your application.
Suddenly your application has an interface that is accessible to Web browsers in your company's
intranet or the global Internet. The Web server provides several ways you can connect it to your
application:
Static pages. As a "normal" web server, you can serve static documents that describe your
application.
Domain handlers. You can arrange for all URL requests in a section of your Web site to be
handled by your application. This is a very general interface where you interpret what the URL
means and what sort of pages to return to each request. For example,
http://www.scriptics.com/resource is implemented this way. The URL past /resource selects an
index in a simple database, and the server returns a page describing the pages under that index.
Application-Direct URLs. This is a domain handler that maps URLs onto Tcl procedures. The
form query data that is part of the HTTP GET or POST request is automatically mapped onto the
parameters of the application-direct procedure. The procedure simply computes the page as its
return value. This is an elegant and efficient alternative to the CGI interface. For example, in
TclHttpd the URLs under /status report various statistics about the web server's operation.
Document handlers. You can define a Tcl procedure that handles all files of a particular type. For
example, the server has a handler for CGI scripts, HTML files, image maps, and HTML+Tcl
template files.
HTML+Tcl Templates. These are web pages that mix Tcl and HTML markup. The server
replaces the Tcl using the subst command and returns the result. The server can cache the result
in a regular HTML file to avoid the overhead of template processing on future requests.
Templates are a great way to maintain common look and feel to a family of web pages, as well as
to implement more advanced dynamic HTML features like self-checking forms.
TclHttpd Architecture
Figure 18-1 shows the basic components of the server. At the core is the Httpd module (httpd.tcl),
which implements the server side of the HTTP protocol. The "d" in Httpd stands for daemon, which is
the name given to system servers on UNIX. This module manages network requests, dispatches them to
the Url module, and provides routines used to return the results to requests.
Figure 18-1. The dotted box represents one application that embeds TclHttpd. Document
templates and Application Direct URLs provide direct connections from an HTTP request to
your application.
The Url module (url.tcl) divides the Web site into domains, which are subtrees of the URL
hierarchy provided by the server. The idea is that different domains may have completely different
implementations. For example, the Document domain (doc.tcl) maps its URLs into files and
directories on your hard disk, while the Application-Direct domain (direct.tcl) maps URLs into Tcl
procedure calls within your application. The CGI domain (cgi.tcl) maps URLs onto other programs
that compute web pages.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter18. TclHttpd Web Server
Domain Handlers
You can implement new kinds of domains that provide your own interpretation of a URL. This is the
most flexible interface available to extend the web server. You provide a callback that is invoked to
handle every request in a domain, or subtree, of the URL hierarchy. The callback interprets the URL,
computes the page content, and returns the data using routines from the Httpd module.
Example 18-1 defines a simple domain that always returns the same page to every request. The domain
is registered with the Url_PrefixInstall command. The arguments to Url_PrefixInstall are the
URL prefix and a callback that is called to handle all URLs that match that prefix. In the example, all
URLs that have the prefix /simple are dispatched to the SimpleDomain procedure.
Example 18-1 A simple URL domain.
Url_PrefixInstall /simple [list SimpleDomain /simple]
proc SimpleDomain {prefix sock suffix} {
upvar #0 Httpd$sock data
# Generate page header
set html "<title>A simple page</title>\n"
append html "<h1>$prefix$suffix</h1>\n"
append html "<h1>Date and Time</h1>\n"
append html [clock format [clock seconds]]
# Display query data
if {[info exist data(query)]} {
append html "<h1>Query Data</h1>\n"
append html "<table>\n"
foreach {name value}[Url_DecodeQuery $data(query)] {
append html "<tr><td>$name</td>\n"
append html "<td>$value</td></tr>\n"
}
append html "</table>\n"
}
Httpd_ReturnData $sock text/html $html
}
The SimpleDomain handler illustrates several properties of domain handlers. The sock and suffix
arguments to SimpleDomain are appended by Url_Dispatch when it invokes the domain handler. The
suffix parameter is the part of the URL after the prefix. The prefix is passed in as part of the
callback definition so the domain handler can recreate the complete URL. For example, if the server
receives a request for the URL /simple/page, then the prefix is /simple, the suffix is /request.
Connection State and Query Data
The sock parameter is a handle on the socket connection to the remote client. This variable is also
used to name a state variable that the Httpd module maintains about the connection. The name of the
state array is Httpd$sock, and SimpleDomain uses upvar to get a more convenient name for this array
(i.e., data):
upvar #0 Httpd$sock data
An important element of the state array is the query data, data(query). This is the information that
comes from HTML forms. The query data arrives in an encoded format, and the Url_DecodeQuery
procedure is used to decode the data into a list of names and values. Url_DecodeQuery is similar to
Cgi_List from Example 11-5 on page 154 and is a standard function provided by url.tcl.
Returning Results
Finally, once the page has been computed, the Httpd_ReturnData procedure is used to return the page
to the client. This takes care of the HTTP protocol as well as returning the data. There are three related
procedures, Httpd_ReturnFile, Httpd_Error, and Httpd_Redirect. These are summarized in Table
18-1 on page 261.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter18. TclHttpd Web Server
Application Direct URLs
The Application Direct domain implementation provides the simplest way to extend the Web server. It
hides the details associated with query data, decoding URL paths, and returning results. All you do is
define Tcl procedures that correspond to URLs. Their arguments are automatically matched up to the
query data as shown in Example 13-3 on page 179. The Tcl procedures compute a string that is the
result data, which is usually HTML. That's all there is to it.
The Direct_Url procedure defines a URL prefix and a corresponding Tcl command prefix. Any URL
that begins with the URL prefix will be handled by a corresponding Tcl procedure that starts with the
Tcl command prefix. This is shown in Example 18-2:
Example 18-2 Application Direct URLs.
Direct_Url /demo Demo
proc Demo {} {
return "<html><head><title>Demo page</title></head>\n\
<body><h1>Demo page</h1>\n\
<a href=/demo/time>What time is it?</a>\n\
<form action=/demo/echo>\n\
Data: <input type=text name=data>\n\
<br>\n\
<input type=submit name=echo value='Echo Data'>\n\
</form>\n\
</body></html>"
}
proc Demo/time {{format "%H:%M:%S"}} {
return [clock format [clock seconds] -format $format]
}
proc Demo/echo {args} {
# Compute a page that echoes the query data
set html "<head><title>Echo</title></head>\n"
append html "<body><table>\n"
auto_path, which assumes you have a tclIndex or pkgIndex.tcl file in the directory so that
the procedures are loaded when needed.
Where to put your Tcl Code
There are three places you can put the code of your application: directly in your template pages, in the
per-directory ".tml" files, or in the library directory.
The advantage of putting procedure definitions in the library is that they are defined one time but
executed many times. This works well with the Tcl byte-code compiler. The disadvantage is that if you
modify procedures in these files, you have to explicitly source them into the server for these changes
to take effect. The /debug/source URL described on page 267 is handy for this chore.
The advantage of putting code into the per-directory ".tml" files is that changes are picked up
immediately with no effort on your part. The server automatically checks if these files are modified
and sources them each time it processes your templates. However, that code is run only one time, so
the byte-code compiler just adds overhead.
I try to put as little code as possible in my file.tml template files. It is awkward to put lots of code
there, and you cannot share procedures and variable definitions easily with other pages. Instead, my
goal is to have just procedure calls in the template files, and put the procedure definitions elsewhere. I
also avoid putting if and foreach commands directly into the page.
Templates for Site Structure
The next few examples show a simple template system used to maintain a common look at feel across
the pages of a site. Example 18-5 shows a simple one-level site definition that is kept in the root .tml
file. This structure lists the title and URL of each page in the site:
Example 18-5 A one-level site structure.
set site(pages) {
Home /index.html
"Ordering Computers"/ordering.html
"New Machine Setup" /setup.html
"Adding a New User" /newuser.html
"Network Addresses" /network.html
}
Each page includes two commands, SitePage and SiteFooter, that generate HTML for the
navigational part of the page. Between these commands is regular HTML for the page content.
Example 18-6 shows a sample template file:
Example 18-6 A HTML + Tcl template file.
[SitePage "New Machine Setup"]
This page describes the steps to take when setting up a new
computer in our environment. See
<a href=/ordering.html>Ordering Computers</a>
for instructions on ordering machines.
<ol>
<li>Unpack and setup the machine.
<li>Use the Network control panel to set the IP address
and hostname.
<!-- Several steps omitted -->
<li>Reboot for the last time.
</ol>
[SiteFooter]
The SitePage procedure takes the page title as an argument. It generates HTML to implement a
standard navigational structure. Example 18-7 has a simple implementation of SitePage:
Example 18-7 SitePage template procedure.
proc SitePage {title} {
global site
set html "<html><head><title>$title</title></head>\n"
append html "<body bgcolor=white text=black>\n"
append html "<h1>$title</h1>\n"
set sep ""
foreach {label url} $site(pages) {
append html $sep
if {[string compare $label $title] == 0} {
append html "$label"
} else {
append html "<a href='$url'>$label</a>"
}
set sep " | "
}
return $html
}
The foreach loop that computes the simple menu of links turns out to be useful in many places.
Example 18-8 splits out the loop and uses it in the Site-Page and SiteFooter procedures. This
version of the templates creates a left column for the navigation and a right column for the page
content:
Example 18-8 SiteMenu and SiteFooter template procedures.
proc SitePage {title} {
global site
set html "<html><head><title>$title</title></head>\n\
<body bgcolor=$site(bg) text=$site(fg)>\n\
<!-- Two Column Layout -->\n\
<table cellpadding=0>\n\
<tr><td>\n\
<!-- Left Column -->\n\
<img src='$site(mainlogo)'>\n\
<font size=+1>\n\
[SiteMenu <br> $site(pages)]\n\
</font>\n\
</td><td>\n\
<!-- Right Column -->\n\
<h1>$title</h1>\n\
<p>\n"
return $html
}
proc SiteFooter {} {
global site
set html "<p><hr>\n\
<font size=-1>[SiteMenu | $site(pages)]</font>\n\
</td></tr></table>\n"
return $html
}
proc SiteMenu {sep list} {
global page
set s ""
set html ""
foreach {label url} $list {
if {[string compare $page(url) $url] == 0} {
append html $s$label
} else {
append html "$s<a href='$url'>$label</a>"
}
set s $sep
}
return $html
}
Of course, a real site will have more elaborate graphics and probably a two-level, three-level, or more
complex tree structure that describes its structure.You can also define a family of templates so that
each page doesn't have to fit the same mold. Once you start using templates, it is fairly easy to change
both the template implementation and to move pages around among different sections of your Web
site.
There are many other applications for "macros" that make repetitive HTML coding chores easy. Take,
for example, the link to /ordering.html in Example 18-6. The proper label for this is already defined
in $site(pages), so we could introduce a SiteLink procedure that uses this:
Example 18-9 The SiteLink procedure.
proc SiteLink {label} {
global site
array set map $site(pages)
if {[info exist map($label)]} {
return "<a href='$map($label)'>$label</a>"
} else {
return $label
}
}
If your pages embed calls to SiteLink, then you can change the URL associated with the page name
by changing the value of site(pages). If this is stored in the top-level ".tml" file, the templates will
automatically track the changes.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter18. TclHttpd Web Server
Form Handlers
HTML forms and form-handling programs go together. The form is presented to the user on the client
machine. The form handler runs on the server after the user fills out the form and presses the submit
button. The form presents input widgets like radiobuttons, checkbuttons, selection lists, and text entry
fields. Each of these widgets is assigned a name, and each widget gets a value based on the user's
input. The form handler is a program that looks at the names and values from the form and computes
the next page for the user to read.
CGI is a standard way to hook external programs to web servers for the purpose of processing form
data. CGI has a special encoding for values so that they can be transported safely. The encoded data is
either read from standard input or taken from the command line. The CGI program decodes the data,
processes it, and writes a new HTML page on its standard output. Chapter 3 describes writing CGI
scripts in Tcl.
TclHttpd provides alternatives to CGI that are more efficient because they are built right into the
server. This eliminates the overhead that comes from running an external program to compute the
page. Another advantage is that the Web server can maintain state between client requests in Tcl
variables. If you use CGI, you must use some sort of database or file storage to maintain information
between requests.
Application Direct Handlers
The server comes with several built-in form handlers that you can use with little effort. The
/mail/forminfo URL will package up the query data and mail it to you. You use form fields to set
various mail headers, and the rest of the data is packaged up into a Tcl-readable mail message.
Example 18-10 shows a form that uses this handler. Other built-in handlers are described starting at
page 266.
Example 18-10 Mail form results with /mail/forminfo.
<form action=/mail/forminfo method=post>
<input type=hidden name=sendto value=mailreader@my.com>
<input type=hidden name=subject value="Name and Address">
<table>
<tr><td>Name</td><td><input name=name></td></tr>
<tr><td>Address</td><td><input name=addr1></td></tr>
<tr><td> </td><td><input name=addr2></td></tr>
<tr><td>City</td><td><input name=city></td></tr>
<tr><td>State</td><td><input name=state></td></tr>
<tr><td>Zip/Postal</td><td><input name=zip></td></tr>
<tr><td>Country</td><td><input name=country></td></tr>
</table>
</form>
The mail message sent by /mail/forminfo is shown in Example 18-11.
Example 18-11 Mail message sent by /mail/forminfo.
To: mailreader@my.com
Subject: Name and Address
data {
name {Joe Visitor}
addr1 {Acme Company}
addr2 {100 Main Street}
city {Mountain View}
state California
zip 12345
country USA
}
It is easy to write a script that strips the headers, defines a data procedure, and uses eval to process
the message body. Whenever you send data via e-mail, if you format it with Tcl list structure, you can
process it quite easily. The basic structure of such a mail reader procedure is shown in Example 18-12:
Example 18-12 Processing mail sent by /mail/forminfo.
# Assume the mail message is on standard input
set X [read stdin]
# Strip off the mail headers, when end with a blank line
if {[regsub {.*?\n\ndata} $X {data} X] != 1} {
error "Malformed mail message"
}
proc data {fields} {
foreach {name value} $fields {
# Do something
}
}
# Process the message. For added security, you may want
# do this part in a safe interpreter.
eval $X
Template Form Handlers
The drawback of using application-direct URL form handlers is that you must modify their Tcl
implementation to change the resulting page. Another approach is to use templates for the result page
that embed a command that handles the form data. The Mail_FormInfo procedure, for example, mails
form data. It takes no arguments. Instead, it looks in the query data for sendto and subject values,
and if they are present, it sends the rest of the data in an e-mail. It returns an HTML comment that
flags that mail was sent.
When you use templates to process form data, you need to turn off result caching because the server
must process the template each time the form is submitted. To turn off caching, embed the
Doc_Dynamic command into your form handler pages, or set the page(dynamic) variable to 1.
Alternatively, you can simply post directly to the file.tml page instead of to the file.html page.
Self Posting Forms
This section illustrates a self-posting form. This is a form on a page that posts the form data to back to
the same page. The page embeds a Tcl command to check its own form data. Once the data is correct,
the page triggers a redirect to the next page in the flow. This is a powerful technique that I use to
create complex page flows using templates. Of course, you need to save the form data at each step.
You can put the data in Tcl variables, use the data to control your application, or store it into a
database. TclHttpd comes with a Session module, which is one way to manage this information. For
details you should scan the session.tcl file in the distribution.
Example 18-13 shows the Form_Simple procedure that generates a simple self-checking form. Its
arguments are a unique ID for the form, a description of the form fields, and the URL of the next page
in the flow. The field description is a list with three elements for each field: a required flag, a form
element name, and a label to display with the form element. You can see this structure in the template
shown in Example 18-14 on page 260. The procedure does two things at once. It computes the HTML
form, and it also checks if the required fields are present. It uses some procedures from the form
module to generate form elements that retain values from the previous page. If all the required fields
are present, it discards the HTML, saves the data, and triggers a redirect by calling Doc_Redirect.
Example 18-13 A self-checking form procedure.
proc Form_Simple {id fields nextpage} {
global page
if {![form::empty formid]} {
# Incoming form values, check them
set check 1
} else {
# First time through the page
set check 0
}
set html "<!-- Self-posting. Next page is $nextpage -->\n"
append html "<form action=\"$page(url)\" method=post>\n"
append html "<input type=hidden name=formid value=$id>\n"
append html "<table border=1>\n"
foreach {required key label} $fields {
append html "<tr><td>"
if {$check && $required && [form::empty $key]} {
lappend missing $label
append html "<font color=red>*</font>"
}
append html "</td><td>$label</td>\n"
append html "<td><input [form::value $key]></td>\n"
append html "</tr>\n"
}
append html "</table>\n"
if {$check} {
if {![info exist missing]} {
# No missing fields, so advance to the next page.
# In practice, you must save the existing fields
# at this point before redirecting to the next page.
Doc_Redirect $nextpage
} else {
set msg "<font color=red>Please fill in "
append msg [join $missing ", "]
append msg "</font>"
set html <p>$msg\n$html
}
}
append html "<input type=submit>\n</form>\n"
return $html
}
Example 18-14 shows a page template that calls Form_Simple with the required field description.
Example 18-14 A page with a self-checking form.
<html><head>
<title>Name and Address Form</title>
</head>
<body bgcolor=white text=black>
<h1>Name and Address</h1>
Please enter your name and address.
[myform::simple nameaddr {
1 name "Name"
1 addr1 "Address"
0 addr2" "Address"
1 city "City"
0 state "State"
1 zip "Zip Code"
0 country "Country"
} nameok.html]
</body></html>
The form package
TclHttpd comes with a form package (form.tcl) that is designed to support self-posting forms. The
Form_Simple procedure uses form::empty to test if particular form values are present in the query
data. For example, it tests to see whether the formid field is present so that the procedure knows
whether or not to check for the rest of the fields. The form::value procedure is useful for constructing
form elements on self-posting form pages. It returns:
name="name" value="value"
The value is the value of form element name based on incoming query data, or just the empty string if
the query value for name is undefined. As a result, the form can post to itself and retain values from the
previous version of the page. It is used like this:
<input type=text [form::value name]>
The form::checkvalue and form::radiovalue procedures are similar to form::value but designed
for checkbuttons and radio buttons. The form::select procedure formats a selection list and
highlights the selected values. The form::data procedure simply returns the value of a given form
element. These are summarized in Table 18-6 on page 264.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter18. TclHttpd Web Server
Programming Reference
This section summarizes many of the more useful functions defined by the server. These tables are not
complete, however. You are encouraged to read through the code to learn more about the features
offered by the server.
Table 18-1 summarizes the Httpd functions used when returning pages to the client.
Table 18-1. Httpd support procedures.
Httpd_Error sock code Returns a simple error page to the client. The code is a numeric
error code like 404 or 500.
Httpd_ReturnData sock
type data
Returns a page with Content-Type type and content data.
Httpd_ReturnFile sock
type file
Returns a file with Content-Type type.
Httpd_Redirect newurl
sock
Generates a 302 error return with a Location of newurl.
Httpd_SelfUrl url Expands url to include the proper http://server:port prefix to
reference the current server.
Table 18-2 summarizes a few useful procedures provided by the Url module (url.tcl). The
Url_DecodeQuery is used to decode query data into a Tcl-friendly list. The Url_Encode procedure is
useful when encoding values directly into URLs. URL encoding is discussed in more detail on page
249
Table 18-2. Url support procedures.
Url_DecodeQuery query Decodes a www-url-encoded query string and return a name, value list.
Url_Encode value Returns value encoded according to the www-url-encoded standard.
Url_PrefxInstall
prefix callback
Registers callback as the handler for all URLs that begin with prefix.
The callback is invoked with two additional arguments: sock, the handle
to the client, and suffix, the part of the URL after prefix.
The Doc module provides procedures for configuration and generating responses, which are
summarized in Tables 18-3 and 18-4 respectively
Table 18-3. Doc procedures for configuration.
Doc_Root ?directory? Sets or queries the directory that corresponds to the root of the URL
hierarchy.
Doc_AddRoot virtual
directory
Maps the file system directory into the URL subtree starting at
virtual.
Doc_ErrorPage file Specifies a file relative to the document root used as a simple
template for error messages. This is processed by DocSubstSystem file
in doc.tcl.
Doc_CheckTemplates how If how is 1, then .html files are compared against corresponding .tml
files and regenerated if necessary.
Doc_IndexFile pattern Registers a file name pattern that will be searched for the default
index file in directories.
Doc_NotFoundPage file Specifies a file relative to the document root used as a simple
template for page not found messages. This is processed by
DocSubstSystem file in doc.tcl.
Doc_PublicHtml dirname Defines the directory used for each users home directory. When a URL
like ~user is specified, the dirname under their home directory is
accessed.
Doc_TemplateLibrary
directory
Adds directory to the auto_path so the source files in it are available
to the server.
Doc_TemplateInterp
interp
Specifies an alternate interpreter in which to process document
templates (i.e., .tml files.)
Doc_Webmaster ?email? Sets or queries the email for the Webmaster.
Table 18-4. Doc procedures for generating responses.
Doc_Error sock
errorInfo
Generates a 500 response on sock based on the template registered with
Doc_ErrorPage. errorInfo is a copy of the Tcl error trace after the error.
Doc_NotFound sock Generates a 404 response on sock by using the template registered with
Doc_NotFoundPage.
Doc_Subst sock file
?interp?
Performs a subst on the file and return the resulting page on sock. interp
specifies an alternate Tcl interpreter.
The Doc module also provides procedures for cookies and redirects that are useful in document
templates. These are described in Table 18-5.
Table 18-5. Doc procedures that support template processing.
Doc_Coookie name Returns the cookie name passed to the server for this
request, or the empty string if it is not present.
Doc_Dynamic
Turns off caching of the HTML result. Meant to be called
from inside a page template.
Doc_IsLinkToSelf url Returns 1 if the url is a link to the current page.
Doc_Redirect newurl Raises a special error that aborts template processing and
triggers a page redirect to newurl.
Doc_SetCookie -name name -value
value -path path -domain domain -
expires date
Sets cookie name with the given value that will be returned
to the client as part of the response. The path and domain
restrict the scope of the cooke. The date sets an expiration
date.
Table 18-6 describes the form module that is useful for self-posting forms, which are discussed on
page 259
Table 18-6. The form package.
form::data name Returns the value of the form value name, or the empty string.
form::empty name Returns 1 if the form value name is missing or zero length.
form::value name Returns name="name" value="value", where value comes from the
query data, if any.
form::checkvalue name
value
Returns name="name" value="value" CHECKED, if value is present in
the query data for name. Otherwise, it just returns name="name"
value="value".
form::radiovalue name
value
Returns name="name" value="value" CHECKED, if the query data for
name is equal to value. Otherwise, it just returns name="name"
value="value".
form::select name
valuelist args
Generates a select form element with name name. The valuelist
determines the option tags and values, and args are optional
parameters to the main select tag.
Table 18-7 shows the initial elements of the page array that is defined during the processing of a
template.
Table 18-7. Elements of the page array.
query
The decoded query data in a name, value list.
dynamic
If 1, the results of processing the template are not cached in the corresponding .html file.
filename
The file system pathname of the requested file (e.g.,
/usr/local/htdocs/tclhttpd/index.html).
template
The file system pathname of the template file (e.g.,
/usr/local/htdocs/tclhttpd/index.tml).
url
The part of the url after the server name (e.g., /tclhttpd/index.html).
root
A relative path from the template file back to the root of the URL tree. This is useful for
creating relative links between pages in different directories.
Table 18-8 shows the elements of the env array. These are defined during CGI requests, application-
direct URL handlers, and page template processing:
Table 18-8. Elements of the env array.
AUTH_TYPE
Authentication protocol (e.g., Basic).
CONTENT_LENGTH
The size of the query data.
CONTENT_TYPE
The type of the query data.
DOCUMENT_ROOT
File system pathname of the document root.
GATEWAY_INTERFACE
Protocol version, which is CGI/1.1.
HTTP_ACCEPT
The Accept headers from the request.
HTTP_AUTHORIZATION
The Authorization challenge from the request.
HTTP_COOKIE
The cookie from the request.
HTTP_FROM
The From: header of the request.
HTTP_REFERER
The Referer indicates the previous page.
HTTP_USER_AGENT
An ID string for the Web browser.
PATH_INFO
Extra path information after the template file.
PATH_TRANSLATED
The extra path information appended to the document root.
QUERY_STRING
The form query data.
REMOTE_ADDR
The client's IP address.
REMOTE_USER
The remote user name specified by Basic authentication.
REQUEST_METHOD
GET, POST, or HEAD.
REQUEST_URI
The complete URL that was requested.
SCRIPT_NAME
The name of the current file relative to the document root.
SERVER_NAME
The server name, e.g., www.beedub.com.
SERVER_PORT
The server's port, e.g., 80.
SERVER_PROTOCOL
The protocol (e.g., http or https).
SERVER_SOFTWARE
A software version string for the server.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter18. TclHttpd Web Server
Standard Application-Direct URLs
The server has several modules that provide application-direct URLs. These application-direct URLs
lets you control the server or examine its state from any Web browser. You can look at the
implementation of these modules as examples for your own application.
Status
The /status URL is implemented in the status.tcl file. The status module implements the display
of hit counts, document hits, and document misses (i.e., documents not found). The Status_Url
command enables the application-direct URLs and assigns the top-level URL for the status module.
The default configuration file contains this command:
Status_Url /status
Assuming this configuration, the following URLs are implemented:
Table 18-9. Status application-direct URLs.
/status
Main status page showing summary counters and hit count histograms.
/status/doc
Shows hit counts for each page. This page lets you sort by name or hit count,
and limit files by patterns.
/status/hello
A trivial URL that returns "hello".
/status/notfound
Shows miss counts for URLs that users tried to fetch.
/status/size
Displays an estimated size of Tcl code and Tcl data used by the TclHttpd
program.
/status/text
This is a version of the main status page that doesn't use the graphical
histograms of hit counts.
Debugging
The /debug URL is implemented in the debug.tcl file. The debug module has several useful URLs
that let you examine variable values and other internal state. It is turned on with this command in the
default configuration file:
Debug_Url /debug
Table 18-10 lists the /debug URLs. These URLs often require parameters that you can specify directly
in the URL. For example, the /debug/echo URL echoes its query parameters:
http://yourserver:port/debug/echo?name=value&name2=val2
Table 18-10. Debug application-direct URLs.
/debug/after
Lists the outstanding after events.
/debug/dbg
Connect TclPro Debugger. This takes a host and port parameter. You need to
install prodebug.tcl from TclPro into the server's script library directory.
/debug/echo
Echoes its query parameters. Accepts a title parameter.
/debug/errorInfo
Displays the errorInfo variable along with the server's version number and
Webmaster e-mail. Accepts title and errorInfo arguments.
/debug/parray
Displays a global array variable. The name of the variable is specified with the
aname parameter.
/debug/pvalue
A more general value display function. The name of the variable is specified
with the aname parameter. This can be a variable name, an array name, or a
pattern that matches several variable names.
/debug/raise
Raises an error (to test error handling). Any parameters become the error string.
The sample URL tree that is included in the distribution includes the file htdocs/hacks.html. This
file has several small forms that use the /debug URLs to examine variables and source files. Example
18-15 shows the implementation of /debug/source. You can see that it limits the files to the main
script library and to the script library associated with document templates. It may seem dangerous to
have these facilities, but I reason that because my source directories are under my control, it cannot
hurt to reload any source files. In general, the library scripts contain only procedure definitions and no
global code that might reset state inappropriately. In practice, the ability to tune (i.e., fix bugs) in the
running server has proven useful to me on many occasions. It lets you evolve your application without
restarting it!
/debug/source
Sources a file from either the server's main library directory or the
Doc_TemplateLibrary directory. The file is specified with the source
parameter.
The sample URL tree that is included in the distribution includes the file htdocs/hacks.html. This
file has several small forms that use the /debug URLs to examine variables and source files. Example
18-15 shows the implementation of /debug/source. You can see that it limits the files to the main
script library and to the script library associated with document templates. It may seem dangerous to
have these facilities, but I reason that because my source directories are under my control, it cannot
hurt to reload any source files. In general, the library scripts contain only procedure definitions and no
global code that might reset state inappropriately. In practice, the ability to tune (i.e., fix bugs) in the
running server has proven useful to me on many occasions. It lets you evolve your application without
restarting it!
Example 18-15 The /debug/source application-direct URL implementation.
proc Debug/source {source} {
global Httpd Doc
set source [file tail $source]
set dirlist $Httpd(library)
if {[info exists Doc(templateLibrary)]} {
lappend dirlist $Doc(templateLibrary)
}
foreach dir $dirlist {
set file [file join $dir $source]
if [file exists $file] {
break
}
}
set error [catch {uplevel #0 [list source $file]} result]
set html "<title>Source $source</title>\n"
if {$error} {
global errorInfo
append html "<H1>Error in $source</H1>\n"
append html "<pre>$result<p>$errorInfo</pre>"
} else {
append html "<H1>Reloaded $source</H1>\n"
append html "<pre>$result</pre>"
}
return $html
}
Administration
The /admin URL is implemented in the admin.tcl file. The admin module lets you load URL redirect
tables, and it provides URLs that reset some of the counters maintained by the server. It is turned on
with this command in the default configuration file:
Admin_Url /admin
Currently, there is only one useful admin URL. The /admin/redirect/reload URL sources the file
named redirect in the document root. This file is expected to contain a number of Url_Redirect
commands that establish URL redirects. These are useful if you change the names of pages and want
the old names to still work.
The administration module has a limited set of application-direct URLs because the simple
application-direct mechanism doesn't provide the right hooks to check authentication credentials. The
HTML+Tcl templates work better with the authentication schemes.
Sending Email
The /mail URL is implemented in the mail.tcl file. The mail module implements various form
handlers that email form data. Currently, it is UNIX-specific because it uses /usr/lib/sendmail to
send the mail. It is turned on with this command in the default configuration file:
Mail_Url /mail
The application-direct URLs shown in Table 18-11 are useful form handlers. You can specify them as
the ACTION parameter in your <FORM> tags. The mail module provides two Tcl procedures that are
generally useful. The MailInner procedure is the one that sends mail. It is called like this:
MailInner sendto subject from type body
The sendto and from arguments are e-mail addresses. The type is the Mime type (e.g., text/plain or
text/html) and appears in a Content-Type header. The body contains the mail message without any
headers.
Table 18-11. Application-direct URLS that e-mail form results.
/mail/bugreport
Sends e-mail with the errorInfo from a server error. It takes an email
parameter for the destination address and an errorInfo parameter. Any
additional arguments get included into the message.
/mail/forminfo
Sends e-mail containing form results. It requires these parameters: sendto for the
destination address, subject for the mail subject, href and label for a link to
display on the results page. Any additional arguments are formatted with the Tcl
list command for easy processing by programs that read the mail.
/mail/formdata
This is an older form of /mail/forminfo that doesn't format the data into Tcl
lists. It requires only the email and subject parameters. The rest are formatted
into the message body.
The Mail_FormInfo procedure is designed for use in HTML+Tcl template files. It takes no arguments
but instead looks in current query data for its parameters. It expects to find the same arguments as the
/mail/forminfo direct URL. Using a template with Mail_FormInfo gives you more control over the
result page than posting directly to /mail/forminfo, and is illustrated in Example 18-10 on page 257.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter18. TclHttpd Web Server
The TclHttpd Distribution
Get the TclHttpd distribution from the CD-ROM, or find it on the Internet at:
ftp://ftp.scriptics.com/pub/tcl/httpd/
http://www.scriptics.com/tclhttpd/
Quick Start
Unpack the tar file or the zip file, and you can run the server from the httpd.tcl script in the bin
directory. On UNIX:
tclsh httpd.tcl -port 80
This command will start the Web server on the standard port (80). By default it uses port 8015 instead.
If you run it with the -help flag, it will tell you what command line options are available. If you use
wish instead of tclsh, then a simple Tk user interface is displayed that shows how many hits the server
is getting.
On Windows you can double-click the httpd.tcl script to start the server. It will use wish and display
the user interface. Again it will start on port 8015. You will need to create a shortcut that passes the -
port argument, or edit the associated configuration file to change this. Configuring the server is
described later.
Once you have the server running, you can connect to it from your Web browser. Use this URL if you
are running on the default (nonstandard) port:
http://hostname:8015/
If you are running without a network connection, you may need to specify 127.0.0.1 for the
hostname. This is the "localhost" address and will bypass the network subsystem.
http://127.0.0.1:8015/
Table 18-12. The command line values are mapped into the Config array by the httpd.tcl start-up
script.
Table 18-12. Basic TclHttpd Parameters.
Parameter Command Option Config Variable
Port number. The default is 8015. -port number Config(port)
Server name. The default is [info hostname]. -name name Config(name)
IP address. The default is 0, for "any address". -ipaddr address Config(ipaddr)
Directory of the root of the URL tree. The default is the
htdocs directory.
-docRoot
directory
Config(docRoot)
User ID of the TclHttpd process. The default is 50.
(UNIX only.)
-uid uid Config(uid)
Group ID of the TclHttpd process. The default is 100.
(UNIX only.)
-gid gid Config(gid)
Webmaster e-mail. The default is webmaster. -webmaster email Config(webmaster)
Configuration file. The default is tclhttpd.rc. -config filename Config(file)
Additional directory to add to the auto_path.
-library
directory
Config(library)
Server Name and Port
The name and port parameters define how your server is known to Web browsers. The URLs that
access your server begin with:
http://name:port/
If the port number is 80, you can leave out the port specification. The call that starts the server using
these parameters is found in httpd.tcl as:
Httpd_Server $Config(name) $Config(port) $Config(ipaddr)
Specifying the IP address is necessary only if you have several network interfaces (or several IP
addresses assigned to one network interface) and want the server to listen to requests on a particular
network address. Otherwise, by default, server accepts requests from any network interface.
User and Group ID
The user and group IDs are used on UNIX systems with the setuid and setgid system calls. This lets
you start the server as root, which is necessary to listen on port 80, and then switch to a less privileged
user account. If you use Tcl+HTML templates that cache the results in HTML files, then you need to
pick an account that can write those files. Otherwise, you may want to pick a very unprivileged
account.
The setuid function is available through the TclX (Extended Tcl) id command, or through a setuid
extension distributed with TclHttpd under the src directory. If do not have either of these facilities
available, then the attempt to change user ID gracefully fails. See the README file in the src directory
for instructions on compiling and installing the extensions found there.
Webmaster Email
The Webmaster e-mail address is used for automatic error reporting in the case of server errors. This is
defined in the configuration file with the following command:
Doc_Webmaster $Config(webmaster)
If you call Doc_Webmaster with no arguments, it returns the e-mail address you previously defined.
This is useful when generating pages that contain mailto: URLs with the Webmaster address.
Document Root
The document root is the directory that contains the static files, templates, CGI scripts, and so on that
make up your Web site. By default the httpd.tcl script uses the htdocs directory next to the directory
containing httpd.tcl. It is worth noting the trick used to locate this directory:
file join [file dirname [info script]] ../htdocs
The info script command returns the full name of the http.tcl script, file dirname computes its
directory, and file join finds the adjacent directory. The path ../htdocs works with file join on
any platform. The default location of the configuration file is found in a similar way:
file join [file dirname [info script]] tclhttpd.rc
The configuration file initializes the document root with this call:
Doc_Root $Config(docRoot)
If you need to find out what the document root is, you can call Doc_Root with no arguments and it
returns the directory of the document root. If you want to add additional document trees into your Web
site, you can do that with a call like this in your configuration file:
Doc_AddRoot directory urlprefix
Other Document Settings
The Doc_IndexFile command sets a pattern used to find the index file in a directory. The command
used in the default configuration file is:
Doc_IndexFile index.{htm,html,tml,subst}
If you invent other file types with different file suffixes, you can alter this pattern to include them. This
pattern will be used by the Tcl glob command.
The Doc_PublicHtml command is used to define "home directories" on your HTML site. If the URL
begins with ~username, then the Web server will look under the home directory of username for a
particular directory. The command in the default configuration file is:
Doc_PublicHtml public_html
For example, if my home directory is /home/welch, then the URL ~welch maps to the directory
/home/welch/public_html. If there is no Doc_PublicHtml command, then this mapping does not
occur.
You can register two special pages that are used when the server encounters an error and when a user
specifies an unknown URL. The default configuration file has these commands:
Doc_ErrorPage error.html
Doc_NotFoundPage notfound.html
These files are treated like templates in that they are passed through subst in order to include the error
information or the URL of the missing page. These are pretty crude templates compared to the
templates described earlier. You can count only on the Doc and Httpd arrays being defined. Look at
the Doc_SubstSystemFile in doc.tcl for the truth about how these files are processed.
Document Templates
The template mechanism has two main configuration options. The first specifies an additional library
directory that contains your application-specific scripts. This lets you keep your application-specific
files separate from the TclHttpd implementation. The command in the default configuration file
specifies the libtml directory of the document tree:
Doc_TemplateLibrary [file join $Config(docRoot) libtml]
You can also specify an alternate Tcl interpreter in which to process the templates. The default is to
use the main interpreter, which is named {} according to the conventions described in Chapter 19.
Doc_TemplateInterp {}
Log Files
The server keeps standard format log files. The Log_SetFile command defines the base name of the
log file. The default configuration file uses this command:
Log_SetFile /tmp/log$Config(port)_
By default the server rotates the log file each night at midnight. Each day's log file is suffixed with the
current date (e.g., /tmp/logport_990218.) The error log, however, is not rotated, and all errors are
accumulated in /tmp/logport_error.
The log records are normally flushed every few minutes to eliminate an extra I/O operation on each
HTTP transaction. You can set this period with Log_FlushMinutes. If minutes is 0, the log is flushed
on every HTTP transaction. The default configuration file contains:
Log_FlushMinutes 1
CGI Directories
You can register a directory that contains CGI programs with the Cgi_Directory command. This
command has the interesting effect of forcing all files in the directory to be executed as CGI scripts, so
you cannot put normal HTML files there. The default configuration file contains:
Cgi_Directory /cgi-bin
This means that the cgi-bin directory under the document root is a CGI directory. If you supply
another argument to Cgi_Directory, then this is a file system directory that gets mapped into the URL
defined by the first argument. You can also put CGI scripts into other directories and use the .cgi
suffix to indicate that they should be executed as CGI scripts.
The cgi.tcl file has some additional parameters that you can tune only by setting some elements of
the Cgi Tcl array. See the comments in the beginning of that file for details.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartII. Advanced Tcl
Chapter 19. Multiple Interpreters and Safe-Tcl
This chapter describes how to create more than one Tcl interpreter in your application. A child
interpreter can be made safe so that it can execute untrusted scripts without compromising your
application or your computer. Command aliases, hidden commands, and shared I/O channels enable
communication among interpreters. Tcl command described is: interp.
Safe-Tcl was invented by Nathaniel Borenstein and Marshall Rose so that they could send Tcl scripts
via e-mail and have the recipient safely execute the script without worry of viruses or other attacks.
Safe-Tcl works by removing dangerous commands like exec and open that would let an untrusted
script damage the host computer. You can think of this restricted interpreter as a "padded cell" in
which it is safe to execute untrusted scripts. To continue the analogy, if the untrusted code wants to do
anything potentially unsafe, it must ask permission. This works by adding additional commands, or
aliases, that are implemented by a different Tcl interpreter. For example, a safeopen command could
be implemented by limiting file space to a temporary directory that is deleted when the untrusted code
terminates.
The key concept of Safe-Tcl is that there are two Tcl interpreters in the application, a trusted one and
an untrusted (or "safe") one. The trusted interpreter can do anything, and it is used for the main
application (e.g., the Web browser or e-mail user interface). When the main application receives a
message containing an untrusted script, it evaluates that script in the context of the untrusted
interpreter. The restricted nature of the untrusted interpreter means that the application is safe from
attack. This model is much like user mode and kernel mode in a multiuser operating system like UNIX
or Windows/NT. In these systems, applications run in user mode and trap into the kernel to access
resources like files and the network. The kernel implements access controls so that users cannot read
and write each other's files, or hijack network services. In Safe-Tcl the application implements access
controls for untrusted scripts.
The dual interpreter model of Safe-Tcl has been generalized in Tcl 7.5 and made accessible to Tcl
scripts. A Tcl script can create other interpreters, destroy them, create command aliases among them,
share I/O channels among them, and evaluate scripts in them.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter19. Multiple Interpreters and Safe-Tcl
The interp Command
The interp command is used to create and manipulate interpreters. The interpreter being created is
called a slave, and the interpreter that creates it is called the master. The master has complete control
over the slave. The interp command is summarized in Table 19-1.
Table 19-1. The interp command.
interp aliases slave Lists aliases that are defined in slave.
interp alias slave cmd1 Returns the target command and arguments for the alias
cmd1 in slave.
interp alias slave cmd1 master
cmd2 arg ...
Defines cmd1 in slave that is an alias to cmd2 in master
with additional args.
interp create ?-safe? slave Creates an interpreter named slave.
interp delete slave Destroys interpreter slave.
interp eval slave cmd args ... Evaluates cmd and args in slave.
interp exists slave Returns 1 if slave is an interpreter, else 0.
interp expose slave cmd Exposes hidden command cmd in slave.
interp hide slave cmd Hides cmd from slave.
interp hidden slave Returns the commands hidden from slave.
interp invokehidden slave cmd arg
...
Invokes hidden command cmd and args in slave.
interp issafe slave Returns 1 if slave was created with -safe flag.
interp marktrusted slave Clears the issafe property of slave.
interp share master file slave Shares the I/O descriptor named file in master with
slave.
command is removed so that untrusted scripts cannot access the network. The exit, source, and load
commands are removed so that an untrusted script cannot harm the hosting application. Note that
commands like puts and gets are not removed. A safe interpreter can still do I/O, but it cannot create
an I/O channel. We will show how to pass an I/O channel to a child interpreter on page 283.
The initial state of a safe interpreter is very safe, but it is too limited. The only thing a safe interpreter
can do is compute a string and return that value to the parent. By creating command aliases, a master
can give a safe interpreter controlled access to resources. A security policy implements a set of
command aliases that add controlled capabilities to a safe interpreter. We will show, for example, how
to provide limited network and file system access to untrusted slaves. Tcl provides a framework to
manage several security policies, which is described in Chapter 20.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter19. Multiple Interpreters and Safe-Tcl
Command Aliases
A command alias is a command in one interpreter that is implemented by a command in another
interpreter. The master interpreter installs command aliases in its slaves. The command to create an
alias has the following general form:
interp alias slave cmd1 target cmd2 ?arg arg ...?
This creates cmd1 in slave that is an alias for cmd2 in target. When cmd1 is invoked in slave, cmd2 is
invoked in target. The alias mechanism is transparent to the slave. Whatever cmd2 returns, the slave
sees as the return value of cmd1. If cmd2 raises an error, the error is propagated to the slave.
Name the current interpreter with {}.
If target is the current interpreter, name it with {}. The empty list is the way to name yourself as the
interpreter. This is the most common case, although target can be a different slave. The slave and
target can even be the same interpreter.
The arguments to cmd1 are passed to cmd2, after any additional arguments to cmd2 that were specified
when the alias was created. These hidden arguments provide a safe way to pass extra arguments to an
alias. For example, it is quite common to pass the name of the slave to the alias. In Example 19-3,
exit in the interpreter foo is an alias that is implemented in the current interpreter (i.e., {}). When the
slave executes exit, the master executes:
interp delete foo
For example, suppose there is an alias to open files. The alias does some checking and then invokes
the hidden open command. An untrusted script might pass [exit] as the name of the file to open in
order to create mischief. The untrusted code is hoping that the master will accidentally eval the
filename and cause the application to exit. This attack has nothing to do with opening files; it just
hopes for a poor alias implementation. Example 19-6 shows an alias that is not subject to this attack:
Example 19-6 Substitutions and hidden commands.
interp alias slave open {}safeopen slave
proc safeopen {slave filename {mode r}} {
# do some checks, then...
interp invokehidden $slave open $filename $mode
}
interp eval slave {open \[exit\]}
The command in the slave starts out as:
open \[exit\]
The master has to quote the brackets in its interp eval command or else the slave will try to invoke
exit because of command substitution. Presumably exit isn't defined, or it is defined to terminate the
slave. Once this quoting is done, the value of filename is [exit] and it is not subject to substitutions.
It is safe to use $filename in the interp invokehidden command because it is only substituted once,
in the master. The hidden open command also gets [exit] as its filename argument, which is never
evaluated as a Tcl command.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter19. Multiple Interpreters and Safe-Tcl
I/O from Safe Interpreters
A safe child interpreter cannot open files or network sockets directly. An alias can create an I/O
channel (i.e., open a file or socket) and give the child access to it. The parent can share the I/O channel
with the child, or it can transfer the I/O channel to the child. If the channel is shared, both the parent
and the child can use it. If the channel is transferred, the parent no longer has access to the channel. In
general, transferring an I/O channel is simpler, but sharing an I/O channel gives the parent more
control over an unsafe child. The differences are illustrated in Example 19-7 and Example 19-9.
There are three properties of I/O channels that are important to consider when choosing between
sharing and transferring: the name, the seek offset, and the reference count.
The name of the I/O channel (e.g., file4) is the same in all interpreters. If a parent transfers a
channel to a child, it can close the channel by evaluating a close command in the child.
Although names are shared, an interpreter cannot attempt I/O on a channel to which it has not
been given access.
The seek offset of the I/O channel is shared by all interpreters that share the I/O channel. An I/O
operation on the channel updates the seek offset for all interpreters that share the channel. This
means that if two interpreters share an I/O channel, their output will be cleanly interleaved in the
channel. If they both read from the I/O channel, they will get different data. Seek offsets are
explained in more detail on page 114.
A channel has a reference count of all interpreters that share the I/O channel. The channel
remains open until all references are closed. When a parent transfers an I/O channel, the
reference count stays the same. When a parent shares an I/O channel, the reference count
increments by one. When an interpreter closes a channel with close, the reference count is
decremented by one. When an interpreter is deleted, all of its references to I/O channels are
removed.
The syntax of commands to share or transfer an I/O channel is:
interp share interp1 chanName interp2
interp transfer interp1 chanName interp2
In these commands, chanName exists in interp1 and is being shared or transferred to interp2. As
with command aliases, if interp1 is the current interpreter, name it with {}.
The following example creates a temporary file for an unsafe interpreter. The file is opened for reading
and writing, and the slave can use it to store data temporarily.
Example 19-7 Opening a file for an unsafe interpreter.
proc TempfileAlias {slave} {
set i 0
while {[file exists Temp$slave$i]} {
incr i
}
set out [open Temp$slave$i w+]
interp transfer {}$out $slave
return $out
}
proc TempfileExitAlias {slave} {
foreach file [glob -nocomplain Temp$slave*] {
file delete -force $file
}
interp delete $slave
}
interp create -safe foo
interp alias foo Tempfile {}TempfileAlias foo
interp alias foo exit {}TempfileExitAlias foo
The TempfileAlias procedure is invoked in the parent when the child interpreter invokes Tempfile.
TempfileAlias returns the name of the open channel, which becomes the return value from
Tempfile, so the child knows the name of the I/O channel. TempfileAlias uses interp transfer to
pass the I/O channel to the child, so the child has permission to access the I/O channel. In this
example, it would also work to invoke the hidden open command to create the I/O channel directly in
the slave.
Example 19-7 is not fully safe because the unsafe interpreter can still overflow the disk or create a
million files. Because the parent has transferred the I/O channel to the child, it cannot easily monitor
the I/O activity by the child. Example 19-9 addresses these issues.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter19. Multiple Interpreters and Safe-Tcl
The Safe Base
An safe interpreter created with interp create -safe has no script library environment and no way
to source scripts. Tcl provides a safe base that extends a raw safe interpreter with the ability to source
scripts and packages, which are described in Chapter 12. The safe base also defines an exit alias that
terminates the slave like the one in Example 19-7. The safe base is implemented as Tcl scripts that are
part of the standard Tcl script library. Create an interpreter that uses the safe base with
safe::interpCreate:
safe::interpCreate foo
The safe base has source and load aliases that only access directories on an access path defined by
the master interpreter. The master has complete control over what files can be loaded into a slave. In
general, it would be all right to source any Tcl program into an untrusted interpreter. However,
untrusted scripts might learn things from the error messages they get by sourcing arbitrary files. The
safe base also has versions of the package and unknown commands that support the library facility.
Table 19-3 lists the Tcl procedures in the safe base:
Table 19-3. The safe base master interface.
safe::interpCreate ?slave? ?
options?
Creates a safe interpreter and initialize the security
policy mechanism.
safe::interpInit slave ?options? Initializes a safe interpreter so it can use security
policies.
safe::interpConfigure slave ?
options?
Options are -accessPath pathlist, -nostatics, -
deleteHook script, -nestedLoadOk.
safe::interpDelete slave Deletes a safe interpreter.
safe::interpAddToAccessPath slave
directory
Adds a directory to the slave's access path.
safe::interpFindInAccessPath
Maps from a directory to the token visible in the slave
for that directory.
safe::setLogCmd ?cmd arg ... ? Sets or queries the logging command used by the safe
base.
Table 19-4 lists the aliases defined in a safe interpreter by the safe base.
Table 19-4. The safe base slave aliases.
source
Loads scripts from directories in the access path.
load
Loads binary extensions from the slaves access path.
file
Only the dirname, join, extension, root, tail, pathname, and split operations are
allowed.
exit
Destroys the slave interpreter.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter19. Multiple Interpreters and Safe-Tcl
Security Policies
A security policy defines what a safe interpreter can do. Designing security policies that are secure is
difficult. If you design your own, make sure to have your colleagues review the code. Give out prizes
to folks who can break your policy. Good policy implementations are proven with lots of review and
trial attacks. The good news is that Safe-Tcl security policies can be implemented in relatively small
amounts of Tcl code. This makes them easier to analyze and get correct. Here are a number of rules of
thumb:
Small policies are better than big, complex policies. If you do a lot of complex processing to
allow or disallow access to resources, chances are there are holes in your policy. Keep it simple.
Never eval arguments to aliases. If an alias accepts arguments that are passed by the slave, you
must avoid being tricked into executing arbitrary Tcl code. The primary way to avoid this never
is to eval arguments that are passed into an alias. Watch your expressions, too. The expr
command does an extra round of substitutions, so brace all your expressions so that an attacker
cannot pass [exit] where you expect a number!
Security policies do not compose. Each time you add a new alias to a security policy, it changes
the nature of the policy. Even if alias1 and alias2 are safe in isolation, there is no guarantee
that they cannot be used together to mount an attack. Each addition to a security policy requires
careful review.
Limited Socket Access
The Safesock security policy provides limited socket access. The policy is designed around a simple
table of allowed hosts and ports. An untrusted interpreter can connect only to addresses listed in the
table. For example, I would never let untrusted code connect to the sendmail, ftp, or telnet ports on my
hosts. There are just too many attacks possible on these ports. On the other hand, I might want to let
untrusted code fetch a URL from certain hosts, or connect to a database server for an intranet
application. The goal of this policy is to have a simple way to specify exactly what hosts and ports a
slave can access. Example 19-8 shows a simplified version of the Safesock security policy that is
distributed with Tcl 8.0.
Example 19-8 The Safesock security policy.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter20. Safe-Tk and the Browser Plugin
Tk in Child Interpreters
A child interpreter starts out with just the core Tcl commands. It does not include Tk or any other
extensions that might be available to the parent interpreter. This is true whether or not the child
interpreter is declared safe. You add extensions to child interpreters by using a form of the load
command that specifies an interpreter:
load {} Tk child
Normally, load takes the name of the library file that contains the extension. In this case, the Tk
package is a static package that is already linked into the program (e.g., wish or the plugin), so the file
name is the empty string. The load command calls the Tk initialization procedure to register all the
Tcl commands provided by Tk.
Embedding Tk Windows
By default, a slave interpreter that loads Tk gets a new top-level window. Wish supports a -use
command line option that directs Tk to use an existing window as dot. You can use this to embed an
application within another. For example, the following commands run a copy of Wish that uses the
.embed toplevel as its main window:
toplevel .embed
exec wish -use [winfo id .embed] somescript.tcl &
More often embedding it is used with child interpreters. If the interpreter is not safe, you can set the
argv and argc variables in the slave before loading Tk:
interp create trustedTk
interp eval trustedTk \
[list set argv [list -use [winfo id .embed]]]
Note that HTML tag parameters are case sensitive. Your Tclet may want to map all the parameter
names to lowercase for convenience:
foreach {name value}[array get embed_args] {
set embed_args([string tolower $name]) $value
}
The plugin array has version, patchLevel, and release elements that identify the version and
release date of the plugin implementation.
Example Plugins
The plugin home page is a great place to find Tclet examples. There are several plugins done by the
Tcl/Tk team at Sunlabs, plus links to a wide variety of Tclets done on the Net.
http://www.scriptics.com/plugin/
I wrote a cute little plugin that calculates the effective wheel diameter of multigear bicycles. Brian
Lewis, who built the Tcl 8.0 byte-code compiler, explained to me the concept and how important this
information is to bicycle enthusiasts. I put together a Tclet that displays the gear combinations on a Tk
canvas and lets you change the number of gears and their size. You can find the result at:
http://www.beedub.com/plugin/bike.html
Setting Up the plugin
There are plugin versions for UNIX, Windows, and Macintosh. The installation scripts take care of
installing the plugin in the correct locations, which are described in the next sections about each
platform. The plugin and the security policies that are distributed with it will continue to be updated.
You should get the latest version from the Tcl/Tk Web site, http://www.scriptics.com/plugin/. If that
URL changes, you can find an up-to-date pointer under http://www.beedub.com/book/. The plugin
may already be installed at your site. Bring up the About Plugins dialog under Help in your browser
to see if the Tcl/Tk plugin is listed.
The plugin is composed of the following parts, although the location of these files varies somewhat
among platforms:
The plugin shared libraries (i.e., DLLs). The Web browser dynamically loads the plugin
implementation when it needs to execute a Tclet embedded in a Web page. There is a standard
directory that the browser scans for the libraries that implement plugins.
The Tcl/Tk script libraries. The plugin needs the standard script libraries that come with Tcl and
Tk, plus it has its own scripts that complete its implementation. Each platform has a plugin script
directory with these subdirectories: tcl, tk, plugin, config, safetcl, and utils. The plugin
implementation is in the plugin directory.
The security policies. These are kept in a safetcl directory that is a peer of the Tcl script library.
The trust configuration. This defines what Tclets can use which security policies. This is in a
config directory that is a peer of the Tcl script library.
Local hooks. Local customization is supported by two hooks, siteInit and siteSafeInit. The
siteInit procedure is called from the plugin when it first loads, and siteSafeInit is called
when each applet is initialized. It is called with the name of the slave interpreter and the list of
arguments from the <EMBED> tag. You can provide these as scripts that get loaded from the
auto_path of the master interpreter. Chapter 12 describes how to manage script libraries found
in the auto_path. The plugin also sources a personal start up script in which you can define
siteInit and siteSafeInit. This script is ~/.pluginrc on UNIX and plugin/tclplugin.rc
on Windows and Macintosh.
Plugin Environment Variables
The plugin can be configured to run Tcl/Tk directly in the browser process, or to run with a small
library in the browser that communicates with an external wish application. The default is to run in
process. The advantage of the external process is that you can use custom wish shells that can load
various extensions. You control this configuration with the environment variables listed in Table 20-2.
Table 20-2. Plugin Environment Variables
TCL_PLUGIN_INPROCESS
If this is defined and 1, then Tcl/Tk is loaded directly into the browser.
Otherwise the plugin forks wish.
TCL_PLUGIN_WISH
This names the wish executable used to run Tclets. This must be version
8.0 or higher to properly support embedding.
TCL_PLUGIN_CONSOLE
If this is set to 1, then a console is opened when a Tclet is loaded. The
console prompts for Tcl commands that are evaluated in the master
interpreter. If the value is something other than 1, then it is taken to be a
script (e.g., TkCon) that implements a console.
TCL_PLUGIN_LOGWINDOW
If 1, various status messages from the plugin are displayed.
TCL_PLUGIN_LOGFILE
If defined, this file captures the log output.
UNIX Configuration
Netscape looks in each user's ~/.netscape/plugins for the shared libraries that implement plugins. It
also looks in a plugins directory under its main directory, which will vary from site to site. You can
define a search path for plugins with the NXP_PLUGIN_PATH environment variable. The plugin script
library is in ~/.tclplug/2.0/plugin. You can change this default location by setting the
TCL_PLUGIN_DIR environment variable. Once the plugin finds its script library, it assumes the Tcl and
Tk script directories, the security policies, and the trust map are in peer directories.
Windows Configuration
The default location for plugins is in the PLUGINS directory of the Netscape installation. The Tcl/Tk
plugin also works in Internet Explorer from the same location. The script libraries are found under
C:\TCLPLUG\2.0. You can change this location by setting the registry variable:
Software\Sun\Tcl Plugin\2.0\Directory
Macintosh Configuration
Installing the plugin on the Macintosh is a three-step process. In step one you unpack the initial
download file, which creates another installer file. In step two you run that installer, which puts files
into two locations. You specify one folder that will hold the documentation and the Netscape plugin.
The rest of the plugin goes into a plugin folder under the Tool Command Language folder in your
system's Extensions folder. It does not conflict with any existing Tcl/Tk installations you may already
have. In step three, you complete the process by moving the Tcl Plugin file into the Plugins
directory of your Netscape installation.
The current version of the Macintosh plugin is limited to Netscape. Version 4 works better than
Netscape 3, and Internet Explorer is not supported at all.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter20. Safe-Tk and the Browser Plugin
Security Policies and Browser Plugin
Tclets run in a safe interpreter that is set up with the safe base facilities described on page 284. This
limits a Tclet to a display-only application. To do something more interesting, you have to grant the
Tclet more privilege. The extra functions are bundled together into a security policy, which is
implemented as a set of command aliases. Unlike a Java applet, a Tclet can choose from different
security policies. A few standard security policies are distributed with the plugin, and these are
described below. You can also create custom security policies to support intranet applications. You
can even choose to grant certain Tclets the full power of Tcl/Tk. The policy command is used to
request a security policy:
policy name
The policies that are part of the standard plugin distribution are described below. The home, inside,
and outside policies all provide limited network access. They differ in what set of hosts are
accessible. The default trust configuration lets any Tclet request the home, inside or outside policy.
home. This provides a socket and fconfigure command that are limited to connecting to the
host from which the Tclet was downloaded. You can specify an empty string for the host
argument to socket to connect back to the home host. This policy also supports open and file
delete that are similar to the Tempfile policy shown in Example 19-9 on page 288. This
provides limited local storage that is inside a directory that is, by default, private to the Tclet.
Files in the private directory persist after the Tclet exits, so it can maintain long term state. Tclets
from the same server can share the directory by putting the same prefix=partialurl argument
in their EMBED tag. The partialurl must be a prefix of the Tclet's URL. Finally, the home policy
automatically provides a browser package that is described later.
inside. This is just like the home policy, except the site administrator controls a table of hosts
and ports to which untrusted slaves can connect with socket. A similar set of tables control what
URLs can be accessed with the browser package. This is similar to the Safesock policy is
shown in Example 19-8 on page 286. The set of hosts is supposed to be inside the firewall. The
local file storage used by this policy is distinct from that used by the home and outside policies.
This is true even if Tclets try to share by using the prefix=partialurl parameter.
outside. This is just like the home and inside policies, except that the set of hosts is configured
to be outside the firewall. The local file storage used by this policy is distinct from that used by
the home and inside policies.
trusted. This policy restores all features of Tcl and Tk. This policy lets you launch all your Tcl
and Tk applications from the Web browser. The default trust map settings do not allow this for
any Tclet. The trust map configuration is described later.
javascript. This policy provides a superset of the browser package that lets you invoke
arbitrary Javascript and to write HTML directly to frames. This does not have the limited socket
or temporary file access that the home, inside, and outside policies have. However, the
javascript policy places no restrictions on the URLs you can fetch, plus it lets Tclets execute
Javascript, which may have its own security risks. The default trust map settings do not allow
this for any Tclet.
The Browser Package
The browser package is bundled with several of the security policies. It makes many features of the
Web browser accessible to Tclets. They can fetch URLs and display HTML in frames. However, the
browser package has some risks associated with it. HTTP requests can be used to transmit
information, so a Tclet using the policy could leak sensitive information if it can fetch a URL outside
the firewall. To avoid information leakage, the inside, outside, and home policies restrict the URL
that can be fetched with browser::getURL. Table 20-3 lists the aliases defined by the browser
package.
Table 20-3. Aliases defined by the browser package.
browser::status string Display string in the browser status window.
browser::getURL url ?timeout? ?
newcallback? ?writecallback? ?
endcallback?
Fetches url, if allowed by the security policy. The
callbacks occur before, during, and after the url data is
returned.
browser::displayURL url frame Causes the browser to display url in frame.
browser::getForm url data ?raw? ?
timeout? ?newcallback? ?
writecallback? ?endcallback?
Posts data to url. The callbacks are the same as for
browser::getURL. If raw is 0, then data is a name value
list that gets encoded automatically. Otherwise it is
assume to be encoded already.
browser::displayForm url frame
data ?raw?
Posts data to url and displays the result in frame. The
raw argument is the same as in browser::getForm.
The browser::getURL function uses the browser's built-in functions, so it understands proxies and
supports ftp:, http:, and file: urls. Unfortunately, the browser::getURL interface is different than
the http::geturl interface. It uses a more complex callback scheme that is due to the nature of the
browser's built-in functions. If you do not specify any callbacks, then the call blocks until all the data
is received, and then that data is returned. The callback functions are described in Table 20-4.
Table 20-4. The browser::getURL callbacks.
newcallback name stream
url mimetype
datemodified size
This is called when data starts to arrive from url. The name identifies
the requesting Tclet, and the stream identifies the connection. The
mimetype, datemodified, and size parameters are attributes of the
returned data.
writecallback name
stream size data
This is called when size bytes of data arrive for Tcllet name over
stream.
endcallback name stream
reason data
This is called when the request has completed, although there may be
some final bytes in data. The reason is one of: EOF, NETWOR_ERROR,
USER_BREAK, or TIMEOUT.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter20. Safe-Tk and the Browser Plugin
Configuring Security Policies
There are three aspects to the plugin security policy mechanism: policies, features, and trust maps. A
policy is an umbrella for a set of features that are allowed for certain Tclets based on the trust map. A
feature is a set of commands and aliases that are defined for a safe interpreter that requests a policy.
The trust map is kind of filter based on the URL of the Tclet. In the future, trust may bet determined by
digital signatures instead of URLs. The trust map determines if a Tclet can request a given policy.
Security Policies are configured for each client.
Remember that the configuration files affect the client machine, which is the workstation that runs the
Web browser. If you create Tclets that require custom security policies, you have the burden of
distributing the configuration files to clients that will use your Tclets. You also have the burden of
convincing them your security policy is safe!
The config/plugin.cfg File
The main configuration file is the config/plugin.cfg file in the plugin distribution. This file lists
what features are supported by the plugin, and it defines the URL filters for the trust map.
The configuration file is defined into sections with a section command. The policies section defines
which Tclets can use which security policies. For example, the default configuration file contains these
lines in the policies section:
section policies
allow home
disallow intercom
disallow inside
disallow outside
disallow trusted
allow javascript ifallowed trustedJavaScriptURLS \
$originURL
This configuration grants all Tclets the right to use the home policy, disallows all Tclets from using the
intercom, inside, outside, and trusted policies, and grants limited access to the javascript
policy.
If you are curious, the config files are almost Tcl, but not quite. I lost an argument about that one, so
these are stylized configuration files that follow their own rules. For example, the originURL variable
is not defined in the config file, but is a value that is tested later when the Tclet is loaded. I'll just give
examples here and you can peer under the covers if you want to learn how they are parsed.
The ifallowed clause depends on another section to describe the trust mapping for that policy. For the
javascript policy, the config/plugin.cfg file contains:
section trustedJavascriptURLs
allow http://sunscript.sun.com:80/plugin/javascript/*
Unfortunately this server isn't running anymore, so you may want to add the Scriptics web server to
your own configuration:
allow http://www.scriptics.com:80/plugin/javascript/*
You can use a combination of allow and disallow rules in a section. The arguments to allow and
disallow are URL string match patterns, and they are processed in order. For example, you could
put a liberal allow rule followed by disallow rules that restrict access, or vice versa. It is probably
safest to explicitly list each server that you trust.
Policy Configuration Files
Each security policy has a configuration file associated with it. For example, the outside policy uses
the file outside.cfg file in the config directory. This file specifies what hosts and ports are
accessible to Tclets using the outside policy. For the inside and outside policies, the configuration
files are similar in spirit to the safesock array used to configure the Safesock security policy shown
on page 286. There are a set of allowed hosts and ports, and a set of excluded hosts. The excluded
hosts are an exception list. If a host matches the include set but also matches the exclude set, it is not
accessible. There is an include and exclude set for URLs that affect browser::geturl. The settings
from the Tempfile policy shown on page 288 are also part of the home, inside, and outside
configuration files. The configuration files are well commented, and you should read through them to
learn about the configuration options for each security policy.
Security Policy Features
The aliases that make up a security policy are organized into sets called features. The features are
listed in the main config/plugin.cfg configuration file:
variable featuresList {url stream network persist unsafe}
In turn, each security policy configuration file lists what features are part of the policy. For example,
the config/home.cfg file lists these features:
section features
allow url
allow network
allow persist unless {[string match {UNKNOWN *}\
[getattr originURL]]}
Each feature is implemented in a file in the safetcl directory of the distribution. For example, the url
feature is implemented in safetcl/url.tcl. The code in these files follows some conventions in
order to work with the configuration mechanism. Each one is implemented inside a namespace that is
a child of the safefeature namespace (e.g., safefeature::url). It must implement an install
procedure that is called to initialize the feature for a new Tclet. It is inside this procedure that the
various allow/disallow rules are checked. The cfg::allowed command supports the rule language
used in the .cfg files.
Creating New Security Policies
This book does not describe the details of the configuration language or the steps necessary to create a
new security policy. There are several manual pages distributed with the plugin that explain these
details. They can be found on the web at:
http://www.scriptics.com/plugin/man/
If you are serious about tuning the existing security policies or creating new ones, you should read the
existing feature implementations in detail. As usual, modifying a working example is the best way to
proceed! I think it is a very nice property of the plugin that its security policies are implemented in Tcl
source code that is clearly factored out from the rest of the Tcl/Tk and plugin implementation.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Part III: Tk Basics
Part III introduces Tk, the toolkit for building graphical user interfaces. The Tcl command
interface to Tk makes it quick and easy to build powerful user interfaces. Tk is portable and your
user interface code can work unchanged on UNIX, Windows, and the Macintosh.
Chapter 21 describes the basic concepts of Tk and provides an overview of its facilities.
Chapter 22 illustrates Tk with three example programs including a browser for the examples
from this book. These examples use facilities that are described in more detail in later chapters.
Geometry managers implement the layout of a user interface. Chapters Chapter 23, Chapter 24,
and Chapter 25 describe the pack, grid, and place geometry managers. The packer and gridder
are general-purpose managers that use constraints to create flexible layouts with a small amount
of code. The placer is a special purpose geometry manager that can be used for special effects.
Chapter 26 describes event bindings that associate Tcl commands with events like keystrokes
and mouse motion.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIII. Tk Basics
Chapter 21. Tk Fundamentals
This chapter introduces the basic concepts used in the Tk graphical user interface toolkit. Tk adds
about 35 Tcl commands that let you create and manipulate widgets in a graphical user interface. Tk
works with the X window system, Windows, and Macintosh. The same script can run unchanged on
all of these major platforms.
Tk is a toolkit for programming graphical user interfaces. It was designed for the X window system
used on UNIX systems, and it was ported later to the Macintosh and Windows environments. Tk
shares many concepts with other windowing toolkits, but you do not need to know much about
graphical user interfaces to get started with Tk.
Tk provides a set of Tcl commands that create and manipulate widgets. A widget is a window in a
graphical user interface that has a particular appearance and behavior. The terms widget and window
are often used interchangeably. Widget types include buttons, scrollbars, menus, and text windows. Tk
also has a general-purpose drawing widget called a canvas that lets you create lighter-weight items
such as lines, boxes, and bitmaps. The Tcl commands added by Tk are summarized at the end of this
chapter.
Tk widgets are organized in a hierarchy. To an application, the window hierarchy means that there is a
primary window, and inside that window there can be a number of children windows. The children
windows can contain more windows, and so on. Just as a hierarchical file system has directories (i.e.,
folders) that are containers for files and directories, a hierarchical window system uses windows as
containers for other windows. The hierarchy affects the naming scheme used for Tk widgets as
described later, and it is used to help arrange widgets on the screen.
Widgets are under the control of a geometry manager that controls their size and location on the
screen. Until a geometry manager learns about a widget, it will not be mapped onto the screen and you
will not see it. Tk has powerful geometry managers that make it very easy to create nice screen
layouts. The main trick with any geometry manager is that you use frame widgets as containers for
other widgets. One or more widgets are created and then arranged in a frame by a geometry manager.
By putting frames within frames you can create complex layouts. There are three different geometry
managers you can use in Tk: grid, pack, and place. The Tk geometry managers are discussed in
detail in Chapters 23, 24, and 25.
A Tk-based application has an event-driven control flow, like most window system toolkits. The Tk
widgets handle most events automatically, so programming your application remains simple. For
specialized behaviors, you use the bind command to register a Tcl command that runs when an event
occurs. There are lots of events, including mouse motion, keystrokes, window resize, and window
destruction. You can also define virtual events, like Cut and Paste, that are caused by different events
on different platforms. Bindings are discussed in detail in Chapter 26. Chapter 16 describes I/O events
and the Tcl event loop, while Chapter 47 describes C programming and the event loop.
Event bindings are grouped into classes, which are called bindtags. The bindtags command
associates a widget with an ordered set of bindtags. The level of indirection between the event
bindings and the widgets creates a flexible and powerful system for managing events. You can create
your own bindtags and dynamically change the bindtags for a widget to support mode changes in your
application.
A concept related to binding is focus. At any given time, one of the widgets has the input focus, and
keyboard events are directed to it. There are two general approaches to focusing: give focus to the
widget under the mouse, or explicitly set the focus to a particular widget. Tk provides commands to
change focus so you can implement either style of focus management. To support modal dialog boxes,
you can forcibly grab the focus away from other widgets. Chapter 36 describes focus, grabs, and
dialogs.
The basic structure of a Tk script begins by creating widgets and arranging them with a geometry
manager, and then binding actions to the widgets. After the interpreter processes the commands that
initialize the user interface, the event loop is entered and your application begins running.
If you use wish interactively, it creates and displays an empty main window and gives you a command-
line prompt. With this interface, your keyboard commands are handled by the event loop, so you can
build your Tk interface gradually. As we will see, you will be able to change virtually all aspects of
your application interactively.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter21. Tk Fundamentals
Hello, World! in Tk
Our first Tk script is very simple. It creates a button that prints "Hello, World!" to standard output
when you press it. Above the button widget is a title bar that is provided by the window manager,
which in this case is twm under X windows:
Example 21-1 "Hello, World!" Tk program.
#!/usr/local/bin/wish
button .hello -text Hello \
-command {puts stdout "Hello, World!"}
pack .hello -padx 20 -pady 10
The first line identifies the interpreter for the script:
#!/usr/local/bin/wish
This special line is necessary if the script is in a file that will be used like other UNIX command files.
Chapter 2 describes how to set up scripts on different platforms.
There are two Tcl commands in the script: one to create the button, and one to make it visible on the
display. The button command creates an instance of a button:
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter21. Tk Fundamentals
Configuring Tk Widgets
Example 21-1 illustrates a style of named parameter passing that is prevalent in the Tk commands.
Pairs of arguments specify the attributes of a widget. The attribute names begin with -, such as -text,
and the next argument is the value of that attribute. Even the simplest Tk widget can have a dozen or
more attributes that can be specified this way, and complex widgets can have 20 or more attributes.
However, the beauty of Tk is that you need to specify only the attributes for which the default value is
not good enough. This is illustrated by the simplicity of the Hello, World example.
Finally, each widget instance supports a configure operation, which can be abbreviated to config,
that can query and change these attributes. The syntax for config uses the same named argument pairs
used when you create the widget. For example, we can change the background color of the button to
red even after it has been created and mapped onto the screen:
.hello config -background red
Widget attributes can be redefined any time, even the text and command that were set when the button
was created. The following command changes .hello into a goodbye button:
.hello config -text Goodbye! -command exit
Widgets have a cget operation to query the current value of an attribute:
.hello cget -background
=> red
You can find out more details about a widget attribute by using configure without a value:
.hello config -background
=> -background background Background #ffe4c4 red
The returned information includes the command-line switch, the resource name, the class name, the
default value, and the current value, which is last. The class and resource name have to do with the
resource mechanism described in Chapter 28. If you only specify configure and no attribute, then a
list of the configuration information for all widget attributes is returned. Example 21-2 uses this to
print out all the information about a widget:
Example 21-2 Looking at all widget attributes.
proc Widget_Attributes {w {out stdout}} {
puts $out [format "%-20s %-10s %s" Attribute Default Value]
foreach item [$w configure] {
puts $out [format "%-20s %-10s %s" \
[lindex $item 0] [lindex $item 3] \
[lindex $item 4]]
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter21. Tk Fundamentals
Tk Widget Attributes and the Resource Database
A widget attribute can be named three different ways: by its command-line option, by its resource
name, and by its resource class. The command-line option is the format you use in Tcl scripts. This
form is always all lowercase and prefixed with a hyphen (e.g., -offvalue). The resource name for the
attribute has no leading hyphen, and it has uppercase letters at internal word boundaries (e.g.,
offValue). The resource class begins with an uppercase letter and has uppercase letters at internal
word boundaries. (e.g., OffValue).
The tables in this book list widget attributes by their resource name.
You need to know these naming conventions if you specify widget attributes via the resource
mechanism. The command-line option can be derived from the resource name by mapping it to all
lowercase. The primary advantage of using resources to specify attributes is that you do not have to
litter your code with attribute specifications. With just a few resource database entries you can specify
attributes for all your widgets. In addition, if attributes are specified with resources, users can provide
alternate resource specifications in order to override the values supplied by the application. For
attributes like colors and fonts, this feature can be important to users. Resource specifications are
described in detail in Chapter 28.
The Tk Manual Pages
This book provides summaries for all the Tk commands, the widget attributes, and the default
bindings. However, for the absolute truth, you may need to read the on-line manual pages that come
with Tk. They provide a complete reference source for the Tk commands. You should be able to use
the UNIX man program to read them:
% man button
The tkman program provides a very nice graphical user interface to the UNIX manual pages. On the
Macintosh platform, the manual pages are formatted into HTML documents that you can find in the
HTML Docs folder of the Tcl/Tk distribution. On Windows, the manual pages are formatted into Help
documents. You can find the manual pages on the web at:
http://www.scriptics.com/man/
There are a large number of attributes that are common across most of the Tk widgets. These are
described in a separate man page under the name options. Each man page begins with a STANDARD
OPTIONS section that lists which of these standard attributes apply, but you have to look at the options
man page for the description. In contrast, the tables in this book always list all widget attributes.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter21. Tk Fundamentals
Summary of the Tk Commands
The following tables list the Tcl commands added by Tk. Table 21-1 lists commands that create
widgets. There are 15 different widgets in Tk, although 4 of them are variations on a button, and 5 are
devoted to different flavors of text display. Table 21-2 lists commands that manipulate widgets and
provide associated functions like input focus, event binding, and geometry management. Table 21-3
lists several support procedures that implement standard dialogs, option menus, and other facilities.
The page number in the table is the primary reference for the command, and there are other references
in the index.
Table 21-1. Tk widget-creation commands.
Command
Pg. Description
button
388 Create a command button.
checkbutton
392 Create a toggle button that is linked to a Tcl variable.
radiobutton
392 Create one of a set of radio buttons linked to one variable.
menubutton
396 Create a button that posts a menu.
menu
396 Create a menu.
canvas
475 Create a canvas, which supports lines, boxes, bitmaps, images, arcs, text,
polygons, and embedded widgets.
label
420 Create a read-only, one-line text label.
entry
437 Create a one-line text entry widget.
message
422 Create a read-only, multiline text message.
listbox
443 Create a line-oriented, scrolling text widget.
text
453 Create a general-purpose, editable text widget.
scrollbar
429 Create a scrollbar that can be linked to another widget.
Command
Pg. Description
scale
425 Create a scale widget that adjusts the value of a variable.
frame
417 Create a container widget used with geometry managers.
toplevel
417 Create a frame that is a new top level window.
Table 21-2. Tk widget-manipulation commands.
Command
Pg. Description
bell
428 Ring the terminal bell device.
bind
369 Bind a Tcl command to an event.
bindtags
371 Create binding classes and control binding inheritance.
clipboard
510 Manipulate the clipboard.
destroy
521 Delete a widget.
event
380 Define and generate virtual events.
focus
518 Control the input focus.
font
555 Set and query font attributes and measurements.
grab
520 Steal the input focus from other widgets.
grid
358 Arrange widgets into a grid with constraints.
image
542 Create and manipulate images.
lower
349 Lower a window in the stacking order.
option
409 Set and query the resources database.
pack
348 Pack a widget in the display with constraints.
place
367 Place a widget in the display with positions.
raise
349 Raise a window in the stacking order.
selection
509 Manipulate the selection.
send
562 Send a Tcl command to another Tk application.
tk
582 Query or set the application name.
tkerror
190 Handler for background errors.
tkwait
520 Wait for an event.
update
524 Update the display by going through the event loop.
winfo
576 Query window state.
wm
571 Interact with the window manager.
scale
425 Create a scale widget that adjusts the value of a variable.
frame
417 Create a container widget used with geometry managers.
toplevel
417 Create a frame that is a new top level window.
Table 21-2. Tk widget-manipulation commands.
Command
Pg. Description
bell
428 Ring the terminal bell device.
bind
369 Bind a Tcl command to an event.
bindtags
371 Create binding classes and control binding inheritance.
clipboard
510 Manipulate the clipboard.
destroy
521 Delete a widget.
event
380 Define and generate virtual events.
focus
518 Control the input focus.
font
555 Set and query font attributes and measurements.
grab
520 Steal the input focus from other widgets.
grid
358 Arrange widgets into a grid with constraints.
image
542 Create and manipulate images.
lower
349 Lower a window in the stacking order.
option
409 Set and query the resources database.
pack
348 Pack a widget in the display with constraints.
place
367 Place a widget in the display with positions.
raise
349 Raise a window in the stacking order.
selection
509 Manipulate the selection.
send
562 Send a Tcl command to another Tk application.
tk
582 Query or set the application name.
tkerror
190 Handler for background errors.
tkwait
520 Wait for an event.
update
524 Update the display by going through the event loop.
winfo
576 Query window state.
wm
571 Interact with the window manager.
Table 21-3. Tk support procedures.
Command
Pg. Description
tk_bisque
537 Install bisque family of colors.
tk_chooseColor
517 Dialog to select a color. (Tk 4.2)
tk_dialog
515 Create simple dialogs.
tk_focusFollowsMouse
518 Install mouse-tracking focus model.
tk_focusNext
519 Focus on next widget in tab order.
tk_focusPrev
519 Focus on previous widget in tab order.
tk_getOpenFile
516 Dialog to open an existing file. (Tk 4.2)
tk_getSaveFile
516 Dialog to open a new file. (Tk 4.2)
tk_messageBox
516 Message dialog. (Tk 4.2)
tk_optionMenu
398 Create an option menu.
tk_popup
398 Create a pop-up menu.
tk_setPalette
537 Set the standard color palette. (Tk 4.2)
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIII. Tk Basics
Chapter 22. Tk by Example
This chapter introduces Tk through a series of short examples. The ExecLog runs a program in the
background and displays its output. The Example Browser displays the Tcl examples from the book.
The Tcl Shell lets you type Tcl commands and execute them in a slave interpreter.
Tk provides a quick and fun way to generate user interfaces. In this chapter we will go through a series
of short example programs to give you a feel for what you can do. Some details are glossed over in
this chapter and considered in more detail later. In particular, the pack geometry manager is covered in
Chapter 23 and event bindings are discussed in Chapter 26. The Tk widgets are discussed in more
detail in later chapters.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter22. Tk by Example
ExecLog
Our first example provides a simple user interface to running another program with the exec
command. The interface consists of two buttons, Run it and Quit, an entry widget in which to enter a
command, and a text widget in which to log the results of running the program. The script runs the
program in a pipeline and uses the fileevent command to wait for output. This structure lets the user
interface remain responsive while the program executes. You could use this to run make, for example,
and it would save the results in the log. The complete example is given first, and then its commands
are discussed in more detail.
Example 22-1 Logging the output of a program run with exec.
#!/usr/local/bin/wish
# execlog - run a program with exec and log the output
# Set window title
wm title . ExecLog
# Create a frame for buttons and entry.
focus $t
set eval(text) $t
# Key bindings that limit input and eval things. The break in
# the bindings skips the default Text binding for the event.
bind $t <Return> {EvalTypein ; break}
bind $t <BackSpace> {
if {[%W tag nextrange sel 1.0 end] != ""} {
%W delete sel.first sel.last
} elseif {[%W compare insert > limit]} {
%W delete insert-1c
%W see insert
}
break
}
bind $t <Key> {
if [%W compare insert < limit] {
%W mark set insert end
}
}
# Evaluate everything between limit and end as a Tcl command
proc EvalTypein {} {
global eval
$eval(text) insert insert \n
set command [$eval(text) get limit end]
if [info complete $command] {
$eval(text) mark set limit insert
Eval $command
}
}
# Echo the command and evaluate it
proc EvalEcho {command} {
global eval
$eval(text) mark set insert end
$eval(text) insert insert $command\n
Eval $command
}
# Evaluate a command and display its result
proc Eval {command} {
global eval
$eval(text) mark set insert end
if [catch {$eval(slave) eval $command}result] {
$eval(text) insert insert $result error
} else {
$eval(text) insert insert $result result
}
if {[$eval(text) compare insert != "insert linestart"]} {
$eval(text) insert insert \n
}
$eval(text) insert insert $eval(prompt) prompt
$eval(text) see insert
$eval(text) mark set limit insert
return
}
# Create and initialize the slave interpreter
proc SlaveInit {slave} {
interp create $slave
load {}Tk $slave
interp alias $slave reset {}ResetAlias $slave
interp alias $slave puts {}PutsAlias $slave
return $slave
}
# The reset alias deletes the slave and starts a new one
proc ResetAlias {slave} {
interp delete $slave
SlaveInit $slave
}
# The puts alias puts stdout and stderr into the text widget
proc PutsAlias {slave args} {
if {[llength $args] > 3} {
error "invalid arguments"
}
set newline "\n"
if {[string match "-nonewline" [lindex $args 0]]} {
set newline ""
set args [lreplace $args 0 0]
}
if {[llength $args] == 1} {
set chan stdout
set string [lindex $args 0]$newline
} else {
set chan [lindex $args 0]
set string [lindex $args 1]$newline
}
if [regexp (stdout|stderr) $chan] {
global eval
$eval(text) mark gravity limit right
$eval(text) insert limit $string output
$eval(text) see limit
$eval(text) mark gravity limit left
} else {
puts -nonewline $chan $string
}
}
set eval(slave) [SlaveInit shell]
Text Marks, Tags, and Bindings
The shell uses a text mark and some extra bindings to ensure that users only type new text into the end
of the text widget. A mark represents a position in the text that is updated as characters are inserted
and deleted. The limit mark keeps track of the boundary between the read-only area and the editable
area. The insert mark is where the cursor is displayed. The end mark is always the end of the text.
The EvalTypein procedure looks at all the text between limit and end to see if it is a complete Tcl
command. If it is, it evaluates the command in the slave interpreter.
The <Key> binding checks to see where the insert mark is and bounces it to the end if the user tries to
input text before the limit mark. The puts alias sets right gravity on limit, so the mark is pushed
along when program output is inserted right at limit. Otherwise, the left gravity on limit means that
the mark does not move when the user inserts right at limit.
Text tags are used to give different regions of text difference appearances. A tag applies to a range of
text. The tags are configured at the beginning of the script and they are applied when text is inserted.
Chapter 33 describes the text widget in more detail.
Multiple Interpreters
The SlaveInit procedure creates another interpreter to evaluate the commands. This prevents
conflicts with the procedures and variables used to implement the shell. Initially, the slave interpreter
only has access to Tcl commands. The load command installs the Tk commands, and it creates a new
top-level window that is "." for the slave interpreter. Chapter 20 describes how you can embed the
window of the slave within other frames.
The shell interpreter is not created with the -safe flag, so it can do anything. For example, if you
type exit, it will exit the whole application. The SlaveInit procedure installs an alias, reset, that
just deletes the slave interpreter and creates a new one. You can use this to clean up after working in
the shell for a while. Chapter 19 describes the interp command in detail.
Native Look and Feel
When you run a Tk script on different platforms, it uses native buttons, menus, and scrollbars. The text
and entry widgets are tuned to give the application the native look and feel. The following screen shots
show the combined browser and shell as it looks on Macintosh, Windows, and UNIX.
Example 22-5 Macintosh look and feel.
Example 22-6 Windows look and feel.
Example 22-7 UNIX look and feel.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIII. Tk Basics
Chapter 23. The Pack Geometry Manager
This chapter explores the pack geometry manager that positions widgets on the screen.
Geometry managers arrange widgets on the screen. This chapter describes the pack geometry manager,
which is a constraint-based system. The next two chapters describe the grid and place geometry
managers. The pack and grid geometry managers are quite general, while place is used for special-
purpose applications. This book uses pack a lot because it was the original geometry manager for Tk.
The grid geometry manager was added in Tk 4.1.
A geometry manager uses one widget as a parent, and it arranges multiple children (also called slaves)
inside the parent. The parent is almost always a frame, but this is not strictly necessary. A widget can
only be managed by one geometry manager at a time, but you can use different managers to control
different widgets in your user interface. If a widget is not managed, then it doesn't appear on your
display at all.
The packer is a powerful constraint-based geometry manager. Instead of specifying in detail the
placement of each window, the programmer defines some constraints about how windows should be
positioned, and the packer works out the details. It is important to understand the algorithm the packer
uses; otherwise, the constraint-based results may not be what you expect.
This chapter explores the packer through a series of examples. The background of the main window is
set to black, and the other frames are given different colors so you can identify frames and observe the
effect of the different packing parameters. When consecutive examples differ by a small amount, the
added command or option is printed in bold courier to highlight the addition.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter23. The Pack Geometry Manager
Packing toward a Side
The following example creates two frames and packs them toward the top side of the main window.
The upper frame, .one, is not as big and the main window shows through on either side. The children
are packed toward the specified side in order, so .one is on top. The four possible sides are: top,
right, bottom, and left. The top side is the default.
Example 23-1 Two frames packed inside the main frame.
# Make the main window black
. config -bg black
# Create and pack two frames
frame .one -width 40 -height 40 -bg white
frame .two -width 100 -height 50 -bg grey50
pack .one .two -side top
Shrinking Frames and pack propagate
In the previous example, the main window shrank down to be just large enough to hold its two
children. In most cases this is the desired behavior. If not, you can turn it off with the pack propagate
command. Apply this to the parent frame, and it will not adjust its size to fit its children:
Example 23-2 Turning off geometry propagation.
because the .two frame occupies the whole bottom side of the packing cavity, even though its display
does not fill up that side.
Can you tell where the packing cavity is after this example? It is to the left of the frame .three, which
is the last frame packed toward the right, and it is above the frame .two, which is the last frame
packed toward the bottom. This explains why there was no difference between the previous two
examples when .one.gamma was packed to the left, but .one.right was packed to the right. At that
point, packing to the left or right of the cavity had the same effect. However, it will affect what
happens if another widget is packed into those two configurations. Try out the following commands
after running Example 23-3 and Example 23-4 and compare the difference.
[*]
[*]
Answer: After Example 23-3 the new button is to the right of all buttons. After Example 23-4 the new button is between .one.beta and
.one.right.
button .one.omega -text omega
pack .one.omega -side right
Each packing parent has its own cavity, which is why introducing nested frames can help. If you use a
horizontal or vertical arrangement inside any given frame, you can more easily simulate the packer's
behavior in your head!
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter23. The Pack Geometry Manager
Packing Space and Display Space
The packer distinguishes between packing space and display space when it arranges the widgets. The
display space is the area requested by a widget for the purposes of painting itself. The packing space is
the area the packer allows for the placement of the widget. Because of geometry constraints, a widget
may be allocated more (or less) packing space than it needs to display itself. The extra space, if any, is
along the side of the cavity against which the widget was packed.
The -fill Option
The -fill packing option causes a widget to fill up the allocated packing space with its display. A
widget can fill in the X or Y direction, or both. The default is not to fill, which is why the black
background of the main window has shown through in the examples so far:
Example 23-6 Filling the display into extra packing space.
frame .one -width 100 -height 50 -bg grey50
frame .two -width 40 -height 40 -bg white
# Pack with fill enabled
pack .one .two -side bottom -fill x
frame .three -width 20 -height 20 -bg red
pack .three -side right -fill x
This is just like Example 23-5, except that -fill x has been specified for all the frames. The .two
frame fills, but the .three frame does not. This is because the fill does not expand into the packing
cavity. In fact, after this example, the packing cavity is the part that shows through in black. Another
way to look at this is that the .two frame was allocated the whole bottom side of the packing cavity, so
its fill can expand the frame to occupy that space. The .three frame has only been allocated the right
side, so a fill in the X direction will not have any effect.
Another use of fill is for a menu bar that has buttons at either end and some empty space between
them. The frame that holds the buttons is packed toward the top. The buttons are packed into the left
and right sides of the menu bar frame. Without fill, the menu bar shrinks to be just large enough to
hold all the buttons, and the buttons are squeezed together. When fill is enabled in the X direction, the
menu bar fills out the top edge of the display:
Example 23-7 Using horizontal fill in a menu bar.
frame .menubar -bg white
frame .body -width 150 -height 50 -bg grey50
# Create buttons at either end of the menubar
foreach b {alpha beta} {
button .menubar.$b -text $b
}
pack .menubar.alpha -side left
pack .menubar.beta -side right
# Let the menu bar fill along the top
pack .menubar -side top -fill x
pack .body
Internal Padding with -ipadx and -ipady
Another way to get more fill space is with the -ipadx and -ipady packing options that request more
display space in the X and Y directions, respectively. Due to other constraints the request might not be
offered, but in general you can use this to give a widget more display space. The next example is just
like the previous one except that some internal padding has been added:
Example 23-8 The effects of internal padding (-ipady).
# Create and pack two frames
frame .menubar -bg white
frame .body -width 150 -height 50 -bg grey50
# Create buttons at either end of the menubar
foreach b {alpha beta} {
button .menubar.$b -text $b
}
pack .menubar.alpha -side left -ipady 10
pack .menubar.beta -side right -ipadx 10
# Let the menu bar fill along the top
pack .menubar -side top -fill x -ipady 5
pack .body
The alpha button is taller and the beta button is wider because of the internal padding. The frame has
internal padding, which reduces the space available for the packing cavity, so the .menubar frame
shows through above and below the buttons.
Some widgets have attributes that result in more display space. For example, it would be hard to
distinguish a frame with width 50 and no internal padding from a frame with width 40 and a -ipadx 5
packing option. The packer would give the frame 5 more pixels of display space on either side for a
total width of 50.
Buttons have their own -padx and -pady options that give them more display space, too. This padding
provided by the button is used to keep its text away from the edge of the button. The following
example illustrates the difference. The -anchor e button option positions the text as far to the right as
possible. Example 37-5 on page 531 provides another comparison of these options:
Example 23-9 Button padding vs. packer padding.
# Foo has internal padding from the packer
button .foo -text Foo -anchor e -padx 0 -pady 0
pack .foo -side right -ipadx 10 -ipady 10
# Bar has its own padding
button .bar -text Bar -anchor e -pady 10 -padx 10
pack .bar -side right -ipadx 0 -ipady 0
External Padding with -padx and -pady
The packer can provide external padding that allocates packing space that cannot be filled. The space
is outside of the border that widgets use to implement their 3D reliefs. Example 37-2 on page 528
shows the different reliefs. The look of a default button is achieved with an extra frame and some
padding:
Example 23-10 The look of a default button.
. config -borderwidth 10
# OK is the default button
frame .ok -borderwidth 2 -relief sunken
button .ok.b -text OK
pack .ok.b -padx 5 -pady 5
# Cancel is not
button .cancel -text Cancel
pack .ok .cancel -side left -padx 5 -pady 5
Even if the .ok.b button were packed with -fill both, it would look the same. The external padding
provided by the packer will not be filled by the child widgets.
Example 23-10 handcrafts the look of a default button. Tk 8.0 has a -default attribute for buttons that
gives them the right appearance for the default button on the current platform. It looks somewhat like
this on UNIX, but the appearance is different on Macintosh and Windows.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter23. The Pack Geometry Manager
Resizing and -expand
The -expand true packing option lets a widget expand its packing space into unclaimed space in the
packing cavity. Example 23-6 could use this on the small frame on top to get it to expand across the
top of the display, even though it is packed to the right side. The more common case occurs when you
have a resizable window. When the user makes the window larger, the widgets have to be told to take
advantage of the extra space. Suppose you have a main widget like a text, listbox, or canvas that is in a
frame with a scrollbar. That frame has to be told to expand into the extra space in its parent (e.g., the
main window) and then the main widget (e.g., the canvas) has to be told to expand into its parent
frame. Example 22-1 on page 316 does this.
In nearly all cases the -fill both option is used along with -expand true so that the widget actually
uses its extra packing space for its own display. The converse is not true. There are many cases where
a widget should fill extra space but not attempt to expand into the packing cavity. The examples below
show the difference.
Now we can investigate what happens when the window is made larger. The next example starts like
Example 23-7 on page 338, but the size of the main window is increased:
Example 23-11 Resizing without the expand option.
# Make the main window black
. config -bg black
# Create and pack two frames
frame .menubar -bg white
frame .body -width 150 -height 50 -bg grey50
The pack slaves command returns the list of children in their packing order. The ShuffleUp
procedure uses this to find out the first child so that it can insert another child before it. The
ShuffleDown procedure is simpler because the default is to append the child to the end of the packing
order.
When a widget is repacked, then it retains all its packing parameters that have already been set. If you
need to examine the current packing parameters for a widget, use the pack info command.
pack info .five
=> -in . -anchor center -expand 0 -fill none -ipadx 0 \
-ipady 0 -padx 0 -pady 0 -side left
Pack the Scrollbar First
The packing order also determines what happens when the window is made too
small. If the window is made small enough the packer will clip children that come
later in the packing order. This is why, when you pack a scrollbar and a text
widget into a frame, you should pack the scrollbar first. Otherwise, when the
window is made smaller the text widget takes up all the space and the scrollbar is
clipped.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter23. The Pack Geometry Manager
Choosing the Parent for Packing
In nearly all of the examples in this chapter, a widget is packed into its parent frame. In general, it is
possible to pack a widget into any descendent of its parent. For example, the .a.b widget could be
packed into .a, .a.c or .a.d.e.f. The -in packing option lets you specify an alternate packing
parent. One motivation for this is that the frames introduced to get the arrangement right can cause
cluttered names for important widgets. In Example 23-4 on page 335, the buttons have names like
.one.alpha and .one.right.delta, which is not consistent. Here is an alternate implementation of
the same example that simplifies the button names and gives the same result:
Example 23-18 Packing into other relatives.
# Create and pack two frames
frame .one -bg white
frame .two -width 100 -height 50 -bg grey50
# Create a row of buttons
foreach b {alpha beta} {
button .$b -text $b
pack .$b -in .one -side left
}
# Create a frame for two more buttons
frame .one.right
foreach b {delta epsilon} {
button .$b -text $b
pack .$b -in .one.right -side bottom
}
pack .one.right -side right
pack .one .two -side top
When you do this, remember that the order in which you create widgets is important. Create the
frames first, then create the widgets. The stacking order for windows will cause the later windows to
obscure the windows created first. The following is a common mistake because the frame obscures the
button:
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter23. The Pack Geometry Manager
Packer Summary
Keep these rules in mind about the packer:
Pack vertically (-side top and -side bottom) or horizontally (-side left and -side right)
within a frame. Only rarely will a different mixture of packing directions work out the way you
want. Add frames to build more complex structures.
By default, the packer puts widgets into their parent frame, and the parent frame must be created
before the children that are packed into it.
If you put widgets into other relatives, remember to create the frames first so the frames stay
underneath the widgets packed into them.
By default, the packer ignores -width and -height attributes of frames that have widgets packed
inside them. It shrinks frames to be just big enough to allow for its border width and to hold the
widgets inside them. Use pack propagate to turn off the shrink-wrap behavior.
The packer distinguishes between packing space and display space. A widget's display might not
take up all the packing space allocated to it.
The -fill option causes the display to fill up the packing space in the X or Y directions, or both.
The -expand true option causes the packing space to expand into any room in the packing
cavity that is otherwise unclaimed. If more than one widget in the same frame wants to expand,
then they share the extra space.
The -ipadx and -ipady options allocate more display space inside the border, if possible.
The -padx and -pady options allocate more packing space outside the border, if possible. This
space is never filled by the widget's display.
The pack Command
Table 23-1 summarizes the pack command. Table 23-2 summarizes the packing options for a widget.
These are set with the pack configure command, and the current settings are returned by the pack
info command:
Table 23-1. The pack command.
pack win ?win ..? ?options? This is just like pack configure.
pack configure win ?win ...?
?options?
Packs one or more widgets according to the options, which are
given in Table 23-2.
pack forget win ?win...? Unpacks the specified windows.
pack info win Returns the packing parameters of win.
pack propagate win ?bool? Queries or sets the geometry propagation of win, which has
other widgets packed inside it.
pack slaves win Returns the list of widgets managed by win.
Table 23-2. Packing options.
-after win Packs after win in the packing order.
-anchor anchor Anchors: center, n, ne, e, se, s, sw, w, or nw.
-before win Packs before win in the packing order.
-expand boolean Controls expansion into the unclaimed packing cavity.
-fill style Controls fill of packing space. Style: x, y, both, or none.
-in win Packs inside win.
-ipadx amount Horizontal internal padding, in screen units.
-ipady amount Vertical internal padding, in screen units.
-padx amount Horizontal external padding, in screen units.
-pady amount Vertical external padding, in screen units.
-side side Sides: top, right, bottom, or left.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter23. The Pack Geometry Manager
Window Stacking Order
The raise and lower commands control the window stacking order. The stacking order controls the
display of windows. Windows higher in the stacking order obscure windows lower in the stacking
order. By default, new windows are created at the top of the stacking order so they obscure older
windows. Consider this sequence of commands:
button .one
frame .two
pack .one -in .two
If you do this, you do not see the button. The problem is that the frame is higher in the stacking order
so it obscures the button. You can change the stacking order with the raise command:
raise .one .two
This puts .one just above .two in the stacking order. If .two was not specified, then .one would be
put at the top of the stacking order.
The lower command has a similar form. With one argument, it puts that window at the bottom of the
stacking order. Otherwise, it puts it just below another window in the stacking order.
You can use raise and lower on top-level windows to control their stacking order among all other
top-level windows. For example, if a user requests a dialog that is already displayed, use raise to
make it pop to the foreground of their cluttered desktop.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIII. Tk Basics
Chapter 24. The Grid Geometry Manager
This chapter explores the grid geometry manager that positions widgets on a grid that automatically
adjusts its size. Grid was added in Tk 4.1.
The grid geometry manager arranges widgets on a grid with variable-sized rows and columns. You
specify the rows and columns occupied by each widget, and the grid is adjusted to accommodate all
the widgets it contains. This is ideal for creating table-like layouts. The manager also has sophisticated
facilities for controlling row and column sizes and the dynamic resize behavior. By introducing
subframes with grids of their own, you can create arbitrary layouts.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter24. The Grid Geometry Manager
A Basic Grid
Example 24-1 uses grid to lay out a set of labels and frames in two parallel columns. It takes
advantage of the relative placement feature of grid. You do not necessarily have to specify rows and
columns. Instead, the order of grid commands and their arguments implies the layout. Each grid
command starts a new row, and the order of the widgets in the grid command determines the column.
In the example, there are two columns, and iteration of the loop adds a new row. Grid makes each
column just wide enough to hold the biggest widget. Widgets that are smaller are centered in their cell.
That's why the labels appear centered in their column:
Example 24-1 A basic grid.
foreach color {red orange yellow green blue purple} {
label .l$color -text $color -bg white
frame .f$color -background $color -width 100 -height 2
grid .l$color .f$color
}
The -sticky Setting
If a grid cell is larger than the widget inside it, you can control the size and position of the widget with
the -sticky option. The -sticky option combines the functions of -fill and -anchor used with the
pack geometry manager. You specify to which sides of its cell a widget sticks. You can specify any
combination of n, e, w, and s to stick a widget to the top, right, left, and bottom sides of its cell. You
can concatenate these letters together (e.g., news) or uses spaces or commas to separate them (e.g.,
n,e,w,s). Example 24-2 uses -sticky w to left justify the labels, and -sticky ns to stretch the color
frames to the full height of their row:
Example 24-2 A grid with sticky settings.
foreach color {red orange yellow green blue purple} {
label .l$color -text $color -bg white
frame .f$color -background $color -width 100 -height 2
grid .l$color .f$color
grid .l$color -sticky w
grid .f$color -sticky ns
}
Example 24-2 uses grid in two ways. The first grid in the loop fixes the positions of the widgets
because it is the first time they are assigned to the master. The next grid commands modify the
existing parameters; they just adjust the -sticky setting because their row and column positions are
already known.
You can specify row and column positions explicitly with the -row and -column attribute. This is
generally more work than using the relative placement, but it is necessary if you need to dynamically
move a widget into a different cell. Example 24-3 keeps track of rows and columns explicitly and
achieves the same layout as Example 24-2:
Example 24-3 A grid with row and column specifications.
set row 0
foreach color {red orange yellow green blue purple} {
label .l$color -text $color -bg white
frame .f$color -background $color -width 100
grid .l$color -row $row -column 0 -sticky w
grid .f$color -row $row -column 1 -sticky ns
incr row
}
External Padding with -padx and -pady
You can keep a widget away from the edge of its cell with the -padx and -pady settings. Example 24-
4 uses external padding to shift the labels away from the left edge, and to keep some blank space
between the color bars:
Example 24-4 A grid with external padding.
foreach color {red orange yellow green blue purple} {
label .l$color -text $color -bg white
frame .f$color -background $color -width 100 -height 2
grid .l$color .f$color
grid .l$color -sticky w -padx 3
grid .f$color -sticky ns -pady 1
}
Internal Padding with -ipadx and -ipady
You can give a widget more display space than it normally needs with internal padding. The internal
padding increases the size of the grid. In contrast, a -sticky setting might stretch a widget, but it will
not change the size of the grid. Example 24-5 makes the labels taller with -ipady:
Example 24-5 A grid with internal padding.
foreach color {red orange yellow green blue purple} {
label .l$color -text $color -bg white
frame .f$color -background $color -width 100 -height 2
grid .l$color .f$color
grid .l$color -sticky w -padx 3 -ipady 5
grid .f$color -sticky ns -pady 1
}
Multiple Widgets in a Cell
Example 24-6 shows all possible -sticky settings. It uses the ability to put more than one widget into
a grid cell. A large square frame is put in each cell, and then a label is put into the same cell with a
different -sticky setting. It is important to create the frame first so it is below the label. Window
stacking is discussed on page 349. External padding is used to keep the labels away from the edge so
that they do not hide the -ridge relief of the frames.
Example 24-6 All combinations of -sticky settings.
set index 0
foreach x {news ns ew " " new sew wsn esn nw ne sw se n s w e} {
frame .f$x -borderwidth 2 -relief ridge -width 40 -height 40
grid .f$x -sticky news \
-row [expr $index/4] -column [expr $index%4]
label .l$x -text $x -background white
grid .l$x -sticky $x -padx 2 -pady 2 \
-row [expr $index/4] -column [expr $index%4]
incr index
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter24. The Grid Geometry Manager
Spanning Rows and Columns
A widget can occupy more than one cell. The -rowspan and -columnspan attributes indicate how
many rows and columns are occupied by a widget. Example 24-7 uses explicit row, column, rowspan,
and columnspan specifications:
Example 24-7 Explicit row and column span.
. config -bg white
foreach color {888 999 aaa bbb ccc fff} {
frame .$color -bg #$color -width 40 -height 40
}
grid .888 -row 0 -column 0 -columnspan 3 -sticky news
grid .999 -row 1 -column 0 -rowspan 2 -sticky news
grid .aaa -row 1 -column 1 -columnspan 2 -sticky news
grid .bbb -row 2 -column 2 -rowspan 2 -sticky news
grid .ccc -row 3 -column 0 -columnspan 2 -sticky news
grid .fff -row 2 -column 1 -sticky news
You can also use special syntax in grid commands that imply row and column placement. Special
characters represent a cell that is spanned or skipped:
- represents a spanned column.
^ represents a spanned row.
x represents a skipped cell.
A nice feature of the implicit row and column assignments is that it is easy to make minor changes to
your layout. Example 24-8 achieves the same layout:
Example 24-8 Grid syntax row and column span.
. config -bg white
foreach color {888 999 aaa bbb ccc ddd fff} {
frame .$color -bg #$color -width 40 -height 40
}
grid .888 - - -sticky news
grid .999 .aaa - -sticky news
grid ^ .fff .bbb -sticky news
grid .ccc - ^ -sticky news
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter24. The Grid Geometry Manager
Row and Column Constraints
The grid manager supports attributes on whole rows and columns. You can control the row and
column sizes with a -pad and -minsize attribute. The -weight attribute controls resize behavior. The
grid command has a rowconfigure and columnconfigure operation to set and query these attributes:
grid columnconfigure master col ?attributes?
grid rowconfigure master row ?attributes?
With no attributes, the current settings are returned. The row and col specifications can be lists
instead of simple indices, so you can configure several rows or columns at once.
Row and Column Padding
The -pad attribute increases a row or column size. The initial size of a row or column is determined by
the largest widget, and -pad adds to this size. This padding can be filled by the widget by using the -
sticky attribute. Row and column padding works like internal padding because it is extra space that
can be occupied by the widget's display. In contrast, the -pad attribute on an individual widget acts
like a spacer that keeps the widget away from the edge of the cell. Example 24-9 shows the difference.
The row padding increases the height of the row, but the padding on .f1 keeps it away from the edge
of the cell:
Example 24-9 Row padding compared to widget padding.
. config -bg black
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter25. The Place Geometry Managery
place Basics
The place command lets you specify the width and height of a window, and the X and Y locations of
the window's anchor point. The size and location can be specified in absolute or relative terms.
Relative specifications are more powerful. Example 25-1 uses place to center a window in its parent.
You can use this command to position dialogs that you do not want to be detached top-level windows:
Example 25-1 Centering a window with place.
place $w -in $parent -relx 0.5 -rely 0.5 -anchor center
The -relx and -rely specify the relative X and Y positions of the anchor point of the widget $w in
$parent. A relative X (or Y) value of zero corresponds to the left (or top) edge of $parent. A value of
one corresponds to the right (or bottom) edge of $parent. A value of 0.5 specifies the middle. The
anchor point determines what point in $w is positioned according to the specifications. In Example 25-
1 the center anchor point is used so that the center of $w is centered in $parent.
The relative height and width settings are used to base a widget's size on another widget. Example 25-
2 completely covers one window with another window. It uses the default anchor point for windows,
which is their upper-left hand corner (nw):
Example 25-2 Covering a window with place.
place $w -in $parent -relwidth 1 -relheight 1 -x 0 -y 0
The absolute and relative size and position parameters are additive (e.g., -width and -relwidth). You
can make a window slightly larger or smaller than the parent by specifying both parameters. In
Example 25-3, a negative width and height are used to make a window smaller than another one:
Example 25-3 Combining relative and absolute sizes.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter26. Binding Commands to Events
The bind Command
The bind command creates event bindings, and it returns information about current bindings. The
general form of the command is:
bind bindingTag ?eventSequence? ?command?
If all arguments are present, a binding from eventSequence to command is defined for bindingTag.
The bindingTag is typically a widget class name (e.g., Button) or a widget instance name (e.g.,
.buttons.foo). Binding tags are described in more detail later. Called with a single argument, a
binding tag, bind returns the events for which there are command bindings:
bind Menubutton
=> <Key-Return> <Key-space> <ButtonRelease-1>
<B1-Motion> <Motion> <Button-1> <Leave> <Enter>
The events in this example are keystroke and mouse events. <Button-1> is the event generated when
the user presses the first, or left-hand, mouse button. <B1-Motion> is generated when the user moves
the mouse while holding down the first mouse button. The <Key-space> event occurs when the user
presses the space bar. The surrounding angle brackets delimit a single event, and you can define
bindings for a sequence of events. The event syntax is described on page 371, and event sequences are
described on page 377.
If bind is given a binding tag and an event sequence, it returns the Tcl command bound to that event
sequence:
bind Menubutton <B1-Motion>
=> tkMbMotion %W down %X %Y
The Tcl commands in event bindings support an additional syntax for event keywords. These
keywords begin with a percent sign and have one more character that identifies some attribute of the
event. The keywords are substituted with event-specific data before the Tcl command is evaluated. For
example, %W is replaced with the widget's pathname. The %X and %Y keywords are replaced with the
coordinates of the event relative to the screen. The %x and %y keywords are replaced with the
coordinates of the event relative to the widget. The event keywords are summarized on page 379.
The % substitutions are performed throughout the entire command bound to an event, without regard to
other quoting schemes. You must use %% to obtain a single percent sign. For this reason you should
make your binding commands short, adding a new procedure if necessary (e.g., tkMbMotion), instead
of littering percent signs throughout your code.
A new binding is created by specifying a binding tag, an event sequence, and a command:
bind Menubutton <B1-Motion> {tkMbMotion %W down %X %Y}
If the first character of the binding command is +, the command (without the +) is added to the
commands, if any, for that event and binding tag:
bind bindingTag event {+ command args}
To delete a binding for an event, bind the event to the null string:
bind bindingTag event {}
Bindings execute in the global scope.
When a binding is triggered, the command is evaluated at the global scope. A very common mistake is
to confuse the scope that is active when the bind command creates a binding, and the scope that is
active when the binding is triggered. The same problem crops up with the commands associated with
buttons, and it is discussed in more detail at the beginning of Chapter 27.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter26. Binding Commands to Events
The bindtags Command
A binding tag groups related bindings, and each widget is associated with an ordered set of binding
tags. The level of indirection between widgets and bindings lets you group functionality on binding
tags and compose widget behavior from different binding tags.
For example, the all binding tag has bindings on <Tab> that change focus among widgets. The Text
binding tag has bindings on keystrokes that insert and edit text. Only text widgets use the Text binding
tag, but all widgets share the all binding tag. You can introduce new binding tags and change the
association of widgets to binding tags dynamically. The result is a powerful and flexible way to
manage bindings.
The bindtags command sets or queries the binding tags for a widget. The general form of the
bindtags command is:
bindtags widget ?tagList?
The following command returns the binding tags for text widget .t:
bindtags .t
=> .t Text . all
You can change the binding tags and their order. The tagList argument to bindtags must be a proper
Tcl list. The following command reorders the binding tags for .t and eliminates the . binding tag:
bindtags .t [list all Text .t]
By default, all the Tk widgets, except a top-level, have four binding tags in the following order:
The widget's Tk pathname (e.g., .t). Use this binding tag to provide special behavior to a
keyboard. For punctuation and non-printing characters, special keysyms are defined. Case is
significant in keysyms, but unfortunately there is no consistent scheme. In particular BackSpace has a
capital B and a capital S. Commonly encountered keysyms include:
Return, Escape, BackSpace, Tab, Up, Down, Left, Right, comma, period, dollar,
asciicircum, numbersign, exclam.
Table 26-1. Event types.
Activate
The application has been activated. (Macintosh)
ButtonPress, Button
A button is pressed (down).
ButtonRelease
A button is released (up).
Circulate
The stacking order of the window changed.
Colormap
The color map has changed.
Configure
The window changed size, position, border, or stacking order.
Deactivate
The application has been deactivated. (Macintosh)
Destroy
The window has been destroyed.
Enter
The mouse has entered the window.
Expose
The window has been exposed.
FocusIn
The window has received focus.
FocusOut
The window has lost focus.
Gravity
The window has moved because of a change in size of its parent window.
KeyPress, Key
A key is pressed (down).
KeyRelease
A key is released (up).
MouseWheel
The scrolling mouse wheel has moved.
Motion
The mouse is moving in the window.
Leave
The mouse is leaving the window.
Map
The window has been mapped (opened).
Property
A property on the window has been changed or deleted.
Reparent
A window has been reparented.
Unmap
The window has been unmapped (iconified).
Visibility
The window has changed visibility.
Finding out what keysyms are generated by your keyboard.
There are times when you do not know what keysym is generated by a special key on your keyboard.
The keysyms are defined by the window system implementation, and on UNIX systems they are
affected by a dynamic keyboard map, the X modmap. You may find the next binding useful to
determine just what the keysym for a particular key is on your system:
bind $w <KeyPress> {puts stdout {%%K=%K %%A=%A}}
The %K keyword is replaced with the keysym from the event. The %A is replaced with the printing
character that results from the event and any modifiers like Shift. The %% is replaced with a single
percent sign. Note that these substitutions occur in spite of the curly braces used for grouping. If the
user types a capital Q, there are two KeyPress events, one for the Shift key, and one for the q key. The
output is:
%K=Shift_R %A={}
%K=Q %A=Q
The Shift_R keysym indicates the right-hand shift key was pressed. The %A keyword is replaced with
{} when modifier keys are pressed. You can check for this in <KeyPress> bindings to avoid doing
anything if only a modifier key is pressed. On Macintosh, there is no event at all when the modifier
keys are pressed. The following can be used with a text widget. The double quotes are necessary to
force a string comparison:
bind $w <KeyPress> {
if {"%A" != "{}"} {%W insert insert %A}
}
Mouse Events
Button events also distinguish between ButtonPress, (or Button), and ButtonRelease. Button can
be left off if a detail specifies a button by number. The following are equivalent:
<ButtonPress-1>
<Button-1>
<1>
Note: The event <1> implies a ButtonPress event, while the event 1 implies a KeyPress event. To
avoid confusion, always specify the Key or Button type.
The mouse is tracked by binding to the Enter, Leave, and Motion events. Enter and Leave are
triggered when the mouse comes into and exits out of the widget, respectively. A Motion event is
generated when the mouse moves within a widget.
The coordinates of the mouse event are represented by the %x and %y keywords in the binding
command. The coordinates are widget-relative, with the origin at the upper-left hand corner of a
widget's window. The keywords %X and %Y represent the coordinates relative to the screen:
bind $w <Enter> {puts stdout "Entered %W at %x %y"}
bind $w <Leave> {puts stdout "Left %W at %x %y"}
bind $w <Motion> {puts stdout "%W %x %y"}
A mouse drag event is a Motion event that occurs when the user holds down a mouse button. In this
case the mouse button is a modifier, which is discussed in more detail on page 375. The binding looks
like this:
bind $w <B1-Motion> {puts stdout "%W %x %y"}
Other Events
The <Map> and <Unmap> events are generated when a window is opened and closed, or when a widget
is packed or unpacked by its geometry manager.
The <Activate> and <Deactivate> events are generated when an application is activated by the
operating system. This applies to Macintosh systems, and it occurs when the user clicks in the
application window.
The <Configure> event is generated when the window changes size. A canvas that computes its
display based on its size can bind a redisplay procedure to the <Configure> event, for example. The
<Configure> event can be caused by interactive resizing. It can also be caused by a configure widget
command that changes the size of the widget. You should not reconfigure a widget's size while
processing a <Configure> event to avoid an indefinite sequence of these events.
The <Destroy> event is generated when a widget is destroyed. You can intercept requests to delete
windows, too. See also the description of the wm command on page 569.
The <MouseWheel> event is generated on Windows by the small scrolling wheel built into the
Microsoft Mouse. It reports a delta value using the %D keyword. Currently the delta is an integer
multiple of 120, where positive values indicate a scroll up, and negative values indicate a scroll down.
Chapter 36 presents some examples that use the <FocusIn> and <FocusOut> events. The remaining
events in Table 26-1 have to do with dark corners of the X protocol, and they are seldom used. More
information can be found on these events in the Event Reference section of the Xlib Reference Manual
(Adrian Nye, O'Reilly & Associates, Inc., 1992).
Bindings on Top-level Windows
Bindings on toplevels are shared by widgets they contain.
Be careful when binding events to top-level windows because their name is used as a binding tag on
all the widgets contained in them. For example, the following binding fires when the user destroys the
main window, which means the application is about to exit:
bind . <Destroy> {puts "goodbye"}
Unfortunately, all widgets inside the main window are destroyed as a side effect, and they all share the
name of their toplevel widget as a binding tag. So this binding fires when every widget inside the main
window is destroyed. Typically you only want to do something one time. The following binding
checks the identity of the widget before doing anything:
bind . <Destroy> {if {"%W" == "."} {puts "goodbye"}}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter26. Binding Commands to Events
Modifiers
A modifier indicates that another key or button is being held down at the time of the event. Typical
modifiers are the Shift and Control keys. The mouse buttons can also be used as modifiers. If an
event does not specify any modifiers, the presence of a modifier key is ignored by the event dispatcher.
However, if there are two possible matching events, the more accurate match will be used. For
example, consider these three bindings:
bind $w <KeyPress> {puts "key=%A"}
bind $w <Key-c> {puts "just a c"}
bind $w <Control-Key-c> {exit}
The last event is more specific than the others. Its binding will be triggered when the user types c with
the Control key held down. If the user types c with the Meta key held down, the second binding will
be triggered. The Meta key is ignored because it does not match any binding. If the user types
something other than a c, the first binding is triggered. If the user presses the Shift key, then the
keysym that is generated is C, not c, so the last two events do not match.
There are eight possible modifier keys. The Control, Shift, and Lock modifiers are found on nearly
all keyboards. The Meta and Alt modifiers tend to vary from system to system, and they may not be
defined at all. They are commonly mapped to be the same as Mod1 or Mod2, and Tk will try to
determine how the mappings are set. The Macintosh has a Command modifier that corresponds to the
clover-leaf or apple key. The remaining modifiers, Mod3 through Mod5, are sometimes mapped to other
special keys. In OpenLook environments, for example, the Paste function key is also mapped to the
Mod5 modifier.
The button modifiers, B1 through B5, are most commonly used with the Motion event to distinguish
different mouse dragging operations. For example, <B1-Motion> is the event generated when the user
drags the mouse with the first mouse button held down.
Double-click warning.
The Double and Triple events match on repetitions of an event within a short period of time. These
are commonly used with mouse events. Be careful: The binding for the regular press event will match
on the first press of the Double. Then the command bound to the Double event will match on the
second press. Similarly, a Double event will match on the first two presses of a Triple event. Verify
this by trying out the following bindings:
bind . <1> {puts stdout 1}
bind . <Double-1> {puts stdout 2}
bind . <Triple-1> {puts stdout 3}
If you click the first mouse button several times quickly, you will see a 1, 2, and then a few 3's output.
Your bindings must take into consideration that more than one binding might match a Double or
Triple event. This effect is compatible with an interface that selects an object with the first click, and
then operates on the selected object with a Double event. In an editor, character, word, and line
selection on a single, double, and triple click, respectively, is a good example.
[*]
[*]
If you really want to disable this, you can experiment with using after to postpone processing of one event. The time constant in the bind
implementation of <Double> is 500 milliseconds. At the single-click event, schedule its action to occur after 600 milliseconds, and verify at
that time that the <Double> event has not occurred.
Table 26-2 summarizes the modifiers.
Table 26-2. Event modifiers.
Control
The control key.
Shift
The shift key.
Lock
The caps-lock key.
Command
The command key. (Macintosh)
Meta, M
Defined to be what ever modifier (M1 through M5) is mapped to the Meta_L and
Meta_R keysyms.
Alt
Defined to be the modifier mapped to Alt_L and Alt_R.
Mod1, M1
The first modifier.
Mod2, M2,
Alt
The second modifier.
Mod3, M3
Another modifier.
Mod4, M4
Another modifier.
Mod5, M5
Another modifier.
Button1, B1
The first mouse button (left).
Button2, B2
The second mouse button (middle).
Button3, B3
The third mouse button (right).
Button4, B4
The fourth mouse button.
Button5, B5
The fifth mouse button.
Double
Matches double-press event.
Triple
Matches triple-press event.
Any
Matches any combination of modifiers. (Before Tk 4.0)
The UNIX xmodmap program returns the current mappings from keys to these modifiers. The first
column of its output lists the modifier. The rest of each line identifies the keysym(s) and low-level
keycodes that are mapped to each modifier. The xmodmap program can also be used to change
mappings. The following example shows the mappings on my system. Your setup may be different.
Example 26-2 Output from the UNIX xmodmap program.
xmodmap: up to 3 keys per modifier,
(keycodes in parentheses):
shift Shift_L (0x6a), Shift_R (0x75)
lock Caps_Lock (0x7e)
control Control_L (0x53)
mod1 Meta_L (0x7f), Meta_R (0x81)
mod2 Mode_switch (0x14)
mod3 Num_Lock (0x69)
mod4 Alt_L (0x1a)
mod5 F13 (0x20), F18 (0x50), F20 (0x68)
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter26. Binding Commands to Events
Event Sequences
The bind command accepts a sequence of events in a specification, and most commonly this is a
sequence of key events. In the following examples, the Key events are abbreviated to just the character
detail, and so abc is a sequence of three Key events:
bind . a {puts stdout A}
bind . abc {puts stdout C}
With these bindings in effect, both bindings are executed when the user types abc. The binding for a is
executed when a is pressed, even though this event is also part of a longer sequence. This is similar to
the behavior with Double and Triple event modifiers. For this reason you must be careful when
binding sequences. You can use break in the binding for the prefix to ensure that it does not do
anything:
bindtags $w [list $w Text [winfo toplevel $w] all]
bind $w <Control-x> break
bind $w <Control-x><Control-s> {Save ; break}
bind $w <Control-x><Control-c> {Quit ; break}
The break ensures that the default Text binding that inserts characters does not trigger. This trick is
embodied by BindSequence in the next example. If a sequence is detected, then a break binding is
added for the prefix. The procedure also supports the emacs convention that <Meta-x> is equivalent to
<Escape>x. This convention arose because Meta is not that standard across keyboards. There is no
meta key at all on Windows and Macintosh keyboards. The regexp command is used to pick out the
detail from the <Meta> event.
Example 26-3 Emacs-like binding convention for Meta and Escape.
proc BindSequence { w seq cmd } {
bind $w $seq $cmd
By default, virtual event definitions add to existing definitions for the same virtual event. The previous
command could be replaced with these three:
event add <<Cancel>> <Control-c>
event add <<Cancel>> <Escape>
event add <<Cancel>> <Command-.>
The event command is summarized in Table 26-3.
Table 26-3. The event command.
event add virt phys1 phy2 ... Adds a mapping from one or more physical events to virtual
event virt.
event delete virt Deletes virtual event virt.
event info
Returns the defined virtual events.
event info virt Returns the physical events that map to virt.
event generate win event ?opt
val? ...
Generates event for window win. The options are listed in
Table 26-4.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter26. Binding Commands to Events
Event Keywords
Table 26-4 lists the percent keywords and the corresponding option to the event generate command.
Remember that keyword substitutions occur throughout the command, regardless of other Tcl quoting
conventions. Keep your binding commands short, introducing procedures if needed. For the details
about various event fields, consult the Xlib Reference Manual (O'Reilly & Associates, Inc.). The string
values for the keyword substitutions are listed after a short description of the keyword. If no string
values are listed, the keyword has an integer value like a coordinate or a window ID.
Table 26-4. A summary of the event keywords.
%%
Use this to get a single percent sign. All events.
%# -serial num The serial number for the event. All events.
%a -above win The above field from the event. Configure event.
%b -button num Button number. Events: ButtonPress and ButtonRelease.
%c -count num The count field. Events: Expose and Map.
%d -detail value The detail field. Values: NotifyAncestor, NotifyNonlinearVirtual,
NotifyDetailNone, NotifyPointer, NotifyInferior, NotifyPointerRoot,
NotifyNonlinear, or NotifyVirtual. Events: Enter, Leave, FocusIn, and
FocusOut.
%f -focus
boolean
The focus field (0 or 1). Events: Enter and Leave.
%h -height num The height field. Events: Configure and Expose.
%k -keycode num The keycode field. Events: KeyPress and KeyRelease.
%m -mode value The mode field. Values: NotifyNormal, NotifyGrab, NotifyUngrab, or
NotifyWhileGrabbed. Events: Enter, Leave, FocusIn, and FocusOut.
%o -override
boolean
The override_redirect field. Events: Map, Reparent, and Configure.
%p -place value The place field. Values: PlaceOnTop, PlaceOnBottom. Circulate event.
%s -state value The state field. A decimal string for events: ButtonPress, ButtonRelease,
Enter, Leave, KeyPress, KeyRelease, and Motion.
Values for the Visibility event: VisibilityUnobscured,
VisibilityPartiallyObscured, or VisibilityFullyObscured.
%t -time num The time field. All events.
%v
The value_mask field. Configure event.
%w -width num The width field. Events: Configure and Expose.
%x -x pixel The X coordinate, widget relative. Mouse events.
%y -y pixel The Y coordinate, widget relative. Mouse events.
%A
The printing character from the event, or
{}
.
Events: KeyPress and KeyRelease.
%B -borderwidth
num
The border width. Configure event.
%D -delta value
The delta value. MouseWheel event.
%E -sendevent
bool
The send_event field. All events.
%K -keysym
symbol
The keysym from the event. Events: KeyPress and KeyRelease.
%N
The keysym as a decimal number. Events: KeyPress and KeyRelease.
%R -root win The root window ID. All events.
%S -subwindow
win
The subwindow ID. All events.
%T
The type field. All events.
%W
The Tk pathname of the widget receiving the event. All events.
%X -rootx pixel The x_root field. Relative to the (virtual) root window. Events: ButtonPress,
ButtonRelease, KeyPress, KeyRelease, and Motion.
%Y -rooty pixel The y_root field. Relative to the (virtual) root window. Events: ButtonPress,
ButtonRelease, KeyPress, KeyRelease, and Motion.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Part IV: Tk Widgets
Part IV describes the Tk widgets. These are the components you use to build up your graphical
user interface. Tk widgets are simple to use, so you can rapidly develop your interface. At the
same time, they have sophisticated features that you can use to fine-tune your interface in
response to user feedback.
Chapter 27 describes buttons and menus. Tk 8.0 adds native look and feel to these widgets, so a
single script will look different depending on the platform it is running on.
Associated with the widgets is a resource database that stores settings like colors and fonts.
Chapter 28 describes the resource database and generalizes it to store button and menu
configurations.
Chapter 29 describes a few simple widgets. The frame and toplevel are containers for other
widgets. The label displays a text string. The message formats a long text string onto multiple
lines. The scale represents a numeric value. The bell command rings the terminal bell.
Chapter 30 describes scrollbars, which can be attached in a general way to other widgets.
Chapter 31 describes entry widgets that provide one line of editable text.
Chapter 32 describes the listbox widget that displays several lines of text. The lines are
manipulated as units.
Chapter 33 describes the general-purpose text widget. It can display multiple fonts and have
binding tags on ranges of text.
Chapter 34 describes the canvas widget. The canvas manages objects like lines, boxes, images,
arcs, and text labels. You can have binding tags on these objects and classes of objects.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIV. Tk Widgets
Chapter 27. Buttons and Menus
Buttons and menus are the primary way that applications expose functions to users. This chapter
describes how to create and manipulate buttons and menus.
A button widget is associated with a Tcl command that invokes an action in the application. The
checkbutton and radiobutton widgets affect an application indirectly by controlling a Tcl variable. A
menu elaborates on this concept by organizing button-like items into related sets, including cascaded
menus. The menubutton widget is a special kind of button that displays a menu when you click on it.
Tk 8.0 provides a cross-platform menu bar facility. The menu bar is really just a menu that is displayed
horizontally along the top of your application's main window. On the Macintosh, the menu bar appears
at the top of the screen. You define the menu bar the same on all platforms. Tk 8.0 also uses native
button and menu widgets on the Windows and Macintosh platforms. This contributes to a native look
and feel for your application. In earlier versions, Tk displayed the widgets identically on all platforms.
Associating a command to a button is usually quite simple, as illustrated by the Tk "Hello, World!"
example:
button .hello -command {puts stdout "Hello, World!"}
This chapter describes a few useful techniques for setting up the commands in more general cases. If
you use variables inside button commands, you have to understand the scoping rules that apply. This is
the first topic of the chapter. Once you get scoping figured out, then the other aspects of buttons and
menus are quite straightforward.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter27. Buttons and Menus
Button Commands and Scope Issues
Perhaps the trickiest issue with button commands has to do with variable scoping. A button command
is executed at the global scope, which is outside of any procedure. If you create a button while inside a
procedure, then the button command executes in a different scope later. The commands used in event
bindings also execute later at the global scope.
I think of this as the "now" (i.e., button definition) and "later" (i.e., button use) scope problem. For
example, you may want to use the values of some variables when you define a button command but
use the value of other variables when the button command is used. When these two contexts are
mixed, it can be confusing. The next example illustrates the problem. The button's command involves
two variables: x and val. The global variable x is needed later, when the button's command executes.
The local variable val is needed now, in order to define the command. Example 27-1 shows this
awkward mixture of scopes:
Example 27-1 A troublesome button command.
proc Trouble {args} {
set b 0
# Display the value of x, a global variable
label .label -textvariable x
set f [frame .buttons -borderwidth 10]
# Create buttons that multiply x by their value
foreach val $args {
button $f.$b -text $val \
-command "set x \[expr \$x * $val\]"
pack $f.$b -side left
incr b
}
pack .label $f
}
set x 1
Trouble -1 4 7 36
The example uses a label widget to display the current value of x. The textvariable attribute is used
so that the label displays the current value of the variable, which is always a global variable. It is not
necessary to have a global command inside Trouble because the value of x is not used there. The
button's command is executed later at the global scope.
The definition of the button's command is ugly, though. The value of the loop variable val is needed
when the button is defined, but the rest of the substitutions need to be deferred until later. The variable
substitution of $x and the command substitution of expr are suppressed by quoting with backslashes:
set x \[expr \$x * $val\]
In contrast, the following command assigns a constant expression to x each time the button is clicked,
and it depends on the current value of x, which is not defined the first time through the loop. Clearly,
this is incorrect:
button $f.$b -text $val \
-command "set x [expr $x * $val]"
Another incorrect approach is to quote the whole command with braces. This defers too much,
preventing the value of val from being used at the correct time.
Use procedures for button commands.
The general technique for dealing with these sorts of scoping problems is to introduce Tcl procedures
for use as the button commands. Example 27-2 introduces a little procedure to encapsulate the
expression:
Example 27-2 Fixing the troublesome situation.
proc LessTrouble { args } {
set b 0
label .label -textvariable x
set f [frame .buttons -borderwidth 10]
foreach val $args {
button $f.$b -text $val \
-command "UpdateX $val"
pack $f.$b -side left
incr b
}
pack .label $f
}
proc UpdateX { val } {
global x
set x [expr $x * $val]
}
set x 1
LessTrouble -1 4 7 36
It may seem just like extra work to introduce the helper procedure, UpdateX. However, it makes the
code clearer in two ways. First, you do not have to struggle with backslashes to get the button
command defined correctly. Second, the code is much clearer about the function of the button. Its job
is to update the global variable x.
You can generalize UpdateX to work on any variable by passing the name of the variable to update.
Now it becomes much like the incr command:
button $f.$b -text $val -command "Update x $val"
The definition of Update uses upvar, which is explained on page 85, to manipulate the named variable
in the global scope:
proc Update {varname val} {
upvar #0 $varname x
set x [expr $x * $val]
}
Double quotes are used in the button command to allow $val to be substituted. Whenever you use
quotes like this, you have to be aware of the possible values for the substitutions. If you are not
careful, the command you create may not be parsed correctly. The safest way to generate the command
is with list:
button $f.$b -text $val -command [list UpdateX $val]
Using list ensures that the command is a list of two elements, UpdateX and the value of val. This is
important because UpdateX takes only a single argument. If val contained white space, then the
resulting command would be parsed into more words than you expected. Of course, in this case we
plan to always call LessTrouble with an integer value, which does not contain white space.
Example 27-3 provides a more straightforward application of procedures for button commands. In this
case the advantage of the procedure MaxLineLength is that it creates a scope for the local variables
used during the button action. This ensures that the local variables do not accidentally conflict with
global variables used elsewhere in the program. There is also the standard advantage of a procedure,
which is that you may find another use for the action in another part of your program.
Example 27-3 A button associated with a Tcl procedure.
proc MaxLineLength { file } {
set max 0
if [catch {open $file}in] {
return $in
}
foreach line [split [read $in] \n] {
set len [string length $line]
if {$len > $max} {
set max $len
}
}
return "Longest line is $max characters"
}
# Create an entry to accept the file name,
# a label to display the result
# and a button to invoke the action
. config -borderwidth 10
entry .e -width 30 -bg white -relief sunken
button .doit -text "Max Line Length" \
-command {.label config -text [MaxLineLength [.e get]]}
label .label -text "Enter file name"
pack .e .doit .label -side top -pady 5
The example is centered around the MaxLineLength procedure. This opens a file and loops over the
lines finding the longest one. The file open is protected with catch in case the user enters a bogus file
name. In that case, the procedure returns the error message from open. Otherwise, the procedure
returns a message about the longest line in the file. The local variables in, max, and len are hidden
inside the scope of the procedure.
The user interface has three widgets: an entry for user input, the button, and a label to display the
result. These are packed into a vertical stack, and the main window is given a border. Obviously, this
simple interface can be improved in several ways. There is no Quit button, for example.
All the action happens in the button command:
.label config -text [MaxLineLength [.e get]]
Braces are used when defining the button command so that the command substitutions all happen
when the button is clicked. The value of the entry widget is obtained with .e get. This value is passed
into MaxLineLength, and the result is configured as the text for the label. This command is still a little
complex for a button command. For example, suppose you wanted to invoke the same command when
the user pressed <Return> in the entry. You would end up repeating this command in the entry
binding. It might be better to introduce a one-line procedure to capture this action so that it is easy to
bind the action to more than one user action. Here is how that might look:
proc Doit {} {
.label config -text [MaxLineLength [.e get]]
}
button .doit -text "Max Line Length" -command Doit
bind .e <Return> Doit
Chapter 26 describes the bind command in detail, Chapter 29 describes the label widget, and Chapter
32 describes the entry widget.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter27. Buttons and Menus
Buttons Associated with Tcl Variables
The checkbutton and radiobutton widgets are associated with a global Tcl variable. When one of these
buttons is clicked, a value is assigned to the Tcl variable. In addition, if the variable is assigned a value
elsewhere in the program, the appearance of the checkbutton or radiobutton is updated to reflect the
new value. A set of radiobuttons all share the same global variable. The set represents a choice among
mutually exclusive options. In contrast, each checkbutton has its own global variable.
The ShowChoices example uses a set of radiobuttons to display a set of mutually exclusive choices in
a user interface. The ShowBooleans example uses checkbutton widgets:
Example 27-4 Radiobuttons and checkbuttons.
proc ShowChoices { parent varname args } {
set f [frame $parent.choices -borderwidth 5]
set b 0
foreach item $args {
radiobutton $f.$b -variable $varname \
-text $item -value $item
pack $f.$b -side left
incr b
}
pack $f -side top
}
proc ShowBooleans { parent args } {
set f [frame $parent.booleans -borderwidth 5]
set b 0
foreach item $args {
cursor
Cursor to display when mouse is over the widget.
default
active displays as a default button. normal and disabled display as
normal button. See page 715 (Tk 8.0).
direction
up, down, left, right, active. Offset direction for posting menus.
menubutton. (Tk 8.0).
disabledForeground
Foreground (text) color when button is disabled.
font
Font for the text.
foreground
Foreground (text) color. (Also fg).
height
Height, in lines for text, or screen units for images.
highlightBackground
Focus highlight color when widget does not have focus.
highlightColor
Focus highlight color when widget has focus.
highlightThickness
Width of highlight border.
image
Image to display instead of text or bitmap.
indicatorOn
Boolean that controls if the indicator is displayed: checkbutton,
menubutton, or radiobutton.
justify
Text justification: center, left, or right.
menu
Menu posted when menubutton is clicked.
offValue
Value for Tcl variable when checkbutton is not selected.
onValue
Value for Tcl variable when checkbutton is selected.
padX
Extra space to the left and right of the button text.
padY
Extra space above and below the button text.
relief
flat, sunken, raised, groove, solid or ridge.
selectColor
Color for selector. checkbutton or radiobutton.
selectImage
Alternate graphic image for selector: checkbutton or radiobutton.
state
Enabled (normal) or deactivated (disabled).
takeFocus
Control focus changes from keyboard traversal.
text
Text to display in the button.
textVariable
Tcl variable that has the value of the text.
underline
Index of text character to underline.
value
Value for Tcl variable when radiobutton is selected.
variable
Tcl variable associated with the button: checkbutton or radiobutton.
width
Width in characters for text, or screen units for image.
wrapLength
Maximum character length before text is wrapped, in screen units.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter27. Buttons and Menus
Button Operations
Table 27-2 summarizes the operations on button widgets. In the table, $w is a button, checkbutton,
radiobutton, or menubutton, except when noted. For the most part, these operations are used by the
script libraries that implement the bindings for buttons. The cget and configure operations are the
most commonly used by applications.
Table 27-2. Button operations.
$w cget option Returns the value of the specified attribute.
$w configure ?
option? ?value? ...
Queries or manipulates the configuration information for the widget.
$w deselect
Deselects the radiobutton or checkbutton. Set the radiobutton
variable to the null string. Set the checkbutton variable to the off value.
$w flash
Redisplays the button several times in alternate colors.
$w invoke
Invokes the command associated with the button.
$w select
Selects the radiobutton or checkbutton, setting the associated variable
appropriately.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter27. Buttons and Menus
Menus and Menubuttons
A menu presents a set of button-like menu entries to users. A menu entry is not a full fledged Tk
widget. Instead, you create a menu widget and then add entries to the menu as shown in the following
examples. There are several kinds of menu entries:
Command entries are like buttons.
Check entries are like checkbuttons.
Radio entries are like radiobuttons.
Separator entries are used to visually set apart entries.
Cascade entries are used to post submenus.
Tear-off entries are used to detach a menu from its menu button so that it becomes a new top-
level window.
A menubutton is a special kind of button that posts (i.e., displays) a menu when you press it. If you
click on a menubutton, then the menu is posted and remains posted until you click on a menu entry to
select it, or click outside the menu to dismiss it. If you press and hold the menubutton, then the menu
is unposted when you release the mouse. If you release the mouse over the menu, it selects the menu
entry that was under the mouse.
You can have a command associated with a menubutton, too. The command is invoked before the
menu is posted, which means you can compute the menu contents when the user presses the
menubutton.
Our first menu example creates a sampler of the different entry types:
Example 27-6 A menu sampler.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter27. Buttons and Menus
Manipulating Menus and Menu Entries
There are a number of operations that apply to menu entries. We have already introduced the add
operation. The entryconfigure operation is similar to the configure operation for widgets. It
accepts the same attribute-value pairs used when the menu entry was added. The delete operation
removes a range of menu entries. The rest of the operations are used by the library scripts that
implement the standard bindings for menus.
A menu entry is referred to by an index. The index can be numerical, counting
from zero, or symbolic. Table 27-3 summarizes the index formats. One of the
most useful indices is a pattern that matches the label in the menu entry. The
pattern matching is done with the rules of string match. Using a pattern
eliminates the need to keep track of the numerical indices.
Table 27-3. Menu entry index keywords
index A numerical index counting from zero.
active
The activated entry, either because it is under the mouse or has been activated by keyboard
traversal.
end
The last menu entry.
last
The same as end.
none
No entry at all.
@ycoord The entry under the given Y coordinate. Use @%y in bindings.
pattern A string match pattern to match the label of a menu entry.
Table 27-4 summarizes the complete set of menu operations. In the table, $w is a menu widget.
Table 27-4. Menu operations.
Table 27-6 describes the attributes for menu entries, as you would use them in a Tcl command (i.e., all
lowercase with a leading dash.) The attributes for menu entries are not supported directly by the
resource database. However, Example 28-6 on page 411 describes how you can use the resource
database for menu entries.
Table 27-6. Attributes for menu entries.
-activebackground
Background color when the mouse is over the entry.
-activeforeground
Foreground (text) color with mouse is over the entry.
-accelerator
Text to display as a reminder about keystroke binding.
-background
The normal background color.
-bitmap
A bitmap to display instead of text.
-columnbreak
Puts the entry at the start of a new column. (Tk 8.0).
-command
Tcl command to invoke when entry is invoked.
-font
Default font for the text.
-foreground
Foreground color. (Also fg).
-hidemargin
Suppresses the margin reserved for button indicators. (Tk 8.0).
-image
Image to display instead of text or bitmap.
-label
Text to display in the menu entry.
-justify
Text justification: center, left, or right.
-menu
Menu posted when cascade entry is invoked.
-offvalue
Variable value when check entry is not selected.
-onvalue
Value for Tcl variable when check entry is selected.
-selectcolor
Color for selector: check and radio entries.
-state
The state: normal, active, or disabled
-underline
Index of text character to underline.
-value
Value for Tcl variable when radiobutton entry is selected.
-variable
Tcl variable associated with the check or radio entry.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter27. Buttons and Menus
A Menu by Name Package
If your application supports extensible or user-defined menus, then it can be tedious to expose all the
details of the Tk menus. The examples in this section create a little package that lets users refer to
menus and entries by name. In addition, the package keeps keystroke accelerators for menus consistent
with bindings.
The Menu_Setup procedure initializes the package. It creates a frame to hold the set of menu buttons,
and it initializes some state variables: the frame for the menubuttons and a counter used to generate
widget pathnames. All the global state for the package is kept in the array called menu.
The Menu procedure creates a menubutton and a menu. It records the association between the text label
of the menubutton and the menu that was created for it. This mapping is used throughout the rest of
the package so that the client of the package can refer to the menu by its label (e.g., File) as opposed
to the internal Tk pathname, (e.g., .top.menubar.file.menu).
Example 27-8 A simple menu by name package.
proc Menu_Setup { menubar } {
global menu
frame $menubar
pack $menubar -side top -fill x
set menu(menubar) $menubar
set menu(uid) 0
}
proc Menu { label } {
global menu
if [info exists menu(menu,$label)] {
error "Menu $label already defined"
}
# Create the menubutton and its menu
set name $menu(menubar).mb$menu(uid)
set menuName $name.menu
incr menu(uid)
set mb [menubutton $name -text $label -menu $menuName]
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter28. The Resource Database
An Introduction to Resources
When a Tk widget is created, its attributes are set by one of three sources. It is important to note that
Tcl command specifications have priority over resource database specifications:
The most evident source of attributes are the options in Tcl commands, such as the -text quit
attribute specification for a button.
If an attribute is not specified on the command line, then the resource database is queried as
described later.
If there is nothing in the resource database, then a hard-coded value from the widget
implementation is used.
The resource database consists of a set of keys and values. Unlike many databases, however, the keys
are patterns that are matched against the names of widgets and attributes. This makes it possible to
specify attribute values for a large number of widgets with just a few database entries. In addition, the
resource database can be shared by many applications, so users and administrators can define common
attributes for their whole set of applications.
The resource database is maintained in main memory by the Tk toolkit. On UNIX the database is
initialized from the RESOURCE_MANAGER property on the root window, or the .Xdefaults file in your
home directory. On Windows and Macintosh there are a few resources added by the tk.tcl library
file. Additional files can be explicitly loaded with the option readfile command, and individual
database entries are added with the option add Tcl command.
The initialization of the database is different from the Xt toolkit, which loads specifications from as
many as five different files to allow per-user, per-site, per-application, per-machine, and per-user-per-
application specifications. You can achieve the same effect in Tk, but you must do it yourself.
Example 42-1 on page 584 gives a partial solution.
Resource Patterns
The pattern language for the keys is related to the naming convention for Tk widgets. Recall that a
widget name reflects its position in the hierarchy of windows. You can think of the resource names as
extending the hierarchy one more level at the bottom to account for all the attributes of each individual
widget. There is also a new level of the hierarchy at the top to specify the application by name. For
example, the database could contain an entry like the following in order to define a font for the quit
button in a frame called .buttons:
Tk.buttons.quit.font: fixed
The leading Tk. matches the default class name for Tcl/Tk applications. You could also specify a
more specific application name, such as exmh, or an asterisk to match any application:
*buttons.quit.font: fixed
Resource keys can also specify classes of widgets and attributes as opposed to individual instances.
The quit button, for example, is an instance of the Button class. Class names for widgets are the same
as the Tcl command used to create them, except for a leading capital. A class-oriented specification
that would set the font for all buttons in the .buttons frame would be:
Tk.buttons.Button.font: fixed
Patterns let you replace one or more components of the resource name with an asterisk (*). For
example, to set the font for all the widgets packed into the .buttons frame, you could use the resource
name *buttons*font. Or, you could specify the font for all buttons with the pattern *Button.font.
In these examples we have replaced the leading Tk. with an asterisk as well. It is the ability to collapse
several layers of the hierarchical name with a single asterisk that makes it easy to specify attributes for
many widgets with just a few database entries.
The tables in this book list attributes by their resource name. The resource names use a capital letter at
the internal word boundaries. For example, if the command line switch is -offvalue, then the
corresponding resource name is offValue. There are also class names for attributes, which are
distinguished with a leading capital (e.g., OffValue).
Warning: Order is Important!
The matching between a widget name and the patterns in the database can be ambiguous. It is possible
that multiple patterns can match the same widget. The way this is resolved in Tk is by the ordering of
database entries, with later entries taking precedence. (This is different from the Xt toolkit, in which
longer matching patterns have precedence, and instance specifications have priority over class
specifications.) Suppose the database contained just two entries, in this order:
*Text*foreground: blue
*foreground: red
Despite the more specific *Text*foreground entry, all widgets will have a red foreground, even text
widgets. For this reason you should list your most general patterns early in your resource files and give
the more specific patterns later.
Tk also supports different priorities among resources as described in the next section. The ordering
precedence described here applies to all resources with the same priority.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter28. The Resource Database
Loading Option Database Files
The option command manipulates the resource database. The first form of the command loads a file
containing database entries:
option readfile filename ?priority?
The priority distinguishes different sources of resource information and gives them different
priorities. Priority levels are numeric, from 0 to 100. However, symbolic names are defined for
standard priorities. From lowest to highest, the standard priorities are widgetDefault (20),
startupFile (40), userDefault (60), and interactive (80). These names can be abbreviated. The
default priority is interactive.
Example 28-1 Reading an option database file.
if [file exists $appdefaults] {
if [catch {option readfile $appdefaults startup}err] {
puts stderr "error in $appdefaults: $err"
}
}
The format of the entries in the file is:
key: value
The key has the pattern format previously described. The value can be anything, and there is no need
to group multiword values with any quoting characters. In fact, quotes will be picked up as part of the
value.
Comment lines are introduced by the exclamation mark (!).
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter28. The Resource Database
Accessing the Database
Often it is sufficient to just set up the database and let the widget implementations use the values.
However, it is also possible to record application-specific information in the database. To fetch a
resource value, use option get:
option get window name class
The window is a Tk widget pathname. The name is a resource name. In this case, it is not a pattern or a
full name. Instead, it is the resource name as specified in the tables in this book. Similarly, the class
is a simple class name. It is possible to specify a null name or class. If there is no matching database
entry, option get returns the empty string.
It is not possible to enumerate the database, nor can you detect the difference between a value that is
the empty string and the absence of a value. You can work around this by introducing well-known
resource names that list other resources. This trick is used in the next section.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter28. The Resource Database
User-Defined Buttons
Suppose you want users to be able to define a set of their own buttons for frequently executed
commands. Or, perhaps users can augment the application with their own Tcl code. The following
scheme, which is based on an idea from John LoVerso, lets them define buttons to invoke their own
code or their favorite commands.
The application creates a special frame to hold the user-defined buttons and places it appropriately.
Assume the frame is created like this:
frame .user -class User
The class specification for the frame means that we can name resources for the widgets inside the
frame relative to *User. Users specify the buttons that go in the frame via a personal file containing
resource specifications.
The first problem is that there is no means to enumerate the database, so we must create a resource that
lists the names of the user-defined buttons. We use the name buttonlist and make an entry for
*User.buttonlist that specifies which buttons are being defined. It is possible to use artificial
resource names (e.g., buttonlist), but they must be relative to an existing Tk widget.
Example 28-3 Using resources to specify user-defined buttons.
*User.buttonlist: save search justify quit
*User.save.text: Save
*User.save.command: File_Save
*User.search.text: Search
*User.search.command: Edit_Search
*User.justify.text: Justify
*User.justify.command: Edit_Justify
*user.quit.text: Quit
*User.quit.command: File_Quit
*User.quit.background: red
In this example, we have listed four buttons and specified some of the attributes for each, most
importantly the text and command attributes. We are assuming, of course, that the application manual
publishes a set of commands that users can invoke safely. In this simple example the commands are all
one word, but there is no problem with multiword commands. There is no interpretation done of the
value, so it can include references to Tcl variables and nested command calls. The following code uses
these resource specifications to define the buttons.
Example 28-4 Resource_ButtonFrame defines buttons based on resources.
proc Resource_ButtonFrame { f class } {
frame $f -class $class -borderwidth 2
pack $f -side top -fill x
foreach b [option get $f buttonlist {}] {
if [catch {button $f.$b}] {
button $f.$b -font fixed
}
pack $f.$b -side right
}
}
The catch phrase is introduced to handle a common problem with fonts and widget creation. If the
user's resources specify a bogus or missing font, then the widget creation command will fail. The
catch phrase guards against this case by falling back to the fixed font, which is guaranteed to exist.
This problem is fixed in Tk 8.0 because the font mechanism will search for alternate fonts.
Example 28-5 assumes the resource specifications from Example 28-2 are in the file
button.resources. It creates the user-defined buttons in the .users frame.
Example 28-5 Using Resource_ButtonFrame.
option readfile button.resources
Resource_ButtonFrame .user User
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter28. The Resource Database
User-Defined Menus
User-defined menus can be set up with a similar scheme. However, it is more complex because there
are no resources for specific menu entries. We must use more artificial resources to emulate this. We
use menulist to name the set of menus. Then, for each of these, we define an entrylist resource.
Finally, for each entry we define a few more resources. The name of the entry has to be combined with
some type information, which leads to the following convention:
l_entry is the label for the entry.
t_entry is the type of the entry.
c_entry is the command associated with the entry.
v_entry is the variable associated with the entry.
m_entry is the menu associated with the entry.
Example 28-6 Specifying menu entries via resources.
*User.menulist: stuff
*User.stuff.text: My stuff
*User.stuff.m.entrylist: keep insert find
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter29. Simple Tk Widgets
Frames and Toplevel Windows
Frames have been introduced before for use with the geometry managers. There is not much to a
frame, except for its background color and border. You can also specify a colormap and visual type for
a frame. Chapter 38 describes visual types and colormaps on page 540.
A toplevel widget is like a frame, except that it is created as a new main window. That is, it is not
positioned inside the main window of the application. This is useful for dialog boxes, for example. A
toplevel has the same attributes as a frame, plus screen and menu attributes. The menu attribute is used
to create menubars along the top edge of a toplevel. This feature was added in Tk 8.0, and it is
described on page 397. On UNIX, the screen option lets you put the toplevel on any X display. The
value of the screen option has the following format:
host:display.screenNum
For example, I have one X server on my workstation sage that controls two screens. My two screens
are named sage:0.0 and sage:0.1. If the screenNum specifier is left off, it defaults to 0.
Attributes for Frames and Toplevels
Table 29-1 lists the attributes for the frame and toplevel widgets. The attributes are named according
to their resource name, which includes a capital letter at internal word boundaries. When you specify
an attribute in a Tcl command when creating or reconfiguring a widget, however, you specify the
attribute with a dash and all lowercase letters. Chapter 28 explains how to use resource specifications
for attributes. Chapters 37, 38, and 39 discuss many of these attributes in more detail.
Table 29-1. Attributes for frame and toplevel widgets.
background
Background color (also bg).
borderWidth
Extra space around the edge of the frame.
class
Resource class and binding class name.
colormap
The value is new or the name of a window.
container
If true, frame embeds another application.
cursor
Cursor to display when mouse is over the frame.
height
Height, in screen units.
highlightBackground
Focus highlight color when widget does not have focus.
highlightColor
Focus highlight color when widget has focus.
highlightThickness
Thickness of focus highlight rectangle.
menu
The menu to use for the menubar. Toplevel only.
relief
flat, sunken, raised, groove, solid or ridge.
screen
An X display specification. (Toplevel only, and this cannot be specified in
the resource database).
takeFocus
Controls focus changes from keyboard traversal.
use
A window ID from winfo id. This embeds the frame or toplevel into the
specified window.
visual
Type: staticgrey, greyscale, staticcolor, pseudocolor,
directcolor, or truecolor.
width
Width, in screen units.
You cannot change the class, colormap, visual, or screen attributes after the frame or toplevel has
been created. These settings are so fundamental that you need to destroy the frame and start over if you
must change them.
Embedding Other Applications
The container and use attributes support application embedding. Embedding puts another
application's window into a Tk frame or puts a Tk frame into another application. The use attribute
specifies the ID of a window that will contain a Tk frame. Wish supports a -use command line
argument that is used for the same purpose. Set the container attribute if you want to embed another
window. For example, here is how to run another wish application and embed its window in one of
your frames:
frame .embed -container 1 -bd 4 -bg red
exec wish somescript.tcl -use [winfo id .embed] &
Toplevel Window Styles
On Windows and Macintosh there are several styles of toplevel windows. They differ in their
appearance and their behavior. On UNIX, toplevel windows are usually decorated by the window
manager, which is a separate application. Chapter 41 describes how to interact with the window
manager.
On Macintosh, Tk has an unsupported1 command that you can use to set the window style:
unsupported1 style window style
The possible values for style include documentProc, dBoxProc, plainDBox, altDBoxProc,
movableDBoxProc, zoomDocProc, rDocProc, floatProc, floatZoomProc, floatSideProc, or
floatSideZoomProc. The dBoxProc, plainDBox, and altDBoxProc styles have no title bar, so there is
no close box on them. The other styles have different title bars, a close box, and possibly a full-sized
zoom box. The default style is documentProc. I used the following code to see what each looked like:
Example 29-1 Macintosh window styles.
set x {documentProc dBoxProc plainDBox altDBoxProc \
movableDBoxProc zoomDocProc rDocProc floatProc \
floatZoomProc floatSideProc floatSideZoomProc}
foreach y $x {
toplevel .$y
label .$y.l -text $y
pack .$y.l -padx 40 -pady 20
if [catch {unsupported1 style .$y $y}err] {
puts "$y: $err"
}
}
This feature may appear as part of the wm command in future releases of Tk. On Windows you can get
a couple different styles by using transient and overrideredirect windows, which are described
on page 576.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter29. Simple Tk Widgets
The Label Widget
The label widget provides a read-only text label, and it has attributes that let you control the position
of the label within the display space. Most commonly, however, you just need to specify the text for
the label:
label .version -text "MyApp v1.0"
The text can be specified indirectly by using a Tcl variable to hold the text. In this case the label is
updated whenever the value of the Tcl variable changes. The variable is used from the global scope,
even if there happens to be a local variable by the same name when you create the widget inside a
procedure:
set version "MyApp v1.0"
label .version -textvariable version
You can change the appearance of a label dynamically by using the configure
widget operation. If you change the text or font of a label, you are liable to change
the size of the widget, and this causes the packer to shuffle window positions.
You can avoid this by specifying a width for the label that is large enough to hold
all the strings you plan to display in it. The width is specified in characters, not
screen coordinates:
Example 29-2 A label that displays different strings.
proc FixedWidthLabel { name values } {
# name is a widget name to be created
# values is a list of strings
set maxWidth 0
foreach value $values {
if {[string length $value] > $maxWidth} {
showvalue option causes the current value to be displayed. The length of the scale is in screen units
(i.e., pixels).
Scale Bindings
Table 29-4 lists the bindings for scale widgets. You must direct focus to a scale explicitly for the key
bindings like <Up> and <Down> to take effect.
Table 29-4. Bindings for scale widgets.
<Button-1>
Clicking on the trough moves the slider by one unit of resolution toward the
mouse click.
<Control-Button-
1>
Clicking on the trough moves the slider all the way to the end of the trough
toward the mouse click.
<Left> <Up>
Moves the slider toward the left (top) by one unit.
<Control-Left>
<Control-Up>
Moves the slider toward the left (top) by the value of the bigIncrement
attribute.
<Right> <Down>
Moves the slider toward the right (bottom) one unit.
<Control-Right>
<Control-Down>
Moves the slider toward the right (bottom) by the value of the bigIncrement
attribute.
<Home>
Moves the slider all the way to the left (top).
<End>
Moves the slider all the way to the right (bottom).
Scale Attributes
Table 29-5 lists the scale widget attributes. The table uses the resource name, which has capitals at
internal word boundaries. In Tcl commands the attributes are specified with a dash and all lowercase.
Table 29-5. Attributes for scale widgets.
activeBackground
Background color when the mouse is over the slider.
background
The background color (also bg in commands).
bigIncrement
Coarse grain slider adjustment value.
borderWidth
Extra space around the edge of the widget.
command
Command to invoke when the value changes. The current value is appended
as another argument
cursor
Cursor to display when mouse is over the widget.
digits
Number of significant digits in scale value.
from
Minimum value. The left or top end of the scale.
font
Font for the label.
foreground
Foreground color (also fg).
highlightBackground
Focus highlight color when widget does not have focus.
highlightColor
Focus highlight color when widget has focus.
highlightThickness
Thickness of focus highlight rectangle.
label
A string to display with the scale.
length
The length, in screen units, of the long axis of the scale.
orient
horizontal or vertical.
relief
flat, sunken, raised, groove, solid or ridge.
repeatDelay
Delay before keyboard auto-repeat starts. Auto-repeat is used when pressing
<Button-1> on the trough.
repeatInterval
Time period between auto-repeat events.
resolution
The value is rounded to a multiple of this value.
showValue
If true, value is displayed next to the slider.
sliderLength
The length, in screen units, of the slider.
sliderRelief
The relief of the slider.
state
normal, active, or disabled.
takeFocus
Controls focus changes from keyboard traversal.
tickInterval
Spacing between tick marks. Zero means no marks.
to
Maximum value. Right or bottom end of the scale.
troughColor
The color of the bar on which the slider sits.
variable
Name of Tcl variable. Changes to the scale widget are reflected in the Tcl
variable value, and changes in the Tcl variable are reflected in the scale
display.
width
Width of the trough, or slider bar.
Programming Scales
The scale operations are primarily used by the default bindings and you do not need to program the
scale directly. Table 29-6 lists the operations supported by the scale. In the table, $w is a scale widget.
Table 29-6. Operations on the scale widget.
$w cget option Returns the value of the configuration option.
$w configure
...
Queries or modifies the widget configuration.
$w coords ?
value?
Returns the coordinates of the point in the trough that corresponds to value, or
the scale's value.
$w get ?x y?
Returns the value of the scale, or the value that corresponds to the position given
by x and y.
$w identify x y Returns trough1, slider, or trough2 to indicate what is under the position
given by x and y.
$w set value Sets the value of the scale.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter29. Simple Tk Widgets
The bell Command
The bell command rings the terminal bell. The bell is associated with the display; even if you are
executing your program on a remote machine, the bell is heard by the user. If your application has
windows on multiple displays, you can direct the bell to the display of a particular window with the -
displayof option. The syntax for the bell command is given below:
bell ?-displayof window?
UNIX has an xset program that controls the bell's duration, pitch, and volume. The volume is in
percent of a maximum, for example, 50. In practice, many keyboard bells only support a variable
duration; the pitch and volume are fixed. The arguments of xset that control the bell are shown below.
exec xset b ?volume? ?hertz? ?milliseconds?
The b argument by itself resets the bell to the default parameters. You can turn the bell off with -b, or
you can use the on or off arguments.
exec xset -b
exec xset b ?on? ?off?
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIV. Tk Widgets
Chapter 30. Scrollbars
This chapter describes the Tk scrollbar. Scrollbars have a general protocol that is used to attach
them to one or more other widgets.
Scrollbars control other widgets through a standard protocol based around Tcl commands. A scrollbar
uses a Tcl command to ask a widget to display part of its contents. The scrollable widget uses a Tcl
command to tell the scrollbar what part of its contents are visible. The Tk widgets designed to work
with scrollbars are: entry, listbox, text, and canvas. The scrollbar protocol is general enough to use
with new widgets, or collections of widgets. This chapter explains the protocol between scrollbars and
the widgets they control, but you don't need to know the details to use a scrollbar. All you need to
know is how to set things up, and then these widgets take care of themselves.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter30. Scrollbars
Using Scrollbars
The following commands create a text widget and two scrollbars that scroll it horizontally and
vertically:
scrollbar .yscroll -command {.text yview} -orient vertical
scrollbar .xscroll -command {.text xview} -orient horizontal
text .text -yscrollcommand {.yscroll set} \
-xscrollcommand {.xscroll set}
The scrollbar's set operation is designed to be called from other widgets when their display changes.
The scrollable widget's xview and yview operations are designed to be called by the scrollbar when the
user manipulates them. Additional parameters are passed to these operations as described later. In
most cases you can ignore the details of the protocol and just set up the connection between the
scrollbar and the widget.
Example 30-1 A text widget and two scrollbars.
proc Scrolled_Text { f args } {
frame $f
eval {text $f.text -wrap none \
<Button-1> <Button-2>
Clicking on the arrows scrolls by one unit. Clicking on the trough
moves by one screenful.
<B1-Motion> <B2-Motion>
Dragging the slider scrolls dynamically.
<Control-Button-1> <Control-
Button-2>
Clicking on the trough or arrow scrolls all the way to the
beginning (end) of the widget.
<Up> <Down>
Scrolls up (down) by one unit.
<Control-Up> <Control-Down> Scrolls up (down) by one screenful.
<Left> <Right>
Scrolls left (right) by one unit.
<Control-Left> <Control-
Right>
Scrolls left (right) by one screenful.
<Prior> <Next>
Scrolls back (forward) by one screenful.
<Home>
Scrolls all the way to the left (top).
<End>
Scrolls all the way to the right (bottom).
Scrollbar Attributes
Table 30-2 lists the scrollbar attributes. The table uses the resource name for the attribute, which has
capitals at internal word boundaries. In Tcl commands, the attributes are specified with a dash and all
lowercase.
There is no length attribute for a scrollbar. Instead, a scrollbar is designed to be packed next to
another widget with a fill option that lets the scrollbar display grow to the right size. Only the relief
of the active element can be set. The background color is used for the slider, the arrows, and the
border. The slider and arrows are displayed in the activeBackground color when the mouse is over
them. The trough is always displayed in the troughColor.
Table 30-2. Attributes for the scrollbar widget.
activeBackground
Color when the mouse is over the slider or arrows.
activeRelief
Relief of slider and arrows when mouse is over them.
background
The background color (also bg in commands).
borderWidth
Extra space around the edge of the scrollbar.
command
Prefix of the command to invoke when the scrollbar changes. Typically this
is a xview or yview operation.
cursor
Cursor to display when mouse is over the widget.
elementBorderWidth
Border width of arrow and slider elements.
highlightBackground
Focus highlight color when widget does not have focus.
highlightColor
Focus highlight color when widget has focus.
highlightThickness
Thickness of focus highlight rectangle.
elementBorderWidth
Width of 3D border on arrows and slider.
jump
If true, dragging the elevator does not scroll dynamically. Instead, the
display jumps to the new position.
orient
Orientation: horizontal or vertical.
repeatDelay
Milliseconds before auto-repeat starts. Auto-repeat is used when pressing
<Button-1> on the trough or arrows.
repeatInterval
Milliseconds between auto-repeat events.
troughColor
The color of the bar on which the slider sits.
width
Width of the narrow dimension of the scrollbar.
Programming Scrollbars
The scrollbar operations are primarily used by the default bindings. Table 30-3 lists the operations
supported by the scrollbar. In the table, $w is a scrollbar widget.
Table 30-3. Operations on the scrollbar widget.
$w activate ?
element?
Queries or sets the active element, which can be arrow1, arrow2, or slider.
$w cget option Returns the value of the configuration option.
$w configure
...
Queries or modifies the widget configuration.
$w delta dx dy Returns the change in the first argument to set required to move the scrollbar
slider by dx or dy.
$w fraction x
y
Returns a number between 0 and 1 that indicates the relative location of the point
in the trough.
$s get
Returns first and last from the set operation.
$w identify x
y
Returns arrow1, trough1, slider, trough2, or arrow2, to indicate what is under
the point.
$w set first
last
Sets the scrollbar parameters. first is the relative position of the top (left) of the
display. last is the relative position of the bottom (right) of the display.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIV. Tk Widgets
Chapter 31. The Entry Widget
The entry widget provides a single line of text for use as a data entry field. The string in the entry can
be linked to a Tcl variable.
Entry widgets are specialized text widgets that display a single line of editable text. They have a subset
of the functionality of the general-purpose text widget described in Chapter 33. The entry is commonly
used in dialog boxes when values need to be filled in, or as a simple command entry widget. A very
useful feature of the entry is the ability to link it to a Tcl variable. The entry displays that variable's
value, and editing the contents of the entry changes the Tcl variable.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter31. The Entry Widget
Using Entry Widgets
The entry widget supports editing, scrolling, and selections, which make it more complex than label or
message widgets. Fortunately, the default settings for an entry widget make it usable right away. You
click with the left button to set the insert point and then type in text. Text is selected by dragging out a
selection with the left button. The entry can be scrolled horizontally by dragging with the middle
mouse button.
One common use of an entry widget is to associate a label with it, and a command to execute when
<Return> is pressed in the entry. The grid geometry manager is ideal for lining up several entries and
their labels. This is implemented in the following example:
Example 31-1 A command entry.
foreach field {Name Address1 Address2 Phone} {
label .l$field -text $field -anchor w
entry .e$field -textvariable address($field) -relief sunken
grid .l$field .e$field -sticky news
bind .e$field <Return> UpdateAddress
}
Example 31-1 creates four entries that are linked to variables with the textvariable attribute. The
variables are elements of the address array. The -relief sunken for the entry widget sets them apart
visually. Widget relief is described in more detail on page 530. The Tcl command UpdateAddress is
bound to the <Return> keystroke. The UpdateAddress procedure, which is not shown, can get the
current values of the entry widgets through the global array address.
Tips for Using Entry Widgets
If you are displaying long strings in an entry, you can use the following command
to keep the end of the string in view. The command requests that all the string be
off screen to the left, but the widget implementation fills up the display; the
scrolling is limited so that the tail of the string is visible:
$entry xview moveto 1.0
The show attribute is useful for entries that accept passwords or other sensitive information. If show is
not empty, it is used as the character to display instead of the real value:
$entry config -show *
The state attribute determines if the contents of an entry can be modified. Set the state to disabled
to prevent modification and set it to normal to allow modification.
$entry config -state disabled ;# read-only
$entry config -state normal ;# editable
The middle mouse button (<Button-2>) is overloaded with two functions. If you click and release the
middle button, the selection is inserted at the insert cursor. The location of the middle click does not
matter. If you press and hold the middle button, you can scroll the contents of the entry by dragging the
mouse to the left or right.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter31. The Entry Widget
The Entry Widget
Table 31-1 gives the bindings for entry widgets. When the table lists two sequences, they are
equivalent. The table does not list all the right arrow key bindings; there are corresponding bindings
for the left and right arrow keys.
Table 31-1. Entry bindings.
<Button-1>
Sets the insert point and starts a selection.
<B1-Motion>
Drags out a selection.
<Double-Button-1>
Selects a word.
<Triple-Button-1>
Selects all text in the entry.
<Shift-B1-Motion>
Adjusts the ends of the selection.
<Control-Button-1>
Sets insert point, leaving selection as is.
<Button-2>
Pastes selection at the insert cursor.
<B2-Motion>
Scrolls horizontally.
<Left> <Control-b>
Moves insert cursor one character left and starts the
selection.
<Shift-Left>
Moves cursor left and extends the selection.
<Control-Left>
Moves cursor left one word and starts the selection.
<Meta-b>
Same as <Control-Left>.
<Control-Shift-Left>
Moves cursor left one word and extends the selection.
<Right> <Control-f>
Moves right one character.
<Meta-f> <Control-Right>
Moves right one word.
<Home> <Control-a>
Moves cursor to beginning of entry.
<Shift-Home>
Moves cursor to beginning and extends the selection.
<End> <Control-e>
Moves cursor to end of entry.
<Shift-End>
Moves cursor to end and extends the selection.
<Select> <Control-Space>
Anchors the selection at the insert cursor.
<Shift-Select> <Control-Shift-
Space>
Adjusts the selection to the insert cursor.
<Control-slash>
Selects all the text in the entry.
<Control-backslash>
Clears the selection in the entry.
<Delete>
Deletes the selection or deletes next character.
<Backspace> <Control-h>
Deletes the selection or deletes previous character.
<Control-d>
Deletes next character.
<Meta-d>
Deletes next word.
<Control-k>
Deletes to the end of the entry.
<Control-w>
Deletes previous word.
<Control-x>
Deletes the section, if it exists.
<Control-t>
Transposes characters.
Entry Attributes
Table 31-2 lists the entry widget attributes. The table lists the resource name, which has capitals at
internal word boundaries. In Tcl commands these options are specified with a dash and are all
lowercase.
Table 31-2. Entry attribute resource names.
background
Background color (also bg).
borderWidth
Extra space around the edge of the text (also bd).
cursor
Cursor to display when mouse is over the widget.
exportSelection
If true, selected text is exported via the X selection mechanism.
font
Font for the text.
foreground
Foreground color (also fg).
highlightBackground
Focus highlight color when widget does not have focus.
highlightColor
Focus highlight color when widget has focus.
highlightThickness
Thickness of focus highlight rectangle.
insertBackground
Background for area covered by insert cursor.
insertBorderWidth
Width of cursor border. Non-zero for 3D effect.
insertOffTime
Time, in milliseconds the insert cursor blinks off.
insertOnTime
Time, in milliseconds the insert cursor blinks on.
insertWidth
Width of insert cursor. Default is 2.
justify
Text justification: left, right, center.
relief
flat, sunken, raised, groove, solid or ridge.
selectBackground
Background color of selection.
selectForeground
Foreground color of selection.
selectBorderWidth
Width of selection border. Nonzero for 3D effect.
show
A character (e.g., *) to display instead of contents.
state
State: disabled (read-only) or normal.
takeFocus
Controls focus changes from keyboard traversal.
textVariable
Name of Tcl variable.
width
Width, in characters.
xScrollCommand
Connects entry to a scrollbar.
Programming Entry Widgets
The default bindings for entry widgets are fairly good. However, you can completely control the entry
with a set of widget operations for inserting, deleting, selecting, and scrolling. The operations involve
addressing character positions called indices. The indices count from zero. The entry defines some
symbolic indices such as end. The index corresponding to an X coordinate is specified with @xcoord,
such as @26. Table 31-3 lists the formats for indices.
Table 31-3. Entry indices.
0
Index of the first character.
anchor
The index of the anchor point of the selection.
end
Index just after the last character.
number Index a character, counting from zero.
insert
The character right after the insertion cursor.
sel.first
The first character in the selection.
sel.last
The character just after the last character in the selection.
@xcoord The character under the specified X coordinate.
Table 31-4 summarizes the operations on entry widgets. In the table, $w is an entry widget.
Table 31-4. Entry operations.
$w cget option Returns the value of the configuration option.
$w configure ...
Queries or modifies the widget configuration.
$w delete first ?
last?
Deletes the characters from first to last, not including the character at
last. The character at first is deleted if last is not specified.
$w get
Returns the string in the entry.
$w icursor index Moves the insert cursor.
$w index index Returns the numerical index corresponding to index.
$w insert index
string
Inserts the string at the given index.
$w scan mark x Starts a scroll operation. x is a screen coordinate.
$w scan dragto x Scrolls from previous mark position.
$w select adjust
index
Moves the boundary of an existing selection.
$w select clear
Clears the selection.
$w select from
index
Sets the anchor position for the selection.
$w select present
Returns 1 if there is a selection in the entry.
$w select range
start end
Selects the characters from start to the one just before end.
$w select to
index
Extends the selection.
$w xview
Returns the offset and span of visible contents. These are both real numbers
between 0 and 1.0.
$w xview index Shifts the display so the character at index is at the left edge of the display.
$w xview moveto
fraction
Shifts the display so that fraction of the contents are off the left edge of the
display.
$w xview scroll
num what
Scrolls the contents by the specified number of what, which can be units or
pages.
For example, the binding for <Button-1> includes the following commands:
%W icursor @%x
%W select from @%x
if {%W cget -state] == "normal"} {focus %W}
Recall that the % triggers substitutions in binding commands, and that %W is replaced with the widget
pathname and %x is replaced with the X coordinate of the mouse event. Chapter 26 describes bindings
and these substitutions in detail. These commands set the insert point to the point of the mouse click
by using the @%x index, which will be turned into something like @17 when the binding is invoked.
The binding also starts a selection. If the entry is not in the disabled state, then keyboard focus is given
to the entry so that it gets KeyPress events.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIV. Tk Widgets
Chapter 32. The Listbox Widget
The listbox provides a scrollable list of text lines. The listbox supports selections of one or more lines.
Listbox widgets display a set of text lines in a scrollable display. The basic text unit is a line. There are
operations to insert, select, and delete lines, but there are no operations to modify the characters in a
line. As such, the listbox is suitable for displaying a set of choices, such as in a file selection dialog.
By default a user can select one item from a listbox, but you can select multiple items by setting the
selection mode attribute.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter32. The Listbox Widget
Using Listboxes
The lines in a listbox are indexed from zero. The keyword index end addresses the last line. Other
indices are described on page 445. The most common programming task for a listbox is to insert text.
If your data is in a list, you can loop through the list and insert each element at the end:
foreach item $list {
$listbox insert end $item
}
You can insert several items at once. The next command uses eval to concatenate the list onto a single
insert command:
eval {$listbox insert end}$list
It is also common to react to mouse clicks on a listbox, although the default bindings handle most of
the details of selecting items. The nearest operation finds the listbox entry that is closest to a mouse
event. If the mouse is clicked beyond the last element, the index of the last element is returned:
set index [$list nearest $y]
Example 32-1 displays two listboxes. The Scrolled_Listbox procedure on page 432 is used to put
scrollbars on the listboxes. When the user clicks on an item in the first listbox, it is copied into the
second listbox. When an item in the second listbox is selected, it is removed. This example shows how
to manipulate items selected from a listbox:
Example 32-1 Choosing items from a listbox.
<Button-1>
Selects the item under the mouse. This becomes the active
element, too.
<B1-Motion>
Same as <Button-1>, the selection moves with the mouse.
<Shift-Button-1>
Activates the item under the mouse. The selection is not
changed.
<Key-Up> <Key-Down>
Moves the active item up (down) one line, and selects it.
<Control-Home>
Activates and select the first element of the listbox.
<Control-End>
Activates and select the last element of the listbox.
<space> <Select> <Control-
slash>
Selects the active element.
Single Select Mode
In single selection mode, <Button-1> selects the item under the mouse, but dragging the mouse does
not change the selection. When you release the mouse, the item under that point is activated. Table 32-
5 specifies the bindings for single mode:
Table 32-5. Bindings for single selection mode.
<ButtonPress-1>
Selects the item under the mouse.
<ButtonRelease-1>
Activates the item under the mouse.
<Shift-Button-1>
Activates the item under the mouse. The selection is not
changed.
<Key-Up> <Key-Down>
Moves the active item up (down) one line. The selection is not
changed.
<Control-Home>
Activates and selects the first element of the listbox.
<Control-End>
Activates and selects the last element of the listbox.
<space> <Select> <Control-
slash>
Selects the active element.
<Control-backslash>
Clears the selection.
Extended Select Mode
In extended selection mode, multiple items are selected by dragging out a selection with the first
mouse button. Hold down the Shift key to adjust the ends of the selection. Use the Control key to
make a disjoint selection. The Control key works in a toggle fashion, changing the selection state of
the item under the mouse. If this starts a new part of the selection, then dragging the mouse extends the
new part of the selection. If the toggle action cleared the selected item, then dragging the mouse
continues to clear the selection. The extended mode is quite intuitive once you try it. Table 32-6
specifies the complete set of bindings for extended mode:
Table 32-6. Bindings for extended selection mode.
<Button-1>
Selects the item under the mouse. This becomes the anchor point for
adjusting the selection.
<B1-Motion>
Sweeps out a selection from the anchor point.
<ButtonRelease-1>
Activates the item under the mouse.
<Shift-Button-1>
Adjusts the selection from the anchor item to the item under the mouse.
<Shift-B1-Motion>
Continues to adjust the selection from the anchor.
<Control-Button-1>
Toggles the selection state of the item under the mouse, and makes this the
anchor point.
<Control-B1-Motion>
Sets the selection state of the items from the anchor point to the item under
the mouse to be the same as the selection state of the anchor point.
<Key-Up> <Key-Down>
Moves the active item up (down) one line, and start a new selection with
this item as the anchor point.
<Shift-Up> <Shift-
Down>
Moves the active element up (down) and extends the selection to include
this element.
<Control-Home>
Activates and selects the first element of the listbox.
<Control-Shift-
Home>
Extends the selection to the first element.
<Control-End>
Activates and selects the last element of the listbox.
<Control-Shift-End>
Extends the selection to the last element.
<space> <Select>
Selects the active element.
<Escape>
Cancels the previous selection action.
<Control-slash>
Selects everything in the listbox.
<Control-backslash>
Clears the selection.
Multiple Select Mode
In multiple selection mode you can select more than one item, but you can add or remove only one
item at a time. Dragging the mouse does not sweep out a selection. If you click on a selected item it is
deselected. Table 32-7 specifies the complete set of bindings for multiple selection mode.
Table 32-7. Bindings for multiple selection mode.
<Button-1>
Selects the item under the mouse.
<ButtonRelease-1>
Activates the item under the mouse.
<Key-Up> <Key-Down>
Moves the active item up (down) one line, and starts a new selection with
this item as the anchor point.
<Shift-Up> <Shift-
Down>
Moves the active element up (down).
<Control-Home>
Activates and selects the first element of the listbox.
<Control-Shift-Home>
Activates the first element of the listbox.
<Control-End>
Activates and selects the last element of the listbox.
<Control-Shift-End>
Activates the last element of the listbox.
<space> <Select>
Selects the active element.
<Control-slash>
Selects everything in the listbox.
<Control-backslash>
Clears the selection.
Scroll Bindings
There are a number of bindings that scroll the display of the listbox. In addition to the standard
middle-drag scrolling, there are some additional key bindings for scrolling. Table 32-8 summarizes the
the scroll-related bindings:
Table 32-8. Listbox scroll bindings.
<Button-2>
Marks the start of a scroll operation.
<B2-Motion>
Scrolls vertically and horizontally.
<Left> <Right>
Scrolls horizontally by one character.
<Control-Left> <Control-Right> <Control-Prior>
<Control-Next>
Scrolls horizontally by one screen width.
<Prior> <Next>
Scrolls vertically by one screen height.
<Home> <End>
Scrolls to left and right edges of the screen,
respectively.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter32. The Listbox Widget
Listbox Attributes
Table 32-9 lists the listbox widget attributes. The table uses the resource name for the attribute, which
has capitals at internal word boundaries. In Tcl commands these options are specified with a dash and
all lowercase.
Table 32-9. Listbox attribute resource names.
background
Background color (also bg).
borderWidth
Extra space around the edge of the text.
cursor
Cursor to display when mouse is over the widget.
exportSelection
If true, then the selected text is exported via the X selection mechanism.
font
Font for the text.
foreground
Foreground color (also fg).
height
Number of lines in the listbox.
highlightBackground
Focus highlight color when widget does not have focus.
highlightColor
Focus highlight color when widget has focus.
highlightThickness
Thickness of focus highlight rectangle.
relief
flat, sunken, raised, groove, solid, or ridge.
selectBackground
Background color of selection.
selectForeground
Foreground color of selection.
selectBorderWidth
Width of selection border. Nonzero for 3D effect.
selectMode
Mode: browse, single, extended, or multiple.
setGrid
Boolean. Set gridding attribute.
takeFocus
Controls focus changes from keyboard traversal.
width
Width, in average character sizes.
xScrollCommand
Connects listbox to a horizontal scrollbar.
yScrollCommand
Connects listbox to a vertical scrollbar.
Geometry Gridding
The setGrid attribute affects interactive resizing of the window containing the listbox. By default, a
window can be resized to any size. If gridding is turned on, the size is restricted so that a whole
number of lines and a whole number of average-width characters is displayed. Gridding affects the
user feedback during an interactive resize. Without gridding the size is reported in pixel dimensions.
When gridding is turned on, then the size is reported in grided units.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIV. Tk Widgets
Chapter 33. The Text Widget
Tk text widget is a general-purpose editable text widget with features for line spacing, justification,
tags, marks, and embedded windows.
The Tk text widget is versatile, simple to use for basic text display and manipulation, and has many
advanced features to support sophisticated applications. The line spacing and justification can be
controlled on a line-by-line basis. Fonts, sizes, and colors are controlled with tags that apply to ranges
of text. Edit operations use positional marks that keep track of locations in text, even as text is inserted
and deleted.
Tags are the most important feature of the text widget. You can define attributes like font and
justification for a tag. When that tag is applied to a range of text, the text uses those attributes. Text
can pick up attributes from any number of tags, so you can compose different tags for justification,
font, line spacing, and more. You can also define bindings for tags so that ranges of text can respond
to the mouse. Any interesting application of the text widget uses tags extensively.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
Text Indices
The characters in a text widget are addressed by their line number and the character position within the
line. Lines are numbered starting at one, while characters are numbered starting at zero. The
numbering for lines was chosen to be compatible with other programs that number lines starting at
one, like compilers that generate line-oriented error messages. Here are some examples of text indices:
1.0
The first character.
1.1
The second character on the first line.
2.end
The newline character on the second line.
There are also symbolic indices. The insert index is the position at which new characters are
normally inserted when the user types in characters. You can define new indices called marks, too, as
described later. Table 33-1 summarizes the various forms for a text index.
Table 33-1. Text indices.
Each mark has a gravity that determines what happens when characters are
inserted at the mark. The default gravity is right, which means that the mark
sticks to the character that is to its right. Inserting text at a mark with right
gravity causes the mark to be pushed along so it is always after the inserted text.
With left gravity the mark stays with the character to its left, so inserted text
goes after the mark and the mark does not move. In versions of Tk before 4.0,
marks had only right gravity, which made some uses of marks awkward. The mark
gravity operation is used to query and modify the gravity of a mark:
$t mark gravity foobar
=> right
$t mark gravity foobar left
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
Text Tags
A tag is a symbolic name that is associated with one or more ranges of characters. A tag has attributes
that affect the display of text that is tagged with it. These attributes include fonts, colors, tab stops, line
spacing and justification. A tag can have event bindings so you can create hypertext. A tag can also be
used to represent application-specific information. The tag names and tag ranges operations
described later tell you what tags are defined and where they are applied.
You can use almost any string for the name of a tag. However, do not use pure numbers, and do not
include spaces, plus (+) or minus (-). These characters are used in the mark arithmetic and may cause
problems if you use them in tag names.
A tag is added to a range with the tag add operation. The following command applies the tag
everywhere to all the text in the widget:
$t tag add everywhere 1.0 end
You can add one or more tags when text is inserted, too:
$t insert insert "new text" {someTag someOtherTag}
If you do not specify tags when text is inserted, then the text picks up any tags that are present on the
characters on both sides of the insertion point. (Before Tk 4.0, tags from the left-hand character were
picked up.) If you specify tags in the insert operation, only those tags are applied to the text.
A tag is removed from a range of text with the tag remove operation. However, even if there is no
text labeled with a tag, its attribute settings are remembered. All information about a tag can be
removed with the tag delete operation:
$t tag remove everywhere 3.0 6.end
$t tag delete everywhere
Tag Attributes
The attributes for a tag are defined with the tag configure operation. For example, a tag for blue text
is defined with the following command:
$t tag configure blue -foreground blue
Table 33-3 specifies the set of attributes for tags. Some attributes can only be applied with tags; there
is no global attribute for -bgstipple, -fgstipple, -justify, -lmargin1, -lmargin2, -offset, -
overstrike, -rmargin, and -underline. Table 33-10 on page 474 lists the attributes for the text
widget as a whole.
The -relief and -borderwidth attributes go together. If you only specify a relief, there is no visible
effect. The default relief is flat, too, so if you specify a border width without a relief you won't see
any effect either.
The stipple attributes require a bitmap argument. Bitmaps and colors are explained in more detail in
Chapter 38. For example, to "grey out" text you could use a foreground stipple of gray50:
$t tag configure disabled -fgstipple gray50
Table 33-3. Attributes for text tags.
-background color The background color for text.
-bgstipple bitmap A stipple pattern for the background color.
-borderwidth pixels The width for 3D border effects.
-fgstipple bitmap A stipple pattern for the foreground color.
-font font The font for the text.
-foreground color The foreground color for text.
-justify how Justification: left, right, or center.
-lmargin1 pixels Normal left indent for a line.
-lmargin2 pixels Indent for the part of a line that gets wrapped.
-offset pixels Baseline offset. Positive for superscripts.
-overstrike boolean
Draw text with a horizontal line through it.
-relief what flat, sunken, raised, groove, solid or ridge.
-rmargin pixels Right-hand margin.
-spacing1 pixels Additional space above a line.
-spacing2 pixels Additional space above wrapped part of line.
-spacing3 pixels Additional space below a line.
-tabs tabstops Specifies tab stops.
-underline boolean If true, the text is underlined.
-wrap mode Line wrap: none, char, or word.
Configure tags early.
You can set up the appearance (and bindings) for tags once in your application, even before you have
labeled any text with the tags. The attributes are retained until you explicitly delete the tag. If you are
going to use the same appearance over and over again, then it is more efficient to do the setup once so
that Tk can retain the graphics context.
On the other hand, if you change the configuration of a tag, any text with that tag will be redrawn with
the new attributes. Similarly, if you change a binding on a tag, all tagged characters are affected
immediately.
Example 33-1 defines a few tags for character styles you might see in an editor. The example is uses
the font naming system added in Tk 8.0, which is described on page 550.
Example 33-1 Tag configurations for basic character styles.
proc TextStyles { t } {
$t tag configure bold -font {times 12 bold}
$t tag configure italic -font {times 12 italic}
$t tag configure fixed -font {courier 12}
$t tag configure underline -underline true
$t tag configure super -offset 6 -font {helvetica 8}
$t tag configure sub -offset -6 -font {helvetica 8}
}
Mixing Attributes from Different Tags
A character can be labeled with more than one tag. For example, one tag could determine the font,
another could determine the background color, and so on. If different tags try to supply the same
attribute, a priority ordering is taken into account. The latest tag added to a range of text has the
highest priority. The ordering of tags can be controlled explicitly with the tag raise and tag lower
commands.
You can achieve interesting effects by composing attributes from different tags. In a mail reader, for
example, the listing of messages in a mail folder can use one color to indicate messages that are
marked for delete, and it can use another color for messages that are marked to be moved into another
folder. The tags might be defined like this:
$t tag configure deleted -background grey75
$t tag configure moved -background yellow
These tags conflict, but they are never used on the same message. However, a selection could be
indicated with an underline, for example:
$t tag configure select -underline true
You can add and remove the select tag to indicate what messages have been selected, and the
underline is independent of the background color determined by the moved or deleted tag. If you look
at the exmh implementation, the ftocColor.tcl file defines several text tags that are composed like
this.
Line Spacing and Justification
The spacing and justification for text have several attributes. These settings are complicated by
wrapped text lines. The text widget distinguishes between the first display line and the remaining
display lines for a given text line. For example, if a line in the text widget has 80 characters but the
window is only wide enough for 30, then the line may be wrapped onto three display lines. See Table
33-10 on page 474 for a description of the text widget's wrap attribute that controls this behavior.
Spacing is controlled with three attributes, and there are global spacing attributes as well as per-tag
spacing attributes. The -spacing1 attribute adds space above the first display line, while -spacing2
adds space above the subsequent display lines that exist because of wrapping. The -spacing3 attribute
adds space below the last display line, which could be the same as the first display line if the line is not
wrapped.
The margin settings also distinguish between the first and remaining display lines. The -lmargin1
attribute specifies the indent for the first display line, while the -lmargin2 attribute specifies the
indent for the rest of the display lines, if any. There is only a single attribute, -rmargin, for the right
indent. These margin attributes are only tag attributes. The closest thing for the text widget as a whole
is the -padx attribute, but this adds an equal amount of spacing on both sides:
Example 33-2 Line spacing and justification in the text widget.
proc TextExample { f } {
frame $f
pack $f -side top -fill both -expand true
set t [text $f.t -setgrid true -wrap word \
-width 42 -height 14 \
-yscrollcommand "$f.sy set"]
scrollbar $f.sy -orient vert -command "$f.t yview"
pack $f.sy -side right -fill y
pack $f.t -side left -fill both -expand true
$t tag configure para -spacing1 0.25i -spacing2 0.1i \
-lmargin1 0.5i -lmargin2 0.1i -rmargin 0.5i
$t tag configure hang -lmargin1 0.1i -lmargin2 0.5i
$t insert end "Here is a line with no special settings\n"
$t insert end "Now is the time for all good women and men
to come to the aid of their country. In this great time of
need, no one can avoid their responsibility.\n"
$t insert end "The quick brown fox jumps over the lazy dog."
$t tag add para 2.0 2.end
$t tag add hang 3.0 3.end
}
The example defines two tags, para and hang, that have different spacing and margins. The -
spacing1 setting for para causes the white space before the second line. The -spacing2 setting
causes the white space between the wrapped portions of the second paragraph. The hang tag has no
spacing attributes, so the last paragraph starts right below the previous paragraph. You can also see the
difference between the -lmargin1 and -lmargin2 settings.
The newline characters are inserted explicitly. Each newline character defines a new line for the
purposes of indexing, but not necessarily for display, as this example shows. In the third line there is
no newline. This means that if more text is inserted at the end mark, it will be on line three.
The values for the spacing and margin parameters are in screen units. Because different fonts are
different sizes, you may need to compute the spacings as a function of the character sizes. The bbox
operation returns the bounding box (x, y, width, height) for a given character:
$t insert 1.0 "ABCDE"
$t bbox 1.0
=> 4 4 8 12
The Tk 8.0 font metrics command, which is described on page 554, also gives detailed measurements:
font metrics {times 12}
-ascent 9 -descent 3 -linespace 12 -fixed 0
Text justification is limited to three styles: left, right, or center. There is no setting that causes the
text to line up on both margins, which would have to be achieved by introducing variable spacing
between words.
Tab Stops
Text widgets have adjustable tab stops. The tabs attribute is a list of tab stops, which are specified
with a screen unit and optionally a keyword that indicates justification. The tab justification keywords
are left, right, center, and numeric, and these can be abbreviated. The default is left. The
following resource specification defines tab stops at 2-centimeter intervals with different justification:
*Text.tabs: 2c left 4c right 6c center 8c numeric
The tabs attribute applies to the whole text widget or to a tag. The last tab stop is extrapolated as
needed. The following command defines a tag that has left justified tab stops every half inch:
$t tag configure foo -tabs ".5i left"
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
The Selection
The selection is implemented with a predefined tag named sel. If the application tags characters with
sel, those characters are added to the selection. This is done as part of the default bindings on the text
widget.
The exportSelection attribute of a text widget controls whether or not selected text is exported by
the selection mechanism to other applications. By default the selection is exported. In this case, when
another widget or application asserts ownership of the selection then the sel tag is removed from any
characters that are tagged with it. Chapter 35 describes the selection mechanism in more detail.
You cannot delete the sel tag with the tag delete operation. However, it is not an error to do so.
You can delete all the tags on the text widget with the following command. The eval command is
used to join the list of tag names into the tag delete command:
eval {$t tag delete}[$t tag names]
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
Tag Bindings
You can associate a tag with bindings so that when the user clicks on different areas of the text
display, different things happen. The syntax for the tag bind command is similar to that of the main
Tk bind command. You can both query and set the bindings for a tag. Chapter 26 describes the bind
command and the syntax for events in detail.
The only events supported by the tag bind command are Enter, Leave, ButtonPress,
ButtonRelease, Motion, KeyPress, and KeyRelease. ButtonPress and KeyPress can be shorted to
Button and Key as in the regular bind command. The Enter and Leave events are triggered when the
mouse moves in and out of characters with a tag, which is different from when the mouse moves in
and out of the window.
If a character has multiple tags, then the bindings associated with all the tags will be invoked, in the
order from lowest priority tag to highest priority tag. After all the tag bindings have run, the binding
associated with the main widget is run, if any. The continue and break commands work inside tag
bindings in a similar fashion as they work with regular command bindings. See Chapter 26 for the
details.
Example 33-3 defines a text button that has a highlighted relief and an action associated with it. The
example generates a new tag name so that each text button is unique. The relief and background are set
for the tag to set it apart visually. The winfo visual command is used to find out if the display
supports color before adding a colored background to the tag. On a black and white display, the button
is displayed in reverse video (i.e., white on black.) The command is bound to <Button-1>, which is
the same as <ButtonPress-1>.
The cursor is changed when the mouse is over the tagged area by binding to the <Enter> and <Leave>
events. Upon leaving the tagged area, the cursor is restored. Another tag is used to remember the
previous setting for the cursor. You could also use a global variable, but it is often useful to decorate
the text with tags for your own purposes.
Example 33-3 An active text button.
proc TextButton { t start end command } {
global textbutton
if ![info exists textbutton(uid)] {
set textbutton(uid) 0
} else {
incr textbutton(uid)
}
set tag button$textbutton(uid)
$t tag configure $tag -relief raised -borderwidth 2
if {[regexp color [winfo visual $t]]} {
$t tag configure $tag -background thistle
} else {
$t tag configure $tag -background [$t cget -fg]
$t tag configure $tag -foreground [$t cget -bg]
}
# Bind the command to the tag
$t tag bind $tag <Button-1> $command
$t tag add $tag $start $end
# use another tag to remember the cursor
$t tag bind $tag <Enter> \
[list TextButtonChangeCursor %W $start $end tcross]
$t tag bind $tag <Leave> {TextButtonRestoreCursor %W}
}
proc TextButtonChangeCursor {t start end cursor} {
$t tag add cursor=[$t cget -cursor] $start $end
$t config -cursor $cursor
}
proc TextButtonRestoreCursor {t} {
regexp {cursor=([^ ]*)}[$t tag names] x cursor
$t config -cursor $cursor
}
To behave even more like a button, the action should trigger upon <ButtonRelease-1>, and the
appearance should change upon <ButtonPress-1>. If this is important to you, you can always embed a
real Tk button. Embedding widgets is described later.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
Searching Text
The search operation scans the text widget for a string that matches a pattern. The index of the text
that matches the pattern is returned. The search starts at an index and covers all the text widget unless
a stop index is supplied. You can use end as the stop index to prevent the search from wrapping back
to the beginning of the document. The general form of the search operation is this:
$t search ?options? pattern index ?stopIndex?
Table 33-4 summarizes the options to the search operation:
Table 33-4. Options to the search operation.
-forward
Searches forward from index. This is the default.
-backward
Searches backward from index.
-exact
Matches pattern exactly. This is the default.
-regexp
Uses regular expression pattern matching.
-nocase
Lowercase letters in pattern can match upper case letters.
-count varName Returns in varName the number of characters that matched pattern.
--
Ends the options. Necessary if pattern begins with -.
If you use a regular expression to match a pattern, you may be interested in how much text matched so
you can highlight the match. The -count option specifies a variable that gets the number of matching
characters:
set start [$t search -count cnt -regexp -- $pattern 1.0 end]
$t tag add sel $start "$start +$cnt chars"
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
Embedded Widgets
The text widget can display embedded widgets as well as text. You can include a picture, for example,
by constructing it in a canvas and then inserting the canvas into the text widget. An embedded widget
takes up one character in terms of indices. You can address the widget by its index position or by the
Tk pathname of the widget.
For example, suppose $t names a text widget. The following commands create a button and insert it
into the text widget. The button behaves normally, and in this case it invokes the Help command when
the user clicks on it:
button $t.help -bitmap questhead -command Help
$t window create end -window $t.help
By default an embedded widget is centered vertically on its text line. You can adjust this with the -
align option to the window create command. This setting only takes effect if the window is smaller
than the text in the line. I find that windows are usually larger than the text line, and in that case the -
align setting has no effect. This setting is also used with images, however, where it is more common
to have small images (e.g., for special bullets). Table 33-5 describes the window and image alignment
settings:
Table 33-5. Window and image alignment options.
top
Top lines up with top of text line.
center
Center lines up with center of text line.
baseline
Bottom lines up with text baseline.
bottom
Bottom lines up with bottom of text line.
You can postpone the creation of the embedded widget by specifying a Tcl command that creates the
window, instead of specifying the -window option. The delayed creation is useful if you have lots of
widgets embedded in your text. In this case the Tcl command is evaluated just before the text widget
needs to display the widget. In other words, when the user scrolls the text so the widget will appear,
the Tcl command is run to create the widget:
Example 33-4 Delayed creation of embedded widgets.
$t window create end -create [list MakeGoBack $t]
proc MakeGoBack { t } {
button $t.goback -text "Go to Line 1" \
-command [list $t see 1.0]
}
The MakeGoBack procedure is introduced to eliminate potential quoting problems. If you need to
execute more than one Tcl command to create the widget or if the embedded button has a complex
command, the quoting can quickly get out of hand.
Table 33-6 gives the complete set of options for creating embedded widgets. You can change these
later with the window configure operation. For example:
$t window configure $t.goback -padx 2
Table 33-6. Options to the window create operation.
-align where Alignment: top, center, bottom, or baseline.
-create command Tcl command to create the widget.
-padx pixels Padding on either side of the widget.
-pady pixels Padding above and below the widget.
-stretch boolean If true, the widget is stretched vertically to match the spacing of the text line.
-window pathname Tk pathname of the widget to embed.
You can specify the window to reconfigure by its pathname or the index where the window is located.
In practice, naming the widget by its pathname is much more useful. Note that end is not useful for
identifying an embedded window because the text widget treats end specially. You can insert a
window at end, but end is always updated to be after the last item in the widget. Thus end will never
name the position of an existing window.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
Embedded Images
Tk 8.0 added embedded images that are much like embedded windows. They provide a more efficient
way to add images than creating a canvas or label widget to hold the image. You can also put the same
image into a text widget many times. Example 33-5 uses an image for the bullets in a bulleted list:
Example 33-5 Using embedded images for a bulleted list.
proc BList_Setup { t imagefile } {
global blist
set blist(image) [image create photo -file $imagefile]
$t tag configure bulletlist -tabs ".5c center 1c left" \
-lmargin1 0 -lmargin2 1c
}
proc BList_Item {t text {mark insert}} {
global blist
# Assume we are at the beginning of the line
$t insert $mark \t bulletlist
$t image create $mark -image $blist(image)
$t insert $mark \t$text bulletlist
}
In Example 33-5, tabs are used to line up the bullet and the left edges of the text. The first tab centers
the bullet over a point 0.5 centimeters from left margin. The second tab stop is the same as the -
lmargin2 setting so the text on the first line lines up with the text that wraps onto more lines.
If you update the image dynamically, all the instances of that image in the text widget are updated, too.
This follows from the image model used in Tk, which is described in Chapter 38 on page 541.
The options for embedded images are mostly the same as those for embedded windows. One
difference is that images have a -name option so you can reference an image without remembering its
position in the text widget. You cannot use the image name directly because the same image can be
embedded many times in the text widget. If you do not choose a name, the text widget assigns a name
for you. The image create operation returns this name:
<Meta-b> <Meta-f>
Same as <Control-Left>, <Control-Right>.
<Key-Up> <Control-p>
Moves the cursor up one line. Clears the selection.
<Shift-Up>
Moves the cursor up one line. Extends the selection.
<Control-Up>
Moves the cursor up by paragraphs, which are a group of lines
separated by a blank line.
<Control-Shift-Up>
Moves the cursor up by paragraph. Extends the selection.
<Key-Down> <Control-n>
All Down bindings are analogous to Up bindings.
<Next> <Prior>
Moves the cursor by one screen. Clears the selection.
<Shift-Next> <Shift-Prior>
Moves the cursor by one screen. Extends the selection.
<Home> <Control-a>
Moves the cursor to line start. Clears the selection.
<Shift-Home>
Moves the cursor to line start. Extends the selection.
<End> <Control-e>
Moves the cursor to line end. Clears the selection.
<Shift-End>
Moves the cursor to line end. Extends the selection.
<Control-Home> <Meta-less>
Moves the cursor to the beginning of text. Clears the selection.
<Control-End> <Meta-
greater>
Moves the cursor to the end of text. Clears the selection.
<Select> <Control-space>
Sets the selection anchor to the position of the cursor.
<Shift-Select> <Control-
Shift-space>
Adjusts the selection to the position of the cursor.
<Control-slash>
Selects everything in the text widget.
<Control-backslash>
Clears the selection.
<Delete>
Deletes the selection, if any. Otherwise deletes the character to the
right of the cursor.
<BackSpace> <Control-h>
Deletes the selection, if any. Otherwise deletes the character to the
left of the cursor.
<Control-d>
Deletes character to the right of the cursor.
<Meta-d>
Deletes word to the right of the cursor.
<Control-k>
Deletes from cursor to end of the line. If you are at the end of line,
delete the newline character.
<Control-o>
Inserts a newline but does not advance the cursor.
<Meta-Delete> <Meta-
BackSpace>
Deletes the word to the left of the cursor.
<Control-t>
Transposes the characters on either side of the cursor.
<<Copy>> <Meta-w>
Copies the selection to the clipboard.
<<Cut>> <Control-w>
Cuts the selection and saves it on the clipboard.
<<Paste>> <Control-y>
Pastes from the clipboard.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter33. The Text Widget
Text Operations
Table 33-9 describes the text widget operations, including some that are not discussed in this chapter.
In the table, $t is a text widget:
Table 33-9. Operations for the text widget.
$t bbox index Returns the bounding box of the character at index. Four numbers
are returned: x y width height.
$t cget option Returns the value of the configuration option.
$t compare i1 op i2 Performs index comparison. i1 and i2 are indexes. op is one of <
<= == >= > !=
$t configure ...
Queries or sets configuration options.
$t debug boolean Enables consistency checking for B-tree code.
$t delete i1 ?i2? Deletes from i1 up to, but not including i2. Just deletes the
character at i1 if i2 is not specified.
$t dlineinfo index Returns the bounding box, in pixels, of the display for the line
containing index. Five numbers are returned: x y width height
baseline.
$t dump ?options? i1 ?
i2?
Returns the marks, tags, windows, images, and text contained in the
widget. Options are -all, -command command, -image, -mark, -tag,
-text, and -window.
$t get i1 ?i2? Returns the text from i1 to i2, or just the character at i1 if i2 is not
specified.
$t image cget option Returns the value of the image option.
$t image configure ?
options?
Queries or sets the configuration of an embedded image.
padY
Extra space above and below the text.
relief
flat, sunken, raised, groove, ridge, or solid.
selectBackground
Background color of selected text.
selectForeground
Foreground color of selected text.
selectBorderWidth
Size of 3D border for selection highlight.
setGrid
Enable/disable geometry gridding.
spacing1
Extra space above each unwrapped line.
spacing2
Space between parts of a line that have wrapped.
spacing3
Extra space below an unwrapped line.
state
Editable (normal) or read-only (disabled).
tabs
Tab stops.
takeFocus
Control focus changes from keyboard traversal.
width
Width, in characters, of the text display.
wrap
Line wrap mode: none, char, or word.
xScrollCommand
Tcl command prefix for horizontal scrolling.
yScrollCommand
Tcl command prefix for vertical scrolling.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartIV. Tk Widgets
Chapter 34. The Canvas Widget
The canvas widget is a general-purpose widget that you can program to display a variety of objects
including arcs, images, lines, ovals, polygons, rectangles, text, and embedded windows.
Canvas widgets display objects such as lines and images, and each object can have bindings that
respond to user input, or be animated under program control. The objects can be labeled with tags, and
the tags can be configured with display attributes and event bindings. This chapter describes all the
predefined canvas object types. Chapter 47 outlines the C programming interface for creating new
canvas objects.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter34. The Canvas Widget
Canvas Coordinates
The coordinate space of the canvas has 0, 0 at the top left corner. Larger X coordinates are to the right,
and larger Y coordinates are downward. The position and possibly the size of a canvas object is
determined by a set of coordinates. Different objects are characterized by different numbers of
coordinates. For example, text objects have two coordinates, x1 y1, that specify their anchor point. A
line can have many pairs of coordinates that specify the end points of its segments. The coordinates are
set when the object is created, and they can be updated later with the coords operation. By default,
coordinates are in pixels. Append a coordinate with one of the following letters to change the units:
c centimeters
i inches
m millimeters
p printer points (1/72 inches)
The tk scale command, which is described on page 582, changes the mapping from pixels to other
screen measures. Use it before creating the canvas.
The width and height attributes of the canvas determine the size of the viewable area. The
scrollRegion attribute of the canvas determines the boundaries of the canvas. Its value is four
numbers that specify the upper-left and lower-right coordinates of the canvas. If you do not specify a
scroll region, it defaults to the size of the viewable area. Example 34-1 creates a canvas that has a 1000
by 400 scrolling region, and a 300 by 200 viewing area. The canvas is connected to two scrollbars to
provide horizontal and vertical scrolling:
Example 34-1 A large scrolling canvas.
proc Scrolled_Canvas { c args } {
frame $c
eval {canvas $c.canvas \
-xscrollcommand [list $c.xscroll set] \
-yscrollcommand [list $c.yscroll set] \
-highlightthickness 0 \
-borderwidth 0}$args
scrollbar $c.xscroll -orient horizontal \
-command [list $c.canvas xview]
scrollbar $c.yscroll -orient vertical \
-command [list $c.canvas yview]
grid $c.canvas $c.yscroll -sticky news
grid $c.xscroll -sticky ew
grid rowconfigure $c 0 -weight 1
grid columnconfigure $c 0 -weight 1
return $c.canvas
}
Scrolled_Canvas .c -width 300 -height 200 \
-scrollregion {0 0 1000 400}
=> .c.canvas
pack .c -fill both -expand true
Borders are drawn in the canvas.
The highlight thickness and border width are set to 0 in Example 34-1. Otherwise, these features
occupy some of the canvas viewable area. If you want a raised border for your canvas, either use
another frame, or remember to offset your positions to avoid having objects clipped by the borders.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter34. The Canvas Widget
Hello, World!
Example 34-2 creates an object that you can drag around with the mouse. It introduces the use of tags
to classify objects. In this case the movable tag gets bindings that let you drag the item, so any item
with the movable tag shares this behavior. The example uses Scrolled_Canvas from Example 34-1.
When you use a scrolled canvas, you must map from the view coordinates reported by bindings to the
canvas coordinates used to locate objects:
Example 34-2 The canvas "Hello, World!" example.
proc CanvasHello {} {
set can [Scrolled_Canvas .c -width 400 -height 100 \
-scrollregion {0 0 800 400}]
pack .c -fill both -expand true
# Create a text object on the canvas
$can create text 50 50 -text "Hello, World!" -tag movable
# Bind actions to objects with the movable tag
$can bind movable <Button-1> {CanvasMark %x %y %W}
$can bind movable <B1-Motion> {CanvasDrag %x %y %W}
}
proc CanvasMark {x y can} {
global canvas
# Map from view coordinates to canvas coordinates
set x [$can canvasx $x]
set y [$can canvasy $y]
# Remember the object and its location
set canvas($can,obj) [$can find closest $x $y]
set canvas($can,x) $x
set canvas($can,y) $y
}
proc CanvasDrag {x y can} {
global canvas
# Map from view coordinates to canvas coordinates
set x [$can canvasx $x]
set y [$can canvasy $y]
scrollRegion
Left, top, right, and bottom coordinates of the canvas.
selectBackground
Background color of selection.
selectForeground
Foreground color of selection.
selectBorderWidth
Width of selection border. Nonzero for 3D effect.
takeFocus
Controls focus changes from keyboard traversal.
width
Width in screen units for viewable area.
xScrollCommand
Tcl command prefix for horizontal scrolling.
xScrollIncrement
Distance for one scrolling unit in the X direction.
yScrollCommand
Tcl command prefix for vertical scrolling.
yScrollIncrement
Distance for one scrolling unit in the Y direction.
The scroll region of a canvas defines the boundaries of the canvas coordinate space. It is specified as
four coordinates, x1 y1 x2 y2 where (x1, y1) is the top-left corner and (x2, y2) is the lower-right
corner. If the confine attribute is true, then the canvas cannot be scrolled outside this region. It is OK
to position canvas objects partially or totally off the scroll region; they just may not be visible. The
scroll increment attributes determine how much the canvas is scrolled when the user clicks on the
arrows in the scrollbar.
The closeEnough attribute indicates how far away a position can be from an object and still be
considered to overlap it. This applies to the overlapping search criteria.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter34. The Canvas Widget
Hints
Screen Coordinates vs. Canvas Coordinates
The canvasx and canvasy operations map from a screen coordinate to a canvas coordinate. If the
scroll region is larger than the display area, then you need to use these operations to map from the X
and Y in an event (i.e., %x and %y) and the canvas coordinates. The typical use is:
set id [$c find closest [$c canvasx %x] [$c canvasy %y]]
Large Coordinate Spaces
Coordinates for canvas items are stored internally as floating point numbers, so the values returned by
the coords operation will be floating point numbers. If you have a very large canvas, you may need to
adjust the precision with which you see coordinates by setting the tcl_precision variable. This is an
issue if you query coordinates, perform a computation on them, and then update the coordinates. (Tcl
8.0 changed the default tcl_precision from 6 to 12.)
Scaling and Rotation
The scale operation scales the coordinates of one or more canvas items. It is not possible to scale the
whole coordinate space. The main problem with this is that you can lose precision when scaling and
unscaling objects because their internal coordinates are actually changed by the scale operation. For
simple cases this is not a problem, but in extreme cases it can show up.
The canvas does not support rotation.
Resources
There is no resource database support built into the canvas and its items. You can, however, define
resources and query them yourself. For example, you could define:
*Canvas.foreground: blue
This would have no effect by default. However, your code could look for this resource with option
get, and specify this color directly for the -fill attribute of your objects:
set fg [option get $c foreground {}]
$c create rect 0 0 10 10 -fill $fg
The main reason to take this approach is to let your users customize the appearance of canvas objects
without changing your code.
Objects with Many Points
The canvas implementation seems well optimized to handle lots of canvas objects. However, if an
object like a line or a polygon has many points that define it, the implementation ends up scanning
through these points linearly. This can adversely affect the time it takes to process mouse events in the
area of the canvas containing such an item. Apparently any object in the vicinity of a mouse click is
scanned to see if the mouse has hit it so that any bindings can be fired.
Selecting Canvas Items
Example 35-5 on page 512 implements cut and paste of canvas objects. The example exchanges the
logical description of canvas objects with the selection mechanism.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Part V: Tk Details
Part V describes the rest of the Tk toolkit.
Chapter 35 describes the selection mechanism that is used for cut and paste between
applications. It includes an example that implements cut and paste of graphical objects on a
canvas.
Chapter 36 describes dialogs. Tk has several built-in dialogs that use the native platform look
and feel. The chapter also describes how to build your own dialogs.
Chapter 37 is the first of three chapters that explain widget attributes in more detail. It describes
size and layout attributes. Chapter 38 describes colors, images, and cursors. It explains how to
use the bitmap and color photo image types. The chapter includes a complete map of the cursor
font. Chapter 39 describes fonts and other text-related attributes. The extended example is a font
selection application.
Chapter 40 describes the Tk send command that lets you send commands among Tk
applications. It also presents a socket-based alternative that can be used among applications on
different hosts and with the Safe-Tcl mechanism to limit the power of remotely invoked
commands.
Chapter 41 explains how to interact with the window manager using the wm command. The
chapter describes all the information available through the winfo command.
Chapter 42 builds upon Chapter 28 to create a user preferences package and an associated user
interface. The preference package links a Tcl variable used in your application to a resource
specification.
Chapter 43 presents a user interface to the binding mechanism. You can browse and edit
bindings for widgets and classes with the interface.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartV. Tk Details
Chapter 35. Selections and the Clipboard
Cut and paste allows information exchange between applications, and it is built upon a general
purpose selection mechanism. The CLIPBOARD selection is used to implement cut and paste on all
platforms. X Windows applications may also use the PRIMARY selection. This chapter describes the
selection and clipboard commands.
Copy and paste is a basic way to transfer data between just about any two applications. In Tk, copy and
paste is based on a general selection mechanism where the selection has a name, type, format, and
value. For the most part you can ignore these details because they are handled by the Tk widgets.
However, you can also control the selection explicitly. This chapter describes the selection model and
the selection and clipboard commands. The last section of this chapter presents an example that
implements copy and paste of graphical objects in a canvas.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter35. Selections and the Clipboard
The Selection Model
The Windows and Macintosh selection model is simpler than the selection model used in X windows.
In the Macintosh and Windows there is one selection, although that selection may store different types
of data like text or images. Users copy data from an application into a clipboard, and later they paste it
into another application.
In X windows the selection model is generalized to support more than one selection, and they are
identified by names like PRIMARY and CLIPBOARD. The CLIPBOARD selection is used for copy and paste
as in Macintosh and Windows. The PRIMARY selection is described later. You could use other selection
names, like SECONDARY or FOOBAR, but that only works if the other applications know about that
selection name. The selection data has both a type and a format. These are described briefly later.
Data is not copied into a selection. Instead, an application asserts ownership of a selection, and other
applications request the value of the selection from that owner. This model is used on all platforms.
The window system keeps track of ownership, and applications are informed when some other
application takes away ownership. Several of the Tk widgets implement selections and take care of
asserting ownership and returning its value.
The X PRIMARY selection is used in a way that eliminates the explicit copy step in copy and paste user
actions. Whenever you select an object in your application, your application automatically puts that
value into the PRIMARY selection. The Tk entry, listbox, and text widgets do this with their text
selections, although you can turn this off with the exportSelection widget attribute. Users typically
insert the value of the PRIMARY selection by clicking with the middle mouse button. There is only one
instance of the PRIMARY selection across all widgets and all applications. If the user makes a new
selection it automatically overwrites the previous value of the PRIMARY selection.
The CLIPBOARD is cross-platform.
If you want a mechanism that works on all platforms, use the CLIPBOARD selection. The PRIMARY
selection is implemented by Tk on all platforms, and you can use it within an application, but on
Windows and Macintosh the non-Tk applications do not know about the PRIMARY selection. The main
goal of copy and paste is to provide general interoperability among all applications, so stick with the
CLIPBOARD.
Tk 3.6 and earlier only supported the PRIMARY selection. When Tk 4.0 added support for the
CLIPBOARD, I tried to merge the two selections to "simplify" things for my users. Example 35-1
implements a Paste function that inserts either the PRIMARY or CLIPBOARD selection into a text widget.
The selection get command is used to retrieve the selection value:
Example 35-1 Paste the PRIMARY or CLIPBOARD selection.
proc Paste { text } {
if [catch {selection get}sel] {
if [catch {selection get -selection CLIPBOARD}sel] {
# no selection or clipboard data
return
}
}
$text insert insert $sel
}
This Paste function can be convenient, but it turns out that users still need to keep track of the
difference between the two selections. If a user only understands the CLIPBOARD, then the use of
PRIMARY is only surprising. I learned that it is best to have a separate paste user action for the two
selections. The convention is that <ButtonRelease-2> sets the insert point and inserts the PRIMARY
selection. (This convention is awkward with the one- and two-button mice on Macintosh and
Windows.) The <<Paste>> event (e.g., the Paste key) simply inserts the CLIPBOARD selection at the
current insert point. This convention is shown in Example 35-2, although these bindings are defined
automatically for the text and entry widgets:
Example 35-2 Separate paste actions.
bind Text <<Paste>> {
catch {%W insert insert \
[selection get -selection CLIPBOARD]
}
}
bind Text <ButtonRelease-2> {
%W mark set insert @%x,%y
catch {%W insert insert \
[selection get -selection PRIMARY]
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter35. Selections and the Clipboard
The selection Command
There are two Tcl commands that deal with selections. The selection command is a general-purpose
command that can set and get different selections. By default it manipulates the PRIMARY selection.
The clipboard command stores data for later retrieval using the CLIPBOARD selection.
The selection command exposes the fully general selection model of different selections, types, and
formats. You can define selection handlers that return selection values, and you can assert ownership
of a selection and find out when you lose ownership to another application. Example 35-5 on page 512
shows a selection handler for a canvas.
A selection can have a type. The default is STRING. The type is different than the name of the selection
(e.g., PRIMARY or CLIPBOARD). Each type can have a format, and the default format is also STRING.
Ordinarily these defaults are fine. If you are dealing with non-Tk applications, however, you may need
to ask for their selections by the right type (e.g., FILE_NAME). Formats include STRING, ATOM, and
INTEGER. An ATOM is a name that is registered with the X server and identified by number. It is
probably not a good idea to use non-STRING types and formats because it limits what other applications
can use the information. The details about X selection types and formats are specified in the Inter-
Client Communication Conventions Manual (David Rosenthal, Stuart Marks, X Consortium
Standard). This is distributed with the X11 sources and can be found on the web at
http://tronche.lri.fr:8000/gui/x/icccm/.
All of the selection operations take a -selection option that specifies the name of the selection
being manipulated. This defaults to PRIMARY. Some of the operations take a -displayof option that
specifies what display the selection is on. The value for this option is a Tk pathname of a window, and
the selection on that window's display is manipulated. This is useful in X where applications can have
their windows on remote displays. The default is to manipulate the selection on the display of the main
window. Table 35-1 summarizes the selection command:
Table 35-1. The selection command.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter35. Selections and the Clipboard
Selection Handlers
The selection handle command registers a Tcl command to handle selection requests. The
command is called to return the value of the selection to a requesting application. If the selection value
is large, the command might be called several times to return the selection in pieces. The command
gets two parameters that indicate the offset within the selection to start returning data, and the
maximum number of bytes to return. If the command returns fewer than that many bytes, the selection
request is assumed to be completed. Otherwise, the command is called again to get the rest of the data,
and the offset parameter is adjusted accordingly.
You can also get a callback when you lose ownership of the selection. At that time it is appropriate to
unhighlight the selected object in your interface. The selection own command sets ownership and
registers a callback for when you lose ownership.
A Canvas Selection Handler
Example 35-3 through Example 35-7 implement cut and paste for a canvas. The CanvasSelect_Demo
procedure creates a canvas and sets up some bindings for cut and paste:
Example 35-3 Bindings for canvas selection.
proc CanvasSelect_Demo { c } {
# Create a canvas with a couple of objects
canvas $c
pack $c
$c create rect 10 10 50 50 -fill red -tag object
$c create poly 100 100 100 30 140 50 -fill orange \
-tag object
# Set up cut and paste bindings
$c bind object <Button-1> [list CanvasSelect $c %x %y]
bind $c <Key-Delete> [list CanvasDelete $c]
bind $c <<Cut>> [list CanvasCut $c]
bind $c <<Copy>> [list CanvasCopy $c]
bind $c <<Paste>> [list CanvasPaste $c]
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter36. Focus, Grabs, and Dialogs
Standard Dialogs
The tk_dialog command presents a choice of buttons and returns a number indicating which one was
clicked by the user. The general form of the command is:
tk_dialog win title text bitmap default ?label? ?label? ...
The title appears in the title bar, and the text appears in the dialog. The bitmap appears to the left of
the text. Specify {} for the bitmap if you do not want one. The set of built-in bitmaps is given on page
541. The label arguments give labels that appear on buttons along the bottom of the dialog. The
default argument gives the index of the default button, counting from zero. If there is no default,
specify {} or -1.
Message Box
The tk_messageBox dialog is a limited form of tk_dialog that has native implementations on the
different platforms. Like tk_dialog, it allows for a message, bitmap, and a set of buttons. However,
the button sets are predefined, and the bitmaps are limited. The yesno button set, for example, displays
a Yes and a No button. The abortretryignore button set displays Abort, Retry, and Ignore buttons.
The tk_messageBox command returns the symbolic name of the selected button (e.g., yes or retry.)
The yesnocancel message box could be used when trying to quit with unsaved changes:
set choice [tk_messageBox -type yesnocancel -default yes \
-message "Save changes before quitting?" \
-icon question]
The complete set of options to tk_messageBox is listed in Table 36-1:
Table 36-1. Options to tk_messageBox.
you click on them with the left mouse button. You can get the focus-follows-mouse model within your
widgets by calling the tk_focusFollowsMouse procedure. However, in many cases you will find that
an explicit focus model is actually more convenient for users. Carefully positioning the mouse over a
small widget can be tedious.
The focus Command
Table 36-4 summarizes the focus command. The focus implementation supports multiple displays
with a separate focus window on each display. This is useful on UNIX where X supports multiple
displays. The -displayof option can be used to query the focus on a particular display. The -lastfor
option finds out what widget last had the focus within the same toplevel as another window. Tk will
restore focus to that window if the widget that has the focus is destroyed. The toplevel widget gets the
focus if no widget claims it.
Table 36-4. The focus command.
focus
Returns the widget that currently has the focus on the display of the
application's main window.
focus ?-force?
window
Sets the focus to window. The -force option ignores the window manger, so
use it sparingly.
focus -displayof
win
Returns the focus widget on the same display as win.
focus -lastfor win Returns the name of the last widget to have the focus in the same toplevel as
win.
Keyboard Focus Traversal
Users can change focus among widgets with <Tab> and <Shift-Tab>. The creation order of widgets
determines a traversal order for focus that is used by the tk_focusNext and tk_focusPrev
procedures. There are global bindings for <Tab> and <Shift-Tab> that call these procedures:
bind all <Tab> {tk_focusNext %W}
bind all <Shift-Tab> {tk_focusPrev %W}
The Tk widgets highlight themselves when they have the focus. The highlight size is controlled with
the highlightThickness attribute, and the color of the highlight is set with the highlightColor
attribute. The Tk widgets, even buttons and scrollbars, have bindings that support keyboard
interaction. A <space> invokes the command associated with a button, if the button has the input
focus.
All widgets have a takeFocus attribute that the tk_focusNext and tk_focusPrev procedures use to
determine if a widget will take the focus during keyboard traversal. There are four possible values to
the attribute:
0 indicates the widget should not take focus.
1 indicates the widget should always take focus.
An empty string means the traversal procedures tk_focusNext and tk_focusPrev should decide
based on the widget's state and bindings.
Otherwise the value is a Tcl command prefix. The command is called with the widget name as an
argument, and it should return either 0, 1, or the empty string.
Grabbing the Focus
An input grab overrides the normal focus mechanism. For example, a dialog box can grab the focus so
that the user cannot interact with other windows in the application. The typical scenario is that the
application is performing some task but it needs user input. The grab restricts the user's actions so it
cannot drive the application into an inconsistent state. A global grab prevents the user from interacting
with other applications, too, even the window manager. Tk menus use a global grab, for example,
which is how they unpost themselves no matter where you click the mouse. When an application
prompts for a password, a global grab is also a good idea. This prevents the user from accidentally
typing their password into a random window. Table 36-5 summarizes the grab command.
Table 36-5. The grab command.
grab ?-global?
window
Sets a grab to a particular window.
grab current ?
window?
Queries the grabs on the display of window, or on all displays if window is
omitted.
grab release window Releases a grab on window.
grab set ?-global?
win
Sets a grab to a particular window.
grab status window Returns none, local, or global.
In most cases you only need to use the grab and grab release commands. Note that the grab set
command is equivalent to the grab command. The next section includes examples that use the grab
command.
The tkwait Command
You wait for the user to interact with the dialog by using the tkwait command. The tkwait waits for
something to happen, and while waiting it allows events to be processed. Like vwait, you can use
tkwait to wait for a Tcl variable to change value. You can also wait for a window to become visible,
or wait for a window to be destroyed. Table 36-6 summarizes the tkwait command.
Table 36-6. The tkwait command.
tkwait variable
varname
Waits for the global variable varname to be set. This is just like the vwait
command.
tkwait visibility win Waits for the window win to become visible.
tkwait window win Waits for the window win to be destroyed.
Use tkwait with global variables.
The variable specified in the tkwait variable command must be a global variable. Remember this if
you use procedures to modify the variable. They must declare it global or the tkwait command will
not notice the assignments.
The tkwait visibility waits for the visibility state of the window to change. Most commonly this is
used to wait for a newly created window to become visible. For example, if you have any sort of
animation in a complex dialog, you could wait until the dialog is displayed before starting the
animation.
Destroying Widgets
The destroy command deletes one or more widgets. If the widget has children, all the children are
destroyed, too. Chapter 41 describes a protocol on page 572 to handle destroy events that come from
the window manager. You wait for a window to be deleted with the tkwait window command.
The focus, grab, tkwait sequence
In practice, I use a slightly more complex command sequence than just focus, grab, and tkwait. You
can remember what widget had focus before the dialog is created so that focus can be restored after the
dialog completes. When you do this, it is more reliable to restore focus before destroying the dialog.
This prevents a tug of war between your application and the window manager. This sequence looks
like:
set old [focus]
focus $toplevel
grab $toplevel
tkwait variable doneVar
grab release $toplevel
focus $old
destroy $toplevel
This sequence supports another trick I use, which is to unmap dialogs instead of destroying them. This
way the dialogs appear more quickly the next time they are used. This makes creating the dialogs a
little more complex because you need to see if the toplevel already exists. Chapter 41 describes the
window manager commands used to map and unmap windows on page 572. Example 36-1 shows
Dialog_Create, Dialog_Wait, and Dialog_Dismiss that capture all of these tricks:
Example 36-1 Procedures to help build dialogs.
proc Dialog_Create {top title args} {
global dialog
if [winfo exists $top] {
switch -- [wm state $top] {
normal {
# Raise a buried window
raise $top
}
withdrawn -
iconic {
# Open and restore geometry
wm deiconify $top
catch {wm geometry $top $dialog(geo,$top)}
}
}
return 0
} else {
eval {toplevel $top}$args
wm title $top $title
return 1
}
}
proc Dialog_Wait {top varName {focus {}}} {
upvar $varName var
# Poke the variable if the user nukes the window
bind $top <Destroy> [list set $varName cancel]
# Grab focus for the dialog
if {[string length $focus] == 0} {
set focus $top
}
set old [focus -displayof $top]
focus $focus
catch {tkwait visibility $top}
catch {grab $top}
# Wait for the dialog to complete
tkwait variable $varName
catch {grab release $top}
focus $old
}
proc Dialog_Dismiss {top} {
global dialog
# Save current size and position
catch {
# window may have been deleted
set dialog(geo,$top) [wm geometry $top]
wm withdraw $top
}
}
The Dialog_Wait procedure allows a different focus widget than the toplevel. The idea is that you can
start the focus out in the appropriate widget within the dialog, such as the first entry widget.
Otherwise, the user has to click in the dialog first.
Grab can fail.
The catch statements in Dialog_Wait come from my experiences on different platforms. The tkwait
visibility is sometimes required because grab can fail if the dialog is not yet visible. However, on
other systems, the tkwait visibility itself can fail in some circumstances. Tk reflects these errors,
but in this case all that can go wrong is no grab. The user can still interact with the dialog without a
grab, so I just ignore these errors.
Prompter Dialog
Example 36-2 A simple dialog.
proc Dialog_Prompt { string } {
global prompt
set f .prompt
if [Dialog_Create $f "Prompt" -borderwidth 10] {
message $f.msg -text $string -aspect 1000
entry $f.entry -textvariable prompt(result)
set b [frame $f.buttons]
pack $f.msg $f.entry $f.buttons -side top -fill x
pack $f.entry -pady 5
button $b.ok -text OK -command {set prompt(ok) 1}
button $b.cancel -text Cancel \
-command {set prompt(ok) 0}
pack $b.ok -side left
pack $b.cancel -side right
bind $f.entry <Return> {set prompt(ok) 1 ; break}
bind $f.entry <Control-c> {set prompt(ok) 0 ; break}
}
set prompt(ok) 0
Dialog_Wait $f prompt(ok) $f.entry
Dialog_Dismiss $f
if {$prompt(ok)} {
return $prompt(result)
} else {
return {}
}
}
Dialog_Prompt "Please enter a name"
The Dialog_Prompt dialog gets a value from the user, returning the value entered, or the empty string
if the user cancels the operation. Dialog_Prompt uses the Tcl variable prompt(ok) to indicate the
dialog is complete. The variable is set if the user presses the OK or Cancel buttons, or if the user
presses <Return> or <Control-c> in the entry widget. The Dialog_Wait procedure waits on
prompt(ok), and it grabs and restores focus. If the Dialog_Create procedure returns 1, then the dialog
is built: otherwise, it already existed.
Keyboard Shortcuts and Focus
Focus is set on the entry widget in the dialog with Dialog_Wait, and it is convenient if users can use
special key bindings to complete the dialog. Otherwise, they need to take their hands off the keyboard
and use the mouse. The example defines bindings for <Return> and <Control-c> that invoke the OK
and Cancel buttons, respectively. The bindings override all other bindings by including a break
command. Otherwise, the Entry class bindings insert the short-cut keystroke into the entry widget.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter36. Focus, Grabs, and Dialogs
Animation with the update Command
Suppose you want to entertain your user while your application is busy. By default, the user interface
hangs until your processing completes. Even if you change a label or entry widget in the middle of
processing, the updates to that widget are deferred until an idle moment. The user does not see your
feedback, and the window is not refreshed if it gets obscured and uncovered. The solution is to use the
update command that forces Tk to go through its event loop and update the display.
The next example shows a Feedback procedure that displays status messages. A read-only entry
widget displays the messages, and the update command ensures that the user sees each new message.
An entry widget is used because it won't change size based on the message length, and it can be
scrolled by dragging with the middle mouse button. Entry widgets also work better with update
idletasks as described later:
Example 36-3 A feedback procedure.
proc Feedback { message } {
global feedback
set e $feedback(entry)
$e config -state normal
$e delete 0 end
$e insert 0 $message
# Leave the entry in a read-only state
$e config -state disabled
# Force a display update
update idletasks
}
The Tk widgets update their display at idle moments, which basically means after everything else is
taken care of. This lets them collapse updates into one interaction with the window system. On UNIX,
this improves the batching effects that are part of the X protocol. A call to update idletasks causes
any pending display updates to be processed. Chapter 16 describes the Tk event loop in more detail.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter37. Tk Widget Attributes
Configuring Attributes
You specify attributes for Tk widgets when you create them. You can also change them dynamically at
any time after that. In both cases the syntax uses pairs of arguments. The first item in the pair identifies
the attribute, the second provides the value. For example, a button can be created like this:
button .doit -text Doit -command DoSomething
The name of the button is .doit, and two attributes are specified: the text and the command. You can
change the .doit button later with the configure widget operation:
.doit configure -text Stop -command StopIt
The current configuration of a widget can be queried with another form of the configure operation. If
you just supply an attribute, the settings associated with that attribute are returned:
.doit configure -text
=> -text text Text {} Stop
This command returns several pieces of information: the command line switch, the resource name, the
resource class, the default value, and the current value. If you don't give any options to configure, then
the configuration information for all the attributes is returned. The following loop formats the
information:
foreach item [$w configure] {
puts "[lindex $item 0] [lindex $item 4]"
}
If you just want the current value, use the cget operation:
aspect
The aspect ratio of a message widget, which is 100 times the ratio of width divided by
height.
height
Height, in text lines or screen units. Widgets: button, canvas, checkbutton, frame,
label, listbox, menubutton, radiobutton, text, and toplevel.
length
The long dimension of a scale.
orient
Orientation for long and narrow widgets: horizontal or vertical. Widgets: scale and
scrollbar.
width
Width, in characters or screen units. Widgets: button, canvas, checkbutton, entry,
frame, label, listbox, menubutton, message, radiobutton, scale, scrollbar,
text, and toplevel.
It is somewhat unfortunate that text-oriented widgets only take character- and line-oriented
dimensions. These sizes change with the font used, and if you want a precise size you might be
frustrated. Both pack and grid let the widgets decide how big to be. One trick is to put each widget,
such as a label, in its own frame. Specify the size you want for the frame, and then pack the label and
turn off size propagation. For example:
Example 37-1 Equal-sized labels.
proc EqualSizedLabels { parent width height strings args } {
set l 0
foreach s $strings {
frame $parent.$l -width $width -height $height
pack propagate $parent.$l false
pack $parent.$l -side left
eval {label $parent.$l.l -text $s} $args
pack $parent.$l.l -fill both -expand true
incr l
}
}
frame .f ; pack .f
EqualSizedLabels .f 1i 1c {apple orange strawberry kiwi} \
-relief raised
The frames $parent.$l are all created with the same size. The pack propagate command prevents
these frames from changing size when the labels are packed into them later. The labels are packed
with fill and expand turned on so that they fill up the fixed-sized frames around them.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter37. Tk Widget Attributes
Borders and Relief
Example 37-2 illustrates the different reliefs:
Example 37-2 3D relief sampler.
frame .f -borderwidth 10
pack .f
foreach relief {raised sunken flat ridge groove solid} {
label .f.$relief -text $relief -relief $relief \
-bd 2 -padx 3
pack .f.$relief -side left -padx 4
}
The three-dimensional appearance of widgets is determined by two attributes: borderWidth and
relief. The borderWidth adds extra space around the edge of a widget's display, and this area can be
displayed in a number of ways according to the relief attribute. The solid relief was added in Tk 8.0
to support the Macintosh look for entry widget, and it works well against white backgrounds.
Macintosh and Windows buttons do not support different reliefs, and the Macintosh buttons do not
honor border width.
The activeBorderWidth attribute is a special case for menus. It defines the border width for the menu
entries. The relief of a menu is not configurable. It probably is not worth adjusting the menu border
width attributes because the default looks OK. The native menus on Windows and Macintosh do not
honor this attribute.
The activeRelief applies to the elements of a scrollbar (the elevator and two arrows) when the
mouse is over them. The elementBorderWidth sets the size of the relief on these elements. Changing
the activeRelief does not look good. The native scrollbars on Macintosh and Windows do not honor
this attribute. Table 37-2 lists the attributes for borders and relief.
Table 37-2. Border and relief attribute resource names.
borderWidth
The width of the border around a widget, in screen units.
Widgets: button, canvas, checkbutton, entry, frame, label,
listbox, menu, menubutton, message, radiobutton, scale,
scrollbar, text, and toplevel.
bd
Short for borderwidth. Tcl commands only.
elementBorderWidth
The width of the border on scrollbar and scale elements.
relief
The appearance of the border.
Values: flat, raised, sunken, ridge, groove, or solid.
Widgets: button, canvas, checkbutton, entry, frame, label,
listbox, menubutton, message, radiobutton, scale, scrollbar,
text, and toplevel.
activeBorderWidth
The border width for menu entries. UNIX only.
activeRelief
The relief for active scrollbar elements. UNIX only.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter37. Tk Widget Attributes
The Focus Highlight
Each widget can have a focus highlight indicating which widget currently has the input focus. This is a
thin rectangle around each widget that is displayed in the highlight background color by default. When
the widget gets the input focus, the highlight rectangle is displayed in an alternate color. The addition
of the highlight adds a small amount of space outside the border described in the previous section. The
attributes in Table 37-3 control the width and color of this rectangle. If the width is zero, no highlight
is displayed.
By default, only the widgets that normally expect input focus have a nonzero width highlight border.
This includes the text, entry, and listbox widgets. It also includes the button and menu widgets
because there is a set of keyboard traversal bindings that focus input on these widgets, too. You can
define nonzero highlight thicknesses for all widgets except Macintosh buttons.
Table 37-3. Highlight attribute resource names.
highlightColor
The color of the highlight when the widget has focus.
highlightBackground
The highlight color when the widget does not have focus.
highlightThickness
The width of the highlight border.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter37. Tk Widget Attributes
Padding and Anchors
Table 37-4 lists padding and anchor attributes that are similar in spirit to some packing attributes
described in Chapter 23. However, they are distinct from the packing attributes, and this section
explains how they work together with the packer
Table 37-4. Layout attribute resource names.
anchor
The anchor position of the widget.
Values: n, ne, e, se, s, sw, w, nw, or center.
Widgets: button, checkbutton, label, menubutton, message, or radiobutton.
padX,
padY
Padding space in the X or Y direction, in screen units.
Widgets: button, checkbutton, label, menubutton, message, radiobutton,
or text.
The padding attributes for a widget define space that is never occupied by the display of the widget's
contents. For example, if you create a label with the following attributes and pack it into a frame by
itself, you will see the text is still centered, despite the anchor attribute.
Example 37-3 Padding provided by labels and buttons.
label .foo -text Foo -padx 20 -anchor e
pack .foo
The anchor attribute only affects the display if there is extra room for another reason. One way to get
extra room is to specify a width attribute that is longer than the text. The following label has right-
justified text. You can see the default padX value for labels, which is one pixel:
Example 37-4 Anchoring text in a label or button.
label .foo -text Foo -width 10 -anchor e
pack .foo
Another way to get extra display space is with the -ipadx and -ipady packing parameters. The
example in the next section illustrates this effect. Chapter 23 has several more examples of the
packing parameters.
Putting It All Together
The number of different attributes that contribute to the size and appearance can
be confusing. The example in this section uses a label to demonstrate the
difference among size, borders, padding, and the highlight. Padding can come
from the geometry manager, and it can come from widget attributes:
Example 37-5 Borders and padding.
frame .f -bg white
label .f.one -text One -relief raised -bd 2 -padx 3m -pady 2m
pack .f.one -side top
label .f.two -text Two \
-highlightthickness 4 -highlightcolor red \
-borderwidth 5 -relief raised \
-padx 0 -pady 0 \
-width 10 -anchor nw
pack .f.two -side top -pady 10 -ipady 10 -fill both
focus .f.two
pack .f
The first label in the example uses a raised relief, so you can see the two-pixel border. (The default
border size changed in Tk 8.0 to one pixel to match the CDE look and feel.) There is no highlight on a
label by default. There is internal padding so that the text is spaced away from the edge of the label.
The second label adds a highlight rectangle by specifying a nonzero thickness. Widgets like buttons,
entries, listboxes, and text have a highlight rectangle by default. The second label's padding attributes
are reduced to zero. The anchor positions the text right next to the border in the upper-left (nw) corner.
However, note the effect of the padding provided by the packer. There is both external and internal
padding in the Y direction. The external padding (from pack -pady) results in unfilled space. The
internal packing (pack -ipady) is used by the label for its display. This is different from the label's
own -pady attribute, which keeps the text away from the top edge of the widget.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartV. Tk Details
Chapter 38. Color, Images, and Cursors
This chapter describes the color attributes shared by the Tk widgets. Images and bitmaps can be
displayed instead of text by several widgets. This chapter describes commands that create and
manipulate images. The cursor attribute controls the shape and color of the mouse cursor when it is
over a particular widget. This chapter includes a figure that shows all the cursors available in Tk.
Color is one of the most fun things to play with in a user interface. However, this chapter makes no
attempt to improve your taste in color choices; it just describes the attributes that affect color. The
tradition of having users change application colors is stronger in UNIX than on the other platforms.
This is because all the X toolkits support color tuning via the resource database. Tk carries this
tradition to Windows and Macintosh. However, if native look and feel is important, you should not
change the default widget colors. On the other hand, tuning colors can provide a flair to your
applications, and knowledge of colors is useful for canvas applications.
This chapter describes images, too. The image facility in Tk lets you create an image and then have
other Tk widgets display it. The same image can be displayed by many different widgets, multiple
times on a canvas, and multiple times within the text widget. If you redefine an image, its display is
updated in whatever widgets are displaying it.
The last topic of the chapter is cursors. All widgets can control what the mouse cursor looks like when
it is over them. In addition, the widgets that support text input define another cursor, the insert cursor.
Its appearance is controlled with a few related attributes.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter38. Color, Images, and Cursors
Colors
Table 38-1 lists the resource names for color attributes. The table indicates what widgets use the
different color attributes. Remember to use all lowercase and a leading dash when specifying attributes
in a Tcl command.
Table 38-1. Color attribute resource names.
background
The normal background color. All widgets.
bg
Short for background. Command line only.
foreground
The normal foreground color. Widgets: button, checkbutton, entry,
label, listbox, menu, menubutton, message, radiobutton, scale,
and text.
fg
Short for foreground. Command line only.
activeBackground
The background when a mouse button will take an action. Widgets:
button, checkbutton, menu, menubutton, radiobutton, scale, and
scrollbar.
activeForeground
The foreground when the mouse is over an active widget. Widgets: button,
checkbutton, menu, menubutton, and radiobutton.
disabledForeground
The foreground when a widget is disabled. Widgets: button,
checkbutton, menu, menubutton, and radiobutton.
highlightBackground
The highlight color when widget does not have focus. All widgets.
highlightColor
The highlight color when the widget has focus. All widgets.
insertBackground
The color of the insert cursor. Widgets: canvas, entry, and text.
selectBackground
The background of selected text. Widgets: canvas, entry, listbox, and
text.
selectColor
The color of the selector indicator. Widgets: checkbutton, and
radiobutton.
selectForeground
The foreground of selected text. Widgets: canvas, entry, listbox, and
text.
troughColor
The trough part of scales and scrollbars.
The foreground color is used to draw an element, while the background color is used for the blank
area behind the element. Text, for example, is painted with the foreground color. There are several
variations on foreground and background that reflect different states for widgets or items they are
displaying.
Each attribute also has a resource class. This is most useful for the variations on foreground and
background colors. For example, Tk does not have a reverse video mode. However, with a couple of
resource specifications you can convert a monochrome display into reverse video. The definitions are
given in Example 38-1. The Foreground and Background resource class names are used, and the
various foreground and background colors (e.g., activeBackground) have the correct resource class so
these settings work. You have to set these resources before you create any widgets:
Example 38-1 Resources for reverse video.
proc ReverseVideo {} {
option add *Foreground white
option add *Background black
}
Color Palettes
The tk_setPalette command changes colors of existing widgets and installs resource values so new
widgets have matching colors. If you give it a single argument, it treats this as the background and
then computes new values for the other color resources. For example, if you do not like the standard
Tk grey, you can lighten your spirits with a cool blue background:
tk_setPalette #0088cc
If you liked the light brown color scheme of Tk 3.6, you can restore that palette with the tk_bisque
command:
tk_bisque
The tk_setPalette command can be used to change any of the color attributes. You can specify a set
of name-value pairs, where the names are color resource names and the values are new color values:
tk_setPalette activeBackground red activeForeground white
Color Values
Color values are specified in two ways: symbolically (e.g., red), or by hexadecimal numbers (e.g.,
#ff0000). The leading # distinguishes the hexadecimal representation from the symbolic one. The
number is divided into three equal-sized fields that give the red, green, and blue values, respectively.
The fields can specify 4, 8, 12, or 16 bits of a color:
#RGB 4 bits per color
#RRGGBB 8 bits per color
#RRRGGGBBB 12 bits per color
#RRRRGGGGBBBB 16 bits per color
If you specify more resolution than is supported by the display, the low-order bits of each field are
discarded. The different display types supported by Tk are described in the next section. Each field
ranges from 0, which means no color, to a maximum, which is all ones in binary, or all f in hex, that
means full color saturation. For example, pure red can be specified four ways:
#f00 #ff0000 #fff000000 #ffff00000000
There is a large collection of symbolic color names like "red," "blue," "green," "thistle," "medium sea
green," and "yellow4." These names originate from X and UNIX, and Tk supports these colors on all
platforms. You can find the list in the Tk sources in the xlib/xcolor.c file. Or, run the xcolors
program that comes with the standard X distribution.
The Windows and Macintosh platforms have a small set of colors that are guaranteed to exist, and Tk
defines names for these. The advantage of using these colors is that they are shared by all applications,
so the system can manage colors efficiently. Table 38-2 lists the system colors on Windows. Several of
these colors map to the same RGB value. Table 38-3 lists the system colors on Macintosh.
Table 38-2. Windows system colors.
system3dDarkShadow
Dark part of button 3D-relief.
system3dLight
Light part of button 3D-relief.
systemActiveBorder
Window border when activated.
systemActiveCaption
Caption (i.e., title bar) when activated.
systemAppWorkspace
Background for MDI workspaces.
systemBackground
Widget background.
systemButtonFace
Button background.
systemButtonHighlight
Lightest part of button 3D-relief.
systemButtonShadow
Darkest part of button 3D-relief.
systemButtonText
Button foreground.
systemCaptionText
Caption (i.e., title bar) text.
systemDisabledText
Text when disabled.
systemGrayText
Grey text color.
systemHighlight
Selection background.
systemHighlightText
Selection foreground.
systemInactiveBorder
Window border when not activated.
systemInactiveCaption
Caption background when not activated.
systemInactiveCaptionText
Caption text when not activated.
systemInfoBackground
Help pop-up background.
systemInfoText
Help pop-up text.
systemMenu
Menu background.
systemMenuText
Menu foreground.
systemScrollbar
Scrollbar background.
systemWindow
Text window background.
systemWindowFrame
Text window frame.
systemWindowText
Text window text color.
Table 38-3. Macintosh system colors.
systemHighlight
Selection background.
systemHighlightText
Selection foreground.
systemButtonFace
Button background.
systemButtonFrame
Button frame.
systemButtonText
Button foreground.
systemWindowBody
Widget background.
systemMenuActive
Selected menu item background.
systemMenuActiveText
Selected menu item foreground.
systemMenu
Menu background.
systemMenuDisabled
Disabled menu item background.
systemMenuText
Menu foreground.
Getting RGB values.
The winfo rgb command maps from a color name (or value) to three numbers that are its red, green,
and blue values. You can use this to compute variations on a color. The ColorDarken procedure
shown below uses the winfo rgb command to get the red, green, and blue components of the input
color. It reduces these amounts by 5 percent, and reconstructs the color specification using the format
command.
Example 38-2 Computing a darker color.
proc ColorDarken { win color } {
set rgb [winfo rgb $win $color]
return [format "#%03x%03x%03x" \
[expr round([lindex $rgb 0] * 0.95)] \
[expr round([lindex $rgb 1] * 0.95)] \
[expr round([lindex $rgb 2] * 0.95)]]
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter38. Color, Images, and Cursors
Colormaps and Visuals
Computer screens can display only a fixed number of different colors at one time. The best monitors
can display 24 million colors, but it is common to find 256 color displays. Really old VGA displays
only display 16 colors. If you run several applications at once, it is possible that more colors are
requested than can be displayed. The Windows and Macintosh platforms manage this scenario
automatically. X provides lower-level facilities that Tk uses on UNIX to do the management. So, for
the most part you don't have to worry. However, if you need more control, especially under X, then
you need to understand colormaps and the different visual types.
Each pixel on the screen is represented by one or more bits of memory. There are a number of ways to
map from a value stored at a pixel to the color that appears on the screen at that pixel. The mapping is
a function of the number of bits at each pixel, which is called the depth of the display, and the style of
interpretation, or visual class. The six visual classes defined by X are listed in the following table.
Some of the visuals use a colormap that maps from the value stored at a pixel to a value used by the
hardware to generate a color. A colormap enables a compact encoding for a much richer color. For
example, a 256-entry colormap can be indexed with 8 bits, but it may contain 24 bits of color
information. The UNIX xdpyinfo program reports the different visual classes supported by your
display. Table 38-4 lists the visual classes:
Table 38-4. Visual classes for displays.
staticgrey
Greyscale with a fixed colormap defined by the system.
greyscale
Greyscale with a writable colormap.
staticcolor
Color with a fixed colormap defined by the system.
pseudocolor
Color values determined by single writable colormap.
truecolor
Color values determined by three colormaps defined by the system: one each for red,
green, and blue.
directcolor
Color values determined by three writable colormaps: one each for red, green, and
blue.
best
Use the best visual for a given depth.
The frame and toplevel widgets support a colormap and visual attribute. You can query these
attributes on all platforms. On Windows and Macintosh there is only one visual type at a time, and
users may be able to change it for their whole system. On UNIX, the X server typically supports more
than one visual class on the same display, and you can create frames and toplevels that use a particular
visual class. The value of the visual attribute has two parts, a visual type and the desired depth of the
display. The following example requests a greyscale visual with a depth of 4 bits per pixel:
toplevel .grey -visual "greyscale 4"
You can start wish with a -visual command line argument:
wish -visual "truecolor 24"
A visual is associated with a colormap. Windows and Macintosh have a single colormap that is shared
by all applications. UNIX allows for private colormaps, which can be useful if you absolutely must
have lots of colors. However, the drawback of a private colormap is that the display flashes as the
mouse enters windows with their own colormap. This is because the monitor hardware really only has
one colormap, so the X server must swap colormaps. Macintosh and Windows manage their colormap
more gracefully, although if you use too many colors some flashing can occur. Tk can simulate private
colormaps on Windows, but it is probably better to let the system manage the colormap. Tk on the
Macintosh always uses a 24-bit truecolor visual, which is basically unlimited colors, and lets the
operating system dither colors if necessary.
By default a widget inherits the colormap and visual from its parent widget. The value of the
colormap attribute can be the keyword new, in which case the frame or toplevel gets a new private
colormap, or it can be the name of another widget, in which case the frame or toplevel shares the
colormap of that widget. When sharing colormaps, the other widget must be on the same screen and
using the same visual class.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter38. Color, Images, and Cursors
Bitmaps and Images
The label and all the button widgets have an image attribute that specifies a graphic image to display.
Using an image takes two steps. In the first step the image is created via the image create command.
This command returns an identifier for the image, and it is this identifier that is passed to widgets as
the value of their image attribute.
Example 38-3 Specifying an image for a widget.
set im [image create bitmap \
-file glyph.bitmap -maskfile glyph.mask \
-background white -foreground blue]
button .foo -image $im
There are three things that can be displayed by labels and all the buttons: text, bitmaps, and images. If
more than one of these attributes are specified, then the image has priority over the bitmap, and the
bitmap has priority over the text. You can remove the image or bitmap attribute by specifying a null
string for its value:
.foo config -image {}
The image Command
Table 38-5 summarizes the image command.
Table 38-5. Summary of the image command.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter38. Color, Images, and Cursors
The Mouse Cursor
The cursor attribute defines the mouse cursor. Example 38-6 shows the cursors
that come built into Tk:
Example 38-6 The Tk cursors.
A foreground and background color for the cursor can be specified. Here are some example cursor
specifications:
$w config -cursor watch ;# stop-watch cursor
$w config -cursor {gumby blue} ;# blue gumby
$w config -cursor {X_cursor red white} ;# red X on white
The other form for the cursor attribute specifies a file that contains the definition of the cursor bitmap.
If two file names are specified, then the second specifies the cursor mask that determines what bits of
the background get covered up. Bitmap editing programs like idraw and iconedit can be used to
generate these files. Here are some example cursor specification using files. You need to specify a
foreground color, and if you specify a mask file, then you also need to specify a background color:
$w config -cursor "@timer.xbm black"
$w config -cursor "@timer.xbm timer.mask black red"
The cursors shown in Example 38-6 are available on all platforms. However, on Windows and
Macintosh some of the cursors are mapped to native cursors and appear differently. On Windows the
following cursors are mapped to native cursors: arrow, ibeam, icon, crosshair, fleur,
sb_v_double_arrow, sb_h_double_arrow, center_ptr, watch, and xterm. These additional cursors
are defined on Windows: starting, size, size_ne_sw, size_ns, size_nw_se, size_we, uparrow,
and wait. On Windows, use the no cursor to eliminate the cursor. On Macintosh, the following cursors
are mapped to native cursors: ibeam, xterm, cross, crosshair, plus, watch, arrow. These additional
cursors are defined on Macintosh: text and cross-hair.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartV. Tk Details
Chapter 39. Fonts and Text Attributes
This chapter describes the naming conventions for fonts. Tk has a font object that you can dynamically
configure and associate with widgets. This chapter also describes other text-related attributes such as
justification, anchoring, and geometry gridding.
Fonts describe how characters look on the screen. Tk widgets like buttons, labels, and listboxes have a
font attribute that determines which font they use to display their text. The text widget has font
attributes on tags that are applied to different regions of text. Tk has a platform-independent way to
name fonts (e.g., times 12 bold), plus it gracefully handles missing fonts. You can define named font
objects and then associate those with widgets and text tags. When the font objects are reconfigured,
the widgets using them update their display automatically. You can use the resource database to define
the fonts used in your interface.
X font names (e.g., -*-times-bold-r-normal-*-12-*) were used in versions of Tk before 8.0, and
the widgets would raise errors if a font could not be found. The X names have a pattern matching
scheme that helps avoid some missing font errors. You can still use X font names in current versions
of Tk. However, the Tk font system does not do font substitutions if you use X font names; if you use
them, you must be prepared for errors. In general, you should use the platform-independent font
names.
After describing fonts, the chapter explains a few of the widget attributes that relate to fonts. This
includes justification, anchors, and geometry gridding.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter39. Fonts and Text Attributes
Naming a Font
There are two basic ways to name a font. You can use predefined font names (e.g., system), or you can
specify a set of font attributes with a platform-independent name:
label .foo -text "Hello" -font {times 12 bold}
In this form, the font is specified with a three element list. The first element is the font family, the
second is the size, in points, and the third is a list of style parameters. The family determines the basic
look, such as courier or helvetica.
The complete set of style parameters are normal, bold, roman, italic, underline, and overstrike.
For example, to specify both bold and italic:
label .foo -text "Hello" -font {times 12 {bold italic}}
The font size is points, which are 1/72 inch. Tk maintains a scale factor that maps from points to
pixels. The default scale is derived from the screen resolution, and you can change it with the tk
scaling command, which is described on page 582. You can specify pixel-based sizes with negative
numbers. The advantage of points over pixels is that text appears about the same size regardless of the
screen resolution. (This works better on Windows and Macintosh than on Unix.) However, sometimes
you want to control font size relative to other widget geometry, in which case pixel-based sizes are
better.
An alternate way to name font attributes uses name-value pairs. These are summarized in Table 39-1.
The format is less compact, but it is useful for changing part of a font configuration because you do
not need to specify everything. The same specification can be made like this:
label .foo -text "Hello" -font \
{-family times -size 12 -weight bold -slant italic}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter39. Fonts and Text Attributes
The font Command
Table 39-3 summarizes the font command. In the table, font is either a description of font
parameters, a logical font name, a system font name, or an X font name. The -displayof option
applies to X where you can have windows on different displays that can support different fonts. Note
that when you delete a logical font name with font delete, the font is not really deleted if there are
widgets that use that font.
Table 39-3. The font command.
font actual font ?-displayof window? ?
option?
Returns the actual parameters of font.
font configure fontname ?option? ?value
option value?
Sets or queries the parameters for fontname.
font create ?fontname? ?option value ...? Defines fontname with the specified parameters.
font delete fontname ?name2 ...? Removes the definition for the named fonts.
font families ?-displayof win? Returns the list of font families supported on the
display of win.
font measure font ?-displayof win? text Returns the width of text displayed in win with
font.
font metrics font ?-displayof win? ?
option?
The option can be -ascent, -descent, -
linespace, or -fixed.
font names
Returns the names of defined fonts.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter39. Fonts and Text Attributes
Text Attributes
Layout
Table 39-4 summarizes two simple text layout attributes: justify and wrapLength. The text widget
has several more layout-related attributes, and Chapter 33 describes those in detail. The two attributes
described in this section apply to the various button widgets, the label, entry, and message widgets.
Those widgets are described in Chapters 27, 29, and 31. The justify attribute causes text to be
centered, left-justified, or right-justified. The default justification is center for all the widgets in the
table, except for the entry widget, which is left-justified by default.
The wrapLength attribute specifies how long a line of text is before it is wrapped onto another line. It
is used to create multiline buttons and labels. This attribute is specified in screen units, however, not
string length. It is probably easier to achieve the desired line breaks by inserting newlines into the text
for the button or label and specifying a wrapLength of 0, which is the default.
Table 39-4. Layout attribute resource names
justify
Text line justification. Values: left, center, or right. Widgets: button,
checkbutton, entry, label, menubutton, message, and radiobutton.
wrapLength
Maximum line length for text, in screen units. Widgets: button, checkbutton,
label, menubutton, and radiobutton.
Selection Attributes
Table 39-5 lists the selection-related attributes. The exportSelection attribute controls if the
selection is exported for cut and paste to other widgets. The colors for selected text are set with
selectForeground and selectBackground. The selection is drawn in a raised relief, and the
selectBorderWidth attribute affects the 3D appearance. Choose a border width of zero to get a flat
relief.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter39. Fonts and Text Attributes
A Font Selection Application
This chapter concludes with an example that lets you select fonts. It is written as a dialog that you can
add to your application. The menus are tied to elements of the font array that are used in font
configure commands. The actual settings of the font are shown above a sampler of what the font
looks like. When the user clicks the OK button, the font configuration is returned:
Example 39-4 Font selection dialog.
proc Font_Select {{top .fontsel}} {
global font
# Create File, Font, Size, and Format menus
toplevel $top -class Fontsel -bd 10
set menubar [menu $top.menubar]
$top config -menu $menubar
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter40. Send
The send Command
The send command invokes a Tcl command in another application. The general form of the command
is:
send options interp arg ?arg...?
The send command behaves like eval; if you give it extra arguments, it concatenates them to form a
single command. If your argument structure is important, use list to build the command. Table 40-1
lists the options to send:
Table 40-1. Options to the send command.
-async
Does not wait for the remote command to complete.
-displayof
window
Sends to the application on the same display as window.
--
Delimits options from the interp argument. Useful if the interp begins with a
dash.
The interp argument is the name of the other application. An application defines its own name when
it creates its main window. The wish shell uses as its name the last component of the file name of the
script. For example, when wish interprets /usr/local/bin/exmh, it sets its application name to exmh.
However, if another instance of the exmh application is already running, wish chooses the name exmh
#2, and so on. If wish is not executing from a file, its name is just wish. You may have noticed wish
#2 or wish #3 in your window title bars, and this reflects the fact that multiple wish applications are
running on your display. If you use Tk 3.6 or earlier and your application crashes, it can forget to
unregister its name. The tkinspect program has a facility to clean up these old registrations.
A script can find out its own name, so you can pass names around or put them into files in order to set
up communications. The tk appname command queries or changes the application name:
}
if {$argc > 3} {
set uiCmd [lindex $argv 3]
}
# Read input and send it to the logger
while {[gets stdin input] >= 0} {
# Ignore errors with the logger
catch {send $app [concat $cmd [list $input\n]]}
}
# Notify the controller, if any
if [info exists ui] {
if [catch {send $ui $uiCmd}msg] {
puts stderr "send.tcl could not notify $ui\n$msg"
}
}
# This is necessary to force wish to exit.
exit
The sender application supports communication with two processes. It sends all its input to a primary
"logging" application. When the input finishes, it can send a notification message to another
"controller" application. The logger and the controller could be the same application.
Use list to quote arguments to send.
Consider the send command used in the example:
send $app [concat $cmd [list $input\n]]
The combination of concat and list is tricky. The list command quotes the value of the input line.
This quoted value is then appended to the command, so it appears as a single extra argument. Without
the quoting by list, the value of the input line will affect the way the remote interpreter parses the
command. Consider these alternatives:
send $app [list $cmd $input]
This form is safe, except that it limits $cmd to a single word. If cmd contains a value like the ones given
below, the remote interpreter will not parse it correctly. It will treat the whole multiword value as the
name of a command:
.log insert end
.log see end ; .log insert end
This is the most common wrong answer:
send $app $cmd $input
The send command concatenates $cmd and $input together, and the result will be parsed again by the
remote interpreter. The success or failure of the remote command depends on the value of the input
data. If the input included Tcl syntax like $ or [ ], errors or other unexpected behavior would result.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter40. Send
Communicating Processes
Chapter 22 presented two examples: a browser for the examples in this book, and a simple shell in
which to try out Tcl commands. In that chapter they are put into the same application. The two
examples shown below hook these two applications together using the send command. Example 40-2
changes the Run and Reset procedures to send Tcl commands the shell defined in Example 22-4 on
page 329. The StartEvalServer procedure starts up the shell, if necessary.
Example 40-2 Hooking the browser to an eval server.
# Replace the Run and Reset procedures from
# Example 22? on page 324 with these procedures
# Start up the evalsrv.tcl script.
proc StartEvalServer {} {
global browse
# Start the shell and pass it our name.
exec evalsrv.tcl [tk appname] &
# Wait for evalsrv.tcl to send us its name
tkwait variable browse(evalInterp)
}
proc Run {} {
global browse
set apps [winfo interps]
set ix [lsearch -glob $apps evalsrv.tcl*]
if {$ix < 0} {
# No evalsrv.tcl application running
StartEvalServer
}
if {![info exists browse(evalInterp)]} {
# Hook up to already running eval server
set browse(evalInterp) [lindex $apps $ix]
}
if [catch {send $browse(evalInterp) {info vars}}err] {
# It probably died - restart it.
StartEvalServer
}
# Send the command asynchronously. The two
# list commands foil the concat done by send and
# the uplevel in EvalServe
send -async $browse(evalInterp) \
[list EvalEcho [list source $browse(current)]]
}
# Reset the shell interpreter in the eval server
proc Reset {} {
global browse
send $browse(evalInterp) {EvalEcho reset}
}
The number of lists created before the send command may seem excessive, but they are all necessary.
The send command concatenates its arguments, so instead of letting it do that, we pass it a single list.
Similarly, EvalServe expects a single argument that is a valid command, so list is used to construct
that.
The shell in Example 22-4 on page 329 has an EvalEcho procedure that we can use as the target of
send. The only thing it needs is to complete the rendez-vous with the browser. When the tcl shell starts
up, it sends the browser its application name. The browser passes its own name on the command line
that starts the shell, so the shell knows how to talk to the browser.
Example 40-3 Making the shell into an eval server.
# Add this to Example 22? on page 329
if {$argc > 0} {
# Check in with the browser
send [lindex $argv 0] \
[list set browse(evalInterp) [tk appname]]
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter40. Send
Remote eval through Sockets
Network sockets provide another communication mechanism you can use to evaluate Tcl commands
in another application. The "name" of the application is just the host and port for the socket
connection. There are a variety of schemes you can use to manage names. A crude, but effective way
to manage host and ports for your servers is to record them in a file in your network file system. These
examples ignore this problem. The server chooses a port and the client is expected to know what it is.
Example 40-4 implements Eval_Server that lets other applications connect and evaluate Tcl
commands. The interp argument specifies the interpreter in which to evaluate the Tcl commands. If
the caller of Eval_Server specifies {} for the interpreter, then the commands are evaluated in the
current interpreter. The openCmd is called when the connection is made. It can do whatever setup or
authentication is required. If it doesn't like the connection, it can close the socket:
Example 40-4 Remote eval using sockets.
proc Eval_Server {port {interp {}} {openCmd EvalOpenProc}} {
socket -server [list EvalAccept $interp $openCmd] $port
}
proc EvalAccept {interp openCmd newsock addr port} {
global eval
set eval(cmdbuf,$newsock) {}
fileevent $newsock readable [list EvalRead $newsock $interp]
if [catch {
interp eval $interp $openCmd $newsock $addr $port
}] {
close $newsock
}
}
proc EvalOpenProc {sock addr port} {
# do authentication here
# close $sock to deny the connection
}
Example 40-5 shows EvalRead that reads commands and evaluates them in an interpreter. If the
interp is {}, it causes the commands to execute in the current interpreter. In this case an uplevel #0
is necessary to ensure the command is executed in the global scope. If you use interp eval to
execute something in yourself, it executes in the current scope:
Example 40-5 Reading commands from a socket.
proc EvalRead {sock interp} {
global eval errorInfo errorCode
if [eof $sock] {
close $sock
} else {
gets $sock line
append eval(cmdbuf,$sock) $line\n
if {[string length $eval(cmdbuf,$sock)] && \
[info complete $eval(cmdbuf,$sock)]} {
set code [catch {
if {[string length $interp] == 0} {
uplevel #0 $eval(cmdbuf,$sock)
} else {
interp eval $interp $eval(cmdbuf,$sock)
}
} result]
set reply [list $code $result $errorInfo \
$errorCode]\n
# Use regsub to count newlines
set lines [regsub -all \n $reply {} junk]
# The reply is a line count followed
# by a Tcl list that occupies that number of lines
puts $sock $lines
puts -nonewline $sock $reply
flush $sock
set eval(cmdbuf,$sock) {}
}
}
}
Example 40-6 presents Eval_Open and Eval_Remote that implement the client side of the eval
connection. Eval_Open connects to the server and returns a token, which is just the socket. The main
task of Eval_Remote is to preserve the information generated when the remote command raises an
error
The network protocol is line-oriented. The Eval_Remote command writes the command on the socket.
The EvalRead procedure uses info complete to detect the end of the command. The reply is more
arbitrary, so server sends a line count and that number of lines. The regsub command counts up all the
newlines because it returns the number of matches it finds. The reply is a list of error codes, results,
and trace information. These details of the return command are described on page 80.
Example 40-6 The client side of remote evaluation.
proc Eval_Open {server port} {
global eval
set sock [socket $server $port]
# Save this info for error reporting
set eval(server,$sock) $server:$port
return $sock
}
proc Eval_Remote {sock args} {
global eval
# Preserve the concat semantics of eval
if {[llength $args] > 1} {
set cmd [concat $args]
} else {
set cmd [lindex $args 0]
}
puts $sock $cmd
flush $sock
# Read return line count and the result.
gets $sock lines
set result {}
while {$lines > 0} {
gets $sock x
append result $x\n
incr lines -1
}
set code [lindex $result 0]
set x [lindex $result 1]
# Cleanup the end of the stack
regsub "\[^\n]+$" [lindex $result 2] \
"*Remote Server $eval(server,$sock)*" stack
set ec [lindex $result 3]
return -code $code -errorinfo $stack -errorcode $ec $x
}
proc Eval_Close {sock} {
close $sock
}
If an error occurs in the remote command, then a stack trace is returned. This includes the command
used inside EvalRead to invoke the command, which is either the uplevel or interp eval command.
This is the very last line in the stack that is returned, and regsub is used to replace this with an
indication of where control transferred to the remote server:
catch [Eval_Remote sock6 set xx]
=> 1
set errorInfo
=> can't read "xx": no such variable
while executing
"set xx
"
("uplevel" body line 1)
invoked from within
*Remote Server sage:4000*
invoked from within
"catch [Eval_Remote sock6 set xx]"
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartV. Tk Details
Chapter 41. Window Managers and Window
Information
The window manager controls the size and location of other applications' windows. The window
manager is built into Windows and Macintosh, while it is a separate application on UNIX. The wm
command provides an interface to the window manager. The winfo command returns information
about windows.
Management of top-level windows is done by the window manager. The Macintosh and Windows
platforms have the window manager built in to the operating system, but in UNIX the window
manager is just another application. The window manager controls the position of top-level windows,
provides a way to resize windows, open and close them, and implements a border and decorative title
for windows. The wm command interacts with the window manager so that the application can control
its size, position, and iconified state.
If you need to fine-tune your display, you may need some detailed information about widgets. The
winfo command returns all sorts of information about windows, including interior widgets, not just
top-level windows.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter41. Window Managers and Window Information
The wm Command
The wm command has about 20 operations that interact with the window manager. The general form of
the command is:
wm operation win ?args?
In all cases the win argument must be a toplevel. Otherwise, an error is raised. In many cases, the
operation either sets or queries a value. If a new value is not specified, then the current settings are
returned. For example, this command returns the current window geometry:
wm geometry .
=> 300x200+327+20
This command defines a new geometry:
wm geometry . 400x200+0+0
There are lots of wm operations, and this reflects the complex protocol with UNIX window managers.
The summary below lists the subset of operations that I find useful. The operations can be grouped
into four main categories:
Size, placement and decoration of windows. Use the geometry and title operations to position
windows and set the title bar.
Icons. Use the iconify, deiconify, and withdraw operations to open and close windows. On
UNIX, closed windows are represented by an icon.
Long-term session state. Use the protocol operation to get a callback when users destroy
windows.
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartV. Tk Details
Chapter 42. Managing User Preferences
This chapter describes a user preferences package. The resource database stores preference settings.
Applications specify Tcl variables that are initialized from the database entries. A user interface lets
the user browse and change their settings.
User customization is an important part of any complex application. There are always design decisions
that could go either way. A typical approach is to choose a reasonable default, but then let users
change the default setting through a preferences user interface. This chapter describes a preference
package that works by tying together a Tcl variable, which the application uses, and a resource
specification, which the user sets. In addition, a user interface is provided so that the user need not edit
the resource database directly.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter42. Managing User Preferences
App-Defaults Files
We will assume that it is sufficient to have two sources of application defaults: a per-application
database and a per-user database. In addition, we will allow for some resources to be specific to color
and monochrome displays. The following example initializes the preference package by reading in the
per-application and per-user resource specification files. There is also an initialization of the global
array pref that will be used to hold state information about the preferences package. The Pref_Init
procedure is called like this:
Pref_Init $library/foo-defaults ~/.foo-defaults
We assume $library is the directory holding support files for the foo application, and that per-user
defaults will be kept in ~/.foo-defaults. These are UNIX-oriented file names. When you write
cross-platform Tk applications, you will find that some file names are inherently platform-specific.
The platform-independent operations described in Chapter 9 are great, but they do not change the fact
that user preferences may be stored in c:/webtk/userpref.txt on Windows, Hard
Disk:System:Preferences:WebTk Prefs on Macintosh, and ~/.webtk on UNIX. I find it useful to
have a small amount of platform-specific startup code that defines these pathnames. The preference
package uses resource files that work on all platforms:
Example 42-1 Preferences initialization.
proc Pref_Init { userDefaults appDefaults } {
global pref
set pref(uid) 0 ;# for a unique identifier for widgets
set pref(userDefaults) $userDefaults
set pref(appDefaults) $appDefaults
PrefReadFile $appDefaults startup
if [file exists $userDefaults] {
PrefReadFile $userDefaults user
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter42. Managing User Preferences
Improving the Package
One small improvement can be made to Pref_Add. If a user specifies a boolean resource manually, he
or she might use "true" instead of one and "false" instead of zero. Pref_Add should check for those
cases and set the boolean variable to one or zero to avoid errors when the variables are used in
expressions.
The interface lets you dismiss it without saving your preference settings. This is either a feature that
lets users try out settings without committing to them, or it is a bug. Fixing this requires introducing a
parallel set of variables to shadow the real variables until the user hits Save, which is tedious to
implement. You can also use a grab as described in Chapter 36 to prevent the user from doing
anything but setting preferences.
This preference package is a slightly simplified version of one I developed for exmh, which has so
many preference items that a two-level scheme is necessary. The first level is a menu of preference
sections, and each section is created with a single call to Pref_Add. This requires additional arguments
to Pref_Add to provide a title for the section and some overall information about the preference
section. The display code changes a small amount. The code for the exmh is on the CD-ROM.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartV. Tk Details
Chapter 43. A User Interface to Bindings
This chapter presents a user interface to view and edit bindings.
A good way to learn about how a widget works is to examine the bindings that are defined for it. This
chapter presents a user interface that lets you browse and change bindings for a widget or a class of
widgets.
The interface uses a pair of listboxes to display the events and their associated commands. An entry
widget is used to enter the name of a widget or a class. There are a few command buttons that let the
user add a new binding, edit an existing binding, save the bindings to a file, and dismiss the dialog.
Here is what the display looks like:
Example 43-1 A user interface to widget bindings.
proc Bind_Interface { w } {
# Our state
global bind
set bind(class) $w
# Set a class used for resource specifications
set frame [toplevel .bindui -class Bindui]
# Default relief
option add *Bindui*Entry.relief sunken startup
}
$f.key.l config -text Event:
$f.cmd.l config -text Command:
}
pack $frame.edit -after $frame.top -fill x
}
proc Bind_Edit { frame } {
Bind_New $frame
set line [$frame.key curselection]
if {$line == {}} {
return
}
$frame.edit.key.e delete 0 end
$frame.edit.key.e insert 0 [$frame.key get $line]
$frame.edit.cmd.e delete 0 end
$frame.edit.cmd.e insert 0 [$frame.cmd get $line]
}
The -width 11 and -anchor e attributes for the label widgets are specified so that the Event: and
Command: labels will line up with the Bindings for label at the top.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter43. A User Interface to Bindings
Saving and Loading Bindings
All that remains is the actual change or definition of a binding and some way to remember the
bindings the next time the application is run. The BindDefine procedure attempts a bind command
that uses the contents of the entries. If it succeeds, then the edit window is removed by unpacking it.
The bindings are saved by Bind_Save as a series of Tcl commands that define the bindings. It is
crucial that the list command be used to construct the commands properly.
Bind_Read uses the source command to read the saved commands. The application must call
Bind_Read as part of its initialization to get the customized bindings for the widget or class. It also
must provide a way to invoke Bind_Interface, such as a button, menu entry, or key binding.
Example 43-7 Defining and saving bindings.
proc BindDefine { f } {
if [catch {
bind [$f.top.e get] [$f.edit.key.e get] \
[$f.edit.cmd.e get]
} err] {
Status $err
} else {
# Remove the edit window
pack forget $f.edit
}
}
proc Bind_Save { dotfile args } {
set out [open $dotfile.new w]
foreach w $args {
foreach seq [bind $w] {
# Output a Tcl command
puts $out [list bind $w $seq [bind $w $seq]]
}
}
close $out
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVI. C Programming
Chapter 44. C Programming and Tcl
This chapter explains how to extend a Tcl application with new built-in commands. Tcl 8.0 replaces
the original string-based command interface with a more efficient dual-ported object interface. This
chapter describes both interfaces.
Tcl is implemented in a C library that is easy to integrate into an existing application. By adding the
Tcl interpreter to your application, you can configure and control it with Tcl scripts, and with Tk you
can provide a nice graphical interface to it. This was the original model for Tcl. Applications would be
largely application-specific C code and include a small amount of Tcl for configuration and the
graphical interface. However, the basic Tcl shells proved so useful by themselves that relatively few
Tcl programmers need to worry about programming in C or C++.
Tcl is designed to be easily extensible by writing new command implementations in C. A command
implemented in C is more efficient than an equivalent Tcl procedure. A more pressing reason to write
C code is that it may not be possible to provide the same functionality purely in Tcl. Suppose you have
a new device, perhaps a color scanner or a unique input device. The programming interface to that
device is through a set of C procedures that initialize and manipulate the state of the device. Without
some work on your part, that interface is not accessible to your Tcl scripts. You are in the same
situation if you have a C or C++ library that implements some specialized function such as a database.
Fortunately, it is rather straightforward to provide a Tcl interface that corresponds to the C or C++
interface.
Note: Where this chapter says "C", you can always think "C or C++". There is also a package called
TclBlend that lets you extend Tcl by writing Java instead of C, and to evaluate Tcl scripts from Java.
Find out more about TclBlend at:
http://www.scriptics.com/java/
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter44. C Programming and Tcl
Basic Concepts
This chapter assumes that you know some C or C++. You do not have to be an expert programmer to
use the Tcl APIs. Indeed, one of Tcl's strengths is the ease with which you can extend it by writing C
code. This chapter provides a few working examples that explain how to initialize your application
and create Tcl commands. It describes how to organize your code into packages. It concludes with
notes about compiling Tcl under UNIX, Windows, and Macintosh.
Getting Started
There are two ways to get started writing C code for Tcl applications. The easiest way is to write an
extension that just adds some new commands to a standard Tcl shell like tclsh or wish. With this
approach the Tcl shell creates a basic framework for you, and your C code just extends this framework
with new commands. Tcl supports dynamic loading, so you can compile your extension as a shared
library (i.e., DLL) and load it into a running Tcl shell. This is the easiest approach because the Tcl
shell handles the details of startup and shutdown, and it provides an interactive console to enter Tcl
commands. In the case of wish, it also provides the framework for a graphical user interface. Finally, a
loadable extension can be shared easily with other Tcl users.
The second way to use the Tcl library is to add it to an existing application. If your application is very
simple, it may make sense to turn it into an extension for a standard Tcl shell, which brings you back
to the first, simpler approach. However, if your application already has a complex framework (e.g., it
is a long-running server process), then you can just add Tcl to it and export the functionality of your
application as one or more Tcl commands. Once you do this, you will find that you can extend your
application with all the features provided by Tcl.
C Command Procedures and Data Objects
The C or C++ code that implements a Tcl command is called a command procedure. The interface to a
command procedure is much like the interface to a main program. The inputs are an array of values
that correspond exactly to the arguments in the Tcl script command. The result of the command
procedure becomes the result of the Tcl command.
There are two kinds of command procedures: string-based and "object-based." I've quoted "object"
here because we are really talking about the data representation of the arguments and results. We are
not talking about methods and inheritance and other things associated with object oriented
programming. However, the Tcl C APIs use a structure called a Tcl_Obj, which is called a dual ported
object in the reference material. I prefer the term "Tcl_Obj value".
The string interface is quite simple. A command procedure gets an array of strings as arguments, and it
computes a string as the result. Tcl 8.0 generalized strings into the Tcl_Obj type, which can have two
representations: both a string and another native representation like an integer, floating point number,
list, or bytecodes. An object-based command takes an array of Tcl_Obj pointers as arguments, and it
computes a Tcl_Obj as its result. The goal of the Tcl_Obj type is to reduce the number of conversions
between strings and native representations. Object-based commands will be more efficient than the
equivalent string-based commands, but the APIs are a little more complex. For simple tasks, and for
learning, you can use just the simpler string-based command interface.
SWIG
David Beasley created a nice tool called SWIG (Simple Wrapper Interface Generator) that generates
the C code that implements command procedures that expose a C or C++ API as Tcl commands. This
can be a great time saver if you need to export many calls to Tcl. The only drawback is that a C
interface may not feel that comfortable to the script writer. Handcrafted Tcl interfaces can be much
nicer, but automatically-generated interfaces are just fine for rapid prototyping and for software testing
environments. You can learn more about SWIG at its web site:
http://www.swig.org/
Tcl Initialization
Before you can use your command procedures from Tcl scripts, you need to register them with Tcl. In
some cases, you may also need to create the Tcl interpreter, although this is done for you by the
standard Tcl shells.
If you are writing an extension, then you must provide an initialization procedure. The job of this
procedure is to register Tcl commands with Tcl_CreateCommand or Tcl_CreateObjCommand. This is
shown in Example 44-1 on page 608. The name of this procedure must end with _Init, as in
Expect_Init, Blt_Init, or Foo_Init, if you plan to create your extension as a shared library. This
procedure is called automatically when the Tcl script loads your library with the load command, which
is described on page 607.
If you need to create the Tcl interpreter yourself, then there are two levels of APIs. At the most basic
level there is Tcl_CreateInterp. This creates an interpreter that includes the standard commands
listed in Table 1-4 on page 22. You still have to initialize all your custom commands (e.g., by calling
Foo_Init) and arrange to run a script using Tcl_Eval or Tcl_EvalFile. However, there are a lot of
details to get right, and Tcl provides a higher level interface in Tcl_Main and Tcl_AppInit. Tcl_Main
creates the interpreter for you, processes command line arguments to get an initial script to run, and
even provides an interactive command loop. It calls out to Tcl_AppInit, which you provide, to
complete the initialization of the interpreter. The use of Tcl_Main is shown in Example 44-13 on page
629. There are even more details to get right with a Tk application because of the window system and
the event loop. These details are hidden behind Tk_Main, which makes a similar call out to
Tk_AppInit that you provide to complete initialization.
Calling Out to Tcl Scripts
An application can call out to the script layer at any point, even inside command procedures. Tcl_Eval
is the basic API for this, and there are several variations depending on how you pass arguments to the
script. When you look up Tcl_Eval in the reference material, you will get a description of the whole
family of Tcl_Eval procedures.
You can also set and query Tcl variables from C using the Tcl_SetVar and Tcl_GetVar procedures.
Again, there are several variations on these procedures that account for different types, like strings or
Tcl_Obj values, and scalar or array variables. The Tcl_LinkVar procedure causes a Tcl variable to
mirror a C variable. Modifications to the Tcl variable are reflected in the C variable, and reading the
Tcl variable always returns the C variable's value. Tcl_LinkVar is built on a more general variable
tracing facility, which is exposed to Tcl as the trace command, and available as the Tcl_TraceVar C
API.
I think a well-behaved extension should provide both a C and Tcl API, but most of the core Tcl and Tk
commands do not provide an exported C API. This forces you to eval Tcl scripts to get at their
functionality. Example 44-15 on page 635 shows the Tcl_Invoke procedure that can help you work
around this limitation. Tcl_Invoke is used to invoke a Tcl command without the parsing and
substitution overhead of Tcl_Eval.
Using the Tcl C Library
Over the years the Tcl C Library has grown from a simple language interpreter into a full featured
library. An important property of the Tcl API is that it is cross platform: its works equally well on
UNIX, Windows, and Macintosh. One can argue that it is easier to write cross-platform applications in
Tcl than in Java! Some of the useful features that you might not expect from a language interpreter
include:
A general hash table package that automatically adjusts itself as the hash table grows. It allows
various types of keys, including strings and integers.
A dynamic string (i.e., DString) package that provides an efficient way to construct strings.
An I/O channel package that replaces the old "standard I/O library" found on UNIX with
something that is cross-platform, does buffering, allows nonblocking I/O, and does character set
translations. You can create new I/O channel types.
Network sockets for TCP/IP communication.
Character set translations between Unicode, UTF-8, and other encodings.
An event loop manager that interfaces with network connections and window system events. You
can create new "event sources" that work with the event loop manager.
Multithreading support in the form of mutexes, condition variables, and thread-local storage.
A registration system for exit handlers that are called when Tcl is shutting down.
This Chapter focuses just on the Tcl C API related to the Tcl interpreter. Chapter 47 gives a high-level
overview of all the procedures in the Tcl and Tk C library, but this book does not provide a complete
reference. Refer to the on-line manual pages for the specific details about each procedure; they are an
excellent source of information. The manual pages should be part of every Tcl distribution. They are
on the book's CD, and they can be found web at:
http://www.scriptics.com/man/
The Tcl source code is worth reading.
Finally, it is worth emphasizing that the source code of the Tcl C library is a great source of
information. The code is well written and well commented. If you want to see how something really
works, reading the code is worthwhile.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter44. C Programming and Tcl
Creating a Loadable Package
You can organize your C code into a loadable package that can be dynamically linked into tclsh, wish,
or your own Tcl application. The details about compiling the code into the shared library that contains
the package are presented in Chapter 45. This section describes a package that implements the random
Tcl command that returns random numbers.
The load Command
The Tcl load command is used to dynamically link in a compiled package:
load library package ?interp?
The library is the file name of the shared library file (i.e., the DLL), and package is the name of the
package implemented by the library. This name corresponds to the package_Init procedure called to
initialize the package (e.g., Random_Init) The optional interp argument lets you load the library into
a slave interpreter. If the library is in /usr/local/lib/random.so, then a Tcl script can load the
package like this:
load /usr/local/lib/random.so Random
On most UNIX systems, you can set the LD_LIBRARY_PATH environment variable to a colon-separated
list of directories that contain shared libraries. If you do that, then you can use relative names for the
libraries:
load librandom.so Random
On Macintosh, the load command looks for libraries in the same folder as the Tcl/Tk application (i.e.,
Wish) and in the System:Extensions:Tool Command Language folder:
then Tcl passes it back to you when the command is invoked. This is especially useful with Tk
widgets, which are explained in more detail in Chapter 46. Our simple RandomCmd command
procedure does not use this feature, so it passes NULL into Tcl_CreateCommand. The DeleteProc is
called when the command is destroyed, which is typically when the whole Tcl interpreter is being
deleted. If your state needs to be cleaned up, you can do it then. RandomCmd does not use this feature,
either.
The arguments from the Tcl command are available as an array of strings defined by an argv
parameter and counted by an argc parameter. This is the same interface that a main program has to its
command line arguments. Example 44-2 shows the RandomCmd command procedure:
Example 44-2 The RandomCmd C command procedure.
/*
* RandomCmd --
* This implements the random Tcl command. With no arguments
* the command returns a random integer.
* With an integer valued argument "range",
* it returns a random integer between 0 and range.
*/
int
RandomCmd(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
int rand, error;
int range = 0;
if (argc > 2) {
interp->result = "Usage: random ?range?";
return TCL_ERROR;
}
if (argc == 2) {
if (Tcl_GetInt(interp, argv[1], &range) != TCL_OK) {
return TCL_ERROR;
}
}
rand = random();
if (range != 0) {
rand = rand % range;
}
sprintf(interp->result, "%d", rand);
return TCL_OK;
}
The return value of a Tcl command is really two things: a result string and a status code. The result is a
string that is either the return value of the command as seen by the Tcl script, or an error message that
is reported upon error. For example, if extra arguments are passed to the command procedure, it raises
a Tcl error by doing this:
interp->result = "Usage: random ?range?";
return TCL_ERROR;
The random implementation accepts an optional argument that is a range over which the random
numbers should be returned. The argc parameter is tested to see if this argument has been given in the
Tcl command. argc counts the command name as well as the arguments, so in our case argc == 2
indicates that the command has been invoked something like:
random 25
The procedure Tcl_GetInt converts the string-valued argument to an integer. It does error checking
and sets the interpreter's result field in the case of error, so we can just return if it fails to return
TCL_OK.
if (Tcl_GetInt(interp, argv[1], &range) != TCL_OK) {
return TCL_ERROR;
}
Finally, the real work of calling random is done, and the result is formatted directly into the result
buffer using sprintf. A normal return looks like this:
sprintf(interp->result, "%d", rand);
return TCL_OK;
Result Codes from Command Procedures
The command procedure returns a status code that is either TCL_OK or TCL_ERROR to indicate success
or failure. If the command procedure returns TCL_ERROR, then a Tcl error is raised, and the result value
is used as the error message. The procedure can also return TCL_BREAK, TCL_CONTINUE, TCL_RETURN,
which affects control structure commands like foreach and proc. You can even return an application-
specific code (e.g., 5 or higher), which might be useful if you are implementing new kinds of control
structures. The status code returned by the command procedure is the value returned by the Tcl catch
command, which is discussed in more detail on page 77.
Managing the String Result
There is a simple protocol that manages the storage for a command procedure's result string. It
involves interp->result, which holds the value, and interp->freeProc, which determines how the
storage is cleaned up. When a command is called, the interpreter initializes interp->result to a static
buffer of TCL_RESULT_SIZE, which is 200 bytes. The default cleanup action is to do nothing. These
defaults support two simple ways to define the result of a command. One way is to use sprintf to
format the result in place:
sprintf(interp->result, "%d", rand);
Using sprintf is suitable if you know your result string is short, which is often the case. The other
way is to set interp->result to the address of a constant string. In this case, the original result buffer
is not used, and there is no cleanup required because the string is compiled into the program:
interp->result = "Usage: random ?random?";
In more general cases, the following procedures should be used to manage the result and freeProc
fields. These procedures automatically manage storage for the result:
Tcl_SetResult(interp, string, freeProc)
Tcl_AppendResult(interp, str1, str2, str3, (char *)NULL)
Tcl_AppendElement(interp, string)
Tcl_SetResult sets the return value to be string. The freeProc argument describes how the result
should be disposed of: TCL_STATIC is used in the case where the result is a constant string allocated by
the compiler, TCL_DYNAMIC is used if the result is allocated with Tcl_Alloc, which is a platform- and
compiler-independent version of malloc, and TCL_VOLATILE is used if the result is in a stack variable.
In the TCL_VOLATILE case, the Tcl interpreter makes a copy of the result before calling any other
command procedures. Finally, if you have your own memory allocator, pass in the address of the
procedure that should free the result.
Tcl_AppendResult copies its arguments into the result buffer, reallocating the buffer if necessary. The
arguments are concatenated onto the end of the existing result, if any. Tcl_AppendResult can be
called several times to build a result. The result buffer is overallocated, so several appends are
efficient.
Tcl_AppendElement adds the string to the result as a proper Tcl list element. It might add braces or
backslashes to get the proper structure.
If you have built up a result and for some reason want to throw it away (e.g., an error occurs), then you
can use Tcl_ResetResult to restore the result to its initial state. Tcl_ResetResult is always called
before a command procedure is invoked.
The Tcl_Obj Command Interface
The Tcl_Obj command interface replaces strings with dual-ported values. The arguments to a
command are an array of pointers to Tcl_Obj structures, and the result of a command is also of type
Tcl_Obj. The replacement of strings by Tcl_Obj values extends throughout Tcl. The value of a Tcl
variable is kept in a Tcl_Obj, and Tcl scripts are stored in a Tcl_Obj, too. You can continue to use the
old string-based API, which converts strings to Tcl_Obj values, but this conversion adds overhead.
The Tcl_Obj structure stores both a string representation and a native representation. The native
representation depends on the type of the value. Tcl lists are stored as an array of pointers to strings.
Integers are stored as 32-bit integers. Floating point values are stored in double-precision. Tcl scripts
are stored as sequences of byte codes. Conversion between the native representation and a string are
done upon demand. There are APIs for accessing Tcl_Obj values, so you do not have to worry about
type conversions unless you implement a new type. Example 44-3 shows the random command
procedure using the Tcl_Obj interfaces:
Example 44-3 The RandomObjCmd C command procedure.
/*
* RandomObjCmd --
* This implements the random Tcl command from
* Example 44? using the object interface.
*/
int
RandomObjCmd(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
Tcl_Obj *resultPtr;
int rand, error;
int range = 0;
if (objc > 2) {
Tcl_WrongNumArgs(interp, 1, objv, "?range?");
return TCL_ERROR;
}
if (objc == 2) {
if (Tcl_GetIntFromObj(interp, objv[1], &range) !=
TCL_OK) {
return TCL_ERROR;
}
}
rand = random();
if (range != 0) {
rand = rand % range;
}
resultPtr = Tcl_GetObjResult(interp);
Tcl_SetIntObj(resultPtr, rand);
return TCL_OK;
}
Compare Example 44-2 with Example 44-3. You can see that the two versions of the C command
procedures are similar. The Tcl_GetInt call is replaced with Tcl_GetIntFromObj call. This receives
an integer value from the command argument. This call can avoid conversion from string to integer if
the Tcl_Obj value is already an integer.
The result is set by getting a handle on the result object and setting its value. This is done instead of
accessing the interp->result field directly:
resultPtr = Tcl_GetObjResult(interp);
Tcl_SetIntObj(resultPtr, rand);
The Tcl_WrongNumArgs procedure is a convenience procedure that formats an error message. You pass
in objv, the number of arguments to use from it, and additional string. The example creates this
message:
wrong # args: should be "random ?range?"
Example 44-3 does not do anything obvious about storage management. Tcl initializes the result object
before calling your command procedure and takes care of cleaning it up later. It is sufficient to set a
value and return TCL_OK or TCL_ERROR. In more complex cases, however, you have to worry about
reference counts to Tcl_Obj values. This is described in more detail later.
If your command procedure returns a string, then you will use Tcl_SetStringObj. This command
makes a copy of the string you pass it. The new Tcl interfaces that take strings also take length
arguments so you can pass binary data in strings. If the length is minus 1, then the string is terminated
by a NULL byte. A command that always returned "boring" would do this:
resultPtr = Tcl_GetObjResult(interp);
Tcl_SetStringObj(resultPtr, "boring", -1);
This is a bit too boring. In practice you may need to build up the result piecemeal. With the string-
based API, you use Tcl_AppendResult. With the Tcl_Obj API you get a pointer to the result and use
Tcl_AppendToObj or Tcl_AppendStringsToObj:
resultPtr = Tcl_GetObjResult(interp);
Tcl_AppendStringsToObj(resultPtr, "hello ", username, NULL);
Managing Tcl_Obj Reference Counts
The string-based interfaces copy strings when passing arguments and returning results, but the
Tcl_Obj interfaces manipulate reference counts to avoid these copy operations. References come from
Tcl variables, from the interpreter's result, and from sharing caused when a value is passed into a Tcl
procedure. Constants are also shared. When a C command procedure is called, Tcl does not
automatically increment the reference count on the arguments. However, each Tcl_Obj referenced by
objv will have at least one reference, and it is quite common to have two or more references.
The C type definition for Tcl_Obj is shown below. There are APIs to access all aspects of an object, so
you should refrain from manipulating a Tcl_Obj directly unless you are implementing a new type:
Example 44-4 The Tcl_Obj structure.
typedef struct Tcl_Obj {
int refCount; /* Counts number of shared references */
char *bytes; /* String representation */
int length; /* Number of bytes in the string */
Tcl_ObjType *typePtr;/* Type implementation */
union {
long longValue; /* Type data */
double doubleValue;
VOID *otherValuePtr;
struct {
VOID *ptr1;
VOID *ptr2;
} twoPtrValue;
} internalRep;
} Tcl_Obj;
Each type implementation provides a few procedures like this:
Tcl_GetTypeFromObj(interp, objPtr, valuePtr);
Tcl_SetTypeObj(resultPtr, value);
objPtr = Tcl_NewTypeObj(value);
The initial reference count is zero.
The Tcl_NewTypeObj allocates storage for a Tcl_Obj and sets its reference count to zero.
Tcl_IncrRefCount and Tcl_DecrRefCount increment and decrement the reference count on an object.
Tcl_DecrRefCount frees the storage for Tcl_Obj when it goes to zero. The initial reference count of
zero was chosen because functions like Tcl_SetObjResult automatically increment the reference
count on an object.
The Tcl_GetTypeFromObj and Tcl_SetTypeObj procedures just get and set the value; the reference
count does not change. Type conversions are automatic. You can set a Tcl_Obj value to an integer and
get back a string or double precision number later. The type implementations automatically take care
of the storage for the Tcl_Obj value as it changes. Of course, if a Tcl_Obj stays the same type, then no
string conversions are necessary and accesses are more efficient.
Modifying Tcl_Obj Values
It is not safe to modify a shared Tcl_Obj. The sharing is only for efficiency: Logically, each reference
is a copy, and you must honor this model when creating and modifying Tcl_Obj values.
Tcl_IsShared returns 1 if there is more than one reference to an object. If a command procedure
modifies a shared object, it must make a private copy with Tcl_DuplicateObj. The new copy starts
with a reference count of zero. You either pass this to Tcl_SetResultObj, which adds a reference, or
you have to explicitly add a reference to the copy with Tcl_IncrRefCount.
Example 44-5 implements a plus1 command that adds one to its argument. If the argument is not
shared, then plus1 can be implemented efficiently by modifying the native representation of the
integer. Otherwise, it has to make a copy of the object before modifying it:
Example 44-5 The Plus1ObjCmd procedure.
/*
* Plus1ObjCmd --
* This adds one to its input argument.
*/
int
Plus1ObjCmd(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
Tcl_Obj *objPtr;
int i;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "value");
return TCL_ERROR;
}
objPtr = objv[1];
if (Tcl_GetIntFromObj(interp, objPtr, &i) != TCL_OK) {
return TCL_ERROR;
}
if (Tcl_IsShared(objPtr)) {
objPtr = Tcl_DuplicateObj(objPtr); /* refCount 0 */
Tcl_IncrRefCount(objPtr); /* refCount 1*/
}
/*
* Assert objPtr has a refCount of one here.
* OK to set the unshared value to something new.
* Tcl_SetIntObj overwrites the old value.
*/
Tcl_SetIntObj(objPtr, i+1);
/*
* Setting the result object adds a new reference,
* so we decrement because we no longer care about
* the integer object we modified.
*/
Tcl_SetObjResult(interp, objPtr); /* refCount 2*/
Tcl_DecrRefCount(objPtr); /* refCount 1*/
/*
* Now only the interpreter result has a reference to objPtr.
*/
return TCL_OK;
}
Pitfalls of Shared Tcl_Obj Values
You have to be careful when using the values from a Tcl_Obj structure. The Tcl C library provides
many procedures like Tcl_GetStringFromObj, Tcl_GetIntFromObj, Tcl_GetListFromObj, and so
on. These all operate efficiently by returning a pointer to the native representation of the object. They
will convert the object to the requested type, if necessary. The problem is that shared values can
undergo type conversions that may invalidate your reference to a particular type of the value.
Value references are only safe until the next Tcl_Get*FromObj call.
Consider a command procedure that takes two arguments, an integer and a list. The command
procedure has a sequence of code like this:
Tcl_GetListFromObj(interp, objv[1], &listPtr);
/* Manipulate listPtr */
Tcl_GetIntFromObj(interp, objv[2], &int);
/* listPtr may be invalid here */
If, by chance, both arguments have the same value, (e.g., 1 and 1), which is possible for a Tcl list and
an integer, then Tcl will automatically arrange to share these values between both arguments. The
pointers in objv[1] and objv[2] will be the same, and the reference count on the Tcl_Obj they
reference will be at least 2. The first Tcl_GetListFromObj call ensures the value is of type list, and it
returns a direct pointer to the native list representation. However, Tcl_GetInFromObj then helpfully
converts the Tcl_Obj value to an integer. This deallocates the memory for the list representation, and
now listPtr is a dang-ling pointer! This particular example can be made safe by reversing the calls
because Tcl_GetIntFromObj copies the integer value:
Tcl_GetIntFromObj(interp, objv[2], &int);
Tcl_GetListFromObj(interp, objv[1], &listPtr);
/* int is still a good copy of the value */
By the way, you should always test your Tcl_Get* calls in case the format of the value is incompatible
with the requested type. If the object is not a valid list, the following command returns an error:
if (Tcl_GetListFromObj(interp, obj[1], &listPtr) != TCL_OK) {
return TCL_ERROR;
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter44. C Programming and Tcl
The blob Command Example
This section illustrates some standard coding practices with a bigger example. The example is still
artificial in that it doesn't actually do very much. However, it illustrates a few more common idioms
you should know about when creating Tcl commands.
The blob command creates and manipulates blobs. Each blob has a name and some associated
properties. The blob command uses a hash table to keep track of blobs by their name. The hash table
is an example of state associated with a command that needs to be cleaned up when the Tcl interpreter
is destroyed. The Tcl hash table implementation is nice and general, too, so you may find it helpful in
a variety of situations.
You can associate a Tcl script with a blob. When you poke the blob, it invokes the script. This shows
how easy it is to associate behaviors with your C extensions. Example 44-6 shows the data structures
used to implement blobs.
Example 44-6 The Blob and BlobState data structures.
/*
* The Blob structure is created for each blob.
*/
typedef struct Blob {
int N; /* Integer-valued property */
Tcl_Obj *objPtr; /* General property */
Tcl_Obj *cmdPtr; /* Callback script */
} Blob;
/*
* The BlobState structure is created once for each interp.
*/
typedef struct BlobState {
Tcl_HashTable hash; /* List blobs by name */
int uid; /* Used to generate names */
} BlobState;
You can get a direct pointer to the string you have created with Tcl_DStringValue:
name = Tcl_DStringValue(&ds);
There are a handful of additional procedures in the DString API that you can read about in the
reference material. There are some that create lists, but this is better done with the Tcl_Obj interface
(e.g., Tcl_NewListObj and friends).
To some degree, a Tcl_Obj can replace the use of a Tcl_DString. For example, the
Tcl_NewStringObj and Tcl_AppendToObj allocate a Tcl_Obj and append strings to it. However, there
are a number of Tcl API procedures that take Tcl_DString types as arguments instead of the Tcl_Obj
type. Also, for small strings, the DString interface is still more efficient because it can do less
dynamic memory allocation.
Character Set Conversions
As described in Chapter 15, Tcl uses UTF-8 strings internally. UTF-8 is a representation of Unicode
that does not contain NULL bytes. It also represents 7-bit ASCII characters in one byte, so if you have
old C code that only manipulates ASCII strings, it can coexist with Tcl without modification.
However, in more general cases, you may need to convert between UTF-8 strings you get from
Tcl_Obj values to strings of a particular encoding. For example, when you pass strings to the
operating system, it expects them in its native encoding, which might be 16-bit Unicode, ISO-Latin-1
(i.e., iso-8859-1), or something else.
Tcl provides an encoding API that does translations for you. The simplest calls use a Tcl_DString to
store the results because it is not possible to predict the size of the result in advance. For example, to
convert from a UTF-8 string to a Tcl_DString in the system encoding, you use this call:
Tcl_UtfToExternalDString(NULL, string, -1, &ds);
You can then pass Tcl_DStringValue(&ds) to your system call that expects a native string.
Afterwards you need to call Tcl_DStringFree(&ds) to free up any memory allocated by
Tcl_UtfToExternalDString.
To translate strings the other way, use Tcl_ExternalToUtfDString:
Tcl_ExternalToUtfDString(NULL, string, -1, &ds);
The third argument to these procedures is the length of string in bytes (not characters), and minus
1 means that Tcl should calculate it by looking for a NULL byte. Tcl stores its UTF-8 strings with a
NULL byte at the end so it can do this.
The first argument to these procedures is the encoding to translate to or from. NULL means the
system encoding. If you have data in nonstandard encodings, or need to translate into something other
than the system encoding, you need to get a handle on the encoding with Tcl_GetEncoding, and free
that handle later with Tcl_FreeEncoding:
encoding = Tcl_GetEncoding(interp, name);
Tcl_FreeEncoding(encoding);
The names of the encodings are returned by the encoding names Tcl command, and you can query
them with a C API, too.
Windows has a quirky string data type called TCHAR, which is an 8-bit byte on Windows 95/98, and a
16-bit Unicode character on Windows NT. If you use a C API that takes an array of TCHAR, then you
have to know what kind of system you are running on to use it properly. Tcl provides two procedures
that deal with this automatically. Tcl_WinTCharToUf works like Tcl_ExternalToUtfDString, and
Tcl_WinUtfToTChar works like Tcl_UtfToExternalDString:
Tcl_WinUtfToTChar(string, -1, &ds);
Tcl_WinTCharToUtf(string, -1, &ds);
Finally, Tcl has several procedures to work with Unicode characters, which are type Tcl_UniChar, and
UTF-8 encoded characters. Examples include Tcl_UniCharToUtf, Tcl_NumUtfChars, and
Tcl_UtfToUniCharDString. Consult the reference materials for details about these procedures.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter44. C Programming and Tcl
Tcl_Main and Tcl_AppInit
This section describes how to make a custom main program that includes Tcl. However, the need for
custom main programs has been reduced by the use of loadable modules. If you create your commands
as a loadable package, you can just load them into tclsh or wish. Even if you do not need a custom
main, this section will explain how all the pieces fit together.
The Tcl library supports the basic application structure through the Tcl_Main procedure that is
designed to be called from your main program. Tcl_Main does three things:
It calls Tcl_CreateInterp to create an interpreter that includes all the standard Tcl commands
like set and proc. It also defines a few Tcl variables like argc and argv. These have the
command-line arguments that were passed to your application.
It calls Tcl_AppInit, which is not part of the Tcl library. Instead, your application provides this
procedure. In Tcl_AppInit you can register additional application-specific Tcl commands.
It reads a script or goes into an interactive loop.
You call Tcl_Main from your main program and provide an implementation of the Tcl_AppInit
procedure:
Example 44-13 A canonical Tcl main program and Tcl_AppInit.
/* main.c */
#include <tcl.h>
int Tcl_AppInit(Tcl_Interp *interp);
/*
* Declarations for application-specific command procedures
*/
int Plus1ObjCmd(ClientData clientData,
Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
main(int argc, char *argv[]) {
/*
* Initialize your application here.
*
* Then initialize and run Tcl.
*/
Tcl_Main(argc, argv, Tcl_AppInit);
exit(0);
}
/*
* Tcl_AppInit is called from Tcl_Main
* after the Tcl interpreter has been created,
* and before the script file
* or interactive command loop is entered.
*/
int
Tcl_AppInit(Tcl_Interp *interp) {
/*
* Tcl_Init reads init.tcl from the Tcl script library.
*/
if (Tcl_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
/*
* Register application-specific commands.
*/
Tcl_CreateObjCommand(interp, "plus1", Plus1ObjCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
Random_Init(interp);
Blob_Init(interp);
/*
* Define the start-up filename. This file is read in
* case the program is run interactively.
*/
Tcl_SetVar(interp, "tcl_rcFileName", "~/.mytcl",
TCL_GLOBAL_ONLY);
/*
* Test of Tcl_Invoke, which is defined on page 635.
*/
Tcl_Invoke(interp, "set", "foo", "$xyz [foo] {", NULL);
return TCL_OK;
}
The main program calls Tcl_Main with the argc and argv parameters passed into the program. These
are the strings passed to the program on the command line, and Tcl_Main will store these values into
Tcl variables by the same name. Tcl_Main is also given the address of the initialization procedure,
which is Tcl_AppInit in our example. Tcl_AppInit is called by Tcl_Main with one argument, a
handle on a newly created interpreter. There are three parts to the Tcl_AppInit procedure:
The first part initializes the various packages the application uses. The example calls Tcl_Init
to set up the script library facility described in Chapter 12. The core Tcl commands have already
been defined by Tcl_CreateInterp, which is called by Tcl_Main before the call to
Tcl_AppInit.
The second part of Tcl_AppInit does application-specific initialization. The example registers
the command procedures defined earlier in this Chapter.
The third part defines a Tcl variable, tcl_RcFileName, which names an application startup script
that executes if the program is used interactively.
You can use your custom program just like tclsh, except that it includes the additional commands you
define in your Tcl_AppInit procedure. The sample makefile on the CD creates a program named
mytcl. You can compile and run that program and test random and the other commands.
Tk_Main
The structure of Tk applications is similar. The Tk_Main procedure creates a Tcl interpreter and the
main Tk window. It calls out to a procedure you provide to complete initialization. After your
Tk_AppInit returns, Tk_Main goes into an event loop until all the windows in your application have
been destroyed.
Example 44-14 shows a Tk_AppInit used with Tk_Main. The main program processes its own
command-line arguments using Tk_ParseArgv, which requires a Tcl interpreter for error reporting.
The Tk_AppInit procedure initializes the clock widget example that is the topic of Chapter 46:
Example 44-14 A canonical Tk main program and Tk_AppInit.
/* main.c */
#include <tk.h>
int Tk_AppInit(Tcl_Interp *interp);
/*
* A table for command line arguments.
*/
char *myoption1 = NULL;
int myint2 = 0;
static Tk_ArgvInfo argTable[] = {
{"-myoption1", TK_ARGV_STRING, (char *) NULL,
(char *) &myoption1, "Explain myoption1"},
{"-myint2", TK_ARGV_CONSTANT, (char *) 1, (char *) &myint2,
"Explain myint2"},
{"", TK_ARGV_END, },
};
main(int argc, char *argv[]) {
Tcl_Interp *interp;
/*
* Create an interpreter for the error message from
* Tk_ParseArgv. Another one is created by Tk_Main.
* Parse our arguments and leave the rest to Tk_Main.
*/
interp = Tcl_CreateInterp();
if (Tk_ParseArgv(interp, (Tk_Window) NULL, &argc, argv,
argTable, 0) != TCL_OK) {
fprintf(stderr, "%s\n", interp->result);
exit(1);
}
Tcl_DeleteInterp(interp);
Tk_Main(argc, argv, Tk_AppInit);
exit(0);
}
int ClockCmd(ClientData clientData,
Tcl_Interp *interp,
int argc, char *argv[]);
int ClockObjCmd(ClientData clientData,
Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
void ClockObjDestroy(ClientData clientData);
int
Tk_AppInit(Tcl_Interp *interp) {
/*
* Initialize packages
*/
if (Tcl_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
if (Tk_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
/*
* Define application-specific commands here.
*/
Tcl_CreateCommand(interp, "wclock", ClockCmd,
(ClientData)Tk_MainWindow(interp),
(Tcl_CmdDeleteProc *)NULL);
Tcl_CreateObjCommand(interp, "oclock", ClockObjCmd,
(ClientData)NULL, ClockObjDestroy);
/*
* Define start-up filename. This file is read in
* case the program is run interactively.
*/
Tcl_SetVar(interp, "tcl_rcFileName", "~/.mytcl",
TCL_GLOBAL_ONLY);
return TCL_OK;
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter44. C Programming and Tcl
The Event Loop
An event loop is used to process window system events and other events like timers and network
sockets. The different event types are described later. All Tk applications must have an event loop so
that they function properly in the window system environment. Tk provides a standard event loop with
the Tk_MainLoop procedure, which is called at the end of Tk_Main. The wish shell provides an event
loop automatically. The tclsh shell does not, although you can add an event loop using pure Tcl as
shown in Example 16-2 on page 220.
Some applications already have their own event loop. You have two choices if you want to add Tk to
such an application. The first is to modify the existing event loop to call Tcl_DoOneEvent to process
any outstanding Tcl events. The unix directory of the source distribution has a file called XtTest.c
that adds Tcl to an Xt (i.e., Motif) application. The other way to customize the event loop is to make
your existing events look like Tcl event sources, and register them with the event loop. Then you can
just use Tk_Main. There are four event classes, and they are handled in the following order by
Tcl_DoOneEvent:
Window events. Use the Tk_CreateEventHandler procedure to register a handler for these
events. Use the TCL_WINDOW_EVENTS flag to process these in Tcl_DoOneEvent.
File events. Use these events to wait on slow devices and network connections. On UNIX you
can register a handler for all files, sockets, and devices with Tcl_CreateFileHandler. On
Windows and Macintosh, there are different APIs for registration because there are different
system handles for files, sockets, and devices. On all platforms you use the TCL_FILE_EVENTS
flag to process these handlers in Tcl_DoOneEvent.
Timer events. You can set up events to occur after a specified time period. Use the
Tcl_CreateTimerHandler procedure to register a handler for the event. Use the
TCL_TIMER_EVENTS flag to process these in Tcl_DoOneEvent.
Idle events. These events are processed when there is nothing else to do. Virtually all the Tk
widgets use idle events to display themselves. Use the Tcl_DoWhenIdle procedure to register a
procedure to call once at the next idle time. Use the TCL_IDLE_EVENTS flag to process these in
Tcl_DoOneEvent.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter44. C Programming and Tcl
Invoking Scripts from C
The main program is not the only place you can evaluate a Tcl script. You can use the Tcl_Eval
procedure essentially at any time to evaluate a Tcl command:
Tcl_Eval(Tcl_Interp *interp, char *command);
The command is evaluated in the current Tcl procedure scope, which may be the global scope. This
means that calls like Tcl_GetVar and Tcl_SetVar access variables in the current scope. If for some
reason you want a new procedure scope, the easiest thing to do is to call your C code from a Tcl
procedure used for this purpose. It is not easy to create a new procedure scope with the exported C
API.
Tcl_Eval modifies its argument.
You should be aware that Tcl_Eval may modify the string that is passed into it as a side effect of the
way substitutions are performed. If you pass a constant string to Tcl_Eval, make sure your compiler
has not put the string constant into read-only memory. If you use the gcc compiler, you may need to
use the -fwritable-strings option. Chapter 45 shows how to get the right compilation settings for
your system.
Variations on Tcl_Eval
There are several variations on Tcl_Eval. The possibilities include strings or Tcl_Obj values,
evaluation at the current or global scope, a single string (or Tcl_Obj value) or a variable number of
arguments, and optional byte-code compilation. The most general string-based eval is Tcl_EvalEx,
which takes a counted string and some flags:
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter45. Compiling Tcl and Extensions
Standard Directory Structure
The Source Distribution
Table 45-1 describes the directory structure of the Tcl source distribution. The Tk distribution is
similar, and you should model your own source distribution after this. It is also standard to place the
Tcl, Tk, and other source packages under a common source directory (e.g., /usr/local/src or
/home/welch/cvs). In fact, this may be necessary if the packages depend on each other.
Table 45-1. The Tcl source directory structure.
tcl8.2
The root of the Tcl sources. This contains a README and
license_terms file, and several subdirectories.
tcl8.2/compat
This contains .c files that implement procedures that are otherwise
broken in the standard C library on some platforms. They are only used
if necessary.
tcl8.2/doc
This contains the reference documentation. Currently this is in nroff
format suitable for use with the UNIX man program. The goal is to
convert this to XML.
tcl8.2/generic
This contains the generic .c and .h source files that are shared among
Unix, Windows, and Macintosh.
tcl8.2/mac
This contains the .c and .h source files that are specific to Macintosh.
It also contains Code Warrior project files.
tcl8.2/library
This contains init.tcl and other Tcl files in the standard Tcl script
library.
tcl8.2/library/encoding
This contains the Unicode conversion tables.
tcl8.2/library/package There are several subdirectories (e.g., http2.0) that contain Tcl script
packages.
tcl8.2/test
This contains the Tcl test suite. These are Tcl scripts that exercise the
Tcl implementation.
tcl8.2/tools
This is a collection of scripts used to help build the Tcl distribution.
tcl8.2/unix
This contains the .c and .h source files that are specific to UNIX. This
also contains the configure script and the Makefile.in template.
tcl8.2/unix/dltest
This contains test files for dynamic loading.
tcl8.2/unix/platform These can be used to build Tcl for several different platforms. You
create the package directories yourself.
tcl8.2/win
This contains the .c and .h source files that are specific to Windows.
This also contains the configure script and the Makefile.in template.
This may contain a makefile.vc that is compatible with nmake.
The Installation Directory Structure
When you install Tcl, the files end up in a different arrangement than the one in the source
distribution. The standard installation directory is organized so it can be shared by computers with
different machine types (e.g., Windows, Linux, and Solaris). The Tcl scripts, include files, and
documentation are all in shared directories. The applications and programming libraries (i.e., DLLs)
are in platform-specific directories. You can choose where these two groups of files are installed with
the --prefix and --exec-prefix options to configure, which is explained in detail in the next
section. Table 45-2 shows the standard installation directory structure:
Table 45-2. The installation directory structure.
arch/bin This contains platform-specific applications. On Windows, this also contains binary
libraries (i.e., DLLs). Typical arch names are solaris-sparc, linux-ix86, and win-
ix86.
arch/lib This contains platform-specific binary libraries on UNIX systems (e.g.,
libtcl8.2.so)
bin
This contains platform-independent applications (e.g., Tcl script applications).
doc
This contains documentation.
include
This contains public .h files
lib
This contains subdirectories for platform-independent script packages. Packages
stored here are found automatically by the Tcl auto loading mechanism described in
Chapter 12.
lib/tcl8.2
This contains the contents of the tcl8.2/library source directory, including
subdirectories.
lib/package This contains Tcl scripts for package. Example package directories include tk8.2
and itcl3.0.1.
man
This contains reference documentation in UNIX man format.
If you are an expert in configure, you may be aware of other options that give you even finer control
over where the different parts of the installation go. However, because of the way Tcl automatically
searches for scripts and binary libraries, you should avoid deviating from the recommended structure.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter45. Compiling Tcl and Extensions
Building Tcl from Source
Compiling Tcl from the source distribution is a two-step process: configuration, which uses a
configure script, then compiling, which is controlled by the make program. The configure script
examines the current system and makes various settings that are used during compilation. When you
run configure, you make some basic choices about how you will compile Tcl, such as whether you will
compile with debugging systems, or whether you will turn on threading support. You also define the
Tcl installation directory with configure. You use make to compile the source code, install the
compiled application, run the test suite, and clean up after yourself.
The make facility is familiar to any Unix programmer. By using the freely available Cygwin tools, you
can use configure and make on Windows, too. Tcl still uses the Microsoft VC++ compiler; it does not
use gcc on Windows.
Windows and Macintosh programmers may not have experience with make. The source distributions
may also include project files for the Microsoft Visual C++ compiler and the Macintosh Code
Warrior compiler, and it may be easier for you to use these project files, especially on the Macintosh.
Look in the win and mac subdirectories of the source distribution for these project files. However, the
focus of this chapter is on using configure and make to build your Tcl applications and extensions.
Configure and Autoconf
If you are the developer of a Tcl extension, there is a preliminary setup step that comes before
configuration. In this step you create templates for the configure script and the Makefile that drives
make. Then, you use the autoconf program to create the configure script. By using autoconf, a
developer on Windows or Linux can generate a configure script that is usable by other developers on
Solaris, HP-UX, FreeBSD, AIX, or any system that is vaguely UNIX-like.
The three steps: setup, configuration and make, are illustrated by the build process for Tcl and Tk:
The developer of a source code package creates a configure.in template that expresses the
system dependencies of the source code. They use the autoconf program to process this template
into a configure script. The developer also creates a Makefile.in template. Creating these
templates is described later. The Tcl and Tk source distributions already contain the configure
script, which can be found in the unix and win subdirectories. However, if you get the Tcl
sources from the network CVS repository, you must run autoconf yourself to generate the
configure script.
A user of a source code package runs configure on the computer system they will use to
compile the sources. This step converts Makefile.in to a Makefile suitable for the platform and
configuration settings. If you only have one platform, simply run configure in the unix (or win)
directory:
% cd /usr/local/src/tcl8.2/unix
% ./configure flags
The configure flags are described in Table 45-3. I use ./configure because I do not have . on
my PATH. Furthermore, I want to ensure I run the configure script from the current directory! If
you build for multiple platforms, create subdirectories of unix and run configure from there. For
example, here we use ../configure:
% cd /usr/local/src/tcl8.2/unix
% mkdir solaris
% cd solaris
% ../configure flags
The configure script uses the Makefile.in template to generate the Makefile. Once configure
is complete, you build your program with make:
% make
You can do other things with make. To run the test suite, do:
% make test
To install the compiled program or extension, do:
% make install
The Tcl Extension Architecture defines a standard set of actions, or make targets, for building Tcl
sources. Table 45-4 on page 652 shows the standard make targets.
Make sure you have a working compiler.
As the configure script executes, it prints out messages about the properties of the current platform.
You can tell if you are in trouble if the output contains either of these messages:
checking for cross compiler ... yes
or
checking if compiler works ... no
Either of these means configure has failed to find a working compiler. In the first case, it assumes you
are configuring on the target system but will cross-compile from a different system. Configure
proceeds bravely ahead, but the resulting Makefile is useless. While cross-compiling is common on
embedded processors, it is rarely necessary on UNIX and Windows. I only see this message when my
UNIX environment isn't set up right to find the compiler.
Many UNIX venders no longer bundle a working compiler. Fortunately, the freely available gcc
compiler has been ported to nearly every UNIX system. You should be able search the Internet and
find a ready to use gcc package for your platform.
On Windows there is a more explicit compiler check, and configure exits if it cannot find the
compiler. Tcl is built with the Microsoft Visual C++ compiler. It ships with a batch file,
vcvars32.bat, that sets up the environment so you can run the compiler from the command line. You
should read that file and configure your environment so you do not have to remember to run the batch
file all the time. If you use a different compiler on Windows, make sure you configure your
environment so it can be run from the DOS prompt.
Standard Configure Flags
Table 45-3 shows the standard options for Tcl configure scripts. These are implemented by a configure
library file (aclocal.m4) that you can use in your own configure scripts. The facilities provided by
aclocal.m4 are described in more detail later.
Table 45-3. Standard configure flags.
--prefix=dir This defines the root of the installation directory hierarchy. The default is
/usr/local.
--exec-
prefix=dir
This defines the root of the installation area for platform-specific files. This
defaults to the --prefix value. An example setting is /usr/local/solaris-
sparc.
--enable-gcc
Use the gcc compiler instead of the default system compiler.
--disable-shared
Disable generation of shared libraries and Tcl shells that dynamically link
against them. Statically linked shells and static archives are built instead.
--enable-symbols
Compile with debugging symbols.
--enable-threads
Compile with thread support turned on.
--with-tcl=dir This specifies the location of the build directory for Tcl.
--with-tk=dir This specifies the location of the build directory for Tk.
--with-
tclinclude=dir
This specifics the directory that contains tcl.h.
--with-tcllib=dir
This specifies the directory that contains the Tcl binary library (e.g.,
libtclstubs.a).
--with-
x11include=dir
This specifics the directory that contains X11.h.
--with-x11lib=dir
This specifies the directory that contains the X11 binary library (e.g.,
libX11.6.0.so).
Any flag with disable or enable in its name can be inverted. Table 45-3 lists the non-default setting,
however, so you can just leave the flag out to turn it off. For example, when building Tcl on Solaris
with the gcc compiler, shared libraries, debugging symbols, and threading support turned on, use this
command:
configure --prefix=/home/welch/install \
--exec-prefix=/home/welch/install/solaris \
--enable-gcc --enable-threads --enable-symbols
Keep all your sources next to the Tcl sources.
Your builds will go the most smoothly if you organize all your sources under a common directory. In
this case, you should be able to specify the same configure flags for Tcl and all the other extensions
you will compile. In particular, you must use the same --prefix and --exec-prefix so everything
gets installed together.
If your source tree is not adjacent to the Tcl source tree, then you must use --with-tclinclude or --
with-tcllib so the header files and runtime library can be found during compilation. Typically this
can happen if you build an extension under your home directory, but you are using a copy of Tcl that
has been installed by your system administrator. The --with-x11include and --with-x11lib flags
are similar options necessary when building Tk if your X11 installation is in a nonstandard location.
Installation
The --prefix flag specifies the main directory (e.g., /home/welch/install). The directories listed in
Table 45-2 are created under this directory. If you do not specify --exec-prefix, then the platform-
specific binary files are mixed into the main bin and lib directories. For example, the tclsh8.2
program and libtcl8.2.so shared library will be installed in:
/home/welch/install/bin/tclsh8.2
/home/welch/install/lib/libtclsh8.2.so
The script libraries and manual pages will be installed in:
/home/welch/install/lib/tcl8.2
/home/welch/install/man
If you want to have installations for several different platforms, then specify an --exec-prefix that is
different for each platform. For example, if you use --exec-prefix=/home/welch/install/solaris,
then the tclsh8.2 program and libtcl8.2.so shared library will be installed in:
/home/welch/install/solaris/bin/tclsh8.2
/home/welch/install/solaris/lib/libtclsh8.2.so
The script libraries and manual pages will remain where they are, so they are shared by all platforms.
Note that Windows has a slightly different installation location for binary libraries. They go into the
arch/bin directory along with the main executable programs.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter45. Compiling Tcl and Extensions
Using Stub Libraries
One problem with extensions is that they get compiled for a particular version of Tcl. As new Tcl
releases occur, you find yourself having to recompile extensions. This was necessary for two reasons.
First, the Tcl C library tended to changes its APIs from release to release. Changes in its symbol table
tie a compiled extension to a specific version of the Tcl library. Another problem occurred if you
compiled tclsh statically, and then tried to dynamically load a library. Some systems do not support
back linking in this situation, so tclsh would crash. Paul Duffin created a stub library mechanism for
Tcl that helps solve these problems.
The main idea is that Tcl creates two binary libraries: the main library (e.g., libtcl8.2.so) and a stub
library (e.g., libtclstub.a). All the code is in the main library. The stub library is just a big jump
table that contains addresses of functions in the main library. An extension calls Tcl through the jump
table. The level of indirection makes the extension immune to changes in the Tcl library. It also
handles the back linking problem. If this sounds expensive, it turns out to be equivalent to what the
operating system does when you use shared libraries (i.e., dynamic link libraries). Tcl has just
implemented dynamic linking in a portable, robust way.
To make your extension use stubs, you have to compile with the correct flags, and you have to add a
new call to your extensions Init procedure (e.g., Examplea_Init). The TCL_USE_STUBS compile-time
flag turns the Tcl C API calls into macros that use the stub table. The Tcl_InitStubs call ensures that
the jump table is initialized, so you must call Tcl_InitStubs as the very first thing in your Init
procedure. A typical call looks like this:
if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
return TCL_ERROR;
}
Tcl_InitStubs is similar in spirit to Tcl_PkgRequire in that you request a minimum Tcl version
number. Stubs have been supported since Tcl 8.1, and the API will evolve in a backward-compatible
way. Unless your extension uses new C APIs introduced in later versions, you should specify the
lowest version possible so that it is compatible with more Tcl applications.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter45. Compiling Tcl and Extensions
Using autoconf
Autoconf uses the m4 macro processor to translate the configure.in template into the configure
script. The configure script is run by /bin/sh (i.e., the Bourne Shell). Creating the configure.in
template is simplified by a standard m4 macro library that is distributed with autoconf. In addition, a
Tcl distribution contains a tcl.m4 file that has additional autoconf macros. Among other things, these
macros support the standard configure flags described in Table 45-3.
Creating configure templates can be complex and confusing. Mostly this is because a macro processor
is being used to create a shell script. The days I have spent trying to change the Tcl configuration files
really made me appreciate the simplicity of Tcl! Fortunately, there is now a standard set of Tcl-specific
autoconf macros and a sample Tcl extension that uses them. By editing the configure.in and
Makefile.in sample templates, you can ignore the details of what is happening under the covers.
The tcl.m4 File
The Tcl source distribution includes tcl.m4 and aclocal.m4 files. The autoconf program looks for the
aclocal.m4 file in the same directory as the configure.in template. In our case, the aclocal.m4 file
just includes the tcl.m4 file. Autoconf also has a standard library of m4 macros as part of its
distribution. To use tcl.m4 for your own extension, you have some options. The most common way is
to copy tcl.m4 into aclocal.m4 in your source directory. Or, you can copy both aclocal.m4 and
tcl.m4 and have aclocal.m4 include tcl.m4. If necessary, you can add more custom macros to this
aclocal.m4 file. Alternatively, you can copy the tcl.m4 file into the standard autoconf macro library.
The tcl.m4 file defines macros whose names begin with SC_ (for Scriptics). The standard autoconf
macro names begin with AC_. This book does not provide an exhaustive explanation of all these
autoconf macros. Instead, the important ones are explained in the context of the sample extension.
The tcl.m4 file replaces the tclConfig.sh found in previous versions of Tcl. (Actually,
tclConfig.sh is still produced by the Tcl 8.2 configure script, but its use is deprecated.) The idea of
tclConfig.sh was to capture some important results of Tcl's configure so they could be included in
the configure scripts used by an extension. However, it is better to recompute these settings when
configuring an extension because, for example, different compilers could be used to build Tcl and the
extension. So, instead of including tclConfig.sh into an extension's configure script, the extension's
configure.in should use the SC_ macros defined in the tcl.m4 file.
Makefile Templates
Autoconf implements yet another macro mechanism for the Makefile.in templates. The basic idea is
that the configure script sets shell variables as it learns things about your system. Finally, it substitutes
these variables into Makefile.in to create the working Makefile. The syntax for the substitutions in
Makefile.in is:
@configure_variable_name@
Often the make variable and the shell variable have the same name. For example, the following
statement in Makefile.in passes the TCL_LIBRARY value determined by configure through to the
Makefile:
TCL_LIBRARY = @TCL_LIBRARY@
The AC_SUBST macro specifies what shell variables should be substituted in the Makefile.in
template. For example:
AC_SUBST(TCL_LIBRARY)
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter45. Compiling Tcl and Extensions
The Sample Extension
This section describes the sample extension that is distributed as part of the Tcl Extension
Architecture (TEA) standard. The goal of TEA is to create a standard for Tcl extensions that makes it
easier to build, install, and share Tcl extensions. The sample Tcl extension is on the CD, and it can be
found on the Web at:
ftp://ftp.scriptics.com/pub/tcl/examples/tea/
There is also documentation on the Web at:
http://www.scriptics.com/products/tcltk/tea/
The extension described here is stored in the network CVS repository under the module name
samplextension. If you want direct access to the latest versions of Tcl source code, you can learn
about the CVS repository at this web page:
http://www.scriptics.com/products/tcltk/netcvs.html
The sample extension implements the Secure Hash Algorithm (SHA1). Steve Reid wrote the original
SHA1 C code, and Dave Dykstra wrote the original Tcl interface to it. Michael Thomas created the
standard configure and Makefile templates.
Instead of using the original name, sha1, the example uses a more generic name, exampleA, in its files,
libraries, and package names. When editing the sample templates for your own extension, you can
simply replace occurrences of "exampleA" with the appropriate name for your extension. The sample
files are well commented, so it is easy to see where you need to make the changes.
configure.in
The configure.in file is the template for the configure script. This file is very well commented. The
places you need to change are marked with __CHANGE__. The first macro to change is:
AC_INIT(exampleA.h)
The AC_INIT macro lists a file that is part of the distribution. The name is relative to the
configure.in file. Other possibilities include ../generic/tcl.h or src/mylib.h, depending on
where the configure.in file is relative to your sources. The AC_INIT macro necessary to support
building the package in different directories (e.g., either tcl8.2/unix or tcl8.2/unix/solaris). The
next thing in configure.in is a set of variable assignments that define the package's name and version
number:
PACKAGE = exampleA
MAJOR_VERSION = 0
MINOR_VERSION = 2
PATCH_LEVEL =
The package name determines the file names used for the directory and the binary library file created
by the Makefile. This name is also used in several configure and Makefile variables. You will need to
change all references to "exampleA" to match the name you choose for your package.
The version and patch level support a three-level scheme, but you can leave the patch level empty for
two-level versions like 0.2. If you do specify a patch-level, you need to include a leading "." or "p" in
it. These values are combined to create the version number like this:
VERSION = ${MAJOR_VERSION}.${MINOR_VERSION}${PATCH_LEVEL}
Windows compilers create a special case for shared libraries (i.e., DLLs). When you compile the
library itself, you need to declare its functions one way. When you compile code that uses the library,
you need to declare its functions another way. This complicates the exampleA.h header file. Happily,
the complexity is hidden inside some macros. In configure.in, you simply define a build_Package
variable. The sample defines:
AC_DEFINE(BUILD_exampleA)
This variable is only set when you are building the library itself, and it is only defined when compiling
on Windows. We will show later how this is used in exampleA.h to control the definition of the
Examplea_Init procedure.
The configure.in file has a bunch of magic to determine the name of the shared library file (e.g.,
packageA02.dll, packageA.0.2.so, packageA.0.2.shlib, etc.). All you need to do is change one
macro to match your package name.
AC_SUBST(exampleA_LIB_FILE)
These should be the only places you need to edit when adapting the sample configure.in to your
extension. It is worth noting that the last macro determines which templates are processed by the
configure script. The sample generates two files from templates, Makefile and mkIndex.tcl:
AC_OUTPUT([Makefile mkIndex.tcl])
The mkIndex.tcl script is a script that runs pkg_mkIndex to generate the pkgIndex.tcl file. The
pkg_mkIndex command is described in Chapter 12. The mkIndex.tcl script is more complex than you
might expect because UNIX systems and Windows have different default locations for binary libraries.
The goal is to create a pkgIndex.tcl script that gets installed into the arch/lib/package directory,
but can find either arch/bin/package.dll or arch/lib/libpackage.so, depending on the system.
You may need to edit the mkIndex.tcl.in template, especially if your extension is made of both Tcl
scripts and a binary library.
Makefile.in
The Makefile.in template is converted by the configure script into the Makefile. The sample
Makefile.in is well commented so that it is easy to see where to make changes. However, there is
some first class trickery done with the Makefile variables that is not worth explaining in detail. (Not in
a Tcl book, at least!) There are a few variables with exampleA in their name. In particular,
exampleA_LIB_FILE corresponds to a variable name in the configure script. You need to change both
files consistently. Some of the lines you need to change are shown below:
exampleA_LIB_FILE = @exampleA_LIB_FILE@
lib_BINARIES = $(exampleA_LIB_FILE)
$(exampleA_LIB_FILE)_OBJECTS = $(exampleA_OBJECTS)
The @varname@ syntax is used to substitute the configure variable with its platform-specific name
(e.g., libexamplea.dll or libexample.so). The lib_BINARIES variable names the set of libraries
built by the "make binaries" target. The _OBJECT variable is a clever trick to allow a generic library
make rule, which appears in the Makefile.in template as @MAKE_LIB@. Towards the end of
Makefile.in, there is a rule that uses these variables, and you must change uses of exampleA it to
match your package name:
$(exampleA_LIB_FILE) : $(exampleA_OBJECTS)
-rm -f $(exampleA_LIB_FILE)
@MAKE_LIB@
$(RANLIB) $(exampleA_LIB_FILE)
You must define the set of source files and the corresponding object files that are part of the library. In
the sample, exampleA.c implements the core of the Secure Hash Algorithm, and the tclexampleA.c
file implements the Tcl command interface:
exampleA_SOURCES = exampleA.c tclexampleA.c
SOURCES = $(exampleA_SOURCES)
The object file definitions use the OBJEXT variable that is .o for UNIX and .obj for Windows:
exampleA_OBJECTS = exampleA.${OBJEXT}tclexampleA.${OBJEXT}
OBJECTS = $(exampleA_OBJECTS)
The header files that you want to have installed are assigned to the GENERIC_HDRS variable. The
srcdir Make variable is defined during configure to be the name of the directory containing the file
named in the AC_INIT macro:
GENERIC_HDRS = $(srcdir)/exampleA.h
Unfortunately, you must specify explicit rules for each C source file. The VPATH mechanism is not
reliable enough to find the correct source files reliably. The configure script uses AC_INIT to locate
source files, and you create rules that use the resulting $(srcdir) value. The rules look like this:
exampleA.$(OBJEXT) : $(srcdir)/exampleA.c
$(COMPILE) -c '@CYGPATH@ $(srcdir)/exampleA.c' -o $@
The sample Makefile includes several standard targets. Even if you decide not to use the sample
Makefile.in template, you should still define the targets listed in Table 45-4 to ensure your extension
is TEA compliant. Plans for automatic build environments depend on every extension implementing
the standard make targets. The targets can be empty, but you should define them so that make will not
complain if they are used.
Table 45-4. TEA standard Makefile targets.
all
Makes these targets in order: binaries, libraries, doc.
binaries
Makes executable programs and binary libraries (e.g., DLLs).
libraries
Makes platform-independent libraries.
doc
Generates documentation files.
install
Makes these targets in order: install-binaries, install-libraries,
install-doc.
install-binaries
Installs programs and binary libraries.
install-
libraries
Installs script libraries.
install-doc
Installs documentation files.
test
Runs the test suite for the package.
depend
Generates makefile dependency rules.
clean
Removes files built during the make process.
distclean
Removes files built during the configure process.
Standard Header Files
This section explains a technique you should use to get symbols defined properly in your binary
library. The issue is raised by Windows compilers, which have a notion of explicitly importing and
exporting symbols. When you build a library you export symbols. When you link against a library, you
import symbols. The BUILD_exampleA variable is defined on Windows when you are building the
library. This variable should be undefined on UNIX, which does not have this issue. Your header file
uses this variable like this:
#ifdef BUILD_exampleA
#undef TCL_STORAGE_CLASS
#define TCL_STORAGE_CLASS DLLEXPORT
#endif
The TCL_STORAGE_CLASS variable is used in the definition of the EXTERN macro. You must use EXTERN
before the prototype for any function you want to export from your library:
EXTERN int Examplea_Init _ANSI_ARGS_((Tcl_Interp *Interp));
The _ANSI_ARGS_ macro is used to guard against old C compilers that do not tolerate function
prototypes.
Using the Sample Extension
You should be able to configure, compile and install the sample extension without modification. On
my Solaris machine it creates a binary library named exampleA0.2.so, while on my Windows NT
machine the library is named exampleA02.dll. The package name is Tclsha1, and it implements the
sha1 Tcl command. Ordinarily these names would be more consistent with the file names and package
names in the template files. However, the names in the sample are designed to be easy to edit in the
template. Assuming you use make install to copy the binary library into the standard location for your
site, you can use the package from Tcl like this:
package require Tclsha1
sha1 -string "some string"
The sha1 command returns a 128 bit encoded hash function of the input string. There are a number of
options to sha1 you can learn about by reading the source code.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVI. C Programming
Chapter 46. Writing a Tk Widget in C
This chapter describes the implementation of a simple clock widget. Two implementations are shown:
the original string-based command interface and the Tcl_Obj command interface.
A custom widget implemented in C has the advantage of being efficient and flexible. However, it
requires more work, too. This chapter illustrates the effort by explaining the implementation of a clock
widget. It is a digital clock that displays the current time according to a format string. This is
something you could implement in several lines of Tcl using a label widget, the clock command, and
after for periodic updates. However, the point of the example is to show the basic structure for a Tk
widget implemented in C, not how much easier Tcl programming is :-). The implementation of a
widget includes:
A data structure to describe one instance of the widget.
A class procedure to create a new instance of the widget.
An instance procedure to operate on an instance of the widget.
A set of configuration options for the widget.
A configuration procedure used when creating and reconfiguring the widget.
An event handling procedure.
A display procedure.
Other widget-specific procedures.
Two implementations are compared: string-based and Tcl_Obj based. The version that uses Tcl_Obj
values can interpret command line options more efficiently. A new option parsing package hides most
of the details. The string-based version of each procedure is shown first, and then the Tcl_Obj version
is shown for comparison. The display portion of the code is the same in the two versions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
Initializing the Extension
The widget is packaged as an extension that you can dynamically load into wish. Example 46-1 shows
the Clock_Init procedure. It registers two commands, clock and oclock, which use the string-based
and Tcl_Obj interfaces, respectively. It also initializes the stub table, which is described in Chapter 45,
and declares a package so that scripts can load the widget with package require.
Example 46-1 The Clock_Init procedure.
int ClockCmd(ClientData clientData,
Tcl_Interp *interp,
int argc, char *argv[]);
int ClockObjCmd(ClientData clientData,
Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]);
void ClockObjDelete(ClientData clientData);
/*
* Clock_Init is called when the package is loaded.
*/
int Clock_Init(Tcl_Interp *interp) {
if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
return TCL_ERROR;
}
Tcl_CreateCommand(interp, "wclock", ClockCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
Tcl_CreateObjCommand(interp, "oclock", ClockObjCmd,
(ClientData)NULL, ClockObjDelete);
Tcl_PkgProvide(interp, "Tkclock", "1.0");
return TCL_OK;
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
The Widget Data Structure
Each widget is associated with a data structure that describes it. Any widget structure will need a
pointer to the Tcl interpreter, the Tk window, and the display. The interpreter is used in most of the
Tcl and Tk library calls, and it provides a way to call out to the script or query and set Tcl variables.
The Tk window is needed for various Tk operations, and the display is used when doing low-level
graphic operations. The rest of the information in the data structure depends on the widget. The
different types will be explained as they are used in the rest of the code. The structure for the clock
widget follows:
Example 46-2 The Clock widget data structure.
#include "tk.h"
#include <sys/time.h>
typedef struct {
Tk_Window tkwin; /* The window for the widget */
Display *display; /* Tk's handle on the display */
Tcl_Interp *interp; /* Interpreter of the widget */
Tcl_Command widgetCmd; /* clock instance command. */
Tk_OptionTable optionTable; /* Used to parse options */
/*
* Clock-specific attributes.
*/
int borderWidth; /* Size of 3-D border */
Tcl_Obj *borderWidthPtr;/* Original string value */
int relief; /* Style of 3-D border */
Tk_3DBorder background; /* Color for border & background */
XColor *foreground; /* Color for the text */
XColor *highlight; /* Color for active highlight */
XColor *highlightBg; /* Color for neutral highlight */
int highlightWidth; /* Thickness of highlight rim */
Tcl_Obj *highlightWidthPtr; /* Original string value */
Tk_Font tkfont; /* Font info for the text */
char *format; /* Format for time string */
/*
* Graphic contexts and other support.
*/
GC textGC; /* Text graphics context */
Tk_TimerToken token; /* Periodic callback handle*/
char *clock; /* Pointer to the clock string */
int numChars; /* length of the text */
int textWidth; /* in pixels */
Tcl_Obj *widthPtr; /* The original width string value*/
int textHeight; /* in pixels */
Tcl_Obj *heightPtr; /* The original height string value*/
int padX; /* Horizontal padding */
Tcl_Obj *padXPtr; /* The original padX string value*/
int padY; /* Vertical padding */
Tcl_Obj *padYPtr; /* The original padY string value */
int flags; /* Flags defined below */
}Clock;
/*
* Flag bit definitions.
*/
#define REDRAW_PENDING 0x1
#define GOT_FOCUS 0x2
#define TICKING 0x4
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
The Widget Class Command
The Tcl command that creates an instance of a widget is known as the class command. In our example,
the clock command creates a clock widget. The command procedure for clock follows. The
procedure allocates the Clock data structure. It registers an event handler that gets called when the
widget is exposed, resized, or gets the focus. It creates a new Tcl command that operates on the
widget. Finally, it calls ClockConfigure to set up the widget according to the attributes specified on
the command line and the default configuration specifications.
Example 46-3 The ClockCmd command procedure.
int
ClockCmd(clientData, interp, argc, argv)
ClientData clientData; /* Main window of the app */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
Tk_Window main = (Tk_Window) clientData;
Clock *clockPtr;
Tk_Window tkwin;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " pathName ?options?\"", (char *) NULL);
return TCL_ERROR;
}
tkwin = Tk_CreateWindowFromPath(interp, main,
argv[1], (char *) NULL);
if (tkwin == NULL) {
return TCL_ERROR;
}
/*
* Set resource class.
*/
Tk_SetClass(tkwin, "Clock");
/*
* Allocate and initialize the widget record.
*/
clockPtr = (Clock *) Tcl_Alloc(sizeof(Clock));
clockPtr->tkwin = tkwin;
clockPtr->display = Tk_Display(tkwin);
clockPtr->interp = interp;
clockPtr->borderWidth = 0;
clockPtr->highlightWidth = 0;
clockPtr->relief = TK_RELIEF_FLAT;
clockPtr->background = NULL;
clockPtr->foreground = NULL;
clockPtr->highlight = NULL;
clockPtr->highlightBg = NULL;
clockPtr->tkfont = NULL;
clockPtr->textGC = None;
clockPtr->token = NULL;
clockPtr->clock = NULL;
clockPtr->format = NULL;
clockPtr->numChars = 0;
clockPtr->textWidth = 0;
clockPtr->textHeight = 0;
clockPtr->padX = 0;
clockPtr->padY = 0;
clockPtr->flags = 0;
/*
* Register a handler for when the window is
* exposed or resized.
*/
Tk_CreateEventHandler(clockPtr->tkwin,
ExposureMask|StructureNotifyMask|FocusChangeMask,
ClockEventProc, (ClientData) clockPtr);
/*
* Create a Tcl command that operates on the widget.
*/
clockPtr->widgetCmd = Tcl_CreateCommand(interp,
Tk_PathName(clockPtr->tkwin),
ClockInstanceCmd,
(ClientData) clockPtr, (void (*)()) NULL);
/*
* Parse the command line arguments.
*/
if (ClockConfigure(interp, clockPtr,
argc-2, argv+2, 0) != TCL_OK) {
Tk_DestroyWindow(clockPtr->tkwin);
return TCL_ERROR;
}
interp->result = Tk_PathName(clockPtr->tkwin);
return TCL_OK;
}
The Tcl_Obj version, ClockObjCmd, does some additional work to set up an option table that is used
to efficiently parse the command line options to the clock command. The option table is created the
first time the clock command is used. The clientData for ClockObjCmd is initially NULL; it is used to
store the option table once it is initialized. While ClockCmd uses the clientData to store a reference to
the main Tk window, ClockObjCmd uses the Tk_MainWindow procedure to get a reference to the main
Tk window.
Example 46-4 The ClockObjCmd command procedure.
int
ClockObjCmd(clientData, interp, objc, objv)
ClientData clientData;/* Main window of the app */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj **objv; /* Argument values. */
{
Tk_OptionTable optionTable;
Clock *clockPtr;
Tk_Window tkwin;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
return TCL_ERROR;
}
optionTable = (Tk_OptionTable) clientData;
if (optionTable == NULL) {
Tcl_CmdInfo info;
char *name;
/*
* Initialize the option table for this widget the
* first time a clock widget is created. The option
* table is saved as our client data.
*/
optionTable = Tk_CreateOptionTable(interp, optionSpecs);
name = Tcl_GetString(objv[0]);
Tcl_GetCommandInfo(interp, name, &info);
info.objClientData = (ClientData) optionTable;
Tcl_SetCommandInfo(interp, name, &info);
}
tkwin = Tk_CreateWindowFromPath(interp,
Tk_MainWindow(interp),
Tcl_GetString(objv[1]), (char *) NULL);
if (tkwin == NULL) {
return TCL_ERROR;
}
/*
* Set resource class.
*/
Tk_SetClass(tkwin, "Clock");
/*
* Allocate and initialize the widget record.
*/
clockPtr = (Clock *) Tcl_Alloc(sizeof(Clock));
clockPtr->tkwin = tkwin;
clockPtr->display = Tk_Display(tkwin);
clockPtr->interp = interp;
clockPtr->optionTable = optionTable;
clockPtr->borderWidth = 0;
clockPtr->borderWidthPtr = NULL;
clockPtr->highlightWidth = 0;
clockPtr->highlightWidthPtr = NULL;
clockPtr->relief = TK_RELIEF_FLAT;
clockPtr->background = NULL;
clockPtr->foreground = NULL;
clockPtr->highlight = NULL;
clockPtr->highlightBg = NULL;
clockPtr->tkfont = NULL;
clockPtr->textGC = None;
clockPtr->token = NULL;
clockPtr->clock = NULL;
clockPtr->format = NULL;
clockPtr->numChars = 0;
clockPtr->textWidth = 0;
clockPtr->widthPtr = NULL;
clockPtr->textHeight = 0;
clockPtr->heightPtr = NULL;
clockPtr->padX = 0;
clockPtr->padXPtr = NULL;
clockPtr->padY = 0;
clockPtr->padYPtr = NULL;
clockPtr->flags = 0;
/*
* Register a handler for when the window is
* exposed or resized.
*/
Tk_CreateEventHandler(clockPtr->tkwin,
ExposureMask|StructureNotifyMask|FocusChangeMask,
ClockEventProc, (ClientData) clockPtr);
/*
* Create a Tcl command that operates on the widget.
*/
clockPtr->widgetCmd = Tcl_CreateObjCommand(interp,
Tk_PathName(clockPtr->tkwin),
ClockInstanceObjCmd,
(ClientData) clockPtr, (void (*)()) NULL);
/*
* Parse the command line arguments.
*/
if ((Tk_InitOptions(interp, (char *)clockPtr,
optionTable, tkwin) != TCL_OK) ||
(ClockObjConfigure(interp, clockPtr,
objc-2, objv+2, 0) != TCL_OK)) {
Tk_DestroyWindow(clockPtr->tkwin);
return TCL_ERROR;
}
Tcl_SetStringObj(Tcl_GetObjResult(interp),
Tk_PathName(clockPtr->tkwin), -1);
return TCL_OK;
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
The Widget Instance Command
For each instance of a widget, a new command is created that operates on that widget. This is called
the widget instance command. Its name is the same as the Tk pathname of the widget. In the clock
example, all that is done on instances is to query and change their attributes. Most of the work is done
by Tk_ConfigureWidget and ClockConfigure, which are shown in the next section. The
ClockInstanceCmd command procedure is shown in the next example:
Example 46-5 The ClockInstanceCmd command procedure.
static int
ClockInstanceCmd(clientData, interp, argc, argv)
ClientData clientData;/* A pointer to a Clock struct */
Tcl_Interp *interp; /* The interpreter */
int argc; /* The number of arguments */
char *argv[]; /* The command line arguments */
{
Clock *clockPtr = (Clock *)clientData;
int result = TCL_OK;
char c;
int len;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " option ?arg arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
c = argv[1][0];
len = strlen(argv[1]);
if ((c == 'c') && (strncmp(argv[1], "cget", len) == 0)
&& (len >= 2)) {
if (argc != 3) {
Tcl_AppendResult(interp,
"wrong # args: should be \"",
argv[0], " cget option\"",
(char *) NULL);
return TCL_ERROR;
}
result = Tk_ConfigureValue(interp, clockPtr->tkwin,
configSpecs, (char *) clockPtr, argv[2], 0);
} else if ((c == 'c') && (strncmp(argv[1], "configure", len)
== 0) && (len >= 2)) {
if (argc == 2) {
/*
* Return all configuration information.
*/
result = Tk_ConfigureInfo(interp, clockPtr->tkwin,
configSpecs, (char *) clockPtr,
(char *) NULL,0);
} else if (argc == 3) {
/*
* Return info about one attribute, like cget.
*/
result = Tk_ConfigureInfo(interp, clockPtr->tkwin,
configSpecs, (char *) clockPtr, argv[2], 0);
} else {
/*
* Change one or more attributes.
*/
result = ClockConfigure(interp, clockPtr, argc-2,
argv+2,TK_CONFIG_ARGV_ONLY);
}
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be cget, configure, position, or size",
(char *) NULL);
return TCL_ERROR;
}
return result;
}
Example 46-6 shows the ClockInstanceObjCmd procedure. It uses the Tk_GetIndexFromObj routine
to map the first argument to an index, which is then used in a switch statement. It uses the
Tk_GetOptionValue and Tk_GetOptionInfo procedures to parse the widget configuration options.
Example 46-6 The ClockInstanceObjCmd command procedure.
static int
ClockInstanceObjCmd(clientData, interp, objc, objv)
ClientData clientData;/* A pointer to a Clock struct */
Tcl_Interp *interp; /* The interpreter */
int objc; /* The number of arguments */
Tcl_Obj *objv[]; /* The command line arguments */
{
Clock *clockPtr = (Clock *)clientData;
char *commands[] = {"cget", "configure", NULL};
enum command {CLOCK_CGET, CLOCK_CONFIGURE};
int result;
Tcl_Obj *objPtr;
int index;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv,
"option ?arg arg ...?");
return TCL_ERROR;
}
result = Tcl_GetIndexFromObj(interp, objv[1], commands,
"option", 0, &index);
if (result != TCL_OK) {
return result;
}
switch (index) {
case CLOCK_CGET: {
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv,
"cget option");
return TCL_ERROR;
}
objPtr = Tk_GetOptionValue(interp,
(char *)clockPtr,
clockPtr->optionTable,
(objc == 3) ? objv[2] : NULL,
clockPtr->tkwin);
if (objPtr == NULL) {
return TCL_ERROR;
} else {
Tcl_SetObjResult(interp, objPtr);
}
break;
}
case CLOCK_CONFIGURE: {
if (objc <= 3) {
/*
* Return one item if the option is given,
* or return all configuration information.
*/
objPtr = Tk_GetOptionInfo(interp,
(char *) clockPtr,
clockPtr->optionTable,
(objc == 3) ? objv[2] : NULL,
clockPtr->tkwin);
if (objPtr == NULL) {
return TCL_ERROR;
} else {
Tcl_SetObjResult(interp, objPtr);
}
} else {
/*
* Change one or more attributes.
*/
result = ClockObjConfigure(interp, clockPtr,
objc-2, objv+2);
}
}
}
return TCL_OK;
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
Configuring and Reconfiguring Attributes
When the widget is created or reconfigured, then the implementation needs to allocate the resources
implied by the attribute settings. Each clock widget uses some colors and a font. These are described
by graphics contexts that parameterize operations. Instead of specifying every possible attribute in
graphics calls, a graphics context is initialized with a subset of the parameters, and this is passed into
the graphic commands. The context can specify the foreground and background colors, clip masks,
line styles, and so on. The clock widget allocates a graphics context once and reuses it each time the
widget is displayed.
There are two kinds of color resources used by the widget. The focus highlight and the text foreground
are simple colors. The background is a Tk_3DBorder, which is a set of colors used to render 3D
borders. The background color is specified in the attribute, and the other colors are computed based on
that color. The code uses Tk_3DBorderColor to map back to the original color for use in the
background of the widget.
After the resources are set up, a call to redisplay the widget is scheduled for the next idle period. This
is a standard idiom for Tk widgets. It means that you can create and reconfigure a widget in the middle
of a script, and all the changes result in only one redisplay. The REDRAW_PENDING flag is used to ensure
that only one redisplay is queued up at any time. The ClockConfigure procedure is shown in the next
example:
Example 46-7 ClockConfigure allocates resources for the widget.
static int
ClockConfigure(interp, clockPtr, argc, argv, flags)
Tcl_Interp *interp;/* For return values and errors */
Clock *clockPtr; /* The per-instance data structure */
int argc; /* Number of valid entries in argv */
char *argv[]; /* The command line arguments */
int flags; /* Tk_ConfigureWidget flags */
{
XGCValues gcValues;
GC newGC;
/*
* Tk_ConfigureWidget parses the command line arguments
* and looks for defaults in the resource database.
*/
if (Tk_ConfigureWidget(interp, clockPtr->tkwin,
configSpecs, argc, argv, (char *) clockPtr, flags)
!= TCL_OK) {
return TCL_ERROR;
}
/*
* Give the widget a default background so it doesn't get
* a random background between the time it is initially
* displayed by the X server and we paint it
*/
Tk_SetWindowBackground(clockPtr->tkwin,
Tk_3DBorderColor(clockPtr->background)->pixel);
/*
* Set up the graphics contexts to display the widget.
* The context is used to draw off-screen pixmaps,
* so turn off exposure notifications.
*/
gcValues.background =
Tk_3DBorderColor(clockPtr->background)->pixel;
gcValues.foreground = clockPtr->foreground->pixel;
gcValues.font = Tk_FontId(clockPtr->tkfont);
gcValues.graphics_exposures = False;
newGC = Tk_GetGC(clockPtr->tkwin,
GCBackground|GCForeground|GCFont|GCGraphicsExposures,
&gcValues);
if (clockPtr->textGC != None) {
Tk_FreeGC(clockPtr->display, clockPtr->textGC);
}
clockPtr->textGC = newGC;
/*
* Determine how big the widget wants to be.
*/
ComputeGeometry(clockPtr);
/*
* Set up a call to display ourself.
*/
if ((clockPtr->tkwin != NULL) &&
Tk_IsMapped(clockPtr->tkwin)
&& !(clockPtr->flags & REDRAW_PENDING)) {
Tk_DoWhenIdle(ClockDisplay, (ClientData) clockPtr);
clockPtr->flags |= REDRAW_PENDING;
}
return TCL_OK;
}
Example 46-8 shows the ClockObjConfigure procedure. The Tk_SetOptions interface, which is used
to set fields in the Clock data structure, has one potential problem. It is possible that some
configuration options are correct, while others cause errors. In this case, ClockObjConfigure backs
out the changes, so the whole configuration has no effect. This requires a two-pass approach, with the
second pass used to restore the original values. Tk_SetOptions has a feature that lets you classify
changes to the widget. The GEOMETRY_MASK and GRAPHICS_MASK are bits defined by the clock widget to
divide its attributes into two classes. It changes its graphics context or recomputes its geometry only if
an attribute from the appropriate class is changed.
Example 46-8 ClockObjConfigure allocates resources for the widget.
static int
ClockObjConfigure(interp, clockPtr, objc, objv)
Tcl_Interp *interp;/* For return values and errors */
Clock *clockPtr; /* The per-instance data structure */
int objc; /* Number of valid entries in argv */
Tcl_Obj *objv[]; /* The command line arguments */
{
XGCValues gcValues;
GC newGC;
Tk_SavedOptions savedOptions;
int mask, error, new;
Tcl_Obj *errorResult;
/*
* The first time through this loop we set the
* configuration from the command line inputs. The second
* pass is used to restore the configuration in case of
* errors
*/
new = (clockPtr->clock == NULL);
for (error = 0 ; error <= 1 ; error++) {
if (!error) {
/*
* Tk_SetOptions parses the command arguments
* and looks for defaults in the resource
* database.
*/
if (Tk_SetOptions(interp, (char *) clockPtr,
clockPtr->optionTable, objc, objv,
clockPtr->tkwin, &savedOptions,
&mask) != TCL_OK) {
continue;
}
} else {
/*
* Restore options from saved values
*/
errorResult = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(errorResult);
Tk_RestoreSavedOptions(&savedOptions);
}
if (new || (mask & GRAPHICS_MASK)) {
/*
* Give the widget a default background so it doesn't
* get a random background between the time it is
* initially displayed by the system and we paint it
*/
Tk_SetBackgroundFromBorder(clockPtr->tkwin,
clockPtr->background);
/*
* Set up the graphics contexts to display the widget.
* The context is used to draw off-screen pixmaps,
* so turn off exposure notifications.
*/
gcValues.background =
Tk_3DBorderColor(clockPtr->background)->pixel;
gcValues.foreground = clockPtr->foreground->pixel;
gcValues.font = Tk_FontId(clockPtr->tkfont);
gcValues.graphics_exposures = False;
newGC = Tk_GetGC(clockPtr->tkwin,
GCBackground|GCForeground|GCFont|GCGraphicsExposures,
&gcValues);
if (clockPtr->textGC != None) {
Tk_FreeGC(clockPtr->display, clockPtr->textGC);
}
clockPtr->textGC = newGC;
}
/*
* Determine how big the widget wants to be.
*/
if (new || (mask & GEOMETRY_MASK)) {
ComputeGeometry(clockPtr);
}
/*
* Set up a call to display ourself.
*/
if ((clockPtr->tkwin != NULL) &&
Tk_IsMapped(clockPtr->tkwin)
&& !(clockPtr->flags & REDRAW_PENDING)) {
Tk_DoWhenIdle(ClockDisplay,
(ClientData) clockPtr);
clockPtr->flags |= REDRAW_PENDING;
}
}
if (!error) {
Tk_FreeSavedOptions(&savedOptions);
return TCL_OK;
} else {
Tcl_SetObjResult(interp, errorResult);
Tcl_DecrRefCount(errorResult);
return TCL_ERROR;
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
Specifying Widget Attributes
Several of the fields in the Clock structure are attributes that can be set when the widget is created or
reconfigured with the configure operation. The Tk_ConfigureWidget procedure is designed to help
you manage the default values, their resource names, and their class names. It works by associating a
widget option with an offset into the widget data structure. When you use a command line argument to
change an option, Tk_ConfigureWidget reaches into your widget structure and changes the value for
you. Several types are supported, such as colors and fonts, and Tk_ConfigureWidget handles all the
memory allocation used to store the values. Example 46-9 shows the Tk_ConfigSpec type used to
represent information about each attribute:
Example 46-9 The Tk_ConfigSpec typedef.
typedef struct Tk_ConfigSpec {
int type;
char *name;
char *dbName;
char *dbClass;
char *defValue;
int offset;
int specflags;
Tk_CustomOption *customPtr;
} Tk_ConfigSpec;
The initial field is a type, such as TK_CONFIG_BORDER. Colors and borders will be explained shortly.
The next field is the command-line flag for the attribute, (e.g., -background). Then comes the resource
name and the class name. The default value is next, (e.g., light blue). The offset of a structure
member is next, and the Tk_Offset macro is used to compute this offset. The specflags field is a
bitmask of flags. The two used in this example are TK_CONFIG_COLOR_ONLY and
TK_CONFIG_MONO_ONLY, which restrict the application of the configuration setting to color and
monochrome displays, respectively. You can define additional flags and pass them into
Tk_ConfigureWidget if you have a family of widgets that share most, but not all, of their attributes.
The tkButton.c file in the Tk sources has an example of this. The customPtr is used if you have a
TK_CONFIG_CUSTOM type, which is explained in detail in the manual page for Tk_ConfigureWidget.
Example 46-10 shows the Tk_ConfigSpec specification of widget attributes for the clock widget.
Example 46-10 Configuration specs for the clock widget.
static Tk_ConfigSpec configSpecs[] = {
{TK_CONFIG_BORDER, "-background", "background",
"Background", "light blue",
Tk_Offset(Clock, background), TK_CONFIG_COLOR_ONLY},
{TK_CONFIG_BORDER, "-background", "background",
"Background", "white", Tk_Offset(Clock, background),
TK_CONFIG_MONO_ONLY},
{TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
(char *) NULL, 0, 0},
{TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
(char *) NULL, 0, 0},
{TK_CONFIG_PIXELS, "-borderwidth", "borderWidth",
"BorderWidth","2", Tk_Offset(Clock, borderWidth), 0},
{TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
"ridge", Tk_Offset(Clock, relief), 0},
{TK_CONFIG_COLOR, "-foreground", "foreground",
"Foreground", "black", Tk_Offset(Clock, foreground),
0},
{TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
(char *) NULL, 0, 0},
{TK_CONFIG_COLOR, "-highlightcolor", "highlightColor",
"HighlightColor", "red", Tk_Offset(Clock, highlight),
TK_CONFIG_COLOR_ONLY},
{TK_CONFIG_COLOR, "-highlightcolor", "highlightColor",
"HighlightColor", "black",
Tk_Offset(Clock, highlight),TK_CONFIG_MONO_ONLY},
{TK_CONFIG_COLOR, "-highlightbackground",
"highlightBackground", "HighlightBackground",
"light blue", Tk_Offset(Clock, highlightBg),
TK_CONFIG_COLOR_ONLY},
{TK_CONFIG_COLOR, "-highlightbackground",
"highlightBackground", "HighlightBackground",
"black", Tk_Offset(Clock, highlightBg),
TK_CONFIG_MONO_ONLY},
{TK_CONFIG_PIXELS, "-highlightthickness",
"highlightThickness","HighlightThickness",
"2", Tk_Offset(Clock, highlightWidth), 0},
{TK_CONFIG_PIXELS, "-padx", "padX", "Pad",
"2", Tk_Offset(Clock, padX), 0},
{TK_CONFIG_PIXELS, "-pady", "padY", "Pad",
"2", Tk_Offset(Clock, padY), 0},
{TK_CONFIG_STRING, "-format", "format", "Format",
"%H:%M:%S", Tk_Offset(Clock, format), 0},
{TK_CONFIG_FONT, "-font", "font", "Font",
"Courier 18",
Tk_Offset(Clock, tkfont), 0},
{TK_CONFIG_END, (char *) NULL, (char *) NULL,
(char *) NULL, (char *) NULL, 0, 0}
};
There is an alternative to the Tk_ConfigureWidget interface that understands Tcl_Obj values in the
widget data structure. It uses a a similar type, Tk_OptionSpec, and Tk_ConfigureWidget is replaced
by the Tk_SetOptions, Tk_GetOptionValue, and Tk_GetOptionInfo procedures. Example 46-11
shows the Tk_OptionSpec type.
Example 46-11 The Tk_OptionSpec typedef.
typedef struct Tk_OptionSpec {
Tk_OptionType type;
char *optionName;
char *dbName;
char *dbClass;
char *defValue;
int objOffset;
int internalOffset;
int flags;
ClientData clientData;
int typeMask;
} Tk_OptionSpec;
The Tk_OptionSpec has two offsets, one for normal values and one for Tcl_Obj values. You can use
the second offset to set Tcl_Obj values directly from the command line configuration. The
TK_CONFIG_PIXELS type uses both offsets. The pixel value is stored in an integer, and a Tcl_Obj is
used to remember the exact string (e.g., 0.2cm) used to specify the screen distance. Most of the
functionality of the specflags field of Tk_ConfigSpec (e.g., TK_CONFIG_MONO_ONLY) has been
changed. The flags field accepts only TK_CONFIG_NULL_OK, and the rest of the features use the
clientData field instead. For example, the color types uses clientData for their default on
monochrome displays. The typeMask supports a general notion of grouping option values into sets.
For example, the clock widget marks attributes that affect geometry and color into different sets. This
lets the widget optimize its configuration procedure. Example 46-12 shows the Tk_OptionSpec
specification of the clock widget attributes.
Example 46-12 The Tk_OptionSpec structure for the clock widget.
#define GEOMETRY_MASK 0X1
#define GRAPHICS_MASK 0X2
static Tk_OptionSpec optionSpecs[] = {
{TK_OPTION_BORDER, "-background", "background",
"Background", "light blue", -1,
Tk_Offset(Clock, background), 0,
(ClientData) "white", GRAPHICS_MASK},
{TK_OPTION_SYNONYM, "-bg", "background", (char *) NULL,
(char *) NULL, -1, 0,0, (ClientData)"-background", 0},
{TK_OPTION_PIXELS, "-borderwidth", "borderWidth",
"BorderWidth", "2", Tk_Offset(Clock, borderWidthPtr),
Tk_Offset(Clock, borderWidth),
0, 0, GEOMETRY_MASK},
{TK_OPTION_SYNONYM, "-bd", "borderWidth", (char *) NULL,
(char *) NULL, -1,0,0,(ClientData)"-borderwidth", 0},
{TK_OPTION_RELIEF, "-relief", "relief", "Relief",
"ridge", -1, Tk_Offset(Clock, relief), 0, 0, 0},
{TK_OPTION_COLOR, "-foreground", "foreground",
"Foreground", "black",-1, Tk_Offset(Clock, foreground),
0, (ClientData) "black", GRAPHICS_MASK},
{TK_OPTION_SYNONYM, "-fg", "foreground", (char *) NULL,
(char *) NULL, -1, 0,0,(ClientData) "-foreground", 0},
{TK_OPTION_COLOR, "-highlightcolor", "highlightColor",
"HighlightColor", "red",-1, Tk_Offset(Clock, highlight),
0, (ClientData) "black", GRAPHICS_MASK},
{TK_OPTION_COLOR, "-highlightbackground",
"highlightBackground", "HighlightBackground",
"light blue",-1, Tk_Offset(Clock, highlightBg),
0, (ClientData) "white", GRAPHICS_MASK},
{TK_OPTION_PIXELS, "-highlightthickness",
"highlightThickness","HighlightThickness",
"2", Tk_Offset(Clock, highlightWidthPtr),
Tk_Offset(Clock, highlightWidth), 0, 0,
GEOMETRY_MASK},
{TK_OPTION_PIXELS, "-padx", "padX", "Pad",
"2", Tk_Offset(Clock, padXPtr),
Tk_Offset(Clock, padX), 0, 0, GEOMETRY_MASK},
{TK_OPTION_PIXELS, "-pady", "padY", "Pad",
"2", Tk_Offset(Clock, padYPtr),
Tk_Offset(Clock, padY), 0, 0, GEOMETRY_MASK},
{TK_OPTION_STRING, "-format", "format", "Format",
"%H:%M:%S",-1, Tk_Offset(Clock, format), 0, 0,
GEOMETRY_MASK},
{TK_OPTION_FONT, "-font", "font", "Font",
"Courier 18",
-1, Tk_Offset(Clock, tkfont), 0, 0,
(GRAPHICS_MASK|GEOMETRY_MASK)},
{TK_OPTION_END, (char *) NULL, (char *) NULL,
(char *) NULL, (char *) NULL, -1, 0, 0, 0, 0}
};
Table 46-1 lists the correspondence between the configuration type of the option and the type of the
associated field in the widget data structure. The same types are supported by the Tk_ConfigSpec and
Tk_OptionSpec types, with a few exceptions. The TK_CONFIG_ACTIVE_CURSOR configuration type
corresponds to the TK_OPTION_CURSOR; both of these set the widgets cursor. The TK_CONFIG_MM and
TK_CONFIG_CURSOR types are simply not supported by Tk_OptionSpec because they were not very
useful. The TK_OPTION_STRING_TABLE replaces TK_CONFIG_CAP_STYLE and TK_CONFIG_JOIN_STYLE
with a more general type that works with Tcl_GetIndexFromObj. In this case, the clientData is an
array of strings that are passed to Tcl_GetIndexFromObj. The index value corresponds to the integer
value returned from procedures like Tk_GetCapStyle.
Table 46-1. Configuration flags and corresponding C types.
TK_CONFIG_ACTIVE_CURSOR
TK_OPTION_CURSOR
Cursor
TK_CONFIG_ANCHOR
TK_OPTION_ANCHOR
Tk_Anchor
TK_CONFIG_BITMAP
TK_OPTION_BITMAP
Pixmap
TK_CONFIG_BOOLEAN
TK_OPTION_BOOLEAN
int (0 or 1)
TK_CONFIG_BORDER
TK_OPTION_BORDER
Tk_3DBorder *
TK_CONFIG_CAP_STYLE int (see Tk_GetCapStyle)
TK_CONFIG_COLOR
TK_OPTION_COLOR
XColor *
clientData is monochrome default.
TK_CONFIG_CURSOR Cursor
TK_CONFIG_CUSTOM
TK_CONFIG_DOUBLE
TK_OPTION_DOUBLE
double
TK_CONFIG_END
TK_OPTION_END
(signals end of options)
TK_CONFIG_FONT
TK_OPTION_FONT
Tk_Font
TK_CONFIG_INT
TK_OPTION_INT
int
TK_CONFIG_JOIN_STYLE int (see Tk_GetJoinStyle)
TK_CONFIG_JUSTIFY
TK_OPTION_JUSTIFY
Tk_Justify
TK_CONFIG_MM double
TK_CONFIG_PIXELS
TK_OPTION_PIXELS
int
objOffset used for original value.
TK_CONFIG_RELIEF
TK_OPTION_RELIEF
int (see Tk_GetRelief)
TK_CONFIG_STRING
TK_OPTION_STRING
char *
TK_OPTION_STRING_TABLE
The clientData is an array of strings used with
Tcl_GetIndexFromObj
TK_CONFIG_SYNONYM
TK_OPTION_SYNONYM
(alias for other option)
clientData is the name of another option.
TK_CONFIG_UID Tk_Uid
TK_CONFIG_WINDOW
TK_OPTION_WINDOW
Tk_Window
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
Displaying the Clock
There are two parts to a widget's display. First, the size must be determined. This is done at
configuration time, and then that space is requested from the geometry manager. When the widget is
later displayed, it should use the Tk_Width and Tk_Height calls to find out how much space was
actually allocated to it by the geometry manager. Example 46-13 shows ComputeGeometry. This
procedure is identical in both versions of the widget.
Example 46-13 ComputeGeometry computes the widget's size.
static void
ComputeGeometry(Clock *clockPtr)
{
int width, height;
Tk_FontMetrics fm; /* Font size information */
struct tm *tmPtr; /* Time info split into fields */
struct timeval tv; /* BSD-style time value */
int bd; /* Padding from borders */
char clock[1000]; /* Displayed time */
/*
* Get the time and format it to see how big it will be.
*/
gettimeofday(&tv, NULL);
tmPtr = localtime(&tv.tv_sec);
strftime(clock, 1000, clockPtr->format, tmPtr);
if (clockPtr->clock != NULL) {
Tcl_Free(clockPtr->clock);
}
clockPtr->clock = Tcl_Alloc(1+strlen(clock));
clockPtr->numChars = strlen(clock);
bd = clockPtr->highlightWidth + clockPtr->borderWidth;
Tk_GetFontMetrics(clockPtr->tkfont, &fm);
height = fm.linespace + 2*(bd + clockPtr->padY);
Tk_MeasureChars(clockPtr->tkfont, clock,
clockPtr->numChars, 0, 0, &clockPtr->textWidth);
width = clockPtr->textWidth + 2*(bd + clockPtr->padX);
Tk_GeometryRequest(clockPtr->tkwin, width, height);
Tk_SetInternalBorder(clockPtr->tkwin, bd);
}
Finally, we get to the actual display of the widget! The routine is careful to check that the widget still
exists and is mapped. This is important because the redisplay is scheduled asynchronously. The current
time is converted to a string. This uses the POSIX library procedures gettimeofday, localtime, and
strftime. There might be different routines on your system. The string is painted into a pixmap,
which is a drawable region of memory that is off-screen. After the whole display has been painted, the
pixmap is copied into on-screen memory to avoid flickering as the image is cleared and repainted. The
text is painted first, then the borders. This ensures that the borders overwrite the text if the widget has
not been allocated enough room by the geometry manager.
This example allocates and frees the off-screen pixmap for each redisplay. This is the standard idiom
for Tk widgets. They temporarily allocate the off-screen pixmap each time they redisplay. In the case
of a clock that updates every second, it might be reasonable to permanently allocate the pixmap and
store its pointer in the Clock data structure. Make sure to reallocate the pixmap if the size changes.
After the display is finished, another call to the display routine is scheduled to happen in one second. If
you were to embellish this widget, you might want to make the uptime period a parameter. The
TICKING flag is used to note that the timer callback is scheduled. It is checked when the widget is
destroyed so that the callback can be canceled. Example 46-14 shows ClockDisplay. This procedure
is identical in both versions of the widget.
Example 46-14 The ClockDisplay procedure.
static void
ClockDisplay(ClientData clientData)
{
Clock *clockPtr = (Clock *)clientData;
Tk_Window tkwin = clockPtr->tkwin;
GC gc; /* Graphics Context for highlight
*/
Tk_TextLayout layout; /* Text measurement state */
Pixmap pixmap; /* Temporary drawing area */
int offset, x, y; /* Coordinates */
int width, height; /* Size */
struct tm *tmPtr; /* Time info split into fields */
struct timeval tv; /* BSD-style time value */
/*
* Make sure the clock still exists
* and is mapped onto the display before painting.
*/
clockPtr->flags &= ~(REDRAW_PENDING|TICKING);
if ((clockPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
return;
}
/*
* Format the time into a string.
* localtime chops up the time into fields.
* strftime formats the fields into a string.
*/
gettimeofday(&tv, NULL);
tmPtr = localtime(&tv.tv_sec);
strftime(clockPtr->clock, clockPtr->numChars+1,
clockPtr->format, tmPtr);
/*
* To avoid flicker when the display is updated, the new
* image is painted in an offscreen pixmap and then
* copied onto the display in one operation. Allocate the
* pixmap and paint its background.
*/
pixmap = Tk_GetPixmap(clockPtr->display,
Tk_WindowId(tkwin), Tk_Width(tkwin),
Tk_Height(tkwin), Tk_Depth(tkwin));
Tk_Fill3DRectangle(tkwin, pixmap,
clockPtr->background, 0, 0, Tk_Width(tkwin),
Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
/*
* Paint the text first.
*/
layout = Tk_ComputeTextLayout(clockPtr->tkfont,
clockPtr->clock, clockPtr->numChars, 0,
TK_JUSTIFY_CENTER, 0, &width, &height);
x = (Tk_Width(tkwin) - width)/2;
y = (Tk_Height(tkwin) - height)/2;
Tk_DrawTextLayout(clockPtr->display, pixmap,
clockPtr->textGC, layout, x, y, 0, -1);
/*
* Display the borders, so they overwrite any of the
* text that extends to the edge of the display.
*/
if (clockPtr->relief != TK_RELIEF_FLAT) {
Tk_Draw3DRectangle(tkwin, pixmap,
clockPtr->background,
clockPtr->highlightWidth,
clockPtr->highlightWidth,
Tk_Width(tkwin) - 2*clockPtr->highlightWidth,
Tk_Height(tkwin) - 2*clockPtr->highlightWidth,
clockPtr->borderWidth, clockPtr->relief);
}
if (clockPtr->highlightWidth != 0) {
GC gc;
/*
* This GC is associated with the color, and Tk caches
* the GC until the color is freed. Hence no freeGC.
*/
if (clockPtr->flags & GOT_FOCUS) {
gc = Tk_GCForColor(clockPtr->highlight, pixmap);
} else {
gc = Tk_GCForColor(clockPtr->highlightBg, pixmap);
}
Tk_DrawFocusHighlight(tkwin, gc,
clockPtr->highlightWidth, pixmap);
}
/*
* Copy the information from the off-screen pixmap onto
* the screen, then delete the pixmap.
*/
XCopyArea(clockPtr->display, pixmap, Tk_WindowId(tkwin),
clockPtr->textGC, 0, 0, Tk_Width(tkwin),
Tk_Height(tkwin), 0, 0);
Tk_FreePixmap(clockPtr->display, pixmap);
/*
* Queue another call to ourselves. The rate at which
* this is done could be optimized.
*/
clockPtr->token = Tk_CreateTimerHandler(1000,
ClockDisplay, (ClientData)clockPtr);
clockPtr->flags |= TICKING;
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
The Window Event Procedure
Each widget registers an event handler for expose and resize events. If it implements a focus highlight,
it also needs to be notified of focus events. If you have used other toolkits, you may expect to register
callbacks for mouse and keystroke events too. You should not need to do that. Instead, use the regular
Tk bind facility and define your bindings in Tcl. That way they can be customized by applications.
This procedure is identical in both versions of the widget.
Example 46-15 The ClockEventProc handles window events.
static void
ClockEventProc(ClientData clientData, XEvent *eventPtr)
{
Clock *clockPtr = (Clock *) clientData;
if ((eventPtr->type == Expose) &&
(eventPtr->xexpose.count == 0)) {
goto redraw;
} else if (eventPtr->type == DestroyNotify) {
Tcl_DeleteCommandFromToken(clockPtr->interp,
clockPtr->widgetCmd);
/*
* Zapping the tkwin lets the other procedures
* know we are being destroyed.
*/
clockPtr->tkwin = NULL;
if (clockPtr->flags & REDRAW_PENDING) {
Tk_CancelIdleCall(ClockDisplay,
(ClientData) clockPtr);
clockPtr->flags &= ~REDRAW_PENDING;
}
if (clockPtr->flags & TICKING) {
Tk_DeleteTimerHandler(clockPtr->token);
clockPtr->flags &= ~TICKING;
}
/*
* This results in a call to ClockDestroy.
*/
Tk_EventuallyFree((ClientData) clockPtr,
ClockDestroy);
} else if (eventPtr->type == FocusIn) {
if (eventPtr->xfocus.detail != NotifyPointer) {
clockPtr->flags |= GOT_FOCUS;
if (clockPtr->highlightWidth > 0) {
goto redraw;
}
}
} else if (eventPtr->type == FocusOut) {
if (eventPtr->xfocus.detail != NotifyPointer) {
clockPtr->flags &= ~GOT_FOCUS;
if (clockPtr->highlightWidth > 0) {
goto redraw;
}
}
}
return;
redraw:
if ((clockPtr->tkwin != NULL) &&
!(clockPtr->flags & REDRAW_PENDING)) {
Tk_DoWhenIdle(ClockDisplay, (ClientData) clockPtr);
clockPtr->flags |= REDRAW_PENDING;
}
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter46. Writing a Tk Widget in C
Final Cleanup
When a widget is destroyed, you need to free up any resources it has allocated.
The resources associated with attributes are cleaned up by Tk_FreeOptions. The
others you must take care of yourself. The ClockDestroy procedure is called as a
result of the Tk_EventuallyFree call in the ClockEventProc. The
Tk_EventuallyFree procedure is part of a protocol that is needed for widgets that
might get deleted when in the middle of processing. Typically the Tk_Preserve
and Tk_Release procedures are called at the beginning and end of the widget
instance command to mark the widget as being in use. Tk_EventuallyFree will
wait until Tk_Release is called before calling the cleanup procedure. The next
example shows ClockDestroy:
Example 46-16 The ClockDestroy cleanup procedure.
static void
ClockDestroy(clientData)
ClientData clientData; /* Info about entry widget. */
{
register Clock *clockPtr = (Clock *) clientData;
/*
* Free up all the stuff that requires special handling,
* then let Tk_FreeOptions handle resources associated
* with the widget attributes.
*/
if (clockPtr->textGC != None) {
Tk_FreeGC(clockPtr->display, clockPtr->textGC);
}
if (clockPtr->clock != NULL) {
Tcl_Free(clockPtr->clock);
}
if (clockPtr->flags & TICKING) {
Tk_DeleteTimerHandler(clockPtr->token);
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter47. C Library Overview
An Overview of the Tcl C Library
Application Initialization
The Tcl_Main and Tcl_AppInit procedures are illustrated by Example 44-13 on page 630. They
provide a standard framework for creating main programs that embed a Tcl interpreter.
The Tcl_InitStubs procedure must be called during initialization by an extension that has been
linked against the Tcl stub library, which is described on page 647. Tcl_InitStubs is illustrated in
Example 44-1 on page 610.
Creating and Deleting Interpreters
A Tcl interpreter is created and deleted with the Tcl_CreateInterp and Tcl_DeleteInterp
procedures. You can find out if a interpreter is in the process of being deleted with the
Tcl_InterpDeleted call. You can register a callback to occur when the interpreter is deleted with
Tcl_CallWhenDeleted. Unregister the callback with Tcl_DontCallWhenDeleted.
Slave interpreters are created and manipulated with Tcl_CreateSlave, Tcl_GetSlave,
Tcl_GetSlaves, Tcl_GetMaster, Tcl_CreateAlias, Tcl_CreateAliasObj, Tcl_GetAlias,
Tcl_GetAliasObj, Tcl_GetAliases, Tcl_GetInterpPath, Tcl_IsSafe, Tcl_MakeSafe,
Tcl_ExposeCommand, and Tcl_HideCommand.
Creating and Deleting Commands
Register a new Tcl command with Tcl_CreateCommand, and delete a command with
Tcl_DeleteCommand. The Tcl_DeleteCommandFromToken procedure is useful if you need to delete a
command that gets renamed. The Tcl_GetCommandInfo and Tcl_SetCommandInfo procedures query
and modify the procedure that implements a Tcl command and the ClientData that is associated with
the command. The command that uses the Tcl_Obj interface is created with Tcl_CreateObjCommand.
Command procedures are illustrated in Chapter 44.
Configuring Windows
The configuration of a window includes its width, height, cursor, and so on. Tk provides a set of
routines that configure a window and also cache the results. This makes it efficient to query these
settings because the system does not need to be contacted. The window configuration routines are
Tk_ConfigureWindow, Tk_ResizeWindow, Tk_MoveResizeWindow, Tk_SetWindowBorderWidth,
Tk_DefineCursor, Tk_ChangeWindowAttributes, Tk_SetWindowBackground,
Tk_SetWindowColormap, Tk_UndefineCursor, Tk_SetWindowBackgroundPixmap,
Tk_SetWindowBorderPixmap, Tk_MoveWindow, and Tk_SetWindowBorder.
The Tk_SetOptions, Tk_GetOptionValue, Tk_GetOptionInfo, Tk_RestoreSavedOptions,
Tk_FreeSavedOptions, Tk_InitOptions, Tk_FreeConfigOptions, and Tk_DeleteOptionTable
procedures are used to parse command line options with procedures that use Tcl_Obj values. Canvas
item types are supported by Tk_CanvasTagsOption.
Window Coordinates
The coordinates of a widget relative to the root window (the main screen) are returned by
Tk_GetRootCoords. The Tk_GetVRootGeometry procedure returns the size and position of a window
relative to the virtual root window. The Tk_CoordsToWindow procedure locates the window under a
given coordinate.
Window Stacking Order
Control the stacking order of windows in the window hierarchy with Tk_RestackWindow. Windows
higher in the stacking order obscure lower windows.
Window Information
Tk keeps lots of information associated with each window, or widget. The following calls are fast
macros that return the information without calling the X server: Tk_WindowId, Tk_Parent,
Tk_Display, Tk_DisplayName, Tk_ScreenNumber, Tk_Screen, Tk_X, Tk_Y, Tk_Width, Tk_Height,
Tk_Changes, Tk_Attributes, Tk_IsMapped, Tk_IsTopLevel, Tk_ReqWidth, Tk_ReqHeight,
Tk_InternalBorderWidth, Tk_Visual, Tk_Depth, and Tk_Colormap.
Configuring Widget Attributes
The Tk_ConfigureWidget procedure parses command-line specification of attributes and allocates
resources like colors and fonts. Related procedures include Tk_Offset, Tk_ConfigureInfo,
Tk_ConfigureValue, and Tk_FreeOptions. The Tk_GetScrollInfo parses arguments to scrolling
commands like the xview and yview widget operations.
The Selection and Clipboard
Retrieve the current selection with Tk_GetSelection. Clear the selection with Tk_ClearSelection.
Register a handler for selection requests with Tk_CreateSelHandler. Unregister the handler with
Tk_DeleteSelHandler. Claim ownership of the selection with Tk_OwnSelection. Manipulate the
clipboard with Tk_ClipboardClear and Tk_ClipboardAppend.
Event Loop Interface
The standard event loop is implemented by Tk_MainLoop. If you write your own event loop you need
to call Tcl_DoOneEvent so Tcl can handle its events. If you read window events directly, (e.g., through
Tk_CreateGenericHandler), you can dispatch to the correct handler for the event with
Tk_HandleEvent. Note that most of the event loop is implemented in the Tcl library, except for
Tk_MainLoop and the window event handler interface that are part of the Tk library. You can create
handlers for file, timer, and idle events after this call. Restrict or delay events with the
Tk_RestrictEvent procedure.
Handling Window Events
Use Tk_CreateEventHandler to set up a handler for specific window events. Widget implementations
need a handler for expose and resize events, for example. Remove the registration with
Tk_DeleteEventHandler. You can set up a handler for all window events with
Tk_CreateGenericHandler. This is useful in some modal interactions where you have to poll for a
certain event. If you get an event you do not want to handle yourself, you can push it onto the event
queue with Tk_QueueWindowEvent. Delete the handler with Tk_DeleteGenericHandler.
Event Bindings
The routines that manage bindings are exported by the Tk library so you can manage bindings
yourself. For example, the canvas widget does this to implement bindings on canvas items. The
procedures are Tk_CreateBindingTable, Tk_DeleteBindingTable, Tk_CreateBinding,
Tk_DeleteBinding, Tk_BindEvent, Tk_GetBinding, Tk_GetAllBindings, and
Tk_DeleteAllBindings. These are described in the BindTable man page.
Handling Graphic Protocol Errors
You can handle graphic protocol errors by registering a handler with Tk_CreateErrorHandler.
Unregister it with Tk_DeleteErrorHandler. UNIX has an asynchronous interface so the error will be
reported sometime after the offending call was made. You can call the Xlib XSynchronize routine to
turn off the asynchronous behavior in order to help you debug.
Using the Resource Database
The Tk_GetOption procedure looks up items in the resource database. The resource class of a window
is set with Tk_SetClass, and the current class setting is retrieved with Tk_Class.
Managing Bitmaps
Tk maintains a registry of bitmaps by name, (e.g., gray50 and questhead). You can define new
bitmaps with Tk_DefineBitmap, and you can get a handle on the bitmap from its name with
Tk_GetBitmap. Related procedures include Tk_NameOfBitmap, Tk_SizeOfBitmap,
Tk_GetBitmapFromData, Tk_FreeBitmap, Tk_AllocBitmapFromObj, Tk_GetBitmapFromObj, and
Tk_FreeBitmapFromObj.
Creating New Image Types
The Tk_CreateImageType procedure is used to register the implementation of a new image type. The
registration includes several procedures that call back into the implementation to support creation,
display, and deletion of images. When an image changes, the widgets that display it are notified by
calling Tk_ImageChanged. The Tk_NameOfImage procedure returns the Tcl name of an image. The
Tk_GetImageMasterData returns the client data associated with an image type.
Using an Image in a Widget
The following routines support widgets that display images. Tk_GetImage maps from the name to a
Tk_Image data structure. Tk_RedrawImage causes the image to update its display. Tk_SizeOfImage
tells you how big it is. When the image is no longer in use, call Tk_FreeImage. The Tk_DeleteImage
deletes an image.
Photo Image Types
One of the image types is photo, which has its own C interface for defining new formats. The job of a
format handler is to read and write different image formats such as GIF or JPEG so that the photo
image can display them. The Tk_CreatePhotoImageFormat procedure sets up the interface. There are
several support routines for photo format handlers. The Tk_FindPhoto procedure maps from a photo
name to its associated Tk_PhotoHandle data structure. The image is updated with Tk_PhotoBlank,
Tk_PhotoPutBlock, and Tk_PhotoPutZoomedBlock. The image values can be obtained with
Tk_PhotoGetImage. The size of the image can be manipulated with Tk_PhotoExpand,
Tk_PhotoGetSize, and Tk_PhotoSetSize
Canvas Object Support
The C interface for defining new canvas items is exported via the Tk_CreateItemType procedure. The
description for a canvas item includes a set of procedures that the canvas widget uses to call the
implementation of the canvas item type. The Tk_GetItemTypes returns information about all types of
canvas objects. There are support routines for the managers of new item types. The CanvTkwin man
page describes Tk_CanvasGetCoord, Tk_CanvasDrawableCoords, Tk_CanvasSetStippleOrigin,
Tk_CanvasTkwin, Tk_CanvasWindowCoords, and Tk_CanvasEventuallyRedraw. The following
procedures help with the generation of postscript: Tk_CanvasPsY, Tk_CanvasPsBitmap,
Tk_CanvasPsColor, Tk_CanvasPsFont, Tk_CanvasPsPath, and Tk_CanvasPsStipple. If you are
manipulating text items directly, then you can use the Tk_CanvasGetTextInfo procedure to get a
description of the selection state and other details about the text item.
Geometry Management
A widget requests a certain size with the Tk_GeometryRequest procedure. If it draws a border inside
that area, it calls Tk_SetInternalBorder. The geometry manager responds to these requests, although
the widget may get a different size. The Tk_ManageGeometry procedure sets up the relationship
between the geometry manager and a widget. The Tk_MaintainGeometry procedure arranges for one
window to stay at a fixed position relative to another widget. This is used by the place geometry
manager. The relationship is broken with the Tk_UnmaintainGeometry call. The Tk_SetGrid call
enables gridded geometry management. The grid is turned off with Tk_UnsetGrid.
String Identifiers (UIDS)
Tk maintains a database of string values such that a string only appears in it once. The Tk_Uid type
refers to such a string. You can test for equality by using the value of Tk_Uid, which is the string's
address, as an identifier. A Tk_Uid is used as a name in the various GetByName calls introduced below.
The Tk_GetUid procedure installs a string into the registry.
Note: The table of Tk_Uid values is a memory leak. The leak is not serious under normal operation.
However, if you continually register new strings as Tk_Uid values, then the hash table that records
them continues to grow. This table is not cleaned up when Tk is finalized. The mapping from a string
to a constant is better served by the Tcl_GetIndexFromObj call.
Colors, Colormaps, and Visuals
Use Tk_GetColor, Tk_GetColorByValue, Tk_AllocColorFromObj, and Tk_GetColorFromObj to
allocate a color. You can retrieve the string name of a color with Tk_NameOfColor. When you are done
using a color, you need to call Tk_FreeColor or Tk_FreeColorFromObj. You can get a graphics
context for drawing a particular color with Tk_GCForColor. Colors are shared among widgets, so it is
important to free them when you are done using them. Use Tk_GetColormap and Tk_FreeColormap to
allocate and free a colormap. Colormaps are shared, if possible, so you should use these routines
instead of the platform-specific routines to allocate colormaps. The window's visual type is set with
Tk_SetWindowVisual. You can get a visual context with Tk_GetVisual.
3D Borders
The three-dimensional relief used for widget borders is supported by a collection of routines described
by the 3DBorder man page. The routines are Tk_Get3DBorder, Tk_3DBorderGC,
Tk_Draw3DRectangle, Tk_Fill3DRectangle, Tk_Draw3DPolygon, Tk_Free3DBorder,
Tk_Fill3DPolygon, Tk_3DVerticalBevel, Tk_3DHorizontalBevel, Tk_SetBackgroundFromBorder,
Tk_NameOf3DBorder, Tk_3DBorderColor, Tk_Alloc3DBorderFromObj, Tk_Get3DBorderFromObj,
and Tk_Free3DBorderFromObj. Widgets use Tk_DrawFocusHighlight to draw their focus highlight.
Mouse Cursors
Allocate a cursor with Tk_GetCursor, Tk_GetCursorFromData, Tk_GetCursorFromObj, and
Tk_AllocCursorFromObj. Map back to the name of the cursor with Tk_NameOfCursor. Release the
cursor resource with Tk_FreeCursor or Tk_FreeCursorFromObj.
Fonts and Text Display
Allocate a font with Tk_GetFont, Tk_GetFontFromObj, or Tk_AllocFontFromObj. Get the name of a
font with Tk_NameOfFont. Release the font with Tk_FreeFont or Tk_FreeFontFromObj. Once you
have a font you can get information about it with Tk_FontId, Tk_FontMetrics, and
Tk_PostscriptFontName. Tk_MeasureChars, Tk_TextWidth, Tk_DrawChars, and
Tk_UnderlineChars measure and display simple strings. Tk_ComputeTextLayout,
Tk_FreeTextLayout, Tk_DrawTextLayout, Tk_UnderlineTextLayout, Tk_CharBbox,
Tk_DistanceToTextLayout, Tk_PointToChar, Tk_IntersectTextLayout, and
Tk_TextLayoutToPostscript measure and display multiline, justified text.
Graphics Contexts
A graphics context records information about colors, fonts, line drawing styles, and so on. Instead of
specifying this information on every graphics operation, a graphics context is created first. Individual
graphics operations specify a particular graphic context. Allocate a graphics context with Tk_GetGC
and free it with Tk_FreeGC.
Allocate a Pixmap
A pixmap is a simple color image. Allocate and free pixmaps with Tk_GetPixmap and
Tk_FreePixmap.
Screen Measurements
Translate between strings like 4c or 72p and screen distances with Tk_GetPixels,
Tk_GetPixelsFromObj, Tk_GetMMFromObj, and Tk_GetScreenMM. The first call returns pixels
(integers), the second returns millimeters as a floating point number.
Relief Style
Window frames are drawn with a particular 3D relief such as raised, sunken, or grooved. Translate
between relief styles and names with Tk_GetRelief, Tk_GetReliefFromObj, and Tk_NameOfRelief.
Text Anchor Positions
Anchor positions specify the position of a window within it geometry parcel. Translate between strings
and anchor positions with Tk_GetAnchor, Tk_GetAnchorFromObj, and Tk_NameOfAnchor.
Line Cap Styles
The line cap defines how the end point of a line is drawn. Translate between line cap styles and names
with Tk_GetCapStyle and Tk_NameOfCapStyle.
Line Join Styles
The line join style defines how the junction between two line segments is drawn. Translate between
line join styles and names with Tk_GetJoinStyle and Tk_NameOfJoinStyle.
Text Justification Styles
Translate between line justification styles and names with Tk_GetJustify, Tk_GetJustifyFromObj,
and Tk_NameOfJustify.
Atoms
An atom is an integer that references a string that has been registered with the system. Tk maintains a
cache of the atom registry to avoid contacting the system when atoms are used. Use Tk_InternAtom to
install an atom in the registry, and Tk_GetAtomName to return the name given an atom.
X Resource ID Management
Each window system resource like a color or pixmap has a resource ID associated with it. The
Tk_FreeXId call releases an ID so it can be reused. This is used, for example, by routines like
Tk_FreeColor and Tk_FreePixmap.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Part VII: Changes
Part VII describes the changes between versions of Tcl and Tk.
Chapter 48 has notes about porting your scripts to Tcl 7.4 and Tk 4.0. Chapter 49 describes
changes in Tcl 7.5 and Tk 4.1. Chapter 50 describes changes in Tcl 7.6 and Tk 4.2. The Tcl and
Tk version numbers were unified in the next release, Tcl/Tk 8.0, which is described in Chapter
51. New features added Tcl/Tk 8.1 are described in Chapter 52, and changes in Tcl/Tk 8.2 are
described in Chapter 53.
Tcl/Tk 8.3 is in the planning stages as this book goes to press. An early view of these plans are
presented in Chapter 54.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVII. Changes
Chapter 48. Tcl 7.4/Tk 4.0
This chapter has notes about upgrading your application to Tcl 7.4 and Tk 4.0 from earlier versions of
Tk such as Tk 3.6. This includes notable new features that you may want to take advantage of as well
as things that need to be fixed because of incompatible changes.
Porting your scripts from any of the Tk version 3 releases is easy. Not that many things have changed.
The sections in this chapter summarize what has changed in Tk 4.0 and what some of the new
commands are.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
wish
The wish shell no longer requires a -file (or -f) argument, so you can drop this from your script
header lines. This flag is still valid, but no longer necessary.
The class name of the application is set from the name of the script file instead of always being Tk. If
the script is /usr/local/bin/foobar, then the class is set to Foobar, for example.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Obsolete Features
Several features that were replaced in previous versions are now completely unsupported.
The variable that contains the version number is tk_version. The ancient (version 1) tkVersion is no
longer supported.
Button widgets no longer have activate and deactivate operations. Instead, configure their state
attribute.
Menus no longer have enable and disable operations. Instead, configure their state attribute.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
The cget Operation
All widgets support a cget operation that returns the current value of the specified configuration
option. The following two commands are equivalent:
lindex [$w config option] 4
$w cget option
Nothing breaks with this change, but you should enjoy this feature.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Input Focus Highlight
Each widget can have an input focus highlight, which is a border that is drawn in color when the
widget has the input focus. This border is outside the border used to draw the 3D relief for widgets. It
has the pleasant visual effect of providing a little bit of space around widgets, even when they do not
have the input focus. The addition of the input focus highlight does not break anything, but it changes
the appearance of your interfaces a little. In particular, the highlight on a canvas obscures objects that
are at its edge. See page 531 for a description of the generic widget attributes related to the input focus
highlight.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Bindings
The hierarchy of bindings has been fixed so that it is actually useful to define bindings at each of the
global (i.e., all), class, and instance levels. The new bindtags command defines the order among
these sources of binding information. You can also introduce new binding classes, (e.g., InsertMode)
and bind things to that class. Use the bindtags command to insert this class into the binding
hierarchy. The order of binding classes in the bindtags command determines the order in which
bindings are triggered. Use break in a binding command to stop the progression, or use continue to
go on to the next level.
bindtags $w [list all Text InsertMode $w]
The various Request events have gone away: CirculateRequest, ConfigureRequest, MapRequest,
and ResizeRequest. The Keymap event is gone, too.
Extra modifier keys are ignored when matching events. While you can still use the Any wild card
modifier, it is no longer necessary. The Alt and Meta modifiers are set up in a general way so they are
associated with the Alt_L, Alt_R, Meta_L, and Meta_R keysyms.
Chapter 26 describes bindings starting at page 369.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Scrollbar Interface
The interface between scrollbars and the scrollable widgets has changed. Happily, the change is
transparent to most scripts. If you hook your scrollbars to widgets in the straightforward way, the new
interface is compatible. If you use the xview and yview widget commands directly, however, you
might need to modify your code. The old interface still works, but there are new features of these
operations that give you even better control. You can also query the view state so you do not need to
watch the scroll set commands to keep track of what is going on. Finally, scrollable widgets are
constrained so that the end of their data remains stuck at the bottom (right) of their display. In most
cases, nothing is broken by this change. Chapter 30 describes the scrollbar protocol starting at page
431.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
pack info
Version 3 of Tk introduced a new syntax for the pack command, but the old syntax was still
supported. This continues to be true in nearly all cases except the pack info command. If you are still
using the old packer format, you should probably take this opportunity to convert to the new packer
syntax.
The problem with pack info is that its semantics changed. The new operation used to be known as
pack newinfo. In the old packer, pack info returned a list of all the slaves of a window and their
packing configuration. Now pack info returns the packing configuration for a particular slave. You
must first use the pack slaves command to get the list of all the slaves and then use the (new) pack
info to get their configuration information. Chapter 23 describes the pack geometery manager starting
at page 336.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Focus
The focus mechanism has been cleaned up to support different focus windows on different screens.
The focus command takes a -displayof argument. Tk remembers which widget inside each toplevel
has the focus. When the focus is given to a toplevel by the window manager, Tk automatically assigns
focus to the right widget. The -lastfor argument queries which widget in a toplevel will get the focus
by this means. Chapter 36 describes focus starting at page 518.
The focus default and focus none commands are no longer supported. There is no real need for
focus default anymore, and focus none can be achieved by passing an empty string to the regular
focus command.
The tk_focusFollowsMouse procedure changes from the default explicit focus model where a widget
must claim the focus to one in which moving the mouse into a widget automatically gives it the focus.
The tk_focusNext and tk_focusPrev procedures implement keyboard traversal of the focus among
widgets. Most widgets have bindings for <Tab> and <Shift-Tab> that cycle the focus among widgets.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
The send Command
The send command has been changed so that it does not time out after 5 seconds, but instead waits
indefinitely for a response. Specify the -async option if you do not want to wait for a result. You can
also specify an alternate display with the -displayof option. Chapter 40 describes send starting on
page 562.
The name of an application can be set and queried with the new tk appname command. Use this
instead of winfo name ".".
Because of the changes in the send implementation, it is not possible to use send between Tk 4.0
applications and earlier versions.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Internal Button Padding
Buttons and labels have new defaults for the amount of padding around their text. There is more
padding now, so your buttons get bigger if you use the default padX and padY attributes. The old
defaults were one pixel for both attributes. The new defaults are 3m for padX and 1m for padY, which
map into three pixels and ten pixels on my display.
There is a difference between buttons and the other button-like widgets. An extra two pixels of
padding is added, in spite of all padX and padY settings in the case of simple buttons. If you want your
checkbuttons, radiobuttons, menubuttons, and buttons all the same dimensions, you'll need two extra
pixels of padding for everything but simple buttons.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Radiobutton Value
The default value for a radiobutton is no longer the name of the widget. Instead, it is an empty string.
Make sure you specify a -value option when setting up your radiobuttons.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Entry Widget
The scrollCommand attribute changed to xScrollCommand to be consistent with other widgets that
scroll horizontally. The view operation changed to the xview operation for the same reason. Chapter
31 describes the entry widget starting on page 437.
The delete operation has changed the meaning of the second index so that the second index refers to
the character just after the affected text. The selection operations have changed in a similar fashion.
The sel.last index refers to the character just after the end of the selection, so deleting from
sel.first to sel.last still works. The default bindings have been updated, of course, but if you
have custom bindings you must fix them.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Menus
The menu associated with a menubutton must be a child widget of the menubutton. Similarly, the
menu for a cascade menu entry must be a child of the menu.
The @y index for a menu always returns a valid index, even if the mouse cursor is outside any entry. In
this case, it simply returns the index of the closest entry, instead of none.
The selector attribute is now selectColor.
The postcascade operation posts the menu of a cascade entry:
$menu postcascade index
The insert operation adds a menu entry before a specified entry:
$menu insert index type options...
Chapter 27 describes menus starting at page 396.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Listboxes
Listboxes changed quite a bit in Tk 4.0. See Chapter 32 for all the details. There are now four Motif-
like selection styles, and two of these support disjoint selections. The tk_listboxSingleSelect
procedure no longer exists. Instead, configure the selectMode attribute of the listbox. A listbox has an
active element, which is drawn with an underline. It is referenced with the active index keyword.
The selection commands for listboxes have changed. Change:
$listbox select from index1
$listbox select to index2
to:
$listbox select anchor index1
$listbox select set anchor index2
The set operation takes two indices, and anchor is a valid index, which typically corresponds to the
start of a selection.
You can selectively clear the selection, and query if there is a selection in the listbox. The command to
clear the selection has changed. It requires one or two indices. Change:
$listbox select clear
to:
$listbox select clear 0 end
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
No geometry Attribute
The frame, toplevel, and listbox widgets no longer have a geometry attribute. Use the width and
height attributes instead. The geometry attribute got confused with geometry specifications for top-
level windows. The use of width and height is more consistent. Note that for listboxes the width and
height are in terms of lines and characters, while for frames and toplevels it is in screen units.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Text Widget
The tags and marks of the text widgets have been cleaned up a bit, justification and spacing are
supported, variable tab stops can be defined, and you can embed widgets in the text display.
A mark now has a gravity, either left or right, that determines what happens when characters are
inserted at the mark. With right gravity you get the old behavior: the mark gets pushed along by the
inserted text by sticking to the right-hand character. With left gravity it remains stuck. The default is
right gravity. The mark gravity operation changes it.
When text is inserted, it only picks up tags that are present on both sides of the insert point. Previously
it would inherit the tags from the character to the left of the insert mark. You can also override this
default behavior by supplying tags to the insert operation.
The widget scan operation supports horizontal scrolling. Instead of using marks like @y, you need a
mark like @x,y.
For a description of the new features, see Chapter 33.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Color Attributes
Table 48-1 lists the names of the color attributes that changed. These attributes are described in more
detail in Chapter 38 starting at page 536.
Table 48-1. Changes in color attribute names.
Tk 3.6 Tk4.0
selector selectColor
Scrollbar.activeForeground Scrollbar.activeBackground
Scrollbar.background troughColor
Scrollbar.foreground Scrollbar.background
Scale.activeForeground Scale.activeBackground
Scale.background troughColor
Scale.sliderForeground Scale.background
(did not exist) highlightBackground
(did not exist) highlightColor
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Color Allocation and tk colormodel
In Tk 3.6 color allocations could fail if the colormap was full. In this case Tk would revert its
colormodel to monochrome and only use black and white. The tk colormodel command was used to
query or set the colormodel. In Tk 4.0 color allocations do not fail. Instead, the closest possible color
is allocated. Because of this, the tk colormodel operation is no longer supported. Use the winfo
visual command which is described on page 540, to find out the characteristics of your display,
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
Canvas scrollincrement
The canvas widget changed the scrollIncrement attribute to a pair of attributes: xScrollIncrement
and yScrollIncrement. The default for these is now one-tenth the width (height) of the canvas
instead of one pixel. Scrolling by one page scrolls by nine-tenths of the canvas display.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
The Selection
The selection support has been generalized in Tk 4.0 to allow use of other selections such as the
CLIPBOARD and SECONDARY selections. The changes do not break anything, but you should check out
the new clipboard command. Some other toolkits, notably OpenLook, can only paste data from the
clipboard. Chapter 35 describes the selection starting at page 507.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter48. Tcl 7.4/Tk 4.0
The bell Command
The bell command rings the bell associated with the terminal. You need to use the xset program to
modify the parameters of the bell such as volume and duration. This command is described on page
428.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVII. Changes
Chapter 49. Tcl 7.5/Tk 4.1
Tk 4.1 is notable for its cross-platform support. Your Tk scripts can run on Windows, Macintosh, as
well as UNIX. The associated Tcl release, 7.5, saw significant changes in event-driven I/O, network
sockets, and multiple interpreters.
Cross-platform support, network sockets, multiple Tcl interpreters, and an enhanced foreach
command are the highlights of Tcl 7.5 and Tk 4.1.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
Cross-Platform Scripts
Cross-platform support lets a Tcl/Tk script run unchanged on UNIX, Windows, and Macintosh.
However, you could still have platform dependencies in your program. The most obvious is if your
script executes other programs or uses C-level extensions. These need to be ported for your script to
continue to work.
File Name Manipulation
File naming conventions vary across platforms. New file operations were added to help you
manipulate file names in a platform-independent manner. These are the file join, file split, and
file pathtype operations, which are described on page 103. Additional commands to copy, delete,
and rename files were added in Tcl 7.6
Newline Translations
Windows and Macintosh have different conventions for representing the end of line in files. These
differences are handled automatically by the new I/O subsystem. However, you can use the new
fconfigure command described on page 221 to control the translations.
The tcl_platform Variable
In practice you may need a small amount of platform-specific code. The tcl_platform array holds
information about the computer and operating system that your script is running on. This array is
described on page 182. You can use a script file with the name of the platform to isolate all your
platform-specific code. The following command sources either unix.tcl, windows.tcl, or
macintosh.tcl from your script library:
source [file join $lib $tcl_platform(platform).tcl]
The console Command
The Windows and Macintosh versions of wish have a built-in console. The commands you enter in the
console are evaluated in the main Tcl interpreter, but the console is really implemented in another Tcl
interpreter to avoid conflicts. You can show and hide the console with the console command, which is
described on page 28.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
The clock Command
The clock command eliminates the need to exec date to get the time of day in Tcl. The equivalent is:
clock format [clock seconds]
The format operation takes an optional format string that lets you control the date and time string.
There is also clock scan to parse clock values, and clock clicks to get high resolution clock values.
The clock command is described on page 173.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
The load Command
The load command supports shared libraries (i.e., DLLs) that implement new Tcl commands in
compiled code. With this feature the preferred way to package extensions is as a shared library. This
eliminates the need to compile custom versions of wish if you use extensions. The details about
creating shared libraries are described on page 609. For example, you could load the Tix library with:
load libtix.so Tix
The info command added two related operations, sharedlibextention and nameofexecutable,
which are described on page 182.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
The package Command
The package command provides an alternate way to organize script libraries. It also supports
extensions that are added with the load command. The package command supports a provide/require
model where packages are provided by scripts in a library, and your application specifies what it needs
with package require commands. The package facility supports multiple versions of a package, if
necessary. Packages are described on page 163.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
Multiple foreach loop variables
This is one of my favorite features. The foreach command supports multiple loop variables and
multiple value lists. This means you can assign values to multiple variables during each loop iteration.
The values can come from the same list, or from lists that are processed in parallel. This is described
on page 75. For example, you can iterate through the contents of an array with:
foreach {name value}[array get arrName] {
# arrName($name) is $value
}
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
Event Loop Moves from Tk to Tcl
To support network sockets, the event loop was moved from Tk to Tcl. This means that the after and
update commands are now part of Tcl. The fileevent command was added to support non-blocking
I/O. The vwait command was added to Tcl, and this is equivalent to the tkwait variable command.
Event-driven I/O is described in Chapter 16 starting on page 217.
The tkerror command has been replaced by bgerror. This is the procedure that is called when an
error occurs while processing an event. Backwards compatibility is provided if you already define
tkerror. These procedures are described on page 190.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
Network Sockets
The socket command provides access to TCP/IP sockets. There are C APIs to define new channels,
and there are extensions that provide UDP and other protocols. Chapter 17 describes sockets starting
on page 228. Example 40-4 on page 567 uses sockets as a replacement for the Tk send command.
info hostname
The info hostname command was added to find out your host identifier.
The fconfigure Command
The best way to use sockets is with event-driven I/O. The fileevent command provides part of the
solution. You also need to be able to control the blocking behavior and buffering modes of sockets.
The fconfigure command lets you do this, and more. You can also control the newline translation
modes and query socket-specific settings. The fconfigure command is described on page 221.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
Multiple Interpreters and Safe-Tcl
Chapter 19 describes the new interp command and the Safe-Tcl security mechanism. You can create
multiple Tcl interpreters in your application and control them with the interp command. You create
command aliases so that the interpreters can exchange Tcl commands. If an interpreter is created in a
safe mode, then its set of Tcl commands is restricted so that its scripts cannot harm your computer or
application. However, with aliases you can give the untrusted scripts limited access to resources.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
The grid Geometry Manager
Chapter 24 describes the new grid geometry manager that provides a table-like metaphor for
arranging widgets. Like pack, grid is constraint-based so the grid automatically adjusts if widgets
change size or if widgets are added and deleted. The grid command was influenced by the blt_table
geometry manager, but it is a whole new implementation.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
The Text Widget
A handful of new operations were added to the text widget. The dump operation provides a way to get
all the information out of the widget, including information about tags, marks, and embedded
windows. The mark next and mark previous operations let you search for marks. The tag
prevrange is the complement of the existing tag nextrange operation.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter49. Tcl 7.5/Tk 4.1
The Entry Widget
The bbox operation was added to the entry widget. This is used to refine the bindings that do character
selection and set the input cursor.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVII. Changes
Chapter 50. Tcl 7.6/Tk 4.2
Tk 4.2 saw improvements in its cross-platform support., including virtual events, additions to the file
command, improvements to the exec command on Windows, and the addition of common dialogs for
choosing colors and selecting files. The grid geometry manager was rebuilt to improve its layout
algorithm.
Grid saw a major rewrite for Tk 4.2 to improve its layout algorithm. Cross-platform scripts were
enhanced by the addition of standard dialogs and virtual events. Tcl 7.6 saw improvements in exec
and pipelines on Windows. The Macintosh version got a significant performance boost from a new
memory allocator.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter50. Tcl 7.6/Tk 4.2
More file Operations
The file command was rounded out with copy, rename, delete, and mkdir operations. These
operations are described on page 105.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter50. Tcl 7.6/Tk 4.2
Virtual Events
The new event command defines virtual events like <<Cut>> <<Copy>> and <<Paste>>. These virtual
events map to different physical events on different platforms. For example, <<Copy>> is <Control-c>
on Windows and <Command-c> on Macintosh. You can write your scripts in terms of virtual events,
and you can define new virtual events for your application. You can also use the event command to
generate events for testing purposes. Virtual events and the event command are described starting at
page 380.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter50. Tcl 7.6/Tk 4.2
Standard Dialogs
Several standard dialogs were added to Tk. These let you display alerts, prompt the user, choose
colors, and select files using dialogs that are implemented in native look for each platform. For
example, to ask the user a yes/no question:
tk_messageBox -type yesno \
-message "Ok to proceed?" \
-icon question
=> yes
To open an existing file:
set file [tk_getOpenFile]
The standard dialogs are described in Chapter 36 starting at page 515.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter50. Tcl 7.6/Tk 4.2
New grid Geometry Manager
The grid geometry manager was overhauled to improve its layout algorithm, and there were several
user-visible changes. The weights on rows and columns that affect resize behavior were changed from
floating point values to integers. A -pad row and column attribute was added to provide padding for a
whole row or column. The columnconfigure and rowconfigure operations now return the current
settings if given no arguments. There are two new grid operations. The update operation forces an
update of the grid layout. The remove operation removes a widget from the grid but remembers all its
grid settings so it is easy to put it back into the grid later.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter50. Tcl 7.6/Tk 4.2
Macintosh unsupported1 Command
The unsupported1 command provides access to different window styles on the Macintosh. If
supported it might be a style operation in the wm command, but it is Macintosh specific so it is not
fully supported. However, you can use it to get several different styles of Macintosh windows. This
command is described on page 419.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVII. Changes
Chapter 51. Tcl/Tk 8.0
Tcl 8.0 includes an on-the-fly byte code compiler that improves performance of scripts from 2 to 20
times depending on what commands they use. The Tk version number was set to match Tcl. Tk 8.0
uses native buttons, menus, menubars, and scrollbars. Font objects allow flexible font handling in a
platform-independent way.
Tcl 8.0 added a byte-code compiler that improves performance dramatically. The compiler is
transparent to Tcl scripts so you do not have to do anything special to take advantage of it. The other
main addition to Tcl is support of binary data. It is now safe to read binary data into Tcl variables, and
new commands convert between strings and binary representations.
Tk 8.0 has native look and feel on UNIX, Windows, and Macintosh. This is due to native buttons,
native menus, native scrollbars, and a new cross-platform menu bar facility. A new cross-platform font
facility improves the font support. Tk also has support for application embedding, which is used in the
Web browser plug-in described in Chapter 20.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
The Tcl Compiler
The Tcl Compiler is an on-the-fly compiler that is virtually transparent to Tcl scripts. The compiler
translates a script to byte codes the first time it evaluates it. If the script is evaluated again, such as in a
loop or in a procedure body, then the byte codes are executed and the translation step is saved. If a
procedure is redefined, then the compiler discards any translated byte codes for it.
The compiler uses a dual-ported object model instead of the simple string-based model used in earlier
versions of Tcl. The dual-ported objects hold a string value and a native representation of that string
such as an integer, double-precision floating point value, or compiler byte codes. This makes it
possible to save translations between strings and the native representations. The object model is
described in Chapter 44 starting at page 615.
The performance improvement for your application will depend on what features you use. Math
expressions and list accesses have improved a lot. Overall you should expect a factor of 2 speedup,
and I have heard reports of 10 and 20 times improvement in parts of some applications.
Compile-Time Errors
The compiler catches some sorts of errors earlier than the pure interpreted version. The first time a
compiler runs a procedure, it translates the whole thing to byte codes first. If there are syntax errors at
the end of the procedure it prevents any code in the procedure from running.
A similar problem occurs with list data. If a string is not a perfect list, then the list commands will fail
when parsing it, even if they do not use the damaged part of the list. For example, lindex used to
process only enough of a list to find the requested element. In Tcl 8.0 the whole list is converted into a
native representation. Errors at the end of a list will prevent you from getting elements at the
beginning. This is mainly an issue when you use list operations on arbitrary input data.
Binary String Support
Tcl now supports binary data. This means that an embedded NULL byte no longer terminates a value.
Instead, Tcl keeps a byte count for its string objects. This is facilitated by the switch from simple
strings to dual-ported objects.
The binary format and binary scan commands support conversions between binary data and
strings. These are described on page 54. The unsupported0 command was improved and became the
fcopy command, which is described on page 239.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Namespaces
Chapter 14 describes the Tcl namespace facility that partitions the global scope for variables and
procedures. Namespaces are optional. Simple scripts can avoid them, but larger applications can use
them for structuring. Library packages should also use namespaces to facility code sharing without
conflict.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Safe-Tcl
Hidden commands were added to the Safe-Tcl security model. Instead of removing unsafe commands
from an interpreter, they are hidden. The master can invoke hidden commands inside a slave. This is
necessary so that the command sees the correct context. This adds new operations to the interp
command: invokehidden, hide, expose, and hidden. Hidden commands are described on page 281.
Initialization of a safe interpreter with a safe base that supports auto loading and a standard exit alias
has been abstracted into a Tcl interface. The safe::interpCreate and safe::interpInit procedures
create or initialize a slave with the safe base. The safe::interpDelete procedure cleans up. The safe
base is described on page 284.
To support the Trusted security policy, the interp marktrusted command was added. This
promotes an unsafe interpreter back into a trusted one. Of course, only the master can do this.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
New lsort
The lsort command was reimplemented. The new implementation is reentrant, which means you can
use lsort inside a sorting function called by lsort. New options have lessened the need for custom
sorting procedures, too. The -dictionary option sorts cases together and it handles numbers better.
The -index option sorts lists on a key field. These are described on page 65.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
tcl_precision Variable
The tcl_precision variable was removed in the 8.0p2 release, and added back in the 8.0.3 release. Its
default value was increased from 6 to 12, which should be enough for most applications.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Year 2000 Convention
The clock command now implements the following standard convention for two-digit year names:
70-99 map to 1970 - 1999.
00-69 map to 2000 - 2069.
Actually, the ability to parse a date value after 2037 and before 1903 may be limited on your system by
the size of an integer (e.g., 32-bits) and the clock epoch, which is January 1, 1970 on Windows and
most UNIX systems.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Http Package
A Tcl implementation of the HTTP/1.0 protocol was added to the Tcl script library. The http::geturl
command is described on page 240.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Serial Line I/O
Support for serial line devices was added to fconfigure. The -mode argument specifies the baud rate,
parity setting, and the number of data and stop bits. The -mode option to fconfigure is described on
page 224.
Windows has some special device names that always connect you to the serial line devices when you
use open. They are com1 and com2. UNIX has names for serial devices in /dev. Interactive applications
can open the current terminal with /dev/tty.
As of this writing there is no way to open serial devices on the Macintosh. I expect a new serial
command for this purpose, or possibly a flag to open.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Platform-Independent Fonts
A platform-independent font naming system was added in Tk 8.0. Names like times 10 bold are
interpreted on all platforms. The font command lets you create font objects that can be associated
with widgets. The font metrics command returns detailed size information. The font command is
described on page 554.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
The tk scaling Command
The tk scaling command queries or sets the mapping from pixels to points. Points are used with
fonts, and points and other screen measures are used in the canvas. The tk scaling command is
described on page 582.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Application Embedding
Tk supports application embedding. Frames and toplevels have a -container attribute that indicates
they embed another application. This is necessary for geometry management and focus protocols.
Frames and toplevels have a -use parameter that embeds them into an existing window. Wish also
takes a -use command-line argument. Embedding is described on page 419 and 296.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Native Menus and Menubars
Tk 8.0 has a native menubar mechanism. You define a menu and associate it with a toplevel. On the
Macintosh this menu appears along the top of the screen when the window is activated. On Windows
and UNIX the menubar appears along the top of the window. This facility is described on page 397.
Tear-off menus now track any changes to the menu they were created from. As part of this, the -
transient attribute was replaced with a -type attribute.
You can create multicolumn menus with the -columnbreak attribute.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
CDE Border Width
On UNIX, the default border width changed from 2 to 1 to match the CDE look and feel.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Native Buttons and Scrollbars
Buttons, menus, and scrollbars are native widgets on Windows and Macintosh. This goes a long way
to providing your applications with a native look. The bindings on the text and entry widgets were also
tuned to match platform standard bindings. See page 332 for an example of the same Tk program on
all platforms.
Buttons on all platforms support a -default attribute, which has three values: active, normal, and
disabled. The active state displays as a default button. The normal state displays like a regular
button, but leaves room for the highlight used in the active state. The disabled state, which is the
default, may be smaller. You still need to program a key binding that invokes the button.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
Images in Text Widgets
The text widget supports embedded images. They work much like the embedded windows but provide
a more efficient way to embed images. These are described on page 466.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
No Errors from destroy
The destroy command used to raise an error if the window did not exist. Now it does not.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
grid rowconfigure
The grid columnconfigure and rowconfigure commands take an argument that specifies a row or
column. This value can be a list:
grid columnconfigure {0 3}-weight 1
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter51. Tcl/Tk 8.0
The Patch Releases
There was a hiatus in the development of the 8.0 release as John Ousterhout left Sun Microsystems to
form Scriptics Corporation. The 8.0p2 release was made in the Fall of 1997, about the same time the
first alpha release of 8.1 was made. Almost a year later there were a series of patch releases: 8.0.3,
8.0.4, and 8.0.5 that were made in conjunction with releases of the TclPro development tools. The
main changes in these patch releases were in C APIs that were added to support TclPro Wrapper and
TclPro Compiler. These tools are introduced on page 189. There were only a few changes after 8.0p2
that were visible to Tcl script writers, and these are documented here.
fconfigure -error
The fconfigure -error option was added so that you can find out whether or not an asynchronous
socket connection attempt has failed. It returns an empty string if the connection completed
successfully. Otherwise, it returns the error message.
tcl_platform(debug)
A new element was added to the tcl_platform variable to indicate that Tcl was compiled with
debugging symbols. The motivation for this is the fact that a Windows application compiled with
debugging symbols cannot safely load a DLL that has been compiled without debugging symbols.
Similarly, an application compiled without debugging symbols cannot safely load a DLL that does
have debugging symbols. The problem is an artifact of the Microsoft C runtime library.
When you build Tcl DLLs with debugging symbols their name has a trailing "d", such as tcl80d.dll
instead of tcl80.dll. By testing tcl_platform(debug) a savvy application can attempt to load a
matching DLL.
tcl_findLibrary
The tcl_findLibrary procedure was added to help extensions find their script library directory. This
is used by Tk and other extensions. The big picture is that Tcl has a complex search path that it uses to
find its own script library. It searches relative to the location of tclsh or wish and assumes a standard
installation or a standard build environment. This supports sites that have several Tcl installations and
helps extensions find their correct script library. The usage of tcl_findLibrary is:
tcl_findLibrary base version patch script enVar varName
The base is the prefix of the script library directory name. The version is the main version number
(e.g., "8.0"). The patch is the full patch level (e.g., "8.0.3"). The script is the initialization script to
source from the directory. The enVar names an environment variable that can be used to override the
default search path. The varName is the name of a variable to set to name of the directory found by
tcl_findLibrary. A side effect of tcl_findLibrary is to source the script from the directory. An
example call is:
tcl_findLibrary tk 8.0 8.0.3 tk.tcl TK_LIBRARY tk_library
auto_mkindex_old
The auto_mkindex procedure was reimplemented by Michael McLennan to support [incr Tcl] classes
and methods. This changed the semantics somewhat so that all procedures are always indexed.
Previously, only procedures defined with the word "proc" starting at the beginning of a line were
indexed. The new implementation sources code into a safe interpreter and watches for proc commands
however they are executed, not how they are typed into the source file. The old version of
auto_mkindex was saved as auto_mkindex_old for those applications that used the trick of indenting
procedure definitions to hide them from the indexing process.
Windows Keysyms for Start and Menu Keys
The Microsoft keyboards have special keys that bring up the Start menu and trigger menu traversal.
New keysyms App, Menu_L, and Menu_R were added for these keys.
The MouseWheel Event
The MouseWheel event was added to support the scrolling wheel built into Microsoft mice. There is a
%d event parameter that gets replaced with a positive or negative number to indicate relative scrolling
motion.
Transparent Fill on Canvas Text
Canvas text items now honor the convention that an empty fill attribute turns them transparent. This
convention was previously implemented for all other canvas item types. This feature makes it easy to
hide items with:
$canvas itemconfigure item -fill ""
safe::loadTk
This procedure was extended to take a -display displayname argument so you can control where the
main window of the safe interpreter is created. Its -use argument was extended to take either window
IDs or Tk window pathnames.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVII. Changes
Chapter 52. Tcl/Tk 8.1
Tcl/Tk 8.1 features Unicode support for Internationalization, thread safety, and a new regular
expression package.
Tcl 8.1 should probably have been called Tcl 9.0. The internal changes required to support Unicode
caused a major overhaul that touched nearly the entire implementation. At the same time, the code
base was cleaned up so it could be used in multi-threaded environments, and it added a platform-
independent dynamic loading facility (i.e., stub libraries). Finally, an all-new regular expression
package was added thanks to Henry Spencer. This package brings Advanced Regular Expressions to
Tcl, which are already familar to Perl programmers. However, in spite of all these changes, scripts
written for earlier versions of Tcl are very compatible with Tcl 8.1.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter52. Tcl/Tk 8.1
Unicode and Internationalization
The effect of Unicode on Tcl scripts is actually very limited. There is a new backslash sequence,
\uXXXX, that specifies a 16-bit Unicode character. There are also facilities to work with character set
encodings and message catalogs.
fconfigure -encoding
The Tcl I/O system supports character set translations. It automatically converts files to Unicode when
it reads them in, and it converts them to the native system encoding during output. The fconfigure -
encoding option can be used to specify alternate encodings for files. This option is described on page
209.
The encoding Command
The encoding command provides access to the basic encoding mechanism used in Tcl. The encoding
convertfrom and convertto operations convert strings between different encodings. The encoding
system operation queries and sets the encoding used by the operating system. The encoding command
is described on page 212.
The msgcat Package
Message catalogs are implemented by the msgcat package, which is described on page 216. A message
catalog stores translations of user messages into other languages. Tcl makes message catalogs easy to
use.
UTF-8 and Unicode C API
The effects of Unicode on the Tcl C API is more fundamental. Tcl uses UTF-8 to represent Unicode
internally. This encoding is compatible with ASCII, so Tcl extentions that only pass ASCII strings to
Tcl continue to work normally. However, to take advantage of Unicode, Tcl extensions need to
translate strings into UTF-8 or Unicode before calling the Tcl C library. There is a C API for this. An
testthread create
?script?
Creates a new thread and a Tcl interpreter. Runs script after creating the Tcl
interpreter. If no script is specified, the new thread waits with testthread
wait.
testthread id
Returns the thread ID of the current thread.
testthread
errorproc proc
Register proc as a hander for errors from other threads. If they terminate with
a Tcl error, this procedure is called with the error message and errorInfo
values as arguments. Otherwise a message is printed to stderr.
testthread exit
Terminate the current thread.
testthread names
Returns a list of thread IDs.
testthread send
id ?-async? script
Send a script to another thread for evaluation. If -async is specified, the
command does not wait for the result.
testthread wait
Enter the event loop. This is used by worker threads to wait for scripts to
arrive for evaluation.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter52. Tcl/Tk 8.1
Advanced Regular Expressions
An all new regular expression implemenation supports Unicode and Advanced Regular Expressions,
which are described in detail in Chapter 11. The new regular expression syntax has been added in a
way that is compatible with earlier versions of regular expressions. There are also new regexp and
regsub command options to control the new regular expression engine.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter52. Tcl/Tk 8.1
New String Commands
The string command was enhanced with several new operations that include string classification
operations (string is), character string mappings (string map), title case conversion (string
totitle), an easier to use equality test (string equal), and new string manipulation commands
(string repeat and string replace). The -nocase and -length option has been added to
commands like string compare and string tolower. These additions are listed in Table 4-1 on
page 46 and explained in more detail in Chapter 4. Note, these only appeared in the Tcl 8.1.1 patch
release.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter52. Tcl/Tk 8.1
The DDE Extension
DDE (Dynamic Data Exchange) is a communication protocol used among Windows applications. The
protocol exchanges data with a server identified by name. Each service implements a number of
operations known as topics. The data exchange can be synchronous or asynchronous. The dde
command is implemented as an extention that is distributed with Tcl. You must use package require
dde to load the extension. The dde command is summarized in Table 52-2
Table 52-2. The dde command options.
dde servername ?
topic?
Registers the current process as a DDE service with name TclEval and
the given topic. If topic is not specified, this command returns the
currently registered topic.
dde ?-async? execute
service topic data
Sends data to the service with the given topic.
dde ?-async? eval
topic cmd ?arg ...?
Sends cmd and its arguments to the TclEval service with the given
topic. This is an alternative to the Tk send command.
dde ?-async? poke
service topic data
Similar to the execute operation, but some services export operations
under poke instead of execute.
dde ?-async? request
service topic item
Fetches the named item from the service with the given topic.
dde services server
topic
Returns server and topic if that server currently exists, otherwise it
returns the empty string.
dde services server
{}
Returns all the topics implemented by server.
dde services {}topic Returns all servers that implement topic.
dde services {} {}
Returns all server, topic registrations.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter52. Tcl/Tk 8.1
Miscellaneous
Serial Line I/O
The serial line drivers were converted to use threads so you can wait for I/O with fileevent when
using serial devices. There is no API change here, but this was a limitation that was annoying in
previous Tcl releases.
tcl_platform(user)
The tcl_platform(user) array element records the currently logged in user. This masks difference in
environment variables and system calls used to get this information on different platforms.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVII. Changes
Chapter 53. Tcl/Tk 8.2
Tcl 8.2 is primarily a bug fix and stabilization release. This release is recommended instead of Tcl 8.1.
Tcl 8.2 adds almost no new features at the Tcl script level. Instead, it adds a few new C APIs that
enable some interesting extensions to be added without having to modify the core Tcl distribution. At
the same time, Scriptics focused on the outstanding bug reports in order to make the 8.2 release as
stable as possible.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter53. Tcl/Tk 8.2
The Trf Patch
Andreas Kupries contributed a mechanism that allows I/O channel processing modules to be stacked
onto open I/O channels. This adds a new Tcl_StackChannel C API, but there are no changes visible at
the Tcl script level. However, it enables several interesting extentions such as compression and
encyption (e.g., SSL) to be added to Tcl. Andreas has an extension that exports the channel filter
mechanism to the Tcl script level. This is used primarily for testing, but you can also use it for script-
level data filters.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter53. Tcl/Tk 8.2
Faster String Operations
The UTF-8 encoding has the drawback that characters are not all the same size: they are either one,
two, or three bytes in length. This makes operations like string length, string index, and string
range quite slow in comparison to a system that uses fixed-sized characters. A new Unicode string
type, which uses 16-bit characters, was added to support faster string operations. This does not cause
any changes that are visible to Tcl scripts, except for improved performance in comparison to Tcl 8.1.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter53. Tcl/Tk 8.2
Empty Array Names
Perhaps the only change in Tcl 8.2 visible to Tcl scripts is support for empty array names. This is a
quirk you can get by using name spaces or upvar, but it was previously difficult to use directly. For
example, the syntax $::foo::(item) references an array. That worked in any version of Tcl that
supported namespaces. However, in Tcl 8.2 you can also use $(item) directly, which implies that the
array name is the empty string. This trick is exploited by Jean-Luc Fontain's STOOOP object-oriented
extension.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter53. Tcl/Tk 8.2
Brower Plugin Compatiblity
The Web Browser plugin requires changes to the event loop mechanism because Tcl is embedded in
an application with its own event loop. While the C APIs have supported this since Tcl 8.0.3, it has
been dificult to do this sort of embedding wthout recompiling Tcl. The Tcl_SetNotifier API was
added to support embedding a "stock" Tcl interpreter.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
PartVII. Changes
Chapter 54. Tcl/Tk 8.3
This chapter provides a look ahead at the next Tcl release.
Tcl 8.3 is already in the planning stages as this book goes to press. The overall theme of 8.3 is to
continue the incorporation of contributed patches, especially for Tk. Jan Nijtmans' dash and image
patches have many handy features such as dashed lines on the canvas, improved image support, and
more. These additions will be added in a compatible way. In many cases, a patch only makes small
changes to the Tcl or Tk core library that enables a more sophisticated extension to loaded without
changing the core. The long term goal for Tcl/Tk is that interesting additions can be made as
extensions that do not require changes to the Tcl core. Many extensions are available today: You can
find them on the Web at:
http://www.scriptics.com/resource/
However, some changes require changes to Tcl/Tk itself. If you are interested in contributing to the
continued improvement of Tcl/Tk, you can help. Contributions should be made via the Scriptics' Web
site at:
http://www.scriptics.com/support/bugForm.html
All bug reports and patch submissions are logged in a database. Source code patches that are made
according to the Tcl Engineering Manual guidelines, which include documentation and test cases, have
the most chance of adoption.These guidelines describe code appearance (e.g., indentation), test suite
requirements, and documentation requirements.
Note: the changes proposed in this chapter are no guarantee that these features will actually appear in
Tcl/Tk 8.3! These features are on the short list proposed by Jeff Hobbs, but the final contents of 8.3
will be a trade-off between features, effort, and schedule.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter54. Tcl/Tk 8.3
Proposed Tcl Changes
Octal Numbers
Invalid octal numbers (e.g., 08) have long been cause for confusion. At the least, the error message
will be improved to alert users that a leading zero implies an octal number, which cannot contain
digits eight or nine. We plan to introduce a tcl command that turns off octal number interpretation:
tcl useoctal boolean
At the same time, a new way to specify octal numbers will be added, probably \0oXXX.
We are considering a new way to specify decimal numbers, probably \0dXXX, that will force
interpretation of XXX as decimal, even if it has leading zeros.
file channels
The file channels command will return a list of open I/O channels, which can be sockets, regular
files, or channels created by extensions. It will take an optional pattern argument (e.g., sock*) to
constrain the list.
Changing File Modify Times
Either a new command, file touch, or a change to file attributes -mtime option will be made to
let you change the modification date on a file.
regsub -subst or -command
The combination of regsub and subst is so powerful, we want to make it perform even better. The -
subst option to regsub will run subst over the result of regsub, but only over the parts that were
generated by regsub. The input data that is not matched by regsub is untouched by the subsequent
subst. This can eliminate the need to quote special Tcl characters in input, which takes extra regsub
passes. The -command option passes the replacement string to a command before substitution into the
result. You can achieve similar effects with both -subst and -command, so we may only implement
one option.
A -start option will be added to regsub and regexp. This indicates a starting offset into the string
being matched.
New glob options
The -dir, -join, -path, and -types options may be added to glob to make it easier to manipulate
directories in a platform-independent manner.
Internationalization
There are still some improvements that can be made in Internationalization support, especially with
respect to input methods.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter54. Tcl/Tk 8.3
Proposed Tk Changes
The Img Patch
The Img patch adds an alpha channel, better transparency support, and improved GIF support,
including the ability to save GIF images. This patch also supports other image types (e.g., JPEG) that
can be loaded as extensions.
Canvas Improvements
The canvas implementation needs to be converted to use Tcl_Obj values, which will improve its
performance. Advanced tag searching may be added. This adds the ability to search for canvas items
based on boolean expressions of tag values. Dashed lines will be added, even though we can support
only single-pixel wide dashed lines on Windows 95. Windows NT, UNIX, and Macintosh can support
thick dashed lines. Canvas coordinates may be specified as a single list argument instead of individual
arguments, which makes it easier to construct commands. The "Visitors Patch" may be adopted, so
that extensions can add new canvas operations without patching Tk directly.
Pointer Warping
Tk applications will have the ability to move the mouse under program control. Use the wm warp
command:
wm warp x y
Hidden Text
The TkMan application uses hidden text to suppress text display. This works by adding a new tag
property.
has a nice set of development tools. You can get a demo license for the tools at:
http://www.scriptics.com/registration/welchbook.html
The exsource folder contains the examples from the book. These are automatically extracted from
Framemaker files. One thing to watch out for is that single quotes get extracted as \" . I have left these
as-is. The browser.tcl script lets you view and try out the examples.
The tclhttpd directory contains an unpacked version of the TclHttpd distribution. You should be able
to start the server by loading the bin/httpd.tcl script into wish or tclsh . This script starts a Web
server on part 8015.
The tea_sample directory contains a sample Tcl extension from the Tcl Extension Architecture. This
example illustrates how to create Tcl extensions on Windows and UNIX.
The download folder contains software downloaded from the Internet. Some of these have associated
.README files. Others are described by files in the index folder. These descriptions are derived from
the Tcl Resource Center found at:
http://www.scriptics.com/resource/
The index/index.html file provides a listing and description of the software in the download folder.
The CD_UTILS folder contains software you may find helpful, such as Winzip and a version of Tar for
Macintosh. I have also included the mkhybrid software used to create the CD image, as well as other
scripts used in the process.
The welch folder contains a picture of me and a copy of my PGP key. The key ID is 7EDF9C79 and its
fingerprint is:
94 D8 90 6A FD 9C AE 94 40 E3 C6 D6 B1 90 E0 03
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
Chapter55. About The CD-ROM
Technical Support
Prentice Hall does not offer technical support for this software. If there is a problem with the media,
however, you may obtain a replacement CD by emailing a description of the problem. Send your email
to:
disc_exchange@prenhall.com
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
( ) for arrays
-fill, pack option
-ipadx and -ipady pack options
-padx and -pady, pack options
-sticky, grid option
/bin/sh _to run a Tcl script
{ } for argument grouping
~ in file names
17362
MyExample
Example 14-12_ The message widget formats long lines of text.
19809
H2x
Tcl and Tk Versions
25346
H2
Event Loop Interface
27139
H2x
World Wide Web
3D Border
3D relief sampler
41514
MyExample
Example 41-6_ A canonical Tcl main program and Tcl_AppInit.
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
abbreviation of commands
AC_INIT
AC_OUTPUT
AC_SUBST
accepting socket connections
activate window event
activeBorderWidth, widget attribute
activeRelief, widget attribute
add elements to a list
addinput [See fileevent]
administration, TclHttpd
Advanced Regular Expressions 2nd
after, safe alias
after, Tcl command
aliases, command
aliases, introspection on
Allocate a Pixmap
Allocation, Memory
Alternation in regular expressions
anchor position, pack
Anchor Positions, Text
anchoring a regular expression
Anchoring text in a label or button
animation with images
animation with update 2nd 3rd
ANSI_ARGS
app-defaults file 2nd
append, Tcl command
AppleScript on Macintosh
applets
application activation
Application and User Resources
application deactivation
Application Direct URL
form handlers
specifying content type
drag object
embedded window
events coordinates
gridded geometry
Hello,World!
hit detection
image object
large scroll area
line object
min max scale
moving objects
object bindings
object support in C
objects with many points
oval object
polygon object
postscript
proposed improvements
rectangle object
resources for objects
rotation
scaling objects 2nd
scroll increment
scroll region 2nd
selection handler example
spline
stroke drawing example
Summary of Operations
tag on object 2nd
text object attributes
text object bindings
transparent text
Cap Styles, Line
capturing program output
Capturing Subpatterns
Cascaded Menus
case [See switch]
catalog files, managing message
catalog files, sample message
catalogs and namespaces, message
catalogs, message
catch, example 2nd
catch, possible return values
catch, Tcl command
Catching More Than Errors
Cavity Model, Pack
cd, Tcl command
CDE Border Width
Centering a Window
centimeters
cget, widget operation
CGI
Application, Guestbook
Argument Parsing
cgi.tcl package
definition of
Directories
example script
script library for
cgi.tcl CGI package
Cgi_Header
Cgi_Parse and Cgi_Value
Chained conditional with elseif
change directory
Changing Command Names with rename
changing widgets 2nd
Channel Drivers, I/O
channels, stacking
character code
Character Set Conversions
character set encoding
Character Set Encodings
Character Sets 2nd
characters from strings
checkbutton, Tk widget
choice preference item
Choosing items from a listbox
Choosing the Parent for Packing
Chopping File Pathnames
circle, canvas object
ckalloc and ckfree
class, event binding
class, of application
class, resource
class, widget
clicks per second
client side of remote evaluation
Client Sockets 2nd
ClientData
CLIPBOARD selection 2nd
clipboard, Tk command
clock widget
Clock widget data structure
clock, Tcl command
Clock_Init procedure
ClockCmd command procedure
ClockConfigure allocates resources for the widget
ClockDestroy cleanup procedure
ClockDisplay procedure
ClockEventProc handles window events
ClockInstanceCmd command procedure
ClockInstanceObjCmd command procedure
ClockObjCmd command procedure
ClockObjConfigure, Tcl_Obj version
ClockObjDelete command
close a window
close errors from pipes
close window callback
close, Tcl command
Closing I/O channels
code checker
Code Warrior compiler
code, procedure for callbacks
Codes from Command Procedures
Coding Style
Collating Elements
color
allocating in C
attributes
darker color
dialog to choose
name
of text
palettes
resource names
reverse video
RGB specification
Tk 4.0 new attributes
values
values, in C
colormap
for frame
in C
widget attribute
command
abbreviations
aliases for safe interpreters
aliases, saving as Tcl commands
body
build with list
buttons
C interface, String
C interface, Tcl_Obj
call with Tcl_Invoke
callbacks
complete Tcl command
creating and deleting
defined by a namespace, listing
evaluate from C
evaluation
example, blob
for entry widget
from C using Tcl_Eval
history 2nd
implement in C 2nd
lookup
on radiobutton or checkbutton
parsing in C
passing variable names
prefix callbacks
reading commands
substitution
syntax
that concatenates its arguments
that uses regular expressions
Command key, Macintosh
command line arguments
command procedure 2nd
and Data Objects
BlobCmd
call with Tcl_Invoke
RandomCmd
RandomObjCmd
Result Codes from
command substitution
command-line
argument
to Wish
arguments 2nd 3rd
arguments, in C
arguments, main programs and
parsing
comments
in line
in resource file
in switch
Communicating Processes
Comparing file modify times
comparing text indices
comparison function, sorting a list
Compatibility, regular expression patterns
Compile-Time Errors
compiler, byte code
compiler, Code Warrior
compiler, Microsoft Visual C++
compiler, TclPro
Compiling Tcl and Extensions
complex indices for arrays
Computing a darker color
concat and eval
concat and lists
concat, list, double quotes comparison
concat, Tcl command
concatenate strings and lists
conditional, if then else.
config/plugin.cfg file
configure and autoconf
configure flags, Standard
configure, widget operation
configure.in 2nd
configuring
attributes, in C 2nd
Read-Write Channels
Security Policies
widget attributes 2nd
window, in C
windows
Connect client to an eval server
connect, socket
connection state, TclHttpd
console, Tcl command 2nd
Constructing Code with the list Command
Constructing Lists
Constructing Procedures Dynamically
containers. [See frame.]
content type for Application Direct URL
Contexts, Graphics
continue in bindings
continue, Tcl command
Control Structure Commands
Conversions Between Encodings
Conversions, Character Set
Converting Between Arrays and Lists
Converting Existing Packages to Namespaces
Converting Numbers
coordinate space, canvas
coordinates of mouse event
coordinates, general
Coordinates, Window
copy and paste
Copy, virtual event
Copying Files
corner grips
Correct Quoting with eval
counting with regsub
Covering a window with place
create
commands in C
directories
elements in a hash table
file pathnames
hash tables in C
hierarchy of interpreters
interpreter in C
interpreter in scripts
interpreters
interpreters in C
loadable package
windows in C 2nd
creating
image types in C
cross-platform
cancel event
file naming
scripts
virtual events
curly braces
stripped off
vs. double quotes
current directory
cursor
in C
mouse
text insert
Custom Dialogs
Cut, virtual event
CVS repository for Tcl software
Cygwin tools
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Data Objects, C Command Procedures and
Data Structures with Arrays
Data Structures, AssocData for per Interpreter
data transformation
Data, Using Tcl_Preserve and Tcl_Release to Guard
data-driven user interface
database, ODBC
database, simple in-memory 2nd
Database, Using the Resource
DDE Extension
deactivate window event
debugger, TclPro
debugger, Tuba
Debugging
declaring variables
Decoding HTML Entities
default button
default parameter values
Defining New Binding Tags
delete
commands in C
files
interpreters in C
list element by value
text
desktop icons
destroy
no errors
Tk command
widget
window event
Destroying Hash Tables, Creating and
detached window
dialog
buiding with procedure
custom
data-driven approach
message for
simple example
to make choice
window for
dictionary [See array]
directory
current
Disabling the Library Facility
display list graphics
display space, with pack
Display, Fonts and Text
distribution, Tcl source code
DLL, loading into Tcl 2nd
DLLEXPORT
document root, TclHttpd
document type handler, TclHttpd
dollar sign
dollar sign syntax
Don Libes
DOS to UNIX
double click
Double quotes compared to concat and list
double quotes vs. curly braces
Doyle, Michael
dp_send, Tcl command
drag object on canvas
drag out a box
drag windows, bindings
drag-and-drop
Drivers, I/O Channel
DString interface 2nd
Dykstra, Dave
Dynamic HTML, CGI
dynamic linking 2nd
Dynamic Loading and Packages
Dynamic Strings, DString
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
e-mail application, exmh
Echo Service, example
EchoArgs
edit bindings for canvas text
editable text
Effective Tcl/Tk Programming, book
elementBorderWidth, widget attribute
else [See if]
elseif [See if]
Emacs-like binding for Meta and Escape
embed_args and plugin, variables
embedded applications
embedded images for a bulleted list
embedded images in text
embedded widgets in text
embedded window in frame
embedded window on canvas
Embedding Tk Windows
Empty Array Names
encoding
binary
character set, in C
Character Sets
Conversions Between
fconfigure option for character sets
I/O channels
Scripts in Different
system
Tcl command 2nd
End of File Character
End of Line Translations
end-of-file condition
entry
attributes
changes in Tk 4.1
command
indices
long strings in
read-only
tips for use
Tk 4.0 changes
variable for value
with label
Environment Variables
eof, Tcl command
Equal-sized labels
EqualSizedLabels
Equivalence Classes
errno
error
background, in Tcl
catching
errorCode, variable
errorInfo, variable 2nd
from return
handler for Tk
information in C
reporting script
Tcl command
X protocol
Escape key
eval
and double quotes
example 2nd
in aliases
server 2nd
Tcl command
wrapper procedure example
Evaluating Expressions from C
Evaluating Tcl Commands from C
event
bindings
bindings, in C
handler, in C
handler, resize in C
keywords
modifier
sequences
syntax, bindings
Tk command
types, binding
virtual
Event Loop
Event Loop Implementation
event loop in tclsh
Event Loop Interface
Event Loop Moves from Tk to Tcl
event sources
event, I/O handler
Event-Driven Programming
Events in C, Timer
Events in C, Window
Examining the File System
Example Browser
Example Plugins
Example, The blob Command
exception [See catch]
exec, limitations on Windows
exec, Tcl command
ExecLog
execute programs automatically
executing programs
Execution Environment
Exit Handlers
exit, command alias
exit, Tcl command
exmh, e-mail application
expand vs. fill, with pack
expand, more than one widget
Expanding Tilde in File Names
Expanding Variables in Resources
Expect
Expect, Tcl extension
Exploring Expect, book
Exporting and Importing Procedures
expr, Tcl command
Expressions and String Matching, Regular
Expressions, Evaluating
expressions, from C code
extended selection mode, listbox
Extension Architecture, Tcl
Extension, Compiling
Extension, The Sample
EXTERN
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
fblocked, Tcl command
fconfigure, changes in Tcl 8.0
fconfigure, changes in Tcl 8.1
fconfigure, changes in Tk 4.1
fconfigure, File Encodings and
fconfigure, Tcl command
fcopy, HttpCopyDone examle
fcopy, Tcl command
feedback, to user
file
attributes
channel names
compare modify times
current script
dialogs
encodings and fconfigure
end of line character
end-of-file
equality test
find by name
for preferences
I/O and Binary Data
limited access with safe interp
name manipulation
name patterns
of widget attributes
open for I/O
operations added in Tcl 7.6
partial re-write
Tcl command
tilde in names
File Handlers
File Name Manipulation in C
File System, Examining the
File_Process
fileevent, Tcl command 2nd
grid
internal padding
grid
minimum size
multiple widgets in a cell
resize behavior
row and column constraints
row and column padding
row and column span 2nd
row and column specifications
sticky settings 2nd
text widget and scrollbar
Tk command 2nd
gridded geometry
gridded geometry for a canvas
geometry:gridding, canvas
gridded geometry, in C
gridded, resizable listbox
gridding, listbox
grips on corners
grouping rules
guard data with, Tcl_Preserve and Tcl_Release
Guestbook CGI Application
guestbook.cgi script
guestbook.cgi, output 2nd
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Handlers, Exit
Handlers, File
Handling Graphic Protocol Errors
Handling Window Events
Harrison, Mark 2nd
hash table
hash table package, in C
Hash Table, Creating and Removing Elements
Hash Tables, Creating and Destroying
hbox, window layout
HEAD, HTTP
Header Files, Standard
Hello, World! Tk program
Hello, World!, canvas example
Help menu
help pop-up
Hidden Commands
Hidden Text
high resolution timer
History Syntax, Tcl
History, command
history, Tcl command
horizontal and vertical layout, nested
horizontal fill in a menu bar
horizontal window layout
hostname
Hot Tip
abbreviate history commands
args and eval
array elements on demand
array for module data
attribute resource names
Big lists can be slow
bind callback is in global scope
borders, padding, and highlight
braces get stripped off
Initialization, Tcl
Initializing the Extension
input data into Tcl lists
input focus 2nd 3rd
input focus highlight
Input/Output
Binary Data
channels, stacking
configuration
events, in C
from C
from Safe Interpreters
redirection
Input/Output Command Summary
Inserting and Deleting Text
inspector, tkinspect
Installation Directory Structure
Installation, Tcl
integrating TclHttpd with your application
interactive command entry
Interactive Command History
Interface, DString
Interface, String Command
Interface, Tcl_Obj Command
internal padding, -ipady
Internationalization 2nd 3rd 4th
Internet Explorer
interp, Tcl Command
interpreter
and namespaces
creating
creating in C
data structures, AssocData
hierarchy of
internal state
name as a command
tracing commands in C
interprocess communication. [See send.]
intranet applications, Tcl plug-in
Introduction to HTML
Introspection
introspection, with pack
Invoking Scripts from C
italic text 2nd
itcl, object-oriented Tcl
Ivler, John 2nd
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Java
Javascript
Javascript, access from Tcl plug-in
Johnson, Eric
join in Tcl, Implementing
Join Styles, Line
join, Tcl command
JPEG
justification in text widget
justification of text
in C
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Keeping References to Tcl_Obj Values
keyboard
Bindings and Focus
events
focus traversal
grab
Key events
map
selection, menu
shortcuts and focus
Kupries, Andreas
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
label
and an entry
arranging
attributes
displaying different strings
lining up 2nd
multiline
size
Tk widget
width and wrap length
lappend, Tcl command
lassign, list assignment with foreach.
ldelete, Tcl procedure
Libes, Don 2nd 3rd
library
and imported commands, loading
Based on the tclIndex File
directory, Tcl
how it works
index
introduction
of procedures
regular expression C
search path
shared
Tcl C 2nd
Tk C
using stubs
Limitations of exec on Windows
Limited Socket Access
Limited Temporary Files
lindex, Tcl command
Line Cap Styles
Line Join Styles
line on canvas
line segment performance
mapping windows
MapWindow
mark gravity, text
mark position in text 2nd
Marshall Rose
Matching Characters
Matching File Names with glob
Matching Precedence
Matching, Regular Expressions and String
math expressions
math functions, built in
trig functions
McLennan, Michael 2nd
Measurements, Screen
Memory Allocation
menu
accelerator
accelerator linked to bindings
apple
attributes for widget
button for
by name package
cascade
cascaded menu helper procedure
define with resource database
entries via resources
example of different types
example, screen shot
help
keyboard selection
menubar
menubuttons
multicolumn palette
operations on entries
packing a menubar
pop-up
system
Tk widget
user defined
menubutton, Tk widget
MenuGet maps from name to menu
message
arranging text
formats long lines of text
text layout
Tk widget
Message Box
message catalogs
and namespaces
example
managing
Meta key
Michael McLennan
Microsoft VC++ compiler
millimeters
MIME type
min max scale, example
minimize, window operation
missing font
Modifiers, Event
Modifying Lists
Modifying Tcl_Obj Values
module data
Module Prefix for Procedure Names
module support
mouse
cursor
cursor on Windows
event coordinates
events
Mouse Cursors
MouseWheel event
msgcat package 2nd
msgcat\
\
mcunknown
Multicolumn Palette Menus
multiline labels
Multiple foreach loop variables
Multiple Interpreters
Multiple Interpreters and Safe-Tcl
Multiple Interpreters andSafe-Tcl
Multiple loop variables with foreach
multiple return values
multiple selection mode, listbox
Multiple value lists with foreach
multiway branch, switch
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
name
giving out yours
manipulating file
of color
of I/O channels
of interpreter, in C
of interpreter, send 2nd 3rd
of procedure
of variable
of widgets
qualified namespace
quirks in namespaces
Named Fonts
namespace
and Callbacks
and uplevel
and upvar
and Widgets, Images, and Interpreters
Converting Packages to use
listing commands
message catalogs
Nested
Using
Variables
Namespaces
Native Buttons and Scrollbars
Native Look and Feel
Native Menus and Menubars
nested frames
Nested Namespaces
Netscape Navigator
Network programming
network server
Network Sockets
New Image Types, Creating
newguest.cgi script
newguest Form
Newline Sensitive Matching
Newline Translations
Nichols, David
Nijtmans, Jan
nonblocking I/O
nongreedy quantifiers
Numbers, Converting
numeric value, widget for
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Object Support, Canvas
Object Types, Primitive
Object Types, String
object-oriented, incr Tcl
Objects with Many Points
Objects, C Command Procedures and Data
Objects, Tcl
Octal Numbers
ODBC
on-line manual
open
a window
a window, binding
catching errors from
file for an unsafe interpreter
file for writing
files for I/O
Process Pipeline
Tcl command
Option Database. [See resource.]
Option Menus
Option Processing
option, Tk command
optional scrollbars
Oracle
OraTcl
Order, Window Stacking
Ousterhout, John 2nd
oval, canvas object
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
pack
binary data
display space
expand vs. fill
into other relatives
nested frames
order of children
packing order 2nd
Packing Space and Display Space
padding
padding vs. button padding
resizing windows
scrollbars first
space for
Tk 4.0 changes
Tk command
unexpected results
package
convert to namespaces
creating a loadable
dynamic loading
implemented in C code
index
index file
initialization in C
Tcl command 2nd
version numbers
padding
and anchors
around widgets
button vs. packer
in buttons
provided by labels and buttons
widget
palette menus
Pane Manager
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
radiobutton, Tk widget
Raines, Paul
raise an error
raise, Tk command
Raising an error with return
Random Access I/O
random access I/O
random number example
Random number example using namespaces 2nd
random, in C
RandomCmd C command procedure
RandomObjCmd C command procedure
read
commands from a socket
file line by line
I/O in background
option database file
Tcl command
Tcl commands from a file 2nd
read-only entry widget
read-only text widget
read-only variables
readline [See gets]
realloc
records, with arrays
rectangle on canvas
redefining procedures
redisplay. [See update.]
redo [See history]
Reference Counts, Managing Tcl_Obj
References to Tcl_Obj Values, Keeping
Referencing an array indirectly
Reflection and Debugging
regexp, Tcl command
register image format, in C
regsub -subst or -command
multiple
relative coordinates
Script Errors, Reporting
script library
initialization
setting with TCL_LIBRARY
using
script, current
Scriptics FTP site
Scripts and the Library
Scripts from C, Invoking 2nd
Scripts in Different Encodings
Scroll_Set manages optional scrollbars
scrollbar
attributes
automatic hiding
bindings
example 2nd
for canvas
for listbox
for two widgets
operations
protocol with widgets
protocol, Tk 3.6
set size
widgets on a canvas
with text
ScrollFixup
search path, library 2nd
search text widget
searching arrays
Searching Lists
Searching through Files
secure hash algorithm, sha1
Secure sockets
security policies
and Browser Plugin
configuration 2nd
creating
feature sets
Safesock
Tempfile
seek, Tcl command
select [See fileevent]
selection
and clipboard in C
canvas example 2nd
CLIPBOARD
exporting to X
handler example
model
ownership
PRIMARY
text widget 2nd
Tk command
self-checking form
send
application name in C
command to another application
constructing command reliably
name of interpreter 2nd
timeout changed in Tk 4.0
Tk command
X authority required
sender application
Serial Line I/O 2nd
serial ports
Server Socket Options
Server Sockets
session, window system
Set Conversions, Character
set, Tcl command 2nd
setgrid, widget attribute
sha1, secure hash algorithm
shared libraries 2nd 3rd
Shared Tcl_Obj Values, Pitfalls of
Shen, Sam
Shrinking Frames and pack propagate
signal handling, in C
Signals, Working with
significant digits
Simple Records
single selection mode, listbox
SiteFooter
SiteMenu
SitePage
size of label
size, relative to widget
sleep [See after]
sleep, Tcl_Sleep
Smith, Chad
socket
accepting connections
added in Tcl 7.5
client side
limited with safe interp
listen
read Tcl commands from
server example
Tcl Command
sort
sorting lists
source
example
in safe interpreters
loading code into TclHttpd
Tcl command 2nd
source code
compiling Tcl from
distribution, Tcl
space around widgets
spaces in array indices
spacing, widgets
Specifying a Locale
Spencer, Henry
splice lists together
spline curve on canvas
split data into Tcl lists
split, Tcl command
square brackets
SSL channel plugin
stack trace 2nd
stack, data structure
stack, example
stacking I/O channels
stacking order, window 2nd 3rd
standalone executable
standalone Tcl script
Standard Configure Flags
Standard Dialogs 2nd
Standard Directory Structure
Standard Header Files
standard options
static code checker
Status, Tcl procedure
stderr, standard error output
stdin, standard input
stdout, standard output
Sticky Geometry Settings
stop bits, serial interface
Stop Procedure
strftime
string
characters from
command interface in C
comparison 2nd
comparison, using expr
concatenate
display. [See label. ]
dynamic (DString) in C
effect of backslash-newline
expressions
identifiers (UIDS) in C
indices
internationalization in C
mapping to new strings
matching
matching, regular expressions in C
object types in C
processing with subst
result, managing in C 2nd
Tcl command
string command, changes in Tcl 8.1
stroke, canvas example
structures, with arrays
Stub Libraries, Using
Stump, John
Style, Relief
Styles, Line Cap
Styles, Line Join
Styles, Text Justification
subst, Tcl command
subst, template example
substitution rules
substitution, before grouping
Substitutions and hidden commands
substitutions, no eval
Summary of Package Loading
Summary of the Tk Commands
Sun Microsystems
SWIG
switch
example
on exact strings
substitutions in patterns
Tcl command
with fallthrough cases
Sybase
SybTcl
syntax
arrays
command
curly braces
dollar sign
list 2nd
square brackets
TclPro Checker
System Encoding
system font
System Menus
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Tab key
Tab Stops
tab, default binding
Table, Hash 2nd
tag
canvas widget 2nd
HTML
text widget 2nd
attributes
bndings
taskbar
TChar
Tcl 7.5
Tcl 7.6 2nd
Tcl 8.0 Patch Releases
Tcl and Extensions, Compiling
Tcl and the Tk Toolkit, book
Tcl and Tk sources, on the web
Tcl C Library 2nd
Tcl command
after, timer events
append, strings
array, data type
bgerror, error handler 2nd
binary, convert between string and binary
break, exit loop
catch, error handler
cd, change directory
close, I/O channel
concat, concatenate strings and lists 2nd
console, Windows and Macintosh 2nd
continue, loop
encoding, character sets
eof, test end of file
error, raise error
eval, evaluate a string
print by name
PrintByName
read-only
scope and procedures
Tcl command
tcl_library 2nd
tcl_pkgPath
tcl_platform
tcl_precision
test if defined
trace access
Variable number of arguments
vbox, vertical layout
version number, Tcl
Version Numbers
versions, of packages 2nd
vertical layout
Virtual Events
virtual events for cut, copy, and paste
virtual root window
visual
class, widget attribute
volume, of bell
vwait, Tcl command
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
wait for Tk event
Web Programming with Tcl and Perl, book
Web server, TclHttpd
Web Tcl Complete, book
When to Use Regular Expressions
while loop to read input
while, Tcl command
white space
widget
attributes
attributes, in C
C data structure
class command, in C
class definition
cleanup in C
container [See frame]
data structure, in C
definition
destroy
destroy, in C
display in C
embed in text
frame for container
hide by unmapping
image, from C
implemented in C
instance command, in C
introduction
naming
reconfigure
spacing between
Tcl_Obj version
unmapping
widgets
and namespaces
button
checkbutton
frame
label
listbox
menu
menubutton
message
radiobutton
scale
text
toplevel
window
activate
binding on open
changes size
close callback
configuration in C
coordinates, in C
creating in C
deleting
detached
embedded in canvas
embedding
events, in C 2nd
family relationships
general information in C
hierarchy
icon bitmap
layout in binding
manager 2nd
maximized
minimize
resize, interactive
rooms
session state
stacking order
stacking order, in C
styles
taskbar
title 2nd
virtual root
window:gridding
geometry:gridding, canvas
Windows
and exec problems
auto_path
com ports
DLL location
look and feel
mouse cursor
Plugin Configuration
shared libraries
Start and Menu Keys
Start Menu
system font size
system menu
text mode files
winfo, Tk command
wish, application
wm, Tk command
WM_DELETE_WINDOW
Working with Signals
World Wide Web
Writing a Tk Widget in C
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
X authority, send
X Font Names
X ID for resource, in C
X protocol errors
X resource database. [See resource. ]
X Resource ID Management
XCopyArea
Xdefaults. [See resource. ]
Xerox PARC
xfontsel, UNIX program
xlsfonts, UNIX program
xmodmap, program
xrdb [See resource]
xset program
XSynchronize
xview and yview scrollbar perations
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Year 2000 Compliance
Top
Practical Programming in Tcl & Tk, Third Edition
By BrentB.Welch
Table of Contents
[SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]
Zimmer, Adrian
zoom
zoom box style
Top