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.

252 lines
6.4KB

  1. /* Canvas 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/Fl_Group.h>
  19. #include <FL/Fl_Output.h>
  20. #include <FL/Fl_Image.h>
  21. #include <FL/Fl_Menu_Button.h>
  22. #include <vector>
  23. #include <string>
  24. #include "../../GraphSort.h"
  25. #include "Fl_DeviceGUI.h"
  26. #ifndef CANVAS_WIDGET
  27. #define CANVAS_WIDGET
  28. using namespace std;
  29. class Fl_DeviceGUI;
  30. class CanvasWire
  31. {
  32. public:
  33. CanvasWire() { Clear(); }
  34. void Clear()
  35. {
  36. OutputPort=-1;
  37. OutputID=-1;
  38. OutputTerminal=false;
  39. InputPort=-1;
  40. InputID=-1;
  41. InputTerminal=false;
  42. DelMe=false;
  43. }
  44. int OutputID;
  45. int OutputPort;
  46. bool OutputTerminal;
  47. int InputID;
  48. int InputPort;
  49. bool InputTerminal;
  50. bool DelMe;
  51. };
  52. class CanvasGroup
  53. {
  54. public:
  55. CanvasGroup() { Clear(); }
  56. void Clear()
  57. {
  58. m_DeviceIds.clear();
  59. }
  60. vector<int> m_DeviceIds;
  61. };
  62. class Fl_Canvas : public Fl_Group
  63. {
  64. public:
  65. Fl_Canvas(int x, int y, int w, int h, char *name);
  66. ~Fl_Canvas();
  67. Fl_Menu_Button *m_Menu;
  68. virtual void draw();
  69. virtual int handle(int event);
  70. int globalhandle(int event);
  71. void PortClicked(Fl_DeviceGUI* Device, int Type, int Port, bool Value);
  72. void Rename(Fl_DeviceGUI* Device);
  73. void SetConnectionCallback(Fl_Callback* s) { cb_Connection=s; }
  74. void SetUnconnectCallback(Fl_Callback* s) { cb_Unconnect=s; }
  75. void SetAddDeviceCallback(Fl_Callback* s) { cb_AddDevice=s; }
  76. void SetRenameCallback(Fl_Callback* s) { cb_Rename=s; }
  77. void SetCutDeviceGroupCallback(Fl_Callback* s) { cb_CutDeviceGroup=s; }
  78. void SetCopyDeviceGroupCallback(Fl_Callback* s) { cb_CopyDeviceGroup=s; }
  79. void SetPasteDeviceGroupCallback(Fl_Callback* s) { cb_PasteDeviceGroup=s; }
  80. void SetMergePatchCallback(Fl_Callback* s) { cb_MergePatch=s; }
  81. void ClearConnections(Fl_DeviceGUI* Device);
  82. void RemoveDevice(Fl_DeviceGUI* Device);
  83. void Clear();
  84. void AddPluginName(const string &s, int ID)
  85. { m_PluginNameList.push_back(pair<string,int>(s,ID)); }
  86. GraphSort* GetGraph() { return &m_Graph; }
  87. void Poll();
  88. void ToTop(Fl_DeviceGUI *o);
  89. void ToBot(Fl_DeviceGUI *o);
  90. void StreamSelectionWiresIn(istream &s, map<int,int> NewDeviceIds, bool merge, bool paste);
  91. void StreamSelectionWiresOut(ostream &s);
  92. void StreamWiresIn(istream &s, bool merge, bool paste);
  93. static void AppendSelection(int DeviceId, Fl_Canvas *data)
  94. {
  95. Fl_DeviceGUI *o = data->FindDevice(DeviceId);
  96. if (o)
  97. {
  98. data->m_HaveSelection = true;
  99. data->m_Selection.m_DeviceIds.push_back(DeviceId);
  100. o->SetOnDragCallback(Fl_Canvas::cb_OnDrag_s, data);
  101. data->redraw();
  102. }
  103. }
  104. static void ClearSelection(Fl_Canvas *data)
  105. {
  106. data->m_HaveSelection=false;
  107. data->m_Selection.Clear();
  108. data->redraw();
  109. }
  110. static void EnablePaste(Fl_Canvas *data)
  111. {
  112. data->m_CanPaste=true;
  113. }
  114. bool HaveSelection() {return m_HaveSelection; }
  115. CanvasGroup Selection() { return m_Selection; }
  116. static void SetDeviceCallbacks(Fl_DeviceGUI *device, Fl_Canvas *data)
  117. {
  118. device->SetOnDragCallback(Fl_Canvas::cb_OnDrag_s, data);
  119. device->SetOnClickCallback(Fl_Canvas::cb_OnDragClick_s, data);
  120. }
  121. private:
  122. void DrawSelection();
  123. void CalculateSelection();
  124. void DrawWires();
  125. void ClearIncompleteWire();
  126. void DrawIncompleteWire();
  127. bool UserMakingConnection();
  128. Fl_DeviceGUI *FindDevice(int ID);
  129. Fl_Image *m_BG;
  130. char *m_BGData;
  131. void (*cb_Connection)(Fl_Widget*, void*);
  132. void (*cb_Unconnect)(Fl_Widget*, void*);
  133. void (*cb_AddDevice)(Fl_Widget*, void*);
  134. void (*cb_Rename)(Fl_Widget*, void*);
  135. void (*cb_CutDeviceGroup)(Fl_Widget*, void*);
  136. void (*cb_CopyDeviceGroup)(Fl_Widget*, void*);
  137. void (*cb_PasteDeviceGroup)(Fl_Widget*, void*);
  138. void (*cb_MergePatch)(Fl_Widget*, void*);
  139. map<int,int> MapNewDeviceIds;
  140. vector<CanvasWire> m_WireVec;
  141. CanvasWire m_IncompleteWire;
  142. bool m_ToolMenu, m_ButtonDown;
  143. int m_x,m_y,m_Selected;
  144. vector< pair<string,int> > m_PluginNameList;
  145. GraphSort m_Graph;
  146. int m_UpdateTimer;
  147. int m_DragX,m_DragY;
  148. bool m_HaveSelection, m_Selecting;
  149. int m_StartSelectX,m_StartSelectY;
  150. int m_EndSelectX,m_EndSelectY;
  151. CanvasGroup m_Selection;
  152. bool m_CanPaste;
  153. friend istream &operator>>(istream &s, Fl_Canvas &o);
  154. friend ostream &operator<<(ostream &s, Fl_Canvas &o);
  155. void PopupEditMenu(Fl_Group *group);
  156. ///Inline Callbacks///
  157. inline void cb_OnDrag_i(Fl_Widget* widget, int x,int y);
  158. inline void cb_OnDragClick_i(Fl_Widget* widget, int button,int shift_state)
  159. {
  160. if ((button==3) && ((shift_state & FL_CTRL) != 0))
  161. {
  162. PopupEditMenu(widget->parent());
  163. }
  164. if ((widget) && (button==1))
  165. {
  166. int ID = ((Fl_DeviceGUI*)(widget->parent()))->GetID();
  167. std::vector<int>::iterator device_iter = std::find(m_Selection.m_DeviceIds.begin(), m_Selection.m_DeviceIds.end(), ID);
  168. if (((shift_state & FL_SHIFT) != 0) || ((shift_state & FL_CTRL) != 0))
  169. {
  170. if (m_HaveSelection)
  171. {
  172. if (device_iter != m_Selection.m_DeviceIds.end())
  173. m_Selection.m_DeviceIds.erase(device_iter);
  174. else
  175. m_Selection.m_DeviceIds.push_back(ID);
  176. }
  177. else
  178. {
  179. m_Selection.Clear();
  180. m_HaveSelection = true;
  181. m_Selection.m_DeviceIds.push_back(ID);
  182. }
  183. }
  184. else
  185. {
  186. m_Selection.Clear();
  187. m_HaveSelection = true;
  188. m_Selection.m_DeviceIds.push_back(ID);
  189. }
  190. redraw();
  191. }
  192. }
  193. inline void cb_DeleteDeviceGroup_i();
  194. ///Static Callbacks///
  195. static void cb_OnDrag_s(Fl_Widget* widget, int x,int y, void* data) { ((Fl_Canvas *)data)->cb_OnDrag_i(widget, x, y); }
  196. static void cb_OnDragClick_s(Fl_Widget* widget, int button,int shift_state, void* data) { ((Fl_Canvas *)data)->cb_OnDragClick_i(widget, button, shift_state); }
  197. static void cb_DeleteDeviceGroup(Fl_Widget* widget, void* data) { ((Fl_Canvas *)data)->cb_DeleteDeviceGroup_i(); }
  198. };
  199. istream &operator>>(istream &s, Fl_Canvas &o);
  200. ostream &operator<<(ostream &s, Fl_Canvas &o);
  201. #endif