with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Strings;
use Ada.Strings;
with Tcl.Ada;
package body Mcc.Gui.Image is

   ------------
   -- Create --
   ------------

   procedure Create (Obj : out External_Image; Name : in String) is
      Changed_Name : String := Name;
   begin
      -- change to Tcl style file names
      for i in Changed_Name'range loop
         if Changed_Name(i) = '\' then
            Changed_Name(i) := '/';
         end if;
      end loop;
      
      Obj.My_Peer := Peer.Create_Peer;
      Obj.My_Peer.Name(Obj.My_Peer.Name'First) := 'p';
      -- get rid of beginning '.'
      
      Peer.Eval("image create photo " & 
         Obj.My_Peer.Name.all & 
         " -file " & Changed_Name);
   exception when TCL.ADA.TCL_ERROR_EXCEPTION =>
      declare
         Exe_Name : String := Ada.Command_Line.Command_Name;
         Last_Slash : Integer;
      begin
         for i in Exe_Name'range loop
            if Exe_Name(i) = '\' then
               Exe_Name(i) := '/';
            end if;
         end loop;
         Last_Slash := Ada.Strings.Fixed.Index(Exe_Name,"/",Backward);
         Peer.Eval("image create photo " & 
            Obj.My_Peer.Name.all & 
            " -file """ & Exe_Name(1..Last_Slash) & Changed_Name & """");
      end;   
   end Create;

end Mcc.Gui.Image;