1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
8 **************************************************************************
9 * This file contains implements the canvas functionality through flowcanvas
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <flowcanvas/Canvas.hpp>
29 #include <flowcanvas/Port.hpp>
30 #include <flowcanvas/Module.hpp>
34 class canvas_cls
: public FlowCanvas::Canvas
40 void (* connect_request
)(void * port1_context
, void * port2_context
),
41 void (* disconnect_request
)(void * port1_context
, void * port2_context
))
42 : FlowCanvas::Canvas(width
, height
)
43 , m_connect_request(connect_request
)
44 , m_disconnect_request(disconnect_request
)
47 virtual ~canvas_cls() {};
48 virtual void connect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
49 virtual void disconnect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
51 void (* m_connect_request
)(void * port1_context
, void * port2_context
);
52 void (* m_disconnect_request
)(void * port1_context
, void * port2_context
);
55 class port_cls
: public FlowCanvas::Port
59 boost::shared_ptr
<FlowCanvas::Module
> module
,
60 const std::string
& name
,
64 : FlowCanvas::Port(module
, name
, is_input
, color
)
65 , context(port_context
)
68 virtual ~port_cls() {};
77 Gnome::Canvas::init();
85 void (* connect_request
)(void * port1_context
, void * port2_context
),
86 void (* disconnect_request
)(void * port1_context
, void * port2_context
),
87 canvas_handle
* canvas_handle_ptr
)
89 boost::shared_ptr
<canvas_cls
> * canvas
;
91 canvas
= new boost::shared_ptr
<canvas_cls
>(new canvas_cls(width
, height
, connect_request
, disconnect_request
));
93 *canvas_handle_ptr
= (canvas_handle
)canvas
;
98 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
102 canvas_handle canvas
)
104 return ((Gtk::Widget
*)canvas_ptr
->get())->gobj();
109 canvas_handle canvas
)
116 canvas_handle canvas
)
118 FlowCanvas::ItemList modules
= canvas_ptr
->get()->items(); // copy
119 for (FlowCanvas::ItemList::iterator m
= modules
.begin(); m
!= modules
.end(); ++m
)
121 boost::shared_ptr
<FlowCanvas::Module
> module
= boost::dynamic_pointer_cast
<FlowCanvas::Module
>(*m
);
125 FlowCanvas::PortVector ports
= module
->ports(); // copy
126 for (FlowCanvas::PortVector::iterator p
= ports
.begin(); p
!= ports
.end(); ++p
)
128 boost::shared_ptr
<FlowCanvas::Port
> port
= boost::dynamic_pointer_cast
<FlowCanvas::Port
>(*p
);
129 assert(port
!= NULL
);
130 module
->remove_port(port
);
134 assert(module
->ports().empty());
135 canvas_ptr
->get()->remove_item(module
);
141 canvas_handle canvas
,
144 canvas_ptr
->get()->set_zoom(pix_per_unit
);
149 canvas_handle canvas
)
151 Glib::RefPtr
<Gdk::Window
> win
= canvas_ptr
->get()->get_window();
154 canvas_ptr
->get()->arrange();
159 canvas_create_module(
160 canvas_handle canvas
,
165 bool show_port_labels
,
166 canvas_module_handle
* module_handle_ptr
)
168 boost::shared_ptr
<FlowCanvas::Module
> * module
;
170 module
= new boost::shared_ptr
<FlowCanvas::Module
>(new FlowCanvas::Module(*canvas_ptr
, name
, x
, y
, show_title
, show_port_labels
));
171 canvas_ptr
->get()->add_item(*module
);
172 module
->get()->resize();
174 *module_handle_ptr
= (canvas_module_handle
)module
;
179 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
182 canvas_destroy_module(
183 canvas_handle canvas
,
184 canvas_module_handle module
)
186 canvas_ptr
->get()->remove_item(*module_ptr
);
193 canvas_handle canvas
,
194 canvas_module_handle module
,
199 canvas_port_handle
* port_handle_ptr
)
201 boost::shared_ptr
<port_cls
> * port
;
203 port
= new boost::shared_ptr
<port_cls
>(new port_cls(*module_ptr
, name
, is_input
, color
, port_context
));
205 module_ptr
->get()->add_port(*port
);
206 module_ptr
->get()->resize();
208 *port_handle_ptr
= (canvas_port_handle
)port
;
214 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
218 canvas_handle canvas
,
219 canvas_port_handle port
)
221 boost::shared_ptr
<FlowCanvas::Module
> module
= port_ptr
->get()->module().lock();
222 module
->remove_port(*port_ptr
);
229 canvas_get_port_color(
230 canvas_port_handle port
)
232 return port_ptr
->get()->color();
236 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
237 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
240 canvas_add_connection(
241 canvas_handle canvas
,
242 canvas_port_handle port1
,
243 canvas_port_handle port2
,
246 canvas_ptr
->get()->add_connection(*port1_ptr
, *port2_ptr
, color
);
251 canvas_remove_connection(
252 canvas_handle canvas
,
253 canvas_port_handle port1
,
254 canvas_port_handle port2
)
256 canvas_ptr
->get()->remove_connection(*port1_ptr
, *port2_ptr
);
265 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
266 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
268 if (m_connect_request
!= NULL
)
270 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
271 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
272 m_connect_request(port1
->context
, port2
->context
);
277 canvas_cls::disconnect(
278 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
279 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
281 if (m_disconnect_request
!= NULL
)
283 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
284 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
285 m_disconnect_request(port1
->context
, port2
->context
);
291 canvas_handle canvas
,
292 void * callback_context
,
293 bool (* callback
)(void * context
, canvas_module_handle module
))
299 canvas_enum_module_ports(
300 canvas_handle canvas
,
301 canvas_module_handle module
,
302 void * callback_context
,
303 bool (* callback
)(void * context
, canvas_port_handle port
))