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.

335 lines
8.3KB

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