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.

371 lines
9.0KB

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