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