mouse.c 6.5 KB

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