]> git.neil.brown.name Git - edlib.git/blob - lib-menubar.c
9c4bbc741689bc598813a380b44529763fb2bc5c
[edlib.git] / lib-menubar.c
1 /*
2  * Copyright Neil Brown ©2023 <neil@brown.name>
3  * May be distributed under terms of GPLv2 - see file:COPYING
4  *
5  * trim a line off the top line of a pane and place a menu
6  * bar.   Actions are sent to the focus.
7  *
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.
12  *
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.
18  *
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.
25  *
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.
30  *
31  * Child panes have ->z value:
32  * 0 for child and bar
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
36  */
37
38 #include <stdio.h>
39 #define PANE_DATA_TYPE struct mbinfo
40 #include "core.h"
41
42 struct mbinfo {
43         struct pane *bar safe;
44         struct pane *child;
45         struct pane *menu, *open;
46         bool hidden, wanted;
47 };
48 #include "core-pane.h"
49
50 static struct map *menubar_map;
51 DEF_LOOKUP_CMD(menubar_handle, menubar_map);
52
53 DEF_CMD(menubar_border)
54 {
55         struct mbinfo *mbi = ci->home->data;
56
57         mbi->hidden = !mbi->wanted || ci->num <= 0;
58         pane_damaged(ci->home, DAMAGED_SIZE);
59         return Efallthrough;
60 }
61
62 DEF_CMD(menubar_refresh_size)
63 {
64         struct mbinfo *mbi = ci->home->data;
65         struct pane *p = mbi->bar;
66
67         if (mbi->hidden) {
68                 /* Put bar below window - out of sight */
69                 pane_resize(p, 0, ci->home->h,
70                             p->w, p->h);
71                 if (mbi->child)
72                         pane_resize(mbi->child, 0, 0,
73                                     ci->home->w, ci->home->h);
74         } else {
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);
80         }
81         pane_damaged(ci->home, DAMAGED_VIEW);
82         return 1;
83 }
84
85 DEF_CMD(menubar_child_notify)
86 {
87         struct mbinfo *mbi = ci->home->data;
88
89         if (ci->focus->z)
90                 /* Ignore */
91                 return 1;
92         if (ci->num < 0) {
93                 if (ci->home->focus == ci->focus)
94                         ci->home->focus = NULL;
95                 mbi->child = NULL;
96         } else {
97                 if (mbi->child)
98                         pane_close(mbi->child);
99                 mbi->child = ci->focus;
100                 ci->home->focus = ci->focus;
101         }
102         return 1;
103 }
104
105 DEF_CMD(menubar_refresh)
106 {
107         struct buf b;
108         struct pane *home = ci->home;
109         struct mbinfo *mbi = home->data;
110         struct pane *p;
111         struct pane *bar = mbi->bar;
112         int h;
113
114         if (mbi->hidden)
115                 return 1;
116         if (!mbi->child)
117                 return 1;
118         buf_init(&b);
119         buf_concat(&b, ACK SOH "tab:20" STX);
120
121         list_for_each_entry(p, &home->children, siblings) {
122                 char *n, *c;
123                 if (p->z >= 0)
124                         continue;
125                 if (!p->focus)
126                         /* Strange - every doc should have a focus... */
127                         continue;
128                 p->x = -1;
129                 p->z = -2;
130                 if (!pane_has_focus(p->focus, mbi->child))
131                         /* Owner of this menu not in focus */
132                         continue;
133                 n = pane_attr_get(p, "doc-name");
134                 if (!n || !*n)
135                         continue;
136                 for (c = n ; *c; c++)
137                         if (*c == ',' || (*c > 0 && *c < ' '))
138                                 *c = '_';
139                 if (mbi->menu && mbi->open == p)
140                         buf_concat(&b, SOH "fg:black,bg:white-80,"
141                                    "menu-name:");
142                 else
143                         buf_concat(&b, SOH "fg:blue,underline,"
144                                    "menu-name:");
145                 buf_concat(&b, n);
146                 buf_concat(&b, STX);
147                 buf_concat(&b, n);
148                 buf_concat(&b, ETX " ");
149                 p->x = b.len;
150                 p->z = -1;
151         }
152         buf_concat(&b, ETX);
153         h = bar->h;
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);
158         if (bar->h != h)
159                 pane_damaged(home, DAMAGED_SIZE);
160         free(buf_final(&b));
161         return 1;
162 }
163
164 enum create_where {
165         C_NOWHERE,
166         C_LEFT,
167         C_RIGHT,
168 };
169 static struct pane *menubar_find(struct pane *home safe,
170                                  struct pane *owner,
171                                  const char *name safe,
172                                  enum create_where create)
173 {
174         struct pane *p, *m, *d;
175         char *a;
176         struct pane *last_left = NULL;
177
178         list_for_each_entry(p, &home->children, siblings) {
179                 if (p->z >= 0)
180                         continue;
181                 if (!p->focus)
182                         /* Strange - every doc should have a focus... */
183                         continue;
184                 /* If no owner, then we only want currently visible docs */
185                 if (!owner && p->z != -1)
186                         continue;
187                 a = pane_attr_get(p, "doc-name");
188                 if (owner && p->focus != owner)
189                         continue;
190                 if (!a || strcmp(name, a) != 0)
191                         continue;
192                 return p;
193         }
194         if (create == C_NOWHERE || !owner)
195                 return NULL;
196         m = call_ret(pane, "attach-menu", home, 0, NULL, "DV", 0, NULL,
197                      "menubar-done");
198         if (!m)
199                 return NULL;
200         d = call_ret(pane, "doc:get-doc", m);
201         if (d)
202                 call("doc:set:autoclose", d, 0);
203         call("popup:close", m);
204         if (!d)
205                 return NULL;
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) {
211                 if (p->z >= 0)
212                         continue;
213                 if (!p->focus)
214                         /* Strange - every doc should have a focus... */
215                         continue;
216                 a = pane_attr_get(p, "menubar-side");
217                 if (a && strcmp(a, "left") == 0)
218                         last_left = p;
219         }
220         d->z = -1;
221         pane_reparent(d, home);
222         d->focus = owner;
223         pane_add_notify(home, owner, "Notify:Close");
224         if (last_left)
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);
229         return d;
230 }
231
232 DEF_CMD(menubar_add)
233 {
234         const char *val;
235         char *menu;
236         struct pane *d;
237
238         if (!ci->str || !ci->str2)
239                 return Enoarg;
240
241         val = strchr(ci->str, '/');
242         if (!val)
243                 return Enoarg;
244         menu = strndup(ci->str, val - ci->str);
245         val += 1;
246         d = menubar_find(ci->home, ci->focus, menu,
247                          ci->num & 2 ? C_RIGHT : C_LEFT);
248         if (!d) {
249                 free(menu);
250                 return Efail;
251         }
252         call("menu:add", d, 0, NULL, val, 0, NULL, ci->str2);
253         return 1;
254 }
255
256 DEF_CMD(menubar_delete)
257 {
258         struct pane *d;
259
260         if (!ci->str)
261                 return Enoarg;
262         d = menubar_find(ci->home, ci->focus, ci->str, C_NOWHERE);
263         if (!d)
264                 return Efail;
265         pane_close(d);
266         return 1;
267 }
268
269 DEF_CMD(menubar_clear)
270 {
271         struct pane *d;
272
273         if (!ci->str)
274                 return Enoarg;
275         d = menubar_find(ci->home, ci->focus, ci->str, C_NOWHERE);
276         if (!d)
277                 return Efail;
278         call("menu:clear", d);
279         return 1;
280 }
281
282 DEF_CMD(menubar_done)
283 {
284         struct pane *home = ci->home;
285         struct mbinfo *mbi = home->data;
286
287         if (mbi->child)
288                 pane_take_focus(mbi->child);
289         call("Keystroke-sequence", home, 0, NULL, ci->str);
290         return 1;
291 }
292
293 DEF_CMD(menubar_root)
294 {
295         /* Provide a pane for popup to attach to */
296         comm_call(ci->comm2, "cb", ci->home);
297         return 1;
298 }
299
300 DEF_CMD(menubar_view_changed)
301 {
302         //struct mbinfo *mbi = ci->home->data;
303
304         //ci->home->focus = mbi->child;
305         return 1;
306 }
307
308 DEF_CMD(menubar_press)
309 {
310         struct mbinfo *mbi = ci->home->data;
311         struct call_return cr;
312         struct xy cih;
313         struct pane *p;
314         int x, y;
315
316         if (ci->focus != mbi->bar)
317                 return Efallthrough;
318         if (mbi->menu) {
319                 call("popup:close", mbi->menu);
320                 mbi->menu = NULL;
321                 pane_damaged(ci->home, DAMAGED_VIEW);
322         }
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,
326                          False);
327         cr = pane_call_ret(all, mbi->bar, "render-line:findxy",
328                            mbi->bar, -1, NULL, NULL,
329                            0, NULL, NULL,
330                            cih.x, cih.y);
331         if (cr.ret <= 0)
332                 return 1;
333         if (cr.s && sscanf(cr.s, "%dx%d,", &x, &y) == 2) {
334                 cih.x = x;
335                 cih.y = y;
336         }
337         list_for_each_entry(p, &ci->home->children, siblings) {
338                 if (p->z != -1)
339                         continue;
340                 if (!p->focus)
341                         continue;
342                 if (p->x < cr.ret - 1)
343                         continue;
344                 /* clicked on 'p' */
345                 mbi->menu = call_ret(pane, "attach-menu", p, 0, NULL, "DVF",
346                                      0, NULL, NULL,
347                                      cih.x, mbi->bar->h);
348                 if (mbi->menu) {
349                         pane_add_notify(ci->home, mbi->menu, "Notify:Close");
350                         mbi->open = p;
351                 }
352                 pane_damaged(ci->home, DAMAGED_VIEW);
353                 return 1;
354         }
355         return 1;
356 }
357
358 DEF_CMD(menubar_release)
359 {
360         struct mbinfo *mbi = ci->home->data;
361         struct pane *c = pane_my_child(ci->home, ci->focus);
362
363         if (c == mbi->child)
364                 return Efallthrough;
365
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);
371 }
372
373 DEF_CMD(menubar_close_notify)
374 {
375         struct mbinfo *mbi = ci->home->data;
376         struct pane *p;
377
378         if (ci->focus == mbi->menu) {
379                 mbi->menu = NULL;
380                 pane_damaged(ci->home, DAMAGED_VIEW);
381                 return 1;
382         }
383         if (ci->focus == mbi->child) {
384                 mbi->child = NULL;
385                 return 1;
386         }
387         if (ci->focus == mbi->bar) {
388                 // FIXME
389                 return 1;
390         }
391         list_for_each_entry(p, &ci->home->children, siblings) {
392                 if (p->z >= 0)
393                         continue;
394                 if (p->focus == ci->focus) {
395                         p->focus = NULL;
396                         pane_close(p);
397                         return 1;
398                 }
399         }
400         return 1;
401 }
402
403 DEF_CMD(menubar_attach)
404 {
405         struct pane *ret, *mbp;
406         struct mbinfo *mbi;
407         char *v = pane_attr_get(ci->focus, "menubar-visible");
408
409         ret = pane_register(ci->focus, 0, &menubar_handle.c);
410         if (!ret)
411                 return Efail;
412         mbi = ret->data;
413         mbi->wanted = True;
414         if (v && strcmp(v, "no") == 0)
415                 mbi->wanted = False;
416         mbi->hidden = ! mbi->wanted;
417         mbp = call_ret(pane, "attach-renderline", ret, 1);
418         if (!mbp) {
419                 pane_close(ret);
420                 return Efail;
421         }
422         mbi->bar = mbp;
423         pane_damaged(ret, DAMAGED_VIEW);
424         return comm_call(ci->comm2, "callback:attach", ret);
425 }
426
427 void edlib_init(struct pane *ed safe)
428 {
429         call_comm("global-set-command", ed, &menubar_attach,
430                   0, NULL, "attach-menubar");
431
432         menubar_map = key_alloc();
433         key_add(menubar_map, "window: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);
446 }