Merge branch 'master' into cairocanvas
[ardour.git] / gtk2_ardour / imageframe_time_axis_group.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 #include <gtkmm2ext/gtk_ui.h>
24
25 #include "imageframe_time_axis_group.h"
26 #include "imageframe_time_axis_view.h"
27 #include "imageframe_view.h"
28 #include "imageframe_time_axis.h"
29 #include "region_selection.h"
30 #include "public_editor.h"
31 #include "gui_thread.h"
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36
37 PBD::Signal1<void,ImageFrameTimeAxisGroup*> ImageFrameTimeAxisGroup::CatchDeletion;
38
39 //---------------------------------------------------------------------------------------//
40 // Constructor / Desctructor
41
42 /**
43  * Constructs a new ImageFrameTimeAxisGroup.
44  *
45  * @param iftav the parent ImageFrameTimeAxis of this view helper
46  * @param group_id the unique name/id of this group
47  */
48 ImageFrameTimeAxisGroup::ImageFrameTimeAxisGroup(ImageFrameTimeAxisView& iftav, const string & group_id)
49         : _view_helper(iftav), _group_id(group_id)
50 {
51         selected_imageframe_item = 0;
52         is_selected = false;
53
54         ImageFrameView::CatchDeletion.connect (*this, boost::bind (&ImageFrameTimeAxisGroup::remove_imageframe_item, this, _1), gui_context());
55 }
56
57 /**
58  * Destructor
59  * Responsible for destroying any Items that may have been added to this group
60  *
61  */
62 ImageFrameTimeAxisGroup::~ImageFrameTimeAxisGroup()
63 {
64         // Destroy all the ImageFramViews that we have
65         for(ImageFrameViewList::iterator iter = imageframe_views.begin(); iter != imageframe_views.end(); ++iter)
66         {
67                 ImageFrameView* ifv = *iter;
68
69                 ImageFrameViewList::iterator next = iter;
70                 next++;
71
72                 imageframe_views.erase(iter);
73
74                 delete ifv;
75                 ifv = 0;
76
77                 iter = next;
78         }
79
80          CatchDeletion; /* EMIT_SIGNAL */
81 }
82
83
84 //---------------------------------------------------------------------------------------//
85 // Name/Id Accessors/Mutators
86
87 /**
88  * Set the name/Id of this group.
89  *
90  * @param new_name the new name of this group
91  * @param src the identity of the object that initiated the change
92  */
93 void
94 ImageFrameTimeAxisGroup::set_group_name(const string & new_name, void* src)
95 {
96         if(_group_id != new_name)
97         {
98                 std::string temp_name = _group_id;
99                 _group_id = new_name;
100                  NameChanged(_group_id, temp_name, src); /* EMIT_SIGNAL */
101         }
102 }
103
104 /**
105  * Returns the id of this group
106  * The group id must be unique upon a time axis
107  *
108  * @return the id of this group
109  */
110 std::string
111 ImageFrameTimeAxisGroup::get_group_name() const
112 {
113         return(_group_id);
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 ImageFrameTimeAxisGroup::set_item_heights(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         // set the heights of all the imaeg frame views within the group
135         for(ImageFrameViewList::const_iterator citer = imageframe_views.begin(); citer != imageframe_views.end(); ++citer)
136         {
137                 (*citer)->set_height(h);
138         }
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 ImageFrameTimeAxisGroup::set_item_frames_per_pixel (double fpp)
151 {
152         if (fpp < 1.0) {
153                 return -1;
154         }
155
156         for (ImageFrameViewList::const_iterator citer = imageframe_views.begin(); citer != imageframe_views.end(); ++citer) {
157                 (*citer)->set_frames_per_pixel (fpp);
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 ImageFrameTimeAxisGroup::apply_item_color(Gdk::Color& color)
170 {
171         region_color = color;
172         for(ImageFrameViewList::const_iterator citer = imageframe_views.begin(); citer != imageframe_views.end(); citer++)
173         {
174                 (*citer)->set_color (region_color);
175         }
176 }
177
178
179
180 //---------------------------------------------------------------------------------------//
181 // child ImageFrameView methods
182
183 /**
184  * Adds an ImageFrameView to the list of items upon this time axis view helper
185  * the new ImageFrameView is returned
186  *
187  * @param item_id the unique id of the new item
188  * @param image_id the id/name of the image data we are usin
189  * @param start the position the new item should be placed upon the time line
190  * @param duration the duration the new item should be placed upon the timeline
191  * @param rgb_data the rgb data of the image
192  * @param width the original image width of the rgb_data (not the size to display)
193  * @param height the irigianl height of the rgb_data
194  * @param num_channels the number of channles within the rgb_data
195  * @param src the identity of the object that initiated the change
196  */
197 ImageFrameView*
198 ImageFrameTimeAxisGroup::add_imageframe_item(const string & frame_id, framepos_t start, framecnt_t duration, unsigned char* rgb_data, uint32_t width, uint32_t height, uint32_t num_channels, void* src)
199 {
200         ImageFrameView* ifv = 0;
201
202         //check that there is not already an imageframe with that id
203         if(get_named_imageframe_item(frame_id) == 0)
204         {
205                 ifv = new ImageFrameView(frame_id,
206                         _view_helper.canvas_item()->property_parent(),
207                         &(_view_helper.trackview()),
208                         this,
209                         _view_helper.trackview().editor.get_current_zoom(),
210                         region_color,
211                         start,
212                         duration,
213                         rgb_data,
214                         width,
215                         height,
216                         num_channels);
217
218                 imageframe_views.push_front(ifv);
219                 ImageFrameAdded(ifv, src); /* EMIT_SIGNAL */
220         }
221
222         return(ifv);
223 }
224
225
226 /**
227  * Returns the named ImageFrameView or 0 if the named view does not exist on this view helper
228  *
229  * @param item_id the unique id of the item to search for
230  * @return the named ImageFrameView, or 0 if it is not held upon this view
231  */
232 ImageFrameView*
233 ImageFrameTimeAxisGroup::get_named_imageframe_item(const string & frame_id)
234 {
235         ImageFrameView* ifv =  0;
236
237         for (ImageFrameViewList::const_iterator i = imageframe_views.begin(); i != imageframe_views.end(); ++i)
238         {
239                 if (((ImageFrameView*)*i)->get_item_name() == frame_id)
240                 {
241                         ifv = ((ImageFrameView*)*i);
242                         break;
243                 }
244         }
245         return(ifv);
246 }
247
248 /**
249  * Removes the currently selected ImageFrameView
250  *
251  * @param src the identity of the object that initiated the change
252  * @todo need to remoev this, the selected item within group is no longer
253  *       used in favour of a time axis selected item
254  * @see add_imageframe_view
255  */
256 void
257 ImageFrameTimeAxisGroup::remove_selected_imageframe_item(void* src)
258 {
259         std::string frame_id;
260
261         if(selected_imageframe_item)
262         {
263                 ImageFrameViewList::iterator i;
264
265                 if((i = find(imageframe_views.begin(), imageframe_views.end(), selected_imageframe_item)) != imageframe_views.end())
266                 {
267                         imageframe_views.erase(i);
268                         frame_id = selected_imageframe_item->get_item_name();
269
270                         // note that we delete the item here
271                         delete(selected_imageframe_item);
272                         selected_imageframe_item = 0;
273
274                         std::string track_id = _view_helper.trackview().name();
275                          ImageFrameRemoved(track_id, _group_id, frame_id, src); /* EMIT_SIGNAL */
276                 }
277         }
278         else
279         {
280                 //cerr << "No Selected ImageFrame" << endl;
281         }
282 }
283
284
285 /**
286  * Removes and returns the named ImageFrameView from the list of ImageFrameViews held by this view helper
287  *
288  * @param item_id the ImageFrameView unique id to remove
289  * @param src the identity of the object that initiated the change
290  * @see add_imageframe_view
291  */
292 ImageFrameView*
293 ImageFrameTimeAxisGroup::remove_named_imageframe_item(const string & frame_id, void* src)
294 {
295         ImageFrameView* removed = 0;
296
297         for(ImageFrameViewList::iterator iter = imageframe_views.begin(); iter != imageframe_views.end(); ++iter)
298         {
299                 ImageFrameView* tempItem = *iter;
300                 if(tempItem->get_item_name() == frame_id)
301                 {
302                         removed = tempItem;
303                         imageframe_views.erase(iter);
304
305                         if (removed == selected_imageframe_item)
306                         {
307                                 selected_imageframe_item = 0;
308                         }
309
310                         std::string track_id = _view_helper.trackview().name();
311                          ImageFrameRemoved(track_id, _group_id, frame_id, src); /* EMIT_SIGNAL */
312
313                         // break from the for loop
314                         break;
315                 }
316                 iter++;
317         }
318
319         return(removed);
320 }
321
322 /**
323  * Removes ifv from the list of ImageFrameViews upon this TimeAxis.
324  * if ifv is not upon this TimeAxis, this method takes no action
325  *
326  * @param ifv the ImageFrameView to remove
327  */
328 void
329 ImageFrameTimeAxisGroup::remove_imageframe_item (ImageFrameView* ifv)
330 {
331         ENSURE_GUI_THREAD (*this, &ImageFrameTimeAxisGroup::remove_imageframe_item, ifv, src)
332
333         ImageFrameViewList::iterator i;
334
335         if((i = find (imageframe_views.begin(), imageframe_views.end(), ifv)) != imageframe_views.end()) {
336                 imageframe_views.erase(i);
337
338                 std::string frame_id = ifv->get_item_name();
339                 std::string track_id = _view_helper.trackview().name();
340                  ImageFrameRemoved(track_id, _group_id, frame_id, src); /* EMIT_SIGNAL */
341         }
342 }
343
344 //---------------------------------------------------------------------------------------//
345 // Selected group methods
346
347 /**
348  * Sets the currently selected item upon this time axis
349  *
350  * @param ifv the item to set selected
351  */
352 //void
353 //ImageFrameTimeAxisGroup::set_selected_imageframe_item(ImageFrameView* ifv)
354 //{
355 //      if(selected_imageframe_item)
356 //      {
357 //              selected_imageframe_item->set_selected(false, this);
358 //      }
359 //
360 //      selected_imageframe_item = ifv;
361 //
362 //      if(!ifv->get_selected())
363 //      {
364 //              selected_imageframe_item->set_selected(true, this);
365 //      }
366 //}
367
368
369 /**
370  * Sets the currently selected item upon this time axis to the named item
371  *
372  * @param item_id the name/id of the item to set selected
373  */
374 //void
375 //ImageFrameTimeAxisGroup::set_selected_imageframe_item(std::string frame_id)
376 //{
377 //      selected_imageframe_item = get_named_imageframe_item(frame_id);
378 //}
379
380
381 /**
382  * Returns the currently selected item upon this time axis
383  *
384  * @return the currently selected item pon this time axis
385  */
386 // ImageFrameView*
387 // ImageFrameTimeAxisGroup::get_selected_imageframe_item()
388 // {
389         // return(selected_imageframe_item);
390 // }
391
392
393
394 /**
395  * Returns whether this grou pis currently selected
396  *
397  * @returns true if this group is currently selected
398  */
399 bool
400 ImageFrameTimeAxisGroup::get_selected() const
401 {
402         return(is_selected);
403 }
404
405
406 /**
407  * Sets he selected state of this group
408  *
409  * @param yn set true if this group is selected, false otherwise
410  */
411 void
412 ImageFrameTimeAxisGroup::set_selected(bool yn)
413 {
414         is_selected = yn;
415 }
416
417
418
419 //---------------------------------------------------------------------------------------//
420 // Handle time axis removal
421
422 /**
423  * Handles the Removal of this VisualTimeAxis
424  * This _needs_ to be called to alert others of the removal properly, ie where the source
425  * of the removal came from.
426  *
427  * XXX Although im not too happy about this method of doing things, I cant think of a cleaner method
428  *     just now to capture the source of the removal
429  *
430  * @param src the identity of the object that initiated the change
431  */
432 void
433 ImageFrameTimeAxisGroup::remove_this_group(void* src)
434 {
435         /*
436            defer to idle loop, otherwise we'll delete this object
437            while we're still inside this function ...
438         */
439         Glib::signal_idle().connect(sigc::bind(ptr_fun(&ImageFrameTimeAxisGroup::idle_remove_this_group), this, src));
440 }
441
442 /**
443  * Callback used to remove this group during the gtk idle loop
444  * This is used to avoid deleting the obejct while inside the remove_this_group
445  * method
446  *
447  * @param group the ImageFrameTimeAxisGroup to remove
448  * @param src the identity of the object that initiated the change
449  */
450 gint
451 ImageFrameTimeAxisGroup::idle_remove_this_group(ImageFrameTimeAxisGroup* group, void* src)
452 {
453         delete group;
454         group = 0;
455          group->GroupRemoved(group->get_group_name(), src); /* EMIT_SIGNAL */
456         return(false);
457 }
458