Make the session start/end location a single location (with start and end) rather...
[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
38 namespace ARDOUR {
39
40 class Location : public PBD::StatefulDestructible
41 {
42   public:
43         enum Flags {
44                 IsMark = 0x1,
45                 IsAutoPunch = 0x2,
46                 IsAutoLoop = 0x4,
47                 IsHidden = 0x8,
48                 IsCDMarker = 0x10,
49                 IsRangeMarker = 0x20,
50                 IsSessionRange = 0x40
51         };
52
53         Location (nframes64_t sample_start,
54                         nframes64_t sample_end,
55                         const std::string &name,
56                         Flags bits = Flags(0))
57
58                 : _name (name),
59                 _start (sample_start),
60                 _end (sample_end),
61                 _flags (bits),
62                 _locked (false) { }
63
64         Location () {
65                 _start = 0;
66                 _end = 0;
67                 _flags = Flags (0);
68                 _locked = false;
69         }
70
71         Location (const Location& other);
72         Location (const XMLNode&);
73         Location* operator= (const Location& other);
74
75         bool locked() const { return _locked; }
76         void lock() { _locked = true; changed (this); }
77         void unlock() { _locked = false; changed (this); }
78
79         nframes64_t start() const  { return _start; }
80         nframes64_t end() const { return _end; }
81         nframes64_t length() const { return _end - _start; }
82
83         int set_start (nframes64_t s);
84         int set_end (nframes64_t e);
85         int set (nframes64_t start, nframes64_t end);
86
87         int move_to (nframes64_t pos);
88
89         const std::string& name() const { return _name; }
90         void set_name (const std::string &str) { _name = str; name_changed(this); }
91
92         void set_auto_punch (bool yn, void *src);
93         void set_auto_loop (bool yn, void *src);
94         void set_hidden (bool yn, void *src);
95         void set_cd (bool yn, void *src);
96         void set_is_session_range (bool yn, void* src);
97         void set_is_range_marker (bool yn, void* src);
98
99         bool is_auto_punch () const { return _flags & IsAutoPunch; }
100         bool is_auto_loop () const { return _flags & IsAutoLoop; }
101         bool is_mark () const { return _flags & IsMark; }
102         bool is_hidden () const { return _flags & IsHidden; }
103         bool is_cd_marker () const { return _flags & IsCDMarker; }
104         bool is_session_range () const { return _flags & IsSessionRange; }
105         bool is_range_marker() const { return _flags & IsRangeMarker; }
106         bool matches (Flags f) const { return _flags & f; }
107
108         PBD::Signal1<void,Location*> name_changed;
109         PBD::Signal1<void,Location*> end_changed;
110         PBD::Signal1<void,Location*> start_changed;
111
112         PBD::Signal2<void,Location*,void*> FlagsChanged;
113
114         /* this is sent only when both start&end change at the same time */
115
116         PBD::Signal1<void,Location*> changed;
117
118         /* CD Track / CD-Text info */
119
120         std::map<std::string, std::string> cd_info;
121         XMLNode& cd_info_node (const std::string &, const std::string &);
122
123         XMLNode& get_state (void);
124         int set_state (const XMLNode&, int version);
125
126   private:
127         std::string   _name;
128         nframes64_t   _start;
129         nframes64_t   _end;
130         Flags         _flags;
131         bool          _locked;
132
133         void set_mark (bool yn);
134         bool set_flag_internal (bool yn, Flags flag);
135 };
136
137 class Locations : public PBD::StatefulDestructible
138 {
139   public:
140         typedef std::list<Location *> LocationList;
141
142         Locations ();
143         ~Locations ();
144
145         const LocationList& list() { return locations; }
146
147         void add (Location *, bool make_current = false);
148         void remove (Location *);
149         void clear ();
150         void clear_markers ();
151         void clear_ranges ();
152
153         XMLNode& get_state (void);
154         int set_state (const XMLNode&, int version);
155         Location *get_location_by_id(PBD::ID);
156
157         Location* auto_loop_location () const;
158         Location* auto_punch_location () const;
159         Location* session_range_location() const;
160
161         int next_available_name(std::string& result,std::string base);
162         uint32_t num_range_markers() const;
163
164         int set_current (Location *, bool want_lock = true);
165         Location *current () const { return current_location; }
166
167         Location *first_location_before (nframes64_t, bool include_special_ranges = false);
168         Location *first_location_after (nframes64_t, bool include_special_ranges = false);
169
170         void marks_either_side (nframes64_t const, nframes64_t &, nframes64_t &) const;
171
172         void find_all_between (nframes64_t start, nframes64_t, LocationList&, Location::Flags);
173
174         PBD::Signal1<void,Location*> current_changed;
175         PBD::Signal0<void>           changed;
176         PBD::Signal1<void,Location*> added;
177         PBD::Signal1<void,Location*> removed;
178         PBD::Signal1<void,const PBD::PropertyChange&>    StateChanged;
179
180         template<class T> void apply (T& obj, void (T::*method)(LocationList&)) {
181                 Glib::Mutex::Lock lm (lock);
182                 (obj.*method)(locations);
183         }
184
185         template<class T1, class T2> void apply (T1& obj, void (T1::*method)(LocationList&, T2& arg), T2& arg) {
186                 Glib::Mutex::Lock lm (lock);
187                 (obj.*method)(locations, arg);
188         }
189
190   private:
191
192         LocationList         locations;
193         Location            *current_location;
194         mutable Glib::Mutex  lock;
195
196         int set_current_unlocked (Location *);
197         void location_changed (Location*);
198 };
199
200 } // namespace ARDOUR
201
202 #endif /* __ardour_location_h__ */