[gimp/wip/Jehan/classy-GIMP: 13/17] libgimp: s/gimp_display_new_by_id/gimp_display_get_by_id/



commit a777841dc4a6270962eab10ebea1da6b59c00869
Author: Jehan <jehan girinstud io>
Date:   Thu Aug 15 10:14:38 2019 +0200

    libgimp: s/gimp_display_new_by_id/gimp_display_get_by_id/
    
    GimpDisplay objects now also belongs to libgimp!

 libgimp/gimp.c            |  5 +++--
 libgimp/gimpdisplay.c     | 41 +++++++++++++++++++++++++++++++++--------
 libgimp/gimpdisplay.h     |  2 +-
 libgimp/gimpdisplay_pdb.c |  4 ++--
 libgimp/gimpui.c          | 14 +++-----------
 pdb/pdb.pl                |  4 ++--
 6 files changed, 44 insertions(+), 26 deletions(-)
---
diff --git a/libgimp/gimp.c b/libgimp/gimp.c
index ebe1ed0ca0..cd48491ef1 100644
--- a/libgimp/gimp.c
+++ b/libgimp/gimp.c
@@ -836,12 +836,13 @@ gimp_check_type (void)
  *
  * This is a constant value given at plug-in configuration time.
  *
- * Returns: (transfer full): the default display ID
+ * Returns: (transfer none): the default display ID
+ *          The object belongs to libgimp and you should not free it.
  **/
 GimpDisplay *
 gimp_default_display (void)
 {
-  return gimp_display_new_by_id (_gdisp_ID);
+  return gimp_display_get_by_id (_gdisp_ID);
 }
 
 /**
diff --git a/libgimp/gimpdisplay.c b/libgimp/gimpdisplay.c
index ff85fc99eb..419fd59074 100644
--- a/libgimp/gimpdisplay.c
+++ b/libgimp/gimpdisplay.c
@@ -37,6 +37,9 @@ struct _GimpDisplayPrivate
   gint id;
 };
 
+static GHashTable *gimp_displays = NULL;
+
+
 static void       gimp_display_set_property  (GObject      *object,
                                               guint         property_id,
                                               const GValue *value,
@@ -132,29 +135,51 @@ gimp_display_get_property (GObject    *object,
 gint32
 gimp_display_get_id (GimpDisplay *display)
 {
-  return display->priv->id;
+  return display ? display->priv->id : -1;
 }
 
 /**
- * gimp_display_new_by_id:
+ * gimp_display_get_by_id:
  * @display_id: The display id.
  *
  * Creates a #GimpDisplay representing @display_id.
  *
- * Returns: (nullable) (transfer full): a #GimpDisplay for @display_id or
+ * Returns: (nullable) (transfer none): a #GimpDisplay for @display_id or
  *          %NULL if @display_id does not represent a valid display.
+ *          The object belongs to libgimp and you should not free it.
  *
  * Since: 3.0
  **/
 GimpDisplay *
-gimp_display_new_by_id (gint32 display_id)
+gimp_display_get_by_id (gint32 display_id)
 {
   GimpDisplay *display = NULL;
 
-  if (_gimp_display_is_valid (display_id))
-    display = g_object_new (GIMP_TYPE_DISPLAY,
-                            "id", display_id,
-                            NULL);
+  if (G_UNLIKELY (! gimp_displays))
+    gimp_displays = g_hash_table_new_full (g_direct_hash,
+                                           g_direct_equal,
+                                           NULL,
+                                           (GDestroyNotify) g_object_unref);
+
+  if (! _gimp_display_is_valid (display_id))
+    {
+      g_hash_table_remove (gimp_displays, GINT_TO_POINTER (display_id));
+    }
+  else
+    {
+      display = g_hash_table_lookup (gimp_displays,
+                                     GINT_TO_POINTER (display_id));
+
+      if (! display)
+        {
+          display = g_object_new (GIMP_TYPE_DISPLAY,
+                                  "id", display_id,
+                                  NULL);
+          g_hash_table_insert (gimp_displays,
+                               GINT_TO_POINTER (display_id),
+                               display);
+        }
+    }
 
   return display;
 }
diff --git a/libgimp/gimpdisplay.h b/libgimp/gimpdisplay.h
index 043311478f..7abfb0b053 100644
--- a/libgimp/gimpdisplay.h
+++ b/libgimp/gimpdisplay.h
@@ -67,7 +67,7 @@ struct _GimpDisplayClass
 GType         gimp_display_get_type     (void) G_GNUC_CONST;
 
 gint32        gimp_display_get_id       (GimpDisplay    *display);
-GimpDisplay * gimp_display_new_by_id    (gint32          display_id);
+GimpDisplay * gimp_display_get_by_id    (gint32          display_id);
 
 
 G_END_DECLS
diff --git a/libgimp/gimpdisplay_pdb.c b/libgimp/gimpdisplay_pdb.c
index a06616c076..d4aea564fb 100644
--- a/libgimp/gimpdisplay_pdb.c
+++ b/libgimp/gimpdisplay_pdb.c
@@ -131,7 +131,7 @@ _gimp_display_is_valid (gint32 display_ID)
  * procedure only makes sense for use with the GIMP UI, and will result
  * in an execution error if called when GIMP has no UI.
  *
- * Returns: (transfer full): The new display.
+ * Returns: (transfer none): The new display.
  **/
 GimpDisplay *
 gimp_display_new (GimpImage *image)
@@ -155,7 +155,7 @@ gimp_display_new (GimpImage *image)
   gimp_value_array_unref (args);
 
   if (g_value_get_enum (gimp_value_array_index (return_vals, 0)) == GIMP_PDB_SUCCESS)
-    display = gimp_display_new_by_id (gimp_value_get_display_id (gimp_value_array_index (return_vals, 1)));
+    display = gimp_display_get_by_id (gimp_value_get_display_id (gimp_value_array_index (return_vals, 1)));
 
   gimp_value_array_unref (return_vals);
 
diff --git a/libgimp/gimpui.c b/libgimp/gimpui.c
index b362a48e26..19c036eeeb 100644
--- a/libgimp/gimpui.c
+++ b/libgimp/gimpui.c
@@ -473,13 +473,7 @@ gimp_osx_focus_window (void)
 GdkWindow *
 gimp_ui_get_display_window_deprecated (guint32 gdisp_ID)
 {
-  GimpDisplay *display = gimp_display_new_by_id (gdisp_ID);
-  GdkWindow   *window;
-
-  window = gimp_ui_get_display_window (display);
-  g_object_unref (display);
-
-  return window;
+  return gimp_ui_get_display_window (gimp_display_get_by_id (gdisp_ID));
 }
 
 /**
@@ -500,8 +494,6 @@ void
 gimp_window_set_transient_for_display_deprecated (GtkWindow *window,
                                                   guint32    gdisp_ID)
 {
-  GimpDisplay *display = gimp_display_new_by_id (gdisp_ID);
-
-  gimp_window_set_transient_for_display (window, display);
-  g_object_unref (display);
+  gimp_window_set_transient_for_display (window,
+                                         gimp_display_get_by_id (gdisp_ID));
 }
diff --git a/pdb/pdb.pl b/pdb/pdb.pl
index 08af331f1c..b09df7df84 100644
--- a/pdb/pdb.pl
+++ b/pdb/pdb.pl
@@ -156,9 +156,9 @@ package Gimp::CodeGen::pdb;
                     app_const_type  => 'GimpObject *',
                     id              => 1,
                     init_value      => 'NULL',
-                    out_annotate    => '(transfer full)',
+                    out_annotate    => '(transfer none)',
                     get_value_func  => '$var = gimp_value_get_display ($value, gimp)',
-                    dup_value_func  => '$var = gimp_display_new_by_id (gimp_value_get_display_id ($value))',
+                    dup_value_func  => '$var = gimp_display_get_by_id (gimp_value_get_display_id ($value))',
                     dup_value_func_d=> '$var = gimp_value_get_display_id ($value)',
                     set_value_func  => 'gimp_value_set_display_id ($value, $var)',
                     take_value_func => 'gimp_value_set_display ($value, $var)' },


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