non/Region.C

478 lines
12 KiB
C++
Raw Normal View History

2008-02-14 08:40:47 +01:00
/*******************************************************************************/
/* Copyright (C) 2008 Jonathan Moore Liles */
/* */
/* 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; see the file COPYING. If not,write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/
#include "Track.H"
2008-02-14 08:40:47 +01:00
#include "Region.H"
#include "Timeline.H"
#include "Waveform.H"
2008-02-14 08:40:47 +01:00
#include <FL/fl_draw.H>
#include <FL/Fl.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Box.H>
#include <stdio.h>
2008-02-14 08:40:47 +01:00
#include <algorithm>
//using std::algorithm;
using namespace std;
2008-02-21 17:20:36 +01:00
extern Timeline *timeline;
Fl_Boxtype Region::_box = FL_UP_BOX;
2008-02-20 13:45:02 +01:00
2008-02-21 08:38:31 +01:00
Fl_Color Region::_selection_color = FL_MAGENTA;
2008-02-21 10:48:53 +01:00
static Fl_Color fl_invert_color ( Fl_Color c )
{
unsigned char r, g, b;
Fl::get_color( c, r, g, b );
return fl_rgb_color( 255 - r, 255 - g, 255 - b );
}
2008-02-26 02:03:59 +01:00
#if 0
/* perhaps use map? */
map_PRIM ( set )
{
/* if ( narg % 2 != 0 ) */
/* printf( "invalid number of arguments\n" ); */
int id = atoi( arg );
map_ARG_NEXT( arg, end );
Logable *l = Loggable::find( id );
char **sa = malloc( sizeof( char * ) * narg + 1 );
for ( int i = 0; i < narg; ++i )
sa[ i ] = strdup( map_ARG_NEXT( arg, end ) );
l->set( sa );
map_RESULT( "" );
}
#endif
void
Region::init ( void )
2008-02-14 08:40:47 +01:00
{
_track = NULL;
_offset = 0;
_start = 0;
_end = 0;
_scale = 1.0f;
_clip = NULL;
2008-02-20 08:53:54 +01:00
_box_color = FL_CYAN;
_color = FL_BLUE;
2008-02-14 08:40:47 +01:00
}
2008-02-28 13:51:51 +01:00
/* copy constructor */
Region::Region ( const Region & rhs )
2008-02-16 08:46:43 +01:00
{
_offset = rhs._offset;
_track = rhs._track;
_clip = rhs._clip;
_start = rhs._start;
_end = rhs._end;
_scale = rhs._scale;
log_create();
2008-02-16 08:46:43 +01:00
}
2008-02-28 13:51:51 +01:00
/* */
Region::Region ( Audio_File *c )
{
init();
_clip = c;
_end = _clip->length();
log_create();
}
2008-02-18 02:13:20 +01:00
2008-02-28 13:51:51 +01:00
/* used when DND importing */
Region::Region ( Audio_File *c, Track *t, nframes_t o )
{
init();
_clip = c;
_end = _clip->length();
_track = t;
_offset = o;
_track->add( this );
log_create();
}
2008-02-20 08:08:36 +01:00
void
Region::trim ( enum trim_e t, int X )
{
redraw();
2008-02-20 08:08:36 +01:00
switch ( t )
{
case LEFT:
{
/* if ( d < 0 ) */
/* // _track->damage( FL_DAMAGE_EXPOSE, x() + d, y(), 1 - d, h() ); */
/* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() ); */
/* else */
/* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), d, h() ); */
int d = X - ( abs_x() - scroll_x() );
2008-02-21 17:20:36 +01:00
long td = timeline->x_to_ts( d );
2008-02-18 02:13:20 +01:00
2008-02-24 03:25:58 +01:00
if ( td < 0 && _start < 0 - td )
2008-02-20 08:08:36 +01:00
td = 0 - _start;
2008-02-24 03:25:58 +01:00
if ( _start + td >= _end )
td = (_end - _start) - timeline->x_to_ts( 1 );
2008-02-20 08:08:36 +01:00
_start += td;
_offset += td;
break;
}
case RIGHT:
{
int d = (( abs_x() - scroll_x() ) + abs_w() ) - X;
/* _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), d, h() ); */
2008-02-21 17:20:36 +01:00
long td = timeline->x_to_ts( d );
2008-02-18 02:13:20 +01:00
2008-02-24 08:42:41 +01:00
// printf( "%li %li\n", td, _end - _start );
2008-02-24 03:25:58 +01:00
if ( td >= 0 && _end - _start < td )
_end = _start + timeline->x_to_ts( 1 );
else
_end -= td;
2008-02-20 08:08:36 +01:00
break;
}
default:
return;
2008-02-16 05:23:58 +01:00
2008-02-20 08:08:36 +01:00
}
}
int
2008-02-20 07:35:29 +01:00
Region::handle ( int m )
{
2008-02-24 14:28:55 +01:00
static bool dragging = false;
2008-02-20 07:35:29 +01:00
static int ox, oy;
static enum trim_e trimming;
2008-02-20 07:35:29 +01:00
static bool copied = false;
static nframes_t os;
int X = Fl::event_x() - _track->x();
2008-02-20 07:35:29 +01:00
int Y = Fl::event_y();
2008-02-21 03:06:39 +01:00
int ret;
2008-02-24 14:28:55 +01:00
Logger _log( this );
//log_start();
2008-02-21 03:06:39 +01:00
2008-02-20 07:35:29 +01:00
switch ( m )
{
case FL_PUSH:
{
2008-02-20 07:35:29 +01:00
if ( Fl::event_state() & FL_SHIFT &&
! ( Fl::event_state() & FL_CTRL ))
{
switch ( Fl::event_button() )
{
2008-02-20 08:08:36 +01:00
case 1:
trim( trimming = LEFT, X );
_log.hold();
2008-02-20 08:08:36 +01:00
break;
case 3:
trim( trimming = RIGHT, X );
_log.hold();
2008-02-20 08:08:36 +01:00
break;
2008-02-24 08:48:07 +01:00
case 2:
{
/* split */
if ( ! copied )
{
Loggable::block_start();
2008-02-24 08:48:07 +01:00
Region *copy = new Region( *this );
trim( RIGHT, X );
copy->trim( LEFT, X );
_track->add( copy );
log_end();
Loggable::block_end();
return 1;
2008-02-24 08:48:07 +01:00
}
}
2008-02-20 07:35:29 +01:00
default:
return 0;
2008-02-24 08:42:41 +01:00
break;
2008-02-20 07:35:29 +01:00
}
2008-02-24 08:42:41 +01:00
2008-02-20 07:35:29 +01:00
fl_cursor( FL_CURSOR_WE );
return 1;
}
else
{
ox = x() - X;
oy = y() - Y;
2008-02-20 07:35:29 +01:00
if ( Fl::event_state() && FL_CTRL )
{
os = _start;
// Fl::local_grab( this );
}
2008-02-21 03:10:44 +01:00
if ( Fl::event_button() == 2 )
{
2008-02-21 10:51:07 +01:00
if ( Fl::event_state() & FL_CTRL )
normalize();
else
_selected = ! _selected;
2008-02-22 05:53:39 +01:00
redraw();
2008-02-24 08:42:41 +01:00
goto changed;
2008-02-21 03:10:44 +01:00
}
2008-02-24 08:42:41 +01:00
2008-02-21 03:06:39 +01:00
ret = Track_Widget::handle( m );
2008-02-24 08:42:41 +01:00
2008-02-21 03:06:39 +01:00
return ret | 1;
2008-02-20 07:35:29 +01:00
}
break;
}
case FL_RELEASE:
Track_Widget::handle( m );
2008-02-20 07:35:29 +01:00
copied = false;
if ( trimming != NO )
{
trimming = NO;
_log.release();
}
2008-02-24 14:28:55 +01:00
if ( dragging )
_log.release();
dragging = false;
2008-02-24 08:42:41 +01:00
goto changed;
2008-02-20 07:35:29 +01:00
case FL_DRAG:
2008-02-24 14:28:55 +01:00
if ( ! dragging )
{
_log.hold();
dragging = true;
}
2008-02-20 07:35:29 +01:00
if ( Fl::event_state() & FL_SHIFT &&
Fl::event_state() & FL_CTRL )
{
int d = (ox + X) - x();
2008-02-21 17:20:36 +01:00
long td = timeline->x_to_ts( d );
2008-02-20 07:35:29 +01:00
2008-02-20 12:48:42 +01:00
nframes_t W = _end - _start;
2008-02-20 07:35:29 +01:00
if ( td > 0 && os < td )
_start = 0;
else
_start = os - td;
2008-02-20 12:48:42 +01:00
_end = _start + W;
_track->redraw();
2008-02-20 07:35:29 +01:00
return 1;
}
2008-02-20 08:08:36 +01:00
if ( Fl::event_state() & FL_SHIFT )
if ( trimming )
{
trim( trimming, X );
return 1;
}
else
return 0;
2008-02-20 07:35:29 +01:00
if ( Fl::event_state() & FL_CTRL )
{
if ( ! copied )
{
_track->add( new Region( *this ) );
copied = true;
return 1;
}
}
2008-02-20 07:35:29 +01:00
if ( Y > y() + h() )
{
if ( _track->next() )
if ( Y > _track->next()->y() )
_track->next()->add( this );
2008-02-20 07:35:29 +01:00
}
else
if ( Y < y() )
{
if ( _track->prev() )
if ( Y < _track->prev()->y() + _track->prev()->h() )
_track->prev()->add( this );
2008-02-20 07:35:29 +01:00
}
// _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
2008-02-20 07:35:29 +01:00
2008-02-21 03:06:39 +01:00
ret = Track_Widget::handle( m );
return ret | 1;
2008-02-20 07:35:29 +01:00
default:
2008-02-21 03:06:39 +01:00
return Track_Widget::handle( m );
2008-02-20 07:35:29 +01:00
break;
}
2008-02-24 08:42:41 +01:00
changed:
2008-02-24 14:28:55 +01:00
// log_end();
2008-02-24 08:42:41 +01:00
return 1;
2008-02-20 07:35:29 +01:00
}
2008-02-14 08:40:47 +01:00
void
Region::draw_box( int X, int Y, int W, int H )
{
/* dirty hack to keep the box from flipping to vertical at small sizes */
2008-02-14 08:40:47 +01:00
fl_push_clip( x(), Y, w(), H );
2008-02-21 08:38:31 +01:00
if ( _selected )
2008-02-23 07:59:10 +01:00
fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
2008-02-21 10:48:53 +01:00
// fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
2008-02-21 08:38:31 +01:00
else
2008-02-23 07:59:10 +01:00
fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
2008-02-21 08:38:31 +01:00
fl_pop_clip();
}
2008-02-21 01:25:21 +01:00
/* Draw (part of) region. OX is pixel offset from start of timeline, X
Y W and H are the portion of the widget to draw (arrived at by
intersection of the clip and relative to OX) */
2008-02-14 08:40:47 +01:00
void
Region::draw ( int X, int Y, int W, int H )
2008-02-14 08:40:47 +01:00
{
2008-02-20 11:45:10 +01:00
if ( ! ( W > 0 && H > 0 ) )
return;
int OX = scroll_x();
2008-02-21 17:20:36 +01:00
int ox = timeline->ts_to_x( _offset );
2008-02-21 01:25:21 +01:00
if ( ox > OX + _track->w() ||
ox < OX && ox + abs_w() < OX )
2008-02-20 11:45:10 +01:00
return;
2008-02-21 17:20:36 +01:00
int rw = timeline->ts_to_x( _end - _start );
2008-02-20 11:45:10 +01:00
nframes_t end = _offset + ( _end - _start );
/* calculate waveform offset due to scrolling */
nframes_t offset = 0;
2008-02-21 01:25:21 +01:00
if ( ox < OX )
2008-02-20 11:45:10 +01:00
{
2008-02-21 17:20:36 +01:00
offset = timeline->x_to_ts( OX - ox );
2008-02-20 11:45:10 +01:00
2008-02-21 17:20:36 +01:00
rw = timeline->ts_to_x( (_end - _start) - offset );
2008-02-20 11:45:10 +01:00
}
rw = min( rw, _track->w() );
int rx = x();
2008-02-20 08:53:54 +01:00
fl_push_clip( rx, Y, rw, H );
/* dirty hack to keep the box from flipping to vertical at small sizes */
// fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
2008-02-20 13:45:02 +01:00
2008-02-14 08:40:47 +01:00
2008-02-16 05:23:58 +01:00
// fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
2008-02-26 10:18:16 +01:00
int ch = (h() - Fl::box_dh( box() )) / _clip->channels();
2008-02-22 19:40:51 +01:00
for ( int i = _clip->channels(); i--; )
// draw_waveform( rx, y() + (i * ch), rw, ch, _clip, i,
// _start + offset, min( (_end - _start) - offset, _end),
// _scale, _selected ? _color : fl_invert_color( _color ) );
2008-02-26 10:18:16 +01:00
draw_waveform( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch, _clip, i,
2008-02-22 19:40:51 +01:00
_start + offset, min( (_end - _start) - offset, _end),
_scale, _selected ? _color : fl_invert_color( _color ) );
2008-02-14 08:40:47 +01:00
2008-02-21 17:20:36 +01:00
timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
2008-02-20 13:45:02 +01:00
2008-02-20 08:53:54 +01:00
fl_color( FL_BLACK );
fl_line( rx, Y, rx, Y + H );
fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
2008-02-16 05:23:58 +01:00
draw_label( _clip->name(), align() );
2008-02-20 11:45:10 +01:00
2008-02-21 08:38:31 +01:00
/* if ( _selected ) */
/* { */
/* fl_color( selection_color() ); */
/* fl_line_style( FL_SOLID, 4 ); */
/* fl_rect( x(), y(), w(), h() ); */
/* fl_line_style( FL_SOLID, 0 ); */
/* } */
2008-02-21 07:12:21 +01:00
fl_pop_clip();
2008-02-20 11:45:10 +01:00
2008-02-14 08:40:47 +01:00
}
2008-02-21 03:10:44 +01:00
void
Region::normalize ( void )
{
printf( "normalize: start=%lu end=%lu\n", _start, _end );
2008-02-22 19:40:51 +01:00
/* FIXME: punt */
_scale = _clip->peaks( 0 )->normalization_factor( _start, _end );
2008-02-21 03:10:44 +01:00
}
2008-02-21 13:57:33 +01:00
void
Region::dump ( void )
{
// printf( "Region %p %lu { \"%s\" %lu %lu }\n", this, _offset, _clip->name(), _start, _end );
/* how about in STD? */
printf( "Region\n\t%p\n\toffset\n\t\t%lu\n\tranage\n\t\t%lu\n\t\t%lu\n\tsource\n\t\t\"%s\"\n\n", this, _offset, _start, _end, _clip->name() );
2008-02-21 13:57:33 +01:00
}