2005-06-15 [paul] 1.9.11cvs70
[claws.git] / src / gtk / progressdialog.c
index b029df0..d922bfd 100644 (file)
 
 #include <glib.h>
 #include <glib/gi18n.h>
+#include <gtk/gtk.h>
 #include <gtk/gtkdialog.h>
 #include <gtk/gtkhbox.h>
 #include <gtk/gtklabel.h>
 #include <gtk/gtkprogressbar.h>
 #include <gtk/gtkscrolledwindow.h>
-#include <gtk/gtkliststore.h>
-#include <gtk/gtktreeview.h>
-#include <gtk/gtktreeselection.h>
-#include <gtk/gtkcellrendererpixbuf.h>
-#include <gtk/gtkcellrenderertext.h>
 #include <gtk/gtkbutton.h>
 #include <gtk/gtkstock.h>
 
 #include "progressdialog.h"
 #include "gtkutils.h"
 #include "utils.h"
+#include "prefs_common.h"
+
+enum {
+       PROGRESS_IMAGE,
+       PROGRESS_ACCOUNT,
+       PROGRESS_STATE,
+       N_PROGRESS_COLUMNS
+};
+
+
+static GtkListStore* progress_dialog_create_data_store(void);
+static gint progress_dialog_list_view_insert_account(GtkWidget   *list_view,
+                                                    gint         row,
+                                                    const gchar *account,
+                                                    const gchar *status,
+                                                    GdkPixbuf   *image);
+static GtkWidget *progress_dialog_list_view_create(void);
+static void progress_dialog_create_list_view_columns(GtkTreeView *list_view);
 
 ProgressDialog *progress_dialog_create(void)
 {
@@ -49,11 +63,12 @@ ProgressDialog *progress_dialog_create(void)
        GtkWidget *cancel_btn;
        GtkWidget *progressbar;
        GtkWidget *scrolledwin;
-       GtkWidget *treeview;
-       GtkListStore *store;
-       GtkTreeSelection *selection;
-       GtkTreeViewColumn *column;
-       GtkCellRenderer *renderer;
+       GtkWidget *clist;
+       GtkWidget *list_view;
+       gchar *text[] = {NULL, NULL, NULL};
+
+       text[1] = _("Account");
+       text[2] = _("Status");
 
        debug_print("Creating progress dialog...\n");
        progress = g_new0(ProgressDialog, 1);
@@ -96,57 +111,51 @@ ProgressDialog *progress_dialog_create(void)
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
                                       GTK_POLICY_AUTOMATIC,
                                       GTK_POLICY_AUTOMATIC);
-       gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
-                                           GTK_SHADOW_IN);
-
-       store = gtk_list_store_new(PROG_N_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING,
-                                  G_TYPE_STRING, G_TYPE_POINTER);
-
-       treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
-       g_object_unref(G_OBJECT(store));
-       gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), TRUE);
-       gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
-       gtk_widget_show(treeview);
-       gtk_container_add(GTK_CONTAINER(scrolledwin), treeview);
-       gtk_widget_set_size_request(treeview, -1, 120);
-
-       selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
-       gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
 
-       renderer = gtk_cell_renderer_pixbuf_new();
-       g_object_set(renderer, "xalign", 0.5, NULL);
-       column = gtk_tree_view_column_new_with_attributes
-               (NULL, renderer, "pixbuf", PROG_COL_PIXBUF, NULL);
-       gtk_tree_view_column_set_alignment(column, 0.5);
-       gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
-       gtk_tree_view_column_set_fixed_width(column, 20);
-       gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
-
-       renderer = gtk_cell_renderer_text_new();
-       column = gtk_tree_view_column_new_with_attributes
-               (_("Account"), renderer, "text", PROG_COL_NAME, NULL);
-       gtk_tree_view_column_set_resizable(column, TRUE);
-       gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
-       gtk_tree_view_column_set_fixed_width(column, 160);
-       gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
-
-       renderer = gtk_cell_renderer_text_new();
-       column = gtk_tree_view_column_new_with_attributes
-               (_("Status"), renderer, "text", PROG_COL_STATUS, NULL);
-       gtk_tree_view_column_set_resizable(column, TRUE);
-       gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
-       gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
+       /* GTK2: we hide the clist, but it is available for migration
+        * purposes. now if there only was a way to catch "set clist 
+        * things"!.. */
+       clist = gtk_clist_new_with_titles(3, text);
+       gtk_widget_hide(clist);
+       /* gtk_container_add(GTK_CONTAINER(scrolledwin), clist); */
+       /* gtk_widget_set_size_request(clist, -1, 120); */
+       gtk_clist_set_column_justification(GTK_CLIST(clist), 0,
+                                          GTK_JUSTIFY_CENTER);
+       gtk_clist_set_column_width(GTK_CLIST(clist), 0, 16);
+       gtk_clist_set_column_width(GTK_CLIST(clist), 1, 160);
+
+       list_view = progress_dialog_list_view_create();
+       gtk_widget_show(list_view);
+       gtk_container_add(GTK_CONTAINER(scrolledwin), list_view);
+       gtk_widget_set_size_request(list_view, -1, 120);
 
        progress->window      = dialog;
        progress->label       = label;
        progress->cancel_btn  = cancel_btn;
        progress->progressbar = progressbar;
-       progress->treeview    = treeview;
-       progress->store       = store;
+       progress->clist       = clist;
+       progress->list_view   = list_view;
 
        return progress;
 }
 
+void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
+{
+       gtk_label_set_text(GTK_LABEL(progress->label), str);
+}
+
+void progress_dialog_get_fraction(ProgressDialog *progress)
+{
+       gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progress->progressbar));
+}
+
+void progress_dialog_set_fraction(ProgressDialog *progress,
+                                 gfloat percentage)
+{
+       gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->progressbar),
+                                     percentage);
+}
+
 void progress_dialog_destroy(ProgressDialog *progress)
 {
        if (progress) {
@@ -155,106 +164,172 @@ void progress_dialog_destroy(ProgressDialog *progress)
        }
 }
 
-void progress_dialog_set_label(ProgressDialog *progress, gchar *str)
+/*!
+ *\return      gint Row where account was set
+ */
+gint progress_dialog_list_set_account(ProgressDialog *progress,
+                                     gint            row,
+                                     const gchar    *account_name)
 {
-       gtk_label_set_text(GTK_LABEL(progress->label), str);
+       return progress_dialog_list_view_insert_account(progress->list_view,
+                                                       row, account_name, NULL, 
+                                                       NULL);
 }
 
-void progress_dialog_set_value(ProgressDialog *progress, gfloat value)
+/*!
+ *\return      gint Row where image was set
+ */
+gint progress_dialog_list_set_image(ProgressDialog *progress,
+                                   gint            row,
+                                   GdkPixbuf      *image)
 {
-       gtk_progress_set_value(GTK_PROGRESS(progress->progressbar), value);
+       return progress_dialog_list_view_insert_account(progress->list_view,
+                                                       row, NULL, NULL, 
+                                                       image);
 }
 
-void progress_dialog_set_percentage(ProgressDialog *progress,
-                                   gfloat percentage)
+/*!
+ *\return      gint Row where status was set
+ */
+gint progress_dialog_list_set_status(ProgressDialog *progress,
+                                    gint            row,
+                                    const gchar    *status)
 {
-       gtk_progress_set_percentage(GTK_PROGRESS(progress->progressbar),
-                                   percentage);
+       return progress_dialog_list_view_insert_account(progress->list_view,
+                                                       row, NULL, status, 
+                                                       NULL);
 }
 
-void progress_dialog_append(ProgressDialog *progress, GdkPixbuf *pixbuf,
-                           const gchar *name, const gchar *status,
-                           gpointer data)
+/*!
+ *\return      gint Row where data were set 
+ */
+gint progress_dialog_list_set(ProgressDialog   *progress,
+                             gint               row,
+                             GdkPixbuf         *image,
+                             const gchar       *account_name,
+                             const gchar       *status)
 {
-       GtkListStore *store = progress->store;
-       GtkTreeIter iter;
-
-       gtk_list_store_append(store, &iter);
-
-       gtk_list_store_set(store, &iter,
-                          PROG_COL_PIXBUF, pixbuf,
-                          PROG_COL_NAME, name,
-                          PROG_COL_STATUS, status,
-                          PROG_COL_POINTER, data,
-                          -1);
+       return progress_dialog_list_view_insert_account(progress->list_view,
+                                                       row,  account_name, 
+                                                       status, image);
 }
 
-void progress_dialog_set_row(ProgressDialog *progress, gint row,
-                            GdkPixbuf *pixbuf, const gchar *name,
-                            const gchar *status, gpointer data)
+/* XXX: maybe scroll into view, but leaving that for someone else to
+ * pickup: I don't have that many accounts... */
+gboolean progress_dialog_list_select_row(ProgressDialog *progress,
+                                        gint            row)
 {
-       GtkListStore *store = progress->store;
+       GtkTreeSelection *selection = gtk_tree_view_get_selection
+                                       (GTK_TREE_VIEW(progress->list_view));
        GtkTreeIter iter;
+       GtkTreeModel *model;
 
-       if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
-                                         &iter, NULL, row)) {
-               gtk_list_store_set(store, &iter,
-                                  PROG_COL_PIXBUF, pixbuf,
-                                  PROG_COL_NAME, name,
-                                  PROG_COL_STATUS, status,
-                                  PROG_COL_POINTER, data,
-                                  -1);
-       }
+       model = gtk_tree_view_get_model(GTK_TREE_VIEW(progress->list_view));
+
+       if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
+               return FALSE;
+
+       gtk_tree_selection_select_iter(selection, &iter);               
+
+       return TRUE;
 }
 
-void progress_dialog_set_row_pixbuf(ProgressDialog *progress, gint row,
-                                   GdkPixbuf *pixbuf)
+static GtkListStore* progress_dialog_create_data_store(void)
 {
-       GtkListStore *store = progress->store;
-       GtkTreeIter iter;
-
-       if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
-                                         &iter, NULL, row)) {
-               gtk_list_store_set(store, &iter, PROG_COL_PIXBUF, pixbuf, -1);
-       }
+       return gtk_list_store_new(N_PROGRESS_COLUMNS,
+                                 GDK_TYPE_PIXBUF,
+                                 G_TYPE_STRING,
+                                 G_TYPE_STRING,        
+                                 -1);
 }
 
-void progress_dialog_set_row_name(ProgressDialog *progress, gint row,
-                                 const gchar *name)
+static gint progress_dialog_list_view_insert_account(GtkWidget   *list_view,
+                                                    gint         row,
+                                                    const gchar *account,
+                                                    const gchar *status,
+                                                    GdkPixbuf   *image)
 {
-       GtkListStore *store = progress->store;
        GtkTreeIter iter;
+       GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
+                                       (GTK_TREE_VIEW(list_view)));
+       gint result = -1;                                       
+       
+       if (account == NULL && status == NULL && image == NULL)
+               return -1;
+
+       /* see if row exists, if not append */
+       if (row < 0 || !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), 
+                                                     &iter, NULL, row)) {
+               result = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(store),
+                                                       NULL);
+               gtk_list_store_append(store, &iter);
+       } else
+               result = row;
+
+       /*
+        * XXX: uhm, when does the iter invalidate? sure not while
+        * just setting a row's column i hope?
+        */
+       
+       if (account)
+               gtk_list_store_set(store, &iter, 
+                                  PROGRESS_ACCOUNT, account,
+                                  -1);
+       if (status)
+               gtk_list_store_set(store, &iter,
+                                  PROGRESS_STATE, status,
+                                  -1);
+       if (image)
+               gtk_list_store_set(store, &iter,
+                                  PROGRESS_IMAGE, image,
+                                  -1);
 
-       if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
-                                         &iter, NULL, row)) {
-               gtk_list_store_set(store, &iter, PROG_COL_NAME, name, -1);
-       }
+       return result;
 }
 
-void progress_dialog_set_row_status(ProgressDialog *progress, gint row,
-                                   const gchar *status)
+static GtkWidget *progress_dialog_list_view_create(void)
 {
-       GtkListStore *store = progress->store;
-       GtkTreeIter iter;
-
-       if (gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store),
-                                         &iter, NULL, row)) {
-               gtk_list_store_set(store, &iter, PROG_COL_STATUS, status, -1);
-       }
+       GtkTreeView *list_view;
+       GtkTreeModel *model;
+
+       model = GTK_TREE_MODEL(progress_dialog_create_data_store());
+       list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
+       g_object_unref(model);  
+       
+       gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
+       
+       /* create the columns */
+       progress_dialog_create_list_view_columns(list_view);
+
+       return GTK_WIDGET(list_view);
 }
 
-void progress_dialog_scroll_to_row(ProgressDialog *progress, gint row)
+static void progress_dialog_create_list_view_columns(GtkTreeView *list_view)
 {
-       GtkTreeModel *model = GTK_TREE_MODEL(progress->store);
-       GtkTreeIter iter;
-       GtkTreePath *path;
+       GtkTreeViewColumn *column;
+       GtkCellRenderer *renderer;
 
-       if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
-               return;
+       renderer = gtk_cell_renderer_pixbuf_new();
+       column = gtk_tree_view_column_new_with_attributes
+                       ("", renderer, 
+                        "pixbuf", PROGRESS_IMAGE,
+                        NULL);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);                   
+
+       renderer = gtk_cell_renderer_text_new();
+       column = gtk_tree_view_column_new_with_attributes
+               (_("Account"),
+                renderer,
+                "text", PROGRESS_ACCOUNT,
+                NULL);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
 
-       path = gtk_tree_model_get_path(model, &iter);
-       gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(progress->treeview),
-                                    path, NULL, FALSE, 0.0, 0.0);
-       gtk_tree_path_free(path);
+       renderer = gtk_cell_renderer_text_new();
+       column = gtk_tree_view_column_new_with_attributes
+               (_("Status"),
+                renderer,
+                "text", PROGRESS_STATE,
+                NULL);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
 }