]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/sample-header_handler.adb
b187f9763874687978304ff137d53c6167749033
[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,2004 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.14 $
39 --  $Date: 2004/08/21 21:37:00 $
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 --  This package handles the painting of the header line of the screen.
47 --
48 package body Sample.Header_Handler is
49
50    package Int_IO is new
51      Terminal_Interface.Curses.Text_IO.Integer_IO (Integer);
52    use Int_IO;
53
54    Header_Window : Window := Null_Window;
55
56    Display_Hour  : Integer := -1; -- hour last displayed
57    Display_Min   : Integer := -1; -- minute last displayed
58    Display_Day   : Integer := -1; -- day last displayed
59    Display_Month : Integer := -1; -- month last displayed
60
61    --  This is the routine handed over to the curses library to be called
62    --  as initialization routine when ripping of the header lines from
63    --  the screen. This routine must follow C conventions.
64    function Init_Header_Window (Win     : Window;
65                                 Columns : Column_Count) return Integer;
66    pragma Convention (C, Init_Header_Window);
67
68    procedure Internal_Update_Header_Window (Do_Update : in Boolean);
69
70
71    --  The initialization must be called before Init_Screen. It steals two
72    --  lines from the top of the screen.
73    procedure Init_Header_Handler
74    is
75    begin
76       Rip_Off_Lines (2, Init_Header_Window'Access);
77    end Init_Header_Handler;
78
79    procedure N_Out (N : in Integer);
80
81    --  Emit a two digit number and ensure that a leading zero is generated if
82    --  necessary.
83    procedure N_Out (N : in Integer)
84    is
85    begin
86       if N < 10 then
87          Add (Header_Window, '0');
88          Put (Header_Window, N, 1);
89       else
90          Put (Header_Window, N, 2);
91       end if;
92    end N_Out;
93
94    --  Paint the header window. The input parameter is a flag indicating
95    --  whether or not the screen should be updated physically after painting.
96    procedure Internal_Update_Header_Window (Do_Update : in Boolean)
97    is
98       type Month_Name_Array is
99          array (Month_Number'First .. Month_Number'Last) of String (1 .. 9);
100
101       Month_Names : constant Month_Name_Array :=
102         ("January  ",
103          "February ",
104          "March    ",
105          "April    ",
106          "May      ",
107          "June     ",
108          "July     ",
109          "August   ",
110          "September",
111          "October  ",
112          "November ",
113          "December ");
114
115       Now    : constant Time         := Clock;
116       Sec    : constant Integer      := Integer (Seconds (Now));
117       Hour   : constant Integer      := Sec / 3600;
118       Minute : constant Integer      := (Sec - Hour * 3600) / 60;
119       Mon    : constant Month_Number := Month (Now);
120       D      : constant Day_Number   := Day (Now);
121    begin
122       if Header_Window /= Null_Window then
123          if Minute /= Display_Min or else Hour /= Display_Hour
124            or else Display_Day /= D or else Display_Month /= Mon then
125             Move_Cursor (Header_Window, 0, 0);
126             N_Out (D); Add (Header_Window, '.');
127             Add (Header_Window, Month_Names (Mon));
128             Move_Cursor (Header_Window, 1, 0);
129             N_Out (Hour); Add (Header_Window, ':');
130             N_Out (Minute);
131             Display_Min   := Minute;
132             Display_Hour  := Hour;
133             Display_Month := Mon;
134             Display_Day   := D;
135             Refresh_Without_Update (Header_Window);
136             if Do_Update then
137                Update_Screen;
138             end if;
139          end if;
140       end if;
141    end Internal_Update_Header_Window;
142
143    --  This routine is called in the keyboard input timeout handler. So it will
144    --  periodically update the header line of the screen.
145    procedure Update_Header_Window
146    is
147    begin
148       Internal_Update_Header_Window (True);
149    end Update_Header_Window;
150
151    function Init_Header_Window (Win     : Window;
152                                 Columns : Column_Count) return Integer
153    is
154       Title  : constant String := "Ada 95 ncurses Binding Sample";
155       Pos    : Column_Position;
156    begin
157       Header_Window := Win;
158       if Win /= Null_Window then
159          if Has_Colors then
160             Set_Background (Win => Win,
161                             Ch  => (Ch    => ' ',
162                                     Color => Header_Color,
163                                     Attr  => Normal_Video));
164             Set_Character_Attributes (Win   => Win,
165                                       Attr  => Normal_Video,
166                                       Color => Header_Color);
167             Erase (Win);
168          end if;
169          Leave_Cursor_After_Update (Win, True);
170          Pos := Columns - Column_Position (Title'Length);
171          Add (Win, 0, Pos / 2, Title);
172          --  In this phase we must not allow a physical update, because
173          --  ncurses isnĀ“t properly initialized at this point.
174          Internal_Update_Header_Window (False);
175          return 0;
176       else
177          return -1;
178       end if;
179    end Init_Header_Window;
180
181 end Sample.Header_Handler;