summaryrefslogtreecommitdiff
path: root/src/user/raleigh/w/label.cpp
blob: 21df2a2b1396a0d9d0501f4b7043f1622c934609 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
#include <raleigh/w/label.h>
#include <libfont/fonts.h>
#include <knob/block.h>

namespace raleigh {
  label::label(const char *value, const char *font, bool bg_transparent, _pixel_t fg, _pixel_t bg)
    : fi(get_font(font)), bg_transparent(bg_transparent), fg(fg), bg(bg) {

    v_size = strlen(value) + 1;
    this->value = new char[v_size];
    blockcpy(this->value, value, v_size);
    size = coord(fi->space_width * (v_size - 2) + fi->char_width, fi->char_height);

    closest_opaque = 0;
  }

  void label::change_value(const char *new_value) {
    delete[] value;
    const uint32_t ns = strlen(new_value) + 1;
    if (ns != v_size) {
      v_size = ns;
      value = new char[ns];
      set_size(coord(fi->space_width * (ns - 2) + fi->char_width, fi->char_height));
    }
    blockcpy(value, new_value, ns);
    w->notify_needs_paint(*this);
  }

  void label::paint(_pixel_t *pixbuf, uint32_t pitch) {
    if (!bg_transparent)
      for (uint32_t y = window_offset.y; y < window_offset.y + size.y; ++y)
        for (uint32_t x = window_offset.x; x < window_offset.x + size.x; ++x)
          pixbuf[y * pitch + x] = bg;
    _pixel_t *ptr = pixbuf + window_offset.y * pitch + window_offset.x;
    for (const char *c = value; *c; ++c) {
      put_char_no_bg(fi, *c, ptr, pitch, fg);
      ptr += fi->space_width;
    }
  }

  void label::notify_has_opaque_parent(widget *parent) {
    closest_opaque = parent;
  }
}