| 1 | /* This file is part of FlowCanvas. |
|---|
| 2 | * Copyright (C) 2007-2009 David Robillard <http://drobilla.net> |
|---|
| 3 | * |
|---|
| 4 | * FlowCanvas is free software; you can redistribute it and/or modify it under the |
|---|
| 5 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 6 | * Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 7 | * version. |
|---|
| 8 | * |
|---|
| 9 | * FlowCanvas is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License along |
|---|
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 15 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #ifndef FLOWCANVAS_CANVAS_HPP |
|---|
| 19 | #define FLOWCANVAS_CANVAS_HPP |
|---|
| 20 | |
|---|
| 21 | #include <list> |
|---|
| 22 | #include <string> |
|---|
| 23 | |
|---|
| 24 | #include <boost/enable_shared_from_this.hpp> |
|---|
| 25 | #include <boost/utility.hpp> |
|---|
| 26 | |
|---|
| 27 | #include <libgnomecanvasmm.h> |
|---|
| 28 | |
|---|
| 29 | #include "Connection.hpp" |
|---|
| 30 | #include "Item.hpp" |
|---|
| 31 | #include "Module.hpp" |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** FlowCanvas namespace, everything is defined under this. |
|---|
| 35 | * |
|---|
| 36 | * \ingroup FlowCanvas |
|---|
| 37 | */ |
|---|
| 38 | namespace FlowCanvas { |
|---|
| 39 | |
|---|
| 40 | class Port; |
|---|
| 41 | class Module; |
|---|
| 42 | class GVNodes; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** \defgroup FlowCanvas FlowCanvas |
|---|
| 46 | * |
|---|
| 47 | * A generic dataflow widget using libgnomecanvas. |
|---|
| 48 | */ |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /** The 'master' canvas widget which contains all other objects. |
|---|
| 52 | * |
|---|
| 53 | * Applications must override some virtual methods to make the widget actually |
|---|
| 54 | * do anything (ie connect). |
|---|
| 55 | * |
|---|
| 56 | * \ingroup FlowCanvas |
|---|
| 57 | */ |
|---|
| 58 | class Canvas : boost::noncopyable |
|---|
| 59 | , public boost::enable_shared_from_this<Canvas> |
|---|
| 60 | , public Gnome::Canvas::CanvasAA |
|---|
| 61 | { |
|---|
| 62 | public: |
|---|
| 63 | Canvas(double width, double height); |
|---|
| 64 | virtual ~Canvas(); |
|---|
| 65 | |
|---|
| 66 | void destroy(); |
|---|
| 67 | |
|---|
| 68 | void add_item(boost::shared_ptr<Item> i); |
|---|
| 69 | bool remove_item(boost::shared_ptr<Item> i); |
|---|
| 70 | |
|---|
| 71 | boost::shared_ptr<Connection> |
|---|
| 72 | get_connection(boost::shared_ptr<Connectable> tail, |
|---|
| 73 | boost::shared_ptr<Connectable> head) const; |
|---|
| 74 | |
|---|
| 75 | bool add_connection(boost::shared_ptr<Connectable> tail, |
|---|
| 76 | boost::shared_ptr<Connectable> head, |
|---|
| 77 | uint32_t color); |
|---|
| 78 | |
|---|
| 79 | bool add_connection(boost::shared_ptr<Connection> connection); |
|---|
| 80 | |
|---|
| 81 | boost::shared_ptr<Connection> remove_connection(boost::shared_ptr<Connectable> tail, |
|---|
| 82 | boost::shared_ptr<Connectable> head); |
|---|
| 83 | |
|---|
| 84 | void set_default_placement(boost::shared_ptr<Module> m); |
|---|
| 85 | |
|---|
| 86 | void clear_selection(); |
|---|
| 87 | void select_item(boost::shared_ptr<Item> item); |
|---|
| 88 | void unselect_ports(); |
|---|
| 89 | void unselect_item(boost::shared_ptr<Item> item); |
|---|
| 90 | void unselect_connection(Connection* c); |
|---|
| 91 | |
|---|
| 92 | ItemList& items() { return _items; } |
|---|
| 93 | ItemList& selected_items() { return _selected_items; } |
|---|
| 94 | ConnectionList& connections() { return _connections; } |
|---|
| 95 | ConnectionList& selected_connections() { return _selected_connections; } |
|---|
| 96 | |
|---|
| 97 | void lock(bool l); |
|---|
| 98 | bool locked() const { return _locked; } |
|---|
| 99 | |
|---|
| 100 | double get_zoom() { return _zoom; } |
|---|
| 101 | void set_zoom(double pix_per_unit); |
|---|
| 102 | void zoom_full(); |
|---|
| 103 | |
|---|
| 104 | void render_to_dot(const std::string& filename); |
|---|
| 105 | virtual void arrange(bool use_length_hints=false, bool center=true); |
|---|
| 106 | |
|---|
| 107 | void move_contents_to(double x, double y); |
|---|
| 108 | |
|---|
| 109 | double width() const { return _width; } |
|---|
| 110 | double height() const { return _height; } |
|---|
| 111 | |
|---|
| 112 | void resize(double width, double height); |
|---|
| 113 | void resize_all_items(); |
|---|
| 114 | |
|---|
| 115 | void scroll_to_center(); |
|---|
| 116 | |
|---|
| 117 | enum FlowDirection { |
|---|
| 118 | HORIZONTAL, |
|---|
| 119 | VERTICAL |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | void set_direction(FlowDirection d) { _direction = d; } |
|---|
| 123 | FlowDirection direction() const { return _direction; } |
|---|
| 124 | |
|---|
| 125 | /** Dash applied to selected items. |
|---|
| 126 | * Set an object's property_dash() to this for the "rubber band" effect */ |
|---|
| 127 | ArtVpathDash* select_dash() { return _select_dash; } |
|---|
| 128 | |
|---|
| 129 | /** Make a connection. Should be overridden by an implementation to do something. */ |
|---|
| 130 | virtual void connect(boost::shared_ptr<Connectable> /*tail*/, |
|---|
| 131 | boost::shared_ptr<Connectable> /*head*/) {} |
|---|
| 132 | |
|---|
| 133 | /** Disconnect two ports. Should be overridden by an implementation to do something */ |
|---|
| 134 | virtual void disconnect(boost::shared_ptr<Connectable> /*tail*/, |
|---|
| 135 | boost::shared_ptr<Connectable> /*head*/) {} |
|---|
| 136 | |
|---|
| 137 | static sigc::signal<void, Gnome::Canvas::Item*> signal_item_entered; |
|---|
| 138 | static sigc::signal<void, Gnome::Canvas::Item*> signal_item_left; |
|---|
| 139 | |
|---|
| 140 | protected: |
|---|
| 141 | ItemList _items; ///< All items on this canvas |
|---|
| 142 | ConnectionList _connections; ///< All connections on this canvas |
|---|
| 143 | std::list< boost::shared_ptr<Item> > _selected_items; ///< All currently selected modules |
|---|
| 144 | std::list< boost::shared_ptr<Connection> > _selected_connections; ///< All currently selected connections |
|---|
| 145 | |
|---|
| 146 | virtual bool canvas_event(GdkEvent* event); |
|---|
| 147 | virtual bool frame_event(GdkEvent* ev); |
|---|
| 148 | |
|---|
| 149 | private: |
|---|
| 150 | friend class Module; |
|---|
| 151 | bool port_event(GdkEvent* event, boost::weak_ptr<Port> port); |
|---|
| 152 | |
|---|
| 153 | GVNodes layout_dot(bool use_length_hints, const std::string& filename); |
|---|
| 154 | |
|---|
| 155 | void remove_connection(boost::shared_ptr<Connection> c); |
|---|
| 156 | bool are_connected(boost::shared_ptr<const Connectable> tail, |
|---|
| 157 | boost::shared_ptr<const Connectable> head); |
|---|
| 158 | |
|---|
| 159 | void select_port(boost::shared_ptr<Port> p, bool unique = false); |
|---|
| 160 | void select_port_toggle(boost::shared_ptr<Port> p, int mod_state); |
|---|
| 161 | void unselect_port(boost::shared_ptr<Port> p); |
|---|
| 162 | void selection_joined_with(boost::shared_ptr<Port> port); |
|---|
| 163 | void join_selection(); |
|---|
| 164 | |
|---|
| 165 | boost::shared_ptr<Port> get_port_at(double x, double y); |
|---|
| 166 | |
|---|
| 167 | bool scroll_drag_handler(GdkEvent* event); |
|---|
| 168 | bool select_drag_handler(GdkEvent* event); |
|---|
| 169 | bool connection_drag_handler(GdkEvent* event); |
|---|
| 170 | |
|---|
| 171 | void ports_joined(boost::shared_ptr<Port> port1, boost::shared_ptr<Port> port2); |
|---|
| 172 | bool animate_selected(); |
|---|
| 173 | |
|---|
| 174 | void move_contents_to_internal(double x, double y, double min_x, double min_y); |
|---|
| 175 | |
|---|
| 176 | void on_parent_changed(Gtk::Widget* old_parent); |
|---|
| 177 | sigc::connection _parent_event_connection; |
|---|
| 178 | |
|---|
| 179 | typedef std::list< boost::shared_ptr<Port> > SelectedPorts; |
|---|
| 180 | |
|---|
| 181 | SelectedPorts _selected_ports; ///< Selected ports (hilited red) |
|---|
| 182 | boost::shared_ptr<Port> _connect_port; ///< Port for which a connection is being made |
|---|
| 183 | boost::shared_ptr<Port> _last_selected_port; |
|---|
| 184 | |
|---|
| 185 | Gnome::Canvas::Rect _base_rect; ///< Background |
|---|
| 186 | Gnome::Canvas::Rect* _select_rect; ///< Rectangle for drag selection |
|---|
| 187 | ArtVpathDash* _select_dash; ///< Animated selection dash style |
|---|
| 188 | |
|---|
| 189 | double _zoom; ///< Current zoom level |
|---|
| 190 | double _width; |
|---|
| 191 | double _height; |
|---|
| 192 | |
|---|
| 193 | enum DragState { NOT_DRAGGING, CONNECTION, SCROLL, SELECT }; |
|---|
| 194 | DragState _drag_state; |
|---|
| 195 | |
|---|
| 196 | FlowDirection _direction; |
|---|
| 197 | |
|---|
| 198 | bool _remove_objects :1; // flag to avoid removing objects from destructors when unnecessary |
|---|
| 199 | bool _locked :1; |
|---|
| 200 | }; |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | } // namespace FlowCanvas |
|---|
| 204 | |
|---|
| 205 | #endif // FLOWCANVAS_CANVAS_HPP |
|---|