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.

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