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.

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