[gnumeric] GnmSheetSel: new widget



commit 6094259fdd9cd402e1282b2c9d294b5bbaab8a38
Author: Morten Welinder <terra gnome org>
Date:   Mon Apr 9 18:44:30 2018 -0400

    GnmSheetSel: new widget

 src/widgets/Makefile.am        |    2 +
 src/widgets/gnm-sheet-sel.c    |  209 ++++++++++++++++++++++++++++++++++++++++
 src/widgets/gnm-sheet-sel.h    |   48 +++++++++
 src/widgets/gnm-workbook-sel.c |    6 +-
 4 files changed, 262 insertions(+), 3 deletions(-)
---
diff --git a/src/widgets/Makefile.am b/src/widgets/Makefile.am
index 3f04cdb..04e4349 100644
--- a/src/widgets/Makefile.am
+++ b/src/widgets/Makefile.am
@@ -25,6 +25,8 @@ libwidgets_la_SOURCES =                               \
        gnm-filter-combo-view.h                 \
        gnm-validation-combo-view.c             \
        gnm-validation-combo-view.h             \
+       gnm-sheet-sel.c                         \
+       gnm-sheet-sel.h                         \
        gnm-sheet-slicer-combo-view.c           \
        gnm-sheet-slicer-combo-view.h           \
        gnm-so-anchor-mode-chooser.c            \
diff --git a/src/widgets/gnm-sheet-sel.c b/src/widgets/gnm-sheet-sel.c
new file mode 100644
index 0000000..b04db78
--- /dev/null
+++ b/src/widgets/gnm-sheet-sel.c
@@ -0,0 +1,209 @@
+/**
+ * gnm-sheet-sel.c: A selector for sheets.
+ *
+ * Copyright (c) 2018 Morten Welinder
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ **/
+
+#include <gnumeric-config.h>
+#include "gnm-sheet-sel.h"
+#include "gnm-i18n.h"
+#include "sheet.h"
+#include "workbook.h"
+#include "application.h"
+
+#define SHEET_KEY "__sheet"
+
+struct GnmSheetSel_ {
+       GOOptionMenu parent;
+
+       Sheet *sheet;
+};
+
+typedef struct {
+       GOOptionMenuClass parent_klass;
+} GnmSheetSelClass;
+
+enum {
+       PROP_0,
+       PROP_SHEET
+};
+
+static GOOptionMenuClass *gnm_sheet_sel_parent_class;
+
+void
+gnm_sheet_sel_set_sheet (GnmSheetSel *ss, Sheet *sheet)
+{
+       GtkWidget *menu;
+
+       g_return_if_fail (GNM_IS_SHEET_SEL (ss));
+
+       if (sheet == ss->sheet)
+               return;
+
+       menu = go_option_menu_get_menu (&ss->parent);
+       if (menu) {
+               GList *children =
+                       gtk_container_get_children (GTK_CONTAINER (menu));
+               GList *l;
+
+               for (l = children; l; l = l->next) {
+                       GtkMenuItem *item = l->data;
+                       Sheet *this_sheet =
+                               g_object_get_data (G_OBJECT (item), SHEET_KEY);
+                       if (this_sheet == sheet) {
+                               go_option_menu_select_item (&ss->parent, item);
+                               break;
+                       }
+               }
+               g_list_free (children);
+       }
+
+       ss->sheet = sheet;
+
+       g_object_notify (G_OBJECT (ss), "sheet");
+}
+
+Sheet *
+gnm_sheet_sel_get_sheet (GnmSheetSel *ss)
+{
+       g_return_val_if_fail (GNM_IS_SHEET_SEL (ss), NULL);
+       return ss->sheet;
+}
+
+static void
+cb_changed (GOOptionMenu *om, GnmSheetSel *ss)
+{
+       GtkWidget *item = go_option_menu_get_history (om);
+       Sheet *sheet = g_object_get_data (G_OBJECT (item), SHEET_KEY);
+       gnm_sheet_sel_set_sheet (ss, sheet);
+}
+
+static void
+gnm_sheet_sel_init (GnmSheetSel *ss)
+{
+       g_signal_connect (G_OBJECT (&ss->parent), "changed",
+                          G_CALLBACK (cb_changed), ss);
+}
+
+static void
+gnm_sheet_sel_set_property (GObject *object, guint property_id,
+                              const GValue *value, GParamSpec *pspec)
+{
+       GnmSheetSel *ss = (GnmSheetSel *)object;
+
+       switch (property_id) {
+       case PROP_SHEET:
+               gnm_sheet_sel_set_sheet (ss, g_value_get_object (value));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+               break;
+       }
+}
+
+static void
+gnm_sheet_sel_get_property (GObject *object, guint property_id,
+                              GValue *value, GParamSpec *pspec)
+{
+       GnmSheetSel *ss = (GnmSheetSel *)object;
+
+       switch (property_id) {
+       case PROP_SHEET:
+               g_value_set_object (value, ss->sheet);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+               break;
+       }
+}
+
+static void
+gnm_sheet_sel_class_init (GObjectClass *klass)
+{
+       gnm_sheet_sel_parent_class = g_type_class_peek (GO_TYPE_OPTION_MENU);
+
+       klass->set_property     = gnm_sheet_sel_set_property;
+       klass->get_property     = gnm_sheet_sel_get_property;
+
+       g_object_class_install_property
+               (klass, PROP_SHEET,
+                g_param_spec_object ("sheet",
+                                     P_("Sheet"),
+                                     P_("The current sheet"),
+                                     GNM_SHEET_TYPE,
+                                     GSF_PARAM_STATIC | G_PARAM_READWRITE));
+}
+
+
+GSF_CLASS (GnmSheetSel, gnm_sheet_sel,
+          gnm_sheet_sel_class_init, gnm_sheet_sel_init,
+          GO_TYPE_OPTION_MENU)
+
+GtkWidget *
+gnm_sheet_sel_new (void)
+{
+       return g_object_new (GNM_TYPE_SHEET_SEL, NULL);
+}
+
+void
+gnm_sheet_sel_set_sheets (GnmSheetSel *ss, GSList *sheets)
+{
+       GtkMenu *menu;
+       GSList *l;
+
+        menu = GTK_MENU (gtk_menu_new ());
+
+       for (l = sheets; l; l = l->next) {
+               Sheet *sheet = l->data;
+               GtkWidget *item =
+                       gtk_check_menu_item_new_with_label
+                       (sheet->name_unquoted);
+               gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), TRUE);
+               gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), FALSE);
+               g_object_set_data (G_OBJECT (item), SHEET_KEY, sheet);
+               gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+       }
+
+       gtk_widget_show_all (GTK_WIDGET (menu));
+       go_option_menu_set_menu (&ss->parent, GTK_WIDGET (menu));
+
+       if (sheets)
+               gnm_sheet_sel_set_sheet (ss, sheets->data);
+}
+
+static void
+cb_wb_changed (GnmWorkbookSel *wbs,
+              G_GNUC_UNUSED GParamSpec *pspec,
+              GnmSheetSel *ss)
+{
+       Workbook *wb = gnm_workbook_sel_get_workbook (wbs);
+       GSList *sheets = wb ? workbook_sheets (wb) : NULL;
+       // FIXME: sort?
+       gnm_sheet_sel_set_sheets (ss, sheets);
+       if (sheets)
+               gnm_sheet_sel_set_sheet (ss, sheets->data);
+       g_slist_free (sheets);
+}
+
+void
+gnm_sheet_sel_link (GnmSheetSel *ss, GnmWorkbookSel *wbs)
+{
+       g_signal_connect_object
+               (wbs,
+                "notify::workbook", G_CALLBACK (cb_wb_changed),
+                ss, 0);
+       cb_wb_changed (wbs, NULL, ss);
+}
diff --git a/src/widgets/gnm-sheet-sel.h b/src/widgets/gnm-sheet-sel.h
new file mode 100644
index 0000000..9dd2627
--- /dev/null
+++ b/src/widgets/gnm-sheet-sel.h
@@ -0,0 +1,48 @@
+/**
+ * gnm-sheet-sel.h: A selector for sheets.
+ *
+ * Copyright (c) 2018 Morten Welinder
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ **/
+
+#ifndef GNM_SHEET_SEL_H
+#define GNM_SHEET_SEL_H
+
+#include "gnumeric.h"
+#include "widgets/gnm-workbook-sel.h"
+#include <goffice/goffice.h>
+
+G_BEGIN_DECLS
+
+GType gnm_sheet_sel_get_type (void);
+
+#define GNM_TYPE_SHEET_SEL             (gnm_sheet_sel_get_type ())
+#define GNM_SHEET_SEL(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GNM_TYPE_SHEET_SEL, GnmSheetSel))
+#define GNM_IS_SHEET_SEL(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GNM_TYPE_SHEET_SEL))
+
+typedef struct GnmSheetSel_ GnmSheetSel;
+
+GtkWidget *gnm_sheet_sel_new (void);
+
+void       gnm_sheet_sel_set_sheets (GnmSheetSel *ss, GSList *sheets);
+
+void       gnm_sheet_sel_link (GnmSheetSel *ss, GnmWorkbookSel *wbs);
+
+void       gnm_sheet_sel_set_sheet (GnmSheetSel *ss, Sheet *sheet);
+Sheet     *gnm_sheet_sel_get_sheet (GnmSheetSel *ss);
+
+G_END_DECLS
+
+#endif /* GNM_SHEET_SEL_H */
diff --git a/src/widgets/gnm-workbook-sel.c b/src/widgets/gnm-workbook-sel.c
index 2926ed8..8f336bc 100644
--- a/src/widgets/gnm-workbook-sel.c
+++ b/src/widgets/gnm-workbook-sel.c
@@ -49,7 +49,7 @@ gnm_workbook_sel_set_workbook (GnmWorkbookSel *wbs, Workbook *wb)
 
        g_return_if_fail (GNM_IS_WORKBOOK_SEL (wbs));
 
-       if (wb == wbs->wb);
+       if (wb == wbs->wb)
                return;
 
        menu = go_option_menu_get_menu (&wbs->parent);
@@ -151,14 +151,14 @@ gnm_workbook_sel_init (GnmWorkbookSel *wbs)
                g_object_set_data (G_OBJECT (item), WB_KEY, wb);
        }
 
-       g_list_free (wb_list);
-
        gtk_widget_show_all (GTK_WIDGET (menu));
        go_option_menu_set_menu (&wbs->parent, GTK_WIDGET (menu));
 
        if (wb_list)
                gnm_workbook_sel_set_workbook (wbs, wb_list->data);
 
+       g_list_free (wb_list);
+
        g_signal_connect (G_OBJECT (&wbs->parent), "changed",
                           G_CALLBACK (cb_changed), wbs);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]