7d2e5e3f11bb7a19330874e795fafa8b8a7fe971
[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*))
34         : editor (ed), length(1.0)
35 {
36         /* "randomly" initialize coords */
37         
38         points.push_back (Gnome::Art::Point (-9383839.0, 0.0));
39         points.push_back (Gnome::Art::Point (1.0, 0.0));
40         
41         canvas_item = new Gnome::Canvas::Line (editor.cursor_group);
42         canvas_item->set_property ("points", points.gobj());
43         canvas_item->set_property ("fill_color", color.c_str());
44         canvas_item->set_property ("width_pixels", 1);
45         canvas_item->set_property ("first_arrowhead", (gboolean) TRUE);
46         canvas_item->set_property ("last_arrowhead", (gboolean) TRUE);
47         canvas_item->set_property ("arrow_shape_a", 11.0);
48         canvas_item->set_property ("arrow_shape_b", 0.0);
49         canvas_item->set_property ("arrow_shape_c", 9.0);
50
51         canvas_item->set_data ("cursor", this);
52         canvas_item->signal_event().connect (slot (ed, callback));
53
54         current_frame = 1; /* force redraw at 0 */
55 }
56
57 Editor::Cursor::~Cursor ()
58
59 {
60         gtk_object_destroy (GTK_OBJECT(canvas_item));
61         gnome_canvas_points_unref (points);
62 }
63
64 void
65 Editor::Cursor::set_position (jack_nframes_t frame)
66 {
67         double new_pos =  editor.frame_to_unit (frame);
68
69         if (editor.session == 0) {
70                 gnome_canvas_item_hide (canvas_item);
71         } else {
72                 gnome_canvas_item_show (canvas_item);
73         }
74
75         current_frame = frame;
76
77         if (new_pos == points->coords[0]) {
78
79                 /* change in position is not visible, so just raise it */
80                 
81                 gnome_canvas_item_raise_to_top (canvas_item);
82                 return;
83         } 
84
85         points->coords[0] = new_pos;
86         points->coords[2] = new_pos;
87
88         // cerr << "set cursor2 al points, nc = " << points->num_points << endl;
89         gnome_canvas_item_set (canvas_item, "points", points, NULL);
90         gnome_canvas_item_raise_to_top (canvas_item);
91 }
92
93 void
94 Editor::Cursor::set_length (double units)
95 {
96         length = units; 
97         points->coords[3] = points->coords[1] + length;
98         // cerr << "set cursor3 al points, nc = " << points->num_points << endl;
99         gnome_canvas_item_set (canvas_item, "points", points, NULL);
100 }
101
102 void 
103 Editor::Cursor::set_y_axis (double position)
104 {
105         points->coords[1] = position;
106         points->coords[3] = position + length;
107         // cerr << "set cursor4 al points, nc = " << points->num_points << endl;
108         gnome_canvas_item_set (canvas_item, "points", points, NULL);
109 }