1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009, 2010 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
->items().push_back(Gtk::Menu_Helpers::MenuElem("Disconnect All", sigc::mem_fun(this, &module_cls::menu_disconnect_all
)));
114 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
) = boost::dynamic_pointer_cast
<canvas_cls
>(canvas().lock())->m_fill_module_menu
;
115 if (fill_module_menu
!= NULL
)
117 fill_module_menu(_menu
->gobj(), m_context
);
121 void menu_disconnect_all()
123 for (FlowCanvas::PortVector::iterator p
= _ports
.begin(); p
!= _ports
.end(); p
++)
125 (*p
)->disconnect_all();
132 class port_cls
: public FlowCanvas::Port
136 boost::shared_ptr
<FlowCanvas::Module
> module
,
137 const std::string
& name
,
141 : FlowCanvas::Port(module
, name
, is_input
, color
)
142 , context(port_context
)
145 virtual ~port_cls() {}
154 Gnome::Canvas::init();
162 void (* connect_request
)(void * port1_context
, void * port2_context
),
163 void (* disconnect_request
)(void * port1_context
, void * port2_context
),
164 void (* module_location_changed
)(void * module_context
, double x
, double y
),
165 void (* fill_canvas_menu
)(GtkMenu
* menu
),
166 void (* fill_module_menu
)(GtkMenu
* menu
, void * module_context
),
167 void (* fill_port_menu
)(GtkMenu
* menu
, void * port_context
),
168 canvas_handle
* canvas_handle_ptr
)
170 boost::shared_ptr
<canvas_cls
> * canvas
;
172 canvas
= new boost::shared_ptr
<canvas_cls
>(new canvas_cls(width
,
176 module_location_changed
,
181 *canvas_handle_ptr
= (canvas_handle
)canvas
;
186 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
190 canvas_handle canvas
)
192 return ((Gtk::Widget
*)canvas_ptr
->get())->gobj();
197 canvas_handle canvas
)
204 canvas_handle canvas
)
206 FlowCanvas::ItemList modules
= canvas_ptr
->get()->items(); // copy
207 for (FlowCanvas::ItemList::iterator m
= modules
.begin(); m
!= modules
.end(); ++m
)
209 boost::shared_ptr
<FlowCanvas::Module
> module
= boost::dynamic_pointer_cast
<FlowCanvas::Module
>(*m
);
213 FlowCanvas::PortVector ports
= module
->ports(); // copy
214 for (FlowCanvas::PortVector::iterator p
= ports
.begin(); p
!= ports
.end(); ++p
)
216 boost::shared_ptr
<FlowCanvas::Port
> port
= boost::dynamic_pointer_cast
<FlowCanvas::Port
>(*p
);
217 ASSERT(port
!= NULL
);
218 module
->remove_port(port
);
222 ASSERT(module
->ports().empty());
223 canvas_ptr
->get()->remove_item(module
);
229 canvas_handle canvas
,
233 *width_ptr
= canvas_ptr
->get()->width();
234 *height_ptr
= canvas_ptr
->get()->height();
238 canvas_scroll_to_center(
239 canvas_handle canvas
)
241 if (canvas_ptr
->get()->is_realized())
243 //log_info("realized");
244 canvas_ptr
->get()->scroll_to_center();
248 //log_info("NOT realized");
254 canvas_handle canvas
)
256 return canvas_ptr
->get()->get_zoom();
261 canvas_handle canvas
,
264 canvas_ptr
->get()->set_zoom(pix_per_unit
);
269 canvas_handle canvas
)
271 canvas_ptr
->get()->zoom_full();
276 canvas_handle canvas
)
278 Glib::RefPtr
<Gdk::Window
> win
= canvas_ptr
->get()->get_window();
281 canvas_ptr
->get()->arrange();
286 canvas_create_module(
287 canvas_handle canvas
,
292 bool show_port_labels
,
293 void * module_context
,
294 canvas_module_handle
* module_handle_ptr
)
296 boost::shared_ptr
<FlowCanvas::Module
> * module
;
298 module
= new boost::shared_ptr
<FlowCanvas::Module
>(new module_cls(*canvas_ptr
, name
, x
, y
, show_title
, show_port_labels
, module_context
));
299 canvas_ptr
->get()->add_item(*module
);
300 module
->get()->resize();
302 *module_handle_ptr
= (canvas_module_handle
)module
;
307 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
310 canvas_set_module_name(
311 canvas_module_handle module
,
314 module_ptr
->get()->set_name(name
);
318 canvas_destroy_module(
319 canvas_handle canvas
,
320 canvas_module_handle module
)
322 canvas_ptr
->get()->remove_item(*module_ptr
);
329 canvas_handle canvas
,
330 canvas_module_handle module
,
335 canvas_port_handle
* port_handle_ptr
)
337 boost::shared_ptr
<port_cls
> * port
;
339 port
= new boost::shared_ptr
<port_cls
>(new port_cls(*module_ptr
, name
, is_input
, color
, port_context
));
341 module_ptr
->get()->add_port(*port
);
342 module_ptr
->get()->resize();
344 *port_handle_ptr
= (canvas_port_handle
)port
;
350 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
354 canvas_handle canvas
,
355 canvas_port_handle port
)
357 boost::shared_ptr
<FlowCanvas::Module
> module
= port_ptr
->get()->module().lock();
358 module
->remove_port(*port_ptr
);
365 canvas_get_port_color(
366 canvas_port_handle port
)
368 return port_ptr
->get()->color();
372 canvas_set_port_name(
373 canvas_port_handle port
,
376 port_ptr
->get()->set_name(name
);
380 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
381 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
384 canvas_add_connection(
385 canvas_handle canvas
,
386 canvas_port_handle port1
,
387 canvas_port_handle port2
,
390 canvas_ptr
->get()->add_connection(*port1_ptr
, *port2_ptr
, color
);
395 canvas_remove_connection(
396 canvas_handle canvas
,
397 canvas_port_handle port1
,
398 canvas_port_handle port2
)
400 canvas_ptr
->get()->remove_connection(*port1_ptr
, *port2_ptr
);
407 bool canvas_cls::canvas_event(GdkEvent
* event
)
411 if (m_fill_canvas_menu
!= NULL
&& event
->type
== GDK_BUTTON_PRESS
&& event
->button
.button
== 3)
413 Gtk::Menu
* menu_ptr
;
414 menu_ptr
= new Gtk::Menu();
415 m_fill_canvas_menu(menu_ptr
->gobj());
416 menu_ptr
->show_all();
417 menu_ptr
->popup(event
->button
.button
, event
->button
.time
);
421 return Canvas::canvas_event(event
);
426 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
427 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
429 if (m_connect_request
!= NULL
)
431 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
432 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
433 m_connect_request(port1
->context
, port2
->context
);
438 canvas_cls::disconnect(
439 boost::shared_ptr
<FlowCanvas::Connectable
> c1
,
440 boost::shared_ptr
<FlowCanvas::Connectable
> c2
)
442 if (m_disconnect_request
!= NULL
)
444 boost::shared_ptr
<port_cls
> port1
= boost::dynamic_pointer_cast
<port_cls
>(c1
);
445 boost::shared_ptr
<port_cls
> port2
= boost::dynamic_pointer_cast
<port_cls
>(c2
);
446 m_disconnect_request(port1
->context
, port2
->context
);
452 canvas_handle canvas
,
453 void * callback_context
,
454 bool (* callback
)(void * context
, canvas_module_handle module
))
460 canvas_enum_module_ports(
461 canvas_handle canvas
,
462 canvas_module_handle module
,
463 void * callback_context
,
464 bool (* callback
)(void * context
, canvas_port_handle port
))