| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 |
<html> |
| 3 |
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ --> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 |
<title>Linked Variables (GCL TK Manual)</title> |
| 7 |
|
| 8 |
<meta name="description" content="Linked Variables (GCL TK Manual)"> |
| 9 |
<meta name="keywords" content="Linked Variables (GCL TK Manual)"> |
| 10 |
<meta name="resource-type" content="document"> |
| 11 |
<meta name="distribution" content="global"> |
| 12 |
<meta name="Generator" content="makeinfo"> |
| 13 |
<link href="index.html" rel="start" title="Top"> |
| 14 |
<link href="wm.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| 15 |
<link href="General.html" rel="up" title="General"> |
| 16 |
<link href="tkconnect.html" rel="next" title="tkconnect"> |
| 17 |
<link href="Lisp-Functions-Invoked-from-Graphics.html" rel="prev" title="Lisp Functions Invoked from Graphics"> |
| 18 |
<style type="text/css"> |
| 19 |
<!-- |
| 20 |
a.summary-letter {text-decoration: none} |
| 21 |
blockquote.indentedblock {margin-right: 0em} |
| 22 |
div.display {margin-left: 3.2em} |
| 23 |
div.example {margin-left: 3.2em} |
| 24 |
div.lisp {margin-left: 3.2em} |
| 25 |
kbd {font-style: oblique} |
| 26 |
pre.display {font-family: inherit} |
| 27 |
pre.format {font-family: inherit} |
| 28 |
pre.menu-comment {font-family: serif} |
| 29 |
pre.menu-preformatted {font-family: serif} |
| 30 |
span.nolinebreak {white-space: nowrap} |
| 31 |
span.roman {font-family: initial; font-weight: normal} |
| 32 |
span.sansserif {font-family: sans-serif; font-weight: normal} |
| 33 |
ul.no-bullet {list-style: none} |
| 34 |
--> |
| 35 |
</style> |
| 36 |
|
| 37 |
|
| 38 |
</head> |
| 39 |
|
| 40 |
<body lang="en"> |
| 41 |
<span id="Linked-Variables"></span><div class="header"> |
| 42 |
<p> |
| 43 |
Next: <a href="tkconnect.html" accesskey="n" rel="next">tkconnect</a>, Previous: <a href="Lisp-Functions-Invoked-from-Graphics.html" accesskey="p" rel="prev">Lisp Functions Invoked from Graphics</a>, Up: <a href="General.html" accesskey="u" rel="up">General</a> [<a href="wm.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p> |
| 44 |
</div> |
| 45 |
<hr> |
| 46 |
<span id="Linked-Variables-1"></span><h3 class="section">1.7 Linked Variables</h3> |
| 47 |
|
| 48 |
<p>It is possible to link lisp variables to <b>TK</b> variables. In general |
| 49 |
when the <b>TK</b> variable is changed, by for instance clicking on a |
| 50 |
radiobutton, the linked lisp variable will be changed. Conversely |
| 51 |
changing the lisp variable will be noticed by the <b>TK</b> graphics side, if |
| 52 |
one does the assignment in lisp using <code>setk</code> instead of |
| 53 |
<code>setq</code>. |
| 54 |
</p> |
| 55 |
<div class="example"> |
| 56 |
<pre class="example">(button '.hello :textvariable '*message* :text "hi there") |
| 57 |
(pack '.hello) |
| 58 |
</pre></div> |
| 59 |
|
| 60 |
<p>This causes linking of the global variable <code>*message*</code> in lisp |
| 61 |
to a corresponding variable in <b>TK</b>. Moreover the message that is in |
| 62 |
the button <code>.hello</code> will be whatever the value of this global |
| 63 |
variable is (so long as the <b>TK</b> side is notified of the change!). |
| 64 |
</p> |
| 65 |
<p>Thus if one does |
| 66 |
</p> |
| 67 |
<div class="example"> |
| 68 |
<pre class="example">(setk *message* "good bye") |
| 69 |
</pre></div> |
| 70 |
|
| 71 |
<p>then the button will change to have <i>good bye</i> as its text. |
| 72 |
The lisp macro <code>setk</code> expands into |
| 73 |
</p> |
| 74 |
<div class="example"> |
| 75 |
<pre class="example">(prog1 (setf *message* "good bye") (notice-text-variables)) |
| 76 |
</pre></div> |
| 77 |
|
| 78 |
<p>which does the assignment, and then goes thru the linked variables |
| 79 |
checking for those that have changed, and updating the <b>TK</b> side should |
| 80 |
there be any. Thus if you have a more complex program which might |
| 81 |
have done the assignment of your global variable, you may include |
| 82 |
the call to <code>notice-text-variables</code> at the end, to assure that |
| 83 |
the graphics side knows about the changes. |
| 84 |
</p> |
| 85 |
<p>A variable which is linked using the keyword <code>:textvariable</code> is |
| 86 |
always a variable containing a string. |
| 87 |
</p> |
| 88 |
<p>However it is possible to have other types of variables. |
| 89 |
</p> |
| 90 |
<div class="example"> |
| 91 |
<pre class="example">(checkbutton '.checkbutton1 :text "A button" :variable '(boolean *joe*)) |
| 92 |
(checkbutton '.checkbutton2 :text "A button" :variable '*joe*) |
| 93 |
(checkbutton '.checkbutton3 :text "Debugging" :variable '(t *debug*) |
| 94 |
:onvalue 100 :offvalue -1) |
| 95 |
</pre></div> |
| 96 |
|
| 97 |
<p>The first two examples are the same in that the default variable type |
| 98 |
for a checkbutton is <code>boolean</code>. Notice that the specification of a |
| 99 |
variable type is by <code>(<i>type</i> variable)</code>. The types which are |
| 100 |
permissible are those which have coercion-fucntions, See <a href="Return-Values.html">Return Values</a>. In the first example a variable <code>*joe*</code> will be linked, and |
| 101 |
its default initial value will be set to nil, since the default initial |
| 102 |
state of the check button is off, and the default off value is nil. |
| 103 |
Actually on the <b>TK</b> side, the corresponding boolean values are <code>"1"</code> |
| 104 |
and <code>"0"</code>, but the <code>boolean</code> type makes these become <code>t</code> |
| 105 |
and <code>nil</code>. |
| 106 |
</p> |
| 107 |
<p>In the third example the variable *debug* may have any lisp value (here |
| 108 |
<i>type</i> is <code>t</code>). The initial value will be made to be <code>-1</code>, |
| 109 |
since the checkbutton is off. Clicking on <code>.checkbutton3</code> will |
| 110 |
result in the value of <code>*debug*</code> being changed to 100, and the light |
| 111 |
in the button will be toggled to on, See <a href="checkbutton.html">checkbutton</a>. You may |
| 112 |
set the variable to be another value besides 100. |
| 113 |
</p> |
| 114 |
<p>You may also call |
| 115 |
</p> |
| 116 |
<div class="example"> |
| 117 |
<pre class="example">(link-text-variable '*joe* 'boolean) |
| 118 |
</pre></div> |
| 119 |
|
| 120 |
<p>to cause the linking of a variable named *joe*. This is done |
| 121 |
automatically |
| 122 |
whenever the variable is specified after one of the keys |
| 123 |
</p> |
| 124 |
<div class="example"> |
| 125 |
<pre class="example">:variable :textvariable. |
| 126 |
</pre></div> |
| 127 |
|
| 128 |
<p>Just as one must be cautious about using global variables in lisp, one |
| 129 |
must be cautious in making such linked variables. In particular note |
| 130 |
that the <b>TK</b> side, uses variables for various purposes. If you make a |
| 131 |
checkbutton with pathname <code>.a.b.c</code> then unless you specify a |
| 132 |
<code>:variable</code> option, the variable <code>c</code> will become associated to |
| 133 |
the <b>TK</b> value of the checkbutton. We do NOT link this variable by |
| 134 |
default, feeling that one might inadvertently alter global variables, |
| 135 |
and that they would not typically use the lisp convention of being of |
| 136 |
the form <code>*c*</code>. You must specify the <code>:variable</code> option, or |
| 137 |
call <code>link-variable</code>. |
| 138 |
</p> |
| 139 |
|
| 140 |
<hr> |
| 141 |
<div class="header"> |
| 142 |
<p> |
| 143 |
Next: <a href="tkconnect.html" accesskey="n" rel="next">tkconnect</a>, Previous: <a href="Lisp-Functions-Invoked-from-Graphics.html" accesskey="p" rel="prev">Lisp Functions Invoked from Graphics</a>, Up: <a href="General.html" accesskey="u" rel="up">General</a> [<a href="wm.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p> |
| 144 |
</div> |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
</body> |
| 149 |
</html> |