gladish: canvas zoom functionality. Fixes #66
[ladish.git] / gui / canvas.cpp
blob73624438910c6a099e416976e81c4c91054d0207
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
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>
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->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();
129 void * m_context;
132 class port_cls: public FlowCanvas::Port
134 public:
135 port_cls(
136 boost::shared_ptr<FlowCanvas::Module> module,
137 const std::string& name,
138 bool is_input,
139 uint32_t color,
140 void * port_context)
141 : FlowCanvas::Port(module, name, is_input, color)
142 , context(port_context)
145 virtual ~port_cls() {}
147 void * context;
150 bool
151 canvas_init(
152 void)
154 Gnome::Canvas::init();
155 return true;
158 bool
159 canvas_create(
160 double width,
161 double height,
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,
173 height,
174 connect_request,
175 disconnect_request,
176 module_location_changed,
177 fill_canvas_menu,
178 fill_module_menu,
179 fill_port_menu));
181 *canvas_handle_ptr = (canvas_handle)canvas;
183 return true;
186 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
188 GtkWidget *
189 canvas_get_widget(
190 canvas_handle canvas)
192 return ((Gtk::Widget *)canvas_ptr->get())->gobj();
195 void
196 canvas_destroy(
197 canvas_handle canvas)
199 delete canvas_ptr;
202 void
203 canvas_clear(
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);
210 if (!module)
211 continue;
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);
219 port->hide();
222 ASSERT(module->ports().empty());
223 canvas_ptr->get()->remove_item(module);
227 void
228 canvas_get_size(
229 canvas_handle canvas,
230 double * width_ptr,
231 double * height_ptr)
233 *width_ptr = canvas_ptr->get()->width();
234 *height_ptr = canvas_ptr->get()->height();
237 void
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();
246 else
248 //log_info("NOT realized");
252 double
253 canvas_get_zoom(
254 canvas_handle canvas)
256 return canvas_ptr->get()->get_zoom();
259 void
260 canvas_set_zoom(
261 canvas_handle canvas,
262 double pix_per_unit)
264 canvas_ptr->get()->set_zoom(pix_per_unit);
267 void
268 canvas_set_zoom_fit(
269 canvas_handle canvas)
271 canvas_ptr->get()->zoom_full();
274 void
275 canvas_arrange(
276 canvas_handle canvas)
278 Glib::RefPtr<Gdk::Window> win = canvas_ptr->get()->get_window();
279 if (win)
281 canvas_ptr->get()->arrange();
285 bool
286 canvas_create_module(
287 canvas_handle canvas,
288 const char * name,
289 double x,
290 double y,
291 bool show_title,
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;
304 return true;
307 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
309 void
310 canvas_set_module_name(
311 canvas_module_handle module,
312 const char * name)
314 module_ptr->get()->set_name(name);
317 bool
318 canvas_destroy_module(
319 canvas_handle canvas,
320 canvas_module_handle module)
322 canvas_ptr->get()->remove_item(*module_ptr);
323 delete module_ptr;
324 return true;
327 bool
328 canvas_create_port(
329 canvas_handle canvas,
330 canvas_module_handle module,
331 const char * name,
332 bool is_input,
333 int color,
334 void * port_context,
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;
346 return true;
349 #undef module_ptr
350 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
352 bool
353 canvas_destroy_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);
359 delete port_ptr;
360 module->resize();
361 return true;
365 canvas_get_port_color(
366 canvas_port_handle port)
368 return port_ptr->get()->color();
371 void
372 canvas_set_port_name(
373 canvas_port_handle port,
374 const char * name)
376 port_ptr->get()->set_name(name);
379 #undef port_ptr
380 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
381 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
383 bool
384 canvas_add_connection(
385 canvas_handle canvas,
386 canvas_port_handle port1,
387 canvas_port_handle port2,
388 uint32_t color)
390 canvas_ptr->get()->add_connection(*port1_ptr, *port2_ptr, color);
391 return true;
394 bool
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);
401 return true;
404 #undef port1_ptr
405 #undef port2_ptr
407 bool canvas_cls::canvas_event(GdkEvent * event)
409 assert(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);
418 return true;
421 return Canvas::canvas_event(event);
424 void
425 canvas_cls::connect(
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);
437 void
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);
450 bool
451 canvas_enum_modules(
452 canvas_handle canvas,
453 void * callback_context,
454 bool (* callback)(void * context, canvas_module_handle module))
456 return false;
459 bool
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))
466 return false;