-
Notifications
You must be signed in to change notification settings - Fork 0
/
meter.c
210 lines (162 loc) · 4.73 KB
/
meter.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "meter.h"
typedef struct _GtkMeterPrivate GtkMeterPrivate;
struct _GtkMeterPrivate
{
gdouble low;
gdouble high;
gdouble value;
gchar *name;
};
#define GTK_METER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_METER, GtkMeterPrivate))
G_DEFINE_TYPE (GtkMeter, gtk_meter, GTK_TYPE_DRAWING_AREA);
static void
gtk_meter_redraw_canvas (GtkMeter *meter)
{
GtkWidget *widget = GTK_WIDGET (meter);
GtkAllocation allocation;
gtk_widget_get_allocation (widget, &allocation);
gtk_widget_queue_draw_area (widget, allocation.x, allocation.y,
allocation.width, allocation.height);
}
static gboolean
gtk_meter_draw (GtkWidget *meter, cairo_t *cr)
{
gint width = gtk_widget_get_allocated_width (meter);
gint height = gtk_widget_get_allocated_height (meter);
gdouble cx = width / 2.0;
gdouble cy = height / 2.0;
gdouble radius = MIN (width/2, height/2) - 5;
gdouble ny = cy + radius * 0.9;
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
gdouble low = priv->low;
gdouble high = priv->high;
gdouble value = priv->value;
gdouble theta = (((value - low) / (high - low)) * 0.5 - 0.25) * M_PI;
cairo_text_extents_t extents;
gchar *name = priv->name;
gdouble inc;
cairo_pattern_t *pat;
cairo_arc (cr, cx, cy, radius, 0, 2 * M_PI);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_fill_preserve (cr);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_stroke (cr);
cairo_set_line_width (cr, 2.0 * cairo_get_line_width (cr));
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
for (inc = -0.25; inc <= 0.25; inc += 0.05)
{
cairo_move_to (cr, cx + radius * 1.2 * sin (inc * M_PI),
ny - radius * 1.2 * cos (inc * M_PI));
cairo_line_to (cr, cx + radius * 1.25 * sin (inc * M_PI),
ny - radius * 1.25 * cos (inc * M_PI));
cairo_stroke (cr);
}
cairo_set_source_rgb (cr, 1, 0, 0);
cairo_move_to (cr, cx, ny);
cairo_line_to (cr, cx + radius * sin (theta),
ny - radius * cos (theta));
cairo_stroke (cr);
pat = cairo_pattern_create_radial (cx, cy, radius,
0, 0, radius);
cairo_pattern_add_color_stop_rgba (pat, 0, 0, 0, 0, 0.4);
cairo_pattern_add_color_stop_rgba (pat, 1, 1, 1, 1, 0.2);
cairo_set_source (cr, pat);
cairo_arc (cr, cx, cy, radius, 0, 2 * M_PI);
cairo_fill (cr);
cairo_pattern_destroy (pat);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_arc (cr, cx, cy, radius, 0.1 * M_PI, 0.9 * M_PI);
cairo_curve_to (cr, cx, cy + radius/2,
cx - radius/2, cy + radius/4,
cx, cy + radius/4);
cairo_curve_to (cr, cx + radius/2, cy + radius/4,
cx, cy + radius/2,
cx + radius * cos (0.1 * M_PI),
cy + radius * sin (0.1 * M_PI));
cairo_close_path (cr);
cairo_fill (cr);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_text_extents (cr, name, &extents);
cairo_move_to (cr, cx - (extents.width/2 + extents.x_bearing),
cy + radius / 2.0);
cairo_show_text (cr, name);
return TRUE;
}
static void
gtk_meter_class_init (GtkMeterClass *klass)
{
GtkWidgetClass *class = GTK_WIDGET_CLASS (klass);
class->draw = gtk_meter_draw;
g_type_class_add_private (class, sizeof (GtkMeterPrivate));
}
static void
gtk_meter_init (GtkMeter *meter)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
priv->low = 0.0;
priv->high = 1.0;
priv->value = 0.5;
gtk_widget_set_has_window (GTK_WIDGET (meter), FALSE);
}
GtkWidget*
gtk_meter_new (gchar *name)
{
GtkWidget *meter = g_object_new (GTK_TYPE_METER, NULL);
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
priv->name = g_strdup(name);
return meter;
}
void
gtk_meter_set_range (GtkMeter *meter, gdouble low, gdouble high)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
priv->low = low;
priv->high = high;
gtk_meter_redraw_canvas (meter);
}
void
gtk_meter_set_value (GtkMeter *meter, gdouble value)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
if (priv->low >= value)
priv->value = priv->low;
else if (priv->high <= value)
priv->value = priv->high;
else
priv->value = value;
gtk_meter_redraw_canvas (meter);
}
void
gtk_meter_set_high (GtkMeter *meter, gdouble high)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
priv->high = high;
gtk_meter_redraw_canvas (meter);
}
void
gtk_meter_set_low (GtkMeter *meter, gdouble low)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
priv->low = low;
gtk_meter_redraw_canvas (meter);
}
gdouble
gtk_meter_get_value (GtkMeter *meter)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
return priv->value;
}
gdouble
gtk_meter_get_low (GtkMeter *meter)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
return priv->low;
}
gdouble
gtk_meter_get_high (GtkMeter *meter)
{
GtkMeterPrivate *priv = GTK_METER_GET_PRIVATE (meter);
return priv->high;
}