2 * Copyright Neil Brown ©2023 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * trim a line off the top line of a pane and place a menu
6 * bar. Actions are sent to the focus.
8 * We place a renderline at the top and construct a string
9 * to give to it as needed.
10 * We create menu documents as children of the main pane,
11 * and display them as needed.
13 * Menus are added either to LHS or RHS and must be added in order
14 * So FILE EDIT VIEW must be in order, and HELP on right before
15 * any other right-side menus.
16 * Before displayed a menu, the pane which requested it is given
17 * a chance to update the content via a "menu:refresh" notification.
19 * Menus are created and populated with "menubar-add" which acts
20 * like menu-add, The name is "X/Y" where is the name of the menu
21 * and Y is the name in the menu. If X doesn't exist, the menu
22 * is created. If Y already exists, the other details are updated.
23 * menubar-delete and menubar-clear can delete individual menus,
24 * or clear all entries so they can be repopulated.
26 * Menu documents are collected as children of this pane. The focus
27 * of each document is the pane which requested the window. This
28 * allows the menu to be discarded when that pane is closed, and to
29 * be hidden when the pane loses focus.
31 * Child panes have ->z value:
33 * 1 or more for active menu
34 * -1 for menu documents created by in-focus clients
35 * -2 for menu documents created by not-in-focus clients
39 #define PANE_DATA_TYPE struct mbinfo
43 struct pane *bar safe;
45 struct pane *menu, *open;
48 #include "core-pane.h"
50 static struct map *menubar_map;
51 DEF_LOOKUP_CMD(menubar_handle, menubar_map);
53 DEF_CMD(menubar_border)
55 struct mbinfo *mbi = ci->home->data;
57 mbi->hidden = !mbi->wanted || ci->num <= 0;
58 pane_damaged(ci->home, DAMAGED_SIZE);
62 DEF_CMD(menubar_refresh_size)
64 struct mbinfo *mbi = ci->home->data;
65 struct pane *p = mbi->bar;
68 /* Put bar below window - out of sight */
69 pane_resize(p, 0, ci->home->h,
72 pane_resize(mbi->child, 0, 0,
73 ci->home->w, ci->home->h);
75 pane_resize(p, 0, 0, ci->home->w, ci->home->h/3);
76 call("render-line:measure", p, -1);
77 if (mbi->child && ci->home->h > p->h)
78 pane_resize(mbi->child, 0, p->h,
79 ci->home->w, ci->home->h - p->h);
81 pane_damaged(ci->home, DAMAGED_VIEW);
85 DEF_CMD(menubar_child_notify)
87 struct mbinfo *mbi = ci->home->data;
93 if (ci->home->focus == ci->focus)
94 ci->home->focus = NULL;
98 pane_close(mbi->child);
99 mbi->child = ci->focus;
100 ci->home->focus = ci->focus;
105 DEF_CMD(menubar_refresh)
108 struct pane *home = ci->home;
109 struct mbinfo *mbi = home->data;
111 struct pane *bar = mbi->bar;
119 buf_concat(&b, ACK SOH "tab:20" STX);
121 list_for_each_entry(p, &home->children, siblings) {
126 /* Strange - every doc should have a focus... */
130 if (!pane_has_focus(p->focus, mbi->child))
131 /* Owner of this menu not in focus */
133 n = pane_attr_get(p, "doc-name");
136 for (c = n ; *c; c++)
137 if (*c == ',' || (*c > 0 && *c < ' '))
139 if (mbi->menu && mbi->open == p)
140 buf_concat(&b, SOH "fg:black,bg:white-80,"
143 buf_concat(&b, SOH "fg:blue,underline,"
148 buf_concat(&b, ETX " ");
154 call("render-line:set", bar, -1, NULL, buf_final(&b),
155 0, NULL, "bg:#ffa500+50");
156 pane_resize(bar, 0, 0, bar->w, home->h/3);
157 call("render-line:measure", bar, -1);
159 pane_damaged(home, DAMAGED_SIZE);
169 static struct pane *menubar_find(struct pane *home safe,
171 const char *name safe,
172 enum create_where create)
174 struct pane *p, *m, *d;
176 struct pane *last_left = NULL;
178 list_for_each_entry(p, &home->children, siblings) {
182 /* Strange - every doc should have a focus... */
184 /* If no owner, then we only want currently visible docs */
185 if (!owner && p->z != -1)
187 a = pane_attr_get(p, "doc-name");
188 if (owner && p->focus != owner)
190 if (!a || strcmp(name, a) != 0)
194 if (create == C_NOWHERE || !owner)
196 m = call_ret(pane, "attach-menu", home, 0, NULL, "DV", 0, NULL,
200 d = call_ret(pane, "doc:get-doc", m);
202 call("doc:set:autoclose", d, 0);
203 call("popup:close", m);
206 call("doc:set-name", d, 0, NULL, name);
207 call("doc:set:menubar-side", d, 0, NULL,
208 create == C_LEFT ? "left" : "right");
209 /* Find insertion point */
210 list_for_each_entry(p, &home->children, siblings) {
214 /* Strange - every doc should have a focus... */
216 a = pane_attr_get(p, "menubar-side");
217 if (a && strcmp(a, "left") == 0)
221 pane_reparent(d, home);
223 pane_add_notify(home, owner, "Notify:Close");
225 list_move(&d->siblings, &last_left->siblings);
226 else if (create == C_RIGHT)
227 list_move_tail(&d->siblings, &home->children);
228 pane_damaged(home, DAMAGED_VIEW);
238 if (!ci->str || !ci->str2)
241 val = strchr(ci->str, '/');
244 menu = strndup(ci->str, val - ci->str);
246 d = menubar_find(ci->home, ci->focus, menu,
247 ci->num & 2 ? C_RIGHT : C_LEFT);
252 call("menu:add", d, 0, NULL, val, 0, NULL, ci->str2);
256 DEF_CMD(menubar_delete)
262 d = menubar_find(ci->home, ci->focus, ci->str, C_NOWHERE);
269 DEF_CMD(menubar_clear)
275 d = menubar_find(ci->home, ci->focus, ci->str, C_NOWHERE);
278 call("menu:clear", d);
282 DEF_CMD(menubar_done)
284 struct pane *home = ci->home;
285 struct mbinfo *mbi = home->data;
288 pane_focus(mbi->child);
289 call("Keystroke-sequence", home, 0, NULL, ci->str);
293 DEF_CMD(menubar_root)
295 /* Provide a pane for popup to attach to */
296 comm_call(ci->comm2, "cb", ci->home);
300 DEF_CMD(menubar_view_changed)
302 //struct mbinfo *mbi = ci->home->data;
304 //ci->home->focus = mbi->child;
308 DEF_CMD(menubar_press)
310 struct mbinfo *mbi = ci->home->data;
311 struct call_return cr;
316 if (ci->focus != mbi->bar)
319 call("popup:close", mbi->menu);
321 pane_damaged(ci->home, DAMAGED_VIEW);
323 cih = pane_mapxy(mbi->bar, ci->home,
324 ci->x == INT_MAX ? ci->focus->cx : ci->x,
325 ci->y == INT_MAX ? ci->focus->cy : ci->y,
327 cr = pane_call_ret(all, mbi->bar, "render-line:findxy",
328 mbi->bar, -1, NULL, NULL,
333 if (cr.s && sscanf(cr.s, "%dx%d,", &x, &y) == 2) {
337 list_for_each_entry(p, &ci->home->children, siblings) {
342 if (p->x < cr.ret - 1)
345 mbi->menu = call_ret(pane, "attach-menu", p, 0, NULL, "DVF",
349 pane_add_notify(ci->home, mbi->menu, "Notify:Close");
352 pane_damaged(ci->home, DAMAGED_VIEW);
358 DEF_CMD(menubar_release)
360 struct mbinfo *mbi = ci->home->data;
361 struct pane *c = pane_my_child(ci->home, ci->focus);
366 /* any button maps to -3 for menu action */
367 return home_call(ci->home->parent, "M:Release-3", ci->focus,
368 ci->num, ci->mark, ci->str,
369 ci->num2, ci->mark2, ci->str2,
370 ci->x, ci->y, ci->comm2);
373 DEF_CMD(menubar_close_notify)
375 struct mbinfo *mbi = ci->home->data;
378 if (ci->focus == mbi->menu) {
380 pane_damaged(ci->home, DAMAGED_VIEW);
383 if (ci->focus == mbi->child) {
387 if (ci->focus == mbi->bar) {
391 list_for_each_entry(p, &ci->home->children, siblings) {
394 if (p->focus == ci->focus) {
403 DEF_CMD(menubar_attach)
405 struct pane *ret, *mbp;
407 char *v = pane_attr_get(ci->focus, "menubar-visible");
409 ret = pane_register(ci->focus, 0, &menubar_handle.c);
414 if (v && strcmp(v, "no") == 0)
416 mbi->hidden = ! mbi->wanted;
417 mbp = call_ret(pane, "attach-renderline", ret, 1);
423 pane_damaged(ret, DAMAGED_VIEW);
424 return comm_call(ci->comm2, "callback:attach", ret);
427 void edlib_init(struct pane *ed safe)
429 call_comm("global-set-command", ed, &menubar_attach,
430 0, NULL, "attach-menubar");
432 menubar_map = key_alloc();
433 key_add(menubar_map, "Display:border", &menubar_border);
434 key_add(menubar_map, "Refresh:size", &menubar_refresh_size);
435 key_add(menubar_map, "Child-Notify", &menubar_child_notify);
436 key_add(menubar_map, "Refresh:view", &menubar_refresh);
437 key_add(menubar_map, "menubar-add", &menubar_add);
438 key_add(menubar_map, "menubar-delete", &menubar_delete);
439 key_add(menubar_map, "menubar-clear", &menubar_clear);
440 key_add(menubar_map, "menubar-done", &menubar_done);
441 key_add(menubar_map, "RootPane", &menubar_root);
442 key_add(menubar_map, "Notify:Close", &menubar_close_notify);
443 key_add(menubar_map, "view:changed", &menubar_view_changed);
444 key_add_prefix(menubar_map, "M:Press-", &menubar_press);
445 key_add_prefix(menubar_map, "M:Release-", &menubar_release);