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.

270 lines
6.3KB

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