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.

352 lines
7.2KB

  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_Group(x,y,w,h,""),
  28. m_Type(NO_TYPE),
  29. m_LastState(false),
  30. m_FirstUpdate(false),
  31. m_LastUpdate(false),
  32. m_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_Group::label(m_Name.c_str());
  48. labelsize(8);
  49. m_Menu = new Fl_Menu_Button(x,y,w,h,"");
  50. m_Menu->type(Fl_Menu_Button::POPUP2);
  51. m_Menu->textsize(8);
  52. m_Menu->add("copy", 0, NULL);
  53. m_Menu->add("instance", 0, NULL);
  54. m_Menu->add("edit", 0, NULL);
  55. m_Menu->add("delete", 0, NULL);
  56. add(m_Menu);
  57. }
  58. Fl_SEvent::Fl_SEvent(const Fl_SEvent &Other) :
  59. Fl_Group(Other.x(),Other.y(),Other.w(),Other.h(),Other.label()),
  60. m_Type(Other.m_Type),
  61. m_LastState(Other.m_LastState),
  62. m_FirstUpdate(Other.m_FirstUpdate),
  63. m_LastUpdate(Other.m_LastUpdate),
  64. m_Name(Other.m_Name),
  65. m_ID(Other.m_ID),
  66. m_GridX(Other.m_GridX),
  67. m_GridY(Other.m_GridY),
  68. m_LockX(Other.m_LockX),
  69. m_LockY(Other.m_LockY),
  70. m_LockResize(Other.m_LockResize),
  71. m_ResizeGrab(Other.m_ResizeGrab),
  72. m_PixelsPerSec(Other.m_PixelsPerSec),
  73. m_StartTime(Other.m_StartTime),
  74. m_LengthTime(Other.m_LengthTime),
  75. m_SnapGap(Other.m_SnapGap),
  76. m_CurrentDragMode(Other.m_CurrentDragMode),
  77. m_DelMe(false),
  78. LastButtonPushed(0)
  79. {
  80. }
  81. Fl_SEvent::~Fl_SEvent()
  82. {
  83. }
  84. bool Fl_SEvent::UpdateState(float Time)
  85. {
  86. m_FirstUpdate=false;
  87. m_LastUpdate=false;
  88. bool State=(Time>m_StartTime && Time<m_StartTime+m_LengthTime);
  89. if (State && !m_LastState) m_FirstUpdate=true;
  90. if (!State && m_LastState) m_LastUpdate=true;
  91. m_LastState=State;
  92. return State;
  93. }
  94. void Fl_SEvent::draw()
  95. {
  96. Fl_Group::draw();
  97. // check if clipped
  98. //if (fl_clip()) return;
  99. // clamp width
  100. if (w()<5) w(5);
  101. Fl_EventMap* P=(Fl_EventMap*)(parent());
  102. if (P->GetType()==Fl_EventMap::MELODY_MAP)
  103. {
  104. int Col=FL_BLACK;
  105. if (parent()) Col=((Fl_EventMap*)(parent()))->GetKeyColMap()[GetGroup()%12];
  106. fl_color(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. }
  113. else
  114. {
  115. if (m_Type==MELODY) fl_color(MELODY_WIDGET_COL);
  116. else fl_color(PERCUSSION_WIDGET_COL);
  117. fl_rectf(x()+1,y()+1,w()-1,h()-1);
  118. // draw shadow
  119. fl_color(FL_BLACK);
  120. fl_line(x()+w(),y()+1,x()+w(),y()+h());
  121. fl_line(x()+w(),y()+h(),x()+1,y()+h());
  122. fl_push_clip(x()+1,y()+1,w()-1,h()-1);
  123. fl_font(fl_font(), 8);
  124. fl_draw(m_Name.c_str(),x()+2,y()+h()-2);
  125. fl_pop_clip();
  126. }
  127. // todo: sample rendering
  128. }
  129. int Fl_SEvent::handle(int event)
  130. {
  131. int mx=Fl::event_x();
  132. int my=Fl::event_y();
  133. static int offsx,offsy;
  134. if (Fl::event_button()==1 && event==FL_PUSH && Fl::event_clicks()==1)
  135. {
  136. // a bit crap I suppose
  137. if (((Fl_EventMap*)parent())->cb_EventDoubleClicked!=NULL)
  138. {
  139. ((Fl_EventMap*)parent())->cb_EventDoubleClicked(this,NULL);
  140. }
  141. }
  142. switch (event)
  143. {
  144. case FL_PUSH:
  145. LastButtonPushed=Fl::event_button();
  146. offsx=mx-x();
  147. offsy=my-y();
  148. if (Fl::event_key(FL_BackSpace))
  149. {
  150. Fl_EventMap *p = (Fl_EventMap*)parent();
  151. if (p && p->GetType()!=Fl_EventMap::ARRANGE_MAP)
  152. {
  153. m_DelMe=true;
  154. }
  155. }
  156. if (LastButtonPushed==1)
  157. {
  158. // if the last EVENT_RESIZE_GRAB pixels
  159. // have been grabbed, resize.
  160. m_CurrentDragMode=MOVING;
  161. //}
  162. }
  163. /*if (LastButtonPushed==2)
  164. {
  165. // copy to end
  166. Fl_EventMap *p = (Fl_EventMap*)parent();
  167. if (p) p->CopyEvent(x()+w(),y(),w(),m_ID,m_LengthTime);
  168. }*/
  169. if (LastButtonPushed==3)
  170. {
  171. m_CurrentDragMode=RESIZING;
  172. }
  173. // fall through
  174. case FL_DRAG:
  175. {
  176. if (LastButtonPushed==1 || LastButtonPushed==3)
  177. {
  178. if(m_CurrentDragMode==RESIZING)
  179. {
  180. w(mx-x()+(m_ResizeGrab/2));
  181. //m_Menu->size(w(),h());
  182. m_LengthTime=w()/(float)m_PixelsPerSec;
  183. }
  184. if(m_CurrentDragMode==MOVING)
  185. {
  186. x(mx-offsx);
  187. y(my-offsy);
  188. m_Menu->position(x(),y());
  189. m_StartTime=(x()-GetParentX())/(float)m_PixelsPerSec;
  190. }
  191. my=y();
  192. SnapY();
  193. parent()->redraw();
  194. }
  195. }
  196. break;
  197. case FL_RELEASE:
  198. {
  199. m_CurrentDragMode=NONE;
  200. }
  201. break;
  202. }
  203. Fl_Group::handle(event);
  204. return 1;
  205. }
  206. void Fl_SEvent::SetPixelsPerSec(int s, bool FirstTime)
  207. {
  208. m_PixelsPerSec=s;
  209. if (FirstTime)
  210. {
  211. m_StartTime=(x()-GetParentX())/(float)m_PixelsPerSec;
  212. m_LengthTime=w()/(float)m_PixelsPerSec;
  213. }
  214. else
  215. {
  216. // convert back to new value
  217. //get relative x
  218. int rel_x=x()-GetParentX();
  219. rel_x=(int)(m_StartTime*m_PixelsPerSec);
  220. x(rel_x+GetParentX());
  221. w((int)(m_LengthTime*m_PixelsPerSec));
  222. m_GridX=(int)(m_SnapGap*(float)m_PixelsPerSec);
  223. }
  224. }
  225. int Fl_SEvent::GetParentX()
  226. {
  227. Fl_EventMap* P=(Fl_EventMap*)(parent());
  228. if (P)
  229. {
  230. return P->GetX();
  231. }
  232. else
  233. {
  234. return 0;
  235. }
  236. }
  237. int Fl_SEvent::GetParentY()
  238. {
  239. Fl_EventMap* P=(Fl_EventMap*)(parent());
  240. if (P)
  241. {
  242. return P->GetY();
  243. }
  244. else
  245. {
  246. return 0;
  247. }
  248. }
  249. ////////////////////////////////////////////////////////////////////
  250. Fl_TriEvent::Fl_TriEvent(int x, int y, int w, int h, const char* label) :
  251. Fl_SEvent(x,y,w,h,label)
  252. {
  253. }
  254. Fl_TriEvent::Fl_TriEvent(const Fl_TriEvent &Other) :
  255. Fl_SEvent(Other)
  256. {
  257. }
  258. void Fl_TriEvent::draw()
  259. {
  260. // hackish way to override the zoon scale of the width
  261. w(10);
  262. h(1000);
  263. fl_color(FL_BLACK);
  264. //fl_line(x(),y(),x()+10,y());
  265. //fl_line(x()+10,y(),x(),y()+10);
  266. //fl_line(x(),y()+10,x(),y());
  267. fl_line(x(),y(),x(),y()+1000);
  268. }
  269. ////////////////////////////////////////////////////////////////////
  270. Fl_CircEvent::Fl_CircEvent(int x, int y, int w, int h, const char* label) :
  271. Fl_SEvent(x,y,12,h,label)
  272. {
  273. }
  274. Fl_CircEvent::Fl_CircEvent(const Fl_CircEvent &Other) :
  275. Fl_SEvent(Other)
  276. {
  277. }
  278. void Fl_CircEvent::draw()
  279. {
  280. // hackish way to override the zoom scale of the width
  281. w(12);
  282. int Col=0;
  283. if (parent()) Col=((Fl_EventMap*)(parent()))->GetKeyColMap()[GetGroup()%12];
  284. fl_color(Col);
  285. fl_rectf(x(),y()+1,12,12);
  286. fl_color(FL_BLACK);
  287. fl_line(x()+1,y()+13, x()+13,y()+13);
  288. fl_line(x()+12,y()+13, x()+12,y()+1);
  289. // fl_pie(x(),y(),12,12,0,360);
  290. }