event: simplify widget get by coords

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-08-25 13:40:55 +02:00
parent da9de6938d
commit b92833274a
1 changed files with 51 additions and 53 deletions

104
event.c
View File

@ -76,6 +76,46 @@ event_handle_mouse_button(client_t *c,
} }
} }
/** Get a widget node from a statusbar by coords.
* \param Container position.
* \param widgets The widget list.
* \param height The container height.
* \param width The container width.
* \param x X coordinate of the widget.
* \param y Y coordinate of the widget.
* \return A widget node.
*/
static widget_node_t *
widget_getbycoords(position_t position, widget_node_t *widgets, int height, int width, int x, int y)
{
int tmp;
widget_node_t *w;
/* Need to transform coordinates like it was top/bottom */
switch(position)
{
case Right:
tmp = y;
y = height - x;
x = tmp;
break;
case Left:
tmp = y;
y = x;
x = width - tmp;
break;
default:
break;
}
for(w = widgets; w; w = w->next)
if(x >= w->area.x && x < w->area.x + w->area.width
&& y >= w->area.y && y < w->area.y + w->area.height)
return w;
return NULL;
}
/** The button press event handler. /** The button press event handler.
* \param data The type of mouse event. * \param data The type of mouse event.
* \param connection The connection to the X server. * \param connection The connection to the X server.
@ -84,7 +124,7 @@ event_handle_mouse_button(client_t *c,
static int static int
event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev) event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_event_t *ev)
{ {
int screen, tmp; int screen;
const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection)); const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection));
client_t *c; client_t *c;
widget_node_t *w; widget_node_t *w;
@ -108,63 +148,21 @@ event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_e
ev->event_x -= statusbar->sw->geometry.x; ev->event_x -= statusbar->sw->geometry.x;
ev->event_y -= statusbar->sw->geometry.y; ev->event_y -= statusbar->sw->geometry.y;
} }
/* Need to transform coordinates like it was if((w = widget_getbycoords(statusbar->position, statusbar->widgets,
* top/bottom */ statusbar->width, statusbar->height,
switch(statusbar->position) ev->event_x, ev->event_y)))
{ w->widget->button(w, ev, statusbar->screen, statusbar, AWESOME_TYPE_STATUSBAR);
case Right: /* return even if no widget match */
tmp = ev->event_y;
ev->event_y = statusbar->height - ev->event_x;
ev->event_x = tmp;
break;
case Left:
tmp = ev->event_y;
ev->event_y = ev->event_x;
ev->event_x = statusbar->width - tmp;
break;
default:
break;
}
for(w = statusbar->widgets; w; w = w->next)
if(ev->event_x >= w->area.x && ev->event_x < w->area.x + w->area.width
&& ev->event_y >= w->area.y && ev->event_y < w->area.y + w->area.height)
{
w->widget->button(w, ev, statusbar->screen,
statusbar, AWESOME_TYPE_STATUSBAR);
return 0;
}
/* return if no widget match */
return 0; return 0;
} }
if((c = client_getbytitlebarwin(ev->event))) if((c = client_getbytitlebarwin(ev->event)))
{ {
/* Need to transform coordinates like it was if((w = widget_getbycoords(c->titlebar->position, c->titlebar->widgets,
* top/bottom */ c->titlebar->width, c->titlebar->height,
switch(c->titlebar->position) ev->event_x, ev->event_y)))
{ w->widget->button(w, ev, c->screen, c->titlebar, AWESOME_TYPE_TITLEBAR);
case Right: /* return even if no widget match */
tmp = ev->event_y;
ev->event_y = c->titlebar->height - ev->event_x;
ev->event_x = tmp;
break;
case Left:
tmp = ev->event_y;
ev->event_y = ev->event_x;
ev->event_x = c->titlebar->width - tmp;
break;
default:
break;
}
for(w = c->titlebar->widgets; w; w = w->next)
if(ev->event_x >= w->area.x && ev->event_x < w->area.x + w->area.width
&& ev->event_y >= w->area.y && ev->event_y < w->area.y + w->area.height)
{
w->widget->button(w, ev, c->screen,
c->titlebar, AWESOME_TYPE_TITLEBAR);
return 0;
}
/* return if no widget match */
return 0; return 0;
} }