tash_8.7.2_4c588c12/demos/timer_app.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
------------------------------------------------
--
--  timer_app.adb -- This program demonstrates how the TASH Ada/Tk interface
--                    provides Tk features for use in an Ada program.
--
--  Copyright (c) 2017-2022 Simon Wright <simon@pushface.org>
--  Copyright (c) 1997 Terry J. Westley
--
--  See the file "license.htm" for information on usage and
--  redistribution of this file, and  a DISCLAIMER OF ALL WARRANTIES.
--
--  This program was adapted from the demo program distributed with Tk.
--  It provides a simple stop watch timer facility.
--
------------------------------------------------

--  This package provides the Init function required for the demo
--  program Timer.

with CArgv;
with Tcl.Ada;
with Tcl.Tk;
with Tcl.Tk.Ada;

package body Timer_App is

   use Tcl;

   type Timer_Type is delta 0.01 digits 8 range 0.0 .. 999999.99;

   Counter      : Tcl.Tk.Ada.Label;
   Start_Button : Tcl.Tk.Ada.Button;
   Stop_Button  : Tcl.Tk.Ada.Button;
   Time_Value   : Timer_Type := 0.0;
   Stopped      : Boolean    := True;

   --  Update the window by displaying the current value of the timer.
   -------------------------------------------------------------------
   procedure Update;
   procedure Update is
   begin --  Update
      Tcl.Tk.Ada.configure
        (Counter,
         "-text " & Timer_Type'Image (Time_Value));
   end Update;

   --  Increment the timer by one "tick."  A tick is 50 milliseconds
   --  (or 5 hundredths of a second).
   -----------------------------------------------------------------
   procedure Tick;
   procedure Tick is
   begin --  Tick

      --  if the timer is stopped, do not increment
      --  its value or reschedule tick for future execution.
      ------------------------------------------------------
      if Stopped then
         return;
      end if;

      --  Schedule tick to be called again in 50 milliseconds.
      --------------------------------------------------------
      Tcl.Tk.Ada.After (50, "tick");

      --  Increment the timer value
      ------------------------------
      Time_Value := Time_Value + 0.05;

      --  Update the timer display.
      ----------------------------
      Update;

   end Tick;

   function Tick_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int;
   pragma Convention (C, Tick_Command);

   --  Declare a procedure, suitable for creating a Tcl command,
   --  which will increment the timer.
   -------------------------------------------------------------
   function Tick_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int
   is
      pragma Unreferenced (ClientData, Interp, Argc, Argv);
   begin --  Tick_Command
      Tick;
      return Tcl.TCL_OK;
   end Tick_Command;

   function Start_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int;
   pragma Convention (C, Start_Command);

   --  Declare a procedure, suitable for creating a Tcl command,
   --  which will start the timer if it is currently stopped.  Also,
   --  change the Stop button (currently labeled "Reset") to display "Stop."
   -------------------------------------------------------------------------
   function Start_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int
   is
      pragma Unreferenced (ClientData, Interp, Argc, Argv);
   begin --  Start_Command
      if Stopped then
         Stopped := False;
         Tcl.Tk.Ada.configure (Stop_Button, "-text Stop -command Stop");
         Tick;
         Tcl.Tk.Ada.Set_Trace (False);
      end if;
      return Tcl.TCL_OK;
   end Start_Command;

   function Stop_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int;
   pragma Convention (C, Stop_Command);

   --  Declare a procedure, suitable for creating a Tcl command,
   --  which will stop incrementing the timer.  Also, relabel the
   --  Stop button to be a Reset button.
   --------------------------------------------------------------
   function Stop_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int
   is
      pragma Unreferenced (ClientData, Interp, Argc, Argv);
   begin --  Stop_Command
      Tcl.Tk.Ada.Set_Trace (True);
      Stopped := True;
      Tcl.Tk.Ada.configure (Stop_Button, "-text Reset -command Reset");
      return Tcl.TCL_OK;
   end Stop_Command;

   --  Reset the timer's value to 0.0 and update the display.
   ----------------------------------------------------------
   procedure Reset;
   procedure Reset is
   begin --  Reset
      Time_Value := 0.0;
      Stopped    := True;
      Update;
   end Reset;

   function Reset_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int;
   pragma Convention (C, Reset_Command);

   --  Declare a procedure, suitable for creating a Tcl command,
   --  which will reset the timer to 0.0 and update the display.
   -------------------------------------------------------------
   function Reset_Command
     (ClientData : Integer;
      Interp     : Tcl.Tcl_Interp;
      Argc       : Interfaces.C.int;
      Argv       : CArgv.Chars_Ptr_Ptr)
      return       Interfaces.C.int
   is
      pragma Unreferenced (ClientData, Interp, Argc, Argv);
   begin --  Reset_Command
      Reset;
      return Tcl.TCL_OK;
   end Reset_Command;

   function Init (Interp : Tcl.Tcl_Interp)
                 return Interfaces.C.int
   is
      package CreateCommands is new Tcl.Ada.Generic_Command (Integer);
      Command      : Tcl.Tcl_Command;
      pragma Unreferenced (Command);
      use type Interfaces.C.int;
   begin
      Tcl.Tk.Ada.Set_Trace (True);

      --  Initialize Tcl
      if Tcl.Tcl_Init (Interp) = Tcl.TCL_ERROR then
         return Tcl.TCL_ERROR;
      end if;

      --  Initialize Tk
      if Tcl.Tk.Tk_Init (Interp) = Tcl.TCL_ERROR then
         return Tcl.TCL_ERROR;
      end if;

      --  Create several new Tcl commands to call Ada subprograms.
      Command :=
        CreateCommands.Tcl_CreateCommand
          (Interp,
           "tick",
           Tick_Command'Access,
           0,
           null);
      Command :=
        CreateCommands.Tcl_CreateCommand
          (Interp,
           "Start",
           Start_Command'Access,
           0,
           null);
      Command :=
        CreateCommands.Tcl_CreateCommand
          (Interp,
           "Stop",
           Stop_Command'Access,
           0,
           null);
      Command :=
        CreateCommands.Tcl_CreateCommand
          (Interp,
           "Reset",
           Reset_Command'Access,
           0,
           null);

      --  Set the Tk context so that we may use shortcut Tk calls
      --  that require reference to the interpreter.
      Tcl.Tk.Ada.Set_Context (Interp);

      --  Create and pack the counter text widget
      Counter :=
        Tcl.Tk.Ada.Create (".counter", "-text 0.00 -relief raised -width 10");
      Tcl.Tk.Ada.Pack (Counter, "-side bottom -fill both");

      --  Create and pack the Start button
      Start_Button := Tcl.Tk.Ada.Create (".start",
                                         "-text Start -command Start");
      Tcl.Tk.Ada.Pack (Start_Button, "-side left -fill both -expand yes");

      --  Create and pack the Stop button
   -----------------------------------
      Stop_Button := Tcl.Tk.Ada.Create (".stop", "-text Reset -command Reset");
      Tcl.Tk.Ada.Pack (Stop_Button, "-side left -fill both -expand yes");

      --  Bind ^C and ^Q keys to exit
   -------------------------------
      Tcl.Tk.Ada.Bind_To_Main_Window (Interp,
                                      "<Control-c>",
                                      "{destroy .;exit}");
      Tcl.Tk.Ada.Bind_To_Main_Window (Interp,
                                      "<Control-q>",
                                      "{destroy .;exit}");

      --  Reset timer value to 0.0
   ----------------------------
      Reset;

      return Tcl.TCL_OK;

   end Init;

end Timer_App;