| 1 | /* This file is part of FlowCanvas. |
|---|
| 2 | * Copyright (C) 2007-2009 David Robillard <http://drobilla.net> |
|---|
| 3 | * |
|---|
| 4 | * FlowCanvas is free software; you can redistribute it and/or modify it under the |
|---|
| 5 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 6 | * Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 7 | * version. |
|---|
| 8 | * |
|---|
| 9 | * FlowCanvas is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License along |
|---|
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 15 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #include <algorithm> |
|---|
| 19 | #include <cassert> |
|---|
| 20 | #include <cmath> |
|---|
| 21 | #include <cstdlib> |
|---|
| 22 | #include <functional> |
|---|
| 23 | #include <list> |
|---|
| 24 | #include <string> |
|---|
| 25 | |
|---|
| 26 | #include "Canvas.hpp" |
|---|
| 27 | #include "Item.hpp" |
|---|
| 28 | #include "Module.hpp" |
|---|
| 29 | |
|---|
| 30 | using std::list; |
|---|
| 31 | using std::string; |
|---|
| 32 | |
|---|
| 33 | namespace FlowCanvas { |
|---|
| 34 | |
|---|
| 35 | static const uint32_t MODULE_FILL_COLOUR = 0x1E2224FF; |
|---|
| 36 | static const uint32_t MODULE_HILITE_FILL_COLOUR = 0x2E3436FF; |
|---|
| 37 | static const uint32_t MODULE_OUTLINE_COLOUR = 0x93978FFF; |
|---|
| 38 | static const uint32_t MODULE_HILITE_OUTLINE_COLOUR = 0xEEEEECFF; |
|---|
| 39 | static const uint32_t MODULE_TITLE_COLOUR = 0xFFFFFFFF; |
|---|
| 40 | static const double MODULE_EMPTY_PORT_BREADTH = 12.0; |
|---|
| 41 | static const double MODULE_EMPTY_PORT_DEPTH = 6.0; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /** Construct a Module |
|---|
| 45 | * |
|---|
| 46 | * Note you must call resize() at some point or the module will look ridiculous. |
|---|
| 47 | * This it to avoid unecessary text measuring and resizing, which is insanely |
|---|
| 48 | * expensive. |
|---|
| 49 | * |
|---|
| 50 | * If @a name is the empty string, the space where the title would usually be |
|---|
| 51 | * is not created (eg the module will be shorter). |
|---|
| 52 | */ |
|---|
| 53 | Module::Module( |
|---|
| 54 | boost::shared_ptr<Canvas> canvas, |
|---|
| 55 | const string& name, |
|---|
| 56 | double x, double y, |
|---|
| 57 | bool show_title, bool show_port_labels) |
|---|
| 58 | : Item(canvas, name, x, y, MODULE_FILL_COLOUR) |
|---|
| 59 | , _module_box(*this, 0, 0, 0, 0) // w, h set later |
|---|
| 60 | , _canvas_title(*this, 0, 8, name) // x set later |
|---|
| 61 | , _stacked_border(NULL) |
|---|
| 62 | , _icon_box(NULL) |
|---|
| 63 | , _embed_container(NULL) |
|---|
| 64 | , _embed_item(NULL) |
|---|
| 65 | , _border_width(1.0) |
|---|
| 66 | , _embed_width(0) |
|---|
| 67 | , _embed_height(0) |
|---|
| 68 | , _icon_size(16) |
|---|
| 69 | , _widest_input(0) |
|---|
| 70 | , _widest_output(0) |
|---|
| 71 | , _title_width(0.0) |
|---|
| 72 | , _title_height(0.0) |
|---|
| 73 | , _title_visible(show_title) |
|---|
| 74 | , _port_renamed(false) |
|---|
| 75 | , _show_port_labels(show_port_labels) |
|---|
| 76 | { |
|---|
| 77 | _module_box.property_fill_color_rgba() = MODULE_FILL_COLOUR; |
|---|
| 78 | _module_box.property_outline_color_rgba() = MODULE_OUTLINE_COLOUR; |
|---|
| 79 | _module_box.property_width_units() = _border_width; |
|---|
| 80 | |
|---|
| 81 | _border_color = MODULE_OUTLINE_COLOUR; |
|---|
| 82 | |
|---|
| 83 | if (show_title) { |
|---|
| 84 | /* WARNING: Doing this makes things extremely slow! |
|---|
| 85 | _canvas_title.property_size_set() = true; |
|---|
| 86 | _canvas_title.property_size() = 9000; |
|---|
| 87 | _canvas_title.property_weight_set() = true; |
|---|
| 88 | _canvas_title.property_weight() = 400; */ |
|---|
| 89 | //if (canvas->get_zoom() != 1.0) |
|---|
| 90 | zoom(canvas->get_zoom()); |
|---|
| 91 | _canvas_title.property_fill_color_rgba() = MODULE_TITLE_COLOUR; |
|---|
| 92 | _title_width = _canvas_title.property_text_width(); |
|---|
| 93 | _title_height = _canvas_title.property_text_height(); |
|---|
| 94 | } else { |
|---|
| 95 | _canvas_title.hide(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | set_width(10.0); |
|---|
| 99 | set_height(10.0); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | Module::~Module() |
|---|
| 104 | { |
|---|
| 105 | delete _stacked_border; |
|---|
| 106 | delete _icon_box; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | bool |
|---|
| 111 | Module::on_event(GdkEvent* event) |
|---|
| 112 | { |
|---|
| 113 | boost::shared_ptr<Canvas> canvas; |
|---|
| 114 | switch (event->type) { |
|---|
| 115 | case GDK_KEY_PRESS: |
|---|
| 116 | case GDK_KEY_RELEASE: |
|---|
| 117 | if ((canvas = _canvas.lock())) |
|---|
| 118 | canvas->canvas_event(event); |
|---|
| 119 | break; |
|---|
| 120 | case GDK_ENTER_NOTIFY: |
|---|
| 121 | set_highlighted(true); |
|---|
| 122 | break; |
|---|
| 123 | case GDK_LEAVE_NOTIFY: |
|---|
| 124 | set_highlighted(false); |
|---|
| 125 | default: break; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | bool ret = Item::on_event(event); |
|---|
| 129 | |
|---|
| 130 | if (event->type == GDK_ENTER_NOTIFY) |
|---|
| 131 | for (PortVector::iterator p = _ports.begin(); p != _ports.end(); ++p) |
|---|
| 132 | (*p)->raise_connections(); |
|---|
| 133 | |
|---|
| 134 | return ret; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | double |
|---|
| 139 | Module::empty_port_breadth() const |
|---|
| 140 | { |
|---|
| 141 | return MODULE_EMPTY_PORT_BREADTH; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | double |
|---|
| 146 | Module::empty_port_depth() const |
|---|
| 147 | { |
|---|
| 148 | return MODULE_EMPTY_PORT_DEPTH; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | /** Set the border width of the module. |
|---|
| 153 | * |
|---|
| 154 | * Do NOT directly set the width_units property on the rect, use this function. |
|---|
| 155 | */ |
|---|
| 156 | void |
|---|
| 157 | Module::set_border_width(double w) |
|---|
| 158 | { |
|---|
| 159 | _border_width = w; |
|---|
| 160 | _module_box.property_width_units() = w; |
|---|
| 161 | if (_stacked_border) |
|---|
| 162 | _stacked_border->property_width_units() = w; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | void |
|---|
| 167 | Module::set_stacked_border(bool b) |
|---|
| 168 | { |
|---|
| 169 | if (b && !_stacked_border) { |
|---|
| 170 | _stacked_border = new Gnome::Canvas::Rect(*this, 4.0, 4.0, _width + 4.0, _height + 4.0); |
|---|
| 171 | _stacked_border->property_fill_color_rgba() = _color; |
|---|
| 172 | _stacked_border->property_outline_color_rgba() = MODULE_OUTLINE_COLOUR; |
|---|
| 173 | _stacked_border->property_width_units() = _border_width; |
|---|
| 174 | _stacked_border->lower_to_bottom(); |
|---|
| 175 | _stacked_border->show(); |
|---|
| 176 | } else if (b) { |
|---|
| 177 | _stacked_border->show(); |
|---|
| 178 | } else { |
|---|
| 179 | delete _stacked_border; |
|---|
| 180 | _stacked_border = NULL; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | void |
|---|
| 186 | Module::set_icon(const Glib::RefPtr<Gdk::Pixbuf>& icon) |
|---|
| 187 | { |
|---|
| 188 | if (_icon_box) { |
|---|
| 189 | delete _icon_box; |
|---|
| 190 | _icon_box = 0; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | if (icon) { |
|---|
| 194 | _icon_box = new Gnome::Canvas::Pixbuf(*this, 8, 10, icon); |
|---|
| 195 | double scale = _icon_size / (icon->get_width() > icon->get_height() ? |
|---|
| 196 | icon->get_width() : icon->get_height()); |
|---|
| 197 | _icon_box->affine_relative(Gnome::Art::AffineTrans::scaling(scale)); |
|---|
| 198 | _icon_box->show(); |
|---|
| 199 | } |
|---|
| 200 | resize(); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | void |
|---|
| 205 | Module::zoom(double z) |
|---|
| 206 | { |
|---|
| 207 | _canvas_title.property_size() = static_cast<int>(floor(9000.0f * z)); |
|---|
| 208 | for (PortVector::iterator p = _ports.begin(); p != _ports.end(); ++p) |
|---|
| 209 | (*p)->zoom(z); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | void |
|---|
| 214 | Module::set_highlighted(bool b) |
|---|
| 215 | { |
|---|
| 216 | if (b) { |
|---|
| 217 | _module_box.property_fill_color_rgba() = MODULE_HILITE_FILL_COLOUR; |
|---|
| 218 | _module_box.property_outline_color_rgba() = MODULE_HILITE_OUTLINE_COLOUR; |
|---|
| 219 | } else { |
|---|
| 220 | _module_box.property_fill_color_rgba() = _color; |
|---|
| 221 | _module_box.property_outline_color_rgba() = MODULE_OUTLINE_COLOUR; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | void |
|---|
| 227 | Module::set_selected(bool selected) |
|---|
| 228 | { |
|---|
| 229 | Item::set_selected(selected); |
|---|
| 230 | assert(_selected == selected); |
|---|
| 231 | |
|---|
| 232 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 233 | if (!canvas) |
|---|
| 234 | return; |
|---|
| 235 | |
|---|
| 236 | if (selected) { |
|---|
| 237 | _module_box.property_outline_color_rgba() = MODULE_HILITE_OUTLINE_COLOUR; |
|---|
| 238 | _module_box.property_dash() = canvas->select_dash(); |
|---|
| 239 | } else { |
|---|
| 240 | _module_box.property_fill_color_rgba() = _color; |
|---|
| 241 | _module_box.property_outline_color_rgba() = MODULE_OUTLINE_COLOUR; |
|---|
| 242 | _module_box.property_dash() = NULL; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | /** Get the port on this module at world coordinate @a x @a y. |
|---|
| 248 | */ |
|---|
| 249 | boost::shared_ptr<Port> |
|---|
| 250 | Module::port_at(double x, double y) |
|---|
| 251 | { |
|---|
| 252 | x -= property_x(); |
|---|
| 253 | y -= property_y(); |
|---|
| 254 | |
|---|
| 255 | for (PortVector::iterator p = _ports.begin(); p != _ports.end(); ++p) { |
|---|
| 256 | boost::shared_ptr<Port> port = *p; |
|---|
| 257 | if (x > port->property_x() && x < port->property_x() + port->width() |
|---|
| 258 | && y > port->property_y() && y < port->property_y() + port->height()) { |
|---|
| 259 | return port; |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | return boost::shared_ptr<Port>(); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | void |
|---|
| 268 | Module::remove_port(boost::shared_ptr<Port> port) |
|---|
| 269 | { |
|---|
| 270 | PortVector::iterator i = std::find(_ports.begin(), _ports.end(), port); |
|---|
| 271 | |
|---|
| 272 | if (i != _ports.end()) { |
|---|
| 273 | _ports.erase(i); |
|---|
| 274 | |
|---|
| 275 | // Find new widest input or output, if necessary |
|---|
| 276 | if (port->is_input() && port->width() >= _widest_input) { |
|---|
| 277 | _widest_input = 0; |
|---|
| 278 | for (PortVector::iterator i = _ports.begin(); i != _ports.end(); ++i) { |
|---|
| 279 | const boost::shared_ptr<Port> p = (*i); |
|---|
| 280 | if (p->is_input() && p->width() >= _widest_input) |
|---|
| 281 | _widest_input = p->width(); |
|---|
| 282 | } |
|---|
| 283 | } else if (port->is_output() && port->width() >= _widest_output) { |
|---|
| 284 | _widest_output = 0; |
|---|
| 285 | for (PortVector::iterator i = _ports.begin(); i != _ports.end(); ++i) { |
|---|
| 286 | const boost::shared_ptr<Port> p = (*i); |
|---|
| 287 | if (p->is_output() && p->width() >= _widest_output) |
|---|
| 288 | _widest_output = p->width(); |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | resize(); |
|---|
| 293 | port->hide(); |
|---|
| 294 | port.reset(); |
|---|
| 295 | |
|---|
| 296 | } else { |
|---|
| 297 | std::cerr << "Unable to find port " << port->name() << " to remove." << std::endl; |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | void |
|---|
| 303 | Module::set_width(double w) |
|---|
| 304 | { |
|---|
| 305 | const bool growing = (w > _width); |
|---|
| 306 | |
|---|
| 307 | _width = w; |
|---|
| 308 | _module_box.property_x2() = _module_box.property_x1() + w; |
|---|
| 309 | if (_stacked_border) |
|---|
| 310 | _stacked_border->property_x2() = _stacked_border->property_x1() + w; |
|---|
| 311 | |
|---|
| 312 | if (growing) |
|---|
| 313 | fit_canvas(); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | void |
|---|
| 318 | Module::set_height(double h) |
|---|
| 319 | { |
|---|
| 320 | const bool growing = (h > _height); |
|---|
| 321 | |
|---|
| 322 | _height = h; |
|---|
| 323 | _module_box.property_y2() = _module_box.property_y1() + h; |
|---|
| 324 | if (_stacked_border) |
|---|
| 325 | _stacked_border->property_y2() = _stacked_border->property_y1() + h; |
|---|
| 326 | |
|---|
| 327 | if (growing) |
|---|
| 328 | fit_canvas(); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | /** Move relative to current location. |
|---|
| 333 | * |
|---|
| 334 | * @param dx distance to move along x axis (in world units) |
|---|
| 335 | * @param dy distance to move along y axis (in world units) |
|---|
| 336 | */ |
|---|
| 337 | void |
|---|
| 338 | Module::move(double dx, double dy) |
|---|
| 339 | { |
|---|
| 340 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 341 | if (!canvas) |
|---|
| 342 | return; |
|---|
| 343 | |
|---|
| 344 | double new_x = property_x() + dx; |
|---|
| 345 | double new_y = property_y() + dy; |
|---|
| 346 | |
|---|
| 347 | if (new_x < 0) |
|---|
| 348 | dx = property_x() * -1; |
|---|
| 349 | else if (new_x + _width > canvas->width()) |
|---|
| 350 | dx = canvas->width() - property_x() - _width; |
|---|
| 351 | |
|---|
| 352 | if (new_y < 0) |
|---|
| 353 | dy = property_y() * -1; |
|---|
| 354 | else if (new_y + _height > canvas->height()) |
|---|
| 355 | dy = canvas->height() - property_y() - _height; |
|---|
| 356 | |
|---|
| 357 | Gnome::Canvas::Group::move(dx, dy); |
|---|
| 358 | |
|---|
| 359 | // Deal with moving the connection lines |
|---|
| 360 | for (PortVector::iterator p = _ports.begin(); p != _ports.end(); ++p) |
|---|
| 361 | (*p)->move_connections(); |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | /** Move to the specified absolute coordinate on the canvas. |
|---|
| 366 | * |
|---|
| 367 | * @param x x coordinate to move to (in world units) |
|---|
| 368 | * @param y y coordinate to move to (in world units) |
|---|
| 369 | */ |
|---|
| 370 | void |
|---|
| 371 | Module::move_to(double x, double y) |
|---|
| 372 | { |
|---|
| 373 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 374 | if (!canvas) |
|---|
| 375 | return; |
|---|
| 376 | |
|---|
| 377 | assert(canvas->width() > 0); |
|---|
| 378 | assert(canvas->height() > 0); |
|---|
| 379 | |
|---|
| 380 | if (x < 0) x = 0; |
|---|
| 381 | if (y < 0) y = 0; |
|---|
| 382 | if (x + _width > canvas->width()) x = canvas->width() - _width - 1; |
|---|
| 383 | if (y + _height > canvas->height()) y = canvas->height() - _height - 1; |
|---|
| 384 | |
|---|
| 385 | assert(x >= 0); |
|---|
| 386 | assert(y >= 0); |
|---|
| 387 | |
|---|
| 388 | if (x + _width >= canvas->width() || y + _height >= canvas->height()) { |
|---|
| 389 | double x1, y1, x2, y2; |
|---|
| 390 | canvas->get_scroll_region(x1, y1, x2, y2); |
|---|
| 391 | canvas->set_scroll_region(x1, y1, std::max(x2, x + _width), std::max(y2, y + _height)); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | property_x() = x; |
|---|
| 395 | property_y() = y; |
|---|
| 396 | |
|---|
| 397 | // Actually move (stupid gnomecanvas) |
|---|
| 398 | move(0, 0); |
|---|
| 399 | |
|---|
| 400 | // Update any connection line positions |
|---|
| 401 | for (PortVector::iterator p = _ports.begin(); p != _ports.end(); ++p) |
|---|
| 402 | (*p)->move_connections(); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | void |
|---|
| 407 | Module::set_name(const string& n) |
|---|
| 408 | { |
|---|
| 409 | if (_name != n) { |
|---|
| 410 | string old_name = _name; |
|---|
| 411 | _name = n; |
|---|
| 412 | _canvas_title.property_text() = _name; |
|---|
| 413 | _title_width = _canvas_title.property_text_width(); |
|---|
| 414 | _title_height = _canvas_title.property_text_height(); |
|---|
| 415 | if (_title_visible) |
|---|
| 416 | resize(); |
|---|
| 417 | } |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | /** Add a port to this module. |
|---|
| 422 | * |
|---|
| 423 | * A reference to p is held until remove_port is called to remove it. Note |
|---|
| 424 | * that the module will not be resized (for performance reasons when adding |
|---|
| 425 | * many ports in succession), so you must explicitly call resize() after this |
|---|
| 426 | * for the module to look at all sensible. |
|---|
| 427 | */ |
|---|
| 428 | void |
|---|
| 429 | Module::add_port(boost::shared_ptr<Port> p) |
|---|
| 430 | { |
|---|
| 431 | PortVector::const_iterator i = std::find(_ports.begin(), _ports.end(), p); |
|---|
| 432 | if (i != _ports.end()) // already added |
|---|
| 433 | return; // so do nothing |
|---|
| 434 | |
|---|
| 435 | if (p->is_input() && p->natural_width() > _widest_input) |
|---|
| 436 | _widest_input = p->width(); |
|---|
| 437 | else if (p->is_output() && p->natural_width() > _widest_output) |
|---|
| 438 | _widest_output = p->width(); |
|---|
| 439 | |
|---|
| 440 | _ports.push_back(p); |
|---|
| 441 | |
|---|
| 442 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 443 | if (canvas) |
|---|
| 444 | p->signal_event().connect( |
|---|
| 445 | sigc::bind(sigc::mem_fun(canvas.get(), &Canvas::port_event), p)); |
|---|
| 446 | |
|---|
| 447 | p->signal_renamed.connect(sigc::mem_fun(this, &Module::port_renamed)); |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | /** Embed a widget on the module. |
|---|
| 452 | * |
|---|
| 453 | * Resize may need to be called after this to ensure the module |
|---|
| 454 | * displays correctly. |
|---|
| 455 | */ |
|---|
| 456 | void |
|---|
| 457 | Module::embed(Gtk::Container* widget) |
|---|
| 458 | { |
|---|
| 459 | if (!widget) { |
|---|
| 460 | delete _embed_item; |
|---|
| 461 | _embed_item = NULL; |
|---|
| 462 | _embed_width = 0; |
|---|
| 463 | _embed_height = 0; |
|---|
| 464 | return; |
|---|
| 465 | } else { |
|---|
| 466 | _embed_container = manage(widget); |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | _embed_container->set_border_width(2); |
|---|
| 470 | _embed_container->show_all(); |
|---|
| 471 | |
|---|
| 472 | const double y = 4 + _title_height; |
|---|
| 473 | delete _embed_item; |
|---|
| 474 | _embed_item = new Gnome::Canvas::Widget(*this, 2.0, y, *_embed_container); |
|---|
| 475 | _embed_item->show(); |
|---|
| 476 | |
|---|
| 477 | Gtk::Requisition r = _embed_container->size_request(); |
|---|
| 478 | embed_size_request(&r, true); |
|---|
| 479 | |
|---|
| 480 | _embed_item->raise_to_top(); |
|---|
| 481 | |
|---|
| 482 | _embed_container->signal_size_request().connect(sigc::bind( |
|---|
| 483 | sigc::mem_fun(this, &Module::embed_size_request), false)); |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | void |
|---|
| 488 | Module::embed_size_request(Gtk::Requisition* r, bool force) |
|---|
| 489 | { |
|---|
| 490 | if (!force && _embed_width == r->width && _embed_height == r->height) |
|---|
| 491 | return; |
|---|
| 492 | |
|---|
| 493 | _embed_width = r->width; |
|---|
| 494 | _embed_height = r->height; |
|---|
| 495 | |
|---|
| 496 | resize(); |
|---|
| 497 | |
|---|
| 498 | Gtk::Allocation allocation; |
|---|
| 499 | allocation.set_width(r->width + 4); |
|---|
| 500 | allocation.set_height(r->height + 4); |
|---|
| 501 | |
|---|
| 502 | _embed_container->size_allocate(allocation); |
|---|
| 503 | _embed_item->property_width() = r->width - 4; |
|---|
| 504 | _embed_item->property_height() = r->height; |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | void |
|---|
| 509 | Module::measure_ports() |
|---|
| 510 | { |
|---|
| 511 | _widest_input = 0.0; |
|---|
| 512 | _widest_output = 0.0; |
|---|
| 513 | for (PortVector::iterator pi = _ports.begin(); pi != _ports.end(); ++pi) { |
|---|
| 514 | const boost::shared_ptr<Port> p = (*pi); |
|---|
| 515 | p->show_label(_show_port_labels); |
|---|
| 516 | if (p->is_input()) { |
|---|
| 517 | _widest_input = 0.0; |
|---|
| 518 | for (PortVector::iterator pi = _ports.begin(); pi != _ports.end(); ++pi) |
|---|
| 519 | if ((*pi)->natural_width() > _widest_input) |
|---|
| 520 | _widest_input = (*pi)->natural_width(); |
|---|
| 521 | } else { |
|---|
| 522 | _widest_output = 0.0; |
|---|
| 523 | for (PortVector::iterator pi = _ports.begin(); pi != _ports.end(); ++pi) |
|---|
| 524 | if ((*pi)->natural_width() > _widest_output) |
|---|
| 525 | _widest_output = (*pi)->natural_width(); |
|---|
| 526 | } |
|---|
| 527 | } |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | /** Resize the module to fit its contents best. |
|---|
| 532 | */ |
|---|
| 533 | void |
|---|
| 534 | Module::resize() |
|---|
| 535 | { |
|---|
| 536 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 537 | if (!canvas) |
|---|
| 538 | return; |
|---|
| 539 | |
|---|
| 540 | switch (canvas->direction()) { |
|---|
| 541 | case Canvas::HORIZONTAL: |
|---|
| 542 | resize_horiz(); |
|---|
| 543 | break; |
|---|
| 544 | case Canvas::VERTICAL: |
|---|
| 545 | resize_vert(); |
|---|
| 546 | break; |
|---|
| 547 | } |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | void |
|---|
| 552 | Module::resize_horiz() |
|---|
| 553 | { |
|---|
| 554 | if (_port_renamed) { |
|---|
| 555 | measure_ports(); |
|---|
| 556 | _port_renamed = false; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | // The amount of space between a port edge and the module edge (on the |
|---|
| 560 | // side that the port isn't right on the edge). |
|---|
| 561 | const double hor_pad = (_title_visible ? 10.0 : 20.0); |
|---|
| 562 | |
|---|
| 563 | double width = (_title_visible ? _title_width + 10.0 : 1.0); |
|---|
| 564 | |
|---|
| 565 | if (_icon_box) |
|---|
| 566 | width += _icon_size + 2; |
|---|
| 567 | |
|---|
| 568 | // Title is wide, put inputs and outputs beside each other |
|---|
| 569 | bool horiz = (_widest_input + _widest_output + 10.0 < std::max(width, _embed_width)); |
|---|
| 570 | |
|---|
| 571 | // Fit ports to module (or vice-versa) |
|---|
| 572 | double widest_in = _widest_input; |
|---|
| 573 | double widest_out = _widest_output; |
|---|
| 574 | double expand_w = (horiz ? (width / 2.0) : width) - hor_pad; |
|---|
| 575 | if (_show_port_labels) { |
|---|
| 576 | widest_in = (_embed_item ? _widest_input : std::max(_widest_input, expand_w)); |
|---|
| 577 | widest_out = (_embed_item ? _widest_output : std::max(_widest_output, expand_w)); |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | const double widest = std::max(widest_in, widest_out); |
|---|
| 581 | |
|---|
| 582 | double above_w = std::max(width, widest + hor_pad); |
|---|
| 583 | double between_w = std::max(width, widest_in + widest_out + _embed_width); |
|---|
| 584 | |
|---|
| 585 | above_w = std::max(above_w, _embed_width); |
|---|
| 586 | |
|---|
| 587 | // Basic height contains title, icon |
|---|
| 588 | double header_height = 2.0; |
|---|
| 589 | if (_show_port_labels) { |
|---|
| 590 | if (_title_visible) |
|---|
| 591 | header_height += 2 + _title_height; |
|---|
| 592 | if (_icon_box && _icon_size > _title_height) |
|---|
| 593 | header_height += _icon_size - _title_height; |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | double height = header_height; |
|---|
| 597 | |
|---|
| 598 | double above_h = 0.0f; |
|---|
| 599 | if (_ports.size() > 0) |
|---|
| 600 | above_h += _ports.size() * ((*_ports.begin())->height()+2.0); |
|---|
| 601 | |
|---|
| 602 | //double between_h = std::max(above_h, _embed_height); |
|---|
| 603 | above_h += _embed_height; |
|---|
| 604 | |
|---|
| 605 | /*cerr << above_w << "x" << above_h << "(" << above_w * above_h << ") ? " |
|---|
| 606 | << between_w << "x" << between_h << "(" << between_w * between_h << ")" << endl;*/ |
|---|
| 607 | |
|---|
| 608 | // Decide where to place embedded widget if necessary) |
|---|
| 609 | enum { BETWEEN, ABOVE } embed_pos = ABOVE; |
|---|
| 610 | //if (above_w * above_h >= between_w * between_h) // minimize area |
|---|
| 611 | if (_embed_width < _embed_height * 2.0) { |
|---|
| 612 | embed_pos = BETWEEN; |
|---|
| 613 | width = between_w; |
|---|
| 614 | if (_embed_item) |
|---|
| 615 | _embed_item->property_x() = widest_in; |
|---|
| 616 | } else { |
|---|
| 617 | width = above_w; |
|---|
| 618 | if (_embed_item) |
|---|
| 619 | _embed_item->property_x() = 0.0f; |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | if (!_title_visible) { |
|---|
| 623 | if (_ports.size() > 0) |
|---|
| 624 | height += 0.5; |
|---|
| 625 | if (_widest_input == 0.0 || _widest_output == 0.0f) |
|---|
| 626 | width += 10.0; |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | width += _border_width * 2.0; |
|---|
| 630 | |
|---|
| 631 | // Actually set width and height |
|---|
| 632 | set_width(width); |
|---|
| 633 | |
|---|
| 634 | // Offset ports below embedded widget |
|---|
| 635 | if (embed_pos == ABOVE) { |
|---|
| 636 | header_height += _embed_height; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | // Move ports to appropriate locations |
|---|
| 640 | int i = 0; |
|---|
| 641 | bool last_was_input = false; |
|---|
| 642 | double y = 0.0; |
|---|
| 643 | double h = 0.0; |
|---|
| 644 | for (PortVector::iterator pi = _ports.begin(); pi != _ports.end(); ++pi) { |
|---|
| 645 | const boost::shared_ptr<Port> p = (*pi); |
|---|
| 646 | h = p->height(); |
|---|
| 647 | |
|---|
| 648 | if (p->is_input()) { |
|---|
| 649 | y = header_height + (i * (h + 1.0)); |
|---|
| 650 | ++i; |
|---|
| 651 | p->set_width(widest_in); |
|---|
| 652 | p->property_x() = -0.5; |
|---|
| 653 | p->property_y() = y; |
|---|
| 654 | last_was_input = true; |
|---|
| 655 | } else { |
|---|
| 656 | if (!horiz || !last_was_input) { |
|---|
| 657 | y = header_height + (i * (h + 1.0)); |
|---|
| 658 | ++i; |
|---|
| 659 | } |
|---|
| 660 | p->set_width(widest_out); |
|---|
| 661 | p->property_x() = _width - p->width() + 0.5; |
|---|
| 662 | p->property_y() = y; |
|---|
| 663 | last_was_input = false; |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | (*pi)->move_connections(); |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | if (_ports.empty()) |
|---|
| 670 | h += header_height; |
|---|
| 671 | |
|---|
| 672 | height = y + h + 2.0; |
|---|
| 673 | if (_embed_item && embed_pos == BETWEEN) |
|---|
| 674 | height = std::max(height, _embed_height + header_height + 2.0); |
|---|
| 675 | |
|---|
| 676 | set_height(height); |
|---|
| 677 | |
|---|
| 678 | if (_title_visible) { |
|---|
| 679 | if (_show_port_labels) |
|---|
| 680 | _canvas_title.property_y() = _title_height / 2.0; |
|---|
| 681 | else |
|---|
| 682 | _canvas_title.property_y() = (_height / 2.0) - 1.0; |
|---|
| 683 | if (_icon_box) |
|---|
| 684 | _canvas_title.property_x() = _icon_size + (_width - _icon_size + 1)/2.0; |
|---|
| 685 | else |
|---|
| 686 | _canvas_title.property_x() = _width/2.0; |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | // Make things actually move to their new locations (?!) |
|---|
| 690 | move(0, 0); |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | void |
|---|
| 695 | Module::resize_vert() |
|---|
| 696 | { |
|---|
| 697 | if (_port_renamed) { |
|---|
| 698 | measure_ports(); |
|---|
| 699 | _port_renamed = false; |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | double width = (_title_visible |
|---|
| 703 | ? _canvas_title.property_text_width() + 10.0 |
|---|
| 704 | : 1.0); |
|---|
| 705 | |
|---|
| 706 | if (_icon_box) |
|---|
| 707 | width += _icon_size + 2; |
|---|
| 708 | |
|---|
| 709 | double height = 4.0; |
|---|
| 710 | if (_show_port_labels) { |
|---|
| 711 | if (_title_visible) |
|---|
| 712 | height += 2 + _title_height; |
|---|
| 713 | if (_icon_box && _icon_size > _title_height) |
|---|
| 714 | height += _icon_size - _title_height; |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | height += MODULE_EMPTY_PORT_DEPTH * 4.0; |
|---|
| 718 | |
|---|
| 719 | // Move ports to appropriate locations |
|---|
| 720 | int i = 0; |
|---|
| 721 | bool last_was_input = false; |
|---|
| 722 | double x = 0.0; |
|---|
| 723 | static const double PAD = 2.0; |
|---|
| 724 | for (PortVector::iterator pi = _ports.begin(); pi != _ports.end(); ++pi) { |
|---|
| 725 | const boost::shared_ptr<Port> p = (*pi); |
|---|
| 726 | p->set_width(MODULE_EMPTY_PORT_BREADTH); |
|---|
| 727 | p->set_height(MODULE_EMPTY_PORT_DEPTH); |
|---|
| 728 | if (p->is_input()) { |
|---|
| 729 | x = PAD + (i * (MODULE_EMPTY_PORT_BREADTH + 1.0)); |
|---|
| 730 | ++i; |
|---|
| 731 | p->property_x() = x; |
|---|
| 732 | p->property_y() = -0.5; |
|---|
| 733 | last_was_input = true; |
|---|
| 734 | } else { |
|---|
| 735 | if (!last_was_input) { |
|---|
| 736 | x = PAD + (i * (MODULE_EMPTY_PORT_BREADTH + 1.0)); |
|---|
| 737 | ++i; |
|---|
| 738 | } |
|---|
| 739 | p->property_x() = x; |
|---|
| 740 | p->property_y() = height - p->height() + 0.5; |
|---|
| 741 | last_was_input = false; |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | (*pi)->move_connections(); |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | x += MODULE_EMPTY_PORT_BREADTH; |
|---|
| 748 | |
|---|
| 749 | if (x > width - 2.0) |
|---|
| 750 | width = x + 2.0; |
|---|
| 751 | |
|---|
| 752 | set_width(width); |
|---|
| 753 | set_height(height); |
|---|
| 754 | |
|---|
| 755 | if (_title_visible) { |
|---|
| 756 | _canvas_title.property_y() = (_height / 2.0) - 1.0; |
|---|
| 757 | _canvas_title.property_x() = (_width / 2.0); |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | // Make things actually move to their new locations (?!) |
|---|
| 761 | move(0, 0); |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | void |
|---|
| 766 | Module::set_show_port_labels(bool b) |
|---|
| 767 | { |
|---|
| 768 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 769 | if (!canvas) |
|---|
| 770 | return; |
|---|
| 771 | |
|---|
| 772 | if (canvas->direction() == Canvas::VERTICAL) |
|---|
| 773 | b = false; // can't show labels in vertical mode |
|---|
| 774 | |
|---|
| 775 | _show_port_labels = b; |
|---|
| 776 | _port_renamed = true; // remeasure |
|---|
| 777 | |
|---|
| 778 | resize(); |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | /** Expand the canvas if the module is outside bounds. |
|---|
| 783 | */ |
|---|
| 784 | void |
|---|
| 785 | Module::fit_canvas() |
|---|
| 786 | { |
|---|
| 787 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 788 | if (canvas) { |
|---|
| 789 | double canvas_width = canvas->width(); |
|---|
| 790 | double canvas_height = canvas->height(); |
|---|
| 791 | |
|---|
| 792 | canvas_width = std::max(canvas_width, property_x() + _width + 5.0); |
|---|
| 793 | canvas_height = std::max(canvas_height, property_y() + _height + 5.0); |
|---|
| 794 | |
|---|
| 795 | canvas->resize(canvas_width, canvas_height); |
|---|
| 796 | } |
|---|
| 797 | } |
|---|
| 798 | |
|---|
| 799 | |
|---|
| 800 | void |
|---|
| 801 | Module::select_tick() |
|---|
| 802 | { |
|---|
| 803 | boost::shared_ptr<Canvas> canvas = _canvas.lock(); |
|---|
| 804 | if (canvas) |
|---|
| 805 | _module_box.property_dash() = canvas->select_dash(); |
|---|
| 806 | } |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | void |
|---|
| 810 | Module::set_border_color(uint32_t c) |
|---|
| 811 | { |
|---|
| 812 | _border_color = c; |
|---|
| 813 | _module_box.property_outline_color_rgba() = _border_color; |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | void |
|---|
| 818 | Module::set_default_base_color() |
|---|
| 819 | { |
|---|
| 820 | _color = MODULE_FILL_COLOUR; |
|---|
| 821 | _module_box.property_fill_color_rgba() = _color; |
|---|
| 822 | if (_stacked_border) |
|---|
| 823 | _stacked_border->property_fill_color_rgba() = _color; |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | void |
|---|
| 828 | Module::set_base_color(uint32_t c) |
|---|
| 829 | { |
|---|
| 830 | _color = c; |
|---|
| 831 | _module_box.property_fill_color_rgba() = _color; |
|---|
| 832 | if (_stacked_border) |
|---|
| 833 | _stacked_border->property_fill_color_rgba() = _color; |
|---|
| 834 | } |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | } // namespace FlowCanvas |
|---|
| 838 | |
|---|