-------------------------------------------------------------------
--           RAPID - RAPID ADA PORTABLE INTERFACE DESIGNER         
--           MCC GUI PACKAGE LIBRARY                               
--           Copyright (C) 1999 Martin C. Carlisle.       
--
-- Adapted from RAPID version 1.2 suggestion by W. Blair Watkinson II
--                                                                 
-- 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 glib;
with Mcc.Gui.Widget.Peer;
with Mcc.Gtk_Signals;
with Gtk.Adjustment;
with Gtk.Object;
with Gtk.Scale;
with Gtk.Widget;
with Gdk.Types;
use type Gdk.Types.Gdk_Event_Mask;
with Gtk.Enums;
with Gdk;

package body Mcc.Gui.Widget.Scale is
   package Mcc_Object_Callback is new Mcc.Gtk_Signals.Object_Callback_Void(
      Base_Type    => Scale);

   procedure Mcc_Changed_Callback(
      Obj : access Scale) is
   begin
      if Obj.Change_Listener /= null then
         Obj.Change_Listener.all(Obj.all);
      end if;
   end Mcc_Changed_Callback;
   ------------
   -- Create --
   ------------

   procedure Create(Obj                  : in out Scale;
                    Parent               : in     Mcc.Gui.Container.Container'Class;
                    X                    : in     Integer;
                    Y                    : in     Integer;
                    Width                : in     Natural;
                    Height               : in     Natural;
                    Min                  : in     Integer;
                    Max                  : in     Integer;
                    Mark_Every           : in     Natural;
                    Orientation          : in     Scale_Orientation := Horizontal;
                    By                   : in     Natural := 1)is

      New_Scale : gtk.scale.gtk_scale;
      Scale_Adjustment : gtk.adjustment.Gtk_Adjustment;
   begin--Create
      Obj.Change_Listener := null;
      
      --From creating the widget it appears that page_size subtracts from the Upper Value
      Gtk.Adjustment.Gtk_New(Adjustment     => Scale_Adjustment,
                             Value          => 0.0,
                             Lower          => glib.Gfloat(Min),
                             Upper          => glib.Gfloat(Max),
                             Step_Increment => glib.Gfloat(By),
                             Page_Increment => glib.Gfloat(Mark_Every),
                             Page_Size      => 0.0);

      CASE Orientation IS
         WHEN Horizontal =>
            Gtk.Scale.Gtk_New_Hscale(Scale      => New_Scale,
                           Adjustment => Scale_Adjustment);
            gtk.scale.Set_Value_Pos(Scale => new_scale,
                                    Pos   => gtk.enums.Pos_Right);
         WHEN Vertical =>
            Gtk.Scale.Gtk_New_Vscale(Scale      => New_Scale,
                            Adjustment => Scale_Adjustment);
            gtk.scale.Set_Value_Pos(Scale => new_scale,
                                    Pos   => gtk.enums.Pos_Bottom);
      END CASE;
      Gtk.Scale.Set_Digits(New_Scale, 0);
      Gtk.Scale.Set_Update_Policy(
         New_Scale, Gtk.Enums.Update_Delayed);
      --Have Object point to Scale Widget
      Obj.My_Peer := gtk.root_type_access(New_Scale);
      
      Mcc.Gui.Widget.Peer.Setup(
         Obj    => Obj,
         X      => X,
         Y      => Y,
         Width  => Width,
         Height => Height,
         Parent => Parent);
   end Create;

   ---------
   -- Get --
   ---------

   function Get (Obj : in Scale) return Integer is
   begin
      return integer(
         gtk.adjustment.Get_Value(
            Adjustment => gtk.scale.Get_Adjustment(
               The_Range => gtk.scale.gtk_scale(Obj.My_Peer))));
   end Get;

   function Get_Scale_Listener(
      Obj : in Scale'Class) return Scale_Listener is
   begin
      return Obj.Change_Listener;
   end Get_Scale_Listener;
   ---------
   -- Set --
   ---------

   procedure Set(Obj      : in out Scale;
                 Location : in     Integer)is
   begin
      gtk.adjustment.Set_Value(
         Adjustment =>
            gtk.scale.Get_Adjustment(
               The_Range => gtk.scale.gtk_scale(Obj.My_Peer)),
               Value     => glib.Gfloat(location));
   end Set;

   ------------------------
   -- Set_Scale_Listener --
   ------------------------

   --have to set this, not sure what I am listening to
   procedure Set_Scale_Listener(Obj           : in Scale_Pointer;
                                Listener      : in Scale_Listener)is
      Cb_Id  : Glib.Guint;  
   begin
      if Obj.Change_Listener = null then
         Cb_Id  := Mcc_Object_Callback.Connect(
           Obj  => Gtk.Object.Gtk_Object(Obj.My_Peer), 
           Name => "value_changed",                      
           Func => Mcc_Changed_Callback'access,
           Slot_Object => Obj,
           After  => False);
      end if;      
      Obj.Change_Listener := Listener;
   end Set_Scale_Listener;

end Mcc.Gui.Widget.Scale;
