Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
4.2KB

  1. // ----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2010-2012 Fons Adriaensen <fons@linuxaudio.org>
  4. // Modified by falkTX on Jan 2015 for inclusion in Carla
  5. //
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. //
  20. // ----------------------------------------------------------------------
  21. #include <cairo/cairo.h>
  22. #include <cairo/cairo-xlib.h>
  23. #include <math.h>
  24. #include "rotary.h"
  25. namespace AT1 {
  26. int RotaryCtl::_wb_up = 4;
  27. int RotaryCtl::_wb_dn = 5;
  28. int RotaryCtl::_keymod = 0;
  29. int RotaryCtl::_button = 0;
  30. int RotaryCtl::_rcount = 0;
  31. int RotaryCtl::_rx = 0;
  32. int RotaryCtl::_ry = 0;
  33. RotaryCtl::RotaryCtl (X_window *parent,
  34. X_callback *cbobj,
  35. int cbind,
  36. RotaryGeom *rgeom,
  37. int xp,
  38. int yp) :
  39. X_window (parent,
  40. rgeom->_x0 + xp, rgeom->_y0 + yp,
  41. rgeom->_dx, rgeom->_dy,
  42. rgeom->_backg->pixel),
  43. _cbobj (cbobj),
  44. _cbind (cbind),
  45. _rgeom (rgeom),
  46. _state (0),
  47. _count (0),
  48. _value (0),
  49. _angle (0)
  50. {
  51. x_add_events ( ExposureMask
  52. | Button1MotionMask | ButtonPressMask | ButtonReleaseMask);
  53. _cairo->initIfNeeded(parent->disp());
  54. _cairotype = _cairo->type;
  55. _cairosurf = _cairo->surf;
  56. }
  57. RotaryCtl::~RotaryCtl (void)
  58. {
  59. }
  60. void RotaryCtl::handle_event (XEvent *E)
  61. {
  62. switch (E->type)
  63. {
  64. case Expose:
  65. render ();
  66. break;
  67. case ButtonPress:
  68. bpress ((XButtonEvent *) E);
  69. break;
  70. case ButtonRelease:
  71. brelse ((XButtonEvent *) E);
  72. break;
  73. case MotionNotify:
  74. motion ((XMotionEvent *) E);
  75. break;
  76. default:
  77. fprintf (stderr, "RotaryCtl: event %d\n", E->type );
  78. }
  79. }
  80. void RotaryCtl::bpress (XButtonEvent *E)
  81. {
  82. int r = 0;
  83. double d;
  84. d = hypot (E->x - _rgeom->_xref, E->y - _rgeom->_yref);
  85. if (d > _rgeom->_rad + 3) return;
  86. _keymod = E->state;
  87. if (E->button < 4)
  88. {
  89. _rx = E->x;
  90. _ry = E->y;
  91. _button = E->button;
  92. r = handle_button ();
  93. _rcount = _count;
  94. }
  95. else if (_button) return;
  96. else if ((int)E->button == _wb_up)
  97. {
  98. r = handle_mwheel (1);
  99. }
  100. else if ((int)E->button == _wb_dn)
  101. {
  102. r = handle_mwheel (-1);
  103. }
  104. if (r)
  105. {
  106. callback (r);
  107. render ();
  108. }
  109. }
  110. void RotaryCtl::brelse (XButtonEvent *E)
  111. {
  112. if (_button == (int)E->button)
  113. {
  114. _button = 0;
  115. callback (RELSE);
  116. }
  117. }
  118. void RotaryCtl::motion (XMotionEvent *E)
  119. {
  120. int dx, dy, r;
  121. if (_button)
  122. {
  123. _keymod = E->state;
  124. dx = E->x - _rx;
  125. dy = E->y - _ry;
  126. r = handle_motion (dx, dy);
  127. if (r)
  128. {
  129. callback (r);
  130. render ();
  131. }
  132. }
  133. }
  134. void RotaryCtl::set_state (int s)
  135. {
  136. if (_state != s)
  137. {
  138. _state = s;
  139. render ();
  140. }
  141. }
  142. void RotaryCtl::render (void)
  143. {
  144. XImage *I;
  145. double a, c, r, x, y;
  146. I = _rgeom->_image [_state];
  147. XPutImage (dpy (), win (), dgc (), I,
  148. _rgeom->_x0, _rgeom->_y0, 0, 0, _rgeom->_dx, _rgeom->_dy);
  149. cairo_xlib_surface_set_drawable (_cairosurf, win(),
  150. _rgeom->_dx, _rgeom->_dy);
  151. c = _rgeom->_lncol [_state] ? 1.0 : 0.0;
  152. a = _angle * M_PI / 180;
  153. r = _rgeom->_rad;
  154. x = _rgeom->_xref;
  155. y = _rgeom->_yref;
  156. cairo_new_path (_cairotype);
  157. cairo_move_to (_cairotype, x, y);
  158. x += r * sin (a);
  159. y -= r * cos (a);
  160. cairo_line_to (_cairotype, x, y);
  161. cairo_set_source_rgb (_cairotype, c, c, c);
  162. cairo_set_line_width (_cairotype, 2.2);
  163. cairo_stroke (_cairotype);
  164. }
  165. }