37302cf6e03d921985f8339d1c1d1ca95323eca2
[ardour.git] / gtk2_ardour / marker_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
23 #include <pbd/error.h>
24
25 #include <gtkmm2ext/utils.h>
26
27 #include <ardour/session.h>
28 #include <ardour/utils.h>
29
30 #include "ardour_ui.h"
31 #include "public_editor.h"
32 #include "imageframe_time_axis.h"
33 #include "canvas-simplerect.h"
34 #include "selection.h"
35 #include "imageframe_time_axis_view.h"
36 #include "marker_time_axis_view.h"
37 #include "imageframe_view.h"
38 #include "marker_time_axis.h"
39
40 #include "i18n.h"
41
42 using namespace ARDOUR;
43 using namespace sigc;
44 using namespace Gtk;
45
46 //---------------------------------------------------------------------------------------//
47 // Constructor / Desctructor
48
49 /**
50  * Constructs a new MarkerTimeAxis
51  *
52  * @param ed the PublicEditor
53  * @param sess the current session
54  * @param canvas the parent canvas item
55  * @param name the name/id of this time axis
56  * @param tav the associated track view that this MarkerTimeAxis is marking up
57  */
58 MarkerTimeAxis::MarkerTimeAxis (PublicEditor& ed, ARDOUR::Session& sess, Widget *canvas, std::string name, TimeAxisView* tav)
59         : AxisView(sess),
60           VisualTimeAxis(name, ed, sess, canvas)
61 {
62         /* the TimeAxisView these markers are associated with */
63         marked_time_axis = tav ;
64         
65         _color = unique_random_color() ;
66         time_axis_name = name ;
67
68         //GTK2FIX -- how to get the group? is the canvas display really a group?
69         //selection_group = gnome_canvas_item_new (GNOME_CANVAS_GROUP(canvas_display), gnome_canvas_group_get_type (), 0) ;
70         selection_group = new Gnome::Canvas::Group (*canvas_display);
71         selection_group->hide();
72
73         // intialize our data items
74         name_prompter = 0 ;
75         marker_menu = 0 ;
76
77         y_position = -1 ;
78
79         /* create our new marker time axis strip view */
80         view = new MarkerTimeAxisView(*this) ;
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(Small) ;
87 }
88
89 /**
90  * Destructor
91  * Responsible for destroying any marker items upon this time axis
92  */
93 MarkerTimeAxis::~MarkerTimeAxis()
94 {
95          GoingAway() ; /* EMIT_SIGNAL */
96
97         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i)
98         {
99                 gtk_object_destroy (GTK_OBJECT((*i)->rect));
100                 gtk_object_destroy (GTK_OBJECT((*i)->start_trim));
101                 gtk_object_destroy (GTK_OBJECT((*i)->end_trim));
102         }
103
104         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i)
105         {
106                 gtk_object_destroy (GTK_OBJECT((*i)->rect));
107                 gtk_object_destroy (GTK_OBJECT((*i)->start_trim));
108                 gtk_object_destroy (GTK_OBJECT((*i)->end_trim));
109         }
110         
111         if(selection_group)
112         {
113                 gtk_object_destroy(GTK_OBJECT (selection_group)) ;
114                 selection_group = 0 ;
115         }
116
117         // destroy the view helper
118         // this handles removing and destroying individual marker items
119         if(view)
120         {
121                 delete view ;
122                 view = 0 ;
123         }
124 }
125
126
127 //---------------------------------------------------------------------------------------//
128 // ui methods & data
129         
130 /**
131  * Sets the height of this TrackView to one of the defined TrackHeights
132  *
133  * @param h the TrackHeight value to set
134  */     
135 void
136 MarkerTimeAxis::set_height (TrackHeight h)
137 {
138         VisualTimeAxis::set_height(h) ;
139         
140         // tell out view helper of the change too
141         if (view != 0)
142         {
143                 view->set_height((double) height) ;
144         }
145         
146         // tell those interested that we have had our height changed
147          gui_changed("track_height",(void*)0) ; /* EMIT_SIGNAL */
148 }
149
150 /**
151  * Sets the number of samples per unit that are used.
152  * This is used to determine the sizes of items upon this time axis
153  *
154  * @param spu the number of samples per unit
155  */
156 void
157 MarkerTimeAxis::set_samples_per_unit(double spu)
158 {
159         TimeAxisView::set_samples_per_unit (editor.get_current_zoom());
160
161         if (view) {
162                 view->set_samples_per_unit(spu) ;
163         }
164 }
165
166 /**
167  * Show the popup edit menu
168  *
169  * @param button the mouse button pressed
170  * @param time when to show the popup
171  * @param clicked_mv the MarkerView that the event ocured upon, or 0 if none
172  * @param with_item true if an item has been selected upon the time axis, used to set context menu
173  */
174 void
175 MarkerTimeAxis::popup_marker_time_axis_edit_menu(int button, int32_t time, MarkerView* clicked_mv, bool with_item)
176 {
177         if (!marker_menu)
178         {
179                 build_marker_menu() ;
180         }
181
182         if (with_item)
183         {
184                 marker_item_menu->set_sensitive(true) ;
185         }
186         else
187         {
188                 marker_item_menu->set_sensitive(false) ;
189         }
190         
191         marker_menu->popup(button,time) ;
192 }
193
194
195 /**
196  * convenience method to select a new track color and apply it to the view and view items
197  *
198  */
199 void
200 MarkerTimeAxis::select_track_color()
201 {
202         if(VisualTimeAxis::choose_time_axis_color())
203         {
204                 if(view)
205                 {
206                         view->apply_color(_color) ;
207                 }
208         }
209 }
210
211 /**
212  * Handles the building of the popup menu
213  */
214 void
215 MarkerTimeAxis::build_display_menu()
216 {
217         using namespace Menu_Helpers;
218
219         /* get the size menu ready */
220         build_size_menu() ;
221
222         /* prepare it */
223         TimeAxisView::build_display_menu();
224
225         /* now fill it with our stuff */
226         MenuList& items = display_menu->items();
227
228         items.push_back(MenuElem (_("Rename"), mem_fun(*this, &VisualTimeAxis::start_time_axis_rename)));
229
230         items.push_back(SeparatorElem()) ;
231         items.push_back(MenuElem (_("Height"), *size_menu));
232         items.push_back(MenuElem (_("Color"), mem_fun(*this, &MarkerTimeAxis::select_track_color)));
233         items.push_back(SeparatorElem()) ;
234         
235         items.push_back(MenuElem (_("Remove"), bind(mem_fun(*this, &MarkerTimeAxis::remove_this_time_axis), (void*)this)));
236 }
237
238 /**
239  * handles the building of the MarkerView sub menu
240  */
241 void
242 MarkerTimeAxis::build_marker_menu()
243 {
244         using namespace Menu_Helpers;
245
246         marker_menu = manage(new Menu) ;
247         marker_menu->set_name ("ArdourContextMenu");
248         MenuList& items = marker_menu->items();
249         
250         marker_item_menu = manage(new Menu) ;
251         marker_item_menu->set_name ("ArdourContextMenu");
252         MenuList& marker_sub_items = marker_item_menu->items() ;
253
254         /* duration menu */
255         Menu* duration_menu = manage(new Menu) ;
256         duration_menu->set_name ("ArdourContextMenu");
257         MenuList& duration_items = duration_menu->items() ;
258         
259         if(view)
260         {
261                 duration_items.push_back(MenuElem (_("1 seconds"), bind (mem_fun (view, &MarkerTimeAxisView::set_marker_duration_sec), 1.0))) ;
262                 duration_items.push_back(MenuElem (_("1.5 seconds"), bind (mem_fun (view, &MarkerTimeAxisView::set_marker_duration_sec), 1.5))) ;
263                 duration_items.push_back(MenuElem (_("2 seconds"), bind (mem_fun (view, &MarkerTimeAxisView::set_marker_duration_sec), 2.0))) ;
264                 duration_items.push_back(MenuElem (_("2.5 seconds"), bind (mem_fun (view, &MarkerTimeAxisView::set_marker_duration_sec), 2.5))) ;
265                 duration_items.push_back(MenuElem (_("3 seconds"), bind (mem_fun (view, &MarkerTimeAxisView::set_marker_duration_sec), 3.0))) ;
266         }
267         //duration_items.push_back(SeparatorElem()) ;
268         //duration_items.push_back(MenuElem (_("custom"), mem_fun(*this, &ImageFrameTimeAxis::set_marker_duration_custom))) ;
269
270         marker_sub_items.push_back(MenuElem(_("Duration (sec)"), *duration_menu)) ;
271
272         marker_sub_items.push_back(SeparatorElem()) ;
273         marker_sub_items.push_back(MenuElem (_("Remove Marker"), bind(mem_fun(view, &MarkerTimeAxisView::remove_selected_marker_view),(void*)this))) ;
274         
275         items.push_back(MenuElem(_("Marker"), *marker_item_menu)) ;
276         items.push_back(MenuElem (_("Rename Track"), mem_fun(*this,&MarkerTimeAxis::start_time_axis_rename))) ;
277
278         marker_menu->show_all() ;
279 }
280
281
282
283 /**
284  * Returns the view helper of this TimeAxis
285  *
286  * @return the view helper of this TimeAxis
287  */
288 MarkerTimeAxisView*
289 MarkerTimeAxis::get_view()
290 {
291         return(view) ;
292 }
293
294 /**
295  * Returns the TimeAxisView that this markerTimeAxis is marking up
296  *
297  * @return the TimeAXisView that this MarkerTimeAxis is marking
298  */
299 TimeAxisView*
300 MarkerTimeAxis::get_marked_time_axis()
301 {
302         return(marked_time_axis) ;
303 }
304
305
306
307