0afbee4fdd8b67bd13b0813f52c6ee0185828b0a
[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 <gtk-canvas.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, GtkSignalFunc callbck)
34         : editor (ed), callback (callbck), length(1.0)
35 {
36         GtkCanvasGroup *group;
37         points = gtk_canvas_points_new (2);
38         
39         /* "randomly" initialize coords */
40
41         points->coords[0] = -9383839.0;
42         points->coords[1] = 0.0;
43         points->coords[2] = 1.0;
44         points->coords[3] = 0.0;
45
46         group = GTK_CANVAS_GROUP (editor.cursor_group);
47
48         // cerr << "set cursor points, nc = " << points->num_points << endl;
49         canvas_item = gtk_canvas_item_new (group,
50                                            gtk_canvas_line_get_type(),
51                                            "points", points,
52                                            "fill_color", color.c_str(),
53                                            "width_pixels", 1,
54                                            "first_arrowhead", (gboolean) TRUE,
55                                            "last_arrowhead", (gboolean) TRUE,
56                                            "arrow_shape_a", 11.0,
57                                            "arrow_shape_b", 0.0,
58                                            "arrow_shape_c", 9.0,
59                                            NULL);
60
61         // cerr << "cursor line @ " << canvas_item << endl;
62
63         gtk_object_set_data (GTK_OBJECT(canvas_item), "cursor", this);
64         gtk_signal_connect (GTK_OBJECT(canvas_item), "event", callback, &editor);
65
66         current_frame = 1; /* force redraw at 0 */
67 }
68
69 Editor::Cursor::~Cursor ()
70
71 {
72         gtk_object_destroy (GTK_OBJECT(canvas_item));
73         gtk_canvas_points_unref (points);
74 }
75
76 void
77 Editor::Cursor::set_position (jack_nframes_t frame)
78 {
79         double new_pos =  editor.frame_to_unit (frame);
80
81         if (editor.session == 0) {
82                 gtk_canvas_item_hide (canvas_item);
83         } else {
84                 gtk_canvas_item_show (canvas_item);
85         }
86
87         current_frame = frame;
88
89         if (new_pos == points->coords[0]) {
90
91                 /* change in position is not visible, so just raise it */
92                 
93                 gtk_canvas_item_raise_to_top (canvas_item);
94                 return;
95         } 
96
97         points->coords[0] = new_pos;
98         points->coords[2] = new_pos;
99
100         // cerr << "set cursor2 al points, nc = " << points->num_points << endl;
101         gtk_canvas_item_set (canvas_item, "points", points, NULL);
102         gtk_canvas_item_raise_to_top (canvas_item);
103 }
104
105 void
106 Editor::Cursor::set_length (double units)
107 {
108         length = units; 
109         points->coords[3] = points->coords[1] + length;
110         // cerr << "set cursor3 al points, nc = " << points->num_points << endl;
111         gtk_canvas_item_set (canvas_item, "points", points, NULL);
112 }
113
114 void 
115 Editor::Cursor::set_y_axis (double position)
116 {
117         points->coords[1] = position;
118         points->coords[3] = position + length;
119         // cerr << "set cursor4 al points, nc = " << points->num_points << endl;
120         gtk_canvas_item_set (canvas_item, "points", points, NULL);
121 }