Add support for Home and End keys for moving

This commit is contained in:
phillbush 2020-09-16 21:48:46 -03:00
parent d3c2131250
commit 006c94ce9c
2 changed files with 38 additions and 20 deletions

53
xmenu.c
View File

@ -1150,38 +1150,53 @@ getitem(struct Menu *menu, int y)
static struct Item * static struct Item *
itemcycle(struct Menu *currmenu, int direction) itemcycle(struct Menu *currmenu, int direction)
{ {
struct Item *item; struct Item *item = NULL;
struct Item *lastitem; struct Item *lastitem;
item = NULL; for (lastitem = currmenu->list; lastitem && lastitem->next; lastitem = lastitem->next)
;
if (direction == ITEMNEXT) { /* select item (either separator or labeled item) in given direction */
switch (direction) {
case ITEMNEXT:
if (currmenu->selected == NULL) if (currmenu->selected == NULL)
item = currmenu->list; item = currmenu->list;
else if (currmenu->selected->next != NULL) else if (currmenu->selected->next != NULL)
item = currmenu->selected->next; item = currmenu->selected->next;
break;
while (item != NULL && item->label == NULL) case ITEMPREV:
item = item->next;
if (item == NULL)
item = currmenu->list;
} else {
for (lastitem = currmenu->list;
lastitem != NULL && lastitem->next != NULL;
lastitem = lastitem->next)
;
if (currmenu->selected == NULL) if (currmenu->selected == NULL)
item = lastitem; item = lastitem;
else if (currmenu->selected->prev != NULL) else if (currmenu->selected->prev != NULL)
item = currmenu->selected->prev; item = currmenu->selected->prev;
break;
case ITEMFIRST:
item = currmenu->list;
break;
case ITEMLAST:
item = lastitem;
break;
}
/*
* the selected item can be a separator
* let's select the closest labeled item (ie., one that isn't a separator)
*/
switch (direction) {
case ITEMNEXT:
case ITEMFIRST:
while (item != NULL && item->label == NULL)
item = item->next;
if (item == NULL)
item = currmenu->list;
break;
case ITEMPREV:
case ITEMLAST:
while (item != NULL && item->label == NULL) while (item != NULL && item->label == NULL)
item = item->prev; item = item->prev;
if (item == NULL) if (item == NULL)
item = lastitem; item = lastitem;
break;
} }
return item; return item;
@ -1257,7 +1272,11 @@ selectitem:
/* cycle through menu */ /* cycle through menu */
item = NULL; item = NULL;
if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) { if (ksym == XK_Home) {
item = itemcycle(currmenu, ITEMFIRST);
} else if (ksym == XK_End) {
item = itemcycle(currmenu, ITEMLAST);
} else if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
item = itemcycle(currmenu, ITEMPREV); item = itemcycle(currmenu, ITEMPREV);
} else if (ksym == XK_Tab || ksym == XK_Down) { } else if (ksym == XK_Tab || ksym == XK_Down) {
item = itemcycle(currmenu, ITEMNEXT); item = itemcycle(currmenu, ITEMNEXT);

View File

@ -1,8 +1,7 @@
#define PROGNAME "xmenu" #define PROGNAME "xmenu"
/* macros for keyboard menu navigation */ /* enum for keyboard menu navigation */
#define ITEMPREV 0 enum { ITEMPREV, ITEMNEXT, ITEMFIRST, ITEMLAST };
#define ITEMNEXT 1
/* macros */ /* macros */
#define LEN(x) (sizeof (x) / sizeof (x[0])) #define LEN(x) (sizeof (x) / sizeof (x[0]))