done_enregistrer = false
positionL_curseur_enregistrer = 65
oldpad = Controls.read()
blanc = Color.new(255,255,255)
noir = Color.new(0,0,0)
bleu = Color.new(0,0,255)
xligne = 66
x = 0
y = 0
chercher = true
debut = 1
fin = 7
aff_aide_enregistrer = false
n_liste = 1
if multi_win_nb == 1 then   
   name = multi_win_nom1
   repertoire = multi_win_dir1 
   dossier_en_cours = multi_win_dir1
else
   name = multi_win_nom2
   repertoire = multi_win_dir2
   dossier_en_cours = multi_win_dir2
end
txt_fichier = name
fichier_selectionne = 1
aff_clavier_enr = true
type_clavier_enregistrer = 1
curseur_enregistrer = Image.createEmpty(1,32)
curseur_enregistrer:clear(couleur_select)
xcurseur_enregistrer = 65
ycurseur_enregistrer = 243
yselectionneur = 64
enregistrer = Image.load("images/enregistrer.png")
repertoire_defaut = System.currentDirectory()

function edit_menu()
   img_temp = Image.load("ms0:/PSP/GAME/LUAPROG/images/fichier.png")
   oldpad = pad
   total_s = (System.getTotalSize("ms0:/")/1024)
   free_s = (System.getFreeSpace("ms0:/")/1000)
   sum = System.md5sum(fichiers[fichier_selectionne].name)
   while true do
   pad=Controls.read()
   screen:clear()
   screen:blit(0,0,img_temp)
   screen:print(60,38,string.sub(dossier_en_cours.."/"..fichiers[fichier_selectionne].name,-60),blanc)
   screen:print(60,122,System.powerGetBatteryLifePercent().." % / "..System.powerGetBatteryLifeTime().." Min",blanc)
   screen:print(50,144,math.floor((free_s/1000)).." / "..math.floor((total_s/1024)).." MB",blanc)
   if pad:select() and oldpad:select() ~= pad:select() then
	 break
   end
   if pad:triangle() and oldpad:triangle() ~= pad:triangle() then
	 copy_selec = "ms0:/PSP/GAME/LUAPROG/"..fichiers[fichier_selectionne].name
	 img_copy = Image.load("ms0:/PSP/GAME/LUAPROG/images/copy.png")
	 while true do
		screen:clear()
		pad = Controls.read()
		screen:blit(0,0,img_copy)
		 if string.len(copy_selec) > 28 then
		screen:print(150,130,"..."..string.sub(copy_selec,-25),blanc)
		 else
		 screen:print(150,130,copy_selec,blanc)
		 end
		 if pad:cross() then
		    copy_selec = System.startOSK(copy_selec, "Nouveau nom")
		end
		 if pad:circle() and oldpad:circle() ~= pad:circle() then
		    oldpad = pad
		    break
		end
		 if pad:start() then
		    System.copyFile(fichiers[fichier_selectionne].name,copy_selec,0)
			 oldpad = pad
		    break
		end
		screen.flip()
		screen.waitVblankStart()
		oldpad = pad
	  end
	 img_copy = nil
	 System.memclean()
   end
   if pad:r() and oldpad:r() ~= pad:r() then
	 dir_selec = "Luaprog"
	 img_dir = Image.load("ms0:/PSP/GAME/LUAPROG/images/folder.png")
	 while true do
		screen:clear()
		pad = Controls.read()
		screen:blit(0,0,img_dir)
		screen:print(150,130,dir_selec,blanc)
		 if pad:cross() then
		    dir_selec = System.startOSK(dir_selec, "Nouveau nom")
		end
		 if pad:circle() and oldpad:circle() ~= pad:circle() then
		    oldpad = pad
		    break
		end
		 if pad:start() then
		    System.createDirectory(dir_selec)
			 oldpad = pad
		    break
		end
		screen.flip()
		screen.waitVblankStart()
		oldpad = pad
	  end
	 img_dir = nil
	 System.memclean()
   end
   if pad:circle() and oldpad:circle() ~= pad:circle() then
	 new="LuaProg"
	 select=1
	 oldpad = pad
	 img_rename = Image.load("ms0:/PSP/GAME/LUAPROG/images/rename.png")
	 while true do
	    screen:clear()
	    pad = Controls.read()
		screen:blit(0,0,img_rename)
	    screen:print(170,93,fichiers[fichier_selectionne].name,blanc)
	    screen:print(170,168,new,blanc)   
	    if pad:cross() then
		   new = System.startOSK(new, "Nouveau nom")
	    end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then
		   oldpad = pad
		   break
	    end
	    if pad:start() then
		   System.rename(fichiers[fichier_selectionne].name,new)
			oldpad = pad
		   break
	    end
	    screen.flip()
	    screen.waitVblankStart()
	    oldpad = pad
	  end
	  img_rename = nil
	 System.memclean()
	  break
   end
   if pad:square() then
	 if fichiers[fichier_selectionne].directory == true then
	    System.removeDirectory(dossier_en_cours.."/"..fichiers[fichier_selectionne].name)
	 else
	    System.removeFile(dossier_en_cours.."/"..fichiers[fichier_selectionne].name)
	 end
	 break
   end
   if pad:cross() then
	 System.message("Md5 : "..sum,0)
   end
   screen.flip()
   screen.waitVblankStart()
   oldpad = pad
   end
   img_temp = nil
   System.memclean()
   chercher = true
   fichier_selectionne = 1
   n_liste = 1
   debut = 1
   fin = 7
end

function cursor_enregistrer()
   screen:blit(xcurseur_enregistrer,ycurseur_enregistrer,curseur_enregistrer)
   if pad:right() and oldpad:right() ~= pad:right() and string.len(txt_fichier) > positionL_curseur_enregistrer and xcurseur_enregistrer ~= 305 then
	 xcurseur_enregistrer = xcurseur_enregistrer + 8
   end
   if pad:left() and oldpad:left() ~= pad:left() and xcurseur_enregistrer ~= 65 then
	 xcurseur_enregistrer = xcurseur_enregistrer - 8
   end
   positionL_curseur_enregistrer = (xcurseur_enregistrer - 65) / 8
end

function afficher_partie(i, pos)
   if fichiers[i].name == "." or fichiers[i].name == ".." or fichiers[i].directory == true then
	 screen:blit(20,pos*22+42,enregistrer,3,273,18,20)
	 screen:print(40,pos*22+8+42,fichiers[i].name,noir)
   else
	 longueur = string.len(fichiers[i].name)
	 nom = string.sub(fichiers[i].name, longueur-3)
	 if string.lower(nom) == ".txt" then
	    screen:blit(20,pos*22+42,enregistrer,21,273,16,20)
	    screen:print(40,pos*22+8+42,fichiers[i].name,noir)
	 elseif string.lower(nom) == ".jpg" or string.lower(name) == ".jpeg" then
	    screen:blit(20,pos*22+42,enregistrer,53,273,25,20)
	    screen:print(48,pos*22+8+42,fichiers[i].name,noir)
	 elseif string.lower(nom) == ".gif" then
	    screen:blit(20,pos*22+42,enregistrer,78,273,25,20)
	    screen:print(48,pos*22+8+42,fichiers[i].name,noir)
	 elseif string.lower(nom) == ".bmp" then
	    screen:blit(20,pos*22+42,enregistrer,103,273,25,20)
	    screen:print(48,pos*22+8+42,fichiers[i].name,noir)
	 elseif string.lower(nom) == ".lua" then
	    screen:blit(20,pos*22+42,enregistrer,128,273,16,20)
	    screen:print(40,pos*22+8+42,fichiers[i].name,noir)
	 elseif string.lower(nom) == ".png" then
	    screen:blit(20,pos*22+42,enregistrer,144,273,25,20)
	    screen:print(48,pos*22+8+42,fichiers[i].name,noir)
	 elseif string.lower(nom) == ".mp3" or string.lower(nom) == ".wav" then
	    screen:blit(20,pos*22+42,enregistrer,169,273,16,20)
	    screen:print(40,pos*22+8+42,fichiers[i].name,noir)
	 else
	    screen:blit(20,pos*22+42,enregistrer,37,273,16,20)
	    screen:print(40,pos*22+8+42,fichiers[i].name,noir)
	 end
   end
end
function afficher_contenu(d, f, dossier)
   if chercher == true then
	 fichiers = nil
	 System.currentDirectory(dossier)
	 fichiers = System.listDirectory()
	 chercher = false
	 yselectionneur = 64
   end
   n_fichiers = table.getn(fichiers)
   pos = 1
   for i=d, n_fichiers do
	 if i <= f then
	    afficher_partie(i, pos)
	 end
	 pos = pos + 1
   end 
end

function afficher_selectionneur()
   screen:blit(15,yselectionneur,enregistrer,15,300,450,20)
   if pad:down() and oldpad:down() ~= pad:down()  and yselectionneur ~= 218 and fichier_selectionne < n_fichiers then
	 yselectionneur = yselectionneur + 22
	 fichier_selectionne = fichier_selectionne + 1
   end
   if pad:up() and oldpad:up() ~= pad:up() and yselectionneur ~= 42 then
	 yselectionneur = yselectionneur - 22
	 if position_selectionneur > 0 then
	    fichier_selectionne = fichier_selectionne - 1
	 end
   end
   position_selectionneur = (yselectionneur-64) / 22
end

function aff_text_enregistrer()
   screen:print(67,244,txt_fichier)
end

function aff_clavier_enregistrer()
   if pad:l() and oldpad:l() ~= pad:l() then
		if type_clavier == 1 then
			type_clavier = 2
		elseif type_clavier == 2 then
			type_clavier = 1
		end
	end
	if pad:r() then
		if type_clavier == 1 then
			type_clavier2 = 1
		end
		if type_clavier == 2 then
			type_clavier2 = 2
		end
	else
		type_clavier2 = 0
	end
	
		
		
   if type_clavier == 1 and type_clavier2 ~= 1 then
	screen:blit(323,100,clavier,0,0,151,151)
	 --milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(374,151,clavier,202,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then effacer_enregistrer() end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("m") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer(" ") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("n") end
	 end
	 --haut gauche
	 if pad:analogX() < -50 and pad:analogY() < -50 then
	    screen:blit(324,101,clavier,152,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer(",") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("a") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("b") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("c") end
	 end
	 --haut milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() < -50 then
	    screen:blit(374,101,clavier,202,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer(".") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("d") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("e") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("f") end
	 end
	 --haut droite
	 if pad:analogX() > 50 and pad:analogY() < -50 then
	    screen:blit(424,101,clavier,252,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("!") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("g") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("h") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("i") end
	 end
	 --gauche
	 if pad:analogX() < -50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(324,151,clavier,152,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("-") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("j") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("k") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("l") end
	 end
	 --droite
	 if pad:analogX() > 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(424,151,clavier,252,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("?") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("o") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("p") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("q") end
	 end
	 --bas gauche
	 if pad:analogX() < -50 and pad:analogY() > 50 then
	    screen:blit(324,201,clavier,152,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("(") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("r") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("s") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("t") end
	 end
	 --bas milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > 50 then
	    screen:blit(374,201,clavier,202,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer(":") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("u") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("v") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("w") end
	 end
	 --bas droite
	 if pad:analogX() > 50 and pad:analogY() > 50 then
	    screen:blit(424,201,clavier,252,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer(")") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("x") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("y") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("z") end
	 end
   end
   if type_clavier == 2 and type_clavier2 ~= 2 then
		screen:blit(323,100,clavier1,0,0,151,151)
	 --milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(374,151,clavier1,202,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then effacer_enregistrer() end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer(" ") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("5") end
	 end
	 --haut gauche
	 if pad:analogX() < -50 and pad:analogY() < -50 then
	    screen:blit(324,101,clavier1,152,1,49,49)
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("1") end
	 end
	 --haut milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() < -50 then
	    screen:blit(374,101,clavier1,202,1,49,49)
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("2") end
	 end
	 --haut droite
	 if pad:analogX() > 50 and pad:analogY() < -50 then
	    screen:blit(424,101,clavier1,252,1,49,49)
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("3") end
	 end
	 --gauche
	 if pad:analogX() < -50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(324,151,clavier1,152,51,49,49)
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("4") end
	 end
	 --droite
	 if pad:analogX() > 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(424,151,clavier1,252,51,49,49)
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("6") end
	 end
	 --bas gauche
	 if pad:analogX() < -50 and pad:analogY() > 50 then
	    screen:blit(324,201,clavier1,152,101,49,49)
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("7") end
	 end
	 --bas milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > 50 then
	    screen:blit(374,201,clavier1,202,101,49,49)
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("8") end
	 end
	 --bas droite
	 if pad:analogX() > 50 and pad:analogY() > 50 then
	    screen:blit(424,201,clavier1,252,101,49,49)
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("0") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("9") end
	 end
   end
	
	if type_clavier2 == 1 then
	screen:blit(323,100,clavier2,0,0,151,151)
	 --milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(374,151,clavier2,202,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then effacer_enregistrer() end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("M") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer(" ") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("N") end
	 end
	 --haut gauche
	 if pad:analogX() < -50 and pad:analogY() < -50 then
	    screen:blit(324,101,clavier2,152,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("^") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("A") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("B") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("C") end
	 end
	 --haut milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() < -50 then
	    screen:blit(374,101,clavier2,202,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("@") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("D") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("E") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("F") end
	 end
	 --haut droite
	 if pad:analogX() > 50 and pad:analogY() < -50 then
	    screen:blit(424,101,clavier2,252,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("*") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("G") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("H") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("I") end
	 end
	 --gauche
	 if pad:analogX() < -50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(324,151,clavier2,152,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("_") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("J") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("K") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("L") end
	 end
	 --droite
	 if pad:analogX() > 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(424,151,clavier2,252,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("\"") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("O") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("P") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("Q") end
	 end
	 --bas gauche
	 if pad:analogX() < -50 and pad:analogY() > 50 then
	    screen:blit(324,201,clavier2,152,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("=") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("R") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("S") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("T") end
	 end
	 --bas milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > 50 then
	    screen:blit(374,201,clavier2,202,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer(";") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("U") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("V") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("W") end
	 end
	 --bas droite
	 if pad:analogX() > 50 and pad:analogY() > 50 then
	    screen:blit(424,201,clavier2,252,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("/") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("X") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("Y") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("Z") end
	 end
   end
	if type_clavier2 == 2 then
	screen:blit(323,100,clavier3,0,0,151,151)
	 --milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(374,151,clavier3,202,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then effacer_enregistrer() end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer(" ") end
	 end
	 --haut gauche
	 if pad:analogX() < -50 and pad:analogY() < -50 then
	    screen:blit(324,101,clavier3,152,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer(",") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("(") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer(".") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer(")") end
	 end
	 --haut milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() < -50 then
	    screen:blit(374,101,clavier3,202,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("\"") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("<") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("'") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer(">") end
	 end
	 --haut droite
	 if pad:analogX() > 50 and pad:analogY() < -50 then
	    screen:blit(424,101,clavier3,252,1,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("-") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("[") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("_") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("]") end
	 end
	 --gauche
	 if pad:analogX() < -50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(324,151,clavier3,152,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("!") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("{") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("?") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("}") end
	 end
	 --droite
	 if pad:analogX() > 50 and pad:analogY() > -50 and pad:analogY() < 50 then
	    screen:blit(424,151,clavier3,252,51,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("+") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("\\") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("=") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("/") end
	 end
	 --bas gauche
	 if pad:analogX() < -50 and pad:analogY() > 50 then
	    screen:blit(324,201,clavier3,152,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer(":") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("@") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer(";") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("#") end
	 end
	 --bas milieu
	 if pad:analogX() > -50 and pad:analogX() < 50 and pad:analogY() > 50 then
	    screen:blit(374,201,clavier3,202,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("~") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("$") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("`") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("%") end
	 end
	 --bas droite
	 if pad:analogX() > 50 and pad:analogY() > 50 then
	    screen:blit(424,201,clavier3,252,101,49,49)
	    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then ajouter_lettre_enregistrer("*") end
	    if pad:square() and oldpad:square() ~= pad:square() then ajouter_lettre_enregistrer("^") end
	    if pad:cross() and oldpad:cross() ~= pad:cross() then ajouter_lettre_enregistrer("|") end
	    if pad:circle() and oldpad:circle() ~= pad:circle() then ajouter_lettre_enregistrer("&") end
	 end
   end
end 
  
function ajouter_lettre_enregistrer(lettre)
   enleve_debut = string.sub(txt_fichier,((xcurseur_enregistrer - 65) / 8)+1)
   enleve_fond = string.sub(txt_fichier,0,string.len(txt_fichier) - string.len(enleve_debut))
   txt_fichier = enleve_fond .. lettre .. enleve_debut
   xcurseur_enregistrer = xcurseur_enregistrer + 8
end
function effacer_enregistrer()
   if txt_fichier == nil then
   elseif positionL_curseur_enregistrer == 0 then
   else
	enleve_debut = string.sub(txt_fichier,((xcurseur_enregistrer - 65) / 8)+1)
	enleve_fond = string.sub(txt_fichier,0,string.len(txt_fichier) - string.len(enleve_debut) - 1)
	txt_fichier = enleve_fond .. enleve_debut
	xcurseur_enregistrer = xcurseur_enregistrer - 8
   end
end

function aff_dossier()
   tmp = System.currentDirectory()
   if string.len(tmp) > 55 then
   tmp = string.sub(tmp,-55)
   end
   screen:print(17,44,tmp,blanc)
   tmp = nil
end

function img_viewer(img_path)
img = Image.load(img_path)
   while true do
	 pad = Controls.read()
	 if pad:circle() then
	    oldpad = pad
	    img = nil
	    System.memclean()
	    return
	 end
	 screen:clear()
	 screen:blit(0,0,img)
	 screen.waitVblankStart()
	 screen.flip()
   end
end

while done_enregistrer == false do
   pad = Controls.read()
   screen:clear()
   screen:blit(0,0,enregistrer,0,0,480,272)
   aff_dossier()
   cursor_enregistrer()
   afficher_selectionneur()
   afficher_contenu(debut, fin, dossier_en_cours)
   aff_text_enregistrer()

   if position_selectionneur == 7 and pad:down() and oldpad:down() ~= pad:down() then
	 yselectionneur = 64
	 debut = debut + 7
	 fin = fin + 7
	 n_liste = n_liste + 1
   end
   if position_selectionneur == -1 and pad:up() and oldpad:up() ~= pad:up() and n_liste > 1 then
	 yselectionneur = 196
	 debut = debut - 7
	 fin = fin - 7
	 n_liste = n_liste - 1
	 fichier_selectionne = fichier_selectionne - 1
   elseif position_selectionneur == -1 and pad:up() and oldpad:up() ~= pad:up() and n_liste <= 1 then
	 yselectionneur = 64
   end
   
   if pad:select() and oldpad:select() ~= pad:select() then
	 edit_menu()
   end
   
   if pad:r() and oldpad:r() ~= pad:r() and aff_clavier_enr == false then 
	  if multi_win_nb == 1 then
	     if i_defilement_horizontal_win1 == 4 then
		 i_defilement_horizontal_win1 = i_defilement_horizontal_win1-1
		 n_lignes_defilement = table.getn(multi_win_lignes1)
		 for id=1, n_lignes_defilement do
		 	multi_win_lignes1[id] = multi_win1_sauv_ligne3[id]..multi_win_lignes1[id]
		 end
      end
      if i_defilement_horizontal_win1 == 3 then
		 i_defilement_horizontal_win1 = i_defilement_horizontal_win1-1
		 n_lignes_defilement = table.getn(multi_win_lignes1)
		 for id=1, n_lignes_defilement do
			multi_win_lignes1[id] = multi_win1_sauv_ligne2[id]..multi_win_lignes1[id]
		 end
      end
      if i_defilement_horizontal_win1 == 2 then
		 i_defilement_horizontal_win1 = i_defilement_horizontal_win1-1
		 n_lignes_defilement = table.getn(multi_win_lignes1)
		 for id=1, n_lignes_defilement do
			multi_win_lignes1[id] = multi_win1_sauv_ligne1[id]..multi_win_lignes1[id]
		 end
      end
      if i_defilement_horizontal_win1 == 1 then
		 i_defilement_horizontal_win1 = i_defilement_horizontal_win1 - 1
		 n_lignes_defilement = table.getn(multi_win_lignes1)
		 for id=1, n_lignes_defilement do
			multi_win_lignes1[id] = multi_win1_sauv_ligne[id]..multi_win_lignes1[id]
		 end
      end
	    file = io.open(txt_fichier,"w")
	    n = table.getn(multi_win_lignes1)
	    if n > 0 then
		  for i=1, n do
			System.sleep(5)
			file:write(multi_win_lignes1[i].."\n")
		  end
		  file:close()
	    else
		  file:write("LuaProg - Aucune ligne a enregistrer.")
		  file:close()
	    end
	 else
	    if i_defilement_horizontal_win2 == 4 then
		  i_defilement_horizontal_win2 = i_defilement_horizontal_win2-1
		  n_lignes_defilement = table.getn(multi_win_lignes2)
		  for id=1, n_lignes_defilement do
			multi_win_lignes2[id] = multi_win2_sauv_ligne3[id]..multi_win_lignes2[id]
		  end
	    end
	    if i_defilement_horizontal_win2 == 3 then
		  i_defilement_horizontal_win2 = i_defilement_horizontal_win2-1
		  n_lignes_defilement = table.getn(multi_win_lignes2)
		  for id=1, n_lignes_defilement do
			multi_win_lignes2[id] = multi_win2_sauv_ligne2[id]..multi_win_lignes2[id]
		  end
	    end
	    if i_defilement_horizontal_win2 == 2 then
		  i_defilement_horizontal_win2 = i_defilement_horizontal_win2-1
		  n_lignes_defilement = table.getn(multi_win_lignes2)
		  for id=1, n_lignes_defilement do
			multi_win_lignes2[id] = multi_win2_sauv_ligne1[id]..multi_win_lignes2[id]
		  end
	    end
	    if i_defilement_horizontal_win2 == 1 then
		  i_defilement_horizontal_win2 = i_defilement_horizontal_win2 - 1
		  n_lignes_defilement = table.getn(multi_win_lignes2)
		  for id=1, n_lignes_defilement do
			multi_win_lignes2[id] = multi_win2_sauv_ligne[id]..multi_win_lignes2[id]
		  end
	    end
	    file = io.open(txt_fichier,"w")
	    n = table.getn(multi_win_lignes2)
	    if n > 0 then
		  for i=1, n do
			System.sleep(5)
			file:write(multi_win_lignes2[i].."\n")
		  end
		  file:close()
	    else
		  file:write("LuaProg - Aucune ligne a enregistrer.")
		  file:close()
	    end
	 end
	 done_enregistrer = true
	 System.currentDirectory(repertoire_defaut)
	 statut_menu = "no"
   end

   if pad:cross() and oldpad:cross() ~= pad:cross() and aff_clavier_enr == false and aff_aide_enregistrer == false then
	 if fichiers[fichier_selectionne].directory == true then
	    if dossier_en_cours == "ms0:/" then
		  dossier_en_cours =  System.currentDirectory()..fichiers[fichier_selectionne].name
	    else
		  dossier_en_cours =  System.currentDirectory().."/"..fichiers[fichier_selectionne].name
	    end
	    chercher = true
	    fichier_selectionne = 1
	    n_liste = 1
	    debut = 1
	    fin = 7
	 elseif string.lower(nom) == ".mp3" then
	    if lect==1 then Mp3me.stop() System.memclean() end
	    Mp3me.load(fichiers[fichier_selectionne].name) 
	    Mp3me.play()
	 elseif string.lower(nom) == ".png" or string.lower(nom) == ".jpg" then
	   img_viewer(fichiers[fichier_selectionne].name)
	 elseif string.lower(nom) == ".iso" or string.lower(nom) == ".cso" then
	    System.startISO(repertoire..fichiers[fichier_selectionne].name)
	 end
   end

   if pad:l() and oldpad:l() ~= pad:l() and aff_clavier_enr == false then
	 done_enregistrer = true
	 System.currentDirectory(repertoire_defaut)
	 statut_menu = "no"
   end

   if pad:triangle() and oldpad:triangle() ~= pad:triangle() and aff_clavier_enr == false then
	 dossier_en_cours =  System.currentDirectory().."/.."
	 chercher = true
	 fichier_selectionne = 1
	 n_liste = 1
	 debut = 1
	 fin = 7
   end
   if aff_clavier_enr == true then aff_clavier_enregistrer() end

   if pad:start() and oldpad:start() ~= pad:start() and aff_clavier_enr == true then
	 aff_clavier_enr = false
   elseif pad:start() and oldpad:start() ~= pad:start() and aff_clavier_enr == false then
	 aff_clavier_enr = true
   end
   screen.waitVblankStart()
   screen.flip()
   oldpad = pad
end
