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