merge from 2.0-ongoing @ 3581
[ardour.git] / gtk2_ardour / time_axis_view_item.h
1 /*
2     Copyright (C) 2003 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 __gtk_ardour_time_axis_view_item_h__
21 #define __gtk_ardour_time_axis_view_item_h__
22
23 #include <jack/jack.h>
24 #include <string>
25
26 #include <libgnomecanvasmm/text.h>
27
28 #include "selectable.h"
29 #include "simplerect.h"
30 #include "canvas.h"
31
32 class TimeAxisView;
33
34 /**
35  * A base class for 'items' that may appear upon a TimeAxisView
36  *
37  */
38 class TimeAxisViewItem : public Selectable
39 {
40    public:
41        virtual ~TimeAxisViewItem() ;
42     
43     /**
44      * Set the position of this item upon the timeline to the specified value
45      *
46      * @param pos the new position
47      * @param src the identity of the object that initiated the change
48      * @return true if the position change was a success, false otherwise
49      */
50     virtual bool set_position(nframes_t pos, void* src, double* delta = 0) ;
51     
52     /**
53      * Return the position of this item upon the timeline
54      *
55      * @return the position of this item
56      */
57     nframes_t get_position() const ; 
58     
59     /**
60      * Sets the duration of this item
61      *
62      * @param dur the new duration of this item
63      * @param src the identity of the object that initiated the change
64      * @return true if the duration change was succesful, false otherwise
65      */
66     virtual bool set_duration(nframes_t dur, void* src) ;
67     
68     /**
69      * Returns the duration of this item
70      *
71      */
72     nframes_t get_duration() const ;
73     
74     /**
75      * Sets the maximum duration that this item make have.
76      *
77      * @param dur the new maximum duration
78      * @param src the identity of the object that initiated the change
79      */
80     virtual void set_max_duration(nframes_t dur, void* src) ;
81     
82     /**
83      * Returns the maxmimum duration that this item may be set to
84      *
85      * @return the maximum duration that this item may be set to
86      */
87     nframes_t get_max_duration() const ;
88     
89     /**
90      * Sets the minimu duration that this item may be set to
91      *
92      * @param the minimum duration that this item may be set to
93      * @param src the identity of the object that initiated the change
94      */
95     virtual void set_min_duration(nframes_t dur, void* src) ;
96     
97     /**
98      * Returns the minimum duration that this item mey be set to
99      *
100      * @return the nimum duration that this item mey be set to
101      */
102     nframes_t get_min_duration() const ;
103     
104     /**
105      * Sets whether the position of this Item is locked to its current position
106      * Locked items cannot be moved until the item is unlocked again.
107      *
108      * @param yn set to true to lock this item to its current position
109      * @param src the identity of the object that initiated the change
110      */
111     virtual void set_position_locked(bool yn, void* src) ;
112     
113     /**
114      * Returns whether this item is locked to its current position
115      *
116      * @return true if this item is locked to its current posotion
117      *         false otherwise
118      */
119     bool get_position_locked() const ;
120     
121     /**
122      * Sets whether the Maximum Duration constraint is active and should be enforced
123      *
124      * @param active set true to enforce the max duration constraint
125      * @param src the identity of the object that initiated the change
126      */
127     void set_max_duration_active(bool active, void* src) ;
128     
129     /**
130      * Returns whether the Maximum Duration constraint is active and should be enforced
131      *
132      * @return true if the maximum duration constraint is active, false otherwise
133      */
134     bool get_max_duration_active() const ;
135     
136     /**
137      * Sets whether the Minimum Duration constraint is active and should be enforced
138      *
139      * @param active set true to enforce the min duration constraint
140      * @param src the identity of the object that initiated the change
141      */
142     void set_min_duration_active(bool active, void* src) ;
143     
144     /**
145      * Returns whether the Maximum Duration constraint is active and should be enforced
146      *
147      * @return true if the maximum duration constraint is active, false otherwise
148      */
149     bool get_min_duration_active() const ;
150     
151     /**
152      * Set the name/Id of this item.
153      *
154      * @param new_name the new name of this item
155      * @param src the identity of the object that initiated the change
156      */
157     void set_item_name(std::string new_name, void* src) ;
158     
159     /**
160      * Returns the name/id of this item
161      *
162      * @return the name/id of this item
163      */
164     virtual std::string get_item_name() const ;
165     
166     /**
167      * Set to true to indicate that this item is currently selected
168      *
169      * @param yn true if this item is currently selected
170      */
171     virtual void set_selected(bool yn) ;
172
173     /**
174      * Set to true to indicate that this item should show its selection status
175      *
176      * @param yn true if this item should show its selected status
177      */
178     virtual void set_should_show_selection (bool yn) ;
179
180     void set_sensitive (bool yn) { _sensitive = yn; }
181     bool sensitive () const { return _sensitive; }
182     
183     //---------------------------------------------------------------------------------------//
184     // Parent Component Methods
185     
186     /**
187      * Returns the TimeAxisView that this item is upon
188      *
189      * @return the timeAxisView that this item is placed upon
190      */
191     TimeAxisView& get_time_axis_view() ;
192     
193     //---------------------------------------------------------------------------------------//
194     // ui methods & data
195     
196     /**
197      * Sets the displayed item text
198      * This item is the visual text name displayed on the canvas item, this can be different to the name of the item
199      *
200      * @param new_name the new name text to display
201      */
202     void set_name_text(const Glib::ustring& new_name) ;
203     
204     void    set_y_position_and_height (double, double);    
205
206     /**
207      * 
208      */
209     void set_color(Gdk::Color& color) ;
210     
211     /**
212      * 
213      */
214     ArdourCanvas::Item* get_canvas_frame() ;
215
216     /**
217      * 
218      */
219     ArdourCanvas::Item* get_canvas_group();
220
221     /**
222      * 
223      */
224     ArdourCanvas::Item* get_name_highlight();
225
226     /**
227      * 
228      */
229     ArdourCanvas::Text* get_name_text();
230
231
232     /**
233      * Returns the time axis that this item is upon
234      */
235     TimeAxisView& get_trackview() const { return trackview; }
236
237     /**
238      * Sets the samples per unit of this item.
239      * this item is used to determine the relative visual size and position of this item
240      * based upon its duration and start value.
241      *
242      * @param spu the new samples per unit value
243      */
244     virtual void set_samples_per_unit(double spu) ;
245     
246     /**
247      * Returns the current samples per unit of this item
248      *
249      * @return the samples per unit of this item
250      */
251     double get_samples_per_unit() ;
252
253     virtual void raise () { return; }
254     virtual void raise_to_top () { return; }
255     virtual void lower () { return; }
256     virtual void lower_to_bottom () { return; }
257     
258     /**
259      * returns true if the name area should respond to events.
260      */
261     bool name_active() const { return name_connected; }
262
263     // Default sizes, font and spacing
264     static Pango::FontDescription* NAME_FONT ;
265     static bool have_name_font;
266     static const double NAME_X_OFFSET ;
267     static const double GRAB_HANDLE_LENGTH ;
268     /* these are not constant, but vary with the pixel size
269        of the font used to display the item name.
270     */
271     static double NAME_Y_OFFSET ;
272     static double NAME_HIGHLIGHT_SIZE ;
273     static double NAME_HIGHLIGHT_THRESH ;
274
275     /**
276      * Handles the Removal of this time axis item
277      * This _needs_ to be called to alert others of the removal properly, ie where the source
278      * of the removal came from.
279      *
280      * XXX Although im not too happy about this method of doing things, I cant think of a cleaner method
281      *     just now to capture the source of the removal
282      *
283      * @param src the identity of the object that initiated the change
284      */
285     virtual void remove_this_item(void* src) ;
286     
287     /**
288      * Emitted when this Group has been removed
289      * This is different to the GoingAway signal in that this signal
290      * is emitted during the deletion of this Time Axis, and not during
291      * the destructor, this allows us to capture the source of the deletion
292      * event
293      */
294
295     sigc::signal<void,std::string,void*> ItemRemoved ;
296     
297     /** Emitted when the name/Id of this item is changed */
298     sigc::signal<void,std::string,std::string,void*> NameChanged ;
299     
300     /** Emiited when the position of this item changes */
301     sigc::signal<void,nframes_t,void*> PositionChanged ;
302     
303     /** Emitted when the position lock of this item is changed */
304     sigc::signal<void,bool,void*> PositionLockChanged ;
305     
306     /** Emitted when the duration of this item changes */
307     sigc::signal<void,nframes_t,void*> DurationChanged ;
308     
309     /** Emitted when the maximum item duration is changed */
310     sigc::signal<void,nframes_t,void*> MaxDurationChanged ;
311     
312     /** Emitted when the mionimum item duration is changed */
313     sigc::signal<void,nframes_t,void*> MinDurationChanged ;
314     
315     enum Visibility {
316             ShowFrame = 0x1,
317             ShowNameHighlight = 0x2,
318             ShowNameText = 0x4,
319             ShowHandles = 0x8,
320             HideFrameLeft = 0x10,
321             HideFrameRight = 0x20,
322             HideFrameTB = 0x40,
323             FullWidthNameHighlight = 0x80
324     };
325   protected:
326     /**
327      * Constructs a new TimeAxisViewItem.
328      *
329      * @param it_name the unique name/Id of this item
330      * @param parent the parent canvas group
331      * @param tv the TimeAxisView we are going to be added to
332      * @param spu samples per unit
333      * @param base_color
334      * @param start the start point of this item
335      * @param duration the duration of this item
336      */
337     TimeAxisViewItem(const std::string & it_name, ArdourCanvas::Group& parent, TimeAxisView& tv, double spu, Gdk::Color& base_color, 
338                      nframes_t start, nframes_t duration, bool recording = false, Visibility v = Visibility (0));
339
340     TimeAxisViewItem (const TimeAxisViewItem& other);
341
342     void init (const std::string& it_name, double spu, Gdk::Color& base_color, nframes_t start, nframes_t duration, Visibility vis);
343     
344     /**
345      * Calculates some contrasting color for displaying various parts of this item, based upon the base color
346      *
347      * @param color the base color of the item
348      */
349     virtual void compute_colors(Gdk::Color& color) ;
350     
351     /**
352      * convenience method to set the various canvas item colors
353      */
354     virtual void set_colors() ;
355     
356     /**
357      * Sets the frame color depending on whether this item is selected
358      */
359     virtual void set_frame_color() ;
360     
361     /**
362      * Sets the colors of the start and end trim handle depending on object state
363      *
364      */
365     void set_trim_handle_colors() ;
366
367     virtual void reset_width_dependent_items (double pixel_width);
368     void reset_name_width (double pixel_width);
369
370     /**
371      * Callback used to remove this item during the gtk idle loop
372      * This is used to avoid deleting the obejct while inside the remove_this_group
373      * method
374      *
375      * @param item the time axis item to remove
376      * @param src the identity of the object that initiated the change
377      */
378     static gint idle_remove_this_item(TimeAxisViewItem* item, void* src) ;
379     
380     /** The time axis that this item is upon */
381     TimeAxisView& trackview ;
382     
383     /** indicates whether this item is locked to its current position */
384     bool position_locked ;
385     
386     /** The posotion of this item on the timeline */
387     nframes_t frame_position ;
388     
389     /** the duration of this item upon the timeline */
390     nframes_t item_duration ;
391     
392     /** the maximum duration that we allow this item to take */
393     nframes_t max_item_duration ;
394     
395     /** the minimu duration that we allow this item to take */
396     nframes_t min_item_duration ;
397     
398     /** indicates whether this Max Duration constraint is active */
399     bool max_duration_active ;
400     
401     /** indicates whether this Min Duration constraint is active */
402     bool min_duration_active ;
403     
404     /** the curretn samples per canvas unit */
405     double samples_per_unit ;
406     
407     /** indicates if this item is currently selected */
408     bool selected ;
409
410     /** should the item show its selected status */
411     bool should_show_selection;
412
413     /** should the item respond to events */
414     bool _sensitive;
415     
416     /**
417      * The unique item name of this Item
418      * Each item upon a time axis must have a unique id
419      */
420     std::string item_name ;
421     
422     /**
423      * true if the name should respond to events
424      */
425     bool name_connected;
426
427     /**
428      * true if a small vestigial rect should be shown when the item gets very narrow
429      */
430
431     bool show_vestigial;
432
433     uint32_t fill_opacity;
434     uint32_t fill_color ;
435     uint32_t frame_color_r ;
436     uint32_t frame_color_g ;
437     uint32_t frame_color_b ;
438     uint32_t selected_frame_color_r ;
439     uint32_t selected_frame_color_g ;
440     uint32_t selected_frame_color_b ;
441     uint32_t label_color ;
442     
443     uint32_t handle_color_r ;
444     uint32_t handle_color_g ;
445     uint32_t handle_color_b ;
446     uint32_t lock_handle_color_r ;
447     uint32_t lock_handle_color_g ;
448     uint32_t lock_handle_color_b ;
449     
450     ArdourCanvas::Group*      group;
451     ArdourCanvas::SimpleRect* vestigial_frame;
452     ArdourCanvas::SimpleRect* frame;
453     ArdourCanvas::Text*       name_text;
454     ArdourCanvas::SimpleRect* name_highlight;
455     ArdourCanvas::SimpleRect* frame_handle_start;
456     ArdourCanvas::SimpleRect* frame_handle_end;
457
458     int name_text_width;
459     double last_name_text_width;
460
461     std::map<Glib::ustring::size_type,int> name_text_size_cache;
462     
463     Visibility visibility;
464         bool _recregion;
465
466
467 }; /* class TimeAxisViewItem */
468
469 #endif /* __gtk_ardour_time_axis_view_item_h__ */