# configure tags
# 
.text tag configure free -background white
.text tag configure $pidVal -background "green"
.text tag configure others -background "light pink"
.text tag configure sel -foreground "blue"
.text tag configure sel -borderwidth 2
.text tag configure sel -relief groove
# Change bindings 
	

# displayFile 
#
# 	Display the file in the editor

proc displayFile { } {
	global lines
        .text delete 1.0 end
	set curIndex 0
	# puts "server returned the following lines "
	set totalLines [ llength $lines(content) ]
	# puts "Total lines is  $totalLines"
	foreach element $lines(content) {
		if { [ string match \^s* [lindex $lines(content) $curIndex] ] != 1} {
			# if line specifies a pid value
			# CHANGED 
			.text insert end " "
			# CHANGED
			.text insert end $element
			.text insert end "\n"
		}
		incr curIndex
	}
}


# setAccessControl	
#
#	Set the access control for the file returned.
#	Mark all owned as owned and disable for editing
# 	Mark all free as separate color
# 	If the current client has no entries in list, let him select.
proc setAccessControl { } {
	global pidVal
	global lines 
	global offset
	global extent
	global updateComplete
	global RetVal
	set totalLines [ llength $lines(content) ]
	#  puts "Total lines is  $totalLines"
  	# puts $totalLines
	set curIndex 0
	set displayIndex 1
	set updateComplete 1
 	while {  $curIndex <= $totalLines} {
		if { [string match \^s* [lindex $lines(content) $curIndex] ] == 1}		 {
			# puts "Adding to owned region "
			# puts  $curIndex
			# if line specifies a pid value
			set pidString  [lindex $lines(content) $curIndex]
			# get the line in a temporary variable
			regsub -all {\^s} $pidString {} ownerpid
			# remove special characters from the line
			incr curIndex
			#.text tag remove free "${displayIndex}.0 linestart" "${displayIndex}.0 +1 lines linestart"
			.text tag remove free "${displayIndex}.0 linestart " "${displayIndex}.0 +1 lines linestart"
			#.text tag add $ownerpid "${displayIndex}.0 linestart" "${displayIndex}.0 +1 lines linestart"
			.text tag add $ownerpid "${displayIndex}.0 linestart " "${displayIndex}.0 +1 lines linestart"
			# add a tag to the line  corresponding to pid of owner
			set loop $extent($ownerpid)
			# set number of times to loop as value of extent
			for {set count 1} { $count < $loop } { incr count} {
				incr curIndex
				incr displayIndex
				#.text tag remove free "${displayIndex}.0  linestart" "${displayIndex}.0 +1 lines linestart"
				.text tag remove free "${displayIndex}.0 linestart " "${displayIndex}.0 +1 lines linestart" 
				#.text tag add $ownerpid "${displayIndex}.0 linestart" "${displayIndex}.0 +1 lines linestart"
				.text tag add $ownerpid "${displayIndex}.0  linestart " "${displayIndex}.0 +1 lines linestart"
					# add tag to rest of the lines  owned by client
			}	
			if { $ownerpid != $pidVal} { 
				DisableEditing $ownerpid 
			}
	 	} else 	{
		#	puts "Adding to free region: $displayIndex"
	#		puts  $curIndex
		#	.text tag add free "${displayIndex}.0   linestart" "${displayIndex}.0 +1 lines linestart"
			.text tag add free "${displayIndex}.0 linestart " "${displayIndex}.0 +1 lines linestart"
			}
				
			incr curIndex 
			incr displayIndex 
	} 
	# while still more lines
	# delete previous line
	set ownedRange [.text tag ranges $pidVal]
	if {$ownedRange==""} {
		# "Call a Edit Control Procedure "
		 # puts " NO Owned Lines "
	} else {
		# puts "Owned Range is :"
		# puts $ownedRange
     	       regexp {([^ ]+) (.*)} $ownedRange useless ownedstart ownedfinish
#		.text mark ownedstart [lindex 1 $ownedRange]
#		.text mark ownedfinish [lindex 2 $ownedRange]
		# puts $ownedstart
		# puts $ownedfinish
		# puts $offset
		if { $offset ==0.0 } {
			set offset 0.1
		}
		.text mark set  ownedstart "$ownedstart"
		.text mark set ownedfinish $ownedfinish 
		.text mark set insert [ addIndices  $ownedstart $offset]
	#	if { $RetVal == 1 } {
	#		puts "I am here"
	#		.text insert insert "\n "
	#		set RetVal 0
	#	}
		.text see insert
		# if info exists about update complete, then put
		#  cursor at new position, 
		# else position it at the previous position
		if { ![info exists updateComplete] } {
			set  newinsert [incrementtags $ownedstart $offset]
			# puts "NEW POINT"
			# puts $newinsert
			.text set insert $newinsert
		}
		# set gravity of the marks also so that editing is correctly
		# controlled to be only between the two marks
		# .text mark gravity ownedstart left
		.text mark gravity ownedstart left
		.text mark gravity ownedfinish right
	}
}

	
