20ccb7d61f23bf8b719d220336e9bf912ea7cb7f
[ardour.git] / gtk2_ardour / imageframe_time_axis_view.cc
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     $Id$
19 */
20
21 #include <algorithm>
22
23 #include <gtk--.h>
24
25 #include <gtkmmext/gtk_ui.h>
26
27 #include "imageframe_time_axis_view.h"
28 #include "imageframe_time_axis_group.h"
29 #include "imageframe_view.h"
30 #include "imageframe_time_axis.h"
31 #include "canvas-simplerect.h"
32 #include "region_selection.h"
33 #include "public_editor.h"
34 #include "rgb_macros.h"
35 #include "gui_thread.h"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR ;
40 using namespace Editing;
41
42 //---------------------------------------------------------------------------------------//
43 // Constructor / Desctructor
44                 
45 /**
46  * Constructs a new ImageFrameTimeAxisView.
47  *
48  * @param ifta the parent ImageFrameTimeAxis of this view helper
49  */
50 ImageFrameTimeAxisView::ImageFrameTimeAxisView (ImageFrameTimeAxis& tv)
51         : _trackview (tv)
52 {
53         region_color = _trackview.color() ;
54         stream_base_color = color_map[cImageTrackBase] ;
55
56         canvas_group = gtk_canvas_item_new (GTK_CANVAS_GROUP(_trackview.canvas_display), gtk_canvas_group_get_type (), 0) ;
57
58         canvas_rect = gtk_canvas_item_new (GTK_CANVAS_GROUP(canvas_group),
59                 gtk_canvas_simplerect_get_type(),
60                 "x1", 0.0,
61                 "y1", 0.0,
62                 "x2", 1000000.0,
63                 "y2", (double) tv.height,
64                 "outline_color_rgba", color_map[cImageTrackOutline],
65                 "fill_color_rgba", stream_base_color,
66                 0) ;
67
68         gtk_signal_connect(GTK_OBJECT(canvas_rect), "event", (GtkSignalFunc) PublicEditor::canvas_imageframe_view_event, &_trackview) ;
69
70         _samples_per_unit = _trackview.editor.get_current_zoom() ;
71
72         _trackview.editor.ZoomChanged.connect (slot (*this, &ImageFrameTimeAxisView::reset_samples_per_unit)) ;
73
74         selected_imageframe_group = 0 ;
75         selected_imageframe_view = 0 ;
76 }
77
78 /**
79  * Destructor 
80  * Responsible for destroying all items tat may have been added to this time axis
81  */
82 ImageFrameTimeAxisView::~ImageFrameTimeAxisView()
83 {
84         // Destroy all the ImageFrameGroups that we have
85         
86         for(ImageFrameGroupList::iterator iter = imageframe_groups.begin(); iter != imageframe_groups.end(); ++iter)
87         {
88                 ImageFrameTimeAxisGroup* iftag = (*iter) ;
89                 
90                 ImageFrameGroupList::iterator next = iter ;
91                 next++ ;
92
93                 // remove the front element
94                 imageframe_groups.erase(iter) ;
95
96                 delete iftag ;
97                 iftag = 0 ;
98                 
99                 iter = next ;
100         }
101         
102         // Destroy all our canvas components
103         if(canvas_rect)
104         {
105                 gtk_object_destroy(GTK_OBJECT(canvas_rect)) ;
106                 canvas_rect = 0 ;
107         }
108         
109         if(canvas_group)
110         {
111                 gtk_object_destroy(GTK_OBJECT(canvas_group));
112                 canvas_group = 0 ;
113         }
114 }
115
116
117 //---------------------------------------------------------------------------------------//
118 // ui methods & data
119         
120 /**
121  * Sets the height of the time axis view and the item upon it
122  *
123  * @param height the new height
124  */
125 int
126 ImageFrameTimeAxisView::set_height (gdouble h)
127 {
128         /* limit the values to something sane-ish */
129         if (h < 10.0 || h > 1000.0)
130         {
131                 return(-1) ;
132         }
133         
134         if(canvas_rect != 0)
135         {
136                 gtk_object_set(GTK_OBJECT(canvas_rect), "y2", h, NULL) ;
137         }
138
139         for(ImageFrameGroupList::const_iterator citer = imageframe_groups.begin(); citer != imageframe_groups.end(); ++citer)
140         {
141                 (*citer)->set_item_heights(h) ;
142         }
143
144         return(0) ;
145 }
146         
147 /**
148  * Sets the position of this view helper on the canvas
149  *
150  * @param x the x position upon the canvas
151  * @param y the y position npon the canvas
152  */
153 int
154 ImageFrameTimeAxisView::set_position (gdouble x, gdouble y)
155
156 {
157         gtk_canvas_item_set (canvas_group, "x", x, "y", y, NULL);
158         return 0;
159 }
160
161 /**
162  * Sets the current samples per unit.
163  * this method tells each item upon the time axis of the change
164  * 
165  * @param spu the new samples per canvas unit value
166  */
167 int 
168 ImageFrameTimeAxisView::set_samples_per_unit (gdouble spp)
169 {
170         if (spp < 1.0) {
171                 return(-1) ;
172         }
173
174         _samples_per_unit = spp;
175         
176         for(ImageFrameGroupList::const_iterator citer = imageframe_groups.begin(); citer != imageframe_groups.end(); ++citer)
177         {
178                 (*citer)->set_item_samples_per_units(spp) ;
179         }
180         
181         return(0) ;
182 }
183
184 /**
185  * Sets the color of the items contained uopn this view helper
186  *
187  * @param color the new base color
188  */
189 void
190 ImageFrameTimeAxisView::apply_color(GdkColor& color)
191 {
192         region_color = color ;
193         for(ImageFrameGroupList::const_iterator citer = imageframe_groups.begin(); citer != imageframe_groups.end(); citer++)
194         {
195                 (*citer)->apply_item_color(region_color) ;
196         }
197 }
198
199
200 /**
201  * convenience method to re-get the samples per unit and tell items upon this view
202  *
203  */
204 void
205 ImageFrameTimeAxisView::reset_samples_per_unit ()
206 {
207         set_samples_per_unit (_trackview.editor.get_current_zoom());
208 }
209
210
211 //---------------------------------------------------------------------------------------//
212 // Child ImageFrameTimeAxisGroup Accessors/Mutators
213                 
214 /**
215  * Adds an ImageFrameTimeAxisGroup to the list of items upon this time axis view helper
216  * the new ImageFrameTimeAxisGroup is returned
217  *
218  * @param group_id the unique id of the new group
219  * @param src the identity of the object that initiated the change
220  */
221 ImageFrameTimeAxisGroup*
222 ImageFrameTimeAxisView::add_imageframe_group(std::string group_id, void* src)
223 {
224         ImageFrameTimeAxisGroup* iftag = 0 ;
225         
226         //check that there is not already a group with that id
227         if(get_named_imageframe_group(group_id) != 0)
228         {
229                 // iftag = 0 ;
230         }
231         else
232         {
233                 iftag = new ImageFrameTimeAxisGroup(*this, group_id) ;
234
235                 imageframe_groups.push_front(iftag) ;
236         
237                 iftag->GoingAway.connect(bind(slot (*this,&ImageFrameTimeAxisView::remove_imageframe_group), iftag, (void*)this)) ;
238         
239                  ImageFrameGroupAdded(iftag, src) ; /* EMIT_SIGNAL */
240         }
241
242         return(iftag) ;
243 }
244
245 /**
246  * Returns the named ImageFrameTimeAxisGroup or 0 if the named group does not exist on this view helper
247  *
248  * @param group_id the unique id of the group to search for
249  * @return the named ImageFrameTimeAxisGroup, or 0 if it is not held upon this view
250  */
251 ImageFrameTimeAxisGroup*
252 ImageFrameTimeAxisView::get_named_imageframe_group(std::string group_id)
253 {
254         ImageFrameTimeAxisGroup* iftag =  0 ;
255         
256         for(ImageFrameGroupList::iterator i = imageframe_groups.begin(); i != imageframe_groups.end(); ++i)
257         {
258                 if (((ImageFrameTimeAxisGroup*)*i)->get_group_name() == group_id)
259                 {
260                         iftag = ((ImageFrameTimeAxisGroup*)*i) ;
261                         break ;
262                 }
263         }
264         
265         return(iftag) ;
266 }
267
268
269 /**
270  * Removes and returns the named ImageFrameTimeAxisGroup from the list of ImageFrameTimeAxisGroup held by this view helper
271  *
272  * @param group_id the ImageFrameTimeAxisGroup unique id to remove
273  * @param src the identity of the object that initiated the change
274  * @see add_imageframe_group
275  */
276 ImageFrameTimeAxisGroup*
277 ImageFrameTimeAxisView::remove_named_imageframe_group(std::string group_id, void* src)
278 {
279         ImageFrameTimeAxisGroup* removed = 0 ;
280         
281         for(ImageFrameGroupList::iterator iter = imageframe_groups.begin(); iter != imageframe_groups.end(); ++iter)
282         {
283                 if(((ImageFrameTimeAxisGroup*)*iter)->get_group_name() == group_id)
284                 {
285                         removed = (*iter) ;
286                         imageframe_groups.erase(iter) ;
287                         
288                         if(removed == selected_imageframe_group)
289                         {
290                                 selected_imageframe_group = 0 ;
291                         }
292                         
293                          ImageFrameGroupRemoved(removed->get_group_name(), src) ; /* EMIT_SIGNAL */
294                         
295                         // break from the for loop
296                         break ;
297                 }
298                 iter++ ;
299         }
300         
301         return(removed) ;
302 }
303
304
305 /**
306  * Removes the specified ImageFrameTimeAxisGroup from the list of ImageFrameTimeAxisGroups upon this TimeAxis.
307  *
308  * @param iftag the ImageFrameView to remove
309  */
310 void
311 ImageFrameTimeAxisView::remove_imageframe_group(ImageFrameTimeAxisGroup* iftag, void* src)
312 {
313         ENSURE_GUI_THREAD(bind (slot (*this, &ImageFrameTimeAxisView::remove_imageframe_group), iftag, src));
314
315         ImageFrameGroupList::iterator i;
316         if((i = find (imageframe_groups.begin(), imageframe_groups.end(), iftag)) != imageframe_groups.end())
317         {
318                 imageframe_groups.erase(i) ;
319                 
320                  ImageFrameGroupRemoved(iftag->get_group_name(), src) ; /* EMIT_SIGNAL */
321         }
322 }
323
324
325
326
327 //---------------------------------------------------------------------------------------//
328 // Selected group methods
329                 
330 /**
331  * Sets the currently selected group upon this time axis
332  *
333  * @param ifv the item to set selected
334  */
335 void
336 ImageFrameTimeAxisView::set_selected_imageframe_group(ImageFrameTimeAxisGroup* iftag)
337 {
338         if(selected_imageframe_group)
339         {
340                 selected_imageframe_group->set_selected(false) ;
341         }
342         
343         selected_imageframe_group = iftag ;
344         selected_imageframe_group->set_selected(true) ;
345 }
346
347 /**
348  * Clears the currently selected image frame group unpo this time axis
349  *
350 */
351 void
352 ImageFrameTimeAxisView::clear_selected_imageframe_group()
353 {
354         if(selected_imageframe_group)
355         {
356                 selected_imageframe_group->set_selected(false) ;
357         }
358         selected_imageframe_group = 0 ;
359 }
360                 
361 /**
362  * Returns the currently selected group upon this time axis
363  *
364  * @return the currently selected group upon this time axis
365  */
366 ImageFrameTimeAxisGroup*
367 ImageFrameTimeAxisView::get_selected_imageframe_group() const
368 {
369         return(selected_imageframe_group) ;
370 }
371
372 //---------------------------------------------------------------------------------------//
373 // Selected item methods
374                 
375 /**
376  * Sets the currently selected imag frame view item
377  *
378  * @param iftag the group the selected item is part
379  * @param ifv the selected item
380  */
381 void
382 ImageFrameTimeAxisView::set_selected_imageframe_view(ImageFrameTimeAxisGroup* iftag, ImageFrameView* ifv)
383 {
384         set_selected_imageframe_group(iftag) ;
385         
386         if(selected_imageframe_view)
387         {
388                 selected_imageframe_view->set_selected(false, this) ;
389         }
390         
391         selected_imageframe_view = ifv ;
392         selected_imageframe_view->set_selected(true, this) ;
393 }
394
395 /**
396  * Clears the currently selected image frame view item
397  *
398  */
399 void
400 ImageFrameTimeAxisView::clear_selected_imageframe_item(bool clear_group)
401 {
402         if(clear_group)
403         {
404                 clear_selected_imageframe_group() ;
405         }
406         
407         if(selected_imageframe_view)
408         {
409                 selected_imageframe_view->set_selected(false, this) ;
410         }
411         selected_imageframe_view = 0 ;
412 }
413                 
414 /**
415  * Returns the currently selected image frame view item upon this time axis
416  *
417  * @return the currently selected image frame view item
418  */
419 ImageFrameView*
420 ImageFrameTimeAxisView::get_selected_imageframe_view() const
421 {
422         return(selected_imageframe_view) ;
423 }
424
425
426
427  
428 void
429 ImageFrameTimeAxisView::set_imageframe_duration_sec(double sec)
430 {
431         if(selected_imageframe_group && selected_imageframe_view)
432         {
433                 selected_imageframe_view->set_duration((jack_nframes_t) (sec * _trackview.editor.current_session()->frame_rate()), this) ;
434         }
435 }
436
437
438
439 /**
440  * Removes the currently selected ImageFrame view item
441  *
442  * @param src the identity of the object that initiated the change
443  * @see add_imageframe_group
444  */
445 void
446 ImageFrameTimeAxisView::remove_selected_imageframe_item(void* src)
447 {
448         if(selected_imageframe_group && selected_imageframe_view)
449         {
450                 ImageFrameView* temp_item = selected_imageframe_view ;
451                 selected_imageframe_group->remove_imageframe_item(temp_item, src) ;
452                 
453                 // XXX although we have removed the item from the group, we need the group id still set within the
454                 //     item as the remove method requires this data when telling others about the deletion
455                 //     to fully specify the item we need the track, group and item id
456                 selected_imageframe_view->remove_this_item(src) ;
457                 clear_selected_imageframe_item(false) ;
458         }
459 }
460