2 * Barely started curve editor widget. Standard GtkCurve is
3 * unreliable and deprecated, so I need to make my own.
5 * Copyright (C) 2008 Krzysztof Foltman
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
22 #ifndef CALF_CTL_CURVE_H
23 #define CALF_CTL_CURVE_H
30 #define CALF_TYPE_CURVE (calf_curve_get_type())
31 #define CALF_CURVE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CALF_TYPE_CURVE, CalfCurve))
32 #define CALF_IS_CURVE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CALF_TYPE_CURVE))
33 #define CALF_CURVE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CALF_TYPE_CURVE, CalfCurveClass))
34 #define CALF_IS_CURVE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((klass), CALF_TYPE_CURVE))
36 /// Mapping curve editor control. May be used for editing multisegment lines (key mapping,
37 /// velocity mapping etc). This version isn't suitable for envelope editing because both
38 /// ends (start and end) have fixed X coordinates and it doesn't resize.
41 /// A point with floating point X and Y coordinates
42 typedef std::pair
<float, float> point
;
43 /// A collection of points
44 typedef std::vector
<point
> point_vector
;
46 /// User callbacks for handling curve events
49 /// Called when a point has been edited, added or removed
50 virtual void curve_changed(CalfCurve
*src
, const point_vector
&data
) = 0;
51 /// Called to clip/snap/otherwise adjust candidate point coordinates
52 virtual void clip(CalfCurve
*src
, int pt
, float &x
, float &y
, bool &hide
) = 0;
53 virtual ~EventSink() {}
56 /// Null implementation of EventSink
57 struct EventAdapter
: public EventSink
59 virtual void curve_changed(CalfCurve
*src
, const point_vector
&data
) {}
60 virtual void clip(CalfCurve
*src
, int pt
, float &x
, float &y
, bool &hide
) {}
63 /// Debug implementation of EventSink
64 struct EventTester
: public EventAdapter
66 virtual void curve_changed(CalfCurve
*src
, const point_vector
&data
) {
67 for(size_t i
= 0; i
< data
.size(); i
++)
68 g_message("Point %d: (%f, %f)", (int)i
, data
[i
].first
, data
[i
].second
);
72 /// Base class instance members
76 /// Coordinate ranges (in logical coordinates for top left and bottom right)
78 /// Currently selected point (when dragging/adding), or -1 if none is selected
80 /// If currently selected point is a candidate for deletion (ie. outside of graph+margin range)
82 /// Interface for notification
84 /// Cached hand (drag) cursor
85 GdkCursor
*hand_cursor
;
86 /// Cached pencil (add point) cursor
87 GdkCursor
*pencil_cursor
;
88 /// Cached arrow (do not add point) cursor
89 GdkCursor
*arrow_cursor
;
90 /// Maximum number of points
91 unsigned int point_limit
;
93 /// Convert logical (mapping) to physical (screen) coordinates
94 void log2phys(float &x
, float &y
);
95 /// Convert physical (screen) to logical (mapping) coordinates
96 void phys2log(float &x
, float &y
);
98 /// @param pt point being clipped
99 /// @param x horizontal logical coordinate
100 /// @param y vertical logical coordinate
101 /// @param hide true if point is outside "valid" range and about to be deleted
102 void clip(int pt
, float &x
, float &y
, bool &hide
);
105 struct CalfCurveClass
107 GtkWidgetClass parent_class
;
110 /// Create a CalfCurve
111 extern GtkWidget
*calf_curve_new(unsigned int point_limit
= -1);
113 /// Return a GObject type for class CalfCurve
114 extern GType
calf_curve_get_type();
116 /// Set points and update the widget
117 extern void calf_curve_set_points(GtkWidget
*widget
, const CalfCurve::point_vector
&src
);