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
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;
67 if (!p || mbi->hidden) {
68 /* Put bar below window - out of sight */
70 pane_resize(p, 0, ci->home->h,
73 pane_resize(mbi->child, 0, 0,
74 ci->home->w, ci->home->h);
76 pane_resize(p, 0, 0, ci->home->w, ci->home->h/3);
77 call("render-line:measure", p, -1);
78 if (mbi->child && ci->home->h > p->h)
79 pane_resize(mbi->child, 0, p->h,
80 ci->home->w, ci->home->h - p->h);
82 pane_damaged(ci->home, DAMAGED_VIEW);
86 DEF_CMD(menubar_child_notify)
88 struct mbinfo *mbi = ci->home->data;
94 if (ci->home->focus == ci->focus)
95 ci->home->focus = NULL;
99 pane_close(mbi->child);
100 mbi->child = ci->focus;
101 ci->home->focus = ci->focus;
106 DEF_CMD(menubar_refresh)
109 struct pane *home = ci->home;
110 struct mbinfo *mbi = home->data;
112 struct pane *bar = mbi->bar;
115 if (mbi->hidden || !bar)
120 buf_concat(&b, ACK SOH "tab:20" STX);
122 list_for_each_entry(p, &home->children, siblings) {
127 /* Strange - every doc should have a focus... */
131 if (!pane_has_focus(p->focus, mbi->child))
132 /* Owner of this menu not in focus */
134 n = pane_attr_get(p, "doc-name");
137 for (c = n ; *c; c++)
138 if (*c == ',' || (*c > 0 && *c < ' '))
140 if (mbi->menu && mbi->open == p)
141 buf_concat(&b, SOH "fg:black,bg:white-80,"
144 buf_concat(&b, SOH "fg:blue,underline,"
149 buf_concat(&b, ETX " ");
155 call("render-line:set", bar, -1, NULL, buf_final(&b),
156 0, NULL, "bg:#ffa500+50");
157 pane_resize(bar, 0, 0, bar->w, home->h/3);
158 call("render-line:measure", bar, -1);
160 pane_damaged(home, DAMAGED_SIZE);
170 static struct pane *menubar_find(struct pane *home safe,
172 const char *name safe,
173 enum create_where create)
175 struct pane *p, *m, *d;
177 struct pane *last_left = NULL;
179 list_for_each_entry(p, &home->children, siblings) {
183 /* Strange - every doc should have a focus... */
185 /* If no owner, then we only want currently visible docs */
186 if (!owner && p->z != -1)
188 a = pane_attr_get(p, "doc-name");
189 if (owner && p->focus != owner)
191 if (!a || strcmp(name, a) != 0)
195 if (create == C_NOWHERE || !owner)
197 m = call_ret(pane, "attach-menu", home, 0, NULL, "DV", 0, NULL,
201 d = call_ret(pane, "doc:get-doc", m);
203 call("doc:set:autoclose", d, 0);
204 call("popup:close", m);
207 call("doc:set-name", d, 0, NULL, name);
208 call("doc:set:menubar-side", d, 0, NULL,
209 create == C_LEFT ? "left" : "right");
210 /* Find insertion point */
211 list_for_each_entry(p, &home->children, siblings) {
215 /* Strange - every doc should have a focus... */
217 a = pane_attr_get(p, "menubar-side");
218 if (a && strcmp(a, "left") == 0)
222 pane_reparent(d, home);
224 pane_add_notify(home, owner, "Notify:Close");
226 list_move(&d->siblings, &last_left->siblings);
227 else if (create == C_RIGHT)
228 list_move_tail(&d->siblings, &home->children);
229 pane_damaged(home, DAMAGED_VIEW);
239 if (!ci->str || !ci->str2)
242 val = strchr(ci->str, '/');
245 menu = strndup(ci->str, val - ci->str);
247 d = menubar_find(ci->home, ci->focus, menu,
248 ci->num & 2 ? C_RIGHT : C_LEFT);
253 call("menu:add", d, 0, NULL, val, 0, NULL, ci->str2);
257 DEF_CMD(menubar_delete)
263 d = menubar_find(ci->home, ci->focus, ci->str, C_NOWHERE);
270 DEF_CMD(menubar_clear)
276 d = menubar_find(ci->home, ci->focus, ci->str, C_NOWHERE);
279 call("menu:clear", d);
283 DEF_CMD(menubar_done)
285 struct pane *home = ci->home;
286 struct mbinfo *mbi = home->data;
289 pane_take_focus(mbi->child);
290 call("Keystroke-sequence", home, 0, NULL, ci->str);
294 DEF_CMD(menubar_root)
296 /* Provide a pane for popup to attach to */
297 comm_call(ci->comm2, "cb", ci->home);
301 DEF_CMD(menubar_view_changed)
303 //struct mbinfo *mbi = ci->home->data;
305 //ci->home->focus = mbi->child;
309 DEF_CMD(menubar_press)
311 struct mbinfo *mbi = ci->home->data;
312 struct call_return cr;
317 if (ci->focus != mbi->bar || !mbi->bar)
320 call("popup:close", mbi->menu);
322 pane_damaged(ci->home, DAMAGED_VIEW);
324 cih = pane_mapxy(mbi->bar, ci->home,
325 ci->x == INT_MAX ? ci->focus->cx : ci->x,
326 ci->y == INT_MAX ? ci->focus->cy : ci->y,
328 cr = pane_call_ret(all, mbi->bar, "render-line:findxy",
329 mbi->bar, -1, NULL, NULL,
334 if (cr.s && sscanf(cr.s, "%dx%d,", &x, &y) == 2) {
338 list_for_each_entry(p, &ci->home->children, siblings) {
343 if (p->x < cr.ret - 1)
346 mbi->menu = call_ret(pane, "attach-menu", p, 0, NULL, "DVF",
350 pane_add_notify(ci->home, mbi->menu, "Notify:Close");
353 pane_damaged(ci->home, DAMAGED_VIEW);
359 DEF_CMD(menubar_release)
361 struct mbinfo *mbi = ci->home->data;
362 struct pane *c = pane_my_child(ci->home, ci->focus);
367 /* any button maps to -3 for menu action */
368 return home_call(ci->home->parent, "M:Release-3", ci->focus,
369 ci->num, ci->mark, ci->str,
370 ci->num2, ci->mark2, ci->str2,
371 ci->x, ci->y, ci->comm2);
374 DEF_CMD(menubar_close_notify)
376 struct mbinfo *mbi = ci->home->data;
379 if (ci->focus == mbi->menu) {
381 pane_damaged(ci->home, DAMAGED_VIEW);
384 if (ci->focus == mbi->child) {
388 if (ci->focus == mbi->bar) {
392 list_for_each_entry(p, &ci->home->children, siblings) {
395 if (p->focus == ci->focus) {
404 DEF_CMD(menubar_attach)
406 struct pane *ret, *mbp;
408 char *v = pane_attr_get(ci->focus, "menubar-visible");
410 ret = pane_register(ci->focus, 0, &menubar_handle.c);
415 if (v && strcmp(v, "no") == 0)
417 mbi->hidden = ! mbi->wanted;
418 mbp = call_ret(pane, "attach-renderline", ret, 1);
424 pane_add_notify(ret, mbp, "Notify:Close");
425 pane_damaged(ret, DAMAGED_VIEW);
426 return comm_call(ci->comm2, "callback:attach", ret);
429 void edlib_init(struct pane *ed safe)
431 call_comm("global-set-command", ed, &menubar_attach,
432 0, NULL, "attach-menubar");
434 menubar_map = key_alloc();
435 key_add(menubar_map, "window:border", &menubar_border);
436 key_add(menubar_map, "Refresh:size", &menubar_refresh_size);
437 key_add(menubar_map, "Child-Notify", &menubar_child_notify);
438 key_add(menubar_map, "Refresh:view", &menubar_refresh);
439 key_add(menubar_map, "menubar-add", &menubar_add);
440 key_add(menubar_map, "menubar-delete", &menubar_delete);
441 key_add(menubar_map, "menubar-clear", &menubar_clear);
442 key_add(menubar_map, "menubar-done", &menubar_done);
443 key_add(menubar_map, "RootPane", &menubar_root);
444 key_add(menubar_map, "Notify:Close", &menubar_close_notify);
445 key_add(menubar_map, "view:changed", &menubar_view_changed);
446 key_add_prefix(menubar_map, "M:Press-", &menubar_press);
447 key_add_prefix(menubar_map, "M:Release-", &menubar_release);