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