Added flowcanvas 0.0.1 (unreleased).
[ardour.git] / libs / flowcanvas / flowcanvas / FlowCanvas.h
1 /* This file is part of FlowCanvas.  Copyright (C) 2005 Dave Robillard.
2  * 
3  * FlowCanvas is free software; you can redistribute it and/or modify it under the
4  * terms of the GNU General Public License as published by the Free Software
5  * Foundation; either version 2 of the License, or (at your option) any later
6  * version.
7  * 
8  * FlowCanvas is distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
11  * 
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write to the Free Software Foundation, Inc.,
14  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17
18 #ifndef FLOWCANVAS_H
19 #define FLOWCANVAS_H
20
21 #include <string>
22 #include <list>
23 #include <libgnomecanvasmm.h>
24 #include "Connection.h"
25 #include "Module.h"
26
27 using std::string;
28 using std::list;
29
30
31 /** FlowCanvas namespace, everything is defined under this.
32  *
33  * \ingroup FlowCanvas
34  */
35 namespace LibFlowCanvas {
36         
37 class Port;
38 class Module;
39
40
41 /** \defgroup FlowCanvas Canvas widget for dataflow systems.
42  *
43  * A generic dataflow widget using libgnomecanvas.  It's used by Om and
44  * Patchage, but could be used by anyone.
45  */
46
47
48 /** The canvas widget.
49  *
50  * Applications must override some virtual methods to make the widget actually
51  * do anything (ie connect).
52  *
53  * \ingroup FlowCanvas
54  */
55 #ifdef ANTI_ALIAS
56 class FlowCanvas : public Gnome::Canvas::CanvasAA
57 #else
58 class FlowCanvas : public Gnome::Canvas::Canvas
59 #endif
60 {
61 public:
62         FlowCanvas(double width, double height);
63         virtual ~FlowCanvas();
64
65         void destroy();
66         
67         void add_module(Module* m);
68         void remove_module(const string& name);
69                 
70         void add_connection(const string& mod1_name, const string& port1_name, const string& mod2_name, const string& port2_name);
71         bool remove_connection(const string& mod1_name, const string& port1_name, const string& mod2_name, const string& port2_name);
72
73         void add_connection(Port* port1, Port* port2);
74         bool remove_connection(Port* port1, Port* port2);
75         
76         Module* find_module(const string& name);
77         Port*   find_port(const string& module_name, const string& port_name);
78         
79         void rename_module(const string& old_name, const string& new_name);
80         
81         void set_default_placement(Module* m);
82         
83         float zoom()                    { return m_zoom; }
84         void  zoom(float pix_per_unit);
85         
86         double width() const  { return m_width; }
87         double height() const { return m_height; }
88
89         void clear_selection();
90         void select_module(Module* m);
91         void unselect_module(Module* m);
92
93         ModuleMap&         modules()              { return m_modules; }
94         list<Module*>&     selected_modules()     { return m_selected_modules; }
95         list<Connection*>& selected_connections() { return m_selected_connections; }
96         
97         /** Dash applied to selected items.
98          * Always animating, set a rect's property_dash() to this and it
99          * will automagically do the rubber band thing. */
100         ArtVpathDash* const select_dash() { return m_select_dash; }
101
102         virtual bool port_event(GdkEvent* event, Port* port);
103         
104         /** Make a connection.  Should be overridden by an implementation to do something. */
105         virtual void connect(const Port* const port1, const Port* const port2) = 0;
106         
107         /** Disconnect two ports.  Should be overridden by an implementation to do something */
108         virtual void disconnect(const Port* const port1, const Port* const port2) = 0;
109
110 protected:
111         ModuleMap                 m_modules;              ///< All modules on this canvas
112         ConnectionList    m_connections;          ///< All connections on this canvas
113         list<Module*>     m_selected_modules;     ///< All currently selected modules
114         list<Connection*> m_selected_connections; ///< All currently selected connections
115
116         virtual bool canvas_event(GdkEvent* event) { return false; } 
117         
118 private:
119         Connection* get_connection(const Port* port1, const Port* port2);
120         void        remove_connection(Connection* c);
121         void        selected_port(Port* p);
122         Port*       selected_port() { return m_selected_port; }
123         bool        are_connected(const Port* port1, const Port* port2);
124         Port*       get_port_at(double x, double y);
125
126         //bool scroll_drag_handler(GdkEvent* event);
127         virtual bool select_drag_handler(GdkEvent* event);
128         virtual bool connection_drag_handler(GdkEvent* event);
129         
130         void ports_joined(Port* port1, Port* port2);
131         bool animate_selected();
132
133         Port*  m_selected_port; ///< Selected port (hilited red from clicking once)
134         Port*  m_connect_port;  ///< Port for which a connection is being made (if applicable)
135         
136         float  m_zoom;   ///< Current zoom level
137         double m_width;  
138         double m_height; 
139
140         enum DragState { NOT_DRAGGING, CONNECTION, SCROLL, SELECT };
141         DragState      m_drag_state;
142         
143         Gnome::Canvas::Rect  m_base_rect; ///< Background
144
145         Gnome::Canvas::Rect* m_select_rect;          ///< Rectangle for drag selection
146         ArtVpathDash*        m_select_dash;          ///< Animated selection dash style
147 };
148
149
150 } // namespace LibFlowCanvas
151
152 #endif // FLOWCANVAS_H