Add a popup menu to the PsppireDictView object.
authorJohn Darrington <john@darrington.wattle.id.au>
Tue, 17 Mar 2009 11:58:51 +0000 (20:58 +0900)
committerJohn Darrington <john@darrington.wattle.id.au>
Tue, 17 Mar 2009 11:58:51 +0000 (20:58 +0900)
Currently this menu contains only one item, which
lets you choose between labels or values.

src/ui/gui/psppire-conf.c
src/ui/gui/psppire-dictview.c
src/ui/gui/psppire-dictview.h

index d9f6800ffd0d494278e29b0ca8a3dc2304b4f6e2..fae56f46de2806461ad55018664592ce870d76c3 100644 (file)
@@ -181,16 +181,20 @@ psppire_conf_get_boolean (PsppireConf *conf, const gchar *base,
                          const gchar *name, gboolean *value)
 {
   gboolean ok;
+  gboolean b;
   GError *err = NULL;
   conf_read (conf);
-  *value = g_key_file_get_boolean (conf->keyfile,
-                                  base,
-                                  name, &err);
+  b = g_key_file_get_boolean (conf->keyfile,
+                             base,
+                             name, &err);
 
   ok = (err == NULL);
   if ( err != NULL )
     g_error_free (err);
 
+  if (ok)
+    *value = b;
+
   return ok;
 }
 
index 08cb2387726b0d993b805192b3b792c74f664bb2..336092f5abfaf97d20b9a07d55ed193a47dfbe03 100644 (file)
@@ -65,6 +65,9 @@ psppire_dict_view_get_type (void)
 static void
 psppire_dict_view_finalize (GObject *object)
 {
+  PsppireDictView *dict_view = PSPPIRE_DICT_VIEW (object);
+
+  g_object_unref (dict_view->menu);
 }
 
 /* Properties */
@@ -242,8 +245,8 @@ psppire_dict_view_base_init (PsppireDictViewClass *class)
 
 
 static void
-psppire_dict_view_base_finalize(PsppireDictViewClass *class,
-                               gpointer class_data)
+psppire_dict_view_base_finalize (PsppireDictViewClass *class,
+                                gpointer class_data)
 {
 
 }
@@ -282,25 +285,20 @@ var_description_cell_data_func (GtkTreeViewColumn *col,
                                GtkTreeIter *top_iter,
                                gpointer data)
 {
+  PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
   struct variable *var;
   GtkTreeIter iter;
   GtkTreeModel *model;
-  gboolean prefer_labels = FALSE;
-
-  PsppireConf *conf = psppire_conf_new ();
 
-  psppire_conf_get_boolean (conf, "dialog-boxes", "prefer-labels",
-                           &prefer_labels);
 
   dv_get_base_model (top_model, top_iter, &model, &iter);
 
   g_assert (PSPPIRE_IS_DICT (model));
 
-
   gtk_tree_model_get (model,
                      &iter, DICT_TVM_COL_VAR, &var, -1);
 
-  if ( var_has_label (var) && prefer_labels)
+  if ( var_has_label (var) && dv->prefer_labels)
     {
       gchar *text = g_strdup_printf (
                                     "<span stretch=\"condensed\">%s</span>",
@@ -371,7 +369,6 @@ set_tooltip_for_variable (GtkTreeView  *treeview,
                          gboolean    keyboard_mode,
                          GtkTooltip *tooltip,
                          gpointer    user_data)
-
 {
   gint bx, by;
   GtkTreeIter iter;
@@ -407,14 +404,8 @@ set_tooltip_for_variable (GtkTreeView  *treeview,
 
   {
     gchar *tip ;
-    gboolean prefer_labels = FALSE;
 
-    PsppireConf *conf = psppire_conf_new ();
-
-    psppire_conf_get_boolean (conf, "dialog-boxes", "prefer-labels",
-                             &prefer_labels);
-
-    if ( prefer_labels )
+    if ( PSPPIRE_DICT_VIEW (treeview)->prefer_labels )
       tip = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
     else
       tip = pspp_locale_to_utf8 (var_get_label (var), -1, NULL);
@@ -427,30 +418,54 @@ set_tooltip_for_variable (GtkTreeView  *treeview,
   return TRUE;
 }
 
+static gboolean
+show_menu (PsppireDictView *dv, GdkEventButton *event, gpointer data)
+{
+  if (event->button != 3)
+    return FALSE;
+
+  gtk_menu_popup (GTK_MENU (dv->menu), NULL, NULL, NULL, NULL,
+                 event->button, event->time);
+
+  return TRUE;
+}
+
+static void
+toggle_label_preference (GtkCheckMenuItem *checkbox, gpointer data)
+{
+  PsppireDictView *dv = PSPPIRE_DICT_VIEW (data);
+
+  dv->prefer_labels = gtk_check_menu_item_get_active (checkbox);
+
+  gtk_widget_queue_draw (GTK_WIDGET (dv));
+}
 
 static void
 psppire_dict_view_init (PsppireDictView *dict_view)
 {
-  GtkTreeViewColumn *col;
+  GtkTreeViewColumn *col = gtk_tree_view_column_new ();
+
+  GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new ();
+
+  dict_view->prefer_labels = TRUE;
 
-  GtkCellRenderer *renderer;
+  psppire_conf_get_boolean (psppire_conf_new (),
+                           "dialog-boxes", "prefer-labels",
+                           &dict_view->prefer_labels);
 
-  col = gtk_tree_view_column_new ();
   gtk_tree_view_column_set_title (col, _("Variable"));
 
-  renderer = gtk_cell_renderer_pixbuf_new ();
   gtk_tree_view_column_pack_start (col, renderer, FALSE);
 
   gtk_tree_view_column_set_cell_data_func (col, renderer,
                                           var_icon_cell_data_func,
                                           NULL, NULL);
 
-
   renderer = gtk_cell_renderer_text_new ();
   gtk_tree_view_column_pack_start (col, renderer, TRUE);
   gtk_tree_view_column_set_cell_data_func (col, renderer,
                                           var_description_cell_data_func,
-                                          NULL, NULL);
+                                          dict_view, NULL);
 
   g_object_set (renderer, "ellipsize-set", TRUE, NULL);
   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
@@ -466,6 +481,29 @@ psppire_dict_view_init (PsppireDictView *dict_view)
 
   g_signal_connect (dict_view, "query-tooltip",
                    G_CALLBACK (set_tooltip_for_variable), NULL);
+
+  dict_view->menu = gtk_menu_new ();
+
+
+  {
+    GtkWidget *checkbox =
+      gtk_check_menu_item_new_with_label  (_("Prefer variable labels"));
+
+    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (checkbox),
+                                   dict_view->prefer_labels);
+
+    g_signal_connect (checkbox, "toggled",
+                     G_CALLBACK (toggle_label_preference), dict_view);
+
+
+    gtk_menu_shell_append (GTK_MENU_SHELL (dict_view->menu), checkbox);
+
+  }
+
+  gtk_widget_show_all (dict_view->menu);
+
+  g_signal_connect (dict_view, "button-press-event",
+                   G_CALLBACK (show_menu), NULL);
 }
 
 
@@ -492,9 +530,7 @@ psppire_dict_view_get_selected_variable (PsppireDictView *treeview)
 
   if (! gtk_tree_selection_get_selected (selection,
                                         &top_model, &top_iter))
-    {
-      return NULL;
-    }
+    return NULL;
 
   dv_get_base_model (top_model, &top_iter, &model, &iter);
 
index 98000ff58ef59f18dbaa6ecd70a15e98d9ca794a..dd08d78f125ce2f735dc2b10193b4b9c637e5296 100644 (file)
@@ -49,6 +49,8 @@ struct _PsppireDictView
 
   PsppireDict *dict;
   var_predicate_func *predicate;
+  GtkWidget *menu;
+  gboolean prefer_labels;
 };
 
 struct _PsppireDictViewClass