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.

359 lines
9.1KB

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