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