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.

587 lines
14KB

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