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.

201 lines
5.1KB

  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. void
  23. Track::draw ( void )
  24. {
  25. Fl_Group::draw();
  26. timeline.draw_measure_lines( x(), y(), w(), h(), color() );
  27. fl_push_clip( x(), y(), w(), h() );
  28. for ( list <Track_Widget *>::iterator r = _regions.begin(); r != _regions.end(); r++ )
  29. (*r)->draw( x(), y(), w(), h() );
  30. fl_pop_clip();
  31. }
  32. void
  33. Track::remove_region ( Track_Widget *r )
  34. {
  35. _regions.remove( r );
  36. }
  37. Track_Widget *
  38. Track::event_region ( void )
  39. {
  40. // FIXME: doesn't handle overlap!
  41. int ets = timeline.xoffset + timeline.x_to_ts( Fl::event_x() );
  42. for ( list <Track_Widget *>::iterator r = _regions.begin(); r != _regions.end(); r++ )
  43. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  44. return (*r);
  45. return NULL;
  46. }
  47. void
  48. Track::add ( Track_Widget *r )
  49. {
  50. if ( r->track() )
  51. {
  52. r->track()->remove_region( r );
  53. r->track()->redraw();
  54. }
  55. _regions.push_back( r );
  56. r->track( this );
  57. // Fl_Group::add( r );
  58. // add( r );
  59. // r->position( r->x(), y() );
  60. // r->redraw();
  61. }
  62. /* snap /r/ to nearest edge */
  63. void
  64. Track::snap ( Track_Widget *r )
  65. {
  66. const int snap_pixels = 10;
  67. int rx1 = r->x();
  68. int rx2 = r->x() + r->w();
  69. for ( list <Track_Widget*>::iterator i = _regions.begin(); i != _regions.end(); i++ )
  70. {
  71. const Track_Widget *w = (*i);
  72. if ( w == r )
  73. continue;
  74. int wx1 = w->x();
  75. int wx2 = w->x() + w->w();
  76. if ( abs( rx1 - wx2 ) < snap_pixels )
  77. {
  78. r->offset( w->offset() + w->length() + 1 );
  79. printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  80. goto done;
  81. }
  82. if ( abs( rx2 - wx1 ) < snap_pixels )
  83. {
  84. r->offset( ( w->offset() - r->length() ) - 1 );
  85. printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  86. goto done;
  87. }
  88. }
  89. // r->offset( timeline.x_to_ts( r->x() ) );
  90. done:
  91. return;
  92. // r->resize();
  93. // r->position( rx1, y() );
  94. }
  95. int
  96. Track::handle ( int m )
  97. {
  98. static Track_Widget *current_region;
  99. switch ( m )
  100. {
  101. case FL_DND_DRAG:
  102. case FL_DND_ENTER:
  103. case FL_ENTER:
  104. return 1;
  105. case FL_DND_LEAVE:
  106. case FL_DND_RELEASE:
  107. return 1;
  108. case FL_PASTE:
  109. {
  110. const char *text = Fl::event_text();
  111. char *file;
  112. if ( ! sscanf( text, "file://%a[^\r\n]\n", &file ) )
  113. {
  114. printf( "invalid drop \"%s\"\n", text );
  115. return 0;
  116. }
  117. printf( "pasted file \"%s\"\n", file );
  118. Clip *c = Clip::from_file( file );
  119. if ( ! c )
  120. {
  121. free( file );
  122. return 0;
  123. }
  124. Region *r = new Region( c );
  125. r->offset( timeline.x_to_ts( Fl::event_x() ) );
  126. // r->position( Fl::event_x(), r->y() );
  127. this->add( r );
  128. redraw();
  129. return 1;
  130. }
  131. case FL_MOVE:
  132. /* these aren't used, so don't bother doing lookups for them */
  133. return 1;
  134. default:
  135. {
  136. Track_Widget *r = event_region();
  137. if ( current_region )
  138. r = current_region;
  139. if ( r )
  140. {
  141. int retval = r->handle( m );
  142. if ( retval && m == FL_PUSH )
  143. current_region = r;
  144. if ( retval && m == FL_RELEASE )
  145. current_region = NULL;
  146. return retval;
  147. }
  148. else
  149. return Fl_Group::handle( m );
  150. }
  151. }
  152. }