r229@gwythaint (orig r769): fugalh | 2006-08-09 08:15:05 -0600
[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     $Id$
19 */
20
21 #ifndef __ardour_location_h__
22 #define __ardour_location_h__
23
24 #include <string>
25 #include <list>
26 #include <iostream>
27 #include <map>
28
29 #include <sys/types.h>
30 #include <sigc++/signal.h>
31
32 #include <glibmm/thread.h>
33
34 #include <pbd/undo.h>
35 #include <pbd/stateful.h> 
36
37 #include <ardour/ardour.h>
38 #include <ardour/state_manager.h>
39
40 using std::string;
41
42 namespace ARDOUR {
43
44 class Location : public Stateful, public sigc::trackable
45 {
46   public:
47         enum Flags {
48                 IsMark = 0x1,
49                 IsAutoPunch = 0x2,
50                 IsAutoLoop = 0x4,
51                 IsHidden = 0x8,
52                 IsCDMarker = 0x10,
53                 IsEnd = 0x20,
54                 IsRangeMarker = 0x40,
55                 IsStart = 0x80
56         };
57
58         Location (jack_nframes_t sample_start,
59                   jack_nframes_t sample_end,
60                   const string &name,
61                   Flags bits = Flags(0))                
62                 
63                 : _name (name),
64                 _start (sample_start),
65                 _end (sample_end),
66                 _flags (bits) { }
67         
68         Location () {
69                 _start = 0;
70                 _end = 0;
71                 _flags = 0;     
72         }
73
74         Location (const Location& other);
75         Location* operator= (const Location& other);
76
77         jack_nframes_t start() { return _start; }
78         jack_nframes_t end() { return _end; }
79         jack_nframes_t length() { return _end - _start; }
80
81         int set_start (jack_nframes_t s);
82         int set_end (jack_nframes_t e);
83         int set (jack_nframes_t start, jack_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         PBD::ID id() { return _id; }
123
124   private:
125         PBD::ID _id;
126         string        _name;
127         jack_nframes_t     _start;
128         jack_nframes_t     _end;
129         uint32_t _flags;
130
131         void set_mark (bool yn);
132         bool set_flag_internal (bool yn, Flags flag);
133 };
134
135 class Locations : public Stateful, public StateManager
136 {
137   public:
138         typedef std::list<Location *> LocationList;
139
140         Locations ();
141         ~Locations ();
142
143         void add (Location *, bool make_current = false);
144         void remove (Location *);
145         void clear ();
146         void clear_markers ();
147         void clear_ranges ();
148
149         XMLNode& get_state (void);
150         int set_state (const XMLNode&);
151         PBD::ID id() { return _id; }
152
153         Location* auto_loop_location () const;
154         Location* auto_punch_location () const;
155         Location* end_location() const;
156         Location* start_location() const;
157
158         uint32_t num_range_markers() const;
159
160         int set_current (Location *, bool want_lock = true);
161         Location *current () const { return current_location; }
162
163         Location *first_location_before (jack_nframes_t);
164         Location *first_location_after (jack_nframes_t);
165
166         jack_nframes_t first_mark_before (jack_nframes_t);
167         jack_nframes_t first_mark_after (jack_nframes_t);
168
169         sigc::signal<void,Location*> current_changed;
170         sigc::signal<void>           changed;
171         sigc::signal<void,Location*> added;
172         sigc::signal<void,Location*> removed;
173
174         template<class T> void apply (T& obj, void (T::*method)(LocationList&)) {
175                 Glib::Mutex::Lock lm (lock);
176                 (obj.*method)(locations);
177         }
178
179         template<class T1, class T2> void apply (T1& obj, void (T1::*method)(LocationList&, T2& arg), T2& arg) {
180                 Glib::Mutex::Lock lm (lock);
181                 (obj.*method)(locations, arg);
182         }
183
184         UndoAction get_memento () const;
185
186   private:
187
188         struct State : public ARDOUR::StateManager::State {
189             LocationList locations;
190             LocationList states;
191
192             State (std::string why) : ARDOUR::StateManager::State (why) {}
193         };
194
195         LocationList       locations;
196         Location          *current_location;
197         mutable Glib::Mutex  lock;
198
199         int set_current_unlocked (Location *);
200         void location_changed (Location*);
201
202         Change   restore_state (StateManager::State&);
203         StateManager::State* state_factory (std::string why) const;
204
205         PBD::ID _id;
206 };
207
208 } // namespace ARDOUR
209
210 #endif /* __ardour_location_h__ */