[gtkada] Problem displaying Time data in GtkAda

Kevin Knepshield kevknep at home.com
Mon Nov 13 06:31:17 CET 2000


Hi all,

    I was having a problem and was hoping someone might have some insight
that could help.  I am using GtkAda 1.3.8 to make a program that serves to
run a race.  At one point I need to have a display showing the elapsed time.
To test my functions, I have set it up so that each time a button is
pressed, a callback is used to get the current time and display it to a text
box.  I used a command line program to test this and everything worked fine.
When I inserted the code into my program so that the output was now written
to a text box(no attemp to format, just to get an updated time displayed), I
got "weird" results.  First off, I got the time to display correctly, but
the time would only update about every 20 seconds, and at the same
intervals.  I can keep clicking the button, and the time stays the same, and
then after about 20 seconds, it will update to the new time, and stay at
that for the next 20 seconds.  I had bounded the data for seconds using a
subtype (subtype Seconds_Range is Float range 0.00 .. 59.99), but right
before the time clicks over to a whole seconds, the value of 61.43 is
displayed for seconds, which should cause a constraint error.  The code for
what I have is listed below, it is somewhat long , but I didn't want to
leave anything out,  I put the types from a separate package here.  Sorry so
long, any help would be appreciated.  The only reason I posted this here was
that the code worked fine from a command line, but started to display
strange behaviour when used in GtkAda.

-- The Time_Rec data structure

type Hour_Range is Integer range 0..23;
type Minute_Range is Integer range 0..59;
type Second_Range is Float range 0.00 .. 59.99;

Type Time_Rec is record
    Hours : Hour_Range;
    Minutes : Minute_Range;
    Seconds : Second_Range;
end record;


---The callback that makes the procedure call
   -------------------------------
   -- On_Btn_Start_Race_Clicked --
   -------------------------------
   procedure On_Btn_Start_Race_Clicked
     (Object : access Gtk_Button_Record'Class)
   is
   begin
    Display_Elapsed_Time(wnd_Time_Race.Txt_Elapsed_Time);
   end On_Btn_Start_Race_Clicked;


-- ***********************************************************
--- The package where the function calls are --
----------------------------------------------------------------------------
-- < TABSTOP=4 >
-- Package: Timepkg;
-- Author: D. Kevin Knepshield
-- Purpose: Provide timing functionality for race_runner program
----------------------------------------------------------------------------
with Glib;    use Glib;
with Gtk;    use Gtk;
with Gdk.Types;         use Gdk.Types;
with Gtk.Widget;        use Gtk.Widget;
with Gtk.Enums;         use Gtk.Enums;
with Gtkada.Handlers;   use Gtkada.Handlers;
with Callbacks_Race_Runner;  use Callbacks_Race_Runner;
with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Racepkg;   use Racepkg;
with Gtk.Editable;  use Gtk.Editable;
with Gtk.GEntry;  use Gtk.GEntry;
with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO;  use Ada.Float_Text_IO;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Calendar;  use Ada.Calendar;
with Gtkada.Dialogs;  use Gtkada.Dialogs;
with Gtk.Clist;   use Gtk.Clist;
with Gtkada.Types; use Gtkada.Types;
---------------------------------------------
---  with'ing each screen that might be used
---------------------------------------------
with wnd_Time_Race_Pkg;  use wnd_Time_Race_Pkg;
with wnd_Timing_Data_Pkg; use wnd_Timing_Data_Pkg;
with wnd_Results_Pkg;  use wnd_Results_Pkg;

package body Timepkg is

----------------------------------------------------------------------------
-- Funcion: Make_Time_Rec
-- Author: D.Kevin Knepshield
-- Purpose: Returns a Time_Rec from the total seconds of a time value
----------------------------------------------------------------------------
function Make_Time_Rec (Tot_Seconds : Float) return Time_Rec is
     Time : Time_Rec;
     Hours : Hour_Range;
     Minutes : Minute_Range;
     Secs : Second_Range;
begin
     Hours := Integer(Float'Truncation(Tot_Seconds / 3600.0));
     Minutes := Integer(Float'Truncation((Tot_Seconds -
      Float(Hours * 3600)) / 60.0 ));

 --Minutes := Integer(Float'Truncaton((Tot_Seconds -
 --  Float(Hours * 3600)) / 60.0));

 Secs := Tot_Seconds - Float((Minutes * 60) + (Hours * 3600));

 Time := (Hours, Minutes, Secs);
 return Time;
end Make_Time_Rec;


----------------------------------------------------------------------------
-
-- Function: Get_Current_Time
-- Author :  D. Kevin Knepshield
-- Purpose: This function returns the current TIME SINCE MIDNIGHT.  Since
--  the times for a race a based on the difference between the
--  start time and current time, this won't matter.  The Time_Diff
--  funcion will correct for any time that crosses midnight.
----------------------------------------------------------------------------
--
function Get_Current_Time return Time_Rec is
     T  ot_Seconds : Float;
     Now : Ada.Calendar.Time;
begin
     Now := Ada.Calendar.Clock;

     Tot_Seconds := Float(Seconds(Now));

 return Make_Time_Rec(Tot_Seconds);
end Get_Current_Time;


----------------------------------------------------------------------------
-- Function: Calc_Tot_Sec
-- Author: D. Kevin Knepshield
-- Purpose: Return the total time associated with a time rec.
----------------------------------------------------------------------------
function Calc_Tot_Sec(Time : Time_Rec) return Float is
begin
 r    eturn  Float((Time.Hours * 3600) + (Time.Minutes * 60)) +
Time.Seconds;
end Calc_Tot_Sec;

----------------------------------------------------------------------------
-
-- Procedure:  Time_Diff
-- Author: D. Kevin Knepshield
-- Purpose: Returns the difference between two times.  Time2 must be
--  greater than Time1, if not (the timer has crossed midnight)
--  then 86,400 (the number of seconds in one day) will be added
--  to time one to get an accurate time.  "Time" in this sense
--  is the number of seconds since midnight of the day the race
--  started.
----------------------------------------------------------------------------
--
function Time_Diff( Time1, Time2 : Time_Rec) return Time_Rec is
     Tot1 : Float;
     Tot2 : Float;
begin
     Tot1 := Calc_Tot_Sec(Time1);
     Tot2 := Calc_Tot_Sec(Time2);

 -- compensate just in case the timing crosses midnight.  NOTE:  Bad
 -- results will happen if the call is made and the parameters are
 -- swapped.
 if Tot1 > Tot2 then
      Tot2 := Tot2 + 86400.0;
 end if;

 return Make_Time_Rec(Tot2 - Tot1);
end Time_Diff;

----------------------------------------------------------------------------
-
-- Procedure: Split_Time_REc
-- Author:  D. Kevin Knepshield
-- Purpose:  return the values for each field in a Time_Rec
----------------------------------------------------------------------------
-
procedure Split_Time_Rec ( aTime : Time_Rec;
       Hours : out Hour_Range;
       Mins : out Minute_Range;
       Secs : out Second_Range) is
begin
     Hours := aTime.Hours;
     Mins := aTime.Minutes;
     Secs := aTime.Seconds;
end Split_Time_Rec;

----------------------------------------------------------
-- Function: Time_Mins
-- Author:  D. Kevin Knepshield
-- Purpose:  Return the minute portion of a time
----------------------------------------------------------
function Time_Mins (aTime : Time_Rec) return Minute_Range is
begin
 return aTime.Minutes;
end Time_Mins;

-----------------------------------------------------------
-- Function: Time_Hours
-- Author:  D.Kevin Knepshield
-- Purpose:  Return the hours portion of a time rec
-----------------------------------------------------------
function Time_Hours (aTime : Time_Rec) return Hour_Range is
begin
 return aTime.Hours;
end Time_Hours;

-----------------------------------------------------------
-- Function: Time_Seconds
-- Author:  D. Kevin Knepshield
-- Purpose:  Return the seconds value of a time_rec
-----------------------------------------------------------
function Time_Secs (aTime : Time_Rec) return Second_Range is
begin
 return aTime.Seconds;
end Time_Secs;

------------------------------------------------------------------
-- Function: Make_Time_String
-- Author:  D. Kevin Knepshield
-- Purpose:  return a string that represents a finishing time in
--    HH:MM:SS.SS format
-------------------------------------------------------------------
function Make_Time_String (Time : Time_Rec) return String is
 -- Always make it a constant length string
 Time_String : String(1..11);
 Hrs : Hour_Range;
 Mins : Minute_Range;
 Secs : Second_Range;
begin
 Split_Time_Rec (Time, Hrs, Mins, Secs);

 if Hrs >= 10 then
  Put(Time_String(1..2), Hrs);--, width => 1);
 else
  Time_String(1) := '0';
  Put(Time_String(2..2), Hrs); --, width => 1);
 end if;

 Time_String(3) := ':';

 if Mins >= 10 then
  PUt(Time_String(4..5), Mins);
 else
  Time_String(4) := '0';
  Put(Time_String(5..5), Mins);
 end if;

 Time_String(6) := ':';

 if Secs >= 10.0 then
  Put(Time_String(7..11), Secs, aft =>2, exp => 0);
 else
  Time_String(7) := '0';
  Put(Time_String(8..11), Secs, aft => 2, exp => 0);
 end if;

 return Time_String;
end Make_Time_String;

 end Timepkg;

:





More information about the gtkada mailing list