Cleanup current studio before loading new one
[ladish.git] / gui / canvas.cpp
blob0830ddf7200bf317ddf23674ae6c8ed39cf33212
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 : FlowCanvas::Canvas(width, height)
43 , m_connect_request(connect_request)
44 , m_disconnect_request(disconnect_request)
45 {};
47 virtual ~canvas_cls() {};
48 virtual void connect(boost::shared_ptr<FlowCanvas::Connectable> port1, boost::shared_ptr<FlowCanvas::Connectable> port2);
49 virtual void disconnect(boost::shared_ptr<FlowCanvas::Connectable> port1, boost::shared_ptr<FlowCanvas::Connectable> port2);
51 void (* m_connect_request)(void * port1_context, void * port2_context);
52 void (* m_disconnect_request)(void * port1_context, void * port2_context);
55 class port_cls: public FlowCanvas::Port
57 public:
58 port_cls(
59 boost::shared_ptr<FlowCanvas::Module> module,
60 const std::string& name,
61 bool is_input,
62 uint32_t color,
63 void * port_context)
64 : FlowCanvas::Port(module, name, is_input, color)
65 , context(port_context)
66 {};
68 virtual ~port_cls() {};
70 void * context;
73 bool
74 canvas_init(
75 void)
77 Gnome::Canvas::init();
78 return true;
81 bool
82 canvas_create(
83 double width,
84 double height,
85 void (* connect_request)(void * port1_context, void * port2_context),
86 void (* disconnect_request)(void * port1_context, void * port2_context),
87 canvas_handle * canvas_handle_ptr)
89 boost::shared_ptr<canvas_cls> * canvas;
91 canvas = new boost::shared_ptr<canvas_cls>(new canvas_cls(width, height, connect_request, disconnect_request));
93 *canvas_handle_ptr = (canvas_handle)canvas;
95 return true;
98 #define canvas_ptr ((boost::shared_ptr<canvas_cls> *)canvas)
100 GtkWidget *
101 canvas_get_widget(
102 canvas_handle canvas)
104 return ((Gtk::Widget *)canvas_ptr->get())->gobj();
107 void
108 canvas_destroy(
109 canvas_handle canvas)
111 delete canvas_ptr;
114 void
115 canvas_clear(
116 canvas_handle canvas)
118 FlowCanvas::ItemList modules = canvas_ptr->get()->items(); // copy
119 for (FlowCanvas::ItemList::iterator m = modules.begin(); m != modules.end(); ++m)
121 boost::shared_ptr<FlowCanvas::Module> module = boost::dynamic_pointer_cast<FlowCanvas::Module>(*m);
122 if (!module)
123 continue;
125 FlowCanvas::PortVector ports = module->ports(); // copy
126 for (FlowCanvas::PortVector::iterator p = ports.begin(); p != ports.end(); ++p)
128 boost::shared_ptr<FlowCanvas::Port> port = boost::dynamic_pointer_cast<FlowCanvas::Port>(*p);
129 assert(port != NULL);
130 module->remove_port(port);
131 port->hide();
134 assert(module->ports().empty());
135 canvas_ptr->get()->remove_item(module);
139 void
140 canvas_set_zoom(
141 canvas_handle canvas,
142 double pix_per_unit)
144 canvas_ptr->get()->set_zoom(pix_per_unit);
147 void
148 canvas_arrange(
149 canvas_handle canvas)
151 Glib::RefPtr<Gdk::Window> win = canvas_ptr->get()->get_window();
152 if (win)
154 canvas_ptr->get()->arrange();
158 bool
159 canvas_create_module(
160 canvas_handle canvas,
161 const char * name,
162 double x,
163 double y,
164 bool show_title,
165 bool show_port_labels,
166 canvas_module_handle * module_handle_ptr)
168 boost::shared_ptr<FlowCanvas::Module> * module;
170 module = new boost::shared_ptr<FlowCanvas::Module>(new FlowCanvas::Module(*canvas_ptr, name, x, y, show_title, show_port_labels));
171 canvas_ptr->get()->add_item(*module);
172 module->get()->resize();
174 *module_handle_ptr = (canvas_module_handle)module;
176 return true;
179 #define module_ptr ((boost::shared_ptr<FlowCanvas::Module> *)module)
181 bool
182 canvas_destroy_module(
183 canvas_handle canvas,
184 canvas_module_handle module)
186 canvas_ptr->get()->remove_item(*module_ptr);
187 delete module_ptr;
188 return true;
191 bool
192 canvas_create_port(
193 canvas_handle canvas,
194 canvas_module_handle module,
195 const char * name,
196 bool is_input,
197 int color,
198 void * port_context,
199 canvas_port_handle * port_handle_ptr)
201 boost::shared_ptr<port_cls> * port;
203 port = new boost::shared_ptr<port_cls>(new port_cls(*module_ptr, name, is_input, color, port_context));
205 module_ptr->get()->add_port(*port);
206 module_ptr->get()->resize();
208 *port_handle_ptr = (canvas_port_handle)port;
210 return true;
213 #undef module_ptr
214 #define port_ptr ((boost::shared_ptr<port_cls> *)port)
216 bool
217 canvas_destroy_port(
218 canvas_handle canvas,
219 canvas_port_handle port)
221 boost::shared_ptr<FlowCanvas::Module> module = port_ptr->get()->module().lock();
222 module->remove_port(*port_ptr);
223 delete port_ptr;
224 module->resize();
225 return true;
229 canvas_get_port_color(
230 canvas_port_handle port)
232 return port_ptr->get()->color();
235 #undef port_ptr
236 #define port1_ptr ((boost::shared_ptr<port_cls> *)port1)
237 #define port2_ptr ((boost::shared_ptr<port_cls> *)port2)
239 bool
240 canvas_add_connection(
241 canvas_handle canvas,
242 canvas_port_handle port1,
243 canvas_port_handle port2,
244 uint32_t color)
246 canvas_ptr->get()->add_connection(*port1_ptr, *port2_ptr, color);
247 return true;
250 bool
251 canvas_remove_connection(
252 canvas_handle canvas,
253 canvas_port_handle port1,
254 canvas_port_handle port2)
256 canvas_ptr->get()->remove_connection(*port1_ptr, *port2_ptr);
257 return true;
260 #undef port1_ptr
261 #undef port2_ptr
263 void
264 canvas_cls::connect(
265 boost::shared_ptr<FlowCanvas::Connectable> c1,
266 boost::shared_ptr<FlowCanvas::Connectable> c2)
268 if (m_connect_request != NULL)
270 boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
271 boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
272 m_connect_request(port1->context, port2->context);
276 void
277 canvas_cls::disconnect(
278 boost::shared_ptr<FlowCanvas::Connectable> c1,
279 boost::shared_ptr<FlowCanvas::Connectable> c2)
281 if (m_disconnect_request != NULL)
283 boost::shared_ptr<port_cls> port1 = boost::dynamic_pointer_cast<port_cls>(c1);
284 boost::shared_ptr<port_cls> port2 = boost::dynamic_pointer_cast<port_cls>(c2);
285 m_disconnect_request(port1->context, port2->context);
289 bool
290 canvas_enum_modules(
291 canvas_handle canvas,
292 void * callback_context,
293 bool (* callback)(void * context, canvas_module_handle module))
295 return false;
298 bool
299 canvas_enum_module_ports(
300 canvas_handle canvas,
301 canvas_module_handle module,
302 void * callback_context,
303 bool (* callback)(void * context, canvas_port_handle port))
305 return false;