------------------------------------------------------------------------------
--                                                                          --
--                 GBManager - Win32 COM Binding Manager                    --
--                                                                          --
--                       G B M A N A G E R _ G U I                          --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                                                                          --
--                 Copyright (C) 1999 - 2005 David Botton                   --
--                                                                          --
-- This is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion. It 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 this;  see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- More information about GWINDOWS and the most current public version can  --
-- be located on the web at http://www.adapower.com/gwindows                --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with GWindows.Application;
with GWindows.Base;
with GWindows.Windows.Main;
with GWindows.Menus;
with GWindows.Common_Dialogs;
with GWindows.Constants;
with GProfileConfig;

with GBManager_Tree;
with GBManager_Lib;

package body GBManager_GUI is
   use GBManager_Tree;
   use GWindows.Windows.Main;
   use GWindows.Menus;
   use GWindows.Constants;
   use GProfileConfig;
   use GWindows.Base;

   ID_Exit  : constant := 40004;
   ID_About : constant := 40001;
   ID_Bind  : constant := 40000;
   ID_Set   : constant := 40003;
   ID_Look  : constant := 40002;

   procedure Do_Menu_Select
     (Window : in out GWindows.Base.Base_Window_Type'Class;
      Item   : in     Integer);
   --  Handle menu selections;

   type App_Window_Type is new Main_Window_Type with null record;

   procedure On_Destroy (Window : in out App_Window_Type);
   --  Handles closing down the message loop when the window is closed

   procedure On_Destroy (Window : in out App_Window_Type) is
      nLeft      : Integer;
      nTop       : Integer;
      nWidth     : Integer;
      nHeight    : Integer;
      WP         : WindowPlacement_Type := WindowPlacement(Window);
      nMaximized : Integer := 0;
   begin
      nLeft   := WP.rcNormalPosition.Left;
      nTop    := WP.rcNormalPosition.Top;
      nWidth  := WP.rcNormalPosition.Right - nLeft;
      nHeight := WP.rcNormalPosition.Bottom - nTop;
      if WP.showCmd = SW_SHOWMAXIMIZED then
	 nMaximized := 1;
      end if;
      WriteConfigInt(
		     "WindowPlacement",
		     "bMaximized",
		     nMaximized
		    );
      WriteConfigInt(
		     "WindowPlacement",
		     "leftNormal",
		     nLeft
		    );
      WriteConfigInt(
		     "WindowPlacement",
		     "topNormal",
		     nTop
		    );
      WriteConfigInt(
		     "WindowPlacement",
		     "widthNormal",
		     nWidth
		    );
      WriteConfigInt(
		     "WindowPlacement",
		     "heightNormal",
		     nHeight
		    );
      On_Destroy(Main_Window_Type(Window));
   end On_Destroy;


   Main_Window : App_Window_Type;
   Lib_Tree    : GBManager_Tree_Control_Type;

   ---------------
   -- Start_GUI --
   ---------------

   procedure Start_GUI is
      Main_Menu  : Menu_Type := Load_Menu("MAIN_MENU");
      Left       : Integer := GetConfigInt(
					   "WindowPlacement",
					   "leftNormal",
					   Use_Default
					  );
      Top        : Integer := GetConfigInt(
					   "WindowPlacement",
					   "topNormal",
					   Use_Default
					  );
      Width      : Integer := GetConfigInt(
					   "WindowPlacement",
					   "widthNormal",
					   Use_Default
					  );
      Height     : Integer := GetConfigInt(
					   "WindowPlacement",
					   "heightNormal",
					   Use_Default
					  );
      bMaximized : Boolean := GetConfigInt(
					   "WindowPlacement",
					   "bMaximized"
					  ) /= 0;
      nCmdShow   : Integer := SW_SHOWNORMAL;
   begin
      if bMaximized then
	 nCmdShow := SW_SHOWMAXIMIZED;
      end if;
      Create (
	      Main_Window,
              "GNATCOM GUI Binding Manager - Binding Repository at: " &
		GBManager_Lib.Get_Binding_Location,
	      Left,
	      Top,
	      Width,
	      Height
	     );
      Menu (Main_Window, Main_Menu);
      On_Menu_Select_Handler (Main_Window, Do_Menu_Select'Access);
      Large_Icon (Main_Window, "Main_Icon");
      Create (Lib_Tree, Main_Window,
              10, 10, 300, 300);
      Border (Lib_Tree);
      Dock (Lib_Tree, GWindows.Base.Fill);
      ShowWindow(Main_Window, nCmdShow);
      Dock_Children (Main_Window);
      UpdateWindow (Main_Window);
      GWindows.Application.Message_Loop;
   end Start_GUI;

   procedure Do_Menu_Select
     (Window : in out GWindows.Base.Base_Window_Type'Class;
      Item   : in     Integer)
   is
      pragma Warnings (Off, Window);
   begin
      case Item is
         when ID_Exit =>
            Close (Main_Window);
         when ID_Bind =>
            Do_Binding (Lib_Tree);
         when ID_Look =>
            Do_Look (Lib_Tree);
         when ID_Set =>
            GBManager_Lib.Set_Binding_Location
              (GWindows.Common_Dialogs.Get_Directory
                 (Main_Window, "Repository Location"));
            Text (Main_Window,
                  "GNATCOM GUI Binding Manager - Binding Repository at: " &
                    GBManager_Lib.Get_Binding_Location);
         when ID_About =>
            null;
         when others =>
            null;
      end case;
   end Do_Menu_Select;

end GBManager_GUI;
