00001
00020 #include "mfcpch.h"
00021 #include <stdio.h>
00022 #include "varable.h"
00023 #include "varblmen.h"
00024 #include "varblwin.h"
00025 #include "varabled.h"
00026 #include "cmndwin.h"
00027 #include "mainblk.h"
00028
00030 #define VARDIR "configs/"
00031
00032 const ERRCODE NO_VARIABLES_TO_EDIT = "No Variables defined to edit";
00033
00040 MENU_L_LIST *build_list_of_all_leaves() {
00041 INT_VARIABLE_C_IT int_it (INT_VARIABLE::get_head ());
00042 BOOL_VARIABLE_C_IT bool_it (BOOL_VARIABLE::get_head ());
00043 STRING_VARIABLE_C_IT str_it (STRING_VARIABLE::get_head ());
00044 double_VARIABLE_C_IT dbl_it (double_VARIABLE::get_head ());
00045
00046 MENU_L_LIST *all_leaves_list = new MENU_L_LIST;
00047 MENU_L_IT it;
00048
00049 it.set_to_list (all_leaves_list);
00050
00051 for (int_it.mark_cycle_pt (); !int_it.cycled_list (); int_it.forward ())
00052 it.add_to_end (new MENU_L (new INT_VAR_MENU_LEAF (int_it.data ())));
00053
00054 for (bool_it.mark_cycle_pt (); !bool_it.cycled_list (); bool_it.forward ())
00055 it.add_to_end (new MENU_L (new BOOL_VAR_MENU_LEAF (bool_it.data ())));
00056
00057 for (str_it.mark_cycle_pt (); !str_it.cycled_list (); str_it.forward ())
00058 it.add_to_end (new MENU_L (new STR_VAR_MENU_LEAF (str_it.data ())));
00059
00060 for (dbl_it.mark_cycle_pt (); !dbl_it.cycled_list (); dbl_it.forward ())
00061 it.add_to_end (new MENU_L (new DBL_VAR_MENU_LEAF (dbl_it.data ())));
00062
00063 return all_leaves_list;
00064 }
00065
00066
00077 MENU_ROOT *build_main_var_menu(
00078 MENU_L_LIST *all_leaves_list) {
00079 MENU_L_IT it;
00080 MENU_L_LIST sub_window_list;
00081 MENU_L_LIST sub_menu_list;
00082
00083 MENU_ROOT *main_root;
00084 VAR_NON_RADIO_MENU *main_menu;
00085 NON_RADIO_MENU *std_menu;
00086 MENU_ROOT *main_misc_root = NULL;
00087 VAR_NON_RADIO_MENU *main_misc_menu = NULL;
00088 MENU_ROOT *sub_window_root;
00089 VAR_NON_RADIO_MENU *current_sub_win_menu;
00090 VAR_NON_RADIO_MENU *sub_window_misc_menu;
00091 MENU_L *leaf;
00092 const char *full_name;
00093 char first_word[80];
00094 char first_two_words[80];
00095 int header_len;
00096 STRING varfile;
00097
00098 main_root = new MENU_ROOT;
00099 main_menu = new VAR_NON_RADIO_MENU ("SubMenus");
00100 main_root->add_child (main_menu);
00101
00102 while (!all_leaves_list->empty ()) {
00103
00104 it.set_to_list (all_leaves_list);
00105 full_name = (it.data ()->ptr)->cmp_str ();
00106
00107 get_first_words (full_name, 1, first_word);
00108 extract_sublist(all_leaves_list, first_word, &sub_window_list);
00109
00110
00111 if (sub_window_list.singleton ()) {
00112 if (main_misc_root == NULL) {
00113 main_misc_root = new MENU_ROOT;
00114 main_misc_menu = new VAR_NON_RADIO_MENU ("Miscellaneous");
00115 main_misc_root->add_child (main_misc_menu);
00116 }
00117 it.set_to_list (&sub_window_list);
00118 leaf = it.extract ();
00119 leaf->ptr->new_label (full_name);
00120 main_misc_menu->add_child (leaf);
00121 }
00122 else {
00123 sub_window_root = new MENU_ROOT;
00124 main_menu->add_child (new VAR_SUB_MENU (first_word,
00125 sub_window_root));
00126 sub_window_misc_menu = new VAR_NON_RADIO_MENU ("Miscellaneous");
00127 sub_window_misc_menu->
00128 add_child (new
00129 SIMPLE_MENU_LEAF ("Kill Sub Window", KILL_WINDOW_CMD));
00130 while (!sub_window_list.empty ()) {
00131 it.set_to_list (&sub_window_list);
00132 full_name = it.data ()->ptr->cmp_str ();
00133 get_first_words (full_name, 2, first_two_words);
00134 extract_sublist(&sub_window_list, first_two_words, &sub_menu_list);
00135
00136 it.set_to_list (&sub_menu_list);
00137
00138 if (sub_menu_list.singleton ()) {
00139 leaf = it.extract ();
00140 leaf->ptr->new_label (full_name);
00141 sub_window_misc_menu->add_child (leaf);
00142 }
00143 else {
00144
00145 current_sub_win_menu =
00146 new VAR_NON_RADIO_MENU(first_two_words);
00147 sub_window_root->add_child (current_sub_win_menu);
00148 header_len = strlen (first_two_words);
00149 while (!sub_menu_list.empty ()) {
00150 it.set_to_list (&sub_menu_list);
00151 leaf = it.extract ();
00152 leaf->ptr->new_label (leaf->ptr->cmp_str () +
00153 header_len);
00154 current_sub_win_menu->add_child (leaf);
00155 }
00156 }
00157 }
00158 sub_window_root->add_child (sub_window_misc_menu);
00159 }
00160 }
00161 if (main_misc_root != NULL)
00162 main_root->add_child (main_misc_root);
00163
00164 varfile = datadir;
00165 varfile += VARDIR;
00166 varfile += "edited";
00167
00168 std_menu = new NON_RADIO_MENU ("Build Config File");
00169 main_root->add_child (std_menu);
00170 std_menu->add_child (new VARIABLE_MENU_LEAF ("All Variables",
00171 WRITE_ALL_CMD,
00172 varfile.string ()));
00173 std_menu->add_child (new VARIABLE_MENU_LEAF ("Changed Variables Only",
00174 WRITE_CHANGED_CMD,
00175 varfile.string ()));
00176 return main_root;
00177 }
00178
00179
00186 void extract_sublist(
00187 MENU_L_LIST *source_list,
00188 char *leading_str,
00189 MENU_L_LIST *extracted_list
00190 ) {
00191 MENU_L_IT start_it(source_list);
00192 MENU_L_IT end_it(source_list);
00193 int match_len = strlen (leading_str);
00194
00195 while (!end_it.at_last () &&
00196 (strncmp (leading_str, end_it.data_relative (1)->ptr->cmp_str (),
00197 match_len) == 0))
00198 end_it.forward ();
00199 extracted_list->assign_to_sublist (&start_it, &end_it);
00200 }
00201
00202
00209 void get_first_words(
00210 const char *s,
00211 int n,
00212 char *t
00213 ) {
00214 int full_length = strlen (s);
00215 int reqd_len = 0;
00216 const char *next_word = s;
00217
00218 while ((n > 0) && reqd_len < full_length) {
00219 reqd_len += strcspn (next_word, "_") + 1;
00220 next_word += reqd_len;
00221 n--;
00222 }
00223 strncpy(t, s, reqd_len);
00224 t[reqd_len] = '\0';
00225 }
00226
00227
00234 int menu_item_sorter(
00235 const void *item1,
00236 const void *item2) {
00237 MENU_L *menu_l_1 = (*(MENU_L **) item1);
00238 MENU_L *menu_l_2 = (*(MENU_L **) item2);
00239
00240 MENU_NODE *node1;
00241 MENU_NODE *node2;
00242
00243 const char *str1;
00244 const char *str2;
00245
00246 node1 = menu_l_1->ptr;
00247 node2 = menu_l_2->ptr;
00248
00249 str1 = node1->cmp_str ();
00250 str2 = node2->cmp_str ();
00251
00252 return strcmp (str1, str2);
00253 }
00254
00255
00262 void start_variables_editor() {
00263 MENU_L_LIST *all_leaves_list = new MENU_L_LIST;
00264 MENU_L_IT it;
00265
00266 all_leaves_list = build_list_of_all_leaves ();
00267 it.set_to_list (all_leaves_list);
00268 if (it.empty ()) {
00269 NO_VARIABLES_TO_EDIT.error ("start_variables_editor", LOG, NULL);
00270 }
00271 it.sort (&menu_item_sorter);
00272
00273 new VARIABLES_WINDOW ("VarEditorMAIN",
00274 build_main_var_menu (all_leaves_list), NULL);
00275 }