[gtk+] gtkpopover: Update the menu's alignment



commit d3a18de97e1033e7be7f5695a6abcdb48f48469f
Author: Timm Bäder <mail baedert org>
Date:   Tue Mar 17 19:34:43 2015 +0100

    gtkpopover: Update the menu's alignment
    
    ... when the popover position changes.

 gtk/gtkmenusectionbox.c |   36 ++++++++++++++++++++++++++++++++++--
 gtk/gtkmenusectionbox.h |    4 +++-
 gtk/gtkpopover.c        |    5 ++++-
 3 files changed, 41 insertions(+), 4 deletions(-)
---
diff --git a/gtk/gtkmenusectionbox.c b/gtk/gtkmenusectionbox.c
index 359d634..1f38e1b 100644
--- a/gtk/gtkmenusectionbox.c
+++ b/gtk/gtkmenusectionbox.c
@@ -277,7 +277,7 @@ gtk_menu_section_box_insert_func (GtkMenuTrackerItem *item,
       gchar *name;
 
       widget = g_object_new (GTK_TYPE_MODEL_BUTTON,
-                             "menu-name", gtk_menu_tracker_item_get_label (item), 
+                             "menu-name", gtk_menu_tracker_item_get_label (item),
                              NULL);
       g_object_bind_property (item, "label", widget, "text", G_BINDING_SYNC_CREATE);
       g_object_bind_property (item, "icon", widget, "icon", G_BINDING_SYNC_CREATE);
@@ -369,10 +369,39 @@ gtk_menu_section_box_class_init (GtkMenuSectionBoxClass *class)
   G_OBJECT_CLASS (class)->dispose = gtk_menu_section_box_dispose;
 }
 
+static void
+update_popover_position_cb (GObject    *source,
+                            GParamSpec *spec,
+                            gpointer   *user_data)
+{
+  GtkPopover *popover = GTK_POPOVER (source);
+  GtkMenuSectionBox *box = GTK_MENU_SECTION_BOX (user_data);
+
+  GtkPositionType new_pos = gtk_popover_get_position (popover);
+
+  GList *children = gtk_container_get_children (GTK_CONTAINER (gtk_widget_get_parent (GTK_WIDGET (box))));
+  GList *l;
+
+  for (l = children;
+       l != NULL;
+       l = l->next)
+    {
+      GtkWidget *w = l->data;
+
+      if (new_pos == GTK_POS_BOTTOM)
+        gtk_widget_set_valign (w, GTK_ALIGN_START);
+      else if (new_pos == GTK_POS_TOP)
+        gtk_widget_set_valign (w, GTK_ALIGN_END);
+      else
+        gtk_widget_set_valign (w, GTK_ALIGN_CENTER);
+    }
+}
+
 void
 gtk_menu_section_box_new_toplevel (GtkStack    *stack,
                                    GMenuModel  *model,
-                                   const gchar *action_namespace)
+                                   const gchar *action_namespace,
+                                   GtkPopover  *popover)
 {
   GtkMenuSectionBox *box;
 
@@ -384,6 +413,9 @@ gtk_menu_section_box_new_toplevel (GtkStack    *stack,
                                        gtk_menu_section_box_insert_func,
                                        gtk_menu_section_box_remove_func, box);
 
+  g_signal_connect (G_OBJECT (popover), "notify::position", G_CALLBACK (update_popover_position_cb), box);
+
+
   gtk_widget_show (GTK_WIDGET (box));
 }
 
diff --git a/gtk/gtkmenusectionbox.h b/gtk/gtkmenusectionbox.h
index f7ec6ab..c300abe 100644
--- a/gtk/gtkmenusectionbox.h
+++ b/gtk/gtkmenusectionbox.h
@@ -23,6 +23,7 @@
 #include <gtk/gtkmenutrackeritem.h>
 #include <gtk/gtkstack.h>
 #include <gtk/gtkbox.h>
+#include <gtk/gtkpopover.h>
 
 G_BEGIN_DECLS
 
@@ -43,7 +44,8 @@ typedef struct _GtkMenuSectionBox                           GtkMenuSectionBox;
 GType                   gtk_menu_section_box_get_type                   (void) G_GNUC_CONST;
 void                    gtk_menu_section_box_new_toplevel               (GtkStack    *stack,
                                                                          GMenuModel  *model,
-                                                                         const gchar *action_namespace);
+                                                                         const gchar *action_namespace,
+                                                                         GtkPopover  *popover);
 
 G_END_DECLS
 
diff --git a/gtk/gtkpopover.c b/gtk/gtkpopover.c
index ba2fcba..c156f8b 100644
--- a/gtk/gtkpopover.c
+++ b/gtk/gtkpopover.c
@@ -2330,7 +2330,10 @@ gtk_popover_bind_model (GtkPopover  *popover,
       gtk_widget_show (stack);
       gtk_container_add (GTK_CONTAINER (popover), stack);
 
-      gtk_menu_section_box_new_toplevel (GTK_STACK (stack), model, action_namespace);
+      gtk_menu_section_box_new_toplevel (GTK_STACK (stack),
+                                         model,
+                                         action_namespace,
+                                         popover);
       gtk_stack_set_visible_child_name (GTK_STACK (stack), "main");
 
       g_signal_connect (popover, "unmap", G_CALLBACK (back_to_main), NULL);


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