]> git.scottworley.com Git - batteryviewer/blame - chart.c
Extract to_screen()
[batteryviewer] / chart.c
CommitLineData
5a945372
SW
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"
148e0e28 7#include <float.h>
5a945372
SW
8#include <gtk/gtk.h>
9
10#define BV_CHART_PRIVATE(obj) \
11 (G_TYPE_INSTANCE_GET_PRIVATE((obj), BV_CHART_TYPE, BVChartPrivate))
12
13typedef struct _BVChartPrivate BVChartPrivate;
14
15struct _BVChartPrivate {
27db65e2 16 GArray *points;
148e0e28 17 float minx, miny, maxx, maxy;
27db65e2
SW
18};
19
20struct BVChartPoint {
21 float x, y;
5a945372
SW
22};
23
487d9d1f
SW
24struct ScreenPoint {
25 float x, y;
26};
27
5a945372
SW
28G_DEFINE_TYPE_WITH_CODE(BVChart, bv_chart, GTK_TYPE_DRAWING_AREA,
29 G_ADD_PRIVATE(BVChart))
30
328956d5
SW
31typedef void (*cairo_draw_func)(cairo_t *cr, double x, double y);
32
487d9d1f
SW
33static struct ScreenPoint to_screen(BVChartPrivate *priv,
34 GtkAllocation *allocation,
35 struct BVChartPoint *p) {
36 int margin = 3;
37 float xscale = (allocation->width - 2 * margin) / (priv->maxx - priv->minx);
38 float yscale = (allocation->height - 2 * margin) / (priv->miny - priv->maxy);
39 struct ScreenPoint screen_p = {
40 .x = xscale * (p->x - priv->minx) + margin,
41 .y = yscale * (p->y - priv->maxy) + margin,
42 };
43 return screen_p;
44}
45
366d0933
SW
46static gboolean bv_chart_draw(GtkWidget *widget, cairo_t *cr) {
47 BVChart *chart = BV_CHART(widget);
48 BVChartPrivate *priv = bv_chart_get_instance_private(chart);
49
50 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
51 cairo_paint(cr);
52
53 if (priv->points->len < 2)
54 return TRUE;
55
56 GtkAllocation allocation;
57 gtk_widget_get_allocation(widget, &allocation);
58
366d0933 59 cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
328956d5
SW
60 float gap_threshold = 3.0;
61 float prevx = 0.0;
62 for (guint i = 0; i < priv->points->len; i++) {
366d0933
SW
63 struct BVChartPoint *p =
64 &g_array_index(priv->points, struct BVChartPoint, i);
328956d5
SW
65 gboolean is_gap = p->x - prevx > gap_threshold;
66 cairo_draw_func f = is_gap ? cairo_move_to : cairo_line_to;
487d9d1f
SW
67 struct ScreenPoint screen_p = to_screen(priv, &allocation, p);
68 f(cr, screen_p.x, screen_p.y);
328956d5 69 prevx = p->x;
366d0933
SW
70 }
71 cairo_stroke(cr);
72
73 return TRUE;
74}
75
76static void bv_chart_class_init(BVChartClass *klass) {
77 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
78 widget_class->draw = bv_chart_draw;
79}
5a945372
SW
80
81static void bv_chart_init(BVChart *chart) {
82 gtk_widget_set_has_window(GTK_WIDGET(chart), FALSE);
27db65e2
SW
83 BVChartPrivate *priv = bv_chart_get_instance_private(chart);
84 gboolean zero_terminated = FALSE;
85 gboolean clear_ = FALSE;
86 priv->points =
87 g_array_new(zero_terminated, clear_, sizeof(struct BVChartPoint));
148e0e28
SW
88 priv->minx = FLT_MAX;
89 priv->miny = FLT_MAX;
90 priv->maxx = FLT_MIN;
91 priv->maxy = FLT_MIN;
5a945372
SW
92}
93
94GtkWidget *bv_chart_new() {
95 return GTK_WIDGET(g_object_new(bv_chart_get_type(), NULL));
96}
27db65e2
SW
97
98void bv_chart_add_point(BVChart *chart, float x, float y) {
99 BVChartPrivate *priv = bv_chart_get_instance_private(chart);
148e0e28
SW
100 if (x < priv->minx)
101 priv->minx = x;
102 if (y < priv->miny)
103 priv->miny = y;
104 if (x > priv->maxx)
105 priv->maxx = x;
106 if (y > priv->maxy)
107 priv->maxy = y;
27db65e2
SW
108 struct BVChartPoint p = {.x = x, .y = y};
109 g_array_append_val(priv->points, p);
110}