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.

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