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