Merge branch 'msys2'
[git-cheetah.git] / common / menuengine.c
blob0931207fb787164a012793913b2ba1ee0d1ba05a
2 #include "cache.h"
3 #include "menuengine.h"
5 struct menu_item *active_menu;
6 unsigned int next_active_item;
8 void reset_active_menu()
10 if (active_menu) {
11 do {
12 struct menu_item item;
13 next_active_item--;
15 item = active_menu[next_active_item];
16 if (MENU_ITEM_CLEANUP == item.selection) {
17 if (item.string)
18 free(item.string);
19 if (item.helptext)
20 free(item.helptext);
23 } while (next_active_item);
25 free(active_menu);
28 active_menu = 0;
29 next_active_item = 0;
32 void append_active_menu(const struct menu_item *item)
34 active_menu = realloc(active_menu,
35 (next_active_item + 1) * sizeof(struct menu_item));
36 active_menu[next_active_item] = *item;
37 next_active_item++;
40 void build_menu_items(struct git_data *data,
41 selection_to_mask *mask_builder,
42 const struct menu_item menu_def[],
43 const unsigned int menu_def_count,
44 void *platform)
46 unsigned int i = 0;
47 const unsigned int selection = mask_builder(data);
49 reset_active_menu();
51 if (MENU_ITEM_LAST == selection)
52 return;
54 for (i = 0;
55 i < menu_def_count && MENU_ITEM_LAST != menu_def[i].selection;
56 i++)
57 if ((menu_def[i].selection & selection) ==
58 menu_def[i].selection) {
59 if (menu_def[i].builder(data, &menu_def[i], platform))
60 append_active_menu(&menu_def[i]);
64 char *get_menu_item_text(unsigned int id)
66 if (!active_menu)
67 return NULL;
69 if (id > next_active_item)
70 return NULL;
72 return active_menu[id].helptext;
75 int handle_menu_item(void *data, unsigned int id)
77 if (id > next_active_item)
78 return 0;
80 return (active_menu[id].handler)(data, id);