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.

333 lines
6.7KB

  1. /* Event Widget
  2. * Copyleft (C) 2001 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "Fl_SEvent.h"
  19. #include "Fl_EventMap.h"
  20. #include <FL/fl_draw.h>
  21. #include <FL/Fl_Window.h>
  22. #include <FL/x.H>
  23. #include <stdio.h>
  24. static const int MELODY_WIDGET_COL = 142;
  25. static const int PERCUSSION_WIDGET_COL = 140;
  26. Fl_SEvent::Fl_SEvent(int x, int y, int w, int h, const char* label) :
  27. Fl_Button(x,y,w,h,label),
  28. m_Type(NO_TYPE),
  29. m_LastState(false),
  30. m_FirstUpdate(false),
  31. m_LastUpdate(false),
  32. m_Name("No Name"),
  33. m_ID(-1),
  34. m_GridX(0),
  35. m_GridY(0),
  36. m_LockX(false),
  37. m_LockY(false),
  38. m_LockResize(true),
  39. m_ResizeGrab(8),
  40. m_PixelsPerSec(100),
  41. m_StartTime(0.0f),
  42. m_LengthTime(0.0f),
  43. m_SnapGap(0.0f),
  44. m_CurrentDragMode(NONE),
  45. m_DelMe(false)
  46. {
  47. Fl_Button::label(m_Name.c_str());
  48. }
  49. Fl_SEvent::Fl_SEvent(const Fl_SEvent &Other) :
  50. Fl_Button(Other.x(),Other.y(),Other.w(),Other.h(),Other.label()),
  51. m_Type(Other.m_Type),
  52. m_LastState(Other.m_LastState),
  53. m_FirstUpdate(Other.m_FirstUpdate),
  54. m_LastUpdate(Other.m_LastUpdate),
  55. m_Name(Other.m_Name),
  56. m_ID(Other.m_ID),
  57. m_GridX(Other.m_GridX),
  58. m_GridY(Other.m_GridY),
  59. m_LockX(Other.m_LockX),
  60. m_LockY(Other.m_LockY),
  61. m_LockResize(Other.m_LockResize),
  62. m_ResizeGrab(Other.m_ResizeGrab),
  63. m_PixelsPerSec(Other.m_PixelsPerSec),
  64. m_StartTime(Other.m_StartTime),
  65. m_LengthTime(Other.m_LengthTime),
  66. m_SnapGap(Other.m_SnapGap),
  67. m_CurrentDragMode(Other.m_CurrentDragMode),
  68. m_DelMe(false),
  69. LastButtonPushed(0)
  70. {
  71. }
  72. Fl_SEvent::~Fl_SEvent()
  73. {
  74. }
  75. bool Fl_SEvent::UpdateState(float Time)
  76. {
  77. m_FirstUpdate=false;
  78. m_LastUpdate=false;
  79. bool State=(Time>m_StartTime && Time<m_StartTime+m_LengthTime);
  80. if (State && !m_LastState) m_FirstUpdate=true;
  81. if (!State && m_LastState) m_LastUpdate=true;
  82. m_LastState=State;
  83. return State;
  84. }
  85. void Fl_SEvent::draw()
  86. {
  87. // check if clipped
  88. //if (fl_clip()) return;
  89. // clamp width
  90. if (w()<5) w(5);
  91. Fl_EventMap* P=(Fl_EventMap*)(parent());
  92. if (P->GetType()==Fl_EventMap::MELODY_MAP)
  93. {
  94. int Col=FL_BLACK;
  95. if (parent()) Col=((Fl_EventMap*)(parent()))->GetKeyColMap()[GetGroup()%12];
  96. fl_color(Col);
  97. fl_rectf(x()+1,y()+1,w()-1,h()-1);
  98. // draw shadow
  99. fl_color(FL_BLACK);
  100. fl_line(x()+w(),y()+1,x()+w(),y()+h());
  101. fl_line(x()+w(),y()+h(),x()+1,y()+h());
  102. }
  103. else
  104. {
  105. if (m_Type==MELODY) fl_color(MELODY_WIDGET_COL);
  106. else fl_color(PERCUSSION_WIDGET_COL);
  107. fl_rectf(x()+1,y()+1,w()-1,h()-1);
  108. // draw shadow
  109. fl_color(FL_BLACK);
  110. fl_line(x()+w(),y()+1,x()+w(),y()+h());
  111. fl_line(x()+w(),y()+h(),x()+1,y()+h());
  112. fl_push_clip(x()+1,y()+1,w()-1,h()-1);
  113. fl_font(fl_font(), 10);
  114. fl_draw(m_Name.c_str(),x()+2,y()+h()-2);
  115. fl_pop_clip();
  116. }
  117. // todo: sample rendering
  118. }
  119. int Fl_SEvent::handle(int event)
  120. {
  121. int mx=Fl::event_x();
  122. int my=Fl::event_y();
  123. static int offsx,offsy;
  124. switch (event)
  125. {
  126. case FL_PUSH:
  127. LastButtonPushed=Fl::event_button();
  128. offsx=mx-x();
  129. offsy=my-y();
  130. if (Fl::event_key(FL_BackSpace))
  131. {
  132. Fl_EventMap *p = (Fl_EventMap*)parent();
  133. if (p && p->GetType()!=Fl_EventMap::ARRANGE_MAP)
  134. {
  135. m_DelMe=true;
  136. }
  137. }
  138. if (LastButtonPushed==1)
  139. {
  140. // if the last EVENT_RESIZE_GRAB pixels
  141. // have been grabbed, resize.
  142. /*if(!m_LockResize && mx>x()+w()-m_ResizeGrab && mx<x()+w())
  143. {
  144. m_CurrentDragMode=RESIZING;
  145. }
  146. else
  147. {*/
  148. m_CurrentDragMode=MOVING;
  149. //}
  150. }
  151. if (LastButtonPushed==2)
  152. {
  153. // copy to end
  154. Fl_EventMap *p = (Fl_EventMap*)parent();
  155. if (p) p->CopyEvent(x()+w(),y(),w(),m_ID,m_LengthTime);
  156. }
  157. if (LastButtonPushed==3)
  158. {
  159. m_CurrentDragMode=RESIZING;
  160. }
  161. // fall through
  162. case FL_DRAG:
  163. {
  164. if (LastButtonPushed==1 || LastButtonPushed==3)
  165. {
  166. if(m_CurrentDragMode==RESIZING)
  167. {
  168. w(mx-x()+(m_ResizeGrab/2));
  169. m_LengthTime=w()/(float)m_PixelsPerSec;
  170. }
  171. if(m_CurrentDragMode==MOVING)
  172. {
  173. x(mx-offsx);
  174. y(my-offsy);
  175. m_StartTime=(x()-GetParentX())/(float)m_PixelsPerSec;
  176. }
  177. my=y();
  178. SnapY();
  179. parent()->redraw();
  180. }
  181. }
  182. break;
  183. case FL_RELEASE:
  184. {
  185. m_CurrentDragMode=NONE;
  186. }
  187. break;
  188. }
  189. return event;
  190. }
  191. void Fl_SEvent::SetPixelsPerSec(int s, bool FirstTime)
  192. {
  193. m_PixelsPerSec=s;
  194. if (FirstTime)
  195. {
  196. m_StartTime=(x()-GetParentX())/(float)m_PixelsPerSec;
  197. m_LengthTime=w()/(float)m_PixelsPerSec;
  198. }
  199. else
  200. {
  201. // convert back to new value
  202. //get relative x
  203. int rel_x=x()-GetParentX();
  204. rel_x=(int)(m_StartTime*m_PixelsPerSec);
  205. x(rel_x+GetParentX());
  206. w((int)(m_LengthTime*m_PixelsPerSec));
  207. m_GridX=(int)(m_SnapGap*(float)m_PixelsPerSec);
  208. }
  209. }
  210. int Fl_SEvent::GetParentX()
  211. {
  212. Fl_EventMap* P=(Fl_EventMap*)(parent());
  213. if (P)
  214. {
  215. return P->GetX();
  216. }
  217. else
  218. {
  219. return 0;
  220. }
  221. }
  222. int Fl_SEvent::GetParentY()
  223. {
  224. Fl_EventMap* P=(Fl_EventMap*)(parent());
  225. if (P)
  226. {
  227. return P->GetY();
  228. }
  229. else
  230. {
  231. return 0;
  232. }
  233. }
  234. ////////////////////////////////////////////////////////////////////
  235. Fl_TriEvent::Fl_TriEvent(int x, int y, int w, int h, const char* label) :
  236. Fl_SEvent(x,y,w,h,label)
  237. {
  238. }
  239. Fl_TriEvent::Fl_TriEvent(const Fl_TriEvent &Other) :
  240. Fl_SEvent(Other)
  241. {
  242. }
  243. void Fl_TriEvent::draw()
  244. {
  245. // hackish way to override the zoon scale of the width
  246. w(10);
  247. h(1000);
  248. fl_color(FL_BLACK);
  249. //fl_line(x(),y(),x()+10,y());
  250. //fl_line(x()+10,y(),x(),y()+10);
  251. //fl_line(x(),y()+10,x(),y());
  252. fl_line(x(),y(),x(),y()+1000);
  253. }
  254. ////////////////////////////////////////////////////////////////////
  255. Fl_CircEvent::Fl_CircEvent(int x, int y, int w, int h, const char* label) :
  256. Fl_SEvent(x,y,12,h,label)
  257. {
  258. }
  259. Fl_CircEvent::Fl_CircEvent(const Fl_CircEvent &Other) :
  260. Fl_SEvent(Other)
  261. {
  262. }
  263. void Fl_CircEvent::draw()
  264. {
  265. // hackish way to override the zoom scale of the width
  266. w(12);
  267. int Col=0;
  268. if (parent()) Col=((Fl_EventMap*)(parent()))->GetKeyColMap()[GetGroup()%12];
  269. fl_color(Col);
  270. fl_rectf(x(),y()+1,12,12);
  271. fl_color(FL_BLACK);
  272. fl_line(x()+1,y()+13, x()+13,y()+13);
  273. fl_line(x()+12,y()+13, x()+12,y()+1);
  274. // fl_pie(x(),y(),12,12,0,360);
  275. }