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