Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
5.7KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "Track.H"
  19. #include "Timeline.H"
  20. #include "Region.H"
  21. #include <FL/fl_draw.H>
  22. queue <Track_Widget *> Track::_delete_queue;
  23. void
  24. Track::sort ( void )
  25. {
  26. _widgets.sort( Track_Widget::sort_func );
  27. }
  28. void
  29. Track::draw ( void )
  30. {
  31. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  32. return;
  33. fl_push_clip( x(), y(), w(), h() );
  34. Fl_Group::draw();
  35. timeline->draw_measure_lines( x(), y(), w(), h(), color() );
  36. int X, Y, W, H;
  37. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  38. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  39. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  40. (*r)->draw_box( X, Y, W, H );
  41. /* TODO: detect overlap and draw with transparency/crossfade */
  42. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  43. (*r)->draw( X, Y, W, H );
  44. fl_pop_clip();
  45. }
  46. void
  47. Track::remove ( Track_Widget *r )
  48. {
  49. // Logger _log( this );
  50. _widgets.remove( r );
  51. }
  52. void
  53. Track::remove_selected ( void )
  54. {
  55. Loggable::block_start();
  56. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  57. if ( (*r)->selected() )
  58. {
  59. Track_Widget *t = *r;
  60. _widgets.erase( r++ );
  61. delete t;
  62. }
  63. else
  64. ++r;
  65. Loggable::block_end();
  66. }
  67. Track_Widget *
  68. Track::event_widget ( void )
  69. {
  70. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  71. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  72. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  73. return (*r);
  74. return NULL;
  75. }
  76. void
  77. Track::select_range ( int X, int W )
  78. {
  79. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  80. nframes_t ets = sts + timeline->x_to_ts( W );
  81. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  82. if ( ! ( (*r)->offset() > ets || (*r)->offset() + (*r)->length() < sts ) )
  83. (*r)->select();
  84. }
  85. void
  86. Track::add ( Track_Widget *r )
  87. {
  88. // Logger _log( this );
  89. if ( r->track() )
  90. {
  91. r->redraw();
  92. r->track()->remove( r );
  93. // r->track()->redraw();
  94. }
  95. r->track( this );
  96. _widgets.push_back( r );
  97. sort();
  98. }
  99. /* snap /r/ to nearest edge */
  100. void
  101. Track::snap ( Track_Widget *r )
  102. {
  103. const int snap_pixels = 10;
  104. const int rx1 = r->x();
  105. const int rx2 = r->x() + r->w();
  106. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  107. {
  108. const Track_Widget *w = (*i);
  109. if ( w == r )
  110. continue;
  111. const int wx1 = w->x();
  112. const int wx2 = w->x() + w->w();
  113. if ( abs( rx1 - wx2 ) < snap_pixels )
  114. {
  115. r->offset( w->offset() + w->length() + 1 );
  116. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  117. goto done;
  118. }
  119. if ( abs( rx2 - wx1 ) < snap_pixels )
  120. {
  121. r->offset( ( w->offset() - r->length() ) - 1 );
  122. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  123. goto done;
  124. }
  125. }
  126. {
  127. int nx = timeline->nearest_line( r->abs_x() );
  128. if ( nx >= 0 )
  129. {
  130. r->offset( timeline->x_to_ts( nx ) );
  131. return;
  132. }
  133. }
  134. // r->offset( timeline->x_to_ts( r->x() ) );
  135. done:
  136. return;
  137. // r->resize();
  138. // r->position( rx1, y() );
  139. }
  140. int
  141. Track::handle ( int m )
  142. {
  143. static Track_Widget *pushed;
  144. switch ( m )
  145. {
  146. case FL_MOVE:
  147. /* these aren't used, so don't bother doing lookups for them */
  148. return 1;
  149. default:
  150. {
  151. Track_Widget *r = pushed ? pushed : event_widget();
  152. if ( r )
  153. {
  154. int retval = r->dispatch( m );
  155. if ( retval && m == FL_PUSH )
  156. pushed = r;
  157. if ( retval && m == FL_RELEASE )
  158. pushed = NULL;
  159. while ( _delete_queue.size() )
  160. {
  161. delete _delete_queue.front();
  162. _delete_queue.pop();
  163. pushed = NULL;
  164. }
  165. return retval;
  166. }
  167. else
  168. return Fl_Group::handle( m );
  169. }
  170. }
  171. }