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.

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