# displayeditor.tk8
#  	This file implements  text editor displaying features

# closeFileDialog
#
# This procedure closes a dialog box for finding text in a file 
#
proc closeFindDialog {} {
       
                .menu.edit.m entryconfigure "Find" -state normal
                catch { destroy .find }
}

# findDialog
#
# This procedure opens a dialog box for finding text in a file 
#
#	Arguments:
#		replace : Do we want to search and replace or 
#			  just search.
#		title: 	  Title of the Dialog Box
#

proc findDialog  {option title} {
	if {$option =="replace"} {
		catch { destroy .find }
	}
	toplevel .find
	wm title .find $title
	#create a dialog box for find or replace option
	if {$option =="replace"} { 
		# Find and  Replace option
	
		frame .find.replacewith
		pack .find.replacewith -side bottom  
		label .find.replacewith.label  -text "Replace With"
		entry .find.replacewith.entry  -width 32 -relief sunken -textvariable replacewith
		pack .find.replacewith.label .find.replacewith.entry -side left 
		# Create a frame with entries  to support Replace option

 		set cmd_option replaceText
  	} else {
	# Find option 
		.menu.edit.m entryconfigure "Find" -state disabled
		set cmd_option findText
		# Create button for OK for replacing text

  	}

	frame .find.buttons
	pack .find.buttons -side right  -fill y
	frame .find.findwhat
	pack .find.findwhat -side top 
	frame .find.checkbuttons
	pack .find.checkbuttons -side left  -expand 0
	frame .find.radiobuttons -relief groove -borderwidth 3
	pack .find.radiobuttons -side right  -expand 0
	# Create a frame with entries  to support Find option
	
	button .find.buttons.ok -text "Ok"   -command "$cmd_option"
	button .find.buttons.cancel -text "Cancel" -command closeFindDialog
	pack .find.buttons.ok .find.buttons.cancel -side top -fill x
	#Create buttons for Ok and Cancel
	
	label .find.findwhat.label  -text "Find What "
	entry .find.findwhat.entry  -width 32 -relief sunken -textvariable findwhat
	pack .find.findwhat.label .find.findwhat.entry -side left 
	# Create entry for text to be searched 

	checkbutton .find.checkbuttons.exact_match -text "Regular Expression" -variable exact_match  -anchor w
	checkbutton .find.checkbuttons.case_sensitive -text "Ignore Case" -variable case_sensitive -anchor w
	pack .find.checkbuttons.exact_match .find.checkbuttons.case_sensitive  -side left 
	# Create checkbuttons for search options 
	label .find.radiobuttons.label -text "Search Direction"
	radiobutton .find.radiobuttons.search_forward -text "Forward" -variable search_direction -value 0
	radiobutton .find.radiobuttons.search_backward -text "Backward" -variable search_direction -value 1
	pack .find.radiobuttons.label .find.radiobuttons.search_forward .find.radiobuttons.search_backward -side top -anchor w 
	# Create radiobuttons for search options 

}; #findDialog 

	
# fileopenDialog
# 	
# This procedure opens a dialog box for open file 
#
 	
proc fileopenDialog {title cmd } {
	global file_type 
	global sock

	# Create a dialog box for open file
	toplevel .openfile
	wm title .openfile $title
	# Create a frame for buttons to accept or decline filename selected   
	frame .openfile.buttons
	pack .openfile.buttons -side right  -fill y
	# Create a frame for the filename selected to be displayed
	frame .openfile.filename
	pack .openfile.filename -side top 
	# Create a frame for displaying the listbox for selecting filetype
	frame .openfile.filetype
	pack .openfile.filetype -side bottom  -expand 1 
	# Create a frame to display files of selected filetype of selected directory
	frame .openfile.filelist
	pack .openfile.filelist -side left  -expand 0
	# Create a frame to display directory list  
	frame .openfile.directorylist
	pack .openfile.directorylist -side right  -expand 0

	#Create buttons for Ok and Cancel
	button .openfile.buttons.filenameok -text "Ok"  -command "$cmd "
	button .openfile.buttons.filenamecancel -text  "Cancel" -command "cancelFileOperation"
	pack .openfile.buttons.filenameok .openfile.buttons.filenamecancel -side top -fill x

	# Create entry for displaying filename selected
	label .openfile.filename.label  -text "Filename "
	entry .openfile.filename.entry  -width 32 -relief sunken -textvariable fileName
	pack .openfile.filename.label .openfile.filename.entry -side left 

	# Create entry for displaying File list for current directory
	label .openfile.filelist.label -text "Files"
	listbox  .openfile.filelist.listbox -yscrollcommand ".openfile.filelist.scroll  set" -selectmode single
	scrollbar .openfile.filelist.scroll -command ".openfile.filelist.listbox yview"  
	pack .openfile.filelist.scroll -side right -fill y
	pack .openfile.filelist.label .openfile.filelist.listbox -side top

	# Create entry for displaying Directory list
	label .openfile.directorylist.label -text "Directories"
	listbox  .openfile.directorylist.listbox  -yscrollcommand ".openfile.directorylist.scroll set"  
	scrollbar .openfile.directorylist.scroll -command   ".openfile.directorylist.listbox yview"
	pack .openfile.directorylist.scroll -side right -fill y
	pack .openfile.directorylist.label .openfile.directorylist.listbox -side top

	# Create entry for displaying list of Filetypes 
	label .openfile.filetype.label -text "List Files of Type:"
	listbox  .openfile.filetype.listbox   
	.openfile.filetype.listbox insert end "*"
	.openfile.filetype.listbox insert end "*.*"
	.openfile.filetype.listbox insert end "*.c"
	.openfile.filetype.listbox insert end "*.txt"
	pack .openfile.filetype.label  .openfile.filetype.listbox -side top
	# Get directory list for present directory
	listDirectories  
	listFiles  
	# set focus on openfile so that one has to take care of openfile first 
	grab set .openfile
	#Set event manager for mouse double click for selecting filetype in listbox
	bind .openfile.filetype.listbox  <Double-Button-1> {
		global file_type
		global sock
		.openfile.filelist.listbox delete 0 end
		catch {selection get} file_type
		append chtype chFileType,
		append chtype $file_type
		puts $sock $chtype
		flush $sock
		unset chtype
	}
	#Set event manager for mouse double click for selecting file in listbox
	bind .openfile.filelist.listbox  <Double-Button-1> {
		catch {selection get} fileName
		loadFile
	}
	# Set event manager for mouse double click for 
	# changing directories in listbox
	bind .openfile.directorylist.listbox <Double-Button-1> {
		global sock
		catch {selection get} new_directory 
		append chdirType chDir,
		append chdirType $new_directory
		puts $sock $chdirType
		flush $sock
		unset chdirType
		.openfile.directorylist.listbox delete 0 end
		listDirectories 
		global file_type
		.openfile.filelist.listbox delete 0 end
		listFiles  
	}
	bind .openfile.filelist.listbox  <ButtonRelease-1> \
		[list listTransfer %W ]
}; #fileopenDialog 
proc listTransfer { src } { 
	global fileName
	set index [$src curselection]
	set fileName [$src get $index]
}

#*********************************************************************
#		 Main Window for Single User Editor
#*********************************************************************

set editor_title "Multi User Editor" 
wm title . $editor_title
# Menu frame is the first frame in the window
frame .menu -relief raised -bd 2
pack .menu -side top -fill x
frame .buttons
pack .buttons -side top -fill x -pady 2m
button .buttons.exit -text Exit -command "exitFile "
button .buttons.save -text Save -command "saveFile "
button .buttons.close -text Close
pack .buttons.exit .buttons.save .buttons.close -side left -expand 0
scrollbar .scrolly -command ".text yview"  
scrollbar .scrollx -command ".text xview"  -orient horizontal
pack .scrolly -side right -fill y
pack .scrollx -side bottom -fill x
text .text -relief raised -bd 2 -background white -yscrollcommand ".scrolly set" -xscrollcommand ".scrollx set" -wrap none
pack .text -expand true -fill both
# Set the present working directory for the client.
# puts $sock setPWD
# flush $sock

#                 Menu Items for Single User Editor


#                 File Menu Items 
set m .menu.file.m
menubutton .menu.file -text "File" -menu $m -underline 0
menu $m
$m add command -label "Open.." -command "openFile"     
$m add command -label "New" -command "creatnewFile "    
$m add command -label "Save" -command "saveFile "  -state disabled
$m add command -label "Save As.." -command {fileopenDialog "Save File" "saveFile"}  
$m add command -label "Close" -command "closeFile "   -state disabled
$m add command -label "Print.." -state disabled
$m add separator
$m add command -label "Quit" -command "exitFile " 


#                 Edit  Menu Items 
set m .menu.edit.m
menubutton .menu.edit -text "Edit" -menu $m -underline 0
menu $m
$m add command -label "Cut" -command [list cut ]
$m add command -label "Paste" -command [list paste ] -state disabled
$m add command -label "Copy"  -command [list copy ]
$m add command -label "Find" -command  {findDialog "find" "Find"} 
$m add command -label "Find Next" -command  "findnextText" -state disabled 
$m add command -label "Replace" -command { findDialog "replace" "Replace"}
$m add command -label "Redo" -state disabled
$m add command -label "Undo" -state disabled

#                 Ownership Menu Items 
set m .menu.refresh.m
menubutton .menu.refresh -text "Refresh" -menu $m -underline 0
menu $m
$m add command -label "Me" -command updateMe -state disabled
$m add command -label "All Clients" -command updateAll -state disabled
pack .menu.file .menu.edit .menu.refresh -side left
