Table of Contents
List of Examples xix
List of Tables xxv
Preface xxix
1. Tcl Fundamentals 1
Getting Started 1
Tcl Commands 3
Hello World 3
Variables 4
Command Substitution 4
Math Expressions 5
Backslash Substitution 6
Grouping with Braces and Double Quotes 7
Procedures 8
A While Loop Example 9
Grouping and Command Substitution 10
More About Variable Substitution 11
Comments 12
Command Line Arguments 12
Substitution and Grouping Summary 13
Fine Points 14
Reference 14
Backslash Sequences 14
Predefined Variables 15
Arithmetic Operators 15
Built-in Math Functions 16
Core Tcl Commands 17
2. Strings and Pattern Matching 19
The string Command 19
Strings and Expresssions 20
The append Command 21
The format Command 21
The scan Command 23
String Matching 24
Regular Expressions 25
The regexp Command 27
The regsub Command 28
3. Tcl Data Structures 29
More About Variables 29
The unset Command 30
Using info to Find Out About Variables 30
Tcl Lists 31
Constructing Lists: list, lappend, and concat 32
Getting List Elements: llength, lindex, and lrange 33
Modifying Lists: linsert and lreplace 34
Searching Lists: lsearch 34
Sorting Lists: lsort 35
The split and join Commands 35
Arrays 36
The array Command 37
4. Control Flow Commands 39
If Then Else 40
Switch 41
Comments in switch Commands 42
Foreach 43
While 44
For 44
Break and Continue 45
Catch 45
Error 46
Return 47
5. Procedures and Scope 49
The proc Command 49
Changing Command Names With rename 50
Scope 51
The global Command 52
Use Arrays for Global State 53
Call by Name Using upvar 53
Passing Arrays by Name 54
6. Eval 57
Construct Commands with list 57
Exploiting the concat Inside eval 59
Double-quotes and eval 60
The uplevel Command 60
Commands that Concatenate Their Arguments 61
The subst Command 62
7. Working with UNIX 63
Running UNIX Programs with exec 63
The auto_noexec Variable 65
Looking at the File System 65
Input/Output Command Summary 68
Opening Files for I/O 68
Opening a Process Pipeline 70
Reading and Writing 70
The puts and gets Commands 70
The read Command 71
Random Access I/O 72
Closing I/O streams 72
The Current Directory - cd and pwd 72
Matching File Names with glob 72
Expanding Tilde in File Names 73
The exit and pid Commands 73
Environment Variables 74
8. Reflection and Debugging 75
The info Command 75
Variables 76
Procedures 77
The Call Stack 77
Command Evaluation 78
Scripts and the Library 78
Version Numbers 78
Tracing Variable Values 79
Interactive Command History 80
History Syntax 81
A Comparison to /bin/csh History Syntax 82
Debugging 83
Don Libes' Debugger 84
Breakpoints by Pattern Matching 85
Deleting Break Points 86
Debugging Tk Scripts 86
The tkinspect Program 86
The tkerror Command 86
Performance Tuning 87
9. Script Libraries 89
The unknown Command 89
The tclIndex File 90
Using a Library: auto_path 91
Disabling the Library Facility: auto_noload 91
How Auto Loading Works 91
Dynamic Linking C Code 92
Interactive Conveniences 92
Auto Execute 92
History 93
Abbreviations 93
Tcl Shell Library Environment 93
Coding Style 94
A Module Prefix for Procedure Names 94
A Global Array for State Variables 94
10. Tk Fundamentals 95
Hello World In Tk 96
Naming Tk Widgets 98
Configuring Tk Widgets 98
Tk Widget Attributes and X Resources 99
The Tk Manual Pages 99
Summary Of The Tk Commands 100
11. Tk by Example 103
ExecLog 103
Window Title 105
A Frame for Buttons 105
Command Buttons 106
A Label and an Entry 106
Key Bindings and Focus 106
A Resizable Text and Scrollbar 107
The Run Procedure 107
The Log Procedure 108
The Stop Procedure 108
The Example Browser 109
More About Resizing Windows 110
Managing Global State 111
Searching Through Files 111
Cascaded Menus 112
A Read-Only Text Widget 112
A Tcl Shell 113
Naming Issues 114
Text Marks and Bindings 114
12. The Pack Geometry Manager 115
Packing Toward a Side 116
Shrinking Frames and Pack Propagate 116
Horizontal and Vertical Stacking 117
The Cavity Model 118
Packing Space and Display Space 119
The -fill Option 119
Internal Padding with -ipadx and -ipady 120
External Padding with -padx and -pady 122
Expand and Resizing 122
Anchoring 124
Packing Order 125
Pack Slaves and Pack Info 126
Pack the Scrollbar First 126
Choosing the Parent for Packing 127
Unpacking a Widget 127
Packer Summary 128
The pack Command 128
The Place Geometry Manager 129
The place Command 130
Window Stacking Order 131
13. Binding Commands to X Events 133
The bind Command 133
The bindtags Command 134
Using break and continue in Bindings 136
Defining New Binding Tags 136
Binding Precedence in Tk 3.6 136
Event Syntax 137
Keyboard Events 138
Detecting Modifiers in Tk 3.6 138
Mouse Events 139
Other Events 139
Modifiers 140
Modifiers in Tk 3.6 142
Event Sequences 142
Event Keywords 143
14. Buttons and Menus 145
Button Commands and Scope Issues 145
Buttons Associated with Tcl Variables 149
Button Attributes 151
Button Operations 153
Menus and Menubuttons 153
Pop-up Menus 155
Option Menus 155
Keyboard Traversal 155
Manipulating Menus and Menu Entries 156
Menu Attributes 157
A Menu by Name Package 158
15. Using X Resources 163
An Introduction to X Resources 163
Warning: Order is Important! 165
Loading Option Database Files 165
Adding Individual Database Entries 166
Accessing the Database 166
User-Defined Buttons 167
User Defined Menus 168
16. Simple Tk Widgets 171
Frames and Toplevel Windows 171
Attributes for Frames and Toplevels 172
The Label Widget 173
Label Attributes 174
Label Width and Wrap Length 174
The Message Widget 175
Message Attributes 176
Arranging Labels and Messages 177
The Scale Widget 177
Scale Bindings 178
Scale Attributes 178
Programming Scales 179
The Scrollbar Widget 180
Scrollbar Bindings 182
Scrollbar Attributes 182
Programming Scrollbars 183
The Tk 3.6 protocol 184
The bell Command 184
17. Entry and Listbox Widgets 185
The Entry Widget 185
A Labeled Entry 185
Entry Attributes 188
Programming Entry Widgets 189
The Listbox Widget 191
Programming Listboxes 193
Listbox Bindings 197
Browse Select Mode 198
Single Select Mode 198
Extended Select Mode 199
Multiple Select Mode 200
Scroll Bindings 200
Listbox Attributes 201
Geometry Gridding 202
18. Focus, Grabs, and Dialogs 203
Input Focus 203
The focus Command 204
Focus Follows Mouse 204
Click to Type 205
Hybrid Models 205
Grabbing the Focus 205
Dialogs 206
The tkwait Command 206
Prompter Dialog 206
Keyboard Shortcuts and Focus 208
Destroying Widgets 208
Animation with the update Command 208
File Selection Dialog 209
Specifying Attributes with X Resources 212
Mouse and Key Bindings 212
Listing the Directory 214
Accepting a Name 215
Easy Stuff 217
File Name Completion 217
19. The Text Widget 219
Text Indices 219
Inserting and Deleting Text 220
Index Arithmatic 220
Text Marks 221
Text Tags 222
Tag Attributes 223
Mixing Attributes from Different Tags 224
Line Spacing and Justification 225
The Selection 227
Tag Bindings 227
Embedded Widgets 228
Text Bindings 230
Text Operations 232
Text Attributes 234
20. The Canvas Widget 235
Canvas Coordinates 235
Hello, World! 236
The Min Max Scale Example 238
Arc Items 241
Bitmap Items 242
Image Items 243
Line Items 244
Oval Items 246
Polygon Items 247
Rectangle Items 248
Text Items 249
Window Items 252
Canvas Operations 254
Generating Postscript 256
Canvas Attributes 258
Hints 259
Screen Coordinates vs. Canvas Coordinates 259
Large Coordinate Spaces 259
Scaling and Rotation 259
X Resources 260
Objects with Many Points 260
Selecting Canvas Items 260
21. Selections and the Clipboard 261
The selection Command 262
The clipboard Command 263
Interoperation with OpenLook 263
Selection Handlers 263
A Canvas Selection Handler 264
22. Callbacks and Handlers 267
The after Command 267
The fileevent Command 268
The send Command 269
Send and X Authority 270
The Sender Script 271
Using Sender 272
Communicating Processes 275
23. Tk Widget Attributes 277
Configuring Attributes 277
Size 278
Borders and Relief 280
The Focus Highlight 281
Padding and Anchors 281
Putting it all Together 282
24. Color, Images, and Cursors 285
Colors 286
Colormaps and Visuals 287
Bitmaps and Images 289
The image Command 289
Bitmap Images 290
The bitmap Attribute 290
Photo Images 291
The Mouse Cursor 294
The Text Insert Cursor 295
25. Fonts and Text Attributes 297
Fonts 297
Text Layout 300
Gridding, Resizing, and Geometry 301
Selection Attributes 301
A Font Selection Application 302
Setup and Widget Layout 302
Tracing Variables 304
Listing Available Fonts 304
Keeping Track of Fonts 305
Creating the Menus 306
The Font Sampler Display 306
Selecting a Font 307
26. Window Managers and Window Information 309
The wm Command 309
Size, Placement, and Decoration 310
Icons 311
Session State 312
Miscellaneous 313
The winfo Command 314
Sending Commands Between Applications 314
Family Relationships 315
Size 315
Location 316
Virtual Root Window 317
Atoms and IDs 317
Colormaps and Visuals 318
The tk Command 319
27. A User Interface to Bindings 321
A Pair of Listboxes Working Together 323
The Editing Interface 325
Saving and Loading Bindings 326
28. Managing User Preferences 329
App-Defaults Files 329
Defining Preferences 331
The Preferences User Interface 333
Managing the Preferences File 336
Tracing Changes to Preference Variables 337
Improving the Package 338
29. C Programming and Tcl 339
Using the Tcl C Library 340
Application Structure 340
Tcl_Main and Tcl_AppInit 341
The Standard Main in Tcl 7.3 342
A C Command Procedure 343
Managing the Result's Storage 344
Invoking Scripts From C 345
Bypassing Tcl_Eval 345
Putting A Tcl Program Together 347
An Overview of the Tcl C library 347
Application Initialization 348
Creating and Deleting Interpreters 348
Creating and Deleting Commands 348
Managing the Result String 348
Lists and Command Parsing 348
Command Pipelines 349
Tracing the Actions of the Tcl Interpreter 349
Evaluating Tcl Commands 349
Manipulating Tcl Variables 349
Evaluating Expressions 350
Converting Numbers 350
Hash Tables 350
Dynamic Strings 352
Regular Expressions and String Matching 352
Tilde Substitution 352
Working with Signals 352
30. C Programming and Tk 353
Tk_Main and Tcl_AppInit 353
A Custom Main Program 355
A Custom Event Loop 358
An Overview of the Tk C library 359
Parsing Command Line Arguments 359
The Standard Application Setup 360
Creating Windows 360
Application Name for Send 360
Configuring Windows 360
Window Coordinates 360
Window Stacking Order 361
Window Information 361
Configuring Widget Attributes 361
Safe Handling of the Widget Data Structure 361
The Selection and Clipboard 361
Event Bindings 362
Event Loop Interface 362
Handling X Events 362
File Handlers 362
Timer Events 363
Idle Callbacks 363
Sleeping 363
Reporting Script Errors 363
Handling X Protocol Errors 363
Using the X Resource Database 363
Managing Bitmaps 364
Creating New Image Types 364
Using an Image in a Widget 364
Photo Image Types 364
Canvas Object Support 364
Geometry Management 365
String Identifiers (UIDS) 365
Colors and Colormaps 365
3D Borders 366
Mouse Cursors 366
Font Structures 366
Graphics Contexts 366
Allocate a Pixmap 366
Screen Measurements 366
Relief Style 367
Text Anchor Positions 367
Line Cap Styles 367
Line Join Styles 367
Text Justification Styles 367
Atoms 367
X Resource ID Management 367
31. Writing a Tk Widget in C 369
The Widget Data Structure 369
Specifying Widget Attributes 370
The Widget Class Command 373
The Widget Instance Command 374
Configuring and Reconfiguring Attributes 376
Displaying the Clock 378
The Window Event Procedure 381
Final Cleanup 382
32. Tcl Extension Packages 385
Extended Tcl 386
Adding TclX to Your Application 386
More UNIX System Calls 387
File Operations 387
New Loop Constructs 387
Command Line Additions 387
Debugging and Development Support 387
TCP/IP Access 388
File Scanning 388
Math Functions as Commands 388
List Operations 388
Keyed List Data Structure 389
String Utilities 389
XPG/3 Message Catalog 389
Memory Debugging 389
Expect: Controlling Interactive Programs 390
The Core Expect Commands 390
Pattern Matching 391
Important Variables 391
An Example expect Script 392
Debugging expect Scripts 393
Expect's Tcl Debugger 394
The Dbg C Interface 394
Handling SIGINT 395
BLT 396
Drag and Drop 397
Hypertext 397
Graphs 397
Table Geometry Manager 397
Bitmap Support 397
Background Exec 397
Busy Window 398
Tracing Tcl Commands 398
The Cutbuffer 398
Tcl-DP 398
Remote Procedure Call 399
Connection Setup 399
Sending Network Data 399
Using UDP 400
Event Processing 400
Replicated Objects 400
The [incr Tcl] Object System 401
Tcl_AppInit with Extensions 402
Other Extensions 405
33. Porting to Tk 4.0 407
wish 407
Obsolete Features 407
The cget Operation 408
Input Focus Highlight 408
Bindings 408
Scrollbar Interface 409
Pack info 409
Focus 409
Send 410
Internal Button Padding 410
Radio Button Value 410
Entry Widget 410
Menus 411
Listboxes 411
No geometry Attribute 411
Text Widget 412
Color Attributes 412
Canvas scrollincrement 412
The Selection 413
The bell Command 413