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.

276 lines
6.5KB

  1. /* DeviceGUI composite Widget
  2. * Copyleft (C) 2002 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_DeviceGUI.h"
  19. #include "Fl_Canvas.h"
  20. #include "../../SpiralSynthModularInfo.h"
  21. int Fl_DeviceGUI::Numbers[512];
  22. Fl_PortButton::Fl_PortButton(int x, int y, int w, int h, char *n) :
  23. Fl_Button(x,y,w,h,n)
  24. {
  25. m_ConnectionCount=0;
  26. }
  27. int Fl_PortButton::handle(int event)
  28. {
  29. if (event==FL_PUSH)
  30. {
  31. m_LastButton=Fl::event_button();
  32. if (m_LastButton == 1)
  33. {
  34. if (m_Type==INPUT && value()) return 1;
  35. do_callback();
  36. return 1;
  37. }
  38. if (m_LastButton == 3)
  39. {
  40. do_callback();
  41. return 1;
  42. }
  43. }
  44. return 1;
  45. }
  46. Fl_DeviceGUI::Fl_DeviceGUI(const DeviceGUIInfo& Info, Fl_Group *PW, Fl_Pixmap *Icon, bool Terminal) :
  47. Fl_Group(Info.XPos, Info.YPos, Info.Width+(PortGroupWidth*2), Info.Height+TitleBarHeight, ""),
  48. m_PluginWindow(NULL),
  49. m_Icon(NULL),
  50. m_Name(Info.Name),
  51. m_ID(-1),
  52. m_DelMe(false),
  53. m_IsTerminal(Terminal)
  54. {
  55. for (int n=0; n<512; n++) Numbers[n]=n;
  56. type(1);
  57. box(FL_PLASTIC_UP_BOX);
  58. labeltype(FL_ENGRAVED_LABEL);
  59. align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
  60. color(SpiralSynthModularInfo::GUICOL_Device);
  61. m_Icon=Icon;
  62. m_DragBar = new Fl_DragBar(Info.XPos, Info.YPos, Info.Width+PortGroupWidth*2, TitleBarHeight, m_Name.c_str());
  63. m_DragBar->labelsize(10);
  64. m_DragBar->type(Fl_DragBar::FLDRAG);
  65. m_DragBar->color(SpiralSynthModularInfo::GUICOL_Device);
  66. m_PluginWindow = PW;
  67. //Add the input/output ports
  68. Setup(Info, true);
  69. }
  70. void Fl_DeviceGUI::Clear()
  71. {
  72. end();
  73. }
  74. int Fl_DeviceGUI::handle(int event)
  75. {
  76. int t=Fl_Group::handle(event);
  77. if (t==0 && Fl::belowmouse()==this)
  78. {
  79. if (event==FL_PUSH && Fl::event_button()==1)
  80. {
  81. if (m_PluginWindow && !m_DelMe)
  82. {
  83. if(!m_PluginWindow->visible())
  84. {
  85. m_PluginWindow->show();
  86. }
  87. /*else
  88. {
  89. m_PluginWindow->hide();
  90. }*/
  91. }
  92. }
  93. if (event==FL_RELEASE && Fl::event_button()==2)
  94. {
  95. m_DelMe = true;
  96. }
  97. }
  98. return t;
  99. }
  100. void Fl_DeviceGUI::draw()
  101. {
  102. Fl_Group::draw();
  103. if (m_Icon)
  104. {
  105. int Centx=x()+w()/2;
  106. int Centy=y()+h()/2;
  107. m_Icon->draw(Centx-m_Icon->w()/2,Centy-m_Icon->h()/2);
  108. }
  109. }
  110. void Fl_DeviceGUI::Setup(const DeviceGUIInfo& Info, bool FirstTime)
  111. {
  112. m_Info=Info;
  113. // Remove all current connections - it's the safest thing to do.
  114. if (parent() && !FirstTime)
  115. {
  116. ((Fl_Canvas*)(parent()))->ClearConnections(this);
  117. }
  118. // delete the current ports
  119. for(vector<Fl_PortButton*>::iterator i=m_PortVec.begin();
  120. i!=m_PortVec.end(); i++)
  121. {
  122. remove(*i);
  123. delete(*i);
  124. }
  125. m_PortVec.clear();
  126. int InputX=x()+2;
  127. int OutputX=x()+PortGroupWidth+Info.Width+4;
  128. int StartY=y()+TitleBarHeight;
  129. int PortDist=10;
  130. int PortNum=0;
  131. h(Info.Height+TitleBarHeight);
  132. for (int n=0; n<Info.NumInputs; n++)
  133. {
  134. Fl_PortButton* NewInput = new Fl_PortButton(InputX,StartY+PortDist*n,PortSize,PortSize,"");
  135. NewInput->type(1);
  136. NewInput->SetType(Fl_PortButton::INPUT);
  137. NewInput->value(false);
  138. NewInput->box(FL_ROUNDED_BOX);
  139. Fl_Color col = (Fl_Color) WIRE_COL0;
  140. switch (Info.PortTypes[n]) {
  141. case 0: col = (Fl_Color) WIRE_COL0;
  142. break;
  143. case 1: col = (Fl_Color) WIRE_COL1;
  144. break;
  145. case 2: col = (Fl_Color) WIRE_COL2;
  146. break;
  147. case 3: col = (Fl_Color) WIRE_COL3;
  148. break;
  149. case 4: col = (Fl_Color) WIRE_COL4;
  150. break;
  151. default: col = (Fl_Color) WIRE_COL0;
  152. }
  153. NewInput->selection_color(col);
  154. NewInput->down_box(FL_ROUNDED_BOX);
  155. NewInput->tooltip(Info.PortTips[n].c_str());
  156. NewInput->callback((Fl_Callback*)cb_Port,(void*)&Numbers[PortNum]);
  157. m_PortVec.push_back(NewInput);
  158. add(NewInput);
  159. PortNum++;
  160. }
  161. for (int n=0; n<Info.NumOutputs; n++)
  162. {
  163. Fl_PortButton* NewOutput = new Fl_PortButton(OutputX,StartY+PortDist*n,PortSize,PortSize,"");
  164. NewOutput->type(1);
  165. NewOutput->SetType(Fl_PortButton::OUTPUT);
  166. NewOutput->value(false);
  167. NewOutput->box(FL_ROUNDED_BOX);
  168. Fl_Color col = (Fl_Color) WIRE_COL0;
  169. switch (Info.PortTypes[n+Info.NumInputs]) {
  170. case 0: col = (Fl_Color) WIRE_COL0;
  171. break;
  172. case 1: col = (Fl_Color) WIRE_COL1;
  173. break;
  174. case 2: col = (Fl_Color) WIRE_COL2;
  175. break;
  176. case 3: col = (Fl_Color) WIRE_COL3;
  177. break;
  178. case 4: col = (Fl_Color) WIRE_COL4;
  179. break;
  180. default: col = (Fl_Color) WIRE_COL0;
  181. }
  182. NewOutput->selection_color(col);
  183. NewOutput->down_box(FL_ROUNDED_BOX);
  184. NewOutput->tooltip(Info.PortTips[n+Info.NumInputs].c_str());
  185. NewOutput->callback((Fl_Callback*)cb_Port,(void*)&Numbers[PortNum]);
  186. m_PortVec.push_back(NewOutput);
  187. add(NewOutput);
  188. PortNum++;
  189. }
  190. }
  191. bool Fl_DeviceGUI::AddConnection(int n)
  192. {
  193. if ( n < m_PortVec.size() )
  194. {
  195. m_PortVec[n]->Add();
  196. m_PortVec[n]->value(1);
  197. redraw();
  198. return true;
  199. }
  200. return false;
  201. }
  202. void Fl_DeviceGUI::RemoveConnection(int n)
  203. {
  204. m_PortVec[n]->Remove();
  205. if (!m_PortVec[n]->GetCount())
  206. {
  207. m_PortVec[n]->value(0);
  208. redraw();
  209. }
  210. }
  211. inline void Fl_DeviceGUI::cb_Port_i(Fl_Button* o, void* v)
  212. {
  213. int Port=*(int*)(v);
  214. Fl_PortButton *PortButton = (Fl_PortButton *)o;
  215. PortType Pt;
  216. if (m_DelMe) return;
  217. // Find out if this is an input or an output.
  218. if (Port<m_Info.NumInputs)
  219. {
  220. Pt=INPUT;
  221. }
  222. else
  223. {
  224. Pt=OUTPUT;
  225. Port-=m_Info.NumInputs;
  226. }
  227. if (PortButton->GetLastButton()==1)
  228. {
  229. ((Fl_Canvas*)(parent()))->PortClicked(this,Pt,Port,1);
  230. }
  231. else
  232. {
  233. ((Fl_Canvas*)(parent()))->PortClicked(this,Pt,Port,0);
  234. }
  235. }
  236. void Fl_DeviceGUI::cb_Port(Fl_Button* o, void* v)
  237. {((Fl_DeviceGUI*)(o->parent()))->cb_Port_i(o,v);}