event_selector.c 6.93 KB
Newer Older
1 2
#include "event_selector.h"
#include "gui/gui.h"
Cedric Roux's avatar
Cedric Roux committed
3 4 5 6
#include "database.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
7

Cedric Roux's avatar
Cedric Roux committed
8 9
struct event_selector {
  int *is_on;
10 11
  int *is_on_paused;    /* when pausing, is_on is set to all 0, this one
                         * is used to copy back data when un-pausing */
Cedric Roux's avatar
Cedric Roux committed
12 13 14 15 16 17 18 19
  int red;
  int green;
  gui *g;
  widget *events;
  widget *groups;
  void *database;
  int nevents;
  int ngroups;
20 21 22 23 24
  int paused;
  /* those three widgets used to pause/unpause reception of events */
  widget *parent_widget;
  widget *normal_widget;
  widget *pause_widget;
25 26
  void (*change_callback)(void *change_callback_data);
  void *change_callback_data;
Cedric Roux's avatar
Cedric Roux committed
27 28 29 30 31 32 33 34 35 36
};

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;
Cedric Roux's avatar
Cedric Roux committed
37 38
  int *d = notification_data;
  int key_modifiers = *d;
Cedric Roux's avatar
Cedric Roux committed
39

40
  textlist_state(g, w, &visible_lines, &start_line, &number_of_lines);
Cedric Roux's avatar
Cedric Roux committed
41 42 43
  inc = 10;
  if (inc > visible_lines - 2) inc = visible_lines - 2;
  if (inc < 1) inc = 1;
Cedric Roux's avatar
Cedric Roux committed
44
  if (key_modifiers & KEY_CONTROL) inc = 1;
Cedric Roux's avatar
Cedric Roux committed
45 46 47 48 49 50 51
  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;

52
  textlist_set_start_line(g, w, new_line);
Cedric Roux's avatar
Cedric Roux committed
53 54 55 56
}

static void click(void *private, gui *g,
    char *notification, widget *w, void *notification_data)
57
{
Cedric Roux's avatar
Cedric Roux committed
58 59 60 61 62 63 64 65 66
  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;

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  /* notification_data depends on the kind of widget */
  if (w == this->pause_widget) {
    line = 0;
    button = d[0];
  } else {
    line = d[0];
    button = d[1];
  }

  /* middle-button toggles - redo with SPACE when keyboard is processed */
  if (button == 2) {
    if (this->paused == 0) {
      widget_del_child(g, this->parent_widget, this->normal_widget);
      widget_add_child(g, this->parent_widget, this->pause_widget, 0);
      container_set_child_growable(g, this->parent_widget,
          this->pause_widget, 1);
      /* pause */
84 85 86
      memcpy(this->is_on_paused, this->is_on, this->nevents * sizeof(int));
      memset(this->is_on, 0, this->nevents * sizeof(int));
      this->change_callback(this->change_callback_data);
87 88 89 90 91 92
    } else {
      widget_del_child(g, this->parent_widget, this->pause_widget);
      widget_add_child(g, this->parent_widget, this->normal_widget, 0);
      container_set_child_growable(g, this->parent_widget,
          this->normal_widget, 1);
      /* un-pause */
93 94
      memcpy(this->is_on, this->is_on_paused, this->nevents * sizeof(int));
      this->change_callback(this->change_callback_data);
95 96 97 98 99 100 101
    }
    this->paused = 1 - this->paused;
    return;
  }

  if (w == this->pause_widget) return;

Cedric Roux's avatar
Cedric Roux committed
102 103 104 105 106
  if (button != 1 && button != 3) return;

  if (button == 1) set_on = 1; else set_on = 0;

  if (w == this->events)
107
    textlist_get_line(this->g, this->events, line, &text, &color);
Cedric Roux's avatar
Cedric Roux committed
108
  else
109
    textlist_get_line(this->g, this->groups, line, &text, &color);
Cedric Roux's avatar
Cedric Roux committed
110 111 112 113

  on_off(this->database, text, this->is_on, set_on);

  for (i = 0; i < this->nevents; i++)
114
    textlist_set_color(this->g, this->events, i,
Cedric Roux's avatar
Cedric Roux committed
115 116 117 118
        this->is_on[database_pos_to_id(this->database, i)] ?
            this->green : this->red);

  for (i = 0; i < this->ngroups; i++)
119
    textlist_set_color(this->g, this->groups, i, FOREGROUND_COLOR);
Cedric Roux's avatar
Cedric Roux committed
120
  if (w == this->groups)
121
    textlist_set_color(this->g, this->groups, line,
Cedric Roux's avatar
Cedric Roux committed
122 123
        set_on ? this->green : this->red);

124
  this->change_callback(this->change_callback_data);
Cedric Roux's avatar
Cedric Roux committed
125 126
}

127 128
event_selector *setup_event_selector(gui *g, void *database, int *is_on,
    void (*change_callback)(void *), void *change_callback_data)
Cedric Roux's avatar
Cedric Roux committed
129 130
{
  struct event_selector *ret;
131
  widget *win;
132
  widget *win_container;
133 134 135 136
  widget *main_container;
  widget *container;
  widget *left, *right;
  widget *events, *groups;
137
  widget *pause_container;
Cedric Roux's avatar
Cedric Roux committed
138 139 140 141 142 143 144 145
  char **ids;
  char **gps;
  int n;
  int i;
  int red, green;

  ret = calloc(1, sizeof(struct event_selector)); if (ret == NULL) abort();

146 147
  red = new_color(g, "#c93535");
  green = new_color(g, "#2f9e2a");
148

Cedric Roux's avatar
Cedric Roux committed
149
  win = new_toplevel_window(g, 470, 300, "event selector");
150 151 152
  win_container = new_container(g, VERTICAL);
  widget_add_child(g, win, win_container, -1);

153
  main_container = new_container(g, VERTICAL);
154 155
  widget_add_child(g, win_container, main_container, -1);
  container_set_child_growable(g, win_container, main_container, 1);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

  container = new_container(g, HORIZONTAL);
  widget_add_child(g, main_container, container, -1);
  container_set_child_growable(g, main_container, container, 1);
  widget_add_child(g, main_container,
      new_label(g, "mouse scroll to scroll - "
                   "left click to activate - "
                   "right click to deactivate"), -1);

  left = new_container(g, VERTICAL);
  right = new_container(g, VERTICAL);
  widget_add_child(g, container, left, -1);
  widget_add_child(g, container, right, -1);
  container_set_child_growable(g, container, left, 1);
  container_set_child_growable(g, container, right, 1);

  widget_add_child(g, left, new_label(g, "Events"), -1);
  widget_add_child(g, right, new_label(g, "Groups"), -1);

175 176
  events = new_textlist(g, 235, 10, new_color(g, "#b3c1e1"));
  groups = new_textlist(g, 235, 10, new_color(g, "#edd6cb"));
177 178 179 180 181

  widget_add_child(g, left, events, -1);
  widget_add_child(g, right, groups, -1);
  container_set_child_growable(g, left, events, 1);
  container_set_child_growable(g, right, groups, 1);
Cedric Roux's avatar
Cedric Roux committed
182

183 184 185 186 187 188
  pause_container = new_positioner(g);
  widget_add_child(g, pause_container,
      new_label(g,
          "events' reception paused - click middle button to resume"), -1);
  label_set_clickable(g, pause_container, 1);

Cedric Roux's avatar
Cedric Roux committed
189 190
  n = database_get_ids(database, &ids);
  for (i = 0; i < n; i++) {
191
    textlist_add(g, events, ids[i], -1,
Cedric Roux's avatar
Cedric Roux committed
192 193 194 195 196 197
        is_on[database_pos_to_id(database, i)] ? green : red);
  }
  free(ids);

  ret->nevents = n;

198 199 200
  ret->is_on_paused = calloc(n, sizeof(int));
  if (ret->is_on_paused == NULL) abort();

Cedric Roux's avatar
Cedric Roux committed
201 202
  n = database_get_groups(database, &gps);
  for (i = 0; i < n; i++) {
203
    textlist_add(g, groups, gps[i], -1, FOREGROUND_COLOR);
Cedric Roux's avatar
Cedric Roux committed
204 205 206 207 208 209 210 211 212 213 214 215
  }
  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;
216 217
  ret->change_callback = change_callback;
  ret->change_callback_data = change_callback_data;
Cedric Roux's avatar
Cedric Roux committed
218

219 220 221 222
  ret->parent_widget = win_container;
  ret->normal_widget = main_container;
  ret->pause_widget = pause_container;

Cedric Roux's avatar
Cedric Roux committed
223 224 225 226 227 228 229 230
  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);

231 232
  register_notifier(g, "click", pause_container, click, ret);

Cedric Roux's avatar
Cedric Roux committed
233
  return ret;
234
}