summaryrefslogtreecommitdiff
path: root/src/user/raleigh/w/button.cpp
blob: da88f6a1bd2f1e8fc53ae7a20dbd5d0e64b2de17 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <raleigh/w/button.h>

namespace raleigh {
  button::button(widget &inner, void (*on_click)(button &),
    _pixel_t border_color, _pixel_t bg_color, _pixel_t pressed_color)
    : inner(inner), on_click(on_click), border_color(border_color),
      bg_color(bg_color), pressed_color(pressed_color), is_pressed(false) {
    size = coord(inner.size.x + 2, inner.size.y + 2);
    closest_opaque = this;
    inner.notify_has_opaque_parent(this);
    inner.parent = this;
  }

  void button::notify_window_change() {
    inner.window_offset = coord(window_offset.x + 1, window_offset.y + 1);
    inner.w = w;
    inner.notify_window_change();
  }

  void button::paint(_pixel_t *pixbuf, uint32_t pitch) {
    if (next_paint_full) {
      next_paint_full = false;
      for (uint32_t x = window_offset.x; x < window_offset.x + size.x; ++x) {
        pixbuf[window_offset.y * pitch + x] = border_color;
        pixbuf[(window_offset.y + size.y - 1) * pitch + x] = border_color;
      }
      for (uint32_t y = window_offset.y + 1; y < window_offset.y + size.y - 1; ++y) {
        pixbuf[y * pitch + window_offset.x] = border_color;
        pixbuf[y * pitch + window_offset.x + size.x - 1] = border_color;
      }
    }
    for (uint32_t y = window_offset.y + 1; y < window_offset.y + size.y - 1; ++y)
      for (uint32_t x = window_offset.x + 1; x < window_offset.x + size.x - 1; ++x)
        pixbuf[y * pitch + x] = is_pressed ? pressed_color : bg_color;
    inner.next_paint_full = true;
    inner.paint(pixbuf, pitch);
  }

  void button::handle_click(coord window_coords, enum mouse_packet::mouse_button click_type, bool up) {
    if (click_type != mouse_packet::LEFT)
      return;
    if (up) {
      is_pressed = false;
      inner.window_offset = coord(window_offset.x + 1, window_offset.y + 1);
      inner.notify_window_change();
      w->notify_needs_paint(*this);
      on_click(*this);
    }
    else {
      is_pressed = true;
      inner.window_offset = coord(window_offset.x + 2, window_offset.y + 2);
      inner.notify_window_change();
      w->notify_needs_paint(*this);
    }
  }

  void button::notify_child_size_change(widget &child, coord old_size) {
    set_size(coord(inner.size.x + 2, inner.size.y + 2));
  }

  void button::notify_has_opaque_parent(widget *parent) {}

  void button::on_mouse_move(coord window_coords) {
    if ((window_coords.x >= inner.window_offset.x) &&
        (window_coords.y >= inner.window_offset.y) &&
        (window_coords.x < inner.window_offset.x + inner.size.x) &&
        (window_coords.y < inner.window_offset.y + inner.size.y))
      inner.on_mouse_move(window_coords);
  }
}