2 Copyright (C) 2002-2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "port_matrix_component.h"
24 * @param p Port matrix that we're in.
26 PortMatrixComponent::PortMatrixComponent (PortMatrix* m, PortMatrixBody* b)
30 _render_required (true),
31 _dimension_computation_required (true)
37 PortMatrixComponent::~PortMatrixComponent ()
40 gdk_pixmap_unref (_pixmap);
45 PortMatrixComponent::setup ()
47 _dimension_computation_required = true;
48 _render_required = true;
52 PortMatrixComponent::get_pixmap (GdkDrawable *drawable)
54 if (_render_required) {
56 if (_dimension_computation_required) {
57 compute_dimensions ();
58 _dimension_computation_required = false;
61 /* we may be zero width or height; if so, just
62 use the smallest allowable pixmap */
70 /* make a pixmap of the right size */
72 gdk_pixmap_unref (_pixmap);
74 _pixmap = gdk_pixmap_new (drawable, _width, _height, -1);
77 cairo_t* cr = gdk_cairo_create (_pixmap);
81 _render_required = false;
88 PortMatrixComponent::set_source_rgb (cairo_t *cr, Gdk::Color const & c)
90 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
94 PortMatrixComponent::set_source_rgba (cairo_t *cr, Gdk::Color const & c, double a)
96 cairo_set_source_rgba (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p(), a);
99 std::pair<uint32_t, uint32_t>
100 PortMatrixComponent::dimensions ()
102 if (_dimension_computation_required) {
103 compute_dimensions ();
104 _dimension_computation_required = false;
107 return std::make_pair (_width, _height);
110 std::pair<std::string, double>
111 PortMatrixComponent::display_port_name (cairo_t* cr, std::string const &n, double avail) const
113 /* XXX hopefully there exists a more efficient way of doing this */
115 Glib::ustring name = Glib::ustring (n).uppercase ();
116 bool abbreviated = false;
120 if (name.length() <= 2) {
124 cairo_text_extents_t ext;
125 cairo_text_extents (cr, name.c_str(), &ext);
126 if (ext.width < avail) {
132 name = name.substr (0, name.length() - 2) + ".";
134 name = name.substr (0, name.length() - 1) + ".";
139 return std::make_pair (name, width);