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.

265 lines
5.7KB

  1. // generated by Fast Light User Interface Designer (fluid) version 1.0107f
  2. #include "WidgetPDial.h"
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <cmath>
  6. #include <string>
  7. #include <FL/Fl_Tooltip.H>
  8. #include <FL/fl_draw.H>
  9. #include <FL/Fl_Group.H>
  10. #include <FL/Fl_Menu_Window.H>
  11. #include "../Misc/Util.h"
  12. //Copyright (c) 2003-2005 Nasca Octavian Paul
  13. //License: GNU GPL version 2 or later
  14. using namespace std;
  15. class TipWin:public Fl_Menu_Window
  16. {
  17. public:
  18. TipWin();
  19. void draw();
  20. void showValue(float f);
  21. void setText(const char *c);
  22. void showText();
  23. private:
  24. void redraw();
  25. const char *getStr() const;
  26. string tip;
  27. string text;
  28. bool textmode;
  29. };
  30. TipWin::TipWin():Fl_Menu_Window(1, 1)
  31. {
  32. set_override();
  33. end();
  34. }
  35. void TipWin::draw()
  36. {
  37. //setup window
  38. draw_box(FL_BORDER_BOX, 0, 0, w(), h(), Fl_Color(175));
  39. fl_color(Fl_Tooltip::textcolor());
  40. fl_font(labelfont(), labelsize());
  41. //Draw the current string
  42. fl_draw(getStr(), 3, 3, w() - 6, h() - 6,
  43. Fl_Align(FL_ALIGN_LEFT | FL_ALIGN_WRAP));
  44. }
  45. void TipWin::showValue(float f)
  46. {
  47. //convert the value to a string
  48. char tmp[10];
  49. snprintf(tmp, 9, "%.2f", f);
  50. tip = tmp;
  51. textmode = false;
  52. redraw();
  53. show();
  54. }
  55. void TipWin::setText(const char *c)
  56. {
  57. text = c;
  58. textmode = true;
  59. redraw();
  60. }
  61. void TipWin::showText()
  62. {
  63. if(!text.empty()) {
  64. textmode = true;
  65. redraw();
  66. show();
  67. }
  68. }
  69. void TipWin::redraw()
  70. {
  71. // Recalc size of window
  72. fl_font(labelfont(), labelsize());
  73. int W = 0, H = 0;
  74. fl_measure(getStr(), W, H, 0);
  75. //provide a bit of extra space
  76. W += 8;
  77. H += 4;
  78. size(W, H);
  79. Fl_Menu_Window::redraw();
  80. }
  81. const char *TipWin::getStr() const
  82. {
  83. return (textmode ? text : tip).c_str();
  84. }
  85. //static int numobj = 0;
  86. WidgetPDial::WidgetPDial(int x, int y, int w, int h, const char *label)
  87. :Fl_Dial(x, y, w, h, label), oldvalue(0.0f), pos(false), textset(false)
  88. {
  89. //cout << "[" << label << "] There are now " << ++numobj << endl;
  90. Fl_Group *save = Fl_Group::current();
  91. tipwin = new TipWin();
  92. tipwin->hide();
  93. Fl_Group::current(save);
  94. }
  95. WidgetPDial::~WidgetPDial()
  96. {
  97. //cout << "There are now " << --numobj << endl;
  98. delete tipwin;
  99. }
  100. int WidgetPDial::handle(int event)
  101. {
  102. //#ifdef NTK_GUI
  103. // return Fl_Dial::handle( event );
  104. //#else
  105. double dragsize, min = minimum(), max = maximum();
  106. int my;
  107. switch(event) {
  108. case FL_PUSH:
  109. oldvalue = value();
  110. case FL_DRAG:
  111. getPos();
  112. my = -(Fl::event_y() - y() - h() / 2);
  113. dragsize = 200.0f;
  114. if(Fl::event_state(FL_BUTTON1) == 0)
  115. dragsize *= 10;
  116. value(limit(oldvalue + my / dragsize * (max - min), min, max));
  117. tipwin->showValue(value());
  118. value_damage();
  119. if(this->when() != 0)
  120. do_callback();
  121. return 1;
  122. case FL_MOUSEWHEEL:
  123. if (Fl::belowmouse() != this)
  124. return 1;
  125. my = - Fl::event_dy();
  126. dragsize = 200.0f;
  127. if(Fl::event_state(FL_CTRL) != 0)
  128. dragsize *= 10;
  129. value(limit(value() + my / dragsize * (max - min), min, max));
  130. tipwin->showValue(value());
  131. value_damage();
  132. if(this->when() != 0)
  133. do_callback();
  134. return 1;
  135. case FL_ENTER:
  136. getPos();
  137. tipwin->showText();
  138. return 1;
  139. case FL_HIDE:
  140. case FL_LEAVE:
  141. tipwin->hide();
  142. resetPos();
  143. break;
  144. case FL_RELEASE:
  145. tipwin->hide();
  146. resetPos();
  147. if(this->when() == 0)
  148. do_callback();
  149. return 1;
  150. break;
  151. }
  152. return 0;
  153. //#endif
  154. }
  155. void WidgetPDial::draw()
  156. {
  157. #ifdef NTK_GUI
  158. box( FL_NO_BOX );
  159. Fl_Dial::draw();
  160. return;
  161. #else
  162. const int cx = x(), cy = y(), sx = w(), sy = h();
  163. const double a1 = angle1(), a2 = angle2();
  164. const double val = (value() - minimum()) / (maximum() - minimum());
  165. // even radius produces less artifacts if no antialiasing is avail
  166. const int rad = (sx > sy ? sy : sx) &~1;
  167. /* clears the button background */
  168. pdialcolor(160, 160, 160);
  169. fl_pie(cx - 2, cy - 2, rad + 4, rad + 4, 0, 360);
  170. /* dark outline */
  171. fl_color(60, 60, 60);
  172. fl_pie(cx - 1, cy - 1, rad + 2, rad + 2, 0, 360);
  173. /* Draws the button faceplate, min/max */
  174. pdialcolor(110, 110, 115);
  175. fl_pie(cx, cy, rad, rad, 270 - a2, 270 - a1);
  176. /* knob center */
  177. if (rad > 8) {
  178. pdialcolor(140, 140, 145);
  179. fl_pie(cx + 4, cy + 4, rad - 8, rad - 8, 0, 360);
  180. }
  181. /* value circle */
  182. double a = -(a2 - a1) * val - a1;
  183. fl_line_style(0, 2, 0);
  184. pdialcolor(0, 200, 0);
  185. fl_arc(cx + 1, cy + 1, rad - 2, rad - 2, a - 90, a1 - 180);
  186. fl_line_style(0);
  187. /* draw value line */
  188. int ll = rad/4;
  189. if (ll < 2) ll = 2;
  190. fl_push_matrix();
  191. fl_translate(cx + rad / 2, cy + rad / 2);
  192. fl_rotate(a - 90.0f);
  193. fl_translate(rad / 2, 0);
  194. fl_begin_polygon();
  195. pdialcolor(0, 0, 0);
  196. fl_vertex(-ll, 0);
  197. fl_vertex(0, 0);
  198. fl_end_polygon();
  199. fl_pop_matrix();
  200. #endif
  201. }
  202. void WidgetPDial::pdialcolor(int r, int g, int b)
  203. {
  204. if(active_r())
  205. fl_color(r, g, b);
  206. else
  207. fl_color(160 - (160 - r) / 3, 160 - (160 - b) / 3, 160 - (160 - b) / 3);
  208. }
  209. void WidgetPDial::tooltip(const char *c)
  210. {
  211. tipwin->setText(c);
  212. textset = true;
  213. }
  214. void WidgetPDial::getPos()
  215. {
  216. if(!pos) {
  217. tipwin->position(Fl::event_x_root(), Fl::event_y_root() + 20);
  218. pos = true;
  219. }
  220. }
  221. void WidgetPDial::resetPos()
  222. {
  223. pos = false;
  224. }