ncurses2-attr_test.adb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- ncurses --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 2000-2007,2008 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: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
  37. -- Version Control
  38. -- $Revision: 1.9 $
  39. -- $Date: 2008/07/26 18:47:26 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with ncurses2.util; use ncurses2.util;
  43. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  44. with Terminal_Interface.Curses.Terminfo;
  45. use Terminal_Interface.Curses.Terminfo;
  46. with Ada.Characters.Handling;
  47. with Ada.Strings.Fixed;
  48. procedure ncurses2.attr_test is
  49. function subset (super, sub : Character_Attribute_Set) return Boolean;
  50. function intersect (b, a : Character_Attribute_Set) return Boolean;
  51. function has_A_COLOR (attr : Attributed_Character) return Boolean;
  52. function show_attr (row : Line_Position;
  53. skip : Natural;
  54. attr : Character_Attribute_Set;
  55. name : String;
  56. once : Boolean) return Line_Position;
  57. procedure attr_getc (skip : in out Integer;
  58. fg, bg : in out Color_Number;
  59. result : out Boolean);
  60. function subset (super, sub : Character_Attribute_Set) return Boolean is
  61. begin
  62. if
  63. (super.Stand_Out or not sub.Stand_Out) and
  64. (super.Under_Line or not sub.Under_Line) and
  65. (super.Reverse_Video or not sub.Reverse_Video) and
  66. (super.Blink or not sub.Blink) and
  67. (super.Dim_Character or not sub.Dim_Character) and
  68. (super.Bold_Character or not sub.Bold_Character) and
  69. (super.Alternate_Character_Set or not sub.Alternate_Character_Set) and
  70. (super.Invisible_Character or not sub.Invisible_Character) -- and
  71. -- (super.Protected_Character or not sub.Protected_Character) and
  72. -- (super.Horizontal or not sub.Horizontal) and
  73. -- (super.Left or not sub.Left) and
  74. -- (super.Low or not sub.Low) and
  75. -- (super.Right or not sub.Right) and
  76. -- (super.Top or not sub.Top) and
  77. -- (super.Vertical or not sub.Vertical)
  78. then
  79. return True;
  80. else
  81. return False;
  82. end if;
  83. end subset;
  84. function intersect (b, a : Character_Attribute_Set) return Boolean is
  85. begin
  86. if
  87. (a.Stand_Out and b.Stand_Out) or
  88. (a.Under_Line and b.Under_Line) or
  89. (a.Reverse_Video and b.Reverse_Video) or
  90. (a.Blink and b.Blink) or
  91. (a.Dim_Character and b.Dim_Character) or
  92. (a.Bold_Character and b.Bold_Character) or
  93. (a.Alternate_Character_Set and b.Alternate_Character_Set) or
  94. (a.Invisible_Character and b.Invisible_Character) -- or
  95. -- (a.Protected_Character and b.Protected_Character) or
  96. -- (a.Horizontal and b.Horizontal) or
  97. -- (a.Left and b.Left) or
  98. -- (a.Low and b.Low) or
  99. -- (a.Right and b.Right) or
  100. -- (a.Top and b.Top) or
  101. -- (a.Vertical and b.Vertical)
  102. then
  103. return True;
  104. else
  105. return False;
  106. end if;
  107. end intersect;
  108. function has_A_COLOR (attr : Attributed_Character) return Boolean is
  109. begin
  110. if attr.Color /= Color_Pair (0) then
  111. return True;
  112. else
  113. return False;
  114. end if;
  115. end has_A_COLOR;
  116. -- Print some text with attributes.
  117. function show_attr (row : Line_Position;
  118. skip : Natural;
  119. attr : Character_Attribute_Set;
  120. name : String;
  121. once : Boolean) return Line_Position is
  122. function make_record (n : Integer) return Character_Attribute_Set;
  123. function make_record (n : Integer) return Character_Attribute_Set is
  124. -- unsupported means true
  125. a : Character_Attribute_Set := (others => False);
  126. m : Integer;
  127. rest : Integer;
  128. begin
  129. -- ncv is a bitmap with these fields
  130. -- A_STANDOUT,
  131. -- A_UNDERLINE,
  132. -- A_REVERSE,
  133. -- A_BLINK,
  134. -- A_DIM,
  135. -- A_BOLD,
  136. -- A_INVIS,
  137. -- A_PROTECT,
  138. -- A_ALTCHARSET
  139. -- It means no_color_video,
  140. -- video attributes that can't be used with colors
  141. -- see man terminfo.5
  142. m := n mod 2;
  143. rest := n / 2;
  144. if 1 = m then
  145. a.Stand_Out := True;
  146. end if;
  147. m := rest mod 2;
  148. rest := rest / 2;
  149. if 1 = m then
  150. a.Under_Line := True;
  151. end if;
  152. m := rest mod 2;
  153. rest := rest / 2;
  154. if 1 = m then
  155. a.Reverse_Video := True;
  156. end if;
  157. m := rest mod 2;
  158. rest := rest / 2;
  159. if 1 = m then
  160. a.Blink := True;
  161. end if;
  162. m := rest mod 2;
  163. rest := rest / 2;
  164. if 1 = m then
  165. a.Bold_Character := True;
  166. end if;
  167. m := rest mod 2;
  168. rest := rest / 2;
  169. if 1 = m then
  170. a.Invisible_Character := True;
  171. end if;
  172. m := rest mod 2;
  173. rest := rest / 2;
  174. if 1 = m then
  175. a.Protected_Character := True;
  176. end if;
  177. m := rest mod 2;
  178. rest := rest / 2;
  179. if 1 = m then
  180. a.Alternate_Character_Set := True;
  181. end if;
  182. return a;
  183. end make_record;
  184. ncv : constant Integer := Get_Number ("ncv");
  185. begin
  186. Move_Cursor (Line => row, Column => 8);
  187. Add (Str => name & " mode:");
  188. Move_Cursor (Line => row, Column => 24);
  189. Add (Ch => '|');
  190. if skip /= 0 then
  191. -- printw("%*s", skip, " ")
  192. Add (Str => Ada.Strings.Fixed."*" (skip, ' '));
  193. end if;
  194. if once then
  195. Switch_Character_Attribute (Attr => attr);
  196. else
  197. Set_Character_Attributes (Attr => attr);
  198. end if;
  199. Add (Str => "abcde fghij klmno pqrst uvwxy z");
  200. if once then
  201. Switch_Character_Attribute (Attr => attr, On => False);
  202. end if;
  203. if skip /= 0 then
  204. Add (Str => Ada.Strings.Fixed."*" (skip, ' '));
  205. end if;
  206. Add (Ch => '|');
  207. if attr /= Normal_Video then
  208. declare begin
  209. if not subset (super => Supported_Attributes, sub => attr) then
  210. Add (Str => " (N/A)");
  211. elsif ncv > 0 and has_A_COLOR (Get_Background) then
  212. declare
  213. Color_Supported_Attributes :
  214. constant Character_Attribute_Set := make_record (ncv);
  215. begin
  216. if intersect (Color_Supported_Attributes, attr) then
  217. Add (Str => " (NCV) ");
  218. end if;
  219. end;
  220. end if;
  221. end;
  222. end if;
  223. return row + 2;
  224. end show_attr;
  225. procedure attr_getc (skip : in out Integer;
  226. fg, bg : in out Color_Number;
  227. result : out Boolean) is
  228. ch : constant Key_Code := Getchar;
  229. nc : constant Color_Number := Color_Number (Number_Of_Colors);
  230. begin
  231. result := True;
  232. if Ada.Characters.Handling.Is_Digit (Character'Val (ch)) then
  233. skip := ctoi (Code_To_Char (ch));
  234. elsif ch = CTRL ('L') then
  235. Touch;
  236. Touch (Current_Window);
  237. Refresh;
  238. elsif Has_Colors then
  239. case ch is
  240. -- Note the mathematical elegance compared to the C version.
  241. when Character'Pos ('f') => fg := (fg + 1) mod nc;
  242. when Character'Pos ('F') => fg := (fg - 1) mod nc;
  243. when Character'Pos ('b') => bg := (bg + 1) mod nc;
  244. when Character'Pos ('B') => bg := (bg - 1) mod nc;
  245. when others =>
  246. result := False;
  247. end case;
  248. else
  249. result := False;
  250. end if;
  251. end attr_getc;
  252. -- pairs could be defined as array ( Color_Number(0) .. colors - 1) of
  253. -- array (Color_Number(0).. colors - 1) of Boolean;
  254. pairs : array (Color_Pair'Range) of Boolean := (others => False);
  255. fg, bg : Color_Number := Black; -- = 0;
  256. xmc : constant Integer := Get_Number ("xmc");
  257. skip : Integer := xmc;
  258. n : Integer;
  259. use Int_IO;
  260. begin
  261. pairs (0) := True;
  262. if skip < 0 then
  263. skip := 0;
  264. end if;
  265. n := skip;
  266. loop
  267. declare
  268. row : Line_Position := 2;
  269. normal : Attributed_Character := Blank2;
  270. -- ???
  271. begin
  272. -- row := 2; -- weird, row is set to 0 without this.
  273. -- TODO delete the above line, it was a gdb quirk that confused me
  274. if Has_Colors then
  275. declare pair : constant Color_Pair :=
  276. Color_Pair (fg * Color_Number (Number_Of_Colors) + bg);
  277. begin
  278. -- Go though each color pair. Assume that the number of
  279. -- Redefinable_Color_Pairs is 8*8 with predefined Colors 0..7
  280. if not pairs (pair) then
  281. Init_Pair (pair, fg, bg);
  282. pairs (pair) := True;
  283. end if;
  284. normal.Color := pair;
  285. end;
  286. end if;
  287. Set_Background (Ch => normal);
  288. Erase;
  289. Add (Line => 0, Column => 20,
  290. Str => "Character attribute test display");
  291. row := show_attr (row, n, (Stand_Out => True, others => False),
  292. "STANDOUT", True);
  293. row := show_attr (row, n, (Reverse_Video => True, others => False),
  294. "REVERSE", True);
  295. row := show_attr (row, n, (Bold_Character => True, others => False),
  296. "BOLD", True);
  297. row := show_attr (row, n, (Under_Line => True, others => False),
  298. "UNDERLINE", True);
  299. row := show_attr (row, n, (Dim_Character => True, others => False),
  300. "DIM", True);
  301. row := show_attr (row, n, (Blink => True, others => False),
  302. "BLINK", True);
  303. -- row := show_attr (row, n, (Protected_Character => True,
  304. -- others => False), "PROTECT", True);
  305. row := show_attr (row, n, (Invisible_Character => True,
  306. others => False), "INVISIBLE", True);
  307. row := show_attr (row, n, Normal_Video, "NORMAL", False);
  308. Move_Cursor (Line => row, Column => 8);
  309. if xmc > -1 then
  310. Add (Str => "This terminal does have the magic-cookie glitch");
  311. else
  312. Add (Str => "This terminal does not have the magic-cookie glitch");
  313. end if;
  314. Move_Cursor (Line => row + 1, Column => 8);
  315. Add (Str => "Enter a digit to set gaps on each side of " &
  316. "displayed attributes");
  317. Move_Cursor (Line => row + 2, Column => 8);
  318. Add (Str => "^L = repaint");
  319. if Has_Colors then
  320. declare tmp1 : String (1 .. 1);
  321. begin
  322. Add (Str => ". f/F/b/F toggle colors (");
  323. Put (tmp1, Integer (fg));
  324. Add (Str => tmp1);
  325. Add (Ch => '/');
  326. Put (tmp1, Integer (bg));
  327. Add (Str => tmp1);
  328. Add (Ch => ')');
  329. end;
  330. end if;
  331. Refresh;
  332. end;
  333. declare result : Boolean; begin
  334. attr_getc (n, fg, bg, result);
  335. exit when not result;
  336. end;
  337. end loop;
  338. Set_Background (Ch => Blank2);
  339. Erase;
  340. End_Windows;
  341. end ncurses2.attr_test;