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.

368 lines
8.6KB

  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. m_Minimised(true)
  57. {
  58. for (int n=0; n<512; n++) Numbers[n]=n;
  59. type(1);
  60. box(FL_PLASTIC_UP_BOX);
  61. labeltype(FL_ENGRAVED_LABEL);
  62. align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
  63. color(SpiralSynthModularInfo::GUICOL_Device);
  64. m_Icon=Icon;
  65. m_MiniWidth=w();
  66. m_MiniHeight=h();
  67. m_DragBar = new Fl_DragBar(Info.XPos, Info.YPos, Info.Width+PortGroupWidth*2, TitleBarHeight, m_Name.c_str());
  68. m_DragBar->labelsize(10);
  69. m_DragBar->type(Fl_DragBar::FLDRAG);
  70. m_DragBar->color(SpiralSynthModularInfo::GUICOL_Device);
  71. m_Menu = new Fl_Menu_Button(x(),y(),w(),h(),"");
  72. m_Menu->type(Fl_Menu_Button::POPUP3);
  73. m_Menu->textsize(8);
  74. m_Menu->add("rename", 0, (Fl_Callback*)cb_Rename,this);
  75. m_Menu->add("delete", 0, (Fl_Callback*)cb_Delete,this);
  76. m_PluginWindow = PW;
  77. if (m_PluginWindow)
  78. {
  79. m_PluginWindow->hide();
  80. add(m_PluginWindow);
  81. }
  82. resizable(NULL);
  83. //Add the input/output ports
  84. Setup(Info, true);
  85. }
  86. void Fl_DeviceGUI::Clear()
  87. {
  88. end();
  89. }
  90. int Fl_DeviceGUI::handle(int event)
  91. {
  92. int t=Fl_Group::handle(event);
  93. /*if (m_PluginWindow)
  94. {
  95. if (Fl::event_x()>x() && Fl::event_x()<x()+w() && Fl::event_y()>y() && Fl::event_y()<y()+h())
  96. {
  97. if (!m_PluginWindow->visible() && m_PluginWindow && !m_DelMe)
  98. {
  99. m_Minimised=false;
  100. Resize(m_PluginWindow->w()+(PortGroupWidth*2),m_PluginWindow->h()+TitleBarHeight);
  101. m_PluginWindow->show();
  102. }
  103. }
  104. else
  105. {
  106. if (m_Minimised==false)// && !m_PluginWindow->visible())
  107. {
  108. m_Minimised=true;
  109. Resize(m_MiniWidth,m_MiniHeight);
  110. m_PluginWindow->hide();
  111. parent()->redraw();
  112. }
  113. }*/
  114. if (t==0 && Fl::belowmouse()==this)
  115. {
  116. if (event==FL_PUSH && Fl::event_button()==1)
  117. {
  118. if (m_PluginWindow && !m_DelMe)
  119. {
  120. if(!m_PluginWindow->visible()) Maximise();
  121. }
  122. }
  123. }
  124. if (m_Minimised==false && !m_PluginWindow->visible()) Minimise();
  125. return t;
  126. }
  127. void Fl_DeviceGUI::Minimise()
  128. {
  129. m_Minimised=true;
  130. Resize(m_MiniWidth,m_MiniHeight);
  131. parent()->redraw();
  132. }
  133. void Fl_DeviceGUI::Maximise()
  134. {
  135. m_Minimised=false;
  136. if (m_PluginWindow->h()+2>m_MiniHeight)
  137. {
  138. Resize(m_PluginWindow->w()+(PortGroupWidth*2)-5,m_PluginWindow->h()+2);
  139. }
  140. else
  141. {
  142. Resize(m_PluginWindow->w()+(PortGroupWidth*2)-5,m_MiniHeight);
  143. }
  144. m_PluginWindow->show();
  145. }
  146. void Fl_DeviceGUI::Resize(int width, int height)
  147. {
  148. int oldw = w();
  149. int oldh = h();
  150. size(width,height);
  151. m_DragBar->size(width,m_DragBar->h());
  152. for (int n=m_Info.NumInputs; n<(int)m_PortVec.size(); n++)
  153. {
  154. m_PortVec[n]->position(x()+width-8,m_PortVec[n]->y());
  155. }
  156. position(x()+(oldw-w())/2,y()+(oldh-h())/2);
  157. }
  158. void Fl_DeviceGUI::draw()
  159. {
  160. Fl_Group::draw();
  161. if (m_Icon && m_Minimised)
  162. {
  163. int Centx=x()+w()/2;
  164. int Centy=y()+h()/2;
  165. m_Icon->draw(Centx-m_Icon->w()/2,Centy-m_Icon->h()/2);
  166. }
  167. }
  168. void Fl_DeviceGUI::Setup(const DeviceGUIInfo& Info, bool FirstTime)
  169. {
  170. m_Info=Info;
  171. // Remove all current connections - it's the safest thing to do.
  172. if (parent() && !FirstTime)
  173. {
  174. ((Fl_Canvas*)(parent()))->ClearConnections(this);
  175. }
  176. // delete the current ports
  177. for(vector<Fl_PortButton*>::iterator i=m_PortVec.begin();
  178. i!=m_PortVec.end(); i++)
  179. {
  180. remove(*i);
  181. delete(*i);
  182. }
  183. m_PortVec.clear();
  184. int InputX=x()+2;
  185. int OutputX=x()+PortGroupWidth+Info.Width+4;
  186. int StartY=y()+TitleBarHeight;
  187. int PortDist=10;
  188. int PortNum=0;
  189. m_MiniHeight=Info.Height+TitleBarHeight;
  190. h(m_MiniHeight);
  191. for (int n=0; n<Info.NumInputs; n++)
  192. {
  193. Fl_PortButton* NewInput = new Fl_PortButton(InputX,StartY+PortDist*n,PortSize,PortSize,"");
  194. NewInput->type(1);
  195. NewInput->SetType(Fl_PortButton::INPUT);
  196. NewInput->value(false);
  197. NewInput->box(FL_ROUNDED_BOX);
  198. Fl_Color col = (Fl_Color) WIRE_COL0;
  199. switch (Info.PortTypes[n]) {
  200. case 0: col = (Fl_Color) WIRE_COL0;
  201. break;
  202. case 1: col = (Fl_Color) WIRE_COL1;
  203. break;
  204. case 2: col = (Fl_Color) WIRE_COL2;
  205. break;
  206. case 3: col = (Fl_Color) WIRE_COL3;
  207. break;
  208. case 4: col = (Fl_Color) WIRE_COL4;
  209. break;
  210. default: col = (Fl_Color) WIRE_COL0;
  211. }
  212. NewInput->selection_color(col);
  213. NewInput->down_box(FL_ROUNDED_BOX);
  214. NewInput->tooltip(Info.PortTips[n].c_str());
  215. NewInput->callback((Fl_Callback*)cb_Port,(void*)&Numbers[PortNum]);
  216. m_PortVec.push_back(NewInput);
  217. add(NewInput);
  218. PortNum++;
  219. }
  220. for (int n=0; n<Info.NumOutputs; n++)
  221. {
  222. Fl_PortButton* NewOutput = new Fl_PortButton(OutputX,StartY+PortDist*n,PortSize,PortSize,"");
  223. NewOutput->type(1);
  224. NewOutput->SetType(Fl_PortButton::OUTPUT);
  225. NewOutput->value(false);
  226. NewOutput->box(FL_ROUNDED_BOX);
  227. Fl_Color col = (Fl_Color) WIRE_COL0;
  228. switch (Info.PortTypes[n+Info.NumInputs]) {
  229. case 0: col = (Fl_Color) WIRE_COL0;
  230. break;
  231. case 1: col = (Fl_Color) WIRE_COL1;
  232. break;
  233. case 2: col = (Fl_Color) WIRE_COL2;
  234. break;
  235. case 3: col = (Fl_Color) WIRE_COL3;
  236. break;
  237. case 4: col = (Fl_Color) WIRE_COL4;
  238. break;
  239. default: col = (Fl_Color) WIRE_COL0;
  240. }
  241. NewOutput->selection_color(col);
  242. NewOutput->down_box(FL_ROUNDED_BOX);
  243. NewOutput->tooltip(Info.PortTips[n+Info.NumInputs].c_str());
  244. NewOutput->callback((Fl_Callback*)cb_Port,(void*)&Numbers[PortNum]);
  245. m_PortVec.push_back(NewOutput);
  246. add(NewOutput);
  247. PortNum++;
  248. }
  249. }
  250. bool Fl_DeviceGUI::AddConnection(int n)
  251. {
  252. if ( n < (int)m_PortVec.size() )
  253. {
  254. m_PortVec[n]->Add();
  255. m_PortVec[n]->value(1);
  256. redraw();
  257. return true;
  258. }
  259. return false;
  260. }
  261. void Fl_DeviceGUI::RemoveConnection(int n)
  262. {
  263. m_PortVec[n]->Remove();
  264. if (!m_PortVec[n]->GetCount())
  265. {
  266. m_PortVec[n]->value(0);
  267. redraw();
  268. }
  269. }
  270. inline void Fl_DeviceGUI::cb_Port_i(Fl_Button* o, void* v)
  271. {
  272. int Port=*(int*)(v);
  273. Fl_PortButton *PortButton = (Fl_PortButton *)o;
  274. PortType Pt;
  275. if (m_DelMe) return;
  276. // Find out if this is an input or an output.
  277. if (Port<m_Info.NumInputs)
  278. {
  279. Pt=INPUT;
  280. }
  281. else
  282. {
  283. Pt=OUTPUT;
  284. Port-=m_Info.NumInputs;
  285. }
  286. if (PortButton->GetLastButton()==1)
  287. {
  288. ((Fl_Canvas*)(parent()))->PortClicked(this,Pt,Port,1);
  289. }
  290. else
  291. {
  292. ((Fl_Canvas*)(parent()))->PortClicked(this,Pt,Port,0);
  293. }
  294. }
  295. void Fl_DeviceGUI::cb_Port(Fl_Button* o, void* v)
  296. {((Fl_DeviceGUI*)(o->parent()))->cb_Port_i(o,v);}
  297. inline void Fl_DeviceGUI::cb_Rename_i(Fl_Menu_Button* o, void* v)
  298. {
  299. char name[256];
  300. if (Pawfal_Input("Rename the module:",m_DragBar->label(),name))
  301. {
  302. m_Name=name;
  303. m_DragBar->label(m_Name.c_str());
  304. ((Fl_Canvas*)(parent()))->Rename(this);
  305. }
  306. }
  307. void Fl_DeviceGUI::cb_Rename(Fl_Menu_Button* o, void* v)
  308. {((Fl_DeviceGUI*)(o->parent()))->cb_Rename_i(o,v);}
  309. inline void Fl_DeviceGUI::cb_Delete_i(Fl_Menu_Button* o, void* v)
  310. {
  311. m_DelMe=true;
  312. }
  313. void Fl_DeviceGUI::cb_Delete(Fl_Menu_Button* o, void* v)
  314. {((Fl_DeviceGUI*)(o->parent()))->cb_Delete_i(o,v);}