FlowCanvas 0.7.1
Item.hpp
Go to the documentation of this file.
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 "flowcanvas/Port.hpp"
32
33namespace FlowCanvas {
34
35class Canvas;
36
37
42class Item : public Gnome::Canvas::Group
43 , public boost::enable_shared_from_this<Item>
44{
45public:
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)
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
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
123protected:
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;
141 double _width;
142 double _height;
144 uint32_t _color;
145 bool _selected :1;
146};
147
148
149typedef std::list<boost::shared_ptr<Item> > ItemList;
150
151
154inline bool
155Item::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
162inline bool
163Item::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
An item on a Canvas.
Definition: Item.hpp:44
virtual void on_drop()
virtual void set_border_color(uint32_t c)
Definition: Item.hpp:97
virtual void set_minimum_width(double w)
Definition: Item.hpp:57
void set_menu(Gtk::Menu *m)
Definition: Item.hpp:80
boost::weak_ptr< Item > partner()
Definition: Item.hpp:110
const boost::weak_ptr< Canvas > _canvas
Definition: Item.hpp:134
boost::weak_ptr< Item > _partner
Definition: Item.hpp:136
void set_partner(boost::shared_ptr< Item > partner)
Set the partner of this node.
Definition: Item.hpp:109
virtual void resize()=0
double _height
Definition: Item.hpp:142
virtual void set_base_color(uint32_t c)
Definition: Item.hpp:98
bool point_is_within(double x, double y) const
Returns whether or not the point x, y (world units) is within the item.
Definition: Item.hpp:155
double _minimum_width
Definition: Item.hpp:140
sigc::signal< void, double, double > signal_dropped
Definition: Item.hpp:121
double _width
Definition: Item.hpp:141
sigc::signal< void, double, double > signal_dragged
Definition: Item.hpp:120
virtual void select_tick()=0
Gtk::Menu * _menu
Definition: Item.hpp:138
virtual void create_menu()
Definition: Item.hpp:77
sigc::signal< void > signal_pointer_entered
Definition: Item.hpp:112
bool on_event(GdkEvent *event)
virtual void on_double_click(GdkEventButton *ev)
Gtk::Menu * menu() const
Definition: Item.hpp:79
virtual void zoom(double z)
Definition: Item.hpp:63
virtual void set_default_base_color()=0
virtual ~Item()
Definition: Item.hpp:52
virtual void set_width(double w)=0
const std::string & name() const
Definition: Item.hpp:93
virtual void set_height(double h)=0
sigc::signal< void > signal_selected
Definition: Item.hpp:114
sigc::signal< void, GdkEventButton * > signal_double_clicked
Definition: Item.hpp:118
virtual void store_location()
Definition: Item.hpp:88
virtual void on_drag(double dx, double dy)
uint32_t _border_color
Definition: Item.hpp:143
bool selected() const
Definition: Item.hpp:54
uint32_t _color
Definition: Item.hpp:144
sigc::signal< void > signal_pointer_exited
Definition: Item.hpp:113
bool _selected
Definition: Item.hpp:145
bool popup_menu(guint button, guint32 activate_time)
Definition: Item.hpp:66
sigc::signal< void, GdkEventButton * > signal_clicked
Definition: Item.hpp:117
virtual void move(double dx, double dy)=0
uint32_t base_color() const
Definition: Item.hpp:96
double width() const
Definition: Item.hpp:82
sigc::signal< void > signal_unselected
Definition: Item.hpp:115
bool is_within(const Gnome::Canvas::Rect &rect) const
Definition: Item.hpp:163
double height() const
Definition: Item.hpp:83
boost::weak_ptr< Canvas > canvas() const
Definition: Item.hpp:64
Item(boost::shared_ptr< Canvas > canvas, const std::string &name, double x, double y, uint32_t color)
std::string _name
Definition: Item.hpp:139
virtual void on_click(GdkEventButton *ev)
virtual void set_selected(bool s)
virtual void set_name(const std::string &n)
Definition: Item.hpp:94
virtual void load_location()
Definition: Item.hpp:87
FlowCanvas namespace, everything is defined under this.
Definition: Canvas.hpp:38
std::list< boost::shared_ptr< Item > > ItemList
Definition: Item.hpp:149