Unconditionally save instant.xml on session-close
[ardour.git] / gtk2_ardour / led.cc
1 /*
2  * Copyright (C) 2010-2012 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2011 David Robillard <d@drobilla.net>
4  * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <iostream>
22 #include <cmath>
23 #include <algorithm>
24
25 #include "led.h"
26
27 using namespace Gdk;
28 using namespace Gtk;
29 using namespace Glib;
30 using namespace Gtkmm2ext;
31
32 LED::LED()
33         : _diameter (0.0)
34         , _red (0.0)
35         , _green (1.0)
36         , _blue (0.0)
37         , _fixed_diameter (false)
38 {
39 }
40
41 LED::~LED()
42 {
43 }
44
45 void
46 LED::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
47 {
48         cairo_t* cr = ctx->cobj();
49         if (!_fixed_diameter) {
50                 _diameter = std::min (get_width(), get_height());
51         }
52
53         //background
54
55         Widget* parent;
56         RefPtr<Style> style;
57         Color c;
58
59         parent = get_parent ();
60
61         while (parent && !parent->get_has_window()) {
62                 parent = parent->get_parent();
63         }
64
65         if (parent && parent->get_has_window()) {
66                 style = parent->get_style ();
67                 c = style->get_bg (parent->get_state());
68         } else {
69                 style = get_style ();
70                 c = style->get_bg (get_state());
71         }
72
73 #if 0
74         cairo_rectangle(cr, 0, 0, _width, _height);
75         cairo_stroke_preserve(cr);
76         cairo_set_source_rgb(cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
77         cairo_fill(cr);
78 #endif
79
80         cairo_translate(cr, get_width()/2, get_height()/2);
81
82         //inset
83         cairo_pattern_t *pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
84         cairo_pattern_add_color_stop_rgba (pat, 0, 0,0,0, 0.4);
85         cairo_pattern_add_color_stop_rgba (pat, 1, 1,1,1, 0.7);
86         cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
87         cairo_set_source (cr, pat);
88         cairo_fill (cr);
89         cairo_pattern_destroy (pat);
90
91         //black ring
92         cairo_set_source_rgb (cr, 0, 0, 0);
93         cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
94         cairo_fill(cr);
95
96         //knob color
97         cairo_set_source_rgba (cr, _red, _green, _blue, (active_state() == Gtkmm2ext::ExplicitActive) ? 0.8 : 0.2);
98         cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
99         cairo_fill(cr);
100
101         //reflection
102         cairo_scale(cr, 0.7, 0.7);
103         cairo_pattern_t *pat2 = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
104         cairo_pattern_add_color_stop_rgba (pat2, 0, 1,1,1, active_state() ? 0.4 : 0.2);
105         cairo_pattern_add_color_stop_rgba (pat2, 1, 1,1,1, 0.0);
106         cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
107         cairo_set_source (cr, pat2);
108         cairo_fill (cr);
109         cairo_pattern_destroy (pat2);
110
111         cairo_stroke (cr);
112 }
113
114 void
115 LED::set_diameter (float d)
116 {
117         _diameter = (d*2) + 5.0;
118
119         if (_diameter != 0.0) {
120                 _fixed_diameter = true;
121         }
122
123         set_dirty ();
124 }
125
126 void
127 LED::on_realize ()
128 {
129         set_colors_from_style ();
130         CairoWidget::on_realize ();
131 }
132
133 void
134 LED::on_size_request (Gtk::Requisition* req)
135 {
136         if (_fixed_diameter) {
137                 req->width = _diameter;
138                 req->height = _diameter;
139         } else {
140                 CairoWidget::on_size_request (req);
141         }
142 }
143
144 void
145 LED::set_colors_from_style ()
146 {
147         RefPtr<Style> style = get_style();
148         Color c;
149
150         switch (_visual_state) {
151         case VisualState(0):
152                 c = style->get_fg (STATE_NORMAL);
153                 break;
154         default:
155                 c = style->get_fg (STATE_ACTIVE);
156                 break;
157         }
158
159         _red = c.get_red_p ();
160         _green = c.get_green_p ();
161         _blue = c.get_blue_p ();
162
163         set_dirty ();
164 }