File: subsection3_8_3.html

package info (click to toggle)
tix 8.4.3-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,316 kB
  • sloc: ansic: 28,084; tcl: 22,774; python: 7,577; makefile: 333; cs: 253; sh: 210; perl: 128
file content (82 lines) | stat: -rw-r--r-- 3,829 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<HEAD>
<TITLE> Writing Methods<A NAME=63>&nbsp;</A></TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff" TEXT="#000000" LINK="#0000ff" VLINK="#000080">
<FONT FACE="Tahoma, Arial, Helvetica">
 <HR> <A NAME=tex2html1186 HREF=subsubsection3_8_3_1.html><IMG ALIGN=MIDDLE SRC="../gif/icons/next_motif.gif"></A> <A NAME=tex2html1184 HREF=section3_8.html><IMG ALIGN=MIDDLE SRC="../gif/icons/up_motif.gif"></A> <A NAME=tex2html1178 HREF=subsubsection3_8_2_1.html><IMG ALIGN=MIDDLE SRC="../gif/icons/previous_motif.gif"></A> <A NAME=tex2html1188 HREF=tableofcontents3_1.html><IMG ALIGN=MIDDLE SRC="../gif/icons/contents_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html1187 HREF=subsubsection3_8_3_1.html> Declaring Public Methods</A>
<B>Up:</B> <A NAME=tex2html1185 HREF=section3_8.html> Tix Object Oriented </A>
<B> Previous:</B> <A NAME=tex2html1179 HREF=subsubsection3_8_2_1.html> Using the tixWidgetClass </A>
<HR> <P>
<A NAME=Contents>&nbsp;</A><H2><A NAME=SECTION00083000000000000000> Writing Methods<A NAME=63>&nbsp;</A></A></H2>
<P>
After we have declared the new widget class, we can write methods
  for this class to define its behavior. Methods are just a special
  type of TCL procedures and they are created by the <tt>proc</tt>
  command. There are, however, three requirements for methods. First,
  their names must be prefixed by the command name of their
  class. Second, they must accept at least one argument and the first
  argument that they accept must be called <tt>w</tt>. Third, the first
  command executed inside each method must be:
<blockquote> <P><tt> upvar #0 $w data
</tt>
<P></blockquote>
<P>For example, the following is an implementation of the invert method
  for the class TixArrowButton:
<P>
<blockquote> <P><tt> proc tixArrowButton:invert {w} {<BR>
    upvar #0 $w data<BR>
<BR>
    set curDirection $data(-direction)<BR>
    case $curDirection {<BR>
        n {<BR>
            set newDirection s<BR>
        }<BR>
        s {<BR>
            set newDirection n<BR>
        }<BR>
        # ....<BR>
    }<BR>
}</tt>
<P></blockquote>
<P>
   Notice that the name of the method is prefixed by the command name
  of the class (<tt>tixArrowButton</tt>). Also, the first and only
  argument that it accepts is <tt>w</tt> and the first line it executes
  is ``<tt>upvar #0 $wdata</tt>''.
<P>
   The argument <tt>w</tt> specifies which widget instance this method
  should act upon. For example, if the user has issued the command
<P>
<blockquote> <P><tt> .up invert
</tt>
<P></blockquote>
   on an instance <tt>.up</tt> of the class tixArrowButton, the method
  <tt>tixArrowButton:invert</tt> will be called and the argument <tt>w</tt>
  will have the value <tt>.up</tt>.
<P>
   The <tt>invert</tt> method is used to invert the direction of the
  arrow. Therefore, it should examine the variable <tt>.up(-direction)</tt>, which stores the current direction of the instance
  <tt>.up</tt>, and modify it appropriately. It turns out that in TCL,
  the only clean way to access an array whose name is stored in a
  variable is the ``<tt>upvar #0 $wdata</tt>'' technique: essentially
  it tells the intepreter that the array data should be an alias for
  the global array whose name is stored in <tt>$w</tt>. We will soon see
  how the widget's methods use the data array.
<P>
   Once the mysterious ``<tt>upvar #0 $wdata</tt>'' line is explained,
  it becomes clear what the rest of the <tt>tixArrowButton:invert</tt>
  method does: it examines the current direction of the arrow, which
  is stored in <tt>$data(-direction)</tt> and inverts it.
<P>
<HR>
<UL> 
<LI> <A NAME=tex2html1189 HREF=subsubsection3_8_3_1.html#SECTION00083100000000000000> Declaring Public Methods<A NAME=631>&nbsp;</A></A>
</UL>
<HR>

</FONT>
</BODY>
<P><ADDRESS>
<A HREF=http://tix.sourceforge.net>http://tix.sourceforge.net</A><BR>
</ADDRESS>