]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/ada_include/terminal_interface-curses-mouse.adb
ncurses 4.2
[ncurses.git] / Ada95 / ada_include / 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 <Juergen.Pfeifer@T-Online.de> 1996
37 --  Version Control:
38 --  $Revision: 1.9 $
39 --  Binding Version 00.93
40 ------------------------------------------------------------------------------
41 with System;
42
43 with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
44 with Interfaces;
45 with Interfaces.C;
46 with Unchecked_Conversion;
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 CInt_To_Mask is new
54      Unchecked_Conversion (Source => C_Int,
55                            Target => Event_Mask);
56
57    function Mask_To_CInt is new
58      Unchecked_Conversion (Source => Event_Mask,
59                            Target => C_Int);
60
61    function Has_Mouse return Boolean
62    is
63       function Mouse_Avail return C_Int;
64       pragma Import (C, Mouse_Avail, "_nc_has_mouse");
65    begin
66       if Has_Key (Key_Mouse) or else Mouse_Avail /= 0 then
67          return True;
68       else
69          return False;
70       end if;
71    end Has_Mouse;
72
73    function Get_Mouse return Mouse_Event
74    is
75       type Event_Access is access all Mouse_Event;
76
77       function Getmouse (Ev : Event_Access) return C_Int;
78       pragma Import (C, Getmouse, "getmouse");
79
80       Event : aliased Mouse_Event;
81    begin
82       if Getmouse (Event'Access) = Curses_Err then
83          raise Curses_Exception;
84       end if;
85       return Event;
86    end Get_Mouse;
87
88    procedure Register_Reportable_Event (B    : in Mouse_Button;
89                                         S    : in Button_State;
90                                         Mask : in out Event_Mask)
91    is
92       type Evt_Access is access all Event_Mask;
93       function Register (B : C_Int;
94                          S : C_Int;
95                          M : Evt_Access) return C_Int;
96       pragma Import (C, Register, "_nc_ada_mouse_mask");
97
98       T : aliased Event_Mask := Mask;
99       M : Evt_Access := T'Access;
100       R : constant C_Int := Register (C_Int (Mouse_Button'Pos (B)),
101                                       C_Int (Button_State'Pos (S)),
102                                       M);
103    begin
104       if R = Curses_Err then
105          raise Curses_Exception;
106       end if;
107       Mask := T;
108    end Register_Reportable_Event;
109
110    function Start_Mouse (Mask : Event_Mask := All_Events)
111                          return Event_Mask
112    is
113       type Int_Access is access all C_Int;
114       function MMask (M : C_Int; O : Int_Access := null) return C_Int;
115       pragma Import (C, MMask, "mousemask");
116       R : C_Int;
117    begin
118       R := MMask (Mask_To_CInt (Mask));
119       return CInt_To_Mask (R);
120    end Start_Mouse;
121
122    procedure End_Mouse
123    is
124       Old : constant Event_Mask := Start_Mouse (No_Events);
125    begin
126       null;
127    end End_Mouse;
128
129    procedure Get_Event (Event  : in  Mouse_Event;
130                         Y      : out Line_Position;
131                         X      : out Column_Position;
132                         Button : out Mouse_Button;
133                         State  : out Button_State)
134    is
135       procedure Dispatch_Event (M : in C_Int;
136                                 B : out C_Int;
137                                 S : out C_Int);
138       pragma Import (C, Dispatch_Event, "_nc_ada_mouse_event");
139
140       Mask  : constant Interfaces.C.int := Mask_To_CInt (Event.Bstate);
141       B, S  : C_Int;
142    begin
143       X := Column_Position (Event.X);
144       Y := Line_Position   (Event.Y);
145       Dispatch_Event (Mask, B, S);
146       Button := Mouse_Button'Val (B);
147       State  := Button_State'Val (S);
148    end Get_Event;
149
150    procedure Unget_Mouse (Event : in Mouse_Event)
151    is
152       function Ungetmouse (Ev : Mouse_Event) return C_Int;
153       pragma Import (C, Ungetmouse, "ungetmouse");
154    begin
155       if Ungetmouse (Event) = Curses_Err then
156          raise Curses_Exception;
157       end if;
158    end Unget_Mouse;
159
160    function Enclosed_In_Window (Win    : Window := Standard_Window;
161                                 Event  : Mouse_Event) return Boolean
162    is
163       function Wenclose (Win : Window; Y : C_Int; X : C_Int) return C_Int;
164       pragma Import (C, Wenclose, "wenclose");
165    begin
166       if Wenclose (Win, C_Int (Event.Y), C_Int (Event.X)) = Curses_False then
167          return False;
168       else
169          return True;
170       end if;
171    end Enclosed_In_Window;
172
173    function Mouse_Interval (Msec : Natural := 200) return Natural
174    is
175       function Mouseinterval (Msec : C_Int) return C_Int;
176       pragma Import (C, Mouseinterval, "mouseinterval");
177    begin
178       return Natural (Mouseinterval (C_Int (Msec)));
179    end Mouse_Interval;
180
181 end Terminal_Interface.Curses.Mouse;