+
+#---------- common to smashing and filtering ----------
+
+proc make-control {parent ctrl label ekind} {
+ debug "MAKE-CONTROL [list $parent $ctrl $label $ekind]"
+ label $parent.lab_$ctrl -text $label -justify left
+ $ekind $parent.$ctrl
+ manyset [grid size $parent] dummy row
+ incr row
+ grid configure $parent.lab_$ctrl -row $row -column 0 -sticky nw -pady 4
+ grid configure $parent.$ctrl -row $row -column 1 -sticky w -pady 3
+ return $parent.$ctrl
+}
+
+proc begin-control-grid {cw count rows inrow} {
+ if {!$inrow} { set inrow [expr {($count + $rows) / $rows}] }
+ upvar #0 control_grid_properties($cw) props
+ set props [list $rows $inrow]
+ return $cw
+}
+
+proc make-control-grid-entry {cw kind ix ekind args} {
+ upvar #0 control_grid_properties($cw) props
+ manyset $props rows inrow
+
+ set ew $cw.$ix
+ eval [list $ekind $ew] $args
+
+ switch -exact $kind {
+ ix {
+ grid configure $ew -sticky sw \
+ -row [expr {$ix / $inrow}] \
+ -column [expr {$ix % $inrow}]
+ }
+ final {
+ grid configure $ew -sticky se \
+ -row [expr {$rows-1}] \
+ -column [expr {$inrow-1}]
+ }
+ default {
+ error "$kind ?"
+ }
+ }
+ return $ew
+}
+
+#---------- smashing ----------
+
+set smash_subclass 0
+set smash_owner 0
+
+proc smash-code {code} {
+ manyset [split $code _] inport class subclass lockown xabbrev
+
+ global smash_subclass
+ if {$smash_subclass > 1} {
+ set subclass {}
+ } elseif {$smash_subclass && [string length $subclass]} {
+ set subclass !
+ }
+
+ global smash_owner
+ switch $smash_owner {
+ 0 { }
+ 1 { regsub {[12]$} $lockown 5 lockown }
+ 2 {
+ if {[regexp {^0.} $lockown]} {
+ # battle ready / all lock states
+ set lockown 03
+ } elseif {[regexp {^.0} $lockown]} {
+ # not battle ready / yours
+ set lockown 40
+ } else {
+ # state (not battle ready) / not known to be yours
+ regsub {.$} $lockown 4 lockown
+ }
+ }
+ 3 { regsub {.$} $lockown {3} lockown }
+ 4 { set lockown 33 }
+ }
+
+ return [join [list $inport $class $subclass $lockown $xabbrev] _]
+}
+
+proc make-smasher {sma label ekind} {
+ return [make-control .smash $sma $label $ekind]
+}
+
+proc make-radio-smasher {sma label variable descs rows inrow} {
+ set w [make-smasher $sma $label frame]
+ begin-control-grid $w [llength $descs] $rows $inrow
+ for {set i 0} {$i < [llength $descs]} {incr i} {
+ make-control-grid-entry $w ix $i \
+ radiobutton \
+ -variable $variable -value $i -command redraw-needed \
+ -text [lindex $descs $i]
+ }
+}
+
+#---------- filtering ----------
+
+set filters {}
+
+proc filter-values/size {} { global vc_codes; return $vc_codes }
+proc filter-icon/size {code} {
+ upvar #0 vc_code2abbrev($code) abb
+ return icon/$abb
+}
+proc filter-default/size {code} { return 1 }
+proc filter-says-yes/size {codel} {
+ set sizecode [lindex $codel 1]
+ upvar #0 filter_size($sizecode) yes
+ return $yes
+}
+
+proc filter-values/lockown {} {
+ foreach lv {0 1 2} {
+ foreach ov {0 1 2} {
+ lappend vals "$lv$ov"
+ }
+ }
+ return $vals
+}
+proc filter-icon/lockown {lockown} { return [code-lockown2icon $lockown] }
+proc filter-default/lockown {lockown} {
+ return [regexp {^[01]|^2[^1]} $lockown]
+}
+proc filter-says-yes/lockown {codel} {
+ set lockown [lindex $codel 3]
+ upvar #0 filter_lockown($lockown) yes
+ debug "FILTER-SAYS-YES/LOCKOWN $codel $lockown $yes"
+ return $yes
+}
+
+proc filter-validate/xabbre {re} {
+ if {[catch {
+ regexp -- $re {}
+ } emsg]} {
+ regsub {^.*:\s*} $emsg {} emsg
+ regsub {^.*(.{30})$} $emsg {\1} emsg
+ return $emsg
+ }
+ return {}
+}
+proc filter-says-yes/xabbre {codel} {
+ global filter_xabbre
+ set xabbrev [lindex $codel 4]
+ return [regexp -- $filter_xabbre $xabbrev]
+}
+
+proc filter-tickbox-flip {fil} {
+ upvar #0 filter_$fil vars
+ set values [filter-values/$fil]
+ foreach val $values {
+ set vars($val) [expr {!$vars($val)}]
+ }
+ redraw-needed
+}
+
+proc make-tickbox-filter {fil label rows inrow} {
+ upvar #0 filter_$fil vars
+ set values [filter-values/$fil]
+ set count [llength $values]
+ set noicons [catch { info args filter-icon/$fil }]
+
+ set fw [make-filter $fil $label frame]
+ begin-control-grid $fw $count $rows $inrow
+
+ for {set ix 0} {$ix < $count} {incr ix} {
+ set val [lindex $values $ix]
+ set vars($val) [filter-default/$fil $val]
+ set ew [make-control-grid-entry $fw ix $ix checkbutton \
+ -variable filter_${fil}($val) \
+ -font fixed \
+ -command [list redraw-needed tickbox-filter $fil $val]]
+ if {!$noicons} {
+ $fw.$ix configure -image [filter-icon/$fil $val] -height 16
+ } else {
+ $fw.$ix configure -text [filter-map/$fil $val]
+ }
+ }
+ [make-control-grid-entry $fw final invert button] \
+ configure \
+ -text flip -command [list filter-tickbox-flip $fil] \
+ -padx 0 -pady 0
+}
+
+proc entry-filter-changed {fw fil n1 n2 op} {
+ global errorInfo
+ upvar #0 filter_$fil realvar
+ upvar #0 filterentered_$fil entryvar
+ global def_background
+ debug "entry-filter-changed $fw $fil $entryvar"
+ if {[catch {
+ set error [filter-validate/$fil $entryvar]
+ if {[string length $error]} {
+ $fw.error configure -text $error -foreground white -background red
+ } else {
+ $fw.error configure -text { } -background $def_background
+ set realvar $entryvar
+ redraw-needed
+ }
+ } emsg]} {
+ puts stderr "FILTER CHECK ERROR $emsg $errorInfo"
+ }
+}
+
+proc make-entry-filter {fil label def} {
+ global filterentered_$fil
+ upvar #0 filter_$fil realvar
+ set realvar $def
+ set fw [make-filter $fil $label frame]
+ entry $fw.entry -textvariable filterentered_$fil
+ label $fw.error
+ glset def_background [$fw.error cget -background]
+ trace add variable filterentered_$fil write \
+ [list entry-filter-changed $fw $fil]
+ pack $fw.entry $fw.error -side top -anchor w
+}
+
+proc make-filter {fil label ekind} {
+ global filters
+ lappend filters $fil
+ return [make-control .filter $fil $label $ekind]
+}
+
+proc make-filters {} {
+ make-tickbox-filter size Size 2 0
+ make-tickbox-filter lockown "Lock/\nowner" 2 6
+ make-entry-filter xabbre "Flags\n regexp" {}
+}
+
+proc filterstyle-changed {n1 n2 op} {
+ global filterstyle
+ debug "filterstyle-changed $filterstyle"
+ redraw-needed
+}
+
+proc filters-say-yes {code} {
+ global filters filterstyle
+ set codel [split $code _]
+ set lockown [lindex $codel 3]
+ switch -exact $filterstyle {
+ 0 { return 1 }
+ 1 { return [filter-default/lockown $lockown] }
+ 2 { return [regexp {^.0} $lockown] }
+ 3 { }
+ default { error $filterstyle }
+ }
+
+ foreach fil $filters {
+ if {![filter-says-yes/$fil $codel]} {
+ debug "FILTERS-SAY-YES $code NO $fil"
+ return 0
+ }
+ }
+ debug "FILTERS-SAY-YES $code YES $filters"
+ return 1
+}