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.

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