Fix error creation and warning
[claws.git] / src / statusbar.c
blobc10c29727b81772512f1c6c07947a25fa65ab691
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and Hiroyuki Yamamoto
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <stdarg.h>
29 #include "mainwindow.h"
30 #include "statusbar.h"
31 #include "gtkutils.h"
32 #include "utils.h"
33 #include "log.h"
34 #include "hooks.h"
36 #define BUFFSIZE 1024
38 static GList *statusbar_list = NULL;
39 static gulong statusbar_puts_all_hook_id = HOOK_NONE;
41 GtkWidget *statusbar_create(void)
43 GtkWidget *statusbar;
44 GtkWidget *child;
45 GtkWidget *parent;
46 GtkWidget *hbox;
48 statusbar = gtk_statusbar_new();
50 gtk_widget_set_margin_top(GTK_WIDGET(statusbar), 0);
51 gtk_widget_set_margin_bottom(GTK_WIDGET(statusbar), 0);
52 statusbar_list = g_list_append(statusbar_list, statusbar);
53 gtk_container_set_border_width(GTK_CONTAINER(statusbar), 1);
54 child = gtk_statusbar_get_message_area(GTK_STATUSBAR(statusbar));
55 gtk_widget_set_margin_top(GTK_WIDGET(child), 0);
56 gtk_widget_set_margin_bottom(GTK_WIDGET(child), 0);
57 gtk_widget_set_margin_start(GTK_WIDGET(child), 0);
58 gtk_widget_set_margin_end(GTK_WIDGET(child), 0);
59 parent = gtk_widget_get_parent(child);
60 gtk_container_remove(GTK_CONTAINER(parent), g_object_ref(child));
61 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
62 gtk_container_add(GTK_CONTAINER(parent), hbox);
63 gtk_widget_show(hbox);
64 gtk_box_pack_start(GTK_BOX(hbox), child, TRUE, TRUE, 0);
65 g_object_unref(child);
67 return statusbar;
70 void statusbar_puts(GtkStatusbar *statusbar, const gchar *str)
72 gint cid;
73 gchar *buf;
74 gchar *tmp;
76 tmp = g_strdup(str);
77 strretchomp(tmp);
78 buf = trim_string(tmp, 76);
79 g_free(tmp);
81 cid = gtk_statusbar_get_context_id(statusbar, "Standard Output");
82 gtk_statusbar_pop(statusbar, cid);
83 gtk_statusbar_push(statusbar, cid, buf);
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_all(const gchar *format, ...)
98 va_list args;
99 gchar buf[BUFFSIZE];
100 GList *cur;
102 va_start(args, format);
103 g_vsnprintf(buf, sizeof(buf), format, args);
104 va_end(args);
106 for (cur = statusbar_list; cur != NULL; cur = cur->next)
107 statusbar_puts(GTK_STATUSBAR(cur->data), buf);
110 void statusbar_pop_all(void)
112 GList *cur;
113 gint cid;
115 for (cur = statusbar_list; cur != NULL; cur = cur->next) {
116 cid = gtk_statusbar_get_context_id(GTK_STATUSBAR(cur->data),
117 "Standard Output");
118 gtk_statusbar_pop(GTK_STATUSBAR(cur->data), cid);
122 static gboolean statusbar_puts_all_hook (gpointer source, gpointer data)
124 LogText *logtext = (LogText *) source;
126 cm_return_val_if_fail(logtext != NULL, TRUE);
127 cm_return_val_if_fail(logtext->text != NULL, TRUE);
129 statusbar_pop_all();
130 if (logtext->type == LOG_NORMAL) {
131 statusbar_puts_all(logtext->text + LOG_TIME_LEN);
132 } else if (logtext->type == LOG_MSG) {
133 statusbar_puts_all(logtext->text);
136 return FALSE;
139 void statusbar_verbosity_set(gboolean verbose)
141 if (verbose && (statusbar_puts_all_hook_id == HOOK_NONE)) {
142 statusbar_puts_all_hook_id =
143 hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook, NULL);
144 } else if (!verbose && (statusbar_puts_all_hook_id != HOOK_NONE)) {
145 hooks_unregister_hook(LOG_APPEND_TEXT_HOOKLIST, statusbar_puts_all_hook_id);
146 statusbar_puts_all_hook_id = HOOK_NONE;
147 statusbar_pop_all();
151 void statusbar_progress_all (gint done, gint total, gint step)
153 GtkProgressBar *progressbar = GTK_PROGRESS_BAR(
154 mainwindow_get_mainwindow()->progressbar);
155 gchar buf[32];
157 if (total && done % step == 0) {
158 #ifdef GENERIC_UMPC
159 /* use a more compact format */
160 const gchar *format = "%d/%d";
161 #else
162 const gchar *format = "%d / %d";
163 #endif
164 g_snprintf(buf, sizeof(buf), format, done, total);
165 gtk_progress_bar_set_show_text(progressbar, TRUE);
166 gtk_progress_bar_set_text(progressbar, buf);
167 gtk_progress_bar_set_fraction(progressbar,
168 (gfloat)done / (gfloat)total);
169 if (!gtk_widget_get_visible(GTK_WIDGET(progressbar)))
170 gtk_widget_show(GTK_WIDGET(progressbar));
171 } else if (total == 0) {
172 gtk_progress_bar_set_text(progressbar, "");
173 gtk_progress_bar_set_fraction(progressbar, 0.0);
174 gtk_widget_hide(GTK_WIDGET(progressbar));