gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r19653 - gnunet-gtk/src/fs


From: gnunet
Subject: [GNUnet-SVN] r19653 - gnunet-gtk/src/fs
Date: Thu, 2 Feb 2012 20:37:37 +0100

Author: grothoff
Date: 2012-02-02 20:37:37 +0100 (Thu, 02 Feb 2012)
New Revision: 19653

Modified:
   gnunet-gtk/src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c
Log:
-even more code cleanup

Modified: gnunet-gtk/src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c    
2012-02-02 19:00:24 UTC (rev 19652)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c    
2012-02-02 19:37:37 UTC (rev 19653)
@@ -27,13 +27,27 @@
 #include "gnunet-fs-gtk.h"
 
 /**
+ * How long until we automatically hide the drop-down if the cursor is outside 
the bounds?
+ */
+#define AUTO_HIDE_TIMEOUT_MS 100
+
+
+/**
+ * ID of a timeout task which we schedule to close the drop-down automatically
+ * if the mouse leaves the area for a while.  0 for no such task.
  *
+ * FIXME: this task is not cancelled if the main window is closed while
+ *        the drop-down is down.
  */
 static guint namespace_selector_window_leave_timeout_source;
 
 
 /**
+ * The mouse has re-entered the dropdown window.  Stop the 
+ * timeout task that would hide the dropdown.
  *
+ * @param widget the dropdown widget
+ * @param event the mouse-enter event
  * @param user_data the builder for the main window
  */
 gboolean
@@ -43,13 +57,16 @@
 {
   if (namespace_selector_window_leave_timeout_source > 0)
     g_source_remove (namespace_selector_window_leave_timeout_source);
+  namespace_selector_window_leave_timeout_source = 0;
   return FALSE;
 }
 
 
 /**
+ * Run when the timeout has expired.  Hides the drop down window.
  *
- * @param user_data the builder for the main window
+ * @param user_data the toggle button which we will use to hide the dropdown
+ * @return FALSE
  */
 static gboolean
 namespace_selector_window_leave_timeout_cb (gpointer user_data)
@@ -57,12 +74,16 @@
   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (user_data);
 
   /* This will eventually hide the namespace selector */
+  namespace_selector_window_leave_timeout_source = 0;
   gtk_toggle_button_set_active (toggle_button, FALSE);
   return FALSE;
 }
 
 
 /**
+ * The cursor has left the window.  Place a timeout to hide the
+ * window. It will be cancelled if the cursor re-enters the namespace
+ * selector window or the toggle button within 100ms
  *
  * @param user_data the builder for the main window
  */
@@ -71,230 +92,166 @@
                                                                      GdkEvent 
* event,
                                                                      gpointer 
user_data)
 {
-  GtkBuilder *builder;
+  GtkBuilder *builder = GTK_BUILDER (user_data);
   GtkToggleButton *toggle_button;
-  guint timeout_id;
 
-  builder = GTK_BUILDER (user_data);
-
   toggle_button =
       GTK_TOGGLE_BUTTON (gtk_builder_get_object
                          (builder,
                           "main_window_search_namespace_dropdown_button"));
-
-  /* Place a timeout to hide the window. It will be cancelled if the cursor
-   * enters the namespace selector window or the toggle button within 100ms.
-   */
-  timeout_id =
-      g_timeout_add (100, &namespace_selector_window_leave_timeout_cb,
-                     toggle_button);
   if (namespace_selector_window_leave_timeout_source > 0)
     g_source_remove (namespace_selector_window_leave_timeout_source);
-  namespace_selector_window_leave_timeout_source = timeout_id;
-
+  namespace_selector_window_leave_timeout_source 
+    = g_timeout_add (AUTO_HIDE_TIMEOUT_MS, 
+                    &namespace_selector_window_leave_timeout_cb,
+                    toggle_button);
   return FALSE;
 }
 
 
 /**
- *
+ * Given a tree view, find out which row is currently selected.
+ * 
+ * @param tree a tree view instance
+ * @return a reference to the currently selected row, or NULL for none
  */
-static gboolean
-get_selected_namespace_treepath_iter_model_widget (GtkBuilder * builder,
-                                                   GtkTreePath ** p_treepath,
-                                                   GtkTreeIter * p_iter,
-                                                   GtkTreeModel ** p_model,
-                                                   GtkWidget ** p_widget)
+static GtkTreeRowReference *
+get_selected_row_from_treeview (GtkTreeView * tree)
 {
-  GtkTreeSelection *selection;
+  GtkTreeSelection *sel;
   GtkTreeModel *model;
-  GList *selected;
-  GtkTreePath *treepath;
-  GtkWidget *widget;
+  GtkTreePath *path;
+  GtkTreeIter iter;
+  GtkTreeRowReference *ref;
 
-  widget =
-      GTK_WIDGET (gtk_builder_get_object
-                  (builder, "namespace_selector_treeview"));
-  if (!widget)
-    return FALSE;
-
-  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
-  model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
-  if (!selection || !model)
-    return FALSE;
-
-  selected = gtk_tree_selection_get_selected_rows (selection, NULL);
-  if (!selected)
-    return FALSE;
-  if (selected->data == NULL)
-  {
-    g_list_foreach (selected, (GFunc) gtk_tree_path_free, NULL);
-    g_list_free (selected);
-    return FALSE;
-  }
-  /* Free everything except the first path, keep it */
-  treepath = (GtkTreePath *) selected->data;
-  selected = g_list_remove (selected, selected->data);
-  g_list_foreach (selected, (GFunc) gtk_tree_path_free, NULL);
-  g_list_free (selected);
-
-  if (p_iter && !gtk_tree_model_get_iter (model, p_iter, treepath))
-  {
-    gtk_tree_path_free (treepath);
-    return FALSE;
-  }
-  *p_treepath = treepath;
-  if (p_model)
-    *p_model = model;
-  if (p_widget)
-    *p_widget = widget;
-  return TRUE;
+  sel = gtk_tree_view_get_selection (tree);
+  if (! gtk_tree_selection_get_selected (sel, &model, &iter))
+    return NULL;
+  path = gtk_tree_model_get_path (model, &iter);
+  ref = gtk_tree_row_reference_new (model, path);  
+  gtk_tree_path_free (path);
+  return ref;
 }
 
 
 /**
+ * Changes were made to the selected entry in the tree view and the
+ * user clicked to confirm.  Hide the drop down and display the
+ * selected entry as the new namespace label.
  *
- * @param user_data the builder for the main window
+ * @param builder the builder for the main window
+ * @param tv the tree view that was updated
  */
 static void
-namespace_selector_treeview_cursor_changed_cb (GtkWidget * widget,
-                                               gpointer user_data)
+commit_changes (GtkBuilder *builder,
+               GtkTreeView *tv)
 {
-  GtkBuilder *builder  = GTK_BUILDER (user_data);
   GtkToggleButton *toggle_button;
-  GtkLabel *sel_namespace_label;
-  GtkTreeModel *model;
-  gchar *value;
+  GtkTreeRowReference *ref;
   GtkTreePath *treepath;
-  GtkEntry *search_entry;
-  GtkTreeRowReference *ref, *old;
-
+  gchar *value;
+    
   toggle_button =
-      GTK_TOGGLE_BUTTON (gtk_builder_get_object
-                         (builder,
-                          "main_window_search_namespace_dropdown_button"));
-  if (!toggle_button)
-    return;
-
-  search_entry =
-      GTK_ENTRY (gtk_builder_get_object (builder, "main_window_search_entry"));
-  if (!search_entry)
-    return;
-
-  if (!get_selected_namespace_treepath_iter_model_widget
-      (builder, &treepath, NULL, &model, NULL))
-    return;
-  ref = gtk_tree_row_reference_new (model, treepath);
-  old = g_object_get_data (G_OBJECT (toggle_button), "selected-row-reference");
-  if (old)
-    gtk_tree_row_reference_free (old);
+    GTK_TOGGLE_BUTTON (gtk_builder_get_object
+                      (builder,
+                       "main_window_search_namespace_dropdown_button"));
+  ref = g_object_get_data (G_OBJECT (toggle_button), "selected-row-reference");
+  if (NULL != ref)
+    gtk_tree_row_reference_free (ref);
+  ref = get_selected_row_from_treeview (tv);
   g_object_set_data (G_OBJECT (toggle_button), "selected-row-reference", ref);
 
+  treepath = gtk_tree_row_reference_get_path (ref);
+  if (GNUNET_GTK_get_tree_string (tv, treepath, 0, &value))
+  {
+    GtkLabel *sel_namespace_label;
 
-  sel_namespace_label =
+    sel_namespace_label =
       GTK_LABEL (gtk_builder_get_object
                  (builder, "main_window_search_selected_namespace_label"));
-  if (!sel_namespace_label)
-    return;
-
-  if (GNUNET_GTK_get_tree_string (GTK_TREE_VIEW (widget), treepath, 0, &value)
-      && value != NULL)
-  {
-    gtk_label_set_text (sel_namespace_label, value);
+    gtk_label_set_text (sel_namespace_label, (NULL != value) ? value : "");
     g_free (value);
   }
-  if (GNUNET_GTK_get_tree_string (GTK_TREE_VIEW (widget), treepath, 2, &value)
-      && value != NULL)
+  if (GNUNET_GTK_get_tree_string (tv, treepath, 2, &value))
   {
-    gtk_entry_set_text (search_entry, value);
+    GtkEntry *search_entry;
+
+    search_entry = GTK_ENTRY (gtk_builder_get_object (builder, 
"main_window_search_entry"));
+    gtk_entry_set_text (search_entry, (NULL != value) ? value : "");
     g_free (value);
   }
-
   gtk_tree_path_free (treepath);
 
-  /* This will eventually hide the namespace selector */
-  gtk_toggle_button_set_active (toggle_button, FALSE);
+  /* hide the namespace selector */
+  gtk_toggle_button_set_active (toggle_button, FALSE);  
 }
 
 
 /**
+ * User pushed the button in the treeview.  Get the selected entry
+ * and remember it in the "pushed-rowreference" of the widget.
+ * This way, we can use it when the button is released.
  *
- */
-static GtkTreeRowReference *
-get_ns_selected_row (GtkTreeView * tree)
-{
-  GtkTreeSelection *sel;
-  GList *rows, *row;
-  GtkTreeModel *model;
-  GtkTreeRowReference *ref = NULL;
-
-  sel = gtk_tree_view_get_selection (tree);
-  rows = gtk_tree_selection_get_selected_rows (sel, &model);
-  for (row = rows; row; row = row->next)
-  {
-    ref = gtk_tree_row_reference_new (model, row->data);
-    if (ref != NULL)
-      break;
-  }
-  g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
-  g_list_free (rows);
-  return ref;
-}
-
-
-/**
- *
+ * @param widget the tree view widget
+ * @param event the push event
  * @param user_data the builder for the main window
+ * @return FALSE
  */
 gboolean
 GNUNET_FS_GTK_namespace_selector_treeview_button_press_event_cb (GtkWidget * 
widget,
                                                                 GdkEvent * 
event,
                                                                 gpointer 
user_data)
 {
-  GtkTreeRowReference *ref = NULL;
+  GtkTreeRowReference *ref;
+  gpointer old = g_object_get_data (G_OBJECT (widget), "pushed-rowreference");
 
-  ref = get_ns_selected_row (GTK_TREE_VIEW (widget));
-  if (ref != NULL)
-  {
-    gpointer old = g_object_get_data (G_OBJECT (widget), 
"pushed-rowreference");
-
-    if (old)
+  if (NULL != old)
       gtk_tree_row_reference_free (old);
-    g_object_set_data (G_OBJECT (widget), "pushed-rowreference", ref);
-  }
+  ref = get_selected_row_from_treeview (GTK_TREE_VIEW (widget));
+  g_object_set_data (G_OBJECT (widget), "pushed-rowreference", ref);
   return FALSE;
 }
 
 
 /**
+ * User released the button in the treeview.  Get the selected entry
+ * and update the cursor accordingly, but only if the user pushed the
+ * button down and released it in the same row.  We have stored the
+ * row that the user selected when pushing the button down in the
+ * "pushed-rowreference" of the widget.
  *
+ * @param widget the tree view widget
+ * @param event the release event
  * @param user_data the builder for the main window
+ * @return FALSE
  */
 gboolean
 GNUNET_FS_GTK_namespace_selector_treeview_button_release_event_cb (GtkWidget * 
widget,
                                                                   GdkEvent * 
event,
                                                                   gpointer 
user_data)
 {
-  GtkTreeRowReference *ref = NULL;
+  GtkBuilder *builder = GTK_BUILDER (user_data);
+  GtkTreeRowReference *ref;
   gpointer old = g_object_get_data (G_OBJECT (widget), "pushed-rowreference");
 
-  ref = get_ns_selected_row (GTK_TREE_VIEW (widget));
-  if (ref && old)
+  ref = get_selected_row_from_treeview (GTK_TREE_VIEW (widget));
+  if ( (NULL != ref) && (NULL != old))
   {
-    GtkTreePath *path_ref, *path_old;
+    GtkTreePath *path_ref;
+    GtkTreePath *path_old;
 
     path_ref = gtk_tree_row_reference_get_path (ref);
     path_old = gtk_tree_row_reference_get_path (old);
-    if (gtk_tree_path_compare (path_ref, path_old) == 0)
-      namespace_selector_treeview_cursor_changed_cb (widget, user_data);
+    if (0 == gtk_tree_path_compare (path_ref, path_old))
+      commit_changes (builder, GTK_TREE_VIEW (widget));
     if (path_ref)
       gtk_tree_path_free (path_ref);
     if (path_old)
       gtk_tree_path_free (path_old);
   }
-  if (ref)
+  if (NULL != ref)
     gtk_tree_row_reference_free (ref);
-  if (old)
+  if (NULL != old)
     gtk_tree_row_reference_free (old);
   g_object_set_data (G_OBJECT (widget), "pushed-rowreference", NULL);
   return FALSE;
@@ -302,6 +259,59 @@
 
 
 /**
+ * The toggle button that changes the visibility of the namespace dropdown
+ * list was toggled.
+ *
+ * @param togglebutton the button that toggles the namespace dropdown list
+ * @param user_data the builder for the main window
+ */
+void
+GNUNET_FS_GTK_search_namespace_dropdown_button_toggled_cb (GtkToggleButton *
+                                                          togglebutton,
+                                                          gpointer user_data)
+{
+  GtkBuilder *builder = GTK_BUILDER (user_data);
+  gboolean active;
+  GtkWidget *namespace_selector_window;
+  GtkWidget *namespace_selector_treeview;
+  GtkAllocation togglebutton_allocation;
+  GdkWindow *main_window_gdk;
+  gint mwg_x;
+  gint mwg_y;
+  gint tgb_x;
+  gint tgb_y;
+  gint popup_x;
+  gint popup_y;
+
+  namespace_selector_window =
+      GTK_WIDGET (gtk_builder_get_object
+                  (builder, "namespace_selector_window"));  
+  g_object_get (G_OBJECT (togglebutton), "active", &active, NULL);
+  if (! active)
+  {
+    gtk_widget_hide (namespace_selector_window);
+    gtk_widget_grab_focus (GTK_WIDGET (togglebutton));
+    return;
+  }
+  namespace_selector_treeview =
+      GTK_WIDGET (gtk_builder_get_object
+                  (builder, "namespace_selector_treeview"));
+  gtk_widget_get_allocation (GTK_WIDGET (togglebutton),
+                            &togglebutton_allocation);
+  main_window_gdk = gtk_widget_get_window (GTK_WIDGET (togglebutton));
+  gdk_window_get_origin (main_window_gdk, &mwg_x, &mwg_y);
+  /* show the window below the button */
+  tgb_x = mwg_x + togglebutton_allocation.x;
+  tgb_y = mwg_y + togglebutton_allocation.y;
+  popup_x = tgb_x;
+  popup_y = tgb_y + togglebutton_allocation.height;  
+  gtk_window_move (GTK_WINDOW (namespace_selector_window), popup_x, popup_y);
+  gtk_widget_show_all (namespace_selector_window);
+  gtk_widget_grab_focus (namespace_selector_treeview);
+}
+
+
+/**
  * Add pseudonym data to tree store
  *
  * @param cls closure (the 'GtkListStore')
@@ -379,68 +389,7 @@
 }
 
 
-
-
 /**
- * The toggle button that changes the visibility of the namespace dropdown
- * list was toggled.
- *
- * @param togglebutton the button that toggles the namespace dropdown list
- * @param user_data the builder for the main window
- */
-void
-GNUNET_FS_GTK_search_namespace_dropdown_button_toggled_cb (GtkToggleButton *
-                                                          togglebutton,
-                                                          gpointer user_data)
-{
-  GtkBuilder *builder = GTK_BUILDER (user_data);
-  gboolean active;
-  GtkWidget *namespace_selector_window;
-  GtkWidget *namespace_selector_treeview;
-  GtkAllocation togglebutton_allocation;
-  GdkWindow *main_window_gdk;
-  gint mwg_x;
-  gint mwg_y;
-  gint tgb_x;
-  gint tgb_y;
-  gint popup_x;
-  gint popup_y;
-
-  namespace_selector_window =
-      GTK_WIDGET (gtk_builder_get_object
-                  (builder, "namespace_selector_window"));  
-  g_object_get (G_OBJECT (togglebutton), "active", &active, NULL);
-  if (! active)
-  {
-    gtk_widget_hide (namespace_selector_window);
-    gtk_widget_grab_focus (GTK_WIDGET (togglebutton));
-    return;
-  }
-  namespace_selector_treeview =
-      GTK_WIDGET (gtk_builder_get_object
-                  (builder, "namespace_selector_treeview"));
-  gtk_widget_get_allocation (GTK_WIDGET (togglebutton),
-                            &togglebutton_allocation);
-  main_window_gdk = gtk_widget_get_window (GTK_WIDGET (togglebutton));
-  gdk_window_get_origin (main_window_gdk, &mwg_x, &mwg_y);
-
-  /* FIXME: this might become a problem in other window managers,
-   * where reference point is not in the top-left corner.
-   * We want to show the window below the button.
-   */
-  tgb_x = mwg_x + togglebutton_allocation.x;
-  tgb_y = mwg_y + togglebutton_allocation.y;
-  popup_x = tgb_x;
-  popup_y = tgb_y + togglebutton_allocation.height;
-  
-  gtk_window_move (GTK_WINDOW (namespace_selector_window), popup_x, popup_y);
-  
-  gtk_widget_show_all (namespace_selector_window);
-  gtk_widget_grab_focus (namespace_selector_treeview);
-}
-
-
-/**
  * Startup hook to initialize the namespace dropdown widget.
  *
  * @param widget the main window




reply via email to

[Prev in Thread] Current Thread [Next in Thread]