1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. private with Actions; 
  10. private with Ada.Containers.Indefinite_Vectors; 
  11. private with Ada.Real_Time; 
  12.  
  13. pragma Elaborate_All( Actions ); 
  14.  
  15. package Widgets.Menu_Enumerations is 
  16.  
  17.     -- Represents a widget action involving an Menu_Enumeration such as a 
  18.     -- selection changed event. 
  19.     type Enum_Action is new Action with private; 
  20.     type A_Enum_Action is access all Enum_Action'Class; 
  21.  
  22.     function Get_Index( this : not null access Enum_Action'Class ) return Natural; 
  23.  
  24.     function Get_Text( this : not null access Enum_Action'Class ) return String; 
  25.  
  26.     Changed : constant Action_Id;             -- the selection changed 
  27.  
  28.     -- This is the interface to implement in order to listen to enum actions. 
  29.     -- When an enum action occurs, the listener will be notified of the type of 
  30.     -- action performed and the Menu_Enumeration box that performed the action. 
  31.     type Enum_Listener is limited interface and Action_Listener; 
  32.     type A_Enum_Listener is access all Enum_Listener'Class; 
  33.  
  34.     -- Handles the enum action 'action'. 
  35.     procedure Handle_Action( this   : access Enum_Listener; 
  36.                              action : A_Enum_Action ) is abstract; 
  37.  
  38.     ---------------------------------------------------------------------------- 
  39.  
  40.     -- A Menu_Enumeration for Keen is a simple select-enumeration widget 
  41.     -- designed to match the look and feel of the options in the menus, with an 
  42.     -- animated glowing dot next to the focus menu item. 
  43.     type Menu_Enumeration is new Widget and Animated with private; 
  44.     type A_Menu_Enumeration is access all Menu_Enumeration'Class; 
  45.  
  46.     -- Creates a new menu enumeration. 
  47.     function Create_Menu_Enum( view : not null access Game_Views.Game_View'Class; 
  48.                                id   : String ) return A_Menu_Enumeration; 
  49.     pragma Precondition( id'Length > 0 ); 
  50.     pragma Postcondition( Create_Menu_Enum'Result /= null ); 
  51.  
  52.     procedure Add_Listener( this     : access Menu_Enumeration; 
  53.                             listener : not null A_Enum_Listener ); 
  54.  
  55.     procedure Add_Option( this : not null access Menu_Enumeration'Class; 
  56.                           text : String ); 
  57.  
  58.     function Get_Index( this : not null access Menu_Enumeration'Class ) return Natural; 
  59.  
  60.     function Get_Text( this : not null access Menu_Enumeration'Class ) return String; 
  61.  
  62.     -- If the index does not exist, nothing will happen. Set 'fireAction' to 
  63.     -- True to dispatch a Changed action if the index changes. 
  64.     procedure Set_Index( this       : not null access Menu_Enumeration'Class; 
  65.                          index      : Positive; 
  66.                          fireAction : Boolean := True ); 
  67.  
  68. private 
  69.  
  70.     use Ada.Real_Time; 
  71.  
  72.     type Enum_Action is new Action with 
  73.         record 
  74.             index : Natural := 0; 
  75.             text  : Unbounded_String; 
  76.         end record; 
  77.  
  78.     procedure Construct( this   : access Enum_Action; 
  79.                          id     : Action_Id; 
  80.                          source : not null A_Widget; 
  81.                          index  : Natural; 
  82.                          text   : String ); 
  83.  
  84.     Changed : constant Action_Id := Actions.To_Action_Id( "enum.changed" ); 
  85.  
  86.     ---------------------------------------------------------------------------- 
  87.  
  88.     package String_Vectors is new Ada.Containers.Indefinite_Vectors( Positive, String, "=" ); 
  89.  
  90.     type Menu_Enumeration is new Widget and Animated with 
  91.         record 
  92.             focusTime : Time_Span;      -- last Tick upTime before getting focus 
  93.             icon      : Natural; 
  94.             options   : String_Vectors.Vector; 
  95.             index     : Natural := 0; 
  96.         end record; 
  97.  
  98.     procedure Construct( this : access Menu_Enumeration; 
  99.                          view : not null access Game_Views.Game_View'Class; 
  100.                          id   : String ); 
  101.     pragma Precondition( id'Length > 0 ); 
  102.  
  103.     procedure Draw_Content( this : access Menu_Enumeration ); 
  104.  
  105.     function Get_Min_Height( this : access Menu_Enumeration ) return Natural; 
  106.  
  107.     function Get_Min_Width( this : access Menu_Enumeration ) return Natural; 
  108.  
  109.     procedure On_Blur( this : access Menu_Enumeration ); 
  110.  
  111.     procedure On_Disabled( this : access Menu_Enumeration ); 
  112.  
  113.     procedure On_Enabled( this : access Menu_Enumeration ); 
  114.  
  115.     procedure On_Focus( this : access Menu_Enumeration ); 
  116.  
  117.     procedure On_Key_Press( this    : access Menu_Enumeration; 
  118.                             evt     : not null A_Key_Event; 
  119.                             handled : in out Boolean ); 
  120.  
  121.     procedure Tick( this : access Menu_Enumeration; time : Tick_Time ); 
  122.  
  123. end Widgets.Menu_Enumerations;