
################################################
## TikVision est une adaptation  par D.Macouin 
## de TKV de Jan Nijtmans <j.nijtmans@chello.nl>
## pour Tikeditor. 
## Les procédures d'insertion sont propres à ce Tikeditor
## Ce code ne fonctionne pas indépendamment deTikeditor
## 
################################################

 source [file join [file dirname [info script]] imgmsg.tcl] ; # ligne nécessaire pour avoir des langues différentes si les fichiers sont présents

catch {[load [file join [file dirname [info script]] libimg1.2.so] ]} ; # ligne utile éventuellement pour Unix 


proc mmc string {
	regsub & $string {} string
	return $string
}

proc mml string {
	return [string first & $string]
}

#
# Make the Image format available.
# Mise en place des formats disponibles .
#

package require Img

#
# Initialization of some global variables
# initialisation de quelques variables globales. 
#

set tkvPriv(count) 0
set tkvPriv(currentdir) [pwd]
set tkvPriv(defaultext) .gif

set tkvPriv(types) [list \
	[list [mc "Image Files"]	{.bmp}		] \
	[list [mc "Image Files"]	{.gif}		] \
	[list [mc "Image Files"]	{.jpeg .jpg}	] \
	[list [mc "Image Files"]	{.png}		] \
	[list [mc "Image Files"]	{.tiff .tif}	] \
	[list [mc "Image Files"]	{.xbm}		] \
	[list [mc "Image Files"]	{.xpm}		] \
	[list [mc "Image Files"]	{.ps .eps}		] \
	[list "BMP [mc Files]"		{.bmp}		] \
	[list "GIF [mc Files]"		{.gif}		] \
	[list "JPEG [mc Files]"		{.jpeg .jpg}	] \
	[list "PNG [mc Files]"		{.png}		] \
	[list "TIFF [mc Files]"		{.tiff .tif}	] \
	[list "XBM [mc Files]"		{.xbm}		] \
	[list "XPM [mc Files]"		{.xpm}		] \
	[list "Postscript [mc Files]"	{.ps .eps}		] \
	[list "GIF [mc Files]"		{}			GIFF] \
	[list "JPEG [mc Files]"		{}			JPEG] \
	[list "PNG [mc Files]"		{}			PNGF] \
	[list "TIFF [mc Files]"		{}			TIFF] \
	[list [mc {All files}]		*] \
]

proc Menu {base name} {
	set menu [ConCat $base menu]
	if {![winfo exists $menu]} {
	menu $menu
	$base configure -menu $menu
	}
	set accelerator [string toupper [string index $name 0]]
	set text [mc "&$accelerator[string range $name 1 end]"]
	set name $menu.$name
	if {![winfo exists $name]} {
	menu $name
	$menu add cascade -label [mmc $text] -menu $name -underline [mml $text]
	}
	return $name
}

#
# Small proc to concatenate window pathnames
# petite procédure pour concatener les chemins des fenetres d'image.
#
proc ConCat args {
	regsub -all {[ 	\.]+} $args . args
	return $args
}

#
#  Create a new image window
# création d'une nouvelle fenêtre d'image 
#

proc image_window {{window {}}} {
	global tkvPriv
	if {![string compare $window {}]} {
	set window .image$tkvPriv(count)
	incr tkvPriv(count)
	}
	if {[winfo exists $window]} {
	catch {eval destroy [winfo children $window]}
	} else {
	toplevel $window
	bind $window <3> {destroy .visio}
	}
	wm title $window [mc {Visionneuse }]
	
	label $window.cheminImage -relief sunken -bg aquamarine  -bd 2 -anchor nw -width 40  -text "chemin de l'image chargée"

	set frame [ConCat $window frame]
	label $frame -relief sunken -bg white -bd 2 -anchor nw
	
	set w [Menu $window fichier]
	$w configure -tearoff 0
	set text [mc &Ouvrir]
	$w add command -label [mmc $text] -command [list load_image $window] -underline [mml $text]
	set text [mc &Sauver]
	$w add command -label [mmc $text] -command [list save_image $window] -underline [mml $text]
	$w add separator
	set text [mc &Fermer]
	$w add command -label [mmc $text] -command [list destroy $window] -underline [mml $text]

	set w [Menu $window images]
	
	#<menu_Inserer>
	set w [Menu $window insérer]
	$w configure -tearoff 1
	set text [mc &Insérer_chemin_absolu]
	$w add command -label [mmc $text] -command "InsererChemin_image $window 0" -underline [mml $text]
	set text [mc &Insérer_chemin_disjoint]
	$w add command -label [mmc $text] -command "InsererChemin_image $window 1" -underline [mml $text]
	set text [mc &Insérer_nom_fichier]
	$w add command -label [mmc $text] -command "InsererChemin_image $window 2" -underline [mml $text]

		
	set w [Menu $window aide]
	$w configure -tearoff 1
	set text [mc &A\ propos]
	$w add command -label [mmc $text] -command About_tikvision -underline [mml $text]
	set text [mc &Aide]
	$w add command -label [mmc $text] -command Aide_image -underline [mml $text]

	catch {wm geometry $window 320x200}
	pack $window.cheminImage -expand false
	pack $frame -side top -expand y -fill both
	return $frame
}

proc register_image {w name} {
	set menu [Menu $w images]
	set item [file tail [lindex $name 0]]
	if {[llength $name] > 1} {
	append item " [lindex $name 1]"
	}
	$menu add command -label $item -command \
	[list show_image $w $name]
}

proc show_image {window name} {

	[ConCat $window frame] configure -image $name
	catch {wm geometry $window {}}
	
set chemin_image $name
$window.cheminImage configure -text $chemin_image

	bind $window <3> {destroy .visio} 

	}

proc load_image window {
	global tkvPriv
	set filename [tk_getOpenFile -filetypes $tkvPriv(types) -parent \
		$window -initialdir $tkvPriv(currentdir)]
	if {[string compare $filename {}]} {
	set imagename [list $filename]
	image create photo $imagename -file $filename
	register_image $window $imagename
	show_image $window $imagename
	set tkvPriv(currentdir) [file dirname $filename]
	}
}

proc save_image window {
	global tkvPriv
	set img [[ConCat $window frame] cget -image]
	set filename [tk_getSaveFile -filetypes $tkvPriv(types) -parent \
		$window -initialdir $tkvPriv(currentdir) -defaultextension .gif \
		-initialfile [file rootname [lindex $img 0]].gif]
	if {[string compare $filename {}]} {
	$img write $filename -format gif
	set tkvPriv(currentdir) [file dirname $filename]
	}
}

proc InsererChemin_image {window flag} {
	global widtexte
	set chemin_image [$window.cheminImage cget -text]
	
#£ 	tk_messageBox -message $chemin_image

		if {$flag == 0} {
		$widtexte insert insert "$chemin_image "
		}
		if {$flag == 1} {
	 	$widtexte insert insert "[file split $chemin_image] "
		}
		if {$flag == 2} {
		$widtexte insert insert "[file tail $chemin_image] "
		}
		


}

proc About_tikvision {} {
	tk_dialog .about "[mmc [mc &A\ propos]] de TikVision " "TikVision est une adaptation pour tikeditor de[mc {Tkv "Tiny Image viewer"}]\n[mc {écrit par}]\
	Jan Nijtmans <j.nijtmans@chello.nl>\nLes procédures d'insertion du chemin des images dans le script sont propres à Tikeditor" {} 0 [mc O.K.]
}

proc Aide_image {} {
	tk_dialog .aide "[mmc [mc &Aide]] de visionneuse"  "[mc { \
"La visionneuse permet :
de chargez des images ;
de les sauvegarder en changeant de répertoire ;
et en utilisant le menu insérer :
-->d'insérer le chemin de l'image
-->ou d'insérer le nom de l'image
dans votre script TCL-Tk en cours
à l'endroit du curseur. "}]" \
						{} 0 [mc " Fermer l'aide "]
}

 
## FIN DE TikVision pour Tikeditor
