b029df024317ac76c5ffe07c698670a9a59bbc99
[claws.git] / src / gtk / progressdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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 <glib/gi18n.h>
26 #include <gtk/gtkdialog.h>
27 #include <gtk/gtkhbox.h>
28 #include <gtk/gtklabel.h>
29 #include <gtk/gtkprogressbar.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtkliststore.h>
32 #include <gtk/gtktreeview.h>
33 #include <gtk/gtktreeselection.h>
34 #include <gtk/gtkcellrendererpixbuf.h>
35 #include <gtk/gtkcellrenderertext.h>
36 #include <gtk/gtkbutton.h>
37 #include <gtk/gtkstock.h>
38
39 #include "progressdialog.h"
40 #include "gtkutils.h"
41 #include "utils.h"
42
43 ProgressDialog *progress_dialog_create(void)
44 {
45         ProgressDialog *progress;
46         GtkWidget *dialog;
47         GtkWidget *hbox;
48         GtkWidget *label;
49         GtkWidget *cancel_btn;
50         GtkWidget *progressbar;
51         GtkWidget *scrolledwin;
52         GtkWidget *treeview;
53         GtkListStore *store;
54         GtkTreeSelection *selection;
55         GtkTreeViewColumn *column;
56         GtkCellRenderer *renderer;
57
58         debug_print("Creating progress dialog...\n");
59         progress = g_new0(ProgressDialog, 1);
60
61         dialog = gtk_dialog_new();
62         gtk_widget_set_size_request(dialog, 460, -1);
63         gtk_container_set_border_width(GTK_CONTAINER(dialog), 8);
64         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, TRUE);
65         gtk_widget_realize(dialog);
66
67         gtk_container_set_border_width
68                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 0);
69         gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 8);
70         gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
71
72         hbox = gtk_hbox_new(FALSE, 0);
73         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox,
74                            FALSE, FALSE, 8);
75         gtk_widget_show(hbox);
76
77         label = gtk_label_new("");
78         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
79         gtk_widget_show(label);
80
81         cancel_btn = gtk_dialog_add_button(GTK_DIALOG(dialog),
82                                            GTK_STOCK_CANCEL,
83                                            GTK_RESPONSE_NONE);
84         gtk_widget_grab_default(cancel_btn);
85         gtk_widget_grab_focus(cancel_btn);
86
87         progressbar = gtk_progress_bar_new();
88         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), progressbar,
89                            FALSE, FALSE, 0);
90         gtk_widget_show(progressbar);
91
92         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
93         gtk_widget_show(scrolledwin);
94         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin,
95                            TRUE, TRUE, 0);
96         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
97                                        GTK_POLICY_AUTOMATIC,
98                                        GTK_POLICY_AUTOMATIC);
99         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
100                                             GTK_SHADOW_IN);
101
102         store = gtk_list_store_new(PROG_N_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING,
103                                    G_TYPE_STRING, G_TYPE_POINTER);
104
105         treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
106         g_object_unref(G_OBJECT(store));
107         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), TRUE);
108         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
109         gtk_widget_show(treeview);
110         gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
111         gtk_widget_set_size_request(treeview, -1, 120);
112
113         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
114         gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
115
116         renderer = gtk_cell_renderer_pixbuf_new();
117         g_object_set(renderer, "xalign", 0.5, NULL);
118         column = gtk_tree_view_column_new_with_attributes
119                 (NULL, renderer, "pixbuf", PROG_COL_PIXBUF, NULL);
120         gtk_tree_view_column_set_alignment(column, 0.5);
121         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
122         gtk_tree_view_column_set_fixed_width(column, 20);
123         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
124
125         renderer = gtk_cell_renderer_text_new();
126         column = gtk_tree_view_column_new_with_attributes
127                 (_("Account"), renderer, "text", PROG_COL_NAME, NULL);
128         gtk_tree_view_column_set_resizable(column, TRUE);
129         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
130         gtk_tree_view_column_set_fixed_width(column, 160);
131         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
132
133         renderer = gtk_cell_renderer_text_new();
134         column = gtk_tree_view_column_new_with_attributes
135                 (_("Status"), renderer, "text", PROG_COL_STATUS, NULL);
136         gtk_tree_view_column_set_resizable(column, TRUE);
137         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
138         gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
139
140         progress->window      = dialog;
141         progress->label       = label;
142         progress->cancel_btn  = cancel_btn;
143         progress->progressbar = progressbar;
144         progress->treeview    = treeview;
145         progress->store       = store;
146
147         return progress;
148 }
149
150 void progress_dialog_destroy(ProgressDialog *progress)
151 {
152         if (progress) {
153                 gtk_widget_destroy(progress->window);
154                 g_free(progress);
155         }
156 }
157
158 void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
159 {
160         gtk_label_set_text(GTK_LABEL(progress->label), str);
161 }
162
163 void progress_dialog_set_value(ProgressDialog *progress, gfloat value)
164 {
165         gtk_progress_set_value(GTK_PROGRESS(progress->progressbar), value);
166 }
167
168 void progress_dialog_set_percentage(ProgressDialog *progress,
169                                     gfloat percentage)
170 {
171         gtk_progress_set_percentage(GTK_PROGRESS(progress->progressbar),
172                                     percentage);
173 }
174
175 void progress_dialog_append(ProgressDialog *progress, GdkPixbuf *pixbuf,
176                             const gchar *name, const gchar *status,
177                             gpointer data)
178 {
179         GtkListStore *store = progress->store;
180         GtkTreeIter iter;
181
182         gtk_list_store_append(store, &iter);
183
184         gtk_list_store_set(store, &iter,
185                            PROG_COL_PIXBUF, pixbuf,
186                            PROG_COL_NAME, name,
187                            PROG_COL_STATUS, status,
188                            PROG_COL_POINTER, data,
189                            -1);
190 }
191
192 void progress_dialog_set_row(ProgressDialog *progress, gint row,
193                              GdkPixbuf *pixbuf, const gchar *name,
194                              const gchar *status, gpointer data)
195 {
196         GtkListStore *store = progress->store;
197         GtkTreeIter iter;
198
199         if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
200                                           &iter, NULL, row)) {
201                 gtk_list_store_set(store, &iter,
202                                    PROG_COL_PIXBUF, pixbuf,
203                                    PROG_COL_NAME, name,
204                                    PROG_COL_STATUS, status,
205                                    PROG_COL_POINTER, data,
206                                    -1);
207         }
208 }
209
210 void progress_dialog_set_row_pixbuf(ProgressDialog *progress, gint row,
211                                     GdkPixbuf *pixbuf)
212 {
213         GtkListStore *store = progress->store;
214         GtkTreeIter iter;
215
216         if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
217                                           &iter, NULL, row)) {
218                 gtk_list_store_set(store, &iter, PROG_COL_PIXBUF, pixbuf, -1);
219         }
220 }
221
222 void progress_dialog_set_row_name(ProgressDialog *progress, gint row,
223                                   const gchar *name)
224 {
225         GtkListStore *store = progress->store;
226         GtkTreeIter iter;
227
228         if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
229                                           &iter, NULL, row)) {
230                 gtk_list_store_set(store, &iter, PROG_COL_NAME, name, -1);
231         }
232 }
233
234 void progress_dialog_set_row_status(ProgressDialog *progress, gint row,
235                                     const gchar *status)
236 {
237         GtkListStore *store = progress->store;
238         GtkTreeIter iter;
239
240         if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
241                                           &iter, NULL, row)) {
242                 gtk_list_store_set(store, &iter, PROG_COL_STATUS, status, -1);
243         }
244 }
245
246 void progress_dialog_scroll_to_row(ProgressDialog *progress, gint row)
247 {
248         GtkTreeModel *model = GTK_TREE_MODEL(progress->store);
249         GtkTreeIter iter;
250         GtkTreePath *path;
251
252         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
253                 return;
254
255         path = gtk_tree_model_get_path(model, &iter);
256         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(progress->treeview),
257                                      path, NULL, FALSE, 0.0, 0.0);
258         gtk_tree_path_free(path);
259 }
260