-
Notifications
You must be signed in to change notification settings - Fork 3
/
gtk3curve.c
1993 lines (1692 loc) · 58.8 KB
/
gtk3curve.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
/* Copyright (C) 2016 Benoit Touchette
*
* This library 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 version
* 2.1 of the License.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
*/
/* Portions of this code Copyright (C) 1997 David Mosberger and
* Copyright (C) 1997 - 2000 GTK+ Team.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gtk/gtk.h>
#include "gtk3curve.h"
#ifdef DEBUG
#define DEBUG_INFO g_print
#define DEBUG_ERROR g_printerr
#else
#define DEBUG_INFO(...)
#define DEBUG_ERROR(...)
#endif
static guint curve_type_changed_signal = 0;
static gint Gtk3Curve_private_offset = 0;
static GtkDrawingAreaClass *gtk3_curve_parent_class = NULL;
#define _gtk3_marshal_VOID__VOID g_cclosure_marshal_VOID__VOID
#define GTK3_PARAM_READABLE G_PARAM_READABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
#define GTK3_PARAM_WRITABLE G_PARAM_WRITABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
#define GTK3_PARAM_READWRITE G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
#define RADIUS 3 /* radius of the control points */
#define MIN_DISTANCE 8 /* min distance between control points */
#define GRAPH_MASK (GDK_EXPOSURE_MASK | \
GDK_POINTER_MOTION_MASK | \
GDK_POINTER_MOTION_HINT_MASK | \
GDK_ENTER_NOTIFY_MASK | \
GDK_LEAVE_NOTIFY_MASK | \
GDK_BUTTON_PRESS_MASK | \
GDK_BUTTON_RELEASE_MASK | \
GDK_BUTTON1_MOTION_MASK)
struct _Gtk3CurvePrivate
{
GdkWindow *event_window;
GList *children;
gint cursor_type;
gfloat min_x;
gfloat max_x;
gfloat min_y;
gfloat max_y;
gboolean use_bg_theme;
Gtk3CurveColor background;
Gtk3CurveColor cpoint;
Gtk3CurveColor grid;
Gtk3CurveColor curve;
Gtk3CurveType curve_type;
gint width;
gint height;
gint grab_point;
gint last;
Gtk3CurveGridSize grid_size;
Gtk3CurveData curve_data;
guint state : 1;
guint in_curve : 1;
};
enum
{
PROP_0,
PROP_CURVE_TYPE,
PROP_MIN_X,
PROP_MAX_X,
PROP_MIN_Y,
PROP_MAX_Y
};
static void gtk3_curve_realize (GtkWidget *widget);
static void gtk3_curve_unrealize (GtkWidget *widget);
static void gtk3_curve_style_updated (GtkWidget *widget);
static void gtk3_curve_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static void gtk3_curve_configure (Gtk3Curve *curve);
static gboolean gtk3_curve_draw (GtkWidget *widget,
cairo_t *cr);
static void gtk3_curve_map (GtkWidget *widget);
static void gtk3_curve_unmap (GtkWidget *widget);
static gboolean gtk3_curve_enter (GtkWidget *widget,
GdkEventCrossing *event);
static gboolean gtk3_curve_leave (GtkWidget *widget,
GdkEventCrossing *event);
static gboolean gtk3_curve_button_press (GtkWidget *widget,
GdkEventButton *event);
static gboolean gtk3_curve_button_release (GtkWidget *widget,
GdkEventButton *event);
static gboolean gtk3_curve_motion_notify (GtkWidget *widget,
GdkEventMotion *event);
static void gtk3_curve_screen_changed (GtkWidget *widget,
GdkScreen *prev_screen);
static void gtk3_curve_style_updated (GtkWidget *widget);
static void gtk3_curve_finalize (GObject *object);
static void gtk3_curve_dispose (GObject *object);
static void gtk3_curve_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void gtk3_curve_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void gtk3_curve_size_graph (Gtk3Curve *curve);
static void gtk3_curve_create_layouts (GtkWidget *widget);
static void gtk3_curve_reset_vector (GtkWidget *widget);
static void gtk3_curve_interpolate (GtkWidget *widget,
gint width,
gint height);
static int project (gfloat value,
gfloat min,
gfloat max,
int norm);
static gfloat unproject (gint value,
gfloat min,
gfloat max,
int norm);
static void spline_solve (int n,
gfloat x[],
gfloat y[],
gfloat y2[]);
static gfloat spline_eval (int n,
gfloat x[],
gfloat y[],
gfloat y2[],
gfloat val);
static void gtk3_curve_draw_line (cairo_t *cr,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2);
static void gtk3_curve_class_init (Gtk3CurveClass *klass);
static void gtk3_curve_init (Gtk3Curve *self);
static void gtk3_curve_get_cursor_coord (GtkWidget *widget,
gint *tx,
gint *ty);
static inline gpointer
gtk3_curve_get_instance_private (Gtk3Curve *self)
{
return (G_STRUCT_MEMBER_P (self, Gtk3Curve_private_offset));
}
static void gtk_gadget_class_intern_init (gpointer klass)
{
g_type_class_adjust_private_offset (klass, &Gtk3Curve_private_offset);
gtk3_curve_parent_class = g_type_class_peek_parent (klass);
gtk3_curve_class_init ((Gtk3CurveClass*) klass);
}
GType
gtk3_curve_get_type (void)
{
static GType curve_type = 0;
if (G_UNLIKELY (curve_type == 0))
{
const GTypeInfo curve_info =
{
sizeof (Gtk3CurveClass),
NULL, // base_init
NULL, // base_finalize
(GClassInitFunc) gtk3_curve_class_init,
NULL, // class_finalize
NULL, // class_data
sizeof (Gtk3Curve),
0, // n_preallocs
(GInstanceInitFunc) gtk3_curve_init,
};
curve_type = g_type_register_static (GTK_TYPE_WIDGET, "Gtk3Curve",
&curve_info, 0);
Gtk3Curve_private_offset =
g_type_add_instance_private (curve_type, sizeof (Gtk3CurvePrivate));
}
return curve_type;
}
GType
gtk3_curve_type_get_type (void)
{
static GType etype = 0;
if (G_UNLIKELY(etype == 0))
{
static const GEnumValue values[] =
{
{ GTK3_CURVE_TYPE_LINEAR, "GTK3_CURVE_TYPE_LINEAR", "linear" },
{ GTK3_CURVE_TYPE_SPLINE, "GTK3_CURVE_TYPE_SPLINE", "spline" },
{ GTK3_CURVE_TYPE_FREE, "GTK3_CURVE_TYPE_FREE", "free" },
{ 0, NULL, NULL }
};
etype = g_enum_register_static (g_intern_static_string ("Gtk3CurveType"),
values);
}
return etype;
}
static void
gtk3_curve_class_init (Gtk3CurveClass* klass)
{
GtkContainerClass *container_class;
GObjectClass *gobject_class;
GtkWidgetClass *widget_class;
DEBUG_INFO("class_init [S]\n");
widget_class = GTK_WIDGET_CLASS (klass);
widget_class->realize = gtk3_curve_realize;
widget_class->unrealize = gtk3_curve_unrealize;
widget_class->draw = gtk3_curve_draw;
widget_class->motion_notify_event = gtk3_curve_motion_notify;
widget_class->map = gtk3_curve_map;
widget_class->unmap = gtk3_curve_unmap;
widget_class->enter_notify_event = gtk3_curve_enter;
widget_class->leave_notify_event = gtk3_curve_leave;
widget_class->button_press_event = gtk3_curve_button_press;
widget_class->button_release_event = gtk3_curve_button_release;
widget_class->size_allocate = gtk3_curve_size_allocate;
widget_class->screen_changed = gtk3_curve_screen_changed;
widget_class->style_updated = gtk3_curve_style_updated;
gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_DRAWING_AREA);
gobject_class = G_OBJECT_CLASS (klass);
g_type_class_adjust_private_offset (klass, &Gtk3Curve_private_offset);
gtk3_curve_parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = gtk3_curve_finalize;
gobject_class->dispose = gtk3_curve_dispose;
gobject_class->set_property = gtk3_curve_set_property;
gobject_class->get_property = gtk3_curve_get_property;
g_object_class_install_property (gobject_class,
PROP_CURVE_TYPE,
g_param_spec_enum ("curve-type",
"Curve type",
"Is this curve linear, spline interpolated, or free-form",
GTK3_TYPE_CURVE_TYPE,
GTK3_CURVE_TYPE_SPLINE,
GTK3_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_MIN_X,
g_param_spec_float ("min-x",
"Minimum X",
"Minimum possible value for X",
-G_MAXFLOAT,
G_MAXFLOAT,
0.0,
GTK3_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_MAX_X,
g_param_spec_float ("max-x",
"Maximum X",
"Maximum possible X value",
-G_MAXFLOAT,
G_MAXFLOAT,
1.0,
GTK3_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_MIN_Y,
g_param_spec_float ("min-y",
"Minimum Y",
"Minimum possible value for Y",
-G_MAXFLOAT,
G_MAXFLOAT,
0.0,
GTK3_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_MAX_Y,
g_param_spec_float ("max-y",
"Maximum Y",
"Maximum possible value for Y",
-G_MAXFLOAT,
G_MAXFLOAT,
1.0,
GTK3_PARAM_READWRITE));
curve_type_changed_signal =
g_signal_new ("curve-type-changed",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (Gtk3CurveClass, curve_type_changed),
NULL, NULL,
_gtk3_marshal_VOID__VOID,
G_TYPE_NONE, 0);
DEBUG_INFO("class_init [E]\n");
}
static void
gtk3_curve_init (Gtk3Curve* self)
{
Gtk3CurvePrivate *priv;
DEBUG_INFO("init [S]\n");
gtk_widget_set_has_window(GTK_WIDGET(self), TRUE);
gtk_widget_set_redraw_on_allocate(GTK_WIDGET(self), TRUE);
self->priv = gtk3_curve_get_instance_private (self);
priv = self->priv;
/* Curve Data Points */
priv->curve_data.description = NULL;
priv->curve_data.n_points = 0;
priv->curve_data.d_point = NULL;
priv->curve_data.n_cpoints = 0;
priv->curve_data.d_cpoints = NULL;
priv->curve_data.curve_type = GTK3_CURVE_TYPE_SPLINE;
/* Misc */
priv->use_bg_theme = TRUE;
priv->cursor_type = GDK_TOP_LEFT_ARROW;
priv->grid_size = GTK3_CURVE_GRID_LARGE;
priv->height = 0;
priv->grab_point = -1;
/* Min & Max range */
priv->min_x = 0.0;
priv->max_x = 1.0;
priv->min_y = 0.0;
priv->max_y = 1.0;
/* Default Colors */
gtk3_curve_set_color_background_rgba (GTK_WIDGET(self), 1.0, 1.0, 1.0, 1.0);
gtk3_curve_set_color_curve_rgba (GTK_WIDGET(self), 0.0, 0.0, 0.0, 1.0);
gtk3_curve_set_color_grid_rgba (GTK_WIDGET(self), 0.0, 0.0, 0.0, 1.0);
gtk3_curve_set_color_cpoint_rgba (GTK_WIDGET(self), 0.2, 0.2, 0.2, 1.0);
gtk3_curve_size_graph (self);
DEBUG_INFO("init [E]\n");
}
GtkWidget *
gtk3_curve_new()
{
return g_object_new (GTK3_TYPE_CURVE, NULL);
}
static void
gtk3_curve_style_updated (GtkWidget *widget)
{
GTK_WIDGET_CLASS (gtk3_curve_parent_class)->style_updated (widget);
DEBUG_INFO("style_updated [S]\n");
if (gtk_widget_get_realized (widget) &&
gtk_widget_get_has_window (widget))
{
gtk_widget_queue_draw (widget);
}
DEBUG_INFO("style_updated [E]\n");
}
static void
gtk3_curve_realize (GtkWidget *widget)
{
GtkAllocation allocation;
GdkWindow *window;
GdkWindow *parent_window;
GdkWindowAttr attributes;
gint attributes_mask;
Gtk3CurvePrivate *priv;
priv = GTK3_CURVE (widget)->priv;
DEBUG_INFO("realize [S]\n");
if (!gtk_widget_get_has_window (widget))
{
GTK_WIDGET_CLASS (gtk3_curve_parent_class)->realize (widget);
}
else
{
gtk_widget_set_realized (widget, TRUE);
parent_window = gtk_widget_get_parent_window (widget);
gtk_widget_set_window (widget, parent_window);
g_object_ref (parent_window);
gtk_widget_get_allocation (widget, &allocation);
DEBUG_INFO("allocation [%d,%d] [%dx%d]\n",
allocation.x,
allocation.y,
allocation.width,
allocation.height);
attributes.window_type = GDK_WINDOW_CHILD;
attributes.x = allocation.x;
attributes.y = allocation.y;
attributes.width = allocation.width;
attributes.height = allocation.height;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.visual = gtk_widget_get_visual (widget);
attributes.event_mask = gtk_widget_get_events (widget) |
GRAPH_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
priv->event_window = gdk_window_new (parent_window,
&attributes,
attributes_mask);
gtk_widget_register_window (widget, priv->event_window);
gtk_widget_set_window (widget, priv->event_window);
}
gtk3_curve_configure (GTK3_CURVE (widget));
DEBUG_INFO("realize [E]\n");
}
static void
gtk3_curve_unrealize (GtkWidget *widget)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
DEBUG_INFO("unrealize [S]\n");
if (priv->event_window != NULL)
{
DEBUG_INFO("unregister/destroy\n");
gtk_widget_unregister_window (widget, priv->event_window);
gdk_window_destroy (priv->event_window);
priv->event_window = NULL;
}
DEBUG_INFO("unrealize [E]\n");
}
static void
gtk3_curve_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
g_return_if_fail (GTK3_IS_CURVE (widget));
g_return_if_fail (allocation != NULL);
DEBUG_INFO("size_allocate [S]\n");
gtk_widget_set_allocation (widget, allocation);
DEBUG_INFO("allocation [%d,%d] [%dx%d]\n",
allocation->x,
allocation->y,
allocation->width,
allocation->height);
if (gtk_widget_get_realized (widget))
{
if (gtk_widget_get_has_window (widget))
{
gdk_window_move_resize (gtk_widget_get_window (widget),
allocation->x, allocation->y,
allocation->width, allocation->height);
}
gtk3_curve_configure (GTK3_CURVE (widget));
}
DEBUG_INFO("size_allocate [E]\n");
}
static void
gtk3_curve_configure (Gtk3Curve *curve)
{
GtkAllocation allocation;
GtkWidget *widget;
GdkEvent *event = gdk_event_new (GDK_CONFIGURE);
DEBUG_INFO("configure [S]\n");
widget = GTK_WIDGET (curve);
gtk_widget_get_allocation (widget, &allocation);
DEBUG_INFO("allocation [%d,%d] [%dx%d]\n",
allocation.x,
allocation.y,
allocation.width,
allocation.height);
event->configure.window = g_object_ref (gtk_widget_get_window (widget));
event->configure.send_event = TRUE;
event->configure.x = allocation.x;
event->configure.y = allocation.y;
event->configure.width = allocation.width;
event->configure.height = allocation.height;
gtk_widget_event (widget, event);
gtk_widget_queue_draw (widget);
gdk_event_free (event);
DEBUG_INFO("configure [E]\n");
}
static void gtk3_curve_draw_line (cairo_t *cr,
gdouble x1, gdouble y1,
gdouble x2, gdouble y2)
{
cairo_move_to (cr, x1, y1);
cairo_line_to (cr, x2, y2);
cairo_stroke (cr);
}
static gboolean
gtk3_curve_draw (GtkWidget *widget,
cairo_t *cr)
{
Gtk3CurvePrivate *priv;
GtkStyleContext *style_context;
GtkStyle *style;
GdkRGBA color;
gint last_x, last_y;
gint i;
GtkAllocation allocation;
Gtk3Curve *curve;
gfloat grid;
curve = GTK3_CURVE (widget);
priv = curve->priv;
DEBUG_INFO("draw [S]\n");
if (!cr)
{
DEBUG_ERROR("cairo == null\n");
return TRUE;
}
gtk_widget_get_allocation (widget, &allocation);
DEBUG_INFO("%d x %d\n", allocation.width, allocation.height);
if (priv->height != allocation.width ||
priv->curve_data.n_points != allocation.height)
{
gtk3_curve_interpolate (widget,
allocation.width - RADIUS * 2,
allocation.height - RADIUS * 2);
}
if (priv->use_bg_theme)
{
style_context = gtk_widget_get_style_context(GTK_WIDGET (curve));
gtk_render_background(style_context, cr,
0, 0,
allocation.width + RADIUS * 2,
allocation.height + RADIUS * 2);
gtk_style_context_get_color (style_context,
gtk_style_context_get_state (style_context),
&color);
gdk_cairo_set_source_rgba (cr, &color);
}
else
{
cairo_set_source_rgba (cr,
priv->background.red,
priv->background.green,
priv->background.blue,
priv->background.alpha);
}
cairo_paint(cr);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
switch (priv->grid_size)
{
default:
case GTK3_CURVE_GRID_MICRO:
grid = 10.0;
break;
case GTK3_CURVE_GRID_SMALL:
grid = 8.0;
break;
case GTK3_CURVE_GRID_MEDIUM:
grid = 6.0;
break;
case GTK3_CURVE_GRID_LARGE:
grid = 4.0;
case GTK3_CURVE_GRID_XLARGE:
grid = 2.0;
}
gfloat wm = allocation.width - (RADIUS * 2);
gfloat hm = allocation.height - (RADIUS * 2);
/* Draw grid */
for (i = 0; i < (int)grid+1; i++)
{
cairo_set_line_width (cr, 0.5);
cairo_set_source_rgba (cr,
priv->grid.red,
priv->grid.green,
priv->grid.blue,
priv->grid.alpha);
gdouble x1 = RADIUS;
gdouble y1 = i * (hm / grid) + RADIUS;
gdouble x2 = wm + RADIUS;
gdouble y2 = i * (hm / grid) + RADIUS;
gtk3_curve_draw_line (cr, x1, y1, x2, y2);
x1 = i * (wm / grid) + RADIUS;
y1 = RADIUS;
x2 = i * (wm / grid) + RADIUS;
y2 = hm + RADIUS;
gtk3_curve_draw_line (cr, x1, y1, x2, y2);
}
/* Draw a curve or line or set of lines */
for (int i=0; i<priv->curve_data.n_points; i++)
{
gdouble x = priv->curve_data.d_point[i].x;
gdouble y = priv->curve_data.d_point[i].y;
if (i > 0)
{
cairo_set_line_width (cr, 0.5);
cairo_set_source_rgba (cr,
priv->curve.red,
priv->curve.green,
priv->curve.blue,
priv->curve.alpha);
gtk3_curve_draw_line (cr, last_x, last_y, x, y);
}
last_x = x;
last_y = y;
}
if (priv->curve_data.curve_type != GTK3_CURVE_TYPE_FREE)
for (i = 0; i < priv->curve_data.n_cpoints; ++i)
{
gdouble x, y;
if (priv->curve_data.d_cpoints[i].x < priv->min_x)
continue;
x = project (priv->curve_data.d_cpoints[i].x,
priv->min_x, priv->max_x,
wm);
y = allocation.height - project (priv->curve_data.d_cpoints[i].y,
priv->min_y,
priv->max_y,
hm);
/* draw a bullet */
cairo_set_source_rgba (cr,
priv->cpoint.red,
priv->cpoint.green,
priv->cpoint.blue,
priv->cpoint.alpha);
cairo_arc(cr,
x,
y,
RADIUS * 1.5,
0,
2 * M_PI);
cairo_fill (cr);
}
DEBUG_INFO("draw [E]\n");
return FALSE;
}
static void
gtk3_curve_map (GtkWidget *widget)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
GTK_WIDGET_CLASS (gtk3_curve_parent_class)->map (widget);
DEBUG_INFO("map [S]\n");
if (priv->event_window)
{
DEBUG_INFO("show\n");
gdk_window_show (priv->event_window);
}
DEBUG_INFO("map [E]\n");
}
static void
gtk3_curve_unmap (GtkWidget *widget)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
DEBUG_INFO("unmap [S]\n");
if (priv->event_window)
{
DEBUG_INFO("hide\n");
gdk_window_hide (priv->event_window);
}
GTK_WIDGET_CLASS (gtk3_curve_parent_class)->unmap (widget);
DEBUG_INFO("unmap [E]\n");
}
static gboolean
gtk3_curve_enter (GtkWidget *widget,
GdkEventCrossing *event)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
DEBUG_INFO("enter [S]\n");
if (event->window == priv->event_window)
{
priv->in_curve = TRUE;
}
DEBUG_INFO("enter [E]\n");
return FALSE;
}
static gboolean
gtk3_curve_leave (GtkWidget *widget,
GdkEventCrossing *event)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
DEBUG_INFO("leave [S]\n");
if (event->window == priv->event_window)
{
priv->in_curve = FALSE;
}
DEBUG_INFO("leave [E]\n");
return FALSE;
}
static
void gtk3_curve_get_cursor_coord(GtkWidget *widget, gint *tx, gint *ty)
{
GdkSeat *user_seat;
GdkDevice *device_pointer;
GdkDisplay *display;
/* get the pointer position */
display = gtk_widget_get_display (widget);
user_seat = gdk_display_get_default_seat (gdk_display_get_default ());
device_pointer = gdk_seat_get_pointer (user_seat);
gdk_window_get_device_position (gtk_widget_get_window (widget),
device_pointer,
tx, ty, NULL);
}
static gboolean
gtk3_curve_button_press (GtkWidget *widget,
GdkEventButton *event)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
GdkCursorType new_type = priv->cursor_type;
GtkAllocation allocation;
gint cx, x, y, width, height, i;
gint closest_point = 0;
gfloat rx, ry, min_x;
gint tx, ty;
guint distance;
DEBUG_INFO("button press [S]\n");
gtk_grab_add (widget);
new_type = GDK_TCROSS;
gtk_widget_get_allocation (widget, &allocation);
width = allocation.width - RADIUS * 2;
height = allocation.height - RADIUS * 2;
if ((width < 0) || (height < 0))
return FALSE;
/* get the pointer position */
gtk3_curve_get_cursor_coord (widget, &tx, &ty);
x = CLAMP ((tx - RADIUS), 0, width - 1);
y = CLAMP ((ty - RADIUS), 0, height - 1);
DEBUG_INFO("press : xy[%dx%d] txy[%dx%d]\n",
x, y,
tx, ty);
min_x = priv->min_x;
distance = ~0U;
for (i = 0; i < priv->curve_data.n_cpoints; ++i)
{
cx = project (priv->curve_data.d_cpoints[i].x, min_x, priv->max_x, width);
if ((guint) abs (x - cx) < distance)
{
distance = abs (x - cx);
closest_point = i;
}
}
switch (priv->curve_data.curve_type)
{
default:
case GTK3_CURVE_TYPE_LINEAR:
case GTK3_CURVE_TYPE_SPLINE:
if (distance > MIN_DISTANCE)
{
/* insert a new control point */
if (priv->curve_data.n_cpoints > 0)
{
cx = project (priv->curve_data.d_cpoints[closest_point].x, min_x,
priv->max_x, width);
if (x > cx)
++closest_point;
}
++priv->curve_data.n_cpoints;
priv->curve_data.d_cpoints =
g_realloc (priv->curve_data.d_cpoints,
priv->curve_data.n_cpoints * sizeof (*priv->curve_data.d_cpoints));
for (i = priv->curve_data.n_cpoints - 1; i > closest_point; --i)
memcpy (priv->curve_data.d_cpoints + i, priv->curve_data.d_cpoints + i - 1,
sizeof (*priv->curve_data.d_cpoints));
}
priv->grab_point = closest_point;
priv->curve_data.d_cpoints[priv->grab_point].x =
unproject (x, min_x, priv->max_x, width);
priv->curve_data.d_cpoints[priv->grab_point].y =
unproject (height - y, priv->min_y, priv->max_y, height);
gtk3_curve_interpolate (widget, width, height);
break;
case GTK3_CURVE_TYPE_FREE:
priv->curve_data.d_point[x].x = RADIUS + x;
priv->curve_data.d_point[x].y = RADIUS + y;
priv->grab_point = x;
priv->last = y;
break;
}
if (gtk_widget_is_visible (widget))
{
DEBUG_INFO("queue draw\n");
gtk_widget_queue_draw (widget);
}
DEBUG_INFO("button press [E]\n");
return FALSE;
}
static gboolean
gtk3_curve_button_release (GtkWidget *widget,
GdkEventButton *event)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
GtkAllocation allocation;
GdkCursorType new_type = priv->cursor_type;
gint src, dst, cx, x, y, width, height, i;
gfloat rx, ry, min_x;
gint closest_point = 0;
guint distance;
gint tx, ty;
DEBUG_INFO("button release [S]\n");
gtk_grab_remove (widget);
gtk_widget_get_allocation (widget, &allocation);
width = allocation.width - RADIUS * 2;
height = allocation.height - RADIUS * 2;
if ((width < 0) || (height < 0))
return FALSE;
/* get the pointer position */
gtk3_curve_get_cursor_coord (widget, &tx, &ty);
x = CLAMP ((tx - RADIUS), 0, width - 1);
y = CLAMP ((ty - RADIUS), 0, height - 1);
DEBUG_INFO("release : xy[%dx%d] txy[%dx%d]\n",
x, y,
tx, ty);
min_x = priv->min_x;
distance = ~0U;
for (i = 0; i < priv->curve_data.n_cpoints; ++i)
{
cx = project (priv->curve_data.d_cpoints[i].x,
min_x, priv->max_x, width);
if ((guint) abs (x - cx) < distance)
{
distance = abs (x - cx);
closest_point = i;
}
}
/* delete inactive points: */
if (priv->curve_data.curve_type != GTK3_CURVE_TYPE_FREE)
{
for (src = dst = 0; src < priv->curve_data.n_cpoints; ++src)
{
if (priv->curve_data.d_cpoints[src].x >= min_x)
{
memcpy (priv->curve_data.d_cpoints + dst,
priv->curve_data.d_cpoints + src,
sizeof (*priv->curve_data.d_cpoints));
++dst;
}
}
if (dst < src)
{
priv->curve_data.n_cpoints -= (src - dst);
if (priv->curve_data.n_cpoints <= 0)
{
priv->curve_data.n_cpoints = 1;
priv->curve_data.d_cpoints[0].x = min_x;
priv->curve_data.d_cpoints[0].y = priv->min_y;
gtk3_curve_interpolate (widget, width, height);
if (gtk_widget_is_visible (widget))
{
DEBUG_INFO("queue draw\n");
gtk_widget_queue_draw (widget);
}
}
priv->curve_data.d_cpoints =
g_realloc (priv->curve_data.d_cpoints,
priv->curve_data.n_cpoints *
sizeof (*priv->curve_data.d_cpoints));
}
}
new_type = GDK_FLEUR;
priv->grab_point = -1;
DEBUG_INFO("button release [E]\n");
return FALSE;
}
static gboolean
gtk3_curve_motion_notify (GtkWidget *widget,
GdkEventMotion *event)
{
Gtk3CurvePrivate *priv = GTK3_CURVE (widget)->priv;
GtkAllocation allocation;
GdkCursorType new_type = priv->cursor_type;
gint i, src, dst, leftbound, rightbound;
GdkEventMotion *mevent;
gint tx, ty, h,w;
gint cx, x, y, width, height;
gint closest_point = 0;
gfloat rx, ry, min_x;
guint distance;
gint x1, x2, y1, y2;
gint retval = FALSE;
DEBUG_INFO("motion_notify [S]\n");
mevent = (GdkEventMotion *) event;
gtk_widget_get_allocation (widget, &allocation);
width = allocation.width - RADIUS * 2;
height = allocation.height - RADIUS * 2;