2005-01-29 [paul] 1.0.0cvs23.2
[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 <gtk/gtk.h>
26 #include <gtk/gtkwindow.h>
27 #include <gtk/gtkvbox.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 "intl.h"
36 #include "progressdialog.h"
37 #include "gtkutils.h"
38 #include "utils.h"
39
40 enum {
41         PROGRESS_IMAGE,
42         PROGRESS_ACCOUNT,
43         PROGRESS_STATE,
44         N_PROGRESS_COLUMNS
45 };
46
47
48 static GtkListStore* progress_dialog_create_data_store(void);
49 static gint progress_dialog_list_view_insert_account(GtkWidget   *list_view,
50                                                      gint         row,
51                                                      const gchar *account,
52                                                      const gchar *status,
53                                                      GdkPixbuf   *image);
54 static GtkWidget *progress_dialog_list_view_create(void);
55 static void progress_dialog_create_list_view_columns(GtkTreeView *list_view);
56
57 ProgressDialog *progress_dialog_create(void)
58 {
59         ProgressDialog *progress;
60         GtkWidget *window;
61         GtkWidget *vbox;
62         GtkWidget *hbox;
63         GtkWidget *label;
64         GtkWidget *cancel_btn;
65         GtkWidget *cancel_area;
66         GtkWidget *progressbar;
67         GtkWidget *scrolledwin;
68         GtkWidget *clist;
69         GtkWidget *list_view;
70         gchar *text[] = {NULL, NULL, NULL};
71
72         text[1] = _("Account");
73         text[2] = _("Status");
74
75         debug_print("Creating progress dialog...\n");
76         progress = g_new0(ProgressDialog, 1);
77
78         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
79         gtk_widget_set_usize(window, 460, -1);
80         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
81         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
82         gtk_widget_realize(window);
83
84         vbox = gtk_vbox_new(FALSE, 8);
85         gtk_container_add(GTK_CONTAINER(window), vbox);
86         gtk_widget_show(vbox);
87
88         hbox = gtk_hbox_new(FALSE, 0);
89         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 8);
90         gtk_widget_show(hbox);
91
92         label = gtk_label_new("");
93         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 8);
94         gtk_widget_show(label);
95
96         gtkut_stock_button_set_create
97                 (&cancel_area, &cancel_btn, GTK_STOCK_CANCEL,
98                  NULL, NULL, NULL, NULL);
99         gtk_box_pack_end(GTK_BOX(vbox), cancel_area, FALSE, FALSE, 0);
100         gtk_widget_grab_default(cancel_btn);
101         gtk_widget_show_all(cancel_area);
102
103         progressbar = gtk_progress_bar_new();
104         gtk_box_pack_start(GTK_BOX(vbox), progressbar, FALSE, FALSE, 0);
105         gtk_widget_show(progressbar);
106
107         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
108         gtk_widget_show(scrolledwin);
109         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, 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_usize(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_usize(list_view, -1, 120);
130
131         progress->window      = window;
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