Fly my pretties!
[ardour.git] / gtk2_ardour / marker_time_axis_view.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 <algorithm>
22
23 #include <gtkmm.h>
24 #include <gtkmm2ext/gtk_ui.h>
25
26 #include "marker_time_axis_view.h"
27 #include "marker_time_axis.h"
28 #include "marker_view.h"
29 #include "imageframe_view.h"
30 #include "imageframe_time_axis.h"
31 #include "canvas-simplerect.h"
32 #include "public_editor.h"
33 #include "rgb_macros.h"
34 #include "gui_thread.h"
35
36 #include "i18n.h"
37
38 using namespace ARDOUR ;
39 using namespace Editing;
40
41 //---------------------------------------------------------------------------------------//
42 // Constructor / Desctructor
43                 
44 /**
45  * Construct a new MarkerTimeAxisView helper time axis helper
46  *
47  * @param mta the TimeAxsiView that this objbect is the helper for
48  */
49 MarkerTimeAxisView::MarkerTimeAxisView(MarkerTimeAxis& tv)
50         : _trackview (tv)
51 {
52         region_color = _trackview.color();
53         stream_base_color = color_map[cMarkerTrackBase];
54
55         canvas_group = gtk_canvas_item_new (GTK_CANVAS_GROUP(_trackview.canvas_display), gtk_canvas_group_get_type (), 0);
56
57         canvas_rect = gtk_canvas_item_new (GTK_CANVAS_GROUP(canvas_group),
58                 gtk_canvas_simplerect_get_type(),
59                 "x1", 0.0,
60                 "y1", 0.0,
61                 "x2", 1000000.0,
62                 "y2", (double)20,
63                 "outline_color_rgba", color_map[cMarkerTrackOutline],
64                 "fill_color_rgba", stream_base_color,
65                 0) ;
66                                            
67         gtk_signal_connect(GTK_OBJECT(canvas_rect), "event", (GtkSignalFunc)PublicEditor::canvas_marker_time_axis_view_event, &_trackview) ;
68
69         _samples_per_unit = _trackview.editor.get_current_zoom() ;
70
71         _trackview.editor.ZoomChanged.connect (slot(*this, &MarkerTimeAxisView::reset_samples_per_unit));
72 }
73
74 /**
75  * Destructor
76  * Reposinsibly for destroying all marker items that may have been added to this time axis view
77  *
78  */
79 MarkerTimeAxisView::~MarkerTimeAxisView()
80 {
81         // destroy everything upon this view
82         for(MarkerViewList::iterator iter = marker_view_list.begin(); iter != marker_view_list.end(); ++iter)
83         {
84                 MarkerView* mv = (*iter) ;
85                 
86                 MarkerViewList::iterator next = iter ;
87                 next++ ;
88                 marker_view_list.erase(iter) ;
89
90                 delete mv ;
91                 mv = 0 ;
92                 
93                 iter = next ;
94         }
95         
96         if(canvas_rect)
97         {
98                 gtk_object_destroy(GTK_OBJECT(canvas_rect)) ;
99                 canvas_rect = 0 ;
100         }
101         
102         if(canvas_group)
103         {
104                 gtk_object_destroy(GTK_OBJECT(canvas_group)) ;
105                 canvas_group = 0 ;
106         }
107 }
108
109
110 //---------------------------------------------------------------------------------------//
111 // ui methods & data
112
113 /**
114  * Sets the height of the time axis view and the item upon it
115  *
116  * @param height the new height
117  */
118 int
119 MarkerTimeAxisView::set_height(gdouble h)
120 {
121         if (h < 10.0 || h > 1000.0)
122         {
123                 return -1 ;
124         }
125         
126         gtk_object_set (GTK_OBJECT(canvas_rect), "y2", h, NULL);
127
128         for (MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); ++i)
129         {
130                 (*i)->set_height(h) ;
131         }
132
133         return 0;
134 }
135
136 /**
137  * Sets the position of this view helper on the canvas
138  *
139  * @param x the x position upon the canvas
140  * @param y the y position upon the canvas
141  */
142 int
143 MarkerTimeAxisView::set_position(gdouble x, gdouble y)
144 {
145         gtk_canvas_item_set (canvas_group, "x", x, "y", y, NULL);
146         return 0;
147 }
148
149 /**
150  * Sets the current samples per unit.
151  * this method tells each item upon the time axis of the change
152  * 
153  * @param spu the new samples per canvas unit value
154  */
155 int
156 MarkerTimeAxisView::set_samples_per_unit(gdouble spp)
157 {
158         if(spp < 1.0) {
159                 return -1 ;
160         }
161         
162         _samples_per_unit = spp ;
163
164         for(MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); ++i)
165         {
166                 (*i)->set_samples_per_unit(spp) ;
167         }
168         return(0) ;
169 }
170
171 /**
172  * Sets the color of the items contained upon this view helper
173  *
174  * @param color the new base color
175  */
176 void
177 MarkerTimeAxisView::apply_color(GdkColor& color)
178 {
179         region_color = color;
180
181         for (MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); i++)
182         {
183                 (*i)->set_color (region_color) ;
184         }
185 }
186
187
188 //---------------------------------------------------------------------------------------//
189 // Child MarkerView Accessors/Mutators
190                 
191 /**
192  * Adds a marker view to the list of items upon this time axis view helper
193  * the new MarkerView is returned
194  *
195  * @param ifv the ImageFrameView that the new item is marking up
196  * @param mark_text the text to be displayed uopn the new marker item
197  * @param mark_id the unique id of the new item
198  * @param start the position the new item should be placed upon the time line
199  * @param duration the duration the new item should be placed upon the timeline
200  * @param src the identity of the object that initiated the change
201  */      
202 MarkerView*
203 MarkerTimeAxisView::add_marker_view(ImageFrameView* ifv, std::string mark_type, std::string mark_id, jack_nframes_t start, jack_nframes_t dur, void* src)
204 {
205         if(ifv->has_marker_view_item(mark_id))
206         {
207                 return(0) ;
208         }
209         
210         MarkerView* mv = new MarkerView(GTK_CANVAS_GROUP(canvas_group),
211                  &_trackview,
212                  ifv,
213                  _trackview.editor.get_current_zoom(),
214                  region_color,
215                  mark_type,
216                  mark_id,
217                  start,
218                  dur) ;
219         
220         ifv->add_marker_view_item(mv, src) ;
221         marker_view_list.push_front(mv) ;
222         
223         mv->GoingAway.connect(bind (slot (*this,&MarkerTimeAxisView::remove_marker_view), (void*)this)) ;
224         
225          MarkerViewAdded(mv,src) ; /* EMIT_SIGNAL */
226         
227         return(mv) ;
228 }
229
230 /**
231  * Returns the named MarkerView or 0 if the named marker does not exist
232  *
233  * @param item_id the unique id of the item to search for
234  * @return the named MarkerView, or 0 if it is not held upon this view
235  */
236 MarkerView*
237 MarkerTimeAxisView::get_named_marker_view(std::string item_id)
238 {
239         MarkerView* mv =  0 ;
240         
241         for(MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); ++i)
242         {
243                 if(((MarkerView*)*i)->get_item_name() == item_id)
244                 {
245                         mv = ((MarkerView*)*i) ;
246                         break ;
247                 }
248         }
249         return(mv) ;
250 }
251
252 /**
253  * Removes the currently selected MarverView
254  * Note that this method actually destroys the MarkerView too.
255  * We assume that since we own the object, we are allowed to do this
256  *
257  * @param src the identity of the object that initiated the change
258  * @see add_marker_view
259  */
260 void
261 MarkerTimeAxisView::remove_selected_marker_view(void* src)
262 {
263         std::string removed ;
264         
265         if (selected_time_axis_item)
266         {
267                 MarkerViewList::iterator i ;
268                 if((i = find (marker_view_list.begin(), marker_view_list.end(), selected_time_axis_item)) != marker_view_list.end())
269                 {
270                         marker_view_list.erase(i) ;
271                         
272                          MarkerViewRemoved(selected_time_axis_item->get_item_name(),src) ; /* EMIT_SIGNAL */
273
274                         delete(selected_time_axis_item) ;
275                         selected_time_axis_item = 0 ;
276                 }
277         }
278         else
279         {
280                 //No selected marker view
281         }
282 }
283
284 /**
285  * Removes and returns the named MarkerView from the list of MarkerView held by this view helper
286  *
287  * @param item_id the MarkerView unique id to remove
288  * @param src the identity of the object that initiated the change
289  * @see add_marker_view
290  */
291 MarkerView*
292 MarkerTimeAxisView::remove_named_marker_view(std::string item_id, void* src)
293 {
294         MarkerView* mv = 0 ;
295         
296         MarkerViewList::iterator i = marker_view_list.begin() ;
297         
298         for(MarkerViewList::iterator iter = marker_view_list.begin(); iter != marker_view_list.end(); ++iter)
299         {
300                 if(((MarkerView*)*i)->get_item_name() == item_id)
301                 {
302                         mv = ((MarkerView*)*i) ;
303                         marker_view_list.erase(i) ;
304                                                 
305                          MarkerViewRemoved(mv->get_item_name(), src) ; /* EMIT_SIGNAL */
306                         
307                         // break from the for loop
308                         break;
309                 }
310                 i++ ;
311         }
312         
313         return(mv) ;
314 }
315
316 /**
317  * Removes mv from the list of MarkerView upon this TimeAxis
318  *
319  * @param mv the MarkerView to remove
320  * @param src the identity of the object that initiated the change
321  */
322 void
323 MarkerTimeAxisView::remove_marker_view(MarkerView* mv, void* src)
324 {
325         ENSURE_GUI_THREAD(bind (slot (*this, &MarkerTimeAxisView::remove_marker_view), mv, src));
326         
327         MarkerViewList::iterator i;
328
329         if((i = find (marker_view_list.begin(), marker_view_list.end(), mv)) != marker_view_list.end()) {
330                 marker_view_list.erase(i) ;
331                 
332                 // Assume this remove happened locally, else use remove_named_marker_time_axis
333                 // let listeners know that the named MarkerTimeAxis has been removed
334                  MarkerViewRemoved(mv->get_item_name(), src) ; /* EMIT_SIGNAL */
335         }
336 }
337
338
339 /**
340  * Sets the duration of the selected MarkerView to the specified number of seconds
341  *
342  * @param sec the duration to set the MArkerView to, in seconds
343  */
344 void
345 MarkerTimeAxisView::set_marker_duration_sec(double sec)
346 {
347   if(get_selected_time_axis_item() != 0)
348   {
349           get_selected_time_axis_item()->set_duration((jack_nframes_t) (sec * _trackview.editor.current_session()->frame_rate()), this) ;
350   }
351 }
352
353
354 //---------------------------------------------------------------------------------------//
355 // Selected item methods
356
357 /**
358  * Sets the currently selected item upon this time axis
359  *
360  * @param mv the item to set selected
361  */
362 void
363 MarkerTimeAxisView::set_selected_time_axis_item(MarkerView* mv)
364 {
365         selected_time_axis_item = mv ;
366 }
367
368 /**
369  * Clears any selected item upon this time axis
370  *
371  */
372 void
373 MarkerTimeAxisView::clear_selected_time_axis_item()
374 {
375         selected_time_axis_item = 0 ;
376 }
377                 
378 /**
379  * Returnsthe currently selected item upon this time axis
380  *
381  * @return the currently selected item pon this time axis
382  */
383 MarkerView*
384 MarkerTimeAxisView::get_selected_time_axis_item()
385 {
386         return(selected_time_axis_item) ;
387 }
388
389
390
391
392 /**
393  * convenience method to re-get the samples per unit and tell items upon this view
394  *
395  */
396 void
397 MarkerTimeAxisView::reset_samples_per_unit ()
398 {
399         set_samples_per_unit(_trackview.editor.get_current_zoom()) ;
400 }