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