-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocsample.c
231 lines (193 loc) · 5.92 KB
/
pocsample.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* This file is part of PocPlot.
*
* PocPlot is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* PocPlot 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PocPlot; if not, see
* <https://www.gnu.org/licenses/>.
*
* Copyright 2020 Brian Stafford
*/
/*
* PocSample
*/
#include "pocsample.h"
#include <math.h>
typedef struct _PocSample PocSample;
struct _PocSample
{
GtkDrawingArea parent_instance;
PocDataset * dataset;
};
G_DEFINE_TYPE (PocSample, poc_sample, GTK_TYPE_DRAWING_AREA)
/**
* SECTION: pocsample
* @title: PocSample
* @short_description: Line sample widget for use with #PocDataset.
* @see_also: #PocDataset
*
* Generate a sample of the line used to plot data in the associated
* #PocDataset. This is useful for use as an "image" in buttons or labels
* elsewhere in the UI.
*/
/**
* poc_sample_new:
* @dataset: a #PocDataset
*
* Create a new #PocSample showing a line sample for @dataset.
*
* Returns: (transfer full): New #PocSample
*/
PocSample *
poc_sample_new (PocDataset *dataset)
{
return g_object_new (POC_TYPE_SAMPLE, "dataset", dataset, NULL);
}
enum
{
PROP_0,
PROP_DATASET,
N_PROPERTIES
};
static GParamSpec *poc_sample_prop[N_PROPERTIES];
static void poc_sample_dispose (GObject *object);
static void poc_sample_finalize (GObject *object);
static void poc_sample_get_property (GObject *object, guint param_id,
GValue *value, GParamSpec *pspec);
static void poc_sample_set_property (GObject *object, guint param_id,
const GValue *value, GParamSpec *pspec);
static gboolean poc_sample_draw (GtkWidget *widget, cairo_t *cr);
/* GObject {{{1 */
static void
poc_sample_class_init (PocSampleClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
gobject_class->dispose = poc_sample_dispose;
gobject_class->finalize = poc_sample_finalize;
gobject_class->set_property = poc_sample_set_property;
gobject_class->get_property = poc_sample_get_property;
gtk_widget_class_set_css_name (widget_class, "sample");
widget_class->draw = poc_sample_draw;
poc_sample_prop[PROP_DATASET] = g_param_spec_object (
"dataset", "Dataset",
"Generate a line sample for the referenced dataset.",
POC_TYPE_DATASET,
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, N_PROPERTIES, poc_sample_prop);
}
static void
poc_sample_init (PocSample *self)
{
self->dataset = NULL;
}
static void
poc_sample_dispose (GObject *object)
{
PocSample *self = (PocSample *) object;
g_clear_object (&self->dataset);
G_OBJECT_CLASS (poc_sample_parent_class)->dispose (object);
}
static void
poc_sample_finalize (GObject *object)
{
//PocSample *self = (PocSample *) object;
G_OBJECT_CLASS (poc_sample_parent_class)->finalize (object);
}
/* Properties {{{1 */
static void
poc_sample_set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
PocSample *self = POC_SAMPLE (object);
switch (prop_id)
{
case PROP_DATASET:
poc_sample_set_dataset (self, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
return;
}
}
static void
poc_sample_get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
PocSample *self = POC_SAMPLE (object);
switch (prop_id)
{
case PROP_DATASET:
g_value_set_object (value, poc_sample_get_dataset (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* "dataset" {{{2 */
/**
* poc_sample_set_dataset:
* @self: A #PocSample
* @dataset: A #PocDataset
*
* Display a line sample for the associated dataset widget.
*/
void
poc_sample_set_dataset (PocSample *self, PocDataset *dataset)
{
g_return_if_fail (POC_IS_SAMPLE (self));
if (g_set_object (&self->dataset, dataset))
g_object_notify_by_pspec (G_OBJECT (self), poc_sample_prop[PROP_DATASET]);
}
/**
* poc_sample_get_dataset:
* @self: A #PocSample
*
* Return the associated dataset.
*
* Returns: (transfer none): a #PocDataset
*/
PocDataset *
poc_sample_get_dataset (PocSample *self)
{
g_return_val_if_fail (POC_IS_SAMPLE (self), NULL);
return self->dataset;
}
/* done */
static gboolean
poc_sample_draw (GtkWidget *widget, cairo_t *cr)
{
PocSample *self = (PocSample *) widget;
GdkRGBA rgba;
const double *dashes;
int num_dashes;
PocLineStyle line_style;
GtkAllocation allocation;
GtkStyleContext *style;
if (self->dataset == NULL)
return FALSE;
style = gtk_widget_get_style_context (GTK_WIDGET (self));
/* fetch sample parameters from dataset */
poc_dataset_get_line_stroke (self->dataset, &rgba);
line_style = poc_dataset_get_line_style (self->dataset);
gtk_widget_get_allocation (widget, &allocation);
gtk_render_background (style, cr, 0, 0, allocation.width, allocation.height);
cairo_move_to (cr, allocation.width / 10.0, round (allocation.height / 2.0) + 0.5);
cairo_rel_line_to (cr, allocation.width - allocation.width / 5.0, 0.0);
/* stroke the sample line */
cairo_set_line_width (cr, 1.0);
dashes = poc_line_style_get_dashes (line_style, &num_dashes);
cairo_set_dash (cr, dashes, num_dashes, 0.0);
gdk_cairo_set_source_rgba (cr, &rgba);
cairo_stroke (cr);
return FALSE;
}