# $Revision$ on $Date$
# The above revision line needs to be the first line for all techtk files
# Copyright (c) 2006 Ansys Inc

#----------------------------------------------------------------------------
#
# PADT---------------------------------------------------- Phoenix Analysis &
#                                                         Design Technologies
#
#----------------------------------------------------------------------------
#
#       Package: AI-Environment GUI Management
#
#
#          File: text_editor.tcl
#
#
#	  Developer: Matt Sutton
#
#
#   Description: This file contains the implementation of an text widget window
#                that is configurable to be either editable or readonly. 
#        
#
#      03/25/02: Demo release 1. No implementation. Just a popup window.
#
#			
#
#-----------------------------------------------------------------------------
# For support: 1-480-813-4884 ext. 125 -or- matt.sutton@padtinc.com
#-----------------------------------------------------------------------------

namespace eval ::AIEnv {}
namespace eval ::AIEnv::GUIManagement {}
namespace eval ::AIEnv::GUIManagement::Widgets {} 
namespace eval ::AIEnv::GUIManagement::Widgets::TextEditor {
	variable TextEditors
	variable Counter
	variable Commands
	set Commands [list "create" "manipulatetext" "destroy" "loadlist" "setlistvariable"]
	set Counter 0
	namespace export TextEditor
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::TextEditor {{Name ""}} {
	variable TextEditors
	variable Counter
	if {[llength [info level 0]] == 1 } {
		incr Counter
		set Name "textEditor${Counter}"
	}
	
	if { ![string equal [info commands ::$Name] ""]} {
		error "Command \"$Name\" already exists, unable to create the TextEditor"
	}
	interp alias {} ::$Name {} ::AIEnv::GUIManagement::Widgets::TextEditor::TextEditorProc $Name
	return $Name
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::TextEditorProc { name { cmd "" } args } {
	if { [llength [info level 0]] == 2 } {
		error "Wrong # args: should be \"$name option ?arg arg ...?\""
	}
	if { [string equal [info commands ::AIEnv::GUIManagement::Widgets::TextEditor::_$cmd] ""] } {
		variable Commands
		set OptList [join $Commands ", "]
		set OptList [linsert $OptList "end-1" "or"]
		error "Bad option \"$cmd\": must be $OptList"
	}
	
	return [eval [list ::AIEnv::GUIManagement::Widgets::TextEditor::_$cmd $name] $args]
	
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::_create {name args} {
	variable TextEditors
	
	if { [llength $args] < 2 } {
		error "Wrong # args: should be \"$name style title\" where \"style\" is either readonly -or- editable "
	}
	
	set TextEditors($name-style) [lindex $args 0]
	set TextEditors($name-title) [lindex $args 1]
	
	if {[info exists TextEditors($name-window)]} {
		raise $TextEditors($name-window)
		return
	}
	
	destroy .$name-txted
	set Wnd [toplevel .$name-txted]
	set TextEditors($name-window) $Wnd
	
	set TextEditors($name-style) [string tolower $TextEditors($name-style)]

	if {$TextEditors($name-style) == "readonly"} {
		::AIEnv::GUIManagement::Widgets::TextEditor::CreateReadOnly $name $Wnd
		set Title "$TextEditors($name-title) - (Read-Only)"
		wm title $Wnd $Title
	} elseif {$TextEditors($name-style) == "editable"} {
		::AIEnv::GUIManagement::Widgets::TextEditor::CreateEditable $name $Wnd
		set Title "$TextEditors($name-title)"
		wm title $Wnd $Title
	} else {
		error "Unknown style \"$TextEditors($name-style)\": should be either readonly or editable"
	}
	
	bind $Wnd <Destroy> "::AIEnv::GUIManagement::Widgets::TextEditor::UnMapEditorDataStructure $name"
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::_destroy {Name Args} {
	variable TextEditors
	if { [info exists TextEditors($Name-window)] } {
		destroy $TextEditors($Name-window)
	}
	return
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::_loadlist {Name Args} {
	variable TextEditors
	set ListName $Args
	global $ListName
	set TextWidget $TextEditors($Name-TextWidget)
	set Contents [subst $[subst $ListName]]
	if { [info exists TextEditors($Name-window)] } {
		foreach Item $Contents {
			$TextWidget insert end "$Item\n"
		}
	}
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::_setlistvariable {Name Args} {
	variable TextEditors
	set TextEditors($Name-ListVariable) $Args
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::_runaftersave {Name Args} {
	variable TextEditors
	set TextEditors($Name-AfterSave) $Args
}


proc ::AIEnv::GUIManagement::Widgets::TextEditor::UnMapEditorDataStructure {Name} {
	variable TextEditors
	global replay_main_box replay_recording replay_current
	global command_hook replay_record_command after_command_hook replay_record_after_command
	
	if { [info exists TextEditors($Name-window)] } {
		unset TextEditors($Name-window)
		interp alias {} ::$Name {}
	}
	if {$Name == "replayEditor"} {
	    set file [aie_get_global replay_temp_file]
	    if {$file != "___NA___" && [file exists $file]} {
	        cmd_rm $file
	        aie_unset_global replay_temp_file
	    }
	    if [winfo exists $replay_main_box] {
            $replay_main_box.recording configure -state normal
            set replay_recording 1
            set command_hook replay_record_command
            set after_command_hook replay_record_after_command
            
            set t $TextEditors($Name-TextWidget)
            if [winfo exists $t] {
	            set idx [expr [lindex [split [$t index end] .] 0] - 1]
	            if [string is integer -strict $idx] {
                    set replay_current $idx
                    replay_list_reset
                    replay_dirty
                }
            }
        }
	}
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateReadOnly {Name Window} {
	
	grid columnconfigure $Window 0 -weight 0
    grid columnconfigure $Window 1 -weight 1
    grid rowconfigure $Window 3 -weight 1
	CreateReadOnlyMenu $Name $Window
	CreateReadOnlyToolbar $Name $Window
	CreateReadOnlyText $Name $Window
	
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateEditable {Name Window} {
	grid columnconfigure $Window 0 -weight 0
	grid columnconfigure $Window 1 -weight 1
	grid rowconfigure $Window 3 -weight 1
	CreateEditableMenu $Name $Window
	CreateEditableToolbar $Name $Window
	CreateEditableText $Name $Window
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateReadOnlyMenu {Name Window} {
	variable ::AIEnv::GUIManagement::GUIDisplayData

	$Window configure -menu $Window.mBar
	set m [menu $Window.mBar]
	
	$m add cascade -label "File" -menu $m.file
	set f [menu $m.file -type menubar -tearoff $::AIEnv::GUIManagement::GUIDisplayData("MenuTearOffEnable")]
	
	$f add command -label "Save As..." -command "::AIEnv::GUIManagement::Widgets::TextEditor::SaveAs $Name"
	$f add separator
	$f add command -label "Print..." -command "::AIEnv::GUIManagement::Widgets::TextEditor::Print $Name"
	$f add separator
	$f add command -label "Exit" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Exit $Name"
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateReadOnlyToolbar {Name Window} {
	
	# First create the top separator
	set TopSep [frame $Window.topsep -height 2 -borderwidth 1 -relief sunken]
	set BotSep [frame $Window.botsep -height 2 -borderwidth 1 -relief sunken]
	
	set Tbf [frame $Window.f]
	set ToolBar [ButtonBox $Tbf.bbox1 -spacing 2 -padx 1 -pady 1]
	
	$ToolBar add -image [Bitmap::get save] \
        		 -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        		 -helptext "Save" -command "::AIEnv::GUIManagement::Widgets::TextEditor::SaveAs $Name"
	
	$ToolBar add -image [Bitmap::get print] \
	        	 -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        		 -helptext "Print" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Print $Name"
    	
	pack $ToolBar -side left -anchor w -padx 4
	
	set sep [Separator $Tbf.sep -orient vertical]

    pack $sep -side left -fill y -anchor w
    
    set CloseBar [ButtonBox $Tbf.bbox2 -spacing 0 -padx 1 -pady 1]
    $CloseBar add -text "Close" -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        		 	-helptext "Close" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Exit $Name"
    pack $CloseBar -side left -anchor w -padx 4
    
    
	grid configure $TopSep -row 0 -column 0 -columnspan 2 -sticky ew
	grid configure $Tbf -row 1 -column 0 -sticky w
	grid configure $BotSep -row 2 -column 0 -columnspan 2 -sticky ew
	
	
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateReadOnlyText {Name Window} {
	variable TextEditors
	variable ::AIEnv::GUIManagement::GUIDisplayData
	
	set f [frame $Window.ft]
	set hscroll [scrollbar $f.xsbar -orient horizontal -command "$f.txt xview"]
	set vscroll [scrollbar $f.ysbar -orient vertical -command "$f.txt yview"]
	set t [text $f.txt -width 80 -height 30 -wrap none -xscrollcommand "$f.xsbar set" -yscrollcommand "$f.ysbar set" -state disabled -font $::AIEnv::GUIManagement::GUIDisplayData("FixedTextFont")]
	
	grid $t $vscroll -sticky nsew -pady 3
	grid $hscroll  -sticky nsew
	grid columnconfigure $f 0 -weight 1
	grid rowconfigure $f 0 -weight 1
	grid $f -row 3 -column 0 -columnspan 2 -sticky nsew
	
	set TextEditors($Name-TextWidget) $t
	set TextEditors($Name-Modified) 0
}

#------------------------------------------------------------------------------------------------------------------
# Editable
#------------------------------------------------------------------------------------------------------------------

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateEditableMenu {Name Window} {
	variable TextEditors
	variable ::AIEnv::GUIManagement::GUIDisplayData
	$Window configure -menu $Window.mBar
	set m [menu $Window.mBar]
	
	$m add cascade -label "File" -menu $m.file
	set f [menu $m.file -type menubar -tearoff $::AIEnv::GUIManagement::GUIDisplayData("MenuTearOffEnable")] 

    if {$Name != "replayEditor"} {
	    $f add command -label "New" -command "::AIEnv::GUIManagement::Widgets::TextEditor::New $Name"
	    $f add command -label "Open" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Open $Name"
	}
	$f add command -label "Save" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Save $Name"
    if {$Name != "replayEditor"} {
        $f add command -label "Save As..." -command "::AIEnv::GUIManagement::Widgets::TextEditor::SaveAs $Name"
    }
	$f add separator
	$f add command -label "Exit" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Exit $Name"
	
	set TextEditors($Name-FileMenu) $f
	
	if {$Name != "replayEditor"} {
        set idxSave 2
        set idxSaveAs 3
	} else {
        set idxSave 0
        set idxSaveAs 1
	}
    set TextEditors(MenuSaveIndex) $idxSave
	set TextEditors(MenuSaveAsIndex) $idxSaveAs
	
	$m add cascade -label "Edit" -menu $m.edit
	set e [menu $m.edit -type menubar -tearoff $::AIEnv::GUIManagement::GUIDisplayData("MenuTearOffEnable")]

    if {$Name == "replayEditor"} {
	    $e add command -label "Undo" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Undo $Name"
	    $e add command -label "Redo" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Redo $Name"
	}
	$e add command -label "Cut" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Cut $Name"
	$e add command -label "Copy" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Copy $Name"
	$e add command -label "Paste" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Paste $Name"
	
	set TextEditors($Name-EditMenu) $e
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateEditableToolbar {Name Window} {
	variable TextEditors
	
	# First create the top separator
	set TopSep [frame $Window.topsep -height 2 -borderwidth 1 -relief sunken]
	set BotSep [frame $Window.botsep -height 2 -borderwidth 1 -relief sunken]
	
	set Tbf [frame $Window.f]
	set ToolBar1 [ButtonBox $Tbf.bbox1 -spacing 2 -padx 1 -pady 1]
	set ToolBar2 [ButtonBox $Tbf.bbox2 -spacing 2 -padx 1 -pady 1]
	
    if {$Name != "replayEditor"} {
	    $ToolBar1 add -image [Bitmap::get new] \
        		 -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        		 -helptext "New" -command "::AIEnv::GUIManagement::Widgets::TextEditor::New $Name"
        $ToolBar1 add -image [Bitmap::get open] \
	        	 -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        		 -helptext "Open" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Open $Name"
    }
    $ToolBar1 add -image [Bitmap::get save] \
	        	 -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        		 -helptext "Save" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Save $Name"
	
	set TextEditors($Name-FileToolbar) $ToolBar1
	
	if {$Name != "replayEditor"} {
	    set TextEditors(ToolbarSaveIndex) 2
	} else {
	    set TextEditors(ToolbarSaveIndex) 0
	}
	
	pack $ToolBar1 -side left -anchor w -padx 4
	
	set sep1 [Separator $Tbf.sep1 -orient vertical]
    pack $sep1 -side left -fill y -anchor w
    
    set CloseBar [ButtonBox $Tbf.bbox3 -spacing 2 -padx 1 -pady 1]
    $CloseBar add -text "Close" -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 -padx 1 -pady 1 \
        		 	-helptext "Close" -command "::AIEnv::GUIManagement::Widgets::TextEditor::Exit $Name"
    pack $CloseBar -side left -anchor w -padx 4

	grid configure $TopSep -row 0 -column 0 -columnspan 2 -sticky ew
	grid configure $Tbf -row 1 -column 0 -sticky w
	grid configure $BotSep -row 2 -column 0 -columnspan 2 -sticky ew
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::CreateEditableText {Name Window} {
	variable TextEditors

	set ScrollFrame [ScrolledWindow $Window.sw -auto both -scrollbar both]
	if {$Name == "replayEditor"} {
	    set t [text $ScrollFrame.txt -width 130 -height 30 -wrap none -undo 1 -autosep 1]
	} else {
	    set t [text $ScrollFrame.txt -width 80 -height 30 -wrap none]
	}
    $ScrollFrame setwidget $t
	pack $t -fill both -expand yes
	grid $ScrollFrame -row 3 -column 0 -columnspan 2 -sticky nsew
	set TextEditors($Name-TextWidget) $t
	
	InitializeMonitor $Name $t
	if {$Name == "replayEditor"} {
	    set TextEditors($Name-Modified) 1
	} else {
	    set TextEditors($Name-Modified) 0
	}
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::InitializeMonitor {Name t} {
	# Set up the stuff for monitoring if the text widget changes
	variable TextEditors
	
    if {$Name != "replayEditor"} {
	    rename $t ${t}_
        interp alias {} $t {} ::AIEnv::GUIManagement::Widgets::TextEditor::MonitorTextWidget $Name ${t}_
	    trace variable TextEditors w ::AIEnv::GUIManagement::Widgets::TextEditor::UpdateUI
	}
}
	
proc ::AIEnv::GUIManagement::Widgets::TextEditor::MonitorTextWidget {Name TextWidgetProc args} {
	variable TextEditors
	set Command [lindex $args 0]
	set Args [lrange $args 1 end]

	if { [string equal $Command "insert"] || [string equal $Command "delete"] } {
		set TextEditors($Name-Modified) 1
	}
	eval "$TextWidgetProc $Command $Args"
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::UpdateUI {Variable Index Op} {
	variable TextEditors

	if { $Index != "" } {
		set Parts [split $Index "-"]
		set Name [lindex $Parts 0]
		set Modifier [lindex $Parts 1]
		if { $Modifier == "Modified" && $Name != "replayEditor"} {
			# We are doing something that modified the text widget
			# so update accordingly
			if { $TextEditors($Index) == 1 } {
				$TextEditors($Name-FileToolbar) itemconfigure $TextEditors(ToolbarSaveIndex) -state normal
				$TextEditors($Name-FileMenu) entryconfigure $TextEditors(MenuSaveIndex) -state normal
			} else {
				$TextEditors($Name-FileToolbar) itemconfigure $TextEditors(ToolbarSaveIndex) -state disabled
				$TextEditors($Name-FileMenu) entryconfigure $TextEditors(MenuSaveIndex) -state disabled
			}
		}
	}
}

#-------------------------------------------------------------------------

proc ::AIEnv::GUIManagement::Widgets::TextEditor::New {Name} {
	variable TextEditors
	global AppName
	
	set t $TextEditors($Name-TextWidget)
	if { $TextEditors($Name-Modified) == 1 } {
		if { [tk_messageBox -type okcancel -icon question -title $AppName -message "Save the changes made to a file?"] == "ok" } {
			Save $Name	
		}
	}
	$t delete 1.0 end
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Open {Name {FileName ""} {Skip 0}} {
	variable TextEditors
	global AppName

	if { $TextEditors($Name-Modified) == 1 && !$Skip} {
		if { [tk_messageBox -type okcancel -icon question -title $AppName -message "Save the changes made to a file?"] == "ok" } {
			Save $Name	
		}
	}
	set t $TextEditors($Name-TextWidget)
    if {$FileName == ""} {
        set FileName [tk_getOpenFile -title "$AppName Open"]
	}
	if { $FileName != "" } {
		set ChannelID [open $FileName r]
	    # To enable inserting in readonly editors
	    set state [$t cget -state]
	    $t configure -state normal
		$t delete 1.0 end
	    while {![eof $ChannelID]} {
		    $t insert end [read -nonewline $ChannelID]
		}
		close $ChannelID
		set TextEditors($Name-CurrentFile) $FileName
		set TextEditors($Name-Modified) 0
	    $t configure -state $state
	}
	if {$Name == "replayEditor"} {
	    set TextEditors($Name-Modified) 1
	    $t edit reset
	    #LineNumbers $Name
	}
}

proc  ::AIEnv::GUIManagement::Widgets::TextEditor::LineNumbers {Name} {
	variable TextEditors
	
	set t $TextEditors($Name-TextWidget)
    if {[llength [$t tag ranges linenumbers]]} {
        foreach {from to} [$t tag ranges linenumbers] {
            $t delete $from $to
        }
    } else {
        set lastline [expr int([$t index "end - 1 c"])]
        for {set i 1} {$i <= $lastline} {incr i} {
            $t tag add linenumbers $i.0 $i.[expr [string length $i] + 2]
        }
        #$t tag configure linenumbers -elide 1
        #$t tag configure linenumbers -background yellow
    }
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Save {Name} {
	variable TextEditors
	global AppName
	set t $TextEditors($Name-TextWidget)
	
	if {[array names TextEditors $Name-CurrentFile] != ""} {
		if { $TextEditors($Name-CurrentFile) != "" } {
			set FileName $TextEditors($Name-CurrentFile)
		} else {
			set FileName [tk_getSaveFile -title "$AppName Save"]
		}
	} else {
		set FileName [tk_getSaveFile -title "$AppName Save"]
	}
	if {$FileName != ""} {
		set ChannelID [open $FileName "w"]
		set Data [$t get 1.0 end]
		puts $ChannelID $Data
		close $ChannelID
		set TextEditors($Name-CurrentFile) $FileName
		if {$Name != "replayEditor"} {
		    set TextEditors($Name-Modified) 0
		}
	}

	if {$Name == "replayEditor" && $FileName != ""} {
	    set idx [expr [lindex [split [$t index current] .] 0] - 1]
	    replay_load $FileName
	    replay_renumber 1
	    replay_list_reset
	    replay_save $FileName
	    Open $Name $FileName 1
        replay_list_set_select $idx
        replay_list_select
        replay_update_editbox
	    $t yview $idx
	}
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::SaveAs {Name} {
	variable TextEditors
	global AppName
	set t $TextEditors($Name-TextWidget)
	
	set FileName [tk_getSaveFile -title "$AppName Save"]
	if { $FileName != "" } {
		set ChannelID [open $FileName "w"]
		set Data [$t get 1.0 end]
		puts $ChannelID $Data
		close $ChannelID
		set TextEditors($Name-CurrentFile) $FileName
		if {$Name != "replayEditor"} {
		    set TextEditors($Name-Modified) 0
		}
	}
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Print {Name} {
    variable TextEditors    
    aie_print_file $TextEditors($Name-CurrentFile)
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Exit {Name} {
	variable TextEditors
	global AppName 

	if {$TextEditors($Name-Modified) == 1 && $Name == "replayEditor"} {
		if { [tk_messageBox -type okcancel -icon question -title $AppName -message "Save the changes made to the replay file?"] == "ok" } {
			Save $Name
		}
	} else {
	    if { [array names TextEditors $Name-ListVariable] != "" } {
		    if { [tk_messageBox -type okcancel -icon question -message "Accept the changes made within $AppName?" -title $AppName] == "ok" } {
			    FormatReturnData $Name
		    }
	    }
	}
	_destroy $Name "NULL"
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::FormatReturnData {Name} {
    variable TextEditors
    global $TextEditors($Name-ListVariable)
    set t $TextEditors($Name-TextWidget)
    set LastIndex [$t index "end - 1 char"]
    set LineCount [lindex [split $LastIndex "."] 0]
    set DataList ""
    for {set Line 1} {$Line <= $LineCount} {incr Line} {
        set LineList [split [$t get $Line.0 $Line.end]]
        if { $LineList != "" } {
            lappend DataList $LineList	
        }
    }
    foreach n [array names TextEditors $Name-*] {
        if [string match "textEditor?-ListVariable" $n] {
            if [string match -nocase "::AIEnv::DEZ::*::*part*" $TextEditors($n)] {
                regsub -all \{ $DataList "" DataList
                regsub -all \} $DataList "" DataList
            }
        }
    }
    set $TextEditors($Name-ListVariable) $DataList
    if [info exists TextEditors($Name-AfterSave)] {
        if {[info procs ::[lindex $TextEditors($Name-AfterSave) 0]] != ""} {
            eval $TextEditors($Name-AfterSave)
        }
    }
}
#---------------------------------------------------------------------------

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Undo {Name} {
	variable TextEditors
	set t $TextEditors($Name-TextWidget)
    if [catch {$t edit undo} r] {
        mess "Nothing to undo\n"
    }
    if {$Name == "replayEditor"} {
        set TextEditors($Name-Modified) 1
    }
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Redo {Name} {
	variable TextEditors
	set t $TextEditors($Name-TextWidget)
    if [catch {$t edit redo} r] {
        mess "Nothing to redo\n"
    }
    if {$Name == "replayEditor"} {
        set TextEditors($Name-Modified) 1
    }
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Cut {Name} {
	variable TextEditors
	set t $TextEditors($Name-TextWidget)
	set indices [$t tag ranges sel]
	if {$indices == ""} return
	variable tkclipboard ""
	clipboard clear
	foreach {i1 i2} $indices {
	    set string [$t get $i1 $i2]
	    clipboard append -- $string
	    append tkclipboard $string
	    $t delete $i1 $i2
	}
	set TextEditors($Name-Modified) 1
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Copy {Name} {
	variable TextEditors
	set t $TextEditors($Name-TextWidget)
	set indices [$t tag ranges sel]
	if {$indices == ""} return
	variable tkclipboard ""
	clipboard clear
	foreach {i1 i2} $indices {
	    set string [$t get $i1 $i2]
	    clipboard append -- $string
	    append tkclipboard $string 
	}
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::Paste {Name} {
	variable TextEditors
	variable tkclipboard
	if {![info exists tkclipboard] || $tkclipboard == ""} return
	set t $TextEditors($Name-TextWidget)
	$t insert insert $tkclipboard
	set TextEditors($Name-Modified) 1
}

proc ::AIEnv::GUIManagement::Widgets::TextEditor::GetName {Name VarName} {
	variable TextEditors
	
	if [info exists TextEditors($Name-$VarName)] {
        return "$TextEditors($Name-$VarName)"
    }
	return ""
}



#----------------------------------------------------------------------------

