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.

571 lines
13KB

  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_Scroll *o = scroll = new Fl_Scroll( X, Y + 24, W, H - 24 );
  190. o->box( FL_NO_BOX );
  191. // o->type( Fl_Scroll::HORIZONTAL_ALWAYS );
  192. // o->box( Fl_Scroll::BOTH );
  193. {
  194. Fl_Flowpack *o = mixer_strips = new Fl_Flowpack( X, Y + 24, W, H - 18 - 24 );
  195. label( "Non-Mixer" );
  196. align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE) );
  197. o->box( FL_NO_BOX );
  198. o->type( Fl_Pack::HORIZONTAL );
  199. o->hspacing( 2 );
  200. o->vspacing( 2 );
  201. o->end();
  202. Fl_Group::current()->resizable( o );
  203. }
  204. o->end();
  205. Fl_Group::current()->resizable( o );
  206. }
  207. end();
  208. // Fl::add_timeout( STATUS_UPDATE_FREQ, update_cb, this );
  209. update_menu();
  210. load_options();
  211. }
  212. Mixer::~Mixer ( )
  213. {
  214. DMESSAGE( "Destroying mixer" );
  215. save_options();
  216. /* FIXME: teardown */
  217. mixer_strips->clear();
  218. }
  219. void Mixer::resize ( int X, int Y, int W, int H )
  220. {
  221. Fl_Group::resize( X, Y, W, H );
  222. mixer_strips->resize( X, Y + 24, W, H - 18 - 24 );
  223. scroll->resize( X, Y + 24, W, H - 24 );
  224. rows( _rows );
  225. }
  226. void Mixer::add ( Mixer_Strip *ms )
  227. {
  228. MESSAGE( "Add mixer strip \"%s\"", ms->name() );
  229. mixer_strips->add( ms );
  230. ms->take_focus();
  231. rows( _rows );
  232. scroll->redraw();
  233. }
  234. void
  235. Mixer::quit ( void )
  236. {
  237. /* TODO: save project? */
  238. while ( Fl::first_window() ) Fl::first_window()->hide();
  239. }
  240. void
  241. Mixer::insert ( Mixer_Strip *ms, Mixer_Strip *before )
  242. {
  243. mixer_strips->remove( ms );
  244. mixer_strips->insert( *ms, before );
  245. scroll->redraw();
  246. }
  247. void
  248. Mixer::insert ( Mixer_Strip *ms, int i )
  249. {
  250. Mixer_Strip *before = (Mixer_Strip*)mixer_strips->child( i );
  251. insert( ms, before);
  252. }
  253. void
  254. Mixer::move_left ( Mixer_Strip *ms )
  255. {
  256. int i = mixer_strips->find( ms );
  257. if ( i > 0 )
  258. insert( ms, i - 1 );
  259. }
  260. void
  261. Mixer::move_right ( Mixer_Strip *ms )
  262. {
  263. int i = mixer_strips->find( ms );
  264. if ( i < mixer_strips->children() - 1 )
  265. insert( ms, i + 2 );
  266. }
  267. void Mixer::remove ( Mixer_Strip *ms )
  268. {
  269. MESSAGE( "Remove mixer strip \"%s\"", ms->name() );
  270. mixer_strips->remove( ms );
  271. parent()->redraw();
  272. }
  273. bool
  274. Mixer::contains ( Mixer_Strip *ms )
  275. {
  276. return ms->parent() == mixer_strips;
  277. }
  278. void
  279. Mixer::rows ( int n )
  280. {
  281. int sh;
  282. if ( n > 1 )
  283. {
  284. sh = (scroll->h() / n) - (mixer_strips->vspacing() * (n - 1));
  285. mixer_strips->flow( true );
  286. }
  287. else
  288. {
  289. sh = (scroll->h() - 18) / n;
  290. mixer_strips->flow( false );
  291. }
  292. if ( sh < Mixer_Strip::min_h() )
  293. {
  294. rows( ( scroll->h() - 18 ) / Mixer_Strip::min_h() );
  295. return;
  296. }
  297. int tw = 0;
  298. for ( int i = 0; i < mixer_strips->children(); ++i )
  299. {
  300. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  301. t->size( t->w(), sh );
  302. tw += t->w() + mixer_strips->hspacing();
  303. }
  304. if ( n > 1 )
  305. mixer_strips->size( scroll->w() - 18, mixer_strips->h() );
  306. else
  307. mixer_strips->size( tw, mixer_strips->h() );
  308. _rows = n;
  309. scroll->redraw();
  310. }
  311. void Mixer::update ( void )
  312. {
  313. THREAD_ASSERT( UI );
  314. for ( int i = mixer_strips->children(); i--; )
  315. {
  316. ((Mixer_Strip*)mixer_strips->child( i ))->update();
  317. }
  318. // redraw();
  319. }
  320. /** retrun a pointer to the track named /name/, or NULL if no track is named /name/ */
  321. Mixer_Strip *
  322. Mixer::track_by_name ( const char *name )
  323. {
  324. for ( int i = mixer_strips->children(); i-- ; )
  325. {
  326. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  327. if ( ! strcmp( name, t->name() ) )
  328. return t;
  329. }
  330. return NULL;
  331. }
  332. /** return a malloc'd string representing a unique name for a new track */
  333. char *
  334. Mixer::get_unique_track_name ( const char *name )
  335. {
  336. char pat[256];
  337. strcpy( pat, name );
  338. for ( int i = 1; track_by_name( pat ); ++i )
  339. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  340. return strdup( pat );
  341. }
  342. void
  343. Mixer::snapshot ( void )
  344. {
  345. for ( int i = 0; i < mixer_strips->children(); ++i )
  346. ((Mixer_Strip*)mixer_strips->child( i ))->log_children();
  347. }
  348. void
  349. Mixer::new_strip ( void )
  350. {
  351. add( new Mixer_Strip( get_unique_track_name( "Unnamed" ) ) );
  352. }
  353. bool
  354. Mixer::save ( void )
  355. {
  356. MESSAGE( "Saving state" );
  357. Loggable::snapshot_callback( &Mixer::snapshot, this );
  358. Loggable::snapshot( "snapshot" );
  359. return true;
  360. }
  361. static const char options_filename[] = "options";
  362. void
  363. Mixer::load_options ( void )
  364. {
  365. // save options
  366. char *path;
  367. asprintf( &path, "%s/options", user_config_dir );
  368. ((Fl_Menu_Settings*)menubar)->load( menubar->find_item( "&Options" ), path );
  369. free( path );
  370. }
  371. void
  372. Mixer::save_options ( void )
  373. {
  374. char *path;
  375. asprintf( &path, "%s/%s", user_config_dir, options_filename );
  376. ((Fl_Menu_Settings*)menubar)->dump( menubar->find_item( "&Options" ), path );
  377. free( path );
  378. }
  379. void
  380. Mixer::update_menu ( void )
  381. {
  382. bool b = Project::open();
  383. if ( b )
  384. {
  385. ((Fl_Menu_Item*)menubar->find_item( "&Mixer" ))->flags &= ~FL_MENU_INACTIVE;
  386. ((Fl_Menu_Item*)menubar->find_item( "&Project/&Save" ))->flags &= ~FL_MENU_INACTIVE;
  387. mixer_strips->activate();
  388. }
  389. else
  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->deactivate();
  394. }
  395. }
  396. int
  397. Mixer::handle ( int m )
  398. {
  399. if ( Fl_Group::handle( m ) )
  400. return 1;
  401. switch ( m )
  402. {
  403. case FL_ENTER:
  404. case FL_LEAVE:
  405. return 1;
  406. }
  407. return 0;
  408. }
  409. /************/
  410. /* Commands */
  411. /************/
  412. bool
  413. Mixer::command_save ( void )
  414. {
  415. return Project::save();
  416. }
  417. bool
  418. Mixer::command_load ( const char *path, const char *display_name )
  419. {
  420. if ( int err = Project::open( path ) )
  421. {
  422. // fl_alert( "Error opening project specified on commandline: %s", Project::errstr( err ) );
  423. return false;
  424. }
  425. if ( display_name )
  426. Project::name( display_name );
  427. update_menu();
  428. return true;
  429. }
  430. bool
  431. Mixer::command_new ( const char *path, const char *display_name )
  432. {
  433. if ( ! Project::create( path, "" ) )
  434. return false;
  435. if ( display_name )
  436. Project::name( display_name );
  437. update_menu();
  438. return true;
  439. // fl_alert( "Error creating project!" );
  440. }
  441. void
  442. Mixer::command_quit ( void )
  443. {
  444. quit();
  445. }
  446. /* */
  447. void
  448. Mixer::command_add_strip ( void )
  449. {
  450. new_strip();
  451. }