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