2008-05-20 08:50:51 -06:00
|
|
|
|
/*
|
2011-05-11 07:53:51 -06:00
|
|
|
|
* calmwm - the calm window manager
|
|
|
|
|
*
|
2008-05-20 08:50:51 -06: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 07:53:51 -06:00
|
|
|
|
*
|
2011-06-24 00:52:23 -06:00
|
|
|
|
* $OpenBSD: menu.c,v 1.28 2011/06/24 06:52:23 okan Exp $
|
2008-05-20 08:50:51 -06:00
|
|
|
|
*/
|
|
|
|
|
|
2009-12-14 21:10:42 -07:00
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2008-05-20 08:50:51 -06:00
|
|
|
|
#include "calmwm.h"
|
|
|
|
|
|
2008-05-21 08:11:19 -06:00
|
|
|
|
#define PROMPT_SCHAR '<27>'
|
|
|
|
|
#define PROMPT_ECHAR '<27>'
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
2011-03-22 04:47:59 -06:00
|
|
|
|
enum ctltype {
|
|
|
|
|
CTL_NONE = -1,
|
|
|
|
|
CTL_ERASEONE = 0, CTL_WIPE, CTL_UP, CTL_DOWN, CTL_RETURN,
|
|
|
|
|
CTL_ABORT, CTL_ALL
|
|
|
|
|
};
|
|
|
|
|
|
2008-05-20 08:50:51 -06:00
|
|
|
|
struct menu_ctx {
|
|
|
|
|
char searchstr[MENU_MAXENTRY + 1];
|
|
|
|
|
char dispstr[MENU_MAXENTRY*2 + 1];
|
|
|
|
|
char promptstr[MENU_MAXENTRY + 1];
|
2008-05-21 08:11:19 -06:00
|
|
|
|
int hasprompt;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
int list;
|
|
|
|
|
int listing;
|
|
|
|
|
int changed;
|
|
|
|
|
int noresult;
|
2008-05-21 08:11:19 -06:00
|
|
|
|
int prev;
|
|
|
|
|
int entry;
|
|
|
|
|
int width;
|
|
|
|
|
int num;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
int x;
|
2008-05-21 08:11:19 -06:00
|
|
|
|
int y;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
void (*match)(struct menu_q *, struct menu_q *, char *);
|
|
|
|
|
void (*print)(struct menu *, int);
|
|
|
|
|
};
|
|
|
|
|
static struct menu *menu_handle_key(XEvent *, struct menu_ctx *,
|
|
|
|
|
struct menu_q *, struct menu_q *);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
static void menu_handle_move(XEvent *, struct menu_ctx *,
|
|
|
|
|
struct screen_ctx *);
|
2009-06-26 06:21:58 -06:00
|
|
|
|
static struct menu *menu_handle_release(XEvent *, struct menu_ctx *,
|
2008-05-21 08:11:19 -06:00
|
|
|
|
struct screen_ctx *, struct menu_q *);
|
2008-05-20 08:50:51 -06:00
|
|
|
|
static void menu_draw(struct screen_ctx *, struct menu_ctx *,
|
|
|
|
|
struct menu_q *, struct menu_q *);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
static int menu_calc_entry(struct screen_ctx *, struct menu_ctx *,
|
|
|
|
|
int, int);
|
2011-03-22 04:47:59 -06:00
|
|
|
|
static int menu_keycode(KeyCode, u_int, enum ctltype *,
|
|
|
|
|
char *);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
menu_init(struct screen_ctx *sc)
|
|
|
|
|
{
|
2009-05-17 17:40:57 -06:00
|
|
|
|
XGCValues gv;
|
|
|
|
|
|
|
|
|
|
sc->menuwin = XCreateSimpleWindow(X_Dpy, sc->rootwin, 0, 0, 1, 1, 0,
|
|
|
|
|
sc->color[CWM_COLOR_BG_MENU].pixel,
|
|
|
|
|
sc->color[CWM_COLOR_BG_MENU].pixel);
|
|
|
|
|
|
|
|
|
|
gv.foreground =
|
|
|
|
|
sc->color[CWM_COLOR_FG_MENU].pixel^sc->color[CWM_COLOR_BG_MENU].pixel;
|
|
|
|
|
gv.background = sc->color[CWM_COLOR_BG_MENU].pixel;
|
|
|
|
|
gv.function = GXxor;
|
|
|
|
|
|
|
|
|
|
sc->gc = XCreateGC(X_Dpy, sc->menuwin,
|
|
|
|
|
GCForeground|GCBackground|GCFunction, &gv);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
}
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
|
|
|
|
struct menu *
|
2009-12-10 10:16:51 -07:00
|
|
|
|
menu_filter(struct screen_ctx *sc, struct menu_q *menuq, char *prompt,
|
|
|
|
|
char *initial, int dummy,
|
2008-05-20 08:50:51 -06: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 10:38:54 -06:00
|
|
|
|
int evmask, focusrevert;
|
2011-03-22 07:50:40 -06:00
|
|
|
|
int xsave, ysave, xcur, ycur;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
|
|
|
|
TAILQ_INIT(&resultq);
|
|
|
|
|
|
|
|
|
|
bzero(&mc, sizeof(mc));
|
|
|
|
|
|
|
|
|
|
xu_ptr_getpos(sc->rootwin, &mc.x, &mc.y);
|
|
|
|
|
|
2011-03-22 07:50:40 -06:00
|
|
|
|
xsave = mc.x;
|
|
|
|
|
ysave = mc.y;
|
|
|
|
|
|
2008-05-21 08:11:19 -06:00
|
|
|
|
if (prompt == NULL) {
|
2009-03-28 10:38:54 -06:00
|
|
|
|
evmask = MenuMask;
|
2008-05-21 08:11:19 -06:00
|
|
|
|
mc.promptstr[0] = '\0';
|
|
|
|
|
mc.list = 1;
|
|
|
|
|
} else {
|
2009-03-28 10:38:54 -06:00
|
|
|
|
evmask = MenuMask | KeyMask; /* only accept keys if prompt */
|
2008-05-21 08:11:19 -06:00
|
|
|
|
snprintf(mc.promptstr, sizeof(mc.promptstr), "%s%c", prompt,
|
|
|
|
|
PROMPT_SCHAR);
|
|
|
|
|
snprintf(mc.dispstr, sizeof(mc.dispstr), "%s%s%c", mc.promptstr,
|
|
|
|
|
mc.searchstr, PROMPT_ECHAR);
|
2009-12-08 09:52:17 -07:00
|
|
|
|
mc.width = font_width(sc, mc.dispstr, strlen(mc.dispstr));
|
2008-05-21 08:11:19 -06:00
|
|
|
|
mc.hasprompt = 1;
|
|
|
|
|
}
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
|
|
|
|
if (initial != NULL)
|
|
|
|
|
strlcpy(mc.searchstr, initial, sizeof(mc.searchstr));
|
|
|
|
|
else
|
|
|
|
|
mc.searchstr[0] = '\0';
|
|
|
|
|
|
|
|
|
|
mc.match = match;
|
|
|
|
|
mc.print = print;
|
2008-05-21 08:11:19 -06:00
|
|
|
|
mc.entry = mc.prev = -1;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
2008-05-21 08:11:19 -06:00
|
|
|
|
XMoveResizeWindow(X_Dpy, sc->menuwin, mc.x, mc.y, mc.width,
|
2009-12-08 09:52:17 -07:00
|
|
|
|
font_height(sc));
|
2009-03-28 10:38:54 -06:00
|
|
|
|
XSelectInput(X_Dpy, sc->menuwin, evmask);
|
2008-05-20 08:50:51 -06:00
|
|
|
|
XMapRaised(X_Dpy, sc->menuwin);
|
|
|
|
|
|
2008-05-21 08:11:19 -06:00
|
|
|
|
if (xu_ptr_grab(sc->menuwin, MenuGrabMask, Cursor_question) < 0) {
|
2008-05-20 08:50:51 -06:00
|
|
|
|
XUnmapWindow(X_Dpy, sc->menuwin);
|
|
|
|
|
return (NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XGetInputFocus(X_Dpy, &focuswin, &focusrevert);
|
|
|
|
|
XSetInputFocus(X_Dpy, sc->menuwin, RevertToPointerRoot, CurrentTime);
|
|
|
|
|
|
2008-05-23 12:48:57 -06:00
|
|
|
|
/* make sure keybindings don't remove keys from the menu stream */
|
|
|
|
|
XGrabKeyboard(X_Dpy, sc->menuwin, True,
|
|
|
|
|
GrabModeAsync, GrabModeAsync, CurrentTime);
|
|
|
|
|
|
2008-05-20 08:50:51 -06:00
|
|
|
|
for (;;) {
|
|
|
|
|
mc.changed = 0;
|
|
|
|
|
|
2009-03-28 10:38:54 -06:00
|
|
|
|
XWindowEvent(X_Dpy, sc->menuwin, evmask, &e);
|
2008-05-20 08:50:51 -06: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 08:11:19 -06:00
|
|
|
|
case MotionNotify:
|
|
|
|
|
menu_handle_move(&e, &mc, sc);
|
|
|
|
|
break;
|
|
|
|
|
case ButtonRelease:
|
|
|
|
|
if ((mi = menu_handle_release(&e, &mc, sc, &resultq))
|
|
|
|
|
!= NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
break;
|
2011-03-22 05:03:05 -06:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out:
|
2011-03-22 05:09:52 -06:00
|
|
|
|
if (dummy == 0 && mi->dummy) { /* no mouse based match */
|
|
|
|
|
xfree(mi);
|
2008-05-20 08:50:51 -06:00
|
|
|
|
mi = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-22 05:09:52 -06:00
|
|
|
|
XSetInputFocus(X_Dpy, focuswin, focusrevert, CurrentTime);
|
2011-03-22 07:50:40 -06: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 05:09:52 -06:00
|
|
|
|
xu_ptr_ungrab();
|
|
|
|
|
|
2008-05-20 08:50:51 -06:00
|
|
|
|
XUnmapWindow(X_Dpy, sc->menuwin);
|
2008-05-23 12:48:57 -06:00
|
|
|
|
XUngrabKeyboard(X_Dpy, CurrentTime);
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
|
|
|
|
return (mi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
char chr;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
2011-03-22 04:47:59 -06:00
|
|
|
|
if (menu_keycode(e->xkey.keycode, e->xkey.state, &ctl, &chr) < 0)
|
2008-05-20 08:50:51 -06:00
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
|
|
switch (ctl) {
|
|
|
|
|
case CTL_ERASEONE:
|
|
|
|
|
if ((len = strlen(mc->searchstr)) > 0) {
|
|
|
|
|
mc->searchstr[len - 1] = '\0';
|
|
|
|
|
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-09 18:23:05 -07:00
|
|
|
|
mi->abort = 0;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
return (mi);
|
|
|
|
|
case CTL_WIPE:
|
|
|
|
|
mc->searchstr[0] = '\0';
|
|
|
|
|
mc->changed = 1;
|
|
|
|
|
break;
|
|
|
|
|
case CTL_ALL:
|
|
|
|
|
mc->list = !mc->list;
|
|
|
|
|
break;
|
|
|
|
|
case CTL_ABORT:
|
|
|
|
|
mi = xmalloc(sizeof *mi);
|
|
|
|
|
mi->text[0] = '\0';
|
|
|
|
|
mi->dummy = 1;
|
2010-02-09 18:23:05 -07:00
|
|
|
|
mi->abort = 1;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
return (mi);
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chr != '\0') {
|
|
|
|
|
char str[2];
|
|
|
|
|
|
|
|
|
|
str[0] = chr;
|
|
|
|
|
str[1] = '\0';
|
|
|
|
|
mc->changed = 1;
|
|
|
|
|
strlcat(mc->searchstr, str, sizeof(mc->searchstr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mc->noresult = 0;
|
2009-09-05 08:10:11 -06:00
|
|
|
|
if (mc->changed && mc->searchstr[0] != '\0') {
|
2008-05-20 08:50:51 -06: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 08:10:11 -06:00
|
|
|
|
if (!mc->list && mc->listing && !mc->changed) {
|
2008-05-20 08:50:51 -06: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 09:32:24 -06:00
|
|
|
|
struct menu *mi;
|
|
|
|
|
XineramaScreenInfo *xine;
|
|
|
|
|
int xmin, xmax, ymin, ymax;
|
|
|
|
|
int n, dy, xsave, ysave;
|
2008-05-20 08:50:51 -06: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 08:11:19 -06:00
|
|
|
|
mc->num = 0;
|
|
|
|
|
mc->width = 0;
|
|
|
|
|
dy = 0;
|
|
|
|
|
if (mc->hasprompt) {
|
|
|
|
|
snprintf(mc->dispstr, sizeof(mc->dispstr), "%s%s%c",
|
|
|
|
|
mc->promptstr, mc->searchstr, PROMPT_ECHAR);
|
2009-12-08 09:52:17 -07:00
|
|
|
|
mc->width = font_width(sc, mc->dispstr, strlen(mc->dispstr));
|
|
|
|
|
dy = font_height(sc);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
mc->num = 1;
|
|
|
|
|
}
|
2008-05-20 08:50:51 -06: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 09:52:17 -07:00
|
|
|
|
mc->width = MAX(mc->width, font_width(sc, text,
|
2008-05-20 08:50:51 -06:00
|
|
|
|
MIN(strlen(text), MENU_MAXENTRY)));
|
2009-12-08 09:52:17 -07:00
|
|
|
|
dy += font_height(sc);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
mc->num++;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-05 09:32:24 -06:00
|
|
|
|
xine = screen_find_xinerama(sc, mc->x, mc->y);
|
|
|
|
|
if (xine) {
|
|
|
|
|
xmin = xine->x_org;
|
|
|
|
|
xmax = xine->x_org + xine->width;
|
|
|
|
|
ymin = xine->y_org;
|
|
|
|
|
ymax = xine->y_org + xine->height;
|
|
|
|
|
} else {
|
|
|
|
|
xmin = ymin = 0;
|
|
|
|
|
xmax = sc->xmax;
|
|
|
|
|
ymax = sc->ymax;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-20 08:50:51 -06:00
|
|
|
|
xsave = mc->x;
|
|
|
|
|
ysave = mc->y;
|
|
|
|
|
|
2011-05-05 09:32:24 -06:00
|
|
|
|
if (mc->x < xmin)
|
|
|
|
|
mc->x = xmin;
|
|
|
|
|
else if (mc->x + mc->width >= xmax)
|
|
|
|
|
mc->x = xmax - mc->width;
|
|
|
|
|
|
|
|
|
|
if (mc->y + dy >= ymax)
|
|
|
|
|
mc->y = ymax - dy;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
/* never hide the top of the menu */
|
2011-05-05 09:32:24 -06:00
|
|
|
|
if (mc->y < ymin) {
|
|
|
|
|
mc->y = ymin;
|
|
|
|
|
dy = ymax - ymin;
|
|
|
|
|
}
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
|
|
|
|
if (mc->x != xsave || mc->y != ysave)
|
|
|
|
|
xu_ptr_setpos(sc->rootwin, mc->x, mc->y);
|
|
|
|
|
|
|
|
|
|
XClearWindow(X_Dpy, sc->menuwin);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
XMoveResizeWindow(X_Dpy, sc->menuwin, mc->x, mc->y, mc->width, dy);
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
2008-05-21 08:11:19 -06:00
|
|
|
|
if (mc->hasprompt) {
|
2008-06-14 20:47:46 -06:00
|
|
|
|
font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin,
|
2009-12-08 09:52:17 -07:00
|
|
|
|
0, font_ascent(sc) + 1);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
n = 1;
|
|
|
|
|
} else
|
|
|
|
|
n = 0;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
|
|
|
|
TAILQ_FOREACH(mi, resultq, resultentry) {
|
|
|
|
|
char *text = mi->print[0] != '\0' ?
|
|
|
|
|
mi->print : mi->text;
|
|
|
|
|
|
2008-06-14 20:47:46 -06:00
|
|
|
|
font_draw(sc, text, MIN(strlen(text), MENU_MAXENTRY),
|
2009-12-08 09:52:17 -07:00
|
|
|
|
sc->menuwin, 0, n * font_height(sc) + font_ascent(sc) + 1);
|
2008-05-20 08:50:51 -06:00
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-22 05:05:13 -06:00
|
|
|
|
if (mc->hasprompt && n > 1 && (mc->searchstr[0] != '\0'))
|
2008-05-20 08:50:51 -06:00
|
|
|
|
XFillRectangle(X_Dpy, sc->menuwin, sc->gc,
|
2009-12-08 09:52:17 -07:00
|
|
|
|
0, font_height(sc), mc->width, font_height(sc));
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
|
|
|
|
if (mc->noresult)
|
|
|
|
|
XFillRectangle(X_Dpy, sc->menuwin, sc->gc,
|
2009-12-08 09:52:17 -07:00
|
|
|
|
0, 0, mc->width, font_height(sc));
|
2008-05-21 08:11:19 -06:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-26 06:21:58 -06:00
|
|
|
|
static void
|
2008-05-21 08:11:19 -06:00
|
|
|
|
menu_handle_move(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc)
|
|
|
|
|
{
|
|
|
|
|
mc->prev = mc->entry;
|
|
|
|
|
mc->entry = menu_calc_entry(sc, mc, e->xbutton.x, e->xbutton.y);
|
|
|
|
|
|
|
|
|
|
if (mc->prev != -1)
|
|
|
|
|
XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0,
|
2009-12-08 09:52:17 -07:00
|
|
|
|
font_height(sc) * mc->prev, mc->width, font_height(sc));
|
2008-05-21 08:11:19 -06:00
|
|
|
|
if (mc->entry != -1) {
|
2011-06-24 00:52:23 -06:00
|
|
|
|
xu_ptr_regrab(MenuGrabMask, Cursor_normal);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0,
|
2009-12-08 09:52:17 -07:00
|
|
|
|
font_height(sc) * mc->entry, mc->width, font_height(sc));
|
2008-05-21 08:11:19 -06:00
|
|
|
|
} else
|
|
|
|
|
xu_ptr_regrab(MenuGrabMask, Cursor_default);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-26 06:21:58 -06:00
|
|
|
|
static struct menu *
|
2008-05-21 08:11:19 -06: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-19 18:22:39 -06:00
|
|
|
|
mi = xmalloc(sizeof(*mi));
|
2008-05-21 08:11:19 -06: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 08:21:28 -06:00
|
|
|
|
int entry;
|
|
|
|
|
|
2009-12-08 09:52:17 -07:00
|
|
|
|
entry = y / font_height(sc);
|
2008-05-21 08:11:19 -06:00
|
|
|
|
|
|
|
|
|
/* in bounds? */
|
2009-12-08 09:52:17 -07:00
|
|
|
|
if (x <= 0 || x > mc->width || y <= 0 ||
|
|
|
|
|
y > font_height(sc) * mc->num || entry < 0 || entry >= mc->num)
|
2008-05-21 08:11:19 -06:00
|
|
|
|
entry = -1;
|
|
|
|
|
|
|
|
|
|
if (mc->hasprompt && entry == 0)
|
|
|
|
|
entry = -1;
|
2008-05-20 08:50:51 -06:00
|
|
|
|
|
2008-05-21 08:11:19 -06:00
|
|
|
|
return (entry);
|
2008-05-20 08:50:51 -06:00
|
|
|
|
}
|
2011-03-22 04:47:59 -06:00
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
menu_keycode(KeyCode kc, u_int state, enum ctltype *ctl, char *chr)
|
|
|
|
|
{
|
|
|
|
|
int ks;
|
|
|
|
|
|
|
|
|
|
*ctl = CTL_NONE;
|
|
|
|
|
*chr = '\0';
|
|
|
|
|
|
|
|
|
|
ks = XKeycodeToKeysym(X_Dpy, kc, (state & ShiftMask) ? 1 : 0);
|
|
|
|
|
|
|
|
|
|
/* Look for control characters. */
|
|
|
|
|
switch (ks) {
|
|
|
|
|
case XK_BackSpace:
|
|
|
|
|
*ctl = CTL_ERASEONE;
|
|
|
|
|
break;
|
|
|
|
|
case XK_Return:
|
|
|
|
|
*ctl = CTL_RETURN;
|
|
|
|
|
break;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* For regular characters, only (part of, actually) Latin 1
|
|
|
|
|
* for now.
|
|
|
|
|
*/
|
|
|
|
|
if (ks < 0x20 || ks > 0x07e)
|
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
|
|
*chr = (char)ks;
|
|
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
|
}
|