2014-06-05 12:31:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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 License, 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <glib/gi18n-lib.h>
|
2014-07-12 03:14:04 +00:00
|
|
|
|
2014-06-05 12:31:06 +00:00
|
|
|
#include "menu.h"
|
2014-07-12 03:14:04 +00:00
|
|
|
|
|
|
|
#include "gtktreestore.h"
|
2014-06-05 12:31:06 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2014-11-23 02:37:39 +00:00
|
|
|
#include "gtklabel.h"
|
2019-04-02 23:24:57 +00:00
|
|
|
#include "gtkstack.h"
|
2014-11-23 02:37:39 +00:00
|
|
|
|
2014-06-05 12:31:06 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
COLUMN_TYPE,
|
|
|
|
COLUMN_LABEL,
|
|
|
|
COLUMN_ACTION,
|
|
|
|
COLUMN_TARGET,
|
|
|
|
COLUMN_ICON
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkInspectorMenuPrivate
|
|
|
|
{
|
|
|
|
GtkTreeStore *model;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorMenu, gtk_inspector_menu, GTK_TYPE_BOX)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_inspector_menu_init (GtkInspectorMenu *sl)
|
|
|
|
{
|
|
|
|
sl->priv = gtk_inspector_menu_get_instance_private (sl);
|
|
|
|
gtk_widget_init_template (GTK_WIDGET (sl));
|
|
|
|
}
|
|
|
|
|
2014-06-05 13:29:18 +00:00
|
|
|
static void add_menu (GtkInspectorMenu *sl,
|
2019-04-02 23:24:57 +00:00
|
|
|
GtkStackPage *page,
|
2014-06-05 13:29:18 +00:00
|
|
|
GMenuModel *menu,
|
|
|
|
GtkTreeIter *parent);
|
|
|
|
|
2014-06-05 12:31:06 +00:00
|
|
|
static void
|
|
|
|
add_item (GtkInspectorMenu *sl,
|
2019-04-02 23:24:57 +00:00
|
|
|
GtkStackPage *page,
|
2014-06-05 12:31:06 +00:00
|
|
|
GMenuModel *menu,
|
2020-07-24 13:54:49 +00:00
|
|
|
int idx,
|
2014-06-05 12:31:06 +00:00
|
|
|
GtkTreeIter *parent)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GVariant *value;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *label = NULL;
|
|
|
|
char *action = NULL;
|
|
|
|
char *target = NULL;
|
|
|
|
char *icon = NULL;
|
2014-06-05 13:29:18 +00:00
|
|
|
GMenuModel *model;
|
2014-06-05 12:31:06 +00:00
|
|
|
|
|
|
|
g_menu_model_get_item_attribute (menu, idx, G_MENU_ATTRIBUTE_LABEL, "s", &label);
|
|
|
|
g_menu_model_get_item_attribute (menu, idx, G_MENU_ATTRIBUTE_ACTION, "s", &action);
|
|
|
|
value = g_menu_model_get_item_attribute_value (menu, idx, G_MENU_ATTRIBUTE_TARGET, NULL);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
target = g_variant_print (value, FALSE);
|
|
|
|
g_variant_unref (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_tree_store_append (sl->priv->model, &iter, parent);
|
|
|
|
gtk_tree_store_set (sl->priv->model, &iter,
|
|
|
|
COLUMN_TYPE, "item",
|
|
|
|
COLUMN_LABEL, label,
|
|
|
|
COLUMN_ACTION, action,
|
|
|
|
COLUMN_TARGET, target,
|
|
|
|
COLUMN_ICON, icon,
|
|
|
|
-1);
|
|
|
|
|
2014-06-05 13:29:18 +00:00
|
|
|
model = g_menu_model_get_item_link (menu, idx, G_MENU_LINK_SECTION);
|
|
|
|
if (model)
|
|
|
|
{
|
|
|
|
if (label == NULL)
|
|
|
|
gtk_tree_store_set (sl->priv->model, &iter,
|
|
|
|
COLUMN_LABEL, _("Unnamed section"),
|
|
|
|
-1);
|
2019-04-02 23:24:57 +00:00
|
|
|
add_menu (sl, page, model, &iter);
|
2014-06-05 13:29:18 +00:00
|
|
|
g_object_unref (model);
|
|
|
|
}
|
|
|
|
|
|
|
|
model = g_menu_model_get_item_link (menu, idx, G_MENU_LINK_SUBMENU);
|
|
|
|
if (model)
|
|
|
|
{
|
2019-04-02 23:24:57 +00:00
|
|
|
add_menu (sl, page, model, &iter);
|
2014-06-05 13:29:18 +00:00
|
|
|
g_object_unref (model);
|
|
|
|
}
|
|
|
|
|
2014-06-05 12:31:06 +00:00
|
|
|
g_free (label);
|
|
|
|
g_free (action);
|
|
|
|
g_free (target);
|
|
|
|
g_free (icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_menu (GtkInspectorMenu *sl,
|
2019-04-02 23:24:57 +00:00
|
|
|
GtkStackPage *page,
|
2014-06-05 12:31:06 +00:00
|
|
|
GMenuModel *menu,
|
|
|
|
GtkTreeIter *parent)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_items;
|
|
|
|
int i;
|
2014-06-05 12:31:06 +00:00
|
|
|
|
2019-04-02 23:24:57 +00:00
|
|
|
g_object_set (page, "visible", TRUE, NULL);
|
2014-06-05 12:31:06 +00:00
|
|
|
|
|
|
|
n_items = g_menu_model_get_n_items (menu);
|
|
|
|
for (i = 0; i < n_items; i++)
|
2019-04-02 23:24:57 +00:00
|
|
|
add_item (sl, page, menu, i, parent);
|
2014-06-05 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_inspector_menu_set_object (GtkInspectorMenu *sl,
|
|
|
|
GObject *object)
|
|
|
|
{
|
2019-04-02 23:24:57 +00:00
|
|
|
GtkWidget *stack;
|
|
|
|
GtkStackPage *page;
|
|
|
|
|
|
|
|
stack = gtk_widget_get_parent (GTK_WIDGET (sl));
|
|
|
|
page = gtk_stack_get_page (GTK_STACK (stack), GTK_WIDGET (sl));
|
|
|
|
|
|
|
|
g_object_set (page, "visible", FALSE, NULL);
|
2014-06-05 12:31:06 +00:00
|
|
|
gtk_tree_store_clear (sl->priv->model);
|
|
|
|
|
|
|
|
if (G_IS_MENU_MODEL (object))
|
2019-04-02 23:24:57 +00:00
|
|
|
add_menu (sl, page, G_MENU_MODEL (object), NULL);
|
2014-06-05 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_inspector_menu_class_init (GtkInspectorMenuClass *klass)
|
|
|
|
{
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
2014-11-30 20:59:53 +00:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/inspector/menu.ui");
|
2014-06-05 12:31:06 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorMenu, model);
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: set et sw=2 ts=2:
|