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.

396 lines
9.3KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2009 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. /* This is the main mixer group. It contains and manages Mixer_Strips. */
  19. #include "Mixer.H"
  20. #include "Mixer_Strip.H"
  21. #include <FL/Fl_Pack.H>
  22. #include <FL/Fl_Scroll.H>
  23. #include <FL/Fl_Menu_Bar.H>
  24. #include <FL/fl_ask.H>
  25. #include <FL/Fl_File_Chooser.H>
  26. #include "New_Project_Dialog.H"
  27. #include "Engine/Engine.H"
  28. #include "FL/Fl_Flowpack.H"
  29. #include "Project.H"
  30. #include <string.h>
  31. #include "debug.h"
  32. const double STATUS_UPDATE_FREQ = 0.2f;
  33. #include "util/debug.h"
  34. static void update_cb( void *v ) {
  35. Fl::repeat_timeout( STATUS_UPDATE_FREQ, update_cb, v );
  36. ((Mixer*)v)->update();
  37. }
  38. void Mixer::cb_menu(Fl_Widget* o) {
  39. Fl_Menu_Bar *menu = (Fl_Menu_Bar*)o;
  40. /* const Fl_Menu_Item *mi = &menu->menu()[menu->value()]; */
  41. char picked[256];
  42. // const char *picked = menu->text();
  43. menu->item_pathname( picked, sizeof( picked ) );
  44. if (! strcmp( picked, "&Project/&New") )
  45. {
  46. DMESSAGE( "New project" );
  47. const char *templates[] = { "Default", NULL };
  48. char *default_path;
  49. char *selected_template;
  50. // read_line( user_config_dir, "default_path", &default_path );
  51. char *path = new_project_chooser( templates, &default_path, &selected_template );
  52. if ( ! Project::create( path, selected_template ) )
  53. fl_alert( "Error creating project!" );
  54. free( path );
  55. free( selected_template );
  56. free( default_path );
  57. // write_line( user_config_dir, "default_path", default_path );
  58. }
  59. else if (! strcmp( picked, "&Project/&Open" ) )
  60. {
  61. char *path = NULL;
  62. // read_line( user_config_dir, "default_path", &path );
  63. const char *name = fl_dir_chooser( "Open Project", path, NULL );
  64. free( path );
  65. mixer->hide();
  66. if ( int err = Project::open( name ) )
  67. {
  68. fl_alert( "Error opening project: %s", Project::errstr( err ) );
  69. mixer->show();
  70. }
  71. mixer->show();
  72. }
  73. else if (! strcmp( picked, "&Project/&Save" ) )
  74. {
  75. Project::save();
  76. }
  77. else if (! strcmp( picked, "&Project/&Quit") )
  78. {
  79. quit();
  80. }
  81. else if ( !strcmp( picked, "&Mixer/&Add Strip" ) )
  82. {
  83. new_strip();
  84. }
  85. else if ( !strcmp( picked, "&Mixer/Add &N Strips" ) )
  86. {
  87. const char *s = fl_input( "Enter number of strips to add" );
  88. if ( s )
  89. {
  90. for ( int i = atoi( s ); i > 0; i-- )
  91. new_strip();
  92. }
  93. }
  94. else if (! strcmp( picked, "&Mixer/&Rows/One") )
  95. {
  96. rows( 1 );
  97. }
  98. else if (! strcmp( picked, "&Mixer/&Rows/Two") )
  99. {
  100. rows( 2 );
  101. }
  102. else if (! strcmp( picked, "&Mixer/&Rows/Three") )
  103. {
  104. rows( 3 );
  105. }
  106. }
  107. void Mixer::cb_menu(Fl_Widget* o, void* v) {
  108. ((Mixer*)(v))->cb_menu(o);
  109. }
  110. Mixer::Mixer ( int X, int Y, int W, int H, const char *L ) :
  111. Fl_Group( X, Y, W, H, L )
  112. {
  113. _rows = 1;
  114. box( FL_NO_BOX );
  115. labelsize( 96 );
  116. { Fl_Menu_Bar *o = new Fl_Menu_Bar( X, Y, W, 24 );
  117. o->add( "&Project/&New" );
  118. o->add( "&Project/&Open" );
  119. o->add( "&Project/&Save", FL_CTRL + 's', 0, 0 );
  120. o->add( "&Project/&Quit", FL_CTRL + 'q', 0, 0 );
  121. o->add( "&Mixer/&Add Strip", 'a', 0, 0 );
  122. o->add( "&Mixer/Add &N Strips" );
  123. o->add( "&Mixer/&Rows/One", '1', 0, 0 );
  124. o->add( "&Mixer/&Rows/Two", '2', 0, 0 );
  125. o->add( "&Mixer/&Rows/Three", '3', 0, 0 );
  126. o->add( "_&Options" );
  127. o->add( "&Help/&Manual" );
  128. o->add( "&Help/&About" );
  129. o->callback( cb_menu, this );
  130. }
  131. { Fl_Scroll *o = scroll = new Fl_Scroll( X, Y + 24, W, H - 24 );
  132. o->box( FL_NO_BOX );
  133. // o->type( Fl_Scroll::HORIZONTAL_ALWAYS );
  134. // o->box( Fl_Scroll::BOTH );
  135. {
  136. Fl_Flowpack *o = mixer_strips = new Fl_Flowpack( X, Y + 24, W, H - 18 - 24 );
  137. label( "Non-Mixer" );
  138. align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE) );
  139. o->box( FL_NO_BOX );
  140. o->type( Fl_Pack::HORIZONTAL );
  141. o->hspacing( 2 );
  142. o->vspacing( 2 );
  143. o->end();
  144. Fl_Group::current()->resizable( o );
  145. }
  146. o->end();
  147. Fl_Group::current()->resizable( o );
  148. }
  149. end();
  150. // Fl::add_timeout( STATUS_UPDATE_FREQ, update_cb, this );
  151. MESSAGE( "Scanning for plugins..." );
  152. }
  153. Mixer::~Mixer ( )
  154. {
  155. DMESSAGE( "Destroying mixer" );
  156. /* FIXME: teardown */
  157. mixer_strips->clear();
  158. }
  159. void Mixer::resize ( int X, int Y, int W, int H )
  160. {
  161. Fl_Group::resize( X, Y, W, H );
  162. mixer_strips->resize( X, Y + 24, W, H - 18 - 24 );
  163. scroll->resize( X, Y + 24, W, H - 24 );
  164. rows( _rows );
  165. }
  166. void Mixer::add ( Mixer_Strip *ms )
  167. {
  168. MESSAGE( "Add mixer strip \"%s\"", ms->name() );
  169. mixer_strips->add( ms );
  170. rows( _rows );
  171. scroll->redraw();
  172. }
  173. void
  174. Mixer::quit ( void )
  175. {
  176. /* TODO: save project? */
  177. while ( Fl::first_window() ) Fl::first_window()->hide();
  178. }
  179. void
  180. Mixer::insert ( Mixer_Strip *ms, Mixer_Strip *before )
  181. {
  182. mixer_strips->remove( ms );
  183. mixer_strips->insert( *ms, before );
  184. scroll->redraw();
  185. }
  186. void
  187. Mixer::insert ( Mixer_Strip *ms, int i )
  188. {
  189. Mixer_Strip *before = (Mixer_Strip*)mixer_strips->child( i );
  190. insert( ms, before);
  191. }
  192. void
  193. Mixer::move_left ( Mixer_Strip *ms )
  194. {
  195. int i = mixer_strips->find( ms );
  196. if ( i > 0 )
  197. insert( ms, i - 1 );
  198. }
  199. void
  200. Mixer::move_right ( Mixer_Strip *ms )
  201. {
  202. int i = mixer_strips->find( ms );
  203. if ( i < mixer_strips->children() - 1 )
  204. insert( ms, i + 2 );
  205. }
  206. void Mixer::remove ( Mixer_Strip *ms )
  207. {
  208. MESSAGE( "Remove mixer strip \"%s\"", ms->name() );
  209. mixer_strips->remove( ms );
  210. parent()->redraw();
  211. }
  212. bool
  213. Mixer::contains ( Mixer_Strip *ms )
  214. {
  215. return ms->parent() == mixer_strips;
  216. }
  217. void
  218. Mixer::rows ( int n )
  219. {
  220. int sh;
  221. if ( n > 1 )
  222. sh = (scroll->h() / n) - (mixer_strips->vspacing() * (n - 1));
  223. else
  224. sh = (scroll->h() - 18) / n;
  225. if ( sh < Mixer_Strip::min_h() )
  226. return;
  227. int tw = 0;
  228. for ( int i = 0; i < mixer_strips->children(); ++i )
  229. {
  230. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  231. t->size( t->w(), sh );
  232. tw += t->w() + mixer_strips->hspacing();
  233. }
  234. if ( n > 1 )
  235. mixer_strips->size( scroll->w() - 18, mixer_strips->h() );
  236. else
  237. mixer_strips->size( tw, mixer_strips->h() );
  238. _rows = n;
  239. scroll->redraw();
  240. }
  241. void Mixer::update ( void )
  242. {
  243. THREAD_ASSERT( UI );
  244. for ( int i = mixer_strips->children(); i--; )
  245. {
  246. ((Mixer_Strip*)mixer_strips->child( i ))->update();
  247. }
  248. // redraw();
  249. }
  250. /** retrun a pointer to the track named /name/, or NULL if no track is named /name/ */
  251. Mixer_Strip *
  252. Mixer::track_by_name ( const char *name )
  253. {
  254. for ( int i = mixer_strips->children(); i-- ; )
  255. {
  256. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  257. if ( ! strcmp( name, t->name() ) )
  258. return t;
  259. }
  260. return NULL;
  261. }
  262. /** return a malloc'd string representing a unique name for a new track */
  263. char *
  264. Mixer::get_unique_track_name ( const char *name )
  265. {
  266. char pat[256];
  267. strcpy( pat, name );
  268. for ( int i = 1; track_by_name( pat ); ++i )
  269. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  270. return strdup( pat );
  271. }
  272. void
  273. Mixer::snapshot ( void )
  274. {
  275. for ( int i = 0; i < mixer_strips->children(); ++i )
  276. ((Mixer_Strip*)mixer_strips->child( i ))->log_children();
  277. }
  278. void
  279. Mixer::new_strip ( void )
  280. {
  281. add( new Mixer_Strip( get_unique_track_name( "Unnamed" ), 1 ) );
  282. }
  283. bool
  284. Mixer::save ( void )
  285. {
  286. MESSAGE( "Saving state" );
  287. Loggable::snapshot_callback( &Mixer::snapshot, this );
  288. Loggable::snapshot( "snapshot" );
  289. return true;
  290. }
  291. int
  292. Mixer::handle ( int m )
  293. {
  294. int r = Fl_Group::handle( m );
  295. switch ( m )
  296. {
  297. case FL_ENTER:
  298. case FL_LEAVE:
  299. return 1;
  300. default:
  301. return r;
  302. break;
  303. }
  304. // return 0;
  305. return r;
  306. }