daemon: add some logs
[ladish.git] / gui / canvas.cpp
blob3eb3c5da0d8f40791a1aa9a4a5059f5ae820402a
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 canvas_ptr->get()->arrange();
231 bool
232 canvas_create_module(
233 canvas_handle canvas,
234 const char * name,
235 double x,
236 double y,
237 bool show_title,
238 bool show_port_labels,
239 void * module_context,
240 canvas_module_handle * module_handle_ptr)
242 boost::shared_ptr<FlowCanvas::Module> * module;
244 module = new boost::shared_ptr<FlowCanvas::Module>(new module_cls(*canvas_ptr, name, x, y, show_title, show_port_labels, module_context));
245 canvas_ptr->get()->add_item(*module);
246 module->get()->resize();
248 *module_handle_ptr = (canvas_module_handle)module;
250 return true;
253 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
255 bool
256 canvas_destroy_module(
257 canvas_handle canvas,
258 canvas_module_handle module)
260 canvas_ptr->get()->remove_item(*module_ptr);
261 delete module_ptr;
262 return true;
265 bool
266 canvas_create_port(
267 canvas_handle canvas,
268 canvas_module_handle module,
269 const char * name,
270 bool is_input,
271 int color,
272 void * port_context,
273 canvas_port_handle * port_handle_ptr)
275 boost::shared_ptr<port_cls> * port;
277 port = new boost::shared_ptr<port_cls>(new port_cls(*module_ptr, name, is_input, color, port_context));
279 module_ptr->get()->add_port(*port);
280 module_ptr->get()->resize();
282 *port_handle_ptr = (canvas_port_handle)port;
284 return true;
287 #undef module_ptr
288 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
290 bool
291 canvas_destroy_port(
292 canvas_handle canvas,
293 canvas_port_handle port)
295 boost::shared_ptr<FlowCanvas::Module> module = port_ptr->get()->module().lock();
296 module->remove_port(*port_ptr);
297 delete port_ptr;
298 module->resize();
299 return true;
303 canvas_get_port_color(
304 canvas_port_handle port)
306 return port_ptr->get()->color();
309 #undef port_ptr
310 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
311 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
313 bool
314 canvas_add_connection(
315 canvas_handle canvas,
316 canvas_port_handle port1,
317 canvas_port_handle port2,
318 uint32_t color)
320 canvas_ptr->get()->add_connection(*port1_ptr, *port2_ptr, color);
321 return true;
324 bool
325 canvas_remove_connection(
326 canvas_handle canvas,
327 canvas_port_handle port1,
328 canvas_port_handle port2)
330 canvas_ptr->get()->remove_connection(*port1_ptr, *port2_ptr);
331 return true;
334 #undef port1_ptr
335 #undef port2_ptr
337 void
338 canvas_cls::connect(
339 boost::shared_ptr<FlowCanvas::Connectable> c1,
340 boost::shared_ptr<FlowCanvas::Connectable> c2)
342 if (m_connect_request != NULL)
344 boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
345 boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
346 m_connect_request(port1->context, port2->context);
350 void
351 canvas_cls::disconnect(
352 boost::shared_ptr<FlowCanvas::Connectable> c1,
353 boost::shared_ptr<FlowCanvas::Connectable> c2)
355 if (m_disconnect_request != NULL)
357 boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
358 boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
359 m_disconnect_request(port1->context, port2->context);
363 bool
364 canvas_enum_modules(
365 canvas_handle canvas,
366 void * callback_context,
367 bool (* callback)(void * context, canvas_module_handle module))
369 return false;
372 bool
373 canvas_enum_module_ports(
374 canvas_handle canvas,
375 canvas_module_handle module,
376 void * callback_context,
377 bool (* callback)(void * context, canvas_port_handle port))
379 return false;