summaryrefslogtreecommitdiff
path: root/gtk/gtkmenubar.c
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2019-05-31 02:36:34 +0000
committerMatthias Clasen <mclasen@redhat.com>2019-05-31 23:36:35 +0000
commit3f6272f56eb7b7d32aad1979c67a744a14aa02ed (patch)
treede51362d91452119790fa1974211fb74705c8c09 /gtk/gtkmenubar.c
parent13e010deb6777601009950720a9e032039df6036 (diff)
downloadgtk+-3f6272f56eb7b7d32aad1979c67a744a14aa02ed.tar.gz
More menu restructuring
Make GtkMenuBar use a box as well, and let GtkMenuShell get the items from GtkMenuBar and GtkMenu via a vfunc. Use that to fix the keynav implementation in GtkMenuShell.
Diffstat (limited to 'gtk/gtkmenubar.c')
-rw-r--r--gtk/gtkmenubar.c186
1 files changed, 67 insertions, 119 deletions
diff --git a/gtk/gtkmenubar.c b/gtk/gtkmenubar.c
index 5ae9f35a7c..165412318f 100644
--- a/gtk/gtkmenubar.c
+++ b/gtk/gtkmenubar.c
@@ -51,10 +51,12 @@
#include "gtksizerequest.h"
#include "gtkwindow.h"
#include "gtkcontainerprivate.h"
+#include "gtkcheckmenuitem.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtktypebuiltins.h"
#include "gtkwidgetprivate.h"
+#include "gtkbox.h"
#define MENU_BAR_POPUP_DELAY 0
@@ -65,6 +67,9 @@ typedef struct _GtkMenuBarClass GtkMenuBarClass;
struct _GtkMenuBar
{
GtkMenuShell menu_shell;
+
+ int toggle_size;
+ GtkWidget *box;
};
struct _GtkMenuBarClass
@@ -72,6 +77,11 @@ struct _GtkMenuBarClass
GtkMenuShellClass parent_class;
};
+static void gtk_menu_bar_add (GtkContainer *container,
+ GtkWidget *widget);
+static void gtk_menu_bar_remove (GtkContainer *container,
+ GtkWidget *widget);
+
static void gtk_menu_bar_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
@@ -88,13 +98,25 @@ static void gtk_menu_bar_unroot (GtkWidget *widget);
static gint gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell);
static void gtk_menu_bar_move_current (GtkMenuShell *menu_shell,
GtkMenuDirectionType direction);
+static void gtk_menu_bar_insert (GtkMenuShell *menu_shell,
+ GtkWidget *child,
+ gint position);
G_DEFINE_TYPE (GtkMenuBar, gtk_menu_bar, GTK_TYPE_MENU_SHELL)
+static GList *
+gtk_menu_bar_get_items (GtkMenuShell *menu_shell)
+{
+ GtkMenuBar *menu_bar = GTK_MENU_BAR (menu_shell);
+
+ return gtk_container_get_children (GTK_CONTAINER (menu_bar->box));
+}
+
static void
gtk_menu_bar_class_init (GtkMenuBarClass *class)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+ GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
GtkBindingSet *binding_set;
@@ -104,9 +126,14 @@ gtk_menu_bar_class_init (GtkMenuBarClass *class)
widget_class->root = gtk_menu_bar_root;
widget_class->unroot = gtk_menu_bar_unroot;
+ container_class->add = gtk_menu_bar_add;
+ container_class->remove = gtk_menu_bar_remove;
+
+ menu_shell_class->insert = gtk_menu_bar_insert;
menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
menu_shell_class->move_current = gtk_menu_bar_move_current;
+ menu_shell_class->get_items = gtk_menu_bar_get_items;
binding_set = gtk_binding_set_by_class (class);
gtk_binding_entry_add_signal (binding_set,
@@ -157,6 +184,8 @@ gtk_menu_bar_class_init (GtkMenuBarClass *class)
static void
gtk_menu_bar_init (GtkMenuBar *menu_bar)
{
+ menu_bar->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+ gtk_widget_set_parent (menu_bar->box, GTK_WIDGET (menu_bar));
}
/**
@@ -181,58 +210,13 @@ gtk_menu_bar_measure (GtkWidget *widget,
int *minimum_baseline,
int *natural_baseline)
{
- GtkMenuShell *menu_shell;
- GtkWidget *child;
- GList *children;
- gboolean use_toggle_size, use_maximize;
- gint child_minimum, child_natural;
-
- *minimum = 0;
- *natural = 0;
-
- menu_shell = GTK_MENU_SHELL (widget);
-
- children = menu_shell->priv->children;
-
- use_toggle_size = (orientation == GTK_ORIENTATION_HORIZONTAL);
- use_maximize = (orientation == GTK_ORIENTATION_VERTICAL);
+ GtkMenuBar *menu_bar = GTK_MENU_BAR (widget);
- while (children)
- {
- child = children->data;
- children = children->next;
-
- if (gtk_widget_get_visible (child))
- {
- gtk_widget_measure (child,
- orientation,
- for_size,
- &child_minimum, &child_natural,
- NULL, NULL);
-
- if (use_toggle_size)
- {
- gint toggle_size;
-
- gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
- &toggle_size);
-
- child_minimum += toggle_size;
- child_natural += toggle_size;
- }
-
- if (use_maximize)
- {
- *minimum = MAX (*minimum, child_minimum);
- *natural = MAX (*natural, child_natural);
- }
- else
- {
- *minimum += child_minimum;
- *natural += child_natural;
- }
- }
- }
+ gtk_widget_measure (menu_bar->box,
+ orientation,
+ for_size,
+ minimum, natural,
+ minimum_baseline, natural_baseline);
}
static void
@@ -241,75 +225,11 @@ gtk_menu_bar_size_allocate (GtkWidget *widget,
int height,
int baseline)
{
- GtkMenuShell *menu_shell;
- GtkWidget *child;
- GList *children;
- GtkAllocation remaining_space;
- GArray *requested_sizes;
- gint toggle_size;
- guint i;
- int size;
- gboolean ltr = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR);
-
- menu_shell = GTK_MENU_SHELL (widget);
-
- if (!menu_shell->priv->children)
- return;
-
- remaining_space = (GtkAllocation) {0, 0, width, height};
- requested_sizes = g_array_new (FALSE, FALSE, sizeof (GtkRequestedSize));
- size = remaining_space.width;
-
- for (children = menu_shell->priv->children; children; children = children->next)
- {
- GtkRequestedSize request;
- child = children->data;
-
- if (!gtk_widget_get_visible (child))
- continue;
-
- request.data = child;
- gtk_widget_measure (child, GTK_ORIENTATION_HORIZONTAL,
- remaining_space.height,
- &request.minimum_size, &request.natural_size,
- NULL, NULL);
- gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
- &toggle_size);
- request.minimum_size += toggle_size;
- request.natural_size += toggle_size;
-
- gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child), toggle_size);
-
- g_array_append_val (requested_sizes, request);
-
- size -= request.minimum_size;
- }
+ GtkMenuBar *menu_bar = GTK_MENU_BAR (widget);
- size = gtk_distribute_natural_allocation (size,
- requested_sizes->len,
- (GtkRequestedSize *) requested_sizes->data);
-
- for (i = 0; i < requested_sizes->len; i++)
- {
- GtkAllocation child_allocation = remaining_space;
- GtkRequestedSize *request = &g_array_index (requested_sizes, GtkRequestedSize, i);
-
- child_allocation.width = request->minimum_size;
- remaining_space.width -= request->minimum_size;
-
- if (i + 1 == requested_sizes->len && GTK_IS_MENU_ITEM (request->data) &&
- GTK_MENU_ITEM (request->data)->priv->right_justify)
- ltr = !ltr;
-
- if (ltr)
- remaining_space.x += request->minimum_size;
- else
- child_allocation.x += remaining_space.width;
-
- gtk_widget_size_allocate (request->data, &child_allocation, -1);
- }
-
- g_array_free (requested_sizes, TRUE);
+ gtk_widget_size_allocate (menu_bar->box,
+ &(GtkAllocation) { 0, 0, width, height },
+ baseline);
}
static GList *
@@ -503,3 +423,31 @@ gtk_menu_bar_new_from_model (GMenuModel *model)
return menubar;
}
+
+static void
+gtk_menu_bar_add (GtkContainer *container,
+ GtkWidget *widget)
+{
+ GtkMenuBar *menu_bar = GTK_MENU_BAR (container);
+
+ gtk_container_add (GTK_CONTAINER (menu_bar->box), widget);
+}
+
+static void
+gtk_menu_bar_remove (GtkContainer *container,
+ GtkWidget *widget)
+{
+ GtkMenuBar *menu_bar = GTK_MENU_BAR (container);
+
+ gtk_container_remove (GTK_CONTAINER (menu_bar->box), widget);
+}
+
+static void
+gtk_menu_bar_insert (GtkMenuShell *menu_shell,
+ GtkWidget *child,
+ gint position)
+{
+ GtkMenuBar *menu_bar = GTK_MENU_BAR (menu_shell);
+
+ gtk_container_add (GTK_CONTAINER (menu_bar->box), child);
+}