mouse.c 7.5 KB

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