-
Notifications
You must be signed in to change notification settings - Fork 0
/
pocplot.c
1364 lines (1184 loc) · 35 KB
/
pocplot.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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
*/
#include "pocplot.h"
#include "pocdataset.h"
#include "pocaxis.h"
#include "pocbag.h"
#include "poctypes.h"
#include <stdlib.h>
/**
* SECTION: pocplot
* @title: PocPlot
* @short_description: #PocPlot canvas widget
* @see_also: #PocAxis #PocDataset #PocLegend #PocSample
*
* #PocPlot is a 2D graph plotting canvas. To create a plot, add #PocAxis and
* #PocDataset gadgets to this widget, see poc_plot_add_dataset() and
* poc_plot_add_axis().
*
* # PocPlot as GtkBuildable
*
* The PocPlot implementation supports a custom `<plot>` element which supports
* any number of `<dataset>` and `<axis>` elements.
*
* An example of a UI definition fragment for a PocPlot.
* |[
* <object class="PocPlot">
* <plot>
* <dataset source="blue"/>
* <axis source="red" orientation="vertical" hidden="false"/>
* </plot>
* </object>
* <object class="PocDataset" id="blue">
* ...
* </object>
* <object class="PocAxis" id="red">
* ...
* </object>
* ]|
*
* The `<dataset>` element supports the following attributes:
*
* - source: Xml id of a PocDataset element.
* - x-pack: an optional #GtkPackType, how to pack the dataset's X axis.
* - y-pack: an optional #GtkPackType, how to pack the dataset's Y axis.
*
* The `<axis>` elements support the following attribute:
*
* - source: Xml id of a PocAxis element.
* - pack: an optional #GtkPackType, how to pack the axis.
* - orientation: a #GtkOrientation, set the Axis orientation
* - hidden: an optional #gboolean, whether the axis should be displayed or hidden.
*/
struct _PocPlot
{
GtkDrawingArea parent_instance;
GdkRGBA plot_fill;
gfloat border;
gchar *title;
PocObjectBag *axes;
PocObjectBag *datasets;
PocAxis *x_axis;
PocAxis *y_axis;
GdkRectangle area;
gint width, height;
gint solo;
guint enable_plot_fill : 1;
guint relayout : 1;
};
typedef struct _PocPlotAxis PocPlotAxis;
struct _PocPlotAxis
{
GtkPackType pack;
GtkOrientation orientation;
gboolean hidden;
GdkRectangle area;
};
typedef struct _PocPlotDataset PocPlotDataset;
struct _PocPlotDataset
{
gboolean solo;
};
static void poc_plot_buildable_init (GtkBuildableIface *iface);
G_DEFINE_TYPE_WITH_CODE (PocPlot, poc_plot, GTK_TYPE_DRAWING_AREA,
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, poc_plot_buildable_init))
/**
* poc_plot_new:
*
* Create a new #PocPlot
*
* Returns: (transfer full): New #PocPlot
*/
PocPlot *
poc_plot_new (void)
{
return g_object_new (POC_TYPE_PLOT, NULL);
}
/* GObject {{{1 */
enum
{
PROP_0,
PROP_ENABLE_PLOT_FILL,
PROP_PLOT_FILL,
PROP_BORDER,
PROP_TITLE,
PROP_X_AXIS,
PROP_Y_AXIS,
N_PROPERTIES
};
static GParamSpec *poc_plot_prop[N_PROPERTIES];
static void poc_plot_dispose (GObject *object);
static void poc_plot_finalize (GObject *object);
static void poc_plot_get_property (GObject *object, guint param_id,
GValue *value, GParamSpec *pspec);
static void poc_plot_set_property (GObject *object, guint param_id,
const GValue *value, GParamSpec *pspec);
static gboolean poc_plot_draw (GtkWidget *widget, cairo_t *cr);
static void
poc_plot_class_init (PocPlotClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
gobject_class->dispose = poc_plot_dispose;
gobject_class->finalize = poc_plot_finalize;
gobject_class->set_property = poc_plot_set_property;
gobject_class->get_property = poc_plot_get_property;
gtk_widget_class_set_css_name (widget_class, "plot");
widget_class->draw = poc_plot_draw;
/*FIXME - the following should be specified by CSS */
poc_plot_prop[PROP_ENABLE_PLOT_FILL] = g_param_spec_boolean (
"enable-plot-fill",
"Enable plot Fill", "Enable plot fill colour",
FALSE,
G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
poc_plot_prop[PROP_PLOT_FILL] = g_param_spec_boxed (
"plot-fill",
"Plot Fill Colour", "Plot fill colour for graph area.",
GDK_TYPE_RGBA,
G_PARAM_EXPLICIT_NOTIFY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
poc_plot_prop[PROP_BORDER] = g_param_spec_float (
"border",
"Internal Border", "Internal border width between plot items",
0.0f, 100.0f, 6.0f,
G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
poc_plot_prop[PROP_TITLE] = g_param_spec_string (
"title",
"Title", "Plot title.",
NULL,
G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
poc_plot_prop[PROP_X_AXIS] = g_param_spec_object (
"x-axis", "X Axis",
"Current X axis - draw X coordinate grid for this axis",
POC_TYPE_AXIS,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
poc_plot_prop[PROP_Y_AXIS] = g_param_spec_object (
"y-axis", "Y Axis",
"Current X axis - draw Y coordinate grid for this axis",
POC_TYPE_AXIS,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, N_PROPERTIES, poc_plot_prop);
}
static void
poc_plot_init (PocPlot *self)
{
GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
self->plot_fill = white;
self->axes = poc_object_bag_new ();
self->datasets = poc_object_bag_new ();
}
static void
poc_plot_dispose (GObject *object)
{
PocPlot *self = (PocPlot *) object;
g_clear_object (&self->x_axis);
g_clear_object (&self->y_axis);
if (self->datasets != NULL)
poc_object_bag_unref (self->datasets);
self->datasets = NULL;
if (self->axes != NULL)
poc_object_bag_unref (self->axes);
self->axes = NULL;
G_OBJECT_CLASS (poc_plot_parent_class)->dispose (object);
}
static void
poc_plot_finalize (GObject *object)
{
PocPlot *self = (PocPlot *) object;
g_free (self->title);
G_OBJECT_CLASS (poc_plot_parent_class)->finalize (object);
}
static void
poc_plot_set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
PocPlot *self = POC_PLOT (object);
switch (prop_id)
{
case PROP_ENABLE_PLOT_FILL:
poc_plot_set_enable_plot_fill (self, g_value_get_boolean (value));
break;
case PROP_PLOT_FILL:
poc_plot_set_plot_fill (self, g_value_get_boxed (value));
break;
case PROP_BORDER:
poc_plot_set_border (self, g_value_get_float (value));
break;
case PROP_TITLE:
poc_plot_set_title (self, g_value_get_string (value));
break;
case PROP_X_AXIS:
poc_plot_set_x_axis (self, g_value_get_object (value));
break;
case PROP_Y_AXIS:
poc_plot_set_y_axis (self, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
return;
}
}
static void
poc_plot_get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
PocPlot *self = POC_PLOT (object);
GdkRGBA rgba;
switch (prop_id)
{
case PROP_ENABLE_PLOT_FILL:
g_value_set_boolean (value, poc_plot_get_enable_plot_fill (self));
break;
case PROP_PLOT_FILL:
poc_plot_get_plot_fill (self, &rgba);
g_value_set_boxed (value, &rgba);
break;
case PROP_BORDER:
g_value_set_float (value, poc_plot_get_border (self));
break;
case PROP_TITLE:
g_value_set_string (value, poc_plot_get_title (self));
break;
case PROP_X_AXIS:
g_value_set_object (value, poc_plot_get_x_axis (self));
break;
case PROP_Y_AXIS:
g_value_set_object (value, poc_plot_get_y_axis (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* Properties {{{1 */
/* enable plot fill {{{2 */
/**
* poc_plot_set_enable_plot_fill:
* @self: A #PocPlot
* @value: %TRUE to enable plot fill.
*
* Set whether to fill the plot area plot.
*/
void
poc_plot_set_enable_plot_fill (PocPlot *self, gboolean value)
{
self->enable_plot_fill = !!value;
poc_plot_notify_update (self);
g_object_notify_by_pspec (G_OBJECT (self), poc_plot_prop[PROP_ENABLE_PLOT_FILL]);
}
/**
* poc_plot_get_enable_plot_fill:
* @self: A #PocPlot
*
* Get whether to fill the plot area plot.
*
* Returns: %TRUE if plot fill is enabled.
*/
gboolean
poc_plot_get_enable_plot_fill (PocPlot *self)
{
return self->enable_plot_fill;
}
/* plot fill {{{2 */
/**
* poc_plot_get_plot_fill:
* @self: A #PocPlot
* @rgba: A #GdkRGBA with the plot background colour.
*
* Set the plot area background colour.
*/
void
poc_plot_get_plot_fill (PocPlot *self, GdkRGBA *rgba)
{
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (rgba != NULL);
*rgba = self->plot_fill;
}
/**
* poc_plot_set_plot_fill:
* @self: A #PocPlot
* @rgba: A #GdkRGBA to receive the plot background colour.
*
* Get the plot area plot colour.
*/
void
poc_plot_set_plot_fill (PocPlot *self, const GdkRGBA *rgba)
{
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (rgba != NULL);
self->plot_fill = *rgba;
poc_plot_notify_update (self);
g_object_notify_by_pspec (G_OBJECT (self), poc_plot_prop[PROP_PLOT_FILL]);
}
/* border {{{1 */
/**
* poc_plot_set_border:
* @self: A #PocPlot
* @size: border width
*
* Set the internal border width.
*/
void
poc_plot_set_border (PocPlot *self, gfloat size)
{
g_return_if_fail (POC_IS_PLOT (self));
self->border = size;
g_object_notify_by_pspec (G_OBJECT (self), poc_plot_prop[PROP_BORDER]);
}
/**
* poc_plot_get_border:
* @self: A #PocPlot
*
* Get the internal border width.
*
* Returns: border width
*/
gfloat
poc_plot_get_border (PocPlot *self)
{
g_return_val_if_fail (POC_IS_PLOT (self), 0.0);
return self->border;
}
/* title {{{2 */
/**
* poc_plot_set_title:
* @self: A #PocPlot
* @title: Title for the plot.
*
* Set the plot title. The title is not used directly by the #PocPlot widget,
* however it may be used by other gadgets added to the plot.
*/
void
poc_plot_set_title (PocPlot *self, const gchar *title)
{
g_return_if_fail (POC_IS_PLOT (self));
g_free (self->title);
self->title = g_strdup (title);
poc_plot_notify_update (self);
g_object_notify_by_pspec (G_OBJECT (self), poc_plot_prop[PROP_TITLE]);
}
/**
* poc_plot_get_title:
* @self: A #PocPlot
*
* Set the plot title.
*
* Returns: (transfer none): Plot title.
*/
const gchar *
poc_plot_get_title (PocPlot *self)
{
g_return_val_if_fail (POC_IS_PLOT (self), NULL);
return self->title;
}
/* "x-axis" {{{2 */
/**
* poc_plot_set_x_axis:
* @self: A #PocPlot
* @x_axis: #PocAxis to set the current plot grid
*
* Set the current X axis. This is used to draw the X axis grid lines in the
* plot area.
*/
void
poc_plot_set_x_axis (PocPlot *self, PocAxis *x_axis)
{
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (POC_IS_AXIS (x_axis));
if (poc_object_bag_contains (self->axes, G_OBJECT (x_axis))
&& g_set_object (&self->x_axis, x_axis))
{
poc_plot_notify_update (self);
g_object_notify_by_pspec (G_OBJECT (self), poc_plot_prop[PROP_X_AXIS]);
}
}
/**
* poc_plot_get_x_axis:
* @self: A #PocPlot
*
* Get the current X axis.
*
* Returns: (transfer none): current X-Axis
*/
PocAxis *
poc_plot_get_x_axis (PocPlot *self)
{
g_return_val_if_fail (POC_IS_PLOT (self), NULL);
return self->x_axis;
}
/* "y-axis" {{{2 */
/**
* poc_plot_set_y_axis:
* @self: A #PocPlot
* @y_axis: #PocAxis to set the current plot grid
*
* Set the current Y axis. This is used to draw the Y axis grid lines in the
* plot area.
*/
void
poc_plot_set_y_axis (PocPlot *self, PocAxis *y_axis)
{
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (POC_IS_AXIS (y_axis));
if (poc_object_bag_contains (self->axes, G_OBJECT (y_axis))
&& g_set_object (&self->y_axis, y_axis))
{
poc_plot_notify_update (self);
g_object_notify_by_pspec (G_OBJECT (self), poc_plot_prop[PROP_Y_AXIS]);
}
}
/**
* poc_plot_get_y_axis:
* @self: A #PocPlot
*
* Get the current Y axis.
*
* Returns: (transfer none): current Y-Axis
*/
PocAxis *
poc_plot_get_y_axis (PocPlot *self)
{
g_return_val_if_fail (POC_IS_PLOT (self), NULL);
return self->y_axis;
}
/**
* poc_plot_set_axis:
* @self: A #PocPlot
* @axis: #PocAxis to set the current plot grid
*
* Set the current axis. The #PocPlot determines whether @axis refers to an
* X or Y axis.
*/
void
poc_plot_set_axis (PocPlot *self, PocAxis *axis)
{
PocPlotAxis *axis_data;
g_return_if_fail (POC_IS_PLOT (self));
axis_data = poc_object_bag_get_data (self->axes, G_OBJECT (axis));
g_return_if_fail (axis_data != NULL);
if (axis_data->orientation == GTK_ORIENTATION_HORIZONTAL)
poc_plot_set_x_axis (self, axis);
else
poc_plot_set_y_axis (self, axis);
}
/* draw {{{1 */
struct poc_plot_closure
{
PocPlot *self;
cairo_t *cr;
GtkStyleContext *style;
GdkRectangle area;
guint x_offset_start;
guint x_offset_end;
guint y_offset_start;
guint y_offset_end;
};
static void poc_plot_layout (PocPlot *self, struct poc_plot_closure *closure);
static void
poc_plot_draw_dataset (gpointer object, gpointer object_data,
gpointer user_data)
{
PocDataset *dataset = POC_DATASET (object);
PocPlotDataset *dataset_data = object_data;
struct poc_plot_closure *closure = user_data;
PocPlot *self = closure->self;
if (self->solo == 0 || dataset_data->solo)
poc_dataset_draw (dataset, closure->cr, self->area.width, self->area.height);
}
static void
poc_plot_draw_axis (gpointer object, gpointer object_data, gpointer user_data)
{
PocAxis *axis = object;
PocPlotAxis *axis_data = object_data;
struct poc_plot_closure *closure = user_data;
if (axis_data->hidden)
return;
cairo_save (closure->cr);
gdk_cairo_rectangle (closure->cr, &axis_data->area);
cairo_clip (closure->cr);
cairo_translate (closure->cr, axis_data->area.x, axis_data->area.y);
poc_axis_draw_axis (axis, closure->cr,
axis_data->orientation, axis_data->pack,
axis_data->area.width, axis_data->area.height,
closure->style);
cairo_restore (closure->cr);
}
static gboolean
poc_plot_draw (GtkWidget *widget, cairo_t *cr)
{
PocPlot *self = (PocPlot *) widget;
struct poc_plot_closure closure = { NULL, NULL, NULL, { 0, 0, 0, 0 }, 0, 0, 0, 0 };
GtkStyleContext *style;
GtkBorder border;
GtkStateFlags state;
style = gtk_widget_get_style_context (widget);
state = gtk_style_context_get_state (style);
closure.self = self;
closure.cr = cr;
closure.area.width = gtk_widget_get_allocated_width (widget);
closure.area.height = gtk_widget_get_allocated_height (widget);
closure.style = style;
gtk_render_background (style, cr, closure.area.x, closure.area.y,
closure.area.width, closure.area.height);
gtk_render_frame (style, cr, closure.area.x, closure.area.y,
closure.area.width, closure.area.height);
gtk_style_context_get_border (style, state, &border);
closure.area.x += border.left;
closure.area.y += border.top;
closure.area.width -= border.right + border.left;
closure.area.height -= border.bottom + border.top;
gtk_style_context_get_padding (style, state, &border);
closure.area.x += border.left;
closure.area.y += border.top;
closure.area.width -= border.right + border.left;
closure.area.height -= border.bottom + border.top;
if (self->relayout || closure.area.width != self->width || closure.area.height != self->height)
{
poc_plot_layout (self, &closure);
self->relayout = FALSE;
self->width = closure.area.width;
self->height = closure.area.height;
}
/* Draw axes */
poc_object_bag_foreach (self->axes, poc_plot_draw_axis, &closure);
/**** draw the plot background ****/
cairo_save (cr);
gdk_cairo_rectangle (cr, &self->area);
if (self->enable_plot_fill)
{
gdk_cairo_set_source_rgba (cr, &self->plot_fill);
cairo_fill_preserve (cr);
}
cairo_clip (cr);
cairo_translate (cr, self->area.x, self->area.y);
/* Draw each dataset */
poc_object_bag_foreach (self->datasets, poc_plot_draw_dataset, &closure);
/* Draw the grid */
if (self->x_axis != NULL)
poc_axis_draw_grid (self->x_axis, cr, GTK_ORIENTATION_HORIZONTAL,
self->area.width, self->area.height, style);
if (self->y_axis != NULL)
poc_axis_draw_grid (self->y_axis, cr, GTK_ORIENTATION_VERTICAL,
self->area.width, self->area.height, style);
cairo_restore (cr);
return FALSE;
}
/* layout {{{1 */
static void
poc_plot_layout_axis1 (gpointer object, gpointer object_data, gpointer user_data)
{
PocAxis *axis = object;
PocPlotAxis *axis_data = object_data;
struct poc_plot_closure *closure = user_data;
guint size;
if (axis_data->hidden)
return;
size = poc_axis_size (axis);
if (axis_data->orientation == GTK_ORIENTATION_VERTICAL)
{
if (axis_data->pack == GTK_PACK_START)
{
axis_data->area.x = closure->area.x + closure->x_offset_start;
axis_data->area.width = size;
closure->x_offset_start += size + closure->self->border;
}
else
{
axis_data->area.x = closure->area.x + closure->area.width - 1
- (closure->x_offset_end + size);
axis_data->area.width = size;
closure->x_offset_end += size + closure->self->border;
}
}
else
{
if (axis_data->pack == GTK_PACK_START)
{
axis_data->area.y = closure->area.y + closure->area.height - 1
- (closure->y_offset_start + size);
axis_data->area.height = size;
closure->y_offset_start += size + closure->self->border;
}
else
{
axis_data->area.y = closure->area.y + closure->y_offset_end;
axis_data->area.height = size;
closure->y_offset_end += size + closure->self->border;
}
}
}
static void
poc_plot_layout_axis2 (G_GNUC_UNUSED gpointer object, gpointer object_data,
gpointer user_data)
{
PocPlotAxis *axis_data = object_data;
struct poc_plot_closure *closure = user_data;
if (axis_data->hidden)
return;
if (axis_data->orientation == GTK_ORIENTATION_VERTICAL)
{
axis_data->area.y = closure->self->area.y;
axis_data->area.height = closure->self->area.height;
}
else
{
axis_data->area.x = closure->self->area.x;
axis_data->area.width = closure->self->area.width;
}
}
static void
poc_plot_layout (PocPlot *self, struct poc_plot_closure *closure)
{
closure->x_offset_start = closure->y_offset_start = 0;
closure->x_offset_end = closure->y_offset_end = 0;
/* first pass set x coords for vertical axes, y for horizontal */
poc_object_bag_foreach (self->axes, poc_plot_layout_axis1, closure);
/* set the plot area */
self->area.x = closure->area.x + closure->x_offset_start;
self->area.width = closure->area.width - closure->x_offset_end
- closure->x_offset_start;
self->area.y = closure->area.y + closure->y_offset_end;
self->area.height = closure->area.height - closure->y_offset_start
- closure->y_offset_end;
//FIXME warn if insufficient area
/* second pass set y coords for vertical axes, x for horizontal */
poc_object_bag_foreach (self->axes, poc_plot_layout_axis2, closure);
}
/* methods {{{1 */
/**
* poc_plot_add_dataset:
* @self: A #PocPlot
* @dataset: #PocDataset to add.
* @x_pack: How to pack the X axis.
* @y_pack: How to pack the Y axis (start = bottom, end = top).
*
* Add a #PocDataset gadget to the plot. @x_pack and @y_pack determine which
* edge of the plot should show the axes.
*/
void
poc_plot_add_dataset (PocPlot *self, PocDataset *dataset,
GtkPackType x_pack, GtkPackType y_pack)
{
PocAxis *axis;
PocPlotDataset *data;
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (POC_IS_DATASET (dataset));
g_object_freeze_notify (G_OBJECT (self));
//XXX Dataset should probably be initiallyunowned and use ref_sink
if (!poc_object_bag_add (self->datasets, G_OBJECT (dataset)))
{
data = g_new0 (PocPlotDataset, 1);
poc_object_bag_set_data_full (self->datasets, G_OBJECT (dataset), data, g_free);
g_signal_connect_object (dataset, "update",
G_CALLBACK (poc_plot_notify_update), self,
G_CONNECT_SWAPPED);
}
if ((axis = poc_dataset_get_x_axis (dataset)) != NULL)
{
poc_plot_add_axis (self, axis, FALSE, x_pack, GTK_ORIENTATION_HORIZONTAL);
if (self->x_axis == NULL)
poc_plot_set_x_axis (self, axis);
}
if ((axis = poc_dataset_get_y_axis (dataset)) != NULL)
{
poc_plot_add_axis (self, axis, FALSE, y_pack, GTK_ORIENTATION_VERTICAL);
if (self->y_axis == NULL)
poc_plot_set_y_axis (self, axis);
}
gtk_widget_queue_draw (GTK_WIDGET (self));
g_object_thaw_notify (G_OBJECT (self));
}
static void
poc_plot_remove_dataset_internal (PocPlot *self, PocDataset *dataset)
{
PocAxis *axis;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
g_signal_handlers_disconnect_by_func (dataset,
G_CALLBACK (poc_plot_notify_update),
self);
#pragma GCC diagnostic pop
if ((axis = poc_dataset_get_x_axis (dataset)) != NULL)
poc_plot_remove_axis (self, axis);
if ((axis = poc_dataset_get_y_axis (dataset)) != NULL)
poc_plot_remove_axis (self, axis);
}
/**
* poc_plot_remove_dataset:
* @self: A #PocPlot
* @dataset: #PocDataset to remove
*
* Remove a @dataset from the plot. The @dataset must already belong to the
* plot. Axes that are no longer referenced by another #PocDataset are also
* removed.
*/
void
poc_plot_remove_dataset (PocPlot *self, PocDataset *dataset)
{
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (POC_IS_DATASET (dataset));
g_object_ref (dataset);
if (poc_object_bag_remove (self->datasets, G_OBJECT (dataset)))
{
poc_plot_remove_dataset_internal (self, dataset);
gtk_widget_queue_draw (GTK_WIDGET (self));
}
g_object_unref (dataset);
}
static void
poc_plot_clear_dataset_cb (gpointer object, G_GNUC_UNUSED gpointer object_data,
gpointer user_data)
{
PocPlot *self = user_data;
PocDataset *dataset = object;
poc_plot_remove_dataset_internal (self, dataset);
}
/**
* poc_plot_clear_dataset:
* @self: A #PocPlot
*
* Remove all datasets from the plot.
*/
void
poc_plot_clear_dataset (PocPlot *self)
{
poc_object_bag_foreach (self->datasets, poc_plot_clear_dataset_cb, self);
poc_object_bag_empty (self->datasets);
gtk_widget_queue_draw (GTK_WIDGET (self));
}
static gboolean
poc_plot_dataset_compare (gpointer object, G_GNUC_UNUSED gpointer object_data,
gpointer user_data)
{
PocDataset *dataset = object;
const gchar *nickname = user_data;
const gchar *ds_name;
ds_name = poc_dataset_get_nickname (dataset);
return g_strcmp0 (nickname, ds_name) == 0;
}
/**
* poc_plot_find_dataset:
* @self: A #PocPlot
* @nickname: Nickname for a #PocDataset
*
* Find a dataset belonging to the plot with the requested nickname.
*
* returns: (transfer none): a #PocDataset or %NULL if not found.
*/
PocDataset *
poc_plot_find_dataset (PocPlot *self, const gchar *nickname)
{
GObject *object;
g_return_val_if_fail (POC_IS_PLOT (self), NULL);
object = poc_object_bag_find (self->datasets, poc_plot_dataset_compare,
(gpointer) nickname);
return object != NULL ? POC_DATASET (object) : NULL;
}
/**
* poc_plot_solo_dataset:
* @self: A #PocPlot.
* @dataset: A #PocDataset belonging to the plot.
* @solo: %TRUE to show only this @dataset.
*
* Show only the grid lines and plot data for the specified @dataset. If
* multiple datasets have solo enabled only they are displayed, if no datasets
* are solo then all datasets are displayed.
*/
void
poc_plot_solo_dataset (PocPlot *self, PocDataset *dataset, gboolean solo)
{
PocPlotDataset *data;
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (POC_IS_DATASET (dataset));
data = poc_object_bag_get_data (self->datasets, G_OBJECT (dataset));
g_return_if_fail (data != NULL);
if (solo && !data->solo)
self->solo += 1;
else if (!solo && data->solo)
self->solo -= 1;
else
return;
data->solo = !!solo;
g_assert (self->solo >= 0);
gtk_widget_queue_draw (GTK_WIDGET (self));
}
/**
* poc_plot_add_axis:
* @self: A #PocPlot
* @axis: #PocAxis to add.
* @hidden: Do not draw @axis if %TRUE
* @pack: How to pack the axis.
* @orientation: Orientation for the axis.
*
* Add an axis to the plot. @pack and @orientation specify how the axis should
* be displayed. Normally this is not required as axes belonging to datasets
* are added automatically.
*/
void
poc_plot_add_axis (PocPlot *self, PocAxis *axis, gboolean hidden,
GtkPackType pack, GtkOrientation orientation)
{
PocPlotAxis *data;
g_return_if_fail (POC_IS_PLOT (self));
g_return_if_fail (POC_IS_AXIS (axis));
//XXX Axis should probably be initiallyunowned and use ref_sink
if (!poc_object_bag_add (self->axes, G_OBJECT (axis)))
{
data = g_new0 (PocPlotAxis, 1);
data->hidden = hidden;
data->pack = pack;
data->orientation = orientation;
poc_object_bag_set_data_full (self->axes, G_OBJECT (axis), data, g_free);
g_signal_connect_object (axis, "update",
G_CALLBACK (poc_plot_notify_update), self,
G_CONNECT_SWAPPED);
self->relayout = TRUE;
gtk_widget_queue_draw (GTK_WIDGET (self));
}
}
/**
* poc_plot_remove_axis:
* @self: A #PocPlot
* @axis: #PocAxis to remove.
*
* Remove the @axis from the plot.
*/
void