Keyboard navigatable menus

This commit is contained in:
Scott Moynes 2003-07-21 23:19:03 +00:00
parent e9a8e272c0
commit ea53850762
3 changed files with 168 additions and 23 deletions

View file

@ -966,27 +966,37 @@ static void event_handle_menu(ObClient *client, XEvent *e)
static ObMenuEntry *over = NULL; static ObMenuEntry *over = NULL;
ObMenuEntry *entry; ObMenuEntry *entry;
ObMenu *top; ObMenu *top;
GSList *it; GList *it = NULL;
top = g_slist_nth_data(menu_visible, 0); top = g_list_nth_data(menu_visible, 0);
g_message("EVENT %d", e->type); g_message("EVENT %d", e->type);
switch (e->type) { switch (e->type) {
case KeyPress: case KeyPress:
if (over) { if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
if (over->parent->mouseover) over = menu_control_keyboard_nav(over, OB_KEY_DOWN);
over->parent->mouseover(over, FALSE); else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
else over = menu_control_keyboard_nav(over, OB_KEY_UP);
menu_control_mouseover(over, FALSE); else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
menu_entry_render(over); over = menu_control_keyboard_nav(over, OB_KEY_RETURN);
over = NULL; else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
} over = menu_control_keyboard_nav(over, OB_KEY_ESCAPE);
else {
if (over) {
if (over->parent->mouseover)
over->parent->mouseover(over, FALSE);
else
menu_control_mouseover(over, FALSE);
menu_entry_render(over);
over = NULL;
}
/* /*
if (top->hide) if (top->hide)
top->hide(top); top->hide(top);
else else
*/ */
menu_hide(top); menu_hide(top);
}
break; break;
case ButtonPress: case ButtonPress:
if (e->xbutton.button > 3) break; if (e->xbutton.button > 3) break;
@ -998,7 +1008,7 @@ static void event_handle_menu(ObClient *client, XEvent *e)
g_message("BUTTON RELEASED"); g_message("BUTTON RELEASED");
for (it = menu_visible; it; it = g_slist_next(it)) { for (it = menu_visible; it; it = g_list_next(it)) {
ObMenu *m = it->data; ObMenu *m = it->data;
if (e->xbutton.x_root >= m->location.x - ob_rr_theme->bwidth && if (e->xbutton.x_root >= m->location.x - ob_rr_theme->bwidth &&
e->xbutton.y_root >= m->location.y - ob_rr_theme->bwidth && e->xbutton.y_root >= m->location.y - ob_rr_theme->bwidth &&
@ -1045,7 +1055,7 @@ static void event_handle_menu(ObClient *client, XEvent *e)
break; break;
case MotionNotify: case MotionNotify:
g_message("motion"); g_message("motion");
for (it = menu_visible; it; it = g_slist_next(it)) { for (it = menu_visible; it; it = g_list_next(it)) {
ObMenu *m = it->data; ObMenu *m = it->data;
if ((entry = menu_find_entry_by_pos(it->data, if ((entry = menu_find_entry_by_pos(it->data,
e->xmotion.x_root - e->xmotion.x_root -

View file

@ -6,9 +6,10 @@
#include "screen.h" #include "screen.h"
#include "geom.h" #include "geom.h"
#include "plugin.h" #include "plugin.h"
#include "misc.h"
GHashTable *menu_hash = NULL; GHashTable *menu_hash = NULL;
GSList *menu_visible = NULL; GList *menu_visible = NULL;
#define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \ #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
LeaveWindowMask) LeaveWindowMask)
@ -337,11 +338,11 @@ void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
self->client = client; self->client = client;
if (!self->shown) { if (!self->shown) {
if (!self->parent) { if (!(self->parent && self->parent->shown)) {
grab_pointer(TRUE, None); grab_pointer(TRUE, None);
grab_keyboard(TRUE); grab_keyboard(TRUE);
} }
menu_visible = g_slist_append(menu_visible, self); menu_visible = g_list_append(menu_visible, self);
} }
if (self->show) { if (self->show) {
@ -360,11 +361,11 @@ void menu_hide(ObMenu *self) {
if (self->parent && self->parent->open_submenu == self) if (self->parent && self->parent->open_submenu == self)
self->parent->open_submenu = NULL; self->parent->open_submenu = NULL;
if (!self->parent) { if (!(self->parent && self->parent->shown)) {
grab_keyboard(FALSE); grab_keyboard(FALSE);
grab_pointer(FALSE, None); grab_pointer(FALSE, None);
} }
menu_visible = g_slist_remove(menu_visible, self); menu_visible = g_list_remove(menu_visible, self);
} }
} }
@ -423,7 +424,8 @@ void menu_entry_fire(ObMenuEntry *self)
Default menu controller action for showing. Default menu controller action for showing.
*/ */
void menu_control_show(ObMenu *self, int x, int y, ObClient *client) { void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
{
guint i; guint i;
Rect *a = NULL; Rect *a = NULL;
@ -451,7 +453,8 @@ void menu_control_show(ObMenu *self, int x, int y, ObClient *client) {
} }
} }
void menu_control_mouseover(ObMenuEntry *self, gboolean enter) { void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
{
int x; int x;
Rect *a; Rect *a;
@ -487,3 +490,134 @@ void menu_control_mouseover(ObMenuEntry *self, gboolean enter) {
} }
} }
} }
ObMenuEntry *menu_control_keyboard_nav(ObMenuEntry *over, ObKey key)
{
GList *it = NULL;
switch (key) {
case OB_KEY_DOWN: {
if (over != NULL) {
if (over->parent->mouseover)
over->parent->mouseover(over, FALSE);
else
menu_control_mouseover(over, FALSE);
menu_entry_render(over);
it = over->parent->entries;
while (it != NULL && it->data != over)
it = it->next;
}
if (it && it->next)
over = (ObMenuEntry *)it->next->data;
else if (over == NULL) {
if (menu_visible && ((ObMenu *)menu_visible->data)->entries)
over = (ObMenuEntry *)
(((ObMenu *)menu_visible->data)->entries)->data;
else
over = NULL;
} else {
over = (over->parent->entries != NULL ?
over->parent->entries->data : NULL);
}
if (over) {
if (over->parent->mouseover)
over->parent->mouseover(over, TRUE);
else
menu_control_mouseover(over, TRUE);
menu_entry_render(over);
}
break;
}
case OB_KEY_UP: {
if (over != NULL) {
if (over->parent->mouseover)
over->parent->mouseover(over, FALSE);
else
menu_control_mouseover(over, FALSE);
menu_entry_render(over);
it = g_list_last(over->parent->entries);
while (it != NULL && it->data != over)
it = it->prev;
}
if (it && it->prev)
over = (ObMenuEntry *)it->prev->data;
else if (over == NULL) {
it = g_list_last(menu_visible);
if (it != NULL) {
it = g_list_last(((ObMenu *)it->data)->entries);
over = (ObMenuEntry *)(it != NULL ? it->data : NULL);
}
} else
over = (over->parent->entries != NULL ?
g_list_last(over->parent->entries)->data :
NULL);
if (over->parent->mouseover)
over->parent->mouseover(over, TRUE);
else
menu_control_mouseover(over, TRUE);
menu_entry_render(over);
break;
}
case OB_KEY_RETURN: {
if (over == NULL)
return over;
if (over->submenu) {
if (over->parent->mouseover)
over->parent->mouseover(over, FALSE);
else
menu_control_mouseover(over, FALSE);
menu_entry_render(over);
if (over->submenu->entries)
over = over->submenu->entries->data;
if (over->parent->mouseover)
over->parent->mouseover(over, TRUE);
else
menu_control_mouseover(over, TRUE);
menu_entry_render(over);
}
else {
if (over->parent->mouseover)
over->parent->mouseover(over, FALSE);
else
menu_control_mouseover(over, FALSE);
menu_entry_render(over);
menu_entry_fire(over);
}
break;
}
case OB_KEY_ESCAPE: {
if (over != NULL) {
if (over->parent->mouseover)
over->parent->mouseover(over, FALSE);
else
menu_control_mouseover(over, FALSE);
menu_entry_render(over);
menu_hide(over->parent);
} else {
it = g_list_last(menu_visible);
if (it) {
menu_hide((ObMenu *)it->data);
}
}
over = NULL;
break;
}
default:
g_error("Unknown key");
}
return over;
}

View file

@ -19,7 +19,7 @@ typedef void(*menu_controller_update)(ObMenu *self);
typedef void(*menu_controller_mouseover)(ObMenuEntry *self, gboolean enter); typedef void(*menu_controller_mouseover)(ObMenuEntry *self, gboolean enter);
extern GHashTable *menu_hash; extern GHashTable *menu_hash;
extern GSList *menu_visible; extern GList *menu_visible;
struct _ObMenu struct _ObMenu
{ {
@ -157,4 +157,5 @@ void menu_render_full(ObMenu *self);
//so plugins can call it? //so plugins can call it?
void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data, gboolean new); void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data, gboolean new);
void menu_control_mouseover(ObMenuEntry *entry, gboolean enter); void menu_control_mouseover(ObMenuEntry *entry, gboolean enter);
ObMenuEntry *menu_control_keyboard_nav(ObMenuEntry *over, ObKey key);
#endif #endif