comparison extra/tk_octave/tk_entry.m @ 0:6b33357c7561 octave-forge

Initial revision
author pkienzle
date Wed, 10 Oct 2001 19:54:49 +0000
parents
children cc9b4c05d84d
comparison
equal deleted inserted replaced
-1:000000000000 0:6b33357c7561
1 ## Copyright (C) 1998, 1999, 2000 Joao Cardoso.
2 ##
3 ## This program is free software; you can redistribute it and/or modify it
4 ## under the terms of the GNU General Public License as published by the
5 ## Free Software Foundation; either version 2 of the License, or (at your
6 ## option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ## General Public License for more details.
12 ##
13 ## This file is part of tk_octave.
14
15 ## usage: [r1, r2, ...] = tk_entry (title, name, value, name, value, ...)
16 ##
17 ## Dialog to input several variables at once.
18
19 ## 2001-09-14 Paul Kienzle <pkienzle@users.sf.net>
20 ## * convert for pthreads-based tk_octave
21
22 function [...] = tk_entry (t, ...)
23
24 tk_init
25
26 if (nargin < 2)
27 usage ("tk_entry (title, legend_1, value_1 | variable_1, ...)");
28 endif
29
30 tk_cmd( sprintf( "proc isnum {x} {regexp {^[0-9.eE+-]*$} $x}") );
31
32 tk_cmd( sprintf( "proc varcheck {name element op} {\n\
33 upvar $name x ${name}_ x_\n\
34 if {([isnum $x] && ![isnum $x_]) || (![isnum $x] && [isnum $x_])} {\n\
35 set erro [format \"Value entered, `%%s' has not same type as previous one, `%%s'\" $x $x_]\n\
36 set x $x_\n tkerror $erro \n}\n}\n") );
37
38 tk_cmd( sprintf("wm deiconify .;frame .master") );
39
40 if (! isempty (t))
41 tk_cmd( sprintf("wm title . \"%s\"", t) );
42 tk_cmd( sprintf("label .master.ltitle -pady 5 -relief groove -borderwidth 2 -text \"%s\";\
43 pack .master.ltitle -fill x -side top", t) );
44 endif
45
46 nopt = (nargin - 1)/2;
47 cmd_ok = cmd_res = "";
48 va_start();
49
50 for i=1:nopt
51 desc = va_arg();
52 val = va_arg();
53
54 tk_cmd( sprintf( "unset val_%d val_%d_\n", i, i) ); # unset previous invocation values
55
56 if (isstr(val)) # string
57 tk_cmd( sprintf( "set val_%d \"%s\"", i, val) ); # work value
58 tk_cmd( sprintf( "set val_%d_ \"%s\"", i, val) ); # original value
59 cmd_ok = strcat( cmd_ok, sprintf("val_%d = \\\"$val_%d\\\";", i, i));
60 elseif (floor(val) == val) # integer
61 tk_cmd( sprintf( "set val_%d %d", i, val));
62 tk_cmd( sprintf( "set val_%d_ %d", i, val));
63 cmd_ok = strcat( cmd_ok, sprintf("val_%d = $val_%d;", i, i));
64 else # float
65 tk_cmd( sprintf( "set val_%d %f", i, val));
66 tk_cmd( sprintf( "set val_%d_ %f", i, val));
67 cmd_ok = strcat( cmd_ok, sprintf("val_%d = $val_%d;", i, i));
68 endif
69
70 tk_cmd( sprintf( "trace variable val_%d w varcheck", i));
71
72 tk_cmd( sprintf( "frame .master.f%d", i));
73 tk_cmd( sprintf( "entry .master.f%d.e%d -textvariable val_%d", i, i, i));
74 tk_cmd( sprintf( "pack .master.f%d -fill x", i));
75 if (desc != "")
76 tk_cmd( sprintf( "label .master.f%d.l%d -font 8x13bold -text \"%s\"", i, i, desc));
77 tk_cmd( sprintf( "pack .master.f%d.l%d -side left", i, i));
78 endif
79 tk_cmd( sprintf( "pack .master.f%d.e%d -side right", i, i));
80 tk_cmd( sprintf( "bind .master.f%d.e%d <Return> {set val_%d $val_%d}", i, i, i, i));
81 tk_cmd( sprintf( "bind .master.f%d.e%d <Tab> {set val_%d $val_%d}", i, i, i, i));
82 cmd_res = strcat( cmd_res, sprintf("set val_%d $val_%d_;", i, i));
83 endfor
84
85 tk_cmd( sprintf( "frame .master.f%d -relief groove -borderwidth 2", nargin) );
86
87 tk_cmd( sprintf( "button .master.f%d.b1 -text OK -command { destroy .master}", nargin) );
88 tk_cmd( sprintf( "bind .master.f%d.b1 <Return> {destroy .master }", nargin) );
89 tk_cmd( sprintf( "button .master.f%d.b2 -text Restore -command {%s}", nargin, cmd_res) );
90 tk_cmd( sprintf( "bind .master.f%d.b2 <Return> {%s}", nargin, cmd_res) );
91 tk_cmd( sprintf( "pack .master.f%d -side bottom -fill x", nargin) );
92 tk_cmd( sprintf( "pack .master.f%d.b1 .master.f%d.b2 -side left", nargin, nargin) );
93
94 tk_cmd( sprintf("pack .master -fill both -expand 1") );
95
96 tk_cmd( "tkwait window .master" );
97 eval(tk_cmd(sprintf("set result \"%s\" ", cmd_ok )));
98
99 for i=1:nopt
100 vr_val(eval(sprintf("val_%d;",i)));
101 tk_cmd( sprintf( "trace vdelete val_%d w varcheck", i) );
102 endfor
103
104 endfunction