File: ir_meter.cc

package info (click to toggle)
ir.lv2 1.3.3~dfsg0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 356 kB
  • ctags: 440
  • sloc: cpp: 3,112; ansic: 188; makefile: 90
file content (177 lines) | stat: -rw-r--r-- 5,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
    Copyright (C) 2011 Tom Szilagyi

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <gtk/gtk.h>
#include <math.h>
#include <stdlib.h>

#include "ir_meter.h"
#include "ir_utils.h"

G_DEFINE_TYPE(IRMeter, ir_meter, GTK_TYPE_DRAWING_AREA);

typedef struct _IRMeterPrivate IRMeterPrivate;

#define IR_METER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), IR_METER_TYPE, IRMeterPrivate))

struct _IRMeterPrivate {
	GdkPixmap * pixmap;
	float level;
};


static gboolean ir_meter_expose(GtkWidget * widget, GdkEventExpose * event);
static gboolean ir_meter_configure(GtkWidget * widget, GdkEventConfigure * event);
static void ir_meter_destroy(GtkObject * object);

static void ir_meter_class_init(IRMeterClass * cls) {
	GtkObjectClass * obj_class = GTK_OBJECT_CLASS(cls); /* changed G_ to GTK_ */
	GtkWidgetClass * widget_class = GTK_WIDGET_CLASS(cls);
	widget_class->expose_event = ir_meter_expose;
	widget_class->configure_event = ir_meter_configure;
	obj_class->destroy = ir_meter_destroy;
	g_type_class_add_private(obj_class, sizeof(IRMeterPrivate));
}

static void ir_meter_init(IRMeter * w) {
	IRMeterPrivate * p = IR_METER_GET_PRIVATE(w);
	p->pixmap = NULL;
	p->level = 0.0;
}

static void ir_meter_destroy(GtkObject * object) {
	IRMeter * w = IR_METER(object);
	IRMeterPrivate * p = IR_METER_GET_PRIVATE(w);
	if (p->pixmap) {
		gdk_pixmap_unref(p->pixmap);
		p->pixmap = NULL;
	}
}

static void draw_fullscale(GtkWidget * widget) {
	IRMeterPrivate * p = IR_METER_GET_PRIVATE(widget);
	int w = widget->allocation.width;
	int h = widget->allocation.height;

	cairo_t * cr = gdk_cairo_create(p->pixmap);

	float fzero = 1.0f + 90.0f/96.0f; /* result of convert_real_to_scale(ADJ_*_GAIN, 0) */
	fzero = exp10(fzero);
	fzero = (fzero - 10.0f) / 90.0f;
	int zero = h * (1.0 - fzero);

	cairo_rectangle(cr, 0, 0, w, zero);
	cairo_set_source_rgb(cr, 1, 0, 0);
	cairo_fill_preserve(cr);
	cairo_set_source_rgb(cr, 1, 0, 0);
	cairo_stroke(cr);

	int n = h - zero - 1;
	int i;
	for (i = 0; i < n/2; i++) {
		cairo_set_source_rgb(cr, 0, 1.0, 2.0*(float)i/n);
		cairo_move_to(cr, 0, zero + i + 1);
		cairo_line_to(cr, w, zero + i + 1);
		cairo_stroke(cr);
	}
	for (; i < n; i++) {
		cairo_set_source_rgb(cr, 0, 1.0 - 2.0*(float)(i-n/2)/n, 1.0);
		cairo_move_to(cr, 0, zero + i + 1);
		cairo_line_to(cr, w, zero + i + 1);
		cairo_stroke(cr);
	}
	cairo_destroy(cr);
}

static void draw(GtkWidget * widget) {
	IRMeterPrivate * p = IR_METER_GET_PRIVATE(widget);
	int w = widget->allocation.width;
	int h = widget->allocation.height;
	int pos = h * (1.0f - (p->level - 10.0f) / 90.0f);

	cairo_t * cr = gdk_cairo_create(widget->window);
	gdk_cairo_set_source_pixmap(cr, p->pixmap, 0, 0);

	cairo_rectangle(cr, 0, 0, w, pos);
	cairo_set_source_rgb(cr, 0, 0, 0);
	cairo_fill_preserve(cr);
	cairo_set_source_rgb(cr, 0, 0, 0);
	cairo_stroke(cr);

	cairo_destroy(cr);
}

static gboolean ir_meter_configure(GtkWidget * widget, GdkEventConfigure * event) {
	IRMeterPrivate * p = IR_METER_GET_PRIVATE(widget);
	int w = widget->allocation.width;
	int h = widget->allocation.height;
	if (p->pixmap) {
		gdk_pixmap_unref(p->pixmap);
	}
	p->pixmap = gdk_pixmap_new(widget->window, w, h, -1);
	draw_fullscale(widget);
	draw(widget);
	return TRUE;
}

static gboolean ir_meter_expose(GtkWidget * widget, GdkEventExpose * event) {
	IRMeterPrivate * p = IR_METER_GET_PRIVATE(widget);
	gdk_draw_drawable(widget->window,
			  widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
			  p->pixmap,
			  event->area.x, event->area.y,
			  event->area.x, event->area.y,
			  event->area.width, event->area.height);
	draw(widget);
	return FALSE;
}

void ir_meter_redraw(IRMeter * w) {
	GtkWidget * widget;
	GdkRegion * region;
	widget = GTK_WIDGET(w);
	if (!widget->window) {
		return;
	}
	region = gdk_drawable_get_clip_region(widget->window);
	gdk_window_invalidate_region(widget->window, region, TRUE);
	gdk_window_process_updates(widget->window, TRUE);
	gdk_region_destroy(region);
}

void ir_meter_redraw_all(IRMeter * w) {
	GtkWidget * widget = GTK_WIDGET(w);
	if (!widget->window) {
		return;
	}
	draw_fullscale(widget);
	ir_meter_redraw(w);
}

void ir_meter_set_level(IRMeter * w, float level) {
	if (!w || !GTK_IS_WIDGET(w)) {
		return;
	}
	IRMeterPrivate * p = IR_METER_GET_PRIVATE(w);
	p->level = level;
	ir_meter_redraw(w);
}

GtkWidget * ir_meter_new(void) {
	return (GtkWidget *)g_object_new(IR_METER_TYPE, NULL);
}