-------------------------------------------------------------------
--           RAPID - RAPID ADA PORTABLE INTERFACE DESIGNER         
--           TCL PEER FOR THE MCC GUI PACKAGE LIBRARY 
--           Copyright (C) 1999 Martin C. Carlisle.                
--                                                                 
-- RAPID is free software;  you can  redistribute it  and/or modify
-- it under terms of the  GNU General Public License as published  
-- by the Free Software  Foundation;  either version 2,  or (at your
-- option) any later version.  RAPID is distributed in the hope that 
-- it will be useful, but WITHOUT ANY WARRANTY;  without even the 
-- implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE.  See the GNU General Public License for more details.  
-- You should have  received  a copy of the GNU General Public License
-- distributed with RAPID; see file COPYING.  If not, write to the 
-- Free Software Foundation,  59 Temple Place - Suite 330,  Boston, 
-- MA 02111-1307, USA.                                                 
--                                                                     
-- As a special exception, if other files instantiate generics from
-- this unit, or you link this unit with other files to produce an 
-- executable, this unit does not by itself cause the resulting 
-- executable to be covered by the GNU General Public License.  
-- This exception does not however invalidate any other reasons 
-- why the executable file might be covered by the GNU Public 
-- License.  This exception does not apply to executables which
-- are GUI design tools, or that could act as a replacement
-- for RAPID.
------------------------------------------------------------------------------
with Tcl.Tk;
with My_Expanding_Array;
with Cargv;
with Interfaces.C;
with Cargv_Helpers;

package body Mcc.Gui.Container.Window is
   Close_Command  : Tcl.Tcl_Command;
   Resize_Command : Tcl.Tcl_Command;

   Have_Close_Command  : Boolean := False;
   Have_Resize_Command : Boolean := False;

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

   procedure Create
     (Obj    : in out Main_Window;
      X      : in     Natural;
      Y      : in     Natural;
      Width  : in     Natural;
      Height : in     Natural)
   is
      Width_String : String := integer'image(width);
      Height_String : String := integer'image(height);
   begin
      Obj.My_Peer.Name := new String'(".");
      Peer.Eval(". configure -width " & Width_String &
         " -height " & Height_String & ASCII.LF & "update");
      Peer.Eval("wm geometry . =" &
         Width_String(Width_String'First+1..Width_String'Last) &
         "x" &
         Height_String(Height_String'First+1..Height_String'Last));
   end Create;

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

   procedure Create
     (Obj    : in out Subwindow;
      X      : in     Natural;
      Y      : in     Natural;
      Width  : in     Natural;
      Height : in     Natural)
   is
      Width_String : String := integer'image(width);
      Height_String : String := integer'image(height);
   begin
      Obj.My_Peer := Peer.Create_Peer;
      Peer.Eval("toplevel " & Obj.My_Peer.Name.all);
      Peer.Eval(Obj.My_Peer.Name.all & " configure -width " &
         integer'image(width) &
         " -height " & integer'image(height) & ASCII.LF & "update");
      Peer.Eval("focus -force " & Obj.My_Peer.Name.all);
      Peer.Eval("wm geometry " & Obj.My_Peer.Name.all & " =" &
         Width_String(Width_String'First+1..Width_String'Last) &
         "x" &
         Height_String(Height_String'First+1..Height_String'Last));
   end Create;

   ----------------
   -- Event_Loop --
   ----------------

   procedure Event_Loop is
   begin
      Tcl.Tk.Tk_Mainloop;
   end Event_Loop;

   ----------
   -- Hide --
   ----------

   procedure Hide (Obj : in Window) is
   begin
      Peer.Eval("wm withdraw " & Obj.My_Peer.Name.all);
   end Hide;

   function Is_Open(Obj : in Window) return Boolean is
   begin
      Peer.Eval("winfo width " & Obj.My_Peer.Name.all);
      return true;
   exception
      when others => return false;
   end Is_Open;
   
   --------------------------
   -- Tcl callback for
   -- Close events
   --------------------------
   function Close_Command_Function (
      ClientData : in Integer;
      Interp     : in Tcl.Tcl_Interp;
      Argc       : in Interfaces.C.Int;
      Argv       : in CArgv.Chars_Ptr_Ptr
   ) return Interfaces.C.Int;
   pragma Convention (C, Close_Command_Function);

   -- protocol for arguments will be
   -- 1st argument : lookup into Expanding_Array
   function Close_Command_Function (
      ClientData : in Integer;
      Interp     : in Tcl.Tcl_Interp;
      Argc       : in Interfaces.C.Int;
      Argv       : in CArgv.Chars_Ptr_Ptr
   ) return Interfaces.C.Int is
     Obj : Window_Pointer;
   begin
      Obj := Window_Pointer(My_Expanding_Array.Retrieve(
         Table    => My_Expanding_Array.Table,
         Location => Cargv_Helpers.Argument(Argv,1)));
      Obj.Close_Callback(Obj.all);
      return Tcl.TCL_OK;
   end Close_Command_Function;

   -----------------------
   -- Set_Close_Handler --
   -----------------------

   procedure Set_Close_Handler
     (Obj     : in Window_Pointer;
      Handler : in Close_Handler)
   is
   begin
      if Handler /= null then
         if Obj.My_Peer.Lookup = 0 then
            My_Expanding_Array.Insert(
               Table    => My_Expanding_Array.Table,
               Element  => Sized_Object_Pointer(Obj),
               Location => Obj.My_Peer.Lookup);
         end if;
   
         if not Have_Close_Command then
            Close_Command := Peer.CreateCommands.Tcl_CreateCommand (
               Peer.Get_Interp,
               "closecommand", Close_Command_Function'access, 0, NULL);
            Have_Close_Command := True;
         end if;
      
         Obj.Close_Callback := Handler;
      
         Peer.Eval("wm protocol " & Obj.My_Peer.Name.all &
            " WM_DELETE_WINDOW {closecommand " &
            Integer'Image(Obj.My_Peer.Lookup) & "}");
      else
         Peer.Eval("wm protocol " & Obj.My_Peer.Name.all &
            " WM_DELETE_WINDOW {destroy " &
            Obj.My_Peer.Name.all & "}");
      end if;      
   end Set_Close_Handler;

   --------------------------
   -- Tcl callback for
   -- Resize events
   --------------------------
   function Resize_Command_Function (
      ClientData : in Integer;
      Interp     : in Tcl.Tcl_Interp;
      Argc       : in Interfaces.C.Int;
      Argv       : in CArgv.Chars_Ptr_Ptr
   ) return Interfaces.C.Int;
   pragma Convention (C, Resize_Command_Function);

   -- protocol for arguments will be
   -- 1st argument : lookup into Expanding_Array
   -- 2nd argument : width
   -- 3rd argument : height
   function Resize_Command_Function (
      ClientData : in Integer;
      Interp     : in Tcl.Tcl_Interp;
      Argc       : in Interfaces.C.Int;
      Argv       : in CArgv.Chars_Ptr_Ptr
   ) return Interfaces.C.Int is
     Obj : Window_Pointer;
   begin
      Obj := Window_Pointer(My_Expanding_Array.Retrieve(
         Table    => My_Expanding_Array.Table,
         Location => Cargv_Helpers.Argument(Argv,1)));
      -- Unfortunately, tcl binds this event to the window and all
      -- of its descendants.  This verifies the event came from the
      -- window instead of a descendant
      if Obj.My_Peer.Name.all = Cargv_Helpers.Argument(Argv,2) then
         Obj.Resize_Callback(Obj.all,
            Width  => Cargv_Helpers.Argument(Argv,3),
            Height => Cargv_Helpers.Argument(Argv,4));
      end if;
      return Tcl.TCL_OK;
   end Resize_Command_Function;

   ------------------------
   -- Set_Resize_Handler --
   ------------------------

   procedure Set_Resize_Handler
     (Obj     : in Window_Pointer;
      Handler : in Resize_Handler)
   is
   begin
      if Obj.My_Peer.Lookup = 0 then
         My_Expanding_Array.Insert(
            Table    => My_Expanding_Array.Table,
            Element  => Sized_Object_Pointer(Obj),
            Location => Obj.My_Peer.Lookup);
      end if;

      if not Have_Resize_Command then
         Resize_Command := Peer.CreateCommands.Tcl_CreateCommand (
            Peer.Get_Interp,
            "resizecommand", Resize_Command_Function'access, 0, NULL);
         Have_Resize_Command := True;
      end if;
      Peer.Eval("update");
      Peer.Eval("bind " & Obj.My_Peer.Name.all &
         " <Configure> {resizecommand " &
         Integer'Image(Obj.My_Peer.Lookup) & " %W %w %h}");
      Obj.Resize_Callback := Handler;
   end Set_Resize_Handler;

   ---------------
   -- Set_Title --
   ---------------

   procedure Set_Title
     (Obj   : in out Window;
      Title : in     String)
   is
   begin
      Peer.Eval("wm title " & Obj.My_Peer.Name.all & " """ &
         Peer.Fix_Quotes(Title) &
         '"');
   end Set_Title;

   ----------
   -- Show --
   ----------

   procedure Show (Obj : in Window) is
   begin
      Peer.Eval("wm deiconify " & Obj.My_Peer.Name.all);
   end Show;

   ------------------------------------------------
   -- procedure To_Top
   --
   -- causes a window to be displayed on top
   ------------------------------------------------
   procedure To_Top(Obj : in Window) is
   begin
      Peer.Eval("raise " & Obj.My_Peer.Name.all);
      Peer.Eval("focus -force " & Obj.My_Peer.Name.all);
   end To_Top;

   -----------------------
   -- Get_Close_Handler --
   -----------------------

   function Get_Close_Handler
     (Obj : in Window'Class)
      return Close_Handler
   is
   begin
      return Obj.Close_Callback;
   end Get_Close_Handler;

   ------------------------
   -- Get_Resize_Handler --
   ------------------------

   function Get_Resize_Handler
     (Obj : in Window'Class)
      return Resize_Handler
   is
   begin
      return Obj.Resize_Callback;
   end Get_Resize_Handler;


end Mcc.Gui.Container.Window;