]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/ada_include/terminal_interface-curses-mouse.adb
ncurses 4.1
[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 --  Version 00.92                                                           --
10 --                                                                          --
11 --  The ncurses Ada95 binding is copyrighted 1996 by                        --
12 --  Juergen Pfeifer, Email: Juergen.Pfeifer@T-Online.de                     --
13 --                                                                          --
14 --  Permission is hereby granted to reproduce and distribute this           --
15 --  binding by any means and for any fee, whether alone or as part          --
16 --  of a larger distribution, in source or in binary form, PROVIDED         --
17 --  this notice is included with any such distribution, and is not          --
18 --  removed from any of its header files. Mention of ncurses and the        --
19 --  author of this binding in any applications linked with it is            --
20 --  highly appreciated.                                                     --
21 --                                                                          --
22 --  This binding comes AS IS with no warranty, implied or expressed.        --
23 ------------------------------------------------------------------------------
24 --  Version Control:
25 --  $Revision: 1.3 $
26 ------------------------------------------------------------------------------
27 with System;
28
29 with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
30 with Interfaces;
31 with Interfaces.C;
32 with Unchecked_Conversion;
33
34 package body Terminal_Interface.Curses.Mouse is
35
36    use type System.Bit_Order;
37    use type Interfaces.C.int;
38
39    function CInt_To_Mask is new
40      Unchecked_Conversion (Source => C_Int,
41                            Target => Event_Mask);
42
43    function Mask_To_CInt is new
44      Unchecked_Conversion (Source => Event_Mask,
45                            Target => C_Int);
46
47    function Get_Mouse return Mouse_Event
48    is
49       type Event_Access is access all Mouse_Event;
50
51       function Getmouse (Ev : Event_Access) return C_Int;
52       pragma Import (C, Getmouse, "getmouse");
53
54       Event : aliased Mouse_Event;
55    begin
56       if Getmouse (Event'Access) = Curses_Err then
57          raise Curses_Exception;
58       end if;
59       return Event;
60    end Get_Mouse;
61
62    procedure Register_Reportable_Event (B    : in Mouse_Button;
63                                         S    : in Button_State;
64                                         Mask : in out Event_Mask)
65    is
66       type Evt_Access is access all Event_Mask;
67       function Register (B : C_Int;
68                          S : C_Int;
69                          M : Evt_Access) return C_Int;
70       pragma Import (C, Register, "_nc_ada_mouse_mask");
71
72       T : aliased Event_Mask := Mask;
73       M : Evt_Access := T'Access;
74       R : constant C_Int := Register (C_Int (Mouse_Button'Pos (B)),
75                                       C_Int (Button_State'Pos (S)),
76                                       M);
77    begin
78       if R = Curses_Err then
79          raise Curses_Exception;
80       end if;
81       Mask := T;
82    end Register_Reportable_Event;
83
84    function Start_Mouse (Mask : Event_Mask := All_Events)
85                          return Event_Mask
86    is
87       type Int_Access is access all C_Int;
88       function MMask (M : C_Int; O : Int_Access := null) return C_Int;
89       pragma Import (C, MMask, "mousemask");
90       R : C_Int;
91    begin
92       R := MMask (Mask_To_CInt (Mask));
93       return CInt_To_Mask (R);
94    end Start_Mouse;
95
96    procedure Get_Event (Event  : in  Mouse_Event;
97                         Y      : out Line_Position;
98                         X      : out Column_Position;
99                         Button : out Mouse_Button;
100                         State  : out Button_State)
101    is
102       procedure Dispatch_Event (M : in C_Int;
103                                 B : out C_Int;
104                                 S : out C_Int);
105       pragma Import (C, Dispatch_Event, "_nc_ada_mouse_event");
106
107       Mask  : constant Interfaces.C.int := Mask_To_CInt (Event.Bstate);
108       B, S  : C_Int;
109    begin
110       X := Column_Position (Event.X);
111       Y := Line_Position   (Event.Y);
112       Dispatch_Event (Mask, B, S);
113       Button := Mouse_Button'Val (B);
114       State  := Button_State'Val (S);
115    end Get_Event;
116
117    procedure Unget_Mouse (Event : in Mouse_Event)
118    is
119       function Ungetmouse (Ev : Mouse_Event) return C_Int;
120       pragma Import (C, Ungetmouse, "ungetmouse");
121    begin
122       if Ungetmouse (Event) = Curses_Err then
123          raise Curses_Exception;
124       end if;
125    end Unget_Mouse;
126
127    function Enclosed_In_Window (Win    : Window := Standard_Window;
128                                 Event  : Mouse_Event) return Boolean
129    is
130       function Wenclose (Win : Window; Y : C_Int; X : C_Int) return C_Int;
131       pragma Import (C, Wenclose, "wenclose");
132    begin
133       if Wenclose (Win, C_Int (Event.Y), C_Int (Event.X)) = Curses_False then
134          return False;
135       else
136          return True;
137       end if;
138    end Enclosed_In_Window;
139
140    function Mouse_Interval (Msec : Natural := 200) return Natural
141    is
142       function Mouseinterval (Msec : C_Int) return C_Int;
143       pragma Import (C, Mouseinterval, "mouseinterval");
144    begin
145       return Natural (Mouseinterval (C_Int (Msec)));
146    end Mouse_Interval;
147
148 begin
149    if Generation_Bit_Order /= System.Default_Bit_Order then
150       raise Constraint_Error;
151    end if;
152 end Terminal_Interface.Curses.Mouse;