File: TSearch.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (259 lines) | stat: -rw-r--r-- 6,253 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# TSearch.tcl
#
#       Megawidget for searching a TreeCtrl widget.
#       Guard against dynamic changes in TreeCtrl. Must be 100% bullet proof!
#       
# Copyright (c) 2007-2008 Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#       
# $Id: TSearch.tcl,v 1.10 2008-08-11 07:39:15 matben Exp $

package require snit 1.0
package require tileutils

package provide UI::TSearch 1.0

namespace eval UI::TSearch {
    
}

interp alias {} UI::TSearch    {} UI::TSearch::widget

# UI::TSearch --
# 
#       Search treectrl megawidget.

snit::widgetadaptor UI::TSearch::widget {
    
    # Widget paths.
    variable T
    variable wentry
    variable wnext
    # Search column.
    variable column
    # Textvariable string in entry widget.
    variable string      ""
    # Item that is the currently found one.
    variable icurrent     ""
    # List of items from a find operation.
    variable ifound  [list]
    # List of {item isopen ...} for all ancestors of the current item which is
    # needed to restore its ancestors open states when picking next item.
    variable buttonStates [list]
     
    delegate method * to hull
    delegate option * to hull 
    
    option -nextstyle    -default Small.Url
    option -closecommand -default [list]

    constructor {_T _column args} {
	
	set T $_T
	set column $_column
	set wentry $win.entry
	set wnext $win.next

	installhull using ttk::frame -class TSearch
	$self configurelist $args

	set im  [::Theme::FindIconSize 16 close-aqua]
	set ima [::Theme::FindIconSize 16 close-aqua-active]

	ttk::label $win.find -style Small.TLabel -padding {4 0 0 0}  \
	  -text [mc "Search"]:
	ttk::entry $win.entry -style Small.Search.TEntry -font CociSmallFont \
	  -textvariable [myvar string]
	ttk::button $win.next -style $options(-nextstyle) \
	  -command [list $self Next] \
	  -text [mc "Next"] -takefocus 0
	ttk::button $win.close -style Plain  \
	  -image [list $im active $ima] -compound image  \
	  -command [list $self Close]
	
	grid  $win.find  $win.entry  $win.next  $win.close
	grid $win.entry -sticky ew
	grid $win.next -padx 4
	grid columnconfigure $win 1 -weight 1
	
	$wnext state {disabled}
	focus $wentry
	trace add variable [myvar string] write [list $self Trace]	

	bind $wentry <Return>        [list $self Find]
	bind $wentry <KeyPress-Up>   [list $self Previous]
	bind $wentry <KeyPress-Down> [list $self Next]
	
	return
    }
    
    destructor {
	trace remove variable [myvar string] write [list $self Trace]
    }

    method Trace {name1 name2 op} {
	$self Reset
    }
    
    method Clear {} {
	set string ""
	$self Reset
    }
    
    # Reset search state.

    method Reset {} {
	$T selection clear
	$wentry state {!invalid}
	$wnext  state {disabled}
	set ifound [list]
	set icurrent ""
	$self SetButtonStates
	set buttonStates [list]
    }
    
    # Selects an item.

    method Hit {item} {
	if {[$T item id $item] ne ""} {
	    $self GetButtonStates $item
	    
	    set icurrent $item
	    $self SeeItem $item
	    $T selection clear
	    $T selection add $icurrent
	}
    }
    
    # Stores a list {item isopen ...} for all ancestors of 'item'.

    method GetButtonStates {item} {
	
	# Get all ancestors, the last one is always the 'root'.
	set ancestors [$T item ancestors $item]
	set openL [list]
	foreach item $ancestors {
	    lappend openL $item [$T item isopen $item]
	}
	set buttonStates $openL
    }
    
    # Uses the current 'buttonStates' list and recreates each ancestors
    # button state.
    
    method SetButtonStates {} {
	foreach {item isopen} $buttonStates {
	    if {[$T item id $item] ne ""} {
		$T item [expr {$isopen ? "expand" : "collapse"}] $item
	    }
	}
    }
    
    method SeeItem {item} {
	foreach aitem [$T item ancestors $item] {
	    $T item expand $aitem
	}
	$T see $item	
    }
    
    method Find {} {
	$T selection clear
	set itemL [list]
	set ifound [list]
	if {[string length $string]} {
	    set lstring [string tolower $string]
	    if {$column eq "all"} {
		foreach item [$T item descendants root] {
		    set textL [$T item text $item]
		    if {[lsearch -glob -nocase $textL *${lstring}*] >= 0} {
			lappend itemL $item
		    }
		}		
	    } else {
		foreach item [$T item descendants root] {
		    set text [$T item text $item $column]
		    if {[string match *${lstring}* [string tolower $text]]} {
			lappend itemL $item
		    }
		}
	    }
	}
	set ifound $itemL
	set len [llength $itemL]
	if {$len > 1} {
	    $wnext state {!disabled}
	} elseif {$len == 0} {
	    $wentry state {invalid}
	    $wnext  state {disabled}
	}
	if {$len} {
	    $self Hit [lindex $itemL 0]
	} else {
	    set icurrent ""
	}
    }
    
    method GetNext {} {
	set item [lsearch -inline $ifound $icurrent]
	if {[string length $item]} {
	    if {[$T item id $item] ne "")} {
		return $item
	    } else {
		set idx [lsearch $ifound $icurrent]
		set ifound [lreplace $ifound $idx $idx]
		return ""
	    }
	} else {
	    return ""
	}
    }

    method Next {} {
	$self SetButtonStates
	$T selection clear
	set ind [lsearch $ifound $icurrent]
	if {$ind >= 0} {
	    if {[expr {$ind+1}] == [llength $ifound]} {
		set ind 0
	    } else {
		incr ind
	    }
	    $self Hit [lindex $ifound $ind]
	}
    }

    method Previous {} {
	$self SetButtonStates
	$T selection clear
	set ind [lsearch $ifound $icurrent]
	if {$ind >= 0} {
	    if {$ind == 0} {
		set ind [expr {[llength $ifound] - 1}]
	    } else {
		incr ind -1
	    }
	    $self Hit [lindex $ifound $ind]
	}
    }
    
    method Close {} {
	if {[llength $options(-closecommand)]} {
	    uplevel $options(-closecommand)
	} else {
	    destroy $win
	}
    }
}