| 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_ITEM_HPP |
|---|
| 19 | #define FLOWCANVAS_ITEM_HPP |
|---|
| 20 | |
|---|
| 21 | #include <algorithm> |
|---|
| 22 | #include <list> |
|---|
| 23 | #include <map> |
|---|
| 24 | #include <string> |
|---|
| 25 | |
|---|
| 26 | #include <boost/enable_shared_from_this.hpp> |
|---|
| 27 | #include <boost/shared_ptr.hpp> |
|---|
| 28 | |
|---|
| 29 | #include <libgnomecanvasmm.h> |
|---|
| 30 | |
|---|
| 31 | #include "Port.hpp" |
|---|
| 32 | |
|---|
| 33 | namespace FlowCanvas { |
|---|
| 34 | |
|---|
| 35 | class Canvas; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /** An item on a Canvas. |
|---|
| 39 | * |
|---|
| 40 | * \ingroup FlowCanvas |
|---|
| 41 | */ |
|---|
| 42 | class Item : public Gnome::Canvas::Group |
|---|
| 43 | , public boost::enable_shared_from_this<Item> |
|---|
| 44 | { |
|---|
| 45 | public: |
|---|
| 46 | Item(boost::shared_ptr<Canvas> canvas, |
|---|
| 47 | const std::string& name, |
|---|
| 48 | double x, |
|---|
| 49 | double y, |
|---|
| 50 | uint32_t color); |
|---|
| 51 | |
|---|
| 52 | virtual ~Item() {} |
|---|
| 53 | |
|---|
| 54 | bool selected() const { return _selected; } |
|---|
| 55 | virtual void set_selected(bool s); |
|---|
| 56 | |
|---|
| 57 | virtual void set_minimum_width(double w) { _minimum_width = w; } |
|---|
| 58 | |
|---|
| 59 | virtual void select_tick() = 0; |
|---|
| 60 | |
|---|
| 61 | virtual void move(double dx, double dy) = 0; |
|---|
| 62 | |
|---|
| 63 | virtual void zoom(double z) {} |
|---|
| 64 | boost::weak_ptr<Canvas> canvas() const { return _canvas; } |
|---|
| 65 | |
|---|
| 66 | bool popup_menu(guint button, guint32 activate_time) { |
|---|
| 67 | if ( ! _menu) |
|---|
| 68 | create_menu(); |
|---|
| 69 | if (_menu) { |
|---|
| 70 | _menu->popup(button, activate_time); |
|---|
| 71 | return true; |
|---|
| 72 | } else { |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | virtual void create_menu() {} |
|---|
| 78 | |
|---|
| 79 | Gtk::Menu* menu() const { return _menu; } |
|---|
| 80 | void set_menu(Gtk::Menu* m) { delete _menu; _menu = m; } |
|---|
| 81 | |
|---|
| 82 | double width() const { return _width; } |
|---|
| 83 | double height() const { return _height; } |
|---|
| 84 | |
|---|
| 85 | virtual void resize() = 0; |
|---|
| 86 | |
|---|
| 87 | virtual void load_location() {} |
|---|
| 88 | virtual void store_location() {} |
|---|
| 89 | |
|---|
| 90 | bool is_within(const Gnome::Canvas::Rect& rect) const; |
|---|
| 91 | inline bool point_is_within(double x, double y) const; |
|---|
| 92 | |
|---|
| 93 | const std::string& name() const { return _name; } |
|---|
| 94 | virtual void set_name(const std::string& n) { _name = n; } |
|---|
| 95 | |
|---|
| 96 | uint32_t base_color() const { return _color; } |
|---|
| 97 | virtual void set_border_color(uint32_t c) { _border_color = c; } |
|---|
| 98 | virtual void set_base_color(uint32_t c) { _color = c; } |
|---|
| 99 | virtual void set_default_base_color() = 0; |
|---|
| 100 | |
|---|
| 101 | /** Set the partner of this node. |
|---|
| 102 | * Partner nodes are nodes that should be visually aligned to correspond to |
|---|
| 103 | * each other, even if they are not necessarily connected (e.g. for separate |
|---|
| 104 | * modules representing the inputs and outputs of a single interface). |
|---|
| 105 | * The partner is invisibly connected as if it had an input that is connected |
|---|
| 106 | * to this item, e.g. foo.set_partner(bar) will arrange like: |
|---|
| 107 | * [foo] [bar] with a left-to-right flow direction. |
|---|
| 108 | */ |
|---|
| 109 | void set_partner(boost::shared_ptr<Item> partner) { _partner = partner; } |
|---|
| 110 | boost::weak_ptr<Item> partner() { return _partner; } |
|---|
| 111 | |
|---|
| 112 | sigc::signal<void> signal_pointer_entered; |
|---|
| 113 | sigc::signal<void> signal_pointer_exited; |
|---|
| 114 | sigc::signal<void> signal_selected; |
|---|
| 115 | sigc::signal<void> signal_unselected; |
|---|
| 116 | |
|---|
| 117 | sigc::signal<void, GdkEventButton*> signal_clicked; |
|---|
| 118 | sigc::signal<void, GdkEventButton*> signal_double_clicked; |
|---|
| 119 | |
|---|
| 120 | sigc::signal<void, double, double> signal_dragged; |
|---|
| 121 | sigc::signal<void, double, double> signal_dropped; |
|---|
| 122 | |
|---|
| 123 | protected: |
|---|
| 124 | virtual void on_drag(double dx, double dy); |
|---|
| 125 | virtual void on_drop(); |
|---|
| 126 | virtual void on_click(GdkEventButton* ev); |
|---|
| 127 | virtual void on_double_click(GdkEventButton* ev); |
|---|
| 128 | |
|---|
| 129 | virtual void set_height(double h) = 0; |
|---|
| 130 | virtual void set_width(double w) = 0; |
|---|
| 131 | |
|---|
| 132 | bool on_event(GdkEvent* event); |
|---|
| 133 | |
|---|
| 134 | const boost::weak_ptr<Canvas> _canvas; |
|---|
| 135 | |
|---|
| 136 | boost::weak_ptr<Item> _partner; |
|---|
| 137 | |
|---|
| 138 | Gtk::Menu* _menu; |
|---|
| 139 | std::string _name; |
|---|
| 140 | double _minimum_width; |
|---|
| 141 | double _width; |
|---|
| 142 | double _height; |
|---|
| 143 | uint32_t _border_color; |
|---|
| 144 | uint32_t _color; |
|---|
| 145 | bool _selected :1; |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | typedef std::list<boost::shared_ptr<Item> > ItemList; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | /** Returns whether or not the point @a x, @a y (world units) is within the item. |
|---|
| 153 | */ |
|---|
| 154 | inline bool |
|---|
| 155 | Item::point_is_within(double x, double y) const |
|---|
| 156 | { |
|---|
| 157 | return (x > property_x() && x < property_x() + _width |
|---|
| 158 | && y > property_y() && y < property_y() + _height); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | inline bool |
|---|
| 163 | Item::is_within(const Gnome::Canvas::Rect& rect) const |
|---|
| 164 | { |
|---|
| 165 | const double x1 = rect.property_x1(); |
|---|
| 166 | const double y1 = rect.property_y1(); |
|---|
| 167 | const double x2 = rect.property_x2(); |
|---|
| 168 | const double y2 = rect.property_y2(); |
|---|
| 169 | |
|---|
| 170 | if (x1 < x2 && y1 < y2) { |
|---|
| 171 | return (property_x() > x1 |
|---|
| 172 | && property_y() > y1 |
|---|
| 173 | && property_x() + width() < x2 |
|---|
| 174 | && property_y() + height() < y2); |
|---|
| 175 | } else if (x2 < x1 && y2 < y1) { |
|---|
| 176 | return (property_x() > x2 |
|---|
| 177 | && property_y() > y2 |
|---|
| 178 | && property_x() + width() < x1 |
|---|
| 179 | && property_y() + height() < y1); |
|---|
| 180 | } else if (x1 < x2 && y2 < y1) { |
|---|
| 181 | return (property_x() > x1 |
|---|
| 182 | && property_y() > y2 |
|---|
| 183 | && property_x() + width() < x2 |
|---|
| 184 | && property_y() + height() < y1); |
|---|
| 185 | } else if (x2 < x1 && y1 < y2) { |
|---|
| 186 | return (property_x() > x2 |
|---|
| 187 | && property_y() > y1 |
|---|
| 188 | && property_x() + width() < x1 |
|---|
| 189 | && property_y() + height() < y2); |
|---|
| 190 | } else { |
|---|
| 191 | return false; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | } // namespace FlowCanvas |
|---|
| 196 | |
|---|
| 197 | #endif // FLOWCANVAS_ITEM_HPP |
|---|
| 198 | |
|---|