summaryrefslogtreecommitdiff
path: root/Ada95/src/terminal_interface-curses-mouse.adb
blob: 9b4032639308e6bb2917c1af9d1de542bee76f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
------------------------------------------------------------------------------
--                                                                          --
--                           GNAT ncurses Binding                           --
--                                                                          --
--                     Terminal_Interface.Curses.Mouse                      --
--                                                                          --
--                                 B O D Y                                  --
--                                                                          --
------------------------------------------------------------------------------
-- Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              --
--                                                                          --
-- Permission is hereby granted, free of charge, to any person obtaining a  --
-- copy of this software and associated documentation files (the            --
-- "Software"), to deal in the Software without restriction, including      --
-- without limitation the rights to use, copy, modify, merge, publish,      --
-- distribute, distribute with modifications, sublicense, and/or sell       --
-- copies of the Software, and to permit persons to whom the Software is    --
-- furnished to do so, subject to the following conditions:                 --
--                                                                          --
-- The above copyright notice and this permission notice shall be included  --
-- in all copies or substantial portions of the Software.                   --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
-- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
-- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
--                                                                          --
-- Except as contained in this notice, the name(s) of the above copyright   --
-- holders shall not be used in advertising or otherwise to promote the     --
-- sale, use or other dealings in this Software without prior written       --
-- authorization.                                                           --
------------------------------------------------------------------------------
--  Author:  Juergen Pfeifer, 1996
--  Version Control:
--  $Revision: 1.24 $
--  $Date: 2009/12/26 17:38:58 $
--  Binding Version 01.00
------------------------------------------------------------------------------
with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
with Interfaces.C; use Interfaces.C;
use Interfaces;

package body Terminal_Interface.Curses.Mouse is

   use type System.Bit_Order;

   function Has_Mouse return Boolean
   is
      function Mouse_Avail return C_Int;
      pragma Import (C, Mouse_Avail, "has_mouse");
   begin
      if Has_Key (Key_Mouse) or else Mouse_Avail /= 0 then
         return True;
      else
         return False;
      end if;
   end Has_Mouse;

   function Get_Mouse return Mouse_Event
   is
      type Event_Access is access all Mouse_Event;

      function Getmouse (Ev : Event_Access) return C_Int;
      pragma Import (C, Getmouse, "getmouse");

      Event : aliased Mouse_Event;
   begin
      if Getmouse (Event'Access) = Curses_Err then
         raise Curses_Exception;
      end if;
      return Event;
   end Get_Mouse;

   procedure Register_Reportable_Event (Button : Mouse_Button;
                                        State  : Button_State;
                                        Mask   : in out Event_Mask)
   is
      Button_Nr : constant Natural := Mouse_Button'Pos (Button);
      State_Nr  : constant Natural := Button_State'Pos (State);
   begin
      if Button in Modifier_Keys and then State /= Pressed then
         raise Curses_Exception;
      else
         if Button in Real_Buttons then
            Mask := Mask or ((2 ** (6 * Button_Nr)) ** State_Nr);
         else
            Mask := Mask or (BUTTON_CTRL ** (Button_Nr - 4));
         end if;
      end if;
   end Register_Reportable_Event;

   procedure Register_Reportable_Events (Button : Mouse_Button;
                                         State  : Button_States;
                                         Mask   : in out Event_Mask)
   is
   begin
      for S in Button_States'Range loop
         if State (S) then
            Register_Reportable_Event (Button, S, Mask);
         end if;
      end loop;
   end Register_Reportable_Events;

   function Start_Mouse (Mask : Event_Mask := All_Events)
                         return Event_Mask
   is
      function MMask (M : Event_Mask;
                      O : access Event_Mask) return Event_Mask;
      pragma Import (C, MMask, "mousemask");
      R   : Event_Mask;
      Old : aliased Event_Mask;
   begin
      R := MMask (Mask, Old'Access);
      if R = No_Events then
         Beep;
      end if;
      return Old;
   end Start_Mouse;

   procedure End_Mouse (Mask : Event_Mask := No_Events)
   is
   begin
      if Mask /= No_Events then
         Beep;
      end if;
   end End_Mouse;

   procedure Dispatch_Event (Mask   : Event_Mask;
                             Button : out Mouse_Button;
                             State  : out Button_State);

   procedure Dispatch_Event (Mask   : Event_Mask;
                             Button : out Mouse_Button;
                             State  : out Button_State) is
      L : Event_Mask;
   begin
      Button := Alt;  --  preset to non real button;
      if (Mask and BUTTON1_EVENTS) /= 0 then
         Button := Left;
      elsif (Mask and BUTTON2_EVENTS) /= 0 then
         Button := Middle;
      elsif (Mask and BUTTON3_EVENTS) /= 0 then
         Button := Right;
      elsif (Mask and BUTTON4_EVENTS) /= 0 then
         Button := Button4;
      end if;
      if Button in Real_Buttons then
         L := 2 ** (6 * Mouse_Button'Pos (Button));
         for I in Button_State'Range loop
            if (Mask and L) /= 0 then
               State := I;
               exit;
            end if;
            L := 2 * L;
         end loop;
      else
         State := Pressed;
         if (Mask and BUTTON_CTRL) /= 0 then
            Button := Control;
         elsif (Mask and BUTTON_SHIFT) /= 0 then
            Button := Shift;
         elsif (Mask and BUTTON_ALT) /= 0 then
            Button := Alt;
         end if;
      end if;
   end Dispatch_Event;

   procedure Get_Event (Event  : Mouse_Event;
                        Y      : out Line_Position;
                        X      : out Column_Position;
                        Button : out Mouse_Button;
                        State  : out Button_State)
   is
      Mask  : constant Event_Mask := Event.Bstate;
   begin
      X := Column_Position (Event.X);
      Y := Line_Position   (Event.Y);
      Dispatch_Event (Mask, Button, State);
   end Get_Event;

   procedure Unget_Mouse (Event : Mouse_Event)
   is
      function Ungetmouse (Ev : Mouse_Event) return C_Int;
      pragma Import (C, Ungetmouse, "ungetmouse");
   begin
      if Ungetmouse (Event) = Curses_Err then
         raise Curses_Exception;
      end if;
   end Unget_Mouse;

   function Enclosed_In_Window (Win    : Window := Standard_Window;
                                Event  : Mouse_Event) return Boolean
   is
      function Wenclose (Win : Window; Y : C_Int; X : C_Int)
                         return Curses_Bool;
      pragma Import (C, Wenclose, "wenclose");
   begin
      if Wenclose (Win, C_Int (Event.Y), C_Int (Event.X))
        = Curses_Bool_False then
         return False;
      else
         return True;
      end if;
   end Enclosed_In_Window;

   function Mouse_Interval (Msec : Natural := 200) return Natural
   is
      function Mouseinterval (Msec : C_Int) return C_Int;
      pragma Import (C, Mouseinterval, "mouseinterval");
   begin
      return Natural (Mouseinterval (C_Int (Msec)));
   end Mouse_Interval;

end Terminal_Interface.Curses.Mouse;