sample-curses_demo-mouse.adb 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- Sample.Curses_Demo.Mouse --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 1998-2006,2008 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.16 $
  39. -- $Date: 2008/07/26 18:48:19 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  43. with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
  44. with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
  45. with Terminal_Interface.Curses.Text_IO; use Terminal_Interface.Curses.Text_IO;
  46. with Terminal_Interface.Curses.Text_IO.Integer_IO;
  47. with Terminal_Interface.Curses.Text_IO.Enumeration_IO;
  48. with Sample.Helpers; use Sample.Helpers;
  49. with Sample.Manifest; use Sample.Manifest;
  50. with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
  51. with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
  52. with Sample.Explanation; use Sample.Explanation;
  53. package body Sample.Curses_Demo.Mouse is
  54. package Int_IO is new
  55. Terminal_Interface.Curses.Text_IO.Integer_IO (Integer);
  56. use Int_IO;
  57. package Button_IO is new
  58. Terminal_Interface.Curses.Text_IO.Enumeration_IO (Mouse_Button);
  59. use Button_IO;
  60. package State_IO is new
  61. Terminal_Interface.Curses.Text_IO.Enumeration_IO (Button_State);
  62. use State_IO;
  63. procedure Demo is
  64. type Controls is array (1 .. 3) of Panel;
  65. Frame : Window;
  66. Msg : Window;
  67. Ctl : Controls;
  68. Pan : Panel;
  69. K : Real_Key_Code;
  70. V : Cursor_Visibility := Invisible;
  71. W : Window;
  72. Note : Window;
  73. Msg_L : constant Line_Count := 8;
  74. Lins : Line_Position := Lines;
  75. Cols : Column_Position;
  76. Mask : Event_Mask;
  77. procedure Show_Mouse_Event;
  78. procedure Show_Mouse_Event
  79. is
  80. Evt : constant Mouse_Event := Get_Mouse;
  81. Y : Line_Position;
  82. X : Column_Position;
  83. Button : Mouse_Button;
  84. State : Button_State;
  85. W : Window;
  86. begin
  87. Get_Event (Evt, Y, X, Button, State);
  88. Put (Msg, "Event at");
  89. Put (Msg, " X="); Put (Msg, Integer (X), 3);
  90. Put (Msg, ", Y="); Put (Msg, Integer (Y), 3);
  91. Put (Msg, ", Btn="); Put (Msg, Button, 10);
  92. Put (Msg, ", Stat="); Put (Msg, State, 15);
  93. for I in Ctl'Range loop
  94. W := Get_Window (Ctl (I));
  95. if Enclosed_In_Window (W, Evt) then
  96. Transform_Coordinates (W, Y, X, From_Screen);
  97. Put (Msg, ",Box(");
  98. Put (Msg, (I), 1); Put (Msg, ",");
  99. Put (Msg, Integer (Y), 1); Put (Msg, ",");
  100. Put (Msg, Integer (X), 1); Put (Msg, ")");
  101. end if;
  102. end loop;
  103. New_Line (Msg);
  104. Flush (Msg);
  105. Update_Panels; Update_Screen;
  106. end Show_Mouse_Event;
  107. begin
  108. Push_Environment ("MOUSE00");
  109. Notepad ("MOUSE-PAD00");
  110. Default_Labels;
  111. Set_Cursor_Visibility (V);
  112. Note := Notepad_Window;
  113. if Note /= Null_Window then
  114. Get_Window_Position (Note, Lins, Cols);
  115. end if;
  116. Frame := Create (Msg_L, Columns, Lins - Msg_L, 0);
  117. if Has_Colors then
  118. Set_Background (Win => Frame,
  119. Ch => (Color => Default_Colors,
  120. Attr => Normal_Video,
  121. Ch => ' '));
  122. Set_Character_Attributes (Win => Frame,
  123. Attr => Normal_Video,
  124. Color => Default_Colors);
  125. Erase (Frame);
  126. end if;
  127. Msg := Derived_Window (Frame, Msg_L - 2, Columns - 2, 1, 1);
  128. Pan := Create (Frame);
  129. Set_Meta_Mode;
  130. Set_KeyPad_Mode;
  131. Mask := Start_Mouse;
  132. Box (Frame);
  133. Window_Title (Frame, "Mouse Protocol");
  134. Refresh_Without_Update (Frame);
  135. Allow_Scrolling (Msg, True);
  136. declare
  137. Middle_Column : constant Integer := Integer (Columns) / 2;
  138. Middle_Index : constant Natural := Ctl'First + (Ctl'Length / 2);
  139. Width : constant Column_Count := 5;
  140. Height : constant Line_Count := 3;
  141. Half : constant Column_Count := Width / 2;
  142. Space : constant Column_Count := 3;
  143. Position : Integer;
  144. W : Window;
  145. begin
  146. for I in Ctl'Range loop
  147. Position := ((I) - Integer (Middle_Index)) *
  148. Integer (Half + Space + Width) + Middle_Column;
  149. W := Create (Height,
  150. Width,
  151. 1,
  152. Column_Position (Position));
  153. if Has_Colors then
  154. Set_Background (Win => W,
  155. Ch => (Color => Menu_Back_Color,
  156. Attr => Normal_Video,
  157. Ch => ' '));
  158. Set_Character_Attributes (Win => W,
  159. Attr => Normal_Video,
  160. Color => Menu_Fore_Color);
  161. Erase (W);
  162. end if;
  163. Ctl (I) := Create (W);
  164. Box (W);
  165. Move_Cursor (W, 1, Half);
  166. Put (W, (I), 1);
  167. Refresh_Without_Update (W);
  168. end loop;
  169. end;
  170. Update_Panels; Update_Screen;
  171. loop
  172. K := Get_Key;
  173. if K in Special_Key_Code'Range then
  174. case K is
  175. when QUIT_CODE => exit;
  176. when HELP_CODE => Explain_Context;
  177. when EXPLAIN_CODE => Explain ("MOUSEKEYS");
  178. when Key_Mouse => Show_Mouse_Event;
  179. when others => null;
  180. end case;
  181. end if;
  182. end loop;
  183. for I in Ctl'Range loop
  184. W := Get_Window (Ctl (I));
  185. Clear (W);
  186. Delete (Ctl (I));
  187. Delete (W);
  188. end loop;
  189. Clear (Frame);
  190. Delete (Pan);
  191. Delete (Msg);
  192. Delete (Frame);
  193. Set_Cursor_Visibility (V);
  194. End_Mouse (Mask);
  195. Pop_Environment;
  196. Update_Panels; Update_Screen;
  197. end Demo;
  198. end Sample.Curses_Demo.Mouse;