gui: another fix for the port menu creation callback
[ladish.git] / gui / canvas.cpp
blob2b5d75e5b6dba70cf8d2d7d13fe744cfa1f48f7d
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
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>
32 #include "canvas.h"
34 class canvas_cls: public FlowCanvas::Canvas
36 public:
37 canvas_cls(
38 double width,
39 double height,
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();
61 scroll_to_center();
64 virtual void on_size_allocate(Gtk::Allocation& allocation)
66 //log_info("canvas_cls::on_size_allocate");
67 FlowCanvas::Canvas::on_size_allocate(allocation);
68 if (is_realized())
70 //log_info("... realized");
71 scroll_to_center();
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
90 public:
91 module_cls(
92 boost::shared_ptr<FlowCanvas::Canvas> canvas,
93 const std::string& name,
94 double x,
95 double y,
96 bool show_title,
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());
110 void create_menu()
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();
130 void on_menu_hide()
132 delete _menu;
133 _menu = NULL;
136 void * m_context;
139 class port_cls: public FlowCanvas::Port
141 public:
142 port_cls(
143 boost::shared_ptr<FlowCanvas::Module> module,
144 const std::string& name,
145 bool is_input,
146 uint32_t color,
147 void * port_context)
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)
156 create_menu();
157 if (_menu)
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);
177 void on_menu_hide()
179 delete _menu;
180 _menu = NULL;
183 void * m_context;
186 bool
187 canvas_init(
188 void)
190 Gnome::Canvas::init();
191 return true;
194 bool
195 canvas_create(
196 double width,
197 double height,
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,
209 height,
210 connect_request,
211 disconnect_request,
212 module_location_changed,
213 fill_canvas_menu,
214 fill_module_menu,
215 fill_port_menu));
217 *canvas_handle_ptr = (canvas_handle)canvas;
219 return true;
222 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
224 GtkWidget *
225 canvas_get_widget(
226 canvas_handle canvas)
228 return ((Gtk::Widget *)canvas_ptr->get())->gobj();
231 void
232 canvas_destroy(
233 canvas_handle canvas)
235 delete canvas_ptr;
238 void
239 canvas_clear(
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);
246 if (!module)
247 continue;
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);
255 port->hide();
258 ASSERT(module->ports().empty());
259 canvas_ptr->get()->remove_item(module);
263 void
264 canvas_get_size(
265 canvas_handle canvas,
266 double * width_ptr,
267 double * height_ptr)
269 *width_ptr = canvas_ptr->get()->width();
270 *height_ptr = canvas_ptr->get()->height();
273 void
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();
282 else
284 //log_info("NOT realized");
288 double
289 canvas_get_zoom(
290 canvas_handle canvas)
292 return canvas_ptr->get()->get_zoom();
295 void
296 canvas_set_zoom(
297 canvas_handle canvas,
298 double pix_per_unit)
300 canvas_ptr->get()->set_zoom(pix_per_unit);
303 void
304 canvas_set_zoom_fit(
305 canvas_handle canvas)
307 canvas_ptr->get()->zoom_full();
310 void
311 canvas_arrange(
312 canvas_handle canvas)
314 Glib::RefPtr<Gdk::Window> win = canvas_ptr->get()->get_window();
315 if (win)
317 canvas_ptr->get()->arrange();
321 bool
322 canvas_create_module(
323 canvas_handle canvas,
324 const char * name,
325 double x,
326 double y,
327 bool show_title,
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;
340 return true;
343 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
345 void
346 canvas_set_module_name(
347 canvas_module_handle module,
348 const char * name)
350 module_ptr->get()->set_name(name);
353 bool
354 canvas_destroy_module(
355 canvas_handle canvas,
356 canvas_module_handle module)
358 canvas_ptr->get()->remove_item(*module_ptr);
359 delete module_ptr;
360 return true;
363 bool
364 canvas_create_port(
365 canvas_handle canvas,
366 canvas_module_handle module,
367 const char * name,
368 bool is_input,
369 int color,
370 void * port_context,
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;
382 return true;
385 #undef module_ptr
386 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
388 bool
389 canvas_destroy_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);
395 delete port_ptr;
396 module->resize();
397 return true;
401 canvas_get_port_color(
402 canvas_port_handle port)
404 return port_ptr->get()->color();
407 void
408 canvas_set_port_name(
409 canvas_port_handle port,
410 const char * name)
412 port_ptr->get()->set_name(name);
415 #undef port_ptr
416 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
417 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
419 bool
420 canvas_add_connection(
421 canvas_handle canvas,
422 canvas_port_handle port1,
423 canvas_port_handle port2,
424 uint32_t color)
426 canvas_ptr->get()->add_connection(*port1_ptr, *port2_ptr, color);
427 return true;
430 bool
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);
437 return true;
440 #undef port1_ptr
441 #undef port2_ptr
443 bool canvas_cls::canvas_event(GdkEvent * event)
445 assert(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);
454 return true;
457 return Canvas::canvas_event(event);
460 void
461 canvas_cls::connect(
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);
473 void
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);
486 bool
487 canvas_enum_modules(
488 canvas_handle canvas,
489 void * callback_context,
490 bool (* callback)(void * context, canvas_module_handle module))
492 return false;
495 bool
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))
502 return false;