environeditor: port to GTK 4

This required using an intermediate object, so will need further testing.
This commit is contained in:
Christian Hergert
2021-09-28 16:02:28 -07:00
parent ae61e9ceca
commit 8136484846
2 changed files with 38 additions and 24 deletions

View File

@ -30,13 +30,14 @@
struct _SysprofEnvironEditor struct _SysprofEnvironEditor
{ {
GtkListBox parent_instance; GtkWidget parent_instance;
GtkListBox *list_box;
SysprofEnviron *environ; SysprofEnviron *environ;
GtkWidget *dummy_row; GtkWidget *dummy_row;
SysprofEnvironVariable *dummy; SysprofEnvironVariable *dummy;
}; };
G_DEFINE_TYPE (SysprofEnvironEditor, sysprof_environ_editor, GTK_TYPE_LIST_BOX) G_DEFINE_TYPE (SysprofEnvironEditor, sysprof_environ_editor, GTK_TYPE_WIDGET)
enum { enum {
PROP_0, PROP_0,
@ -114,8 +115,7 @@ sysprof_environ_editor_disconnect (SysprofEnvironEditor *self)
g_assert (SYSPROF_IS_ENVIRON_EDITOR (self)); g_assert (SYSPROF_IS_ENVIRON_EDITOR (self));
g_assert (SYSPROF_IS_ENVIRON (self->environ)); g_assert (SYSPROF_IS_ENVIRON (self->environ));
gtk_list_box_bind_model (GTK_LIST_BOX (self), NULL, NULL, NULL, NULL); gtk_list_box_bind_model (self->list_box, NULL, NULL, NULL, NULL);
g_clear_object (&self->dummy); g_clear_object (&self->dummy);
} }
@ -125,12 +125,12 @@ sysprof_environ_editor_connect (SysprofEnvironEditor *self)
g_assert (SYSPROF_IS_ENVIRON_EDITOR (self)); g_assert (SYSPROF_IS_ENVIRON_EDITOR (self));
g_assert (SYSPROF_IS_ENVIRON (self->environ)); g_assert (SYSPROF_IS_ENVIRON (self->environ));
gtk_list_box_bind_model (GTK_LIST_BOX (self), gtk_list_box_bind_model (self->list_box,
G_LIST_MODEL (self->environ), G_LIST_MODEL (self->environ),
sysprof_environ_editor_create_row, self, NULL); sysprof_environ_editor_create_row, self, NULL);
self->dummy_row = sysprof_environ_editor_create_dummy_row (self); self->dummy_row = sysprof_environ_editor_create_dummy_row (self);
gtk_container_add (GTK_CONTAINER (self), self->dummy_row); gtk_list_box_append (self->list_box, self->dummy_row);
} }
static void static void
@ -168,17 +168,19 @@ find_row (SysprofEnvironEditor *self,
g_assert (SYSPROF_IS_ENVIRON_EDITOR (self)); g_assert (SYSPROF_IS_ENVIRON_EDITOR (self));
g_assert (SYSPROF_IS_ENVIRON_VARIABLE (variable)); g_assert (SYSPROF_IS_ENVIRON_VARIABLE (variable));
gtk_container_foreach (GTK_CONTAINER (self), find_row_cb, &lookup); for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (self->list_box));
child;
child = gtk_widget_get_next_sibling (child))
find_row_cb (child, &lookup);
return lookup.row; return lookup.row;
} }
static void static void
sysprof_environ_editor_row_activated (GtkListBox *list_box, sysprof_environ_editor_row_activated (SysprofEnvironEditor *self,
GtkListBoxRow *row) GtkListBoxRow *row,
GtkListBox *list_box)
{ {
SysprofEnvironEditor *self = (SysprofEnvironEditor *)list_box;
g_assert (GTK_IS_LIST_BOX (list_box)); g_assert (GTK_IS_LIST_BOX (list_box));
g_assert (GTK_IS_LIST_BOX_ROW (row)); g_assert (GTK_IS_LIST_BOX_ROW (row));
@ -196,13 +198,19 @@ sysprof_environ_editor_row_activated (GtkListBox *list_box,
} }
static void static void
sysprof_environ_editor_destroy (GtkWidget *widget) sysprof_environ_editor_dispose (GObject *object)
{ {
SysprofEnvironEditor *self = (SysprofEnvironEditor *)widget; SysprofEnvironEditor *self = (SysprofEnvironEditor *)object;
GTK_WIDGET_CLASS (sysprof_environ_editor_parent_class)->destroy (widget); if (self->list_box)
{
gtk_widget_unparent (GTK_WIDGET (self->list_box));
self->list_box = NULL;
}
g_clear_object (&self->environ); g_clear_object (&self->environ);
G_OBJECT_CLASS (sysprof_environ_editor_parent_class)->dispose (object);
} }
static void static void
@ -248,16 +256,12 @@ sysprof_environ_editor_class_init (SysprofEnvironEditorClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkListBoxClass *list_box_class = GTK_LIST_BOX_CLASS (klass);
SysprofThemeManager *theme_manager = sysprof_theme_manager_get_default (); SysprofThemeManager *theme_manager = sysprof_theme_manager_get_default ();
object_class->dispose = sysprof_environ_editor_dispose;
object_class->get_property = sysprof_environ_editor_get_property; object_class->get_property = sysprof_environ_editor_get_property;
object_class->set_property = sysprof_environ_editor_set_property; object_class->set_property = sysprof_environ_editor_set_property;
widget_class->destroy = sysprof_environ_editor_destroy;
list_box_class->row_activated = sysprof_environ_editor_row_activated;
properties [PROP_ENVIRON] = properties [PROP_ENVIRON] =
g_param_spec_object ("environ", g_param_spec_object ("environ",
"Environment", "Environment",
@ -267,16 +271,26 @@ sysprof_environ_editor_class_init (SysprofEnvironEditorClass *klass)
g_object_class_install_properties (object_class, N_PROPS, properties); g_object_class_install_properties (object_class, N_PROPS, properties);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
sysprof_theme_manager_register_resource (theme_manager, NULL, NULL, "/org/gnome/sysprof/css/SysprofEnvironEditor-shared.css"); sysprof_theme_manager_register_resource (theme_manager, NULL, NULL, "/org/gnome/sysprof/css/SysprofEnvironEditor-shared.css");
} }
static void static void
sysprof_environ_editor_init (SysprofEnvironEditor *self) sysprof_environ_editor_init (SysprofEnvironEditor *self)
{ {
gtk_list_box_set_selection_mode (GTK_LIST_BOX (self), GTK_SELECTION_NONE); self->list_box = GTK_LIST_BOX (gtk_list_box_new ());
gtk_widget_set_parent (GTK_WIDGET (self->list_box), GTK_WIDGET (self));
gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (self)), gtk_list_box_set_selection_mode (self->list_box, GTK_SELECTION_NONE);
"environ-editor");
gtk_widget_add_css_class (GTK_WIDGET (self), "environ-editor");
g_signal_connect_object (self->list_box,
"row-activated",
G_CALLBACK (sysprof_environ_editor_row_activated),
self,
G_CONNECT_SWAPPED);
} }
GtkWidget * GtkWidget *

View File

@ -28,11 +28,11 @@ G_BEGIN_DECLS
#define SYSPROF_TYPE_ENVIRON_EDITOR (sysprof_environ_editor_get_type()) #define SYSPROF_TYPE_ENVIRON_EDITOR (sysprof_environ_editor_get_type())
G_DECLARE_FINAL_TYPE (SysprofEnvironEditor, sysprof_environ_editor, SYSPROF, ENVIRON_EDITOR, GtkListBox) G_DECLARE_FINAL_TYPE (SysprofEnvironEditor, sysprof_environ_editor, SYSPROF, ENVIRON_EDITOR, GtkWidget)
GtkWidget *sysprof_environ_editor_new (void); GtkWidget *sysprof_environ_editor_new (void);
SysprofEnviron *sysprof_environ_editor_get_environ (SysprofEnvironEditor *self); SysprofEnviron *sysprof_environ_editor_get_environ (SysprofEnvironEditor *self);
void sysprof_environ_editor_set_environ (SysprofEnvironEditor *self, void sysprof_environ_editor_set_environ (SysprofEnvironEditor *self,
SysprofEnviron *environ); SysprofEnviron *environ);
G_END_DECLS G_END_DECLS