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