Fix bug #3574: Template addressing
[claws.git] / src / statusbar.c
blob4ced87f329cc0923746b2a14df119a15e9af6036
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28 #include <stdarg.h>
30 #include "mainwindow.h"
31 #include "statusbar.h"
32 #include "gtkutils.h"
33 #include "utils.h"
34 #include "log.h"
35 #include "hooks.h"
37 #define BUFFSIZE 1024
39 static GList *statusbar_list = NULL;
40 gint statusbar_puts_all_hook_id = -1;
42 GtkWidget *statusbar_create(void)
44 GtkWidget *statusbar;
45 GtkWidget *child;
46 GtkWidget *parent;
47 GtkWidget *hbox;
49 statusbar = gtk_statusbar_new();
50 gtk_widget_set_size_request(statusbar, 1, -1);
51 statusbar_list = g_list_append(statusbar_list, statusbar);
52 #if !GTK_CHECK_VERSION(3, 0, 0)
53 gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(statusbar),
54 FALSE);
55 #endif
56 gtk_container_set_border_width(GTK_CONTAINER(statusbar), 1);
57 child = gtk_statusbar_get_message_area(GTK_STATUSBAR(statusbar));
58 parent = gtk_widget_get_parent(child);
59 gtk_container_remove(GTK_CONTAINER(parent), g_object_ref(child));
60 hbox = gtk_hbox_new(FALSE, 0);
61 gtk_container_add(GTK_CONTAINER(parent), hbox);
62 gtk_widget_show(hbox);
63 gtk_box_pack_start(GTK_BOX(hbox), child, TRUE, TRUE, 0);
64 g_object_unref(child);
66 return statusbar;
69 void statusbar_puts(GtkStatusbar *statusbar, const gchar *str)
71 gint cid;
72 gchar *buf;
73 gchar *tmp;
75 tmp = g_strdup(str);
76 strretchomp(tmp);
77 buf = trim_string(tmp, 76);
78 g_free(tmp);
80 cid = gtk_statusbar_get_context_id(statusbar, "Standard Output");
81 gtk_statusbar_pop(statusbar, cid);
82 gtk_statusbar_push(statusbar, cid, buf);
83 gtkut_widget_draw_now(GTK_WIDGET(statusbar));
85 g_free(buf);
88 void statusbar_puts_all(const gchar *str)
90 GList *cur;
92 for (cur = statusbar_list; cur != NULL; cur = cur->next)
93 statusbar_puts(GTK_STATUSBAR(cur->data), str);
96 void statusbar_print(GtkStatusbar *statusbar, const gchar *format, ...)
98 va_list args;
99 gchar buf[BUFFSIZE];
101 va_start(args, format);
102 g_vsnprintf(buf, sizeof(buf), format, args);
103 va_end(args);
105 statusbar_puts(statusbar, buf);
108 void statusbar_print_all(const gchar *format, ...)
110 va_list args;
111 gchar buf[BUFFSIZE];
112 GList *cur;
114 va_start(args, format);
115 g_vsnprintf(buf, sizeof(buf), format, args);
116 va_end(args);
118 for (cur = statusbar_list; cur != NULL; cur = cur->next)
119 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
122 void statusbar_pop_all(void)
124 GList *cur;
125 gint cid;
127 for (cur = statusbar_list; cur != NULL; cur = cur->next) {
128 cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(cur->data),
129 "Standard Output");
130 gtk_statusbar_pop(GTK_STATUSBAR(cur->data), cid);
134 static gboolean statusbar_puts_all_hook (gpointer source, gpointer data)
136 LogText *logtext = (LogText *) source;
138 cm_return_val_if_fail(logtext != NULL, TRUE);
139 cm_return_val_if_fail(logtext->text != NULL, TRUE);
141 statusbar_pop_all();
142 if (logtext->type == LOG_NORMAL) {
143 statusbar_puts_all(logtext->text + LOG_TIME_LEN);
144 } else if (logtext->type == LOG_MSG) {
145 statusbar_puts_all(logtext->text);
148 return FALSE;
151 void statusbar_verbosity_set(gboolean verbose)
153 if (verbose && (statusbar_puts_all_hook_id == -1)) {
154 statusbar_puts_all_hook_id =
155 hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook, NULL);
156 } else if (!verbose && (statusbar_puts_all_hook_id != -1)) {
157 hooks_unregister_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook_id);
158 statusbar_puts_all_hook_id = -1;
159 statusbar_pop_all();
163 void statusbar_progress_all (gint done, gint total, gint step)
165 GtkProgressBar *progressbar = GTK_PROGRESS_BAR(
166 mainwindow_get_mainwindow()->progressbar);
167 gchar buf[32];
169 if (total && done % step == 0) {
170 #ifdef GENERIC_UMPC
171 /* use a more compact format */
172 const gchar *format = "%d/%d";
173 #else
174 const gchar *format = "%d / %d";
175 #endif
176 g_snprintf(buf, sizeof(buf), format, done, total);
177 gtk_progress_bar_set_text(progressbar, buf);
178 gtk_progress_bar_set_fraction(progressbar,
179 (gfloat)done / (gfloat)total);
180 if (!gtk_widget_get_visible(GTK_WIDGET(progressbar)))
181 gtk_widget_show(GTK_WIDGET(progressbar));
182 } else if (total == 0) {
183 gtk_progress_bar_set_text(progressbar, "");
184 gtk_progress_bar_set_fraction(progressbar, 0.0);
185 gtk_widget_hide(GTK_WIDGET(progressbar));