e2d1e07027538e1358e02e5c8f1f6f8fa53ca601
[ardour.git] / gtk2_ardour / port_matrix_component.cc
1 /*
2     Copyright (C) 2002-2009 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21 #include "port_matrix_component.h"
22
23 /** Constructor.
24  *  @param p Port matrix that we're in.
25  */
26 PortMatrixComponent::PortMatrixComponent (PortMatrixBody* b)
27         : _body (b),
28           _pixmap (0),
29           _render_required (true),
30           _dimension_computation_required (true)
31 {
32         
33 }
34
35 /** Destructor */
36 PortMatrixComponent::~PortMatrixComponent ()
37 {
38         if (_pixmap) {
39                 gdk_pixmap_unref (_pixmap);
40         }
41 }
42
43 void
44 PortMatrixComponent::setup ()
45 {
46         _dimension_computation_required = true;
47         _render_required = true;
48 }
49
50 GdkPixmap *
51 PortMatrixComponent::get_pixmap (GdkDrawable *drawable)
52 {
53         if (_render_required) {
54
55                 if (_dimension_computation_required) {
56                         compute_dimensions ();
57                         _dimension_computation_required = false;
58                 }
59
60                 /* we may be zero width or height; if so, just
61                    use the smallest allowable pixmap */
62                 if (_width == 0) {
63                         _width = 1;
64                 }
65                 if (_height == 0) {
66                         _height = 1;
67                 }
68
69                 /* make a pixmap of the right size */
70                 if (_pixmap) {
71                         gdk_pixmap_unref (_pixmap);
72                 }
73                 _pixmap = gdk_pixmap_new (drawable, _width, _height, -1);
74
75                 /* render */
76                 cairo_t* cr = gdk_cairo_create (_pixmap);
77                 render (cr);
78                 cairo_destroy (cr);
79
80                 _render_required = false;
81         }
82
83         return _pixmap;
84 }
85
86 void
87 PortMatrixComponent::set_source_rgb (cairo_t *cr, Gdk::Color const & c)
88 {
89         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
90 }
91
92 void
93 PortMatrixComponent::set_source_rgba (cairo_t *cr, Gdk::Color const & c, double a)
94 {
95         cairo_set_source_rgba (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p(), a);
96 }
97
98 std::pair<uint32_t, uint32_t>
99 PortMatrixComponent::dimensions ()
100 {
101         if (_dimension_computation_required) {
102                 compute_dimensions ();
103                 _dimension_computation_required = false;
104         }
105
106         return std::make_pair (_width, _height);
107 }