1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009, 2010, 2011 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
41 void (* connect_request
)(void * port1_context
, void * port2_context
),
42 void (* disconnect_request
)(void * port1_context
, void * port2_context
),
43 void (* module_location_changed
)(void * module_context
, double x
, double y
),
44 void (* fill_canvas_menu
)(GtkMenu
* menu
, void * canvas_context
),
45 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
),
46 void (* fill_port_menu
)(GtkMenu
* menu
, void * port_context
))
47 : FlowCanvas::Canvas(width
, height
)
49 , m_connect_request(connect_request
)
50 , m_disconnect_request(disconnect_request
)
51 , m_module_location_changed(module_location_changed
)
52 , m_fill_canvas_menu(fill_canvas_menu
)
53 , m_fill_module_menu(fill_module_menu
)
54 , m_fill_port_menu(fill_port_menu
)
57 virtual ~canvas_cls() {}
59 virtual void on_realize()
61 log_info("canvas_cls::on_realize");
62 FlowCanvas::Canvas::on_realize();
66 virtual void on_size_allocate(Gtk::Allocation
& allocation
)
68 //log_info("canvas_cls::on_size_allocate");
69 FlowCanvas::Canvas::on_size_allocate(allocation
);
72 //log_info("... realized");
77 virtual bool canvas_event(GdkEvent
* event
);
79 virtual void connect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
80 virtual void disconnect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
83 void (* m_connect_request
)(void * port1_context
, void * port2_context
);
84 void (* m_disconnect_request
)(void * port1_context
, void * port2_context
);
85 void (* m_module_location_changed
)(void * module_context
, double x
, double y
);
86 void (* m_fill_canvas_menu
)(GtkMenu
* menu
, void * canvas_context
);
87 void (* m_fill_module_menu
)(GtkMenu
* menu
, void * module_context
);
88 void (* m_fill_port_menu
)(GtkMenu
* menu
, void * port_context
);
91 class module_cls
: public FlowCanvas::Module
95 boost::shared_ptr
<FlowCanvas::Canvas
> canvas
,
96 const std::string
& name
,
100 bool show_port_labels
,
101 void * module_context
)
102 : FlowCanvas::Module(canvas
, name
, x
, y
, show_title
, show_port_labels
)
103 , m_context(module_context
)
106 virtual ~module_cls() {}
108 virtual void store_location()
110 boost::dynamic_pointer_cast
<canvas_cls
>(canvas().lock())->m_module_location_changed(m_context
, property_x(), property_y());
115 _menu
= new Gtk::Menu();
116 _menu
->signal_selection_done().connect(sigc::mem_fun(this, &module_cls::on_menu_hide
));
117 _menu
->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Disconnect All"), sigc::mem_fun(this, &module_cls::menu_disconnect_all
)));
118 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
) = boost::dynamic_pointer_cast
<canvas_cls
>(canvas().lock())->m_fill_module_menu
;
119 if (fill_module_menu
!= NULL
)
121 fill_module_menu(_menu
->gobj(), m_context
);
126 void menu_disconnect_all()
128 for (FlowCanvas::PortVector::iterator p
= _ports
.begin(); p
!= _ports
.end(); p
++)
130 (*p
)->disconnect_all();
143 class port_cls
: public FlowCanvas::Port
147 boost::shared_ptr
<FlowCanvas::Module
> module
,
148 const std::string
& name
,
152 : FlowCanvas::Port(module
, name
, is_input
, color
)
153 , m_context(port_context
)
156 virtual ~port_cls() {}
158 void popup_menu(guint button
, guint32 activate_time
)
162 _menu
->popup(button
, activate_time
);
165 virtual void create_menu()
167 void (* fill_port_menu
)(GtkMenu
* menu
, void * port_context
);
169 /* Cannot call FlowCanvas::Port::create_menu() because on_menu_hide() is not virtual */
170 _menu
= new Gtk::Menu();
171 _menu
->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Disconnect All"), sigc::mem_fun(this, &Port::disconnect_all
)));
172 _menu
->signal_selection_done().connect(sigc::mem_fun(this, &port_cls::on_menu_hide
));
174 fill_port_menu
= boost::dynamic_pointer_cast
<canvas_cls
>(module().lock()->canvas().lock())->m_fill_port_menu
;
175 if (fill_port_menu
!= NULL
)
177 fill_port_menu(_menu
->gobj(), m_context
);
195 Gnome::Canvas::init();
203 void * canvas_context
,
204 void (* connect_request
)(void * port1_context
, void * port2_context
),
205 void (* disconnect_request
)(void * port1_context
, void * port2_context
),
206 void (* module_location_changed
)(void * module_context
, double x
, double y
),
207 void (* fill_canvas_menu
)(GtkMenu
* menu
, void * canvas_context
),
208 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
),
209 void (* fill_port_menu
)(GtkMenu
* menu
, void * port_context
),
210 canvas_handle
* canvas_handle_ptr
)
212 boost::shared_ptr
<canvas_cls
> * canvas
;
214 canvas
= new boost::shared_ptr
<canvas_cls
>(new canvas_cls(width
,
219 module_location_changed
,
224 *canvas_handle_ptr
= (canvas_handle
)canvas
;
229 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
233 canvas_handle canvas
)
235 return ((Gtk::Widget
*)canvas_ptr
->get())->gobj();
240 canvas_handle canvas
)
247 canvas_handle canvas
)
249 FlowCanvas::ItemList modules
= canvas_ptr
->get()->items(); // copy
250 for (FlowCanvas::ItemList::iterator m
= modules
.begin(); m
!= modules
.end(); ++m
)
252 boost::shared_ptr
<FlowCanvas::Module
> module
= boost::dynamic_pointer_cast
<FlowCanvas::Module
>(*m
);
256 FlowCanvas::PortVector ports
= module
->ports(); // copy
257 for (FlowCanvas::PortVector::iterator p
= ports
.begin(); p
!= ports
.end(); ++p
)
259 boost::shared_ptr
<FlowCanvas::Port
> port
= boost::dynamic_pointer_cast
<FlowCanvas::Port
>(*p
);
260 ASSERT(port
!= NULL
);
261 module
->remove_port(port
);
265 ASSERT(module
->ports().empty());
266 canvas_ptr
->get()->remove_item(module
);
272 canvas_handle canvas
,
276 *width_ptr
= canvas_ptr
->get()->width();
277 *height_ptr
= canvas_ptr
->get()->height();
281 canvas_scroll_to_center(
282 canvas_handle canvas
)
284 if (canvas_ptr
->get()->is_realized())
286 //log_info("realized");
287 canvas_ptr
->get()->scroll_to_center();
291 //log_info("NOT realized");
297 canvas_handle canvas
)
299 return canvas_ptr
->get()->get_zoom();
304 canvas_handle canvas
,
307 canvas_ptr
->get()->set_zoom(pix_per_unit
);
312 canvas_handle canvas
)
314 canvas_ptr
->get()->zoom_full();
319 canvas_handle canvas
)
321 Glib::RefPtr
<Gdk::Window
> win
= canvas_ptr
->get()->get_window();
324 canvas_ptr
->get()->arrange();
329 canvas_get_selected_modules_count(
330 canvas_handle canvas
)
332 return canvas_ptr
->get()->selected_items().size();
336 canvas_get_one_selected_module(
337 canvas_handle canvas
,
338 void ** module_context_ptr
)
342 std::list
<boost::shared_ptr
<FlowCanvas::Item
> > modules
= canvas_ptr
->get()->selected_items();
343 if (modules
.size() != 1)
349 for (std::list
<boost::shared_ptr
<FlowCanvas::Item
> >::iterator m
= modules
.begin(); m
!= modules
.end(); ++m
)
351 boost::shared_ptr
<module_cls
> module
= boost::dynamic_pointer_cast
<module_cls
>(*m
);
360 *module_context_ptr
= module
->m_context
;
376 canvas_get_two_selected_modules(
377 canvas_handle canvas
,
378 void ** module1_context_ptr
,
379 void ** module2_context_ptr
)
383 std::list
<boost::shared_ptr
<FlowCanvas::Item
> > modules
= canvas_ptr
->get()->selected_items();
384 if (modules
.size() != 2)
390 for (std::list
<boost::shared_ptr
<FlowCanvas::Item
> >::iterator m
= modules
.begin(); m
!= modules
.end(); ++m
)
392 boost::shared_ptr
<module_cls
> module
= boost::dynamic_pointer_cast
<module_cls
>(*m
);
402 *module1_context_ptr
= module
->m_context
;
406 *module2_context_ptr
= module
->m_context
;
422 canvas_create_module(
423 canvas_handle canvas
,
428 bool show_port_labels
,
429 void * module_context
,
430 canvas_module_handle
* module_handle_ptr
)
432 boost::shared_ptr
<FlowCanvas::Module
> * module
;
434 module
= new boost::shared_ptr
<FlowCanvas::Module
>(new module_cls(*canvas_ptr
, name
, x
, y
, show_title
, show_port_labels
, module_context
));
435 canvas_ptr
->get()->add_item(*module
);
436 module
->get()->resize();
438 *module_handle_ptr
= (canvas_module_handle
)module
;
443 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
446 canvas_set_module_name(
447 canvas_module_handle module
,
450 module_ptr
->get()->set_name(name
);
451 module_ptr
->get()->resize();
455 canvas_get_module_name(
456 canvas_module_handle module
)
458 return module_ptr
->get()->name().c_str();
462 canvas_destroy_module(
463 canvas_handle canvas
,
464 canvas_module_handle module
)
466 canvas_ptr
->get()->remove_item(*module_ptr
);
473 canvas_handle canvas
,
474 canvas_module_handle module
,
479 canvas_port_handle
* port_handle_ptr
)
481 boost::shared_ptr
<port_cls
> * port
;
483 port
= new boost::shared_ptr
<port_cls
>(new port_cls(*module_ptr
, name
, is_input
, color
, port_context
));
485 module_ptr
->get()->add_port(*port
);
486 module_ptr
->get()->resize();
488 *port_handle_ptr
= (canvas_port_handle
)port
;
494 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
498 canvas_handle canvas
,
499 canvas_port_handle port
)
501 boost::shared_ptr
<FlowCanvas::Module
> module
= port_ptr
->get()->module().lock();
502 module
->remove_port(*port_ptr
);
509 canvas_get_port_color(
510 canvas_port_handle port
)
512 return port_ptr
->get()->color();
516 canvas_set_port_name(
517 canvas_port_handle port
,
520 port_ptr
->get()->set_name(name
);
521 port_ptr
->get()->module().lock()->resize();
525 canvas_get_port_name(
526 canvas_port_handle port
)
528 return port_ptr
->get()->name().c_str();
532 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
533 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
536 canvas_add_connection(
537 canvas_handle canvas
,
538 canvas_port_handle port1
,
539 canvas_port_handle port2
,
542 canvas_ptr
->get()->add_connection(*port1_ptr
, *port2_ptr
, color
);
547 canvas_remove_connection(
548 canvas_handle canvas
,
549 canvas_port_handle port1
,
550 canvas_port_handle port2
)
552 canvas_ptr
->get()->remove_connection(*port1_ptr
, *port2_ptr
);
559 bool canvas_cls::canvas_event(GdkEvent
* event
)
563 if (m_fill_canvas_menu
!= NULL
&& event
->type
== GDK_BUTTON_PRESS
&& event
->button
.button
== 3)
565 Gtk::Menu
* menu_ptr
;
566 menu_ptr
= new Gtk::Menu();
567 m_fill_canvas_menu(menu_ptr
->gobj(), m_context
);
568 menu_ptr
->show_all();
569 menu_ptr
->popup(event
->button
.button
, event
->button
.time
);
573 return Canvas::canvas_event(event
);
578 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
579 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
581 if (m_connect_request
!= NULL
)
583 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
584 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
585 m_connect_request(port1
->m_context
, port2
->m_context
);
590 canvas_cls::disconnect(
591 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
592 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
594 if (m_disconnect_request
!= NULL
)
596 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
597 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
598 m_disconnect_request(port1
->m_context
, port2
->m_context
);
604 canvas_handle canvas
,
605 void * callback_context
,
606 bool (* callback
)(void * context
, canvas_module_handle module
))
612 canvas_enum_module_ports(
613 canvas_handle canvas
,
614 canvas_module_handle module
,
615 void * callback_context
,
616 bool (* callback
)(void * context
, canvas_port_handle port
))