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.

198 lines
4.9KB

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