]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * h/t https://ptomato.name/advanced-gtk-techniques/html/custom-container.html | |
3 | * for examples of how to make GTK widgets | |
4 | */ | |
5 | ||
6 | #include "chart.h" | |
7 | #include <float.h> | |
8 | #include <gtk/gtk.h> | |
9 | ||
10 | #define BV_CHART_PRIVATE(obj) \ | |
11 | (G_TYPE_INSTANCE_GET_PRIVATE((obj), BV_CHART_TYPE, BVChartPrivate)) | |
12 | ||
13 | typedef struct _BVChartPrivate BVChartPrivate; | |
14 | ||
15 | struct _BVChartPrivate { | |
16 | GArray *points; | |
17 | float minx, miny, maxx, maxy; | |
18 | }; | |
19 | ||
20 | struct BVChartPoint { | |
21 | float x, y; | |
22 | }; | |
23 | ||
24 | G_DEFINE_TYPE_WITH_CODE(BVChart, bv_chart, GTK_TYPE_DRAWING_AREA, | |
25 | G_ADD_PRIVATE(BVChart)) | |
26 | ||
27 | static void bv_chart_class_init(BVChartClass *klass __attribute__((unused))) {} | |
28 | ||
29 | static void bv_chart_init(BVChart *chart) { | |
30 | gtk_widget_set_has_window(GTK_WIDGET(chart), FALSE); | |
31 | BVChartPrivate *priv = bv_chart_get_instance_private(chart); | |
32 | gboolean zero_terminated = FALSE; | |
33 | gboolean clear_ = FALSE; | |
34 | priv->points = | |
35 | g_array_new(zero_terminated, clear_, sizeof(struct BVChartPoint)); | |
36 | priv->minx = FLT_MAX; | |
37 | priv->miny = FLT_MAX; | |
38 | priv->maxx = FLT_MIN; | |
39 | priv->maxy = FLT_MIN; | |
40 | } | |
41 | ||
42 | GtkWidget *bv_chart_new() { | |
43 | return GTK_WIDGET(g_object_new(bv_chart_get_type(), NULL)); | |
44 | } | |
45 | ||
46 | void bv_chart_add_point(BVChart *chart, float x, float y) { | |
47 | BVChartPrivate *priv = bv_chart_get_instance_private(chart); | |
48 | if (x < priv->minx) | |
49 | priv->minx = x; | |
50 | if (y < priv->miny) | |
51 | priv->miny = y; | |
52 | if (x > priv->maxx) | |
53 | priv->maxx = x; | |
54 | if (y > priv->maxy) | |
55 | priv->maxy = y; | |
56 | struct BVChartPoint p = {.x = x, .y = y}; | |
57 | g_array_append_val(priv->points, p); | |
58 | } |