home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-12-20 | 20.0 KB | 759 lines | [TEXT/ALFA] |
- # Menu creation procs
-
- namespace eval menu {}
- namespace eval global {}
- namespace eval file {}
-
- proc menu::buildBasic {} {
- global winMenu HOME
- # These are built on the fly
- menu -n File -p menu::generalProc {}
- menu -n Edit -p menu::generalProc {}
- menu -n Text -p menu::generalProc {}
- menu -n Search {}
- menu -n Utils {}
- menu -n Config {}
- menu -n $winMenu {}
-
- insertMenu "File"
- insertMenu "Edit"
- insertMenu "Text"
- insertMenu "Search"
- insertMenu "Utils"
- insertMenu "Config"
- insertMenu $winMenu
-
- if {![catch {glob "$HOME:Help:*"} files]} {
- set men { "Alpha Manual" "Quick Start" "Alpha Commands" "Tcl Commands" \
- "(-" "Readme" "Changes" \
- "Extending Alpha" "Bug Reports and Debugging" "(-" }
- foreach f $men {
- if {$f != "(-" && ![file exists "${HOME}:Help:$f"]} {
- set men [lremove $men $f]
- }
- }
- set ignore ""
- foreach f [lsort $files] {
- set f [file tail $f]
- if {[lsearch $men $f] < 0 && [lsearch $ignore $f] < 0} {
- lappend men $f
- }
- }
- regsub -all {\(-[ \t\r\n]+\(-} $men {\(-} men
- foreach f $men {
- addHelpMenu $f
- }
- }
-
- }
-
- proc menu::buildwinMenu {} {
- global winMenu winNameToNum
- set ma {
- "//<Szoom"
- "//<S<I<OsinglePage"
- "<S/;chooseAWindow"
- "/I<Biconify"
- {menu -n arrange -p menu::winTileProc {
- "/Jvertically^1"
- "/J<O<Ihorizontally^2"
- "/J<B<OunequalVert^6"
- "/J<B<I<OunequalHor^5"
- "(-"
- {menu -n other {
- {bufferOtherWindow}
- {iconify}
- {nextWin}
- {nextWindow}
- {prevWindow}
- {shrinkFull}
- {shrinkHigh}
- {shrinkLeft}
- {shrinkLow}
- {shrinkRight}
- {singlePage}
- {swapWithNext}
- {zoom}
- }}}}
- "(-"
- "/msplitWindow"
- "/otoggleScrollbar"
- "(-"
- }
- # We may be reloading, so add whatever windows we have
- if {[info exists winNameToNum]} {
- set nms [array names winNameToNum]
- foreach name $nms {
- regexp {[^:]*$} $name item
- set num $winNameToNum($name)
- if {$num < 10} {
- lappend ma /$num${item}
- } else {
- lappend ma ${item}
- }
- }
- }
- return [list "build" $ma menu::winProc "" $winMenu]
- }
-
- proc global::listAllBindings {} {
- new -n {* All Key Bindings *} -m Tcl
- insertText [bindingList]
- winReadOnly
- }
-
- proc global::listGlobalBindings {} {
- global modeMenus
- new -n {* Global Key Bindings *} -m Tcl
- set text ""
- foreach b [split [bindingList] "\r"] {
- set lst [lindex $b end]
- if {[lsearch [lsort -ignore [array names modeMenus]] $lst] < 0} {
- append text "$b\r"
- }
- }
- insertText $text
- winReadOnly
- }
-
- proc global::listPackages {} {
- cache::read index::maintainer
- foreach i [array names index::maintainer] {
- set j [lindex [set index::maintainer($i)] 1]
- set au($i) "[lindex $j 0], [lindex $j 1]"
- }
- new -n {* Installed Packages *} -m Text
- append t "Currently installed packages\r\r"
- append t "columns are: name, version, and maintainer\r\r"
- append t "Extensions ('•' = active):"
- insertText $t ; set t ""
- foreach p [lsort -ignore [alpha::package names -extension]] {
- foreach v [alpha::package versions $p] {
- append t "\r[format {%s %-25s %-10s } [package::active $p {• { }}] $p $v]"
- if [info exists au($p)] {append t $au($p)}
- }
- }
- append t "\r\rMenus:"
- insertText $t ; set t ""
- foreach p [lsort -ignore [alpha::package names -menu]] {
- foreach v [alpha::package versions $p] {
- append t "\r[format { %-25s %-10s } $p $v]"
- if [info exists au($p)] {append t $au($p)}
- }
- }
- append t "\r\rModes:"
- insertText $t ; set t ""
- foreach p [lsort -ignore [alpha::package names -mode]] {
- foreach v [alpha::package versions $p] {
- append t "\r[format { %-8s %-10s } $p $v]"
- if [info exists au($p)] {append t $au($p)}
- }
- }
- insertText $t ; set t ""
- winReadOnly
- shrinkWindow
- }
-
- proc global::listFunctions {} {
- global win::Modes
- new -n {* Functions *} -m Tcl
- insertText "===\r\tCommand-double-click on a function to see its definition\r===\r\r" [join [lsort -ignore [info commands]] "\r"] "\r"
- winReadOnly
- }
-
- proc global::menus {} {
- global menus globalMenus_curr modifiedVars modeMenus mode
-
- set newGlobals [dialog::pickMenus global]
- set removed [lremove -l $globalMenus_curr $newGlobals]
- set added [lremove -l $newGlobals $globalMenus_curr]
- set globalMenus_curr $newGlobals
- lappend modifiedVars globalMenus_curr
- # now synchronise menus, paying attention to current mode menus
- if {[info exists modeMenus($mode)]} {
- set removed [lremove -l $removed $modeMenus($mode)]
- set added [lremove -l $added $modeMenus($mode)]
- }
- # remove removed menus
- foreach m $removed {
- global $m
- if {[info exists $m]} {
- catch "removeMenu [set $m]"
- catch {markMenuItem packageMenus $m 0}
- }
- }
- foreach m $added {
- global $m
- catch $m
- insertMenu [set $m]
- catch {markMenuItem packageMenus $m 1}
- }
- }
-
- proc global::insertAllMenus {} {
- global globalMenus_curr
- foreach m $globalMenus_curr {
- if {![catch $m res]} {
- global $m
- if {[info exists $m]} {
- insertMenu [set $m]
- }
- } else {
- alpha::error $res
- lappend problems $m
- }
- }
- if {[info exists problems]} {
- alertnote "Problems building menus: $problems"
- }
- }
-
- ##
- # -------------------------------------------------------------------------
- #
- # "menu::buildProc" --
- #
- # Register a procedure to be the 'build proc' for a given menu. This
- # procedure can do one of two things:
- #
- # i) build the entire menu, including evaluating the 'menu ...' command.
- # In this case the build proc should return anything which doesn't
- # begin 'build ...'
- #
- # ii) build up part of the menu, and then allow pre-registered menu
- # insertions/replacements to take-effect. In this case the procedure
- # should return a list of the items (listed by index):
- #
- # 0: "build"
- # 1: list-of-items-in-the-menu
- # 2: list of other flags. If the list doesn't contain '-p', we use
- # the standard menu::generalProc procedure. If it does contain '-p'
- # general prmenu procedure to call when an item is selected.
- # If nothing is given,
- # or if '-1' is given, then we don't have a procedure. If "" is given,
- # we use the standard 'menu::generalProc' procedure. Else we use the
- # given procedure.
- # 3: list of submenus which need building.
- # 4: over-ride for the name of the menu.
- #
- # You must register the build-proc before attempting to build the menu.
- # Once registered, any call of 'menu::buildSome name' will build your
- # menu.
- # -------------------------------------------------------------------------
- ##
- proc menu::buildProc {name proc} {
- global menu::build_procs
- set menu::build_procs($name) $proc
- }
-
- ##
- # -------------------------------------------------------------------------
- #
- # "menu::insert" --
- #
- # name, type, where, then list of items. type = 'items' 'submenu'
- #
- # Add given items to a given menu, provided they are not already there.
- # Rebuild that menu if necessary.
- #
- # There are also procs 'menu::removeFrom' which does the opposite of
- # this one, and 'menu::replaceWith' which replaces a given menu item
- # with others.
- # -------------------------------------------------------------------------
- ##
- proc menu::insert {name args} {
- if {[llength $args] < 3} { error "Too few args to menu::insert" }
- global menu::additions alpha::noMenusYet
- if [info exists menu::additions($name)] {
- set a [set menu::additions($name)]
- if {[lsearch -exact $a $args] != -1} {
- return
- }
- # check if it's there but in a different place; we over-ride
- set dblchk [lreplace $args 1 1 "*"]
- if {[set i [lsearch -glob $a $dblchk]] == -1} {
- unset i
- }
- }
- if [info exists i] {
- set menu::additions($name) [lreplace $a $i $i $args]
- } else {
- lappend menu::additions($name) $args
- }
- if ![info exists alpha::noMenusYet] {
- # we were called after start-up; build the menu now
- menu::buildSome $name
- }
- }
-
- proc menu::uninsert {name args} {
- global menu::additions alpha::noMenusYet
- set a [set menu::additions($name)]
- if {[set idx [lsearch -exact $a $args]] == -1} {
- return
- }
- set menu::additions($name) [lreplace $a $idx $idx]
- if ![info exists alpha::noMenusYet] {
- # we were called after start-up; build the menu now
- menu::buildSome $name
- }
- }
-
- proc alpha::buildMainMenus {} {
- menu::buildProc alphaDownloads remote::makeDownloadsMenu
- menu::buildProc packages package::makeMenu
- menu::buildProc global menu::globalBuild
- menu::buildProc mode menu::modeBuild
- menu::buildProc winMenu menu::buildwinMenu
- menu::buildProc preferences menu::preferencesBuild
- uplevel #0 {
- source "$HOME:Tcl:SystemCode:alphaMenus.tcl"
- menu::buildSome "File" "Edit" "Text" "Search" "Utils" "Config" "winMenu"
- }
- }
-
- ##
- # -------------------------------------------------------------------------
- #
- # "menu::buildSome" --
- #
- # Important procedure which builds all known/registered menus from a
- # number of pieces. It allows the inclusion of menus pieces registered
- # with the menu::insert procedure, which allows you easily to add items
- # (including dynamic and hierarchial) to any of Alpha's menus.
- #
- # Results:
- # Various menus are (re)built
- #
- # Side effects:
- # Items added to those menus with 'addMenuItem' will vanish.
- #
- # --Version--Author------------------Changes-------------------------------
- # 1.0 <darley@fas.harvard.edu> original
- # 2.0 <darley@fas.harvard.edu> more compact, more like tk
- # -------------------------------------------------------------------------
- ##
- proc menu::buildSome {args} {
- set msubs ""
- foreach token $args {
- eval lappend msubs [menu::buildOne $token]
- }
- # build sub-menus of those built
- if {$msubs != ""} {eval menu::buildSome $msubs}
- }
-
- proc menu::buildOne {args} {
- global menu::additions menu::build_procs alpha::noMenusYet \
- menu::items
- set token [lindex $args 0] ; set args [lrange $args 1 end]
- if {[set len [llength $args]] > 0 || [info exists menu::build_procs($token)]} {
- if {$len > 0} {
- set res $args
- } else {
- if [catch "[set menu::build_procs($token)]" res] {
- alpha::error "The menu $token had a problem starting up ; $res"
- }
- }
- switch -- [lindex $res 0] {
- "build" {
- set ma [lindex $res 1]
- if {[llength $res] > 2} {
- set theotherflags [lrange [lindex $res 2] 1 end]
- if {[lindex [lindex $res 2] 0] != -1} {
- set mproc [lindex [lindex $res 2] 0]
- }
- if {[lindex $res 3] != ""} {
- eval lappend msubs [lindex $res 3]
- }
- if {[lindex $res 4] != ""} { set name [lindex $res 4] }
- }
- } "menu" {
- eval $res
- return ""
- } default {
- return ""
- }
- }
- } else {
- set ma ""
- if [info exists menu::items($token)] {
- set ma [set menu::items($token)]
- global menu::proc menu::which_subs menu::otherflags
- if [info exists menu::proc($token)] {
- set mproc [set menu::proc($token)]
- }
- if [info exists menu::which_subs($token)] {
- eval lappend msubs [set menu::which_subs($token)]
- }
- if [info exists menu::otherflags($token)] {
- set theotherflags [set menu::otherflags($token)]
- }
- }
- }
-
- if ![info exists name] { set name $token }
- # add any registered items and make the menu contents
- if [info exists menu::additions($token)] {
- foreach ins [set menu::additions($token)] {
- set where [lindex $ins 1]
- set type [lindex $ins 0]
- set ins [lrange $ins 2 end]
- switch -- $type {
- "submenu" {
- lappend msubs [lindex $ins 0]
- set ins [list [list menu -n [lindex $ins 0] {}]]
- }
- }
- switch -- [lindex $where 0] {
- "replace" {
- set old [lindex $where 1]
- if {[set ix [eval llindex ma $old]] != -1} {
- set ma [eval [list lreplace $ma $ix [expr $ix -1 + [llength $old]]] $ins]
- } else {
- alertnote "Bad menu::replacement registered '$old'"
- }
-
- }
- "end" {
- eval lappend ma $ins
- }
- default {
- set ma [eval linsert [list $ma] $where $ins]
- }
- }
- }
- }
- regsub -all {"?\(-"?([ \t\r\n]+"?\(-"?)+} $ma "(-" ma
- regsub -all {(^[ \t\r\n]*"?\(-"?|"?\(-"?[ \t\r\n]*$)} $ma "" ma
- # build the menu
- set name [list -n $name]
- if [info exists theotherflags] {
- set name [concat $theotherflags $name]
- }
- if [info exists mproc] {
- if {$mproc != ""} {
- eval menu $name -p $mproc [list $ma]
- } else {
- eval menu $name [list $ma]
- }
- } else {
- eval menu $name -p menu::generalProc [list $ma]
- }
- if [info exists msubs] {
- return $msubs
- }
- return ""
- }
-
- proc menu::replaceRebuild {name title} {
- global $name
- catch {removeMenu [set $name]}
- set $name $title
- menu::buildSome $name
- insertMenu [set $name]
- }
-
- proc menu::globalBuild {} {
- global alpha::package_menus package::prefs
- cache::read index::menu
- lappend ma "menus…" \
- {menu -n preferences {}}
- if [info exists alpha::package_menus] {
- global globalMenus_curr
- set i [list menu -n packageMenus -p menu::menuPackages]
- regsub -all "([join $globalMenus_curr |])" \
- [lsort -ignore ${alpha::package_menus}] "!•\\1" j
- lappend ma [concat $i [list $j]]
- }
- lappend ma "editPrefsFile" "(-"
- if [info exists package::prefs] {
- foreach pkg ${package::prefs} {
- lappend ma "${pkg}Prefs…"
- }
- }
- lappend ma "(-" "specialKeys…" "listGlobalBindings" \
- "listPackages" "listAllBindings" "listFunctions"
- return [list build $ma menu::globalProc preferences]
- }
-
- proc menu::menuPackages {menu m} {
- if [package::helpOrDescribe $m] {
- return
- }
- # toggle global existence of '$m' menu
- global globalMenus_curr modifiedVars
- if {[set idx [lsearch $globalMenus_curr $m]] == -1} {
- lappend globalMenus_curr $m
- global $m
- catch $m
- insertMenu [set $m]
- markMenuItem packageMenus $m 1
- } else {
- set globalMenus_curr [lreplace $globalMenus_curr $idx $idx]
- global $m
- catch "removeMenu [set $m]"
- markMenuItem packageMenus $m 0
- }
- lappend modifiedVars globalMenus_curr
- }
-
- proc menu::modeBuild {} {
- set ma [list "menus…" "/ppreferences…" "editPrefsFile" \
- "loadPrefsFile…" "describeMode" "(-" "/m<Uchange…"]
- return [list build $ma mode::menuProc "" currentMode]
- }
-
- proc menu::preferencesBuild {} {
- global flagPrefs
- set ma [list [menu::itemWithIcon "Interface Preferences" 84]]
- lappend ma Tiling Window Wrapping Gui "(-" \
- [menu::itemWithIcon "Standard Preferences" 84]
- lappend ma Backups Electrics Miscellaneous Printer Tags WWW "(-" \
- [menu::itemWithIcon "Other Preferences" 84]
- eval lunion ma [lsort [array names flagPrefs]]
- return [list build $ma {dialog::preferences -m}]
- }
-
- proc menu::removeFrom {name args} {
- global menu::additions alpha::noMenusYet
- if [info exists menu::additions($name)] {
- if {[set i [lsearch -exact [set menu::additions($name)] $args]] != -1} {
- set menu::additions($name) [lreplace [set menu::additions($name)] $i $i]
- if ![info exists alpha::noMenusYet] {
- # we were called after start-up; build the menu now
- menu::buildSome $name
- }
- }
- }
- }
-
- proc menu::replaceWith {name current type args} {
- global menu::additions alpha::noMenusYet
- if ![info exists menu::additions($name)] {
- lappend menu::additions($name) [concat [list $type [list replace $current]] $args]
- } else {
- set add 1
- set j 0
- foreach i [set menu::additions($name)] {
- if {[lrange $i 0 1] == [list $type [list replace $current]]} {
- if {[lindex $i 1] != $args} {
- set add 0
- set menu::additions($name) \
- [lreplace [set menu::additions($name)] $j $j \
- [concat [list $type [list replace $current]] $args]]
- break
- } else {
- # no change
- return
- }
- }
- incr j
- }
- if $add {
- lappend menu::additions($name) [concat [list $type [list replace $current]] $args]
- }
- }
- if ![info exists alpha::noMenusYet] {
- # we were called after start-up; build the menu now
- menu::buildSome $name
- }
- }
-
- proc menu::itemWithIcon {name icon} {
- return "/\x1e${name}^[text::Ascii $icon 1]"
- }
-
- proc file::open {} {findFile}
- proc file::close {} {killWindow}
-
- ##
- # -------------------------------------------------------------------------
- #
- # "menu::generalProc" --
- #
- # If either 'item' or 'menu::item' exists, call it. Else try and
- # autoload 'item', if that fails try and autoload 'menu::item'
- # -------------------------------------------------------------------------
- ##
- if {[info tclversion] < 8.0} {
- proc menu::generalProc {menu item} {
- set menu [string tolower $menu]
- if {[info commands ${menu}::${item}] != ""} {
- uplevel \#0 ${menu}::$item
- } elseif {[info commands $item] != ""} {
- uplevel \#0 $item
- } elseif {[catch {${menu}::$item}]} {
- if {[info commands ${menu}::$item] == ""} {
- uplevel \#0 $item
- }
- }
- }
- } else {
- proc menu::generalProc {menu item} {
- set menu [string tolower $menu]
- if {[info commands ::${menu}::${item}] != ""} {
- uplevel \#0 ::${menu}::$item
- } elseif {[info commands $item] != ""} {
- uplevel \#0 $item
- } elseif {[catch {::${menu}::$item}]} {
- if {[info commands ::${menu}::$item] == ""} {
- if {[catch {${menu}::$item}]} {
- if {[info commands ::${menu}::$item] == ""} {
- uplevel \#0 $item
- }
- }
- }
- }
- }
- }
-
- proc menu::globalProc {menu item} {
- global package::prefs
- if [regexp "(.*)Prefs" $item d pkg] {
- if [lcontains package::prefs $pkg] {
- dialog::pkg_options $pkg
- return
- }
- }
- menu::generalProc $menu $item
- }
-
- ##
- # proc namedClipMenuProc {menu item} {
- # switch $item {
- # "copy" "copyNamedClipboard"
- # "cut" "cutNamedClipboard"
- # "paste" "pasteNamedClipboard"
- # }
- # }
- ##
-
- proc menu::colorProc {menu item} {
- global colorInds modifiedArrVars
- if {[info exists colorInds($item)]} {
- set color [eval [list colorTriple "New \"$item\":"] $colorInds($item)]
- } else {
- switch -- $item {
- foreground { set inds "0 0 0" }
- background { set inds "65535 65535 65535" }
- blue { set inds "0 0 65535" }
- cyan { set inds "61404 11464 34250" }
- green { set inds "1151 33551 8297" }
- magenta { set inds "44790 1591 51333" }
- red { set inds "65535 0 0" }
- white { set inds "65535 65535 65535" }
- yellow { set inds "61834 64156 12512" }
- default { set inds "65535 65535 65535" }
- }
- set color [eval [list colorTriple "New \"$item\":"] $inds]
- }
- eval setRGB $item $color
-
- set colorInds($item) $color
- alpha::makeColourList
- lappend modifiedArrVars colorInds
- }
-
- proc alpha::makeColourList {} {
- global alpha::colors colorInds alpha::basiccolors
- # Set up color indices
- foreach ind [array names colorInds] {
- eval setRGB $ind $colorInds($ind)
- }
- set alpha::basiccolors {none blue cyan green magenta red white yellow}
- set alpha::colors ${alpha::basiccolors}
- foreach c {color_9 color_10 color_11 color_12 color_13 color_14 color_15} {
- if [info exists colorInds($c)] {lappend alpha::colors $c}
- }
- }
-
-
-
- #===============================================================================
- proc helpMenu {item} {
- global HOME
- edit -r -c "$HOME:Help:$item"
- }
-
- ##
- # -------------------------------------------------------------------------
- #
- # "alphaHelp" --
- #
- # Called from about box
- # -------------------------------------------------------------------------
- ##
- proc alphaHelp {} {
- global HOME
- if [file exists [set f "$HOME:Help:Alpha Manual"]] {
- edit -r -c $f
- } else {
- edit -r -c "$HOME:Help:Quick Start"
- }
- }
-
- proc register {} {
- global HOME
- launch -f "$HOME:Register"
- }
-
- namespace eval icon {}
- namespace eval file {}
-
- proc icon::FromSig {sig} {
- global alpha::_icons
- if {[set p [lsearch -glob ${alpha::_icons} "${sig} *"]] != -1} {
- set p [lindex ${alpha::_icons} $p]
- return [lindex $p 2]
- } else {
- return ""
- }
- }
-
- proc icon::MenuFromSig {sig} {
- global alpha::_icons
- if {[set p [lsearch -glob ${alpha::_icons} "${sig} *"]] != -1} {
- set char [expr [lindex [lindex ${alpha::_icons} $p] 2] -208]
- if {$char < 1 || $char > 256} { return "" }
- return "^[text::Ascii $char 1]"
- } else {
- return ""
- }
- }
-
- proc file::getSig {f} {
- if [catch {getFileInfo $f arr}] { return "" }
- return $arr(creator)
- }
-
-
- proc menu::fileUtils {menu item} {
- if {[lsearch -exact {"insertPathName" "insertFile" "fileRemove" "fileInfo" "wordCount" "textToAlpha"} $item] != -1} {return [$item]}
- switch -- $menu {
- "moreUtils" {
- file::Utils::$item
- }
- default {
- file::$item
- }
- }
- }
-
- proc menu::winTileProc {menu item} {
- win$item
- }
-
- proc menu::reinterpretOldMenu {args} {
- set ma [lindex $args end]
- set args [lreplace $args end end]
- getOpts {-n -M -p}
- if [info exists opts(-p)] {
- lappend proc $opts(-p)
- } else {
- lappend proc "-1"
- }
- if [info exists opts(-M)] { lappend proc -M $opts(-m) }
- if [info exists opts(-m)] { lappend proc -m }
- menu::buildOne $opts(-n) build $ma $proc
- }
-