summaryrefslogtreecommitdiff
path: root/gtk2_ardour/marker_time_axis_view.cc
blob: f33e0bbefc202d4ba3c67a473b65a8682d2ff3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
    Copyright (C) 2003 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <algorithm>

#include <gtkmm.h>
#include <gtkmm2ext/gtk_ui.h>

#include "marker_time_axis_view.h"
#include "marker_time_axis.h"
#include "marker_view.h"
#include "imageframe_view.h"
#include "imageframe_time_axis.h"
#include "canvas-simplerect.h"
#include "public_editor.h"
#include "rgb_macros.h"
#include "gui_thread.h"

#include "i18n.h"

using namespace ARDOUR ;
using namespace Editing;

//---------------------------------------------------------------------------------------//
// Constructor / Desctructor
		
/**
 * Construct a new MarkerTimeAxisView helper time axis helper
 *
 * @param mta the TimeAxsiView that this objbect is the helper for
 */
MarkerTimeAxisView::MarkerTimeAxisView(MarkerTimeAxis& tv)
	: _trackview (tv)
{
	region_color = _trackview.color();
	stream_base_color = color_map[cMarkerTrackBase];

	canvas_group = new ArdourCanvas::Group (*_trackview.canvas_display);

	canvas_rect =  new ArdourCanvas::SimpleRect (*canvas_group);
	canvas_rect->property_x1() = 0.0;
	canvas_rect->property_y1() = 0.0;
	canvas_rect->property_x2() = max_frames;
	canvas_rect->property_y2() = (double)20;
	canvas_rect->property_outline_color_rgba() = color_map[cMarkerTrackOutline];
	canvas_rect->property_fill_color_rgba() = stream_base_color;
		   
	canvas_rect->signal_event().connect (bind (mem_fun (_trackview.editor, &PublicEditor::canvas_marker_time_axis_view_event), canvas_rect, &_trackview));

	_samples_per_unit = _trackview.editor.get_current_zoom() ;

	_trackview.editor.ZoomChanged.connect (mem_fun(*this, &MarkerTimeAxisView::reset_samples_per_unit));
}

/**
 * Destructor
 * Reposinsibly for destroying all marker items that may have been added to this time axis view
 *
 */
MarkerTimeAxisView::~MarkerTimeAxisView()
{
	// destroy everything upon this view
	for(MarkerViewList::iterator iter = marker_view_list.begin(); iter != marker_view_list.end(); ++iter)
	{
		MarkerView* mv = (*iter) ;
		
		MarkerViewList::iterator next = iter ;
		next++ ;
		marker_view_list.erase(iter) ;

		delete mv ;
		mv = 0 ;
		
		iter = next ;
	}
	
	if(canvas_rect)
	{
		delete canvas_rect;
		canvas_rect = 0 ;
	}
	
	if(canvas_group)
	{
		delete canvas_group;
		canvas_group = 0 ;
	}
}


//---------------------------------------------------------------------------------------//
// ui methods & data

/**
 * Sets the height of the time axis view and the item upon it
 *
 * @param height the new height
 */
int
MarkerTimeAxisView::set_height(gdouble h)
{
	if (h < 10.0 || h > 1000.0)
	{
		return -1 ;
	}
	
	canvas_rect->property_y2() = h;

	for (MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); ++i)
	{
		(*i)->set_height(h) ;
	}

	return 0;
}

/**
 * Sets the position of this view helper on the canvas
 *
 * @param x the x position upon the canvas
 * @param y the y position upon the canvas
 */
int
MarkerTimeAxisView::set_position(gdouble x, gdouble y)
{
	canvas_group->property_x() = x;
	canvas_group->property_y() = y;
	return 0;
}

/**
 * Sets the current samples per unit.
 * this method tells each item upon the time axis of the change
 * 
 * @param spu the new samples per canvas unit value
 */
int
MarkerTimeAxisView::set_samples_per_unit(gdouble spp)
{
	if(spp < 1.0) {
		return -1 ;
	}
	
	_samples_per_unit = spp ;

	for(MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); ++i)
	{
		(*i)->set_samples_per_unit(spp) ;
	}
	return(0) ;
}

/**
 * Sets the color of the items contained upon this view helper
 *
 * @param color the new base color
 */
void
MarkerTimeAxisView::apply_color(Gdk::Color& color)
{
	region_color = color;

	for (MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); i++)
	{
		(*i)->set_color (region_color) ;
	}
}


//---------------------------------------------------------------------------------------//
// Child MarkerView Accessors/Mutators
		
/**
 * Adds a marker view to the list of items upon this time axis view helper
 * the new MarkerView is returned
 *
 * @param ifv the ImageFrameView that the new item is marking up
 * @param mark_text the text to be displayed uopn the new marker item
 * @param mark_id the unique id of the new item
 * @param start the position the new item should be placed upon the time line
 * @param duration the duration the new item should be placed upon the timeline
 * @param src the identity of the object that initiated the change
 */	 
MarkerView*
MarkerTimeAxisView::add_marker_view(ImageFrameView* ifv, std::string mark_type, std::string mark_id, nframes_t start, nframes_t dur, void* src)
{
	if(ifv->has_marker_view_item(mark_id))
	{
		return(0) ;
	}
	
	MarkerView* mv = new MarkerView(canvas_group,
		 &_trackview,
		 ifv,
		 _trackview.editor.get_current_zoom(),
		 region_color,
		 mark_type,
		 mark_id,
		 start,
		 dur) ;
	
	ifv->add_marker_view_item(mv, src) ;
	marker_view_list.push_front(mv) ;
	
	mv->GoingAway.connect(bind (mem_fun(*this,&MarkerTimeAxisView::remove_marker_view), (void*)this)) ;
	
	 MarkerViewAdded(mv,src) ; /* EMIT_SIGNAL */
	
	return(mv) ;
}

/**
 * Returns the named MarkerView or 0 if the named marker does not exist
 *
 * @param item_id the unique id of the item to search for
 * @return the named MarkerView, or 0 if it is not held upon this view
 */
MarkerView*
MarkerTimeAxisView::get_named_marker_view(std::string item_id)
{
	MarkerView* mv =  0 ;
	
	for(MarkerViewList::iterator i = marker_view_list.begin(); i != marker_view_list.end(); ++i)
	{
		if(((MarkerView*)*i)->get_item_name() == item_id)
		{
			mv = ((MarkerView*)*i) ;
			break ;
		}
	}
	return(mv) ;
}

/**
 * Removes the currently selected MarverView
 * Note that this method actually destroys the MarkerView too.
 * We assume that since we own the object, we are allowed to do this
 *
 * @param src the identity of the object that initiated the change
 * @see add_marker_view
 */
void
MarkerTimeAxisView::remove_selected_marker_view(void* src)
{
	std::string removed ;
	
	if (selected_time_axis_item)
	{
		MarkerViewList::iterator i ;
		if((i = find (marker_view_list.begin(), marker_view_list.end(), selected_time_axis_item)) != marker_view_list.end())
		{
			marker_view_list.erase(i) ;
			
			 MarkerViewRemoved(selected_time_axis_item->get_item_name(),src) ; /* EMIT_SIGNAL */

			delete(selected_time_axis_item) ;
			selected_time_axis_item = 0 ;
		}
	}
	else
	{
		//No selected marker view
	}
}

/**
 * Removes and returns the named MarkerView from the list of MarkerView held by this view helper
 *
 * @param item_id the MarkerView unique id to remove
 * @param src the identity of the object that initiated the change
 * @see add_marker_view
 */
MarkerView*
MarkerTimeAxisView::remove_named_marker_view(std::string item_id, void* src)
{
	MarkerView* mv = 0 ;
	
	MarkerViewList::iterator i = marker_view_list.begin() ;
	
	for(MarkerViewList::iterator iter = marker_view_list.begin(); iter != marker_view_list.end(); ++iter)
	{
		if(((MarkerView*)*i)->get_item_name() == item_id)
		{
			mv = ((MarkerView*)*i) ;
			marker_view_list.erase(i) ;
						
			 MarkerViewRemoved(mv->get_item_name(), src) ; /* EMIT_SIGNAL */
			
			// break from the for loop
			break;
		}
		i++ ;
	}
	
	return(mv) ;
}

/**
 * Removes mv from the list of MarkerView upon this TimeAxis
 *
 * @param mv the MarkerView to remove
 * @param src the identity of the object that initiated the change
 */
void
MarkerTimeAxisView::remove_marker_view(MarkerView* mv, void* src)
{
	ENSURE_GUI_THREAD(bind (mem_fun(*this, &MarkerTimeAxisView::remove_marker_view), mv, src));
	
	MarkerViewList::iterator i;

	if((i = find (marker_view_list.begin(), marker_view_list.end(), mv)) != marker_view_list.end()) {
		marker_view_list.erase(i) ;
		
		// Assume this remove happened locally, else use remove_named_marker_time_axis
		// let listeners know that the named MarkerTimeAxis has been removed
		 MarkerViewRemoved(mv->get_item_name(), src) ; /* EMIT_SIGNAL */
	}
}


/**
 * Sets the duration of the selected MarkerView to the specified number of seconds
 *
 * @param sec the duration to set the MArkerView to, in seconds
 */
void
MarkerTimeAxisView::set_marker_duration_sec(double sec)
{
  if(get_selected_time_axis_item() != 0)
  {
	  get_selected_time_axis_item()->set_duration((nframes_t) (sec * _trackview.editor.current_session()->frame_rate()), this) ;
  }
}


//---------------------------------------------------------------------------------------//
// Selected item methods

/**
 * Sets the currently selected item upon this time axis
 *
 * @param mv the item to set selected
 */
void
MarkerTimeAxisView::set_selected_time_axis_item(MarkerView* mv)
{
	selected_time_axis_item = mv ;
}

/**
 * Clears any selected item upon this time axis
 *
 */
void
MarkerTimeAxisView::clear_selected_time_axis_item()
{
	selected_time_axis_item = 0 ;
}
		
/**
 * Returnsthe currently selected item upon this time axis
 *
 * @return the currently selected item pon this time axis
 */
MarkerView*
MarkerTimeAxisView::get_selected_time_axis_item()
{
	return(selected_time_axis_item) ;
}




/**
 * convenience method to re-get the samples per unit and tell items upon this view
 *
 */
void
MarkerTimeAxisView::reset_samples_per_unit ()
{
	set_samples_per_unit(_trackview.editor.get_current_zoom()) ;
}