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.

511 lines
12KB

  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 "const.h"
  20. #include "Mixer.H"
  21. #include "Mixer_Strip.H"
  22. #include <FL/Fl_Pack.H>
  23. #include <FL/Fl_Scroll.H>
  24. #include <FL/Fl_Menu_Bar.H>
  25. #include <FL/fl_ask.H>
  26. #include <FL/Fl.H>
  27. #include <FL/Fl_File_Chooser.H>
  28. #include "New_Project_Dialog.H"
  29. #include "Engine/Engine.H"
  30. #include "FL/Fl_Flowpack.H"
  31. #include "Project.H"
  32. #include "FL/Fl_Menu_Settings.H"
  33. #include "About_Dialog.H"
  34. #include "util/file.h"
  35. #include <string.h>
  36. #include "debug.h"
  37. const double STATUS_UPDATE_FREQ = 0.2f;
  38. extern char *user_config_dir;
  39. #include "util/debug.h"
  40. static void update_cb( void *v ) {
  41. Fl::repeat_timeout( STATUS_UPDATE_FREQ, update_cb, v );
  42. ((Mixer*)v)->update();
  43. }
  44. void Mixer::cb_menu(Fl_Widget* o) {
  45. Fl_Menu_Bar *menu = (Fl_Menu_Bar*)o;
  46. /* const Fl_Menu_Item *mi = &menu->menu()[menu->value()]; */
  47. char picked[256];
  48. // const char *picked = menu->text();
  49. menu->item_pathname( picked, sizeof( picked ) );
  50. if (! strcmp( picked, "&Project/&New") )
  51. {
  52. DMESSAGE( "New project" );
  53. const char *templates[] = { "Default", NULL };
  54. char *default_path;
  55. char *selected_template;
  56. read_line( user_config_dir, "default_path", &default_path );
  57. char *path = new_project_chooser( templates, &default_path, &selected_template );
  58. if ( path )
  59. {
  60. if ( ! Project::create( path, selected_template ) )
  61. fl_alert( "Error creating project!" );
  62. free( path );
  63. free( selected_template );
  64. }
  65. if ( default_path )
  66. {
  67. write_line( user_config_dir, "default_path", default_path );
  68. free( default_path );
  69. }
  70. }
  71. else if (! strcmp( picked, "&Project/&Open" ) )
  72. {
  73. char *path = NULL;
  74. // read_line( user_config_dir, "default_path", &path );
  75. const char *name = fl_dir_chooser( "Open Project", path, NULL );
  76. free( path );
  77. mixer->hide();
  78. if ( int err = Project::open( name ) )
  79. {
  80. fl_alert( "Error opening project: %s", Project::errstr( err ) );
  81. mixer->show();
  82. }
  83. mixer->show();
  84. }
  85. else if (! strcmp( picked, "&Project/&Save" ) )
  86. {
  87. Project::save();
  88. }
  89. else if (! strcmp( picked, "&Project/&Quit") )
  90. {
  91. quit();
  92. }
  93. else if ( !strcmp( picked, "&Mixer/&Add Strip" ) )
  94. {
  95. new_strip();
  96. }
  97. else if ( !strcmp( picked, "&Mixer/Add &N Strips" ) )
  98. {
  99. const char *s = fl_input( "Enter number of strips to add" );
  100. if ( s )
  101. {
  102. for ( int i = atoi( s ); i > 0; i-- )
  103. new_strip();
  104. }
  105. }
  106. else if (! strcmp( picked, "&Mixer/&Rows/One") )
  107. {
  108. rows( 1 );
  109. }
  110. else if (! strcmp( picked, "&Mixer/&Rows/Two") )
  111. {
  112. rows( 2 );
  113. }
  114. else if (! strcmp( picked, "&Mixer/&Rows/Three") )
  115. {
  116. rows( 3 );
  117. }
  118. else if (! strcmp( picked, "&Options/&Display/&Style/&Default") )
  119. {
  120. Fl::scheme( "plastic" );
  121. }
  122. else if (! strcmp( picked, "&Options/&Display/&Style/&Flat") )
  123. {
  124. Fl::scheme( "gtk+" );
  125. }
  126. else if (! strcmp( picked, "&Options/&Display/&Colors/&System") )
  127. {
  128. //Fl::get_system_colors();
  129. unsigned char r, g, b;
  130. Fl::get_color( system_colors[ 0 ], r, g, b );
  131. Fl::background( r, g, b );
  132. Fl::get_color( system_colors[ 1 ], r, g, b );
  133. Fl::foreground( r, g, b );
  134. Fl::get_color( system_colors[ 2 ], r, g, b );
  135. Fl::background2( r, g, b );
  136. Fl::scheme( Fl::scheme() );
  137. }
  138. else if (! strcmp( picked, "&Options/&Display/&Colors/&Dark") )
  139. {
  140. Fl::background2( 100, 100, 100 );
  141. Fl::background( 50, 50, 50 );
  142. Fl::foreground( 255, 255, 255 );
  143. Fl::scheme( Fl::scheme() );
  144. }
  145. else if (! strcmp( picked, "&Options/&Display/&Colors/&Light") )
  146. {
  147. Fl::background2( 192, 192, 192 );
  148. Fl::background( 220, 220, 220 );
  149. Fl::foreground( 0, 0, 0 );
  150. Fl::scheme( Fl::scheme() );
  151. }
  152. else if ( ! strcmp( picked, "&Help/&About" ) )
  153. {
  154. About_Dialog ab;
  155. ab.run();
  156. }
  157. else if ( !strcmp( picked, "&Help/&Manual" ))
  158. {
  159. char *pat;
  160. asprintf( &pat, "file://%s%s.html", DOCUMENT_PATH, "MANUAL.html" );
  161. open_url( pat );
  162. free( pat );
  163. }
  164. }
  165. void Mixer::cb_menu(Fl_Widget* o, void* v) {
  166. ((Mixer*)(v))->cb_menu(o);
  167. }
  168. Mixer::Mixer ( int X, int Y, int W, int H, const char *L ) :
  169. Fl_Group( X, Y, W, H, L )
  170. {
  171. Fl::get_system_colors();
  172. Fl::scheme( "plastic" );
  173. system_colors[ 0 ] = (Fl_Color)Fl::get_color( FL_BACKGROUND_COLOR );
  174. system_colors[ 1 ] = (Fl_Color)Fl::get_color( FL_FOREGROUND_COLOR );
  175. system_colors[ 2 ] = (Fl_Color)Fl::get_color( FL_BACKGROUND2_COLOR );
  176. _rows = 1;
  177. box( FL_NO_BOX );
  178. labelsize( 96 );
  179. { Fl_Menu_Bar *o = menubar = new Fl_Menu_Bar( X, Y, W, 24 );
  180. o->add( "&Project/&New" );
  181. o->add( "&Project/&Open" );
  182. o->add( "&Project/&Save", FL_CTRL + 's', 0, 0 );
  183. o->add( "&Project/&Quit", FL_CTRL + 'q', 0, 0 );
  184. o->add( "&Mixer/&Add Strip", 'a', 0, 0 );
  185. o->add( "&Mixer/Add &N Strips" );
  186. o->add( "&Mixer/&Rows/One", '1', 0, 0 );
  187. o->add( "&Mixer/&Rows/Two", '2', 0, 0 );
  188. o->add( "&Mixer/&Rows/Three", '3', 0, 0 );
  189. o->add( "_&Options/&Display/&Style/&Default", 0, 0, 0, FL_MENU_RADIO | FL_MENU_VALUE );
  190. o->add( "_&Options/&Display/&Style/&Flat", 0, 0, 0, FL_MENU_RADIO );
  191. o->add( "_&Options/&Display/&Colors/&System", 0, 0, 0, FL_MENU_RADIO | FL_MENU_VALUE );
  192. o->add( "_&Options/&Display/&Colors/&Dark", 0, 0, 0, FL_MENU_RADIO );
  193. o->add( "_&Options/&Display/&Colors/&Light", 0, 0, 0, FL_MENU_RADIO );
  194. o->add( "&Help/&Manual" );
  195. o->add( "&Help/&About" );
  196. o->callback( cb_menu, this );
  197. }
  198. { Fl_Scroll *o = scroll = new Fl_Scroll( X, Y + 24, W, H - 24 );
  199. o->box( FL_NO_BOX );
  200. // o->type( Fl_Scroll::HORIZONTAL_ALWAYS );
  201. // o->box( Fl_Scroll::BOTH );
  202. {
  203. Fl_Flowpack *o = mixer_strips = new Fl_Flowpack( X, Y + 24, W, H - 18 - 24 );
  204. label( "Non-Mixer" );
  205. align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE) );
  206. o->box( FL_NO_BOX );
  207. o->type( Fl_Pack::HORIZONTAL );
  208. o->hspacing( 2 );
  209. o->vspacing( 2 );
  210. o->end();
  211. Fl_Group::current()->resizable( o );
  212. }
  213. o->end();
  214. Fl_Group::current()->resizable( o );
  215. }
  216. end();
  217. // Fl::add_timeout( STATUS_UPDATE_FREQ, update_cb, this );
  218. load_options();
  219. }
  220. Mixer::~Mixer ( )
  221. {
  222. DMESSAGE( "Destroying mixer" );
  223. save_options();
  224. /* FIXME: teardown */
  225. mixer_strips->clear();
  226. }
  227. void Mixer::resize ( int X, int Y, int W, int H )
  228. {
  229. Fl_Group::resize( X, Y, W, H );
  230. mixer_strips->resize( X, Y + 24, W, H - 18 - 24 );
  231. scroll->resize( X, Y + 24, W, H - 24 );
  232. rows( _rows );
  233. }
  234. void Mixer::add ( Mixer_Strip *ms )
  235. {
  236. MESSAGE( "Add mixer strip \"%s\"", ms->name() );
  237. mixer_strips->add( ms );
  238. rows( _rows );
  239. scroll->redraw();
  240. }
  241. void
  242. Mixer::quit ( void )
  243. {
  244. /* TODO: save project? */
  245. while ( Fl::first_window() ) Fl::first_window()->hide();
  246. }
  247. void
  248. Mixer::insert ( Mixer_Strip *ms, Mixer_Strip *before )
  249. {
  250. mixer_strips->remove( ms );
  251. mixer_strips->insert( *ms, before );
  252. scroll->redraw();
  253. }
  254. void
  255. Mixer::insert ( Mixer_Strip *ms, int i )
  256. {
  257. Mixer_Strip *before = (Mixer_Strip*)mixer_strips->child( i );
  258. insert( ms, before);
  259. }
  260. void
  261. Mixer::move_left ( Mixer_Strip *ms )
  262. {
  263. int i = mixer_strips->find( ms );
  264. if ( i > 0 )
  265. insert( ms, i - 1 );
  266. }
  267. void
  268. Mixer::move_right ( Mixer_Strip *ms )
  269. {
  270. int i = mixer_strips->find( ms );
  271. if ( i < mixer_strips->children() - 1 )
  272. insert( ms, i + 2 );
  273. }
  274. void Mixer::remove ( Mixer_Strip *ms )
  275. {
  276. MESSAGE( "Remove mixer strip \"%s\"", ms->name() );
  277. mixer_strips->remove( ms );
  278. parent()->redraw();
  279. }
  280. bool
  281. Mixer::contains ( Mixer_Strip *ms )
  282. {
  283. return ms->parent() == mixer_strips;
  284. }
  285. void
  286. Mixer::rows ( int n )
  287. {
  288. int sh;
  289. if ( n > 1 )
  290. {
  291. sh = (scroll->h() / n) - (mixer_strips->vspacing() * (n - 1));
  292. mixer_strips->flow( true );
  293. }
  294. else
  295. {
  296. sh = (scroll->h() - 18) / n;
  297. mixer_strips->flow( false );
  298. }
  299. if ( sh < Mixer_Strip::min_h() )
  300. {
  301. rows( ( scroll->h() - 18 ) / Mixer_Strip::min_h() );
  302. return;
  303. }
  304. int tw = 0;
  305. for ( int i = 0; i < mixer_strips->children(); ++i )
  306. {
  307. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  308. t->size( t->w(), sh );
  309. tw += t->w() + mixer_strips->hspacing();
  310. }
  311. if ( n > 1 )
  312. mixer_strips->size( scroll->w() - 18, mixer_strips->h() );
  313. else
  314. mixer_strips->size( tw, mixer_strips->h() );
  315. _rows = n;
  316. scroll->redraw();
  317. }
  318. void Mixer::update ( void )
  319. {
  320. THREAD_ASSERT( UI );
  321. for ( int i = mixer_strips->children(); i--; )
  322. {
  323. ((Mixer_Strip*)mixer_strips->child( i ))->update();
  324. }
  325. // redraw();
  326. }
  327. /** retrun a pointer to the track named /name/, or NULL if no track is named /name/ */
  328. Mixer_Strip *
  329. Mixer::track_by_name ( const char *name )
  330. {
  331. for ( int i = mixer_strips->children(); i-- ; )
  332. {
  333. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  334. if ( ! strcmp( name, t->name() ) )
  335. return t;
  336. }
  337. return NULL;
  338. }
  339. /** return a malloc'd string representing a unique name for a new track */
  340. char *
  341. Mixer::get_unique_track_name ( const char *name )
  342. {
  343. char pat[256];
  344. strcpy( pat, name );
  345. for ( int i = 1; track_by_name( pat ); ++i )
  346. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  347. return strdup( pat );
  348. }
  349. void
  350. Mixer::snapshot ( void )
  351. {
  352. for ( int i = 0; i < mixer_strips->children(); ++i )
  353. ((Mixer_Strip*)mixer_strips->child( i ))->log_children();
  354. }
  355. void
  356. Mixer::new_strip ( void )
  357. {
  358. add( new Mixer_Strip( get_unique_track_name( "Unnamed" ) ) );
  359. }
  360. bool
  361. Mixer::save ( void )
  362. {
  363. MESSAGE( "Saving state" );
  364. Loggable::snapshot_callback( &Mixer::snapshot, this );
  365. Loggable::snapshot( "snapshot" );
  366. return true;
  367. }
  368. static const char options_filename[] = "options";
  369. void
  370. Mixer::load_options ( void )
  371. {
  372. // save options
  373. char *path;
  374. asprintf( &path, "%s/options", user_config_dir );
  375. ((Fl_Menu_Settings*)menubar)->load( menubar->find_item( "&Options" ), path );
  376. free( path );
  377. }
  378. void
  379. Mixer::save_options ( void )
  380. {
  381. char *path;
  382. asprintf( &path, "%s/%s", user_config_dir, options_filename );
  383. ((Fl_Menu_Settings*)menubar)->dump( menubar->find_item( "&Options" ), path );
  384. free( path );
  385. }
  386. int
  387. Mixer::handle ( int m )
  388. {
  389. int r = Fl_Group::handle( m );
  390. switch ( m )
  391. {
  392. case FL_ENTER:
  393. case FL_LEAVE:
  394. return 1;
  395. default:
  396. return r;
  397. break;
  398. }
  399. // return 0;
  400. return r;
  401. }