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