]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/sample-curses_demo-mouse.adb
ncurses 5.0
[ncurses.git] / Ada95 / samples / sample-curses_demo-mouse.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                         Sample.Curses_Demo.Mouse                         --
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 <juergen.pfeifer@gmx.net> 1996
37 --  Version Control
38 --  $Revision: 1.11 $
39 --  Binding Version 01.00
40 ------------------------------------------------------------------------------
41 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
42 with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
43 with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
44 with Terminal_Interface.Curses.Text_IO; use Terminal_Interface.Curses.Text_IO;
45 with Terminal_Interface.Curses.Text_IO.Integer_IO;
46 with Terminal_Interface.Curses.Text_IO.Enumeration_IO;
47
48 with Sample.Helpers; use Sample.Helpers;
49 with Sample.Manifest; use Sample.Manifest;
50 with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
51 with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
52 with Sample.Explanation; use Sample.Explanation;
53
54 package body Sample.Curses_Demo.Mouse is
55
56    package Int_IO is new
57      Terminal_Interface.Curses.Text_IO.Integer_IO (Integer);
58    use Int_IO;
59
60    package Button_IO is new
61      Terminal_Interface.Curses.Text_IO.Enumeration_IO (Mouse_Button);
62    use Button_IO;
63
64    package State_IO is new
65      Terminal_Interface.Curses.Text_IO.Enumeration_IO (Button_State);
66    use State_IO;
67
68    procedure Demo is
69
70       type Controls is array (1 .. 3) of Panel;
71
72       Frame : Window;
73       Msg   : Window;
74       Ctl   : Controls;
75       Pan   : Panel;
76       K     : Real_Key_Code;
77       V     : Cursor_Visibility := Invisible;
78       W     : Window;
79       Note  : Window;
80       Msg_L : constant Line_Count := 8;
81       Lins  : Line_Position := Lines;
82       Cols  : Column_Position;
83       Mask  : Event_Mask;
84       procedure Show_Mouse_Event;
85
86       procedure Show_Mouse_Event
87       is
88          Evt    : constant Mouse_Event := Get_Mouse;
89          Y      : Line_Position;
90          X      : Column_Position;
91          Button : Mouse_Button;
92          State  : Button_State;
93          W      : Window;
94       begin
95          Get_Event (Evt, Y, X, Button, State);
96          Put (Msg, "Event at");
97          Put (Msg, "  X=");    Put (Msg, Integer (X), 3);
98          Put (Msg, ", Y=");    Put (Msg, Integer (Y), 3);
99          Put (Msg, ", Btn=");  Put (Msg, Button, 10);
100          Put (Msg, ", Stat="); Put (Msg, State, 15);
101          for I in Ctl'Range loop
102             W := Get_Window (Ctl (I));
103             if Enclosed_In_Window (W, Evt) then
104                Transform_Coordinates (W, Y, X, From_Screen);
105                Put (Msg, ",Box(");
106                Put (Msg, Integer (I), 1); Put (Msg, ",");
107                Put (Msg, Integer (Y), 1); Put (Msg, ",");
108                Put (Msg, Integer (X), 1); Put (Msg, ")");
109             end if;
110          end loop;
111          New_Line (Msg);
112          Flush (Msg);
113          Update_Panels; Update_Screen;
114       end Show_Mouse_Event;
115
116    begin
117       Push_Environment ("MOUSE00");
118       Notepad ("MOUSE-PAD00");
119       Default_Labels;
120       Set_Cursor_Visibility (V);
121
122       Note  := Notepad_Window;
123       if Note /= Null_Window then
124          Get_Window_Position (Note, Lins, Cols);
125       end if;
126       Frame := Create (Msg_L, Columns, Lins - Msg_L, 0);
127       if Has_Colors then
128          Set_Background (Win => Frame,
129                          Ch => (Color => Default_Colors,
130                                 Attr  => Normal_Video,
131                                 Ch    => ' '));
132          Set_Character_Attributes (Win   => Frame,
133                                    Attr  => Normal_Video,
134                                    Color => Default_Colors);
135          Erase (Frame);
136       end if;
137       Msg   := Derived_Window (Frame, Msg_L - 2, Columns - 2, 1, 1);
138       Pan   := Create (Frame);
139
140       Set_Meta_Mode;
141       Set_KeyPad_Mode;
142       Mask := Start_Mouse;
143
144       Box (Frame);
145       Window_Title (Frame, "Mouse Protocol");
146       Refresh_Without_Update (Frame);
147       Allow_Scrolling (Msg, True);
148
149       declare
150          Middle_Column : constant Integer := Integer (Columns) / 2;
151          Middle_Index  : constant Natural := Ctl'First + (Ctl'Length / 2);
152          Width         : constant Column_Count := 5;
153          Height        : constant Line_Count   := 3;
154          Half          : constant Column_Count := Width / 2;
155          Space         : constant Column_Count := 3;
156          Position      : Integer;
157          W             : Window;
158       begin
159          for I in Ctl'Range loop
160             Position := (Integer (I) - Integer (Middle_Index)) *
161               Integer (Half + Space + Width) + Middle_Column;
162             W := Create (Height,
163                          Width,
164                          1,
165                          Column_Position (Position));
166             if Has_Colors then
167                Set_Background (Win => W,
168                                Ch => (Color => Menu_Back_Color,
169                                       Attr  => Normal_Video,
170                                       Ch    => ' '));
171                Set_Character_Attributes (Win   => W,
172                                          Attr  => Normal_Video,
173                                          Color => Menu_Fore_Color);
174                Erase (W);
175             end if;
176             Ctl (I) := Create (W);
177             Box (W);
178             Move_Cursor (W, 1, Half);
179             Put (W, Integer (I), 1);
180             Refresh_Without_Update (W);
181          end loop;
182       end;
183
184       Update_Panels; Update_Screen;
185
186       loop
187          K := Get_Key;
188          if K in Special_Key_Code'Range then
189             case K is
190                when QUIT_CODE     => exit;
191                when HELP_CODE     => Explain_Context;
192                when EXPLAIN_CODE  => Explain ("MOUSEKEYS");
193                when Key_Mouse     => Show_Mouse_Event;
194                when others        => null;
195             end case;
196          end if;
197       end loop;
198
199       for I in Ctl'Range loop
200          W := Get_Window (Ctl (I));
201          Clear (W);
202          Delete (Ctl (I));
203          Delete (W);
204       end loop;
205
206       Clear (Frame);
207       Delete (Pan);
208       Delete (Msg);
209       Delete (Frame);
210
211       Set_Cursor_Visibility (V);
212       End_Mouse (Mask);
213
214       Pop_Environment;
215       Update_Panels; Update_Screen;
216
217    end Demo;
218
219 end Sample.Curses_Demo.Mouse;
220