
package require Tk

namespace eval textprops {

proc fillMenus {} {
	if {![winfo exists .entrymenu]} {
	menu .entrymenu -tearoff 0
	foreach {event shortcut} {Cut Ctrl-X  Copy Ctrl-C  Paste Ctrl-V} {
		.entrymenu add command -label $event -command "event generate \$::textprops::currentEntry <<$event>>" -accelerator $shortcut
	}
	.entrymenu add command -label Delete -command "event generate \$::textprops::currentEntry <Key-Delete>" -accelerator Del
	}
	if {![winfo exists .textmenu]} {
		menu .textmenu -tearoff 0
		foreach {event shortcut} {Undo Ctrl-Z Redo Ctrl-Shift-Z Cut
			Ctrl-X Copy Ctrl-C Paste Ctrl-V} {
			.textmenu add command -label $event -command "event generate \$textprops::currentText <<$event>>" -accelerator $shortcut
		}	
		.textmenu insert Redo separator 
		.textmenu add command -label Delete -command "event generate \$::textprops::currentText <Key-Delete>" -accelerator Del
		.textmenu add separator
		.textmenu add command -label "Insert file..." -command [namespace code insertFile ]
	}
}

proc insertFile {} {
	variable currentText
	set w $currentText
	set filename [tk_getOpenFile -title "Insert a file" -filetypes\
			{{"Text files" {txt}} {"All files" {*}}}]
	if {![string length $filename]} return
	set f [open $filename]
    set oldSeparator [$w cget -autoseparators]
	if { $oldSeparator } {
	     $w configure -autoseparators 0
	     $w edit separator
	}
	$w insert insert [read $f]
	if { $oldSeparator } {
	     $w edit separator
	     $w configure -autoseparators 1
	}
	close $f
}							

proc textRightButton {widget x y} {
	variable currentText
	set hasSelection [llength [$widget tag ranges sel]] 
	if {$hasSelection} {
		.textmenu entryconfigure Copy -state normal
	} else {
		.textmenu entryconfigure Copy -state disabled
	}	
	if {[$widget cget -state] eq "disabled"} {
		foreach entry {Undo Redo Paste "Insert file..." Cut Delte} {
			.textmenu entryconfigure $entry -state disabled
	    }	
	} else {
		.textmenu entryconfigure "Insert file..." -state normal
		if {[catch {clipboard get}]} {
			.textmenu entryconfigure Paste -state disabled
		} else {	
			.textmenu entryconfigure Paste -state normal
		}	
		if {$hasSelection} {
			.textmenu entryconfigure Cut -state normal
			.textmenu entryconfigure Delete -state normal
		} else {	
			.textmenu entryconfigure Cut -state disabled
			.textmenu entryconfigure Delete -state disabled
		}	
		if {[$widget cget -undo]} {
			if {[$widget edit modified]} {
				.textmenu entryconfigure Undo -state normal
			} else {
				.textmenu entryconfigure Undo -state disabled
			}	
			.textmenu entryconfigure Redo -state normal
		} else {
			.textmenu entryconfigure Undo -state disabled
			.textmenu entryconfigure Redo -state disabled
		}	
			
	}	
	set currentText $widget
	tk_popup .textmenu $x $y
}	
proc entryRightButton {widget x y} {
	variable currentEntry
	if {[$widget cget -state] eq "disabled"||[catch {clipboard get}]} {
		.entrymenu entryconfigure Paste -state disabled
	} else {
		.entrymenu entryconfigure Paste -state normal
	}	
	if {[$widget selection present]} {
		.entrymenu entryconfigure Copy -state normal
		if {[$widget cget -state] ne "disabled"} {
			.entrymenu entryconfigure Cut -state normal
			.entrymenu entryconfigure Delete -state normal
		} else {	
			.entrymenu entryconfigure Cut -state disabled
			.entrymenu entryconfigure Delete -state disabled
		}
	} else {	
		.entrymenu entryconfigure Cut -state disabled
		.entrymenu entryconfigure Delete -state disabled
		.entrymenu entryconfigure Copy -state disabled
	}	
	set currentEntry $widget
	tk_popup .entrymenu $x $y	
}

bind Entry <Button-3> [namespace code {entryRightButton %W %X %Y}]
bind Text <Button-3> [namespace code {textRightButton %W %X %Y}]
fillMenus
}
package provide textprops

