2008-05-20 18:50:51 +04:00
|
|
|
/*
|
2011-05-11 17:53:51 +04:00
|
|
|
* calmwm - the calm window manager
|
|
|
|
*
|
2008-05-20 18:50:51 +04:00
|
|
|
* Copyright (c) 2008 Owain G. Ainsworth <oga@openbsd.org>
|
|
|
|
* Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2011-05-11 17:53:51 +04:00
|
|
|
*
|
|
|
|
* $OpenBSD$
|
2008-05-20 18:50:51 +04:00
|
|
|
*/
|
|
|
|
|
2009-12-15 07:10:42 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
2012-11-09 07:52:02 +04:00
|
|
|
#include <ctype.h>
|
2009-12-15 07:10:42 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2012-11-09 07:52:02 +04:00
|
|
|
#include <stdio.h>
|
2009-12-15 07:10:42 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-05-20 18:50:51 +04:00
|
|
|
#include "calmwm.h"
|
|
|
|
|
2011-06-27 16:46:54 +04:00
|
|
|
#define PROMPT_SCHAR "\xc2\xbb"
|
|
|
|
#define PROMPT_ECHAR "\xc2\xab"
|
2008-05-20 18:50:51 +04:00
|
|
|
|
2011-03-22 13:47:59 +03:00
|
|
|
enum ctltype {
|
|
|
|
CTL_NONE = -1,
|
|
|
|
CTL_ERASEONE = 0, CTL_WIPE, CTL_UP, CTL_DOWN, CTL_RETURN,
|
2012-11-07 18:39:44 +04:00
|
|
|
CTL_TAB, CTL_ABORT, CTL_ALL
|
2011-03-22 13:47:59 +03:00
|
|
|
};
|
|
|
|
|
2008-05-20 18:50:51 +04:00
|
|
|
struct menu_ctx {
|
2012-11-07 18:39:44 +04:00
|
|
|
struct screen_ctx *sc;
|
2008-05-20 18:50:51 +04:00
|
|
|
char searchstr[MENU_MAXENTRY + 1];
|
|
|
|
char dispstr[MENU_MAXENTRY*2 + 1];
|
|
|
|
char promptstr[MENU_MAXENTRY + 1];
|
2008-05-21 18:11:19 +04:00
|
|
|
int hasprompt;
|
2008-05-20 18:50:51 +04:00
|
|
|
int list;
|
|
|
|
int listing;
|
|
|
|
int changed;
|
|
|
|
int noresult;
|
2008-05-21 18:11:19 +04:00
|
|
|
int prev;
|
|
|
|
int entry;
|
2012-10-23 20:08:59 +04:00
|
|
|
int height;
|
2008-05-21 18:11:19 +04:00
|
|
|
int width;
|
|
|
|
int num;
|
2012-11-07 18:39:44 +04:00
|
|
|
int flags;
|
2008-05-20 18:50:51 +04:00
|
|
|
int x;
|
2008-05-21 18:11:19 +04:00
|
|
|
int y;
|
2013-04-05 21:07:25 +04:00
|
|
|
void (*match)(struct menu_q *, struct menu_q *, char *);
|
|
|
|
void (*print)(struct menu *, int);
|
2008-05-20 18:50:51 +04:00
|
|
|
};
|
|
|
|
static struct menu *menu_handle_key(XEvent *, struct menu_ctx *,
|
|
|
|
struct menu_q *, struct menu_q *);
|
2008-05-21 18:11:19 +04:00
|
|
|
static void menu_handle_move(XEvent *, struct menu_ctx *,
|
2012-12-17 06:28:45 +04:00
|
|
|
struct screen_ctx *, struct menu_q *);
|
2009-06-26 16:21:58 +04:00
|
|
|
static struct menu *menu_handle_release(XEvent *, struct menu_ctx *,
|
2008-05-21 18:11:19 +04:00
|
|
|
struct screen_ctx *, struct menu_q *);
|
2008-05-20 18:50:51 +04:00
|
|
|
static void menu_draw(struct screen_ctx *, struct menu_ctx *,
|
|
|
|
struct menu_q *, struct menu_q *);
|
2012-12-17 06:28:45 +04:00
|
|
|
static void menu_draw_entry(struct screen_ctx *, struct menu_ctx *,
|
|
|
|
struct menu_q *, int, int);
|
2008-05-21 18:11:19 +04:00
|
|
|
static int menu_calc_entry(struct screen_ctx *, struct menu_ctx *,
|
|
|
|
int, int);
|
2013-04-08 04:56:21 +04:00
|
|
|
static struct menu *menu_complete_path(struct menu_ctx *);
|
2012-08-07 18:05:49 +04:00
|
|
|
static int menu_keycode(XKeyEvent *, enum ctltype *,
|
2013-04-05 21:07:25 +04:00
|
|
|
char *);
|
2008-05-21 18:11:19 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
menu_init(struct screen_ctx *sc)
|
|
|
|
{
|
2011-07-25 19:41:05 +04:00
|
|
|
sc->menuwin = XCreateSimpleWindow(X_Dpy, sc->rootwin, 0, 0, 1, 1,
|
|
|
|
Conf.bwidth,
|
2012-12-17 06:28:45 +04:00
|
|
|
sc->xftcolor[CWM_COLOR_MENU_FG].pixel,
|
|
|
|
sc->xftcolor[CWM_COLOR_MENU_BG].pixel);
|
2008-05-21 18:11:19 +04:00
|
|
|
}
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
struct menu *
|
2009-12-10 20:16:51 +03:00
|
|
|
menu_filter(struct screen_ctx *sc, struct menu_q *menuq, char *prompt,
|
2013-04-05 21:07:25 +04:00
|
|
|
char *initial, int flags,
|
2008-05-20 18:50:51 +04:00
|
|
|
void (*match)(struct menu_q *, struct menu_q *, char *),
|
|
|
|
void (*print)(struct menu *, int))
|
|
|
|
{
|
|
|
|
struct menu_ctx mc;
|
|
|
|
struct menu_q resultq;
|
|
|
|
struct menu *mi = NULL;
|
|
|
|
XEvent e;
|
|
|
|
Window focuswin;
|
2009-03-28 19:38:54 +03:00
|
|
|
int evmask, focusrevert;
|
2011-03-22 16:50:40 +03:00
|
|
|
int xsave, ysave, xcur, ycur;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
TAILQ_INIT(&resultq);
|
|
|
|
|
|
|
|
bzero(&mc, sizeof(mc));
|
|
|
|
|
|
|
|
xu_ptr_getpos(sc->rootwin, &mc.x, &mc.y);
|
|
|
|
|
2011-03-22 16:50:40 +03:00
|
|
|
xsave = mc.x;
|
|
|
|
ysave = mc.y;
|
|
|
|
|
2012-11-07 18:39:44 +04:00
|
|
|
mc.sc = sc;
|
|
|
|
mc.flags = flags;
|
2008-05-21 18:11:19 +04:00
|
|
|
if (prompt == NULL) {
|
2011-07-23 17:09:11 +04:00
|
|
|
evmask = MENUMASK;
|
2008-05-21 18:11:19 +04:00
|
|
|
mc.promptstr[0] = '\0';
|
|
|
|
mc.list = 1;
|
|
|
|
} else {
|
2011-07-23 17:09:11 +04:00
|
|
|
evmask = MENUMASK | KEYMASK; /* only accept keys if prompt */
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)snprintf(mc.promptstr, sizeof(mc.promptstr), "%s%s",
|
|
|
|
prompt, PROMPT_SCHAR);
|
|
|
|
(void)snprintf(mc.dispstr, sizeof(mc.dispstr), "%s%s%s",
|
|
|
|
mc.promptstr, mc.searchstr, PROMPT_ECHAR);
|
2009-12-08 19:52:17 +03:00
|
|
|
mc.width = font_width(sc, mc.dispstr, strlen(mc.dispstr));
|
2008-05-21 18:11:19 +04:00
|
|
|
mc.hasprompt = 1;
|
|
|
|
}
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
if (initial != NULL)
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)strlcpy(mc.searchstr, initial, sizeof(mc.searchstr));
|
2008-05-20 18:50:51 +04:00
|
|
|
else
|
|
|
|
mc.searchstr[0] = '\0';
|
|
|
|
|
|
|
|
mc.match = match;
|
|
|
|
mc.print = print;
|
2008-05-21 18:11:19 +04:00
|
|
|
mc.entry = mc.prev = -1;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
2008-05-21 18:11:19 +04:00
|
|
|
XMoveResizeWindow(X_Dpy, sc->menuwin, mc.x, mc.y, mc.width,
|
2009-12-08 19:52:17 +03:00
|
|
|
font_height(sc));
|
2009-03-28 19:38:54 +03:00
|
|
|
XSelectInput(X_Dpy, sc->menuwin, evmask);
|
2008-05-20 18:50:51 +04:00
|
|
|
XMapRaised(X_Dpy, sc->menuwin);
|
|
|
|
|
2011-07-23 17:09:11 +04:00
|
|
|
if (xu_ptr_grab(sc->menuwin, MENUGRABMASK, Cursor_question) < 0) {
|
2008-05-20 18:50:51 +04:00
|
|
|
XUnmapWindow(X_Dpy, sc->menuwin);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
XGetInputFocus(X_Dpy, &focuswin, &focusrevert);
|
|
|
|
XSetInputFocus(X_Dpy, sc->menuwin, RevertToPointerRoot, CurrentTime);
|
|
|
|
|
2008-05-23 22:48:57 +04:00
|
|
|
/* make sure keybindings don't remove keys from the menu stream */
|
|
|
|
XGrabKeyboard(X_Dpy, sc->menuwin, True,
|
|
|
|
GrabModeAsync, GrabModeAsync, CurrentTime);
|
|
|
|
|
2008-05-20 18:50:51 +04:00
|
|
|
for (;;) {
|
|
|
|
mc.changed = 0;
|
|
|
|
|
2009-03-28 19:38:54 +03:00
|
|
|
XWindowEvent(X_Dpy, sc->menuwin, evmask, &e);
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
switch (e.type) {
|
|
|
|
case KeyPress:
|
|
|
|
if ((mi = menu_handle_key(&e, &mc, menuq, &resultq))
|
|
|
|
!= NULL)
|
|
|
|
goto out;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case Expose:
|
|
|
|
menu_draw(sc, &mc, menuq, &resultq);
|
|
|
|
break;
|
2008-05-21 18:11:19 +04:00
|
|
|
case MotionNotify:
|
2012-12-17 06:28:45 +04:00
|
|
|
menu_handle_move(&e, &mc, sc, &resultq);
|
2008-05-21 18:11:19 +04:00
|
|
|
break;
|
|
|
|
case ButtonRelease:
|
|
|
|
if ((mi = menu_handle_release(&e, &mc, sc, &resultq))
|
|
|
|
!= NULL)
|
|
|
|
goto out;
|
|
|
|
break;
|
2011-03-22 14:03:05 +03:00
|
|
|
default:
|
|
|
|
break;
|
2008-05-20 18:50:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
2012-11-07 18:39:44 +04:00
|
|
|
if ((mc.flags & CWM_MENU_DUMMY) == 0 && mi->dummy) {
|
|
|
|
/* no mouse based match */
|
2012-11-08 00:34:39 +04:00
|
|
|
free(mi);
|
2008-05-20 18:50:51 +04:00
|
|
|
mi = NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-22 14:09:52 +03:00
|
|
|
XSetInputFocus(X_Dpy, focuswin, focusrevert, CurrentTime);
|
2011-03-22 16:50:40 +03:00
|
|
|
/* restore if user didn't move */
|
|
|
|
xu_ptr_getpos(sc->rootwin, &xcur, &ycur);
|
|
|
|
if (xcur == mc.x && ycur == mc.y)
|
|
|
|
xu_ptr_setpos(sc->rootwin, xsave, ysave);
|
2011-03-22 14:09:52 +03:00
|
|
|
xu_ptr_ungrab();
|
|
|
|
|
2008-05-20 18:50:51 +04:00
|
|
|
XUnmapWindow(X_Dpy, sc->menuwin);
|
2008-05-23 22:48:57 +04:00
|
|
|
XUngrabKeyboard(X_Dpy, CurrentTime);
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
return (mi);
|
|
|
|
}
|
|
|
|
|
2012-11-07 18:39:44 +04:00
|
|
|
static struct menu *
|
|
|
|
menu_complete_path(struct menu_ctx *mc)
|
|
|
|
{
|
|
|
|
struct menu *mi, *mr;
|
|
|
|
struct menu_q menuq;
|
|
|
|
char *path = NULL;
|
|
|
|
|
|
|
|
path = xcalloc(1, sizeof(mr->text));
|
|
|
|
mr = xcalloc(1, sizeof(*mr));
|
|
|
|
|
|
|
|
TAILQ_INIT(&menuq);
|
2013-04-08 17:02:31 +04:00
|
|
|
|
2012-11-07 18:39:44 +04:00
|
|
|
if ((mi = menu_filter(mc->sc, &menuq, mc->searchstr, NULL,
|
|
|
|
CWM_MENU_DUMMY, search_match_path_any, NULL)) != NULL) {
|
|
|
|
mr->abort = mi->abort;
|
|
|
|
mr->dummy = mi->dummy;
|
|
|
|
strlcpy(path, mi->text, sizeof(mi->text));
|
|
|
|
}
|
|
|
|
|
2012-12-17 18:32:39 +04:00
|
|
|
menuq_clear(&menuq);
|
2012-11-07 18:39:44 +04:00
|
|
|
|
2013-04-05 21:07:25 +04:00
|
|
|
if (path[0] != '\0')
|
2012-11-07 18:39:44 +04:00
|
|
|
snprintf(mr->text, sizeof(mr->text), "%s \"%s\"",
|
|
|
|
mc->searchstr, path);
|
|
|
|
else if (!mr->abort)
|
|
|
|
strlcpy(mr->text, mc->searchstr, sizeof(mr->text));
|
2012-11-08 00:34:39 +04:00
|
|
|
free(path);
|
2012-11-07 18:39:44 +04:00
|
|
|
return (mr);
|
|
|
|
}
|
|
|
|
|
2008-05-20 18:50:51 +04:00
|
|
|
static struct menu *
|
|
|
|
menu_handle_key(XEvent *e, struct menu_ctx *mc, struct menu_q *menuq,
|
|
|
|
struct menu_q *resultq)
|
|
|
|
{
|
|
|
|
struct menu *mi;
|
|
|
|
enum ctltype ctl;
|
2012-08-07 18:05:49 +04:00
|
|
|
char chr[32];
|
2008-05-20 18:50:51 +04:00
|
|
|
size_t len;
|
2012-08-07 18:05:49 +04:00
|
|
|
int clen, i;
|
|
|
|
wchar_t wc;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
2012-08-07 18:05:49 +04:00
|
|
|
if (menu_keycode(&e->xkey, &ctl, chr) < 0)
|
2008-05-20 18:50:51 +04:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
switch (ctl) {
|
|
|
|
case CTL_ERASEONE:
|
|
|
|
if ((len = strlen(mc->searchstr)) > 0) {
|
2012-08-07 18:05:49 +04:00
|
|
|
clen = 1;
|
|
|
|
while (mbtowc(&wc, &mc->searchstr[len-clen], MB_CUR_MAX) == -1)
|
|
|
|
clen++;
|
|
|
|
for (i = 1; i <= clen; i++)
|
|
|
|
mc->searchstr[len - i] = '\0';
|
2008-05-20 18:50:51 +04:00
|
|
|
mc->changed = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CTL_UP:
|
|
|
|
mi = TAILQ_LAST(resultq, menu_q);
|
|
|
|
if (mi == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
TAILQ_REMOVE(resultq, mi, resultentry);
|
|
|
|
TAILQ_INSERT_HEAD(resultq, mi, resultentry);
|
|
|
|
break;
|
|
|
|
case CTL_DOWN:
|
|
|
|
mi = TAILQ_FIRST(resultq);
|
|
|
|
if (mi == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
TAILQ_REMOVE(resultq, mi, resultentry);
|
|
|
|
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
|
|
|
|
break;
|
|
|
|
case CTL_RETURN:
|
|
|
|
/*
|
|
|
|
* Return whatever the cursor is currently on. Else
|
|
|
|
* even if dummy is zero, we need to return something.
|
|
|
|
*/
|
|
|
|
if ((mi = TAILQ_FIRST(resultq)) == NULL) {
|
|
|
|
mi = xmalloc(sizeof *mi);
|
|
|
|
(void)strlcpy(mi->text,
|
|
|
|
mc->searchstr, sizeof(mi->text));
|
|
|
|
mi->dummy = 1;
|
|
|
|
}
|
2010-02-10 04:23:05 +03:00
|
|
|
mi->abort = 0;
|
2008-05-20 18:50:51 +04:00
|
|
|
return (mi);
|
|
|
|
case CTL_WIPE:
|
|
|
|
mc->searchstr[0] = '\0';
|
|
|
|
mc->changed = 1;
|
|
|
|
break;
|
2012-11-07 18:39:44 +04:00
|
|
|
case CTL_TAB:
|
|
|
|
if ((mi = TAILQ_FIRST(resultq)) != NULL) {
|
2013-04-05 21:07:25 +04:00
|
|
|
/*
|
2012-11-07 18:39:44 +04:00
|
|
|
* - We are in exec_path menu mode
|
|
|
|
* - It is equal to the input
|
|
|
|
* We got a command, launch the file menu
|
|
|
|
*/
|
|
|
|
if ((mc->flags & CWM_MENU_FILE) &&
|
|
|
|
(strncmp(mc->searchstr, mi->text,
|
|
|
|
strlen(mi->text))) == 0)
|
|
|
|
return (menu_complete_path(mc));
|
|
|
|
|
2013-04-05 21:07:25 +04:00
|
|
|
/*
|
2012-11-07 18:39:44 +04:00
|
|
|
* Put common prefix of the results into searchstr
|
|
|
|
*/
|
|
|
|
(void)strlcpy(mc->searchstr,
|
|
|
|
mi->text, sizeof(mc->searchstr));
|
|
|
|
while ((mi = TAILQ_NEXT(mi, resultentry)) != NULL) {
|
|
|
|
i = 0;
|
|
|
|
while (tolower(mc->searchstr[i]) ==
|
|
|
|
tolower(mi->text[i]))
|
|
|
|
i++;
|
|
|
|
mc->searchstr[i] = '\0';
|
|
|
|
}
|
|
|
|
mc->changed = 1;
|
|
|
|
}
|
|
|
|
break;
|
2008-05-20 18:50:51 +04:00
|
|
|
case CTL_ALL:
|
|
|
|
mc->list = !mc->list;
|
|
|
|
break;
|
|
|
|
case CTL_ABORT:
|
|
|
|
mi = xmalloc(sizeof *mi);
|
|
|
|
mi->text[0] = '\0';
|
|
|
|
mi->dummy = 1;
|
2010-02-10 04:23:05 +03:00
|
|
|
mi->abort = 1;
|
2008-05-20 18:50:51 +04:00
|
|
|
return (mi);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:05:49 +04:00
|
|
|
if (chr[0] != '\0') {
|
2008-05-20 18:50:51 +04:00
|
|
|
mc->changed = 1;
|
2012-08-07 18:05:49 +04:00
|
|
|
(void)strlcat(mc->searchstr, chr, sizeof(mc->searchstr));
|
2008-05-20 18:50:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mc->noresult = 0;
|
2009-09-05 18:10:11 +04:00
|
|
|
if (mc->changed && mc->searchstr[0] != '\0') {
|
2008-05-20 18:50:51 +04:00
|
|
|
(*mc->match)(menuq, resultq, mc->searchstr);
|
|
|
|
/* If menuq is empty, never show we've failed */
|
|
|
|
mc->noresult = TAILQ_EMPTY(resultq) && !TAILQ_EMPTY(menuq);
|
|
|
|
} else if (mc->changed)
|
|
|
|
TAILQ_INIT(resultq);
|
|
|
|
|
2009-09-05 18:10:11 +04:00
|
|
|
if (!mc->list && mc->listing && !mc->changed) {
|
2008-05-20 18:50:51 +04:00
|
|
|
TAILQ_INIT(resultq);
|
|
|
|
mc->listing = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
|
|
|
|
struct menu_q *resultq)
|
|
|
|
{
|
2011-05-05 19:32:24 +04:00
|
|
|
struct menu *mi;
|
2013-01-02 20:26:34 +04:00
|
|
|
struct geom xine;
|
2012-10-23 20:08:59 +04:00
|
|
|
int n, xsave, ysave;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
if (mc->list) {
|
|
|
|
if (TAILQ_EMPTY(resultq) && mc->list) {
|
|
|
|
/* Copy them all over. */
|
|
|
|
TAILQ_FOREACH(mi, menuq, entry)
|
|
|
|
TAILQ_INSERT_TAIL(resultq, mi,
|
|
|
|
resultentry);
|
|
|
|
|
|
|
|
mc->listing = 1;
|
|
|
|
} else if (mc->changed)
|
|
|
|
mc->listing = 0;
|
|
|
|
}
|
|
|
|
|
2008-05-21 18:11:19 +04:00
|
|
|
mc->num = 0;
|
|
|
|
mc->width = 0;
|
2012-10-23 20:08:59 +04:00
|
|
|
mc->height = 0;
|
2008-05-21 18:11:19 +04:00
|
|
|
if (mc->hasprompt) {
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)snprintf(mc->dispstr, sizeof(mc->dispstr), "%s%s%s",
|
2008-05-21 18:11:19 +04:00
|
|
|
mc->promptstr, mc->searchstr, PROMPT_ECHAR);
|
2009-12-08 19:52:17 +03:00
|
|
|
mc->width = font_width(sc, mc->dispstr, strlen(mc->dispstr));
|
2012-10-23 20:08:59 +04:00
|
|
|
mc->height = font_height(sc);
|
2008-05-21 18:11:19 +04:00
|
|
|
mc->num = 1;
|
|
|
|
}
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
TAILQ_FOREACH(mi, resultq, resultentry) {
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
if (mc->print != NULL) {
|
|
|
|
(*mc->print)(mi, mc->listing);
|
|
|
|
text = mi->print;
|
|
|
|
} else {
|
|
|
|
mi->print[0] = '\0';
|
|
|
|
text = mi->text;
|
|
|
|
}
|
|
|
|
|
2009-12-08 19:52:17 +03:00
|
|
|
mc->width = MAX(mc->width, font_width(sc, text,
|
2008-05-20 18:50:51 +04:00
|
|
|
MIN(strlen(text), MENU_MAXENTRY)));
|
2012-10-23 20:08:59 +04:00
|
|
|
mc->height += font_height(sc);
|
2008-05-21 18:11:19 +04:00
|
|
|
mc->num++;
|
2008-05-20 18:50:51 +04:00
|
|
|
}
|
|
|
|
|
2011-05-05 19:32:24 +04:00
|
|
|
xine = screen_find_xinerama(sc, mc->x, mc->y);
|
2013-01-08 01:53:23 +04:00
|
|
|
xine.w += xine.x;
|
|
|
|
xine.h += xine.y;
|
2011-05-05 19:32:24 +04:00
|
|
|
|
2008-05-20 18:50:51 +04:00
|
|
|
xsave = mc->x;
|
|
|
|
ysave = mc->y;
|
|
|
|
|
2012-10-23 20:13:59 +04:00
|
|
|
/* Never hide the top, or left side, of the menu. */
|
2013-01-02 20:26:34 +04:00
|
|
|
if (mc->x + mc->width >= xine.w)
|
|
|
|
mc->x = xine.w - mc->width;
|
|
|
|
if (mc->x < xine.x) {
|
|
|
|
mc->x = xine.x;
|
|
|
|
mc->width = xine.w - xine.x;
|
2012-10-23 19:50:15 +04:00
|
|
|
}
|
2013-01-02 20:26:34 +04:00
|
|
|
if (mc->y + mc->height >= xine.h)
|
|
|
|
mc->y = xine.h - mc->height;
|
|
|
|
if (mc->y < xine.y) {
|
|
|
|
mc->y = xine.y;
|
|
|
|
mc->height = xine.h - xine.y;
|
2011-05-05 19:32:24 +04:00
|
|
|
}
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
if (mc->x != xsave || mc->y != ysave)
|
|
|
|
xu_ptr_setpos(sc->rootwin, mc->x, mc->y);
|
|
|
|
|
|
|
|
XClearWindow(X_Dpy, sc->menuwin);
|
2012-10-23 20:08:59 +04:00
|
|
|
XMoveResizeWindow(X_Dpy, sc->menuwin, mc->x, mc->y,
|
|
|
|
mc->width, mc->height);
|
2008-05-20 18:50:51 +04:00
|
|
|
|
2008-05-21 18:11:19 +04:00
|
|
|
if (mc->hasprompt) {
|
2012-12-17 06:28:45 +04:00
|
|
|
font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin, 0,
|
|
|
|
0, font_ascent(sc));
|
2008-05-21 18:11:19 +04:00
|
|
|
n = 1;
|
|
|
|
} else
|
|
|
|
n = 0;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
TAILQ_FOREACH(mi, resultq, resultentry) {
|
|
|
|
char *text = mi->print[0] != '\0' ?
|
|
|
|
mi->print : mi->text;
|
2012-10-23 19:32:38 +04:00
|
|
|
int y = n * font_height(sc) + font_ascent(sc) + 1;
|
|
|
|
|
|
|
|
/* Stop drawing when menu doesn't fit inside the screen. */
|
2013-01-02 20:26:34 +04:00
|
|
|
if (mc->y + y > xine.h)
|
2012-10-23 19:32:38 +04:00
|
|
|
break;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
2008-06-15 06:47:46 +04:00
|
|
|
font_draw(sc, text, MIN(strlen(text), MENU_MAXENTRY),
|
2012-12-17 06:28:45 +04:00
|
|
|
sc->menuwin, 0, 0, y);
|
2008-05-20 18:50:51 +04:00
|
|
|
n++;
|
|
|
|
}
|
2012-12-17 06:28:45 +04:00
|
|
|
if (mc->hasprompt && n > 1 && (mc->searchstr[0] != '\0')) {
|
|
|
|
mc->entry = 1;
|
|
|
|
menu_draw_entry(sc, mc, resultq, mc->entry, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
menu_draw_entry(struct screen_ctx *sc, struct menu_ctx *mc,
|
|
|
|
struct menu_q *resultq, int entry, int active)
|
|
|
|
{
|
|
|
|
struct menu *mi;
|
|
|
|
char *text;
|
|
|
|
int color, i = 0;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
2012-12-17 06:28:45 +04:00
|
|
|
if (mc->hasprompt)
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(mi, resultq, resultentry)
|
|
|
|
if (entry == i++)
|
|
|
|
break;
|
|
|
|
if (mi == NULL)
|
|
|
|
return;
|
2012-12-17 06:53:29 +04:00
|
|
|
|
2012-12-17 06:28:45 +04:00
|
|
|
color = active ? CWM_COLOR_MENU_FG : CWM_COLOR_MENU_BG;
|
2012-12-17 06:53:29 +04:00
|
|
|
text = mi->print[0] != '\0' ? mi->print : mi->text;
|
2012-12-17 06:28:45 +04:00
|
|
|
XftDrawRect(sc->xftdraw, &sc->xftcolor[color], 0,
|
2012-12-17 06:53:29 +04:00
|
|
|
font_height(sc) * entry, mc->width,
|
|
|
|
font_height(sc) + font_descent(sc));
|
2012-12-17 06:28:45 +04:00
|
|
|
font_draw(sc, text, strlen(text), sc->menuwin, active,
|
2012-12-17 06:53:29 +04:00
|
|
|
0, font_height(sc) * entry + font_ascent(sc) + 1);
|
2008-05-21 18:11:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:21:58 +04:00
|
|
|
static void
|
2012-12-17 06:28:45 +04:00
|
|
|
menu_handle_move(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc,
|
|
|
|
struct menu_q *resultq)
|
2008-05-21 18:11:19 +04:00
|
|
|
{
|
|
|
|
mc->prev = mc->entry;
|
|
|
|
mc->entry = menu_calc_entry(sc, mc, e->xbutton.x, e->xbutton.y);
|
|
|
|
|
2012-12-17 06:28:45 +04:00
|
|
|
if (mc->prev == mc->entry)
|
|
|
|
return;
|
|
|
|
|
2008-05-21 18:11:19 +04:00
|
|
|
if (mc->prev != -1)
|
2012-12-17 06:28:45 +04:00
|
|
|
menu_draw_entry(sc, mc, resultq, mc->prev, 0);
|
2008-05-21 18:11:19 +04:00
|
|
|
if (mc->entry != -1) {
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)xu_ptr_regrab(MENUGRABMASK, Cursor_normal);
|
2012-12-17 06:28:45 +04:00
|
|
|
menu_draw_entry(sc, mc, resultq, mc->entry, 1);
|
2008-05-21 18:11:19 +04:00
|
|
|
} else
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)xu_ptr_regrab(MENUGRABMASK, Cursor_default);
|
2012-12-17 06:53:29 +04:00
|
|
|
|
2012-12-17 06:28:45 +04:00
|
|
|
if (mc->hasprompt)
|
|
|
|
menu_draw_entry(sc, mc, resultq, 1, 1);
|
2008-05-21 18:11:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:21:58 +04:00
|
|
|
static struct menu *
|
2008-05-21 18:11:19 +04:00
|
|
|
menu_handle_release(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc,
|
|
|
|
struct menu_q *resultq)
|
|
|
|
{
|
|
|
|
struct menu *mi;
|
|
|
|
int entry, i = 0;
|
|
|
|
|
|
|
|
entry = menu_calc_entry(sc, mc, e->xbutton.x, e->xbutton.y);
|
|
|
|
|
|
|
|
if (mc->hasprompt)
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(mi, resultq, resultentry)
|
|
|
|
if (entry == i++)
|
|
|
|
break;
|
|
|
|
if (mi == NULL) {
|
2009-06-20 04:22:39 +04:00
|
|
|
mi = xmalloc(sizeof(*mi));
|
2008-05-21 18:11:19 +04:00
|
|
|
mi->text[0] = '\0';
|
|
|
|
mi->dummy = 1;
|
|
|
|
}
|
|
|
|
return (mi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
menu_calc_entry(struct screen_ctx *sc, struct menu_ctx *mc, int x, int y)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
int entry;
|
|
|
|
|
2009-12-08 19:52:17 +03:00
|
|
|
entry = y / font_height(sc);
|
2008-05-21 18:11:19 +04:00
|
|
|
|
|
|
|
/* in bounds? */
|
2012-12-17 06:28:45 +04:00
|
|
|
if (x < 0 || x > mc->width || y < 0 ||
|
2009-12-08 19:52:17 +03:00
|
|
|
y > font_height(sc) * mc->num || entry < 0 || entry >= mc->num)
|
2008-05-21 18:11:19 +04:00
|
|
|
entry = -1;
|
|
|
|
|
|
|
|
if (mc->hasprompt && entry == 0)
|
|
|
|
entry = -1;
|
2008-05-20 18:50:51 +04:00
|
|
|
|
2008-05-21 18:11:19 +04:00
|
|
|
return (entry);
|
2008-05-20 18:50:51 +04:00
|
|
|
}
|
2011-03-22 13:47:59 +03:00
|
|
|
|
|
|
|
static int
|
2012-08-07 18:05:49 +04:00
|
|
|
menu_keycode(XKeyEvent *ev, enum ctltype *ctl, char *chr)
|
2011-03-22 13:47:59 +03:00
|
|
|
{
|
2012-08-07 18:05:49 +04:00
|
|
|
KeySym ks;
|
|
|
|
u_int state = ev->state;
|
2011-03-22 13:47:59 +03:00
|
|
|
|
|
|
|
*ctl = CTL_NONE;
|
2012-08-07 18:05:49 +04:00
|
|
|
chr[0] = '\0';
|
2011-03-22 13:47:59 +03:00
|
|
|
|
2012-08-07 18:05:49 +04:00
|
|
|
ks = XkbKeycodeToKeysym(X_Dpy, ev->keycode, 0,
|
|
|
|
(state & ShiftMask) ? 1 : 0);
|
2011-03-22 13:47:59 +03:00
|
|
|
|
|
|
|
/* Look for control characters. */
|
|
|
|
switch (ks) {
|
|
|
|
case XK_BackSpace:
|
|
|
|
*ctl = CTL_ERASEONE;
|
|
|
|
break;
|
|
|
|
case XK_Return:
|
|
|
|
*ctl = CTL_RETURN;
|
|
|
|
break;
|
2012-11-07 18:39:44 +04:00
|
|
|
case XK_Tab:
|
|
|
|
*ctl = CTL_TAB;
|
|
|
|
break;
|
2011-03-22 13:47:59 +03:00
|
|
|
case XK_Up:
|
|
|
|
*ctl = CTL_UP;
|
|
|
|
break;
|
|
|
|
case XK_Down:
|
|
|
|
*ctl = CTL_DOWN;
|
|
|
|
break;
|
|
|
|
case XK_Escape:
|
|
|
|
*ctl = CTL_ABORT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ctl == CTL_NONE && (state & ControlMask)) {
|
|
|
|
switch (ks) {
|
|
|
|
case XK_s:
|
|
|
|
case XK_S:
|
|
|
|
/* Emacs "next" */
|
|
|
|
*ctl = CTL_DOWN;
|
|
|
|
break;
|
|
|
|
case XK_r:
|
|
|
|
case XK_R:
|
|
|
|
/* Emacs "previous" */
|
|
|
|
*ctl = CTL_UP;
|
|
|
|
break;
|
|
|
|
case XK_u:
|
|
|
|
case XK_U:
|
|
|
|
*ctl = CTL_WIPE;
|
|
|
|
break;
|
|
|
|
case XK_h:
|
|
|
|
case XK_H:
|
|
|
|
*ctl = CTL_ERASEONE;
|
|
|
|
break;
|
|
|
|
case XK_a:
|
|
|
|
case XK_A:
|
|
|
|
*ctl = CTL_ALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ctl == CTL_NONE && (state & Mod1Mask)) {
|
|
|
|
switch (ks) {
|
|
|
|
case XK_j:
|
|
|
|
case XK_J:
|
|
|
|
/* Vi "down" */
|
|
|
|
*ctl = CTL_DOWN;
|
|
|
|
break;
|
|
|
|
case XK_k:
|
|
|
|
case XK_K:
|
|
|
|
/* Vi "up" */
|
|
|
|
*ctl = CTL_UP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ctl != CTL_NONE)
|
|
|
|
return (0);
|
|
|
|
|
2012-08-07 18:05:49 +04:00
|
|
|
if (XLookupString(ev, chr, 32, &ks, NULL) < 0)
|
2011-03-22 13:47:59 +03:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2012-12-17 18:32:39 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
menuq_clear(struct menu_q *mq)
|
|
|
|
{
|
|
|
|
struct menu *mi;
|
|
|
|
|
|
|
|
while ((mi = TAILQ_FIRST(mq)) != NULL) {
|
|
|
|
TAILQ_REMOVE(mq, mi, entry);
|
|
|
|
free(mi);
|
|
|
|
}
|
|
|
|
}
|