summaryrefslogtreecommitdiff
path: root/alsamixer/textbox.c
blob: ebac9fa41177779f927eb53f6f196a5bd8ae9454 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * textbox.c - show a text box for messages, files or help
 * Copyright (c) 1998,1999 Tim Janik
 *                         Jaroslav Kysela <perex@perex.cz>
 * Copyright (c) 2009      Clemens Ladisch <clemens@ladisch.de>
 *
 * 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 2 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/>.
 */

#include "aconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include CURSESINC
#include <alsa/asoundlib.h>
#include "gettext_curses.h"
#include "utils.h"
#include "die.h"
#include "mem.h"
#include "colors.h"
#include "widget.h"
#include "textbox.h"
#include "bindings.h"

static void create_text_box(const char *const *lines, unsigned int count,
			    const char *title, int attrs);

void show_error(const char *msg, int err)
{
	const char *lines[2];
	unsigned int count;

	lines[0] = msg;
	count = 1;
	if (err) {
		lines[1] = strerror(err);
		count = 2;
	}
	create_text_box(lines, count, _("Error"), attrs.errormsg);
}

void show_alsa_error(const char *msg, int err)
{
	const char *lines[2];
	unsigned int count;

	lines[0] = msg;
	count = 1;
	if (err < 0) {
		lines[1] = snd_strerror(err);
		count = 2;
	}
	create_text_box(lines, count, _("Error"), attrs.errormsg);
}

void show_textfile(const char *file_name)
{
	char *buf;
	unsigned int file_size;
	unsigned int line_count;
	unsigned int i;
	const char **lines;
	const char *start_line;

	buf = read_file(file_name, &file_size);
	if (!buf) {
		int err = errno;
		buf = casprintf(_("Cannot open file \"%s\"."), file_name);
		show_error(buf, err);
		return;
	}
	line_count = 0;
	for (i = 0; i < file_size; ++i)
		line_count += buf[i] == '\n';
	lines = ccalloc(line_count, sizeof *lines);
	line_count = 0;
	start_line = buf;
	for (i = 0; i < file_size; ++i) {
		if (buf[i] == '\n') {
			lines[line_count++] = start_line;
			buf[i] = '\0';
			start_line = &buf[i + 1];
		}
		if (buf[i] == '\t')
			buf[i] = ' ';
	}
	create_text_box(lines, line_count, file_name, attrs.textbox);
	free(lines);
	free(buf);
}

void show_text(const char *const *lines, unsigned int count, const char *title)
{
	create_text_box(lines, count, title, attrs.textbox);
}

/**********************************************************************/

static struct widget text_widget;
static char *title;
static int widget_attrs;
static char **text_lines;
static unsigned int text_lines_count;
static int max_line_width;
static int text_box_y;
static int text_box_x;
static int max_scroll_y;
static int max_scroll_x;
static int current_top;
static int current_left;

static void update_text_lines(void)
{
	int i;
	int width;
	const char *line_begin;
	const char *line_end;
	int cur_y, cur_x;
	int rest_of_line;

	for (i = 0; i < text_box_y; ++i) {
		width = current_left;
		line_begin = mbs_at_width(text_lines[current_top + i], &width, 1);
		wmove(text_widget.window, i + 1, 1);
		if (width > current_left)
			waddch(text_widget.window, ' ');
		if (*line_begin != '\0') {
			width = text_box_x - (width > current_left);
			line_end = mbs_at_width(line_begin, &width, -1);
			if (width)
				waddnstr(text_widget.window, line_begin,
					 line_end - line_begin);
		}
		getyx(text_widget.window, cur_y, cur_x);
		if (cur_y == i + 1) {
			rest_of_line = text_box_x + 1 - cur_x;
			if (rest_of_line > 0)
				wprintw(text_widget.window, "%*s", rest_of_line, "");
		}
	}
}

static void update_y_scroll_bar(void)
{
	int length;
	int begin;

	if (max_scroll_y <= 0 || text_lines_count == 0)
		return;
	length = text_box_y * text_box_y / text_lines_count;
	if (length >= text_box_y)
		return;
	begin = current_top * (text_box_y - length) / max_scroll_y;
	mvwvline(text_widget.window, 1, text_box_x + 1, ' ', text_box_y);
	mvwvline(text_widget.window, begin + 1, text_box_x + 1, ACS_BOARD, length);
}

static void update_x_scroll_bar(void)
{
	int length;
	int begin;

	if (max_scroll_x <= 0 || max_line_width <= 0)
		return;
	length = text_box_x * text_box_x / max_line_width;
	if (length >= text_box_x)
		return;
	begin = current_left * (text_box_x - length) / max_scroll_x;
	mvwhline(text_widget.window, text_box_y + 1, 1, ' ', text_box_x);
	mvwhline(text_widget.window, text_box_y + 1, begin + 1, ACS_BOARD, length);
}

static void move_x(int delta)
{
	int left;

	left = current_left + delta;
	if (left < 0)
		left = 0;
	else if (left > max_scroll_x)
		left = max_scroll_x;
	if (left != current_left) {
		current_left = left;
		update_text_lines();
		update_x_scroll_bar();
	}
}

static void move_y(int delta)
{
	int top;

	top = current_top + delta;
	if (top < 0)
		top = 0;
	else if (top > max_scroll_y)
		top = max_scroll_y;
	if (top != current_top) {
		current_top = top;
		update_text_lines();
		update_y_scroll_bar();
	}
}

static void on_handle_key(int key)
{
	if (key >= ARRAY_SIZE(textbox_bindings))
		return;

	switch (textbox_bindings[key]) {
	case CMD_TEXTBOX_CLOSE:
		text_widget.close();
		break;
	case CMD_TEXTBOX_DOWN:
		move_y(1);
		break;
	case CMD_TEXTBOX_UP:
		move_y(-1);
		break;
	case CMD_TEXTBOX_LEFT:
		move_x(-1);
		break;
	case CMD_TEXTBOX_RIGHT:
		move_x(1);
		break;
	case CMD_TEXTBOX_PAGE_DOWN:
		move_y(text_box_y);
		break;
	case CMD_TEXTBOX_PAGE_UP:
		move_y(-text_box_y);
		break;
	case CMD_TEXTBOX_TOP:
		move_x(-max_scroll_x);
		break;
	case CMD_TEXTBOX_BOTTOM:
		move_x(max_scroll_x);
		break;
	case CMD_TEXTBOX_PAGE_RIGHT:
		move_x(8);
		break;
	case CMD_TEXTBOX_PAGE_LEFT:
		move_x(-8);
		break;
	}
}

static bool create(void)
{
	int len, width;

	if (screen_lines < 3 || screen_cols < 8) {
		text_widget.close();
		beep();
		return FALSE;
	}

	width = max_line_width;
	len = get_mbs_width(title) + 2;
	if (width < len)
		width = len;

	text_box_y = text_lines_count;
	if (text_box_y > screen_lines - 2)
		text_box_y = screen_lines - 2;
	max_scroll_y = text_lines_count - text_box_y;
	text_box_x = width;
	if (text_box_x > screen_cols - 2)
		text_box_x = screen_cols - 2;
	max_scroll_x = max_line_width - text_box_x;

	widget_init(&text_widget, text_box_y + 2, text_box_x + 2,
		    SCREEN_CENTER, SCREEN_CENTER, widget_attrs, WIDGET_BORDER);
	mvwprintw(text_widget.window, 0, (text_box_x + 2 - get_mbs_width(title) - 2) / 2, " %s ", title);

	if (current_top > max_scroll_y)
		current_top = max_scroll_y;
	if (current_left > max_scroll_x)
		current_left = max_scroll_x;
	update_text_lines();
	update_y_scroll_bar();
	update_x_scroll_bar();
	return TRUE;
}

static void on_window_size_changed(void)
{
	create();
}

static void on_close(void)
{
	unsigned int i;

	for (i = 0; i < text_lines_count; ++i)
		free(text_lines[i]);
	free(text_lines);
	widget_free(&text_widget);
}

static struct widget text_widget = {
	.handle_key = on_handle_key,
	.window_size_changed = on_window_size_changed,
	.close = on_close,
};

static void create_text_box(const char *const *lines, unsigned int count,
			    const char *title_, int attrs)
{
	unsigned int i;

	text_lines = ccalloc(count, sizeof *text_lines);
	for (i = 0; i < count; ++i)
		text_lines[i] = cstrdup(lines[i]);
	text_lines_count = count;
	max_line_width = get_max_mbs_width(lines, count);
	title = cstrdup(title_);
	widget_attrs = attrs;

	current_top = 0;
	current_left = 0;

	create();
}