source: tol/tolbase/lib/byswidget/bfunctions/bflistbox.tcl

devel
Last change on this file was 6d7779d, checked in by Jorge Pérez <josp.jorge@…>, 18 years ago

initial import

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 7.3 KB
Line 
1#/////////////////////////////////////////////////////////////////////////////
2# FILE    : bflistbox.tcl
3# PURPOSE : This file implements functions to a component listbox
4#/////////////////////////////////////////////////////////////////////////////
5
6#/////////////////////////////////////////////////////////////////////////////
7proc lbDeleteSelection {lb_} {
8# PURPOSE: Delete selected elements
9#
10# PARAMETERS:
11#   lb_ -> List of elements
12#
13#/////////////////////////////////////////////////////////////////////////////
14  set lst [$lb_ curselection]
15  set lst [lreverse $lst]
16  foreach it $lst {
17    $lb_ delete $it
18  }
19}
20
21#/////////////////////////////////////////////////////////////////////////////
22proc lbToggleSelection {lb_} {
23# PURPOSE: Toggle selection elements
24#
25# PARAMETERS:
26#   lb_ -> List of elements
27#
28#/////////////////////////////////////////////////////////////////////////////
29  # controlamos que no se permita seleccion multiple
30  set sm [$lb_ cget -selectmode]
31  if {[string equal $sm multiple] || [string equal $sm extended]} {
32    set sz [$lb_ size]
33    for {set f 0} {$f < $sz} {incr f} {
34      if {[$lb_ selection includes $f]} {
35        $lb_ selection clear $f
36      } else {
37        $lb_ selection set   $f
38      }
39    }
40  }
41}
42
43#/////////////////////////////////////////////////////////////////////////////
44proc lbToClipBoard {lb_ sl unSe {pref ""} {suf ""} {sep "\n"}} {
45# PURPOSE: copy the elements indicates in clipboard
46#
47# PARAMETERS:
48#   lb_  -> List of elements
49#   sl   -> Copy items selections
50#   unSe -> Copy items not selections
51#   pref -> Text to concatenate in the beginning. Default ""
52#   suf  -> Text to concatenate in the end. Default ""
53#   sep  -> Separating text of elements. Default "\n"
54#
55#/////////////////////////////////////////////////////////////////////////////
56  # items to copy
57  set lst [lbGetItems $lb_ $sl $unSe]
58  # copy
59  set str [ListToStr $lst $sep $pref $suf]
60  clipboard clear
61  clipboard append $str
62}
63
64#/////////////////////////////////////////////////////////////////////////////
65proc lbGetItems {lb_ se unSe} {
66# PURPOSE: Obtain one lists with the selected and/or not selected elements
67#
68# PARAMETERS:
69#   lb_  -> List of elements
70#   sl   -> Items selections
71#   unSe -> Items not selections
72#
73# RETURN: List with the elements
74#
75#///////////////////////////////////////////////////////////////////////////// 
76  set lst {}
77  if {$se || $unSe} {
78    if {$se && $unSe} {
79      # all
80      set lst [$lb_ get 0 end]
81    } elseif {$se} {
82      # selections
83      set ls [$lb_ curselection]
84      foreach it $ls {
85        lappend lst [$lb_ get $it]
86      }
87    } elseif {$unSe} {
88      # not selections
89      set sz [$lb_ size]
90      for {set f 0} {$f < $sz} {incr f} {
91        if {![$lb_ selection includes $f]} {
92          lappend lst [$lb_ get $f]
93        }
94      }
95    }
96  }
97  return $lst
98}
99
100#/////////////////////////////////////////////////////////////////////////////
101proc lbCountItems {lb_} {
102# PURPOSE: Information about the elemetos of one lists
103#
104# PARAMETERS:
105#   lb_  -> List of elements
106#
107# RETURN: One list. 1º Total, 2º selections, 3º not selections
108#
109#/////////////////////////////////////////////////////////////////////////////
110  set lon     [$lb_ size]
111  set lSel    [llength [$lb_ curselection]]
112  set lNotSel [expr $lon - $lSel]
113  return [list $lon $lSel $lNotSel]
114}
115
116#/////////////////////////////////////////////////////////////////////////////
117proc lbLoadFromFile {lb_} {
118# PURPOSE: Load the content from listbox in a file
119#
120# PARAMETERS:
121#   lb_  -> List of elements
122#
123#/////////////////////////////////////////////////////////////////////////////
124  # select file
125  set filNam [tk_getOpenFile -parent $lb_ -title [mc "File selector"]]
126  if {$filNam != ""} {
127    # open file to read
128    if { ![ catch { set fd [open $filNam r] } msgerr] } {
129      while { [gets $fd line ] >= 0} {
130        $lb_ insert end $line
131      }
132      close $fd     
133      return 1
134    }
135    error $msgerr
136  }
137}
138
139#/////////////////////////////////////////////////////////////////////////////
140proc lbSaveToFile {lb_ se unSe {pref ""} {suf ""} {sep "\n"}} {
141# PURPOSE: Save the content from listbox in a file
142#
143# PARAMETERS:
144#   lb_  -> List of elements
145#   sl   -> Copy items selections
146#   unSe -> Copy items not selections
147#   pref -> Text to concatenate in the beginning. Default ""
148#   suf  -> Text to concatenate in the end. Default ""
149#   sep  -> Separating text of elements. Default "\n"
150#
151#/////////////////////////////////////////////////////////////////////////////
152  # select file
153  set filNam [tk_getSaveFile -parent $lb_ -title [mc "File selector"]]
154  if {$filNam != ""} {
155     # open file to write
156    if { ![ catch { set fd [open $filNam w] } msgerr] } {
157      # text to save
158      set lst [lbGetItems $lb_ $se $unSe]
159      set str [ListToStr $lst $sep $pref $suf]
160      # write
161      puts -nonewline $fd $str
162      close $fd
163      return 1
164    }
165    error $msgerr
166  }
167}
168
169#/////////////////////////////////////////////////////////////////////////////
170proc lbSelectAll {lb_} {
171# PURPOSE: Selection all
172#
173# PARAMETERS:
174#   lb_  -> List of elements
175#
176#/////////////////////////////////////////////////////////////////////////////
177  # controlamos que no se permita seleccion multiple
178  set sm [$lb_ cget -selectmode]
179  if {[string equal $sm multiple] || [string equal $sm extended]} {
180    $lb_ selection set 0 end 
181  }
182}
183
184#///////////////////////////////////////////////////////////////////////////
185proc lbOnKeyPress {lb key lstkey aftertime diference {col ""}} {
186#
187# PURPOSE: Activa la fila que coincide con las teclas pulsadas. Distinge
188#          entre muysuculas y minusculas
189#
190# PARAMETERS:
191#  lb -> path del lisbox
192#  key -> tecla pulsada
193#  lstkey -> lista de caracteres pulsados anteriormente
194#  aftertime -> momento de la ultima pulsacion
195#  diference -> tiempo maxiom entre pulsaciones
196#  col -> columna por la que realizar el filtrado (solo blistbox+)
197#
198# RETURN: Una lista con los valores actualizados para la lista de caracteres
199#         y el momento de la ultma pulsacion
200#
201# TODO: opciones de componente para un futuro
202#   - case sensitive
203#
204#///////////////////////////////////////////////////////////////////////////
205  if {[expr [string length $key] == 1]} {
206    # time control
207    set now_press_key [clock clicks -milliseconds]
208    set dif [expr $now_press_key - $aftertime]
209    if {$dif <= $diference} {
210      set key $lstkey$key
211    }
212    set aftertime $now_press_key
213    # busqueda en minusculas
214    set lstkey    [string tolower $key]
215    # FIND
216    # widget ?
217    switch [winfo class $lb] {
218      Listbox -
219      Blistbox {       
220        for {set row 0} {$row < [$lb index end]} {incr row} {
221          set r [lsearch [string tolower [$lb get $row]] ${lstkey}*]
222          if {[expr $r > -1]} {
223            $lb selection clear 0 end
224            $lb selection set $r
225            $lb activate $r
226            $lb see      $r           
227            break
228          }           
229        }   
230      }
231      Treeview -
232      Btreeview -
233      Btreeviewplus -
234      Blistboxplus {
235        foreach id [$lb get] {
236          set item [string tolower [$lb get $id $col]]
237          if { "-1" ne [lsearch $item ${lstkey}*] } {
238            $lb selection clearall
239            $lb selection set $id
240            $lb see   $id
241            $lb focus $id
242            break
243          }
244        }
245      }
246    }
247  }
248  update
249  return [list $lstkey $aftertime]
250}
Note: See TracBrowser for help on using the repository browser.