add hook for statusbar logging
[claws.git] / src / statusbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <gtk/gtkstatusbar.h>
26 #include <stdarg.h>
27
28 #include "intl.h"
29 #include "statusbar.h"
30 #include "gtkutils.h"
31 #include "utils.h"
32 #include "log.h"
33 #include "hooks.h"
34
35 #define BUFFSIZE 1024
36
37 static GList *statusbar_list = NULL;
38 void statusbar_puts_all_hook (gpointer source, gpointer data);
39
40 GtkWidget *statusbar_create(void)
41 {
42         GtkWidget *statusbar;
43
44         statusbar = gtk_statusbar_new();
45         
46         if(statusbar_list == NULL)
47                 hooks_register_hook(STATUSBAR_PUTS_ALL_HOOKLIST, statusbar_puts_all_hook, NULL);
48
49         statusbar_list = g_list_append(statusbar_list, statusbar);
50
51         return statusbar;
52 }
53
54 void statusbar_puts_all_hook (gpointer source, gpointer data)
55 {
56         LogText *logtext = (LogText *) source;
57         statusbar_puts_all(logtext->text);
58 }
59
60 void statusbar_puts(GtkStatusbar *statusbar, const gchar *str)
61 {
62         gint cid;
63         gchar *buf;
64
65         buf = g_strdup(str);
66         strretchomp(buf);
67         if (strlen(buf) > 76) {
68                 wchar_t *wbuf;
69
70                 wbuf = strdup_mbstowcs(buf);
71
72                 if (wcslen(wbuf) > 60) {
73                         gchar *tmp;
74
75                         g_free(buf);
76                         wbuf[60] = (wchar_t)0;
77                         tmp = strdup_wcstombs(wbuf);
78                         buf = g_strconcat(tmp, "...", NULL);
79                         g_free(tmp);
80                 }
81
82                 g_free(wbuf);
83         }
84
85         cid = gtk_statusbar_get_context_id(statusbar, "Standard Output");
86         gtk_statusbar_pop(statusbar, cid);
87         gtk_statusbar_push(statusbar, cid, buf);
88         gtkut_widget_wait_for_draw(GTK_WIDGET(statusbar)->parent);
89
90         g_free(buf);
91 }
92
93 void statusbar_puts_all(const gchar *str)
94 {
95         GList *cur;
96
97         for (cur = statusbar_list; cur != NULL; cur = cur->next)
98                 statusbar_puts(GTK_STATUSBAR(cur->data), str);
99 }
100
101 void statusbar_print(GtkStatusbar *statusbar, const gchar *format, ...)
102 {
103         va_list args;
104         gchar buf[BUFFSIZE];
105
106         va_start(args, format);
107         g_vsnprintf(buf, sizeof(buf), format, args);
108         va_end(args);
109
110         statusbar_puts(statusbar, buf);
111 }
112
113 void statusbar_print_all(const gchar *format, ...)
114 {
115         va_list args;
116         gchar buf[BUFFSIZE];
117         GList *cur;
118
119         va_start(args, format);
120         g_vsnprintf(buf, sizeof(buf), format, args);
121         va_end(args);
122
123         for (cur = statusbar_list; cur != NULL; cur = cur->next)
124                 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
125 }
126
127 void statusbar_pop_all(void)
128 {
129         GList *cur;
130         gint cid;
131
132         for (cur = statusbar_list; cur != NULL; cur = cur->next) {
133                 cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(cur->data),
134                                                    "Standard Output");
135                 gtk_statusbar_pop(GTK_STATUSBAR(cur->data), cid);
136         }
137 }