---------------------------------------------------------------
--                                                           
--  RAPID - Rapid Ada Portable Interface Designer     
--                                                           
--  RAPID.ADB 
--  Description : Main procedure 
--                       
--  By: Martin Carlisle
--                                         
-- RAPID is free software; you can redistribute it and/or 
-- modify it without restriction.  However, we ask that you
-- please retain the original author information, and clearly  
-- indicate if it has been modified.
--  
-- 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.  
--
---------------------------------------------------------------
with Main_Window;
with Toolbar;
with Ada.Exceptions;
with Ada.Command_Line;
with Mcc.Gui.Container.Window;
with Rapid_Helpers;
with Ada.Strings.Unbounded;
--with Mcc.Directory_Operations;
with State;
with Gui.Window;
with Mcc.Common_Dialogs;
with File_Menu;
with Ada.Text_Io;
use Ada.Text_Io;

procedure Rapid is

begin
   main_Window.Generate_Window;
   Toolbar.Initialize_Toolbar;
   -- set minimum window size
   Mcc.Gui.Container.Window.Set_Resize_Handler(
      Obj     => main_Window.main_Window'access,
      Handler => Rapid_Helpers.Rapid_Resize_Handler'access);

   -- adapted from submission by Dmitri Anisimkov
   -- allows Windows users to double click a .GUI file
   -- and have it be opened in RAPID

   if Ada.Command_Line.Argument_Count=1 then
      declare
         Argument      : constant String  := Ada.Command_Line.Argument(1);
         Has_Directory :          Boolean := False;
         End_Directory :          Natural;
         -- following will be filled in based on argument
         Filename      : Ada.Strings.Unbounded.Unbounded_String;
         use Ada.Strings.Unbounded;
      begin
--         for i in reverse Argument'range loop
--            if Argument(i) = Mcc.Directory_Operations.
--               Directory_Separator and not Has_Directory then
--               Has_Directory := True;
--               End_Directory := i;
--            end if;
--         end loop;
         
--         if Has_Directory then
            Filename := To_Unbounded_String(Argument);
--            Mcc.Directory_Operations.Change_Dir(
--               Argument(Argument'First..End_Directory-1));
--         else
--            Filename := To_Unbounded_String(
--               Mcc.Directory_Operations.Get_Current_Dir &
--               Argument);
--         end if;
      
         State.Set_Current_Window(State.Allocate_Window);
         State.Set_Filename(To_String(Filename));
--         State.Set_Directory(
         --            Mcc.Directory_Operations.Get_Current_Dir);
         Gui.Window.Read_Window(Window => State.Get_Current_Window.all,
           Filename => State.Get_Filename);
         Gui.Window.Display_Window(
            Window => State.Get_Current_Window.all);
         State.Set_Changed(False);
      exception
         when others =>
            Mcc.Common_Dialogs.Ok_Box("Unable to open " &
               To_String(Filename));
      end;
   end if;
   Mcc.Gui.Container.Window.Set_Close_Handler(
      Obj     => Main_Window.Main_Window'access,
      Handler => File_Menu.Exit_Choice'access);
   Mcc.Gui.Set_Key_Listener(
      Obj      => Main_Window.Main_Window'access,
      Listener => State.Main_Window_Key_Listener'access);
   Mcc.Gui.Container.Window.Event_Loop;
exception when E:others =>
   declare
      Window   : Gui.Window.Window_Pointer := State.Get_Current_Window;
      Filename : aliased String := "emergency.gui";
      use type Gui.Window.Window_Pointer;
   begin
      if Window /= null then
         Window.Filename := Filename'Unchecked_Access;
         Gui.Window.Write_Window(Window.all);
      end if;
      
   end;
   Mcc.Common_Dialogs.OK_Box(
      "Open window (if any) dumped to emergency.gui" & ASCII.LF &
      "Uncaught exception -- contact author:" &
      Ada.Exceptions.Exception_Information(E));
   raise;
end Rapid;