3fd0a561e7204aed29e291afe59e4a93b314bf3b
[ardour.git] / gtk2_ardour / editor_cursors.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <cstdlib>
22 #include <cmath>
23
24 #include <libgnomecanvas/libgnomecanvas.h>
25
26 #include "utils.h"
27 #include "editor.h"
28
29 using namespace sigc;
30 using namespace ARDOUR;
31 using namespace Gtk;
32
33 Editor::Cursor::Cursor (Editor& ed, const string& color, bool (Editor::*callbck)(GdkEvent*,ArdourCanvas::Item*))
34         : editor (ed),
35           points (2),
36           canvas_item (*editor.cursor_group),
37           length(1.0)
38 {
39         
40         /* "randomly" initialize coords */
41
42         points.push_back(Gnome::Art::Point(-9383839.0, 0.0));
43         points.push_back(Gnome::Art::Point(1.0, 0.0));
44
45         // cerr << "set cursor points, nc = " << points->num_points << endl;
46
47         canvas_item.property_points().set_value(points);
48         canvas_item.property_fill_color().set_value(color.c_str());
49         canvas_item.property_width_pixels().set_value(1);
50         canvas_item.property_first_arrowhead().set_value(TRUE);
51         canvas_item.property_last_arrowhead().set_value(TRUE);
52         canvas_item.property_arrow_shape_a().set_value(11.0);
53         canvas_item.property_arrow_shape_b().set_value(0.0);
54         canvas_item.property_arrow_shape_c().set_value(9.0);
55
56         // cerr << "cursor line @ " << canvas_item << endl;
57
58         canvas_item.set_data ("cursor", this);
59         canvas_item.signal_event().connect (bind (mem_fun (ed, callbck), &canvas_item));
60
61         current_frame = 1; /* force redraw at 0 */
62 }
63
64 Editor::Cursor::~Cursor ()
65
66 {
67 }
68
69 void
70 Editor::Cursor::set_position (jack_nframes_t frame)
71 {
72         double new_pos =  editor.frame_to_unit (frame);
73
74         if (editor.session == 0) {
75                 canvas_item.hide();
76         } else {
77                 canvas_item.show();
78         }
79
80         current_frame = frame;
81
82         if (new_pos == points.front().get_x()) {
83
84                 /* change in position is not visible, so just raise it */
85
86                 canvas_item.raise_to_top();
87                 return;
88         } 
89
90         points.front().set_x(new_pos);
91         points.back().set_x(new_pos);
92
93         // cerr << "set cursor2 al points, nc = " << points->num_points << endl;
94         canvas_item.property_points().set_value(points);
95         canvas_item.raise_to_top();
96 }
97
98 void
99 Editor::Cursor::set_length (double units)
100 {
101         length = units; 
102         points.back().set_x (points.front().get_y() + length);
103         // cerr << "set cursor3 al points, nc = " << points->num_points << endl;
104         canvas_item.property_points().set_value(points);
105 }
106
107 void 
108 Editor::Cursor::set_y_axis (double position)
109 {
110         points.front().set_y (position);
111         points.back().set_x (position + length);
112         // cerr << "set cursor4 al points, nc = " << points->num_points << endl;
113         canvas_item.property_points().set_value(points);
114 }