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
|
#include <stdlib.h>
#include <assert.h>
#include "ui.internal.h"
unsigned ui__dock_position_opposite(unsigned position)
{
switch (position)
{
case UI__WINDOW_DOCK_TOP:
return UI__WINDOW_DOCK_BOTTOM;
case UI__WINDOW_DOCK_BOTTOM:
return UI__WINDOW_DOCK_TOP;
case UI__WINDOW_DOCK_LEFT:
return UI__WINDOW_DOCK_RIGHT;
case UI__WINDOW_DOCK_RIGHT:
return UI__WINDOW_DOCK_LEFT;
default:
abort(); /* trap: the center dock has no opposite! (or position is invalid) */
}
}
void ui__dock_adjust(struct ui_window_dock *dock, unsigned flags, int *maxy, int *maxx, int *begy, int *begx)
{
if (flags & 1)
{
if (dock->children[UI__WINDOW_DOCK_TOP])
{
unsigned top_maxy = getmaxy(dock->children[UI__WINDOW_DOCK_TOP]->cwindow);
*maxy -= top_maxy;
*begy += top_maxy;
}
if (dock->children[UI__WINDOW_DOCK_BOTTOM])
{
unsigned bottom_maxy = getmaxy(dock->children[UI__WINDOW_DOCK_BOTTOM]->cwindow);
*maxy -= bottom_maxy;
}
}
if (flags & 2)
{
if (dock->children[UI__WINDOW_DOCK_LEFT])
{
unsigned left_maxx = getmaxx(dock->children[UI__WINDOW_DOCK_LEFT]->cwindow);
*maxx -= left_maxx;
*begx += left_maxx;
}
if (dock->children[UI__WINDOW_DOCK_RIGHT])
{
unsigned right_maxx = getmaxx(dock->children[UI__WINDOW_DOCK_RIGHT]->cwindow);
*maxx -= right_maxx;
}
}
}
/* This is hand-coded to fit with the window priorities, as specified here in ascending order:
* - Top/bottom
* - Left/right
* - Center
*
* Note that other methods (layout proc namely) require this same order to be present in the values of the dock constants. */
WINDOW *ui__dock_place_window(struct ui_window_dock *dock, unsigned position, float size)
{
int maxy, maxx;
int begy, begx;
int out_lines, out_cols;
int out_y, out_x;
getmaxyx(dock->super.cwindow, maxy, maxx);
getbegyx(dock->super.cwindow, begy, begx);
switch (position)
{
case UI__WINDOW_DOCK_TOP:
out_lines = maxy * size;
out_y = begy;
out_cols = maxx;
out_x = begx;
break;
case UI__WINDOW_DOCK_BOTTOM:
out_lines = maxy * size;
out_y = maxy - out_lines + begy;
out_cols = maxx;
out_x = begx;
break;
case UI__WINDOW_DOCK_LEFT:
ui__dock_adjust(dock, 1, &maxy, &maxx, &begy, &begx);
out_lines = maxy;
out_y = begy;
out_cols = maxx * size;
out_x = begx;
break;
case UI__WINDOW_DOCK_RIGHT:
ui__dock_adjust(dock, 1, &maxy, &maxx, &begy, &begx);
out_lines = maxy;
out_y = begy;
out_cols = maxx * size;
out_x = maxx - out_cols + begx;
break;
case UI__WINDOW_DOCK_CENTER:
ui__dock_adjust(dock, 3, &maxy, &maxx, &begy, &begx);
out_lines = maxy;
out_y = begy;
out_cols = maxx;
out_x = begx;
break;
default:
assert(false);
}
return newwin(out_lines, out_cols, out_y, out_x);
}
void ui__dock_add_child(struct ui_window_dock *dock, struct ui_window_base *child, unsigned position, float size)
{
assert(!dock->children[position]); /* TODO: handle gracefully (technically invalid usage) */
assert(!child->parent); /* TODO: take this window from its current parent */
assert(!child->cwindow);
child->parent = (struct ui_window_base *)dock;
dock->children[position] = child;
dock->childsizes[position] = size;
if (position != UI__WINDOW_DOCK_CENTER)
{
unsigned opposite = ui__dock_position_opposite(position);
if (dock->children[opposite] && size + dock->childsizes[opposite] > 1)
{
dock->childsizes[position] = 1 - dock->childsizes[opposite];
}
}
/* now set up the window for this child */
child->cwindow = ui__dock_place_window(dock, position, size);
}
void ui__dock_default_draw_proc(struct ui_window_base *base)
{
struct ui_window_dock *dock = ui__cast(dock, base);
wrefresh(dock->super.cwindow);
for (unsigned i = 0; i < UI__WINDOW_DOCK_MAX; ++i)
{
if (dock->children[i])
ui__call_draw_proc(dock->children[i]);
}
}
void ui__dock_default_layout_proc(struct ui_window_base *base)
{
struct ui_window_dock *dock = ui__cast(dock, base);
/* fix the layout of children */
for (unsigned i = 0; i < UI__WINDOW_DOCK_MAX; ++i)
{
struct ui_window_base *child = dock->children[i];
if (!child) continue;
delwin(child->cwindow);
child->cwindow = ui__dock_place_window(dock, i, dock->childsizes[i]);
ui__call_layout_proc(child);
}
}
|