]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/sample-header_handler.adb
ncurses 6.4 - patch 20240414
[ncurses.git] / Ada95 / samples / sample-header_handler.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                            Sample.Header_Handler                         --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright 2020 Thomas E. Dickey                                          --
11 -- Copyright 1998-2011,2014 Free Software Foundation, Inc.                  --
12 --                                                                          --
13 -- Permission is hereby granted, free of charge, to any person obtaining a  --
14 -- copy of this software and associated documentation files (the            --
15 -- "Software"), to deal in the Software without restriction, including      --
16 -- without limitation the rights to use, copy, modify, merge, publish,      --
17 -- distribute, distribute with modifications, sublicense, and/or sell       --
18 -- copies of the Software, and to permit persons to whom the Software is    --
19 -- furnished to do so, subject to the following conditions:                 --
20 --                                                                          --
21 -- The above copyright notice and this permission notice shall be included  --
22 -- in all copies or substantial portions of the Software.                   --
23 --                                                                          --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
25 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
26 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
27 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
28 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
29 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
30 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
31 --                                                                          --
32 -- Except as contained in this notice, the name(s) of the above copyright   --
33 -- holders shall not be used in advertising or otherwise to promote the     --
34 -- sale, use or other dealings in this Software without prior written       --
35 -- authorization.                                                           --
36 ------------------------------------------------------------------------------
37 --  Author:  Juergen Pfeifer, 1996
38 --  Version Control
39 --  $Revision: 1.21 $
40 --  $Date: 2020/02/02 23:34:34 $
41 --  Binding Version 01.00
42 ------------------------------------------------------------------------------
43 with Ada.Calendar; use Ada.Calendar;
44 with Terminal_Interface.Curses.Text_IO.Integer_IO;
45 with Sample.Manifest; use Sample.Manifest;
46
47 pragma Elaborate_All (Terminal_Interface.Curses.Text_Io.Integer_IO);
48
49 --  This package handles the painting of the header line of the screen.
50 --
51 package body Sample.Header_Handler is
52
53    package Int_IO is new
54      Terminal_Interface.Curses.Text_IO.Integer_IO (Integer);
55    use Int_IO;
56
57    Header_Window : Window := Null_Window;
58
59    Display_Hour  : Integer := -1; -- hour last displayed
60    Display_Min   : Integer := -1; -- minute last displayed
61    Display_Day   : Integer := -1; -- day last displayed
62    Display_Month : Integer := -1; -- month last displayed
63
64    --  This is the routine handed over to the curses library to be called
65    --  as initialization routine when ripping of the header lines from
66    --  the screen. This routine must follow C conventions.
67    function Init_Header_Window (Win     : Window;
68                                 Columns : Column_Count) return Integer;
69    pragma Convention (C, Init_Header_Window);
70
71    procedure Internal_Update_Header_Window (Do_Update : Boolean);
72
73    --  The initialization must be called before Init_Screen. It steals two
74    --  lines from the top of the screen.
75    procedure Init_Header_Handler
76    is
77    begin
78       Rip_Off_Lines (2, Init_Header_Window'Access);
79    end Init_Header_Handler;
80
81    procedure N_Out (N : Integer);
82
83    --  Emit a two digit number and ensure that a leading zero is generated if
84    --  necessary.
85    procedure N_Out (N : Integer)
86    is
87    begin
88       if N < 10 then
89          Add (Header_Window, '0');
90          Put (Header_Window, N, 1);
91       else
92          Put (Header_Window, N, 2);
93       end if;
94    end N_Out;
95
96    --  Paint the header window. The input parameter is a flag indicating
97    --  whether or not the screen should be updated physically after painting.
98    procedure Internal_Update_Header_Window (Do_Update : Boolean)
99    is
100       type Month_Name_Array is
101          array (Month_Number'First .. Month_Number'Last) of String (1 .. 9);
102
103       Month_Names : constant Month_Name_Array :=
104         ("January  ",
105          "February ",
106          "March    ",
107          "April    ",
108          "May      ",
109          "June     ",
110          "July     ",
111          "August   ",
112          "September",
113          "October  ",
114          "November ",
115          "December ");
116
117       Now    : constant Time         := Clock;
118       Sec    : constant Integer      := Integer (Seconds (Now));
119       Hour   : constant Integer      := Sec / 3600;
120       Minute : constant Integer      := (Sec - Hour * 3600) / 60;
121       Mon    : constant Month_Number := Month (Now);
122       D      : constant Day_Number   := Day (Now);
123    begin
124       if Header_Window /= Null_Window then
125          if Minute /= Display_Min
126            or else Hour /= Display_Hour
127            or else Display_Day /= D
128            or else Display_Month /= Mon
129          then
130             Move_Cursor (Header_Window, 0, 0);
131             N_Out (D); Add (Header_Window, '.');
132             Add (Header_Window, Month_Names (Mon));
133             Move_Cursor (Header_Window, 1, 0);
134             N_Out (Hour); Add (Header_Window, ':');
135             N_Out (Minute);
136             Display_Min   := Minute;
137             Display_Hour  := Hour;
138             Display_Month := Mon;
139             Display_Day   := D;
140             Refresh_Without_Update (Header_Window);
141             if Do_Update then
142                Update_Screen;
143             end if;
144          end if;
145       end if;
146    end Internal_Update_Header_Window;
147
148    --  This routine is called in the keyboard input timeout handler. So it will
149    --  periodically update the header line of the screen.
150    procedure Update_Header_Window
151    is
152    begin
153       Internal_Update_Header_Window (True);
154    end Update_Header_Window;
155
156    function Init_Header_Window (Win     : Window;
157                                 Columns : Column_Count) return Integer
158    is
159       Title  : constant String := "Ada 95 ncurses Binding Sample";
160       Pos    : Column_Position;
161    begin
162       Header_Window := Win;
163       if Win /= Null_Window then
164          if Has_Colors then
165             Set_Background (Win => Win,
166                             Ch  => (Ch    => ' ',
167                                     Color => Header_Color,
168                                     Attr  => Normal_Video));
169             Set_Character_Attributes (Win   => Win,
170                                       Attr  => Normal_Video,
171                                       Color => Header_Color);
172             Erase (Win);
173          end if;
174          Leave_Cursor_After_Update (Win, True);
175          Pos := Columns - Column_Position (Title'Length);
176          Add (Win, 0, Pos / 2, Title);
177          --  In this phase we must not allow a physical update, because
178          --  ncurses is not properly initialized at this point.
179          Internal_Update_Header_Window (False);
180          return 0;
181       else
182          return -1;
183       end if;
184    end Init_Header_Window;
185
186 end Sample.Header_Handler;