affd2b9f518aa9601a24d20988f791bb0fbd4d92
[runtime.git] / apps / irssi / src / fe-text / gui-printtext.c
1 /*
2  gui-printtext.c : irssi
3
4     Copyright (C) 1999 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "settings.h"
24
25 #include "formats.h"
26 #include "printtext.h"
27
28 #include "term.h"
29 #include "gui-printtext.h"
30 #include "gui-windows.h"
31
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;
34
35 static int last_fg, last_bg, last_flags;
36 static int next_xpos, next_ypos;
37
38 static GHashTable *indent_functions;
39 static INDENT_FUNC default_indent_func;
40
41 void gui_register_indent_func(const char *name, INDENT_FUNC func)
42 {
43         gpointer key, value;
44         GSList *list;
45
46         if (g_hash_table_lookup_extended(indent_functions, name, &key, &value)) {
47                 list = value;
48                 g_hash_table_remove(indent_functions, key);
49         } else {
50                 key = g_strdup(name);
51                 list = NULL;
52         }
53
54         list = g_slist_append(list, (void *) func);
55         g_hash_table_insert(indent_functions, key, list);
56 }
57
58 void gui_unregister_indent_func(const char *name, INDENT_FUNC func)
59 {
60         gpointer key, value;
61         GSList *list;
62
63         if (g_hash_table_lookup_extended(indent_functions, name, &key, &value)) {
64                 list = value;
65
66                 list = g_slist_remove(list, (void *) func);
67                 g_hash_table_remove(indent_functions, key);
68                 if (list == NULL)
69                         g_free(key);
70                 else
71                         g_hash_table_insert(indent_functions, key, list);
72         }
73
74         if (default_indent_func == func)
75                 gui_set_default_indent(NULL);
76
77         textbuffer_views_unregister_indent_func(func);
78 }
79
80 void gui_set_default_indent(const char *name)
81 {
82         GSList *list;
83
84         list = name == NULL ? NULL :
85                 g_hash_table_lookup(indent_functions, name);
86         default_indent_func = list == NULL ? NULL : list->data;
87         gui_windows_reset_settings();
88 }
89
90 INDENT_FUNC get_default_indent_func(void)
91 {
92         return default_indent_func;
93 }
94
95 void gui_printtext(int xpos, int ypos, const char *str)
96 {
97         next_xpos = xpos;
98         next_ypos = ypos;
99
100         printtext_gui(str);
101
102         next_xpos = next_ypos = -1;
103 }
104
105 void gui_printtext_after(TEXT_DEST_REC *dest, LINE_REC *prev, const char *str)
106 {
107         GUI_WINDOW_REC *gui;
108
109         gui = WINDOW_GUI(dest->window);
110
111         gui->use_insert_after = TRUE;
112         gui->insert_after = prev;
113         format_send_to_gui(dest, str);
114         gui->use_insert_after = FALSE;
115 }
116
117 static void remove_old_lines(TEXT_BUFFER_VIEW_REC *view)
118 {
119         LINE_REC *line;
120         time_t old_time;
121
122         old_time = time(NULL)-(scrollback_hours*3600)+1;
123         if (view->buffer->lines_count >=
124             scrollback_lines+scrollback_burst_remove) {
125                 /* remove lines by line count */
126                 while (view->buffer->lines_count > scrollback_lines) {
127                         line = view->buffer->first_line;
128                         if (line->info.time >= old_time ||
129                             scrollback_lines == 0) {
130                                 /* too new line, don't remove yet - also
131                                    if scrollback_lines is 0, we want to check
132                                    only scrollback_hours setting. */
133                                 break;
134                         }
135                         textbuffer_view_remove_line(view, line);
136                 }
137         }
138 }
139
140 static void get_colors(int flags, int *fg, int *bg, int *attr)
141 {
142         if (flags & GUI_PRINT_FLAG_MIRC_COLOR) {
143                 /* mirc colors - real range is 0..15, but after 16
144                    colors wrap to 0, 1, ... */
145                 if (*bg >= 0) *bg = mirc_colors[*bg % 16];
146                 if (*fg >= 0) *fg = mirc_colors[*fg % 16];
147         }
148
149         if (*fg < 0 || *fg > 15)
150                 *fg = current_theme->default_color;
151         if (*bg < 0 || *bg > 15)
152                 *bg = -1;
153
154         *attr = 0;
155         if (flags & GUI_PRINT_FLAG_REVERSE) *attr |= ATTR_REVERSE;
156         if (flags & GUI_PRINT_FLAG_BOLD) *attr |= ATTR_BOLD;
157         if (flags & GUI_PRINT_FLAG_UNDERLINE) *attr |= ATTR_UNDERLINE;
158         if (flags & GUI_PRINT_FLAG_BLINK) *attr |= ATTR_BLINK;
159 }
160
161 static void line_add_colors(TEXT_BUFFER_REC *buffer, LINE_REC **line,
162                             int fg, int bg, int flags)
163 {
164         unsigned char data[20];
165         int pos;
166
167         /* get the fg & bg command chars */
168         fg = fg < 0 ? LINE_COLOR_DEFAULT : fg & 0x0f;
169         bg = LINE_COLOR_BG | (bg < 0 ? LINE_COLOR_DEFAULT : bg & 0x0f);
170         if (flags & GUI_PRINT_FLAG_BOLD)
171                 fg |= LINE_COLOR_BOLD;
172         if (flags & GUI_PRINT_FLAG_BLINK)
173                 bg |= LINE_COLOR_BLINK;
174
175         pos = 0;
176         if (fg != last_fg) {
177                 last_fg = fg;
178                 data[pos++] = 0;
179                 data[pos++] = fg == 0 ? LINE_CMD_COLOR0 : fg;
180         }
181         if (bg != last_bg) {
182                 last_bg = bg;
183                 data[pos++] = 0;
184                 data[pos++] = bg;
185         }
186
187         if ((flags & GUI_PRINT_FLAG_UNDERLINE) != (last_flags & GUI_PRINT_FLAG_UNDERLINE)) {
188                 data[pos++] = 0;
189                 data[pos++] = LINE_CMD_UNDERLINE;
190         }
191         if ((flags & GUI_PRINT_FLAG_REVERSE) != (last_flags & GUI_PRINT_FLAG_REVERSE)) {
192                 data[pos++] = 0;
193                 data[pos++] = LINE_CMD_REVERSE;
194         }
195         if (flags & GUI_PRINT_FLAG_INDENT) {
196                 data[pos++] = 0;
197                 data[pos++] = LINE_CMD_INDENT;
198         }
199
200         if (pos > 0)
201                 *line = textbuffer_insert(buffer, *line, data, pos, NULL);
202
203         last_flags = flags;
204 }
205
206 static void line_add_indent_func(TEXT_BUFFER_REC *buffer, LINE_REC **line,
207                                  const char *function)
208 {
209         GSList *list;
210         unsigned char data[1+sizeof(INDENT_FUNC)];
211
212         list = g_hash_table_lookup(indent_functions, function);
213         if (list != NULL) {
214                 data[0] = LINE_CMD_INDENT_FUNC;
215                 memcpy(data+1, list->data, sizeof(INDENT_FUNC));
216                 *line = textbuffer_insert(buffer, *line,
217                                           data, sizeof(data), NULL);
218         }
219 }
220
221 static void view_add_eol(TEXT_BUFFER_VIEW_REC *view, LINE_REC **line)
222 {
223         static const unsigned char eol[] = { 0, LINE_CMD_EOL };
224
225         *line = textbuffer_insert(view->buffer, *line, eol, 2, NULL);
226         textbuffer_view_insert_line(view, *line);
227 }
228
229 static void sig_gui_print_text(WINDOW_REC *window, void *fgcolor,
230                                void *bgcolor, void *pflags,
231                                char *str, void *level)
232 {
233         GUI_WINDOW_REC *gui;
234         TEXT_BUFFER_VIEW_REC *view;
235         LINE_REC *insert_after;
236         LINE_INFO_REC lineinfo;
237         int fg, bg, flags, attr;
238
239         flags = GPOINTER_TO_INT(pflags);
240         fg = GPOINTER_TO_INT(fgcolor);
241         bg = GPOINTER_TO_INT(bgcolor);
242         get_colors(flags, &fg, &bg, &attr);
243
244         if (window == NULL) {
245                 g_return_if_fail(next_xpos != -1);
246
247                 attr |= fg >= 0 ? fg : ATTR_RESETFG;
248                 attr |= bg >= 0 ? (bg << 4) : ATTR_RESETBG;
249                 term_set_color(root_window, attr);
250
251                 term_move(root_window, next_xpos, next_ypos);
252                 if (flags & GUI_PRINT_FLAG_CLRTOEOL)
253                         term_clrtoeol(root_window);
254                 term_addstr(root_window, str);
255                 next_xpos += strlen(str);
256                 return;
257         }
258
259         lineinfo.level = GPOINTER_TO_INT(level);
260         lineinfo.time = time(NULL);
261
262         gui = WINDOW_GUI(window);
263         view = gui->view;
264         insert_after = gui->use_insert_after ?
265                 gui->insert_after : view->buffer->cur_line;
266
267         if (flags & GUI_PRINT_FLAG_NEWLINE)
268                 view_add_eol(view, &insert_after);
269         line_add_colors(view->buffer, &insert_after, fg, bg, flags);
270
271         if (flags & GUI_PRINT_FLAG_INDENT_FUNC) {
272                 /* specify the indentation function */
273                 line_add_indent_func(view->buffer, &insert_after, str);
274         } else {
275                 insert_after = textbuffer_insert(view->buffer, insert_after,
276                                                  (unsigned char *) str,
277                                                  strlen(str), &lineinfo);
278         }
279         if (gui->use_insert_after)
280                 gui->insert_after = insert_after;
281 }
282
283 static void sig_gui_printtext_finished(WINDOW_REC *window)
284 {
285         TEXT_BUFFER_VIEW_REC *view;
286         LINE_REC *insert_after;
287
288         last_fg = last_bg = -1;
289         last_flags = 0;
290
291         view = WINDOW_GUI(window)->view;
292         insert_after = WINDOW_GUI(window)->use_insert_after ?
293                 WINDOW_GUI(window)->insert_after : view->buffer->cur_line;
294
295         view_add_eol(view, &insert_after);
296         remove_old_lines(view);
297 }
298
299 static void read_settings(void)
300 {
301         scrollback_lines = settings_get_int("scrollback_lines");
302         scrollback_hours = settings_get_int("scrollback_hours");
303         scrollback_burst_remove = settings_get_int("scrollback_burst_remove");
304 }
305
306 void gui_printtext_init(void)
307 {
308         next_xpos = next_ypos = -1;
309         default_indent_func = NULL;
310         indent_functions = g_hash_table_new((GHashFunc) g_str_hash,
311                                             (GCompareFunc) g_str_equal);
312
313         settings_add_int("history", "scrollback_lines", 500);
314         settings_add_int("history", "scrollback_hours", 24);
315         settings_add_int("history", "scrollback_burst_remove", 10);
316
317         signal_add("gui print text", (SIGNAL_FUNC) sig_gui_print_text);
318         signal_add("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
319         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
320
321         read_settings();
322 }
323
324 void gui_printtext_deinit(void)
325 {
326         g_hash_table_destroy(indent_functions);
327
328         signal_remove("gui print text", (SIGNAL_FUNC) sig_gui_print_text);
329         signal_remove("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
330         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
331 }