]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/src/terminal_interface-curses-mouse.adb
ncurses 6.1 - patch 20190914
[ncurses.git] / Ada95 / src / terminal_interface-curses-mouse.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                           GNAT ncurses Binding                           --
4 --                                                                          --
5 --                     Terminal_Interface.Curses.Mouse                      --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 1998-2014,2018 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.26 $
39 --  $Date: 2018/07/07 23:35:05 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
42 with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
43 with Interfaces.C; use Interfaces.C;
44 use Interfaces;
45
46 package body Terminal_Interface.Curses.Mouse is
47
48    function Has_Mouse return Boolean
49    is
50       function Mouse_Avail return C_Int;
51       pragma Import (C, Mouse_Avail, "has_mouse");
52    begin
53       if Has_Key (Key_Mouse) or else Mouse_Avail /= 0 then
54          return True;
55       else
56          return False;
57       end if;
58    end Has_Mouse;
59
60    function Get_Mouse return Mouse_Event
61    is
62       type Event_Access is access all Mouse_Event;
63
64       function Getmouse (Ev : Event_Access) return C_Int;
65       pragma Import (C, Getmouse, "getmouse");
66
67       Event : aliased Mouse_Event;
68    begin
69       if Getmouse (Event'Access) = Curses_Err then
70          raise Curses_Exception;
71       end if;
72       return Event;
73    end Get_Mouse;
74
75    procedure Register_Reportable_Event (Button : Mouse_Button;
76                                         State  : Button_State;
77                                         Mask   : in out Event_Mask)
78    is
79       Button_Nr : constant Natural := Mouse_Button'Pos (Button);
80       State_Nr  : constant Natural := Button_State'Pos (State);
81    begin
82       if Button in Modifier_Keys and then State /= Pressed then
83          raise Curses_Exception;
84       else
85          if Button in Real_Buttons then
86             Mask := Mask or ((2 ** (6 * Button_Nr)) ** State_Nr);
87          else
88             Mask := Mask or (BUTTON_CTRL ** (Button_Nr - 4));
89          end if;
90       end if;
91    end Register_Reportable_Event;
92
93    procedure Register_Reportable_Events (Button : Mouse_Button;
94                                          State  : Button_States;
95                                          Mask   : in out Event_Mask)
96    is
97    begin
98       for S in Button_States'Range loop
99          if State (S) then
100             Register_Reportable_Event (Button, S, Mask);
101          end if;
102       end loop;
103    end Register_Reportable_Events;
104
105    function Start_Mouse (Mask : Event_Mask := All_Events)
106                          return Event_Mask
107    is
108       function MMask (M : Event_Mask;
109                       O : access Event_Mask) return Event_Mask;
110       pragma Import (C, MMask, "mousemask");
111       R   : Event_Mask;
112       Old : aliased Event_Mask;
113    begin
114       R := MMask (Mask, Old'Access);
115       if R = No_Events then
116          Beep;
117       end if;
118       return Old;
119    end Start_Mouse;
120
121    procedure End_Mouse (Mask : Event_Mask := No_Events)
122    is
123    begin
124       if Mask /= No_Events then
125          Beep;
126       end if;
127    end End_Mouse;
128
129    procedure Dispatch_Event (Mask   : Event_Mask;
130                              Button : out Mouse_Button;
131                              State  : out Button_State);
132
133    procedure Dispatch_Event (Mask   : Event_Mask;
134                              Button : out Mouse_Button;
135                              State  : out Button_State) is
136       L : Event_Mask;
137    begin
138       Button := Alt;  --  preset to non real button;
139       if (Mask and BUTTON1_EVENTS) /= 0 then
140          Button := Left;
141       elsif (Mask and BUTTON2_EVENTS) /= 0 then
142          Button := Middle;
143       elsif (Mask and BUTTON3_EVENTS) /= 0 then
144          Button := Right;
145       elsif (Mask and BUTTON4_EVENTS) /= 0 then
146          Button := Button4;
147       end if;
148       if Button in Real_Buttons then
149          L := 2 ** (6 * Mouse_Button'Pos (Button));
150          for I in Button_State'Range loop
151             if (Mask and L) /= 0 then
152                State := I;
153                exit;
154             end if;
155             L := 2 * L;
156          end loop;
157       else
158          State := Pressed;
159          if (Mask and BUTTON_CTRL) /= 0 then
160             Button := Control;
161          elsif (Mask and BUTTON_SHIFT) /= 0 then
162             Button := Shift;
163          elsif (Mask and BUTTON_ALT) /= 0 then
164             Button := Alt;
165          end if;
166       end if;
167    end Dispatch_Event;
168
169    procedure Get_Event (Event  : Mouse_Event;
170                         Y      : out Line_Position;
171                         X      : out Column_Position;
172                         Button : out Mouse_Button;
173                         State  : out Button_State)
174    is
175       Mask  : constant Event_Mask := Event.Bstate;
176    begin
177       X := Column_Position (Event.X);
178       Y := Line_Position   (Event.Y);
179       Dispatch_Event (Mask, Button, State);
180    end Get_Event;
181
182    procedure Unget_Mouse (Event : Mouse_Event)
183    is
184       function Ungetmouse (Ev : Mouse_Event) return C_Int;
185       pragma Import (C, Ungetmouse, "ungetmouse");
186    begin
187       if Ungetmouse (Event) = Curses_Err then
188          raise Curses_Exception;
189       end if;
190    end Unget_Mouse;
191
192    function Enclosed_In_Window (Win    : Window := Standard_Window;
193                                 Event  : Mouse_Event) return Boolean
194    is
195       function Wenclose (Win : Window; Y : C_Int; X : C_Int)
196                          return Curses_Bool;
197       pragma Import (C, Wenclose, "wenclose");
198    begin
199       if Wenclose (Win, C_Int (Event.Y), C_Int (Event.X))
200         = Curses_Bool_False
201       then
202          return False;
203       else
204          return True;
205       end if;
206    end Enclosed_In_Window;
207
208    function Mouse_Interval (Msec : Natural := 200) return Natural
209    is
210       function Mouseinterval (Msec : C_Int) return C_Int;
211       pragma Import (C, Mouseinterval, "mouseinterval");
212    begin
213       return Natural (Mouseinterval (C_Int (Msec)));
214    end Mouse_Interval;
215
216 end Terminal_Interface.Curses.Mouse;