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