-------------------------------------------------------------------
--           RAPID - RAPID ADA PORTABLE INTERFACE DESIGNER         
--           MCC GUI PACKAGE LIBRARY                               
--           GWINDOWS PEER IMPLEMENTATION                               
--           Copyright (C) 2001 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 Gwindows.Windows.Main;
with Gwindows.Windows;
with Gwindows.Base;
use Gwindows.Base;
with Gwindows.Application;
with Peer_To_Mcc;
with Peer;
use Peer;
package body Mcc.Gui.Container.Window is

   procedure My_Close_Handler(
         Window    : in out GWindows.Base.Base_Window_Type'Class;
         Can_Close :    out Boolean) is
      The_Window : Window_Pointer;
   begin
      The_Window := Window_Pointer(
         Peer_To_Mcc.Hash_Package.Get(Window'Address));
      if The_Window.Close_Callback/=null then
         Can_Close := False;
         The_Window.Close_Callback.all(
            Obj    => The_Window.all);
      end if;
   end My_Close_Handler;

   procedure My_Resize_Handler(
         Window : in out GWindows.Base.Base_Window_Type'Class;
         Width  : in     Integer;
         Height : in     Integer) is
      The_Window : Window_Pointer;
   begin
      The_Window := Window_Pointer(
         Peer_To_Mcc.Hash_Package.Get(Window'Address));
      if The_Window.Resize_Callback/=null then
         The_Window.Resize_Callback.all(
            Obj    => The_Window.all,
            Width  => Width,
            Height => Height);
      end if;
   end My_Resize_Handler;
   
   ------------
   -- Create --
   ------------

   procedure Create
     (Obj    : in out Main_Window;
      X      : in     Natural;
      Y      : in     Natural;
      Width  : in     Natural;
      Height : in     Natural)
   is
      My_Peer : Gwindows.Windows.Main.Main_Window_Access;
   begin
      My_Peer := new Gwindows.Windows.Main.Main_Window_Type;
      Gwindows.Windows.Main.Create(
         Window => My_Peer.all,
         Left   => X,
         Top    => Y,
         Width  => Width,
         Height => Height);
      Gwindows.Windows.Main.Visible(My_Peer.all,True);
      Peer.Set_Main_Window_Peer(Peer.Main_Peer(My_Peer));
      Obj.My_Peer := Peer.Peer(My_Peer);
      Peer_To_Mcc.Hash_Package.Set(
         K => My_Peer.all'Address,
         E => Obj'Unrestricted_Access);
      Gwindows.Windows.Main.On_Size_Handler(
         Window  => My_Peer.all,
         Handler => My_Resize_Handler'access);
      Gwindows.Windows.Main.On_Close_Handler(
         Window  => My_Peer.all,
         Handler => My_Close_Handler'access);
   end Create;

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

   procedure Create
     (Obj    : in out Subwindow;
      X      : in     Natural;
      Y      : in     Natural;
      Width  : in     Natural;
      Height : in     Natural)
   is
      My_Peer : Gwindows.Windows.Window_Access;
   begin
      My_Peer := new Gwindows.Windows.Window_Type;
      Gwindows.Windows.Create(
         Window => My_Peer.all,
         Left   => X,
         Top    => Y,
         Width  => Width,
         Height => Height);
      Gwindows.Windows.Visible(My_Peer.all,True);
      Obj.My_Peer := Peer.Peer(My_Peer);
      Peer_To_Mcc.Hash_Package.Set(
         K => My_Peer.all'Address,
         E => Obj'Unrestricted_Access);
      Gwindows.Windows.On_Size_Handler(
         Window  => My_Peer.all,
         Handler => My_Resize_Handler'access);
      Gwindows.Windows.On_Close_Handler(
         Window  => My_Peer.all,
         Handler => My_Close_Handler'access);
   end Create;

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

   procedure Event_Loop is
   begin
      Gwindows.Application.Message_Loop;
   end Event_Loop;

   -----------------------
   -- 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;

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

   procedure Hide (Obj : in Window) is
   begin
      Gwindows.Windows.Visible(Gwindows.Windows.Window_Type(
         Obj.My_Peer.all),False);
   end Hide;

   ------------------------------------------------
   -- function Is_Open
   --
   -- returns True if the Window is open (even if 
   -- hidden).  Returns false if window has not
   -- been created, or has been destroyed or closed
   ------------------------------------------------
   function Is_Open(Obj : in Window) return Boolean is
   begin
      if Obj.My_Peer /= null then
         return True;
      else
         return False;
      end if;
       
   end Is_Open;
   
   -----------------------
   -- Set_Close_Handler --
   -----------------------

   procedure Set_Close_Handler
     (Obj     : in Window_Pointer;
      Handler : in Close_Handler)
   is
   begin
      Obj.Close_Callback := Handler;
   end Set_Close_Handler;

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

   procedure Set_Resize_Handler
     (Obj     : in Window_Pointer;
      Handler : in Resize_Handler)
   is
   begin
      Obj.Resize_Callback := Handler;
   end Set_Resize_Handler;

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

   procedure Set_Title
     (Obj   : in out Window;
      Title : in     String)
   is
   begin
      Gwindows.Base.Text(Gwindows.Base.Base_Window_Type(
         Obj.My_Peer.all),
         +Title);
   end Set_Title;

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

   procedure Show (Obj : in Window) is
   begin
      Gwindows.Windows.Visible(Gwindows.Windows.Window_Type(
         Obj.My_Peer.all),True);
   end Show;

   ------------
   -- To_Top --
   ------------

   procedure To_Top (Obj : in Window) is
   begin
      Gwindows.Base.Focus(Gwindows.Base.Base_Window_Type(
         Obj.My_Peer.all));
   end To_Top;

end Mcc.Gui.Container.Window;

