widget.c 7.34 KB
Newer Older
1 2 3 4 5 6 7 8
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

9
static void default_clear(gui *gui, widget *_this);
10 11 12 13 14
static void default_repack(gui *gui, widget *_this);
static void default_allocate(
    gui *gui, widget *_this, int x, int y, int width, int height);
static void default_add_child(
    gui *_gui, widget *_this, widget *child, int position);
Cedric Roux's avatar
Cedric Roux committed
15
static void default_del_child(gui *_gui, widget *_this, widget *child);
16
static void default_hints(gui *g, widget *this, int *width, int *height);
17 18
static void default_button(gui *gui, widget *_this, int x, int y,
    int key_modifiers, int button, int up);
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

static void toplevel_list_append(struct gui *g, struct widget *e)
{
  struct widget_list *new;

  new = calloc(1, sizeof(struct widget_list));
  if (new == NULL) OOM;

  new->item = e;

  if (g->toplevel == NULL) {
    g->toplevel = new;
    new->last = new;
    return;
  }

  g->toplevel->last->next = new;
  g->toplevel->last = new;
}

widget *new_widget(struct gui *g, enum widget_type type, int size)
{
  struct widget *ret;

  //glock(g);

  ret = calloc(1, size);
  if (ret == NULL) OOM;

48
  ret->clear     = default_clear;
49 50
  ret->repack    = default_repack;
  ret->add_child = default_add_child;
Cedric Roux's avatar
Cedric Roux committed
51
  ret->del_child = default_del_child;
52 53
  ret->allocate  = default_allocate;
  ret->hints     = default_hints;
Cedric Roux's avatar
Cedric Roux committed
54
  ret->button    = default_button;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  /* there is no default paint, on purpose */

  ret->type      = type;
  ret->id        = g->next_id;
  g->next_id++;
  ret->width     = 0;
  ret->height    = 0;

  /* add toplevel windows to g->toplevel */
  if (type == TOPLEVEL_WINDOW)
    toplevel_list_append(g, ret);

  //gunlock(g);

  return ret;
}

/*************************************************************************/
/*                          internal functions                           */
/*************************************************************************/

void widget_add_child_internal(
    gui *_gui, widget *parent, widget *child, int position)
{
  struct widget *p = parent;
  struct widget *c = child;
  struct widget_list *new;
  struct widget_list *prev, *cur;
  int i;

  new = calloc(1, sizeof(struct widget_list));
  if (new == NULL) OOM;

  new->item = c;
  c->parent = p;

  prev = NULL;
  cur = p->children;

  for (i = 0; position < 0 || i < position; i++) {
    if (cur == NULL) break;
    prev = cur;
    cur = cur->next;
  }

  /* TODO: warn/err if i != position+1? */

  if (prev == NULL) {
    /* new is at head */
    new->next = p->children;
    if (p->children != NULL) new->last = p->children->last;
    else                     new->last = new;
    p->children = new;
    goto repack;
  }

  if (cur == NULL) {
    /* new is at tail */
    prev->next = new;
    p->children->last = new;
    goto repack;
  }

  /* new is between two existing items */
  prev->next = new;
  new->next = cur;

repack:
  send_event(_gui, REPACK, p->id);
}

Cedric Roux's avatar
Cedric Roux committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
void widget_del_child_internal(gui *_gui, widget *parent, widget *child)
{
  struct widget *p = parent;
  struct widget *c = child;
  struct widget_list *prev, *cur;

  c->parent = NULL;

  prev = NULL;
  cur = p->children;

  while (cur != NULL && cur->item != c) {
    prev = cur;
    cur = cur->next;
  }

  if (cur == NULL) ERR("child not found\n");

  if (prev == NULL) {
    /* child is at head */
    p->children = cur->next;
    if (p->children != NULL) p->children->last = cur->last;
    goto done;
  }

  if (cur->next == NULL) {
    /* child is last (and prev is != NULL) */
    prev->next = NULL;
    p->children->last = prev;
    goto done;
  }

  /* child is between two existing items */
  prev->next = cur->next;

done:
  free(cur);
  send_event(_gui, REPACK, p->id);
}

int widget_get_child_position(gui *_gui, widget *parent, widget *child)
{
  struct widget *p = parent;
  struct widget *c = child;
  struct widget_list *cur;
  int i = 0;

  cur = p->children;

  while (cur != NULL && cur->item != c) {
    cur = cur->next;
    i++;
  }

  if (cur == NULL) return -1;
  return i;
}

184 185 186 187
/*************************************************************************/
/*                           default functions                           */
/*************************************************************************/

188 189 190 191 192 193 194 195
static void default_clear(gui *_gui, widget *_this)
{
  struct gui *g = _gui;
  struct widget *this = _this;
  x_fill_rectangle(g->x, g->xwin, BACKGROUND_COLOR,
      this->x, this->y, this->width, this->height);
}

196 197 198 199 200 201 202
static void default_repack(gui *gui, widget *_this)
{
  struct widget *this = _this;
  return this->parent->repack(gui, this->parent);
}

static void default_add_child(
Cedric Roux's avatar
Cedric Roux committed
203
    gui *_gui, widget *_this, widget *child, int position)
204 205 206 207 208
{
  struct widget *this = _this;
  WARN("cannot add child to widget %s\n", widget_name(this->type));
}

Cedric Roux's avatar
Cedric Roux committed
209 210 211 212 213 214
static void default_del_child( gui *_gui, widget *_this, widget *child)
{
  struct widget *this = _this;
  WARN("cannot del child from widget %s\n", widget_name(this->type));
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
static void default_allocate(
    gui *gui, widget *_this, int x, int y, int width, int height)
{
  struct widget *this = _this;
  this->x = x;
  this->y = y;
  this->width = width;
  this->height = height;
}

static void default_hints(gui *g, widget *this, int *width, int *height)
{
  *width = 1;
  *height = 1;
}

231 232
static void default_button(gui *gui, widget *_this, int x, int y,
    int key_modifiers, int button, int up)
Cedric Roux's avatar
Cedric Roux committed
233 234 235 236
{
  /* nothing */
}

237 238 239 240 241 242 243 244 245 246 247 248
/*************************************************************************/
/*                             utils functions                           */
/*************************************************************************/

void widget_add_child(gui *_gui, widget *parent, widget *child, int position)
{
  struct widget *this = parent;
  glock(_gui);
  this->add_child(_gui, parent, child, position);
  gunlock(_gui);
}

Cedric Roux's avatar
Cedric Roux committed
249 250 251 252 253 254 255 256
void widget_del_child(gui *_gui, widget *parent, widget *child)
{
  struct widget *this = parent;
  glock(_gui);
  this->del_child(_gui, parent, child);
  gunlock(_gui);
}

257 258 259 260 261 262 263 264 265
void widget_dirty(gui *_gui, widget *_this)
{
  struct gui *g = _gui;
  struct widget *this = _this;
  glock(g);
  send_event(g, DIRTY, this->id);
  gunlock(g);
}

266
static const char *names[] = {
267
  "TOPLEVEL_WINDOW", "CONTAINER", "POSITIONER", "TEXT_LIST",
Cedric Roux's avatar
Cedric Roux committed
268
  "XY_PLOT", "BUTTON", "LABEL", "TIMELINE", "SPACE", "IMAGE"
269 270 271 272 273 274
};
const char *widget_name(enum widget_type type)
{
  switch (type) {
  case TOPLEVEL_WINDOW:
  case CONTAINER:
275
  case POSITIONER:
276 277 278 279
  case TEXT_LIST:
  case XY_PLOT:
  case BUTTON:
  case LABEL:
Cedric Roux's avatar
Cedric Roux committed
280
  case TIMELINE:
281
  case SPACE:
Cedric Roux's avatar
Cedric Roux committed
282
  case IMAGE:
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    return names[type];
  }
  return "UNKNOWN (error)";
}

/*************************************************************************/
/*                             find a widget                             */
/*************************************************************************/

/* TODO: optimize traversal and also use a cache */
struct widget *_find_widget(struct widget *c, int id)
{
  struct widget_list *l;
  struct widget *ret;
  if (c == NULL) return NULL;
  if (c->id == id) return c;
  l = c->children;
  while (l) {
    ret = _find_widget(l->item, id);
    if (ret != NULL) return ret;
    l = l->next;
  }
  return NULL;
}

struct widget *find_widget(struct gui *g, int id)
{
  struct widget_list *l;
  struct widget *ret;

  l = g->toplevel;

  while (l) {
    ret = _find_widget(l->item, id);
    if (ret != NULL) return ret;
    l = l->next;
  }

  return NULL;
}