gui: store new positions after arrange so the get saved eventually
[ladish.git] / gui / canvas.cpp
blobfc5de14512b3a3053bf38e157d140ae77bb01a55
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 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 : FlowCanvas::Canvas(width, height)
44 , m_connect_request(connect_request)
45 , m_disconnect_request(disconnect_request)
46 , m_module_location_changed(module_location_changed)
49 virtual ~canvas_cls() {}
51 virtual void on_realize()
53 log_info("canvas_cls::on_realize");
54 FlowCanvas::Canvas::on_realize();
55 scroll_to_center();
58 virtual void on_size_allocate(Gtk::Allocation& allocation)
60 //log_info("canvas_cls::on_size_allocate");
61 FlowCanvas::Canvas::on_size_allocate(allocation);
62 if (is_realized())
64 //log_info("... realized");
65 scroll_to_center();
69 virtual void connect(boost::shared_ptr<FlowCanvas::Connectable> port1, boost::shared_ptr<FlowCanvas::Connectable> port2);
70 virtual void disconnect(boost::shared_ptr<FlowCanvas::Connectable> port1, boost::shared_ptr<FlowCanvas::Connectable> port2);
72 void (* m_connect_request)(void * port1_context, void * port2_context);
73 void (* m_disconnect_request)(void * port1_context, void * port2_context);
74 void (* m_module_location_changed)(void * module_context, double x, double y);
77 class module_cls: public FlowCanvas::Module
79 public:
80 module_cls(
81 boost::shared_ptr<FlowCanvas::Canvas> canvas,
82 const std::string& name,
83 double x,
84 double y,
85 bool show_title,
86 bool show_port_labels,
87 void * module_context)
88 : FlowCanvas::Module(canvas, name, x, y, show_title, show_port_labels)
89 , m_context(module_context)
92 virtual ~module_cls() {}
94 virtual void store_location()
96 boost::dynamic_pointer_cast<canvas_cls>(canvas().lock())->m_module_location_changed(m_context, property_x(), property_y());
99 void * m_context;
102 class port_cls: public FlowCanvas::Port
104 public:
105 port_cls(
106 boost::shared_ptr<FlowCanvas::Module> module,
107 const std::string& name,
108 bool is_input,
109 uint32_t color,
110 void * port_context)
111 : FlowCanvas::Port(module, name, is_input, color)
112 , context(port_context)
115 virtual ~port_cls() {}
117 void * context;
120 bool
121 canvas_init(
122 void)
124 Gnome::Canvas::init();
125 return true;
128 bool
129 canvas_create(
130 double width,
131 double height,
132 void (* connect_request)(void * port1_context, void * port2_context),
133 void (* disconnect_request)(void * port1_context, void * port2_context),
134 void (* module_location_changed)(void * module_context, double x, double y),
135 canvas_handle * canvas_handle_ptr)
137 boost::shared_ptr<canvas_cls> * canvas;
139 canvas = new boost::shared_ptr<canvas_cls>(new canvas_cls(width, height, connect_request, disconnect_request, module_location_changed));
141 *canvas_handle_ptr = (canvas_handle)canvas;
143 return true;
146 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
148 GtkWidget *
149 canvas_get_widget(
150 canvas_handle canvas)
152 return ((Gtk::Widget *)canvas_ptr->get())->gobj();
155 void
156 canvas_destroy(
157 canvas_handle canvas)
159 delete canvas_ptr;
162 void
163 canvas_clear(
164 canvas_handle canvas)
166 FlowCanvas::ItemList modules = canvas_ptr->get()->items(); // copy
167 for (FlowCanvas::ItemList::iterator m = modules.begin(); m != modules.end(); ++m)
169 boost::shared_ptr<FlowCanvas::Module> module = boost::dynamic_pointer_cast<FlowCanvas::Module>(*m);
170 if (!module)
171 continue;
173 FlowCanvas::PortVector ports = module->ports(); // copy
174 for (FlowCanvas::PortVector::iterator p = ports.begin(); p != ports.end(); ++p)
176 boost::shared_ptr<FlowCanvas::Port> port = boost::dynamic_pointer_cast<FlowCanvas::Port>(*p);
177 ASSERT(port != NULL);
178 module->remove_port(port);
179 port->hide();
182 ASSERT(module->ports().empty());
183 canvas_ptr->get()->remove_item(module);
187 void
188 canvas_get_size(
189 canvas_handle canvas,
190 double * width_ptr,
191 double * height_ptr)
193 *width_ptr = canvas_ptr->get()->width();
194 *height_ptr = canvas_ptr->get()->height();
197 void
198 canvas_scroll_to_center(
199 canvas_handle canvas)
201 if (canvas_ptr->get()->is_realized())
203 //log_info("realized");
204 canvas_ptr->get()->scroll_to_center();
206 else
208 //log_info("NOT realized");
212 void
213 canvas_set_zoom(
214 canvas_handle canvas,
215 double pix_per_unit)
217 canvas_ptr->get()->set_zoom(pix_per_unit);
220 void
221 canvas_arrange(
222 canvas_handle canvas)
224 Glib::RefPtr<Gdk::Window> win = canvas_ptr->get()->get_window();
225 if (!win)
227 return;
230 canvas_ptr->get()->arrange();
232 // arrange does not cause locations stored, emulate it
233 FlowCanvas::ItemList modules = canvas_ptr->get()->items(); // copy
234 for (FlowCanvas::ItemList::iterator m = modules.begin(); m != modules.end(); ++m)
236 boost::shared_ptr<FlowCanvas::Module> module = boost::dynamic_pointer_cast<FlowCanvas::Module>(*m);
237 if (!module)
238 continue;
240 module->store_location();
244 bool
245 canvas_create_module(
246 canvas_handle canvas,
247 const char * name,
248 double x,
249 double y,
250 bool show_title,
251 bool show_port_labels,
252 void * module_context,
253 canvas_module_handle * module_handle_ptr)
255 boost::shared_ptr<FlowCanvas::Module> * module;
257 module = new boost::shared_ptr<FlowCanvas::Module>(new module_cls(*canvas_ptr, name, x, y, show_title, show_port_labels, module_context));
258 canvas_ptr->get()->add_item(*module);
259 module->get()->resize();
261 *module_handle_ptr = (canvas_module_handle)module;
263 return true;
266 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
268 void
269 canvas_set_module_name(
270 canvas_module_handle module,
271 const char * name)
273 module_ptr->get()->set_name(name);
276 bool
277 canvas_destroy_module(
278 canvas_handle canvas,
279 canvas_module_handle module)
281 canvas_ptr->get()->remove_item(*module_ptr);
282 delete module_ptr;
283 return true;
286 bool
287 canvas_create_port(
288 canvas_handle canvas,
289 canvas_module_handle module,
290 const char * name,
291 bool is_input,
292 int color,
293 void * port_context,
294 canvas_port_handle * port_handle_ptr)
296 boost::shared_ptr<port_cls> * port;
298 port = new boost::shared_ptr<port_cls>(new port_cls(*module_ptr, name, is_input, color, port_context));
300 module_ptr->get()->add_port(*port);
301 module_ptr->get()->resize();
303 *port_handle_ptr = (canvas_port_handle)port;
305 return true;
308 #undef module_ptr
309 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
311 bool
312 canvas_destroy_port(
313 canvas_handle canvas,
314 canvas_port_handle port)
316 boost::shared_ptr<FlowCanvas::Module> module = port_ptr->get()->module().lock();
317 module->remove_port(*port_ptr);
318 delete port_ptr;
319 module->resize();
320 return true;
324 canvas_get_port_color(
325 canvas_port_handle port)
327 return port_ptr->get()->color();
330 void
331 canvas_set_port_name(
332 canvas_port_handle port,
333 const char * name)
335 port_ptr->get()->set_name(name);
338 #undef port_ptr
339 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
340 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
342 bool
343 canvas_add_connection(
344 canvas_handle canvas,
345 canvas_port_handle port1,
346 canvas_port_handle port2,
347 uint32_t color)
349 canvas_ptr->get()->add_connection(*port1_ptr, *port2_ptr, color);
350 return true;
353 bool
354 canvas_remove_connection(
355 canvas_handle canvas,
356 canvas_port_handle port1,
357 canvas_port_handle port2)
359 canvas_ptr->get()->remove_connection(*port1_ptr, *port2_ptr);
360 return true;
363 #undef port1_ptr
364 #undef port2_ptr
366 void
367 canvas_cls::connect(
368 boost::shared_ptr<FlowCanvas::Connectable> c1,
369 boost::shared_ptr<FlowCanvas::Connectable> c2)
371 if (m_connect_request != NULL)
373 boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
374 boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
375 m_connect_request(port1->context, port2->context);
379 void
380 canvas_cls::disconnect(
381 boost::shared_ptr<FlowCanvas::Connectable> c1,
382 boost::shared_ptr<FlowCanvas::Connectable> c2)
384 if (m_disconnect_request != NULL)
386 boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
387 boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
388 m_disconnect_request(port1->context, port2->context);
392 bool
393 canvas_enum_modules(
394 canvas_handle canvas,
395 void * callback_context,
396 bool (* callback)(void * context, canvas_module_handle module))
398 return false;
401 bool
402 canvas_enum_module_ports(
403 canvas_handle canvas,
404 canvas_module_handle module,
405 void * callback_context,
406 bool (* callback)(void * context, canvas_port_handle port))
408 return false;