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.

326 lines
8.1KB

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