1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding --
5 -- Terminal_Interface.Curses.Mouse --
9 ------------------------------------------------------------------------------
10 -- Copyright 2018,2020 Thomas E. Dickey --
11 -- Copyright 1999-2009,2014 Free Software Foundation, Inc. --
13 -- Permission is hereby granted, free of charge, to any person obtaining a --
14 -- copy of this software and associated documentation files (the --
15 -- "Software"), to deal in the Software without restriction, including --
16 -- without limitation the rights to use, copy, modify, merge, publish, --
17 -- distribute, distribute with modifications, sublicense, and/or sell --
18 -- copies of the Software, and to permit persons to whom the Software is --
19 -- furnished to do so, subject to the following conditions: --
21 -- The above copyright notice and this permission notice shall be included --
22 -- in all copies or substantial portions of the Software. --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
25 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
26 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
27 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
28 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
29 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
30 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
32 -- Except as contained in this notice, the name(s) of the above copyright --
33 -- holders shall not be used in advertising or otherwise to promote the --
34 -- sale, use or other dealings in this Software without prior written --
36 ------------------------------------------------------------------------------
37 -- Author: Juergen Pfeifer, 1996
40 -- $Date: 2020/02/02 23:34:34 $
41 -- Binding Version 01.00
42 ------------------------------------------------------------------------------
43 with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
44 with Interfaces.C; use Interfaces.C;
47 package body Terminal_Interface.Curses.Mouse is
49 function Has_Mouse return Boolean
51 function Mouse_Avail return C_Int;
52 pragma Import (C, Mouse_Avail, "has_mouse");
54 if Has_Key (Key_Mouse) or else Mouse_Avail /= 0 then
61 function Get_Mouse return Mouse_Event
63 type Event_Access is access all Mouse_Event;
65 function Getmouse (Ev : Event_Access) return C_Int;
66 pragma Import (C, Getmouse, "getmouse");
68 Event : aliased Mouse_Event;
70 if Getmouse (Event'Access) = Curses_Err then
71 raise Curses_Exception;
76 procedure Register_Reportable_Event (Button : Mouse_Button;
78 Mask : in out Event_Mask)
80 Button_Nr : constant Natural := Mouse_Button'Pos (Button);
81 State_Nr : constant Natural := Button_State'Pos (State);
83 if Button in Modifier_Keys and then State /= Pressed then
84 raise Curses_Exception;
86 if Button in Real_Buttons then
87 Mask := Mask or ((2 ** (6 * Button_Nr)) ** State_Nr);
89 Mask := Mask or (BUTTON_CTRL ** (Button_Nr - 4));
92 end Register_Reportable_Event;
94 procedure Register_Reportable_Events (Button : Mouse_Button;
95 State : Button_States;
96 Mask : in out Event_Mask)
99 for S in Button_States'Range loop
101 Register_Reportable_Event (Button, S, Mask);
104 end Register_Reportable_Events;
106 function Start_Mouse (Mask : Event_Mask := All_Events)
109 function MMask (M : Event_Mask;
110 O : access Event_Mask) return Event_Mask;
111 pragma Import (C, MMask, "mousemask");
113 Old : aliased Event_Mask;
115 R := MMask (Mask, Old'Access);
116 if R = No_Events then
122 procedure End_Mouse (Mask : Event_Mask := No_Events)
125 if Mask /= No_Events then
130 procedure Dispatch_Event (Mask : Event_Mask;
131 Button : out Mouse_Button;
132 State : out Button_State);
134 procedure Dispatch_Event (Mask : Event_Mask;
135 Button : out Mouse_Button;
136 State : out Button_State) is
139 Button := Alt; -- preset to non real button;
140 if (Mask and BUTTON1_EVENTS) /= 0 then
142 elsif (Mask and BUTTON2_EVENTS) /= 0 then
144 elsif (Mask and BUTTON3_EVENTS) /= 0 then
146 elsif (Mask and BUTTON4_EVENTS) /= 0 then
149 if Button in Real_Buttons then
150 L := 2 ** (6 * Mouse_Button'Pos (Button));
151 for I in Button_State'Range loop
152 if (Mask and L) /= 0 then
160 if (Mask and BUTTON_CTRL) /= 0 then
162 elsif (Mask and BUTTON_SHIFT) /= 0 then
164 elsif (Mask and BUTTON_ALT) /= 0 then
170 procedure Get_Event (Event : Mouse_Event;
171 Y : out Line_Position;
172 X : out Column_Position;
173 Button : out Mouse_Button;
174 State : out Button_State)
176 Mask : constant Event_Mask := Event.Bstate;
178 X := Column_Position (Event.X);
179 Y := Line_Position (Event.Y);
180 Dispatch_Event (Mask, Button, State);
183 procedure Unget_Mouse (Event : Mouse_Event)
185 function Ungetmouse (Ev : Mouse_Event) return C_Int;
186 pragma Import (C, Ungetmouse, "ungetmouse");
188 if Ungetmouse (Event) = Curses_Err then
189 raise Curses_Exception;
193 function Enclosed_In_Window (Win : Window := Standard_Window;
194 Event : Mouse_Event) return Boolean
196 function Wenclose (Win : Window; Y : C_Int; X : C_Int)
198 pragma Import (C, Wenclose, "wenclose");
200 if Wenclose (Win, C_Int (Event.Y), C_Int (Event.X))
207 end Enclosed_In_Window;
209 function Mouse_Interval (Msec : Natural := 200) return Natural
211 function Mouseinterval (Msec : C_Int) return C_Int;
212 pragma Import (C, Mouseinterval, "mouseinterval");
214 return Natural (Mouseinterval (C_Int (Msec)));
217 end Terminal_Interface.Curses.Mouse;