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