home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / HTML and CSS Modes / htmlMenusAndKeys.tcl < prev    next >
Encoding:
Text File  |  2001-01-12  |  6.3 KB  |  202 lines

  1. ## -*-Tcl-*-
  2.  # ###################################################################
  3.  #  HTML mode - tools for editing HTML documents
  4.  # 
  5.  #  FILE: "htmlMenusAndKeys.tcl"
  6.  #                                    created: 00-01-22 15.06.06 
  7.  #                                last update: 00-12-30 13.12.02 
  8.  #  Author: Johan Linde
  9.  #  E-mail: <alpha_www_tools@go.to>
  10.  #     www: <http://go.to/alpha_www_tools>
  11.  #  
  12.  # Version: 3.0
  13.  # 
  14.  # Copyright 1996-2001 by Johan Linde
  15.  #  
  16.  # This program is free software; you can redistribute it and/or modify
  17.  # it under the terms of the GNU General Public License as published by
  18.  # the Free Software Foundation; either version 2 of the License, or
  19.  # (at your option) any later version.
  20.  # 
  21.  # This program is distributed in the hope that it will be useful,
  22.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  # GNU General Public License for more details.
  25.  # 
  26.  # You should have received a copy of the GNU General Public License
  27.  # along with this program; if not, write to the Free Software
  28.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  # 
  30.  # ###################################################################
  31.  ##
  32.  
  33. #===============================================================================
  34. # This file contains some general procs for building menus and redefining
  35. # key bindings for the menu items.
  36. #===============================================================================
  37.  
  38. proc html::BuildWholeMenu {me} {
  39.     global html::buildingWholeMenu
  40.     set html::buildingWholeMenu 1
  41.     menu::buildSome $me
  42.     unset html::buildingWholeMenu
  43. }
  44.  
  45. proc html::CreateCustomSub {} {
  46.     global htmlCustomSub html::PrefsFolder html::Plugins
  47.     set elems [glob -nocomplain -dir [file join ${html::PrefsFolder} "New elements"] *]
  48.     set htmlCustomSub ""
  49.     foreach e [lsort $elems] {
  50.         set e [file tail $e]
  51.         set cssElem $e
  52.         if {[lcontains html::Plugins $e]} {set cssElem EMBED}
  53.         if {[regexp "INPUT TYPE=(.*)" $e "" e]} {set cssElem INPUT}
  54.         lappend htmlCustomSub "{} [string index $e 0][string tolower [string range $e 1 end]] $cssElem"
  55.     }
  56. }
  57.  
  58. # Returns a list defining a submenu.
  59. proc html::BuildOneMenu {me} {
  60.     global htmlMenuKey
  61.     set me0 [lindex $me 0]
  62.     global html${me0}Sub
  63.     set tmp ""
  64.     foreach it [set html${me0}Sub] {
  65.         if {$it == "(-"} {lappend tmp $it; continue}
  66.         if {[info exists htmlMenuKey(${me0}/[lindex $it 1])]} {
  67.             set key $htmlMenuKey(${me0}/[lindex $it 1])
  68.         } else {
  69.             set key [lindex $it 0]
  70.         }
  71.         set it2 [split [lindex $it 1] /]
  72.         if {[llength $it2] == 1} {
  73.             lappend tmp "$key[lindex $it2 0]"
  74.         } elseif {$key != ""} {
  75.             lappend tmp "<S$key[lindex $it2 0]" "<S<I$key[lindex $it2 1]"
  76.         } else {
  77.             lappend tmp "<S$key[lindex $it2 1]" "<S$key[lindex $it2 0]"
  78.         }
  79.     }
  80.     return $tmp
  81. }
  82.  
  83. #===============================================================================
  84. # ◊◊◊◊ Custom Key Bindings ◊◊◊◊ #
  85. #===============================================================================
  86.  
  87. proc html::ReadMenuKeys {} {
  88.     global html::PrefsFolder htmlMenuKey
  89.     catch {unset htmlMenuKey}
  90.     if {[file exists [file join ${html::PrefsFolder} "Menu key bindings"]]} {
  91.         catch {uplevel #0 [list source [file join ${html::PrefsFolder} "Menu key bindings"]]}
  92.     }
  93. }
  94.  
  95. proc html::WriteMenuKeys {} {
  96.     global html::PrefsFolder htmlMenuKey
  97.     if {![info exists htmlMenuKey]} {return}
  98.     message "Saving custom key bindings…"
  99.     foreach m [array names htmlMenuKey] {
  100.         lappend txt "set htmlMenuKey(\[list $m\]) [list $htmlMenuKey($m)]"
  101.     }
  102.     file::ensureDirExists ${html::PrefsFolder}
  103.     if {[info exists txt]} {
  104.         set fid [open [file join ${html::PrefsFolder} "Menu key bindings"] w]
  105.         puts $fid [join $txt \n]
  106.         close $fid
  107.     } elseif {[file exists [file join ${html::PrefsFolder} "Menu key bindings"]]} {
  108.         file delete [file join ${html::PrefsFolder} "Menu key bindings"]
  109.     }
  110.     unset htmlMenuKey
  111. }
  112.  
  113. proc html::KeyBindings {} {
  114.     global htmlSubMenus htmlUtilSubMenus htmlMenuKey
  115.     html::ReadMenuKeys
  116.     set menus [concat $htmlSubMenus $htmlUtilSubMenus {{Small Chars} {Capital Chars} {Other Chars 1} {Other Chars 2}}]
  117.     if {[html::NewElementsExists]} {lappend menus Custom}
  118.     if {![catch {listpick -p "Choose a submenu to change key bindings in" \
  119.       [lsort $menus]} meny] && $meny != ""} {
  120.         if {[string match "*Chars*" $meny]} {
  121.             catch {html::SetEntityKeys $meny}
  122.         } else {
  123.             catch {html::SetKeysInMenu $meny}
  124.         }
  125.     }
  126.     catch {unset htmlMenuKey}
  127. }
  128.  
  129.  
  130. # Redefine key bindings in one submenu.
  131. proc html::SetKeysInMenu {meny} {
  132.     global htmlMenuKey htmlModeIsLoaded cssModeIsLoaded
  133.     
  134.     set meny0 [lindex $meny 0]
  135.     global html${meny0}Sub
  136.     set items [set html${meny0}Sub]
  137.     regsub -all {\"\(-\"} $items "" items
  138.     foreach it $items {
  139.         if {[info exists htmlMenuKey(${meny0}/[lindex $it 1])]} {
  140.             set tmpKeys([lindex $it 1]) $htmlMenuKey(${meny0}/[lindex $it 1])
  141.         } else {
  142.             set tmpKeys([lindex $it 1]) [lindex $it 0]
  143.         }
  144.         lappend items2 [list $tmpKeys([lindex $it 1]) [lindex $it 1]]
  145.     }
  146.     if {[eval dialog::adjustBindings [list $meny] newKeys modified 1 $items2] == "Cancel"} {return}
  147.  
  148.     # Save new key bindings
  149.     foreach it $modified {
  150.         set htmlMenuKey(${meny0}/$it) $newKeys($it)
  151.     }
  152.     if {[llength $modified]} {
  153.         html::DeleteCache "CSS keybindings cache"
  154.         html::WriteMenuKeys
  155.         switch $meny {
  156.             HTML {html::BuildWholeMenu htmlMenu}
  157.             Utilities {html::BuildWholeMenu htmlUtilsMenu}
  158.             CSS {html::BuildWholeMenu cssMenu}
  159.             default {
  160.                 menu::buildSome $meny
  161.                 # Redefine key bindinds in CSS mode.
  162.                 if {[info exists cssModeIsLoaded]} {
  163.                     foreach k [array names newKeys] {
  164.                         lappend re [list $k $tmpKeys($k) $newKeys($k)]
  165.                         css::ReBindKey $meny0 $re
  166.                     }
  167.                 }
  168.             }
  169.         }
  170.     }
  171. }
  172.  
  173. proc css::BindOneKey {key elem {un ""} {tmplist ""}} {
  174.     set key1 [keys::toBind $key]
  175.     if {$key1 == ""} {return}
  176.     eval ${un}Bind $key1 [list "css::HTMLelement $elem"] CSS
  177.     if {$tmplist != ""} {
  178.         upvar $tmplist tmp
  179.         append tmp [concat ${un}Bind $key1 [list "css::HTMLelement $elem"] CSS] \n
  180.     }
  181. }
  182.  
  183. # Redefine key bindings when changed in HTML menu.
  184. proc css::ReBindKey {meny keyItems} {
  185.     global html${meny}Sub
  186.     set items [set html${meny}Sub]
  187.     foreach it $keyItems {
  188.         set it0 [lindex $it 0]
  189.         foreach it1 $items {
  190.             if {[lindex $it1 1] == $it0} {
  191.                 set elem [lindex $it1 2]
  192.                 break
  193.             }
  194.         }
  195.         # Skip those which aren't html elements
  196.         if {[llength $it1] < 3} {continue}
  197.         css::BindOneKey [lindex $it 1] $elem un
  198.         css::BindOneKey [lindex $it 2] $elem
  199.     }
  200. }
  201.  
  202.