36 #include "stock.h" |
36 #include "stock.h" |
37 #include "container.h" |
37 #include "container.h" |
38 #include "../common/context.h" |
38 #include "../common/context.h" |
39 #include "../ui/window.h" |
39 #include "../ui/window.h" |
40 |
40 |
41 UcxList *menus; |
41 #include <cx/linked_list.h> |
42 UcxList *current; |
42 #include <cx/array_list.h> |
43 |
43 |
44 void ui_menu(char *label) { |
44 static ui_menu_add_f createMenuItem[] = { |
45 // free current menu hierarchy |
45 /* UI_MENU */ add_menu_widget, |
46 ucx_list_free(current); |
46 /* UI_MENU_SUBMENU */ add_menu_widget, |
47 |
47 /* UI_MENU_ITEM */ add_menuitem_widget, |
48 // create menu |
48 /* UI_MENU_STOCK_ITEM */ add_menuitem_st_widget, |
49 UiMenu *menu = malloc(sizeof(UiMenu)); |
49 /* UI_MENU_CHECK_ITEM */ add_checkitem_widget, |
50 menu->item.add_to = (ui_menu_add_f)add_menu_widget; |
50 /* UI_MENU_CHECK_ITEM_NV */ add_checkitemnv_widget, |
51 |
51 /* UI_MENU_ITEM_LIST */ add_menuitem_list_widget, |
52 menu->label = label; |
52 /* UI_MENU_ITEM_LIST_NV */ NULL, // TODO |
53 menu->items = NULL; |
53 /* UI_MENU_SEPARATOR */ add_menuseparator_widget |
54 menu->parent = NULL; |
54 }; |
55 |
|
56 current = ucx_list_prepend(NULL, menu); |
|
57 menus = ucx_list_append(menus, menu); |
|
58 |
|
59 } |
|
60 |
|
61 void ui_submenu(char *label) { |
|
62 UiMenu *menu = malloc(sizeof(UiMenu)); |
|
63 menu->item.add_to = (ui_menu_add_f)add_menu_widget; |
|
64 |
|
65 menu->label = label; |
|
66 menu->items = NULL; |
|
67 menu->parent = NULL; |
|
68 |
|
69 // add submenu to current menu |
|
70 UiMenu *cm = current->data; |
|
71 cm->items = ucx_list_append(cm->items, menu); |
|
72 |
|
73 // set the submenu to current menu |
|
74 current = ucx_list_prepend(current, menu); |
|
75 } |
|
76 |
|
77 void ui_submenu_end() { |
|
78 if(ucx_list_size(current) < 2) { |
|
79 return; |
|
80 } |
|
81 current = ucx_list_remove(current, current); |
|
82 } |
|
83 |
|
84 void ui_menuitem(char *label, ui_callback f, void *userdata) { |
|
85 ui_menuitem_gr(label, f, userdata, -1); |
|
86 } |
|
87 |
|
88 void ui_menuitem_st(char *stockid, ui_callback f, void *userdata) { |
|
89 ui_menuitem_stgr(stockid, f, userdata, -1); |
|
90 } |
|
91 |
|
92 void ui_menuitem_gr(char *label, ui_callback f, void *userdata, ...) { |
|
93 if(!current) { |
|
94 return; |
|
95 } |
|
96 |
|
97 UiMenuItem *item = malloc(sizeof(UiMenuItem)); |
|
98 item->item.add_to = (ui_menu_add_f)add_menuitem_widget; |
|
99 |
|
100 item->label = label; |
|
101 item->userdata = userdata; |
|
102 item->callback = f; |
|
103 item->groups = NULL; |
|
104 |
|
105 // add groups |
|
106 va_list ap; |
|
107 va_start(ap, userdata); |
|
108 int group; |
|
109 while((group = va_arg(ap, int)) != -1) { |
|
110 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); |
|
111 } |
|
112 va_end(ap); |
|
113 |
|
114 UiMenu *cm = current->data; |
|
115 cm->items = ucx_list_append(cm->items, item); |
|
116 } |
|
117 |
|
118 void ui_menuitem_stgr(char *stockid, ui_callback f, void *userdata, ...) { |
|
119 if(!current) { |
|
120 return; |
|
121 } |
|
122 |
|
123 UiStMenuItem *item = malloc(sizeof(UiStMenuItem)); |
|
124 item->item.add_to = (ui_menu_add_f)add_menuitem_st_widget; |
|
125 |
|
126 item->stockid = stockid; |
|
127 item->userdata = userdata; |
|
128 item->callback = f; |
|
129 item->groups = NULL; |
|
130 |
|
131 // add groups |
|
132 va_list ap; |
|
133 va_start(ap, userdata); |
|
134 int group; |
|
135 while((group = va_arg(ap, int)) != -1) { |
|
136 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); |
|
137 } |
|
138 va_end(ap); |
|
139 |
|
140 UiMenu *cm = current->data; |
|
141 cm->items = ucx_list_append(cm->items, item); |
|
142 } |
|
143 |
|
144 void ui_menuseparator() { |
|
145 if(!current) { |
|
146 return; |
|
147 } |
|
148 |
|
149 UiMenuItemI *item = malloc(sizeof(UiMenuItemI)); |
|
150 item->add_to = (ui_menu_add_f)add_menuseparator_widget; |
|
151 |
|
152 UiMenu *cm = current->data; |
|
153 cm->items = ucx_list_append(cm->items, item); |
|
154 } |
|
155 |
|
156 |
|
157 void ui_checkitem(char *label, ui_callback f, void *userdata) { |
|
158 if(!current) { |
|
159 return; |
|
160 } |
|
161 |
|
162 UiCheckItem *item = malloc(sizeof(UiCheckItem)); |
|
163 item->item.add_to = (ui_menu_add_f)add_checkitem_widget; |
|
164 item->label = label; |
|
165 item->callback = f; |
|
166 item->userdata = userdata; |
|
167 |
|
168 UiMenu *cm = current->data; |
|
169 cm->items = ucx_list_append(cm->items, item); |
|
170 } |
|
171 |
|
172 void ui_checkitem_nv(char *label, char *vname) { |
|
173 if(!current) { |
|
174 return; |
|
175 } |
|
176 |
|
177 UiCheckItemNV *item = malloc(sizeof(UiCheckItemNV)); |
|
178 item->item.add_to = (ui_menu_add_f)add_checkitemnv_widget; |
|
179 item->varname = vname; |
|
180 item->label = label; |
|
181 |
|
182 UiMenu *cm = current->data; |
|
183 cm->items = ucx_list_append(cm->items, item); |
|
184 } |
|
185 |
|
186 void ui_menuitem_list(UiList *items, ui_callback f, void *userdata) { |
|
187 if(!current) { |
|
188 return; |
|
189 } |
|
190 |
|
191 UiMenuItemList *item = malloc(sizeof(UiMenuItemList)); |
|
192 item->item.add_to = (ui_menu_add_f)add_menuitem_list_widget; |
|
193 item->callback = f; |
|
194 item->userdata = userdata; |
|
195 item->list = items; |
|
196 |
|
197 UiMenu *cm = current->data; |
|
198 cm->items = ucx_list_append(cm->items, item); |
|
199 } |
|
200 |
|
201 |
55 |
202 // private menu functions |
56 // private menu functions |
203 void ui_create_menubar(UiObject *obj) { |
57 void ui_create_menubar(UiObject *obj) { |
|
58 UiMenu *menus = uic_get_menu_list(); |
204 if(!menus) { |
59 if(!menus) { |
205 return; |
60 return; |
206 } |
61 } |
207 |
62 |
208 Widget menubar = XmCreateMenuBar(obj->widget, "main_list", NULL, 0); |
63 Widget menubar = XmCreateMenuBar(obj->widget, "main_list", NULL, 0); |
209 XtManageChild(menubar); |
64 XtManageChild(menubar); |
210 |
65 |
211 UcxList *ls = menus; |
66 UiMenu *menu = menus; |
212 int menu_index = 0; |
67 int menu_index = 0; |
213 while(ls) { |
68 while(menu) { |
214 UiMenu *menu = ls->data; |
69 menu_index += add_menu_widget(menubar, menu_index, &menu->item, obj); |
215 menu_index += menu->item.add_to(menubar, menu_index, &menu->item, obj); |
|
216 |
70 |
217 ls = ls->next; |
71 menu = (UiMenu*)menu->item.next; |
218 } |
72 } |
219 } |
73 } |
220 |
74 |
221 int add_menu_widget(Widget parent, int i, UiMenuItemI *item, UiObject *obj) { |
75 int add_menu_widget(Widget parent, int i, UiMenuItemI *item, UiObject *obj) { |
222 UiMenu *menu = (UiMenu*)item; |
76 UiMenu *menu = (UiMenu*)item; |