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.

189 lines
5.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 DeleteSelection (void);
  82. void ClearConnections(Fl_DeviceGUI* Device);
  83. void RemoveDevice(Fl_DeviceGUI* Device);
  84. void Clear();
  85. void AddPluginName(const string &s, int 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) { data->m_CanPaste=true; }
  111. bool HaveSelection() {return m_HaveSelection; }
  112. CanvasGroup Selection() { return m_Selection; }
  113. static void SetDeviceCallbacks(Fl_DeviceGUI *device, Fl_Canvas *data)
  114. {
  115. device->SetOnDragCallback(Fl_Canvas::cb_OnDrag_s, data);
  116. device->SetOnClickCallback(Fl_Canvas::cb_OnDragClick_s, data);
  117. }
  118. private:
  119. void PopupEditMenu (Fl_Group *group);
  120. void DrawSelection();
  121. void CalculateSelection();
  122. void DrawWires();
  123. void ClearIncompleteWire();
  124. void DrawIncompleteWire();
  125. bool UserMakingConnection();
  126. Fl_DeviceGUI *FindDevice(int ID);
  127. Fl_Image *m_BG;
  128. char *m_BGData;
  129. void (*cb_Connection)(Fl_Widget*, void*);
  130. void (*cb_Unconnect)(Fl_Widget*, void*);
  131. void (*cb_AddDevice)(Fl_Widget*, void*);
  132. void (*cb_Rename)(Fl_Widget*, void*);
  133. void (*cb_CutDeviceGroup)(Fl_Widget*, void*);
  134. void (*cb_CopyDeviceGroup)(Fl_Widget*, void*);
  135. void (*cb_PasteDeviceGroup)(Fl_Widget*, void*);
  136. void (*cb_MergePatch)(Fl_Widget*, void*);
  137. map<int,int> MapNewDeviceIds;
  138. vector<CanvasWire> m_WireVec;
  139. CanvasWire m_IncompleteWire;
  140. GraphSort m_Graph;
  141. CanvasGroup m_Selection;
  142. bool m_CanPaste, m_HaveSelection, m_Selecting;
  143. int m_x, m_y, m_UpdateTimer, m_DragX, m_DragY;
  144. int m_StartSelectX, m_StartSelectY, m_EndSelectX,m_EndSelectY;
  145. friend istream &operator>>(istream &s, Fl_Canvas &o);
  146. friend ostream &operator<<(ostream &s, Fl_Canvas &o);
  147. // Callbacks
  148. static void cb_OnDrag_s (Fl_Widget* widget, int x, int y, void* data);
  149. inline void cb_OnDrag_i (Fl_Widget* widget, int x,int y);
  150. static void cb_OnDragClick_s (Fl_Widget* widget, int button, int shift_state, void* data);
  151. inline void cb_OnDragClick_i(Fl_Widget* widget, int button,int shift_state);
  152. static void cb_DeleteDeviceGroup (Fl_Widget* widget, void* data);
  153. inline void cb_DeleteDeviceGroup_i();
  154. static void cb_AddDeviceFromMenu (Fl_Widget* widget, void* data);
  155. inline void cb_AddDeviceFromMenu_i (Fl_Widget* widget, void* data);
  156. };
  157. istream &operator>>(istream &s, Fl_Canvas &o);
  158. ostream &operator<<(ostream &s, Fl_Canvas &o);
  159. #endif