2014-05-10 04:50:21 +00:00
|
|
|
/*
|
2020-06-30 19:17:14 +00:00
|
|
|
* Copyright (c) 2014, 2020 Red Hat, Inc.
|
2014-05-10 04:50:21 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2020-06-30 19:17:14 +00:00
|
|
|
#include "tree-data.h"
|
2014-05-10 04:50:21 +00:00
|
|
|
|
2018-08-29 14:19:37 +00:00
|
|
|
#include "object-tree.h"
|
|
|
|
|
2014-07-12 03:14:04 +00:00
|
|
|
#include "gtktreeview.h"
|
|
|
|
#include "gtkcellrenderertext.h"
|
2014-11-23 01:13:26 +00:00
|
|
|
#include "gtktogglebutton.h"
|
|
|
|
#include "gtklabel.h"
|
2019-04-02 23:24:57 +00:00
|
|
|
#include "gtkstack.h"
|
2020-06-30 19:17:14 +00:00
|
|
|
#include "gtkboxlayout.h"
|
|
|
|
#include "gtkorientable.h"
|
2014-11-23 01:13:26 +00:00
|
|
|
|
2014-07-12 03:14:04 +00:00
|
|
|
|
2020-06-30 19:17:14 +00:00
|
|
|
struct _GtkInspectorTreeData
|
2014-05-10 04:50:21 +00:00
|
|
|
{
|
2020-06-30 19:17:14 +00:00
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
2014-05-16 04:27:28 +00:00
|
|
|
GtkTreeModel *object;
|
|
|
|
GtkTreeModel *types;
|
|
|
|
GtkTreeView *view;
|
2014-11-23 01:13:26 +00:00
|
|
|
GtkWidget *object_title;
|
2014-05-16 04:27:28 +00:00
|
|
|
gboolean show_data;
|
2014-05-10 04:50:21 +00:00
|
|
|
};
|
|
|
|
|
2020-06-30 19:17:14 +00:00
|
|
|
typedef struct _GtkInspectorTreeDataClass GtkInspectorTreeDataClass;
|
|
|
|
struct _GtkInspectorTreeDataClass
|
|
|
|
{
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkInspectorTreeData, gtk_inspector_tree_data, GTK_TYPE_WIDGET)
|
2014-05-10 04:50:21 +00:00
|
|
|
|
|
|
|
static void
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_inspector_tree_data_init (GtkInspectorTreeData *sl)
|
2014-05-10 04:50:21 +00:00
|
|
|
{
|
2014-05-16 04:27:28 +00:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (sl));
|
2020-06-30 19:17:14 +00:00
|
|
|
|
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (gtk_widget_get_layout_manager (GTK_WIDGET (sl))),
|
|
|
|
GTK_ORIENTATION_VERTICAL);
|
2014-05-16 04:27:28 +00:00
|
|
|
}
|
2014-05-10 04:50:21 +00:00
|
|
|
|
2014-05-16 04:27:28 +00:00
|
|
|
static void
|
|
|
|
cell_data_func (GtkTreeViewColumn *col,
|
|
|
|
GtkCellRenderer *cell,
|
|
|
|
GtkTreeModel *model,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int num;
|
2014-05-16 04:27:28 +00:00
|
|
|
GValue gvalue = { 0, };
|
2020-07-24 18:40:36 +00:00
|
|
|
char *value;
|
2014-05-16 04:27:28 +00:00
|
|
|
|
|
|
|
num = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (col), "num"));
|
|
|
|
gtk_tree_model_get_value (model, iter, num, &gvalue);
|
|
|
|
value = g_strdup_value_contents (&gvalue);
|
|
|
|
g_object_set (cell, "text", value ? value : "", NULL);
|
|
|
|
g_free (value);
|
|
|
|
g_value_unset (&gvalue);
|
2014-05-10 04:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-06-30 19:17:14 +00:00
|
|
|
add_columns (GtkInspectorTreeData *sl)
|
2014-05-10 04:50:21 +00:00
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_columns;
|
2014-05-16 04:27:28 +00:00
|
|
|
GtkCellRenderer *cell;
|
|
|
|
GType type;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *title;
|
2014-05-16 04:27:28 +00:00
|
|
|
GtkTreeViewColumn *col;
|
2020-07-24 13:54:49 +00:00
|
|
|
int i;
|
2014-05-16 04:27:28 +00:00
|
|
|
|
2020-06-30 19:17:14 +00:00
|
|
|
n_columns = gtk_tree_model_get_n_columns (sl->object);
|
2014-05-16 04:27:28 +00:00
|
|
|
for (i = 0; i < n_columns; i++)
|
|
|
|
{
|
|
|
|
cell = gtk_cell_renderer_text_new ();
|
2020-06-30 19:17:14 +00:00
|
|
|
type = gtk_tree_model_get_column_type (sl->object, i);
|
2014-05-16 04:27:28 +00:00
|
|
|
title = g_strdup_printf ("%d: %s", i, g_type_name (type));
|
|
|
|
col = gtk_tree_view_column_new_with_attributes (title, cell, NULL);
|
|
|
|
g_object_set_data (G_OBJECT (col), "num", GINT_TO_POINTER (i));
|
|
|
|
gtk_tree_view_column_set_cell_data_func (col, cell, cell_data_func, sl, NULL);
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_tree_view_append_column (sl->view, col);
|
2014-05-16 04:27:28 +00:00
|
|
|
g_free (title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-06-30 19:17:14 +00:00
|
|
|
show_types (GtkInspectorTreeData *sl)
|
2014-05-16 04:27:28 +00:00
|
|
|
{
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_tree_view_set_model (sl->view, NULL);
|
|
|
|
sl->show_data = FALSE;
|
2014-05-16 04:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-06-30 19:17:14 +00:00
|
|
|
show_data (GtkInspectorTreeData *sl)
|
2014-05-16 04:27:28 +00:00
|
|
|
{
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_tree_view_set_model (sl->view, sl->object);
|
|
|
|
sl->show_data = TRUE;
|
2014-05-16 04:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-06-30 19:17:14 +00:00
|
|
|
clear_view (GtkInspectorTreeData *sl)
|
2014-05-16 04:27:28 +00:00
|
|
|
{
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_tree_view_set_model (sl->view, NULL);
|
|
|
|
while (gtk_tree_view_get_n_columns (sl->view) > 0)
|
|
|
|
gtk_tree_view_remove_column (sl->view,
|
|
|
|
gtk_tree_view_get_column (sl->view, 0));
|
2014-05-10 04:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_inspector_tree_data_set_object (GtkInspectorTreeData *sl,
|
2014-05-10 04:50:21 +00:00
|
|
|
GObject *object)
|
|
|
|
{
|
2019-04-02 23:24:57 +00:00
|
|
|
GtkWidget *stack;
|
|
|
|
GtkStackPage *page;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *title;
|
2014-11-23 01:13:26 +00:00
|
|
|
|
2019-04-02 23:24:57 +00:00
|
|
|
stack = gtk_widget_get_parent (GTK_WIDGET (sl));
|
|
|
|
page = gtk_stack_get_page (GTK_STACK (stack), GTK_WIDGET (sl));
|
|
|
|
|
2014-05-16 04:27:28 +00:00
|
|
|
clear_view (sl);
|
2020-06-30 19:17:14 +00:00
|
|
|
sl->object = NULL;
|
|
|
|
sl->show_data = FALSE;
|
2014-05-10 04:50:21 +00:00
|
|
|
|
|
|
|
if (!GTK_IS_TREE_MODEL (object))
|
|
|
|
{
|
2019-04-02 23:24:57 +00:00
|
|
|
g_object_set (page, "visible", FALSE, NULL);
|
2014-05-10 04:50:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-29 14:19:37 +00:00
|
|
|
title = gtk_inspector_get_object_title (object);
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_label_set_label (GTK_LABEL (sl->object_title), title);
|
2018-08-29 14:19:37 +00:00
|
|
|
g_free (title);
|
2014-11-23 01:13:26 +00:00
|
|
|
|
2019-04-02 23:24:57 +00:00
|
|
|
g_object_set (page, "visible", TRUE, NULL);
|
2014-05-10 04:50:21 +00:00
|
|
|
|
2020-06-30 19:17:14 +00:00
|
|
|
sl->object = GTK_TREE_MODEL (object);
|
2014-05-16 04:27:28 +00:00
|
|
|
add_columns (sl);
|
|
|
|
show_types (sl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-11-23 01:13:26 +00:00
|
|
|
toggle_show (GtkToggleButton *button,
|
2020-06-30 19:17:14 +00:00
|
|
|
GtkInspectorTreeData *sl)
|
2014-05-16 04:27:28 +00:00
|
|
|
{
|
2020-06-30 19:17:14 +00:00
|
|
|
if (gtk_toggle_button_get_active (button) == sl->show_data)
|
2014-05-16 04:27:28 +00:00
|
|
|
return;
|
|
|
|
|
2014-11-23 01:13:26 +00:00
|
|
|
if (gtk_toggle_button_get_active (button))
|
2014-05-16 04:27:28 +00:00
|
|
|
show_data (sl);
|
|
|
|
else
|
|
|
|
show_types (sl);
|
2014-05-10 04:50:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 02:54:19 +00:00
|
|
|
static void
|
|
|
|
dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkInspectorTreeData *sl = GTK_INSPECTOR_TREE_DATA (object);
|
|
|
|
GtkWidget *child;
|
|
|
|
|
|
|
|
while ((child = gtk_widget_get_first_child (GTK_WIDGET (sl))))
|
|
|
|
gtk_widget_unparent (child);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_inspector_tree_data_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2014-05-10 04:50:21 +00:00
|
|
|
static void
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_inspector_tree_data_class_init (GtkInspectorTreeDataClass *klass)
|
2014-05-10 04:50:21 +00:00
|
|
|
{
|
2020-07-18 02:54:19 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2014-05-10 04:50:21 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
2020-07-18 02:54:19 +00:00
|
|
|
object_class->dispose = dispose;
|
|
|
|
|
2020-06-30 19:17:14 +00:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/inspector/tree-data.ui");
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkInspectorTreeData, view);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkInspectorTreeData, object_title);
|
2014-05-16 04:27:28 +00:00
|
|
|
gtk_widget_class_bind_template_callback (widget_class, toggle_show);
|
2020-06-30 19:17:14 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
|
2014-05-10 04:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim: set et sw=2 ts=2:
|