#! /usr/bin/wish -f

#revised 22/06/2006 - test for Img package

####################################################################################
#COPYRIGHT © 2005 J B Marchant													   #
#    This is free software; you are free to use it for non commercial purposes	   #
#	 as you wish.  There is NO warranty; not even for MERCHANTABILITY or FITNESS   #
#	 FOR  A  PARTICULAR PURPOSE.												   #
####################################################################################

#============================================
global  dirnm filenm font GotSelection platform sndfile

set dirnm "/"
set filenm "-"
#set font "helvetica 12 bold" 
set GotSelection 0 
set platform $tcl_platform(platform)
set seltxt ""
set showed2 "1"

if {$platform == "unix" && [file exists "~/photoutilsrc"]} {
	set fid [open ~/photoutilsrc r]
	gets $fid dirnm
	gets $fid font
	close $fid
} else {
#	set dirnm "e:\\files\\images\\THSlogo"			;# works in windows
}

set wide ""
set high ""

cd $dirnm

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

frame .fr -background red									 ;#main window
wm title . "Simple Text Editor V 0"
pack .fr -expand y  -fill both								;# main container holds menubar

#configure menubar on top
frame .menubar -relief raised -bd 2
pack .menubar -in .fr -fill x

#.ed is the left frame for the display of Thumbnails
#.ed2 is the right frame for text display
frame .edf		;# container for .ed & scrollbar
frame .edf2		;# container for .ed2 & scrollbar
frame .edf3		;# container for .edf2 & button at bottom for saving \
 edits or edf4
frame .edf4		;# container for .ed3 &  scrollbar

#put a text widget with scroll bars this holds the thumbnails & filenames

text .ed -width 20 -height 20 -bg lightblue -tabs { 105 center } \
-yscrollcommand ".ys set" \
-font {helvetica 12}

scrollbar .ys -command ".ed yview"
pack .ed -in .edf -side left -expand n -fill both
pack .ys -in .edf -side left -fill y

pack .edf -in .fr -after .menubar -expand y -fill x

#put a 2nd text widget with scroll bars

#create a text widget to display details of selected thumbnails
text .ed2 -width 40 -height 20 -bg lightyellow -exportselection true \
-wrap word -yscrollcommand ".ys2 set"

#create a text widget to display thumbnails from camera
text .ed3 -width 40 -height 20 -bg red \
-yscrollcommand ".ys3 set"

scrollbar .ys2 -command ".ed2 yview"
pack .ys2 -in .edf2 -side right -fill y
pack .ed2 -in .edf2 -side right -expand y -fill both

scrollbar .ys3 -command ".ed3 yview"
pack .ys3 -in .edf4 -side right -fill y
pack .ed3 -in .edf4 -side right -expand y -fill both

pack .edf -in .fr -after .menubar -expand y -fill both
pack .edf2 -in .edf3  -expand y -fill both
pack .edf3 -in .edf  -expand y -fill both

#--------------------------------------------
#fill the  top menu
menubutton .menubar.file -text File -underline 0 -menu .menubar.file.menu
menubutton .menubar.edit -text Edit -underline 0 -menu .menubar.edit.menu
menubutton .menubar.picture -text Picture -underline 0 -menu .menubar.picture.menu
menubutton .menubar.find -text Find -underline 0 -menu .menubar.find.menu
menubutton .menubar.camera -text Camera -underline 0 -menu .menubar.camera.menu
menubutton .menubar.setup -text Setup -underline 0 -menu .menubar.setup.menu
pack .menubar.file .menubar.edit .menubar.picture .menubar.find .menubar.camera .menubar.setup \
-side left
menubutton .menubar.help -text Help -underline 0 -menu .menubar.help.menu
pack .menubar.help -side right
#--------------------------------------------
#create pulldown menus

menu .menubar.file.menu
.menubar.file.menu add command -label "- - $platform - -"

if {$platform == "windows"} {
	.menubar.file.menu add command -label C: -command "ChangeDir c:"
	.menubar.file.menu add command -label D: -command "ChangeDir d:"
	.menubar.file.menu add command -label E: -command "ChangeDir e:"
}
.menubar.file.menu add command -label "Open in The Gimp" -command {exec gimp-remote -n $dirnm/$filenm &}
#.menubar.file.menu add command -label Save -command "SaveFile"
#.menubar.file.menu add command -label "Save As" -command {SaveAsFile}
.menubar.file.menu add command -label "Up to Parent Directory" -command "ChangeDir .."
.menubar.file.menu add command -label Quit -command Finish

menu .menubar.edit.menu
.menubar.edit.menu add command -label Cut -command CutSelection
.menubar.edit.menu add command -label Paste -command PasteSelection
.menubar.edit.menu add command -label Copy -command CopySelection
.menubar.edit.menu add command -label "Clear Text Panel" -command {.ed2 delete 1.0 end}
.menubar.edit.menu add command -label "Change Exif Date" -command ExifDate
.menubar.edit.menu add command -label "Edit Jpeg Comment" -command EditJpegCom
#.menubar.edit.menu add command -label "showtags" -command showtags

menu .menubar.picture.menu
#.menubar.picture.menu add command -label Cut -command CutSelection
#.menubar.picture.menu add command -label Paste -command PasteSelection
.menubar.picture.menu add command -label "Create missing Thumbnails" -command CreateAllThumbs
.menubar.picture.menu add command -label "Create Thumbnail" -command CreateThumb
.menubar.picture.menu add command -label "Resize" -command Resize1
.menubar.picture.menu add command -label "Reload Thumbnails" -command {ShowThumbs}
.menubar.picture.menu add command -label "Show Full Screen" -command Fscreen
.menubar.picture.menu add command -label "Show Exif Data" -command ShowExif
.menubar.picture.menu add command -label "Show/Hide Text frame" -command ShowHide
.menubar.picture.menu add command -label "Slide Show" -command Slideshow


#Find menu
menu .menubar.find.menu
.menubar.find.menu add cascade -label "Find Selection" \
	-menu .menubar.find.menu.fmenu

#Camera menu
menu .menubar.camera.menu
.menubar.camera.menu add command -label "Find Camera" -command FindCamera
.menubar.camera.menu add command -label "Find Thumbnail" -command FindThumb
.menubar.camera.menu add command -label "List Photos" -command ListFiles

#Camera menu
menu .menubar.setup.menu
.menubar.setup.menu add command -label "Small font" -command FontSetSmall
.menubar.setup.menu add command -label "Medium font" -command FontSetMedium
.menubar.setup.menu add command -label "Large font" -command FontSetLarge

menu .menubar.find.menu.fmenu
.menubar.find.menu.fmenu add radiobutton -label Forward \
 	-command {FindSelection -forwards}
.menubar.find.menu.fmenu add radiobutton -label Backward \
	-command {FindSelection -backwards}

menu .menubar.help.menu
.menubar.help.menu add command -label "Help" -command HelpEdit

.menubar.find.menu add command -label "Find and Replace" -command FindValue
.menubar.find.menu add command -label "Find Selection and Tag" \
	-command TagSelection

#Input Focus to the menubar

tk_menuBar .menubar .menubar.file .menubar.edit .menubar.picture \
	.menubar.find .menubar.camera .menubar.help
focus .menubar
#--------------------------------------------
frame .labels -bg yellow
pack .labels -in .fr -after .menubar -fill x

label .l1 -text "Type" -bg yellow
label .type -width 6 -bg green
label .l2 -text "Width" -bg yellow
label .wide -width 6 -bg green
label .l3 -text "Height" -bg yellow
label .high -width 6 -bg green


pack .l1 .type .l2 .wide .l3 .high  -in .labels -side left
#--------------------------------------------
# find the monitor size

set x [winfo screenwidth .] ; set y [winfo screenheight .]
#.ed2 insert end "x = $x, y = $y\n"
#wm maxsize . $x $y
#wm minsize . $x $y
set z "x"
wm geometry . $x$z$y

#============================================
#============================================
proc Action {a} {

global dirnm filenm

set fidd $a
#.ed2 insert end "fidd=$fidd\n"
set filenm [file tail $fidd]			;# fidd contains the full path to the thumbnail,
				;# all we want is the filename
CreateThumb
}
#============================================
proc ChangeDir {a} {

# called from menu, ShowThumbs
global dirnm filenm

destroy .snd
set i $a
#.ed2 insert end  “a  =  $a\n\n”
##cd "./$i"			;# not windows

cd $i					;# works in windows & linux
set dirnm [eval pwd]			;# not windows
##set dirnm pwd
wm title . "Photoutils - - $dirnm"
ShowThumbs
.type conf -text ""						;# clear the TYPE field
.wide conf -text ""						;# clear the width field
.high conf -text ""						;# clear the height field
.ed2 delete 1.0 end
}
#============================================
proc CreateAllThumbs {} {

global dirnm filenm

set filelist [exec ls -a $dirnm]		;# create a list of filenames

set x 0
foreach i $filelist {
	incr x
	set y [file extension $i]
	set test ".jpg"						;# test for image files
	if {$y == $test} {
		set fidd [join [list $dirnm/thumbnails/$i]]		;# this line reads the thumbnail file
		if {[file exists $fidd]} {
			# do nothing - no need for any action
		} else {
			set filenm $i
			set fidd [file join [list $dirnm $i]]				;# this line reads the main file
			CreateThumb
		}
		set filenm $i

	}
ShowThumbs
}
}
#============================================
proc CreateThumb {} {

global dirnm filenm
#	.ed2 insert end "++$dirnm/$filenm\n\n"
#	.ed2 insert end "++$dirnm/thumbnails/$filenm\n\n"
if {[file exists "$dirnm/thumbnails"]} {
#		.ed2 insert end "thumbnails exists\n"
	} else {
		file mkdir $dirnm/thumbnails
#		.ed2 insert end "thumbnails directory created\n"
	}
eval {exec convert -geometry 160x160 $dirnm/$filenm $dirnm/thumbnails/$filenm}
ShowThumbs
}
#============================================
proc EditJpegCom {} {

global dirnm filenm

set fidd $dirnm/$filenm			;# here, fidd contains the full path to the picture
#set filenm [file tail $fidd]
.ed2 delete 1.0 end
#.ed2 insert end "$fidd selected\n\n"
##set tmp [exec rdjpgcom $fidd]
set tmp [ShowTags "comment"]
.ed2 insert end $tmp
#ShowData $dirnm/$filenm

button .b -text " Save Comment "  -comm WriteJpgCom
pack .b -in .edf3 -side bottom
}
#============================================
proc ExifDate {} {

# change the date/time fields in exif data
global dirnm filenm platform type

.ed2 delete 1.0 end
if {$type == "Exif"} {
#    .ed2 insert end "Change exif date\n\n"
	.ed2 insert end [eval {exec exiflist -o l -f date-taken $dirnm/$filenm}]
    
	button .b -text " Save New Date "  -comm SaveExifDate
    pack .b -in .edf3 -side bottom
	} else {
	.ed2 insert end "Not an exif file\n"
	}
}
#============================================
proc Finish {} {

global dirnm filenm font

set fid [open ~/photoutilsrc w]
puts $fid $dirnm
puts $fid $font
close $fid
exit
}
#============================================
proc FontSetSmall {} {

global font

set font "helvetica 14 bold"
.ed conf -width 32 -tabs {130 center}
FontResize
}
#============================================
proc FontSetMedium {} {

global font

set font "helvetica 18 bold"
.ed conf -width 28 -tabs {130 center}
FontResize
}
#============================================
proc FontSetLarge {} {

global font

set font "helvetica 24 bold"
.ed conf -width 22 -tabs {130 center}
FontResize
}
#============================================
proc FontResize {} {

global font

.menubar.file conf -font $font
.menubar.file.menu conf -font $font
.menubar.edit conf -font $font
.menubar.edit.menu conf -font $font
.menubar.picture conf -font $font
.menubar.picture.menu conf -font $font
.menubar.find conf -font $font
.menubar.find.menu conf -font $font
.menubar.camera conf -font $font
.menubar.camera.menu conf -font $font
.menubar.setup conf -font $font
.menubar.setup.menu conf -font $font
.ed2 conf -font $font
ShowThumbs
}
#============================================
proc Fscreen {} {

# Display the selected image full screen

global dirnm filenm fidd font sndfile

set i [file tail $filenm]
set y [file extension $filenm]
set test ".jpg"				;# test for image files
set test2 ".JPG"				;# test for image files

if {$y == $test || $y == $test2} {

toplevel .full -bg black
# find the monitor size

set x [winfo screenwidth .] ; set y [winfo screenheight .]
set z "x"
wm geometry .full $x$z$y
wm title .full  "Photoutils - $dirnm/$filenm"

eval {exec convert -geometry [join [list $x x $y ""]] $dirnm/$filenm "$dirnm/tmp.jpg"}
image create photo .pic$x -file $dirnm/tmp.jpg
label .full.l -image ".pic$x"
text .full.l2 -height 4 -font $font
# -text "label"  -font {-family times -size 18} -bg lightblue
pack .full.l2 -side bottom -fill x
pack .full.l -side top -fill x

set tmp ""
set tmp [exec rdjpgcom $dirnm/$filenm]
#.full.l2 conf -text "  $tmp  "
.full.l2 insert end $tmp 
#.full.l2 insert end "sndfile = $sndfile "

if {[file exists $sndfile]} {
	button .full.l2.snd2 -command "PlaySnd" -text "Play Sound"
	.full.l2 window create end -window .full.l2.snd2
#	.ed3 window create end -window .tt$xy
	}
#	pack .snd2 -in .full.l2 -side left


bind .full <Button-2> {destroy .full }
#file delete $dirnm/tmp.jpg				;# get rid of the tmp file

	}
}
#============================================
proc HelpEdit {} {

global dirnm filenm

set filenm "~/bin/photoutils-help.txt"
wm title . "Photoutils - - Help"
.ed2 delete 1.0 end
set fsize [file size $filenm]
set fid [open $filenm r]
.ed2 insert end [read $fid $fsize]
close $fid

#button .b -text " Save $filenm "  -comm WriteText
button .bb -text " close "  -comm {destroy .b .bb; .ed2 delete 1.0 end}
pack .bb -in .edf3 -side bottom
}
#============================================
proc ListFiles {} {

# list all photos in camera

global showed2

if {$showed2 == "1"} {
	ShowHide
}

.ed3 delete 1.0 end
.ed3 insert end "Files \n\n"
.ed3 insert end [eval {exec gphoto2 --num-files}]
.ed3 insert end \n\n\n
set x [eval {exec gphoto2 --num-files}]
.ed3 insert end $x\n\n\n
.ed3 insert end \n[eval {exec gphoto2 --list-files}]
}
#============================================
proc PlaySnd {} {

#play the associated .wav file

global sndfile

#.ed2 insert end "Play Sound\n"
# next line plays the snd track in the bg using mplayer
eval [ exec mplayer -quiet $sndfile > /dev/null 2>&1]
}
#============================================
proc Resize1 {} {

global x y wide high

toplevel .r -bg white
frame .r.r1
frame .r.r1.r1l -bg blue -width 100
frame .r.r1.r1r -bg red -width 100
grab .r

wm title .r " Resize Photo "
wm geometry .r 200x150+[expr "$x / 2"]+[expr "$y / 2"]

label .r.r1.r1l.size1 -text " Current width " -bg lightblue
label .r.r1.r1l.size2 -text " Current height " -bg lightblue
label .r.r1.r1l.size3 -text " New width " -bg lightblue
label .r.r1.r1l.size4 -text " New height " -bg lightblue
label .r.r1.r1r.size1 -text " $wide " -bg red
label .r.r1.r1r.size2 -text " $high " -bg red
entry .r.r1.r1r.size3 -text ""
entry .r.r1.r1r.size4 -text ""
#.r.r1.r1r.size3 conf -text " $wide "
#.r.r1.r1r.size4 conf -text " $high "

.r.r1.r1r.size3 insert end " $wide "
.r.r1.r1r.size4 insert end " $high "

button .r.close -text " Resize now " -command {Resize2}
button .r.canc -text " Cancel " -command {destroy .r}

pack .r.r1.r1l.size1 -side top -anchor e
pack .r.r1.r1l.size2 -side top -anchor e
pack .r.r1.r1l.size3 -side top -anchor e
pack .r.r1.r1l.size4 -side top -anchor e
pack .r.r1.r1r.size1 -side top
pack .r.r1.r1r.size2 -side top
pack .r.r1.r1r.size3 -side top
pack .r.r1.r1r.size4 -side top

pack .r.r1.r1l -side left -expand y -fill both
pack .r.r1.r1r -side right -expand y -fill both

pack .r.r1 -side top  -expand y -fill both

pack .r.close -side right
pack .r.canc -side left
}
#============================================
proc Resize2 {} {

global dirnm filenm

set nwide [.r.r1.r1r.size3 get]
#.ed2 insert end $nwide
set nhigh [.r.r1.r1r.size4 get]
#.ed2 insert end $nhigh

eval {exec convert -geometry [join [list $nwide x $nhigh ""]] $dirnm/$filenm "$dirnm/tmp.jpg"}
ShowThumbs

destroy .r
}
#============================================
proc SaveExifDate {} {

# save the date/time in exif data
#called from ExifDate

global dirnm filenm platform type
#eval {exec exifdate "2001:01:01 01:00:01" $dirnm/$filenm}

set dat [.ed2 get 1.0 "end - 1 chars"]
set dat "exec exifdate \"$dat\" $dirnm/$filenm"
#puts $dat
eval $dat 
destroy .b
.ed2 delete 1.0 end
}
#============================================
proc ShowData {a} {

# called from button in ShowThumbs

global dirnm filenm font platform type sndfile

destroy .snd

.ed2 conf -font $font

set fidd $a			;# here, fidd contains the full path to the picture

set filenm [file tail $fidd]
.ed2 delete 1.0 end

#set fidd "$dirnm\\$filenm"
#set fidd [join [list $dirnm $filenm] \\ ]

#.ed2 insert end "a = $a \nDirnm = $dirnm \nFilenm = $filenm \n\nfidd = $fidd selected \n\n"

##if {$platform == "unix"} {
##	set tmp [exec rdjpgcom $fidd]
	.ed2 insert end "JPEG Comment  : "
    ShowTags "comment"
	.ed2 insert end "\n"
##} else {
##	.ed2 insert end "JPEG Comment not available\n"		;# fix this
##}
##set fid [file split $fidd ]
##set fidd [join [list $fidd] \ ] 
wm title . "Photoutils - $fidd -- x"

#set fidd [file normalize $fidd]
set fid [open $fidd r]
fconfigure $fid -encoding binary -translation binary -eofchar {}

set type [read $fid 6]
set type [read $fid 4]

close $fid
.type conf -text $type		;# working to here

if {$type == "Exif"} {
#    .ed2 insert end "Change exif date\n\n"
	.ed2 insert end "Date taken:  "
	.ed2 insert end [eval {exec exiflist -o l -f date-taken $dirnm/$filenm}]
}

ShowTags "size"
#---------------------------- the following code looks for a .wav file with
# the same name as the img file

# only tried with my Canon camera
#.ed2 insert end "Play Sound-----\n"
#.ed2 insert end $dirnm\n
#.ed2 insert end $filenm\n\n
set y [file rootname $filenm]			;# $filenm is the jpeg image file
#.ed2 insert end $y\n\n

set sndfile [string range $y 3 end]		;# extract chars 3-end
set sndfile [concat snd$sndfile.wav]	;# create the sound filenm
	
		;# next check whether the sound file exists, if so create a button

if {[file exists $sndfile]} {
	button .snd -command "PlaySnd" -text "Play Sound"
	pack .snd -in .labels -side left
}
}
#============================================
proc ShowExif {} {

global dirnm filenm

.ed2 delete 1.0 end
.ed2 insert end [eval {exec exiflist $dirnm/$filenm}]

}
#============================================
proc ShowTags {field} {

# called from ShowData

global dirnm filenm wide high

#jpeg tags
#	d8	start of img	4, 2=size, 4=JPEG/EXIF, ???
#	da	start of picture
#	d9	end of image
#	e0	start of jpeg
#	e1	start of exif
#	fe	comment			*
#	db					*
#	c0					* width x height, 17 bytes, ignore 3, 2=width,2=height, ignore 8
#	c4					*
#	da					*
#	00	is not a tag, part of image
#
#		tags marked * are followed by 2 bytes containing the size of the
#		tag including those 2 bytes
set da [binary format B8 11011010]
set db [binary format B8 11011011]
set e0 [binary format B8 11100000]	;# exif tag
set e1 [binary format B8 11100001]	;# exif tag
set c0 [binary format B8 11000000]	;# size
set c4 [binary format B8 11000100]	;# size
set fe [binary format B8 11111110]	;# comment
set ff [binary format B8 11111111]

#.ed2 insert end "\n$ff $c0\n"
##################################
set fid $dirnm/$filenm					;# check that this and next line work in windows
file normalize $fid
#.ed2 insert end "\n$dirnm - $filenm - $fid\n\n"
#return								;# for testing - ok to here

set fid [open $fid r]
fconfigure $fid -encoding binary -translation binary -eofchar {}
set xx 0
#############################
while {$xx == 0} {
	set tst [read $fid 1]
	if {$tst == $ff} {
#		.ed2 insert end "ff found "
		set tst2 [read $fid 1]					;# this char is a tag
#		.ed2 insert end "$tst2 "
#		.ed2 insert end "pos = [tell $fid]\n"
##	return										;# stops at 1st char
		if {$tst2 == $da} {
#			.ed2 insert end "$--- tst2--- "
			set xx 1
#			.ed2 insert end " da "
			return

		} elseif {$tst2 == $c0} {

#			.ed2 insert end " C0 "
##return
			set tst2 [read $fid 3]

			set wide 0
			set high 0

			set tst2 [read $fid 2]
#			.ed2 insert end "height = $tst2 | " 	;# comment out when working in windows
			binary scan $tst2 S* high
#			.ed2 insert end "$high \n" 				;# comment out when working in windows

			set tst2 [read $fid 2]
#			.ed2 insert end "width = $tst2 | " 	;# comment out when working in windows
			binary scan $tst2 S* wide
#			.ed2 insert end "$wide \n" 				;# comment out when working in windows
			
			if {$field == "size"} {
			    .high conf -text $high
			    .wide conf -text $wide
##return
            }
		} elseif {$tst2 == $e1} {

#			.ed2 insert end "exif format \n"
			set tst2 [read $fid 2]
#			.ed2 insert end "jumpE1 = $tst2 \n"
			binary scan $tst2 S* jump
#			.ed2 insert end "jumpE2 = $jump \n"
#						we need to go to jump - 4 absolute for start of main image
			set jump [expr $jump - 4]
			seek $fid $jump current
#			.ed2 insert end "jumpE3 = $jump \n"
#return
		} elseif {$tst2 == $fe} {
#												called from EditJpegCom, ShowData
#			.ed2 insert end "COMMENT FOUND --"
			set tst2 [read $fid 2]      		;# binary field size
			binary scan $tst2 S* jump
			set jump [expr $jump - 2]			;# decimal field size
			set tmp [read $fid $jump]
			if {$field == "comment"} {
    			.ed2 insert end "$tmp\n"
			}
		} elseif {$tst2 == $db | $tst2 == $c4} {

			set tst2 [read $fid 2]
#			.ed2 insert end "tst2 = $tst2 \n"
			binary scan $tst2 S* jump
#			.ed2 insert end "jump other = $jump \n"
#						we need to go to jump - 3 absolute for start of next tag
			set jump [expr $jump - 3]
			seek $fid $jump current
#			.ed2 insert end "jumpE4\n"
		}
	}
#	incr x
}
	close $fid
	.ed2 insert end "EOF\n"
}
#============================================
proc ShowThumbs {} {

# called from menu, ChangeDir
global dirnm filenm font i platform

.ed delete 1.0 end

.ed conf -font $font

#.ed2 insert end [exec ls $dirnm]
#- - - - - - - - - - - -
##set filelist [exec ls -a $dirnm]				;# create a list of filenames not windows


#puts "dirnm = $dirnm\n"
set filelist [glob -directory $dirnm -- .. *]		;# create a list of filenames - works in win & linux
set filelist [lsort $filelist]							;# put the list into order
set x 0

#treating .. separately not now required, included in glob pattern above
#		set i ".."
#		button .$x  -width 20 -bg yellow -comm "ChangeDir $i" -text $i
#		.ed insert end \t
#		.ed window create end -window .$x
#		.ed insert end \n\t$i\n

foreach i $filelist {
	incr x
	set i [file tail $i]
	set y [file extension $i]
	set test ".jpg"				;# test for image files
	set test2 ".JPG"				;# test for image files

	if {$y == $test || $y == $test2} {
		set fidd [join [list $dirnm/thumbnails/$i]]	;# this line reads the thumbnail file

#  next display all directory entries - thumbnail if it exists, yellow button for a jpg,
# no thumb magenta button for a directory, blue label for others

		if {[file exists $fidd]} {
			.ed insert end \t
			image create photo .pic$x -file $fidd
set fidd [file join $dirnm $i]
set fidd [file normalize $fidd]
#			button .$x -comm "ShowData $dirnm/$i" -image .pic$x
			button .$x -comm "ShowData  $fidd" -image .pic$x

			.ed window create end -window .$x
			.ed insert end \n\t$i\n
		} else {
			set fidd [file join $dirnm $i]				;# this line reads the main file
			.ed insert end \t
			button .$x  -width 20 -bg yellow -comm "Action $fidd" -text $i
						;# Action creates a thumbnail
			.ed window create end -window .$x
			.ed insert end \n\t$i\n
		}
		set filenm $i
	} elseif {[file isdirectory $i]} {
#		.ed insert end \t
#		button .$x -text $i -bg magenta -fg white -width 20 -comm "ChangeDir $i"
#		.ed window create end -window .$x
	} elseif {[file extension $i]  == ".txt"} {
		.ed insert end \t
		button .$x -text $i -bg green -fg white -width 20 -font $font -comm "TextEdit $i"
		.ed window create end -window .$x
	 	.ed insert end \n\n
	} else {
		.ed insert end \t
		label .$x -text $i -bg blue -fg white -width 20 -font $font
		.ed window create end -window .$x
	 	.ed insert end \n\n
	}
#	 	.ed insert end \n\n
}
#- - - - - - - - - - - -
#- - - - - - - - - - - -

# look for directory entries

set x 0
foreach i $filelist {
	incr x
	set i [file tail $i]
	set y [file extension $i]
	set test ".jpg"						;# test for image files
	if {[file isdirectory $i]} {
		.ed insert end \t
		button .$x -text $i -bg magenta -fg white -width 20 -font $font -comm "ChangeDir $i"
		.ed window create end -window .$x
	 	.ed insert end \n\n
	}
#	 	.ed insert end \n\n
}

#- - - - - - - - - - - -
#	.ed2 insert end ++$dirnm/$filenm++\n\n

}
#============================================
proc Slideshow {} {

global dirnm filenm

eval exec "qiv -mf [glob *.* > /dev/null 2>&1] &"

}
#============================================
proc TextEdit {i} {

global dirnm filenm

set filenm $i
wm title . "Photoutils - - $dirnm/$i"
.ed2 delete 1.0 end
set fsize [file size $dirnm/$filenm]
#.ed2 insert end "$fsize \n\n"	;# check size of file
set fid [open $dirnm/$filenm r]
.ed2 insert end [read $fid $fsize]
close $fid

button .b -text " Save $filenm "  -comm WriteText
button .bb -text " close "  -comm {destroy .b .bb; .ed2 delete 1.0 end}
pack .bb .b -in .edf3 -side bottom
}
#============================================
proc WriteJpgCom {} {

global dirnm filenm

set comment [.ed2 get 1.0 end]		;# get comment from screen
set outfile "$dirnm/tmp2.jpg"		;# name of temporary file
#.ed2 insert end  "$dirnm/tmp2.jpg"		;# name of temporary file
#.ed2 insert end [exec ls -a $dirnm]
file delete $outfile				;# get rid of it if it exists
#.ed2 insert end [exec ls -a $dirnm]

eval {exec wrjpgcom -replace -comment $comment $dirnm/$filenm > $outfile}

file delete $dirnm/$filenm				;# get rid of the old file
eval [exec cp $outfile $dirnm/$filenm]	;# copy the tmp file to original name
file delete $outfile					;# get rid of the tmp file
#.ed2 insert end [exec ls -a $dirnm]

destroy .b

}
#============================================
proc WriteText {} {

global dirnm filenm

set comment [.ed2 get 1.0 end]		;# get comment from screen
set outfile "$dirnm/tmp2.jpg"		;# name of temporary file
#.ed2 insert end  "$dirnm/tmp2.jpg"		;# name of temporary file
#.ed2 insert end [exec ls -a $dirnm]
file delete $outfile				;# get rid of it if it exists
#.ed2 insert end [exec ls -a $dirnm]

#eval {exec wrjpgcom -replace -comment $comment $dirnm/$filenm > $outfile}

set fid [open $outfile w+]
puts $fid $comment
close $fid

file delete $dirnm/$filenm			;# get rid of the old file
eval [exec cp $outfile $dirnm/$filenm]	;# copy the tmp file to original name
file delete $outfile				;# get rid of the tmp file

destroy .b .bb
.ed2 delete 1.0 end
}
#============================================
proc CutSelection {} {

global seltxt

#focus .ed2
set seltxt [ selection get]

.ed delete insert "insert + [string length $seltxt] chars"
}
#============================================
proc PasteSelection {} {

global seltxt

.ed2 insert insert $seltxt

}
#============================================
proc CopySelection {} {

global seltxt

#focus .ed2
set seltxt [ selection get]

}
#============================================
#============================================
proc FindCamera {} {

# find & display camera info using gphoto2

.ed2 delete 1.0 end
.ed2 insert end "camera \n"
.ed2 insert end [eval { exec gphoto2 --auto-detect }]
}
#============================================
proc FindThumb {} {

# This proc displays a thumbnail of each photo in the camera on the screen.
# Selecting a thumbnail will download it to the current directory.

global showed2

if {$showed2 == "1"} {
	ShowHide
}
.ed3 delete 1.0 end
.ed3 insert end "Thumbnails \n"
##.ed3 insert end \n\n[eval {exec gphoto2 --auto-detect}]\n\n	;# find the camera

set filelist [eval {exec gphoto2 --list-files}]		;# create a list of photos - works??? in win & linux
#--.ed3 insert end "\n\n *$filelist* \n\n1--------------\n"
set filelist2 [split $filelist "#"]

#.ed2 insert end "\n\n *$filelist2* \n\n--------------\n\n"
#.ed2 insert end "\n\nrecords = [llength $filelist2] \n\n--------------\n\n"

set listlen [llength $filelist2]
set listlen [expr $listlen - 1]

#.ed2 insert end $listlen

#--set xx [lindex $filelist2 1 ]			;# index 1 is the 1st photo
#--.ed3 insert end "\n\n [join $xx ]\n"		;# join reduces the number of spaces to 1
#--.ed3 insert end "\n\n [lindex $xx 1]"		;# 1 extracts the camera filename, 0 would get the nth image in the camera
#--.ed3 insert end "\n\n3--------------\n\n"

set xy 0
foreach i $filelist2 {
	incr xy
	
	if {$xy > $listlen} {
		break
	}
	
	eval {exec rm -f /tmp/test.jpg}		;# works
	FindThumb2 $xy
	
	.ed3 insert end $xy
	
	}
}
#============================================
proc FindThumb2 {xy} {

catch {eval [exec gphoto2 --get-thumbnail $xy --filename /tmp/test.jpg]}

set colour "blue"
if {[file exists /tmp/test.jpg]} {
# create a button displaying the thumbnail img from the camera
	image create photo .pict$xy -file /tmp/test.jpg
	button .tt$xy -image ".pict$xy" -bd 10 -bg blue -command "Button .tt$xy $colour $xy"
	.ed3 window create end -window .tt$xy
} else {
	button .tt$xy -text Audio\n$xy -command "Button .tt$xy $colour $xy"
	.ed3 window create end -window .tt$xy
	}
}

#============================================
proc Button {i j xy} {

# i = button id, j = button bg color, xy = image number
# called from button in FindThumb2
global dirnm filenm

if {$j == "red"} {
	set colour "blue"
	$i conf -bg blue -command "Button $i $colour $xy"
	} else {
	set colour "red"
	$i conf -bg red -command "Button $i $colour $xy"
	}
#.ed3 insert end "\n i = $i j = $j xy = $xy $dirnm $filenm\n"
# the next line puts the image $xy in the selected directory
# but locks up if the image already exists
catch {eval [exec gphoto2 -p $xy 2>&1]}
CreateAllThumbs
}
#============================================
proc ShowHide {} {

# switch between text display and thumbnail display

global showed2

if {$showed2 == "1"} {
    set showed2 "0"
    pack forget .edf2
    pack .edf4 -in .edf3  -expand y -fill both
	} else {
    set showed2 "1"
    pack forget .edf4
    pack .edf2 -in .edf3  -expand y -fill both
	}

}
#============================================
# the following is a continuation of the initial setup routines.
# the procs can not be called before they have been set up!!!!

if { [catch {set img_version [package require Img] } ] } {
	.ed2 insert end "\n\t\tImg package Missing - Sorry, this script will not run"
	set font "helvetica 18 bold"
	.ed2 conf -font $font
} else {
	.ed2 insert end "Img package found"
	ShowThumbs
	FontSetMedium
}


wm title . "Photoutils - $dirnm"

# display the image full screen
bind . <Double-Button-2> {Fscreen}

