gtkada_24.0.0_80c56171/testgtk/gtkextra/create_plot_realtime.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
------------------------------------------------------------------------------
--               GtkAda - Ada95 binding for the Gimp Toolkit                --
--                                                                          --
--                     Copyright (C) 2001-2017, AdaCore                     --
--                                                                          --
-- This library 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 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with Gdk.Color;             use Gdk.Color;
with Gdk.GC;                use Gdk.GC;
with Gdk.Rectangle;         use Gdk.Rectangle;
with Glib;                  use Glib;
with Gtk.Enums;             use Gtk.Enums;
with Gtk.Extra.Plot;        use Gtk.Extra.Plot;
with Gtk.Extra.Plot_Data;   use Gtk.Extra.Plot_Data;
with Gtk.Extra.Plot_Canvas; use Gtk.Extra.Plot_Canvas;
with Gtk.Extra.Plot_Canvas.Plot; use Gtk.Extra.Plot_Canvas.Plot;
with Gtk.Extra.Plot_Canvas.Text; use Gtk.Extra.Plot_Canvas.Text;
with Gtk.Frame;             use Gtk.Frame;
with Glib.Main;             use Glib.Main;
with Gtk.Scrolled_Window;   use Gtk.Scrolled_Window;
with Gtk.Widget;            use Gtk.Widget;
with Gtkada.Handlers;       use Gtkada.Handlers;
with Unchecked_Deallocation;
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

package body Create_Plot_Realtime is

   Timer : G_Source_Id;
   Random_Generator : Ada.Numerics.Float_Random.Generator;

   type Timeout_Data is record
      Canvas  : Gtk_Plot_Canvas;
      Plot    : Gtk_Plot;
      Dataset : Gtk_Plot_Data;
      X       : Gdouble_Array_Access;
      Y       : Gdouble_Array_Access;
   end record;
   type Timeout_Data_Access is access Timeout_Data;

   package Plot_Timeout is new Glib.Main.Generic_Sources (Timeout_Data_Access);
   use Plot_Timeout;

   procedure Free is new Unchecked_Deallocation
     (Gdouble_Array, Gdouble_Array_Access);

   procedure Destroy (Widget : access Gtk_Widget_Record'Class);
   --  Called when the widget is destroyed, removes the timeout

   function Update (Data : Timeout_Data_Access) return Boolean;
   --  Update the contents of the widget periodically

   ----------
   -- Help --
   ----------

   function Help return String is
   begin
      return "This demo shows how a plot can be updated periodically to"
        & " show new data.";
   end Help;

   -------------
   -- Destroy --
   -------------

   procedure Destroy (Widget : access Gtk_Widget_Record'Class) is
      pragma Warnings (Off, Widget);
   begin
      Remove (Timer);
   end Destroy;

   ------------
   -- Update --
   ------------

   function Update (Data : Timeout_Data_Access) return Boolean is
      X, Y : Gdouble_Array_Access;
      Xmin, Xmax : Gdouble;
      Length : Natural := 1;
   begin
      if Data.X /= null then
         Length := Data.X'Length + 1;
      end if;

      X := new Gdouble_Array (1 .. Length);
      Y := new Gdouble_Array (1 .. Length);

      if Length /= 1 then
         X (1 .. Length - 1) := Data.X.all;
         Y (1 .. Length - 1) := Data.Y.all;
         X (Length) := X (Length - 1) + 1.0;
      else
         X (Length) := 1.0;
      end if;

      Y (Length) := Gdouble (Random (Random_Generator));

      Free (Data.X);
      Free (Data.Y);

      Data.X := X;
      Data.Y := Y;

      Set_Numpoints (Data.Dataset, Gint (Length));
      Set_X (Data.Dataset, X);
      Set_Y (Data.Dataset, Y);

      Get_Xrange (Data.Plot, Xmin, Xmax);
      if X (Length) > Xmax then
         Set_Xrange (Data.Plot, Xmin + 5.0, Xmax + 5.0);
         Paint (Data.Canvas);
         Queue_Draw (Data.Canvas);

      else
         --  Draw only the last point added
         Draw_Points (Data.Dataset, 1);
         Refresh (Data.Plot, Full_Area);
      end if;
      return True;
   end Update;

   ---------
   -- Run --
   ---------

   procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
      Scrollw1    : Gtk_Scrolled_Window;
      Canvas      : Gtk_Plot_Canvas;
      Color       : Gdk_Color;
      Active_Plot : Gtk_Plot;
      Dataset     : Gtk_Plot_Data;
      Plot_Child  : Gtk_Plot_Canvas_Plot;
      Text_Child  : Gtk_Plot_Canvas_Text;
   begin
      Set_Label (Frame, "gtk_plot realtime");

      --  Put the Plot in a scrolled window, in case it is too big for
      --  the screen
      Gtk_New (Scrollw1);
      Set_Border_Width (Scrollw1, 0);
      Set_Policy (Scrollw1, Policy_Always, Policy_Always);
      Add (Frame, Scrollw1);

      --  Create the canvas in which the plot will be drawn
      Gtk_New (Canvas, Gint (Get_Allocation_Width (Frame) - 10),
               Gint (Get_Allocation_Height (Frame) - 10), 1.0);
      Plot_Canvas_Unset_Flags (Canvas, Dnd_Flags);
      Add_With_Viewport (Scrollw1, Canvas);

      Widget_Callback.Connect
        (Canvas, "destroy", Widget_Callback.To_Marshaller (Destroy'Access));

      Color := Parse ("light blue");
      Alloc (Get_Default_Colormap, Color);
      Set_Background (Canvas, Color);
      Show (Canvas);

      --  Create the plot, the axis and legends
      Gtk_New (Active_Plot, 0.65, 0.45);
      Color := Parse ("light yellow");
      Alloc (Get_Default_Colormap, Color);
      Set_Background (Active_Plot, Color);

      Color := Parse ("white");
      Alloc (Get_Default_Colormap, Color);
      Set_Legends_Border (Active_Plot, Border_Shadow, 3);
      Legends_Move (Active_Plot, 0.6, 0.1);
      Set_Range (Active_Plot, 0.0, 20.0, 0.0, 1.0);

      Set_Ticks (Active_Plot, Axis_X, 2.0, 1);
      Set_Ticks (Active_Plot, Axis_Y, 0.1, 1);
      Axis_Set_Visible (Get_Axis (Active_Plot, Axis_Top), True);
      Axis_Set_Visible (Get_Axis (Active_Plot, Axis_Right), True);
      Grids_Set_Visible (Active_Plot, True, True, True, True);
      Axis_Hide_Title (Get_Axis (Active_Plot, Axis_Top));
      Axis_Hide_Title (Get_Axis (Active_Plot, Axis_Right));
      Axis_Set_Title (Get_Axis (Active_Plot, Axis_Left), "Intensity");
      Axis_Set_Title (Get_Axis (Active_Plot, Axis_Bottom), "Time");
      Grids_Set_Visible (Active_Plot, True, True, True, True);

      Gtk_New (Plot_Child, Active_Plot);
      Put_Child (Canvas, Plot_Child, 0.15, 0.15, 0.9, 0.9);
      Show (Active_Plot);

      Gtk_New (Text_Child,
               Font          => "Times-BoldItalic",
               Height        => 10,
               Text          => "Real Time Demo");
      Put_Child (Canvas, Text_Child, 0.45, 0.05, 0.85, 0.9);

      --  Create the data

      Gtk_New (Dataset);
      Add_Data (Active_Plot, Dataset);
      Show (Dataset);

      Color := Parse ("red");
      Alloc (Get_Default_Colormap, Color);

      Set_Legend (Dataset, "Random Pulse");
      Set_Symbol (Dataset,
                  Symbol_Diamond,
                  Symbol_Opaque,
                  10, 2.0, Color, Color);
      Set_Line_Attributes
        (Dataset, Line_Solid, Cap_Not_Last, Join_Miter, 1.0, Color);

      --  Paint everything
      Paint (Canvas);
      Refresh (Canvas);

      --  Register the periodic timer

      Timer := Plot_Timeout.Timeout_Add
        (300,
         Update'Access,
         new Timeout_Data'(Canvas  => Canvas,
                           Plot    => Active_Plot,
                           Dataset => Dataset,
                           X       => null,
                           Y       => null));
      Show_All (Frame);
   end Run;

end Create_Plot_Realtime;