FlowCanvas  0.7.1
Module.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_MODULE_HPP
19 #define FLOWCANVAS_MODULE_HPP
20 
21 #include <string>
22 #include <algorithm>
23 #include <boost/shared_ptr.hpp>
24 #include <libgnomecanvasmm.h>
25 #include "flowcanvas/Port.hpp"
26 #include "flowcanvas/Item.hpp"
27 
28 namespace FlowCanvas {
29 
30 class Canvas;
31 
32 
37 class Module : public Item
38 {
39 public:
40  Module(boost::shared_ptr<Canvas> canvas,
41  const std::string& name,
42  double x = 0,
43  double y = 0,
44  bool show_title = true,
45  bool show_port_labels = true);
46 
47  virtual ~Module();
48 
49  const PortVector& ports() const { return _ports; }
50  PortVector& ports() { return _ports; }
51 
52  inline boost::shared_ptr<Port> get_port(const std::string& name) const;
53 
54  void add_port(boost::shared_ptr<Port> port);
55  void remove_port(boost::shared_ptr<Port> port);
56  boost::shared_ptr<Port> port_at(double x, double y);
57 
58  void zoom(double z);
59  void resize();
60 
61  bool show_port_labels(bool b) { return _show_port_labels; }
62  void set_show_port_labels(bool b);
63 
64  virtual void move(double dx, double dy);
65  virtual void move_to(double x, double y);
66 
67  virtual void set_name(const std::string& n);
68 
69  double border_width() const { return _border_width; }
70  void set_border_width(double w);
71 
72  void select_tick();
73  void set_selected(bool b);
74 
75  void set_highlighted(bool b);
76  void set_border_color(uint32_t c);
77  void set_base_color(uint32_t c);
79  void set_stacked_border(bool b);
80  void set_icon(const Glib::RefPtr<Gdk::Pixbuf>& icon);
81 
82  size_t num_ports() const { return _ports.size(); }
83 
84  double empty_port_breadth() const;
85  double empty_port_depth() const;
86 
87 protected:
88  virtual bool on_event(GdkEvent* ev);
89 
90  virtual void set_width(double w);
91  virtual void set_height(double h);
92 
93  void fit_canvas();
94  void measure_ports();
95  void resize_horiz();
96  void resize_vert();
97 
98  void port_renamed() { _port_renamed = true; }
99 
100  void embed(Gtk::Container* widget);
101 
103 
104  Gnome::Canvas::Rect _module_box;
105  Gnome::Canvas::Text _canvas_title;
106  Gnome::Canvas::Rect* _stacked_border;
107  Gnome::Canvas::Pixbuf* _icon_box;
108  Gtk::Container* _embed_container;
109  Gnome::Canvas::Widget* _embed_item;
110 
112  double _embed_width;
114  double _icon_size;
117  double _title_width;
119  bool _title_visible :1;
120  bool _port_renamed :1;
122 
123 private:
124  friend class Canvas;
125 
126  struct PortComparator {
127  explicit PortComparator(const std::string& name) : _name(name) {}
128  inline bool operator()(const boost::shared_ptr<Port> port)
129  { return (port && port->name() == _name); }
130  const std::string& _name;
131  };
132 
133  void embed_size_request(Gtk::Requisition* req, bool force);
134 };
135 
136 
137 
138 // Performance critical functions:
139 
140 
142 inline boost::shared_ptr<Port>
143 Module::get_port(const std::string& port_name) const
144 {
145  PortComparator comp(port_name);
146  PortVector::const_iterator i = std::find_if(_ports.begin(), _ports.end(), comp);
147  return (i != _ports.end()) ? *i : boost::shared_ptr<Port>();
148 }
149 
150 
151 } // namespace FlowCanvas
152 
153 #endif // FLOWCANVAS_MODULE_HPP