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.

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