Flu_Helpers.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // $Id: Flu_Helpers.cpp,v 1.5 2004/11/21 02:42:34 jbryan Exp $
  2. /***************************************************************
  3. * FLU - FLTK Utility Widgets
  4. * Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
  5. *
  6. * This file and its content is protected by a software license.
  7. * You should have received a copy of this license with this file.
  8. * If not, please contact the Ohio Supercomputer Center immediately:
  9. * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
  10. *
  11. ***************************************************************/
  12. #include "FLU/Flu_Helpers.H"
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #define streq(a,b) (strcmp(a,b)==0)
  17. static int fl_Full_Find_In_Menu( const Fl_Menu_* menu, const Fl_Menu_Item* items, int &which, const char* fullname )
  18. {
  19. if( fullname == NULL )
  20. return -1;
  21. if( fullname[0] == '\0' )
  22. return -1;
  23. char *name = strdup( fullname );
  24. bool submenu = false;
  25. // strip off the first part of the path
  26. char* slash = strchr( name, '/' );
  27. if( slash )
  28. {
  29. *slash = '\0';
  30. submenu = true; // if there is a slash, then the first part is a submenu
  31. }
  32. // search for the name
  33. for(;;)
  34. {
  35. // if we're at the end, quit searching
  36. if( which >= menu->size() )
  37. {
  38. return -1;
  39. }
  40. bool match = false;
  41. if( items[which].label() )
  42. match = streq( name, items[which].label() );
  43. else
  44. match = false;
  45. // see if the name matches the next menu item
  46. if( match )
  47. {
  48. // if the path indicates this is a submenu...
  49. if( submenu )
  50. {
  51. // ...but the menu item does not indicate that it is a submenu, then we have a problem
  52. if( !items[which].submenu() )
  53. {
  54. free(name);
  55. return -1;
  56. }
  57. // ...the menu item agrees that it is a submenu, so recurse on the remaining items and path
  58. else
  59. {
  60. fullname += (slash-name) + 1;
  61. which++;
  62. free(name);
  63. return fl_Full_Find_In_Menu( menu, items, which, fullname );
  64. }
  65. }
  66. // we have an exact match and the the path indicates the item is not a submenu
  67. else
  68. {
  69. // if the item disagrees, then we have a problem
  70. //if( items[which].submenu() )
  71. //{
  72. // free(name);
  73. // return -1;
  74. //}
  75. // otherwise the path and the item are in agreement that this is the actual item
  76. // being searched for, so return it
  77. //else
  78. {
  79. free(name);
  80. return which;
  81. }
  82. }
  83. }
  84. // the name doesn't match, so skip to the next item
  85. else
  86. {
  87. // if the item is a submenu, skip all its children
  88. if( items[which].submenu() )
  89. {
  90. while( items[which].label() != 0 )
  91. which++;
  92. which++; // increment one more to eat the end-of-submenu marker
  93. }
  94. // otherwise just skip the item
  95. else
  96. which++;
  97. }
  98. }
  99. }
  100. int fl_Full_Find_In_Menu( const Fl_Menu_* menu, const char* fullname )
  101. {
  102. if( menu == NULL || fullname == NULL )
  103. return -1;
  104. const Fl_Menu_Item *items = menu->menu();
  105. if( items == NULL )
  106. return -1;
  107. // remove any leading '/' (there shouldn't be one...but just in case)
  108. if( fullname[0] == '/' )
  109. fullname++;
  110. // delete any 'flag' characters
  111. char *correctedName = strdup( fullname );
  112. {
  113. int index = 0;
  114. for( int i = 0; i < (int)strlen( fullname ); i++ )
  115. {
  116. if( fullname[i] == '&' && fullname[i+1] == '&' )
  117. correctedName[index++] = '&';
  118. else if( fullname[i] == '&' )
  119. continue;
  120. else if( fullname[i] == '_' )
  121. continue;
  122. else
  123. correctedName[index++] = fullname[i];
  124. }
  125. correctedName[index] = '\0';
  126. }
  127. // find the menu entry
  128. int which = 0;
  129. while( items[which].label() && which != menu->size() )
  130. {
  131. int val = fl_Full_Find_In_Menu( menu, items, which, correctedName );
  132. if( val != -1 )
  133. {
  134. free( correctedName );
  135. return val;
  136. }
  137. }
  138. free( correctedName );
  139. return -1;
  140. }
  141. int fl_Find_In_Menu( const Fl_Menu_* menu, const char* name )
  142. {
  143. if( menu == NULL )
  144. return -1;
  145. if( name == NULL )
  146. return -1;
  147. const Fl_Menu_Item *items = menu->menu();
  148. for( int i = 0; i < menu->size(); i++ )
  149. {
  150. if( items[i].label() == NULL )
  151. continue;
  152. if( strlen( items[i].label() ) == 0 )
  153. continue;
  154. if( strcmp( name, items[i].label() ) == 0 )
  155. return i;
  156. }
  157. return -1;
  158. }