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.

510 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. ms->take_focus();
  239. rows( _rows );
  240. scroll->redraw();
  241. }
  242. void
  243. Mixer::quit ( void )
  244. {
  245. /* TODO: save project? */
  246. while ( Fl::first_window() ) Fl::first_window()->hide();
  247. }
  248. void
  249. Mixer::insert ( Mixer_Strip *ms, Mixer_Strip *before )
  250. {
  251. mixer_strips->remove( ms );
  252. mixer_strips->insert( *ms, before );
  253. scroll->redraw();
  254. }
  255. void
  256. Mixer::insert ( Mixer_Strip *ms, int i )
  257. {
  258. Mixer_Strip *before = (Mixer_Strip*)mixer_strips->child( i );
  259. insert( ms, before);
  260. }
  261. void
  262. Mixer::move_left ( Mixer_Strip *ms )
  263. {
  264. int i = mixer_strips->find( ms );
  265. if ( i > 0 )
  266. insert( ms, i - 1 );
  267. }
  268. void
  269. Mixer::move_right ( Mixer_Strip *ms )
  270. {
  271. int i = mixer_strips->find( ms );
  272. if ( i < mixer_strips->children() - 1 )
  273. insert( ms, i + 2 );
  274. }
  275. void Mixer::remove ( Mixer_Strip *ms )
  276. {
  277. MESSAGE( "Remove mixer strip \"%s\"", ms->name() );
  278. mixer_strips->remove( ms );
  279. parent()->redraw();
  280. }
  281. bool
  282. Mixer::contains ( Mixer_Strip *ms )
  283. {
  284. return ms->parent() == mixer_strips;
  285. }
  286. void
  287. Mixer::rows ( int n )
  288. {
  289. int sh;
  290. if ( n > 1 )
  291. {
  292. sh = (scroll->h() / n) - (mixer_strips->vspacing() * (n - 1));
  293. mixer_strips->flow( true );
  294. }
  295. else
  296. {
  297. sh = (scroll->h() - 18) / n;
  298. mixer_strips->flow( false );
  299. }
  300. if ( sh < Mixer_Strip::min_h() )
  301. {
  302. rows( ( scroll->h() - 18 ) / Mixer_Strip::min_h() );
  303. return;
  304. }
  305. int tw = 0;
  306. for ( int i = 0; i < mixer_strips->children(); ++i )
  307. {
  308. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  309. t->size( t->w(), sh );
  310. tw += t->w() + mixer_strips->hspacing();
  311. }
  312. if ( n > 1 )
  313. mixer_strips->size( scroll->w() - 18, mixer_strips->h() );
  314. else
  315. mixer_strips->size( tw, mixer_strips->h() );
  316. _rows = n;
  317. scroll->redraw();
  318. }
  319. void Mixer::update ( void )
  320. {
  321. THREAD_ASSERT( UI );
  322. for ( int i = mixer_strips->children(); i--; )
  323. {
  324. ((Mixer_Strip*)mixer_strips->child( i ))->update();
  325. }
  326. // redraw();
  327. }
  328. /** retrun a pointer to the track named /name/, or NULL if no track is named /name/ */
  329. Mixer_Strip *
  330. Mixer::track_by_name ( const char *name )
  331. {
  332. for ( int i = mixer_strips->children(); i-- ; )
  333. {
  334. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  335. if ( ! strcmp( name, t->name() ) )
  336. return t;
  337. }
  338. return NULL;
  339. }
  340. /** return a malloc'd string representing a unique name for a new track */
  341. char *
  342. Mixer::get_unique_track_name ( const char *name )
  343. {
  344. char pat[256];
  345. strcpy( pat, name );
  346. for ( int i = 1; track_by_name( pat ); ++i )
  347. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  348. return strdup( pat );
  349. }
  350. void
  351. Mixer::snapshot ( void )
  352. {
  353. for ( int i = 0; i < mixer_strips->children(); ++i )
  354. ((Mixer_Strip*)mixer_strips->child( i ))->log_children();
  355. }
  356. void
  357. Mixer::new_strip ( void )
  358. {
  359. add( new Mixer_Strip( get_unique_track_name( "Unnamed" ) ) );
  360. }
  361. bool
  362. Mixer::save ( void )
  363. {
  364. MESSAGE( "Saving state" );
  365. Loggable::snapshot_callback( &Mixer::snapshot, this );
  366. Loggable::snapshot( "snapshot" );
  367. return true;
  368. }
  369. static const char options_filename[] = "options";
  370. void
  371. Mixer::load_options ( void )
  372. {
  373. // save options
  374. char *path;
  375. asprintf( &path, "%s/options", user_config_dir );
  376. ((Fl_Menu_Settings*)menubar)->load( menubar->find_item( "&Options" ), path );
  377. free( path );
  378. }
  379. void
  380. Mixer::save_options ( void )
  381. {
  382. char *path;
  383. asprintf( &path, "%s/%s", user_config_dir, options_filename );
  384. ((Fl_Menu_Settings*)menubar)->dump( menubar->find_item( "&Options" ), path );
  385. free( path );
  386. }
  387. int
  388. Mixer::handle ( int m )
  389. {
  390. if ( Fl_Group::handle( m ) )
  391. return 1;
  392. switch ( m )
  393. {
  394. case FL_ENTER:
  395. case FL_LEAVE:
  396. return 1;
  397. }
  398. return 0;
  399. }