New presentation format.
[ir-tcl-moved-to-github.git] / client.tcl
1 #
2 # $Log: client.tcl,v $
3 # Revision 1.32  1995-06-07 09:16:37  adam
4 # New presentation format.
5 #
6 # Revision 1.31  1995/06/06  16:31:09  adam
7 # Bug fix: target names couldn't contain blanks.
8 # Bug fix: scan.
9 #
10 # Revision 1.30  1995/06/06  11:35:41  adam
11 # Work on scan. Display of old sets.
12 #
13 # Revision 1.29  1995/06/05  14:11:18  adam
14 # Bug fix in present-more.
15 #
16 # Revision 1.28  1995/06/02  14:52:13  adam
17 # Minor changes really.
18 #
19 # Revision 1.27  1995/06/02  14:29:42  adam
20 # Work on scan interface - up/down buttons.
21 #
22 # Revision 1.26  1995/06/01  16:36:46  adam
23 # About buttons. Minor bug fixes.
24 #
25 # Revision 1.25  1995/05/31  13:09:57  adam
26 # Client searches/presents may be interrupted.
27 # New moving book-logo.
28 #
29 # Revision 1.24  1995/05/31  08:36:24  adam
30 # Bug fix in client.tcl: didn't save options on clientrc.tcl.
31 # New method: referenceId. More work on scan.
32 #
33 # Revision 1.23  1995/05/29  10:33:41  adam
34 # README and rename of startup script.
35 #
36 # Revision 1.22  1995/05/26  11:44:09  adam
37 # Bugs fixed. More work on MARC utilities and queries. Test
38 # client is up-to-date again.
39 #
40 # Revision 1.21  1995/05/11  15:34:46  adam
41 # Scan request changed a bit. This version works with RLG.
42 #
43 # Revision 1.20  1995/04/21  16:31:57  adam
44 # New radiobutton: protocol (z39v2/SR).
45 #
46 # Revision 1.19  1995/04/18  16:11:50  adam
47 # First version of graphical Scan. Some work on query-by-form.
48 #
49 # Revision 1.18  1995/04/10  10:50:22  adam
50 # Result-set name defaults to suffix of ir-set name.
51 # Started working on scan. Not finished at this point.
52 #
53 # Revision 1.17  1995/03/31  09:34:57  adam
54 # Search-button disabled when there is no connection.
55 #
56 # Revision 1.16  1995/03/31  08:56:36  adam
57 # New button "Search".
58 #
59 # Revision 1.15  1995/03/28  12:45:22  adam
60 # New ir method failback: called on disconnect/protocol error.
61 # New ir set/get method: protocol: SR / Z3950.
62 # Simple popup and disconnect when failback is invoked.
63 #
64 # Revision 1.14  1995/03/22  16:07:55  adam
65 # Minor changes.
66 #
67 # Revision 1.13  1995/03/21  17:27:26  adam
68 # Short-hand keys in setup.
69 #
70 # Revision 1.12  1995/03/21  13:41:03  adam
71 # Comstack cs_create not used too often. Non-blocking connect.
72 #
73 # Revision 1.11  1995/03/21  10:39:06  adam
74 # Diagnostic error message displayed with tkerror.
75 #
76 # Revision 1.10  1995/03/20  15:24:06  adam
77 # Diagnostic records saved on searchResponse.
78 #
79 # Revision 1.9  1995/03/17  18:26:16  adam
80 # Non-blocking i/o used now. Database names popup as cascade items.
81 #
82 # Revision 1.8  1995/03/17  15:45:00  adam
83 # Improved target/database setup.
84 #
85 # Revision 1.7  1995/03/16  17:54:03  adam
86 # Minor changes really.
87 #
88 # Revision 1.6  1995/03/15  19:10:20  adam
89 # Database setup in protocol-setup (rather target setup).
90 #
91 # Revision 1.5  1995/03/15  13:59:23  adam
92 # Minor changes.
93 #
94 # Revision 1.4  1995/03/14  17:32:29  adam
95 # Presentation of full Marc record in popup window.
96 #
97 # Revision 1.3  1995/03/12  19:31:52  adam
98 # Pattern matching implemented when retrieving MARC records. More
99 # diagnostic functions.
100 #
101 # Revision 1.2  1995/03/10  18:00:15  adam
102 # Actual presentation in line-by-line format. RPN query support.
103 #
104 # Revision 1.1  1995/03/09  16:15:07  adam
105 # First presentRequest attempts. Hot-target list.
106 #
107 #
108 set hotTargets {}
109 set hotInfo {}
110 set busy 0
111
112 set profile(Default) {{} {} {210} {} 16384 8192 tcpip {} 1 {} {} z39v2}
113 set hostid Default
114 set settingsChanged 0
115 set setNo 0
116 set cancelFlag 0
117 set searchEnable 0
118 set fullMarcSeq 0
119 set displayFormat nice
120
121 set queryTypes {Simple}
122 set queryButtons { { {I 0} {I 1} {I 2} } }
123 set queryInfo { { {Title {1=4}} {Author {1=1}} \
124         {Subject {1=21}} {Any {1=1016}} } }
125
126 wm minsize . 0 0
127
128 if {[file readable "clientrc.tcl"]} {
129     source "clientrc.tcl"
130 }
131
132 set queryButtonsFind [lindex $queryButtons 0]
133 set queryInfoFind [lindex $queryInfo 0]
134
135 proc top-down-window {w} {
136     frame $w.top -relief raised -border 1
137     frame $w.bot -relief raised -border 1
138     
139     pack  $w.top -side top -fill both -expand yes
140     pack  $w.bot -fill both
141 }
142
143 proc top-down-ok-cancel {w ok-action g} {
144     frame $w.bot.left -relief sunken -border 1
145     pack $w.bot.left -side left -expand yes -ipadx 2 -ipady 2 -padx 5 -pady 5
146     button $w.bot.left.ok -width 6 -text {Ok} \
147             -command ${ok-action}
148     pack $w.bot.left.ok -expand yes -ipadx 2 -ipady 2 -padx 3 -pady 3
149     button $w.bot.cancel -width 6 -text {Cancel} \
150             -command "destroy $w"
151     pack $w.bot.cancel -side left -expand yes    
152
153     if {$g} {
154         grab $w
155         tkwait window $w
156     }
157 }
158
159 proc bottom-buttons {w buttonList g} {
160     set i 0
161     set l [llength $buttonList]
162
163     frame $w.bot.$i -relief sunken -border 1
164     pack $w.bot.$i -side left -expand yes -padx 5 -pady 5
165     button $w.bot.$i.ok -text [lindex $buttonList $i] \
166             -command [lindex $buttonList [expr $i+1]]
167     pack $w.bot.$i.ok -expand yes -ipadx 2 -ipady 2 -padx 3 -pady 3 -side left
168
169     incr i 2
170     while {$i < $l} {
171         button $w.bot.$i -text [lindex $buttonList $i] \
172                 -command [lindex $buttonList [expr $i+1]]
173         pack $w.bot.$i -expand yes -ipadx 2 -ipady 2 -padx 3 -pady 3 -side left
174         incr i 2
175     }
176     if {$g} {
177         # Grab ...
178         grab $w
179         tkwait window $w
180     }
181 }
182
183 proc cancel-operation {} {
184     global cancelFlag
185     global busy
186
187     set cancelFlag 1
188     if {$busy} {
189         show-status Cancelled 0 {}
190     }
191 }
192
193 proc show-target {target} {
194     .bot.a.target configure -text "$target"
195 }
196
197 proc show-logo {v1} {
198     global busy
199     if {$busy != 0} {
200         incr v1 -1
201         if {$v1==0} {
202             set v1 9
203         }
204         .bot.logo configure -bitmap @book${v1}
205         after 140 [list show-logo $v1]
206         return
207     }
208     while {1} {
209         .bot.logo configure -bitmap @book1
210         tkwait variable busy
211         if {$busy} {
212             show-logo 1
213             return
214         }
215     }
216 }
217         
218 proc show-status {status b sb} {
219     global busy
220     global searchEnable
221
222     .bot.a.status configure -text "$status"
223     if {$b == 1} {
224         if {$busy == 0} {set busy 1}
225     } else {
226         set busy 0
227     }
228     if {$sb == {}} {
229         return
230     }
231     if {$sb} {
232         .top.service configure -state normal
233         .mid.search configure -state normal
234         .mid.scan configure -state normal
235         .mid.present configure -state normal
236         if {[winfo exists .scan-window]} {
237             .scan-window.bot.2 configure -state normal
238             .scan-window.bot.4 configure -state normal
239         }
240         set searchEnable 1
241     } else {
242         .top.service configure -state disabled
243         .mid.search configure -state disabled
244         .mid.scan configure -state disabled
245         .mid.present configure -state disabled
246
247         if {[winfo exists .scan-window]} {
248             .scan-window.bot.2 configure -state disabled
249             .scan-window.bot.4 configure -state disabled
250         }
251         set searchEnable 0
252     }
253 }
254
255 proc show-message {msg} {
256     .bot.a.message configure -text "$msg"
257 }
258
259 proc insertWithTags {w text args} {
260     set start [$w index insert]
261     $w insert insert $text
262     foreach tag [$w tag names $start] {
263         $w tag remove $tag $start insert
264     }
265     foreach i $args {
266         $w tag add $i $start insert
267     }
268 }
269
270 proc about-target {} {
271     set w .about-target-w
272
273     toplevel $w
274
275     wm title $w "About target"
276     place-force $w .
277     top-down-window $w
278
279     set i [z39 targetImplementationName]
280     label $w.top.in -text "Implementation name: $i"
281     set i [z39 targetImplementationId]
282     label $w.top.ii -text "Implementation id: $i"
283     set i [z39 targetImplementationVersion]
284     label $w.top.iv -text "Implementation version: $i"
285     set i [z39 options]
286     label $w.top.op -text "Protocol options: $i"
287
288     pack $w.top.in $w.top.ii $w.top.iv $w.top.op -side top -anchor nw
289
290     bottom-buttons $w [list {Close} [list destroy $w]] 1
291 }
292
293 proc about-origin {} {
294     set w .about-origin-w
295
296     toplevel $w
297
298     wm title $w "About IrTcl"
299     place-force $w .
300     top-down-window $w
301
302     set i [z39 implementationName]
303     label $w.top.in -text "Implementation name: $i"
304     set i [z39 implementationId]
305     label $w.top.ii -text "Implementation id: $i"
306
307     pack $w.top.in $w.top.ii -side top -anchor nw
308
309     bottom-buttons $w [list {Close} [list destroy $w]] 1
310 }
311
312 proc display-raw {sno no w} {
313     $w delete 0.0 end
314     set r [z39.$sno getMarc $no list * * *]
315     foreach line $r {
316         set tag [lindex $line 0]
317         set indicator [lindex $line 1]
318         set fields [lindex $line 2]
319         
320         if {$indicator != ""} {
321             insertWithTags $w "$tag $indicator" marc-tag
322         } else {
323             insertWithTags $w "$tag    " marc-tag
324         }
325         foreach field $fields {
326             set id [lindex $field 0]
327             set data [lindex $field 1]
328             if {$id != ""} {
329                 insertWithTags $w " $id " marc-id
330             }
331             set start [$w index insert]
332             insertWithTags $w $data {}
333         }
334         $w insert end "\n"
335     }
336 }
337
338 proc display-nice {sno no w} {
339     $w delete 0.0 end
340     set i [z39.$sno getMarc $no field 245 * a]
341     if {$i != ""} {
342         set i [lindex $i 0]
343         insertWithTags $w "Title:      " marc-tag
344         insertWithTags $w $i marc-data
345         set i [z39.$sno getMarc $no field 245 * b]
346         if {$i != ""} {
347             insertWithTags $w [lindex $i 0] marc-data
348         }
349         $w insert end "\n"
350     }
351     set i [z39.$sno getMarc $no field 700 * a]
352     if {$i == ""} {
353         set i [z39.$sno getMarc $no field 100 * a]
354     }
355     if {$i != ""} {
356         if {[llength $i] > 1} {
357             insertWithTags $w "Authors:    " marc-tag
358         } else {
359             insertWithTags $w "Author:     " marc-tag
360         }
361         foreach x $i {
362             insertWithTags $w $x marc-data
363         }
364         $w insert end "\n"
365     }
366     set i [z39.$sno getMarc $no field 110 * *]
367     if {$i != ""} {
368         insertWithTags $w "Co-Author:  " marc-tag
369         foreach x $i {
370             insertWithTags $w $x marc-data
371         }
372         $w insert end "\n"
373     }
374
375     set i [z39.$sno getMarc $no field 650 * *]
376     if {$i != ""} {
377         set n 0
378         insertWithTags $w "Keywords:   " marc-tag
379         foreach x $i {
380             if {$n > 0} {
381                 $w insert end ", "
382             }
383             insertWithTags $w $x marc-data
384             incr n
385         }
386         $w insert end "\n"
387     }
388     set i [concat [z39.$sno getMarc $no field 260 * a] \
389             [z39.$sno getMarc $no field 260 * b]]
390     if {$i != ""} {
391         insertWithTags $w "Publisher:  " marc-tag
392         foreach x $i {
393             insertWithTags $w $x marc-data
394         }
395         $w insert end "\n"
396     }
397     set i [z39.$sno getMarc $no field 020 * a]
398     if {$i != ""} {
399         insertWithTags $w "ISBN:       " marc-tag
400         foreach x $i {
401             insertWithTags $w $x marc-data
402         }
403         $w insert end "\n"
404     }
405     set i [z39.$sno getMarc $no field 022 * a]
406     if {$i != ""} {
407         insertWithTags $w "ISSN:       " marc-tag
408         foreach x $i {
409             insertWithTags $w $x marc-data
410         }
411         $w insert end "\n"
412     }
413     set i [z39.$sno getMarc $no field 030 * a]
414     if {$i != ""} {
415         insertWithTags $w "CODEN:      " marc-tag
416         foreach x $i {
417             insertWithTags $w $x marc-data
418         }
419         $w insert end "\n"
420     }
421     set i [z39.$sno getMarc $no field 015 * a]
422     if {$i != ""} {
423         insertWithTags $w "Ctl number: " marc-tag
424         foreach x $i {
425             insertWithTags $w $x marc-data
426         }
427         $w insert end "\n"
428     }
429     set i [z39.$sno getMarc $no field 010 * a]
430     if {$i != ""} {
431         insertWithTags $w "LC number:  " marc-tag
432         foreach x $i {
433             insertWithTags $w $x marc-data
434         }
435         $w insert end "\n"
436     }
437 }
438
439 proc show-full-marc {sno no b} {
440     global fullMarcSeq
441     global displayFormat
442
443     if {[z39.$sno type $no] != "DB"} {
444         return
445     }
446     if {$b} {
447         set w .full-marc-$fullMarcSeq
448         incr fullMarcSeq
449     } else {
450         set w .full-marc
451     }
452     if {[winfo exists $w]} {
453         set new 0
454     } else {
455
456         toplevel $w
457
458         wm minsize $w 0 0
459         
460         frame $w.top -relief raised -border 1
461         frame $w.bot -relief raised -border 1
462
463         pack  $w.top -side top -fill both -expand yes
464         pack  $w.bot -fill both
465
466         text $w.top.record -width 60 -height 12 -wrap word \
467                 -yscrollcommand [list $w.top.s set]
468         scrollbar $w.top.s -command [list $w.top.record yview]
469
470         set new 1
471     }
472     $w.top.record tag configure marc-tag -foreground blue
473     $w.top.record tag configure marc-data -foreground black
474     $w.top.record tag configure marc-id -foreground red
475
476     if {$displayFormat == "nice"} {
477         display-nice $sno $no $w.top.record
478     } else {
479         display-raw $sno $no $w.top.record
480     }
481     if {$new} {
482         bind $w.top.record <Return> {destroy .full-marc}
483         
484         pack $w.top.s -side right -fill y
485         pack $w.top.record -expand yes -fill both
486
487         bottom-buttons $w [list \
488                 {Close} [list destroy $w] \
489                 {Raw} [list display-raw $sno $no $w.top.record] \
490                 {Duplicate} [list show-full-marc $sno $no 1]] 0
491     } else {
492         $w.bot.2 configure -command [list display-raw $sno $no $w.top.record]
493         $w.bot.4 configure -command [list show-full-marc $sno $no 1]
494     }
495 }
496
497 proc update-target-hotlist {target} {
498     global hotTargets
499
500     set len [llength $hotTargets]
501     if {$len > 0} {
502         .top.target.m delete 6 [expr 6+[llength $hotTargets]]
503     }
504     set indx [lsearch $hotTargets $target]
505     if {$indx >= 0} {
506         set hotTargets [lreplace $hotTargets $indx $indx]
507     }
508     set hotTargets [linsert $hotTargets 0 $target]
509     set-target-hotlist    
510
511
512 proc set-target-hotlist {} {
513     global hotTargets
514     
515     set i 1
516     foreach target $hotTargets {
517         .top.target.m add command -label "$i $target" -command \
518                 [list reopen-target $target {}]
519         incr i
520         if {$i > 8} {
521              break
522         }
523     }
524 }
525
526 proc reopen-target {target base} {
527     close-target
528     open-target $target $base
529     update-target-hotlist $target
530 }
531
532 proc define-target-action {} {
533     global profile
534     
535     set target [.target-define.top.target.entry get]
536     if {$target == ""} {
537         return
538     }
539     update-target-hotlist $target
540     foreach n [array names profile] {
541         if {$n == $target} {
542             protocol-setup $n
543             return
544         }
545     }
546     set seq [lindex $profile(Default) 12]
547     puts "seq=${seq}"
548     set profile($target) $profile(Default)
549     set profile(Default) [lreplace $profile(Default) 12 12 [incr seq]]
550
551     protocol-setup $target
552     destroy .target-define
553 }
554
555 proc fail-response {target} {
556     close-target
557     tkerror "Target connection closed or protocol error"
558 }
559
560 proc connect-response {target} {
561     puts "connect-response"
562     show-target $target
563     init-request
564 }
565
566 proc open-target {target base} {
567     global profile
568     global hostid
569
570     z39 disconnect
571     z39 comstack [lindex $profile($target) 6]
572     z39 idAuthentication [lindex $profile($target) 3]
573     z39 maximumRecordSize [lindex $profile($target) 4]
574     z39 preferredMessageSize [lindex $profile($target) 5]
575     puts -nonewline "maximumRecordSize="
576     puts [z39 maximumRecordSize]
577     puts -nonewline "preferredMessageSize="
578     puts [z39 preferredMessageSize]
579     show-status {Connecting} 0 0
580     if {$base == ""} {
581         z39 databaseNames [lindex [lindex $profile($target) 7] 0]
582     } else {
583         z39 databaseNames $base
584     }
585     z39 failback [list fail-response $target]
586     z39 callback [list connect-response $target]
587     z39 connect [lindex $profile($target) 1]:[lindex $profile($target) 2]
588 #    z39 options search present scan namedResultSets triggerResourceCtrl
589     show-status {Connecting} 1 {}
590     set hostid $target
591     .top.target.m disable 0
592     .top.target.m enable 1
593     .top.target.m enable 2
594 }
595
596 proc close-target {} {
597     global hostid
598     global cancelFlag
599
600     set cancelFlag 0
601     set hostid Default
602     z39 disconnect
603     show-target {}
604     show-status {Not connected} 0 0
605     show-message {}
606     .top.target.m disable 1
607     .top.target.m disable 2
608     .top.target.m enable 0
609 }
610
611 proc load-set-action {} {
612     global setNo
613
614     incr setNo
615     ir-set z39.$setNo z39
616
617     set fname [.load-set.top.filename.entry get]
618     destroy .load-set
619     if {$fname != ""} {
620         show-status {Loading} 1 {}
621         z39.$setNo loadFile $fname
622
623         set no [z39.$setNo numberOfRecordsReturned]
624         add-title-lines $setNo $no 1
625     }
626     set l [format "%-4d %7d" $setNo $no]
627     .top.rset.m add command -label $l \
628             -command [list add-title-lines $setNo 10000 1]
629     show-status {Ready} 0 {}
630 }
631
632 proc load-set {} {
633     set w .load-set
634
635     set oldFocus [focus]
636     toplevel $w
637
638     place-force $w .
639
640     top-down-window $w
641
642     frame $w.top.filename
643     
644     pack $w.top.filename -side top -anchor e -pady 2
645     
646     entry-fields $w.top {filename} \
647             {{Filename:}} \
648             {load-set-action} {destroy .load-set}
649     
650     top-down-ok-cancel $w {load-set-action} 1
651     focus $oldFocus
652 }
653
654 proc init-request {} {
655     global setNo
656     global cancelFlag
657
658     if {$cancelFlag} {
659         close-target
660         return
661     }
662     z39 callback {init-response}
663     show-status {Initializing} 1 {}
664     z39 init
665 }
666
667 proc init-response {} {
668     global cancelFlag
669
670     if {$cancelFlag} {
671         close-target
672         return
673     }
674     show-status {Ready} 0 1
675     if {![z39 initResult]} {
676         set u [z39 userInformationField]
677         close-target
678         tkerror "Connection rejected by target: $u"
679     }
680 }
681
682 proc search-request {} {
683     global setNo
684     global profile
685     global hostid
686     global busy
687     global cancelFlag
688     global searchEnable
689
690     set target $hostid
691
692     if {$searchEnable == 0} {
693         return
694     }
695     set query [index-query]
696     if {$query==""} {
697         return
698     }
699     incr setNo
700     ir-set z39.$setNo z39
701
702     if {[lindex $profile($target) 10] == 1} {
703         z39.$setNo setName $setNo
704         puts "setName=${setNo}"
705     } else {
706         z39.$setNo setName Default
707         puts "setName=Default"
708     }
709     if {[lindex $profile($target) 8] == 1} {
710         z39.$setNo queryType rpn
711     }
712     if {[lindex $profile($target) 9] == 1} {
713         z39.$setNo queryType ccl
714     }
715     z39 callback {search-response}
716     z39.$setNo search $query
717     show-status {Search} 1 0
718 }
719
720 proc scan-request {attr} {
721     set w .scan-window
722
723     global profile
724     global hostid
725     global scanView
726     global scanTerm
727
728     set target $hostid
729     set scanView 0
730     set scanTerm {}
731
732     ir-scan z39.scan z39
733
734     if {![winfo exists $w]} {
735         toplevel $w
736         
737         wm title $w "Scan"
738         
739         wm minsize $w 0 0
740
741         top-down-window $w
742
743         entry $w.top.entry -relief sunken 
744         pack $w.top.entry -fill x -padx 4 -pady 2
745         bind $w.top.entry <KeyRelease> [list scan-term-h $attr]
746         if {1} {
747             listbox $w.top.list -yscrollcommand [list $w.top.scroll set] \
748                     -font fixed -geometry 50x14
749             scrollbar $w.top.scroll -orient vertical -border 1
750             pack $w.top.list -side left -fill both -expand yes
751             pack $w.top.scroll -side right -fill y
752             $w.top.scroll config -command [list $w.top.list yview]
753         } else {
754             listbox $w.top.list -font fixed -geometry 60x14
755             pack $w.top.list -side left -fill both -expand yes
756         }
757         
758         bottom-buttons $w [list {Close} [list destroy $w] \
759                 {Up} [list scan-up $attr] \
760                 {Down} [list scan-down $attr]] 0
761         bind $w.top.list <Up> [list scan-up $attr]
762         bind $w.top.list <Down> [list scan-down $attr]
763     }
764     focus $w.top.entry
765     z39 callback [list scan-response $attr 0 35]
766     z39.scan numberOfTermsRequested 5
767     z39.scan preferredPositionInResponse 1
768     z39.scan scan "${attr} 0"
769     
770     show-status {Scan} 1 0
771 }
772
773 proc scan-term-h {attr} {
774     global busy
775     global scanTerm
776
777     if {$busy} {
778         return
779     }
780     set w .scan-window
781     set nScanTerm [$w.top.entry get]
782     if {$nScanTerm == $scanTerm} {
783         return
784     }
785     set scanTerm $nScanTerm
786     z39 callback [list scan-response $attr 0 35]
787     z39.scan numberOfTermsRequested 5
788     z39.scan preferredPositionInResponse 1
789     puts "${attr} \{${scanTerm}\}"
790     if {$scanTerm == ""} {
791         z39.scan scan "${attr} 0"
792     } else {
793         z39.scan scan "${attr} \{${scanTerm}\}"
794     }
795     show-status {Scan} 1 0
796 }
797
798 proc scan-response {attr start toget} {
799     global cancelFlag
800     global scanTerm
801     global scanView
802
803     set w .scan-window
804     puts "In scan-response"
805     set m [z39.scan numberOfEntriesReturned]
806     puts $m
807     puts attr=$attr
808     puts start=$start
809     puts toget=$toget
810
811     if {![winfo exists .scan-window]} {
812         show-status {Ready} 0 1
813         set cancelFlag 0
814         return
815     }
816     set nScanTerm [$w.top.entry get]
817     if {$nScanTerm != $scanTerm} {
818         z39 callback [list scan-response $attr 0 35]
819         z39.scan numberOfTermsRequested 5
820         z39.scan preferredPositionInResponse 1
821         set scanTerm $nScanTerm
822         puts "${attr} \{${scanTerm}\}"
823         if {$scanTerm == ""} {
824             z39.scan scan "${attr} 0"
825         } else {
826             z39.scan scan "${attr} \{${scanTerm}\}"
827         }
828         show-status {Scan} 1 0
829         return
830     }
831     set status [z39.scan scanStatus]
832     if {$status == 6} {
833         tkerror "Scan fail"
834         show-status {Ready} 0 1
835         set cancelFlag 0
836         return
837     }
838     if {$toget < 0} {
839         for {set i 0} {$i < $m} {incr i} {
840             set term [lindex [z39.scan scanLine $i] 1]
841             set nostr [format " %-6d" [lindex [z39.scan scanLine $i] 2]]
842             $w.top.list insert $i "$nostr $term"
843         }
844         incr scanView $m
845         $w.top.list yview $scanView
846     } else {
847         $w.top.list delete $start end
848         for {set i 0} {$i < $m} {incr i} {
849             set term [lindex [z39.scan scanLine $i] 1]
850             set nostr [format " %-6d" [lindex [z39.scan scanLine $i] 2]]
851             $w.top.list insert end "$nostr $term"
852         }
853     }
854     if {$cancelFlag} {
855         show-status {Ready} 0 1
856         set cancelFlag 0
857         return
858     }
859     if {$toget > 0 && $m > 1 && $m < $toget} {
860         set ntoget [expr $toget - $m + 1]
861         puts ntoget=$ntoget
862         z39 callback [list scan-response $attr [expr $start + $m - 1] $ntoget]
863         set q $term
864         puts "down continue: $q"
865         if {$ntoget > 10} {
866             z39.scan numberOfTermsRequested 10
867         } else {
868             z39.scan numberOfTermsRequested $ntoget
869         }
870         z39.scan preferredPositionInResponse 1
871         puts "${attr} \{$q\}"
872         z39.scan scan "${attr} \{$q\}"
873         return
874     }
875     if {$toget < 0 && $m > 1 && $m < [expr - $toget]} {
876         set ntoget [expr - $toget - $m]
877         puts ntoget=$ntoget
878         z39 callback [list scan-response $attr 0 -$ntoget]
879         set q [string range [$w.top.list get 0] 8 end]
880         puts "up continue: $q"
881         if {$ntoget > 10} {
882             z39.scan numberOfTermsRequested 10
883             z39.scan preferredPositionInResponse 11
884         } else {
885             z39.scan numberOfTermsRequested $ntoget
886             z39.scan preferredPositionInResponse [incr ntoget]
887         }
888         puts "${attr} \{$q\}"
889         z39.scan scan "${attr} \{$q\}"
890         return
891     }
892     show-status {Ready} 0 1
893 }
894
895 proc scan-down {attr} {
896     global scanView
897
898     set w .scan-window
899     set scanView [expr $scanView + 5]
900     set s [$w.top.list size]
901     if {$scanView > $s} {
902         z39 callback [list scan-response $attr [expr $s - 1] 25]
903         set q [string range [$w.top.list get [expr $s - 1]] 8 end]
904         puts "down: $q"
905         z39.scan numberOfTermsRequested 10
906         z39.scan preferredPositionInResponse 1
907         show-status {Scan} 1 0
908         puts "${attr} \{$q\}"
909         z39.scan scan "${attr} \{$q\}"
910         return
911     }
912     $w.top.list yview $scanView
913 }
914
915 proc scan-up {attr} {
916     global scanView
917
918     set w .scan-window
919     set scanView [expr $scanView - 5]
920     if {$scanView < 0} {
921         z39 callback [list scan-response $attr 0 -25]
922         set q [string range [$w.top.list get 0] 8 end]
923         puts "up: $q"
924         z39.scan numberOfTermsRequested 10
925         z39.scan preferredPositionInResponse 11
926         show-status {Scan} 1 0
927         z39.scan scan "${attr} \{$q\}"
928         return
929     }
930     $w.top.list yview $scanView
931 }
932
933 proc search-response {} {
934     global setNo
935     global setOffset
936     global setMax
937     global cancelFlag
938     global busy
939
940     puts "In search-response"
941     init-title-lines
942     show-status {Ready} 0 1
943     set setMax [z39.$setNo resultCount]
944     show-message "${setMax} hits"
945     set l [format "%-4d %7d" $setNo $setMax]
946     .top.rset.m add command -label $l \
947             -command [list add-title-lines $setNo 10000 1]
948     if {$setMax <= 0} {
949         set status [z39.$setNo responseStatus]
950         if {[lindex $status 0] == "NSD"} {
951             set code [lindex $status 1]
952             set msg [lindex $status 2]
953             set addinfo [lindex $status 3]
954             tkerror "NSD$code: $msg: $addinfo"
955         }
956         return
957     }
958     if {$setMax > 20} {
959         set setMax 20
960     }
961     set setOffset 1
962     if {$cancelFlag} {
963         set cancelFlag 0
964         return
965     }
966     z39 callback {present-response}
967     z39.$setNo present $setOffset 1
968     show-status {Retrieve} 1 0
969 }
970
971 proc present-more {number} {
972     global setNo
973     global setOffset
974     global setMax
975
976     puts "setOffset=$setOffset"
977     puts "present-more"
978     if {$setNo == 0} {
979         puts "setNo=$setNo"
980         return
981     }
982     set max [z39.$setNo resultCount]
983     if {$max <= $setOffset} {
984         puts "max=$max"
985         puts "setOffset=$setOffset"
986         return
987     }
988     if {$number == ""} {
989         set setMax $max
990     } else {
991         incr setMax $number
992         if {$setMax > $max} {
993             set setMax $max
994         }
995     }
996     z39 callback {present-response}
997
998     set toGet [expr $setMax - $setOffset + 1]
999     if {$toGet <= 0} {
1000         return
1001     }
1002     if {$toGet > 3} {
1003         set toGet 3
1004     } 
1005     z39.$setNo present $setOffset $toGet
1006     show-status {Retrieve} 1 0
1007 }
1008
1009 proc init-title-lines {} {
1010     .data.list delete 0 end
1011 }
1012
1013 proc title-press {y setno} {
1014     show-full-marc $setno [expr 1 + [.data.list nearest $y]] 0
1015 }
1016
1017 proc add-title-lines {setno no offset} {
1018     if {$offset == 1} {
1019         .bot.a.set configure -text $setno
1020         .data.list delete 0 end
1021     }
1022     bind .data.list <Double-Button-1> [list title-press %y $setno]
1023     bind .data.list <Button-2> [list title-press %y $setno]
1024     for {set i 0} {$i < $no} {incr i} {
1025         set o [expr $i + $offset]
1026         set type [z39.$setno type $o]
1027         if {$type == "DB"} {
1028             set title [lindex [z39.$setno getMarc $o field 245 * a] 0]
1029             set year  [lindex [z39.$setno getMarc $o field 260 * c] 0]
1030             set nostr [format "%5d" $o]
1031             .data.list insert end "$nostr $title - $year"
1032         } elseif {$type == "SD"} {
1033             set err [lindex [z39.$setno diag $o] 1]
1034             set add [lindex [z39.$setno diag $o] 2]
1035             if {$add != {}} {
1036                 set add " :${add}"
1037             }
1038             .data.list insert end "Error ${err}${add}"
1039         } elseif {$type == ""} {
1040             break
1041         }
1042     }
1043 }
1044
1045 proc present-response {} {
1046     global setNo
1047     global setOffset
1048     global setMax
1049     global cancelFlag
1050
1051     puts "In present-response"
1052     set no [z39.$setNo numberOfRecordsReturned]
1053     puts "Returned $no records, setOffset $setOffset"
1054     add-title-lines $setNo $no $setOffset
1055     set setOffset [expr $setOffset + $no]
1056     set status [z39.$setNo responseStatus]
1057     if {[lindex $status 0] == "NSD"} {
1058         show-status {Ready} 0 1
1059         set code [lindex $status 1]
1060         set msg [lindex $status 2]
1061         set addinfo [lindex $status 3]
1062         tkerror "NSD$code: $msg: $addinfo"
1063         return
1064     }
1065     if {$cancelFlag} {
1066         show-status {Ready} 0 1
1067         set cancelFlag 0
1068         return
1069     }
1070     if {$no > 0 && $setOffset <= $setMax} {
1071         puts "present from ${setOffset}"
1072         set toGet [expr $setMax - $setOffset + 1]
1073         if {$toGet > 3} {
1074             set toGet 3
1075         }
1076         z39.$setNo present $setOffset $toGet
1077     } else {
1078         show-status {Finished} 0 1
1079     }
1080 }
1081
1082 proc left-cursor {w} {
1083     set i [$w index insert]
1084     if {$i > 0} {
1085         incr i -1
1086         $w icursor $i
1087     }
1088 }
1089
1090 proc right-cursor {w} {
1091     set i [$w index insert]
1092     incr i
1093     $w icursor $i
1094 }
1095
1096 proc bind-fields {list returnAction escapeAction} {
1097     set max [expr [llength $list]-1]
1098     for {set i 0} {$i < $max} {incr i} {
1099         bind [lindex $list $i] <Return> $returnAction
1100         bind [lindex $list $i] <Escape> $escapeAction
1101         bind [lindex $list $i] <Tab> [list focus [lindex $list [expr $i+1]]]
1102         bind [lindex $list $i] <Left> [list left-cursor [lindex $list $i]]
1103         bind [lindex $list $i] <Right> [list right-cursor [lindex $list $i]]
1104     }
1105     bind [lindex $list $i] <Return> $returnAction
1106     bind [lindex $list $i] <Escape> $escapeAction
1107     bind [lindex $list $i] <Tab>    [list focus [lindex $list 0]]
1108     bind [lindex $list $i] <Left> [list left-cursor [lindex $list $i]]
1109     bind [lindex $list $i] <Right> [list right-cursor [lindex $list $i]]
1110     focus [lindex $list 0]
1111 }
1112
1113 proc entry-fields {parent list tlist returnAction escapeAction} {
1114     set alist {}
1115     set i 0
1116     foreach field $list {
1117         set label ${parent}.${field}.label
1118         set entry ${parent}.${field}.entry
1119         label $label -text [lindex $tlist $i] -anchor e
1120         entry $entry -width 32 -relief sunken
1121         pack $label -side left
1122         pack $entry -side right
1123         lappend alist $entry
1124         incr i
1125     }
1126     bind-fields $alist $returnAction $escapeAction
1127 }
1128
1129 proc define-target-dialog {} {
1130     set w .target-define
1131
1132     toplevel $w
1133     place-force $w .
1134     top-down-window $w
1135     frame $w.top.target
1136     pack $w.top.target \
1137             -side top -anchor e -pady 2 
1138     entry-fields $w.top {target} \
1139             {{Target:}} \
1140             {define-target-action} {destroy .target-define}
1141     top-down-ok-cancel $w {define-target-action} 1
1142 }
1143
1144 proc protocol-setup-action {target} {
1145     global profile
1146     global csRadioType
1147     global protocolRadioType
1148     global settingsChanged
1149     global RPNCheck
1150     global CCLCheck
1151     global ResultSetCheck
1152
1153     set wno [lindex $profile($target) 12]
1154     set w .setup-${wno}
1155     
1156     set b {}
1157     set settingsChanged 1
1158     set len [$w.top.databases.list size]
1159     for {set i 0} {$i < $len} {incr i} {
1160         lappend b [$w.top.databases.list get $i]
1161     }
1162     set profile($target) [list [$w.top.description.entry get] \
1163             [$w.top.host.entry get] \
1164             [$w.top.port.entry get] \
1165             [$w.top.idAuthentication.entry get] \
1166             [$w.top.maximumRecordSize.entry get] \
1167             [$w.top.preferredMessageSize.entry get] \
1168             $csRadioType \
1169             $b \
1170             $RPNCheck \
1171             $CCLCheck \
1172             $ResultSetCheck \
1173             $protocolRadioType \
1174             $wno]
1175
1176     cascade-target-list
1177     puts $profile($target)
1178     destroy $w
1179 }
1180
1181 proc place-force {window parent} {
1182     set g [wm geometry $parent]
1183
1184     set p1 [string first + $g]
1185     set p2 [string last + $g]
1186
1187     set x [expr 40+[string range $g [expr $p1 +1] [expr $p2 -1]]]
1188     set y [expr 60+[string range $g [expr $p2 +1] end]]
1189     wm geometry $window +${x}+${y}
1190 }
1191
1192 proc add-database-action {target} {
1193     global profile
1194
1195     set wno [lindex $profile($target) 12]
1196     set w .setup-${wno}
1197
1198     $w.top.databases.list insert end \
1199             [.database-select.top.database.entry get]
1200     destroy .database-select
1201 }
1202
1203 proc add-database {target} {
1204     global profile
1205
1206     set w .database-select
1207
1208     set oldFocus [focus]
1209     toplevel $w
1210  
1211     set wno [lindex $profile($target) 12]
1212     place-force $w .setup-${wno}
1213
1214     top-down-window $w
1215
1216     frame $w.top.database
1217
1218     pack $w.top.database -side top -anchor e -pady 2
1219     
1220     entry-fields $w.top {database} \
1221             {{Database to add:}} \
1222             [list add-database-action $target] {destroy .database-select}
1223
1224     top-down-ok-cancel $w [list add-database-action $target] 1
1225     focus $oldFocus
1226 }
1227
1228 proc delete-database {target} {
1229     global profile
1230
1231     set wno [lindex $profile($target) 12]
1232     set w .setup-${wno}
1233
1234     foreach i [lsort -decreasing \
1235             [$w.top.databases.list curselection]] {
1236         $w.top.databases.list delete $i
1237     }
1238 }
1239
1240 proc protocol-setup {target} {
1241
1242     global profile
1243     global csRadioType
1244     global protocolRadioType
1245     global RPNCheck
1246     global CCLCheck
1247     global ResultSetCheck
1248
1249     set wno [lindex $profile($target) 12]
1250     set w .setup-${wno}
1251
1252     toplevel $w
1253
1254     wm title $w "Setup $target"
1255     place-force $w .
1256
1257     top-down-window $w
1258     
1259     if {$target == ""} {
1260         set target Default
1261     }
1262     puts target
1263     puts $profile($target)
1264
1265     frame $w.top.host
1266     frame $w.top.port
1267     frame $w.top.description
1268     frame $w.top.idAuthentication
1269     frame $w.top.maximumRecordSize
1270     frame $w.top.preferredMessageSize
1271     frame $w.top.cs-type -relief ridge -border 2
1272     frame $w.top.protocol -relief ridge -border 2
1273     frame $w.top.query -relief ridge -border 2
1274     frame $w.top.databases -relief ridge -border 2
1275
1276     # Maximum/preferred/idAuth ...
1277     pack $w.top.description $w.top.host $w.top.port \
1278             $w.top.idAuthentication $w.top.maximumRecordSize \
1279             $w.top.preferredMessageSize -side top -anchor e -pady 2
1280     
1281     entry-fields $w.top {description host port idAuthentication \
1282             maximumRecordSize preferredMessageSize} \
1283             {{Description:} {Host:} {Port:} {Id Authentication:} \
1284             {Maximum Record Size:} {Preferred Message Size:}} \
1285             [list protocol-setup-action $target] [list destroy $w]
1286     
1287     foreach sub {description host port idAuthentication \
1288             maximumRecordSize preferredMessageSize} {
1289         puts $sub
1290         bind $w.top.$sub.entry <Control-a> [list add-database $target]
1291         bind $w.top.$sub.entry <Control-d> [list delete-database $target]
1292     }
1293     $w.top.description.entry insert 0 [lindex $profile($target) 0]
1294     $w.top.host.entry insert 0 [lindex $profile($target) 1]
1295     $w.top.port.entry insert 0 [lindex $profile($target) 2]
1296     $w.top.idAuthentication.entry insert 0 [lindex $profile($target) 3]
1297     $w.top.maximumRecordSize.entry insert 0 [lindex $profile($target) 4]
1298     $w.top.preferredMessageSize.entry insert 0 [lindex $profile($target) 5]
1299     set csRadioType [lindex $profile($target) 6]
1300     set RPNCheck [lindex $profile($target) 8]
1301     set CCLCheck [lindex $profile($target) 9]
1302     set ResultSetCheck [lindex $profile($target) 10]
1303     set protocolRadioType [lindex $profile($target) 11]
1304     if {$protocolRadioType == ""} {
1305         set protocolRadioType z39v2
1306     }
1307
1308     # Databases ....
1309     pack $w.top.databases -side left -pady 6 -padx 6 -expand yes -fill both
1310
1311     label $w.top.databases.label -text "Databases"
1312     button $w.top.databases.add -text "Add" \
1313             -command [list add-database $target]
1314     button $w.top.databases.delete -text "Delete" \
1315             -command [list delete-database $target]
1316     listbox $w.top.databases.list -geometry 20x6 \
1317             -yscrollcommand "$w.top.databases.scroll set"
1318     scrollbar $w.top.databases.scroll -orient vertical -border 1
1319     pack $w.top.databases.label -side top -fill x \
1320             -padx 2 -pady 2
1321     pack $w.top.databases.add $w.top.databases.delete -side top -fill x \
1322             -padx 2 -pady 2
1323     pack $w.top.databases.list -side left -fill both -expand yes \
1324             -padx 2 -pady 2
1325     pack $w.top.databases.scroll -side right -fill y \
1326             -padx 2 -pady 2
1327     $w.top.databases.scroll config -command "$w.top.databases.list yview"
1328
1329     foreach b [lindex $profile($target) 7] {
1330         $w.top.databases.list insert end $b
1331     }
1332
1333     # Transport ...
1334     pack $w.top.cs-type -pady 6 -padx 6 -side top -fill x
1335     
1336     label $w.top.cs-type.label -text "Transport" 
1337     radiobutton $w.top.cs-type.tcpip -text "TCP/IP" -anchor w \
1338             -command {puts tcp/ip} -variable csRadioType -value tcpip
1339     radiobutton $w.top.cs-type.mosi -text "MOSI" -anchor w\
1340             -command {puts mosi} -variable csRadioType -value mosi
1341     
1342     pack $w.top.cs-type.label $w.top.cs-type.tcpip $w.top.cs-type.mosi \
1343             -padx 4 -side top -fill x
1344
1345     # Protocol ...
1346     pack $w.top.protocol -pady 6 -padx 6 -side top -fill x
1347     
1348     label $w.top.protocol.label -text "Protocol" 
1349     radiobutton $w.top.protocol.z39v2 -text "Z39.50" -anchor w \
1350             -command {puts z39v2} -variable protocolRadioType -value z39v2
1351     radiobutton $w.top.protocol.sr -text "SR" -anchor w \
1352             -command {puts sr} -variable protocolRadioType -value sr
1353     
1354     pack $w.top.protocol.label $w.top.protocol.z39v2 $w.top.protocol.sr \
1355             -padx 4 -side top -fill x
1356
1357     # Query ...
1358     pack $w.top.query -pady 6 -padx 6 -side top -fill x
1359
1360     label $w.top.query.label -text "Query support"
1361     checkbutton $w.top.query.c1 -text "RPN query" -anchor w -variable RPNCheck
1362     checkbutton $w.top.query.c2 -text "CCL query" -anchor w -variable CCLCheck
1363     checkbutton $w.top.query.c3 -text "Result sets" -anchor w -variable ResultSetCheck
1364
1365     pack $w.top.query.label -side top 
1366     pack $w.top.query.c1 $w.top.query.c2 $w.top.query.c3 \
1367             -padx 4 -side top -fill x
1368
1369     # Ok-cancel
1370     top-down-ok-cancel $w [list protocol-setup-action $target] 0
1371 }
1372
1373 proc database-select-action {} {
1374     set w .database-select.top
1375     set b {}
1376     foreach indx [$w.databases.list curselection] {
1377         lappend b [$w.databases.list get $indx]
1378     }
1379     if {$b != ""} {
1380         z39 databaseNames $b
1381     }
1382     destroy .database-select
1383 }
1384
1385 proc database-select {} {
1386     set w .database-select
1387     global profile
1388     global hostid
1389
1390     toplevel $w
1391
1392     place-force $w .
1393
1394     top-down-window $w
1395
1396     frame $w.top.databases -relief ridge -border 2
1397
1398     pack $w.top.databases -side left -pady 6 -padx 6 -expand yes -fill x
1399
1400     label $w.top.databases.label -text "List"
1401     listbox $w.top.databases.list -geometry 20x6 \
1402             -yscrollcommand "$w.top.databases.scroll set"
1403     scrollbar $w.top.databases.scroll -orient vertical -border 1
1404     pack $w.top.databases.label -side top -fill x \
1405             -padx 2 -pady 2
1406     pack $w.top.databases.list -side left -fill both -expand yes \
1407             -padx 2 -pady 2
1408     pack $w.top.databases.scroll -side right -fill y \
1409             -padx 2 -pady 2
1410     $w.top.databases.scroll config -command "$w.top.databases.list yview"
1411
1412     foreach b [lindex $profile($hostid) 7] {
1413         $w.top.databases.list insert end $b
1414     }
1415     top-down-ok-cancel $w {database-select-action} 1
1416 }
1417
1418 proc cascade-target-list {} {
1419     global profile
1420     
1421     foreach sub [winfo children .top.target.m.clist] {
1422         puts "deleting $sub"
1423         destroy $sub
1424     }
1425     .top.target.m.clist delete 0 last
1426     foreach n [array names profile] {
1427         if {$n != "Default"} {
1428             set nl [string tolower $n]
1429             if {[llength [lindex $profile($n) 7]] > 1} {
1430                 .top.target.m.clist add cascade -label $n \
1431                         -menu .top.target.m.clist.$nl
1432                 menu .top.target.m.clist.$nl
1433                 foreach b [lindex $profile($n) 7] {
1434                     .top.target.m.clist.$nl add command -label $b \
1435                             -command [list reopen-target $n $b]
1436                 }
1437             } else {
1438                 .top.target.m.clist add command -label $n \
1439                         -command [list reopen-target $n {}]
1440             }
1441         }
1442     }
1443     .top.target.m.slist delete 0 last
1444     foreach n [array names profile] {
1445         if {$n != "Default"} {
1446             .top.target.m.slist add command -label $n \
1447                     -command [list protocol-setup $n]
1448         }
1449     }
1450 }
1451
1452 proc cascade-query-list {} {
1453     global queryTypes
1454
1455     set i 0
1456     .top.options.m.slist delete 0 last
1457     foreach n $queryTypes {
1458         .top.options.m.slist add command -label $n \
1459                 -command [list query-setup $i]
1460         incr i
1461     }
1462
1463     set i 0
1464     .top.options.m.clist delete 0 last
1465     foreach n $queryTypes {
1466         .top.options.m.clist add command -label $n \
1467                 -command [list query-select $i]
1468         incr i
1469     }
1470 }
1471
1472 proc save-settings {} {
1473     global hotTargets 
1474     global profile
1475     global settingsChanged
1476     global queryTypes
1477     global queryButtons
1478     global queryInfo
1479
1480     set f [open "clientrc.tcl" w]
1481     puts $f "# Setup file"
1482     puts $f "set hotTargets \{ $hotTargets \}"
1483
1484     foreach n [array names profile] {
1485         puts -nonewline $f "set \{profile($n)\} \{"
1486         puts -nonewline $f $profile($n)
1487         puts $f "\}"
1488     }
1489     puts -nonewline $f "set queryTypes \{" 
1490     puts -nonewline $f $queryTypes
1491     puts $f "\}"
1492     
1493     puts -nonewline $f "set queryButtons \{" 
1494     puts -nonewline $f $queryButtons
1495     puts $f "\}"
1496     
1497     puts -nonewline $f "set queryInfo \{"
1498     puts -nonewline $f $queryInfo
1499     puts $f "\}"
1500     
1501     close $f
1502     set settingsChanged 0
1503 }
1504
1505 proc alert {ask} {
1506     set w .alert
1507
1508     global alertAnswer
1509
1510     toplevel $w
1511     place-force $w .
1512     top-down-window $w
1513
1514     message $w.top.message -text $ask
1515
1516     pack $w.top.message -side left -pady 6 -padx 20 -expand yes -fill x
1517   
1518     set alertAnswer 0
1519     top-down-ok-cancel $w {alert-action} 1
1520     return $alertAnswer
1521 }
1522
1523 proc alert-action {} {
1524     global alertAnswer
1525     set alertAnswer 1
1526     destroy .alert
1527 }
1528
1529 proc exit-action {} {
1530     global settingsChanged
1531
1532     if {$settingsChanged} {
1533         set a [alert "you havent saved your settings. Do you wish to save?"]
1534         if {$a} {
1535             save-settings
1536         }
1537     }
1538     exit 0
1539 }
1540
1541 proc listbuttonaction {w name h user i} {
1542     $w configure -text [lindex $name 0]
1543     $h [lindex $name 1] $user $i
1544 }
1545     
1546 proc listbuttonx {button no names handle user} {
1547     if {[winfo exists $button]} {
1548         $button configure -text [lindex [lindex $names $no] 0]
1549         ${button}.m delete 0 last
1550     } else {
1551         menubutton $button -text [lindex [lindex $names $no] 0] \
1552                 -width 10 -menu ${button}.m -relief raised -border 1
1553         menu ${button}.m
1554     }
1555     set i 0
1556     foreach name $names {
1557         ${button}.m add command -label [lindex $name 0] \
1558                 -command [list listbuttonaction ${button} $name \
1559                 $handle $user $i]
1560         incr i
1561     }
1562 }
1563
1564 proc listbutton {button no names} {
1565     menubutton $button -text [lindex $names $no] -width 10 -menu ${button}.m \
1566             -relief raised -border 1
1567     menu ${button}.m
1568     foreach name $names {
1569         ${button}.m add command -label $name \
1570                 -command [list ${button} configure -text $name]
1571     }
1572 }
1573
1574 proc query-add-index-action {queryNo} {
1575     set w .setup-query-$queryNo
1576
1577     global queryInfoTmp
1578     global queryButtonsTmp
1579
1580     lappend queryInfoTmp [list [.query-add-index.top.index.entry get] {}]
1581
1582     destroy .query-add-index
1583     #destroy $w.top.lines
1584     #frame $w.top.lines -relief ridge -border 2
1585     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
1586     #pack $w.top.lines -side left -pady 6 -padx 6 -fill y
1587 }
1588
1589 proc query-add-line {queryNo} {
1590     set w .setup-query-$queryNo
1591
1592     global queryInfoTmp
1593     global queryButtonsTmp
1594
1595     lappend queryButtonsTmp {I 0}
1596
1597     #destroy $w.top.lines
1598     #frame $w.top.lines -relief ridge -border 2
1599     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
1600     #pack $w.top.lines -side left -pady 6 -padx 6 -fill y
1601 }
1602
1603 proc query-del-line {queryNo} {
1604     set w .setup-query-$queryNo
1605
1606     global queryInfoTmp
1607     global queryButtonsTmp
1608
1609     set l [llength $queryButtonsTmp]
1610     if {$l <= 0} {
1611         return
1612     }
1613     incr l -1
1614     set queryButtonsTmp [lreplace $queryButtonsTmp $l $l]
1615     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
1616 }
1617
1618 proc query-add-index {queryNo} {
1619     set w .query-add-index
1620
1621     toplevel $w
1622     place-force $w .setup-query-$queryNo
1623     top-down-window $w
1624     frame $w.top.index
1625     pack $w.top.index \
1626             -side top -anchor e -pady 2 
1627     entry-fields $w.top {index} \
1628             {{Index Name:}} \
1629             [list query-add-index-action $queryNo] {destroy .query-add-index}
1630     top-down-ok-cancel $w [list query-add-index-action $queryNo] 1
1631 }
1632
1633 proc query-setup-action {queryNo} {
1634     global queryButtons
1635     global queryInfo
1636     global queryButtonsTmp
1637     global queryInfoTmp
1638     global queryButtonsFind
1639     global queryInfoFind
1640
1641     set queryInfo [lreplace $queryInfo $queryNo $queryNo \
1642             $queryInfoTmp]
1643     set queryButtons [lreplace $queryButtons $queryNo $queryNo \
1644             $queryButtonsTmp]
1645     set queryInfoFind $queryInfoTmp
1646     set queryButtonsFind $queryButtonsTmp
1647
1648     puts $queryInfo
1649     puts $queryButtons
1650     destroy .setup-query-$queryNo
1651
1652     index-lines .lines 1 $queryButtonsFind $queryInfoFind activate-index
1653 }
1654
1655 proc activate-e-index {value no i} {
1656     global queryButtonsTmp
1657     
1658     puts $queryButtonsTmp
1659     set queryButtonsTmp [lreplace $queryButtonsTmp $no $no [list I $i]]
1660     puts $queryButtonsTmp
1661     puts "value $value"
1662     puts "no $no"
1663     puts "i $i"
1664 }
1665
1666 proc activate-index {value no i} {
1667     global queryButtonsFind
1668
1669     set queryButtonsFind [lreplace $queryButtonsFind $no $no [list I $i]]
1670
1671     puts "queryButtonsFind $queryButtonsFind"
1672     puts "value $value"
1673     puts "no $no"
1674     puts "i $i"
1675 }
1676
1677 proc query-setup {queryNo} {
1678     set w .setup-query-$queryNo
1679     global queryTypes
1680     set queryTypes {Simple}
1681     global queryButtons
1682     global queryInfo
1683     global queryButtonsTmp
1684     global queryInfoTmp
1685
1686     set queryName [lindex $queryTypes $queryNo]
1687     set queryInfoTmp [lindex $queryInfo $queryNo]
1688     set queryButtonsTmp [lindex $queryButtons $queryNo]
1689
1690     #set queryButtons { {I 0 I 1 I 2} }
1691     #set queryInfo { { {Title ti} {Author au} {Subject sh} } }
1692
1693     toplevel $w
1694
1695     wm title $w "Query setup $queryName"
1696     place-force $w .
1697
1698     top-down-window $w
1699
1700     frame $w.top.lines -relief ridge -border 2
1701     frame $w.top.use -relief ridge -border 2
1702     frame $w.top.relation -relief ridge -border 2
1703     frame $w.top.position -relief ridge -border 2
1704     frame $w.top.structure -relief ridge -border 2
1705     frame $w.top.truncation -relief ridge -border 2
1706     frame $w.top.completeness -relief ridge -border 2
1707
1708     # Index Lines
1709
1710     index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
1711
1712     pack $w.top.lines -side left -pady 6 -padx 6 -fill y
1713
1714     # Use Attributes
1715     pack $w.top.use -side left -pady 6 -padx 6 -fill y
1716
1717     label $w.top.use.label -text "Use"
1718     listbox $w.top.use.list -geometry 20x10 \
1719             -yscrollcommand "$w.top.use.scroll set"
1720     scrollbar $w.top.use.scroll -orient vertical -border 1
1721     pack $w.top.use.label -side top -fill x \
1722             -padx 2 -pady 2
1723     pack $w.top.use.list -side left -fill both -expand yes \
1724             -padx 2 -pady 2
1725     pack $w.top.use.scroll -side right -fill y \
1726             -padx 2 -pady 2
1727     $w.top.use.scroll config -command "$w.top.use.list yview"
1728
1729     foreach u {{Personal name} {Corporate name}} {
1730         $w.top.use.list insert end $u
1731     }
1732     # Relation Attributes
1733     pack $w.top.relation -pady 6 -padx 6 -side top
1734
1735     label $w.top.relation.label -text "Relation" -width 18
1736     
1737     listbutton $w.top.relation.b 0\
1738             {{None} {Less than} {Greater than or equal} \
1739             {Equal} {Greater than or equal} {Greater than} {Not equal} \
1740             {Phonetic} \
1741             {Stem} {Relevance} {AlwaysMatches}}
1742     
1743     pack $w.top.relation.label $w.top.relation.b -fill x 
1744
1745     # Position Attributes
1746     pack $w.top.position -pady 6 -padx 6 -side top
1747
1748     label $w.top.position.label -text "Position" -width 18
1749
1750     listbutton $w.top.position.b 0 {{None} {First in field} {First in subfield}
1751     {Any position in field}}
1752     
1753     pack $w.top.position.label $w.top.position.b -fill x
1754
1755     # Structure Attributes
1756
1757     pack $w.top.structure -pady 6 -padx 6 -side top
1758     
1759     label $w.top.structure.label -text "Structure" -width 18
1760
1761     listbutton $w.top.structure.b 0 {{None} {Phrase} {Word} {Key} {Year}
1762     {Date (norm)} {Word list} {Date (un-norm)} {Name (norm)} {Date (un-norm)}
1763     {Structure} {urx} {free-form} {doc-text} {local-number} {string}
1764     {numeric string}}
1765
1766     pack $w.top.structure.label $w.top.structure.b -fill x
1767
1768     # Truncation Attributes
1769
1770     pack $w.top.truncation -pady 6 -padx 6 -side top
1771     
1772     label $w.top.truncation.label -text "Truncation" -width 18
1773
1774     listbutton $w.top.truncation.b 0 {{Auto} {Right} {Left} {Left and right} \
1775             {No truncation} {Process #} {Re-1} {Re-2}}
1776     pack $w.top.truncation.label $w.top.truncation.b -fill x
1777
1778     # Completeness Attributes
1779
1780     pack $w.top.completeness -pady 6 -padx 6 -side top
1781     
1782     label $w.top.completeness.label -text "Truncation" -width 18
1783
1784     listbutton $w.top.completeness.b 0 {{None} {Incomplete subfield} \
1785             {Complete subfield} {Complete field}}
1786     pack $w.top.completeness.label $w.top.completeness.b -fill x
1787
1788     # Ok-cancel
1789     bottom-buttons $w [list \
1790             {Ok} [list query-setup-action $queryNo] \
1791             {Add index} [list query-add-index $queryNo] \
1792             {Add line} [list query-add-line $queryNo] \
1793             {Delete line} [list query-del-line $queryNo] \
1794             {Cancel} [list destroy $w]] 0
1795 }
1796
1797 proc index-clear {} {
1798     global queryButtonsFind
1799
1800     set i 0
1801     foreach b $queryButtonsFind {
1802         .lines.$i.e delete 0 end
1803         incr i
1804     }
1805 }
1806     
1807 proc index-query {} {
1808     global queryButtonsFind
1809     global queryInfoFind
1810
1811     set i 0
1812     set qs {}
1813
1814     foreach b $queryButtonsFind {
1815         set term [string trim [.lines.$i.e get]]
1816         if {$term != ""} {
1817             set attr [lindex [lindex $queryInfoFind [lindex $b 1]] 1]
1818
1819             set term "\{${term}\}"
1820             foreach a $attr {
1821                 set term "@attr $a ${term}"
1822             }
1823             if {$qs != ""} {
1824                 set qs "@and ${qs} ${term}"
1825             } else {
1826                 set qs $term
1827             }
1828         }
1829         incr i
1830     }
1831     puts "qs=  $qs"
1832     return $qs
1833 }
1834
1835 proc index-lines {w realOp buttonInfo queryInfo handle} {
1836     set i 0
1837     foreach b $buttonInfo {
1838         if {! [winfo exists $w.$i]} {
1839             frame $w.$i -background white -border 1
1840         }
1841         listbuttonx $w.$i.l [lindex $b 1] $queryInfo $handle $i
1842
1843         if {$realOp} {
1844             if {! [winfo exists $w.$i.e]} {
1845                 entry $w.$i.e -width 32 -relief sunken -border 1
1846                 bind $w.$i.e <FocusIn> [list $w.$i configure \
1847                         -background red]
1848                 bind $w.$i.e <FocusOut> [list $w.$i configure \
1849                         -background white]
1850                 pack $w.$i.l -side left
1851                 pack $w.$i.e -side left -fill x -expand yes
1852                 pack $w.$i -side top -fill x -padx 2 -pady 2
1853                 bind $w.$i.e <Left> [list left-cursor $w.$i.e]
1854                 bind $w.$i.e <Right> [list right-cursor $w.$i.e]
1855                 bind $w.$i.e <Return> search-request
1856             }
1857         } else {
1858             pack $w.$i.l -side left
1859             pack $w.$i -side top -fill x -padx 2 -pady 2
1860         }
1861         incr i
1862     }
1863     set j $i
1864     while {[winfo exists $w.$j]} {
1865         destroy $w.$j
1866         incr j
1867     }
1868     if {! $realOp} {
1869         return
1870     }
1871     set j 0
1872     incr i -1
1873     while {$j < $i} {
1874         set k [expr $j+1]
1875         bind $w.$j.e <Tab> "focus $w.$k.e"
1876         set j $k
1877     }
1878     if {$i >= 0} {
1879         bind $w.$i.e <Tab> "focus $w.0.e"
1880         focus $w.0.e
1881     }
1882 }
1883
1884 proc search-fields {w buttondefs} {
1885     set i 0
1886     foreach buttondef $buttondefs {
1887         frame $w.$i -background white
1888         
1889         listbutton $w.$i.l 0 $buttondef
1890         entry $w.$i.e -width 32 -relief sunken
1891         
1892         pack $w.$i.l -side left
1893         pack $w.$i.e -side left -fill x -expand yes
1894
1895         pack $w.$i -side top -fill x -padx 2 -pady 2
1896
1897         bind $w.$i.e <Left> [list left-cursor $w.$i.e]
1898         bind $w.$i.e <Right> [list right-cursor $w.$i.e]
1899
1900         incr i
1901     }
1902     set j 0
1903     incr i -1
1904     while {$j < $i} {
1905         set k [expr $j+1]
1906         bind $w.$j.e <Tab> "focus $w.$k.e \n
1907         $w.$k configure -background red \n
1908         $w.$j configure -background white"
1909         set j $k
1910     }
1911     bind $w.$i.e <Tab> "focus $w.0.e \n
1912         $w.0 configure -background red \n
1913         $w.$i configure -background white"
1914     focus $w.0.e
1915     $w.0 configure -background red
1916 }
1917
1918 frame .top  -border 1 -relief raised
1919 frame .lines  -border 1 -relief raised
1920 frame .mid  -border 1 -relief raised
1921 frame .data -border 1 -relief raised
1922 frame .bot  -border 1 -relief raised
1923 pack .top .lines .mid -side top -fill x
1924 pack .data -side top -fill both -expand yes
1925 pack .bot -fill x
1926
1927 menubutton .top.file -text "File" -underline 0 -menu .top.file.m
1928 menu .top.file.m
1929 .top.file.m add command -label "Save settings" -command {save-settings}
1930 .top.file.m add separator
1931 .top.file.m add command -label "Exit" -command {exit-action}
1932 .top.file.m add separator
1933 .top.file.m add command -label "About" -command {about-origin}
1934
1935 menubutton .top.target -text "Target" -underline 0 -menu .top.target.m
1936 menu .top.target.m
1937 .top.target.m add cascade -label "Connect" -menu .top.target.m.clist
1938 .top.target.m add command -label "Disconnect" -command {close-target}
1939 .top.target.m add command -label "About" -command {about-target}
1940 .top.target.m add cascade -label "Setup" -menu .top.target.m.slist
1941 .top.target.m add command -label "Setup new" -command {define-target-dialog}
1942 .top.target.m add separator
1943 set-target-hotlist
1944
1945 .top.target.m disable 1
1946 .top.target.m disable 2
1947
1948 menu .top.target.m.clist
1949 menu .top.target.m.slist
1950 cascade-target-list
1951
1952 menubutton .top.service -text "Service" -underline 0 -menu .top.service.m
1953 menu .top.service.m
1954 .top.service.m add command -label "Database" -command {database-select}
1955 .top.service.m add cascade -label "Query type" -menu .top.service.m.querytype
1956 menu .top.service.m.querytype
1957 .top.service.m.querytype add radiobutton -label "RPN"
1958 .top.service.m.querytype add radiobutton -label "CCL"
1959 .top.service.m add cascade -label "Present" -menu .top.service.m.present
1960 menu .top.service.m.present
1961 .top.service.m.present add command -label "More" \
1962         -command [list present-more 10]
1963 .top.service.m.present add command -label "All" \
1964         -command [list present-more {}]
1965 .top.service configure -state disabled
1966
1967 menubutton .top.rset -text "Set" -menu .top.rset.m
1968 menu .top.rset.m
1969 .top.rset.m add command -label "Load" -command {load-set}
1970 .top.rset.m add separator
1971
1972 menubutton .top.options -text "Options" -underline 0 -menu .top.options.m
1973 menu .top.options.m
1974 .top.options.m add cascade -label "Choose query" -menu .top.options.m.clist
1975 .top.options.m add command -label "Define query" -command {new-query-dialog}
1976 .top.options.m add cascade -label "Edit query" -menu .top.options.m.slist
1977 menu .top.options.m.clist
1978 menu .top.options.m.slist
1979 cascade-query-list
1980
1981 menubutton .top.help -text "Help" -menu .top.help.m
1982 menu .top.help.m
1983
1984 .top.help.m add command -label "Help on help" \
1985         -command {tkerror "Help on help not available. Sorry"}
1986 .top.help.m add command -label "About" \
1987         -command {tkerror "About not available. Sorry"}
1988
1989 pack .top.file .top.target .top.service .top.rset .top.options -side left
1990 pack .top.help -side right
1991
1992 index-lines .lines 1 $queryButtonsFind [lindex $queryInfo 0] activate-index
1993
1994 button .mid.search -width 7 -text {Search} -command search-request \
1995         -state disabled
1996 button .mid.scan -width 7 -text {Scan} \
1997         -command [list scan-request "@attr 1=4 @attr 5=1 @attr 4=1"] -state disabled 
1998 button .mid.present -width 7 -text {Present} -command [list present-more 10] \
1999         -state disabled
2000
2001 button .mid.clear -width 7 -text {Clear} -command index-clear
2002 pack .mid.search .mid.scan .mid.present .mid.clear -side left \
2003         -fill y -padx 5 -pady 3
2004
2005 listbox .data.list -yscrollcommand {.data.scroll set} -font fixed -geometry 20x2
2006 scrollbar .data.scroll -orient vertical -border 1
2007 pack .data.list -side left -fill both -expand yes
2008 pack .data.scroll -side right -fill y
2009 .data.scroll config -command {.data.list yview}
2010
2011 button .bot.logo  -bitmap @book1 -command cancel-operation
2012 frame .bot.a
2013 pack .bot.a -side left -fill x
2014 pack .bot.logo -side right -padx 2 -pady 2
2015
2016 message .bot.a.target -text "" -aspect 1000 -border 1
2017
2018 label .bot.a.status -text "Not connected" -width 15 -relief \
2019         sunken -anchor w -border 1
2020 label .bot.a.set -text "" -width 5 -relief \
2021         sunken -anchor w -border 1
2022 label .bot.a.message -text "" -width 15 -relief \
2023         sunken -anchor w -border 1
2024
2025 pack .bot.a.target -side top -anchor nw -padx 2 -pady 2
2026 pack .bot.a.status .bot.a.set .bot.a.message \
2027         -side left -padx 2 -pady 2
2028
2029 ir z39
2030
2031 show-logo 1
2032