aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/root.c
blob: 3877359c439167bb1c8ce500625f285f7c92b3ac (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
#include "ui.internal.h"
#include "macros.h"
#include "ui/uimenu.internal.h"
#include <curses.h>

#define UI__ROOT_MIN_Y (24)
#define UI__ROOT_MIN_X (80)

/* top margin for menu bar */
#define UI__ROOT_MARGIN_TOP    (1)

/* bottom margin for status bar */
#define UI__ROOT_MARGIN_BOTTOM (1)

#define UI__ROOT_MARGIN_LEFT   (0)
#define UI__ROOT_MARGIN_RIGHT  (0)

const char *ui__status_text = "Ready";

WINDOW *ui__root_place_content_window(struct ui_window_root *root)
{
  int maxy, maxx;
  int begy, begx;
  getmaxyx(root->super.cwindow, maxy, maxx);
  getbegyx(root->super.cwindow, begy, begx);

  begy += UI__ROOT_MARGIN_TOP;
  maxy -= UI__ROOT_MARGIN_TOP + UI__ROOT_MARGIN_BOTTOM;

  begx += UI__ROOT_MARGIN_LEFT;
  begx -= UI__ROOT_MARGIN_LEFT + UI__ROOT_MARGIN_RIGHT;

  return newwin(maxy, maxx, begy, begx);
}

void ui__root_draw_menu(struct ui_window_root *root);
void ui__root_draw_status(struct ui_window_root *root);

void ui__root_draw_proc(struct ui_window_base *base)
{
  struct ui_window_root *root = ui__cast(root, base);

  int maxy, maxx;
  getmaxyx(base->cwindow, maxy, maxx);
  if (root->undersize_scr) {
    for (int y = 0; y < maxy; ++y)
      mvwhline(base->cwindow, y, 0, y < 3 ? ' ' : '/', maxx);

    mvwprintw(base->cwindow, 0, 0, "Your terminal is too small! It must be at least %dx%d.", UI__ROOT_MIN_X, UI__ROOT_MIN_Y);

    wrefresh(base->cwindow);
    return;
  }

  ui__root_draw_menu(root);
  ui__root_draw_status(root);

  wnoutrefresh(base->cwindow);

  if (root->content) {
    ui__call_draw_proc(root->content);
  }

  if (root->floating) {
    ui__call_draw_proc(root->floating);
  }

  doupdate();
}

void ui__root_draw_menu(struct ui_window_root *root)
{
  WINDOW *mywin = root->super.cwindow;
  attron(A_REVERSE);
  mvwhline(mywin, 0, 0, ' ', getmaxx(mywin));
  mvwaddstr(mywin, 0, 0, " UMPS v0.1.0-dev");

  char *text;
  for (struct uimenu_item_header *item = root->menu_root->head; item; item = item->next) {
    waddstr(mywin, "  ");
    switch (item->type) {
      case UMPS__MENU_TYPE_SPACER:
        waddstr(mywin, "--");
        continue;
      case UMPS__MENU_TYPE_BUTTON:
        text = ((struct uimenu_item_button *)item)->text;
        break;
      case UMPS__MENU_TYPE_MENU:
        text = ((struct uimenu_item_menu *)item)->text;
        break;
      default:
        umps_trap;
    }

    if (text) waddstr(mywin, text);
    else waddstr(mywin, "???");
  }

  attroff(A_REVERSE);
}

void ui__root_draw_status(struct ui_window_root *root)
{
  WINDOW *mywin = root->super.cwindow;

  attron(A_REVERSE);
  mvwhline(mywin, getmaxy(mywin)-1, 0, ' ', getmaxx(mywin));

  if (ui__status_text) {
    mvwaddstr(mywin, getmaxy(mywin)-1, 1, ui__status_text);
  }

  attroff(A_REVERSE);
}

void ui__root_layout_proc(struct ui_window_base *base)
{
  struct ui_window_root *root = ui__cast(root, base);

  int maxy, maxx;
  getmaxyx(base->cwindow, maxy, maxx);

  if (maxy < UI__ROOT_MIN_Y || maxx < UI__ROOT_MIN_X) {
    root->undersize_scr = true;
    return;
  }

  root->undersize_scr = false;

  if (root->content) {
    delwin(root->content->cwindow);
    root->content->cwindow = ui__root_place_content_window(root);
    ui__call_layout_proc(root->content);
  }
}

void ui__root_set_content(struct ui_window_root *root, struct ui_window_base *window)
{
  umps_assert(!window->parent);
  umps_assert(!window->cwindow);
  umps_assert(!root->content);

  window->cwindow = ui__root_place_content_window(root);
  root->content = window;
  window->parent = ui__cast(base, root);
}

void ui__root_set_floating(struct ui_window_root *root, struct ui_window_base *window)
{
  umps_assert(!window->parent);
  umps_assert(!window->cwindow);
  umps_assert(!root->floating);

  
}