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.

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