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.

205 lines
5.0KB

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