1namespace eval LibraryMgr {
2  variable libList
3  variable libTitle
4  variable libStatus
5  variable libReqStatus
6  variable w .lib_mgr
7  variable ss_w
8
9  #############################################################################
10  #
11  # Get the title of a library file.
12  #
13  proc gettitle {name} {
14    set f [open $name]
15    if { $f == "" } { return "" }
16
17    while {![eof $f]} {
18      set s [gets $f]
19
20      if {[lindex $s 0] == "//:" && [lindex $s 1] == "property" && [lindex $s 2] == "title"} {
21	close $f
22	return [lindex $s 4]
23      }
24    }
25    close $f
26    return ""
27  }
28
29  #############################################################################
30  #
31  # Scan the library path for the list of available libraries.
32  #
33  proc scanlibs args {
34    global tkg_simVLibPath
35    variable libTitle
36    variable libStatus
37    variable libReqStatus
38
39    set path $tkg_simVLibPath
40    if {[llength $args] > 0} {
41      set path [lindex $args 0]
42    }
43
44    set L {}
45
46    foreach directory $path {
47      set directory [namespace eval :: "eval concat $directory"]
48      foreach file [glob -nocomplain $directory/*.v] {
49	set lib [file rootname [file tail $file]]
50	set title [gettitle $file]
51	lappend L $lib
52	set libTitle($lib) $title
53	set libStatus($lib) [gat_libIsLoaded $lib]
54	set libReqStatus($lib) $libStatus($lib)
55      }
56    }
57    return [lsort -dictionary $L]
58  }
59
60  #############################################################################
61  #
62  # Return a string describing the status of a library.
63  #
64  proc status {lib} {
65    variable libStatus
66    variable libReqStatus
67    set status unknown
68    switch $libStatus($lib):$libReqStatus($lib) {
69      0:0 { set status [m libmgr.status.unload] }
70      1:1 { set status [m libmgr.status.load] }
71      1:0 { set status [m libmgr.status.unloadp] }
72      0:1 { set status [m libmgr.status.loadp] }
73    }
74    return $status
75  }
76
77  #############################################################################
78  #
79  # Mark libraries in the selection as "to be loaded".
80  #
81  proc load {} {
82    variable libReqStatus
83    variable ss_w
84
85    foreach idx [SpreadSheet::getselection $ss_w] {
86      set item [SpreadSheet::get $ss_w $idx]
87      set lib [lindex $item 0]
88      set libReqStatus($lib) 1
89      set item [lreplace $item 2 2 [status $lib]]
90      SpreadSheet::put $ss_w $idx $item
91    }
92  }
93
94  #############################################################################
95  #
96  # Mark libraries in the selection as "to be unloaded".
97  #
98  proc unload {} {
99    variable libReqStatus
100    variable ss_w
101
102    foreach idx [SpreadSheet::getselection $ss_w] {
103      set item [SpreadSheet::get $ss_w $idx]
104      set lib [lindex $item 0]
105      set libReqStatus($lib) 0
106      set item [lreplace $item 2 2 [status $lib]]
107      SpreadSheet::put $ss_w $idx $item
108    }
109  }
110
111  #############################################################################
112  #
113  # Mark all libraries as "to be unloaded".
114  #
115  proc unloadall {} {
116    variable libReqStatus
117    variable ss_w
118
119    set n [SpreadSheet::size $ss_w]
120    for { set idx 0 } { $idx < $n } { incr idx } {
121      set item [SpreadSheet::get $ss_w $idx]
122      set lib [lindex $item 0]
123      set libReqStatus($lib) 0
124      set item [lreplace $item 2 2 [status $lib]]
125      SpreadSheet::put $ss_w $idx $item
126    }
127  }
128
129  #############################################################################
130  #
131  # Commit changes to library status.
132  #
133  proc ok {} {
134    variable libStatus
135    variable libReqStatus
136    variable libList
137    variable w
138
139    set actionname ""
140
141    destroy $w
142
143    set doload 0
144    set dounload 0
145    foreach lib $libList {
146      if { $libReqStatus($lib) == $libStatus($lib) } continue
147
148      if { $libReqStatus($lib) } {
149	set doload 1
150      } else {
151	set dounload 1
152      }
153    }
154
155    if {$doload && !$dounload} {
156      set actionname LoadLibs
157    } elseif {!$doload && $dounload} {
158      set actionname UnloadLibs
159    } elseif {$doload && $dounload} {
160      set actionname ChgLibs
161    } else {
162      set actionname ""
163    }
164
165    if { $actionname != "" } {
166      action $actionname {
167	foreach lib $libList {
168	  if { $libReqStatus($lib) == $libStatus($lib) } continue
169
170	  if { $libReqStatus($lib) } {
171	    gat_loadLibrary $lib
172	  } else {
173	    gat_unloadLibrary $lib
174	  }
175	}
176      }
177    }
178  }
179
180  #############################################################################
181  #
182  # Cancel any changes to library status.
183  #
184  proc cancel {} {
185    variable w
186    destroy $w
187  }
188
189  proc manager {cmd args} {
190    switch $cmd {
191      canenter {
192	return 0
193      }
194    }
195  }
196
197
198  #############################################################################
199  #
200  # Post the library manager.
201  #
202  proc post {} {
203    variable w
204    variable ss_w
205    variable libList
206    variable libTitle
207    variable libStatus
208    variable libReqStatus
209    global simOn
210
211    if {$simOn} {
212      tk_messageBox -type ok -icon warning -message [m libmgr.notedit]
213      return
214    }
215
216    if {[catch {toplevel $w}]} return
217    wm resizable $w 0 0
218    wm title $w "TKGate: Library Manager"
219    wm geometry $w [offsetgeometry . 50 50]
220    wm transient $w .
221
222    set libList [scanlibs]
223
224    okcancel $w.ok -okcommand LibraryMgr::ok -cancelcommand LibraryMgr::cancel
225    pack $w.ok -side bottom -fill both
226
227    frame $w.main -bd 2 -relief raised
228    pack $w.main -fill both
229
230    dialogImage $w.main.image -image [gifI general.gif] -caption  [m libmgr.cap] -explaination [m libmgr.exp]
231    pack $w.main.image -side left
232
233    frame $w.main.g
234    SpreadSheet::create $w.main.g.lb  -height 13 -bd 2 -relief sunken -yscrollcommand "$w.main.g.vb set"  \
235	-dograb 0 -entrycommand LibraryMgr::manager
236    scrollbar $w.main.g.vb -orient vertical -command "SpreadSheet::yview  $w.main.g.lb"
237    SpreadSheet::addcolumn $w.main.g.lb -width 15 -header [m libmgr.library]
238    SpreadSheet::addcolumn $w.main.g.lb -width 50 -header [m libmgr.description]
239    SpreadSheet::addcolumn $w.main.g.lb -width 15 -header [m libmgr.status]
240    set ss_w $w.main.g.lb
241
242    foreach item $libList {
243      SpreadSheet::insert $ss_w end [list $item $libTitle($item) [status $item]]
244    }
245
246    pack $w.main.g.lb $w.main.g.vb -side left -fill y
247    pack $w.main.g -padx 15 -pady 15
248
249    frame $w.main.bb
250    button $w.main.bb.load -text [m libmgr.load] -command LibraryMgr::load
251    button $w.main.bb.unload -text [m libmgr.unload] -command LibraryMgr::unload
252    button $w.main.bb.unloadall -text [m libmgr.unloadall] -command LibraryMgr::unloadall
253    pack $w.main.bb.load $w.main.bb.unload $w.main.bb.unloadall -side left -padx 5 -pady 5
254    pack $w.main.bb -anchor e
255  }
256}
257
258