summaryrefslogtreecommitdiff
path: root/src/fe-text/gui-entry.c
blob: 62ea83fb333376daff76d06a26622891fd2a433b (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
/*
 gui-entry.c : irssi

    Copyright (C) 1999 Timo Sirainen

    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"

#include "screen.h"

static GString *entry;
static int promptlen, permanent_prompt, pos, scrstart, scrpos;
static char *prompt;

static void entry_screenpos(void)
{
	if (pos-scrstart < COLS-1-promptlen && pos-scrstart > 0) {
		scrpos = pos-scrstart;
		return;
	}

	if (pos < COLS-1-promptlen) {
		scrstart = 0;
		scrpos = pos;
	} else {
		scrpos = COLS-1-promptlen;
		scrstart = pos-scrpos;
	}
}

static void entry_update(void)
{
	char *p;
	int n, len;

	len = entry->len-scrstart > COLS-promptlen ?
		COLS-promptlen : entry->len-scrstart;

	set_color(0);
	move(LINES-1, promptlen);

	for (p = entry->str+scrstart, n = 0; n < len; n++, p++) {
		if ((unsigned char) *p >= 32)
			addch((unsigned char) *p);
		else {
			set_color(ATTR_REVERSE);
			addch(*p+'A'-1);
			set_color(0);
		}
	}
	clrtoeol();

	move_cursor(LINES-1, scrpos+promptlen);
	screen_refresh();
}

void gui_entry_set_prompt(const char *str)
{
	if (str != NULL) {
		if (permanent_prompt) return;

		g_free_not_null(prompt);
		prompt = g_strdup(str);
		promptlen = strlen(prompt);
	}

	set_color(0);
	mvaddstr(LINES-1, 0, prompt);

	entry_screenpos();
	entry_update();
}

void gui_entry_set_perm_prompt(const char *str)
{
	g_return_if_fail(str != NULL);

	g_free_not_null(prompt);
	prompt = g_strdup(str);
	promptlen = strlen(prompt);

	permanent_prompt = TRUE;
	gui_entry_set_prompt(NULL);
}

void gui_entry_remove_perm_prompt(void)
{
        permanent_prompt = FALSE;
}

void gui_entry_set_text(const char *str)
{
	g_return_if_fail(str != NULL);

	g_string_assign(entry, str);
	pos = entry->len;

	entry_screenpos();
	entry_update();
}

char *gui_entry_get_text(void)
{
	return entry->str;
}

void gui_entry_insert_text(const char *str)
{
	g_return_if_fail(str != NULL);

	g_string_insert(entry, pos, str);
	pos += strlen(str);

	entry_screenpos();
	entry_update();
}

void gui_entry_insert_char(char chr)
{
	g_string_insert_c(entry, pos, chr);
	pos++;

	entry_screenpos();
	entry_update();
}

void gui_entry_erase(int size)
{
	if (pos < size) return;

	pos -= size;
	g_string_erase(entry, pos, size);

	entry_screenpos();
	entry_update();
}

int gui_entry_get_pos(void)
{
	return pos;
}

void gui_entry_set_pos(int p)
{
	if (p >= 0 && p <= entry->len)
		pos = p;

	entry_screenpos();
	entry_update();
}

void gui_entry_move_pos(int p)
{
	if (pos+p >= 0 && pos+p <= entry->len)
		pos += p;

	entry_screenpos();
	entry_update();
}

void gui_entry_redraw(void)
{
	gui_entry_set_prompt(NULL);

	entry_screenpos();
	entry_update();
}

void gui_entry_init(void)
{
	entry = g_string_new(NULL);

	pos = scrpos = 0;
	prompt = NULL; promptlen = 0;
	permanent_prompt = FALSE;
}

void gui_entry_deinit(void)
{
	if (prompt != NULL) g_free(prompt);
	g_string_free(entry, TRUE);
}