• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

flowcanvas/Item.hpp

Go to the documentation of this file.
00001 /* This file is part of FlowCanvas.
00002  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
00003  *
00004  * FlowCanvas is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * FlowCanvas is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  *
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
00016  */
00017 
00018 #ifndef FLOWCANVAS_ITEM_HPP
00019 #define FLOWCANVAS_ITEM_HPP
00020 
00021 #include <algorithm>
00022 #include <list>
00023 #include <map>
00024 #include <string>
00025 
00026 #include <boost/enable_shared_from_this.hpp>
00027 #include <boost/shared_ptr.hpp>
00028 
00029 #include <libgnomecanvasmm.h>
00030 
00031 #include "flowcanvas/Port.hpp"
00032 
00033 namespace FlowCanvas {
00034 
00035 class Canvas;
00036 
00037 
00042 class Item : public Gnome::Canvas::Group
00043            , public boost::enable_shared_from_this<Item>
00044 {
00045 public:
00046     Item(boost::shared_ptr<Canvas> canvas,
00047          const std::string&        name,
00048          double                    x,
00049          double                    y,
00050          uint32_t                  color);
00051 
00052     virtual ~Item() {}
00053 
00054     bool selected() const { return _selected; }
00055     virtual void set_selected(bool s);
00056 
00057     virtual void set_minimum_width(double w) { _minimum_width = w; }
00058 
00059     virtual void select_tick() = 0;
00060 
00061     virtual void move(double dx, double dy) = 0;
00062 
00063     virtual void zoom(double z) {}
00064     boost::weak_ptr<Canvas> canvas() const { return _canvas; }
00065 
00066     bool popup_menu(guint button, guint32 activate_time) {
00067         if ( ! _menu)
00068             create_menu();
00069         if (_menu) {
00070             _menu->popup(button, activate_time);
00071             return true;
00072         } else {
00073             return false;
00074         }
00075     }
00076 
00077     virtual void create_menu() {}
00078 
00079     Gtk::Menu* menu() const           { return _menu; }
00080     void       set_menu(Gtk::Menu* m) { delete _menu; _menu = m; }
00081 
00082     double width() const { return _width; }
00083     double height() const { return _height; }
00084 
00085     virtual void resize() = 0;
00086 
00087     virtual void load_location()  {}
00088     virtual void store_location() {}
00089 
00090     bool        is_within(const Gnome::Canvas::Rect& rect) const;
00091     inline bool point_is_within(double x, double y) const;
00092 
00093     const std::string& name() const                   { return _name; }
00094     virtual void       set_name(const std::string& n) { _name = n; }
00095 
00096     uint32_t     base_color() const           { return _color; }
00097     virtual void set_border_color(uint32_t c) { _border_color = c; }
00098     virtual void set_base_color(uint32_t c)   { _color = c; }
00099     virtual void set_default_base_color() = 0;
00100 
00109     void set_partner(boost::shared_ptr<Item> partner) { _partner = partner; }
00110     boost::weak_ptr<Item> partner()                   { return _partner; }
00111 
00112     sigc::signal<void> signal_pointer_entered;
00113     sigc::signal<void> signal_pointer_exited;
00114     sigc::signal<void> signal_selected;
00115     sigc::signal<void> signal_unselected;
00116 
00117     sigc::signal<void, GdkEventButton*> signal_clicked;
00118     sigc::signal<void, GdkEventButton*> signal_double_clicked;
00119 
00120     sigc::signal<void, double, double> signal_dragged;
00121     sigc::signal<void, double, double> signal_dropped;
00122 
00123 protected:
00124     virtual void on_drag(double dx, double dy);
00125     virtual void on_drop();
00126     virtual void on_click(GdkEventButton* ev);
00127     virtual void on_double_click(GdkEventButton* ev);
00128 
00129     virtual void set_height(double h) = 0;
00130     virtual void set_width(double w) = 0;
00131     
00132     bool on_event(GdkEvent* event);
00133 
00134     const boost::weak_ptr<Canvas> _canvas;
00135 
00136     boost::weak_ptr<Item> _partner;
00137 
00138     Gtk::Menu*  _menu;
00139     std::string _name;
00140     double      _minimum_width;
00141     double      _width;
00142     double      _height;
00143     uint32_t    _border_color;
00144     uint32_t    _color;
00145     bool        _selected :1;
00146 };
00147 
00148 
00149 typedef std::list<boost::shared_ptr<Item> > ItemList;
00150 
00151 
00154 inline bool
00155 Item::point_is_within(double x, double y) const
00156 {
00157     return (x > property_x() && x < property_x() + _width
00158             && y > property_y() && y < property_y() + _height);
00159 }
00160 
00161 
00162 inline bool
00163 Item::is_within(const Gnome::Canvas::Rect& rect) const
00164 {
00165     const double x1 = rect.property_x1();
00166     const double y1 = rect.property_y1();
00167     const double x2 = rect.property_x2();
00168     const double y2 = rect.property_y2();
00169 
00170     if (x1 < x2 && y1 < y2) {
00171         return (property_x() > x1
00172             && property_y() > y1
00173             && property_x() + width() < x2
00174             && property_y() + height() < y2);
00175     } else if (x2 < x1 && y2 < y1) {
00176         return (property_x() > x2
00177             && property_y() > y2
00178             && property_x() + width() < x1
00179             && property_y() + height() < y1);
00180     } else if (x1 < x2 && y2 < y1) {
00181         return (property_x() > x1
00182             && property_y() > y2
00183             && property_x() + width() < x2
00184             && property_y() + height() < y1);
00185     } else if (x2 < x1 && y1 < y2) {
00186         return (property_x() > x2
00187             && property_y() > y1
00188             && property_x() + width() < x1
00189             && property_y() + height() < y2);
00190     } else {
00191         return false;
00192     }
00193 }
00194 
00195 } // namespace FlowCanvas
00196 
00197 #endif // FLOWCANVAS_ITEM_HPP
00198 

Generated on Tue Jan 11 2011 19:26:04 for FlowCanvas by  doxygen 1.7.1