1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
|
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2008, 2009, 2010, 2011 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2007 Dave Robillard <http://drobilla.net>
*
**************************************************************************
* 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.
*/
#include <flowcanvas/Canvas.hpp>
#include <flowcanvas/Port.hpp>
#include <flowcanvas/Module.hpp>
#include "canvas.h"
class canvas_cls: public FlowCanvas::Canvas
{
public:
canvas_cls(
double width,
double height,
void * context,
void (* connect_request)(void * port1_context, void * port2_context),
void (* disconnect_request)(void * port1_context, void * port2_context),
void (* module_location_changed)(void * module_context, double x, double y),
void (* fill_canvas_menu)(GtkMenu * menu, void * canvas_context),
void (* fill_module_menu)(GtkMenu * menu, void * module_context),
void (* fill_port_menu)(GtkMenu * menu, void * port_context))
: FlowCanvas::Canvas(width, height)
, m_context(context)
, m_connect_request(connect_request)
, m_disconnect_request(disconnect_request)
, m_module_location_changed(module_location_changed)
, m_fill_canvas_menu(fill_canvas_menu)
, m_fill_module_menu(fill_module_menu)
, m_fill_port_menu(fill_port_menu)
{}
virtual ~canvas_cls() {}
virtual void on_realize()
{
log_info("canvas_cls::on_realize");
FlowCanvas::Canvas::on_realize();
scroll_to_center();
}
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();
}
}
virtual bool canvas_event(GdkEvent * event);
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);
void * m_context;
void (* m_connect_request)(void * port1_context, void * port2_context);
void (* m_disconnect_request)(void * port1_context, void * port2_context);
void (* m_module_location_changed)(void * module_context, double x, double y);
void (* m_fill_canvas_menu)(GtkMenu * menu, void * canvas_context);
void (* m_fill_module_menu)(GtkMenu * menu, void * module_context);
void (* m_fill_port_menu)(GtkMenu * menu, void * port_context);
};
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)
{}
virtual ~module_cls() {}
virtual void store_location()
{
boost::dynamic_pointer_cast<canvas_cls>(canvas().lock())->m_module_location_changed(m_context, property_x(), property_y());
}
void create_menu()
{
_menu = new Gtk::Menu();
_menu->signal_selection_done().connect(sigc::mem_fun(this, &module_cls::on_menu_hide));
_menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Disconnect All"), sigc::mem_fun(this, &module_cls::menu_disconnect_all)));
void (* fill_module_menu)(GtkMenu * menu, void * module_context) = boost::dynamic_pointer_cast<canvas_cls>(canvas().lock())->m_fill_module_menu;
if (fill_module_menu != NULL)
{
fill_module_menu(_menu->gobj(), m_context);
}
_menu->show_all();
}
void menu_disconnect_all()
{
for (FlowCanvas::PortVector::iterator p = _ports.begin(); p != _ports.end(); p++)
{
(*p)->disconnect_all();
}
}
void on_menu_hide()
{
delete _menu;
_menu = NULL;
}
void * m_context;
};
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)
, m_context(port_context)
{}
virtual ~port_cls() {}
void popup_menu(guint button, guint32 activate_time)
{
create_menu();
if (_menu)
_menu->popup(button, activate_time);
}
virtual void create_menu()
{
void (* fill_port_menu)(GtkMenu * menu, void * port_context);
/* Cannot call FlowCanvas::Port::create_menu() because on_menu_hide() is not virtual */
_menu = new Gtk::Menu();
_menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Disconnect All"), sigc::mem_fun(this, &Port::disconnect_all)));
_menu->signal_selection_done().connect(sigc::mem_fun(this, &port_cls::on_menu_hide));
fill_port_menu = boost::dynamic_pointer_cast<canvas_cls>(module().lock()->canvas().lock())->m_fill_port_menu;
if (fill_port_menu != NULL)
{
fill_port_menu(_menu->gobj(), m_context);
}
_menu->show_all();
}
void on_menu_hide()
{
delete _menu;
_menu = NULL;
}
void * m_context;
};
bool
canvas_init(
void)
{
Gnome::Canvas::init();
return true;
}
bool
canvas_create(
double width,
double height,
void * canvas_context,
void (* connect_request)(void * port1_context, void * port2_context),
void (* disconnect_request)(void * port1_context, void * port2_context),
void (* module_location_changed)(void * module_context, double x, double y),
void (* fill_canvas_menu)(GtkMenu * menu, void * canvas_context),
void (* fill_module_menu)(GtkMenu * menu, void * module_context),
void (* fill_port_menu)(GtkMenu * menu, void * port_context),
canvas_handle * canvas_handle_ptr)
{
boost::shared_ptr<canvas_cls> * canvas;
canvas = new boost::shared_ptr<canvas_cls>(new canvas_cls(width,
height,
canvas_context,
connect_request,
disconnect_request,
module_location_changed,
fill_canvas_menu,
fill_module_menu,
fill_port_menu));
*canvas_handle_ptr = (canvas_handle)canvas;
return true;
}
#define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
GtkWidget *
canvas_get_widget(
canvas_handle canvas)
{
return ((Gtk::Widget *)canvas_ptr->get())->gobj();
}
void
canvas_destroy(
canvas_handle canvas)
{
delete canvas_ptr;
}
void
canvas_clear(
canvas_handle canvas)
{
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;
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);
ASSERT(port != NULL);
module->remove_port(port);
port->hide();
}
ASSERT(module->ports().empty());
canvas_ptr->get()->remove_item(module);
}
}
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();
}
void
canvas_scroll_to_center(
canvas_handle canvas)
{
if (canvas_ptr->get()->is_realized())
{
//log_info("realized");
canvas_ptr->get()->scroll_to_center();
}
else
{
//log_info("NOT realized");
}
}
double
canvas_get_zoom(
canvas_handle canvas)
{
return canvas_ptr->get()->get_zoom();
}
void
canvas_set_zoom(
canvas_handle canvas,
double pix_per_unit)
{
canvas_ptr->get()->set_zoom(pix_per_unit);
}
void
canvas_set_zoom_fit(
canvas_handle canvas)
{
canvas_ptr->get()->zoom_full();
}
void
canvas_arrange(
canvas_handle canvas)
{
Glib::RefPtr<Gdk::Window> win = canvas_ptr->get()->get_window();
if (win)
{
canvas_ptr->get()->arrange();
}
}
size_t
canvas_get_selected_modules_count(
canvas_handle canvas)
{
return canvas_ptr->get()->selected_items().size();
}
bool
canvas_get_one_selected_module(
canvas_handle canvas,
void ** module_context_ptr)
{
int i;
std::list<boost::shared_ptr<FlowCanvas::Item> > modules = canvas_ptr->get()->selected_items();
if (modules.size() != 1)
{
return false;
}
i = 0;
for (std::list<boost::shared_ptr<FlowCanvas::Item> >::iterator m = modules.begin(); m != modules.end(); ++m)
{
boost::shared_ptr<module_cls> module = boost::dynamic_pointer_cast<module_cls>(*m);
if (module == NULL)
{
ASSERT_NO_PASS;
return false;
}
if (i == 0)
{
*module_context_ptr = module->m_context;
i++;
continue;
}
}
if (i != 1)
{
ASSERT_NO_PASS;
return false;
}
return true;
}
bool
canvas_get_two_selected_modules(
canvas_handle canvas,
void ** module1_context_ptr,
void ** module2_context_ptr)
{
int i;
std::list<boost::shared_ptr<FlowCanvas::Item> > modules = canvas_ptr->get()->selected_items();
if (modules.size() != 2)
{
return false;
}
i = 0;
for (std::list<boost::shared_ptr<FlowCanvas::Item> >::iterator m = modules.begin(); m != modules.end(); ++m)
{
boost::shared_ptr<module_cls> module = boost::dynamic_pointer_cast<module_cls>(*m);
if (module == NULL)
{
ASSERT_NO_PASS;
return false;
}
switch (i)
{
case 0:
*module1_context_ptr = module->m_context;
i++;
continue;
case 1:
*module2_context_ptr = module->m_context;
i++;
continue;
}
}
if (i != 2)
{
ASSERT_NO_PASS;
return false;
}
return true;
}
bool
canvas_create_module(
canvas_handle canvas,
const char * name,
double x,
double y,
bool show_title,
bool show_port_labels,
void * module_context,
canvas_module_handle * module_handle_ptr)
{
boost::shared_ptr<FlowCanvas::Module> * module;
module = new boost::shared_ptr<FlowCanvas::Module>(new module_cls(*canvas_ptr, name, x, y, show_title, show_port_labels, module_context));
canvas_ptr->get()->add_item(*module);
module->get()->resize();
*module_handle_ptr = (canvas_module_handle)module;
return true;
}
#define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
void
canvas_set_module_name(
canvas_module_handle module,
const char * name)
{
module_ptr->get()->set_name(name);
module_ptr->get()->resize();
}
const char *
canvas_get_module_name(
canvas_module_handle module)
{
return module_ptr->get()->name().c_str();
}
bool
canvas_destroy_module(
canvas_handle canvas,
canvas_module_handle module)
{
canvas_ptr->get()->remove_item(*module_ptr);
delete module_ptr;
return true;
}
bool
canvas_create_port(
canvas_handle canvas,
canvas_module_handle module,
const char * name,
bool is_input,
int color,
void * port_context,
canvas_port_handle * port_handle_ptr)
{
boost::shared_ptr<port_cls> * port;
port = new boost::shared_ptr<port_cls>(new port_cls(*module_ptr, name, is_input, color, port_context));
module_ptr->get()->add_port(*port);
module_ptr->get()->resize();
*port_handle_ptr = (canvas_port_handle)port;
return true;
}
#undef module_ptr
#define port_ptr ((boost::shared_ptr<port_cls> *)port)
bool
canvas_destroy_port(
canvas_handle canvas,
canvas_port_handle port)
{
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();
}
void
canvas_set_port_name(
canvas_port_handle port,
const char * name)
{
port_ptr->get()->set_name(name);
port_ptr->get()->module().lock()->resize();
}
const char *
canvas_get_port_name(
canvas_port_handle port)
{
return port_ptr->get()->name().c_str();
}
#undef port_ptr
#define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
#define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
bool
canvas_add_connection(
canvas_handle canvas,
canvas_port_handle port1,
canvas_port_handle port2,
uint32_t color)
{
canvas_ptr->get()->add_connection(*port1_ptr, *port2_ptr, color);
return true;
}
bool
canvas_remove_connection(
canvas_handle canvas,
canvas_port_handle port1,
canvas_port_handle port2)
{
canvas_ptr->get()->remove_connection(*port1_ptr, *port2_ptr);
return true;
}
#undef port1_ptr
#undef port2_ptr
bool canvas_cls::canvas_event(GdkEvent * event)
{
assert(event);
if (m_fill_canvas_menu != NULL && event->type == GDK_BUTTON_PRESS && event->button.button == 3)
{
Gtk::Menu * menu_ptr;
menu_ptr = new Gtk::Menu();
m_fill_canvas_menu(menu_ptr->gobj(), m_context);
menu_ptr->show_all();
menu_ptr->popup(event->button.button, event->button.time);
return true;
}
return Canvas::canvas_event(event);
}
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->m_context, port2->m_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->m_context, port2->m_context);
}
}
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;
}
|