mouse.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 2016-2021
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Human beings.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file mouse.c
  20. * \brief Header: High-level mouse API
  21. */
  22. #include <config.h>
  23. #include "lib/global.h"
  24. #include "lib/widget.h"
  25. #include "lib/widget/mouse.h"
  26. /*** global variables ****************************************************************************/
  27. /*** file scope macro definitions ****************************************************************/
  28. /*** file scope type declarations ****************************************************************/
  29. /*** file scope variables ************************************************************************/
  30. /* --------------------------------------------------------------------------------------------- */
  31. /*** file scope functions ************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. /**
  34. * Constructs a mouse event structure.
  35. *
  36. * It receives a Gpm_Event event and translates it into a higher level protocol.
  37. *
  38. * Tip: for details on the C mouse API, see MC's lib/tty/mouse.h,
  39. * or GPM's excellent 'info' manual:
  40. *
  41. * http://www.fifi.org/cgi-bin/info2www?(gpm)Event+Types
  42. */
  43. static void
  44. init_mouse_event (mouse_event_t * event, mouse_msg_t msg, const Gpm_Event * global_gpm,
  45. const Widget * w)
  46. {
  47. event->msg = msg;
  48. event->x = global_gpm->x - w->x - 1; /* '-1' because Gpm_Event is 1-based. */
  49. event->y = global_gpm->y - w->y - 1;
  50. event->count = global_gpm->type & (GPM_SINGLE | GPM_DOUBLE | GPM_TRIPLE);
  51. event->buttons = global_gpm->buttons;
  52. event->result.abort = FALSE;
  53. event->result.repeat = FALSE;
  54. }
  55. /* --------------------------------------------------------------------------------------------- */
  56. /**
  57. * Translate GPM event to high-level event,
  58. *
  59. * @param w Widget object
  60. * @param event GPM event
  61. *
  62. * @return high level mouse event
  63. */
  64. static mouse_event_t
  65. mouse_translate_event (Widget * w, Gpm_Event * event)
  66. {
  67. gboolean in_widget;
  68. mouse_msg_t msg = MSG_MOUSE_NONE;
  69. mouse_event_t local;
  70. /*
  71. * Very special widgets may want to control area outside their bounds.
  72. * For such widgets you will have to turn on the 'forced_capture' flag.
  73. * You'll also need, in your mouse handler, to inform the system of
  74. * events you want to pass on by setting 'event->result.abort' to TRUE.
  75. */
  76. in_widget = w->mouse.forced_capture || mouse_global_in_widget (event, w);
  77. if ((event->type & GPM_DOWN) != 0)
  78. {
  79. if (in_widget)
  80. {
  81. if ((event->buttons & GPM_B_UP) != 0)
  82. msg = MSG_MOUSE_SCROLL_UP;
  83. else if ((event->buttons & GPM_B_DOWN) != 0)
  84. msg = MSG_MOUSE_SCROLL_DOWN;
  85. else
  86. {
  87. /* Handle normal buttons: anything but the mouse wheel's.
  88. *
  89. * (Note that turning on capturing for the mouse wheel
  90. * buttons doesn't make sense as they don't generate a
  91. * mouse_up event, which means we'd never get uncaptured.)
  92. */
  93. w->mouse.capture = TRUE;
  94. msg = MSG_MOUSE_DOWN;
  95. w->mouse.last_buttons_down = event->buttons;
  96. }
  97. }
  98. }
  99. else if ((event->type & GPM_UP) != 0)
  100. {
  101. /* We trigger the mouse_up event even when !in_widget. That's
  102. * because, for example, a paint application should stop drawing
  103. * lines when the button is released even outside the canvas. */
  104. if (w->mouse.capture)
  105. {
  106. w->mouse.capture = FALSE;
  107. msg = MSG_MOUSE_UP;
  108. /*
  109. * When using xterm, event->buttons reports the buttons' state
  110. * after the event occurred (meaning that event->buttons is zero,
  111. * because the mouse button is now released). When using GPM,
  112. * however, that field reports the button(s) that was released.
  113. *
  114. * The following makes xterm behave effectively like GPM:
  115. */
  116. if (event->buttons == 0)
  117. event->buttons = w->mouse.last_buttons_down;
  118. }
  119. }
  120. else if ((event->type & GPM_DRAG) != 0)
  121. {
  122. if (w->mouse.capture)
  123. msg = MSG_MOUSE_DRAG;
  124. }
  125. else if ((event->type & GPM_MOVE) != 0)
  126. {
  127. if (in_widget)
  128. msg = MSG_MOUSE_MOVE;
  129. }
  130. init_mouse_event (&local, msg, event, w);
  131. return local;
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. /**
  135. * Call widget mouse handler to process high-level mouse event.
  136. *
  137. * Besides sending to the widget the event itself, this function may also
  138. * send one or more pseudo events. Currently, MSG_MOUSE_CLICK is the only
  139. * pseudo event in existence but in the future (e.g., with the introduction
  140. * of a drag-drop API) there may be more.
  141. *
  142. * @param w Widget object
  143. * @param event high level mouse event
  144. *
  145. * @return result of mouse event handling
  146. */
  147. static int
  148. mouse_process_event (Widget * w, mouse_event_t * event)
  149. {
  150. int ret = MOU_UNHANDLED;
  151. if (event->msg != MSG_MOUSE_NONE)
  152. {
  153. w->mouse_callback (w, event->msg, event);
  154. /* If a widget aborts a MSG_MOUSE_DOWN, we uncapture it so it
  155. * doesn't steal events from other widgets. */
  156. if (event->msg == MSG_MOUSE_DOWN && event->result.abort)
  157. w->mouse.capture = FALSE;
  158. /* Upon releasing the mouse button: if the mouse hasn't been dragged
  159. * since the MSG_MOUSE_DOWN, we also trigger a click. */
  160. if (event->msg == MSG_MOUSE_UP && w->mouse.last_msg == MSG_MOUSE_DOWN)
  161. w->mouse_callback (w, MSG_MOUSE_CLICK, event);
  162. /* Record the current event type for the benefit of the next event. */
  163. w->mouse.last_msg = event->msg;
  164. if (!event->result.abort)
  165. ret = event->result.repeat ? MOU_REPEAT : MOU_NORMAL;
  166. }
  167. return ret;
  168. }
  169. /* --------------------------------------------------------------------------------------------- */
  170. /*** public functions ****************************************************************************/
  171. /* --------------------------------------------------------------------------------------------- */
  172. /**
  173. * Translate GPM event to high-level event and process it
  174. *
  175. * @param w Widget object
  176. * @param event GPM event
  177. *
  178. * @return result of mouse event handling
  179. */
  180. int
  181. mouse_handle_event (Widget * w, Gpm_Event * event)
  182. {
  183. mouse_event_t me;
  184. me = mouse_translate_event (w, event);
  185. return mouse_process_event (w, &me);
  186. }
  187. /* --------------------------------------------------------------------------------------------- */