FlowCanvas 0.5.1
/usr/src/RPM/BUILD/libflowcanvas-0.5.1/flowcanvas/Item.hpp
Go to the documentation of this file.
00001 /* This file is part of FlowCanvas.
00002  * Copyright (C) 2007 Dave 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 <string>
00022 #include <map>
00023 #include <algorithm>
00024 #include <boost/shared_ptr.hpp>
00025 #include <boost/enable_shared_from_this.hpp>
00026 #include <libgnomecanvasmm.h>
00027 #include <flowcanvas/Port.hpp>
00028 
00029 namespace FlowCanvas {
00030     
00031 class Canvas;
00032 
00033 
00038 class Item : public Gnome::Canvas::Group
00039            , public boost::enable_shared_from_this<Item>
00040 {
00041 public:
00042     Item(boost::shared_ptr<Canvas> canvas,
00043          const std::string&        name,
00044          double                    x,
00045          double                    y,
00046          uint32_t                  color);
00047 
00048     virtual ~Item() {}
00049     
00050     bool selected() const { return _selected; }
00051     virtual void set_selected(bool s);
00052     
00053     virtual void set_minimum_width(double w) { _minimum_width = w; }
00054 
00055     virtual void select_tick() = 0;
00056 
00057     virtual void move(double dx, double dy) = 0;
00058 
00059     virtual void zoom(double) {}
00060     boost::weak_ptr<Canvas> canvas() const { return _canvas; }
00061     
00062     bool popup_menu(guint button, guint32 activate_time) {
00063         if ( ! _menu)
00064             create_menu();
00065         if (_menu) {
00066             _menu->popup(button, activate_time);
00067             return true;
00068         } else {
00069             return false;
00070         }
00071     }
00072     
00073     virtual void create_menu() {}
00074 
00075     Gtk::Menu* menu() const           { return _menu; }
00076     void       set_menu(Gtk::Menu* m) { delete _menu; _menu = m; }
00077     
00078     double width() const { return _width; }
00079     double height() const { return _height; }
00080 
00081     virtual void resize() = 0;
00082     
00083     virtual void load_location()  {}
00084     virtual void store_location() {}
00085 
00086     bool        is_within(const Gnome::Canvas::Rect& rect) const;
00087     inline bool point_is_within(double x, double y) const;
00088 
00089     const std::string& name() const                   { return _name; }
00090     virtual void       set_name(const std::string& n) { _name = n; }
00091 
00092     uint32_t     base_color() const           { return _color; }
00093     virtual void set_border_color(uint32_t c) { _border_color = c; }
00094     virtual void set_base_color(uint32_t c)   { _color = c; }
00095     virtual void set_default_base_color() = 0;
00096 
00097     sigc::signal<void> signal_pointer_entered;
00098     sigc::signal<void> signal_pointer_exited;
00099     sigc::signal<void> signal_selected;
00100     sigc::signal<void> signal_unselected;
00101     
00102     sigc::signal<void, GdkEventButton*> signal_clicked;
00103     sigc::signal<void, GdkEventButton*> signal_double_clicked;
00104 
00105     sigc::signal<void, double, double> signal_dragged;
00106     sigc::signal<void, double, double> signal_dropped;
00107 
00108 protected:
00109     
00110     virtual void on_drag(double dx, double dy);
00111     virtual void on_click(GdkEventButton*);
00112     virtual void on_double_click(GdkEventButton*);
00113     
00114     virtual void set_height(double h) = 0;
00115     virtual void set_width(double w) = 0;
00116 
00117     const boost::weak_ptr<Canvas> _canvas;
00118     
00119     bool on_event(GdkEvent* event);
00120 
00121     std::string _name;
00122     double      _minimum_width;
00123     double      _width;
00124     double      _height;
00125     uint32_t    _border_color;
00126     uint32_t    _color;
00127     bool        _selected;
00128     Gtk::Menu*  _menu;
00129 };
00130 
00131 
00132 typedef std::list<boost::shared_ptr<Item> > ItemList;
00133 
00134 
00137 inline bool
00138 Item::point_is_within(double x, double y) const
00139 {
00140     return (x > property_x() && x < property_x() + _width
00141             && y > property_y() && y < property_y() + _height);
00142 }
00143 
00144 
00145 inline bool
00146 Item::is_within(const Gnome::Canvas::Rect& rect) const
00147 {
00148     const double x1 = rect.property_x1();
00149     const double y1 = rect.property_y1();
00150     const double x2 = rect.property_x2();
00151     const double y2 = rect.property_y2();
00152 
00153     if (x1 < x2 && y1 < y2) {
00154         return (property_x() > x1
00155             && property_y() > y1
00156             && property_x() + width() < x2
00157             && property_y() + height() < y2);
00158     } else if (x2 < x1 && y2 < y1) {
00159         return (property_x() > x2
00160             && property_y() > y2
00161             && property_x() + width() < x1
00162             && property_y() + height() < y1);
00163     } else if (x1 < x2 && y2 < y1) {
00164         return (property_x() > x1
00165             && property_y() > y2
00166             && property_x() + width() < x2
00167             && property_y() + height() < y1);
00168     } else if (x2 < x1 && y1 < y2) {
00169         return (property_x() > x2
00170             && property_y() > y1
00171             && property_x() + width() < x1
00172             && property_y() + height() < y2);
00173     } else {
00174         return false;
00175     }
00176 }
00177 
00178 } // namespace FlowCanvas
00179 
00180 #endif // FLOWCANVAS_ITEM_HPP
00181