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 void (* module_location_changed
)(void * module_context
, double x
, double y
))
43 : FlowCanvas::Canvas(width
, height
)
44 , m_connect_request(connect_request
)
45 , m_disconnect_request(disconnect_request
)
46 , m_module_location_changed(module_location_changed
)
49 virtual ~canvas_cls() {}
51 virtual void on_realize()
53 log_info("canvas_cls::on_realize");
54 FlowCanvas::Canvas::on_realize();
58 virtual void on_size_allocate(Gtk::Allocation
& allocation
)
60 //log_info("canvas_cls::on_size_allocate");
61 FlowCanvas::Canvas::on_size_allocate(allocation
);
64 //log_info("... realized");
69 virtual void connect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
70 virtual void disconnect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
72 void (* m_connect_request
)(void * port1_context
, void * port2_context
);
73 void (* m_disconnect_request
)(void * port1_context
, void * port2_context
);
74 void (* m_module_location_changed
)(void * module_context
, double x
, double y
);
77 class module_cls
: public FlowCanvas::Module
81 boost::shared_ptr
<FlowCanvas::Canvas
> canvas
,
82 const std::string
& name
,
86 bool show_port_labels
,
87 void * module_context
)
88 : FlowCanvas::Module(canvas
, name
, x
, y
, show_title
, show_port_labels
)
89 , m_context(module_context
)
92 virtual ~module_cls() {}
94 virtual void store_location()
96 boost::dynamic_pointer_cast
<canvas_cls
>(canvas().lock())->m_module_location_changed(m_context
, property_x(), property_y());
102 class port_cls
: public FlowCanvas::Port
106 boost::shared_ptr
<FlowCanvas::Module
> module
,
107 const std::string
& name
,
111 : FlowCanvas::Port(module
, name
, is_input
, color
)
112 , context(port_context
)
115 virtual ~port_cls() {}
124 Gnome::Canvas::init();
132 void (* connect_request
)(void * port1_context
, void * port2_context
),
133 void (* disconnect_request
)(void * port1_context
, void * port2_context
),
134 void (* module_location_changed
)(void * module_context
, double x
, double y
),
135 canvas_handle
* canvas_handle_ptr
)
137 boost::shared_ptr
<canvas_cls
> * canvas
;
139 canvas
= new boost::shared_ptr
<canvas_cls
>(new canvas_cls(width
, height
, connect_request
, disconnect_request
, module_location_changed
));
141 *canvas_handle_ptr
= (canvas_handle
)canvas
;
146 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
150 canvas_handle canvas
)
152 return ((Gtk::Widget
*)canvas_ptr
->get())->gobj();
157 canvas_handle canvas
)
164 canvas_handle canvas
)
166 FlowCanvas::ItemList modules
= canvas_ptr
->get()->items(); // copy
167 for (FlowCanvas::ItemList::iterator m
= modules
.begin(); m
!= modules
.end(); ++m
)
169 boost::shared_ptr
<FlowCanvas::Module
> module
= boost::dynamic_pointer_cast
<FlowCanvas::Module
>(*m
);
173 FlowCanvas::PortVector ports
= module
->ports(); // copy
174 for (FlowCanvas::PortVector::iterator p
= ports
.begin(); p
!= ports
.end(); ++p
)
176 boost::shared_ptr
<FlowCanvas::Port
> port
= boost::dynamic_pointer_cast
<FlowCanvas::Port
>(*p
);
177 ASSERT(port
!= NULL
);
178 module
->remove_port(port
);
182 ASSERT(module
->ports().empty());
183 canvas_ptr
->get()->remove_item(module
);
189 canvas_handle canvas
,
193 *width_ptr
= canvas_ptr
->get()->width();
194 *height_ptr
= canvas_ptr
->get()->height();
198 canvas_scroll_to_center(
199 canvas_handle canvas
)
201 if (canvas_ptr
->get()->is_realized())
203 //log_info("realized");
204 canvas_ptr
->get()->scroll_to_center();
208 //log_info("NOT realized");
214 canvas_handle canvas
,
217 canvas_ptr
->get()->set_zoom(pix_per_unit
);
222 canvas_handle canvas
)
224 Glib::RefPtr
<Gdk::Window
> win
= canvas_ptr
->get()->get_window();
227 canvas_ptr
->get()->arrange();
232 canvas_create_module(
233 canvas_handle canvas
,
238 bool show_port_labels
,
239 void * module_context
,
240 canvas_module_handle
* module_handle_ptr
)
242 boost::shared_ptr
<FlowCanvas::Module
> * module
;
244 module
= new boost::shared_ptr
<FlowCanvas::Module
>(new module_cls(*canvas_ptr
, name
, x
, y
, show_title
, show_port_labels
, module_context
));
245 canvas_ptr
->get()->add_item(*module
);
246 module
->get()->resize();
248 *module_handle_ptr
= (canvas_module_handle
)module
;
253 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
256 canvas_destroy_module(
257 canvas_handle canvas
,
258 canvas_module_handle module
)
260 canvas_ptr
->get()->remove_item(*module_ptr
);
267 canvas_handle canvas
,
268 canvas_module_handle module
,
273 canvas_port_handle
* port_handle_ptr
)
275 boost::shared_ptr
<port_cls
> * port
;
277 port
= new boost::shared_ptr
<port_cls
>(new port_cls(*module_ptr
, name
, is_input
, color
, port_context
));
279 module_ptr
->get()->add_port(*port
);
280 module_ptr
->get()->resize();
282 *port_handle_ptr
= (canvas_port_handle
)port
;
288 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
292 canvas_handle canvas
,
293 canvas_port_handle port
)
295 boost::shared_ptr
<FlowCanvas::Module
> module
= port_ptr
->get()->module().lock();
296 module
->remove_port(*port_ptr
);
303 canvas_get_port_color(
304 canvas_port_handle port
)
306 return port_ptr
->get()->color();
310 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
311 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
314 canvas_add_connection(
315 canvas_handle canvas
,
316 canvas_port_handle port1
,
317 canvas_port_handle port2
,
320 canvas_ptr
->get()->add_connection(*port1_ptr
, *port2_ptr
, color
);
325 canvas_remove_connection(
326 canvas_handle canvas
,
327 canvas_port_handle port1
,
328 canvas_port_handle port2
)
330 canvas_ptr
->get()->remove_connection(*port1_ptr
, *port2_ptr
);
339 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
340 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
342 if (m_connect_request
!= NULL
)
344 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
345 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
346 m_connect_request(port1
->context
, port2
->context
);
351 canvas_cls::disconnect(
352 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
353 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
355 if (m_disconnect_request
!= NULL
)
357 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
358 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
359 m_disconnect_request(port1
->context
, port2
->context
);
365 canvas_handle canvas
,
366 void * callback_context
,
367 bool (* callback
)(void * context
, canvas_module_handle module
))
373 canvas_enum_module_ports(
374 canvas_handle canvas
,
375 canvas_module_handle module
,
376 void * callback_context
,
377 bool (* callback
)(void * context
, canvas_port_handle port
))