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