| 1 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | # FILE : blistorder.tcl |
|---|
| 3 | # PURPOSE : Component that adds order funcionality to blistboxplus |
|---|
| 4 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 5 | |
|---|
| 6 | package require byscommon |
|---|
| 7 | package require blistboxplus |
|---|
| 8 | |
|---|
| 9 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 10 | ::snit::widget blistorder { |
|---|
| 11 | # |
|---|
| 12 | # PURPOSE: Listboxplus with order funcionality |
|---|
| 13 | # |
|---|
| 14 | # variables |
|---|
| 15 | # list -> blistboxplus |
|---|
| 16 | # bbox -> buttonbox |
|---|
| 17 | # created -> indicates if the widget has been fully created |
|---|
| 18 | # |
|---|
| 19 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 20 | widgetclass Blistorder |
|---|
| 21 | |
|---|
| 22 | variable _list |
|---|
| 23 | variable bbox |
|---|
| 24 | variable created 0 |
|---|
| 25 | |
|---|
| 26 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 27 | constructor { args } { |
|---|
| 28 | # |
|---|
| 29 | # PURPOSE: blistorder's constructor. Creates an instance of blistorder |
|---|
| 30 | # |
|---|
| 31 | # PARAMETERS: |
|---|
| 32 | # args -> Arguments for the component |
|---|
| 33 | # |
|---|
| 34 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 35 | # Create the widget |
|---|
| 36 | $self createwidget $args |
|---|
| 37 | |
|---|
| 38 | # Apply all arguments given: |
|---|
| 39 | $self configurelist $args |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 43 | method createwidget { args } { |
|---|
| 44 | # |
|---|
| 45 | # PURPOSE: Creation of the widget |
|---|
| 46 | # |
|---|
| 47 | # PARAMETERS: |
|---|
| 48 | # args -> Arguments for the component |
|---|
| 49 | # |
|---|
| 50 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 51 | # Widget that contains blistboxplus |
|---|
| 52 | if {0} { |
|---|
| 53 | install list using blistboxplus $self.lb |
|---|
| 54 | } else { |
|---|
| 55 | set _list [blistboxplus $win.lb] |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | # Widget that contains buttonbox |
|---|
| 59 | install bbox using ButtonBox $win.bb -orient vertical -spacing 0 \ |
|---|
| 60 | -padx 4 -pady 1 |
|---|
| 61 | |
|---|
| 62 | $bbox add -image [Bitmap::get upNext] -height 16 -width 16 \ |
|---|
| 63 | -command [list $self _MoveUp] -relief link \ |
|---|
| 64 | -helptext [mc "Moves selection up"] |
|---|
| 65 | $bbox add -image [Bitmap::get downNext] -height 16 -width 16 \ |
|---|
| 66 | -command [list $self _MoveDown] -relief link \ |
|---|
| 67 | -helptext [mc "Moves selection down"] |
|---|
| 68 | |
|---|
| 69 | grid $win.lb $bbox -sticky news |
|---|
| 70 | grid rowconfigure $self 0 -weight 1 |
|---|
| 71 | grid columnconfigure $self 0 -weight 1 |
|---|
| 72 | grid columnconfigure $self 1 -pad 5 |
|---|
| 73 | update |
|---|
| 74 | |
|---|
| 75 | #The widget is created |
|---|
| 76 | set created 1 |
|---|
| 77 | |
|---|
| 78 | return $self |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 82 | method _MoveUp {} { |
|---|
| 83 | # |
|---|
| 84 | # PURPOSE: Moves selected items in listboxplus up |
|---|
| 85 | # |
|---|
| 86 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 87 | set indexes [$self curselection] |
|---|
| 88 | if {$indexes != ""} { |
|---|
| 89 | set first [lindex $indexes 0] |
|---|
| 90 | set last [lindex $indexes end] |
|---|
| 91 | set start [$self first] |
|---|
| 92 | if { $first != $start } { |
|---|
| 93 | $self move [lindex [$self range [$self first] $first] end-1] \ |
|---|
| 94 | after $last |
|---|
| 95 | $self selection clearall |
|---|
| 96 | $self selection set $first $last |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 102 | method _MoveDown {} { |
|---|
| 103 | # |
|---|
| 104 | # PURPOSE: Moves selected items in listboxplus down |
|---|
| 105 | # |
|---|
| 106 | #/////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | set indexes [$self curselection] |
|---|
| 108 | if {$indexes != ""} { |
|---|
| 109 | set first [lindex $indexes 0] |
|---|
| 110 | set last [lindex $indexes end] |
|---|
| 111 | set end [$self last] |
|---|
| 112 | if { $last != $end } { |
|---|
| 113 | $self move [lindex [$self find $last] 1] before $first |
|---|
| 114 | $self selection clearall |
|---|
| 115 | $self selection set $first $last |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | # methods and options delegation |
|---|
| 120 | delegate method * to _list |
|---|
| 121 | delegate option * to _list |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | package provide blistorder 1.0 |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | # TEST CODE |
|---|
| 128 | |
|---|
| 129 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 130 | proc test_blistorder {} { |
|---|
| 131 | # |
|---|
| 132 | # PURPOSE: Function that test blistorder |
|---|
| 133 | # |
|---|
| 134 | #///////////////////////////////////////////////////////////////////////////// |
|---|
| 135 | |
|---|
| 136 | # defining the widgets |
|---|
| 137 | variable data |
|---|
| 138 | set data(-selected) 1 |
|---|
| 139 | |
|---|
| 140 | destroy .top |
|---|
| 141 | toplevel .top -width 200 -height 100 |
|---|
| 142 | |
|---|
| 143 | TitleFrame .top.f1 -text "Blistorder test" |
|---|
| 144 | set f1 [ .top.f1 getframe ] |
|---|
| 145 | blistorder $f1.blb -cols { col1 col2 col3 col4 } -parent $f1 \ |
|---|
| 146 | -height 150 -width 500 -orientation vertical \ |
|---|
| 147 | -hiddencols { col1 col2 } |
|---|
| 148 | $f1.blb insert end {1 2 3 4} {one two three four} {uno dos tres cuatro} \ |
|---|
| 149 | {k kk kkk kkkk} {0 0 0 0} {@ # $ %} |
|---|
| 150 | TitleFrame .top.f2 -text "Configure" |
|---|
| 151 | set f2 [ .top.f2 getframe ] |
|---|
| 152 | LabelEntry $f2.lei -width 5 -label Index -text 1 |
|---|
| 153 | LabelEntry $f2.lee -width 5 -label End |
|---|
| 154 | LabelEntry $f2.lec -width 50 -label Content -text "{hay que introducir cada} \ |
|---|
| 155 | {fila entre corchetes y} {entre estos los items}" |
|---|
| 156 | checkbutton $f2.cb -text "Allow multiple selection" \ |
|---|
| 157 | -command { toggleSelectModeCmd } |
|---|
| 158 | $f2.cb select |
|---|
| 159 | button $f2.bi -text "Insert" -command { insertCmd } |
|---|
| 160 | button $f2.bd -text "Delete" -command { deleteCmd } |
|---|
| 161 | button $f2.br -text "Reset" -command { resetCmd } |
|---|
| 162 | $f2.bi configure -width 20 |
|---|
| 163 | $f2.bd configure -width 20 |
|---|
| 164 | |
|---|
| 165 | # packing all |
|---|
| 166 | |
|---|
| 167 | grid .top.f1 -row 0 -column 0 -sticky "new" -padx 10 -pady 10 |
|---|
| 168 | grid .top.f2 -row 1 -column 0 -sticky "new" -padx 10 -pady 10 |
|---|
| 169 | grid columnconfigure .top 0 -weight 1 |
|---|
| 170 | grid rowconfigure .top 0 -weight 0 |
|---|
| 171 | grid rowconfigure .top 1 -weight 1 |
|---|
| 172 | |
|---|
| 173 | grid $f1.blb -row 0 -column 0 -sticky "new" |
|---|
| 174 | grid columnconfigure $f1 0 -weight 1 |
|---|
| 175 | grid rowconfigure $f1 0 -weight 1 |
|---|
| 176 | |
|---|
| 177 | grid $f2.lec -row 0 -column 0 -sticky "new" -columnspan 3 |
|---|
| 178 | grid $f2.lei -row 1 -column 0 -sticky "new" |
|---|
| 179 | grid $f2.lee -row 1 -column 1 -sticky "new" |
|---|
| 180 | |
|---|
| 181 | grid $f2.cb -row 1 -column 2 -sticky "new" |
|---|
| 182 | grid $f2.bi -row 2 -column 0 -sticky "new" |
|---|
| 183 | grid $f2.bd -row 2 -column 1 -sticky "new" |
|---|
| 184 | grid $f2.br -row 2 -column 2 -sticky "new" |
|---|
| 185 | grid columnconfigure $f2 0 -weight 0 |
|---|
| 186 | grid columnconfigure $f2 1 -weight 0 |
|---|
| 187 | grid columnconfigure $f2 2 -weight 1 |
|---|
| 188 | grid rowconfigure $f2 0 -weight 0 |
|---|
| 189 | grid rowconfigure $f2 1 -weight 0 |
|---|
| 190 | grid rowconfigure $f2 2 -weight 0 |
|---|
| 191 | |
|---|
| 192 | puts "WIDGET blistoder: $f1.blb" |
|---|
| 193 | |
|---|
| 194 | # commands attached to the widgets |
|---|
| 195 | proc insertCmd {} { |
|---|
| 196 | eval .top.f1.f.blb insert [.top.f2.f.lei cget -text] \ |
|---|
| 197 | [.top.f2.f.lec cget -text] |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | proc deleteCmd {} { |
|---|
| 201 | .top.f1.f.blb delete [.top.f2.f.lei cget -text] \ |
|---|
| 202 | [.top.f2.f.lee cget -text] |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | proc resetCmd {} { |
|---|
| 206 | .top.f1.f.blb clear |
|---|
| 207 | .top.f1.f.blb insert end {1 2 3 4} {one two three four} \ |
|---|
| 208 | {uno dos tres cuatro} {k kk kkk kkkk} {0 0 0 0} {@ # $ %} |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | proc toggleSelectModeCmd {} { |
|---|
| 212 | if { [.top.f1.f.blb cget -selectmode] eq "multiple" } { |
|---|
| 213 | .top.f1.f.blb configure -selectmode single |
|---|
| 214 | } else { |
|---|
| 215 | .top.f1.f.blb configure -selectmode multiple |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | } |
|---|