sample-header_handler.adb 7.7 KB

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