1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1993-1998 Lucent Technologies, Inc.
|
3 |
|
|
'\"
|
4 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
5 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
6 |
|
|
'\"
|
7 |
|
|
'\" RCS: $Id: itcl.n,v 1.1.1.1 2002-01-16 10:24:46 markom Exp $
|
8 |
|
|
'\"
|
9 |
|
|
.so man.macros
|
10 |
|
|
.TH itcl n 3.0 itcl "[incr\ Tcl]"
|
11 |
|
|
.BS
|
12 |
|
|
'\" Note: do not modify the .SH NAME line immediately below!
|
13 |
|
|
.SH NAME
|
14 |
|
|
itcl \- object-oriented extensions to Tcl
|
15 |
|
|
.BE
|
16 |
|
|
|
17 |
|
|
.SH DESCRIPTION
|
18 |
|
|
.PP
|
19 |
|
|
\fB[incr\ Tcl]\fR provides object-oriented extensions to Tcl, much as
|
20 |
|
|
C++ provides object-oriented extensions to C. The emphasis of this
|
21 |
|
|
work, however, is not to create a whiz-bang object-oriented
|
22 |
|
|
programming environment. Rather, it is to support more structured
|
23 |
|
|
programming practices in Tcl without changing the flavor of the language.
|
24 |
|
|
More than anything else, \fB[incr\ Tcl]\fR provides a means of
|
25 |
|
|
encapsulating related procedures together with their shared data
|
26 |
|
|
in a namespace that is hidden from the outside world.
|
27 |
|
|
It encourages better programming by promoting the object-oriented
|
28 |
|
|
"library" mindset. It also allows for code re-use through inheritance.
|
29 |
|
|
|
30 |
|
|
.SH CLASSES
|
31 |
|
|
.PP
|
32 |
|
|
The fundamental construct in \fB[incr\ Tcl]\fR is the class definition.
|
33 |
|
|
Each class acts as a template for actual objects that can be created.
|
34 |
|
|
Each object has its own unique bundle of data, which contains instances
|
35 |
|
|
of the "variables" defined in the class. Special procedures called
|
36 |
|
|
"methods" are used to manipulate individual objects. Methods are just
|
37 |
|
|
like the operations that are used to manipulate Tk widgets. The
|
38 |
|
|
"\fBbutton\fR" widget, for example, has methods such as "flash" and
|
39 |
|
|
"invoke" that cause a particular button to blink and invoke its command.
|
40 |
|
|
.PP
|
41 |
|
|
Within the body of a method, the "variables" defined in the class
|
42 |
|
|
are automatically available. They need not be declared with anything
|
43 |
|
|
like the \fBglobal\fR command. Within another class method, a method
|
44 |
|
|
can be invoked like any other command\-simply by using its name.
|
45 |
|
|
From any other context, the method name must be prefaced by an object
|
46 |
|
|
name, which provides a context for the data that the method can access.
|
47 |
|
|
.PP
|
48 |
|
|
Each class has its own namespace containing things that are common
|
49 |
|
|
to all objects which belong to the class. For example, "common" data
|
50 |
|
|
members are shared by all objects in the class. They are global
|
51 |
|
|
variables that exist in the class namespace, but since they are
|
52 |
|
|
included in the class definition, they need not be declared using
|
53 |
|
|
the \fBglobal\fR command; they are automatically available to any
|
54 |
|
|
code executing in the class context. A class can also create
|
55 |
|
|
ordinary global variables, but these must be declared using the
|
56 |
|
|
\fBglobal\fR command each time they are used.
|
57 |
|
|
.PP
|
58 |
|
|
Classes can also have ordinary procedures declared as "procs".
|
59 |
|
|
Within another class method or proc, a proc can be invoked like
|
60 |
|
|
any other command\-simply by using its name. From any other context,
|
61 |
|
|
the procedure name should be qualified with the class namespace
|
62 |
|
|
like "\fIclassName\fB::\fIproc\fR". Class procs execute in the
|
63 |
|
|
class context, and therefore have automatic access to all "common"
|
64 |
|
|
data members. However, they cannot access object-specific "variables",
|
65 |
|
|
since they are invoked without reference to any specific object.
|
66 |
|
|
They are usually used to perform generic operations which affect
|
67 |
|
|
all objects belonging to the class.
|
68 |
|
|
.PP
|
69 |
|
|
Each of the elements in a class can be declared "public", "protected"
|
70 |
|
|
or "private". Public elements can be accessed by the class, by
|
71 |
|
|
derived classes (other classes that inherit this class), and by
|
72 |
|
|
external clients that use the class. Protected elements can be
|
73 |
|
|
accessed by the class, and by derived classes. Private elements
|
74 |
|
|
are only accessible in the class where they are defined.
|
75 |
|
|
.PP
|
76 |
|
|
The "public" elements within a class define its interface to the
|
77 |
|
|
external world. Public methods define the operations that can
|
78 |
|
|
be used to manipulate an object. Public variables are recognized
|
79 |
|
|
as configuration options by the "configure" and "cget" methods
|
80 |
|
|
that are built into each class. The public interface says
|
81 |
|
|
\fIwhat\fR an object will do but not \fIhow\fR it will do it.
|
82 |
|
|
Protected and private members, along with the bodies of class
|
83 |
|
|
methods and procs, provide the implementation details. Insulating
|
84 |
|
|
the application developer from these details leaves the class designer
|
85 |
|
|
free to change them at any time, without warning, and without affecting
|
86 |
|
|
programs that rely on the class. It is precisely this encapsulation
|
87 |
|
|
that makes object-oriented programs easier to understand and maintain.
|
88 |
|
|
.PP
|
89 |
|
|
The fact that \fB[incr\ Tcl]\fR objects look like Tk widgets is
|
90 |
|
|
no accident. \fB[incr\ Tcl]\fR was designed this way, to blend
|
91 |
|
|
naturally into a Tcl/Tk application. But \fB[incr\ Tcl]\fR
|
92 |
|
|
extends the Tk paradigm from being merely object-based to being
|
93 |
|
|
fully object-oriented. An object-oriented system supports
|
94 |
|
|
inheritance, allowing classes to share common behaviors by
|
95 |
|
|
inheriting them from an ancestor or base class. Having a base
|
96 |
|
|
class as a common abstraction allows a programmer to treat
|
97 |
|
|
related classes in a similar manner. For example, a toaster
|
98 |
|
|
and a blender perform different (specialized) functions, but
|
99 |
|
|
both share the abstraction of being appliances. By abstracting
|
100 |
|
|
common behaviors into a base class, code can be \fIshared\fR rather
|
101 |
|
|
than \fIcopied\fR. The resulting application is easier to
|
102 |
|
|
understand and maintain, and derived classes (e.g., specialized
|
103 |
|
|
appliances) can be added or removed more easily.
|
104 |
|
|
.PP
|
105 |
|
|
This description was merely a brief overview of object-oriented
|
106 |
|
|
programming and \fB[incr\ Tcl]\fR. A more tutorial introduction is
|
107 |
|
|
presented in the paper included with this distribution. See the
|
108 |
|
|
\fBclass\fR command for more details on creating and using classes.
|
109 |
|
|
|
110 |
|
|
.SH NAMESPACES
|
111 |
|
|
.PP
|
112 |
|
|
\fB[incr\ Tcl]\fR now includes a complete namespace facility.
|
113 |
|
|
A namespace is a collection of commands and global variables that
|
114 |
|
|
is kept apart from the usual global scope. This allows Tcl code
|
115 |
|
|
libraries to be packaged in a well-defined manner, and prevents
|
116 |
|
|
unwanted interactions with other libraries. A namespace can also
|
117 |
|
|
have child namespaces within it, so one library can contain its
|
118 |
|
|
own private copy of many other libraries. A namespace can also
|
119 |
|
|
be used to wrap up a group of related classes. The global scope
|
120 |
|
|
(named "\fC::\fR") is the root namespace for an interpreter; all
|
121 |
|
|
other namespaces are contained within it.
|
122 |
|
|
.PP
|
123 |
|
|
See the \fBnamespace\fR command for details on creating and
|
124 |
|
|
using namespaces.
|
125 |
|
|
|
126 |
|
|
.SH MEGA-WIDGETS
|
127 |
|
|
.PP
|
128 |
|
|
Mega-widgets are high-level widgets that are constructed using
|
129 |
|
|
Tk widgets as component parts, usually without any C code. A
|
130 |
|
|
fileselectionbox, for example, may have a few listboxes, some
|
131 |
|
|
entry widgets and some control buttons. These individual widgets
|
132 |
|
|
are put together in a way that makes them act like one big
|
133 |
|
|
widget.
|
134 |
|
|
.PP
|
135 |
|
|
\fB[incr\ Tk]\fR is a framework for building mega-widgets. It
|
136 |
|
|
uses \fB[incr\ Tcl]\fR to support the object paradigm, and adds
|
137 |
|
|
base classes which provide default widget behaviors. See the
|
138 |
|
|
\fBitk\fR man page for more details.
|
139 |
|
|
.PP
|
140 |
|
|
\fB[incr\ Widgets]\fR is a library of mega-widgets built using
|
141 |
|
|
\fB[incr\ Tk]\fR. It contains more than 30 different widget
|
142 |
|
|
classes that can be used right out of the box to build Tcl/Tk
|
143 |
|
|
applications. Each widget class has its own man page describing
|
144 |
|
|
the features available.
|
145 |
|
|
|
146 |
|
|
.SH KEYWORDS
|
147 |
|
|
class, object, object-oriented, namespace, mega-widget
|