[gtk+/wip/gbsneto/other-locations] placesview: implement keyboard handler



commit 54160b06d6e41fc0d684a96ed986a493722f9acc
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Jun 30 22:31:37 2015 -0300

    placesview: implement keyboard handler
    
    PlacesView is now able to detect if Shift or
    Ctrl keys are pressed, and properly send the
    correct open flag.

 gtk/gtkplacesview.c     |  123 ++++++++++++++++++++++++++++++++++++++---------
 gtk/ui/gtkplacesview.ui |    1 +
 2 files changed, 101 insertions(+), 23 deletions(-)
---
diff --git a/gtk/gtkplacesview.c b/gtk/gtkplacesview.c
index c1eee3f..d6549d5 100644
--- a/gtk/gtkplacesview.c
+++ b/gtk/gtkplacesview.c
@@ -64,6 +64,9 @@ struct _GtkPlacesViewPrivate
   guint local_only             : 1;
 };
 
+static void        mount_volume                                  (GtkPlacesView *view,
+                                                                  GVolume       *volume);
+
 G_DEFINE_TYPE_WITH_PRIVATE (GtkPlacesView, gtk_places_view, GTK_TYPE_BOX)
 
 /* GtkPlacesView properties & signals */
@@ -188,6 +191,95 @@ server_list_add_server (GFile *file)
   g_free (uri);
 }
 
+/* Returns a toplevel GtkWindow, or NULL if none */
+static GtkWindow *
+get_toplevel (GtkWidget *widget)
+{
+  GtkWidget *toplevel;
+
+  toplevel = gtk_widget_get_toplevel (widget);
+  if (!gtk_widget_is_toplevel (toplevel))
+    return NULL;
+  else
+    return GTK_WINDOW (toplevel);
+}
+
+/* Activates the given row, with the given flags as parameter */
+static void
+activate_row (GtkPlacesView      *view,
+              GtkPlacesViewRow   *row,
+              GtkPlacesOpenFlags  flags)
+{
+  GVolume *volume;
+  GFile *location;
+  GMount *mount;
+
+  g_assert (GTK_IS_PLACES_VIEW (view));
+  g_assert (GTK_IS_PLACES_VIEW_ROW (row));
+
+  mount = gtk_places_view_row_get_mount (row);
+  volume = gtk_places_view_row_get_volume (row);
+
+  if (mount)
+    {
+      location = g_mount_get_root (mount);
+
+      emit_open_location (view, location, GTK_PLACES_OPEN_NORMAL);
+
+      g_object_unref (location);
+    }
+  else if (volume && g_volume_can_mount (volume))
+    {
+        mount_volume (view, volume);
+    }
+}
+
+static gboolean
+on_key_press_event (GtkWidget     *widget,
+                    GdkEventKey   *event,
+                    GtkPlacesView *view)
+{
+  if (event)
+    {
+      guint modifiers;
+
+      modifiers = gtk_accelerator_get_default_mod_mask ();
+
+      if (event->keyval == GDK_KEY_Return ||
+          event->keyval == GDK_KEY_KP_Enter ||
+          event->keyval == GDK_KEY_ISO_Enter ||
+          event->keyval == GDK_KEY_space)
+        {
+          GtkPlacesOpenFlags open_flags;
+          GtkWidget *focus_widget;
+          GtkWindow *toplevel;
+
+          open_flags = GTK_PLACES_OPEN_NORMAL;
+          toplevel = get_toplevel (GTK_WIDGET (view));
+
+          if (!toplevel)
+            return FALSE;
+
+          focus_widget = gtk_window_get_focus (toplevel);
+
+          if (!focus_widget)
+            return FALSE;
+
+          if ((event->state & modifiers) == GDK_SHIFT_MASK)
+            open_flags = GTK_PLACES_OPEN_NEW_TAB;
+          else if ((event->state & modifiers) == GDK_CONTROL_MASK)
+            open_flags = GTK_PLACES_OPEN_NEW_WINDOW;
+
+          if (GTK_IS_PLACES_VIEW_ROW (focus_widget))
+            activate_row (view, GTK_PLACES_VIEW_ROW (focus_widget), open_flags);
+
+          return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
 static gboolean
 gtk_places_view_real_get_local_only (GtkPlacesView *view)
 {
@@ -770,13 +862,15 @@ on_connect_button_clicked (GtkButton     *button,
   if (uri != NULL && uri[0] != '\0')
     file = g_file_new_for_commandline_arg (uri);
 
-  if (!file)
+  if (file)
+    {
+      gtk_entry_set_text (GTK_ENTRY (priv->address_entry), NULL);
+      mount_location (view, file);
+    }
+  else
     {
       g_warning ("Unable to get remote server location");
-      return;
     }
-
-  mount_location (view, file);
 }
 
 static void
@@ -814,28 +908,10 @@ static void
 on_listbox_row_activated (GtkPlacesView    *view,
                           GtkPlacesViewRow *row)
 {
-  GVolume *volume;
-  GFile *location;
-  GMount *mount;
-
   g_return_if_fail (GTK_IS_PLACES_VIEW (view));
   g_return_if_fail (GTK_IS_PLACES_VIEW_ROW (row));
 
-  mount = gtk_places_view_row_get_mount (row);
-  volume = gtk_places_view_row_get_volume (row);
-
-  if (mount)
-    {
-      location = g_mount_get_root (mount);
-
-      emit_open_location (view, location, GTK_PLACES_OPEN_NORMAL);
-
-      g_object_unref (location);
-    }
-  else if (volume && g_volume_can_mount (volume))
-    {
-        mount_volume (view, volume);
-    }
+  activate_row (view, row, GTK_PLACES_OPEN_NORMAL);
 }
 
 static void
@@ -941,6 +1017,7 @@ gtk_places_view_class_init (GtkPlacesViewClass *klass)
 
   gtk_widget_class_bind_template_callback (widget_class, on_address_entry_text_changed);
   gtk_widget_class_bind_template_callback (widget_class, on_connect_button_clicked);
+  gtk_widget_class_bind_template_callback (widget_class, on_key_press_event);
   gtk_widget_class_bind_template_callback (widget_class, on_listbox_row_activated);
 }
 
diff --git a/gtk/ui/gtkplacesview.ui b/gtk/ui/gtkplacesview.ui
index a0b4b18..ef96b99 100644
--- a/gtk/ui/gtkplacesview.ui
+++ b/gtk/ui/gtkplacesview.ui
@@ -58,6 +58,7 @@
     <property name="visible">True</property>
     <property name="can_focus">False</property>
     <property name="orientation">vertical</property>
+    <signal name="key-press-event" handler="on_key_press_event" object="GtkPlacesView" swapped="no" />
     <child>
       <object class="GtkScrolledWindow" id="scrolled_window">
         <property name="visible">True</property>


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