[gnome-builder/wip/gtk4-port: 1451/1774] libide/gtk: add IdeEnumObject




commit 6f18068863f3cf452677629e71a3afafe0c2f02d
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jun 9 16:45:43 2022 -0700

    libide/gtk: add IdeEnumObject
    
    This is meant to be used somewhat like GtkStringObject so that we can have
    list models with objects we can bind to, and then translate from nicks to
    the various GEnumValue.

 src/libide/gtk/ide-enum-object.c | 182 +++++++++++++++++++++++++++++++++++++++
 src/libide/gtk/ide-enum-object.h |  43 +++++++++
 src/libide/gtk/ide-gtk-init.c    |   2 +
 src/libide/gtk/libide-gtk.h      |   1 +
 src/libide/gtk/meson.build       |   2 +
 5 files changed, 230 insertions(+)
---
diff --git a/src/libide/gtk/ide-enum-object.c b/src/libide/gtk/ide-enum-object.c
new file mode 100644
index 000000000..f40c9e7ea
--- /dev/null
+++ b/src/libide/gtk/ide-enum-object.c
@@ -0,0 +1,182 @@
+/* ide-enum-object.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-enum-object"
+
+#include "config.h"
+
+#include "ide-enum-object.h"
+
+struct _IdeEnumObject
+{
+  GObject parent_instance;
+  char *description;
+  char *nick;
+  char *title;
+};
+
+enum {
+  PROP_0,
+  PROP_DESCRIPTION,
+  PROP_NICK,
+  PROP_TITLE,
+  N_PROPS
+};
+
+G_DEFINE_FINAL_TYPE (IdeEnumObject, ide_enum_object, G_TYPE_OBJECT)
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_enum_object_dispose (GObject *object)
+{
+  IdeEnumObject *self = (IdeEnumObject *)object;
+
+  g_clear_pointer (&self->description, g_free);
+  g_clear_pointer (&self->nick, g_free);
+  g_clear_pointer (&self->title, g_free);
+
+  G_OBJECT_CLASS (ide_enum_object_parent_class)->dispose (object);
+}
+
+static void
+ide_enum_object_get_property (GObject    *object,
+                              guint       prop_id,
+                              GValue     *value,
+                              GParamSpec *pspec)
+{
+  IdeEnumObject *self = IDE_ENUM_OBJECT (object);
+
+  switch (prop_id)
+    {
+    case PROP_DESCRIPTION:
+      g_value_set_string (value, self->description);
+      break;
+
+    case PROP_NICK:
+      g_value_set_string (value, self->nick);
+      break;
+
+    case PROP_TITLE:
+      g_value_set_string (value, self->title);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_enum_object_set_property (GObject      *object,
+                              guint         prop_id,
+                              const GValue *value,
+                              GParamSpec   *pspec)
+{
+  IdeEnumObject *self = IDE_ENUM_OBJECT (object);
+
+  switch (prop_id)
+    {
+    case PROP_DESCRIPTION:
+      self->description = g_value_dup_string (value);
+      break;
+
+    case PROP_NICK:
+      self->nick = g_value_dup_string (value);
+      break;
+
+    case PROP_TITLE:
+      self->title = g_value_dup_string (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_enum_object_class_init (IdeEnumObjectClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = ide_enum_object_dispose;
+  object_class->get_property = ide_enum_object_get_property;
+  object_class->set_property = ide_enum_object_set_property;
+
+  properties [PROP_DESCRIPTION] =
+    g_param_spec_string ("description", NULL, NULL, NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_NICK] =
+    g_param_spec_string ("nick", NULL, NULL, NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_TITLE] =
+    g_param_spec_string ("title", NULL, NULL, NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_enum_object_init (IdeEnumObject *self)
+{
+}
+
+const char *
+ide_enum_object_get_description (IdeEnumObject *self)
+{
+  g_return_val_if_fail (IDE_IS_ENUM_OBJECT (self), NULL);
+
+  return self->description;
+}
+
+const char *
+ide_enum_object_get_title (IdeEnumObject *self)
+{
+  g_return_val_if_fail (IDE_IS_ENUM_OBJECT (self), NULL);
+
+  return self->title;
+}
+
+const char *
+ide_enum_object_get_nick (IdeEnumObject *self)
+{
+  g_return_val_if_fail (IDE_IS_ENUM_OBJECT (self), NULL);
+
+  return self->nick;
+}
+
+IdeEnumObject *
+ide_enum_object_new (const char *nick,
+                     const char *title,
+                     const char *description)
+{
+  return g_object_new (IDE_TYPE_ENUM_OBJECT,
+                       "nick", nick,
+                       "title", title,
+                       "description", description,
+                       NULL);
+}
diff --git a/src/libide/gtk/ide-enum-object.h b/src/libide/gtk/ide-enum-object.h
new file mode 100644
index 000000000..1c98c6e59
--- /dev/null
+++ b/src/libide/gtk/ide-enum-object.h
@@ -0,0 +1,43 @@
+/* ide-enum-object.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libide-core.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_ENUM_OBJECT (ide_enum_object_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (IdeEnumObject, ide_enum_object, IDE, ENUM_OBJECT, GObject)
+
+IDE_AVAILABLE_IN_ALL
+IdeEnumObject *ide_enum_object_new             (const char    *nick,
+                                                const char    *title,
+                                                const char    *description);
+IDE_AVAILABLE_IN_ALL
+const char    *ide_enum_object_get_description (IdeEnumObject *self);
+IDE_AVAILABLE_IN_ALL
+const char    *ide_enum_object_get_nick        (IdeEnumObject *self);
+IDE_AVAILABLE_IN_ALL
+const char    *ide_enum_object_get_title       (IdeEnumObject *self);
+
+G_END_DECLS
diff --git a/src/libide/gtk/ide-gtk-init.c b/src/libide/gtk/ide-gtk-init.c
index 5821a299b..950bbf4ca 100644
--- a/src/libide/gtk/ide-gtk-init.c
+++ b/src/libide/gtk/ide-gtk-init.c
@@ -29,6 +29,7 @@
 #include "ide-file-chooser-entry.h"
 #include "ide-gtk-private.h"
 #include "ide-cell-renderer-fancy.h"
+#include "ide-enum-object.h"
 #include "ide-fancy-tree-view.h"
 #include "ide-progress-icon.h"
 #include "ide-radio-box.h"
@@ -41,6 +42,7 @@ _ide_gtk_init (void)
 {
   g_type_ensure (IDE_TYPE_ANIMATION);
   g_type_ensure (IDE_TYPE_CELL_RENDERER_FANCY);
+  g_type_ensure (IDE_TYPE_ENUM_OBJECT);
   g_type_ensure (IDE_TYPE_ENTRY_POPOVER);
   g_type_ensure (IDE_TYPE_FANCY_TREE_VIEW);
   g_type_ensure (IDE_TYPE_FILE_CHOOSER_ENTRY);
diff --git a/src/libide/gtk/libide-gtk.h b/src/libide/gtk/libide-gtk.h
index 0ee86097c..a1b13df38 100644
--- a/src/libide/gtk/libide-gtk.h
+++ b/src/libide/gtk/libide-gtk.h
@@ -24,6 +24,7 @@
 # include "ide-animation.h"
 # include "ide-cell-renderer-fancy.h"
 # include "ide-entry-popover.h"
+# include "ide-enum-object.h"
 # include "ide-fancy-tree-view.h"
 # include "ide-file-chooser-entry.h"
 # include "ide-file-manager.h"
diff --git a/src/libide/gtk/meson.build b/src/libide/gtk/meson.build
index 2e2998bd4..67b72df22 100644
--- a/src/libide/gtk/meson.build
+++ b/src/libide/gtk/meson.build
@@ -10,6 +10,7 @@ libide_gtk_public_headers = [
   'ide-animation.h',
   'ide-cell-renderer-fancy.h',
   'ide-entry-popover.h',
+  'ide-enum-object.h',
   'ide-fancy-tree-view.h',
   'ide-file-chooser-entry.h',
   'ide-file-manager.h',
@@ -40,6 +41,7 @@ libide_gtk_public_sources = [
   'ide-animation.c',
   'ide-cell-renderer-fancy.c',
   'ide-entry-popover.c',
+  'ide-enum-object.c',
   'ide-fancy-tree-view.c',
   'ide-file-chooser-entry.c',
   'ide-file-manager.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]