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