summaryrefslogtreecommitdiff
path: root/src/widget/table.c
blob: e7d412eb8f2ccf31a7db4fcdd87fecb3d89d4553 (plain)
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
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
#include "widget.h"

#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))

uiTable* uiMakeTable(int rows, int cols, const char** header) {
  uiTable* table = UI_NEW(uiTable);

  *table = (uiTable){
      .widget = (uiWidget){.type = uiTypeTable},
      .rows   = rows,
      .cols   = cols,
      .widths = (cols > 0) ? calloc(cols, sizeof(int)) : 0,
      .header = header ? calloc(cols, sizeof(uiCell)) : 0,
      .cells  = (rows * cols > 0) ? calloc(rows, sizeof(uiCell*)) : 0,
      .flags  = {0},
  };

  if (header) {
    for (int col = 0; col < cols; ++col) {
      table->header[col].text = string_new(header[col]);
    }
  }

  return table;
}

void uiTableClear(uiTable* table) {
  assert(table);

  // Free row data.
  if (table->cells) {
    for (int row = 0; row < table->rows; ++row) {
      for (int col = 0; col < table->cols; ++col) {
        string_del(&table->cells[row][col].text);
      }
      free(table->cells[row]);
    }
    free(table->cells);
    table->cells = 0;
  }
  table->rows = 0;

  // Clear row widths.
  for (int i = 0; i < table->cols; ++i) {
    table->widths[i] = 0;
  }

  table->offset = 0;

  table->flags.vertical_overflow = 0;
}

void uiTableAddRow(uiTable* table, const char** row) {
  assert(table);

  table->rows++;

  uiCell** cells = realloc(table->cells, table->rows * sizeof(uiCell*));
  ASSERT(cells);
  table->cells = cells;

  uiCell** pLastRow = TableGetLastRow(table);
  *pLastRow         = calloc(table->cols, sizeof(uiCell));
  ASSERT(*pLastRow);
  uiCell* lastRow = *pLastRow;

  for (int col = 0; col < table->cols; ++col) {
    lastRow[col].text = string_new(row[col]);
  }
}

void uiTableSet(uiTable* table, int row, int col, const char* text) {
  assert(table);
  assert(text);
  TableGetCellMut(table, row, col)->text = string_new(text);
}

const char* uiTableGet(const uiTable* table, int row, int col) {
  assert(table);
  return string_data(TableGetCell(table, row, col)->text);
}

void uiTableScroll(uiTable* table, int row) {
  assert(table);
  table->offset = Min(table->rows - table->num_visible_rows, Max(0, row));
  SyncScrollbarToTable(table);
}

void SyncScrollbarToTable(uiTable* table) {
  assert(table);
  ScrollbarScroll(
      &table->scrollbar, (int)((double)table->offset / (double)table->rows *
                               (double)table->height));
}

void SyncTableToScrollbar(uiTable* table) {
  assert(table);
  table->offset = (int)((double)table->scrollbar.handle_y /
                        (double)table->height * (double)table->rows);
}

const uiCell* TableGetCell(const uiTable* table, int row, int col) {
  assert(table);
  return &table->cells[row][col];
}

uiCell* TableGetCellMut(uiTable* table, int row, int col) {
  assert(table);
  return (uiCell*)TableGetCell(table, row, col);
}

uiCell** TableGetLastRow(uiTable* table) {
  assert(table);
  assert(table->rows > 0);
  return &table->cells[table->rows - 1];
}