2014-04-28 15:55:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Canonical Limited
|
|
|
|
* Copyright © 2013 Carlos Garnacho
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the licence, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-06-10 03:10:13 +00:00
|
|
|
#include "gtkmenusectionboxprivate.h"
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
#include "gtkwidgetprivate.h"
|
|
|
|
#include "gtklabel.h"
|
2019-06-10 03:10:13 +00:00
|
|
|
#include "gtkmenutrackerprivate.h"
|
2019-11-11 10:58:43 +00:00
|
|
|
#include "gtkmodelbuttonprivate.h"
|
2014-04-28 15:55:52 +00:00
|
|
|
#include "gtkseparator.h"
|
|
|
|
#include "gtksizegroup.h"
|
|
|
|
#include "gtkstack.h"
|
2019-11-11 10:58:43 +00:00
|
|
|
#include "gtkpopovermenuprivate.h"
|
2014-04-28 15:55:52 +00:00
|
|
|
#include "gtkorientable.h"
|
2020-01-28 14:03:42 +00:00
|
|
|
#include "gtkbuiltiniconprivate.h"
|
2020-10-20 18:26:45 +00:00
|
|
|
#include "gtkgizmoprivate.h"
|
|
|
|
#include "gtkbinlayout.h"
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
typedef GtkBoxClass GtkMenuSectionBoxClass;
|
|
|
|
|
|
|
|
struct _GtkMenuSectionBox
|
|
|
|
{
|
2019-08-27 08:28:29 +00:00
|
|
|
GtkBox parent_instance;
|
|
|
|
|
|
|
|
GtkMenuSectionBox *toplevel;
|
|
|
|
GtkMenuTracker *tracker;
|
|
|
|
GtkBox *item_box;
|
|
|
|
GtkWidget *separator;
|
|
|
|
guint separator_sync_idle;
|
|
|
|
gboolean iconic;
|
|
|
|
gboolean inline_buttons;
|
|
|
|
gboolean circular;
|
2020-07-24 13:54:49 +00:00
|
|
|
int depth;
|
2019-08-27 08:28:29 +00:00
|
|
|
GtkPopoverMenuFlags flags;
|
|
|
|
GtkSizeGroup *indicators;
|
2020-10-20 18:26:45 +00:00
|
|
|
GHashTable *custom_slots;
|
2014-04-28 15:55:52 +00:00
|
|
|
};
|
|
|
|
|
2014-10-18 09:19:26 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_items;
|
2014-10-18 09:19:26 +00:00
|
|
|
gboolean previous_is_iconic;
|
|
|
|
} MenuData;
|
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
G_DEFINE_TYPE (GtkMenuSectionBox, gtk_menu_section_box, GTK_TYPE_BOX)
|
|
|
|
|
2014-10-26 20:23:31 +00:00
|
|
|
static void gtk_menu_section_box_sync_separators (GtkMenuSectionBox *box,
|
|
|
|
MenuData *data);
|
|
|
|
static void gtk_menu_section_box_new_submenu (GtkMenuTrackerItem *item,
|
|
|
|
GtkMenuSectionBox *toplevel,
|
|
|
|
GtkWidget *focus,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *name);
|
2014-10-26 20:23:31 +00:00
|
|
|
static GtkWidget * gtk_menu_section_box_new_section (GtkMenuTrackerItem *item,
|
|
|
|
GtkMenuSectionBox *parent);
|
2014-04-28 15:55:52 +00:00
|
|
|
|
2014-04-29 15:50:54 +00:00
|
|
|
/* We are trying to implement the following rules here:
|
|
|
|
*
|
|
|
|
* rule 1: never ever show separators for empty sections
|
|
|
|
* rule 2: always show a separator if there is a label
|
|
|
|
* rule 3: don't show a separator for the first section
|
|
|
|
* rule 4: don't show a separator for the following sections if there are
|
|
|
|
* no items before it
|
2014-10-18 09:19:26 +00:00
|
|
|
* rule 5: never show separators directly above or below an iconic box
|
|
|
|
* (rule 6: these rules don't apply exactly the same way for subsections)
|
2014-04-29 15:50:54 +00:00
|
|
|
*/
|
2014-10-26 20:23:31 +00:00
|
|
|
static void
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_menu_section_box_sync_separators (GtkMenuSectionBox *box,
|
2014-10-18 09:19:26 +00:00
|
|
|
MenuData *data)
|
2014-04-28 15:55:52 +00:00
|
|
|
{
|
2014-10-18 09:19:26 +00:00
|
|
|
gboolean previous_section_is_iconic;
|
2014-04-28 15:55:52 +00:00
|
|
|
gboolean should_have_separator;
|
2014-10-18 09:19:26 +00:00
|
|
|
gboolean should_have_top_margin = FALSE;
|
|
|
|
gboolean is_not_empty_item;
|
2014-04-29 15:48:52 +00:00
|
|
|
gboolean has_separator;
|
|
|
|
gboolean has_label;
|
2014-10-18 09:19:26 +00:00
|
|
|
gboolean separator_condition;
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_items_before;
|
2020-05-09 12:26:52 +00:00
|
|
|
GtkWidget *child;
|
2014-04-28 15:55:52 +00:00
|
|
|
|
2014-10-18 09:19:26 +00:00
|
|
|
n_items_before = data->n_items;
|
|
|
|
previous_section_is_iconic = data->previous_is_iconic;
|
2014-04-28 15:55:52 +00:00
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
for (child = gtk_widget_get_first_child (GTK_WIDGET (box->item_box));
|
|
|
|
child != NULL;
|
|
|
|
child = gtk_widget_get_next_sibling (child))
|
|
|
|
{
|
|
|
|
if (GTK_IS_MENU_SECTION_BOX (child))
|
|
|
|
gtk_menu_section_box_sync_separators (GTK_MENU_SECTION_BOX (child), data);
|
|
|
|
else
|
|
|
|
data->n_items++;
|
|
|
|
}
|
2014-10-18 09:19:26 +00:00
|
|
|
|
|
|
|
is_not_empty_item = (data->n_items > n_items_before);
|
|
|
|
|
|
|
|
if (is_not_empty_item)
|
|
|
|
data->previous_is_iconic = box->iconic;
|
2014-06-28 04:00:16 +00:00
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
if (box->separator == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-04-29 15:48:52 +00:00
|
|
|
has_separator = gtk_widget_get_parent (box->separator) != NULL;
|
|
|
|
has_label = !GTK_IS_SEPARATOR (box->separator);
|
|
|
|
|
2014-10-18 09:19:26 +00:00
|
|
|
separator_condition = has_label ? TRUE : n_items_before > 0 &&
|
|
|
|
box->depth <= 1 &&
|
|
|
|
!previous_section_is_iconic &&
|
|
|
|
!box->iconic;
|
|
|
|
|
|
|
|
should_have_separator = separator_condition && is_not_empty_item;
|
|
|
|
|
|
|
|
should_have_top_margin = !should_have_separator &&
|
2019-06-06 01:36:17 +00:00
|
|
|
(box->depth <= 1 || box->iconic || box->circular) &&
|
2014-10-18 09:19:26 +00:00
|
|
|
n_items_before > 0 &&
|
|
|
|
is_not_empty_item;
|
|
|
|
|
|
|
|
gtk_widget_set_margin_top (GTK_WIDGET (box->item_box), should_have_top_margin ? 10 : 0);
|
2014-04-29 15:48:52 +00:00
|
|
|
|
2020-04-28 00:26:02 +00:00
|
|
|
if (has_label)
|
|
|
|
{
|
|
|
|
GtkWidget *separator = gtk_widget_get_first_child (box->separator);
|
|
|
|
|
|
|
|
gtk_widget_set_visible (separator, n_items_before > 0);
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:48:52 +00:00
|
|
|
if (should_have_separator == has_separator)
|
2014-04-28 15:55:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (should_have_separator)
|
2019-01-22 23:44:34 +00:00
|
|
|
gtk_box_insert_child_after (GTK_BOX (box), box->separator, NULL);
|
2014-04-28 15:55:52 +00:00
|
|
|
else
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_remove (GTK_BOX (box), box->separator);
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_menu_section_box_handle_sync_separators (gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkMenuSectionBox *box = user_data;
|
2014-10-18 09:19:26 +00:00
|
|
|
MenuData data;
|
2014-04-28 15:55:52 +00:00
|
|
|
|
2014-10-18 09:19:26 +00:00
|
|
|
data.n_items = 0;
|
|
|
|
data.previous_is_iconic = FALSE;
|
|
|
|
gtk_menu_section_box_sync_separators (box, &data);
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
box->separator_sync_idle = 0;
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_menu_section_box_schedule_separator_sync (GtkMenuSectionBox *box)
|
|
|
|
{
|
|
|
|
box = box->toplevel;
|
|
|
|
|
|
|
|
if (!box->separator_sync_idle)
|
2018-02-02 14:08:02 +00:00
|
|
|
{
|
|
|
|
box->separator_sync_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE, /* before resize... */
|
|
|
|
gtk_menu_section_box_handle_sync_separators,
|
|
|
|
box, NULL);
|
2019-02-05 10:26:20 +00:00
|
|
|
g_source_set_name_by_id (box->separator_sync_idle, "[gtk] menu section box handle sync separators");
|
2018-02-02 14:08:02 +00:00
|
|
|
}
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_popover_item_activate (GtkWidget *button,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkMenuTrackerItem *item = user_data;
|
2014-08-23 13:55:07 +00:00
|
|
|
GtkWidget *popover = NULL;
|
|
|
|
|
|
|
|
if (gtk_menu_tracker_item_get_role (item) == GTK_MENU_TRACKER_ITEM_ROLE_NORMAL)
|
|
|
|
{
|
|
|
|
/* Activating the item could cause the popover
|
|
|
|
* to be free'd, for example if it is a Quit item
|
|
|
|
*/
|
2020-08-02 02:03:06 +00:00
|
|
|
popover = gtk_widget_get_ancestor (button, GTK_TYPE_POPOVER);
|
|
|
|
if (popover)
|
|
|
|
g_object_ref (popover);
|
2014-08-23 13:55:07 +00:00
|
|
|
}
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
gtk_menu_tracker_item_activated (item);
|
|
|
|
|
2014-08-23 13:55:07 +00:00
|
|
|
if (popover != NULL)
|
|
|
|
{
|
|
|
|
gtk_widget_hide (popover);
|
|
|
|
g_object_unref (popover);
|
|
|
|
}
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-07-24 13:54:49 +00:00
|
|
|
gtk_menu_section_box_remove_func (int position,
|
2014-04-28 15:55:52 +00:00
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkMenuSectionBox *box = user_data;
|
2016-03-09 12:44:45 +00:00
|
|
|
GtkMenuTrackerItem *item;
|
|
|
|
GtkWidget *widget;
|
2020-05-09 12:26:52 +00:00
|
|
|
int pos;
|
2016-03-09 12:44:45 +00:00
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
for (widget = gtk_widget_get_first_child (GTK_WIDGET (box->item_box)), pos = 0;
|
|
|
|
widget != NULL;
|
|
|
|
widget = gtk_widget_get_next_sibling (widget), pos++)
|
|
|
|
{
|
|
|
|
if (pos == position)
|
|
|
|
break;
|
|
|
|
}
|
2016-03-09 12:44:45 +00:00
|
|
|
|
|
|
|
item = g_object_get_data (G_OBJECT (widget), "GtkMenuTrackerItem");
|
2016-04-22 03:24:13 +00:00
|
|
|
if (gtk_menu_tracker_item_get_has_link (item, G_MENU_LINK_SUBMENU))
|
|
|
|
{
|
|
|
|
GtkWidget *stack, *subbox;
|
2016-03-09 12:44:45 +00:00
|
|
|
|
2016-04-22 03:24:13 +00:00
|
|
|
stack = gtk_widget_get_ancestor (GTK_WIDGET (box->toplevel), GTK_TYPE_STACK);
|
|
|
|
subbox = gtk_stack_get_child_by_name (GTK_STACK (stack), gtk_menu_tracker_item_get_label (item));
|
|
|
|
if (subbox != NULL)
|
2020-05-10 01:01:24 +00:00
|
|
|
gtk_stack_remove (GTK_STACK (stack), subbox);
|
2016-03-09 12:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_remove (GTK_BOX (box->item_box), widget);
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
gtk_menu_section_box_schedule_separator_sync (box);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
get_ancestors (GtkWidget *widget,
|
|
|
|
GType widget_type,
|
|
|
|
GtkWidget **ancestor,
|
|
|
|
GtkWidget **below)
|
|
|
|
{
|
|
|
|
GtkWidget *a, *b;
|
|
|
|
|
|
|
|
a = NULL;
|
|
|
|
b = widget;
|
|
|
|
while (b != NULL)
|
|
|
|
{
|
|
|
|
a = gtk_widget_get_parent (b);
|
|
|
|
if (!a)
|
|
|
|
return FALSE;
|
|
|
|
if (g_type_is_a (G_OBJECT_TYPE (a), widget_type))
|
|
|
|
break;
|
|
|
|
b = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
*below = b;
|
|
|
|
*ancestor = a;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_submenu (GtkWidget *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkMenuTrackerItem *item = data;
|
|
|
|
GtkWidget *focus;
|
|
|
|
|
|
|
|
if (gtk_menu_tracker_item_get_should_request_show (item))
|
|
|
|
gtk_menu_tracker_item_request_submenu_shown (item, FALSE);
|
|
|
|
|
|
|
|
focus = GTK_WIDGET (g_object_get_data (G_OBJECT (button), "focus"));
|
|
|
|
gtk_widget_grab_focus (focus);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_submenu (GtkWidget *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkMenuTrackerItem *item = data;
|
|
|
|
GtkWidget *focus;
|
|
|
|
|
|
|
|
if (gtk_menu_tracker_item_get_should_request_show (item))
|
|
|
|
gtk_menu_tracker_item_request_submenu_shown (item, TRUE);
|
|
|
|
|
|
|
|
focus = GTK_WIDGET (g_object_get_data (G_OBJECT (button), "focus"));
|
|
|
|
gtk_widget_grab_focus (focus);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_menu_section_box_insert_func (GtkMenuTrackerItem *item,
|
2020-07-24 13:54:49 +00:00
|
|
|
int position,
|
2014-04-28 15:55:52 +00:00
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkMenuSectionBox *box = user_data;
|
|
|
|
GtkWidget *widget;
|
|
|
|
|
|
|
|
if (gtk_menu_tracker_item_get_is_separator (item))
|
|
|
|
{
|
2014-04-29 16:17:40 +00:00
|
|
|
widget = gtk_menu_section_box_new_section (item, box);
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
|
|
|
else if (gtk_menu_tracker_item_get_has_link (item, G_MENU_LINK_SUBMENU))
|
|
|
|
{
|
2019-08-27 08:28:29 +00:00
|
|
|
if (box->flags & GTK_POPOVER_MENU_NESTED)
|
|
|
|
{
|
|
|
|
GMenuModel *model;
|
|
|
|
GtkWidget *submenu;
|
2014-10-26 20:23:31 +00:00
|
|
|
|
2019-08-27 08:28:29 +00:00
|
|
|
model = _gtk_menu_tracker_item_get_link (item, G_MENU_LINK_SUBMENU);
|
2014-10-26 20:23:31 +00:00
|
|
|
|
2020-02-24 12:21:46 +00:00
|
|
|
submenu = gtk_popover_menu_new_from_model_full (model, box->flags);
|
2019-08-27 08:28:29 +00:00
|
|
|
gtk_popover_set_has_arrow (GTK_POPOVER (submenu), FALSE);
|
|
|
|
gtk_widget_set_valign (submenu, GTK_ALIGN_START);
|
|
|
|
|
|
|
|
widget = g_object_new (GTK_TYPE_MODEL_BUTTON,
|
|
|
|
"popover", submenu,
|
|
|
|
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);
|
|
|
|
g_object_bind_property (item, "sensitive", widget, "sensitive", G_BINDING_SYNC_CREATE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GtkWidget *stack = NULL;
|
|
|
|
GtkWidget *parent = NULL;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name;
|
2019-08-27 08:28:29 +00:00
|
|
|
|
|
|
|
widget = g_object_new (GTK_TYPE_MODEL_BUTTON,
|
|
|
|
"menu-name", gtk_menu_tracker_item_get_label (item),
|
|
|
|
"indicator-size-group", box->indicators,
|
|
|
|
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);
|
|
|
|
g_object_bind_property (item, "sensitive", widget, "sensitive", G_BINDING_SYNC_CREATE);
|
|
|
|
|
|
|
|
get_ancestors (GTK_WIDGET (box->toplevel), GTK_TYPE_STACK, &stack, &parent);
|
|
|
|
g_object_get (gtk_stack_get_page (GTK_STACK (stack), parent), "name", &name, NULL);
|
|
|
|
gtk_menu_section_box_new_submenu (item, box->toplevel, widget, name);
|
|
|
|
g_free (name);
|
|
|
|
}
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
2020-10-20 18:26:45 +00:00
|
|
|
else if (gtk_menu_tracker_item_get_custom (item))
|
|
|
|
{
|
|
|
|
const char *id = gtk_menu_tracker_item_get_custom (item);
|
|
|
|
|
|
|
|
widget = gtk_gizmo_new ("widget", NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
gtk_widget_set_layout_manager (widget, gtk_bin_layout_new ());
|
|
|
|
|
|
|
|
if (g_hash_table_lookup (box->custom_slots, id))
|
|
|
|
g_warning ("Duplicate custom ID: %s", id);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *slot_id = g_strdup (id);
|
|
|
|
g_object_set_data_full (G_OBJECT (widget), "slot-id", slot_id, g_free);
|
|
|
|
g_hash_table_insert (box->custom_slots, slot_id, widget);
|
|
|
|
}
|
|
|
|
}
|
2014-04-28 15:55:52 +00:00
|
|
|
else
|
|
|
|
{
|
2019-06-06 00:41:45 +00:00
|
|
|
widget = g_object_new (GTK_TYPE_MODEL_BUTTON,
|
|
|
|
"indicator-size-group", box->indicators,
|
|
|
|
NULL);
|
2014-04-28 15:55:52 +00:00
|
|
|
g_object_bind_property (item, "label", widget, "text", G_BINDING_SYNC_CREATE);
|
2014-04-28 18:18:39 +00:00
|
|
|
|
|
|
|
if (box->iconic)
|
|
|
|
{
|
|
|
|
g_object_bind_property (item, "verb-icon", widget, "icon", G_BINDING_SYNC_CREATE);
|
2019-06-05 22:31:14 +00:00
|
|
|
g_object_set (widget, "iconic", TRUE, NULL);
|
2014-04-28 18:18:39 +00:00
|
|
|
}
|
2019-06-06 01:36:17 +00:00
|
|
|
else if (box->inline_buttons)
|
|
|
|
{
|
|
|
|
g_object_bind_property (item, "verb-icon", widget, "icon", G_BINDING_SYNC_CREATE);
|
|
|
|
g_object_set (widget, "iconic", TRUE, NULL);
|
2020-02-06 16:32:26 +00:00
|
|
|
gtk_widget_add_css_class (widget, "flat");
|
2019-06-06 01:36:17 +00:00
|
|
|
}
|
|
|
|
else if (box->circular)
|
|
|
|
{
|
|
|
|
g_object_bind_property (item, "verb-icon", widget, "icon", G_BINDING_SYNC_CREATE);
|
|
|
|
g_object_set (widget, "iconic", TRUE, NULL);
|
2020-02-06 16:32:26 +00:00
|
|
|
gtk_widget_add_css_class (widget, "circular");
|
2019-06-06 01:36:17 +00:00
|
|
|
}
|
2014-04-28 18:18:39 +00:00
|
|
|
else
|
|
|
|
g_object_bind_property (item, "icon", widget, "icon", G_BINDING_SYNC_CREATE);
|
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
g_object_bind_property (item, "sensitive", widget, "sensitive", G_BINDING_SYNC_CREATE);
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_bind_property (item, "role", widget, "role", G_BINDING_SYNC_CREATE);
|
2014-10-24 01:43:28 +00:00
|
|
|
g_object_bind_property (item, "toggled", widget, "active", G_BINDING_SYNC_CREATE);
|
2019-06-07 13:02:45 +00:00
|
|
|
g_object_bind_property (item, "accel", widget, "accel", G_BINDING_SYNC_CREATE);
|
2014-04-28 15:55:52 +00:00
|
|
|
g_signal_connect (widget, "clicked", G_CALLBACK (gtk_popover_item_activate), item);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_set_data_full (G_OBJECT (widget), "GtkMenuTrackerItem", g_object_ref (item), g_object_unref);
|
|
|
|
|
2019-06-06 01:36:17 +00:00
|
|
|
if (box->circular)
|
|
|
|
{
|
|
|
|
gtk_widget_set_hexpand (widget, TRUE);
|
|
|
|
gtk_widget_set_halign (widget, GTK_ALIGN_CENTER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_widget_set_halign (widget, GTK_ALIGN_FILL);
|
|
|
|
}
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box->item_box), widget);
|
2019-01-22 23:44:34 +00:00
|
|
|
|
|
|
|
if (position == 0)
|
|
|
|
gtk_box_reorder_child_after (GTK_BOX (box->item_box), widget, NULL);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GtkWidget *sibling = gtk_widget_get_first_child (GTK_WIDGET (box->item_box));
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < position; i++)
|
|
|
|
sibling = gtk_widget_get_next_sibling (sibling);
|
|
|
|
gtk_box_reorder_child_after (GTK_BOX (box->item_box), widget, sibling);
|
|
|
|
}
|
2014-04-28 15:55:52 +00:00
|
|
|
|
2020-04-23 02:16:46 +00:00
|
|
|
if (box->circular)
|
|
|
|
{
|
|
|
|
GtkWidget *c1, *c2, *c3;
|
|
|
|
|
|
|
|
/* special-case the n > 2 case */
|
|
|
|
c1 = gtk_widget_get_first_child (GTK_WIDGET (box->item_box));
|
|
|
|
if ((c2 = gtk_widget_get_next_sibling (c1)) != NULL &&
|
|
|
|
(c3 = gtk_widget_get_next_sibling (c2)) != NULL)
|
|
|
|
{
|
|
|
|
gtk_widget_set_halign (c1, GTK_ALIGN_START);
|
|
|
|
while (c3 != NULL)
|
|
|
|
{
|
|
|
|
gtk_widget_set_halign (c2, GTK_ALIGN_CENTER);
|
|
|
|
c2 = c3;
|
|
|
|
c3 = gtk_widget_get_next_sibling (c3);
|
|
|
|
}
|
|
|
|
gtk_widget_set_halign (c2, GTK_ALIGN_END);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_menu_section_box_schedule_separator_sync (box);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_menu_section_box_init (GtkMenuSectionBox *box)
|
|
|
|
{
|
|
|
|
GtkWidget *item_box;
|
|
|
|
|
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (box), GTK_ORIENTATION_VERTICAL);
|
|
|
|
|
|
|
|
box->toplevel = box;
|
|
|
|
|
|
|
|
item_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
box->item_box = GTK_BOX (item_box);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), item_box);
|
2014-04-28 18:18:39 +00:00
|
|
|
gtk_widget_set_halign (GTK_WIDGET (item_box), GTK_ALIGN_FILL);
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_widget_set_halign (GTK_WIDGET (box), GTK_ALIGN_FILL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_menu_section_box_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkMenuSectionBox *box = GTK_MENU_SECTION_BOX (object);
|
|
|
|
|
|
|
|
if (box->separator_sync_idle)
|
|
|
|
{
|
|
|
|
g_source_remove (box->separator_sync_idle);
|
|
|
|
box->separator_sync_idle = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-27 11:19:50 +00:00
|
|
|
g_clear_object (&box->separator);
|
2014-04-29 14:18:22 +00:00
|
|
|
|
|
|
|
if (box->tracker)
|
|
|
|
{
|
|
|
|
gtk_menu_tracker_free (box->tracker);
|
|
|
|
box->tracker = NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-06 00:41:45 +00:00
|
|
|
g_clear_object (&box->indicators);
|
2020-10-20 18:26:45 +00:00
|
|
|
g_clear_pointer (&box->custom_slots, g_hash_table_unref);
|
2019-06-06 00:41:45 +00:00
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
G_OBJECT_CLASS (gtk_menu_section_box_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_menu_section_box_class_init (GtkMenuSectionBoxClass *class)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (class)->dispose = gtk_menu_section_box_dispose;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:34:43 +00:00
|
|
|
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);
|
2020-05-10 01:01:24 +00:00
|
|
|
GtkWidget *w;
|
2015-03-17 18:34:43 +00:00
|
|
|
|
|
|
|
GtkPositionType new_pos = gtk_popover_get_position (popover);
|
|
|
|
|
2020-05-10 01:01:24 +00:00
|
|
|
for (w = gtk_widget_get_first_child (gtk_widget_get_parent (GTK_WIDGET (box)));
|
|
|
|
w != NULL;
|
|
|
|
w = gtk_widget_get_next_sibling (w))
|
2015-03-17 18:34:43 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
void
|
2019-08-27 08:28:29 +00:00
|
|
|
gtk_menu_section_box_new_toplevel (GtkPopoverMenu *popover,
|
|
|
|
GMenuModel *model,
|
|
|
|
GtkPopoverMenuFlags flags)
|
2014-04-28 15:55:52 +00:00
|
|
|
{
|
|
|
|
GtkMenuSectionBox *box;
|
|
|
|
|
2020-10-20 18:26:45 +00:00
|
|
|
box = g_object_new (GTK_TYPE_MENU_SECTION_BOX, NULL);
|
2019-06-06 00:41:45 +00:00
|
|
|
box->indicators = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
|
2020-10-20 18:26:45 +00:00
|
|
|
box->custom_slots = g_hash_table_new (g_str_hash, g_str_equal);
|
2019-08-27 08:28:29 +00:00
|
|
|
box->flags = flags;
|
2019-06-06 00:41:45 +00:00
|
|
|
|
2019-06-07 17:05:48 +00:00
|
|
|
gtk_popover_menu_add_submenu (popover, GTK_WIDGET (box), "main");
|
2014-04-28 15:55:52 +00:00
|
|
|
|
2014-12-03 14:53:59 +00:00
|
|
|
box->tracker = gtk_menu_tracker_new (GTK_ACTION_OBSERVABLE (_gtk_widget_get_action_muxer (GTK_WIDGET (box), TRUE)),
|
2019-06-07 17:05:48 +00:00
|
|
|
model, TRUE, FALSE, FALSE, NULL,
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_menu_section_box_insert_func,
|
|
|
|
gtk_menu_section_box_remove_func, box);
|
|
|
|
|
2015-03-17 18:34:43 +00:00
|
|
|
g_signal_connect (G_OBJECT (popover), "notify::position", G_CALLBACK (update_popover_position_cb), box);
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
|
|
|
|
2014-10-26 20:23:31 +00:00
|
|
|
static void
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_menu_section_box_new_submenu (GtkMenuTrackerItem *item,
|
|
|
|
GtkMenuSectionBox *toplevel,
|
2014-10-26 20:23:31 +00:00
|
|
|
GtkWidget *focus,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *name)
|
2014-04-28 15:55:52 +00:00
|
|
|
{
|
|
|
|
GtkMenuSectionBox *box;
|
|
|
|
GtkWidget *button;
|
|
|
|
|
2016-10-24 18:08:58 +00:00
|
|
|
box = g_object_new (GTK_TYPE_MENU_SECTION_BOX, NULL);
|
2019-06-06 00:41:45 +00:00
|
|
|
box->indicators = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
|
2020-10-20 18:26:45 +00:00
|
|
|
box->custom_slots = g_hash_table_ref (toplevel->custom_slots);
|
2019-08-27 08:28:29 +00:00
|
|
|
box->flags = toplevel->flags;
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
button = g_object_new (GTK_TYPE_MODEL_BUTTON,
|
2014-10-26 20:23:31 +00:00
|
|
|
"menu-name", name,
|
2019-06-05 21:22:01 +00:00
|
|
|
"role", GTK_BUTTON_ROLE_TITLE,
|
2014-04-28 15:55:52 +00:00
|
|
|
NULL);
|
2014-10-26 20:23:31 +00:00
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
g_object_bind_property (item, "label", button, "text", G_BINDING_SYNC_CREATE);
|
|
|
|
g_object_bind_property (item, "icon", button, "icon", G_BINDING_SYNC_CREATE);
|
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (button), "focus", focus);
|
|
|
|
g_object_set_data (G_OBJECT (focus), "focus", button);
|
|
|
|
|
2019-01-22 23:44:34 +00:00
|
|
|
gtk_box_insert_child_after (GTK_BOX (box), button, NULL);
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
g_signal_connect (focus, "clicked", G_CALLBACK (open_submenu), item);
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (close_submenu), item);
|
|
|
|
|
|
|
|
gtk_stack_add_named (GTK_STACK (gtk_widget_get_ancestor (GTK_WIDGET (toplevel), GTK_TYPE_STACK)),
|
|
|
|
GTK_WIDGET (box), gtk_menu_tracker_item_get_label (item));
|
|
|
|
|
2014-12-16 17:14:23 +00:00
|
|
|
box->tracker = gtk_menu_tracker_new_for_item_link (item, G_MENU_LINK_SUBMENU, FALSE, FALSE,
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_menu_section_box_insert_func,
|
|
|
|
gtk_menu_section_box_remove_func,
|
|
|
|
box);
|
|
|
|
}
|
|
|
|
|
2014-10-26 20:23:31 +00:00
|
|
|
static GtkWidget *
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_menu_section_box_new_section (GtkMenuTrackerItem *item,
|
2014-04-29 16:17:40 +00:00
|
|
|
GtkMenuSectionBox *parent)
|
2014-04-28 15:55:52 +00:00
|
|
|
{
|
|
|
|
GtkMenuSectionBox *box;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *label;
|
|
|
|
const char *hint;
|
|
|
|
const char *text_direction;
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
box = g_object_new (GTK_TYPE_MENU_SECTION_BOX, NULL);
|
2019-06-06 00:41:45 +00:00
|
|
|
box->indicators = g_object_ref (parent->indicators);
|
2020-10-20 18:26:45 +00:00
|
|
|
box->custom_slots = g_hash_table_ref (parent->toplevel->custom_slots);
|
2014-04-29 16:17:40 +00:00
|
|
|
box->toplevel = parent->toplevel;
|
|
|
|
box->depth = parent->depth + 1;
|
2019-08-27 08:28:29 +00:00
|
|
|
box->flags = parent->flags;
|
2014-04-28 15:55:52 +00:00
|
|
|
|
|
|
|
label = gtk_menu_tracker_item_get_label (item);
|
2014-04-28 18:18:39 +00:00
|
|
|
hint = gtk_menu_tracker_item_get_display_hint (item);
|
2016-10-12 20:56:57 +00:00
|
|
|
text_direction = gtk_menu_tracker_item_get_text_direction (item);
|
2014-04-28 18:18:39 +00:00
|
|
|
|
|
|
|
if (hint && g_str_equal (hint, "horizontal-buttons"))
|
|
|
|
{
|
2017-04-21 16:59:59 +00:00
|
|
|
gtk_box_set_homogeneous (box->item_box, TRUE);
|
2014-04-28 18:18:39 +00:00
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (box->item_box), GTK_ORIENTATION_HORIZONTAL);
|
2020-02-06 16:32:26 +00:00
|
|
|
gtk_widget_add_css_class (GTK_WIDGET (box->item_box), "linked");
|
|
|
|
gtk_widget_add_css_class (GTK_WIDGET (box->item_box), "horizontal-buttons");
|
2014-04-28 18:18:39 +00:00
|
|
|
box->iconic = TRUE;
|
2016-10-12 20:56:57 +00:00
|
|
|
|
|
|
|
if (text_direction)
|
|
|
|
{
|
|
|
|
GtkTextDirection dir = GTK_TEXT_DIR_NONE;
|
|
|
|
|
|
|
|
if (g_str_equal (text_direction, "rtl"))
|
|
|
|
dir = GTK_TEXT_DIR_RTL;
|
|
|
|
else if (g_str_equal (text_direction, "ltr"))
|
|
|
|
dir = GTK_TEXT_DIR_LTR;
|
|
|
|
|
|
|
|
gtk_widget_set_direction (GTK_WIDGET (box->item_box), dir);
|
|
|
|
}
|
2014-04-28 18:18:39 +00:00
|
|
|
}
|
2019-06-06 01:36:17 +00:00
|
|
|
else if (hint && g_str_equal (hint, "inline-buttons"))
|
|
|
|
{
|
|
|
|
GtkWidget *item_box;
|
|
|
|
GtkWidget *spacer;
|
|
|
|
|
|
|
|
box->inline_buttons = TRUE;
|
|
|
|
|
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (box->item_box), GTK_ORIENTATION_HORIZONTAL);
|
2020-02-06 16:32:26 +00:00
|
|
|
gtk_widget_add_css_class (GTK_WIDGET (box->item_box), "inline-buttons");
|
2019-06-06 01:36:17 +00:00
|
|
|
|
2020-01-28 14:03:42 +00:00
|
|
|
spacer = gtk_builtin_icon_new ("none");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box->item_box), spacer);
|
2019-06-06 01:36:17 +00:00
|
|
|
gtk_size_group_add_widget (box->indicators, spacer);
|
|
|
|
|
|
|
|
if (label != NULL)
|
|
|
|
{
|
|
|
|
GtkWidget *title;
|
|
|
|
|
|
|
|
title = gtk_label_new (label);
|
|
|
|
gtk_widget_set_hexpand (title, TRUE);
|
|
|
|
gtk_widget_set_halign (title, GTK_ALIGN_START);
|
|
|
|
g_object_bind_property (item, "label", title, "label", G_BINDING_SYNC_CREATE);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box->item_box), title);
|
2019-06-06 01:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box->item_box), item_box);
|
2019-06-06 01:36:17 +00:00
|
|
|
box->item_box = GTK_BOX (item_box);
|
|
|
|
}
|
|
|
|
else if (hint && g_str_equal (hint, "circular-buttons"))
|
|
|
|
{
|
|
|
|
gtk_box_set_homogeneous (box->item_box, TRUE);
|
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (box->item_box), GTK_ORIENTATION_HORIZONTAL);
|
2020-02-06 16:32:26 +00:00
|
|
|
gtk_widget_add_css_class (GTK_WIDGET (box->item_box), "circular-buttons");
|
2019-06-06 01:36:17 +00:00
|
|
|
box->circular = TRUE;
|
|
|
|
}
|
2014-04-28 15:55:52 +00:00
|
|
|
|
2019-06-06 01:36:17 +00:00
|
|
|
if (label != NULL && !box->inline_buttons)
|
2014-04-28 15:55:52 +00:00
|
|
|
{
|
2016-06-01 15:52:20 +00:00
|
|
|
GtkWidget *separator;
|
2014-04-28 15:55:52 +00:00
|
|
|
GtkWidget *title;
|
|
|
|
|
2020-04-21 18:48:39 +00:00
|
|
|
box->separator = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2016-06-01 15:52:20 +00:00
|
|
|
g_object_ref_sink (box->separator);
|
|
|
|
|
|
|
|
separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
|
|
|
|
gtk_widget_set_valign (separator, GTK_ALIGN_CENTER);
|
2020-04-21 18:48:39 +00:00
|
|
|
gtk_widget_set_hexpand (separator, TRUE);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box->separator), separator);
|
2016-06-01 15:52:20 +00:00
|
|
|
|
2014-04-28 15:55:52 +00:00
|
|
|
title = gtk_label_new (label);
|
|
|
|
g_object_bind_property (item, "label", title, "label", G_BINDING_SYNC_CREATE);
|
2020-08-13 23:49:02 +00:00
|
|
|
gtk_widget_add_css_class (title, "separator");
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_widget_set_halign (title, GTK_ALIGN_START);
|
2020-04-21 18:48:39 +00:00
|
|
|
gtk_label_set_xalign (GTK_LABEL (title), 0.0);
|
2020-08-13 23:49:02 +00:00
|
|
|
gtk_widget_add_css_class (title, "title");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box->separator), title);
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-01 15:52:20 +00:00
|
|
|
box->separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
|
2014-10-18 09:19:26 +00:00
|
|
|
g_object_ref_sink (box->separator);
|
2014-04-28 15:55:52 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 17:14:23 +00:00
|
|
|
box->tracker = gtk_menu_tracker_new_for_item_link (item, G_MENU_LINK_SECTION, FALSE, FALSE,
|
2014-04-28 15:55:52 +00:00
|
|
|
gtk_menu_section_box_insert_func,
|
|
|
|
gtk_menu_section_box_remove_func,
|
|
|
|
box);
|
|
|
|
|
|
|
|
return GTK_WIDGET (box);
|
|
|
|
}
|
2020-10-20 18:26:45 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_menu_section_box_add_custom (GtkPopoverMenu *popover,
|
|
|
|
GtkWidget *child,
|
|
|
|
const char *id)
|
|
|
|
{
|
|
|
|
GtkWidget *stack;
|
|
|
|
GtkMenuSectionBox *box;
|
|
|
|
GtkWidget *slot;
|
|
|
|
|
|
|
|
stack = gtk_popover_get_child (GTK_POPOVER (popover));
|
|
|
|
box = GTK_MENU_SECTION_BOX (gtk_stack_get_child_by_name (GTK_STACK (stack), "main"));
|
|
|
|
|
|
|
|
slot = (GtkWidget *)g_hash_table_lookup (box->custom_slots, id);
|
|
|
|
|
|
|
|
if (slot == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (gtk_widget_get_first_child (slot))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_widget_insert_before (child, slot, NULL);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_menu_section_box_remove_custom (GtkPopoverMenu *popover,
|
|
|
|
GtkWidget *child)
|
|
|
|
{
|
|
|
|
GtkWidget *stack;
|
|
|
|
GtkMenuSectionBox *box;
|
|
|
|
GtkWidget *parent;
|
|
|
|
const char *id;
|
|
|
|
GtkWidget *slot;
|
|
|
|
|
|
|
|
stack = gtk_popover_get_child (GTK_POPOVER (popover));
|
|
|
|
box = GTK_MENU_SECTION_BOX (gtk_stack_get_child_by_name (GTK_STACK (stack), "main"));
|
|
|
|
parent = gtk_widget_get_parent (child);
|
|
|
|
|
|
|
|
id = (const char *) g_object_get_data (G_OBJECT (parent), "slot-id");
|
|
|
|
g_return_val_if_fail (id != NULL, FALSE);
|
|
|
|
|
|
|
|
slot = (GtkWidget *)g_hash_table_lookup (box->custom_slots, id);
|
|
|
|
|
|
|
|
if (slot != parent)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_widget_unparent (child);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|