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