Fly my pretties!
[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         selection_group = gtk_canvas_item_new (GTK_CANVAS_GROUP(canvas_display), gtk_canvas_group_get_type (), 0) ;
69         gtk_canvas_item_hide(selection_group) ;
70
71         // intialize our data items
72         name_prompter = 0 ;
73         marker_menu = 0 ;
74
75         y_position = -1 ;
76
77         /* create our new marker time axis strip view */
78         view = new MarkerTimeAxisView(*this) ;
79
80         // set the initial time axis text label
81         label_view() ;
82                 
83         // set the initial height of this time axis
84         set_height(Small) ;
85 }
86
87 /**
88  * Destructor
89  * Responsible for destroying any marker items upon this time axis
90  */
91 MarkerTimeAxis::~MarkerTimeAxis()
92 {
93          GoingAway() ; /* EMIT_SIGNAL */
94
95         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i)
96         {
97                 gtk_object_destroy (GTK_OBJECT((*i)->rect));
98                 gtk_object_destroy (GTK_OBJECT((*i)->start_trim));
99                 gtk_object_destroy (GTK_OBJECT((*i)->end_trim));
100         }
101
102         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i)
103         {
104                 gtk_object_destroy (GTK_OBJECT((*i)->rect));
105                 gtk_object_destroy (GTK_OBJECT((*i)->start_trim));
106                 gtk_object_destroy (GTK_OBJECT((*i)->end_trim));
107         }
108         
109         if(selection_group)
110         {
111                 gtk_object_destroy(GTK_OBJECT (selection_group)) ;
112                 selection_group = 0 ;
113         }
114
115         // destroy the view helper
116         // this handles removing and destroying individual marker items
117         if(view)
118         {
119                 delete view ;
120                 view = 0 ;
121         }
122 }
123
124
125 //---------------------------------------------------------------------------------------//
126 // ui methods & data
127         
128 /**
129  * Sets the height of this TrackView to one of the defined TrackHeights
130  *
131  * @param h the TrackHeight value to set
132  */     
133 void
134 MarkerTimeAxis::set_height (TrackHeight h)
135 {
136         VisualTimeAxis::set_height(h) ;
137         
138         // tell out view helper of the change too
139         if (view != 0)
140         {
141                 view->set_height((double) height) ;
142         }
143         
144         // tell those interested that we have had our height changed
145          gui_changed("track_height",(void*)0) ; /* EMIT_SIGNAL */
146 }
147
148 /**
149  * Sets the number of samples per unit that are used.
150  * This is used to determine the sizes of items upon this time axis
151  *
152  * @param spu the number of samples per unit
153  */
154 void
155 MarkerTimeAxis::set_samples_per_unit(double spu)
156 {
157         TimeAxisView::set_samples_per_unit (editor.get_current_zoom());
158
159         if (view) {
160                 view->set_samples_per_unit(spu) ;
161         }
162 }
163
164 /**
165  * Show the popup edit menu
166  *
167  * @param button the mouse button pressed
168  * @param time when to show the popup
169  * @param clicked_mv the MarkerView that the event ocured upon, or 0 if none
170  * @param with_item true if an item has been selected upon the time axis, used to set context menu
171  */
172 void
173 MarkerTimeAxis::popup_marker_time_axis_edit_menu(int button, int32_t time, MarkerView* clicked_mv, bool with_item)
174 {
175         if (!marker_menu)
176         {
177                 build_marker_menu() ;
178         }
179
180         if (with_item)
181         {
182                 marker_item_menu->set_sensitive(true) ;
183         }
184         else
185         {
186                 marker_item_menu->set_sensitive(false) ;
187         }
188         
189         marker_menu->popup(button,time) ;
190 }
191
192
193 /**
194  * convenience method to select a new track color and apply it to the view and view items
195  *
196  */
197 void
198 MarkerTimeAxis::select_track_color()
199 {
200         if(VisualTimeAxis::choose_time_axis_color())
201         {
202                 if(view)
203                 {
204                         view->apply_color(_color) ;
205                 }
206         }
207 }
208
209 /**
210  * Handles the building of the popup menu
211  */
212 void
213 MarkerTimeAxis::build_display_menu()
214 {
215         using namespace Menu_Helpers;
216
217         /* get the size menu ready */
218         build_size_menu() ;
219
220         /* prepare it */
221         TimeAxisView::build_display_menu();
222
223         /* now fill it with our stuff */
224         MenuList& items = display_menu->items();
225
226         items.push_back(MenuElem (_("Rename"), slot (*this, &VisualTimeAxis::start_time_axis_rename)));
227
228         items.push_back(SeparatorElem()) ;
229         items.push_back(MenuElem (_("Height"), *size_menu));
230         items.push_back(MenuElem (_("Color"), slot (*this, &MarkerTimeAxis::select_track_color)));
231         items.push_back(SeparatorElem()) ;
232         
233         items.push_back(MenuElem (_("Remove"), bind(slot(*this, &MarkerTimeAxis::remove_this_time_axis), (void*)this)));
234 }
235
236 /**
237  * handles the building of the MarkerView sub menu
238  */
239 void
240 MarkerTimeAxis::build_marker_menu()
241 {
242         using namespace Menu_Helpers;
243
244         marker_menu = manage(new Menu) ;
245         marker_menu->set_name ("ArdourContextMenu");
246         MenuList& items = marker_menu->items();
247         
248         marker_item_menu = manage(new Menu) ;
249         marker_item_menu->set_name ("ArdourContextMenu");
250         MenuList& marker_sub_items = marker_item_menu->items() ;
251
252         /* duration menu */
253         Menu* duration_menu = manage(new Menu) ;
254         duration_menu->set_name ("ArdourContextMenu");
255         MenuList& duration_items = duration_menu->items() ;
256         
257         if(view)
258         {
259                 duration_items.push_back(MenuElem (_("1 seconds"), bind (slot (view, &MarkerTimeAxisView::set_marker_duration_sec), 1.0))) ;
260                 duration_items.push_back(MenuElem (_("1.5 seconds"), bind (slot (view, &MarkerTimeAxisView::set_marker_duration_sec), 1.5))) ;
261                 duration_items.push_back(MenuElem (_("2 seconds"), bind (slot (view, &MarkerTimeAxisView::set_marker_duration_sec), 2.0))) ;
262                 duration_items.push_back(MenuElem (_("2.5 seconds"), bind (slot (view, &MarkerTimeAxisView::set_marker_duration_sec), 2.5))) ;
263                 duration_items.push_back(MenuElem (_("3 seconds"), bind (slot (view, &MarkerTimeAxisView::set_marker_duration_sec), 3.0))) ;
264         }
265         //duration_items.push_back(SeparatorElem()) ;
266         //duration_items.push_back(MenuElem (_("custom"), slot (*this, &ImageFrameTimeAxis::set_marker_duration_custom))) ;
267
268         marker_sub_items.push_back(MenuElem(_("Duration (sec)"), *duration_menu)) ;
269
270         marker_sub_items.push_back(SeparatorElem()) ;
271         marker_sub_items.push_back(MenuElem (_("Remove Marker"), bind(slot(view, &MarkerTimeAxisView::remove_selected_marker_view),(void*)this))) ;
272         
273         items.push_back(MenuElem(_("Marker"), *marker_item_menu)) ;
274         items.push_back(MenuElem (_("Rename Track"), slot (*this,&MarkerTimeAxis::start_time_axis_rename))) ;
275
276         marker_menu->show_all() ;
277 }
278
279
280
281 /**
282  * Returns the view helper of this TimeAxis
283  *
284  * @return the view helper of this TimeAxis
285  */
286 MarkerTimeAxisView*
287 MarkerTimeAxis::get_view()
288 {
289         return(view) ;
290 }
291
292 /**
293  * Returns the TimeAxisView that this markerTimeAxis is marking up
294  *
295  * @return the TimeAXisView that this MarkerTimeAxis is marking
296  */
297 TimeAxisView*
298 MarkerTimeAxis::get_marked_time_axis()
299 {
300         return(marked_time_axis) ;
301 }
302
303
304
305
306 /**
307  * Handle the closing of the renaming dialog during the rename of this item
308  */
309 void
310 MarkerTimeAxis::finish_route_rename()
311 {
312         name_prompter->hide_all ();
313         ARDOUR_UI::instance()->allow_focus (false);
314
315         if (name_prompter->status == Gtkmm2ext::Prompter::cancelled) {
316                 return;
317         }
318         
319         string result;
320         name_prompter->get_result(result);
321         time_axis_name = result ;
322         editor.route_name_changed(this) ;
323         label_view() ;
324         delete name_prompter ;
325         name_prompter = 0 ;
326 }
327
328
329
330
331
332
333