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.

173 lines
4.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. static bool
  23. sort_func ( Track_Widget *lhs, Track_Widget *rhs )
  24. {
  25. return *lhs < *rhs;
  26. }
  27. void
  28. Track::sort ( void )
  29. {
  30. _widgets.sort( sort_func );
  31. }
  32. void
  33. Track::draw ( void )
  34. {
  35. Fl_Group::draw();
  36. timeline.draw_measure_lines( x(), y(), w(), h(), color() );
  37. fl_push_clip( x(), y(), w(), h() );
  38. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  39. (*r)->draw_box( x(), y(), w(), h() );
  40. /* TODO: detect overlap and draw with transparency/crossfade */
  41. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  42. (*r)->draw( x(), y(), w(), h() );
  43. fl_pop_clip();
  44. }
  45. void
  46. Track::remove ( Track_Widget *r )
  47. {
  48. _widgets.remove( r );
  49. }
  50. Track_Widget *
  51. Track::event_widget ( void )
  52. {
  53. // FIXME: doesn't handle overlap!
  54. int ets = timeline.xoffset + timeline.x_to_ts( Fl::event_x() );
  55. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  56. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  57. return (*r);
  58. return NULL;
  59. }
  60. void
  61. Track::add ( Track_Widget *r )
  62. {
  63. if ( r->track() )
  64. {
  65. r->track()->remove( r );
  66. r->track()->redraw();
  67. }
  68. _widgets.push_back( r );
  69. r->track( this );
  70. }
  71. /* snap /r/ to nearest edge */
  72. void
  73. Track::snap ( Track_Widget *r )
  74. {
  75. const int snap_pixels = 10;
  76. int rx1 = r->x();
  77. int rx2 = r->x() + r->w();
  78. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  79. {
  80. const Track_Widget *w = (*i);
  81. if ( w == r )
  82. continue;
  83. int wx1 = w->x();
  84. int wx2 = w->x() + w->w();
  85. if ( abs( rx1 - wx2 ) < snap_pixels )
  86. {
  87. r->offset( w->offset() + w->length() + 1 );
  88. printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  89. goto done;
  90. }
  91. if ( abs( rx2 - wx1 ) < snap_pixels )
  92. {
  93. r->offset( ( w->offset() - r->length() ) - 1 );
  94. printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  95. goto done;
  96. }
  97. }
  98. // r->offset( timeline.x_to_ts( r->x() ) );
  99. done:
  100. return;
  101. // r->resize();
  102. // r->position( rx1, y() );
  103. }
  104. int
  105. Track::handle ( int m )
  106. {
  107. static Track_Widget *current_widget;
  108. switch ( m )
  109. {
  110. case FL_MOVE:
  111. /* these aren't used, so don't bother doing lookups for them */
  112. return 1;
  113. default:
  114. {
  115. Track_Widget *r = event_widget();
  116. if ( current_widget )
  117. r = current_widget;
  118. if ( r )
  119. {
  120. int retval = r->handle( m );
  121. if ( retval && m == FL_PUSH )
  122. current_widget = r;
  123. if ( retval && m == FL_RELEASE )
  124. current_widget = NULL;
  125. return retval;
  126. }
  127. else
  128. return Fl_Group::handle( m );
  129. }
  130. }
  131. }