626e9171bf52589d34c25403acb4c965525a751f
[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/gtk.h>
27 #include <gtk/gtkdialog.h>
28 #include <gtk/gtkhbox.h>
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkprogressbar.h>
31 #include <gtk/gtkscrolledwindow.h>
32 #include <gtk/gtkbutton.h>
33 #include <gtk/gtkstock.h>
34
35 #include "progressdialog.h"
36 #include "gtkutils.h"
37 #include "utils.h"
38
39 enum {
40         PROGRESS_IMAGE,
41         PROGRESS_ACCOUNT,
42         PROGRESS_STATE,
43         N_PROGRESS_COLUMNS
44 };
45
46
47 static GtkListStore* progress_dialog_create_data_store(void);
48 static gint progress_dialog_list_view_insert_account(GtkWidget   *list_view,
49                                                      gint         row,
50                                                      const gchar *account,
51                                                      const gchar *status,
52                                                      GdkPixbuf   *image);
53 static GtkWidget *progress_dialog_list_view_create(void);
54 static void progress_dialog_create_list_view_columns(GtkTreeView *list_view);
55
56 ProgressDialog *progress_dialog_create(void)
57 {
58         ProgressDialog *progress;
59         GtkWidget *dialog;
60         GtkWidget *hbox;
61         GtkWidget *label;
62         GtkWidget *cancel_btn;
63         GtkWidget *progressbar;
64         GtkWidget *scrolledwin;
65         GtkWidget *clist;
66         GtkWidget *list_view;
67         gchar *text[] = {NULL, NULL, NULL};
68
69         text[1] = _("Account");
70         text[2] = _("Status");
71
72         debug_print("Creating progress dialog...\n");
73         progress = g_new0(ProgressDialog, 1);
74
75         dialog = gtk_dialog_new();
76         gtk_widget_set_size_request(dialog, 460, -1);
77         gtk_container_set_border_width(GTK_CONTAINER(dialog), 8);
78         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, TRUE);
79         gtk_widget_realize(dialog);
80
81         gtk_container_set_border_width
82                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 0);
83         gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 8);
84         gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
85
86         hbox = gtk_hbox_new(FALSE, 0);
87         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox,
88                            FALSE, FALSE, 8);
89         gtk_widget_show(hbox);
90
91         label = gtk_label_new("");
92         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
93         gtk_widget_show(label);
94
95         cancel_btn = gtk_dialog_add_button(GTK_DIALOG(dialog),
96                                            GTK_STOCK_CANCEL,
97                                            GTK_RESPONSE_NONE);
98         gtk_widget_grab_default(cancel_btn);
99         gtk_widget_grab_focus(cancel_btn);
100
101         progressbar = gtk_progress_bar_new();
102         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), progressbar,
103                            FALSE, FALSE, 0);
104         gtk_widget_show(progressbar);
105
106         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
107         gtk_widget_show(scrolledwin);
108         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin,
109                            TRUE, TRUE, 0);
110         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
111                                        GTK_POLICY_AUTOMATIC,
112                                        GTK_POLICY_AUTOMATIC);
113
114         /* GTK2: we hide the clist, but it is available for migration
115          * purposes. now if there only was a way to catch "set clist 
116          * things"!.. */
117         clist = gtk_clist_new_with_titles(3, text);
118         gtk_widget_hide(clist);
119         /* gtk_container_add(GTK_CONTAINER(scrolledwin), clist); */
120         /* gtk_widget_set_size_request(clist, -1, 120); */
121         gtk_clist_set_column_justification(GTK_CLIST(clist), 0,
122                                            GTK_JUSTIFY_CENTER);
123         gtk_clist_set_column_width(GTK_CLIST(clist), 0, 16);
124         gtk_clist_set_column_width(GTK_CLIST(clist), 1, 160);
125
126         list_view = progress_dialog_list_view_create();
127         gtk_widget_show(list_view);
128         gtk_container_add(GTK_CONTAINER(scrolledwin), list_view);
129         gtk_widget_set_size_request(list_view, -1, 120);
130
131         progress->window      = dialog;
132         progress->label       = label;
133         progress->cancel_btn  = cancel_btn;
134         progress->progressbar = progressbar;
135         progress->clist       = clist;
136         progress->list_view   = list_view;
137
138         return progress;
139 }
140
141 void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
142 {
143         gtk_label_set_text(GTK_LABEL(progress->label), str);
144 }
145
146 void progress_dialog_get_fraction(ProgressDialog *progress)
147 {
148         gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progress->progressbar));
149 }
150
151 void progress_dialog_set_fraction(ProgressDialog *progress,
152                                   gfloat percentage)
153 {
154         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->progressbar),
155                                       percentage);
156 }
157
158 void progress_dialog_destroy(ProgressDialog *progress)
159 {
160         if (progress) {
161                 gtk_widget_destroy(progress->window);
162                 g_free(progress);
163         }
164 }
165
166 /*!
167  *\return       gint Row where account was set
168  */
169 gint progress_dialog_list_set_account(ProgressDialog *progress,
170                                       gint            row,
171                                       const gchar    *account_name)
172 {
173         return progress_dialog_list_view_insert_account(progress->list_view,
174                                                         row, account_name, NULL, 
175                                                         NULL);
176 }
177
178 /*!
179  *\return       gint Row where image was set
180  */
181 gint progress_dialog_list_set_image(ProgressDialog *progress,
182                                     gint            row,
183                                     GdkPixbuf      *image)
184 {
185         return progress_dialog_list_view_insert_account(progress->list_view,
186                                                         row, NULL, NULL, 
187                                                         image);
188 }
189
190 /*!
191  *\return       gint Row where status was set
192  */
193 gint progress_dialog_list_set_status(ProgressDialog *progress,
194                                      gint            row,
195                                      const gchar    *status)
196 {
197         return progress_dialog_list_view_insert_account(progress->list_view,
198                                                         row, NULL, status, 
199                                                         NULL);
200 }
201
202 /*!
203  *\return       gint Row where data were set 
204  */
205 gint progress_dialog_list_set(ProgressDialog    *progress,
206                               gint               row,
207                               GdkPixbuf         *image,
208                               const gchar       *account_name,
209                               const gchar       *status)
210 {
211         return progress_dialog_list_view_insert_account(progress->list_view,
212                                                         row,  account_name, 
213                                                         status, image);
214 }
215
216 /* XXX: maybe scroll into view, but leaving that for someone else to
217  * pickup: I don't have that many accounts... */
218 gboolean progress_dialog_list_select_row(ProgressDialog *progress,
219                                          gint            row)
220 {
221         GtkTreeSelection *selection = gtk_tree_view_get_selection
222                                         (GTK_TREE_VIEW(progress->list_view));
223         GtkTreeIter iter;
224         GtkTreeModel *model;
225
226         model = gtk_tree_view_get_model(GTK_TREE_VIEW(progress->list_view));
227
228         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
229                 return FALSE;
230
231         gtk_tree_selection_select_iter(selection, &iter);               
232
233         return TRUE;
234 }
235
236 static GtkListStore* progress_dialog_create_data_store(void)
237 {
238         return gtk_list_store_new(N_PROGRESS_COLUMNS,
239                                   GDK_TYPE_PIXBUF,
240                                   G_TYPE_STRING,
241                                   G_TYPE_STRING,        
242                                   -1);
243 }
244
245 static gint progress_dialog_list_view_insert_account(GtkWidget   *list_view,
246                                                      gint         row,
247                                                      const gchar *account,
248                                                      const gchar *status,
249                                                      GdkPixbuf   *image)
250 {
251         GtkTreeIter iter;
252         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
253                                         (GTK_TREE_VIEW(list_view)));
254         gint result = -1;                                       
255         
256         if (account == NULL && status == NULL && image == NULL)
257                 return -1;
258
259         /* see if row exists, if not append */
260         if (row < 0 || !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), 
261                                                       &iter, NULL, row)) {
262                 result = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store),
263                                                         NULL);
264                 gtk_list_store_append(store, &iter);
265         } else
266                 result = row;
267
268         /*
269          * XXX: uhm, when does the iter invalidate? sure not while
270          * just setting a row's column i hope?
271          */
272         
273         if (account)
274                 gtk_list_store_set(store, &iter, 
275                                    PROGRESS_ACCOUNT, account,
276                                    -1);
277         if (status)
278                 gtk_list_store_set(store, &iter,
279                                    PROGRESS_STATE, status,
280                                    -1);
281         if (image)
282                 gtk_list_store_set(store, &iter,
283                                    PROGRESS_IMAGE, image,
284                                    -1);
285
286         return result;
287 }
288
289 static GtkWidget *progress_dialog_list_view_create(void)
290 {
291         GtkTreeView *list_view;
292         GtkTreeModel *model;
293
294         model = GTK_TREE_MODEL(progress_dialog_create_data_store());
295         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
296         g_object_unref(model);  
297         
298         gtk_tree_view_set_rules_hint(list_view, TRUE);
299         
300         /* create the columns */
301         progress_dialog_create_list_view_columns(list_view);
302
303         return GTK_WIDGET(list_view);
304 }
305
306 static void progress_dialog_create_list_view_columns(GtkTreeView *list_view)
307 {
308         GtkTreeViewColumn *column;
309         GtkCellRenderer *renderer;
310
311         renderer = gtk_cell_renderer_pixbuf_new();
312         column = gtk_tree_view_column_new_with_attributes
313                         ("", renderer, 
314                          "pixbuf", PROGRESS_IMAGE,
315                          NULL);
316         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);                   
317
318         renderer = gtk_cell_renderer_text_new();
319         column = gtk_tree_view_column_new_with_attributes
320                 (_("Account"),
321                  renderer,
322                  "text", PROGRESS_ACCOUNT,
323                  NULL);
324         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
325
326         renderer = gtk_cell_renderer_text_new();
327         column = gtk_tree_view_column_new_with_attributes
328                 (_("Status"),
329                  renderer,
330                  "text", PROGRESS_STATE,
331                  NULL);
332         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
333 }
334