New button "Search".
[ir-tcl-moved-to-github.git] / client.tcl
1 #
2 # $Log: client.tcl,v $
3 # Revision 1.16  1995-03-31 08:56:36  adam
4 # New button "Search".
5 #
6 # Revision 1.15  1995/03/28  12:45:22  adam
7 # New ir method failback: called on disconnect/protocol error.
8 # New ir set/get method: protocol: SR / Z3950.
9 # Simple popup and disconnect when failback is invoked.
10 #
11 # Revision 1.14  1995/03/22  16:07:55  adam
12 # Minor changes.
13 #
14 # Revision 1.13  1995/03/21  17:27:26  adam
15 # Short-hand keys in setup.
16 #
17 # Revision 1.12  1995/03/21  13:41:03  adam
18 # Comstack cs_create not used too often. Non-blocking connect.
19 #
20 # Revision 1.11  1995/03/21  10:39:06  adam
21 # Diagnostic error message displayed with tkerror.
22 #
23 # Revision 1.10  1995/03/20  15:24:06  adam
24 # Diagnostic records saved on searchResponse.
25 #
26 # Revision 1.9  1995/03/17  18:26:16  adam
27 # Non-blocking i/o used now. Database names popup as cascade items.
28 #
29 # Revision 1.8  1995/03/17  15:45:00  adam
30 # Improved target/database setup.
31 #
32 # Revision 1.7  1995/03/16  17:54:03  adam
33 # Minor changes really.
34 #
35 # Revision 1.6  1995/03/15  19:10:20  adam
36 # Database setup in protocol-setup (rather target setup).
37 #
38 # Revision 1.5  1995/03/15  13:59:23  adam
39 # Minor changes.
40 #
41 # Revision 1.4  1995/03/14  17:32:29  adam
42 # Presentation of full Marc record in popup window.
43 #
44 # Revision 1.3  1995/03/12  19:31:52  adam
45 # Pattern matching implemented when retrieving MARC records. More
46 # diagnostic functions.
47 #
48 # Revision 1.2  1995/03/10  18:00:15  adam
49 # Actual presentation in line-by-line format. RPN query support.
50 #
51 # Revision 1.1  1995/03/09  16:15:07  adam
52 # First presentRequest attempts. Hot-target list.
53 #
54 #
55 set hotTargets {}
56 set hotInfo {}
57 set busy 0
58
59 set profile(Default) {{} {} {210} {} 16384 8192 tcpip {}}
60 set hostid Default
61 set settingsChanged 0
62 set setNo 0
63
64 wm minsize . 300 250
65
66 if {[file readable "~/.tk-c"]} {
67     source "~/.tk-c"
68 }
69
70 proc top-down-window {w} {
71     frame $w.top -relief raised -border 1
72     frame $w.bot -relief raised -border 1
73     
74     pack  $w.top $w.bot -side top -fill both -expand yes
75 }
76
77 proc top-down-ok-cancel {w ok-action g} {
78     frame $w.bot.left -relief sunken -border 1
79     pack $w.bot.left -side left -expand yes -padx 5 -pady 5
80     button $w.bot.left.ok -width 6 -text {Ok} \
81             -command ${ok-action}
82     pack $w.bot.left.ok -expand yes -padx 3 -pady 3
83     button $w.bot.cancel -width 6 -text {Cancel} \
84             -command "destroy $w"
85     pack $w.bot.cancel -side left -expand yes    
86
87     if {$g} {
88         # Grab ...
89         grab $w
90         tkwait window $w
91     }
92 }
93
94 proc show-target {target} {
95     .bot.target configure -text "$target"
96 }
97
98 proc show-busy {v1 v2} {
99     global busy
100     if {$busy != 0} {
101         .bot.status configure -fg $v1
102         after 200 [list show-busy $v2 $v1]
103     }
104 }
105         
106 proc show-status {status b} {
107     global busy
108     global statusbg
109     .bot.status configure -text "$status"
110     .bot.status configure -fg black
111     if {$b != 0} {
112         if {$busy == 0} {
113             set busy $b   
114             show-busy red blue
115         }
116         #        . config -cursor {watch black white}
117     } else {
118         #        . config -cursor {top_left_arrow black white}
119         puts "Normal"
120     }
121     set busy $b
122 }
123
124 proc show-message {msg} {
125     .bot.message configure -text "$msg"
126 }
127
128 proc insertWithTags {w text args} {
129     set start [$w index insert]
130     $w insert insert $text
131     foreach tag [$w tag names $start] {
132         $w tag remove $tag $start insert
133     }
134     foreach i $args {
135         $w tag add $i $start insert
136     }
137 }
138
139 proc show-full-marc {no} {
140     global setNo
141
142     set w .full-marc
143
144     if {[winfo exists $w]} {
145         $w.top.record delete 0.0 end
146         set new 0
147     } else {
148
149         toplevel $w
150
151         wm minsize $w 200 200
152         
153         frame $w.top -relief raised -border 1
154         frame $w.bot -relief raised -border 1
155
156         pack  $w.top -side top -fill both -expand yes
157         pack  $w.bot -fill both
158
159         text $w.top.record -width 60 -height 12 -wrap word \
160                 -yscrollcommand [list $w.top.s set]
161         scrollbar $w.top.s -command [list $w.top.record yview]
162
163         set new 1
164     }
165     incr no
166     
167     set r [z39.$setNo recordMarc $no line * * *]
168
169     $w.top.record tag configure marc-tag -foreground blue
170     $w.top.record tag configure marc-data -foreground black
171     $w.top.record tag configure marc-id -foreground red
172
173     foreach line $r {
174         set tag [lindex $line 0]
175         set indicator [lindex $line 1]
176         set fields [lindex $line 2]
177
178         if {$indicator != ""} {
179             insertWithTags $w.top.record "$tag $indicator" marc-tag
180         } else {
181             insertWithTags $w.top.record "$tag    " marc-tag
182         }
183         foreach field $fields {
184             set id [lindex $field 0]
185             set data [lindex $field 1]
186             if {$id != ""} {
187                 insertWithTags $w.top.record " $id " marc-id
188             }
189             set start [$w.top.record index insert]
190             insertWithTags $w.top.record $data {}
191         }
192         $w.top.record insert end "\n"
193     }
194     if {$new} {
195         bind $w <Return> {destroy .full-marc}
196         
197         pack $w.top.s -side right -fill y
198         pack $w.top.record -expand yes -fill both
199         
200         frame $w.bot.left -relief sunken -border 1
201         pack $w.bot.left -side left -expand yes -padx 5 -pady 5
202         button $w.bot.left.close -width 6 -text {Close} \
203                 -command {destroy .full-marc}
204         pack $w.bot.left.close -expand yes -padx 3 -pady 3
205         button $w.bot.edit -width 6 -text {Edit} \
206                 -command {destroy .full-marc}
207         pack $w.bot.edit -side left -expand yes
208     }
209 }
210
211 proc update-target-hotlist {target} {
212     global hotTargets
213
214     set len [llength $hotTargets]
215     if {$len > 0} {
216         .top.target.m delete 5 [expr 5+[llength $hotTargets]]
217     }
218     set indx [lsearch $hotTargets $target]
219     if {$indx >= 0} {
220         set hotTargets [lreplace $hotTargets $indx $indx]
221     }
222     set hotTargets [linsert $hotTargets 0 $target]
223     set-target-hotlist    
224
225
226 proc set-target-hotlist {} {
227     global hotTargets
228     
229     set i 1
230     foreach target $hotTargets {
231         .top.target.m add command -label "$i $target" -command \
232                 "menu-open-target $target {}"
233         incr i
234         if {$i > 8} {
235              break
236         }
237     }
238 }
239
240 proc menu-open-target {target base} {
241     open-target $target $base
242     update-target-hotlist $target
243 }
244
245 proc define-target-action {} {
246     global profile
247
248     set target [.target-define.top.target.entry get]
249     if {$target == ""} {
250         return
251     }
252     update-target-hotlist $target
253     foreach n [array names profile] {
254         if {$n == $target} {
255             protocol-setup $n
256             return
257         }
258     }
259     set profile($target) $profile(Default)
260     protocol-setup $target
261     destroy .target-define
262 }
263
264 proc fail-response {target} {
265     close-target
266     tkerror "Target connection closed or protocol error"
267 }
268
269 proc connect-response {target} {
270     puts "connect-response"
271     show-target $target
272     init-request
273 }
274
275 proc open-target {target base} {
276     global profile
277     global hostid
278
279     z39 disconnect
280     z39 comstack [lindex $profile($target) 6]
281     # z39 idAuthentication [lindex $profile($target) 3]
282     z39 maximumRecordSize [lindex $profile($target) 4]
283     z39 preferredMessageSize [lindex $profile($target) 5]
284     puts -nonewline "maximumRecordSize="
285     puts [z39 maximumRecordSize]
286     puts -nonewline "preferredMessageSize="
287     puts [z39 preferredMessageSize]
288     if {$base == ""} {
289         z39 databaseNames [lindex [lindex $profile($target) 7] 0]
290     } else {
291         z39 databaseNames $base
292     }
293     z39 failback [list fail-response $target]
294     z39 callback [list connect-response $target]
295     z39 connect [lindex $profile($target) 1]:[lindex $profile($target) 2]
296     show-status {Connecting} 1
297     set hostid $target
298     .top.target.m disable 0
299     .top.target.m enable 1
300 }
301
302 proc close-target {} {
303     global hostid
304
305     set hostid Default
306     z39 disconnect
307     show-target {None}
308     show-status {Not connected} 0
309     show-message {}
310     .top.target.m disable 1
311     .top.target.m enable 0
312 }
313
314 proc load-set-action {} {
315     global setNo
316
317     incr setNo
318     ir-set z39.$setNo
319
320     set fname [.load-set.top.filename.entry get]
321     destroy .load-set
322     if {$fname != ""} {
323         init-title-lines
324
325         show-status {Loading} 1
326         z39.$setNo loadFile $fname
327
328         set no [z39.$setNo numberOfRecordsReturned]
329         add-title-lines $setNo $no 1
330     }
331     show-status {Ready} 0
332 }
333
334 proc load-set {} {
335     set w .load-set
336
337     set oldFocus [focus]
338     toplevel $w
339
340     place-force $w .
341
342     top-down-window $w
343
344     frame $w.top.filename
345     
346     pack $w.top.filename -side top -anchor e -pady 2
347     
348     entry-fields $w.top {filename} \
349             {{Filename:}} \
350             {load-set-action} {destroy .load-set}
351     
352     top-down-ok-cancel $w {load-set-action} 1
353     focus $oldFocus
354 }
355
356 proc init-request {} {
357     global setNo
358     
359     z39 callback {init-response}
360     z39 init
361     show-status {Initializing} 1
362 }
363
364 proc init-response {} {
365     show-status {Ready} 0
366     bind .mid.searchentry <Return> search-request
367     focus .mid.searchentry
368 }
369
370 proc search-request {} {
371     global setNo
372     global profile
373     global hostid
374
375     set target $hostid
376
377     incr setNo
378     ir-set z39.$setNo
379
380     if {[lindex $profile($target) 10]} {
381         z39.$setNo setName $setNo
382     }
383     if {[lindex $profile($target) 8]} {
384         z39 query rpn
385     }
386     if {[lindex $profile($target) 9]} {
387         z39 query ccl
388     }
389     z39 callback {search-response}
390     z39.$setNo search [.mid.searchentry get]
391     show-status {Search} 1
392 }
393
394 proc search-response {} {
395     global setNo
396     global setOffset
397     global setMax
398
399     init-title-lines
400     show-status {Ready} 0
401     show-message "[z39.$setNo resultCount] hits"
402     set setMax [z39.$setNo resultCount]
403     puts $setMax
404     if {$setMax == 0} {
405         set status [z39.$setNo responseStatus]
406         if {[lindex $status 0] == "NSD"} {
407             set code [lindex $status 1]
408             set msg [lindex $status 2]
409             set addinfo [lindex $status 3]
410             tkerror "NSD$code: $msg: $addinfo"
411         }
412         return
413     }
414     if {$setMax > 4} {
415         set setMax 4
416     }
417     z39 callback {present-response}
418     set setOffset 1
419     z39.$setNo present $setOffset $setMax
420     show-status {Retrieve} 1
421 }
422
423 proc present-more {number} {
424     global setNo
425     global setOffset
426     global setMax
427
428     puts "present-more"
429     set max [z39.$setNo resultCount]
430     if {$max <= $setMax} {
431         return
432     }
433     puts "max=$max"
434     puts "setOffset=$setOffset"
435     if {$number == ""} {
436         set setMax $max
437     } else {
438         incr setMax $number
439     }
440     z39 callback {present-response}
441     z39.$setNo present $setOffset [expr $setMax - $setOffset + 1]
442     show-status {Retrieve} 1
443 }
444
445 proc init-title-lines {} {
446     .data.list delete 0 end
447 }
448
449 proc add-title-lines {setno no offset} {
450     for {set i 0} {$i < $no} {incr i} {
451         set o [expr $i + $offset]
452         set title [lindex [z39.$setno recordMarc $o field 245 * a] 0]
453         set year  [lindex [z39.$setno recordMarc $o field 260 * c] 0]
454         set nostr [format "%3d" $o]
455         .data.list insert end "$nostr $title - $year"
456     }
457 }
458
459 proc present-response {} {
460     global setNo
461     global setOffset
462     global setMax
463
464     puts "In present-response"
465     set no [z39.$setNo numberOfRecordsReturned]
466     puts "Returned $no records, setOffset $setOffset"
467     add-title-lines $setNo $no $setOffset
468     set setOffset [expr $setOffset + $no]
469     set status [z39.$setNo responseStatus]
470     if {[lindex $status 0] == "NSD"} {
471         show-status {Ready} 0
472         set code [lindex $status 1]
473         set msg [lindex $status 2]
474         set addinfo [lindex $status 3]
475         tkerror "NSD$code: $msg: $addinfo"
476         return
477     }
478     if {$no > 0 && $setOffset <= $setMax} {
479         z39.$setNo present $setOffset [expr $setMax - $setOffset + 1]
480     } else {
481         show-status {Finished} 0
482     }
483 }
484
485 proc left-cursor {w} {
486     set i [$w index insert]
487     if {$i > 0} {
488         incr i -1
489         $w icursor $i
490     }
491 }
492
493 proc right-cursor {w} {
494     set i [$w index insert]
495     incr i
496     $w icursor $i
497 }
498
499 proc bind-fields {list returnAction escapeAction} {
500     set max [expr [llength $list]-1]
501     for {set i 0} {$i < $max} {incr i} {
502         bind [lindex $list $i] <Return> $returnAction
503         bind [lindex $list $i] <Escape> $escapeAction
504         bind [lindex $list $i] <Tab> [list focus [lindex $list [expr $i+1]]]
505         bind [lindex $list $i] <Left> [list left-cursor [lindex $list $i]]
506         bind [lindex $list $i] <Right> [list right-cursor [lindex $list $i]]
507     }
508     bind [lindex $list $i] <Return> $returnAction
509     bind [lindex $list $i] <Escape> $escapeAction
510     bind [lindex $list $i] <Tab>    [list focus [lindex $list 0]]
511     bind [lindex $list $i] <Left> [list left-cursor [lindex $list $i]]
512     bind [lindex $list $i] <Right> [list right-cursor [lindex $list $i]]
513     focus [lindex $list 0]
514 }
515
516 proc entry-fields {parent list tlist returnAction escapeAction} {
517     set alist {}
518     set i 0
519     foreach field $list {
520         set label ${parent}.${field}.label
521         set entry ${parent}.${field}.entry
522         label $label -text [lindex $tlist $i] -anchor e
523         entry $entry -width 32 -relief sunken
524         pack $label -side left
525         pack $entry -side right
526         lappend alist $entry
527         incr i
528     }
529     bind-fields $alist $returnAction $escapeAction
530 }
531
532 proc define-target-dialog {} {
533     set w .target-define
534
535     toplevel $w
536
537     place-force $w .
538
539     top-down-window $w
540
541     frame $w.top.target
542
543     pack $w.top.target \
544             -side top -anchor e -pady 2 
545
546     entry-fields $w.top {target} \
547             {{Target:}} \
548             {define-target-action} {destroy .target-define}
549     
550     top-down-ok-cancel $w {define-target-action} 1
551 }
552
553 proc protocol-setup-action {target} {
554     global profile
555     global csRadioType
556     global settingsChanged
557     global RPNCheck
558     global CCLCheck
559     global ResultSetCheck
560
561     set w .setup-${target}.top
562
563     #set w .protocol-setup.top
564     
565     set b {}
566     set settingsChanged 1
567     set len [$w.databases.list size]
568     for {set i 0} {$i < $len} {incr i} {
569         lappend b [$w.databases.list get $i]
570     }
571     set profile($target) [list [$w.description.entry get] \
572             [$w.host.entry get] \
573             [$w.port.entry get] \
574             [$w.idAuthentication.entry get] \
575             [$w.maximumRecordSize.entry get] \
576             [$w.preferredMessageSize.entry get] \
577             $csRadioType \
578             $b \
579             $RPNCheck \
580             $CCLCheck \
581             $ResultSetCheck ]
582
583     cascade-target-list
584     puts $profile($target)
585     destroy .setup-${target}
586 }
587
588
589 proc place-force {window parent} {
590     set g [wm geometry $parent]
591
592     set p1 [string first + $g]
593     set p2 [string last + $g]
594
595     set x [expr 40+[string range $g [expr $p1 +1] [expr $p2 -1]]]
596     set y [expr 60+[string range $g [expr $p2 +1] end]]
597     wm geometry $window +${x}+${y}
598 }
599
600
601 proc add-database-action {target} {
602     set w .setup-${target}
603     
604     ${w}.top.databases.list insert end \
605             [.database-select.top.database.entry get]
606     destroy .database-select
607 }
608
609 proc add-database {target} {
610     set w .database-select
611
612     set oldFocus [focus]
613     toplevel $w
614
615     place-force $w .setup-${target}
616
617     top-down-window $w
618
619     frame $w.top.database
620
621     pack $w.top.database -side top -anchor e -pady 2
622     
623     entry-fields $w.top {database} \
624             {{Database to add:}} \
625             [list add-database-action $target] {destroy .database-select}
626
627     top-down-ok-cancel $w [list add-database-action $target] 1
628     focus $oldFocus
629 }
630
631 proc delete-database {target} {
632     set w .setup-${target}
633     
634     foreach i [lsort -decreasing \
635             [$w.top.databases.list curselection]] {
636         $w.top.databases.list delete $i
637     }
638 }
639
640 proc protocol-setup {target} {
641     set w .setup-$target
642
643     global profile
644     global csRadioType
645     global RPNCheck
646     global CCLCheck
647     global ResultSetCheck
648
649     toplevel $w
650
651     wm title $w "Setup $target"
652     place-force $w .
653
654     top-down-window $w
655     
656     if {$target == ""} {
657         set target Default
658     }
659     puts target
660     puts $profile($target)
661
662     frame $w.top.host
663     frame $w.top.port
664     frame $w.top.description
665     frame $w.top.idAuthentication
666     frame $w.top.maximumRecordSize
667     frame $w.top.preferredMessageSize
668     frame $w.top.cs-type -relief ridge -border 2
669     frame $w.top.query -relief ridge -border 2
670     frame $w.top.databases -relief ridge -border 2
671
672     # Maximum/preferred/idAuth ...
673     pack $w.top.description $w.top.host $w.top.port \
674             $w.top.idAuthentication $w.top.maximumRecordSize \
675             $w.top.preferredMessageSize -side top -anchor e -pady 2
676     
677     entry-fields $w.top {description host port idAuthentication \
678             maximumRecordSize preferredMessageSize} \
679             {{Description:} {Host:} {Port:} {Id Authentification:} \
680             {Maximum Record Size:} {Preferred Message Size:}} \
681             [list protocol-setup-action $target] [list destroy $w]
682     
683     foreach sub {description host port idAuthentication \
684             maximumRecordSize preferredMessageSize} {
685         puts $sub
686         bind $w.top.$sub.entry <Control-a> "add-database $target"
687         bind $w.top.$sub.entry <Control-d> "delete-database $target"
688     }
689     $w.top.description.entry insert 0 [lindex $profile($target) 0]
690     $w.top.host.entry insert 0 [lindex $profile($target) 1]
691     $w.top.port.entry insert 0 [lindex $profile($target) 2]
692     $w.top.idAuthentication.entry insert 0 [lindex $profile($target) 3]
693     $w.top.maximumRecordSize.entry insert 0 [lindex $profile($target) 4]
694     $w.top.preferredMessageSize.entry insert 0 [lindex $profile($target) 5]
695     set csRadioType [lindex $profile($target) 6]
696     set RPNCheck [lindex $profile($target) 8]
697     set CCLCheck [lindex $profile($target) 9]
698     set ResultSetCheck [lindex $profile($target) 10]
699
700     # Databases ....
701     pack $w.top.databases -side left -pady 6 -padx 6 -expand yes -fill x
702
703     label $w.top.databases.label -text "Databases"
704     button $w.top.databases.add -text "Add" \
705             -command "add-database $target"
706     button $w.top.databases.delete -text "Delete" \
707             -command "delete-database $target"
708     listbox $w.top.databases.list -geometry 20x6 \
709             -yscrollcommand "$w.top.databases.scroll set"
710     scrollbar $w.top.databases.scroll -orient vertical -border 1
711     pack $w.top.databases.label -side top -fill x \
712             -padx 2 -pady 2
713     pack $w.top.databases.add $w.top.databases.delete -side top -fill x \
714             -padx 2 -pady 2
715     pack $w.top.databases.list -side left -fill both -expand yes \
716             -padx 2 -pady 2
717     pack $w.top.databases.scroll -side right -fill y \
718             -padx 2 -pady 2
719     $w.top.databases.scroll config -command "$w.top.databases.list yview"
720
721     foreach b [lindex $profile($target) 7] {
722         $w.top.databases.list insert end $b
723     }
724
725     # Transport ...
726     pack $w.top.cs-type -pady 6 -padx 6 -side top
727     
728     label $w.top.cs-type.label -text "Transport" 
729     radiobutton $w.top.cs-type.tcpip -text "TCP/IP" \
730             -command {puts tcp/ip} -variable csRadioType -value tcpip
731     radiobutton $w.top.cs-type.mosi -text "MOSI" \
732             -command {puts mosi} -variable csRadioType -value mosi
733     
734     pack $w.top.cs-type.label $w.top.cs-type.tcpip $w.top.cs-type.mosi \
735             -padx 4 -side top -fill x
736
737     # Query ...
738     pack $w.top.query -pady 6 -padx 6 -side top
739
740     label $w.top.query.label -text "Query support" -anchor e
741     checkbutton $w.top.query.c1 -text "RPN query" -variable RPNCheck
742     checkbutton $w.top.query.c2 -text "CCL query" -variable CCLCheck
743     checkbutton $w.top.query.c3 -text "Result sets" -variable ResultSetCheck
744
745     pack $w.top.query.label -side top 
746     pack $w.top.query.c1 $w.top.query.c2 $w.top.query.c3 \
747             -padx 4 -side top -fill x
748
749     # Ok-cancel
750     top-down-ok-cancel $w [list protocol-setup-action $target] 0
751 }
752
753 proc database-select-action {} {
754     set w .database-select.top
755     set b {}
756     foreach indx [$w.databases.list curselection] {
757         lappend b [$w.databases.list get $indx]
758     }
759     if {$b != ""} {
760         z39 databaseNames $b
761     }
762     destroy .database-select
763 }
764
765 proc database-select {} {
766     set w .database-select
767     global profile
768     global hostid
769
770     toplevel $w
771
772     place-force $w .
773
774     top-down-window $w
775
776     frame $w.top.databases -relief ridge -border 2
777
778     pack $w.top.databases -side left -pady 6 -padx 6 -expand yes -fill x
779
780     label $w.top.databases.label -text "List"
781     listbox $w.top.databases.list -geometry 20x6 \
782             -yscrollcommand "$w.top.databases.scroll set"
783     scrollbar $w.top.databases.scroll -orient vertical -border 1
784     pack $w.top.databases.label -side top -fill x \
785             -padx 2 -pady 2
786     pack $w.top.databases.list -side left -fill both -expand yes \
787             -padx 2 -pady 2
788     pack $w.top.databases.scroll -side right -fill y \
789             -padx 2 -pady 2
790     $w.top.databases.scroll config -command "$w.top.databases.list yview"
791
792     foreach b [lindex $profile($hostid) 7] {
793         $w.top.databases.list insert end $b
794     }
795     top-down-ok-cancel $w {database-select-action} 1
796 }
797
798 proc cascade-target-list {} {
799     global profile
800     
801     foreach sub [winfo children .top.target.m.clist] {
802         puts "deleting $sub"
803         destroy $sub
804     }
805     .top.target.m.clist delete 0 last
806     foreach n [array names profile] {
807         if {$n != "Default"} {
808             set nl [string tolower $n]
809             if {[llength [lindex $profile($n) 7]] > 1} {
810                 .top.target.m.clist add cascade -label $n \
811                         -menu .top.target.m.clist.$nl
812                 menu .top.target.m.clist.$nl
813                 foreach b [lindex $profile($n) 7] {
814                     .top.target.m.clist.$nl add command -label $b \
815                             -command "menu-open-target $n $b"
816                 }
817             } else {
818                 .top.target.m.clist add command -label $n \
819                         -command "menu-open-target $n {}"
820             }
821         }
822     }
823     .top.target.m.slist delete 0 last
824     foreach n [array names profile] {
825         if {$n != "Default"} {
826             .top.target.m.slist add command -label $n \
827                     -command "protocol-setup $n"
828         }
829     }
830 }
831
832 proc save-settings {} {
833     global hotTargets 
834     global profile
835     global settingsChanged
836
837     set f [open "~/.tk-c" w]
838     puts $f "# Setup file"
839     puts $f "set hotTargets \{ $hotTargets \}"
840
841     foreach n [array names profile] {
842         puts -nonewline $f "set profile($n) \{"
843         puts -nonewline $f $profile($n)
844         puts $f "\}"
845     }
846     close $f
847     set settingsChanged 0
848 }
849
850 proc alert {ask} {
851     set w .alert
852
853     global alertAnswer
854
855     toplevel $w
856     place-force $w .
857     top-down-window $w
858
859     message $w.top.message -text $ask
860
861     pack $w.top.message -side left -pady 6 -padx 20 -expand yes -fill x
862   
863     set alertAnswer 0
864     top-down-ok-cancel $w {alert-action} 1
865     return $alertAnswer
866 }
867
868 proc alert-action {} {
869     global alertAnswer
870     set alertAnswer 1
871     destroy .alert
872 }
873
874 proc exit-action {} {
875     global settingsChanged
876
877     if {$settingsChanged} {
878         set a [alert "you havent saved your settings. Do you wish to save?"]
879         if {$a} {
880             save-settings
881         }
882     }
883     destroy .
884 }
885
886 frame .top  -border 1 -relief raised
887 frame .mid  -border 1 -relief raised
888 frame .data -border 1 -relief raised
889 frame .bot  -border 1 -relief raised
890 pack .top .mid -side top -fill x
891 pack .data      -side top -fill both -expand yes
892 pack .bot      -fill x
893
894 menubutton .top.file -text "File" -underline 0 -menu .top.file.m
895 menu .top.file.m
896 .top.file.m add command -label "Save settings" -command {save-settings}
897 .top.file.m add command -label "Load Set" -command {load-set}
898 .top.file.m add separator
899 .top.file.m add command -label "Exit" -command {exit-action}
900
901 menubutton .top.target -text "Target" -underline 0 -menu .top.target.m
902 menu .top.target.m
903 .top.target.m add cascade -label "Connect" -menu .top.target.m.clist
904 .top.target.m add command -label "Disconnect" -command {close-target}
905 #.top.target.m add command -label "Initialize" -command {init-request}
906 .top.target.m add cascade -label "Setup" -menu .top.target.m.slist
907 .top.target.m add command -label "Setup new" -command {define-target-dialog}
908 .top.target.m add separator
909 set-target-hotlist
910
911 .top.target.m disable 1
912
913 menu .top.target.m.clist
914 menu .top.target.m.slist
915 cascade-target-list
916
917 menubutton .top.search -text "Search" -underline 0 -menu .top.search.m
918 menu .top.search.m
919 .top.search.m add command -label "Database" -command {database-select}
920 .top.search.m add cascade -label "Query type" -menu .top.search.m.querytype
921 menu .top.search.m.querytype
922 .top.search.m.querytype add radiobutton -label "RPN"
923 .top.search.m.querytype add radiobutton -label "CCL"
924 .top.search.m add cascade -label "Present" -menu .top.search.m.present
925 menu .top.search.m.present
926 .top.search.m.present add command -label "More" -command [list present-more 10]
927 .top.search.m.present add command -label "All" -command [list present-more {}]
928
929 menubutton .top.help -text "Help" -menu .top.help.m
930 menu .top.help.m
931
932 .top.help.m add command -label "Help on help" -command {puts "Help on help"}
933 .top.help.m add command -label "About" -command {puts "About"}
934
935 pack .top.file .top.target .top.search -side left
936 pack .top.help -side right
937
938 label .mid.searchlabel -text {Search:}
939 entry .mid.searchentry -width 32 -relief sunken
940 pack .mid.searchlabel  -side left
941 pack .mid.searchentry -side left -fill x -expand yes
942
943 focus .mid.searchentry
944 bind .mid.searchentry <Left> {left-cursor .mid.searchentry}
945 bind .mid.searchentry <Right> {right-cursor .mid.searchentry}
946
947 listbox .data.list -yscrollcommand {.data.scroll set}
948 scrollbar .data.scroll -orient vertical -border 1
949 pack .data.list -side left -fill both -expand yes
950 pack .data.scroll -side right -fill y
951 .data.scroll config -command {.data.list yview}
952
953 message .bot.target -text "None" -aspect 1000 -relief sunken -border 1
954 label .bot.status -text "Not connected" -width 12 -relief \
955         sunken -anchor w -border 1
956 label .bot.set -textvariable setNo -width 5 -relief \
957         sunken -anchor w -border 1
958 label .bot.message -text "" -width 14 -relief \
959         sunken -anchor w -border 1
960 pack .bot.target .bot.status .bot.set .bot.message -anchor nw \
961         -side left -padx 2 -pady 2
962
963 bind .data.list <Double-Button-1> {set indx [.data.list nearest %y]
964 show-full-marc $indx}
965
966 ir z39