c31255db84ff9e24d5f9116e9fa916f3159dc947
[ardour.git] / gtk2_ardour / imageframe_time_axis.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 <string>
22 #include <algorithm>
23
24 #include <pbd/error.h>
25
26 #include <gtkmm2ext/utils.h>
27 #include <gtkmm2ext/gtk_ui.h>
28
29 #include <ardour/session.h>
30 #include <ardour/utils.h>
31
32 #include "public_editor.h"
33 #include "imageframe_time_axis.h"
34 #include "canvas-simplerect.h"
35 #include "enums.h"
36 #include "imageframe_time_axis_view.h"
37 #include "imageframe_time_axis_group.h"
38 #include "marker_time_axis_view.h"
39 #include "imageframe_view.h"
40 #include "marker_time_axis.h"
41 #include "marker_view.h"
42 #include "gui_thread.h"
43
44 #include "i18n.h"
45
46 using namespace ARDOUR ;
47 using namespace sigc ;
48 using namespace Gtk ;
49
50 /**
51  * Constructs a new ImageFrameTimeAxis.
52  *
53  * @param track_id the track name/id
54  * @param ed the PublicEditor
55  * @param sess the current session
56  * @param canvas the parent canvas item
57  */
58 ImageFrameTimeAxis::ImageFrameTimeAxis(std::string track_id, PublicEditor& ed, ARDOUR::Session& sess, Widget *canvas)
59         : AxisView(sess),
60           VisualTimeAxis(track_id, ed, sess, canvas)
61 {
62         _color = unique_random_color() ;
63         
64         selection_group = gtk_canvas_item_new (GTK_CANVAS_GROUP(canvas_display), gtk_canvas_group_get_type (), NULL) ;
65         gtk_canvas_item_hide(selection_group) ;
66
67         // intialize our data items
68         _marked_for_display = true;
69         y_position = -1 ;
70         name_prompter = 0 ;
71
72         /* create our new image frame view */
73         view = new ImageFrameTimeAxisView(*this) ;
74         
75         /* create the Image Frame Edit Menu */
76         create_imageframe_menu() ;
77         
78         // set the initial time axis text label
79         label_view() ;
80                 
81         // set the initial height of this time axis
82         set_height(Normal) ;
83 }
84
85 /**
86  * Destructor
87  * Responsible for destroying any child image items that may have been added to thie time axis
88  */
89 ImageFrameTimeAxis::~ImageFrameTimeAxis ()
90 {
91          GoingAway() ; /* EMIT_SIGNAL */
92         
93         // Destroy all the marker views we may have associaited with this TimeAxis
94         for(MarkerTimeAxisList::iterator iter = marker_time_axis_list.begin(); iter != marker_time_axis_list.end(); ++iter)
95         {
96                 MarkerTimeAxis* mta = *iter ;
97                 MarkerTimeAxisList::iterator next = iter ;
98                 next++ ;
99                 
100                 marker_time_axis_list.erase(iter) ;
101
102                 delete mta ;
103                 mta = 0 ;
104                 
105                 iter = next ;
106         }
107         
108         if(image_action_menu)
109         {
110                 delete image_action_menu ;
111                 image_action_menu = 0 ;
112         }
113         
114         for(list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i)
115         {
116                 gtk_object_destroy (GTK_OBJECT((*i)->rect));
117                 gtk_object_destroy (GTK_OBJECT((*i)->start_trim));
118                 gtk_object_destroy (GTK_OBJECT((*i)->end_trim));
119         }
120
121         for(list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i)
122         {
123                 gtk_object_destroy (GTK_OBJECT((*i)->rect));
124                 gtk_object_destroy (GTK_OBJECT((*i)->start_trim));
125                 gtk_object_destroy (GTK_OBJECT((*i)->end_trim));
126         }
127         
128         if (selection_group)
129         {
130                 gtk_object_destroy (GTK_OBJECT (selection_group));
131                 selection_group = 0 ;
132         }
133         
134         // Destroy our Axis View helper
135         if(view)
136         {
137                 delete view ;
138                 view = 0 ;
139         }
140 }
141
142 //---------------------------------------------------------------------------------------//
143 // ui methods & data
144
145 /**
146  * Sets the height of this TrackView to one of ths TrackHeghts
147  *
148  * @param h the TrackHeight value to set
149  */
150 void
151 ImageFrameTimeAxis::set_height (TrackHeight h)
152 {
153         VisualTimeAxis::set_height(h) ;
154         
155         // tell out view helper of the change too
156         if(view != 0)
157         {
158                 view->set_height((double) height) ;
159         }
160         
161         // tell those interested that we have had our height changed
162          gui_changed("track_height",(void*)0); /* EMIT_SIGNAL */
163 }
164
165 /**
166  * Sets the number of samples per unit that are used.
167  * This is used to determine the siezes of items upon this time axis
168  *
169  * @param spu the number of samples per unit
170  */
171 void
172 ImageFrameTimeAxis::set_samples_per_unit(double spu)
173 {
174         TimeAxisView::set_samples_per_unit (editor.get_current_zoom());
175
176         if(view) {
177                 view->set_samples_per_unit(spu) ;
178         }
179 }
180
181
182 /**
183  * Returns the available height for images to be drawn onto
184  *
185  * @return the available height for an image item to be drawn onto
186  */
187 int
188 ImageFrameTimeAxis::get_image_display_height()
189 {
190         return(height - (gint)TimeAxisViewItem::NAME_HIGHLIGHT_SIZE) ;
191 }
192
193
194 /**
195  * Show the popup edit menu
196  *
197  * @param button the mouse button pressed
198  * @param time when to show the popup
199  * @param clicked_imageframe the ImageFrameItem that the event ocured upon, or 0 if none
200  * @param with_item true if an item has been selected upon the time axis, used to set context menu
201  */
202 void
203 ImageFrameTimeAxis::popup_imageframe_edit_menu(int button, int32_t time, ImageFrameView* clicked_imageframe, bool with_item)
204 {
205         if (!imageframe_menu)
206         {
207                 create_imageframe_menu() ;
208         }
209
210         if(with_item)
211         {
212                 imageframe_item_menu->set_sensitive(true) ;
213         }
214         else
215         {
216                 imageframe_item_menu->set_sensitive(false) ;
217         }
218         
219         imageframe_menu->popup(button,time) ;
220 }
221
222 /**
223  * convenience method to select a new track color and apply it to the view and view items
224  *
225  */
226 void
227 ImageFrameTimeAxis::select_track_color()
228 {
229         if (choose_time_axis_color())
230         {
231                 if (view)
232                 {
233                         view->apply_color (_color) ;
234                 }
235         }
236 }
237
238 /**
239  * Handles the building of the popup menu
240  */
241 void
242 ImageFrameTimeAxis::build_display_menu()
243 {
244         using namespace Menu_Helpers;
245
246         /* get the size menu ready */
247
248         build_size_menu();
249
250         /* prepare it */
251
252         TimeAxisView::build_display_menu () ;
253
254         /* now fill it with our stuff */
255
256         MenuList& items = display_menu->items();
257
258         items.push_back (MenuElem (_("Rename"), mem_fun(*this, &ImageFrameTimeAxis::start_time_axis_rename)));
259
260         image_action_menu = new Menu() ;
261         image_action_menu->set_name ("ArdourContextMenu");
262         MenuList image_items = image_action_menu->items() ;
263         
264         items.push_back (SeparatorElem());
265         items.push_back (MenuElem (_("Height"), *size_menu));
266         items.push_back (MenuElem (_("Color"), mem_fun(*this, &ImageFrameTimeAxis::select_track_color)));
267
268         items.push_back (SeparatorElem());
269         items.push_back (MenuElem (_("Remove"), bind(mem_fun(*this, &VisualTimeAxis::remove_this_time_axis), (void*)this))) ;
270 }
271
272 /**
273  * handles the building of the ImageFrameView sub menu
274  */
275 void
276 ImageFrameTimeAxis::create_imageframe_menu()
277 {
278         using namespace Menu_Helpers;
279
280         imageframe_menu = manage(new Menu) ;
281         imageframe_menu->set_name ("ArdourContextMenu");
282         MenuList& items = imageframe_menu->items();
283         
284         imageframe_item_menu = manage(new Menu) ;
285         imageframe_item_menu->set_name ("ArdourContextMenu");
286         MenuList& imageframe_sub_items = imageframe_item_menu->items() ;
287
288         /* duration menu */
289         Menu* duration_menu = manage(new Menu) ;
290         duration_menu->set_name ("ArdourContextMenu");
291         MenuList& duration_items = duration_menu->items() ;
292
293         if(view)
294         {
295                 duration_items.push_back(MenuElem (_("0.5 seconds"), bind (mem_fun (view, &ImageFrameTimeAxisView::set_imageframe_duration_sec), 0.5))) ;
296                 duration_items.push_back(MenuElem (_("1 seconds"), bind (mem_fun (view, &ImageFrameTimeAxisView::set_imageframe_duration_sec), 1.0))) ;
297                 duration_items.push_back(MenuElem (_("1.5 seconds"), bind (mem_fun (view, &ImageFrameTimeAxisView::set_imageframe_duration_sec), 1.5))) ;
298                 duration_items.push_back(MenuElem (_("2 seconds"), bind (mem_fun (view, &ImageFrameTimeAxisView::set_imageframe_duration_sec), 2.0))) ;
299                 duration_items.push_back(MenuElem (_("2.5 seconds"), bind (mem_fun (view, &ImageFrameTimeAxisView::set_imageframe_duration_sec), 2.5))) ;
300                 duration_items.push_back(MenuElem (_("3 seconds"), bind (mem_fun (view, &ImageFrameTimeAxisView::set_imageframe_duration_sec), 3.0))) ;
301                 //duration_items.push_back(SeparatorElem()) ;
302                 //duration_items.push_back(MenuElem (_("custom"), mem_fun(*this, &ImageFrameTimeAxis::set_imageframe_duration_custom))) ;
303         }
304
305         imageframe_sub_items.push_back(MenuElem(_("Duration (sec)"), *duration_menu)) ;
306
307         imageframe_sub_items.push_back(SeparatorElem()) ;
308         if(view)
309         {
310                 imageframe_sub_items.push_back(MenuElem (_("Remove Frame"), bind(mem_fun (view, &ImageFrameTimeAxisView::remove_selected_imageframe_item), (void*)this))) ;
311         }
312         
313         items.push_back(MenuElem(_("Image Frame"), *imageframe_item_menu)) ;
314         items.push_back(MenuElem (_("Rename Track"), mem_fun(*this,&ImageFrameTimeAxis::start_time_axis_rename))) ;
315
316         imageframe_menu->show_all() ;
317 }
318
319
320
321
322 //---------------------------------------------------------------------------------------//
323 // Marker Time Axis Methods
324
325 /**
326  * Add a MarkerTimeAxis to the ilst of MarkerTimeAxis' associated with this ImageFrameTimeAxis
327  *
328  * @param marker_track the MarkerTimeAxis to add
329  * @param src the identity of the object that initiated the change
330  * @return true if the addition was a success,
331  *         false otherwise
332  */
333 bool
334 ImageFrameTimeAxis::add_marker_time_axis(MarkerTimeAxis* marker_track, void* src)
335 {
336         bool ret = false ;
337         
338         if(get_named_marker_time_axis(marker_track->name()) != 0)
339         {
340                 ret = false ;
341         }
342         else
343         {
344                 marker_time_axis_list.push_back(marker_track) ;
345                 marker_track->GoingAway.connect(bind(mem_fun(*this, &ImageFrameTimeAxis::remove_time_axis_view), marker_track, (void*)this));
346         
347                  MarkerTimeAxisAdded(marker_track, src) ; /* EMIT_SIGNAL */
348                 ret = true ;
349         }
350         
351         return(ret) ;
352 }
353
354 /**
355  * Returns the named MarkerTimeAxis associated with this ImageFrameTimeAxis
356  *
357  * @param track_id the track_id of the MarkerTimeAxis to search for
358  * @return the named markerTimeAxis, or 0 if the named MarkerTimeAxis is not associated with this ImageFrameTimeAxis
359  */
360 MarkerTimeAxis*
361 ImageFrameTimeAxis::get_named_marker_time_axis(std::string track_id)
362 {
363         MarkerTimeAxis* mta =  0 ;
364         
365         for (MarkerTimeAxisList::iterator i = marker_time_axis_list.begin(); i != marker_time_axis_list.end(); ++i)
366         {
367                 if (((MarkerTimeAxis*)*i)->name() == track_id)
368                 {
369                         mta = ((MarkerTimeAxis*)*i) ;
370                         break ;
371                 }
372         }
373         return(mta) ;
374 }
375
376 /**
377  * Removes the named markerTimeAxis from those associated with this ImageFrameTimeAxis
378  *
379  * @param track_id the track id of the MarkerTimeAxis to remove
380  * @param src the identity of the object that initiated the change
381  * @return the removed MarkerTimeAxis
382  */
383 MarkerTimeAxis*
384 ImageFrameTimeAxis::remove_named_marker_time_axis(std::string track_id, void* src)
385 {
386         MarkerTimeAxis* mta = 0 ;
387         
388         for(MarkerTimeAxisList::iterator i = marker_time_axis_list.begin(); i != marker_time_axis_list.end(); ++i)
389         {
390                 if (((MarkerTimeAxis*)*i)->name() == track_id)
391                 {
392                         mta = ((MarkerTimeAxis*)*i) ;
393                         
394                         // the iterator is invalid after this call, so we can no longer use it as is.
395                         marker_time_axis_list.erase(i) ;
396                         
397                          MarkerTimeAxisRemoved(mta->name(), src) ; /* EMIT_SIGNAL */
398                         break ;
399                 }
400         }
401         
402         return(mta) ;
403 }
404
405 /**
406  * Removes the specified MarkerTimeAxis from the list of MarkerTimaAxis associated with this ImageFrameTimeAxis
407  * Note that the MarkerTimeAxis is not deleted, only removed from the list os associated tracks
408  *
409  * @param mta the TimeAxis to remove
410  * @param src the identity of the object that initiated the change
411  */
412 void
413 ImageFrameTimeAxis::remove_time_axis_view(MarkerTimeAxis* mta, void* src)
414 {
415         ENSURE_GUI_THREAD(bind (mem_fun(*this, &ImageFrameTimeAxis::remove_time_axis_view), mta, src));
416         
417         MarkerTimeAxisList::iterator i;
418         if((i = find (marker_time_axis_list.begin(), marker_time_axis_list.end(), mta)) != marker_time_axis_list.end())
419         {
420                 // note that we dont delete the object itself, we just remove it from our list
421                 marker_time_axis_list.erase(i) ;
422
423                  MarkerTimeAxisRemoved(mta->name(), src) ; /* EMIT_SIGNAL */
424         }
425 }
426
427
428 //---------------------------------------------------------------------------------------//
429 // Parent/Child helper object accessors
430
431 /**
432  * Returns the view helper of this TimeAxis
433  *
434  * @return the view helper of this TimeAxis
435  */
436 ImageFrameTimeAxisView*
437 ImageFrameTimeAxis::get_view()
438 {
439         return(view) ;
440 }