summaryrefslogtreecommitdiff
path: root/gtk2_ardour/imageframe_time_axis_view.cc
blob: 69205cd5119961f1fa2cdec980334c4e4bd4ffd5 (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
    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 "imageframe_time_axis_view.h"
#include "imageframe_time_axis_group.h"
#include "imageframe_view.h"
#include "imageframe_time_axis.h"
#include "canvas-simplerect.h"
#include "region_selection.h"
#include "public_editor.h"
#include "rgb_macros.h"
#include "gui_thread.h"
#include "ardour_ui.h"

#include "i18n.h"

using namespace ARDOUR ;
using namespace Editing;

//---------------------------------------------------------------------------------------//
// Constructor / Desctructor
		
/**
 * Constructs a new ImageFrameTimeAxisView.
 *
 * @param ifta the parent ImageFrameTimeAxis of this view helper
 */
ImageFrameTimeAxisView::ImageFrameTimeAxisView (ImageFrameTimeAxis& tv)
	: _trackview (tv),
	  canvas_group (*_trackview.canvas_display),
	  canvas_rect (canvas_group, 0.0, 0.0, 1000000.0, tv.height)
{
	region_color = _trackview.color() ;
	stream_base_color = ARDOUR_UI::config()->canvasvar_ImageTrack.get() ;

	canvas_rect.property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ImageTrack.get();
	canvas_rect.property_fill_color_rgba() = stream_base_color;

	canvas_rect.signal_event().connect (bind (mem_fun (_trackview.editor, &PublicEditor::canvas_imageframe_view_event), (ArdourCanvas::Item*) &canvas_rect, &tv));

	_samples_per_unit = _trackview.editor.get_current_zoom() ;

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

	selected_imageframe_group = 0 ;
	selected_imageframe_view = 0 ;
}

/**
 * Destructor 
 * Responsible for destroying all items tat may have been added to this time axis
 */
ImageFrameTimeAxisView::~ImageFrameTimeAxisView()
{
	// Destroy all the ImageFrameGroups that we have
	
	for(ImageFrameGroupList::iterator iter = imageframe_groups.begin(); iter != imageframe_groups.end(); ++iter)
	{
		ImageFrameTimeAxisGroup* iftag = (*iter) ;
		
		ImageFrameGroupList::iterator next = iter ;
		next++ ;

		// remove the front element
		imageframe_groups.erase(iter) ;

		delete iftag ;
		iftag = 0 ;
		
		iter = next ;
	}
	
}


//---------------------------------------------------------------------------------------//
// ui methods & data
	
/**
 * Sets the height of the time axis view and the item upon it
 *
 * @param height the new height
 */
int
ImageFrameTimeAxisView::set_height (gdouble h)
{
	/* limit the values to something sane-ish */
	if (h < 10.0 || h > 1000.0) {
		return(-1) ;
	}
	
	canvas_rect.property_y2() = h ;


	for(ImageFrameGroupList::const_iterator citer = imageframe_groups.begin(); citer != imageframe_groups.end(); ++citer)
	{
		(*citer)->set_item_heights(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 npon the canvas
 */
int
ImageFrameTimeAxisView::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 
ImageFrameTimeAxisView::set_samples_per_unit (gdouble spp)
{
	if (spp < 1.0) {
		return(-1) ;
	}

	_samples_per_unit = spp;
	
	for(ImageFrameGroupList::const_iterator citer = imageframe_groups.begin(); citer != imageframe_groups.end(); ++citer)
	{
		(*citer)->set_item_samples_per_units(spp) ;
	}
	
	return(0) ;
}

/**
 * Sets the color of the items contained uopn this view helper
 *
 * @param color the new base color
 */
void
ImageFrameTimeAxisView::apply_color(Gdk::Color& color)
{
	region_color = color ;
	for(ImageFrameGroupList::const_iterator citer = imageframe_groups.begin(); citer != imageframe_groups.end(); citer++)
	{
		(*citer)->apply_item_color(region_color) ;
	}
}


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


//---------------------------------------------------------------------------------------//
// Child ImageFrameTimeAxisGroup Accessors/Mutators
		
/**
 * Adds an ImageFrameTimeAxisGroup to the list of items upon this time axis view helper
 * the new ImageFrameTimeAxisGroup is returned
 *
 * @param group_id the unique id of the new group
 * @param src the identity of the object that initiated the change
 */
ImageFrameTimeAxisGroup*
ImageFrameTimeAxisView::add_imageframe_group(std::string group_id, void* src)
{
	ImageFrameTimeAxisGroup* iftag = 0 ;
	
	//check that there is not already a group with that id
	if(get_named_imageframe_group(group_id) != 0)
	{
		// iftag = 0 ;
	}
	else
	{
		iftag = new ImageFrameTimeAxisGroup(*this, group_id) ;

		imageframe_groups.push_front(iftag) ;
	
		iftag->GoingAway.connect(bind(mem_fun(*this,&ImageFrameTimeAxisView::remove_imageframe_group), iftag, (void*)this)) ;
	
		 ImageFrameGroupAdded(iftag, src) ; /* EMIT_SIGNAL */
	}

	return(iftag) ;
}

/**
 * Returns the named ImageFrameTimeAxisGroup or 0 if the named group does not exist on this view helper
 *
 * @param group_id the unique id of the group to search for
 * @return the named ImageFrameTimeAxisGroup, or 0 if it is not held upon this view
 */
ImageFrameTimeAxisGroup*
ImageFrameTimeAxisView::get_named_imageframe_group(std::string group_id)
{
	ImageFrameTimeAxisGroup* iftag =  0 ;
	
	for(ImageFrameGroupList::iterator i = imageframe_groups.begin(); i != imageframe_groups.end(); ++i)
	{
		if (((ImageFrameTimeAxisGroup*)*i)->get_group_name() == group_id)
		{
			iftag = ((ImageFrameTimeAxisGroup*)*i) ;
			break ;
		}
	}
	
	return(iftag) ;
}


/**
 * Removes and returns the named ImageFrameTimeAxisGroup from the list of ImageFrameTimeAxisGroup held by this view helper
 *
 * @param group_id the ImageFrameTimeAxisGroup unique id to remove
 * @param src the identity of the object that initiated the change
 * @see add_imageframe_group
 */
ImageFrameTimeAxisGroup*
ImageFrameTimeAxisView::remove_named_imageframe_group(std::string group_id, void* src)
{
	ImageFrameTimeAxisGroup* removed = 0 ;
	
	for(ImageFrameGroupList::iterator iter = imageframe_groups.begin(); iter != imageframe_groups.end(); ++iter)
	{
		if(((ImageFrameTimeAxisGroup*)*iter)->get_group_name() == group_id)
		{
			removed = (*iter) ;
			imageframe_groups.erase(iter) ;
			
			if(removed == selected_imageframe_group)
			{
				selected_imageframe_group = 0 ;
			}
			
			 ImageFrameGroupRemoved(removed->get_group_name(), src) ; /* EMIT_SIGNAL */
			
			// break from the for loop
			break ;
		}
		iter++ ;
	}
	
	return(removed) ;
}


/**
 * Removes the specified ImageFrameTimeAxisGroup from the list of ImageFrameTimeAxisGroups upon this TimeAxis.
 *
 * @param iftag the ImageFrameView to remove
 */
void
ImageFrameTimeAxisView::remove_imageframe_group(ImageFrameTimeAxisGroup* iftag, void* src)
{
	ENSURE_GUI_THREAD(bind (mem_fun(*this, &ImageFrameTimeAxisView::remove_imageframe_group), iftag, src));

	ImageFrameGroupList::iterator i;
	if((i = find (imageframe_groups.begin(), imageframe_groups.end(), iftag)) != imageframe_groups.end())
	{
		imageframe_groups.erase(i) ;
		
		 ImageFrameGroupRemoved(iftag->get_group_name(), src) ; /* EMIT_SIGNAL */
	}
}




//---------------------------------------------------------------------------------------//
// Selected group methods
		
/**
 * Sets the currently selected group upon this time axis
 *
 * @param ifv the item to set selected
 */
void
ImageFrameTimeAxisView::set_selected_imageframe_group(ImageFrameTimeAxisGroup* iftag)
{
	if(selected_imageframe_group)
	{
		selected_imageframe_group->set_selected(false) ;
	}
	
	selected_imageframe_group = iftag ;
	selected_imageframe_group->set_selected(true) ;
}

/**
 * Clears the currently selected image frame group unpo this time axis
 *
*/
void
ImageFrameTimeAxisView::clear_selected_imageframe_group()
{
	if(selected_imageframe_group)
	{
		selected_imageframe_group->set_selected(false) ;
	}
	selected_imageframe_group = 0 ;
}
		
/**
 * Returns the currently selected group upon this time axis
 *
 * @return the currently selected group upon this time axis
 */
ImageFrameTimeAxisGroup*
ImageFrameTimeAxisView::get_selected_imageframe_group() const
{
	return(selected_imageframe_group) ;
}

//---------------------------------------------------------------------------------------//
// Selected item methods
		
/**
 * Sets the currently selected imag frame view item
 *
 * @param iftag the group the selected item is part
 * @param ifv the selected item
 */
void
ImageFrameTimeAxisView::set_selected_imageframe_view(ImageFrameTimeAxisGroup* iftag, ImageFrameView* ifv)
{
	set_selected_imageframe_group(iftag) ;
	
	if(selected_imageframe_view)
	{
		selected_imageframe_view->set_selected(false) ;
	}
	
	selected_imageframe_view = ifv ;
	selected_imageframe_view->set_selected(true) ;
}

/**
 * Clears the currently selected image frame view item
 *
 */
void
ImageFrameTimeAxisView::clear_selected_imageframe_item(bool clear_group)
{
	if(clear_group)
	{
		clear_selected_imageframe_group() ;
	}
	
	if(selected_imageframe_view)
	{
		selected_imageframe_view->set_selected(false) ;
	}
	selected_imageframe_view = 0 ;
}
		
/**
 * Returns the currently selected image frame view item upon this time axis
 *
 * @return the currently selected image frame view item
 */
ImageFrameView*
ImageFrameTimeAxisView::get_selected_imageframe_view() const
{
	return(selected_imageframe_view) ;
}



 
void
ImageFrameTimeAxisView::set_imageframe_duration_sec(double sec)
{
	if(selected_imageframe_group && selected_imageframe_view)
	{
		selected_imageframe_view->set_duration((nframes_t) (sec * _trackview.editor.current_session()->frame_rate()), this) ;
	}
}



/**
 * Removes the currently selected ImageFrame view item
 *
 * @param src the identity of the object that initiated the change
 * @see add_imageframe_group
 */
void
ImageFrameTimeAxisView::remove_selected_imageframe_item(void* src)
{
	if(selected_imageframe_group && selected_imageframe_view)
	{
		ImageFrameView* temp_item = selected_imageframe_view ;
		selected_imageframe_group->remove_imageframe_item(temp_item, src) ;
		
		// XXX although we have removed the item from the group, we need the group id still set within the
		//     item as the remove method requires this data when telling others about the deletion
		//     to fully specify the item we need the track, group and item id
		selected_imageframe_view->remove_this_item(src) ;
		clear_selected_imageframe_item(false) ;
	}
}