2009-07-29 22:00:37 +03:00
|
|
|
/* -*- Mode: C ; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
|
|
|
* LADI Session Handler (ladish)
|
|
|
|
*
|
2009-08-12 18:04:29 +03:00
|
|
|
* Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
|
|
|
|
* Copyright (C) 2007 Dave Robillard <http://drobilla.net>
|
2009-07-29 22:00:37 +03:00
|
|
|
*
|
|
|
|
**************************************************************************
|
|
|
|
* This file contains implements the canvas functionality through flowcanvas
|
|
|
|
**************************************************************************
|
|
|
|
*
|
|
|
|
* LADI Session Handler is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LADI Session Handler is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
* or write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2009-07-29 22:23:18 +03:00
|
|
|
#include <flowcanvas/Canvas.hpp>
|
|
|
|
#include <flowcanvas/Port.hpp>
|
|
|
|
#include <flowcanvas/Module.hpp>
|
|
|
|
|
2009-07-29 22:00:37 +03:00
|
|
|
#include "canvas.h"
|
|
|
|
|
2009-08-12 19:12:10 +03:00
|
|
|
class canvas_cls: public FlowCanvas::Canvas
|
2009-08-11 10:13:00 +03:00
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
public:
|
|
|
|
canvas_cls(
|
|
|
|
double width,
|
|
|
|
double height,
|
|
|
|
void (* connect_request)(void * port1_context, void * port2_context),
|
2009-09-07 20:53:56 +03:00
|
|
|
void (* disconnect_request)(void * port1_context, void * port2_context),
|
|
|
|
void (* module_location_changed)(void * module_context, double x, double y))
|
2009-08-12 19:12:10 +03:00
|
|
|
: FlowCanvas::Canvas(width, height)
|
|
|
|
, m_connect_request(connect_request)
|
|
|
|
, m_disconnect_request(disconnect_request)
|
2009-09-07 20:53:56 +03:00
|
|
|
, m_module_location_changed(module_location_changed)
|
2009-10-04 23:49:35 +03:00
|
|
|
{}
|
2009-08-12 19:12:10 +03:00
|
|
|
|
2009-10-04 23:49:35 +03:00
|
|
|
virtual ~canvas_cls() {}
|
2009-10-05 00:02:08 +03:00
|
|
|
|
|
|
|
virtual void on_realize()
|
|
|
|
{
|
2009-10-05 00:45:28 +03:00
|
|
|
log_info("canvas_cls::on_realize");
|
2009-10-05 00:02:08 +03:00
|
|
|
FlowCanvas::Canvas::on_realize();
|
2009-12-08 00:46:31 +02:00
|
|
|
scroll_to_center();
|
2009-10-05 00:45:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void on_size_allocate(Gtk::Allocation& allocation)
|
|
|
|
{
|
|
|
|
//log_info("canvas_cls::on_size_allocate");
|
|
|
|
FlowCanvas::Canvas::on_size_allocate(allocation);
|
|
|
|
if (is_realized())
|
|
|
|
{
|
|
|
|
//log_info("... realized");
|
|
|
|
scroll_to_center();
|
|
|
|
}
|
2009-10-05 00:02:08 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 19:27:27 +03:00
|
|
|
virtual void connect(boost::shared_ptr<FlowCanvas::Connectable> port1, boost::shared_ptr<FlowCanvas::Connectable> port2);
|
|
|
|
virtual void disconnect(boost::shared_ptr<FlowCanvas::Connectable> port1, boost::shared_ptr<FlowCanvas::Connectable> port2);
|
2009-08-12 19:12:10 +03:00
|
|
|
|
|
|
|
void (* m_connect_request)(void * port1_context, void * port2_context);
|
|
|
|
void (* m_disconnect_request)(void * port1_context, void * port2_context);
|
2009-09-07 20:53:56 +03:00
|
|
|
void (* m_module_location_changed)(void * module_context, double x, double y);
|
|
|
|
};
|
|
|
|
|
|
|
|
class module_cls: public FlowCanvas::Module
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
module_cls(
|
|
|
|
boost::shared_ptr<FlowCanvas::Canvas> canvas,
|
|
|
|
const std::string& name,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
bool show_title,
|
|
|
|
bool show_port_labels,
|
|
|
|
void * module_context)
|
|
|
|
: FlowCanvas::Module(canvas, name, x, y, show_title, show_port_labels)
|
|
|
|
, m_context(module_context)
|
2009-10-04 23:49:35 +03:00
|
|
|
{}
|
2009-09-07 20:53:56 +03:00
|
|
|
|
2009-10-04 23:49:35 +03:00
|
|
|
virtual ~module_cls() {}
|
2009-09-07 20:53:56 +03:00
|
|
|
|
|
|
|
virtual void store_location()
|
|
|
|
{
|
|
|
|
boost::dynamic_pointer_cast<canvas_cls>(canvas().lock())->m_module_location_changed(m_context, property_x(), property_y());
|
|
|
|
}
|
|
|
|
|
2009-12-27 14:05:10 +02:00
|
|
|
void create_menu()
|
|
|
|
{
|
|
|
|
_menu = new Gtk::Menu();
|
|
|
|
_menu->items().push_back(Gtk::Menu_Helpers::MenuElem("Disconnect All", sigc::mem_fun(this, &module_cls::menu_disconnect_all)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_disconnect_all()
|
|
|
|
{
|
|
|
|
for (FlowCanvas::PortVector::iterator p = _ports.begin(); p != _ports.end(); p++)
|
|
|
|
{
|
|
|
|
(*p)->disconnect_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-07 20:53:56 +03:00
|
|
|
void * m_context;
|
2009-08-12 19:12:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class port_cls: public FlowCanvas::Port
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
port_cls(
|
|
|
|
boost::shared_ptr<FlowCanvas::Module> module,
|
|
|
|
const std::string& name,
|
|
|
|
bool is_input,
|
|
|
|
uint32_t color,
|
|
|
|
void * port_context)
|
|
|
|
: FlowCanvas::Port(module, name, is_input, color)
|
|
|
|
, context(port_context)
|
2009-10-04 23:49:35 +03:00
|
|
|
{}
|
2009-08-12 19:12:10 +03:00
|
|
|
|
2009-10-04 23:49:35 +03:00
|
|
|
virtual ~port_cls() {}
|
2009-08-12 19:12:10 +03:00
|
|
|
|
|
|
|
void * context;
|
2009-08-11 10:13:00 +03:00
|
|
|
};
|
|
|
|
|
2009-08-17 04:17:26 +03:00
|
|
|
bool
|
|
|
|
canvas_init(
|
|
|
|
void)
|
|
|
|
{
|
|
|
|
Gnome::Canvas::init();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-29 22:00:37 +03:00
|
|
|
bool
|
|
|
|
canvas_create(
|
2009-08-12 19:12:10 +03:00
|
|
|
double width,
|
|
|
|
double height,
|
|
|
|
void (* connect_request)(void * port1_context, void * port2_context),
|
|
|
|
void (* disconnect_request)(void * port1_context, void * port2_context),
|
2009-09-07 20:53:56 +03:00
|
|
|
void (* module_location_changed)(void * module_context, double x, double y),
|
2009-07-29 22:00:37 +03:00
|
|
|
canvas_handle * canvas_handle_ptr)
|
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
boost::shared_ptr<canvas_cls> * canvas;
|
2009-08-11 10:13:00 +03:00
|
|
|
|
2009-09-07 20:53:56 +03:00
|
|
|
canvas = new boost::shared_ptr<canvas_cls>(new canvas_cls(width, height, connect_request, disconnect_request, module_location_changed));
|
2009-08-11 10:13:00 +03:00
|
|
|
|
2009-08-12 19:12:10 +03:00
|
|
|
*canvas_handle_ptr = (canvas_handle)canvas;
|
2009-08-11 10:13:00 +03:00
|
|
|
|
|
|
|
return true;
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 19:12:10 +03:00
|
|
|
#define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
|
2009-08-11 10:13:00 +03:00
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
canvas_get_widget(
|
|
|
|
canvas_handle canvas)
|
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
return ((Gtk::Widget *)canvas_ptr->get())->gobj();
|
2009-08-11 10:13:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-29 22:00:37 +03:00
|
|
|
canvas_destroy(
|
|
|
|
canvas_handle canvas)
|
|
|
|
{
|
2009-08-11 10:13:00 +03:00
|
|
|
delete canvas_ptr;
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
canvas_clear(
|
|
|
|
canvas_handle canvas)
|
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
FlowCanvas::ItemList modules = canvas_ptr->get()->items(); // copy
|
2009-08-12 18:04:29 +03:00
|
|
|
for (FlowCanvas::ItemList::iterator m = modules.begin(); m != modules.end(); ++m)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<FlowCanvas::Module> module = boost::dynamic_pointer_cast<FlowCanvas::Module>(*m);
|
|
|
|
if (!module)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FlowCanvas::PortVector ports = module->ports(); // copy
|
|
|
|
for (FlowCanvas::PortVector::iterator p = ports.begin(); p != ports.end(); ++p)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<FlowCanvas::Port> port = boost::dynamic_pointer_cast<FlowCanvas::Port>(*p);
|
2009-09-20 18:48:42 +03:00
|
|
|
ASSERT(port != NULL);
|
2009-08-12 18:04:29 +03:00
|
|
|
module->remove_port(port);
|
|
|
|
port->hide();
|
|
|
|
}
|
|
|
|
|
2009-09-20 18:48:42 +03:00
|
|
|
ASSERT(module->ports().empty());
|
2009-08-12 19:12:10 +03:00
|
|
|
canvas_ptr->get()->remove_item(module);
|
2009-08-12 18:04:29 +03:00
|
|
|
}
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
2009-10-05 00:15:53 +03:00
|
|
|
void
|
|
|
|
canvas_get_size(
|
|
|
|
canvas_handle canvas,
|
|
|
|
double * width_ptr,
|
|
|
|
double * height_ptr)
|
|
|
|
{
|
|
|
|
*width_ptr = canvas_ptr->get()->width();
|
|
|
|
*height_ptr = canvas_ptr->get()->height();
|
|
|
|
}
|
|
|
|
|
2009-10-05 00:02:53 +03:00
|
|
|
void
|
|
|
|
canvas_scroll_to_center(
|
|
|
|
canvas_handle canvas)
|
|
|
|
{
|
2009-10-05 00:45:28 +03:00
|
|
|
if (canvas_ptr->get()->is_realized())
|
|
|
|
{
|
|
|
|
//log_info("realized");
|
|
|
|
canvas_ptr->get()->scroll_to_center();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//log_info("NOT realized");
|
|
|
|
}
|
2009-10-05 00:02:53 +03:00
|
|
|
}
|
|
|
|
|
2009-07-29 22:00:37 +03:00
|
|
|
void
|
|
|
|
canvas_set_zoom(
|
|
|
|
canvas_handle canvas,
|
|
|
|
double pix_per_unit)
|
|
|
|
{
|
2009-08-12 19:27:27 +03:00
|
|
|
canvas_ptr->get()->set_zoom(pix_per_unit);
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
canvas_arrange(
|
|
|
|
canvas_handle canvas)
|
|
|
|
{
|
2009-08-12 19:27:07 +03:00
|
|
|
Glib::RefPtr<Gdk::Window> win = canvas_ptr->get()->get_window();
|
2009-12-27 01:19:07 +02:00
|
|
|
if (!win)
|
2009-08-12 19:27:07 +03:00
|
|
|
{
|
2009-12-27 01:19:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas_ptr->get()->arrange();
|
|
|
|
|
|
|
|
// arrange does not cause locations stored, emulate it
|
|
|
|
FlowCanvas::ItemList modules = canvas_ptr->get()->items(); // copy
|
|
|
|
for (FlowCanvas::ItemList::iterator m = modules.begin(); m != modules.end(); ++m)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<FlowCanvas::Module> module = boost::dynamic_pointer_cast<FlowCanvas::Module>(*m);
|
|
|
|
if (!module)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
module->store_location();
|
2009-08-12 19:27:07 +03:00
|
|
|
}
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
canvas_create_module(
|
|
|
|
canvas_handle canvas,
|
|
|
|
const char * name,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
bool show_title,
|
|
|
|
bool show_port_labels,
|
2009-09-07 20:53:56 +03:00
|
|
|
void * module_context,
|
2009-07-29 22:00:37 +03:00
|
|
|
canvas_module_handle * module_handle_ptr)
|
|
|
|
{
|
2009-08-12 18:04:29 +03:00
|
|
|
boost::shared_ptr<FlowCanvas::Module> * module;
|
|
|
|
|
2009-09-07 20:53:56 +03:00
|
|
|
module = new boost::shared_ptr<FlowCanvas::Module>(new module_cls(*canvas_ptr, name, x, y, show_title, show_port_labels, module_context));
|
2009-08-12 19:12:10 +03:00
|
|
|
canvas_ptr->get()->add_item(*module);
|
2009-08-12 18:04:29 +03:00
|
|
|
module->get()->resize();
|
|
|
|
|
|
|
|
*module_handle_ptr = (canvas_module_handle)module;
|
|
|
|
|
|
|
|
return true;
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 18:04:29 +03:00
|
|
|
#define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
|
|
|
|
|
2009-12-26 22:46:20 +02:00
|
|
|
void
|
|
|
|
canvas_set_module_name(
|
|
|
|
canvas_module_handle module,
|
|
|
|
const char * name)
|
|
|
|
{
|
|
|
|
module_ptr->get()->set_name(name);
|
|
|
|
}
|
|
|
|
|
2009-07-29 22:00:37 +03:00
|
|
|
bool
|
|
|
|
canvas_destroy_module(
|
|
|
|
canvas_handle canvas,
|
|
|
|
canvas_module_handle module)
|
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
canvas_ptr->get()->remove_item(*module_ptr);
|
2009-08-12 18:04:29 +03:00
|
|
|
delete module_ptr;
|
|
|
|
return true;
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
canvas_create_port(
|
|
|
|
canvas_handle canvas,
|
|
|
|
canvas_module_handle module,
|
|
|
|
const char * name,
|
|
|
|
bool is_input,
|
2009-08-12 18:04:29 +03:00
|
|
|
int color,
|
2009-08-12 19:12:10 +03:00
|
|
|
void * port_context,
|
2009-08-12 18:04:29 +03:00
|
|
|
canvas_port_handle * port_handle_ptr)
|
2009-07-29 22:00:37 +03:00
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
boost::shared_ptr<port_cls> * port;
|
|
|
|
|
|
|
|
port = new boost::shared_ptr<port_cls>(new port_cls(*module_ptr, name, is_input, color, port_context));
|
2009-08-12 18:04:29 +03:00
|
|
|
|
|
|
|
module_ptr->get()->add_port(*port);
|
|
|
|
module_ptr->get()->resize();
|
|
|
|
|
|
|
|
*port_handle_ptr = (canvas_port_handle)port;
|
|
|
|
|
|
|
|
return true;
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 18:04:29 +03:00
|
|
|
#undef module_ptr
|
2009-08-12 19:12:10 +03:00
|
|
|
#define port_ptr ((boost::shared_ptr<port_cls> *)port)
|
2009-08-12 18:04:29 +03:00
|
|
|
|
2009-07-29 22:00:37 +03:00
|
|
|
bool
|
|
|
|
canvas_destroy_port(
|
|
|
|
canvas_handle canvas,
|
|
|
|
canvas_port_handle port)
|
|
|
|
{
|
2009-08-12 18:04:29 +03:00
|
|
|
boost::shared_ptr<FlowCanvas::Module> module = port_ptr->get()->module().lock();
|
|
|
|
module->remove_port(*port_ptr);
|
|
|
|
delete port_ptr;
|
|
|
|
module->resize();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
canvas_get_port_color(
|
|
|
|
canvas_port_handle port)
|
|
|
|
{
|
|
|
|
return port_ptr->get()->color();
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
2009-12-21 20:46:14 +02:00
|
|
|
void
|
|
|
|
canvas_set_port_name(
|
|
|
|
canvas_port_handle port,
|
|
|
|
const char * name)
|
|
|
|
{
|
|
|
|
port_ptr->get()->set_name(name);
|
|
|
|
}
|
|
|
|
|
2009-08-12 18:04:29 +03:00
|
|
|
#undef port_ptr
|
2009-08-12 19:12:10 +03:00
|
|
|
#define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
|
|
|
|
#define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
|
2009-08-12 18:04:29 +03:00
|
|
|
|
2009-07-29 22:00:37 +03:00
|
|
|
bool
|
|
|
|
canvas_add_connection(
|
|
|
|
canvas_handle canvas,
|
|
|
|
canvas_port_handle port1,
|
|
|
|
canvas_port_handle port2,
|
|
|
|
uint32_t color)
|
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
canvas_ptr->get()->add_connection(*port1_ptr, *port2_ptr, color);
|
2009-08-12 18:04:29 +03:00
|
|
|
return true;
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
canvas_remove_connection(
|
|
|
|
canvas_handle canvas,
|
|
|
|
canvas_port_handle port1,
|
|
|
|
canvas_port_handle port2)
|
|
|
|
{
|
2009-08-12 19:12:10 +03:00
|
|
|
canvas_ptr->get()->remove_connection(*port1_ptr, *port2_ptr);
|
2009-08-12 18:04:29 +03:00
|
|
|
return true;
|
2009-07-29 22:00:37 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 18:04:29 +03:00
|
|
|
#undef port1_ptr
|
|
|
|
#undef port2_ptr
|
|
|
|
|
2009-08-12 19:12:10 +03:00
|
|
|
void
|
|
|
|
canvas_cls::connect(
|
|
|
|
boost::shared_ptr<FlowCanvas::Connectable> c1,
|
|
|
|
boost::shared_ptr<FlowCanvas::Connectable> c2)
|
|
|
|
{
|
|
|
|
if (m_connect_request != NULL)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
|
|
|
|
boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
|
|
|
|
m_connect_request(port1->context, port2->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
canvas_cls::disconnect(
|
|
|
|
boost::shared_ptr<FlowCanvas::Connectable> c1,
|
|
|
|
boost::shared_ptr<FlowCanvas::Connectable> c2)
|
|
|
|
{
|
|
|
|
if (m_disconnect_request != NULL)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
|
|
|
|
boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
|
|
|
|
m_disconnect_request(port1->context, port2->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-29 22:00:37 +03:00
|
|
|
bool
|
|
|
|
canvas_enum_modules(
|
|
|
|
canvas_handle canvas,
|
|
|
|
void * callback_context,
|
|
|
|
bool (* callback)(void * context, canvas_module_handle module))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
canvas_enum_module_ports(
|
|
|
|
canvas_handle canvas,
|
|
|
|
canvas_module_handle module,
|
|
|
|
void * callback_context,
|
|
|
|
bool (* callback)(void * context, canvas_port_handle port))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|