2007-04-26 [colin] 2.9.1cvs28
[claws.git] / src / statusbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtkstatusbar.h>
27 #include <gtk/gtkprogressbar.h>
28 #include <stdarg.h>
29
30 #include "mainwindow.h"
31 #include "statusbar.h"
32 #include "gtkutils.h"
33 #include "utils.h"
34 #include "log.h"
35 #include "hooks.h"
36
37 #define BUFFSIZE 1024
38
39 static GList *statusbar_list = NULL;
40 gint statusbar_puts_all_hook_id = -1;
41
42 GtkWidget *statusbar_create(void)
43 {
44         GtkWidget *statusbar;
45
46         statusbar = gtk_statusbar_new();
47         gtk_widget_set_size_request(statusbar, 1, -1);
48         statusbar_list = g_list_append(statusbar_list, statusbar);
49         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(statusbar), 
50                                           FALSE);
51         gtk_container_set_border_width(GTK_CONTAINER(statusbar), 1);
52
53         gtk_widget_ref(GTK_STATUSBAR(statusbar)->label);
54         gtk_container_remove(GTK_CONTAINER(GTK_STATUSBAR(statusbar)->frame),
55                 GTK_STATUSBAR(statusbar)->label);
56         gtk_widget_hide(GTK_STATUSBAR(statusbar)->frame);
57         gtk_box_pack_start (GTK_BOX(statusbar), GTK_STATUSBAR(statusbar)->label, 
58                 TRUE, TRUE, 0);
59         gtk_widget_unref(GTK_STATUSBAR(statusbar)->label);
60         gtk_container_remove(GTK_CONTAINER(statusbar),
61                 GTK_STATUSBAR(statusbar)->frame);
62         GTK_STATUSBAR(statusbar)->frame = gtk_frame_new(NULL);
63
64         return statusbar;
65 }
66
67 void statusbar_puts(GtkStatusbar *statusbar, const gchar *str)
68 {
69         gint cid;
70         gchar *buf;
71         gchar *tmp;
72
73         tmp = g_strdup(str);
74         strretchomp(tmp);
75         buf = trim_string(tmp, 76);
76         g_free(tmp);
77
78         cid = gtk_statusbar_get_context_id(statusbar, "Standard Output");
79         gtk_statusbar_pop(statusbar, cid);
80         gtk_statusbar_push(statusbar, cid, buf);
81         gtkut_widget_draw_now(GTK_WIDGET(statusbar));
82
83         g_free(buf);
84 }
85
86 void statusbar_puts_all(const gchar *str)
87 {
88         GList *cur;
89
90         for (cur = statusbar_list; cur != NULL; cur = cur->next)
91                 statusbar_puts(GTK_STATUSBAR(cur->data), str);
92 }
93
94 void statusbar_print(GtkStatusbar *statusbar, const gchar *format, ...)
95 {
96         va_list args;
97         gchar buf[BUFFSIZE];
98
99         va_start(args, format);
100         g_vsnprintf(buf, sizeof(buf), format, args);
101         va_end(args);
102
103         statusbar_puts(statusbar, buf);
104 }
105
106 void statusbar_print_all(const gchar *format, ...)
107 {
108         va_list args;
109         gchar buf[BUFFSIZE];
110         GList *cur;
111
112         va_start(args, format);
113         g_vsnprintf(buf, sizeof(buf), format, args);
114         va_end(args);
115
116         for (cur = statusbar_list; cur != NULL; cur = cur->next)
117                 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
118 }
119
120 void statusbar_pop_all(void)
121 {
122         GList *cur;
123         gint cid;
124
125         for (cur = statusbar_list; cur != NULL; cur = cur->next) {
126                 cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(cur->data),
127                                                    "Standard Output");
128                 gtk_statusbar_pop(GTK_STATUSBAR(cur->data), cid);
129         }
130 }
131
132 static gboolean statusbar_puts_all_hook (gpointer source, gpointer data)
133 {
134         LogText *logtext = (LogText *) source;
135
136         g_return_val_if_fail(logtext != NULL, TRUE);
137         g_return_val_if_fail(logtext->text != NULL, TRUE);
138
139         statusbar_pop_all();
140         if (logtext->type == LOG_NORMAL) {
141                 statusbar_puts_all(logtext->text + LOG_TIME_LEN);
142         } else if (logtext->type == LOG_MSG) {
143                 statusbar_puts_all(logtext->text);
144         }
145
146         return FALSE;
147 }
148
149 void statusbar_verbosity_set(gboolean verbose)
150 {
151         if (verbose && (statusbar_puts_all_hook_id == -1)) {
152                 statusbar_puts_all_hook_id =
153                         hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook, NULL);
154         } else if (!verbose && (statusbar_puts_all_hook_id != -1)) {
155                 hooks_unregister_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook_id);
156                 statusbar_puts_all_hook_id = -1;
157                 statusbar_pop_all();
158         }
159 }
160
161 void statusbar_progress_all (gint done, gint total, gint step) 
162 {
163         gchar buf[32];
164         if (total && done % step == 0) {
165                 g_snprintf(buf, sizeof(buf), "%d / %d", done, total);
166                 gtk_progress_bar_set_text
167                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), buf);
168                 gtk_progress_bar_set_fraction
169                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar),
170                          (total == 0) ? 0 : (gfloat)done / (gfloat)total);
171         } else if (total == 0) {
172                 gtk_progress_bar_set_text
173                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), "");
174                 gtk_progress_bar_set_fraction
175                         (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), 0.0);
176         }
177 }