Merge branch 'master' into cairocanvas
[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/threads.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 LIBARDOUR_API 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 &, framepos_t, framepos_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 operator==(const Location& other);
61
62         bool locked() const { return _locked; }
63         void lock ();
64         void unlock ();
65
66         framepos_t start() const  { return _start; }
67         framepos_t end() const { return _end; }
68         framecnt_t length() const { return _end - _start; }
69
70         int set_start (framepos_t s, bool force = false, bool allow_bbt_recompute = true);
71         int set_end (framepos_t e, bool force = false, bool allow_bbt_recompute = true);
72         int set (framepos_t start, framepos_t end, bool allow_bbt_recompute = true);
73
74         int move_to (framepos_t pos);
75
76         const std::string& name() const { return _name; }
77         void set_name (const std::string &str) { _name = str; name_changed(this); }
78
79         void set_auto_punch (bool yn, void *src);
80         void set_auto_loop (bool yn, void *src);
81         void set_hidden (bool yn, void *src);
82         void set_cd (bool yn, void *src);
83         void set_is_range_marker (bool yn, void* src);
84
85         bool is_auto_punch () const { return _flags & IsAutoPunch; }
86         bool is_auto_loop () const { return _flags & IsAutoLoop; }
87         bool is_mark () const { return _flags & IsMark; }
88         bool is_hidden () const { return _flags & IsHidden; }
89         bool is_cd_marker () const { return _flags & IsCDMarker; }
90         bool is_session_range () const { return _flags & IsSessionRange; }
91         bool is_range_marker() const { return _flags & IsRangeMarker; }
92         bool matches (Flags f) const { return _flags & f; }
93
94         Flags flags () const { return _flags; }
95
96         PBD::Signal1<void,Location*> name_changed;
97         PBD::Signal1<void,Location*> end_changed;
98         PBD::Signal1<void,Location*> start_changed;
99
100         PBD::Signal1<void,Location*> LockChanged;
101         PBD::Signal2<void,Location*,void*> FlagsChanged;
102         PBD::Signal1<void,Location*> PositionLockStyleChanged;
103
104         /* this is sent only when both start and end change at the same time */
105         PBD::Signal1<void,Location*> changed;
106
107         /* CD Track / CD-Text info */
108
109         std::map<std::string, std::string> cd_info;
110         XMLNode& cd_info_node (const std::string &, const std::string &);
111
112         XMLNode& get_state (void);
113         int set_state (const XMLNode&, int version);
114
115         PositionLockStyle position_lock_style() const { return _position_lock_style; }
116         void set_position_lock_style (PositionLockStyle ps);
117         void recompute_frames_from_bbt ();
118
119   private:
120         std::string        _name;
121         framepos_t         _start;
122         Timecode::BBT_Time _bbt_start;
123         framepos_t         _end;
124         Timecode::BBT_Time _bbt_end;
125         Flags              _flags;
126         bool               _locked;
127         PositionLockStyle  _position_lock_style;
128
129         void set_mark (bool yn);
130         bool set_flag_internal (bool yn, Flags flag);
131         void recompute_bbt_from_frames ();
132 };
133
134 class LIBARDOUR_API Locations : public SessionHandleRef, public PBD::StatefulDestructible
135 {
136   public:
137         typedef std::list<Location *> LocationList;
138
139         Locations (Session &);
140         ~Locations ();
141
142         const LocationList& list() { return locations; }
143
144         void add (Location *, bool make_current = false);
145         void remove (Location *);
146         void clear ();
147         void clear_markers ();
148         void clear_ranges ();
149
150         XMLNode& get_state (void);
151         int set_state (const XMLNode&, int version);
152         Location *get_location_by_id(PBD::ID);
153
154         Location* auto_loop_location () const;
155         Location* auto_punch_location () const;
156         Location* session_range_location() const;
157
158         int next_available_name(std::string& result,std::string base);
159         uint32_t num_range_markers() const;
160
161         int set_current (Location *, bool want_lock = true);
162         Location *current () const { return current_location; }
163
164         framepos_t first_mark_before (framepos_t, bool include_special_ranges = false);
165         framepos_t first_mark_after (framepos_t, bool include_special_ranges = false);
166
167         void marks_either_side (framepos_t const, framepos_t &, framepos_t &) const;
168
169         void find_all_between (framepos_t start, framepos_t, LocationList&, Location::Flags);
170
171         enum Change {
172                 ADDITION, ///< a location was added, but nothing else changed
173                 REMOVAL, ///< a location was removed, but nothing else changed
174                 OTHER ///< something more complicated happened
175         };
176
177         PBD::Signal1<void,Location*> current_changed;
178         /** something changed about the location list; the parameter gives some idea as to what */
179         PBD::Signal1<void,Change>    changed;
180         /** a location has been added to the end of the list */
181         PBD::Signal1<void,Location*> added;
182         PBD::Signal1<void,Location*> removed;
183         PBD::Signal1<void,const PBD::PropertyChange&>    StateChanged;
184
185         template<class T> void apply (T& obj, void (T::*method)(LocationList&)) {
186                 Glib::Threads::Mutex::Lock lm (lock);
187                 (obj.*method)(locations);
188         }
189
190         template<class T1, class T2> void apply (T1& obj, void (T1::*method)(LocationList&, T2& arg), T2& arg) {
191                 Glib::Threads::Mutex::Lock lm (lock);
192                 (obj.*method)(locations, arg);
193         }
194
195   private:
196
197         LocationList         locations;
198         Location            *current_location;
199         mutable Glib::Threads::Mutex  lock;
200
201         int set_current_unlocked (Location *);
202         void location_changed (Location*);
203 };
204
205 } // namespace ARDOUR
206
207 #endif /* __ardour_location_h__ */