aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/base.c
blob: 8e7a7ea42929941c6d210e844f9ebace35bf131a (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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "ui.internal.h"
#include "ui/uimenu.internal.h"
#include "../macros.h"
#include "config.h"

struct ui_window_root *ui_root = NULL;

void ui__default_draw_proc(struct ui_window_base *base)
{
  umps_unused(base);
}

void ui__leaf_draw_proc(struct ui_window_base *base)
{
  WINDOW *mywin = ((struct ui_window_leaf *)base)->cwindow;
  int maxy, maxx;
  getmaxyx(mywin, maxy, maxx);

  box(mywin, 0, 0);
  mvwaddstr(mywin, 0, 2, "Traces");
  for (int i = 1; i < maxy-1; ++i) {
    mvwhline(mywin, i, 1, '.', maxx - 2);
  }

  wnoutrefresh(mywin);
}

void ui__leaf_layout_proc(struct ui_window_base *base)
{
  struct ui_window_leaf *leaf = (struct ui_window_leaf *)base;
  if (leaf->cwindow) NCCI(delwin(leaf->cwindow));
  leaf->cwindow = NCCP(newwin(leaf->super.dims.maxy, leaf->super.dims.maxx, leaf->super.dims.begy, leaf->super.dims.begx));
}

void ui__init_window_base(struct ui_window_base *base)
{
  base->type = UI__WINDOW_TYPE_BASE;
  base->parent = NULL;

  base->dims.begy = 0;
  base->dims.begx = 0;
  base->dims.maxy = 0;
  base->dims.maxx = 0;

  base->draw_proc = &ui__default_draw_proc;
  base->layout_proc = NULL;
  base->control_proc = NULL;
}

void ui__init_window_leaf(struct ui_window_leaf *leaf)
{
  ui__init_window_base(&leaf->super);
  leaf->super.type = UI__WINDOW_TYPE_LEAF;
  leaf->super.draw_proc = &ui__leaf_draw_proc;
  leaf->super.layout_proc = &ui__leaf_layout_proc;

  leaf->cwindow = NULL;
}

void ui__init_window_dock(struct ui_window_dock *dock)
{
  ui__init_window_base(&dock->super);
  dock->super.type = UI__WINDOW_TYPE_DOCK;
  dock->super.draw_proc = &ui__dock_default_draw_proc;
  dock->super.layout_proc = &ui__dock_default_layout_proc;
  dock->super.control_proc = &ui__dock_default_control_proc;

  for (unsigned i = 0; i < UI__WINDOW_DOCK_MAX; ++i)
  {
    dock->children[i] = NULL;
    dock->childsizes[i] = 0.0;
  }

  dock->focus = UI__WINDOW_FOCUS_NONE;
}

void ui__init_window_root(struct ui_window_root *root, WINDOW *cwindow)
{
  ui__init_window_base(&root->super);
  root->super.type = UI__WINDOW_TYPE_ROOT;
  root->super.draw_proc = &ui__root_draw_proc;
  root->super.layout_proc = &ui__root_layout_proc;
  root->super.control_proc = &ui__root_control_proc;

  root->cwindow = cwindow;
  getmaxyx(cwindow, root->super.dims.maxy, root->super.dims.maxx);
  getbegyx(cwindow, root->super.dims.begy, root->super.dims.begx);

  root->menu_prefix = "";
  root->menu_prefix_len = 0;

  root->menu_cwindow = newwin(1, root->super.dims.maxx, 0, 0);
  root->menu_selected = NULL;
  root->menu_scroll = 0;

  root->undersize_scr = false;
  root->content = NULL;
  root->floating = NULL;
  root->modal = NULL;

  root->menu_root = malloc(sizeof(struct uimenu_item_menu));
  uimenu_item_menu_init(root->menu_root, NULL);
}

/* type-specific destructors */
void ui__destroy_window_base(struct ui_window_base *base);
void ui__destroy_window_leaf(struct ui_window_leaf *leaf);
void ui__destroy_window_dock(struct ui_window_dock *dock);
void ui__destroy_window_menu(struct ui_window_menu *menu);

void ui__window_destroy_root(struct ui_window_root *root)
{
  if (root->content)
    ui__destroy_window(root->content);

  if (root->floating)
    ui__destroy_window(root->floating);

  uimenu_menu_free(root->menu_root);

  if (root->cwindow) delwin(root->cwindow);

  ui__destroy_window_base(&root->super);
}

void ui__destroy_window_dock(struct ui_window_dock *dock)
{
  for (unsigned i = 0; i < UI__WINDOW_DOCK_MAX; ++i)
  {
    if (dock->children[i]) ui__destroy_window(dock->children[i]);
    dock->children[i] = NULL;
  }

  ui__destroy_window_base(&dock->super);
}

void ui__destroy_window_leaf(struct ui_window_leaf *leaf)
{
  if (leaf->cwindow) delwin(leaf->cwindow);
  ui__destroy_window_base(&leaf->super);
}

void ui__destroy_window_base(struct ui_window_base *base)
{
  free(base);
}

void ui__destroy_window(struct ui_window_base *base)
{
  if (!base) return;

  switch (base->type)
  {
    case UI__WINDOW_TYPE_ROOT:
      ui__window_destroy_root(ui__cast(root, base));
      break;
    case UI__WINDOW_TYPE_DOCK:
      ui__destroy_window_dock(ui__cast(dock, base));
      break;
    case UI__WINDOW_TYPE_LEAF:
      ui__destroy_window_leaf(ui__cast(leaf, base));
      break;
    case UI__WINDOW_TYPE_BASE:
      ui__destroy_window_base(base);
      break;
    case UI__WINDOW_TYPE_MENU:
      ui__destroy_window_menu(ui__cast(menu, base));
  }
}

/* the focused window MUST be a leaf window, or the screen will get clobbered.
 * This is because the focused window will have wgetch() called for it, which
 * calls wrefresh() sometimes. This will clobber windows inside of it. */
struct ui_window_base *ui__find_focused(void)
{
  struct ui_window_base *window = ui__cast(base, ui_root);

  while (true) {
    switch (window->type) {
      case UI__WINDOW_TYPE_ROOT:
        {
          struct ui_window_root *root = ui__cast(root, window);
          if (root->undersize_scr) {
            return window; /* gobble up focus */
          } else if (root->floating) {
            window = root->floating;
          } else if (root->content) {
            window = root->content;
          } else {
            return window;
          }
          break;
        }
      case UI__WINDOW_TYPE_DOCK:
        {
          struct ui_window_dock *dock = ui__cast(dock, window);

          if (dock->focus == UI__WINDOW_FOCUS_NONE || !dock->children[dock->focus])
          {
            for (unsigned i = 0; i < UI__WINDOW_DOCK_MAX; ++i)
            {
              if (dock->children[i])
              {
                dock->focus = i; /* focus the child (skip fallback return statement) */
                goto childfound;
              }
            }

            return window; /* the dock is focused if it is really a leaf */
          }

childfound:
          window = dock->children[dock->focus];
          break;
        }
      default:
        return window; /* a leaf window is focused */
    }
  }
}

WINDOW *ui__find_focused_leaf(struct ui_window_base *start)
{
  switch (start->type) {
    case UI__WINDOW_TYPE_LEAF:
    case UI__WINDOW_TYPE_MENU:
      return ui__cast(leaf, start)->cwindow;
    case UI__WINDOW_TYPE_DOCK: {
      struct ui_window_dock *dock = ui__cast(dock, start);

      for (unsigned i = 0; i < UI__WINDOW_DOCK_MAX; ++i) {
        if (dock->children[i]) {
          WINDOW *focus = ui__find_focused_leaf(dock->children[i]);
          if (focus) return focus;
        }
      }

      return NULL;
    }
    case UI__WINDOW_TYPE_ROOT: {
      struct ui_window_root *root = ui__cast(root, start);
      WINDOW *focus;

      if (root->undersize_scr)
        return root->cwindow; /* gobble up focus if the screen is undersize */

      if (root->modal) {
        focus = ui__find_focused_leaf(root->modal);
        if (focus) return focus;
      }

      if (root->floating) {
        focus = ui__find_focused_leaf(root->modal);
        if (focus) return focus;
      }

      if (root->menu_selected) {
        return root->menu_cwindow;
      }

      if (root->content) {
        return ui__find_focused_leaf(root->content);
      }

      return NULL;
    }
    default:
      umps_trap;
  }
}

void ui__call_draw_proc(struct ui_window_base *base)
{
  if (base->draw_proc)
  {
    (*base->draw_proc)(base);
  }
}

void ui__call_layout_proc(struct ui_window_base *base)
{
  if (base->layout_proc)
  {
    (*base->layout_proc)(base);
  }
}

struct ui_window_base *ui__call_control_proc(struct ui_window_base *base, ui_control inp)
{
  if (base->control_proc)
  {
    return (*base->control_proc)(base, inp);
  }

  return NULL;
}

void ui_init(void)
{
  ui_root = malloc(sizeof(struct ui_window_root)); /* TODO: check */
  assert(ui_root);

  initscr();

  raw();
  noecho();
  keypad(stdscr, TRUE);

  curs_set(0);

  ui__init_window_root(ui_root, stdscr);
  ui__call_layout_proc(ui__cast(base, ui_root));

  /* set up UI */

  ui_root->menu_prefix = " " UMPS_NAME " " UMPS_VERSION " ";
  ui_root->menu_prefix_len = strlen(ui_root->menu_prefix);

  struct ui_window_dock *maindock = malloc(sizeof(struct ui_window_dock));
  ui__init_window_dock(maindock);
  ui__root_set_content(ui_root, ui__cast(base, maindock));

  const char *menu_names[] = { "File", "View", "Tools", "Filters", "Help", "1", "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999", "0000000000", NULL };
  for (int i = 0; menu_names[i]; ++i) {
    struct uimenu_item_menu *menu = malloc(sizeof(struct uimenu_item_menu));
    uimenu_item_menu_init(menu, menu_names[i]);
    uimenu_menu_add(ui_root->menu_root, ui_root->menu_root->tail, (struct uimenu_item_header *)menu, true);
  }

  for (unsigned i = 0; i < UI__WINDOW_DOCK_MAX; ++i)
  {
    if (i == UI__WINDOW_DOCK_LEFT) continue;
    struct ui_window_leaf *win_traces = malloc(sizeof(struct ui_window_leaf));
    ui__init_window_leaf(win_traces);
    ui__dock_add_child(maindock, ui__cast(base, win_traces), i, 1./5);
  }

  ui__call_layout_proc(ui__cast(base, ui_root)); /* should call layout proc to check for undersize at least */
  ui__call_draw_proc(ui__cast(base, ui_root));
}

void ui_handle(void)
{
  while (true)
  {
    WINDOW *cwindow = ui__find_focused_leaf(ui__cast(base, ui_root));

    ui_control inp;

#ifdef NCURSES_WIDE
    keypad(cwindow, TRUE);
    wget_wch(cwindow, &inp);
#else
    inp = wgetch(window->cwindow);
#endif

    if (inp == NS('q')) /* TODO: make quitting flag in ui_root */
    {
      break;
    }
    else if (inp == KEY_RESIZE)
    {
      getmaxyx(ui_root->cwindow, ui_root->super.dims.maxy, ui_root->super.dims.maxx);
      getbegyx(ui_root->cwindow, ui_root->super.dims.begy, ui_root->super.dims.begx);
      ui__call_layout_proc(ui__cast(base, ui_root));
      ui__call_draw_proc(ui__cast(base, ui_root));
    }

    struct ui_window_base *target = ui__cast(base, ui_root);
    while (target) {
      target = ui__call_control_proc(target, inp);
    }
  }

  ui__window_destroy_root(ui_root);
  ui_root = NULL;

  endwin();
}