4 * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
27 struct cmdq_item
*item
;
30 struct cmd_find_state fs
;
44 menu_add_items(struct menu
*menu
, const struct menu_item
*items
,
45 struct cmdq_item
*qitem
, struct client
*c
, struct cmd_find_state
*fs
)
47 const struct menu_item
*loop
;
49 for (loop
= items
; loop
->name
!= NULL
; loop
++)
50 menu_add_item(menu
, loop
, qitem
, c
, fs
);
54 menu_add_item(struct menu
*menu
, const struct menu_item
*item
,
55 struct cmdq_item
*qitem
, struct client
*c
, struct cmd_find_state
*fs
)
57 struct menu_item
*new_item
;
58 const char *key
= NULL
, *cmd
, *suffix
= "";
59 char *s
, *trimmed
, *name
;
60 u_int width
, max_width
;
64 line
= (item
== NULL
|| item
->name
== NULL
|| *item
->name
== '\0');
65 if (line
&& menu
->count
== 0)
67 if (line
&& menu
->items
[menu
->count
- 1].name
== NULL
)
70 menu
->items
= xreallocarray(menu
->items
, menu
->count
+ 1,
72 new_item
= &menu
->items
[menu
->count
++];
73 memset(new_item
, 0, sizeof *new_item
);
79 s
= format_single_from_state(qitem
, item
->name
, c
, fs
);
81 s
= format_single(qitem
, item
->name
, c
, NULL
, NULL
, NULL
);
82 if (*s
== '\0') { /* no item if empty after format expanded */
86 max_width
= c
->tty
.sx
- 4;
89 if (*s
!= '-' && item
->key
!= KEYC_UNKNOWN
&& item
->key
!= KEYC_NONE
) {
90 key
= key_string_lookup_key(item
->key
, 0);
91 keylen
= strlen(key
) + 3; /* 3 = space and two brackets */
94 * Add the key if it is shorter than a quarter of the available
95 * space or there is space for the entire item text and the
98 if (keylen
<= max_width
/ 4)
100 else if (keylen
>= max_width
|| slen
>= max_width
- keylen
)
104 if (slen
> max_width
) {
108 trimmed
= format_trim_right(s
, max_width
);
110 xasprintf(&name
, "%s%s#[default] #[align=right](%s)",
111 trimmed
, suffix
, key
);
113 xasprintf(&name
, "%s%s", trimmed
, suffix
);
116 new_item
->name
= name
;
122 s
= format_single_from_state(qitem
, cmd
, c
, fs
);
124 s
= format_single(qitem
, cmd
, c
, NULL
, NULL
, NULL
);
127 new_item
->command
= s
;
128 new_item
->key
= item
->key
;
130 width
= format_width(new_item
->name
);
131 if (*new_item
->name
== '-')
133 if (width
> menu
->width
)
138 menu_create(const char *title
)
142 menu
= xcalloc(1, sizeof *menu
);
143 menu
->title
= xstrdup(title
);
144 menu
->width
= format_width(title
);
150 menu_free(struct menu
*menu
)
154 for (i
= 0; i
< menu
->count
; i
++) {
155 free((void *)menu
->items
[i
].name
);
156 free((void *)menu
->items
[i
].command
);
160 free((void *)menu
->title
);
165 menu_mode_cb(__unused
struct client
*c
, void *data
, u_int
*cx
, u_int
*cy
)
167 struct menu_data
*md
= data
;
170 if (md
->choice
== -1)
173 *cy
= md
->py
+ 1 + md
->choice
;
178 /* Return parts of the input range which are not obstructed by the menu. */
180 menu_check_cb(__unused
struct client
*c
, void *data
, u_int px
, u_int py
,
181 u_int nx
, struct overlay_ranges
*r
)
183 struct menu_data
*md
= data
;
184 struct menu
*menu
= md
->menu
;
186 server_client_overlay_range(md
->px
, md
->py
, menu
->width
+ 4,
187 menu
->count
+ 2, px
, py
, nx
, r
);
191 menu_draw_cb(struct client
*c
, void *data
,
192 __unused
struct screen_redraw_ctx
*rctx
)
194 struct menu_data
*md
= data
;
195 struct tty
*tty
= &c
->tty
;
196 struct screen
*s
= &md
->s
;
197 struct menu
*menu
= md
->menu
;
198 struct screen_write_ctx ctx
;
199 u_int i
, px
= md
->px
, py
= md
->py
;
202 style_apply(&gc
, c
->session
->curw
->window
->options
, "mode-style", NULL
);
204 screen_write_start(&ctx
, s
);
205 screen_write_clearscreen(&ctx
, 8);
206 screen_write_menu(&ctx
, menu
, md
->choice
, BOX_LINES_DEFAULT
,
207 &grid_default_cell
, &grid_default_cell
, &gc
);
208 screen_write_stop(&ctx
);
210 for (i
= 0; i
< screen_size_y(&md
->s
); i
++) {
211 tty_draw_line(tty
, s
, 0, i
, menu
->width
+ 4, px
, py
+ i
,
212 &grid_default_cell
, NULL
);
217 menu_free_cb(__unused
struct client
*c
, void *data
)
219 struct menu_data
*md
= data
;
221 if (md
->item
!= NULL
)
222 cmdq_continue(md
->item
);
225 md
->cb(md
->menu
, UINT_MAX
, KEYC_NONE
, md
->data
);
233 menu_key_cb(struct client
*c
, void *data
, struct key_event
*event
)
235 struct menu_data
*md
= data
;
236 struct menu
*menu
= md
->menu
;
237 struct mouse_event
*m
= &event
->m
;
239 int count
= menu
->count
, old
= md
->choice
;
240 const char *name
= NULL
;
241 const struct menu_item
*item
;
242 struct cmdq_state
*state
;
243 enum cmd_parse_status status
;
246 if (KEYC_IS_MOUSE(event
->key
)) {
247 if (md
->flags
& MENU_NOMOUSE
) {
248 if (MOUSE_BUTTONS(m
->b
) != MOUSE_BUTTON_1
)
253 m
->x
> md
->px
+ 4 + menu
->width
||
255 m
->y
> md
->py
+ 1 + count
- 1) {
256 if (~md
->flags
& MENU_STAYOPEN
) {
257 if (MOUSE_RELEASE(m
->b
))
260 if (!MOUSE_RELEASE(m
->b
) &&
261 !MOUSE_WHEEL(m
->b
) &&
265 if (md
->choice
!= -1) {
267 c
->flags
|= CLIENT_REDRAWOVERLAY
;
271 if (~md
->flags
& MENU_STAYOPEN
) {
272 if (MOUSE_RELEASE(m
->b
))
275 if (!MOUSE_WHEEL(m
->b
) && !MOUSE_DRAG(m
->b
))
278 md
->choice
= m
->y
- (md
->py
+ 1);
279 if (md
->choice
!= old
)
280 c
->flags
|= CLIENT_REDRAWOVERLAY
;
283 for (i
= 0; i
< (u_int
)count
; i
++) {
284 name
= menu
->items
[i
].name
;
285 if (name
== NULL
|| *name
== '-')
287 if (event
->key
== menu
->items
[i
].key
) {
292 switch (event
->key
& ~KEYC_MASK_FLAGS
) {
298 if (md
->choice
== -1 || md
->choice
== 0)
299 md
->choice
= count
- 1;
302 name
= menu
->items
[md
->choice
].name
;
303 } while ((name
== NULL
|| *name
== '-') && md
->choice
!= old
);
304 c
->flags
|= CLIENT_REDRAWOVERLAY
;
307 if (~md
->flags
& MENU_TAB
)
310 case '\011': /* Tab */
311 if (~md
->flags
& MENU_TAB
)
313 if (md
->choice
== count
- 1)
321 if (md
->choice
== -1 || md
->choice
== count
- 1)
325 name
= menu
->items
[md
->choice
].name
;
326 } while ((name
== NULL
|| *name
== '-') && md
->choice
!= old
);
327 c
->flags
|= CLIENT_REDRAWOVERLAY
;
330 case '\002': /* C-b */
337 name
= menu
->items
[md
->choice
].name
;
338 if (md
->choice
!= 0 &&
339 (name
!= NULL
&& *name
!= '-'))
341 else if (md
->choice
== 0)
345 c
->flags
|= CLIENT_REDRAWOVERLAY
;
348 if (md
->choice
> count
- 6) {
349 md
->choice
= count
- 1;
350 name
= menu
->items
[md
->choice
].name
;
355 name
= menu
->items
[md
->choice
].name
;
356 if (md
->choice
!= count
- 1 &&
357 (name
!= NULL
&& *name
!= '-'))
359 else if (md
->choice
== count
- 1)
363 while (name
== NULL
|| *name
== '-') {
365 name
= menu
->items
[md
->choice
].name
;
367 c
->flags
|= CLIENT_REDRAWOVERLAY
;
372 name
= menu
->items
[md
->choice
].name
;
373 while (name
== NULL
|| *name
== '-') {
375 name
= menu
->items
[md
->choice
].name
;
377 c
->flags
|= CLIENT_REDRAWOVERLAY
;
381 md
->choice
= count
- 1;
382 name
= menu
->items
[md
->choice
].name
;
383 while (name
== NULL
|| *name
== '-') {
385 name
= menu
->items
[md
->choice
].name
;
387 c
->flags
|= CLIENT_REDRAWOVERLAY
;
389 case '\006': /* C-f */
393 case '\033': /* Escape */
394 case '\003': /* C-c */
395 case '\007': /* C-g */
402 if (md
->choice
== -1)
404 item
= &menu
->items
[md
->choice
];
405 if (item
->name
== NULL
|| *item
->name
== '-') {
406 if (md
->flags
& MENU_STAYOPEN
)
410 if (md
->cb
!= NULL
) {
411 md
->cb(md
->menu
, md
->choice
, item
->key
, md
->data
);
416 if (md
->item
!= NULL
)
417 event
= cmdq_get_event(md
->item
);
420 state
= cmdq_new_state(&md
->fs
, event
, 0);
422 status
= cmd_parse_and_append(item
->command
, NULL
, c
, state
, &error
);
423 if (status
== CMD_PARSE_ERROR
) {
424 cmdq_append(c
, cmdq_get_error(error
));
427 cmdq_free_state(state
);
433 menu_prepare(struct menu
*menu
, int flags
, int starting_choice
,
434 struct cmdq_item
*item
, u_int px
, u_int py
, struct client
*c
,
435 struct cmd_find_state
*fs
, menu_choice_cb cb
, void *data
)
437 struct menu_data
*md
;
441 if (c
->tty
.sx
< menu
->width
+ 4 || c
->tty
.sy
< menu
->count
+ 2)
443 if (px
+ menu
->width
+ 4 > c
->tty
.sx
)
444 px
= c
->tty
.sx
- menu
->width
- 4;
445 if (py
+ menu
->count
+ 2 > c
->tty
.sy
)
446 py
= c
->tty
.sy
- menu
->count
- 2;
448 md
= xcalloc(1, sizeof *md
);
453 cmd_find_copy_state(&md
->fs
, fs
);
454 screen_init(&md
->s
, menu
->width
+ 4, menu
->count
+ 2, 0);
455 if (~md
->flags
& MENU_NOMOUSE
)
456 md
->s
.mode
|= (MODE_MOUSE_ALL
|MODE_MOUSE_BUTTON
);
457 md
->s
.mode
&= ~MODE_CURSOR
;
465 if (md
->flags
& MENU_NOMOUSE
) {
466 if (starting_choice
>= (int)menu
->count
) {
467 starting_choice
= menu
->count
- 1;
468 choice
= starting_choice
+ 1;
470 name
= menu
->items
[choice
- 1].name
;
471 if (name
!= NULL
&& *name
!= '-') {
472 md
->choice
= choice
- 1;
476 choice
= menu
->count
;
477 if (choice
== starting_choice
+ 1)
480 } else if (starting_choice
>= 0) {
481 choice
= starting_choice
;
483 name
= menu
->items
[choice
].name
;
484 if (name
!= NULL
&& *name
!= '-') {
488 if (++choice
== (int)menu
->count
)
490 if (choice
== starting_choice
)
502 menu_display(struct menu
*menu
, int flags
, int starting_choice
,
503 struct cmdq_item
*item
, u_int px
, u_int py
, struct client
*c
,
504 struct cmd_find_state
*fs
, menu_choice_cb cb
, void *data
)
506 struct menu_data
*md
;
508 md
= menu_prepare(menu
, flags
, starting_choice
, item
, px
, py
, c
, fs
, cb
,
512 server_client_set_overlay(c
, 0, NULL
, menu_mode_cb
, menu_draw_cb
,
513 menu_key_cb
, menu_free_cb
, NULL
, md
);