Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
history.tcl
Go to the documentation of this file.
1 
2 ### history.tcl
3 ### Text entry history functions for Scid.
4 ### Copyright (C) 2004 Shane Hudson.
5 
6 namespace eval ::utils::history {}
7 
8 
9 set ::utils::history::defaultListLength 10
10 array set ::utils::history::listLength {}
11 array set ::utils::history::comboboxWidget {}
12 
13 if {! [info exists ::utils::history::listData]} {
14  array set ::utils::history::listData {}
15 }
16 
17 # Load any history lists that were saved in the last session:
18 catch {source [scidConfigFile history]}
19 
20 
21 proc ::utils::history::SetList {key list} {
22  set ::utils::history::listData($key) $list
23 }
24 
25 
26 proc ::utils::history::GetList {key} {
27  variable listData
28  if {[info exists listData($key)]} {
29  return $listData($key)
30  }
31  return {}
32 }
33 
34 
35 # ::utils::history::AddEntry
36 #
37 # Inserts an entry at the top of the list of the specified key and
38 # ensures that only one copy of the entry exists in the list.
39 # The list is then pruned to the limit size is necessary.
40 #
41 proc ::utils::history::AddEntry {key entry} {
42  variable listData
43  # We do not add the empty string to a history list:
44  if {$entry == "" } {
45  return
46  }
47 
48  if {[info exists listData($key)]} {
49  # Take out this entry if it exists, so it will not appear twice:
50  set index [lsearch -exact $listData($key) $entry]
51  if {$index == 0} {
52  # The entry is already at the start of the list; nothing to do
53  return
54  } elseif {$index > 0} {
55  set listData($key) [lreplace $listData($key) $index $index]
56  }
57  set listData($key) [linsert $listData($key) 0 $entry]
59  } else {
60  set listData($key) [list $entry]
61  }
62  RefillCombobox $key
63 
64  if { [llength [GetList $key]] > 0 } {
65  set cb [ GetCombobox $key]
66  if { $cb != "" && [winfo exists $cb]} {
67  $cb current 0
68  }
69  }
70 
71 }
72 
73 
74 proc ::utils::history::SetLimit {key length} {
75  set ::utils::history::listLength($key) $length
77 }
78 
79 
80 proc ::utils::history::GetLimit {key} {
81  variable listLength
82  variable defaultListLength
83  if {[info exists ::utils::history::listLength($key)]} {
84  return $::utils::history::listLength($key)
85  }
86  return $defaultListLength
87 }
88 
89 
90 proc ::utils::history::PruneList {key {length -1}} {
91  variable listData
92  if {! [info exists listData($key)]} { return}
93  if {$length < 0} {
94  set length [::utils::history::GetLimit $key]
95  }
96  set listData($key) [lrange $listData($key) 0 [expr {$length - 1}]]
97 }
98 
99 
100 
101 # ::utils::history::SetCombobox
102 #
103 # Returns the combobox widget associated with this history key.
104 #
105 proc ::utils::history::GetCombobox {key} {
106  variable comboboxWidget
107  if {[info exists comboboxWidget($key)]} {
108  return $comboboxWidget($key)
109  }
110  return ""
111 }
112 
113 
114 # ::utils::history::SetCombobox
115 #
116 # Associates the specified widget (which must be a megawidget created
117 # with ::combobox::combobox, see contrib/combobox.tcl) with the specific
118 # history key. Whenever the list for this history key is modified, the
119 # combobox widget will be updated.
120 #
121 proc ::utils::history::SetCombobox {key cbWidget} {
122  set ::utils::history::comboboxWidget($key) $cbWidget
123  RefillCombobox $key
124 }
125 
126 
127 # ::utils::history::SetCombobox
128 #
129 # Refills the history list of the combobox associated with the specified
130 # history key, if there is one.
131 #
132 proc ::utils::history::RefillCombobox {key} {
133  variable comboboxWidget
134 
135  set cbWidget [GetCombobox $key]
136  if {$cbWidget == ""} { return}
137 
138  # If the combobox widget is part of a dialog which is generated as needed,
139  # it may not exist right now:
140  if {! [winfo exists $cbWidget]} { return}
141 
142  $cbWidget delete 0 end
143  set entries [GetList $key]
144  $cbWidget configure -values $entries
145 }
146 
147 
148 # ::utils::history::Save
149 # Saves the combobox history file, reporting any error in a message box
150 # if reportError is true.
151 #
152 proc ::utils::history::Save {{reportError 0}} {
153  variable listData
154 
155  set f {}
156  set filename [scidConfigFile history]
157 
158  if {[catch {open $filename w} f]} {
159  if {$reportError} {
160  tk_messageBox -title "Scid" -type ok -icon warning \
161  -message "Unable to write file: $filename\n$f"
162  }
163  return
164  }
165 
166  puts $f "# Scid $::scidVersion combobox history lists"
167  puts $f ""
168  foreach i [lsort [array names listData]] {
169  puts $f "set ::utils::history::listData($i) [list $listData($i)]"
170  }
171  close $f
172 }