| 1 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | # FILE : ediselec.tcl |
|---|
| 3 | # PURPOSE : This file contains a namespace called EditSelector with its |
|---|
| 4 | # functions. It's for creating a widget that has an entry widget |
|---|
| 5 | # and a button that calls a multiple selector (a widget that allows |
|---|
| 6 | # selecting multiple items from a list of items, see mulselec.tcl). |
|---|
| 7 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 11 | # TESTING |
|---|
| 12 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 13 | # Put if {1} for testing this file. |
|---|
| 14 | if {0} { |
|---|
| 15 | package require Tk |
|---|
| 16 | package require BWidget |
|---|
| 17 | # force BWidget autoload |
|---|
| 18 | Bitmap::use |
|---|
| 19 | set toltk_script_path [file dirname [info script]] |
|---|
| 20 | set toltk_images_path [file join ${toltk_script_path} images] |
|---|
| 21 | if { [lsearch $Bitmap::path $toltk_images_path] == -1 } { |
|---|
| 22 | lappend Bitmap::path [file join $toltk_script_path images] |
|---|
| 23 | } |
|---|
| 24 | package require msgcat |
|---|
| 25 | namespace import msgcat::* |
|---|
| 26 | ::msgcat::mclocale es |
|---|
| 27 | ::msgcat::mcload [file join $toltk_script_path msgs] |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 32 | namespace eval EditSelector { |
|---|
| 33 | # |
|---|
| 34 | # PURPOSE: The purpose of this namespace is organize code and functions. |
|---|
| 35 | # A special namespace is created for each instance of editSelector. |
|---|
| 36 | # |
|---|
| 37 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 43 | proc ::EditSelector::Create { parent w list {label "Nombre" } {pack 1} } { |
|---|
| 44 | # |
|---|
| 45 | # PURPOSE: Creates an instance of a edit selector. It creates the widget |
|---|
| 46 | # composed by a label, an entry and a button that shows a multiple |
|---|
| 47 | # selector widget. Also creates a procedure asociated. |
|---|
| 48 | # |
|---|
| 49 | # PARAMETERS: |
|---|
| 50 | # parent -> widget's parent |
|---|
| 51 | # w -> name of widget |
|---|
| 52 | # list -> list of initial values of multiple selector |
|---|
| 53 | # label -> text of the label |
|---|
| 54 | # pack -> if the widget must be packed or not (yes by default) |
|---|
| 55 | # |
|---|
| 56 | # RETURN: The procedure created. |
|---|
| 57 | # |
|---|
| 58 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 59 | |
|---|
| 60 | set Instance ::EditSelector::${parent}$w |
|---|
| 61 | if { [lsearch [namespace children ::EditSelector] $Instance] != -1 } { |
|---|
| 62 | error "$Instance already exists as a children of ::EditSelector" |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | # A namespace is created for each edit selector widget. |
|---|
| 66 | |
|---|
| 67 | namespace eval $Instance { |
|---|
| 68 | |
|---|
| 69 | # data -> array with information about the instance: |
|---|
| 70 | # parent -> widget's parent path |
|---|
| 71 | # label -> label widget's path |
|---|
| 72 | # entry -> entry widget's path |
|---|
| 73 | # button -> button widget's path |
|---|
| 74 | # unselected -> list of unselected items |
|---|
| 75 | # selected -> list of selected items |
|---|
| 76 | # |
|---|
| 77 | |
|---|
| 78 | variable data |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | upvar #0 ${Instance}::data data |
|---|
| 82 | |
|---|
| 83 | #set f [frame $w] |
|---|
| 84 | set data(parent) $parent |
|---|
| 85 | set data(label) [label $parent.${w}ldes -text $label -anchor w] |
|---|
| 86 | set data(entry) [Entry $parent.${w}sel -state disabled \ |
|---|
| 87 | -disabledforeground black] |
|---|
| 88 | set data(button) [Button $parent.${w}bsel -image [Bitmap::get puntos] \ |
|---|
| 89 | -relief link -helptext [mc "Multiple Selector"] \ |
|---|
| 90 | -command [list ::EditSelector::OpenMS $Instance]] |
|---|
| 91 | if $pack { |
|---|
| 92 | grid $data(label) $data(entry) $data(button) -sticky ew -padx 2 |
|---|
| 93 | grid columnconfigure $parent 1 -weight 1 |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | bind $data(label) <Destroy> [list ::EditSelector::Destroy $Instance] |
|---|
| 97 | InitInstance $Instance $list |
|---|
| 98 | |
|---|
| 99 | set InstanceProc ${Instance}::proc |
|---|
| 100 | proc $InstanceProc { option args} \ |
|---|
| 101 | "eval ::EditSelector::InstanceProc {$Instance} \$option \$args" |
|---|
| 102 | return $InstanceProc |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | proc ::EditSelector::InstanceProc { instance option args } { |
|---|
| 108 | # |
|---|
| 109 | # PURPOSE: Procedure created to attend calls to the widget. Sets or gets the |
|---|
| 110 | # selected or unselected items of the multiple selector. |
|---|
| 111 | # |
|---|
| 112 | # PARAMETERS: |
|---|
| 113 | # instance -> Instance of edit selector |
|---|
| 114 | # option -> set or get |
|---|
| 115 | # args -> arguments of the command: |
|---|
| 116 | # if option is set, args is a list of the form: |
|---|
| 117 | # {selected|unselected values} |
|---|
| 118 | # if option is get, args is selected or unselected |
|---|
| 119 | # |
|---|
| 120 | # RETURN: If options is get it returns the selected or unselected items of the |
|---|
| 121 | # multiple selector. |
|---|
| 122 | # |
|---|
| 123 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 124 | |
|---|
| 125 | upvar #0 ${instance}::data data |
|---|
| 126 | |
|---|
| 127 | switch -- $option { |
|---|
| 128 | get { |
|---|
| 129 | if { [catch {set a $data($args)}] } { |
|---|
| 130 | error "Invalid argument '$args'" |
|---|
| 131 | } |
|---|
| 132 | return $a |
|---|
| 133 | } |
|---|
| 134 | set { |
|---|
| 135 | set item [lindex $args 0] |
|---|
| 136 | if { ([string equal $item unselected]) || |
|---|
| 137 | ([string equal $item selected]) } { |
|---|
| 138 | set data($item) [lindex $args 1] |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | default { |
|---|
| 142 | error "Invalid option '$option': should be set or get" |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 149 | proc ::EditSelector::Destroy { instance } { |
|---|
| 150 | # |
|---|
| 151 | # PURPOSE: Destroy the instance given by instance. |
|---|
| 152 | # |
|---|
| 153 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 154 | |
|---|
| 155 | namespace delete $instance |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 160 | proc ::EditSelector::InitInstance { instance list} { |
|---|
| 161 | # |
|---|
| 162 | # PURPOSE: Initializes the lists of selected and unselected items. |
|---|
| 163 | # |
|---|
| 164 | # PARAMETERS: |
|---|
| 165 | # instance -> the instance |
|---|
| 166 | # list -> list of selected items |
|---|
| 167 | # |
|---|
| 168 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 169 | |
|---|
| 170 | upvar #0 ${instance}::data data |
|---|
| 171 | |
|---|
| 172 | set data(unselected) $list |
|---|
| 173 | set data(selected) {} |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 178 | proc ::EditSelector::OpenMS { instance } { |
|---|
| 179 | # |
|---|
| 180 | # PURPOSE: Calls (and creates) a multiple selector widget with the selected |
|---|
| 181 | # and unselected items. |
|---|
| 182 | # |
|---|
| 183 | # PARAMETERS: |
|---|
| 184 | # instance -> the instance of edit selector |
|---|
| 185 | # |
|---|
| 186 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 187 | |
|---|
| 188 | upvar #0 ${instance}::data data |
|---|
| 189 | |
|---|
| 190 | ::MultipleSelector::CreateToplevel $data(parent).s \ |
|---|
| 191 | ${instance}::data(unselected) ${instance}::data(selected) |
|---|
| 192 | $data(entry) configure -text [ListToStr $data(selected)] \ |
|---|
| 193 | -helptext [::ListToStr $data(selected) "\n" { } { }] |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 198 | proc ::EditSelector::ListToStr { list } { |
|---|
| 199 | # |
|---|
| 200 | # PURPOSE: Concatenates list element names into a string for the label of the |
|---|
| 201 | # edit selector widget. If list has more than one element the string |
|---|
| 202 | # is first element name plus '...'. |
|---|
| 203 | # |
|---|
| 204 | # RETURN: The string created. |
|---|
| 205 | # |
|---|
| 206 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 207 | |
|---|
| 208 | switch -exact -- [llength $list] { |
|---|
| 209 | 0 { |
|---|
| 210 | set result {} |
|---|
| 211 | } |
|---|
| 212 | 1 { |
|---|
| 213 | set result [lindex $list 0] |
|---|
| 214 | } |
|---|
| 215 | default { |
|---|
| 216 | set result [lindex $list 0]... |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | return $result |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 223 | proc ::EditSelector::BClear { instance } { |
|---|
| 224 | # |
|---|
| 225 | # PURPOSE: Empty de element |
|---|
| 226 | # |
|---|
| 227 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 228 | upvar #0 ${instance}::data data |
|---|
| 229 | |
|---|
| 230 | set data(unselected) {} |
|---|
| 231 | set data(selected) {} |
|---|
| 232 | |
|---|
| 233 | $data(entry) configure -text "" -helptext "" |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|