-------------------------------------------------------------------
--           RAPID - RAPID ADA PORTABLE INTERFACE DESIGNER         
--           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 Gdk;
with Gdk.Types;
with Gdk.Types.Keysyms;
with Gdk.Window;
with Glib;
with gtk.accel_group;
with Gtk.Box;
with Gtk.Fixed;
with gtk.menu;
with gtk.menu_item;
with gtk.menu_bar;
with Gtk.Label;
with Gtk.Widget;
with Gtk.Window;
with Mcc.Gtk_signals;
with Mcc.Gui.Container.Window;
with mcc.gui.window_peer;
with Peer.Frame;
use Peer.Frame;
with System;
with ada.text_io;
with ada.characters.handling;
use ada.characters.handling;
package body Mcc.Gui.Menu is

   
   type mcc_menu_bar_record is new Gtk.Menu_Bar.Gtk_Menu_Bar_Record with record
      Accel_Group : gtk.accel_group.Gtk_Accel_Group;
      Is_Frame    : Boolean := True;
      First_One   : Boolean := True;
      Vbox        : gtk.box.Gtk_Vbox;
   end record;
   type mcc_menu_bar is access all mcc_menu_bar_record'class;
   
   type mcc_menu_record is new Gtk.Menu.Gtk_Menu_Record with record
      Accel_Group  : gtk.accel_group.Gtk_Accel_Group;
      submenu_item : gtk.menu_item.Gtk_Menu_Item;
   end record;
   type mcc_menu is access all mcc_menu_record'class;
   
   type mcc_menu_item_record is new Gtk.Menu_Item.Gtk_Menu_Item_Record with record
      Accel_Group : gtk.accel_group.Gtk_Accel_Group;
   end record;
   type mcc_menu_item is access all mcc_menu_item_record'class;



   procedure Set_Menu_Item_Pattern(
       Submenu_Item : in Gtk.Menu_Item.Gtk_Menu_Item;
       Text         : in String;
       Underline    : in Natural) is
      Label    : Gtk.Label.Gtk_Label;
      Children : Gtk.Widget.Widget_List.Glist;
      use Gtk.Widget;
      Underlines   : String(1..Text'Length) := (others => ' ');
   begin
      if Underline > 0 and Underline <= Text'Length then
         Underlines(Underline) := '_';
      else
         return;
      end if;
      
      children := gtk.menu_item.children(submenu_item);
      -- all good C programmers have a 0th child
      Label := Gtk.Label.Gtk_Label(
         gtk.widget.widget_list.nth_data(children,0));
      Gtk.Label.Set_Pattern(Label,Underlines);
   end Set_Menu_Item_Pattern;

   procedure Get_Keystroke(Accelerator : in String;
      Keycode   : out Gdk.Types.Gdk_Key_Type;
      Modifier  : out Gdk.Types.Gdk_Modifier_Type) is
      use type Gdk.Types.Gdk_Modifier_Type;
      function Get_Keycode(Key : in String) return Gdk.Types.Gdk_Key_Type is
         use type Gdk.Types.Gdk_Key_Type;
      begin
         if Ada.Characters.Handling.To_Upper(Key(Key'First)) = 'F' and
            Key'Length > 1 then
            return Gdk.Types.Gdk_Key_Type'value(Key(Key'First+1..Key'Last)) +
               Gdk.Types.Keysyms.GDK_F1-1;
         else
            return Gdk.Types.Gdk_Key_Type(Character'Pos(Key(Key'First)));
         end if;
      end Get_Keycode;
      
   begin
      if accelerator'length > 5 and then Ada.Characters.Handling.To_Lower(
         Accelerator(Accelerator'First..Accelerator'First+4)) = "ctrl+" then
         Modifier := Gdk.Types.Control_MASK;
         Keycode := Get_Keycode(
            Accelerator(Accelerator'First+5..Accelerator'Last));
      elsif accelerator'length > 4 and then Ada.Characters.Handling.To_Lower(
         Accelerator(Accelerator'First..Accelerator'First+3)) = "alt+" then
         Modifier := Gdk.Types.Mod1_Mask;
         Keycode := Get_Keycode(
            Accelerator(Accelerator'First+4..Accelerator'Last));
      elsif accelerator'length > 2 and then Ada.Characters.Handling.To_Lower(
         Accelerator(Accelerator'First..Accelerator'First+2)) = "del" then
         Modifier := 0;
         Keycode := Gdk.Types.Keysyms.Gdk_Delete;
      elsif accelerator'length > 2 and then Ada.Characters.Handling.To_Lower(
         Accelerator(Accelerator'First..Accelerator'First+2)) = "ins" then
         Modifier := 0;
         Keycode := Gdk.Types.Keysyms.Gdk_Insert;
      else
         Modifier := 0;
         Keycode := Get_Keycode(Accelerator);
      end if;
      
   end Get_Keystroke;

   procedure Add_Choice_Common(
         Obj         : in out Choice;
         To_Menu     : in     Menu'Class;
         Text        : in     String;
         Action      : in     Menu_Callback;
         Underline   : in     Natural;
         Accelerator : in     String;
         Item        : in     Gtk.Menu_Item.Gtk_Menu_Item) is
      Keycode   : Gdk.Types.Gdk_Key_Type;
      Modifier  : Gdk.Types.Gdk_Modifier_Type;
   begin
      Set_Menu_Item_Pattern(
         Submenu_Item => Item,
         Text         => Text,
         Underline    => Underline);

      if Accelerator /= "" then
         Get_Keystroke(
            Accelerator => Accelerator,
            Keycode     => Keycode,
            Modifier    => Modifier);

         gtk.accel_group.unlock(
            Mcc_Menu(To_Menu.My_Peer).accel_group);
         gtk.menu_item.Add_Accelerator(
            Widget       => item,
            Accel_Signal => "activate",
            Accel_Group  => Mcc_Menu(To_Menu.My_Peer).accel_group,
            Accel_Key    => keycode,
            Accel_Mods   => modifier,
            Accel_Flags  => Gtk.Accel_Group.Accel_Visible);
         gtk.accel_group.lock(
            Mcc_Menu(To_Menu.My_Peer).accel_group);
      end if;

      if Underline > 0 then
         gtk.menu_item.Add_Accelerator(
            Widget       => item,
            Accel_Signal => "activate",
            Accel_Group  => Ensure_Uline_Accel_Group(
               Mcc_Menu(To_Menu.My_Peer)),
            Accel_Key    => Gdk.Types.Gdk_Key_Type(
               Character'Pos(To_Lower(Text(Underline)))),
            Accel_Mods   => 0,
            Accel_Flags  => Gtk.Accel_Group.Accel_Locked);
      end if;
      
      if Action /= null then
         Mcc.Gtk_Signals.Gtk_Signal_Void_Connect(
            item,"activate",Mcc.Gtk_Signals.Void_Callback(Action));
      end if;
      Obj.My_Peer := gtk.root_type_access(Item);
   end Add_Choice_Common;
   ----------------
   -- Add_Choice --
   ----------------

   procedure Add_Choice(Obj         : in out Choice;
                         To_Menu     : in     Menu'Class;
                         Text        : in     String;
                         Action      : in     Menu_Callback;
                         Underline   : in     Natural;
                         Accelerator : in     String := "") is
      Item : Gtk.Menu_Item.Gtk_Menu_Item;
   begin
      Gtk.Menu_Item.Gtk_New(Item,Text);
      Gtk.Menu.Append(Gtk.Menu.Gtk_Menu(To_Menu.My_Peer),Item);
      Gtk.Menu_Item.Show(Item);
      
      Add_Choice_Common(
         Obj         => Obj,
         To_Menu     => To_Menu,
         Text        => Text,
         Action      => Action,
         Underline   => Underline,
         Accelerator => Accelerator,
         Item        => Item);
   end Add_Choice;

   ----------------
   -- Add_Choice --
   ----------------

   procedure Add_Choice(Obj         : in out Choice;
                        To_Menu     : in     Menu'Class;
                        Text        : in     String;
                        Action      : in     Menu_Callback;
                        Underline   : in     Natural;
                        Location    : in     Natural;
                        Accelerator : in     String := "")is
      Item : Gtk.Menu_Item.Gtk_Menu_Item;
   begin
      Gtk.Menu_Item.Gtk_New(Item,Text);
      Gtk.Menu.Insert(
         Menu     => Gtk.Menu.Gtk_Menu(To_Menu.My_Peer),
         Child    => Item,
         Position => Glib.Gint(Location));
      Gtk.Menu_Item.Show(Item);

      Add_Choice_Common(
         Obj         => Obj,
         To_Menu     => To_Menu,
         Text        => Text,
         Action      => Action,
         Underline   => Underline,
         Accelerator => Accelerator,
         Item        => Item);
   end Add_Choice;

   procedure Add_Submenu_Common(
        Obj          : in out Submenu;
        Text         : in     String;
        Underline    : in     Natural;
        Parent_Menu  : in     Menu'Class;
        On_Post      : in     Menu_Callback;
        submenu_item : in     gtk.menu_item.Gtk_Menu_Item) is
      new_submenu  : gtk.menu.gtk_menu;
   begin
      gtk.menu_item.show(submenu_item);
      --Add the submenu to the label
      new_submenu := new mcc_menu_record;
      mcc_menu(new_submenu).submenu_item :=
         submenu_item;
      mcc_menu(new_submenu).accel_group :=
         Mcc_Menu_Bar(Parent_Menu.My_Peer).accel_group;
      gtk.menu.Initialize(Widget => new_submenu);
      gtk.menu_item.Set_Submenu(Menu_Item => submenu_item,
                                Submenu   => new_submenu);
                                
      --Add the accel key to the submenu

      if Parent_Menu in Window_Menu'Class then
         if Mcc_Menu_Bar(Parent_Menu.My_Peer).First_One and
            (Mcc_Menu_Bar(Parent_Menu.My_Peer).Is_Frame=False) then
            declare
               Width, Height  : Glib.Gint;
               Menubar_Height : Glib.Gint := 
                  Glib.Gint(Mcc.Gui.Window_Peer.Menubar_Height);
               use type Glib.Gint;
            begin
               -- I couldn't figure out how to find the height of the 
               -- menubar (kept getting 1), so I just set it
               Mcc_Menu_Bar(Parent_Menu.My_Peer).First_One := False;
               Gdk.Window.Get_size (
                   Window => 
                      Gtk.Window.Get_Window(Gtk.Window.Gtk_Window(
                        Gtk.Widget.Get_Parent(
                          Gtk.Widget.Gtk_Widget(
                            Mcc_Menu_Bar(Parent_Menu.My_Peer).Vbox)))),
                   Width  => Width, 
                   Height => Height);            
               Gdk.Window.Resize (
                   Window => 
                      Gtk.Window.Get_Window(Gtk.Window.Gtk_Window(
                        Gtk.Widget.Get_Parent(
                          Gtk.Widget.Gtk_Widget(
                            Mcc_Menu_Bar(Parent_Menu.My_Peer).Vbox)))),
                   Width  => Width, 
                   Height => Height+Glib.Gint(Menubar_Height));  
            end;
         end if;
         
         if (Mcc_Menu_Bar(Parent_Menu.My_Peer).Is_Frame=False 
         and Underline > 0) then
            gtk.accel_group.unlock(
               Mcc_Menu_Bar(Parent_Menu.My_Peer).accel_group);
            gtk.menu_item.Add_Accelerator(
               Widget       => submenu_item,
               Accel_Signal => "activate_item",
               Accel_Group  => Mcc_Menu_Bar(Parent_Menu.My_Peer).accel_group,
               Accel_Key    => Gdk.Types.Gdk_Key_Type(
                  Character'Pos(To_Lower(Text(Underline)))),
               Accel_Mods   => Gdk.Types.Mod1_Mask,
               Accel_Flags  => Gtk.Accel_Group.Accel_Locked);
            gtk.accel_group.lock(
               Mcc_Menu_Bar(Parent_Menu.My_Peer).accel_group);
         end if;
      end if;

      if On_Post /= null then
         Mcc.Gtk_Signals.Gtk_Signal_Void_Connect(
            submenu_item,"activate",
            Mcc.Gtk_Signals.Void_Callback(On_Post));
      end if;

      Obj.My_Peer := gtk.root_type_access(new_submenu);
   end Add_Submenu_Common;
   -----------------
   -- Add_Submenu --
   -----------------

   procedure Add_Submenu(Obj         : in out Submenu;
                         Text        : in     String;
                         Underline   : in     Natural;
                         Parent_Menu : in     Menu'Class;
                         On_Post     : in     Menu_Callback := null)is

      submenu_item : gtk.menu_item.Gtk_Menu_Item;
   begin
      gtk.menu_item.gtk_new(menu_item => submenu_item,
                            Label     => Text);
      Set_Menu_Item_Pattern(
         Submenu_Item => Submenu_Item,
         Text         => Text,
         Underline    => Underline);
      
      if Parent_Menu in Window_Menu'Class then
         gtk.menu_bar.append(
            Menu_bar  => gtk.menu_bar.gtk_menu_bar(parent_menu.my_peer),
            Child     => submenu_item);
      else
         gtk.menu.append(
            Menu   => gtk.menu.gtk_menu(parent_menu.my_peer),
            Child  => submenu_item);
      end if;

      Add_Submenu_Common(
         Obj          => Obj,
         Text         => Text,
         Underline    => Underline,
         Parent_Menu  => Parent_Menu,
         On_Post      => On_Post,
         Submenu_Item => Submenu_Item);      
   end Add_Submenu;

   -----------------
   -- Add_Submenu --
   -----------------

   procedure Add_Submenu(Obj         : in out Submenu;
                         Text        : in     String;
                         Underline   : in     Natural;
                         Parent_Menu : in     Menu'Class;
                         Location    : in     Natural;
                         On_Post     : in     Menu_Callback := null)is
      submenu_item : gtk.menu_item.Gtk_Menu_Item;
   begin
      gtk.menu_item.gtk_new(menu_item => submenu_item,
                            Label     => Text);
      Set_Menu_Item_Pattern(
         Submenu_Item => Submenu_Item,
         Text         => Text,
         Underline    => Underline);
      
      if Parent_Menu in Window_Menu'Class then
         gtk.menu_bar.insert(
            Menu_bar  => gtk.menu_bar.gtk_menu_bar(parent_menu.my_peer),
            Child     => submenu_item,
            Position  => Glib.Gint(Location));
      else
         gtk.menu.insert(
            Menu      => gtk.menu.gtk_menu(parent_menu.my_peer),
            Child     => submenu_item,
            Position  => Glib.Gint(Location));
      end if;

      Add_Submenu_Common(
         Obj          => Obj,
         Text         => Text,
         Underline    => Underline,
         Parent_Menu  => Parent_Menu,
         On_Post      => On_Post,
         Submenu_Item => Submenu_Item);      
   end Add_Submenu;

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

   procedure Create(Obj    : in out Window_Menu;
                    Window : in     Mcc.Gui.Container.Container'Class) is

      Menu_Bar : Mcc_Menu_Bar;
   begin
      Menu_Bar := new Mcc_Menu_Bar_Record;
      gtk.accel_group.Gtk_New(Widget => Menu_Bar.accel_group);
      Gtk.Menu_Bar.Initialize(Gtk.Menu_Bar.Gtk_Menu_Bar(Menu_Bar));
      
      if Window in Mcc.Gui.Container.Window.Window'Class then
         Menu_Bar.Is_Frame := False;
         gtk.box.Pack_Start(
            In_Box  => gtk.box.Gtk_Vbox(
               Gtk.Widget.Get_Parent(Gtk.Widget.Gtk_Widget(
                  Mcc.Gui.Container.Get_Peer(window)))),
            Child   => menu_bar,
            Expand  => False,
            Fill    => False);  
         Menu_Bar.Vbox := gtk.box.Gtk_Vbox(
            Gtk.Widget.Get_Parent(Gtk.Widget.Gtk_Widget(
               Mcc.Gui.Container.Get_Peer(window))));
      else
         Menu_Bar.Is_Frame := True;
         gtk.box.Pack_Start(
            In_Box  => Gtk.Box.Gtk_Vbox(
               Gtk.Fixed.Get_Parent(Gtk.Fixed.Gtk_Fixed(
                  Mcc.Gui.Container.Get_Peer(window)))),
            Child   => menu_bar,
            Expand  => False,
            Fill    => False);  
      end if;

      if Window in Mcc.Gui.Container.Window.Window'Class then
         gtk.accel_group.Attach(
            Accel_Group => Menu_Bar.accel_group,
            Object      => mcc.gui.window_peer.get_window(
               mcc.gui.container.window.window'class(window)));
         gtk.accel_group.lock(
            Menu_Bar.accel_group);
      end if;
      
      Gtk.Menu_Bar.Show(Gtk.Menu_Bar.Gtk_Menu_Bar(Menu_Bar));
      --Have Object point to menu bar Widget
      Obj.My_Peer := gtk.root_type_access(menu_bar);
      
   end Create;

   ------------
   -- Delete --
   ------------

   procedure Delete (Obj : in out Menu_Item) is
   begin
      Gtk.Widget.Destroy(Gtk.Widget.Gtk_Widget(Obj.My_Peer));
      if Menu_Item'Class(Obj) in Submenu'Class then
         Gtk.Widget.Destroy(Gtk.Widget.Gtk_Widget(
            Mcc_Menu(Obj.My_Peer).Submenu_Item));
      end if;
   end Delete;

   -------------
   -- Disable --
   -------------

   procedure Disable (Obj : in out Choice) is
   begin
      Gtk.Widget.Set_Sensitive(Gtk.Widget.Gtk_Widget(Obj.My_Peer),False);
   end Disable;

   ------------
   -- Enable --
   ------------

   procedure Enable (Obj : in out Choice) is
   begin
      Gtk.Widget.Set_Sensitive(Gtk.Widget.Gtk_Widget(Obj.My_Peer),True);
   end Enable;

   procedure Add_Separator(
      Obj         : in out Separator;
      To_Menu     : in     Menu'Class) is
      Item : Gtk.Menu_Item.Gtk_Menu_Item;
   begin
      Gtk.Menu_Item.Gtk_New(Item,"");
      
      Gtk.Menu.Append(Gtk.Menu.Gtk_Menu(To_Menu.My_Peer),Item);
      Gtk.Menu_Item.Show(Item);
      Obj.My_Peer := gtk.root_type_access(Item);
   end Add_Separator;
      
   procedure Add_Separator(
      Obj         : in out Separator;
      To_Menu     : in     Menu'Class;
      Location    : in     Natural) is
      Item : Gtk.Menu_Item.Gtk_Menu_Item;
   begin
      Gtk.Menu_Item.Gtk_New(Item,"");
      
      Gtk.Menu.Insert(
         Menu     => Gtk.Menu.Gtk_Menu(To_Menu.My_Peer),
         Child    => Item,
         Position => Glib.Gint(Location));
      Gtk.Menu_Item.Show(Item);
      Obj.My_Peer := gtk.root_type_access(Item);
   end Add_Separator;

end Mcc.Gui.Menu;

