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.

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