# This software is distributed under the Lesser General Public License
#
# easy.tcl
#
# The file easy.tcl contains a couple of easy interfaces for
# adding Graphlet and GraphScript features.
#
#------------------------------------------ CVS
#
# CVS Headers -- The following headers are generated by the CVS
# version control system. Note that especially the attribute
# Author is not necessarily the author of the code.
#
# $source: /home/br/CVS/graphlet/src/gt_base/Attributes.h,v $
# $Author: himsolt $
# $Revision: 1.4 $
# $Date: 1999/07/14 10:53:20 $
# $Locker:  $
# $State: Exp $
#
#------------------------------------------ CVS
#
# (C) University of Passau 1995-1999, Graphlet Project
#

package require Graphlet
package provide Graphscript [gt_version]


namespace eval GT {
    namespace export \
	add_action \
	add_mode \
	add_menu_separator \
	add_menu
}

proc GT::add_action { name body } {
    global GT
    set GT(action,$name) GT::action_$name
    namespace eval GT {}
    proc GT::action_$name editor $body	
}


proc GT::add_mode { name bindings } {

    global GT

    set modename [string tolower $name]_mode

    global GT_modes
    lappend GT_modes(modes) $modename
    set GT_modes($modename,name) $name
    set GT_modes($modename,bindings) $bindings
    GT::add_action $modename "GT::switch_to_mode \$editor $modename"
    if [info exists GT(toplevel_windows)] {
	foreach editor $GT(toplevel_windows) {
	    GT::add_mode_button $editor $modename
	}
    }
}


proc GT::add_menu_separator { editor menu } {
    $menu add separator
}


proc GT::add_menu { menu {name {}} {code {}} } {

    global GT GT_menu

    set path [split $menu .]
 
    set first [lindex $path 0]
    set last [lindex $path end]
    set front [join [lrange $path 0 [expr [llength $path] - 2]] .]

    if {[lsearch -exact $GT(menubar) $first] == -1} {
	lappend GT(menubar) $first
	if [info exists GT(toplevel_windows)] {
	    foreach editor $GT(toplevel_windows) {
		GT::add_to_menubar $editor $first
	    }
	}
    }

    if {$code == {} && ![string match -* $name]} {

	if { $name == {} } {
	    set name $last
	}

	if {[llength $path] >= 2} {
	    proc GT_add_menu_$menu { editor menu } \
		"\$menu add cascade -label $name -menu \$menu.$last; \
                 menu \$menu.$last; \
                 global GT_menu; \
                 foreach proc \$GT_menu($menu,create_procs) { \
                     \$proc \$editor \$menu.$last \
                 }"
	    lappend GT_menu($front,create_procs) GT_add_menu_$menu
	}

    } else {

	if {[string match -* $name]} {

	    lappend GT_menu($front,create_procs) GT::add_menu_separator

	} else {

	    proc ::GT_action_$menu {editor} $code
	    set GT(action,$menu) GT_action_$menu
	    
	    proc GT_add_menu_$menu {editor menu} \
		"GT::add_menu_command \$editor \$menu { \"$name\" $menu }"
	    lappend GT_menu($front,create_procs) GT_add_menu_$menu

	    if [info exists GT(toplevel_windows)] {
		foreach editor $GT(toplevel_windows) {
		    set tk_window_of_menu \
			$editor.menubar.[string tolower $front]
		    if [winfo exists $tk_window_of_menu] {
			GT_add_menu_$menu $editor $tk_window_of_menu
		    }
		}
	    }
	}
    }
}



##########################################
#
# Set emacs variables
#
##########################################

# ;;; Local Variables: ***
# ;;; mode: tcl ***
# ;;; End: ***
