1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
5 -- Sample.Header_Handler --
9 ------------------------------------------------------------------------------
10 -- Copyright 2020 Thomas E. Dickey --
11 -- Copyright 1998-2011,2014 Free Software Foundation, Inc. --
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: --
21 -- The above copyright notice and this permission notice shall be included --
22 -- in all copies or substantial portions of the Software. --
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. --
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 --
36 ------------------------------------------------------------------------------
37 -- Author: Juergen Pfeifer, 1996
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;
47 pragma Elaborate_All (Terminal_Interface.Curses.Text_Io.Integer_IO);
49 -- This package handles the painting of the header line of the screen.
51 package body Sample.Header_Handler is
54 Terminal_Interface.Curses.Text_IO.Integer_IO (Integer);
57 Header_Window : Window := Null_Window;
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
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);
71 procedure Internal_Update_Header_Window (Do_Update : Boolean);
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
78 Rip_Off_Lines (2, Init_Header_Window'Access);
79 end Init_Header_Handler;
81 procedure N_Out (N : Integer);
83 -- Emit a two digit number and ensure that a leading zero is generated if
85 procedure N_Out (N : Integer)
89 Add (Header_Window, '0');
90 Put (Header_Window, N, 1);
92 Put (Header_Window, N, 2);
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)
100 type Month_Name_Array is
101 array (Month_Number'First .. Month_Number'Last) of String (1 .. 9);
103 Month_Names : constant Month_Name_Array :=
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);
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
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, ':');
136 Display_Min := Minute;
137 Display_Hour := Hour;
138 Display_Month := Mon;
140 Refresh_Without_Update (Header_Window);
146 end Internal_Update_Header_Window;
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
153 Internal_Update_Header_Window (True);
154 end Update_Header_Window;
156 function Init_Header_Window (Win : Window;
157 Columns : Column_Count) return Integer
159 Title : constant String := "Ada 95 ncurses Binding Sample";
160 Pos : Column_Position;
162 Header_Window := Win;
163 if Win /= Null_Window then
165 Set_Background (Win => Win,
167 Color => Header_Color,
168 Attr => Normal_Video));
169 Set_Character_Attributes (Win => Win,
170 Attr => Normal_Video,
171 Color => Header_Color);
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);
184 end Init_Header_Window;
186 end Sample.Header_Handler;