fix a few things related to fit-to-tracks and toggle-visual-state (from 2.0)
[ardour.git] / gtk2_ardour / lineset.h
1 /*
2     Copyright (C) 2007 Paul Davis
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 #ifndef __gnome_canvas_lineset_h__
19 #define __gnome_canvas_lineset_h__
20
21 #include <libgnomecanvasmm/item.h>
22
23 namespace Gnome {
24 namespace Canvas {
25
26 class LineSetClass : public Glib::Class {
27 public:
28         const Glib::Class& init();
29         static void class_init_function(void* g_class, void* class_data);
30 };
31
32 /** A canvas item that displays a set of vertical or horizontal lines,
33  * spanning the entire size of the item.
34  */
35 class LineSet : public Item {
36 public:
37         enum Orientation {
38                 Vertical,
39                 Horizontal
40         };
41
42         LineSet(Group& parent, Orientation);
43         virtual ~LineSet();
44
45         Glib::PropertyProxy<double> property_x1() { return x1.get_proxy(); }
46         Glib::PropertyProxy<double> property_y1() { return y1.get_proxy(); }
47         Glib::PropertyProxy<double> property_x2() { return x2.get_proxy(); }
48         Glib::PropertyProxy<double> property_y2() { return y2.get_proxy(); }
49
50         /* Note: every line operation takes a coord parameter, as an index to
51          * the line it modifies. The index will identify a line if it is between
52          * line.coord and line.coord + line.width.
53          */
54
55         /** Move a line to a new position.
56          * For this to work (to move the desired line) it is important that
57          * lines have unique coordinates. This also applies to every line
58          * accessing functions below
59          */
60         void move_line(double coord, double dest);
61
62         /** Change the width of a line.
63          * Only allow if the new width doesn't overlap the next line (see below)
64          */
65         void change_line_width(double coord, double width);
66
67         /** Change the color of a line.
68          */
69         void change_line_color(double coord, uint32_t color);
70
71         /** Add a line to draw.
72          * width is an offset, so that coord + width specifies the end of the line.
73          * lines should not overlap, as no layering information is provided.
74          * however, line_coord[i] + line_width[i] == line_coord[i+1] is
75          * be legal, as the coordinates are real numbers and represents
76          * real world coordinates. Two real world object sharing coordinates for start
77          * and end are not overlapping.
78          */
79         void add_line(double coord, double width, uint32_t color);
80
81         /** Remove the line at coord
82          */
83         void remove_line(double coord);
84         
85         /** Remove all lines in a coordinate range
86          */
87         void remove_lines(double c1, double c2);
88
89         /** Remove all lines with a coordinate lower than coord
90          */
91         void remove_until(double coord);
92         
93         /** Remove all lines with a coordinate equal to or higher than coord.
94          */
95         void remove_from(double coord);
96
97         /** Remove all lines.
98          */
99         void clear();
100
101         /** Add a set of lines in the given range.
102          * For every line visible in the provided coordinate range, call add_line().
103          * This is called when the area between c1 and c2 becomes visible, when
104          * previously outside any possible view.
105          * The number of calls to this function should be kept at a minimum.
106          */
107         virtual void request_lines(double c1, double c2);
108
109         /** Instead of overriding the update_lines function one can connect to this
110          * and add lines externally instead.
111          * If add_lines() is overrided, this signal will not be emitted.
112          */
113         sigc::signal<void, LineSet&, double, double> signal_request_lines;
114
115         /* overridden from Gnome::Canvas::Item */
116         void update_vfunc(double* affine, ArtSVP* clip_path, int flags);
117         void realize_vfunc();
118         void unrealize_vfunc();
119         void map_vfunc();
120         void unmap_vfunc();
121         void draw_vfunc(const Glib::RefPtr<Gdk::Drawable>& drawable, int x, int y, int width, int height);
122         void render_vfunc(GnomeCanvasBuf* buf);
123         double point_vfunc(double x, double y, int cx, int cy, GnomeCanvasItem** actual_item);
124         void bounds_vfunc(double* x1, double* y1, double* x2, double* y2);
125         bool on_event(GdkEvent* p1);
126
127         /* debug */
128         void print_lines();
129         
130 protected:
131         struct Line {
132                 Line(double c, double w, uint32_t color);
133                 Line(double c);
134
135                 void set_color(uint32_t color);
136
137                 double coord;
138                 double width;
139                 unsigned char r;
140                 unsigned char g;
141                 unsigned char b;
142                 unsigned char a;
143         };
144
145         static inline void paint_vert(GnomeCanvasBuf* buf, LineSet::Line& line, int x1, int y1, int x2, int y2);
146         static inline void paint_horiz(GnomeCanvasBuf* buf, LineSet::Line& line, int x1, int y1, int x2, int y2);
147
148         static bool line_compare(const Line& a, const Line& b);
149
150         typedef std::list<Line> Lines;
151         void bounds_need_update();
152         void region_needs_update(double coord1, double coord2);
153         bool update_bounds();
154         void update_lines(bool need_redraw);
155         void redraw_request(ArtIRect&);
156         void redraw_request(ArtDRect&);
157
158         Lines::iterator line_at(double coord);
159
160         /** Stores last accessed line so adjacent lines are found faster */
161         Lines::iterator cached_pos;
162
163         static LineSetClass lineset_class;
164         Orientation orientation;
165         Lines lines;
166
167         /* properties */
168         Glib::Property<double> x1;
169         Glib::Property<double> y1;
170         Glib::Property<double> x2;
171         Glib::Property<double> y2;
172
173         /** Cached bounding box in canvas coordinates */
174         ArtIRect bbox;
175
176 private:
177         LineSet();
178         LineSet(const LineSet&);
179
180         bool in_update;
181
182         /* a range that needs update update1 > update2 ==> no update needed */
183         double update_region1;
184         double update_region2;
185         bool bounds_changed;
186
187         double covered1;
188         double covered2;
189 };
190
191 } /* namespace Canvas */
192 } /* namespace Gnome */
193
194 #endif /* __gnome_canvas_lineset_h__ */