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.

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