2 gui-printtext.c : irssi
4 Copyright (C) 1999 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "printtext.h"
29 #include "gui-printtext.h"
30 #include "gui-windows.h"
32 int mirc_colors[] = { 15, 0, 1, 2, 12, 4, 5, 6, 14, 10, 3, 11, 9, 13, 8, 7 };
33 static int scrollback_lines, scrollback_hours, scrollback_burst_remove;
35 static int last_fg, last_bg, last_flags;
36 static int next_xpos, next_ypos;
38 static GHashTable *indent_functions;
39 static INDENT_FUNC default_indent_func;
41 void gui_register_indent_func(const char *name, INDENT_FUNC func)
46 if (g_hash_table_lookup_extended(indent_functions, name, &key, &value)) {
48 g_hash_table_remove(indent_functions, key);
54 list = g_slist_append(list, (void *) func);
55 g_hash_table_insert(indent_functions, key, list);
58 void gui_unregister_indent_func(const char *name, INDENT_FUNC func)
63 if (g_hash_table_lookup_extended(indent_functions, name, &key, &value)) {
66 list = g_slist_remove(list, (void *) func);
67 g_hash_table_remove(indent_functions, key);
71 g_hash_table_insert(indent_functions, key, list);
74 if (default_indent_func == func)
75 gui_set_default_indent(NULL);
77 textbuffer_views_unregister_indent_func(func);
80 void gui_set_default_indent(const char *name)
84 list = name == NULL ? NULL :
85 g_hash_table_lookup(indent_functions, name);
86 default_indent_func = list == NULL ? NULL :
87 (INDENT_FUNC) list->data;
88 gui_windows_reset_settings();
91 INDENT_FUNC get_default_indent_func(void)
93 return default_indent_func;
96 void gui_printtext(int xpos, int ypos, const char *str)
103 next_xpos = next_ypos = -1;
106 void gui_printtext_after(TEXT_DEST_REC *dest, LINE_REC *prev, const char *str)
110 gui = WINDOW_GUI(dest->window);
112 gui->use_insert_after = TRUE;
113 gui->insert_after = prev;
114 format_send_to_gui(dest, str);
115 gui->use_insert_after = FALSE;
118 static void remove_old_lines(TEXT_BUFFER_VIEW_REC *view)
123 old_time = time(NULL)-(scrollback_hours*3600)+1;
124 if (view->buffer->lines_count >=
125 scrollback_lines+scrollback_burst_remove) {
126 /* remove lines by line count */
127 while (view->buffer->lines_count > scrollback_lines) {
128 line = view->buffer->first_line;
129 if (line->info.time >= old_time ||
130 scrollback_lines == 0) {
131 /* too new line, don't remove yet - also
132 if scrollback_lines is 0, we want to check
133 only scrollback_hours setting. */
136 textbuffer_view_remove_line(view, line);
141 static void get_colors(int flags, int *fg, int *bg, int *attr)
143 if (flags & GUI_PRINT_FLAG_MIRC_COLOR) {
144 /* mirc colors - real range is 0..15, but after 16
145 colors wrap to 0, 1, ... */
146 if (*bg >= 0) *bg = mirc_colors[*bg % 16];
147 if (*fg >= 0) *fg = mirc_colors[*fg % 16];
150 if (*fg < 0 || *fg > 15)
151 *fg = current_theme->default_color;
152 if (*bg < 0 || *bg > 15)
156 if (flags & GUI_PRINT_FLAG_REVERSE) *attr |= ATTR_REVERSE;
157 if (flags & GUI_PRINT_FLAG_BOLD) *attr |= ATTR_BOLD;
158 if (flags & GUI_PRINT_FLAG_UNDERLINE) *attr |= ATTR_UNDERLINE;
159 if (flags & GUI_PRINT_FLAG_BLINK) *attr |= ATTR_BLINK;
162 static void line_add_colors(TEXT_BUFFER_REC *buffer, LINE_REC **line,
163 int fg, int bg, int flags)
165 unsigned char data[20];
168 /* get the fg & bg command chars */
169 fg = fg < 0 ? LINE_COLOR_DEFAULT : fg & 0x0f;
170 bg = LINE_COLOR_BG | (bg < 0 ? LINE_COLOR_DEFAULT : bg & 0x0f);
171 if (flags & GUI_PRINT_FLAG_BOLD)
172 fg |= LINE_COLOR_BOLD;
173 if (flags & GUI_PRINT_FLAG_BLINK)
174 bg |= LINE_COLOR_BLINK;
180 data[pos++] = fg == 0 ? LINE_CMD_COLOR0 : fg;
188 if ((flags & GUI_PRINT_FLAG_UNDERLINE) != (last_flags & GUI_PRINT_FLAG_UNDERLINE)) {
190 data[pos++] = LINE_CMD_UNDERLINE;
192 if ((flags & GUI_PRINT_FLAG_REVERSE) != (last_flags & GUI_PRINT_FLAG_REVERSE)) {
194 data[pos++] = LINE_CMD_REVERSE;
196 if (flags & GUI_PRINT_FLAG_INDENT) {
198 data[pos++] = LINE_CMD_INDENT;
202 *line = textbuffer_insert(buffer, *line, data, pos, NULL);
207 static void line_add_indent_func(TEXT_BUFFER_REC *buffer, LINE_REC **line,
208 const char *function)
211 unsigned char data[1+sizeof(INDENT_FUNC)];
213 list = g_hash_table_lookup(indent_functions, function);
215 data[0] = LINE_CMD_INDENT_FUNC;
216 memcpy(data+1, list->data, sizeof(INDENT_FUNC));
217 *line = textbuffer_insert(buffer, *line,
218 data, sizeof(data), NULL);
222 static void view_add_eol(TEXT_BUFFER_VIEW_REC *view, LINE_REC **line)
224 static const unsigned char eol[] = { 0, LINE_CMD_EOL };
226 *line = textbuffer_insert(view->buffer, *line, eol, 2, NULL);
227 textbuffer_view_insert_line(view, *line);
230 static void sig_gui_print_text(WINDOW_REC *window, void *fgcolor,
231 void *bgcolor, void *pflags,
232 char *str, void *level)
235 TEXT_BUFFER_VIEW_REC *view;
236 LINE_REC *insert_after;
237 LINE_INFO_REC lineinfo;
238 int fg, bg, flags, attr;
240 flags = GPOINTER_TO_INT(pflags);
241 fg = GPOINTER_TO_INT(fgcolor);
242 bg = GPOINTER_TO_INT(bgcolor);
243 get_colors(flags, &fg, &bg, &attr);
245 if (window == NULL) {
246 g_return_if_fail(next_xpos != -1);
248 attr |= fg >= 0 ? fg : ATTR_RESETFG;
249 attr |= bg >= 0 ? (bg << 4) : ATTR_RESETBG;
250 term_set_color(root_window, attr);
252 term_move(root_window, next_xpos, next_ypos);
253 if (flags & GUI_PRINT_FLAG_CLRTOEOL)
254 term_clrtoeol(root_window);
255 term_addstr(root_window, str);
256 next_xpos += strlen(str);
260 lineinfo.level = GPOINTER_TO_INT(level);
261 lineinfo.time = time(NULL);
263 gui = WINDOW_GUI(window);
265 insert_after = gui->use_insert_after ?
266 gui->insert_after : view->buffer->cur_line;
268 if (flags & GUI_PRINT_FLAG_NEWLINE)
269 view_add_eol(view, &insert_after);
270 line_add_colors(view->buffer, &insert_after, fg, bg, flags);
272 if (flags & GUI_PRINT_FLAG_INDENT_FUNC) {
273 /* specify the indentation function */
274 line_add_indent_func(view->buffer, &insert_after, str);
276 insert_after = textbuffer_insert(view->buffer, insert_after,
277 (unsigned char *) str,
278 strlen(str), &lineinfo);
280 if (gui->use_insert_after)
281 gui->insert_after = insert_after;
284 static void sig_gui_printtext_finished(WINDOW_REC *window)
286 TEXT_BUFFER_VIEW_REC *view;
287 LINE_REC *insert_after;
289 last_fg = last_bg = -1;
292 view = WINDOW_GUI(window)->view;
293 insert_after = WINDOW_GUI(window)->use_insert_after ?
294 WINDOW_GUI(window)->insert_after : view->buffer->cur_line;
296 view_add_eol(view, &insert_after);
297 remove_old_lines(view);
300 static void read_settings(void)
302 scrollback_lines = settings_get_int("scrollback_lines");
303 scrollback_hours = settings_get_int("scrollback_hours");
304 scrollback_burst_remove = settings_get_int("scrollback_burst_remove");
307 void gui_printtext_init(void)
309 next_xpos = next_ypos = -1;
310 default_indent_func = NULL;
311 indent_functions = g_hash_table_new((GHashFunc) g_str_hash,
312 (GCompareFunc) g_str_equal);
314 settings_add_int("history", "scrollback_lines", 500);
315 settings_add_int("history", "scrollback_hours", 24);
316 settings_add_int("history", "scrollback_burst_remove", 10);
318 signal_add("gui print text", (SIGNAL_FUNC) sig_gui_print_text);
319 signal_add("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
320 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
325 void gui_printtext_deinit(void)
327 g_hash_table_destroy(indent_functions);
329 signal_remove("gui print text", (SIGNAL_FUNC) sig_gui_print_text);
330 signal_remove("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
331 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);