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
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 void (* fill_canvas_menu
)(GtkMenu
* menu
),
44 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
),
45 void (* fill_port_menu
)(GtkMenu
* menu
, void * port_context
))
46 : FlowCanvas::Canvas(width
, height
)
47 , m_connect_request(connect_request
)
48 , m_disconnect_request(disconnect_request
)
49 , m_module_location_changed(module_location_changed
)
50 , m_fill_canvas_menu(fill_canvas_menu
)
51 , m_fill_module_menu(fill_module_menu
)
52 , m_fill_port_menu(fill_port_menu
)
55 virtual ~canvas_cls() {}
57 virtual void on_realize()
59 log_info("canvas_cls::on_realize");
60 FlowCanvas::Canvas::on_realize();
64 virtual void on_size_allocate(Gtk::Allocation
& allocation
)
66 //log_info("canvas_cls::on_size_allocate");
67 FlowCanvas::Canvas::on_size_allocate(allocation
);
70 //log_info("... realized");
75 virtual bool canvas_event(GdkEvent
* event
);
77 virtual void connect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
78 virtual void disconnect(boost::shared_ptr
<FlowCanvas::Connectable
> port1
, boost::shared_ptr
<FlowCanvas::Connectable
> port2
);
80 void (* m_connect_request
)(void * port1_context
, void * port2_context
);
81 void (* m_disconnect_request
)(void * port1_context
, void * port2_context
);
82 void (* m_module_location_changed
)(void * module_context
, double x
, double y
);
83 void (* m_fill_canvas_menu
)(GtkMenu
* menu
);
84 void (* m_fill_module_menu
)(GtkMenu
* menu
, void * module_context
);
85 void (* m_fill_port_menu
)(GtkMenu
* menu
, void * port_context
);
88 class module_cls
: public FlowCanvas::Module
92 boost::shared_ptr
<FlowCanvas::Canvas
> canvas
,
93 const std::string
& name
,
97 bool show_port_labels
,
98 void * module_context
)
99 : FlowCanvas::Module(canvas
, name
, x
, y
, show_title
, show_port_labels
)
100 , m_context(module_context
)
103 virtual ~module_cls() {}
105 virtual void store_location()
107 boost::dynamic_pointer_cast
<canvas_cls
>(canvas().lock())->m_module_location_changed(m_context
, property_x(), property_y());
112 _menu
= new Gtk::Menu();
113 _menu
->signal_selection_done().connect(sigc::mem_fun(this, &module_cls::on_menu_hide
));
114 _menu
->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Disconnect All"), sigc::mem_fun(this, &module_cls::menu_disconnect_all
)));
115 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
) = boost::dynamic_pointer_cast
<canvas_cls
>(canvas().lock())->m_fill_module_menu
;
116 if (fill_module_menu
!= NULL
)
118 fill_module_menu(_menu
->gobj(), m_context
);
122 void menu_disconnect_all()
124 for (FlowCanvas::PortVector::iterator p
= _ports
.begin(); p
!= _ports
.end(); p
++)
126 (*p
)->disconnect_all();
139 class port_cls
: public FlowCanvas::Port
143 boost::shared_ptr
<FlowCanvas::Module
> module
,
144 const std::string
& name
,
148 : FlowCanvas::Port(module
, name
, is_input
, color
)
149 , m_context(port_context
)
152 virtual ~port_cls() {}
154 void popup_menu(guint button
, guint32 activate_time
)
158 _menu
->popup(button
, activate_time
);
161 virtual void create_menu()
163 void (* fill_port_menu
)(GtkMenu
* menu
, void * port_context
);
165 /* Cannot call FlowCanvas::Port::create_menu() because on_menu_hide() is not virtual */
166 _menu
= new Gtk::Menu();
167 _menu
->items().push_back(Gtk::Menu_Helpers::MenuElem("Disconnect All", sigc::mem_fun(this, &Port::disconnect_all
)));
168 _menu
->signal_selection_done().connect(sigc::mem_fun(this, &port_cls::on_menu_hide
));
170 fill_port_menu
= boost::dynamic_pointer_cast
<canvas_cls
>(module().lock()->canvas().lock())->m_fill_port_menu
;
171 if (fill_port_menu
!= NULL
)
173 fill_port_menu(_menu
->gobj(), m_context
);
190 Gnome::Canvas::init();
198 void (* connect_request
)(void * port1_context
, void * port2_context
),
199 void (* disconnect_request
)(void * port1_context
, void * port2_context
),
200 void (* module_location_changed
)(void * module_context
, double x
, double y
),
201 void (* fill_canvas_menu
)(GtkMenu
* menu
),
202 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
),
203 void (* fill_port_menu
)(GtkMenu
* menu
, void * port_context
),
204 canvas_handle
* canvas_handle_ptr
)
206 boost::shared_ptr
<canvas_cls
> * canvas
;
208 canvas
= new boost::shared_ptr
<canvas_cls
>(new canvas_cls(width
,
212 module_location_changed
,
217 *canvas_handle_ptr
= (canvas_handle
)canvas
;
222 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
226 canvas_handle canvas
)
228 return ((Gtk::Widget
*)canvas_ptr
->get())->gobj();
233 canvas_handle canvas
)
240 canvas_handle canvas
)
242 FlowCanvas::ItemList modules
= canvas_ptr
->get()->items(); // copy
243 for (FlowCanvas::ItemList::iterator m
= modules
.begin(); m
!= modules
.end(); ++m
)
245 boost::shared_ptr
<FlowCanvas::Module
> module
= boost::dynamic_pointer_cast
<FlowCanvas::Module
>(*m
);
249 FlowCanvas::PortVector ports
= module
->ports(); // copy
250 for (FlowCanvas::PortVector::iterator p
= ports
.begin(); p
!= ports
.end(); ++p
)
252 boost::shared_ptr
<FlowCanvas::Port
> port
= boost::dynamic_pointer_cast
<FlowCanvas::Port
>(*p
);
253 ASSERT(port
!= NULL
);
254 module
->remove_port(port
);
258 ASSERT(module
->ports().empty());
259 canvas_ptr
->get()->remove_item(module
);
265 canvas_handle canvas
,
269 *width_ptr
= canvas_ptr
->get()->width();
270 *height_ptr
= canvas_ptr
->get()->height();
274 canvas_scroll_to_center(
275 canvas_handle canvas
)
277 if (canvas_ptr
->get()->is_realized())
279 //log_info("realized");
280 canvas_ptr
->get()->scroll_to_center();
284 //log_info("NOT realized");
290 canvas_handle canvas
)
292 return canvas_ptr
->get()->get_zoom();
297 canvas_handle canvas
,
300 canvas_ptr
->get()->set_zoom(pix_per_unit
);
305 canvas_handle canvas
)
307 canvas_ptr
->get()->zoom_full();
312 canvas_handle canvas
)
314 Glib::RefPtr
<Gdk::Window
> win
= canvas_ptr
->get()->get_window();
317 canvas_ptr
->get()->arrange();
322 canvas_create_module(
323 canvas_handle canvas
,
328 bool show_port_labels
,
329 void * module_context
,
330 canvas_module_handle
* module_handle_ptr
)
332 boost::shared_ptr
<FlowCanvas::Module
> * module
;
334 module
= new boost::shared_ptr
<FlowCanvas::Module
>(new module_cls(*canvas_ptr
, name
, x
, y
, show_title
, show_port_labels
, module_context
));
335 canvas_ptr
->get()->add_item(*module
);
336 module
->get()->resize();
338 *module_handle_ptr
= (canvas_module_handle
)module
;
343 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
346 canvas_set_module_name(
347 canvas_module_handle module
,
350 module_ptr
->get()->set_name(name
);
354 canvas_destroy_module(
355 canvas_handle canvas
,
356 canvas_module_handle module
)
358 canvas_ptr
->get()->remove_item(*module_ptr
);
365 canvas_handle canvas
,
366 canvas_module_handle module
,
371 canvas_port_handle
* port_handle_ptr
)
373 boost::shared_ptr
<port_cls
> * port
;
375 port
= new boost::shared_ptr
<port_cls
>(new port_cls(*module_ptr
, name
, is_input
, color
, port_context
));
377 module_ptr
->get()->add_port(*port
);
378 module_ptr
->get()->resize();
380 *port_handle_ptr
= (canvas_port_handle
)port
;
386 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
390 canvas_handle canvas
,
391 canvas_port_handle port
)
393 boost::shared_ptr
<FlowCanvas::Module
> module
= port_ptr
->get()->module().lock();
394 module
->remove_port(*port_ptr
);
401 canvas_get_port_color(
402 canvas_port_handle port
)
404 return port_ptr
->get()->color();
408 canvas_set_port_name(
409 canvas_port_handle port
,
412 port_ptr
->get()->set_name(name
);
416 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
417 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
420 canvas_add_connection(
421 canvas_handle canvas
,
422 canvas_port_handle port1
,
423 canvas_port_handle port2
,
426 canvas_ptr
->get()->add_connection(*port1_ptr
, *port2_ptr
, color
);
431 canvas_remove_connection(
432 canvas_handle canvas
,
433 canvas_port_handle port1
,
434 canvas_port_handle port2
)
436 canvas_ptr
->get()->remove_connection(*port1_ptr
, *port2_ptr
);
443 bool canvas_cls::canvas_event(GdkEvent
* event
)
447 if (m_fill_canvas_menu
!= NULL
&& event
->type
== GDK_BUTTON_PRESS
&& event
->button
.button
== 3)
449 Gtk::Menu
* menu_ptr
;
450 menu_ptr
= new Gtk::Menu();
451 m_fill_canvas_menu(menu_ptr
->gobj());
452 menu_ptr
->show_all();
453 menu_ptr
->popup(event
->button
.button
, event
->button
.time
);
457 return Canvas::canvas_event(event
);
462 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
463 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
465 if (m_connect_request
!= NULL
)
467 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
468 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
469 m_connect_request(port1
->m_context
, port2
->m_context
);
474 canvas_cls::disconnect(
475 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
476 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
478 if (m_disconnect_request
!= NULL
)
480 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
481 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
482 m_disconnect_request(port1
->m_context
, port2
->m_context
);
488 canvas_handle canvas
,
489 void * callback_context
,
490 bool (* callback
)(void * context
, canvas_module_handle module
))
496 canvas_enum_module_ports(
497 canvas_handle canvas
,
498 canvas_module_handle module
,
499 void * callback_context
,
500 bool (* callback
)(void * context
, canvas_port_handle port
))