label.c 2.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#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 *_w)
{
  struct gui *g = _gui;
  struct label_widget *l = _w;
Cedric Roux's avatar
Cedric Roux committed
12
  LOGD("PAINT label '%s'\n", l->t);
Cedric Roux's avatar
Cedric Roux committed
13
  x_draw_string(g->x, g->xwin, DEFAULT_FONT, l->color,
14 15 16 17 18 19
      l->common.x, l->common.y + l->baseline, l->t);
}

static void hints(gui *_gui, widget *_w, int *width, int *height)
{
  struct label_widget *l = _w;
Cedric Roux's avatar
Cedric Roux committed
20
  LOGD("HINTS label '%s'\n", l->t);
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  *width = l->width;
  *height = l->height;
}

widget *new_label(gui *_gui, const char *label)
{
  struct gui *g = _gui;
  struct label_widget *w;

  glock(g);

  w = new_widget(g, LABEL, sizeof(struct label_widget));

  w->t = strdup(label);
  if (w->t == NULL) OOM;
  w->color = FOREGROUND_COLOR;

Cedric Roux's avatar
Cedric Roux committed
38 39
  x_text_get_dimensions(g->x, DEFAULT_FONT, label,
      &w->width, &w->height, &w->baseline);
40 41 42 43 44 45 46 47

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

  gunlock(g);

  return w;
}
48

49 50
static void button(gui *gui, widget *_this, int x, int y,
    int key_modifiers, int button, int up)
51 52 53 54 55 56 57 58 59
{
  LOGD("BUTTON label %p xy %d %d button %d up %d\n", _this, x, y, button, up);

  if (up != 0) return;

  gui_notify(gui, "click", _this, &button);
}

/* we could use default_button, but it's in widget.c, so, well... */
60 61
static void no_button(gui *gui, widget *_this, int x, int y,
    int key_modifiers, int button, int up)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
  /* do nothing */
}

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

void label_set_clickable(gui *_g, widget *_this, int clickable)
{
  struct gui *g = _g;
  struct label_widget *this = _this;

  glock(g);

  if (clickable)
    this->common.button = button;
  else
    this->common.button = no_button;

  gunlock(g);
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

void label_set_text(gui *_g, widget *_this, char *text)
{
  struct gui *g = _g;
  struct label_widget *this = _this;

  glock(g);

  free(this->t);
  this->t = strdup(text); if (this->t == NULL) OOM;

  x_text_get_dimensions(g->x, DEFAULT_FONT, text,
      &this->width, &this->height, &this->baseline);

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

  gunlock(g);
}