mouse.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 2016-2017
  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. /*** public functions ****************************************************************************/
  57. /* --------------------------------------------------------------------------------------------- */
  58. /**
  59. * Translate GPM event to high-level event,
  60. *
  61. * @param w Widget object
  62. * @param event GPM event
  63. *
  64. * @return high level mouse event
  65. */
  66. mouse_event_t
  67. mouse_translate_event (Widget * w, Gpm_Event * event)
  68. {
  69. gboolean in_widget;
  70. mouse_msg_t msg = MSG_MOUSE_NONE;
  71. mouse_event_t local;
  72. /*
  73. * Very special widgets may want to control area outside their bounds.
  74. * For such widgets you will have to turn on the 'forced_capture' flag.
  75. * You'll also need, in your mouse handler, to inform the system of
  76. * events you want to pass on by setting 'event->result.abort' to TRUE.
  77. */
  78. in_widget = w->mouse.forced_capture || mouse_global_in_widget (event, w);
  79. if ((event->type & GPM_DOWN) != 0)
  80. {
  81. if (in_widget)
  82. {
  83. if ((event->buttons & GPM_B_UP) != 0)
  84. msg = MSG_MOUSE_SCROLL_UP;
  85. else if ((event->buttons & GPM_B_DOWN) != 0)
  86. msg = MSG_MOUSE_SCROLL_DOWN;
  87. else
  88. {
  89. /* Handle normal buttons: anything but the mouse wheel's.
  90. *
  91. * (Note that turning on capturing for the mouse wheel
  92. * buttons doesn't make sense as they don't generate a
  93. * mouse_up event, which means we'd never get uncaptured.)
  94. */
  95. w->mouse.capture = TRUE;
  96. msg = MSG_MOUSE_DOWN;
  97. w->mouse.last_buttons_down = event->buttons;
  98. }
  99. }
  100. }
  101. else if ((event->type & GPM_UP) != 0)
  102. {
  103. /* We trigger the mouse_up event even when !in_widget. That's
  104. * because, for example, a paint application should stop drawing
  105. * lines when the button is released even outside the canvas. */
  106. if (w->mouse.capture)
  107. {
  108. w->mouse.capture = FALSE;
  109. msg = MSG_MOUSE_UP;
  110. /*
  111. * When using xterm, event->buttons reports the buttons' state
  112. * after the event occurred (meaning that event->buttons is zero,
  113. * because the mouse button is now released). When using GPM,
  114. * however, that field reports the button(s) that was released.
  115. *
  116. * The following makes xterm behave effectively like GPM:
  117. */
  118. if (event->buttons == 0)
  119. event->buttons = w->mouse.last_buttons_down;
  120. }
  121. }
  122. else if ((event->type & GPM_DRAG) != 0)
  123. {
  124. if (w->mouse.capture)
  125. msg = MSG_MOUSE_DRAG;
  126. }
  127. else if ((event->type & GPM_MOVE) != 0)
  128. {
  129. if (in_widget)
  130. msg = MSG_MOUSE_MOVE;
  131. }
  132. init_mouse_event (&local, msg, event, w);
  133. return local;
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. /**
  137. * Call widget mouse handler to process high-level mouse event.
  138. *
  139. * Besides sending to the widget the event itself, this function may also
  140. * send one or more pseudo events. Currently, MSG_MOUSE_CLICK is the only
  141. * pseudo event in existence but in the future (e.g., with the introduction
  142. * of a drag-drop API) there may be more.
  143. *
  144. * @param w Widget object
  145. * @param event high level mouse event
  146. *
  147. * @return result of mouse event handling
  148. */
  149. int
  150. mouse_process_event (Widget * w, mouse_event_t * event)
  151. {
  152. int ret = MOU_UNHANDLED;
  153. if (event->msg != MSG_MOUSE_NONE)
  154. {
  155. w->mouse_callback (w, event->msg, event);
  156. /* If a widget aborts a MSG_MOUSE_DOWN, we uncapture it so it
  157. * doesn't steal events from other widgets. */
  158. if (event->msg == MSG_MOUSE_DOWN && event->result.abort)
  159. w->mouse.capture = FALSE;
  160. /* Upon releasing the mouse button: if the mouse hasn't been dragged
  161. * since the MSG_MOUSE_DOWN, we also trigger a click. */
  162. if (event->msg == MSG_MOUSE_UP && w->mouse.last_msg == MSG_MOUSE_DOWN)
  163. w->mouse_callback (w, MSG_MOUSE_CLICK, event);
  164. /* Record the current event type for the benefit of the next event. */
  165. w->mouse.last_msg = event->msg;
  166. if (!event->result.abort)
  167. ret = event->result.repeat ? MOU_REPEAT : MOU_NORMAL;
  168. }
  169. return ret;
  170. }
  171. /* --------------------------------------------------------------------------------------------- */