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.

227 lines
5.4KB

  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. static bool
  24. sort_func ( Track_Widget *lhs, Track_Widget *rhs )
  25. {
  26. return *lhs < *rhs;
  27. }
  28. void
  29. Track::sort ( void )
  30. {
  31. _widgets.sort( sort_func );
  32. }
  33. void
  34. Track::draw ( void )
  35. {
  36. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  37. return;
  38. fl_push_clip( x(), y(), w(), h() );
  39. Fl_Group::draw();
  40. timeline->draw_measure_lines( x(), y(), w(), h(), color() );
  41. int X, Y, W, H;
  42. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  43. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  44. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  45. (*r)->draw_box( X, Y, W, H );
  46. /* TODO: detect overlap and draw with transparency/crossfade */
  47. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  48. (*r)->draw( X, Y, W, H );
  49. fl_pop_clip();
  50. }
  51. void
  52. Track::remove ( Track_Widget *r )
  53. {
  54. // Logger _log( this );
  55. _widgets.remove( r );
  56. }
  57. void
  58. Track::remove_selected ( void )
  59. {
  60. Loggable::block_start();
  61. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  62. if ( (*r)->selected() )
  63. {
  64. Track_Widget *t = *r;
  65. _widgets.erase( r++ );
  66. delete t;
  67. }
  68. else
  69. ++r;
  70. Loggable::block_end();
  71. }
  72. Track_Widget *
  73. Track::event_widget ( void )
  74. {
  75. int ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  76. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  77. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  78. return (*r);
  79. return NULL;
  80. }
  81. void
  82. Track::add ( Track_Widget *r )
  83. {
  84. // Logger _log( this );
  85. if ( r->track() )
  86. {
  87. r->redraw();
  88. r->track()->remove( r );
  89. // r->track()->redraw();
  90. }
  91. r->track( this );
  92. _widgets.push_back( r );
  93. sort();
  94. }
  95. /* snap /r/ to nearest edge */
  96. void
  97. Track::snap ( Track_Widget *r )
  98. {
  99. const int snap_pixels = 10;
  100. const int rx1 = r->x();
  101. const int rx2 = r->x() + r->w();
  102. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  103. {
  104. const Track_Widget *w = (*i);
  105. if ( w == r )
  106. continue;
  107. const int wx1 = w->x();
  108. const int wx2 = w->x() + w->w();
  109. if ( abs( rx1 - wx2 ) < snap_pixels )
  110. {
  111. r->offset( w->offset() + w->length() + 1 );
  112. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  113. goto done;
  114. }
  115. if ( abs( rx2 - wx1 ) < snap_pixels )
  116. {
  117. r->offset( ( w->offset() - r->length() ) - 1 );
  118. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  119. goto done;
  120. }
  121. }
  122. {
  123. int nx = timeline->nearest_line( r->abs_x() );
  124. if ( nx >= 0 )
  125. {
  126. r->offset( timeline->x_to_ts( nx ) );
  127. return;
  128. }
  129. }
  130. // r->offset( timeline->x_to_ts( r->x() ) );
  131. done:
  132. return;
  133. // r->resize();
  134. // r->position( rx1, y() );
  135. }
  136. int
  137. Track::handle ( int m )
  138. {
  139. static Track_Widget *pushed;
  140. switch ( m )
  141. {
  142. case FL_MOVE:
  143. /* these aren't used, so don't bother doing lookups for them */
  144. return 1;
  145. default:
  146. {
  147. Track_Widget *r = pushed ? pushed : event_widget();
  148. if ( r )
  149. {
  150. int retval = r->dispatch( m );
  151. if ( retval && m == FL_PUSH )
  152. pushed = r;
  153. if ( retval && m == FL_RELEASE )
  154. pushed = NULL;
  155. while ( _delete_queue.size() )
  156. {
  157. delete _delete_queue.front();
  158. _delete_queue.pop();
  159. pushed = NULL;
  160. }
  161. return retval;
  162. }
  163. else
  164. return Fl_Group::handle( m );
  165. }
  166. }
  167. }