source: trunk/lang/tcl/scripts/types/string.tcl @ 2154

Last change on this file since 2154 was 2154, checked in by mmc, 13 years ago

Added a definition for the "choice" object, along with the "choices" editor
for specifying values.

Numerous fixes to the object system: Cleaned up the palettes for all
objects so they can be used to warn about things like output objects on
the input side. Marked may attributes as "input only" via the new -only
option for attributes. Fixed the label/description checks so that they
occur properly when inherited from the "base" object class.

File size: 3.1 KB
Line 
1# ----------------------------------------------------------------------
2#  EDITOR: string attribute values
3#
4#  Used within the Instant Rappture builder to edit string values.
5#  Strings can have the following additional options:
6#
7#    -lines N ......... height of editor (default is 1 line)
8#    -validate type ... validation routine applied to string values
9#
10# ======================================================================
11#  AUTHOR:  Michael McLennan, Purdue University
12#  Copyright (c) 2004-2010  Purdue Research Foundation
13#
14#  See the file "license.terms" for information on usage and
15#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16# ======================================================================
17itcl::class AttrString {
18    constructor {win args} {
19        Rappture::getopts args params {
20            value -lines 1
21            value -validate ""
22        }
23
24        if {$params(-lines) > 1} {
25            Rappture::Scroller $win.scrl -xscrollmode none -yscrollmode auto
26            pack $win.scrl -expand yes -fill both
27            text $win.scrl.text -width 10 -height $params(-lines) -wrap word
28            $win.scrl contents $win.scrl.text
29        } else {
30            entry $win.str
31            pack $win.str -fill x
32        }
33        set _win $win
34        set _validate $params(-validate)
35    }
36
37    public method load {val} {
38        if {[winfo exists $_win.str]} {
39            $_win.str delete 0 end
40            $_win.str insert end $val
41        } else {
42            $_win.scrl.text delete 1.0 end
43            $_win.scrl.text insert end $val
44        }
45    }
46
47    public method check {} {
48        if {[string length $_validate] > 0} {
49            if {[winfo exists $_win.str]} {
50                set str [$_win.str get]
51            } else {
52                set str [$_win.scrl.text get 1.0 end-1char]
53            }
54            if {[catch {uplevel #0 $_validate [list $str]} result]} {
55                return [list error "Bad value: $result"]
56            }
57        }
58    }
59
60    public method save {var} {
61        upvar $var value
62
63        set err [lindex [check] 1]
64        if {[string length $err] > 0} {
65            Rappture::Tooltip::cue $_win $err
66            return 0
67        }
68
69        if {[winfo exists $_win.str]} {
70            set value [$_win.str get]
71        } else {
72            set value [$_win.scrl.text get 1.0 end-1char]
73        }
74        return 1
75    }
76
77    public method edit {} {
78        if {[winfo exists $_win.str]} {
79            focus -force $_win.str
80            $_win.str selection from 0
81            $_win.str selection to end
82        } else {
83            focus -force $_win.scrl.text
84            $_win.scrl.text tag add sel 1.0 end
85        }
86    }
87
88    public proc import {xmlobj path} {
89        # trivial import -- just return info as-is from XML
90        return [$xmlobj get $path]
91    }
92
93    public proc export {xmlobj path value} {
94        # trivial export -- just save info as-is into XML
95        if {[string length $value] > 0} {
96            $xmlobj put $path $value
97        }
98    }
99
100    private variable _win ""       ;# containing frame
101    private variable _validate ""  ;# validation command for checking values
102}
Note: See TracBrowser for help on using the repository browser.