merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[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         
67         Location () {
68                 _start = 0;
69                 _end = 0;
70                 _flags = Flags (0);
71         }
72
73         Location (const Location& other);
74         Location (const XMLNode&);
75         Location* operator= (const Location& other);
76
77         nframes_t start() { return _start; }
78         nframes_t end() { return _end; }
79         nframes_t length() { return _end - _start; }
80
81         int set_start (nframes_t s);
82         int set_end (nframes_t e);
83         int set (nframes_t start, nframes_t end);
84
85         const string& name() { return _name; }
86         void set_name (const string &str) { _name = str; name_changed(this); }
87
88         void set_auto_punch (bool yn, void *src);
89         void set_auto_loop (bool yn, void *src);
90         void set_hidden (bool yn, void *src);
91         void set_cd (bool yn, void *src);
92         void set_is_end (bool yn, void* src);
93         void set_is_start (bool yn, void* src);
94
95         bool is_auto_punch ()  { return _flags & IsAutoPunch; }
96         bool is_auto_loop () { return _flags & IsAutoLoop; }
97         bool is_mark () { return _flags & IsMark; }
98         bool is_hidden () { return _flags & IsHidden; }
99         bool is_cd_marker () { return _flags & IsCDMarker; }
100         bool is_end() { return _flags & IsEnd; }
101         bool is_start() { return _flags & IsStart; }
102         bool is_range_marker() { return _flags & IsRangeMarker; }
103
104         sigc::signal<void,Location*> name_changed;
105         sigc::signal<void,Location*> end_changed;
106         sigc::signal<void,Location*> start_changed;
107
108         sigc::signal<void,Location*,void*> FlagsChanged;
109
110         /* this is sent only when both start&end change at the same time */
111
112         sigc::signal<void,Location*> changed;
113    
114         /* CD Track / CD-Text info */
115
116         std::map<string, string> cd_info;
117         XMLNode& cd_info_node (const string &, const string &);
118
119         XMLNode& get_state (void);
120         int set_state (const XMLNode&);
121
122   private:
123         string        _name;
124         nframes_t     _start;
125         nframes_t     _end;
126         Flags         _flags;
127
128         void set_mark (bool yn);
129         bool set_flag_internal (bool yn, Flags flag);
130 };
131
132 class Locations : public PBD::StatefulDestructible
133 {
134   public:
135         typedef std::list<Location *> LocationList;
136
137         Locations ();
138         ~Locations ();
139
140         void add (Location *, bool make_current = false);
141         void remove (Location *);
142         void clear ();
143         void clear_markers ();
144         void clear_ranges ();
145
146         XMLNode& get_state (void);
147         int set_state (const XMLNode&);
148         Location *get_location_by_id(PBD::ID);
149
150         Location* auto_loop_location () const;
151         Location* auto_punch_location () const;
152         Location* end_location() const;
153         Location* start_location() const;
154
155         int next_available_name(string& result,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 (nframes_t, bool include_special_ranges = false);
162         Location *first_location_after (nframes_t, bool include_special_ranges = false);
163
164         nframes_t first_mark_before (nframes_t, bool include_special_ranges = false);
165         nframes_t first_mark_after (nframes_t, bool include_special_ranges = false);
166
167         sigc::signal<void,Location*> current_changed;
168         sigc::signal<void>           changed;
169         sigc::signal<void,Location*> added;
170         sigc::signal<void,Location*> removed;
171         sigc::signal<void,Change>    StateChanged;
172
173         template<class T> void apply (T& obj, void (T::*method)(LocationList&)) {
174                 Glib::Mutex::Lock lm (lock);
175                 (obj.*method)(locations);
176         }
177
178         template<class T1, class T2> void apply (T1& obj, void (T1::*method)(LocationList&, T2& arg), T2& arg) {
179                 Glib::Mutex::Lock lm (lock);
180                 (obj.*method)(locations, arg);
181         }
182
183   private:
184
185         LocationList       locations;
186         Location          *current_location;
187         mutable Glib::Mutex  lock;
188
189         int set_current_unlocked (Location *);
190         void location_changed (Location*);
191 };
192
193 } // namespace ARDOUR
194
195 #endif /* __ardour_location_h__ */