some work on event selector

This commit is contained in:
Cedric Roux
2016-05-01 22:43:36 +02:00
parent e6d5f22c8b
commit c3e59b16b3
10 changed files with 275 additions and 64 deletions
+130 -1
View File
@@ -1,13 +1,106 @@
#include "event_selector.h"
#include "gui/gui.h"
#include "database.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
void setup_event_selector(gui *g, void *database, int socket, int *is_on)
struct event_selector {
int *is_on;
int red;
int green;
gui *g;
widget *events;
widget *groups;
void *database;
int nevents;
int ngroups;
int socket;
};
static void scroll(void *private, gui *g,
char *notification, widget *w, void *notification_data)
{
int visible_lines;
int start_line;
int number_of_lines;
int new_line;
int inc;
text_list_state(g, w, &visible_lines, &start_line, &number_of_lines);
inc = 10;
if (inc > visible_lines - 2) inc = visible_lines - 2;
if (inc < 1) inc = 1;
if (!strcmp(notification, "scrollup")) inc = -inc;
new_line = start_line + inc;
if (new_line > number_of_lines - visible_lines)
new_line = number_of_lines - visible_lines;
if (new_line < 0) new_line = 0;
text_list_set_start_line(g, w, new_line);
}
static void click(void *private, gui *g,
char *notification, widget *w, void *notification_data)
{
int *d = notification_data;
struct event_selector *this = private;
int set_on;
int line = d[0];
int button = d[1];
char *text;
int color;
int i;
char t;
if (button != 1 && button != 3) return;
if (button == 1) set_on = 1; else set_on = 0;
if (w == this->events)
text_list_get_line(this->g, this->events, line, &text, &color);
else
text_list_get_line(this->g, this->groups, line, &text, &color);
on_off(this->database, text, this->is_on, set_on);
for (i = 0; i < this->nevents; i++)
text_list_set_color(this->g, this->events, i,
this->is_on[database_pos_to_id(this->database, i)] ?
this->green : this->red);
for (i = 0; i < this->ngroups; i++)
text_list_set_color(this->g, this->groups, i, FOREGROUND_COLOR);
if (w == this->groups)
text_list_set_color(this->g, this->groups, line,
set_on ? this->green : this->red);
t = 1;
socket_send(this->socket, &t, 1);
socket_send(this->socket, &this->nevents, sizeof(int));
socket_send(this->socket, this->is_on, this->nevents * sizeof(int));
}
event_selector *setup_event_selector(gui *g, void *database, int socket,
int *is_on)
{
struct event_selector *ret;
widget *win;
widget *main_container;
widget *container;
widget *left, *right;
widget *events, *groups;
char **ids;
char **gps;
int n;
int i;
int red, green;
ret = calloc(1, sizeof(struct event_selector)); if (ret == NULL) abort();
red = new_color(g, "#944");
green = new_color(g, "#272");
win = new_toplevel_window(g, 610, 800, "event selector");
main_container = new_container(g, VERTICAL);
@@ -38,4 +131,40 @@ void setup_event_selector(gui *g, void *database, int socket, int *is_on)
widget_add_child(g, right, groups, -1);
container_set_child_growable(g, left, events, 1);
container_set_child_growable(g, right, groups, 1);
n = database_get_ids(database, &ids);
for (i = 0; i < n; i++) {
text_list_add(g, events, ids[i], -1,
is_on[database_pos_to_id(database, i)] ? green : red);
}
free(ids);
ret->nevents = n;
n = database_get_groups(database, &gps);
for (i = 0; i < n; i++) {
text_list_add(g, groups, gps[i], -1, FOREGROUND_COLOR);
}
free(gps);
ret->ngroups = n;
ret->g = g;
ret->is_on = is_on;
ret->red = red;
ret->green = green;
ret->events = events;
ret->groups = groups;
ret->database = database;
ret->socket = socket;
register_notifier(g, "scrollup", events, scroll, ret);
register_notifier(g, "scrolldown", events, scroll, ret);
register_notifier(g, "click", events, click, ret);
register_notifier(g, "scrollup", groups, scroll, ret);
register_notifier(g, "scrolldown", groups, scroll, ret);
register_notifier(g, "click", groups, click, ret);
return ret;
}