+proc alert {ask} {
+ set w .alert
+
+ global alertAnswer
+
+ toplevel $w
+ place-force $w .
+ top-down-window $w
+
+ message $w.top.message -text $ask
+
+ pack $w.top.message -side left -pady 6 -padx 20 -expand yes -fill x
+
+ set alertAnswer 0
+ top-down-ok-cancel $w {alert-action} 1
+ return $alertAnswer
+}
+
+proc alert-action {} {
+ global alertAnswer
+ set alertAnswer 1
+ destroy .alert
+}
+
+proc exit-action {} {
+ global settingsChanged
+
+ save-geometry
+ if {$settingsChanged} {
+ set a [alert "you havent saved your settings. Do you wish to save?"]
+ if {$a} {
+ save-settings
+ }
+ }
+ exit 0
+}
+
+proc listbuttonaction {w name h user i} {
+ $w configure -text [lindex $name 0]
+ $h [lindex $name 1] $user $i
+}
+
+proc listbuttonx {button no names handle user} {
+ if {[winfo exists $button]} {
+ $button configure -text [lindex [lindex $names $no] 0]
+ ${button}.m delete 0 last
+ } else {
+ menubutton $button -text [lindex [lindex $names $no] 0] \
+ -width 10 -menu ${button}.m -relief raised -border 1
+ menu ${button}.m
+ }
+ set i 0
+ foreach name $names {
+ ${button}.m add command -label [lindex $name 0] \
+ -command [list listbuttonaction ${button} $name \
+ $handle $user $i]
+ incr i
+ }
+}
+
+proc listbutton {button no names} {
+ menubutton $button -text [lindex $names $no] -width 10 -menu ${button}.m \
+ -relief raised -border 1
+ menu ${button}.m
+ foreach name $names {
+ ${button}.m add command -label $name \
+ -command [list ${button} configure -text $name]
+ }
+}
+
+proc query-add-index-action {queryNo} {
+ set w .setup-query-$queryNo
+
+ global queryInfoTmp
+ global queryButtonsTmp
+
+ lappend queryInfoTmp [list [.query-add-index.top.index.entry get] {}]
+
+ destroy .query-add-index
+ #destroy $w.top.lines
+ #frame $w.top.lines -relief ridge -border 2
+ index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
+ #pack $w.top.lines -side left -pady 6 -padx 6 -fill y
+}
+
+proc query-add-line {queryNo} {
+ set w .setup-query-$queryNo
+
+ global queryInfoTmp
+ global queryButtonsTmp
+
+ lappend queryButtonsTmp {I 0}
+
+ #destroy $w.top.lines
+ #frame $w.top.lines -relief ridge -border 2
+ index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
+ #pack $w.top.lines -side left -pady 6 -padx 6 -fill y
+}
+
+proc query-del-line {queryNo} {
+ set w .setup-query-$queryNo
+
+ global queryInfoTmp
+ global queryButtonsTmp
+
+ set l [llength $queryButtonsTmp]
+ if {$l <= 0} {
+ return
+ }
+ incr l -1
+ set queryButtonsTmp [lreplace $queryButtonsTmp $l $l]
+ index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
+}
+
+proc query-add-index {queryNo} {
+ set w .query-add-index
+
+ toplevel $w
+ place-force $w .setup-query-$queryNo
+ top-down-window $w
+ frame $w.top.index
+ pack $w.top.index \
+ -side top -anchor e -pady 2
+ entry-fields $w.top {index} \
+ {{Index Name:}} \
+ [list query-add-index-action $queryNo] {destroy .query-add-index}
+ top-down-ok-cancel $w [list query-add-index-action $queryNo] 1
+}
+
+proc query-setup-action {queryNo} {
+ global queryButtons
+ global queryInfo
+ global queryButtonsTmp
+ global queryInfoTmp
+ global queryButtonsFind
+ global queryInfoFind
+
+ set queryInfo [lreplace $queryInfo $queryNo $queryNo \
+ $queryInfoTmp]
+ set queryButtons [lreplace $queryButtons $queryNo $queryNo \
+ $queryButtonsTmp]
+ set queryInfoFind $queryInfoTmp
+ set queryButtonsFind $queryButtonsTmp
+
+ puts $queryInfo
+ puts $queryButtons
+ destroy .setup-query-$queryNo
+
+ index-lines .lines 1 $queryButtonsFind $queryInfoFind activate-index
+}
+
+proc activate-e-index {value no i} {
+ global queryButtonsTmp
+
+ puts $queryButtonsTmp
+ set queryButtonsTmp [lreplace $queryButtonsTmp $no $no [list I $i]]
+ puts $queryButtonsTmp
+ puts "value $value"
+ puts "no $no"
+ puts "i $i"
+}
+
+proc activate-index {value no i} {
+ global queryButtonsFind
+
+ set queryButtonsFind [lreplace $queryButtonsFind $no $no [list I $i]]
+
+ puts "queryButtonsFind $queryButtonsFind"
+ puts "value $value"
+ puts "no $no"
+ puts "i $i"
+}
+
+proc query-setup {queryNo} {
+ set w .setup-query-$queryNo
+ global queryTypes
+ set queryTypes {Simple}
+ global queryButtons
+ global queryInfo
+ global queryButtonsTmp
+ global queryInfoTmp
+
+ set queryName [lindex $queryTypes $queryNo]
+ set queryInfoTmp [lindex $queryInfo $queryNo]
+ set queryButtonsTmp [lindex $queryButtons $queryNo]
+
+ #set queryButtons { {I 0 I 1 I 2} }
+ #set queryInfo { { {Title ti} {Author au} {Subject sh} } }
+
+ toplevel $w
+
+ wm title $w "Query setup $queryName"
+ place-force $w .
+
+ top-down-window $w
+
+ frame $w.top.lines -relief ridge -border 2
+ frame $w.top.use -relief ridge -border 2
+ frame $w.top.relation -relief ridge -border 2
+ frame $w.top.position -relief ridge -border 2
+ frame $w.top.structure -relief ridge -border 2
+ frame $w.top.truncation -relief ridge -border 2
+ frame $w.top.completeness -relief ridge -border 2
+
+ # Index Lines
+
+ index-lines $w.top.lines 0 $queryButtonsTmp $queryInfoTmp activate-e-index
+
+ pack $w.top.lines -side left -pady 6 -padx 6 -fill y
+
+ # Use Attributes
+ pack $w.top.use -side left -pady 6 -padx 6 -fill y
+
+ label $w.top.use.label -text "Use"
+ listbox $w.top.use.list -geometry 20x10 \
+ -yscrollcommand "$w.top.use.scroll set"
+ scrollbar $w.top.use.scroll -orient vertical -border 1
+ pack $w.top.use.label -side top -fill x \
+ -padx 2 -pady 2
+ pack $w.top.use.list -side left -fill both -expand yes \
+ -padx 2 -pady 2
+ pack $w.top.use.scroll -side right -fill y \
+ -padx 2 -pady 2
+ $w.top.use.scroll config -command "$w.top.use.list yview"
+
+ foreach u {{Personal name} {Corporate name}} {
+ $w.top.use.list insert end $u
+ }
+ # Relation Attributes
+ pack $w.top.relation -pady 6 -padx 6 -side top
+
+ label $w.top.relation.label -text "Relation" -width 18
+
+ listbutton $w.top.relation.b 0\
+ {{None} {Less than} {Greater than or equal} \
+ {Equal} {Greater than or equal} {Greater than} {Not equal} \
+ {Phonetic} \
+ {Stem} {Relevance} {AlwaysMatches}}
+
+ pack $w.top.relation.label $w.top.relation.b -fill x
+
+ # Position Attributes
+ pack $w.top.position -pady 6 -padx 6 -side top
+
+ label $w.top.position.label -text "Position" -width 18
+
+ listbutton $w.top.position.b 0 {{None} {First in field} {First in subfield}
+ {Any position in field}}
+
+ pack $w.top.position.label $w.top.position.b -fill x
+
+ # Structure Attributes
+
+ pack $w.top.structure -pady 6 -padx 6 -side top
+
+ label $w.top.structure.label -text "Structure" -width 18
+
+ listbutton $w.top.structure.b 0 {{None} {Phrase} {Word} {Key} {Year}
+ {Date (norm)} {Word list} {Date (un-norm)} {Name (norm)} {Date (un-norm)}
+ {Structure} {urx} {free-form} {doc-text} {local-number} {string}
+ {numeric string}}
+
+ pack $w.top.structure.label $w.top.structure.b -fill x
+
+ # Truncation Attributes
+
+ pack $w.top.truncation -pady 6 -padx 6 -side top
+
+ label $w.top.truncation.label -text "Truncation" -width 18
+
+ listbutton $w.top.truncation.b 0 {{Auto} {Right} {Left} {Left and right} \
+ {No truncation} {Process #} {Re-1} {Re-2}}
+ pack $w.top.truncation.label $w.top.truncation.b -fill x
+
+ # Completeness Attributes
+
+ pack $w.top.completeness -pady 6 -padx 6 -side top
+
+ label $w.top.completeness.label -text "Truncation" -width 18
+
+ listbutton $w.top.completeness.b 0 {{None} {Incomplete subfield} \
+ {Complete subfield} {Complete field}}
+ pack $w.top.completeness.label $w.top.completeness.b -fill x
+
+ # Ok-cancel
+ bottom-buttons $w [list \
+ {Ok} [list query-setup-action $queryNo] \
+ {Add index} [list query-add-index $queryNo] \
+ {Add line} [list query-add-line $queryNo] \
+ {Delete line} [list query-del-line $queryNo] \
+ {Cancel} [list destroy $w]] 0
+}
+
+proc index-clear {} {
+ global queryButtonsFind
+
+ set i 0
+ foreach b $queryButtonsFind {
+ .lines.$i.e delete 0 end
+ incr i
+ }
+}
+
+proc index-query {} {
+ global queryButtonsFind
+ global queryInfoFind
+
+ set i 0
+ set qs {}
+
+ foreach b $queryButtonsFind {
+ set term [string trim [.lines.$i.e get]]
+ if {$term != ""} {
+ set attr [lindex [lindex $queryInfoFind [lindex $b 1]] 1]
+
+ set term "\{${term}\}"
+ foreach a $attr {
+ set term "@attr $a ${term}"
+ }
+ if {$qs != ""} {
+ set qs "@and ${qs} ${term}"
+ } else {
+ set qs $term
+ }
+ }
+ incr i
+ }
+ puts "qs= $qs"
+ return $qs
+}
+
+proc index-lines {w realOp buttonInfo queryInfo handle} {
+ set i 0
+ foreach b $buttonInfo {
+ if {! [winfo exists $w.$i]} {
+ frame $w.$i -background white -border 1
+ }
+ listbuttonx $w.$i.l [lindex $b 1] $queryInfo $handle $i
+
+ if {$realOp} {
+ if {! [winfo exists $w.$i.e]} {
+ entry $w.$i.e -width 32 -relief sunken -border 1
+ bind $w.$i.e <FocusIn> [list $w.$i configure \
+ -background red]
+ bind $w.$i.e <FocusOut> [list $w.$i configure \
+ -background white]
+ pack $w.$i.l -side left
+ pack $w.$i.e -side left -fill x -expand yes
+ pack $w.$i -side top -fill x -padx 2 -pady 2
+ bind $w.$i.e <Left> [list left-cursor $w.$i.e]
+ bind $w.$i.e <Right> [list right-cursor $w.$i.e]
+ bind $w.$i.e <Return> search-request
+ }
+ } else {
+ pack $w.$i.l -side left
+ pack $w.$i -side top -fill x -padx 2 -pady 2
+ }
+ incr i
+ }
+ set j $i
+ while {[winfo exists $w.$j]} {
+ destroy $w.$j
+ incr j
+ }
+ if {! $realOp} {
+ return
+ }
+ set j 0
+ incr i -1
+ while {$j < $i} {
+ set k [expr $j+1]
+ bind $w.$j.e <Tab> "focus $w.$k.e"
+ set j $k
+ }
+ if {$i >= 0} {
+ bind $w.$i.e <Tab> "focus $w.0.e"
+ focus $w.0.e
+ }
+}
+
+proc search-fields {w buttondefs} {
+ set i 0
+ foreach buttondef $buttondefs {
+ frame $w.$i -background white
+
+ listbutton $w.$i.l 0 $buttondef
+ entry $w.$i.e -width 32 -relief sunken
+
+ pack $w.$i.l -side left
+ pack $w.$i.e -side left -fill x -expand yes
+
+ pack $w.$i -side top -fill x -padx 2 -pady 2
+
+ bind $w.$i.e <Left> [list left-cursor $w.$i.e]
+ bind $w.$i.e <Right> [list right-cursor $w.$i.e]
+
+ incr i
+ }
+ set j 0
+ incr i -1
+ while {$j < $i} {
+ set k [expr $j+1]
+ bind $w.$j.e <Tab> "focus $w.$k.e \n
+ $w.$k configure -background red \n
+ $w.$j configure -background white"
+ set j $k
+ }
+ bind $w.$i.e <Tab> "focus $w.0.e \n
+ $w.0 configure -background red \n
+ $w.$i configure -background white"
+ focus $w.0.e
+ $w.0 configure -background red
+}
+
+if {[info exists windowGeometry(.)]} {
+ set g $windowGeometry(.)
+ if {$g != ""} {
+ wm geometry . $g
+ }
+}
+
+read-formats
+
+frame .top -border 1 -relief raised
+frame .lines -border 1 -relief raised