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.

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