Allow markers to be glued to bar/beat time. Fixes #1815.
[ardour.git] / libs / ardour / ardour / location.h
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 */
19
20 #ifndef __ardour_location_h__
21 #define __ardour_location_h__
22
23 #include <string>
24 #include <list>
25 #include <iostream>
26 #include <map>
27
28 #include <sys/types.h>
29
30 #include <glibmm/thread.h>
31
32 #include "pbd/undo.h"
33 #include "pbd/stateful.h"
34 #include "pbd/statefuldestructible.h"
35
36 #include "ardour/ardour.h"
37 #include "ardour/session_handle.h"
38
39 namespace ARDOUR {
40
41 class Location : public SessionHandleRef, public PBD::StatefulDestructible
42 {
43   public:
44         enum Flags {
45                 IsMark = 0x1,
46                 IsAutoPunch = 0x2,
47                 IsAutoLoop = 0x4,
48                 IsHidden = 0x8,
49                 IsCDMarker = 0x10,
50                 IsRangeMarker = 0x20,
51                 IsSessionRange = 0x40
52         };
53
54         Location (Session &);
55         Location (Session &, nframes64_t, nframes64_t, const std::string &, Flags bits = Flags(0));
56         Location (const Location& other);
57         Location (Session &, const XMLNode&);
58         Location* operator= (const Location& other);
59
60         bool locked() const { return _locked; }
61         void lock() { _locked = true; changed (this); }
62         void unlock() { _locked = false; changed (this); }
63
64         nframes64_t start() const  { return _start; }
65         nframes64_t end() const { return _end; }
66         nframes64_t length() const { return _end - _start; }
67
68         int set_start (nframes64_t s, bool force = false, bool allow_bbt_recompute = true);
69         int set_end (nframes64_t e, bool force = false, bool allow_bbt_recompute = true);
70         int set (nframes64_t start, nframes64_t end, bool allow_bbt_recompute = true);
71
72         int move_to (nframes64_t pos);
73
74         const std::string& name() const { return _name; }
75         void set_name (const std::string &str) { _name = str; name_changed(this); }
76
77         void set_auto_punch (bool yn, void *src);
78         void set_auto_loop (bool yn, void *src);
79         void set_hidden (bool yn, void *src);
80         void set_cd (bool yn, void *src);
81         void set_is_range_marker (bool yn, void* src);
82
83         bool is_auto_punch () const { return _flags & IsAutoPunch; }
84         bool is_auto_loop () const { return _flags & IsAutoLoop; }
85         bool is_mark () const { return _flags & IsMark; }
86         bool is_hidden () const { return _flags & IsHidden; }
87         bool is_cd_marker () const { return _flags & IsCDMarker; }
88         bool is_session_range () const { return _flags & IsSessionRange; }
89         bool is_range_marker() const { return _flags & IsRangeMarker; }
90         bool matches (Flags f) const { return _flags & f; }
91
92         Flags flags () const { return _flags; }
93
94         PBD::Signal1<void,Location*> name_changed;
95         PBD::Signal1<void,Location*> end_changed;
96         PBD::Signal1<void,Location*> start_changed;
97
98         PBD::Signal2<void,Location*,void*> FlagsChanged;
99
100         /* this is sent only when both start&end change at the same time */
101
102         PBD::Signal1<void,Location*> changed;
103
104         /* CD Track / CD-Text info */
105
106         std::map<std::string, std::string> cd_info;
107         XMLNode& cd_info_node (const std::string &, const std::string &);
108
109         XMLNode& get_state (void);
110         int set_state (const XMLNode&, int version);
111
112         PositionLockStyle position_lock_style() const { return _position_lock_style; }
113         void set_position_lock_style (PositionLockStyle ps);
114         void recompute_frames_from_bbt ();
115
116   private:
117         std::string   _name;
118         nframes64_t   _start;
119         BBT_Time      _bbt_start;
120         nframes64_t   _end;
121         BBT_Time      _bbt_end;
122         Flags         _flags;
123         bool          _locked;
124         PositionLockStyle _position_lock_style;
125
126         void set_mark (bool yn);
127         bool set_flag_internal (bool yn, Flags flag);
128         void recompute_bbt_from_frames ();
129 };
130
131 class Locations : public SessionHandleRef, public PBD::StatefulDestructible
132 {
133   public:
134         typedef std::list<Location *> LocationList;
135
136         Locations (Session &);
137         ~Locations ();
138
139         const LocationList& list() { return locations; }
140
141         void add (Location *, bool make_current = false);
142         void remove (Location *);
143         void clear ();
144         void clear_markers ();
145         void clear_ranges ();
146
147         XMLNode& get_state (void);
148         int set_state (const XMLNode&, int version);
149         Location *get_location_by_id(PBD::ID);
150
151         Location* auto_loop_location () const;
152         Location* auto_punch_location () const;
153         Location* session_range_location() const;
154
155         int next_available_name(std::string& result,std::string base);
156         uint32_t num_range_markers() const;
157
158         int set_current (Location *, bool want_lock = true);
159         Location *current () const { return current_location; }
160
161         Location* first_location_before (nframes64_t, bool include_special_ranges = false);
162         Location* first_location_after (nframes64_t, bool include_special_ranges = false);
163
164         void marks_either_side (nframes64_t const, nframes64_t &, nframes64_t &) const;
165
166         void find_all_between (nframes64_t start, nframes64_t, LocationList&, Location::Flags);
167
168         enum Change {
169                 ADDITION, ///< a location was added, but nothing else changed
170                 REMOVAL, ///< a location was removed, but nothing else changed
171                 OTHER ///< something more complicated happened
172         };
173
174         PBD::Signal1<void,Location*> current_changed;
175         /** something changed about the location list; the parameter gives some idea as to what */
176         PBD::Signal1<void,Change>    changed;
177         /** a location has been added to the end of the list */
178         PBD::Signal1<void,Location*> added;
179         PBD::Signal1<void,Location*> removed;
180         PBD::Signal1<void,const PBD::PropertyChange&>    StateChanged;
181
182         template<class T> void apply (T& obj, void (T::*method)(LocationList&)) {
183                 Glib::Mutex::Lock lm (lock);
184                 (obj.*method)(locations);
185         }
186
187         template<class T1, class T2> void apply (T1& obj, void (T1::*method)(LocationList&, T2& arg), T2& arg) {
188                 Glib::Mutex::Lock lm (lock);
189                 (obj.*method)(locations, arg);
190         }
191
192   private:
193
194         LocationList         locations;
195         Location            *current_location;
196         mutable Glib::Mutex  lock;
197
198         int set_current_unlocked (Location *);
199         void location_changed (Location*);
200 };
201
202 } // namespace ARDOUR
203
204 #endif /* __ardour_location_h__ */