text_list.c 6.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void paint(gui *_gui, widget *_this)
{
  struct gui *g = _gui;
  struct text_list_widget *this = _this;
  int i, j;
Cedric Roux's avatar
Cedric Roux committed
13
  LOGD("PAINT text_list %p xywh %d %d %d %d starting line %d allocated nlines %d text_count %d\n", _this, this->common.x, this->common.y, this->common.width, this->common.height, this->starting_line, this->allocated_nlines, this->text_count);
14 15 16 17 18
  x_fill_rectangle(g->x, g->xwin, this->background_color,
      this->common.x, this->common.y,
      this->common.width, this->common.height);
  for (i = 0, j = this->starting_line;
       i < this->allocated_nlines && j < this->text_count; i++, j++)
Cedric Roux's avatar
Cedric Roux committed
19
    x_draw_clipped_string(g->x, g->xwin, this->color[j],
20 21
        this->common.x,
        this->common.y + i * this->line_height + this->baseline,
22 23 24
        this->text[j],
        this->common.x, this->common.y,
        this->common.width, this->common.height);
25 26 27 28 29 30 31
}

static void hints(gui *_gui, widget *_w, int *width, int *height)
{
  struct text_list_widget *w = _w;
  *width = w->wanted_width;
  *height = w->wanted_nlines * w->line_height;
Cedric Roux's avatar
Cedric Roux committed
32
  LOGD("HINTS text_list wh %d %d\n", *width, *height);
33 34 35 36 37 38 39 40 41 42 43
}

static void allocate(
    gui *gui, widget *_this, int x, int y, int width, int height)
{
  struct text_list_widget *this = _this;
  this->common.x = x;
  this->common.y = y;
  this->common.width = width;
  this->common.height = height;
  this->allocated_nlines = height / this->line_height;
Cedric Roux's avatar
Cedric Roux committed
44
  LOGD("ALLOCATE text_list %p xywh %d %d %d %d nlines %d\n", this, x, y, width, height, this->allocated_nlines);
45 46
}

Cedric Roux's avatar
Cedric Roux committed
47
static void button(gui *_g, widget *_this, int x, int y, int button, int up)
Cedric Roux's avatar
Cedric Roux committed
48
{
Cedric Roux's avatar
Cedric Roux committed
49
  struct gui *g = _g;
Cedric Roux's avatar
Cedric Roux committed
50
  struct text_list_widget *this = _this;
Cedric Roux's avatar
Cedric Roux committed
51
  LOGD("BUTTON test_list %p xy %d %d button %d up %d\n", _this, x, y, button, up);
Cedric Roux's avatar
Cedric Roux committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65
  /* scroll up */
  if (button == 4 && up == 0) {
    gui_notify(g, "scrollup", _this, NULL);
  }
  /* scroll down */
  if (button == 5 && up == 0) {
    gui_notify(g, "scrolldown", _this, NULL);
  }
  /* button 1/2/3 click */
  if (button >= 1 && button <= 3 && up == 0) {
    int line = this->starting_line + y / this->line_height;
    if (line >= 0 && line < this->text_count)
      gui_notify(g, "click", _this, (int[2]){ line, button });
  }
Cedric Roux's avatar
Cedric Roux committed
66 67
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
widget *new_text_list(gui *_gui, int width, int nlines, int bgcol)
{
  struct gui *g = _gui;
  struct text_list_widget *w;
  int dummy;

  glock(g);

  w = new_widget(g, TEXT_LIST, sizeof(struct text_list_widget));

  w->wanted_nlines = nlines;
  x_text_get_dimensions(g->x, ".", &dummy, &w->line_height, &w->baseline);
  w->background_color = bgcol;
  w->wanted_width = width;

  w->common.paint = paint;
  w->common.hints = hints;
  w->common.allocate = allocate;

Cedric Roux's avatar
Cedric Roux committed
87 88
  w->common.button = button;

89 90 91 92 93 94 95 96 97
  gunlock(g);

  return w;
}

/*************************************************************************/
/*                             public functions                          */
/*************************************************************************/

Cedric Roux's avatar
Cedric Roux committed
98 99
static void _text_list_add(gui *_gui, widget *_this, const char *text,
    int position, int color, int silent)
100 101 102 103 104 105 106 107 108 109 110 111
{
  struct gui *g = _gui;
  struct text_list_widget *this = _this;

  glock(g);

  if (position < 0) position = this->text_count;
  if (position > this->text_count) position = this->text_count;

  this->text_count++;
  this->text = realloc(this->text, this->text_count * sizeof(char *));
  if (this->text == NULL) OOM;
Cedric Roux's avatar
Cedric Roux committed
112 113
  this->color = realloc(this->color, this->text_count * sizeof(int));
  if (this->color == NULL) OOM;
114 115 116

  memmove(this->text + position + 1, this->text + position,
          (this->text_count-1 - position) * sizeof(char *));
Cedric Roux's avatar
Cedric Roux committed
117 118
  memmove(this->color + position + 1, this->color + position,
          (this->text_count-1 - position) * sizeof(int));
119 120

  this->text[position] = strdup(text); if (this->text[position] == NULL) OOM;
Cedric Roux's avatar
Cedric Roux committed
121
  this->color[position] = color;
122

Cedric Roux's avatar
Cedric Roux committed
123
  if (!silent) send_event(g, DIRTY, this->common.id);
124 125 126

  gunlock(g);
}
Cedric Roux's avatar
Cedric Roux committed
127

Cedric Roux's avatar
Cedric Roux committed
128
static void _text_list_del(gui *_gui, widget *_this, int position, int silent)
Cedric Roux's avatar
Cedric Roux committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
{
  struct gui *g = _gui;
  struct text_list_widget *this = _this;

  glock(g);

  /* TODO: useful check? */
  if (this->text_count == 0) goto done;

  if (position < 0) position = this->text_count;
  if (position > this->text_count-1) position = this->text_count-1;

  free(this->text[position]);

  memmove(this->text + position, this->text + position + 1,
          (this->text_count-1 - position) * sizeof(char *));
Cedric Roux's avatar
Cedric Roux committed
145 146
  memmove(this->color + position, this->color + position + 1,
          (this->text_count-1 - position) * sizeof(int));
Cedric Roux's avatar
Cedric Roux committed
147 148 149 150

  this->text_count--;
  this->text = realloc(this->text, this->text_count * sizeof(char *));
  if (this->text == NULL) OOM;
Cedric Roux's avatar
Cedric Roux committed
151 152
  this->color = realloc(this->color, this->text_count * sizeof(int));
  if (this->color == NULL) OOM;
Cedric Roux's avatar
Cedric Roux committed
153

Cedric Roux's avatar
Cedric Roux committed
154
  if (!silent) send_event(g, DIRTY, this->common.id);
Cedric Roux's avatar
Cedric Roux committed
155 156 157 158

done:
  gunlock(g);
}
Cedric Roux's avatar
Cedric Roux committed
159

Cedric Roux's avatar
Cedric Roux committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
void text_list_add(gui *gui, widget *this, const char *text, int position,
    int color)
{
  _text_list_add(gui, this, text, position, color, 0);
}

void text_list_del(gui *gui, widget *this, int position)
{
  _text_list_del(gui, this, position, 0);
}

void text_list_add_silent(gui *gui, widget *this, const char *text,
    int position, int color)
{
  _text_list_add(gui, this, text, position, color, 1);
}

void text_list_del_silent(gui *gui, widget *this, int position)
{
  _text_list_del(gui, this, position, 1);
}

Cedric Roux's avatar
Cedric Roux committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
void text_list_state(gui *_gui, widget *_this,
    int *visible_lines, int *start_line, int *number_of_lines)
{
  struct gui *g = _gui;
  struct text_list_widget *this = _this;

  glock(g);

  *visible_lines   = this->allocated_nlines;
  *start_line      = this->starting_line;
  *number_of_lines = this->text_count;

  gunlock(g);
}

void text_list_set_start_line(gui *_gui, widget *_this, int line)
{
  struct gui *g = _gui;
  struct text_list_widget *this = _this;

  glock(g);

  this->starting_line = line;

  send_event(g, DIRTY, this->common.id);

  gunlock(g);
}

void text_list_get_line(gui *_gui, widget *_this, int line,
    char **text, int *color)
{
  struct gui *g = _gui;
  struct text_list_widget *this = _this;

  glock(g);

  if (line < 0 || line >= this->text_count) {
    *text = NULL;
    *color = -1;
  } else {
    *text = this->text[line];
    *color = this->color[line];
  }

  gunlock(g);
}

void text_list_set_color(gui *_gui, widget *_this, int line, int color)
{
  struct gui *g = _gui;
  struct text_list_widget *this = _this;

  glock(g);

  if (line >= 0 && line < this->text_count) {
    this->color[line] = color;

    send_event(g, DIRTY, this->common.id);
  }

  gunlock(g);
}