]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/sample-menu_demo-aux.adb
ncurses 5.4
[ncurses.git] / Ada95 / samples / sample-menu_demo-aux.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                            Sample.Menu_Demo.Aux                          --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 1998 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.11 $
39 --  Binding Version 01.00
40 ------------------------------------------------------------------------------
41 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
42
43 with Sample.Manifest; use Sample.Manifest;
44 with Sample.Helpers; use Sample.Helpers;
45 with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
46 with Sample.Explanation; use Sample.Explanation;
47
48 package body Sample.Menu_Demo.Aux is
49
50    procedure Geometry (M  : in  Menu;
51                        L  : out Line_Count;
52                        C  : out Column_Count;
53                        Y  : out Line_Position;
54                        X  : out Column_Position;
55                        Fy : out Line_Position;
56                        Fx : out Column_Position);
57
58    procedure Geometry (M  : in  Menu;
59                        L  : out Line_Count;        -- Lines used for menu
60                        C  : out Column_Count;      -- Columns used for menu
61                        Y  : out Line_Position;     -- Proposed Line for menu
62                        X  : out Column_Position;   -- Proposed Column for menu
63                        Fy : out Line_Position;     -- Vertical inner frame
64                        Fx : out Column_Position)   -- Horiz. inner frame
65    is
66       Spc_Desc : Column_Position; -- spaces between description and item
67    begin
68       Set_Mark (M, Menu_Marker);
69
70       Spacing (M, Spc_Desc, Fy, Fx);
71       Scale (M, L, C);
72
73       Fx := Fx + Column_Position (Fy - 1); -- looks a bit nicer
74
75       L := L + 2 * Fy;  -- count for frame at top and bottom
76       C := C + 2 * Fx;  -- "
77
78       --  Calculate horizontal coordinate at the screen center
79       X := (Columns - C) / 2;
80       Y := 1;  -- always startin line 1
81
82    end Geometry;
83
84    procedure Geometry (M : in  Menu;
85                        L : out Line_Count;        -- Lines used for menu
86                        C : out Column_Count;      -- Columns used for menu
87                        Y : out Line_Position;     -- Proposed Line for menu
88                        X  : out Column_Position)  -- Proposed Column for menu
89    is
90       Fy : Line_Position;
91       Fx : Column_Position;
92    begin
93       Geometry (M, L, C, Y, X, Fy, Fx);
94    end Geometry;
95
96    function Create (M     : Menu;
97                     Title : String;
98                     Lin   : Line_Position;
99                     Col   : Column_Position) return Panel
100    is
101       W, S : Window;
102       L : Line_Count;
103       C : Column_Count;
104       Y, Fy : Line_Position;
105       X, Fx : Column_Position;
106       Pan : Panel;
107    begin
108       Geometry (M, L, C, Y, X, Fy, Fx);
109       W := New_Window (L, C, Lin, Col);
110       Set_Meta_Mode (W);
111       Set_KeyPad_Mode (W);
112       if Has_Colors then
113          Set_Background (Win => W,
114                          Ch  => (Ch    => ' ',
115                                  Color => Menu_Back_Color,
116                                  Attr  => Normal_Video));
117          Set_Foreground (Men => M, Color => Menu_Fore_Color);
118          Set_Background (Men => M, Color => Menu_Back_Color);
119          Set_Grey (Men => M, Color => Menu_Grey_Color);
120          Erase (W);
121       end if;
122       S := Derived_Window (W, L - Fy, C - Fx, Fy, Fx);
123       Set_Meta_Mode (S);
124       Set_KeyPad_Mode (S);
125       Box (W);
126       Set_Window (M, W);
127       Set_Sub_Window (M, S);
128       if Title'Length > 0 then
129          Window_Title (W, Title);
130       end if;
131       Pan := New_Panel (W);
132       Post (M);
133       return Pan;
134    end Create;
135
136    procedure Destroy (M : in Menu;
137                       P : in out Panel)
138    is
139       W, S : Window;
140    begin
141       W := Get_Window (M);
142       S := Get_Sub_Window (M);
143       Post (M, False);
144       Erase (W);
145       Delete (P);
146       Set_Window (M, Null_Window);
147       Set_Sub_Window (M, Null_Window);
148       Delete (S);
149       Delete (W);
150       Update_Panels;
151    end Destroy;
152
153    function Get_Request (M : Menu; P : Panel) return Key_Code
154    is
155       W  : constant Window := Get_Window (M);
156       K  : Real_Key_Code;
157       Ch : Character;
158    begin
159       Top (P);
160       loop
161          K := Get_Key (W);
162          if K in Special_Key_Code'Range then
163             case K is
164                when HELP_CODE           => Explain_Context;
165                when EXPLAIN_CODE        => Explain ("MENUKEYS");
166                when Key_Home            => return REQ_FIRST_ITEM;
167                when QUIT_CODE           => return QUIT;
168                when Key_Cursor_Down     => return REQ_DOWN_ITEM;
169                when Key_Cursor_Up       => return REQ_UP_ITEM;
170                when Key_Cursor_Left     => return REQ_LEFT_ITEM;
171                when Key_Cursor_Right    => return REQ_RIGHT_ITEM;
172                when Key_End             => return REQ_LAST_ITEM;
173                when Key_Backspace       => return REQ_BACK_PATTERN;
174                when Key_Next_Page       => return REQ_SCR_DPAGE;
175                when Key_Previous_Page   => return REQ_SCR_UPAGE;
176                when others              => return K;
177             end case;
178          elsif K in Normal_Key_Code'Range then
179             Ch := Character'Val (K);
180             case Ch is
181                when CAN => return QUIT;                  --  CTRL-X
182                when SO  => return REQ_NEXT_ITEM;         --  CTRL-N
183                when DLE => return REQ_PREV_ITEM;         --  CTRL-P
184                when NAK => return REQ_SCR_ULINE;         --  CTRL-U
185                when EOT => return REQ_SCR_DLINE;         --  CTRL-D
186                when ACK => return REQ_SCR_DPAGE;         --  CTRL-F
187                when STX => return REQ_SCR_UPAGE;         --  CTRL-B
188                when EM  => return REQ_CLEAR_PATTERN;     --  CTRL-Y
189                when BS  => return REQ_BACK_PATTERN;      --  CTRL-H
190                when SOH => return REQ_NEXT_MATCH;        --  CTRL-A
191                when ENQ => return REQ_PREV_MATCH;        --  CTRL-E
192                when DC4 => return REQ_TOGGLE_ITEM;       --  CTRL-T
193
194                when CR | LF  => return SELECT_ITEM;
195                when others   => return K;
196             end case;
197          else
198             return K;
199          end if;
200       end loop;
201    end Get_Request;
202
203 end Sample.Menu_Demo.Aux;
204