aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/interface-curses/curses-info-popup.c
blob: 13d8d651cf122d87826af27d4ed9a007af666a4e (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
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
/*
 * Copyright (C) 2012-2025 Robin Haberkorn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>

#include <curses.h>

#undef lines
#undef buttons

#include "list.h"
#include "string-utils.h"
#include "interface.h"
#include "curses-utils.h"
#include "curses-info-popup.h"
#include "curses-icons.h"

/*
 * FIXME: This is redundant with gtk-info-popup.c.
 */
typedef struct {
	teco_stailq_entry_t entry;

	teco_popup_entry_type_t type;
	teco_string_t name;
	gboolean highlight;
} teco_popup_entry_t;

void
teco_curses_info_popup_add(teco_curses_info_popup_t *ctx, teco_popup_entry_type_t type,
                           const gchar *name, gsize name_len, gboolean highlight)
{
	if (G_UNLIKELY(!ctx->chunk))
		ctx->chunk = g_string_chunk_new(32);

	/*
	 * FIXME: Test with g_slice_new()...
	 * It could however cause problems upon command-line termination
	 * and may not be measurably faster.
	 */
	teco_popup_entry_t *entry = g_new(teco_popup_entry_t, 1);
	entry->type = type;
	/*
	 * Popup entries aren't removed individually, so we can
	 * more efficiently store them via GStringChunk.
	 */
	teco_string_init_chunk(&entry->name, name, name_len, ctx->chunk);
	entry->highlight = highlight;

	teco_stailq_insert_tail(&ctx->list, &entry->entry);

	ctx->longest = MAX(ctx->longest, (gint)name_len);
	ctx->length++;
}

static void
teco_curses_info_popup_init_pad(teco_curses_info_popup_t *ctx, attr_t attr)
{
	G_GNUC_UNUSED gint cols, rows;	/**! screen size */
	getmaxyx(stdscr, rows, cols);
	int pad_lines;			/**! pad height */
	gint pad_cols;			/**! entry columns */
	gint pad_colwidth;		/**! width per entry column */

	/*
	 * With Unicode icons enabled, we reserve 2 characters at the beginning and one
	 * after the filename/directory.
	 * Otherwise 2 characters after the entry.
	 */
	gint reserve = teco_ed & TECO_ED_ICONS ? 2+1 : 2;
	pad_colwidth = MIN(ctx->longest + reserve, cols - 2);

	/* pad_cols = floor((cols - 2) / pad_colwidth) */
	pad_cols = (cols - 2) / pad_colwidth;
	/* pad_lines = ceil(length / pad_cols) */
	pad_lines = (ctx->length+pad_cols-1) / pad_cols;

	/*
	 * Render the entire autocompletion list into a pad
	 * which can be higher than the physical screen.
	 * The pad uses two columns less than the screen since
	 * it will be drawn into the popup window which has left
	 * and right borders.
	 */
	ctx->pad = newpad(pad_lines, cols - 2);

	/*
	 * NOTE: attr could contain A_REVERSE on monochrome terminals,
	 * so we use foreground attributes instead of background attributes.
	 * This way, we can cancel out the A_REVERSE if necessary.
	 */
	wattrset(ctx->pad, attr);
	teco_curses_clrtobot(ctx->pad);

	/*
	 * cur_col is the row currently written.
	 * It does not wrap but grows indefinitely.
	 * Therefore the real current row is (cur_col % popup_cols)
	 */
	gint cur_col = 0;
	for (teco_stailq_entry_t *cur = ctx->list.first; cur != NULL; cur = cur->next) {
		teco_popup_entry_t *entry = (teco_popup_entry_t *)cur;
		gint cur_line = cur_col/pad_cols + 1;

		wmove(ctx->pad, cur_line-1,
		      (cur_col % pad_cols)*pad_colwidth);

		if (entry->highlight)
			wattron(ctx->pad, A_BOLD);

		switch (entry->type) {
		case TECO_POPUP_FILE:
			g_assert(!teco_string_contains(&entry->name, '\0'));
			if (teco_ed & TECO_ED_ICONS) {
				teco_curses_add_wc(ctx->pad, teco_curses_icons_lookup_file(entry->name.data));
				waddch(ctx->pad, ' ');
			}
			teco_curses_format_filename(ctx->pad, entry->name.data, -1);
			break;
		case TECO_POPUP_DIRECTORY:
			g_assert(!teco_string_contains(&entry->name, '\0'));
			if (teco_ed & TECO_ED_ICONS) {
				teco_curses_add_wc(ctx->pad, teco_curses_icons_lookup_dir(entry->name.data));
				waddch(ctx->pad, ' ');
			}
			teco_curses_format_filename(ctx->pad, entry->name.data, -1);
			break;
		default:
			teco_curses_format_str(ctx->pad, entry->name.data, entry->name.len, -1);
			break;
		}

		wattroff(ctx->pad, A_BOLD);

		cur_col++;
	}
}

void
teco_curses_info_popup_show(teco_curses_info_popup_t *ctx, attr_t attr)
{
	if (!ctx->length)
		/* nothing to display */
		return;

	int lines, cols; /* screen dimensions */
	getmaxyx(stdscr, lines, cols);

	if (ctx->window)
		delwin(ctx->window);

	if (!ctx->pad)
		teco_curses_info_popup_init_pad(ctx, attr);
	G_GNUC_UNUSED gint pad_cols, pad_lines;
	getmaxyx(ctx->pad, pad_lines, pad_cols);

	/*
	 * Popup window can cover all but one screen row.
	 * Another row is reserved for the top border.
	 */
	gint popup_lines = MIN(pad_lines + 1, lines - 1);

	/* window covers message, scintilla and info windows */
	ctx->window = newwin(popup_lines, 0, lines - 1 - popup_lines, 0);

	wattrset(ctx->window, attr);

	wborder(ctx->window,
	        ACS_VLINE,
	        ACS_VLINE,	/* may be overwritten with scrollbar */
	        ACS_HLINE,
	        ' ',		/* no bottom line */
	        ACS_ULCORNER, ACS_URCORNER,
	        ACS_VLINE, ACS_VLINE);

	copywin(ctx->pad, ctx->window,
	        ctx->pad_first_line, 0,
	        1, 1, popup_lines - 1, cols - 2, FALSE);

	if (pad_lines <= popup_lines - 1)
		/* no need for scrollbar */
		return;

	/* bar_height = ceil((popup_lines-1)/pad_lines * (popup_lines-2)) */
	gint bar_height = ((popup_lines-1)*(popup_lines-2) + pad_lines-1) /
	                  pad_lines;
	/* bar_y = floor(pad_first_line/pad_lines * (popup_lines-2)) + 1 */
	gint bar_y = ctx->pad_first_line*(popup_lines-2) / pad_lines + 1;

	mvwvline(ctx->window, 1, cols-1, ACS_CKBOARD, popup_lines-2);
	/*
	 * We do not use ACS_BLOCK here since it will not
	 * always be drawn as a solid block (e.g. xterm).
	 * Instead, simply draw reverse blanks.
	 */
	wmove(ctx->window, bar_y, cols-1);
	wattrset(ctx->window, attr ^ A_REVERSE);
	wvline(ctx->window, ' ', bar_height);
}

/**
 * Find the entry at the given character coordinates.
 *
 * @param ctx The popup widget to look up
 * @param y The pointer's Y position, relative to the popup's window
 * @param x The pointer's X position, relative to the popup's window
 * @return Pointer to the entry's string under the pointer or NULL.
 *   This string is owned by the popup and is only valid until the
 *   popup is cleared.
 *
 * @note This must match the calculations in teco_curses_info_popup_init_pad().
 *   But we could perhaps also cache these values.
 */
const teco_string_t *
teco_curses_info_popup_getentry(teco_curses_info_popup_t *ctx, gint y, gint x)
{
	G_GNUC_UNUSED gint lines, cols;	/**! screen dimensions */
	getmaxyx(stdscr, lines, cols);
	gint pad_cols;			/**! entry columns */
	gint pad_colwidth;		/**! width per entry column */

	if (y == 0)
		return NULL;

	/*
	 * With Unicode icons enabled, we reserve 2 characters at the beginning and one
	 * after the filename/directory.
	 * Otherwise 2 characters after the entry.
	 */
	gint reserve = teco_ed & TECO_ED_ICONS ? 2+1 : 2;
	pad_colwidth = MIN(ctx->longest + reserve, cols - 2);

	/* pad_cols = floor((cols - 2) / pad_colwidth) */
	pad_cols = (cols - 2) / pad_colwidth;

	gint cur_col = 0;
	for (teco_stailq_entry_t *cur = ctx->list.first; cur != NULL; cur = cur->next) {
		teco_popup_entry_t *entry = (teco_popup_entry_t *)cur;
		gint cur_line = cur_col/pad_cols + 1;

		if (cur_line > ctx->pad_first_line+y)
			break;
		if (cur_line == ctx->pad_first_line+y &&
		    x > (cur_col % pad_cols)*pad_colwidth && x <= ((cur_col % pad_cols)+1)*pad_colwidth)
			return &entry->name;

		cur_col++;
	}

	return NULL;
}

void
teco_curses_info_popup_scroll_page(teco_curses_info_popup_t *ctx)
{
	G_GNUC_UNUSED gint lines, cols;
	getmaxyx(stdscr, lines, cols);
	G_GNUC_UNUSED gint pad_lines, pad_cols;
	getmaxyx(ctx->pad, pad_lines, pad_cols);
	gint popup_lines = MIN(pad_lines + 1, lines - 1);

	/* progress scroll position */
	ctx->pad_first_line += popup_lines - 1;
	/* wrap on last shown page */
	ctx->pad_first_line %= pad_lines;
	if (pad_lines - ctx->pad_first_line < popup_lines - 1)
		/* show last page */
		ctx->pad_first_line = pad_lines - (popup_lines - 1);
}

void
teco_curses_info_popup_scroll(teco_curses_info_popup_t *ctx, gint delta)
{
	G_GNUC_UNUSED gint lines, cols;
	getmaxyx(stdscr, lines, cols);
	G_GNUC_UNUSED gint pad_lines, pad_cols;
	getmaxyx(ctx->pad, pad_lines, pad_cols);
	gint popup_lines = MIN(pad_lines + 1, lines - 1);

	ctx->pad_first_line = MAX(ctx->pad_first_line+delta, 0);
	if (pad_lines - ctx->pad_first_line < popup_lines - 1)
		/* show last page */
		ctx->pad_first_line = pad_lines - (popup_lines - 1);
}

void
teco_curses_info_popup_clear(teco_curses_info_popup_t *ctx)
{
	if (ctx->window)
		delwin(ctx->window);
	if (ctx->pad)
		delwin(ctx->pad);
	if (ctx->chunk)
		g_string_chunk_free(ctx->chunk);

	teco_stailq_entry_t *entry;
	while ((entry = teco_stailq_remove_head(&ctx->list)))
		g_free(entry);

	teco_curses_info_popup_init(ctx);
}