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.

184 lines
4.8KB

  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. Track_Widget * Track::_queued_widget = NULL;
  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. Fl_Group::draw();
  37. timeline->draw_measure_lines( x(), y(), w(), h(), color() );
  38. fl_push_clip( 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. _widgets.remove( r );
  50. }
  51. Track_Widget *
  52. Track::event_widget ( void )
  53. {
  54. // FIXME: doesn't handle overlap!
  55. int ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() );
  56. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  57. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  58. return (*r);
  59. return NULL;
  60. }
  61. void
  62. Track::add ( Track_Widget *r )
  63. {
  64. if ( r->track() )
  65. {
  66. r->track()->remove( r );
  67. r->track()->redraw();
  68. }
  69. _widgets.push_back( r );
  70. r->track( this );
  71. }
  72. /* snap /r/ to nearest edge */
  73. void
  74. Track::snap ( Track_Widget *r )
  75. {
  76. const int snap_pixels = 10;
  77. int rx1 = r->x();
  78. int rx2 = r->x() + r->w();
  79. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  80. {
  81. const Track_Widget *w = (*i);
  82. if ( w == r )
  83. continue;
  84. int wx1 = w->x();
  85. int wx2 = w->x() + w->w();
  86. if ( abs( rx1 - wx2 ) < snap_pixels )
  87. {
  88. r->offset( w->offset() + w->length() + 1 );
  89. printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  90. goto done;
  91. }
  92. if ( abs( rx2 - wx1 ) < snap_pixels )
  93. {
  94. r->offset( ( w->offset() - r->length() ) - 1 );
  95. printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  96. goto done;
  97. }
  98. }
  99. // r->offset( timeline->x_to_ts( r->x() ) );
  100. done:
  101. return;
  102. // r->resize();
  103. // r->position( rx1, y() );
  104. }
  105. int
  106. Track::handle ( int m )
  107. {
  108. static Track_Widget *current_widget;
  109. switch ( m )
  110. {
  111. case FL_MOVE:
  112. /* these aren't used, so don't bother doing lookups for them */
  113. return 1;
  114. default:
  115. {
  116. Track_Widget *r = event_widget();
  117. if ( current_widget )
  118. r = current_widget;
  119. if ( r )
  120. {
  121. int retval = r->handle( m );
  122. if ( retval && m == FL_PUSH )
  123. current_widget = r;
  124. if ( retval && m == FL_RELEASE )
  125. current_widget = NULL;
  126. if ( _queued_widget )
  127. {
  128. remove( _queued_widget );
  129. delete _queued_widget;
  130. _queued_widget = NULL;
  131. current_widget = NULL;
  132. redraw();
  133. }
  134. return retval;
  135. }
  136. else
  137. return Fl_Group::handle( m );
  138. }
  139. }
  140. }