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.

321 lines
8.0KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 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. #include "Fl_Menu_Settings.H"
  19. #include <string.h>
  20. /* code to dump and restore (portions of) an Fl_Menu_ */
  21. #define MAX_PATH 1024
  22. void
  23. Fl_Menu_Settings::remove_ampersands ( char *str, int n )
  24. {
  25. char *d = str;
  26. char *s = str;
  27. while ( n-- )
  28. {
  29. if ( *s == '&' )
  30. {
  31. ++s;
  32. continue;
  33. }
  34. *(d++) = *(s++);
  35. }
  36. *d = '\0';
  37. }
  38. void
  39. Fl_Menu_Settings::indent ( FILE *fp, int n )
  40. {
  41. while ( n-- )
  42. fprintf( fp, "\t" );
  43. }
  44. int
  45. Fl_Menu_Settings::item_pathname_x ( char *path, int n, Fl_Menu_Item *item )
  46. {
  47. Fl_Menu_::item_pathname( path, n, item );
  48. remove_ampersands( path, n );
  49. }
  50. /** dump options from submenu /menu/ of menubar /bar/ to file /fp/ */
  51. Fl_Menu_Item *
  52. Fl_Menu_Settings::dump ( Fl_Menu_ *bar, Fl_Menu_Item *menu, FILE *fp, int depth )
  53. {
  54. static char path[256];
  55. Fl_Menu_Item *m = menu;
  56. for ( ; m->text; ++m )
  57. {
  58. bool is_radio = false;
  59. if ( m->flags & FL_SUBMENU )
  60. {
  61. strcpy( path, m->text );
  62. remove_ampersands( path, strlen( path ) );
  63. indent( fp, depth );
  64. fprintf( fp, "%s\n", path );
  65. /* recurse */
  66. m = dump( bar, ++m, fp, depth + 1 );
  67. continue;
  68. }
  69. if ( m->flags & FL_MENU_RADIO )
  70. is_radio = true;
  71. // bar->item_pathname( path, sizeof( path ) - 1, m );
  72. item_pathname_x( path, sizeof( path ) - 1, m );
  73. if ( m->flags & FL_MENU_TOGGLE || m->flags & FL_MENU_RADIO )
  74. {
  75. if ( ! is_radio )
  76. {
  77. indent( fp, depth );
  78. fprintf( fp, "%s\n", rindex( path, '/' ) + 1 );
  79. indent( fp, depth + 1 );
  80. fprintf( fp, "%s\n", m->flags & FL_MENU_VALUE ? "true" : "false" );
  81. }
  82. else if ( m->flags & FL_MENU_VALUE )
  83. {
  84. *rindex( path, '/' ) = '\0';
  85. indent( fp, depth );
  86. fprintf( fp, "%s\n", path + strlen( path ) + 1 );
  87. }
  88. }
  89. }
  90. return m;
  91. }
  92. /** dump menu to file /name/ starting at /item. */
  93. int
  94. Fl_Menu_Settings::dump ( Fl_Menu_Item *item, const char *name )
  95. {
  96. FILE *fp = fopen( name, "w" );
  97. if ( ! fp )
  98. return false;
  99. dump( this, item, fp, 0 );
  100. fclose( fp );
  101. return true;
  102. }
  103. #define strlcat strncat
  104. /* taken from Fl_Menu_.cxx and modified to ignore hotkeys and case */
  105. const Fl_Menu_Item *
  106. Fl_Menu_Settings::find_item_x ( const char *name, const Fl_Menu_Item *item )
  107. {
  108. char menupath [ MAX_PATH ] = ""; // File/Export
  109. const Fl_Menu_Item *m = item ? item : menu();
  110. int depth = 0;
  111. while ( depth >= 0 )
  112. for ( ;m ; ++m )
  113. {
  114. if ( m->flags & FL_SUBMENU )
  115. {
  116. // IT'S A SUBMENU
  117. // we do not support searches through FL_SUBMENU_POINTER links
  118. if ( menupath[0] )
  119. strlcat( menupath, "/", sizeof( menupath ) );
  120. strlcat( menupath, m->label(), sizeof( menupath ) );
  121. remove_ampersands( menupath, strlen( menupath ) );
  122. if ( ! strcasecmp( menupath, name ) )
  123. return m;
  124. else
  125. {
  126. ++depth;
  127. continue;
  128. }
  129. }
  130. else
  131. {
  132. if ( ! m->label() )
  133. {
  134. // END OF SUBMENU? Pop back one level.
  135. char *ss = strrchr( menupath, '/' );
  136. if ( ss )
  137. *ss = 0;
  138. else
  139. menupath[0] = '\0';
  140. --depth;
  141. ++m;
  142. break;
  143. }
  144. // IT'S A MENU ITEM
  145. char itempath[ MAX_PATH ]; // eg. Edit/Copy
  146. strcpy( itempath, menupath );
  147. if ( itempath[0] )
  148. strlcat( itempath, "/", sizeof( itempath ) );
  149. strlcat( itempath, m->label(), sizeof( itempath ) );
  150. remove_ampersands( itempath, strlen( itempath ) );
  151. if ( !strcasecmp( itempath, name ) )
  152. return m;
  153. }
  154. }
  155. return ( Fl_Menu_Item * )0;
  156. }
  157. static void
  158. path_push ( char *path, const char *s )
  159. {
  160. strcat( path, s );
  161. strcat( path, "/" );
  162. }
  163. static void
  164. path_pop ( char *path )
  165. {
  166. char *s;
  167. int l = strlen( path );
  168. if ( path[ l - 1 ] == '/' )
  169. path[ l - 1 ] = '\0';
  170. s = rindex( path, '/' );
  171. s = s ? s : path;
  172. *(s + 1) = '\0';
  173. }
  174. void
  175. Fl_Menu_Settings::load ( Fl_Menu_ *bar, Fl_Menu_Item *item, FILE *fp, int depth, char *path, int pmax )
  176. {
  177. char line[256];
  178. while ( ! feof( fp ) )
  179. {
  180. *line = '\0';
  181. fgets( line, sizeof( line ), fp );
  182. if ( *line == '#' )
  183. continue;
  184. line[ strlen( line ) - 1 ] = '\0';
  185. int ld = strspn( line, "\t" );
  186. if ( ld > depth )
  187. {
  188. path_push( path, line + ld );
  189. ++depth;
  190. // load( bar, item, fp, depth + 1, path, pmax );
  191. /* */;
  192. }
  193. else if ( ld < depth )
  194. {
  195. /* we should know the path and the value now */
  196. // path_pop( path );
  197. *rindex( path, '/' ) = '\0';
  198. // printf( "%s = %s\n", path, path + strlen( path ) + 1 );
  199. const Fl_Menu_Item *it = find_item_x( path, item + 1 );
  200. if ( it && it->radio() ) /* radio button */
  201. {
  202. bar->picked( it );
  203. path_pop( path );
  204. }
  205. else /* toggle */
  206. {
  207. *rindex( path, '/' ) = '\0';
  208. if ( ( it = find_item_x( path, item + 1 ) ) && it->checkbox() )
  209. {
  210. int v = 0 == strcasecmp( "true", (path + strlen( path ) + 1 ) );
  211. if ( v != ( it->value() != 0 ) /* grr, FLTK */ )
  212. bar->picked( it );
  213. }
  214. }
  215. while ( ld < depth )
  216. {
  217. path_pop( path );
  218. depth--;
  219. }
  220. path_push( path, line + ld );
  221. }
  222. else /* d == depth */
  223. {
  224. /* doesn't apply? */
  225. }
  226. }
  227. }
  228. /** load settings from file /name/ into menu starting at /item */
  229. int
  230. Fl_Menu_Settings::load ( Fl_Menu_Item *item, const char *name )
  231. {
  232. FILE *fp = fopen( name, "r" );
  233. if ( ! fp )
  234. return false;
  235. char path[ MAX_PATH ];
  236. path[0] = '\0';
  237. load( this, item, fp, 0, path, sizeof( path ) );
  238. fclose( fp );
  239. }